• 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_ARK_AOT_LINKER_H
17 #define LIBLLVMBACKEND_OBJECT_CODE_ARK_AOT_LINKER_H
18 
19 #include <llvm/ExecutionEngine/SectionMemoryManager.h>
20 #include <llvm/Pass.h>
21 #include <llvm/Target/TargetMachine.h>
22 
23 #include "created_object_file.h"
24 #include "llvm_ark_interface.h"
25 #include "llvm_compiler_options.h"
26 #include "transforms/llvm_optimizer.h"
27 
28 // NB: Include carefully. May lead to macro collisions between LLVM and Panda headers.
29 
30 namespace ark::llvmbackend {
31 
32 class PandaSectionMemoryManager : public llvm::SectionMemoryManager {
33 public:
34     bool allowStubAllocation() const override;
35 
36     uint8_t *allocateCodeSection(uintptr_t size, unsigned int alignment, unsigned int sectionId,
37                                  llvm::StringRef sectionName) override;
38 
39     uint8_t *allocateDataSection(uintptr_t size, unsigned int alignment, unsigned int sectionId,
40                                  llvm::StringRef sectionName, bool readOnly) override;
41 
42     const std::unordered_map<std::string, SectionReference> &GetSections() const;
43 
44 private:
45     void RememberAllocation(llvm::StringRef sectionName, uint8_t *memory, uintptr_t size, size_t alignment);
46 
47 private:
48     std::unordered_map<std::string, SectionReference> sections_;
49 };
50 
51 class ArkAotLinker {
52 public:
53     using RoDataSections = llvm::SmallVector<SectionReference, 4U>;
54 
55     explicit ArkAotLinker(size_t functionHeaderSize);
56 
57     ArkAotLinker(const ArkAotLinker &) = delete;
58     ArkAotLinker &operator=(const ArkAotLinker &) = delete;
59     ArkAotLinker(ArkAotLinker &&) = delete;
60     ArkAotLinker &operator=(ArkAotLinker &&) = delete;
61 
62     ~ArkAotLinker();
63 
64     llvm::Error LoadObject(std::unique_ptr<CreatedObjectFile> objectFile);
65 
66     SectionReference GetLinkedSection(const std::string &name) const;
67 
68     SectionReference GetLinkedFunctionSection(const std::string &fullFunctionName) const;
69 
70     RoDataSections GetLinkedRoDataSections() const;
71 
72     llvm::Error RelocateSections(const std::unordered_map<std::string, size_t> &sectionAddresses,
73                                  const std::unordered_map<std::string, size_t> &methodOffsets, uint32_t moduleId);
74 
75 private:
76     void RelocateFunctionSection(const SectionReference &sectionReference,
77                                  const std::unordered_map<std::string, size_t> &sectionAddresses,
78                                  const std::unordered_map<std::string, size_t> &methodOffsets);
79 
80     void RelocateSection(const SectionReference &sectionReference,
81                          const std::unordered_map<std::string, size_t> &sectionAddresses, uint32_t moduleId);
82 
83 private:
84     size_t functionHeaderSize_;
85     llvm::SmallVector<std::unique_ptr<CreatedObjectFile>, 1> objects_;
86 
87     PandaSectionMemoryManager memoryManager_;
88     llvm::RuntimeDyld runtimeDyLd_ {memoryManager_, memoryManager_};
89 };
90 
91 }  // namespace ark::llvmbackend
92 
93 #endif  //  LIBLLVMBACKEND_OBJECT_CODE_ARK_AOT_LINKER_H
94