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