LLVM  3.7.0
RuntimeDyldMachOX86_64.h
Go to the documentation of this file.
1 //===-- RuntimeDyldMachOX86_64.h ---- MachO/X86_64 specific code. -*- C++ -*-=//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDMACHOX86_64_H
11 #define LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDMACHOX86_64_H
12 
13 #include "../RuntimeDyldMachO.h"
14 
15 #define DEBUG_TYPE "dyld"
16 
17 namespace llvm {
18 
20  : public RuntimeDyldMachOCRTPBase<RuntimeDyldMachOX86_64> {
21 public:
22 
23  typedef uint64_t TargetPtrT;
24 
27  : RuntimeDyldMachOCRTPBase(MM, Resolver) {}
28 
29  unsigned getMaxStubSize() override { return 8; }
30 
31  unsigned getStubAlignment() override { return 1; }
32 
34  processRelocationRef(unsigned SectionID, relocation_iterator RelI,
35  const ObjectFile &BaseObjT,
36  ObjSectionToIDMap &ObjSectionToID,
37  StubMap &Stubs) override {
38  const MachOObjectFile &Obj =
39  static_cast<const MachOObjectFile &>(BaseObjT);
41  Obj.getRelocation(RelI->getRawDataRefImpl());
42 
43  assert(!Obj.isRelocationScattered(RelInfo) &&
44  "Scattered relocations not supported on X86_64");
45 
46  RelocationEntry RE(getRelocationEntry(SectionID, Obj, RelI));
47  RE.Addend = memcpyAddend(RE);
49  getRelocationValueRef(Obj, RelI, RE, ObjSectionToID));
50 
51  bool IsExtern = Obj.getPlainRelocationExternal(RelInfo);
52  if (!IsExtern && RE.IsPCRel)
53  makeValueAddendPCRel(Value, RelI, 1 << RE.Size);
54 
55  if (RE.RelType == MachO::X86_64_RELOC_GOT ||
56  RE.RelType == MachO::X86_64_RELOC_GOT_LOAD)
57  processGOTRelocation(RE, Value, Stubs);
58  else {
59  RE.Addend = Value.Offset;
60  if (Value.SymbolName)
62  else
64  }
65 
66  return ++RelI;
67  }
68 
69  void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override {
70  DEBUG(dumpRelocationToResolve(RE, Value));
72  uint8_t *LocalAddress = Section.Address + RE.Offset;
73 
74  // If the relocation is PC-relative, the value to be encoded is the
75  // pointer difference.
76  if (RE.IsPCRel) {
77  // FIXME: It seems this value needs to be adjusted by 4 for an effective
78  // PC address. Is that expected? Only for branches, perhaps?
79  uint64_t FinalAddress = Section.LoadAddress + RE.Offset;
80  Value -= FinalAddress + 4;
81  }
82 
83  switch (RE.RelType) {
84  default:
85  llvm_unreachable("Invalid relocation type!");
92  writeBytesUnaligned(Value + RE.Addend, LocalAddress, 1 << RE.Size);
93  break;
98  Error("Relocation type not implemented yet!");
99  }
100  }
101 
102  void finalizeSection(const ObjectFile &Obj, unsigned SectionID,
103  const SectionRef &Section) {}
104 
105 private:
106  void processGOTRelocation(const RelocationEntry &RE,
107  RelocationValueRef &Value, StubMap &Stubs) {
109  assert(RE.IsPCRel);
110  assert(RE.Size == 2);
111  Value.Offset -= RE.Addend;
112  RuntimeDyldMachO::StubMap::const_iterator i = Stubs.find(Value);
113  uint8_t *Addr;
114  if (i != Stubs.end()) {
115  Addr = Section.Address + i->second;
116  } else {
117  Stubs[Value] = Section.StubOffset;
118  uint8_t *GOTEntry = Section.Address + Section.StubOffset;
119  RelocationEntry GOTRE(RE.SectionID, Section.StubOffset,
120  MachO::X86_64_RELOC_UNSIGNED, Value.Offset, false,
121  3);
122  if (Value.SymbolName)
123  addRelocationForSymbol(GOTRE, Value.SymbolName);
124  else
125  addRelocationForSection(GOTRE, Value.SectionID);
126  Section.StubOffset += 8;
127  Addr = GOTEntry;
128  }
129  RelocationEntry TargetRE(RE.SectionID, RE.Offset,
130  MachO::X86_64_RELOC_UNSIGNED, RE.Addend, true, 2);
131  resolveRelocation(TargetRE, (uint64_t)Addr);
132  }
133 };
134 }
135 
136 #undef DEBUG_TYPE
137 
138 #endif
RelocationEntry - used to represent relocations internally in the dynamic linker. ...
bool getPlainRelocationExternal(const MachO::any_relocation_info &RE) const
void writeBytesUnaligned(uint64_t Value, uint8_t *Dst, unsigned Size) const
Endian-aware write.
This class is the base class for all object file types.
Definition: ObjectFile.h:176
uint8_t * Address
Address - address in the linker's memory where the section resides.
void finalizeSection(const ObjectFile &Obj, unsigned SectionID, const SectionRef &Section)
bool IsPCRel
True if this is a PCRel relocation (MachO specific).
unsigned SectionID
SectionID - the section this relocation points to.
RelocationEntry getRelocationEntry(unsigned SectionID, const ObjectFile &BaseTObj, const relocation_iterator &RI) const
Given a relocation_iterator for a non-scattered relocation, construct a RelocationEntry and fill in t...
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:98
int64_t memcpyAddend(const RelocationEntry &RE) const
This convenience method uses memcpy to extract a contiguous addend (the addend size and offset are ta...
std::map< RelocationValueRef, uintptr_t > StubMap
uintptr_t StubOffset
StubOffset - used for architectures with stub functions for far relocations (like ARM)...
void dumpRelocationToResolve(const RelocationEntry &RE, uint64_t Value) const
Dump information about the relocation entry (RE) and resolved value.
RuntimeDyldMachOTarget - Templated base class for generic MachO linker algorithms and data structures...
bool Error(const Twine &Msg)
void addRelocationForSymbol(const RelocationEntry &RE, StringRef SymbolName)
MachO::any_relocation_info getRelocation(DataRefImpl Rel) const
RuntimeDyld::SymbolResolver & Resolver
void addRelocationForSection(const RelocationEntry &RE, unsigned SectionID)
relocation_iterator processRelocationRef(unsigned SectionID, relocation_iterator RelI, const ObjectFile &BaseObjT, ObjSectionToIDMap &ObjSectionToID, StubMap &Stubs) override
Parses one or more object file relocations (some object files use relocation pairs) and stores it to ...
int64_t Addend
Addend - the relocation addend encoded in the instruction itself.
uint32_t RelType
RelType - relocation type.
RuntimeDyldMachOX86_64(RuntimeDyld::MemoryManager &MM, RuntimeDyld::SymbolResolver &Resolver)
uint64_t Offset
Offset - offset into the section.
std::map< SectionRef, unsigned > ObjSectionToIDMap
RelocationValueRef getRelocationValueRef(const ObjectFile &BaseTObj, const relocation_iterator &RI, const RelocationEntry &RE, ObjSectionToIDMap &ObjSectionToID)
Construct a RelocationValueRef representing the relocation target.
uint64_t LoadAddress
LoadAddress - the address of the section in the target process's memory.
void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override
A object file specific relocation resolver.
SectionEntry - represents a section emitted into memory by the dynamic linker.
LLVM Value Representation.
Definition: Value.h:69
unsigned Size
The size of this relocation (MachO specific).
#define DEBUG(X)
Definition: Debug.h:92
void makeValueAddendPCRel(RelocationValueRef &Value, const relocation_iterator &RI, unsigned OffsetToNextPC)
Make the RelocationValueRef addend PC-relative.
bool isRelocationScattered(const MachO::any_relocation_info &RE) const
This is a value type class that represents a single section in the list of sections in the object fil...
Definition: ObjectFile.h:69