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_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDMACHOARM_H
11 #define LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDMACHOARM_H
13 #include "../RuntimeDyldMachO.h"
15 #define DEBUG_TYPE "dyld"
19 class RuntimeDyldMachOARM
20 : public RuntimeDyldMachOCRTPBase<RuntimeDyldMachOARM> {
22 typedef RuntimeDyldMachOCRTPBase<RuntimeDyldMachOARM> ParentT;
26 typedef uint32_t TargetPtrT;
28 RuntimeDyldMachOARM(RTDyldMemoryManager *MM) : RuntimeDyldMachOCRTPBase(MM) {}
30 unsigned getMaxStubSize() override { return 8; }
32 unsigned getStubAlignment() override { return 4; }
34 int64_t decodeAddend(const RelocationEntry &RE) const {
35 const SectionEntry &Section = Sections[RE.SectionID];
36 uint8_t *LocalAddress = Section.Address + RE.Offset;
40 return memcpyAddend(RE);
41 case MachO::ARM_RELOC_BR24: {
43 memcpy(&Temp, LocalAddress, 4);
44 Temp &= 0x00ffffff; // Mask out the opcode.
45 // Now we've got the shifted immediate, shift by 2, sign extend and ret.
46 return SignExtend32<26>(Temp << 2);
52 processRelocationRef(unsigned SectionID, relocation_iterator RelI,
53 ObjectImage &ObjImg, ObjSectionToIDMap &ObjSectionToID,
54 const SymbolTableMap &Symbols, StubMap &Stubs) override {
55 const MachOObjectFile &Obj =
56 static_cast<const MachOObjectFile &>(*ObjImg.getObjectFile());
57 MachO::any_relocation_info RelInfo =
58 Obj.getRelocation(RelI->getRawDataRefImpl());
60 if (Obj.isRelocationScattered(RelInfo))
63 RelocationEntry RE(getRelocationEntry(SectionID, ObjImg, RelI));
64 RE.Addend = decodeAddend(RE);
65 RelocationValueRef Value(
66 getRelocationValueRef(ObjImg, RelI, RE, ObjSectionToID, Symbols));
69 makeValueAddendPCRel(Value, ObjImg, RelI, 8);
71 if ((RE.RelType & 0xf) == MachO::ARM_RELOC_BR24)
72 processBranchRelocation(RE, Value, Stubs);
74 RE.Addend = Value.Offset;
76 addRelocationForSymbol(RE, Value.SymbolName);
78 addRelocationForSection(RE, Value.SectionID);
84 void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override {
85 DEBUG(dumpRelocationToResolve(RE, Value));
86 const SectionEntry &Section = Sections[RE.SectionID];
87 uint8_t *LocalAddress = Section.Address + RE.Offset;
89 // If the relocation is PC-relative, the value to be encoded is the
90 // pointer difference.
92 uint64_t FinalAddress = Section.LoadAddress + RE.Offset;
93 Value -= FinalAddress;
94 // ARM PCRel relocations have an effective-PC offset of two instructions
95 // (four bytes in Thumb mode, 8 bytes in ARM mode).
96 // FIXME: For now, assume ARM mode.
100 switch (RE.RelType) {
102 llvm_unreachable("Invalid relocation type!");
103 case MachO::ARM_RELOC_VANILLA:
104 writeBytesUnaligned(Value, LocalAddress, 1 << RE.Size);
106 case MachO::ARM_RELOC_BR24: {
107 // Mask the value into the target address. We know instructions are
108 // 32-bit aligned, so we can do it all at once.
109 uint32_t *p = (uint32_t *)LocalAddress;
110 // The low two bits of the value are not encoded.
112 // Mask the value to 24 bits.
113 uint64_t FinalValue = Value & 0xffffff;
114 // FIXME: If the destination is a Thumb function (and the instruction
115 // is a non-predicated BL instruction), we need to change it to a BLX
116 // instruction instead.
118 // Insert the value into the instruction.
119 *p = (*p & ~0xffffff) | FinalValue;
122 case MachO::ARM_THUMB_RELOC_BR22:
123 case MachO::ARM_THUMB_32BIT_BRANCH:
124 case MachO::ARM_RELOC_HALF:
125 case MachO::ARM_RELOC_HALF_SECTDIFF:
126 case MachO::ARM_RELOC_PAIR:
127 case MachO::ARM_RELOC_SECTDIFF:
128 case MachO::ARM_RELOC_LOCAL_SECTDIFF:
129 case MachO::ARM_RELOC_PB_LA_PTR:
130 Error("Relocation type not implemented yet!");
135 void finalizeSection(ObjectImage &ObjImg, unsigned SectionID,
136 const SectionRef &Section) {}
139 void processBranchRelocation(const RelocationEntry &RE,
140 const RelocationValueRef &Value,
142 // This is an ARM branch relocation, need to use a stub function.
143 // Look up for existing stub.
144 SectionEntry &Section = Sections[RE.SectionID];
145 RuntimeDyldMachO::StubMap::const_iterator i = Stubs.find(Value);
147 if (i != Stubs.end()) {
148 Addr = Section.Address + i->second;
150 // Create a new stub function.
151 Stubs[Value] = Section.StubOffset;
152 uint8_t *StubTargetAddr =
153 createStubFunction(Section.Address + Section.StubOffset);
154 RelocationEntry StubRE(RE.SectionID, StubTargetAddr - Section.Address,
155 MachO::GENERIC_RELOC_VANILLA, Value.Offset, false,
157 if (Value.SymbolName)
158 addRelocationForSymbol(StubRE, Value.SymbolName);
160 addRelocationForSection(StubRE, Value.SectionID);
161 Addr = Section.Address + Section.StubOffset;
162 Section.StubOffset += getMaxStubSize();
164 RelocationEntry TargetRE(RE.SectionID, RE.Offset, RE.RelType, 0,
165 RE.IsPCRel, RE.Size);
166 resolveRelocation(TargetRE, (uint64_t)Addr);