1 //===----- MachOAtomGraphBuilder.h - MachO AtomGraph builder ----*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // Generic MachO AtomGraph building code.
11 //===----------------------------------------------------------------------===//
13 #ifndef LIB_EXECUTIONENGINE_JITLINK_MACHOATOMGRAPHBUILDER_H
14 #define LIB_EXECUTIONENGINE_JITLINK_MACHOATOMGRAPHBUILDER_H
16 #include "llvm/ExecutionEngine/JITLink/JITLink.h"
18 #include "JITLinkGeneric.h"
20 #include "llvm/Object/MachO.h"
25 class MachOAtomGraphBuilder
{
27 virtual ~MachOAtomGraphBuilder();
28 Expected
<std::unique_ptr
<AtomGraph
>> buildGraph();
31 using OffsetToAtomMap
= std::map
<JITTargetAddress
, DefinedAtom
*>;
35 MachOSection() = default;
37 /// Create a MachO section with the given address and alignment.
38 MachOSection(Section
&GenericSection
, JITTargetAddress Address
,
40 : Address(Address
), GenericSection(&GenericSection
),
41 Alignment(Alignment
) {}
43 /// Create a section without address, content or size (used for common
45 MachOSection(Section
&GenericSection
) : GenericSection(&GenericSection
) {}
47 Section
&getGenericSection() const {
48 assert(GenericSection
&& "Section is null");
49 return *GenericSection
;
52 StringRef
getName() const {
53 assert(GenericSection
&& "No generic section attached");
54 return GenericSection
->getName();
57 MachOSection
&setContent(StringRef Content
) {
58 assert(!ContentPtr
&& !Size
&& "Content/zeroFill already set");
59 ContentPtr
= Content
.data();
60 Size
= Content
.size();
64 MachOSection
&setZeroFill(uint64_t Size
) {
65 assert(!ContentPtr
&& !this->Size
&& "Content/zeroFill already set");
70 bool isZeroFill() const { return !ContentPtr
; }
72 bool empty() const { return getSize() == 0; }
74 size_t getSize() const { return Size
; }
76 StringRef
getContent() const {
77 assert(ContentPtr
&& "getContent() called on zero-fill section");
78 return {ContentPtr
, static_cast<size_t>(Size
)};
81 JITTargetAddress
getAddress() const { return Address
; }
83 unsigned getAlignment() const { return Alignment
; }
85 MachOSection
&setNoDeadStrip(bool NoDeadStrip
) {
86 this->NoDeadStrip
= NoDeadStrip
;
90 bool isNoDeadStrip() const { return NoDeadStrip
; }
93 JITTargetAddress Address
= 0;
94 Section
*GenericSection
= nullptr;
95 const char *ContentPtr
= nullptr;
97 unsigned Alignment
= 0;
98 bool NoDeadStrip
= false;
101 using CustomAtomizeFunction
= std::function
<Error(MachOSection
&S
)>;
103 MachOAtomGraphBuilder(const object::MachOObjectFile
&Obj
);
105 AtomGraph
&getGraph() const { return *G
; }
107 const object::MachOObjectFile
&getObject() const { return Obj
; }
109 void addCustomAtomizer(StringRef SectionName
, CustomAtomizeFunction Atomizer
);
111 virtual Error
addRelocations() = 0;
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
);
118 static unsigned getPointerSize(const object::MachOObjectFile
&Obj
);
119 static support::endianness
getEndianness(const object::MachOObjectFile
&Obj
);
121 MachOSection
&getCommonSection();
123 Error
parseSections();
124 Error
addNonCustomAtoms();
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
;
135 } // end namespace jitlink
136 } // end namespace llvm
138 #endif // LIB_EXECUTIONENGINE_JITLINK_MACHOATOMGRAPHBUILDER_H