LLVM  10.0.0svn
MachOLinkGraphBuilder.h
Go to the documentation of this file.
1 //===----- MachOLinkGraphBuilder.h - MachO LinkGraph 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 LinkGraph building code.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LIB_EXECUTIONENGINE_JITLINK_MACHOLINKGRAPHBUILDER_H
14 #define LIB_EXECUTIONENGINE_JITLINK_MACHOLINKGRAPHBUILDER_H
15 
17 
18 #include "EHFrameSupportImpl.h"
19 #include "JITLinkGeneric.h"
20 #include "llvm/Object/MachO.h"
21 
22 #include <list>
23 
24 namespace llvm {
25 namespace jitlink {
26 
28 public:
29  virtual ~MachOLinkGraphBuilder();
31 
32 protected:
34  public:
36  JITTargetAddress EHFrameAddress,
37  StringRef EHFrameContent, Section &EHFrameSection,
38  uint64_t CIEAlignment, uint64_t FDEAlignment,
39  Edge::Kind FDEToCIERelocKind,
40  Edge::Kind FDEToTargetRelocKind)
41  : EHFrameBinaryParser(EHFrameAddress, EHFrameContent,
42  Builder.getGraph().getPointerSize(),
43  Builder.getGraph().getEndianness()),
44  Builder(Builder), EHFrameSection(EHFrameSection),
45  CIEAlignment(CIEAlignment), FDEAlignment(FDEAlignment),
46  FDEToCIERelocKind(FDEToCIERelocKind),
47  FDEToTargetRelocKind(FDEToTargetRelocKind) {}
48 
50  if (auto *Sym = Builder.getSymbolByAddress(Address))
51  if (Sym->getAddress() == Address)
52  return Sym;
53  return nullptr;
54  }
55 
57  StringRef RecordContent) override {
58  auto &G = Builder.getGraph();
59  auto &B = G.createContentBlock(EHFrameSection, RecordContent, RecordAddr,
60  CIEAlignment, 0);
61  auto &CIESymbol =
62  G.addAnonymousSymbol(B, 0, RecordContent.size(), false, false);
63  Builder.setCanonicalSymbol(CIESymbol);
64  return CIESymbol;
65  }
66 
68  StringRef RecordContent, Symbol &CIE,
69  size_t CIEOffset, Symbol &Func,
70  size_t FuncOffset, Symbol *LSDA,
71  size_t LSDAOffset) override {
72  auto &G = Builder.getGraph();
73  auto &B = G.createContentBlock(EHFrameSection, RecordContent, RecordAddr,
74  FDEAlignment, 0);
75 
76  // Add edges to CIE, Func, and (conditionally) LSDA.
77  B.addEdge(FDEToCIERelocKind, CIEOffset, CIE, 0);
78  B.addEdge(FDEToTargetRelocKind, FuncOffset, Func, 0);
79 
80  if (LSDA)
81  B.addEdge(FDEToTargetRelocKind, LSDAOffset, *LSDA, 0);
82 
83  auto &FDESymbol =
84  G.addAnonymousSymbol(B, 0, RecordContent.size(), false, false);
85 
86  // Add a keep-alive relocation from the function to the FDE to ensure it
87  // is not dead stripped.
88  Func.getBlock().addEdge(Edge::KeepAlive, 0, FDESymbol, 0);
89 
90  return FDESymbol;
91  }
92 
93  private:
94  MachOLinkGraphBuilder &Builder;
95  Section &EHFrameSection;
96  uint64_t CIEAlignment;
97  uint64_t FDEAlignment;
98  Edge::Kind FDEToCIERelocKind;
99  Edge::Kind FDEToTargetRelocKind;
100  };
101 
103  friend class MachOLinkGraphBuilder;
104 
105  private:
107  uint8_t Sect, uint16_t Desc, Linkage L, Scope S)
108  : Name(Name), Value(Value), Type(Type), Sect(Sect), Desc(Desc), L(L),
109  S(S) {
110  assert((!Name || !Name->empty()) && "Name must be none or non-empty");
111  }
112 
113  public:
114  NormalizedSymbol(const NormalizedSymbol &) = delete;
115  NormalizedSymbol &operator=(const NormalizedSymbol &) = delete;
116  NormalizedSymbol(NormalizedSymbol &&) = delete;
117  NormalizedSymbol &operator=(NormalizedSymbol &&) = delete;
118 
120  uint64_t Value = 0;
121  uint8_t Type = 0;
122  uint8_t Sect = 0;
123  uint16_t Desc = 0;
126  Symbol *GraphSymbol = nullptr;
127  };
128 
130  friend class MachOLinkGraphBuilder;
131 
132  private:
133  NormalizedSection() = default;
134 
135  public:
136  Section *GraphSection = nullptr;
137  uint64_t Address = 0;
138  uint64_t Size = 0;
139  uint64_t Alignment = 0;
140  uint32_t Flags = 0;
141  const char *Data = nullptr;
142  };
143 
144  using SectionParserFunction = std::function<Error(NormalizedSection &S)>;
145 
147 
148  LinkGraph &getGraph() const { return *G; }
149 
150  const object::MachOObjectFile &getObject() const { return Obj; }
151 
153  SectionParserFunction Parse);
154 
155  virtual Error addRelocations() = 0;
156 
157  /// Create a symbol.
158  template <typename... ArgTs>
160  NormalizedSymbol *Sym = reinterpret_cast<NormalizedSymbol *>(
161  Allocator.Allocate<NormalizedSymbol>());
162  new (Sym) NormalizedSymbol(std::forward<ArgTs>(Args)...);
163  return *Sym;
164  }
165 
166  /// Index is zero-based (MachO section indexes are usually one-based) and
167  /// assumed to be in-range. Client is responsible for checking.
169  auto I = IndexToSection.find(Index);
170  assert(I != IndexToSection.end() && "No section recorded at index");
171  return I->second;
172  }
173 
174  /// Try to get the section at the given index. Will return an error if the
175  /// given index is out of range, or if no section has been added for the given
176  /// index.
178  auto I = IndexToSection.find(Index);
179  if (I == IndexToSection.end())
180  return make_error<JITLinkError>("No section recorded for index " +
181  formatv("{0:u}", Index));
182  return I->second;
183  }
184 
185  /// Try to get the symbol at the given index. Will return an error if the
186  /// given index is out of range, or if no symbol has been added for the given
187  /// index.
189  if (Index >= IndexToSymbol.size())
190  return make_error<JITLinkError>("Symbol index out of range");
191  auto *Sym = IndexToSymbol[Index];
192  if (!Sym)
193  return make_error<JITLinkError>("No symbol at index " +
194  formatv("{0:u}", Index));
195  return *Sym;
196  }
197 
198  /// Returns the symbol with the highest address not greater than the search
199  /// address, or null if no such symbol exists.
201  auto I = AddrToCanonicalSymbol.upper_bound(Address);
202  if (I == AddrToCanonicalSymbol.begin())
203  return nullptr;
204  return std::prev(I)->second;
205  }
206 
207  /// Returns the symbol with the highest address not greater than the search
208  /// address, or an error if no such symbol exists.
210  auto *Sym = getSymbolByAddress(Address);
211  if (Sym)
212  if (Address < Sym->getAddress() + Sym->getSize())
213  return *Sym;
214  return make_error<JITLinkError>("No symbol covering address " +
215  formatv("{0:x16}", Address));
216  }
217 
218  static Linkage getLinkage(uint16_t Desc);
219  static Scope getScope(StringRef Name, uint8_t Type);
220  static bool isAltEntry(const NormalizedSymbol &NSym);
221 
222 private:
223  static unsigned getPointerSize(const object::MachOObjectFile &Obj);
224  static support::endianness getEndianness(const object::MachOObjectFile &Obj);
225 
226  void setCanonicalSymbol(Symbol &Sym) {
227  auto *&CanonicalSymEntry = AddrToCanonicalSymbol[Sym.getAddress()];
228  // There should be no symbol at this address, or, if there is,
229  // it should be a zero-sized symbol from an empty section (which
230  // we can safely override).
231  assert((!CanonicalSymEntry || CanonicalSymEntry->getSize() == 0) &&
232  "Duplicate canonical symbol at address");
233  CanonicalSymEntry = &Sym;
234  }
235 
236  Section &getCommonSection();
237  void addSectionStartSymAndBlock(Section &GraphSec, uint64_t Address,
238  const char *Data, uint64_t Size,
239  uint32_t Alignment, bool IsLive);
240 
241  Error createNormalizedSections();
242  Error createNormalizedSymbols();
243 
244  /// Create graph blocks and symbols for externals, absolutes, commons and
245  /// all defined symbols in sections without custom parsers.
246  Error graphifyRegularSymbols();
247 
248  /// Create graph blocks and symbols for all sections.
249  Error graphifySectionsWithCustomParsers();
250 
251  // Put the BumpPtrAllocator first so that we don't free any of the underlying
252  // memory until the Symbol/Addressable destructors have been run.
254 
255  const object::MachOObjectFile &Obj;
256  std::unique_ptr<LinkGraph> G;
257 
259  Section *CommonSection = nullptr;
260 
262  std::map<JITTargetAddress, Symbol *> AddrToCanonicalSymbol;
263  StringMap<SectionParserFunction> CustomSectionParserFunctions;
264 };
265 
266 } // end namespace jitlink
267 } // end namespace llvm
268 
269 #endif // LIB_EXECUTIONENGINE_JITLINK_MACHOLINKGRAPHBUILDER_H
This class represents lattice values for constants.
Definition: AllocatorList.h:23
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
auto formatv(const char *Fmt, Ts &&... Vals) -> formatv_object< decltype(std::make_tuple(detail::build_format_adapter(std::forward< Ts >(Vals))...))>
Tagged union holding either a T or a Error.
Definition: yaml2obj.h:21
#define G(x, y, z)
Definition: MD5.cpp:56
static uint64_t getPointerSize(const Value *V, const DataLayout &DL, const TargetLibraryInfo &TLI, const Function *F)
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")
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:141
Basic Register Allocator
const DataFlowGraph & G
Definition: RDFGraph.cpp:202
#define I(x, y, z)
Definition: MD5.cpp:58
uint32_t Size
Definition: Profile.cpp:46
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:74
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
constexpr char Args[]
Key for Kernel::Metadata::mArgs.