]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm-project/llvm/lib/ExecutionEngine/JITLink/MachOAtomGraphBuilder.h
MFC r355940:
[FreeBSD/FreeBSD.git] / contrib / llvm-project / llvm / lib / ExecutionEngine / JITLink / MachOAtomGraphBuilder.h
1 //===----- MachOAtomGraphBuilder.h - MachO AtomGraph builder ----*- 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 // Generic MachO AtomGraph building code.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #ifndef LIB_EXECUTIONENGINE_JITLINK_MACHOATOMGRAPHBUILDER_H
14 #define LIB_EXECUTIONENGINE_JITLINK_MACHOATOMGRAPHBUILDER_H
15
16 #include "llvm/ExecutionEngine/JITLink/JITLink.h"
17
18 #include "JITLinkGeneric.h"
19
20 #include "llvm/Object/MachO.h"
21
22 namespace llvm {
23 namespace jitlink {
24
25 class MachOAtomGraphBuilder {
26 public:
27   virtual ~MachOAtomGraphBuilder();
28   Expected<std::unique_ptr<AtomGraph>> buildGraph();
29
30 protected:
31   using OffsetToAtomMap = std::map<JITTargetAddress, DefinedAtom *>;
32
33   class MachOSection {
34   public:
35     MachOSection() = default;
36
37     /// Create a MachO section with the given address and alignment.
38     MachOSection(Section &GenericSection, JITTargetAddress Address,
39                  unsigned Alignment)
40         : Address(Address), GenericSection(&GenericSection),
41           Alignment(Alignment) {}
42
43     /// Create a section without address, content or size (used for common
44     /// symbol sections).
45     MachOSection(Section &GenericSection) : GenericSection(&GenericSection) {}
46
47     Section &getGenericSection() const {
48       assert(GenericSection && "Section is null");
49       return *GenericSection;
50     }
51
52     StringRef getName() const {
53       assert(GenericSection && "No generic section attached");
54       return GenericSection->getName();
55     }
56
57     MachOSection &setContent(StringRef Content) {
58       assert(!ContentPtr && !Size && "Content/zeroFill already set");
59       ContentPtr = Content.data();
60       Size = Content.size();
61       return *this;
62     }
63
64     MachOSection &setZeroFill(uint64_t Size) {
65       assert(!ContentPtr && !this->Size && "Content/zeroFill already set");
66       this->Size = Size;
67       return *this;
68     }
69
70     bool isZeroFill() const { return !ContentPtr; }
71
72     bool empty() const { return getSize() == 0; }
73
74     size_t getSize() const { return Size; }
75
76     StringRef getContent() const {
77       assert(ContentPtr && "getContent() called on zero-fill section");
78       return {ContentPtr, static_cast<size_t>(Size)};
79     }
80
81     JITTargetAddress getAddress() const { return Address; }
82
83     unsigned getAlignment() const { return Alignment; }
84
85     MachOSection &setNoDeadStrip(bool NoDeadStrip) {
86       this->NoDeadStrip = NoDeadStrip;
87       return *this;
88     }
89
90     bool isNoDeadStrip() const { return NoDeadStrip; }
91
92   private:
93     JITTargetAddress Address = 0;
94     Section *GenericSection = nullptr;
95     const char *ContentPtr = nullptr;
96     uint64_t Size = 0;
97     unsigned Alignment = 0;
98     bool NoDeadStrip = false;
99   };
100
101   using CustomAtomizeFunction = std::function<Error(MachOSection &S)>;
102
103   MachOAtomGraphBuilder(const object::MachOObjectFile &Obj);
104
105   AtomGraph &getGraph() const { return *G; }
106
107   const object::MachOObjectFile &getObject() const { return Obj; }
108
109   void addCustomAtomizer(StringRef SectionName, CustomAtomizeFunction Atomizer);
110
111   virtual Error addRelocations() = 0;
112
113   /// Returns true if Atom A and Atom B are at a fixed offset from one another
114   /// (i.e. if they're part of the same alt-entry chain).
115   bool areLayoutLocked(const Atom &A, const Atom &B);
116
117 private:
118   static unsigned getPointerSize(const object::MachOObjectFile &Obj);
119   static support::endianness getEndianness(const object::MachOObjectFile &Obj);
120
121   MachOSection &getCommonSection();
122
123   Error parseSections();
124   Error addNonCustomAtoms();
125   Error addAtoms();
126
127   const object::MachOObjectFile &Obj;
128   std::unique_ptr<AtomGraph> G;
129   DenseMap<const DefinedAtom *, const DefinedAtom *> AltEntryStarts;
130   DenseMap<unsigned, MachOSection> Sections;
131   StringMap<CustomAtomizeFunction> CustomAtomizeFunctions;
132   Optional<MachOSection> CommonSymbolsSection;
133 };
134
135 } // end namespace jitlink
136 } // end namespace llvm
137
138 #endif // LIB_EXECUTIONENGINE_JITLINK_MACHOATOMGRAPHBUILDER_H