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 RelocationEntry &RE,
26 const SectionEntry &Section = Sections[RE.SectionID];
27 return resolveRelocation(Section, RE.Offset, Value, RE.RelType, RE.Addend,
31 void RuntimeDyldMachO::resolveRelocation(const SectionEntry &Section,
38 uint8_t *LocalAddress = Section.Address + Offset;
39 uint64_t FinalAddress = Section.LoadAddress + Offset;
40 unsigned MachoType = Type;
41 unsigned Size = 1 << LogSize;
43 DEBUG(dbgs() << "resolveRelocation LocalAddress: "
44 << format("%p", LocalAddress)
45 << " FinalAddress: " << format("%p", FinalAddress)
46 << " Value: " << format("%p", Value)
47 << " Addend: " << Addend
48 << " isPCRel: " << isPCRel
49 << " MachoType: " << MachoType
53 // This just dispatches to the proper target specific routine.
55 default: llvm_unreachable("Unsupported CPU type!");
57 resolveX86_64Relocation(LocalAddress,
66 resolveI386Relocation(LocalAddress,
74 case Triple::arm: // Fall through.
76 resolveARMRelocation(LocalAddress,
87 bool RuntimeDyldMachO::resolveI386Relocation(uint8_t *LocalAddress,
88 uint64_t FinalAddress,
95 Value -= FinalAddress + 4; // see resolveX86_64Relocation
99 llvm_unreachable("Invalid relocation type!");
100 case macho::RIT_Vanilla: {
101 uint8_t *p = LocalAddress;
102 uint64_t ValueToWrite = Value + Addend;
103 for (unsigned i = 0; i < Size; ++i) {
104 *p++ = (uint8_t)(ValueToWrite & 0xff);
109 case macho::RIT_Difference:
110 case macho::RIT_Generic_LocalDifference:
111 case macho::RIT_Generic_PreboundLazyPointer:
112 return Error("Relocation type not implemented yet!");
116 bool RuntimeDyldMachO::resolveX86_64Relocation(uint8_t *LocalAddress,
117 uint64_t FinalAddress,
123 // If the relocation is PC-relative, the value to be encoded is the
124 // pointer difference.
126 // FIXME: It seems this value needs to be adjusted by 4 for an effective PC
127 // address. Is that expected? Only for branches, perhaps?
128 Value -= FinalAddress + 4;
132 llvm_unreachable("Invalid relocation type!");
133 case macho::RIT_X86_64_Signed1:
134 case macho::RIT_X86_64_Signed2:
135 case macho::RIT_X86_64_Signed4:
136 case macho::RIT_X86_64_Signed:
137 case macho::RIT_X86_64_Unsigned:
138 case macho::RIT_X86_64_Branch: {
140 // Mask in the target value a byte at a time (we don't have an alignment
141 // guarantee for the target address, so this is safest).
142 uint8_t *p = (uint8_t*)LocalAddress;
143 for (unsigned i = 0; i < Size; ++i) {
144 *p++ = (uint8_t)Value;
149 case macho::RIT_X86_64_GOTLoad:
150 case macho::RIT_X86_64_GOT:
151 case macho::RIT_X86_64_Subtractor:
152 case macho::RIT_X86_64_TLV:
153 return Error("Relocation type not implemented yet!");
157 bool RuntimeDyldMachO::resolveARMRelocation(uint8_t *LocalAddress,
158 uint64_t FinalAddress,
164 // If the relocation is PC-relative, the value to be encoded is the
165 // pointer difference.
167 Value -= FinalAddress;
168 // ARM PCRel relocations have an effective-PC offset of two instructions
169 // (four bytes in Thumb mode, 8 bytes in ARM mode).
170 // FIXME: For now, assume ARM mode.
176 llvm_unreachable("Invalid relocation type!");
177 case macho::RIT_Vanilla: {
178 // Mask in the target value a byte at a time (we don't have an alignment
179 // guarantee for the target address, so this is safest).
180 uint8_t *p = (uint8_t*)LocalAddress;
181 for (unsigned i = 0; i < Size; ++i) {
182 *p++ = (uint8_t)Value;
187 case macho::RIT_ARM_Branch24Bit: {
188 // Mask the value into the target address. We know instructions are
189 // 32-bit aligned, so we can do it all at once.
190 uint32_t *p = (uint32_t*)LocalAddress;
191 // The low two bits of the value are not encoded.
193 // Mask the value to 24 bits.
195 // FIXME: If the destination is a Thumb function (and the instruction
196 // is a non-predicated BL instruction), we need to change it to a BLX
197 // instruction instead.
199 // Insert the value into the instruction.
200 *p = (*p & ~0xffffff) | Value;
203 case macho::RIT_ARM_ThumbBranch22Bit:
204 case macho::RIT_ARM_ThumbBranch32Bit:
205 case macho::RIT_ARM_Half:
206 case macho::RIT_ARM_HalfDifference:
207 case macho::RIT_Pair:
208 case macho::RIT_Difference:
209 case macho::RIT_ARM_LocalDifference:
210 case macho::RIT_ARM_PreboundLazyPointer:
211 return Error("Relocation type not implemented yet!");
216 void RuntimeDyldMachO::processRelocationRef(unsigned SectionID,
217 relocation_iterator RelI,
219 ObjSectionToIDMap &ObjSectionToID,
220 const SymbolTableMap &Symbols,
222 const ObjectFile *OF = Obj.getObjectFile();
223 const MachOObjectFile *MachO = static_cast<const MachOObjectFile*>(OF);
224 macho::RelocationEntry RE = MachO->getRelocation(RelI->getRawDataRefImpl());
226 uint32_t RelType = MachO->getAnyRelocationType(RE);
227 RelocationValueRef Value;
228 SectionEntry &Section = Sections[SectionID];
230 bool isExtern = MachO->getPlainRelocationExternal(RE);
231 bool IsPCRel = MachO->getAnyRelocationPCRel(RE);
232 unsigned Size = MachO->getAnyRelocationLength(RE);
234 // Obtain the symbol name which is referenced in the relocation
236 RelI->getSymbol(Symbol);
237 StringRef TargetName;
238 Symbol.getName(TargetName);
239 // First search for the symbol in the local symbol table
240 SymbolTableMap::const_iterator lsi = Symbols.find(TargetName.data());
241 if (lsi != Symbols.end()) {
242 Value.SectionID = lsi->second.first;
243 Value.Addend = lsi->second.second;
245 // Search for the symbol in the global symbol table
246 SymbolTableMap::const_iterator gsi = GlobalSymbolTable.find(TargetName.data());
247 if (gsi != GlobalSymbolTable.end()) {
248 Value.SectionID = gsi->second.first;
249 Value.Addend = gsi->second.second;
251 Value.SymbolName = TargetName.data();
255 uint8_t sectionIndex = static_cast<uint8_t>(RelType & 0xFF);
256 section_iterator si = Obj.begin_sections(),
257 se = Obj.end_sections();
258 for (uint8_t i = 1; i < sectionIndex; i++) {
264 assert(si != se && "No section containing relocation!");
265 Value.SectionID = findOrEmitSection(Obj, *si, true, ObjSectionToID);
267 // FIXME: The size and type of the relocation determines if we can
268 // encode an Addend in the target location itself, and if so, how many
269 // bytes we should read in order to get it. We don't yet support doing
270 // that, and just assuming it's sizeof(intptr_t) is blatantly wrong.
271 //Value.Addend = *(const intptr_t *)Target;
273 // The MachO addend is an offset from the current section. We need it
274 // to be an offset from the destination section
275 Value.Addend += Section.ObjAddress - Sections[Value.SectionID].ObjAddress;
280 RelI->getOffset(Offset);
281 if (Arch == Triple::arm && (RelType & 0xf) == macho::RIT_ARM_Branch24Bit) {
282 // This is an ARM branch relocation, need to use a stub function.
284 // Look up for existing stub.
285 StubMap::const_iterator i = Stubs.find(Value);
286 if (i != Stubs.end())
287 resolveRelocation(Section, Offset,
288 (uint64_t)Section.Address + i->second,
289 RelType, 0, IsPCRel, Size);
291 // Create a new stub function.
292 Stubs[Value] = Section.StubOffset;
293 uint8_t *StubTargetAddr = createStubFunction(Section.Address +
295 RelocationEntry RE(SectionID, StubTargetAddr - Section.Address,
296 macho::RIT_Vanilla, Value.Addend);
297 if (Value.SymbolName)
298 addRelocationForSymbol(RE, Value.SymbolName);
300 addRelocationForSection(RE, Value.SectionID);
301 resolveRelocation(Section, Offset,
302 (uint64_t)Section.Address + Section.StubOffset,
303 RelType, 0, IsPCRel, Size);
304 Section.StubOffset += getMaxStubSize();
307 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend,
309 if (Value.SymbolName)
310 addRelocationForSymbol(RE, Value.SymbolName);
312 addRelocationForSection(RE, Value.SectionID);
317 bool RuntimeDyldMachO::isCompatibleFormat(
318 const ObjectBuffer *InputBuffer) const {
319 if (InputBuffer->getBufferSize() < 4)
321 StringRef Magic(InputBuffer->getBufferStart(), 4);
322 if (Magic == "\xFE\xED\xFA\xCE") return true;
323 if (Magic == "\xCE\xFA\xED\xFE") return true;
324 if (Magic == "\xFE\xED\xFA\xCF") return true;
325 if (Magic == "\xCF\xFA\xED\xFE") return true;
329 } // end namespace llvm