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 #ifndef LIBLLVMBACKEND_OBJECT_CODE_CREATED_OBJECT_FILE_H 17 #define LIBLLVMBACKEND_OBJECT_CODE_CREATED_OBJECT_FILE_H 18 19 #include <llvm/Object/ObjectFile.h> 20 21 #include <map> 22 #include <unordered_map> 23 24 namespace ark::llvmbackend { 25 26 constexpr llvm::StringRef RO_DATA_SECTION_PREFIX {".ro"}; 27 constexpr llvm::StringRef TEXT_SECTION_PREFIX {".text."}; 28 constexpr llvm::StringRef AOT_GOT_SECTION {".aot_got"}; 29 constexpr llvm::StringRef RELA_LLVM_STACKMAPS_SECTION {".rela.llvm_stackmaps"}; 30 constexpr llvm::StringRef LLVM_STACKMAPS_SECTION {".llvm_stackmaps"}; 31 32 class SectionReference { 33 public: 34 SectionReference() = default; 35 SectionReference(const uint8_t * memory,uintptr_t size,std::string name,size_t alignment)36 explicit SectionReference(const uint8_t *memory, uintptr_t size, std::string name, size_t alignment) 37 : memory_(memory), size_(size), name_(std::move(name)), alignment_(alignment) 38 { 39 } 40 GetMemory()41 const uint8_t *GetMemory() const 42 { 43 return memory_; 44 } GetSize()45 uintptr_t GetSize() const 46 { 47 return size_; 48 } GetName()49 const std::string &GetName() const 50 { 51 return name_; 52 } 53 GetAlignment()54 size_t GetAlignment() const 55 { 56 return alignment_; 57 } 58 59 std::vector<uint8_t> ContentToVector(); 60 61 private: 62 const uint8_t *memory_ {nullptr}; 63 uintptr_t size_ {0}; 64 std::string name_; 65 size_t alignment_ {0}; 66 }; 67 68 class CreatedObjectFile { 69 public: 70 using ObjectFilePostProcessor = std::function<void(llvm::object::ObjectFile *)>; 71 72 struct StackMapSymbol { 73 uint64_t idx; 74 uint64_t sectionOffset; 75 }; 76 77 static llvm::Expected<std::unique_ptr<CreatedObjectFile>> CopyOf(llvm::MemoryBufferRef objectFileBuffer); 78 79 explicit CreatedObjectFile(std::unique_ptr<llvm::MemoryBuffer> objectFileBuffer, 80 std::unique_ptr<llvm::object::ObjectFile> objectFile); 81 82 llvm::object::ObjectFile *GetObjectFile() const; 83 84 bool HasSection(const std::string &name) const; 85 86 SectionReference GetSection(const std::string &name) const; 87 88 SectionReference GetSectionByFunctionName(const std::string &fullFunctionName) const; 89 90 std::vector<SectionReference> GetRoDataSections() const; 91 92 std::unordered_map<std::string, StackMapSymbol> GetStackMapInfo() const; 93 std::unordered_map<std::string, uint32_t> GetFaultMapInfo() const; 94 95 void WriteTo(std::string_view output) const; 96 97 size_t Size() const; 98 99 private: 100 std::unique_ptr<llvm::MemoryBuffer> buffer_; 101 std::unique_ptr<llvm::object::ObjectFile> objectFile_; 102 std::map<llvm::StringRef, llvm::object::SectionRef> sectionIndex_; 103 }; 104 105 } // namespace ark::llvmbackend 106 #endif // LIBLLVMBACKEND_OBJECT_CODE_CREATED_OBJECT_FILE_H 107