• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "libpandabase/macros.h"
17 #include "created_object_file.h"
18 
19 #include <llvm/Support/Debug.h>
20 #include <llvm/Support/FileSystem.h>
21 
22 #define DEBUG_TYPE "created-object-file"
23 
24 namespace panda::llvmbackend {
25 
CreatedObjectFile(std::unique_ptr<llvm::MemoryBuffer> objectFileBuffer,std::unique_ptr<llvm::object::ObjectFile> objectFile)26 CreatedObjectFile::CreatedObjectFile(std::unique_ptr<llvm::MemoryBuffer> objectFileBuffer,
27                                      std::unique_ptr<llvm::object::ObjectFile> objectFile)
28     : buffer_(std::move(objectFileBuffer)), objectFile_(std::move(objectFile))
29 {
30     if (objectFile_ != nullptr) {
31         // Generally, we need this index for Irtoc to collect used registers.
32         // In other cases, it seems a single scan is pretty enough.
33         for (auto section : objectFile_->sections()) {
34             sectionIndex_[cantFail(section.getName())] = section;
35         }
36     }
37 }
38 
CopyOf(llvm::MemoryBufferRef objectFileBuffer,const ObjectFilePostProcessor & objectFilePostProcessor)39 llvm::Expected<std::unique_ptr<CreatedObjectFile>> CreatedObjectFile::CopyOf(
40     llvm::MemoryBufferRef objectFileBuffer, const ObjectFilePostProcessor &objectFilePostProcessor)
41 {
42     auto copy = llvm::MemoryBuffer::getMemBufferCopy(objectFileBuffer.getBuffer());
43     auto elf = llvm::object::ObjectFile::createObjectFile(copy->getMemBufferRef());
44     if (!elf) {
45         return elf.takeError();
46     }
47     auto file = cantFail(std::move(elf));
48     objectFilePostProcessor(file.get());
49     return std::make_unique<CreatedObjectFile>(std::move(copy), std::move(file));
50 }
51 
GetObjectFile() const52 llvm::object::ObjectFile *CreatedObjectFile::GetObjectFile() const
53 {
54     return objectFile_.get();
55 }
56 
HasSection(const std::string & name) const57 bool CreatedObjectFile::HasSection(const std::string &name) const
58 {
59     return sectionIndex_.find(name) != sectionIndex_.end();
60 }
61 
ContentToVector()62 std::vector<uint8_t> SectionReference::ContentToVector()
63 {
64     std::vector<uint8_t> vector;
65     ASSERT(GetSize() != 0);
66     // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
67     std::copy(GetMemory(), GetMemory() + GetSize(), std::back_inserter(vector));
68     return vector;
69 }
70 
GetSection(const std::string & name) const71 SectionReference CreatedObjectFile::GetSection(const std::string &name) const
72 {
73     LLVM_DEBUG(llvm::dbgs() << "Getting section = '" << name << "'\n");
74     ASSERT(sectionIndex_.find(name) != sectionIndex_.end() && "Attempt to access an unknown section");
75     const auto &section = sectionIndex_.at(name);
76     auto contents = cantFail(section.getContents());
77     auto memory = reinterpret_cast<const uint8_t *>(contents.data());
78     return SectionReference {memory, contents.size(), name, section.getAlignment()};
79 }
80 
GetSectionByFunctionName(const std::string & fullFunctionName) const81 SectionReference CreatedObjectFile::GetSectionByFunctionName(const std::string &fullFunctionName) const
82 {
83     auto sectionReference = GetSection(".text." + fullFunctionName + ".");
84     LLVM_DEBUG(llvm::dbgs() << "Got section by function = " << fullFunctionName
85                             << " section's size = " << sectionReference.GetSize() << "\n");
86     return sectionReference;
87 }
88 
WriteTo(std::string_view output) const89 void CreatedObjectFile::WriteTo(std::string_view output) const
90 {
91     std::error_code errorCode;
92     llvm::raw_fd_ostream dest(output, errorCode, llvm::sys::fs::FA_Write);
93     static constexpr bool GEN_CRASH_DIAG = false;
94     if (errorCode) {
95         report_fatal_error(llvm::Twine("Unable to open file = '") + output + "' to dump object file, error_code = " +
96                                llvm::Twine(errorCode.value()) + ", message = " + errorCode.message(),
97                            GEN_CRASH_DIAG);
98     }
99     llvm::StringRef data;
100     if (objectFile_ != nullptr) {
101         data = objectFile_->getData();
102     }
103     LLVM_DEBUG(llvm::dbgs() << "Writing object file, size =  " << data.size() << " to " << output << "\n");
104     dest << data;
105     if (dest.has_error()) {
106         auto error = dest.error();
107         report_fatal_error(llvm::Twine("Unable to write object file to '") + output +
108                                ", error_code = " + llvm::Twine(error.value()) + ", message = " + error.message(),
109                            GEN_CRASH_DIAG);
110     }
111 }
Size() const112 size_t CreatedObjectFile::Size() const
113 {
114     ASSERT(objectFile_ != nullptr && "Attempt to get size of empty CreatedObjectFile");
115     return objectFile_->getData().size();
116 }
117 
118 }  // namespace panda::llvmbackend
119