LLVM  10.0.0svn
MachOAtomGraphBuilder.h
Go to the documentation of this file.
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 
17 
18 #include "JITLinkGeneric.h"
19 
20 #include "llvm/Object/MachO.h"
21 
22 namespace llvm {
23 namespace jitlink {
24 
26 public:
27  virtual ~MachOAtomGraphBuilder();
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 
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 
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 
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 
104 
105  AtomGraph &getGraph() const { return *G; }
106 
107  const object::MachOObjectFile &getObject() const { return Obj; }
108 
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;
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
This class represents lattice values for constants.
Definition: AllocatorList.h:23
Tagged union holding either a T or a Error.
Definition: yaml2obj.h:21
#define G(x, y, z)
Definition: MD5.cpp:56
LLVM_NODISCARD size_t size() const
size - Get the string size.
Definition: StringRef.h:144
uint64_t JITTargetAddress
Represents an address in the target process&#39;s address space.
Definition: JITSymbol.h:41
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
LLVM_NODISCARD const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:136
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48