• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2024 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 "created_object_file.h"
17 
18 #include "transforms/transform_utils.h"
19 
20 #include <llvm/Support/Debug.h>
21 #include <llvm/Support/FileSystem.h>
22 
23 #define DEBUG_TYPE "created-object-file"
24 
25 namespace ark::llvmbackend {
26 
CreatedObjectFile(std::unique_ptr<llvm::MemoryBuffer> objectFileBuffer,std::unique_ptr<llvm::object::ObjectFile> objectFile)27 CreatedObjectFile::CreatedObjectFile(std::unique_ptr<llvm::MemoryBuffer> objectFileBuffer,
28                                      std::unique_ptr<llvm::object::ObjectFile> objectFile)
29     : buffer_(std::move(objectFileBuffer)), objectFile_(std::move(objectFile))
30 {
31     if (objectFile_ != nullptr) {
32         // Generally, we need this index for Irtoc to collect used registers.
33         // In other cases, it seems a single scan is pretty enough.
34         for (auto section : objectFile_->sections()) {
35             sectionIndex_[cantFail(section.getName())] = section;
36         }
37     }
38 }
39 
CopyOf(llvm::MemoryBufferRef objectFileBuffer)40 llvm::Expected<std::unique_ptr<CreatedObjectFile>> CreatedObjectFile::CopyOf(llvm::MemoryBufferRef objectFileBuffer)
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     return std::make_unique<CreatedObjectFile>(std::move(copy), std::move(file));
49 }
50 
GetObjectFile() const51 llvm::object::ObjectFile *CreatedObjectFile::GetObjectFile() const
52 {
53     return objectFile_.get();
54 }
55 
HasSection(const std::string & name) const56 bool CreatedObjectFile::HasSection(const std::string &name) const
57 {
58     return sectionIndex_.find(name) != sectionIndex_.end();
59 }
60 
ContentToVector()61 std::vector<uint8_t> SectionReference::ContentToVector()
62 {
63     std::vector<uint8_t> vector;
64     ASSERT(GetSize() != 0);
65     // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
66     std::copy(GetMemory(), GetMemory() + GetSize(), std::back_inserter(vector));
67     return vector;
68 }
69 
GetSection(const std::string & name) const70 SectionReference CreatedObjectFile::GetSection(const std::string &name) const
71 {
72     LLVM_DEBUG(llvm::dbgs() << "Getting section = '" << name << "'\n");
73     ASSERT(sectionIndex_.find(name) != sectionIndex_.end() && "Attempt to access an unknown section");
74     const auto &section = sectionIndex_.at(name);
75     auto contents = cantFail(section.getContents());
76     auto memory = reinterpret_cast<const uint8_t *>(contents.data());
77     return SectionReference {memory, contents.size(), name, section.getAlignment()};
78 }
79 
GetSectionByFunctionName(const std::string & fullFunctionName) const80 SectionReference CreatedObjectFile::GetSectionByFunctionName(const std::string &fullFunctionName) const
81 {
82     auto sectionReference = GetSection(".text." + fullFunctionName + ".");
83     LLVM_DEBUG(llvm::dbgs() << "Got section by function = " << fullFunctionName
84                             << " section's size = " << sectionReference.GetSize() << "\n");
85     return sectionReference;
86 }
87 
GetStackMapInfo() const88 std::unordered_map<std::string, CreatedObjectFile::StackMapSymbol> CreatedObjectFile::GetStackMapInfo() const
89 {
90     std::unordered_map<std::string, StackMapSymbol> info;
91     ASSERT(sectionIndex_.find(RELA_LLVM_STACKMAPS_SECTION) != sectionIndex_.end() &&
92            "Attempt to access unknown section '.rela.llvm_stackmaps'");
93     const auto &section = sectionIndex_.at(RELA_LLVM_STACKMAPS_SECTION);
94     uint32_t counter = 0;
95     for (auto relocation : section.relocations()) {
96         const auto &symbol = relocation.getSymbol();
97         info.insert({cantFail(symbol->getName()).str(), {counter++, cantFail(symbol->getValue())}});
98     }
99     return info;
100 }
101 
GetFaultMapInfo() const102 std::unordered_map<std::string, uint32_t> CreatedObjectFile::GetFaultMapInfo() const
103 {
104     std::unordered_map<std::string, uint32_t> info;
105     const auto &section = sectionIndex_.at(".rela.llvm_faultmaps");
106     uint32_t counter = 0;
107     for (auto relocation : section.relocations()) {
108         const auto &symbol = relocation.getSymbol();
109         info.insert({cantFail(symbol->getName()).str(), counter++});
110     }
111     return info;
112 }
113 
GetRoDataSections() const114 std::vector<SectionReference> CreatedObjectFile::GetRoDataSections() const
115 {
116     std::vector<SectionReference> references;
117     for (const auto &[name, section] : sectionIndex_) {
118         const auto &sectionName = cantFail(section.getName());
119         if (sectionName.startswith(RO_DATA_SECTION_PREFIX)) {
120             auto contents = cantFail(section.getContents());
121             auto memory = reinterpret_cast<const uint8_t *>(contents.data());
122             references.emplace_back(memory, contents.size(), name.str(), section.getAlignment());
123         }
124     }
125 
126     return references;
127 }
128 
WriteTo(std::string_view output) const129 void CreatedObjectFile::WriteTo(std::string_view output) const
130 {
131     std::error_code errorCode;
132     llvm::raw_fd_ostream dest(output, errorCode, llvm::sys::fs::FA_Write);
133     static constexpr bool GEN_CRASH_DIAG = false;
134     if (errorCode) {
135         report_fatal_error(llvm::Twine("Unable to open file = '") + output + "' to dump object file, error_code = " +
136                                llvm::Twine(errorCode.value()) + ", message = " + errorCode.message(),
137                            GEN_CRASH_DIAG);
138     }
139     llvm::StringRef data;
140     if (objectFile_ != nullptr) {
141         data = objectFile_->getData();
142     }
143     LLVM_DEBUG(llvm::dbgs() << "Writing object file, size =  " << data.size() << " to " << output << "\n");
144     dest << data;
145     if (dest.has_error()) {
146         auto error = dest.error();
147         report_fatal_error(llvm::Twine("Unable to write object file to '") + output +
148                                ", error_code = " + llvm::Twine(error.value()) + ", message = " + error.message(),
149                            GEN_CRASH_DIAG);
150     }
151 }
Size() const152 size_t CreatedObjectFile::Size() const
153 {
154     ASSERT(objectFile_ != nullptr && "Attempt to get size of empty CreatedObjectFile");
155     return objectFile_->getData().size();
156 }
157 
158 }  // namespace ark::llvmbackend
159