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