1 //===----- RuntimeDyldMachOARM.h ---- MachO/ARM specific code. ----*- 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 #ifndef LLVM_RUNTIMEDYLDMACHOARM_H
11 #define LLVM_RUNTIMEDYLDMACHOARM_H
13 #include "../RuntimeDyldMachO.h"
15 #define DEBUG_TYPE "dyld"
19 class RuntimeDyldMachOARM
20 : public RuntimeDyldMachOCRTPBase<RuntimeDyldMachOARM> {
22 RuntimeDyldMachOARM(RTDyldMemoryManager *MM) : RuntimeDyldMachOCRTPBase(MM) {}
24 unsigned getMaxStubSize() override { return 8; }
26 unsigned getStubAlignment() override { return 4; }
29 processRelocationRef(unsigned SectionID, relocation_iterator RelI,
30 ObjectImage &ObjImg, ObjSectionToIDMap &ObjSectionToID,
31 const SymbolTableMap &Symbols, StubMap &Stubs) override {
32 const MachOObjectFile &Obj =
33 static_cast<const MachOObjectFile &>(*ObjImg.getObjectFile());
34 MachO::any_relocation_info RelInfo =
35 Obj.getRelocation(RelI->getRawDataRefImpl());
37 if (Obj.isRelocationScattered(RelInfo))
40 RelocationEntry RE(getBasicRelocationEntry(SectionID, ObjImg, RelI));
41 RelocationValueRef Value(
42 getRelocationValueRef(ObjImg, RelI, RE, ObjSectionToID, Symbols));
44 bool IsExtern = Obj.getPlainRelocationExternal(RelInfo);
45 if (!IsExtern && RE.IsPCRel)
46 makeValueAddendPCRel(Value, ObjImg, RelI);
48 if ((RE.RelType & 0xf) == MachO::ARM_RELOC_BR24)
49 processBranchRelocation(RE, Value, Stubs);
51 RE.Addend = Value.Addend;
53 addRelocationForSymbol(RE, Value.SymbolName);
55 addRelocationForSection(RE, Value.SectionID);
61 void resolveRelocation(const RelocationEntry &RE, uint64_t Value) {
62 DEBUG(dumpRelocationToResolve(RE, Value));
63 const SectionEntry &Section = Sections[RE.SectionID];
64 uint8_t *LocalAddress = Section.Address + RE.Offset;
66 // If the relocation is PC-relative, the value to be encoded is the
67 // pointer difference.
69 uint64_t FinalAddress = Section.LoadAddress + RE.Offset;
70 Value -= FinalAddress;
71 // ARM PCRel relocations have an effective-PC offset of two instructions
72 // (four bytes in Thumb mode, 8 bytes in ARM mode).
73 // FIXME: For now, assume ARM mode.
79 llvm_unreachable("Invalid relocation type!");
80 case MachO::ARM_RELOC_VANILLA:
81 writeBytesUnaligned(LocalAddress, Value, 1 << RE.Size);
83 case MachO::ARM_RELOC_BR24: {
84 // Mask the value into the target address. We know instructions are
85 // 32-bit aligned, so we can do it all at once.
86 uint32_t *p = (uint32_t *)LocalAddress;
87 // The low two bits of the value are not encoded.
89 // Mask the value to 24 bits.
90 uint64_t FinalValue = Value & 0xffffff;
91 // Check for overflow.
92 if (Value != FinalValue) {
93 Error("ARM BR24 relocation out of range.");
96 // FIXME: If the destination is a Thumb function (and the instruction
97 // is a non-predicated BL instruction), we need to change it to a BLX
98 // instruction instead.
100 // Insert the value into the instruction.
101 *p = (*p & ~0xffffff) | FinalValue;
104 case MachO::ARM_THUMB_RELOC_BR22:
105 case MachO::ARM_THUMB_32BIT_BRANCH:
106 case MachO::ARM_RELOC_HALF:
107 case MachO::ARM_RELOC_HALF_SECTDIFF:
108 case MachO::ARM_RELOC_PAIR:
109 case MachO::ARM_RELOC_SECTDIFF:
110 case MachO::ARM_RELOC_LOCAL_SECTDIFF:
111 case MachO::ARM_RELOC_PB_LA_PTR:
112 Error("Relocation type not implemented yet!");
117 void finalizeSection(ObjectImage &ObjImg, unsigned SectionID,
118 const SectionRef &Section) {}
121 void processBranchRelocation(const RelocationEntry &RE,
122 const RelocationValueRef &Value,
124 // This is an ARM branch relocation, need to use a stub function.
125 // Look up for existing stub.
126 SectionEntry &Section = Sections[RE.SectionID];
127 RuntimeDyldMachO::StubMap::const_iterator i = Stubs.find(Value);
129 if (i != Stubs.end()) {
130 Addr = Section.Address + i->second;
132 // Create a new stub function.
133 Stubs[Value] = Section.StubOffset;
134 uint8_t *StubTargetAddr =
135 createStubFunction(Section.Address + Section.StubOffset);
136 RelocationEntry StubRE(RE.SectionID, StubTargetAddr - Section.Address,
137 MachO::GENERIC_RELOC_VANILLA, Value.Addend);
138 if (Value.SymbolName)
139 addRelocationForSymbol(StubRE, Value.SymbolName);
141 addRelocationForSection(StubRE, Value.SectionID);
142 Addr = Section.Address + Section.StubOffset;
143 Section.StubOffset += getMaxStubSize();
145 RelocationEntry TargetRE(Value.SectionID, RE.Offset, RE.RelType, 0,
146 RE.IsPCRel, RE.Size);
147 resolveRelocation(TargetRE, (uint64_t)Addr);
154 #endif // LLVM_RUNTIMEDYLDMACHOARM_H