1 //===-- RuntimeDyldMachO.cpp - Run-time dynamic linker for MC-JIT -*- C++ -*-=//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Implementation of the MC-JIT runtime dynamic linker.
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "dyld"
15 #include "RuntimeDyldMachO.h"
16 #include "llvm/ADT/OwningPtr.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/StringRef.h"
20 using namespace llvm::object;
24 void RuntimeDyldMachO::resolveRelocation(const SectionEntry &Section,
29 uint8_t *LocalAddress = Section.Address + Offset;
30 uint64_t FinalAddress = Section.LoadAddress + Offset;
31 bool isPCRel = (Type >> 24) & 1;
32 unsigned MachoType = (Type >> 28) & 0xf;
33 unsigned Size = 1 << ((Type >> 25) & 3);
35 DEBUG(dbgs() << "resolveRelocation LocalAddress: "
36 << format("%p", LocalAddress)
37 << " FinalAddress: " << format("%p", FinalAddress)
38 << " Value: " << format("%p", Value)
39 << " Addend: " << Addend
40 << " isPCRel: " << isPCRel
41 << " MachoType: " << MachoType
45 // This just dispatches to the proper target specific routine.
47 default: llvm_unreachable("Unsupported CPU type!");
49 resolveX86_64Relocation(LocalAddress,
58 resolveI386Relocation(LocalAddress,
66 case Triple::arm: // Fall through.
68 resolveARMRelocation(LocalAddress,
79 bool RuntimeDyldMachO::resolveI386Relocation(uint8_t *LocalAddress,
80 uint64_t FinalAddress,
87 Value -= FinalAddress + 4; // see resolveX86_64Relocation
91 llvm_unreachable("Invalid relocation type!");
92 case macho::RIT_Vanilla: {
93 uint8_t *p = LocalAddress;
94 uint64_t ValueToWrite = Value + Addend;
95 for (unsigned i = 0; i < Size; ++i) {
96 *p++ = (uint8_t)(ValueToWrite & 0xff);
101 case macho::RIT_Difference:
102 case macho::RIT_Generic_LocalDifference:
103 case macho::RIT_Generic_PreboundLazyPointer:
104 return Error("Relocation type not implemented yet!");
108 bool RuntimeDyldMachO::resolveX86_64Relocation(uint8_t *LocalAddress,
109 uint64_t FinalAddress,
115 // If the relocation is PC-relative, the value to be encoded is the
116 // pointer difference.
118 // FIXME: It seems this value needs to be adjusted by 4 for an effective PC
119 // address. Is that expected? Only for branches, perhaps?
120 Value -= FinalAddress + 4;
124 llvm_unreachable("Invalid relocation type!");
125 case macho::RIT_X86_64_Signed1:
126 case macho::RIT_X86_64_Signed2:
127 case macho::RIT_X86_64_Signed4:
128 case macho::RIT_X86_64_Signed:
129 case macho::RIT_X86_64_Unsigned:
130 case macho::RIT_X86_64_Branch: {
132 // Mask in the target value a byte at a time (we don't have an alignment
133 // guarantee for the target address, so this is safest).
134 uint8_t *p = (uint8_t*)LocalAddress;
135 for (unsigned i = 0; i < Size; ++i) {
136 *p++ = (uint8_t)Value;
141 case macho::RIT_X86_64_GOTLoad:
142 case macho::RIT_X86_64_GOT:
143 case macho::RIT_X86_64_Subtractor:
144 case macho::RIT_X86_64_TLV:
145 return Error("Relocation type not implemented yet!");
149 bool RuntimeDyldMachO::resolveARMRelocation(uint8_t *LocalAddress,
150 uint64_t FinalAddress,
156 // If the relocation is PC-relative, the value to be encoded is the
157 // pointer difference.
159 Value -= FinalAddress;
160 // ARM PCRel relocations have an effective-PC offset of two instructions
161 // (four bytes in Thumb mode, 8 bytes in ARM mode).
162 // FIXME: For now, assume ARM mode.
168 llvm_unreachable("Invalid relocation type!");
169 case macho::RIT_Vanilla: {
170 // Mask in the target value a byte at a time (we don't have an alignment
171 // guarantee for the target address, so this is safest).
172 uint8_t *p = (uint8_t*)LocalAddress;
173 for (unsigned i = 0; i < Size; ++i) {
174 *p++ = (uint8_t)Value;
179 case macho::RIT_ARM_Branch24Bit: {
180 // Mask the value into the target address. We know instructions are
181 // 32-bit aligned, so we can do it all at once.
182 uint32_t *p = (uint32_t*)LocalAddress;
183 // The low two bits of the value are not encoded.
185 // Mask the value to 24 bits.
187 // FIXME: If the destination is a Thumb function (and the instruction
188 // is a non-predicated BL instruction), we need to change it to a BLX
189 // instruction instead.
191 // Insert the value into the instruction.
192 *p = (*p & ~0xffffff) | Value;
195 case macho::RIT_ARM_ThumbBranch22Bit:
196 case macho::RIT_ARM_ThumbBranch32Bit:
197 case macho::RIT_ARM_Half:
198 case macho::RIT_ARM_HalfDifference:
199 case macho::RIT_Pair:
200 case macho::RIT_Difference:
201 case macho::RIT_ARM_LocalDifference:
202 case macho::RIT_ARM_PreboundLazyPointer:
203 return Error("Relocation type not implemented yet!");
208 void RuntimeDyldMachO::processRelocationRef(unsigned SectionID,
209 relocation_iterator RelI,
211 ObjSectionToIDMap &ObjSectionToID,
212 const SymbolTableMap &Symbols,
214 const ObjectFile *OF = Obj.getObjectFile();
215 const MachOObjectFile *MachO = static_cast<const MachOObjectFile*>(OF);
216 macho::RelocationEntry RE = MachO->getRelocation(RelI->getRawDataRefImpl());
218 uint32_t RelType = MachO->getAnyRelocationType(RE);
219 RelocationValueRef Value;
220 SectionEntry &Section = Sections[SectionID];
222 bool isExtern = MachO->getPlainRelocationExternal(RE);
224 // Obtain the symbol name which is referenced in the relocation
226 RelI->getSymbol(Symbol);
227 StringRef TargetName;
228 Symbol.getName(TargetName);
229 // First search for the symbol in the local symbol table
230 SymbolTableMap::const_iterator lsi = Symbols.find(TargetName.data());
231 if (lsi != Symbols.end()) {
232 Value.SectionID = lsi->second.first;
233 Value.Addend = lsi->second.second;
235 // Search for the symbol in the global symbol table
236 SymbolTableMap::const_iterator gsi = GlobalSymbolTable.find(TargetName.data());
237 if (gsi != GlobalSymbolTable.end()) {
238 Value.SectionID = gsi->second.first;
239 Value.Addend = gsi->second.second;
241 Value.SymbolName = TargetName.data();
245 uint8_t sectionIndex = static_cast<uint8_t>(RelType & 0xFF);
246 section_iterator si = Obj.begin_sections(),
247 se = Obj.end_sections();
248 for (uint8_t i = 1; i < sectionIndex; i++) {
254 assert(si != se && "No section containing relocation!");
255 Value.SectionID = findOrEmitSection(Obj, *si, true, ObjSectionToID);
257 // FIXME: The size and type of the relocation determines if we can
258 // encode an Addend in the target location itself, and if so, how many
259 // bytes we should read in order to get it. We don't yet support doing
260 // that, and just assuming it's sizeof(intptr_t) is blatantly wrong.
261 //Value.Addend = *(const intptr_t *)Target;
263 // The MachO addend is an offset from the current section. We need it
264 // to be an offset from the destination section
265 Value.Addend += Section.ObjAddress - Sections[Value.SectionID].ObjAddress;
270 RelI->getOffset(Offset);
271 if (Arch == Triple::arm && (RelType & 0xf) == macho::RIT_ARM_Branch24Bit) {
272 // This is an ARM branch relocation, need to use a stub function.
274 // Look up for existing stub.
275 StubMap::const_iterator i = Stubs.find(Value);
276 if (i != Stubs.end())
277 resolveRelocation(Section, Offset,
278 (uint64_t)Section.Address + i->second,
281 // Create a new stub function.
282 Stubs[Value] = Section.StubOffset;
283 uint8_t *StubTargetAddr = createStubFunction(Section.Address +
285 RelocationEntry RE(SectionID, StubTargetAddr - Section.Address,
286 macho::RIT_Vanilla, Value.Addend);
287 if (Value.SymbolName)
288 addRelocationForSymbol(RE, Value.SymbolName);
290 addRelocationForSection(RE, Value.SectionID);
291 resolveRelocation(Section, Offset,
292 (uint64_t)Section.Address + Section.StubOffset,
294 Section.StubOffset += getMaxStubSize();
297 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend);
298 if (Value.SymbolName)
299 addRelocationForSymbol(RE, Value.SymbolName);
301 addRelocationForSection(RE, Value.SectionID);
306 bool RuntimeDyldMachO::isCompatibleFormat(
307 const ObjectBuffer *InputBuffer) const {
308 if (InputBuffer->getBufferSize() < 4)
310 StringRef Magic(InputBuffer->getBufferStart(), 4);
311 if (Magic == "\xFE\xED\xFA\xCE") return true;
312 if (Magic == "\xCE\xFA\xED\xFE") return true;
313 if (Magic == "\xFE\xED\xFA\xCF") return true;
314 if (Magic == "\xCF\xFA\xED\xFE") return true;
318 } // end namespace llvm