xref: /llvm-project/bolt/include/bolt/Rewrite/ExecutableFileMemoryManager.h (revision 05634f7346a59f6dab89cde53f39b40d9a70b9c9)
1 //===- bolt/Rewrite/ExecutableFileMemoryManager.h ---------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef BOLT_REWRITE_EXECUTABLE_FILE_MEMORY_MANAGER_H
10 #define BOLT_REWRITE_EXECUTABLE_FILE_MEMORY_MANAGER_H
11 
12 #include "llvm/ADT/StringRef.h"
13 #include "llvm/ExecutionEngine/JITLink/JITLinkMemoryManager.h"
14 #include <cstdint>
15 #include <string>
16 
17 namespace llvm {
18 
19 namespace bolt {
20 class BinaryContext;
21 
22 /// Class responsible for allocating and managing code and data sections.
23 class ExecutableFileMemoryManager : public jitlink::JITLinkMemoryManager {
24 private:
25   void updateSection(const jitlink::Section &Section, uint8_t *Contents,
26                      size_t Size, size_t Alignment);
27 
28   BinaryContext &BC;
29 
30   // All new sections will be identified by the following prefix.
31   std::string NewSecPrefix;
32 
33   // Name prefix used for sections from the input.
34   std::string OrgSecPrefix;
35 
36 public:
37   // Our linker's main purpose is to handle a single object file, created
38   // by RewriteInstance after reading the input binary and reordering it.
39   // After objects finish loading, we increment this. Therefore, whenever
40   // this is greater than zero, we are dealing with additional objects that
41   // will not be managed by BinaryContext but only exist to support linking
42   // user-supplied objects into the main input executable.
43   uint32_t ObjectsLoaded{0};
44 
ExecutableFileMemoryManager(BinaryContext & BC)45   ExecutableFileMemoryManager(BinaryContext &BC) : BC(BC) {}
46 
47   void allocate(const jitlink::JITLinkDylib *JD, jitlink::LinkGraph &G,
48                 OnAllocatedFunction OnAllocated) override;
49 
50   void deallocate(std::vector<FinalizedAlloc> Allocs,
51                   OnDeallocatedFunction OnDeallocated) override;
52   using JITLinkMemoryManager::deallocate;
53 
54   /// Section name management.
setNewSecPrefix(StringRef Prefix)55   void setNewSecPrefix(StringRef Prefix) { NewSecPrefix = Prefix; }
setOrgSecPrefix(StringRef Prefix)56   void setOrgSecPrefix(StringRef Prefix) { OrgSecPrefix = Prefix; }
57 };
58 
59 } // namespace bolt
60 } // namespace llvm
61 
62 #endif
63