Jim Grosbach | e0934be | 2012-01-16 23:50:58 +0000 | [diff] [blame] | 1 | //===-- RuntimeDyldMachO.cpp - Run-time dynamic linker for MC-JIT -*- C++ -*-=// |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 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 | // Implementation of the MC-JIT runtime dynamic linker. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #define DEBUG_TYPE "dyld" |
| 15 | #include "llvm/ADT/OwningPtr.h" |
| 16 | #include "llvm/ADT/StringRef.h" |
| 17 | #include "llvm/ADT/STLExtras.h" |
Eli Bendersky | 76463fd | 2012-01-22 07:05:02 +0000 | [diff] [blame^] | 18 | #include "RuntimeDyldMachO.h" |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 19 | using namespace llvm; |
| 20 | using namespace llvm::object; |
| 21 | |
| 22 | namespace llvm { |
| 23 | |
| 24 | bool RuntimeDyldMachO:: |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 25 | resolveRelocation(uint8_t *Address, uint64_t Value, bool isPCRel, |
| 26 | unsigned Type, unsigned Size, int64_t Addend) { |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 27 | // This just dispatches to the proper target specific routine. |
| 28 | switch (CPUType) { |
| 29 | default: assert(0 && "Unsupported CPU type!"); |
| 30 | case mach::CTM_x86_64: |
| 31 | return resolveX86_64Relocation((uintptr_t)Address, (uintptr_t)Value, |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 32 | isPCRel, Type, Size, Addend); |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 33 | case mach::CTM_ARM: |
| 34 | return resolveARMRelocation((uintptr_t)Address, (uintptr_t)Value, |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 35 | isPCRel, Type, Size, Addend); |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 36 | } |
| 37 | llvm_unreachable(""); |
| 38 | } |
| 39 | |
| 40 | bool RuntimeDyldMachO:: |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 41 | resolveX86_64Relocation(uintptr_t Address, uintptr_t Value, bool isPCRel, |
| 42 | unsigned Type, unsigned Size, int64_t Addend) { |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 43 | // If the relocation is PC-relative, the value to be encoded is the |
| 44 | // pointer difference. |
| 45 | if (isPCRel) |
| 46 | // FIXME: It seems this value needs to be adjusted by 4 for an effective PC |
| 47 | // address. Is that expected? Only for branches, perhaps? |
| 48 | Value -= Address + 4; |
| 49 | |
| 50 | switch(Type) { |
| 51 | default: |
| 52 | llvm_unreachable("Invalid relocation type!"); |
Jim Grosbach | 652ca2f | 2012-01-16 23:50:49 +0000 | [diff] [blame] | 53 | case macho::RIT_X86_64_Signed1: |
| 54 | case macho::RIT_X86_64_Signed2: |
| 55 | case macho::RIT_X86_64_Signed4: |
| 56 | case macho::RIT_X86_64_Signed: |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 57 | case macho::RIT_X86_64_Unsigned: |
| 58 | case macho::RIT_X86_64_Branch: { |
Jim Grosbach | 652ca2f | 2012-01-16 23:50:49 +0000 | [diff] [blame] | 59 | Value += Addend; |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 60 | // Mask in the target value a byte at a time (we don't have an alignment |
| 61 | // guarantee for the target address, so this is safest). |
| 62 | uint8_t *p = (uint8_t*)Address; |
| 63 | for (unsigned i = 0; i < Size; ++i) { |
| 64 | *p++ = (uint8_t)Value; |
| 65 | Value >>= 8; |
| 66 | } |
| 67 | return false; |
| 68 | } |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 69 | case macho::RIT_X86_64_GOTLoad: |
| 70 | case macho::RIT_X86_64_GOT: |
| 71 | case macho::RIT_X86_64_Subtractor: |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 72 | case macho::RIT_X86_64_TLV: |
| 73 | return Error("Relocation type not implemented yet!"); |
| 74 | } |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 77 | bool RuntimeDyldMachO:: |
| 78 | resolveARMRelocation(uintptr_t Address, uintptr_t Value, bool isPCRel, |
| 79 | unsigned Type, unsigned Size, int64_t Addend) { |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 80 | // If the relocation is PC-relative, the value to be encoded is the |
| 81 | // pointer difference. |
| 82 | if (isPCRel) { |
| 83 | Value -= Address; |
| 84 | // ARM PCRel relocations have an effective-PC offset of two instructions |
| 85 | // (four bytes in Thumb mode, 8 bytes in ARM mode). |
| 86 | // FIXME: For now, assume ARM mode. |
| 87 | Value -= 8; |
| 88 | } |
| 89 | |
| 90 | switch(Type) { |
| 91 | default: |
| 92 | llvm_unreachable("Invalid relocation type!"); |
| 93 | case macho::RIT_Vanilla: { |
| 94 | llvm_unreachable("Invalid relocation type!"); |
| 95 | // Mask in the target value a byte at a time (we don't have an alignment |
| 96 | // guarantee for the target address, so this is safest). |
| 97 | uint8_t *p = (uint8_t*)Address; |
| 98 | for (unsigned i = 0; i < Size; ++i) { |
| 99 | *p++ = (uint8_t)Value; |
| 100 | Value >>= 8; |
| 101 | } |
| 102 | break; |
| 103 | } |
| 104 | case macho::RIT_ARM_Branch24Bit: { |
| 105 | // Mask the value into the target address. We know instructions are |
| 106 | // 32-bit aligned, so we can do it all at once. |
| 107 | uint32_t *p = (uint32_t*)Address; |
| 108 | // The low two bits of the value are not encoded. |
| 109 | Value >>= 2; |
| 110 | // Mask the value to 24 bits. |
| 111 | Value &= 0xffffff; |
| 112 | // FIXME: If the destination is a Thumb function (and the instruction |
| 113 | // is a non-predicated BL instruction), we need to change it to a BLX |
| 114 | // instruction instead. |
| 115 | |
| 116 | // Insert the value into the instruction. |
| 117 | *p = (*p & ~0xffffff) | Value; |
| 118 | break; |
| 119 | } |
| 120 | case macho::RIT_ARM_ThumbBranch22Bit: |
| 121 | case macho::RIT_ARM_ThumbBranch32Bit: |
| 122 | case macho::RIT_ARM_Half: |
| 123 | case macho::RIT_ARM_HalfDifference: |
| 124 | case macho::RIT_Pair: |
| 125 | case macho::RIT_Difference: |
| 126 | case macho::RIT_ARM_LocalDifference: |
| 127 | case macho::RIT_ARM_PreboundLazyPointer: |
| 128 | return Error("Relocation type not implemented yet!"); |
| 129 | } |
| 130 | return false; |
| 131 | } |
| 132 | |
| 133 | bool RuntimeDyldMachO:: |
| 134 | loadSegment32(const MachOObject *Obj, |
| 135 | const MachOObject::LoadCommandInfo *SegmentLCI, |
| 136 | const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC) { |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 137 | // FIXME: This should really be combined w/ loadSegment64. Templatized |
| 138 | // function on the 32/64 datatypes maybe? |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 139 | InMemoryStruct<macho::SegmentLoadCommand> SegmentLC; |
| 140 | Obj->ReadSegmentLoadCommand(*SegmentLCI, SegmentLC); |
| 141 | if (!SegmentLC) |
| 142 | return Error("unable to load segment load command"); |
| 143 | |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 144 | |
| 145 | SmallVector<unsigned, 16> SectionMap; |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 146 | for (unsigned SectNum = 0; SectNum != SegmentLC->NumSections; ++SectNum) { |
| 147 | InMemoryStruct<macho::Section> Sect; |
| 148 | Obj->ReadSection(*SegmentLCI, SectNum, Sect); |
| 149 | if (!Sect) |
| 150 | return Error("unable to load section: '" + Twine(SectNum) + "'"); |
| 151 | |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 152 | // Allocate memory via the MM for the section. |
| 153 | uint8_t *Buffer; |
| 154 | uint32_t SectionID = Sections.size(); |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 155 | if (Sect->Flags != 0x80000400) |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 156 | Buffer = MemMgr->allocateCodeSection(Sect->Size, Sect->Align, SectionID); |
| 157 | else |
| 158 | Buffer = MemMgr->allocateDataSection(Sect->Size, Sect->Align, SectionID); |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 159 | |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 160 | DEBUG(dbgs() << "Loading " |
| 161 | << ((Sect->Flags == 0x80000400) ? "text" : "data") |
| 162 | << " (ID #" << SectionID << ")" |
| 163 | << " '" << Sect->SegmentName << "," |
| 164 | << Sect->Name << "' of size " << Sect->Size |
| 165 | << " to address " << Buffer << ".\n"); |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 166 | |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 167 | // Copy the payload from the object file into the allocated buffer. |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 168 | uint8_t *Base = (uint8_t*)Obj->getData(SegmentLC->FileOffset, |
| 169 | SegmentLC->FileSize).data(); |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 170 | memcpy(Buffer, Base + Sect->Address, Sect->Size); |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 171 | |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 172 | // Remember what got allocated for this SectionID. |
| 173 | Sections.push_back(sys::MemoryBlock(Buffer, Sect->Size)); |
Jim Grosbach | 020f4e8 | 2012-01-16 23:50:55 +0000 | [diff] [blame] | 174 | SectionLocalMemToID[Buffer] = SectionID; |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 175 | |
| 176 | // By default, the load address of a section is its memory buffer. |
| 177 | SectionLoadAddress.push_back((uint64_t)Buffer); |
| 178 | |
| 179 | // Keep a map of object file section numbers to corresponding SectionIDs |
| 180 | // while processing the file. |
| 181 | SectionMap.push_back(SectionID); |
| 182 | } |
| 183 | |
| 184 | // Process the symbol table. |
| 185 | SmallVector<StringRef, 64> SymbolNames; |
| 186 | processSymbols32(Obj, SectionMap, SymbolNames, SymtabLC); |
| 187 | |
| 188 | // Process the relocations for each section we're loading. |
| 189 | Relocations.grow(Relocations.size() + SegmentLC->NumSections); |
| 190 | for (unsigned SectNum = 0; SectNum != SegmentLC->NumSections; ++SectNum) { |
| 191 | InMemoryStruct<macho::Section> Sect; |
| 192 | Obj->ReadSection(*SegmentLCI, SectNum, Sect); |
| 193 | if (!Sect) |
| 194 | return Error("unable to load section: '" + Twine(SectNum) + "'"); |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 195 | for (unsigned j = 0; j != Sect->NumRelocationTableEntries; ++j) { |
| 196 | InMemoryStruct<macho::RelocationEntry> RE; |
| 197 | Obj->ReadRelocationEntry(Sect->RelocationTableOffset, j, RE); |
| 198 | if (RE->Word0 & macho::RF_Scattered) |
| 199 | return Error("NOT YET IMPLEMENTED: scattered relocations."); |
| 200 | // Word0 of the relocation is the offset into the section where the |
| 201 | // relocation should be applied. We need to translate that into an |
| 202 | // offset into a function since that's our atom. |
| 203 | uint32_t Offset = RE->Word0; |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 204 | bool isExtern = (RE->Word1 >> 27) & 1; |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 205 | |
| 206 | // FIXME: Get the relocation addend from the target address. |
| 207 | // FIXME: VERY imporant for internal relocations. |
| 208 | |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 209 | // Figure out the source symbol of the relocation. If isExtern is true, |
| 210 | // this relocation references the symbol table, otherwise it references |
| 211 | // a section in the same object, numbered from 1 through NumSections |
| 212 | // (SectionBases is [0, NumSections-1]). |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 213 | uint32_t SourceNum = RE->Word1 & 0xffffff; // 24-bit value |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 214 | if (!isExtern) { |
| 215 | assert(SourceNum > 0 && "Invalid relocation section number!"); |
| 216 | unsigned SectionID = SectionMap[SourceNum - 1]; |
| 217 | unsigned TargetID = SectionMap[SectNum]; |
| 218 | DEBUG(dbgs() << "Internal relocation at Section #" |
| 219 | << TargetID << " + " << Offset |
| 220 | << " from Section #" |
| 221 | << SectionID << " (Word1: " |
| 222 | << format("0x%x", RE->Word1) << ")\n"); |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 223 | |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 224 | // Store the relocation information. It will get resolved when |
| 225 | // the section addresses are assigned. |
| 226 | Relocations[SectionID].push_back(RelocationEntry(TargetID, |
| 227 | Offset, |
| 228 | RE->Word1, |
| 229 | 0 /*Addend*/)); |
| 230 | } else { |
| 231 | StringRef SourceName = SymbolNames[SourceNum]; |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 232 | |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 233 | // Now store the relocation information. Associate it with the source |
| 234 | // symbol. Just add it to the unresolved list and let the general |
| 235 | // path post-load resolve it if we know where the symbol is. |
| 236 | UnresolvedRelocations[SourceName].push_back(RelocationEntry(SectNum, |
| 237 | Offset, |
| 238 | RE->Word1, |
| 239 | 0 /*Addend*/)); |
| 240 | DEBUG(dbgs() << "Relocation at Section #" << SectNum << " + " << Offset |
| 241 | << " from '" << SourceName << "(Word1: " |
| 242 | << format("0x%x", RE->Word1) << ")\n"); |
| 243 | } |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 244 | } |
| 245 | } |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 246 | |
| 247 | // Resolve the addresses of any symbols that were defined in this segment. |
| 248 | for (int i = 0, e = SymbolNames.size(); i != e; ++i) |
| 249 | resolveSymbol(SymbolNames[i]); |
| 250 | |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 251 | return false; |
| 252 | } |
| 253 | |
| 254 | |
| 255 | bool RuntimeDyldMachO:: |
| 256 | loadSegment64(const MachOObject *Obj, |
| 257 | const MachOObject::LoadCommandInfo *SegmentLCI, |
| 258 | const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC) { |
| 259 | InMemoryStruct<macho::Segment64LoadCommand> Segment64LC; |
| 260 | Obj->ReadSegment64LoadCommand(*SegmentLCI, Segment64LC); |
| 261 | if (!Segment64LC) |
| 262 | return Error("unable to load segment load command"); |
| 263 | |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 264 | |
| 265 | SmallVector<unsigned, 16> SectionMap; |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 266 | for (unsigned SectNum = 0; SectNum != Segment64LC->NumSections; ++SectNum) { |
| 267 | InMemoryStruct<macho::Section64> Sect; |
| 268 | Obj->ReadSection64(*SegmentLCI, SectNum, Sect); |
| 269 | if (!Sect) |
| 270 | return Error("unable to load section: '" + Twine(SectNum) + "'"); |
| 271 | |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 272 | // Allocate memory via the MM for the section. |
| 273 | uint8_t *Buffer; |
| 274 | uint32_t SectionID = Sections.size(); |
Jim Grosbach | 9339134 | 2012-01-21 00:21:53 +0000 | [diff] [blame] | 275 | unsigned Align = 1 << Sect->Align; // .o file has log2 alignment. |
Jim Grosbach | b442d7c | 2012-01-20 22:44:03 +0000 | [diff] [blame] | 276 | if (Sect->Flags == 0x80000400) |
Jim Grosbach | 9339134 | 2012-01-21 00:21:53 +0000 | [diff] [blame] | 277 | Buffer = MemMgr->allocateCodeSection(Sect->Size, Align, SectionID); |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 278 | else |
Jim Grosbach | 9339134 | 2012-01-21 00:21:53 +0000 | [diff] [blame] | 279 | Buffer = MemMgr->allocateDataSection(Sect->Size, Align, SectionID); |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 280 | |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 281 | DEBUG(dbgs() << "Loading " |
| 282 | << ((Sect->Flags == 0x80000400) ? "text" : "data") |
| 283 | << " (ID #" << SectionID << ")" |
| 284 | << " '" << Sect->SegmentName << "," |
| 285 | << Sect->Name << "' of size " << Sect->Size |
Jim Grosbach | 9339134 | 2012-01-21 00:21:53 +0000 | [diff] [blame] | 286 | << " (align " << Align << ")" |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 287 | << " to address " << Buffer << ".\n"); |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 288 | |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 289 | // Copy the payload from the object file into the allocated buffer. |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 290 | uint8_t *Base = (uint8_t*)Obj->getData(Segment64LC->FileOffset, |
| 291 | Segment64LC->FileSize).data(); |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 292 | memcpy(Buffer, Base + Sect->Address, Sect->Size); |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 293 | |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 294 | // Remember what got allocated for this SectionID. |
| 295 | Sections.push_back(sys::MemoryBlock(Buffer, Sect->Size)); |
Jim Grosbach | 020f4e8 | 2012-01-16 23:50:55 +0000 | [diff] [blame] | 296 | SectionLocalMemToID[Buffer] = SectionID; |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 297 | |
| 298 | // By default, the load address of a section is its memory buffer. |
| 299 | SectionLoadAddress.push_back((uint64_t)Buffer); |
| 300 | |
| 301 | // Keep a map of object file section numbers to corresponding SectionIDs |
| 302 | // while processing the file. |
| 303 | SectionMap.push_back(SectionID); |
| 304 | } |
| 305 | |
| 306 | // Process the symbol table. |
| 307 | SmallVector<StringRef, 64> SymbolNames; |
| 308 | processSymbols64(Obj, SectionMap, SymbolNames, SymtabLC); |
| 309 | |
| 310 | // Process the relocations for each section we're loading. |
| 311 | Relocations.grow(Relocations.size() + Segment64LC->NumSections); |
| 312 | for (unsigned SectNum = 0; SectNum != Segment64LC->NumSections; ++SectNum) { |
| 313 | InMemoryStruct<macho::Section64> Sect; |
| 314 | Obj->ReadSection64(*SegmentLCI, SectNum, Sect); |
| 315 | if (!Sect) |
| 316 | return Error("unable to load section: '" + Twine(SectNum) + "'"); |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 317 | for (unsigned j = 0; j != Sect->NumRelocationTableEntries; ++j) { |
| 318 | InMemoryStruct<macho::RelocationEntry> RE; |
| 319 | Obj->ReadRelocationEntry(Sect->RelocationTableOffset, j, RE); |
| 320 | if (RE->Word0 & macho::RF_Scattered) |
| 321 | return Error("NOT YET IMPLEMENTED: scattered relocations."); |
| 322 | // Word0 of the relocation is the offset into the section where the |
| 323 | // relocation should be applied. We need to translate that into an |
| 324 | // offset into a function since that's our atom. |
| 325 | uint32_t Offset = RE->Word0; |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 326 | bool isExtern = (RE->Word1 >> 27) & 1; |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 327 | |
| 328 | // FIXME: Get the relocation addend from the target address. |
| 329 | // FIXME: VERY imporant for internal relocations. |
| 330 | |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 331 | // Figure out the source symbol of the relocation. If isExtern is true, |
| 332 | // this relocation references the symbol table, otherwise it references |
| 333 | // a section in the same object, numbered from 1 through NumSections |
| 334 | // (SectionBases is [0, NumSections-1]). |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 335 | uint32_t SourceNum = RE->Word1 & 0xffffff; // 24-bit value |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 336 | if (!isExtern) { |
| 337 | assert(SourceNum > 0 && "Invalid relocation section number!"); |
| 338 | unsigned SectionID = SectionMap[SourceNum - 1]; |
| 339 | unsigned TargetID = SectionMap[SectNum]; |
| 340 | DEBUG(dbgs() << "Internal relocation at Section #" |
| 341 | << TargetID << " + " << Offset |
| 342 | << " from Section #" |
| 343 | << SectionID << " (Word1: " |
| 344 | << format("0x%x", RE->Word1) << ")\n"); |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 345 | |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 346 | // Store the relocation information. It will get resolved when |
| 347 | // the section addresses are assigned. |
| 348 | Relocations[SectionID].push_back(RelocationEntry(TargetID, |
| 349 | Offset, |
| 350 | RE->Word1, |
| 351 | 0 /*Addend*/)); |
| 352 | } else { |
| 353 | StringRef SourceName = SymbolNames[SourceNum]; |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 354 | |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 355 | // Now store the relocation information. Associate it with the source |
| 356 | // symbol. Just add it to the unresolved list and let the general |
| 357 | // path post-load resolve it if we know where the symbol is. |
| 358 | UnresolvedRelocations[SourceName].push_back(RelocationEntry(SectNum, |
| 359 | Offset, |
| 360 | RE->Word1, |
| 361 | 0 /*Addend*/)); |
| 362 | DEBUG(dbgs() << "Relocation at Section #" << SectNum << " + " << Offset |
| 363 | << " from '" << SourceName << "(Word1: " |
| 364 | << format("0x%x", RE->Word1) << ")\n"); |
| 365 | } |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 366 | } |
| 367 | } |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 368 | |
| 369 | // Resolve the addresses of any symbols that were defined in this segment. |
| 370 | for (int i = 0, e = SymbolNames.size(); i != e; ++i) |
| 371 | resolveSymbol(SymbolNames[i]); |
| 372 | |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 373 | return false; |
| 374 | } |
| 375 | |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 376 | bool RuntimeDyldMachO:: |
| 377 | processSymbols32(const MachOObject *Obj, |
| 378 | SmallVectorImpl<unsigned> &SectionMap, |
| 379 | SmallVectorImpl<StringRef> &SymbolNames, |
| 380 | const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC) { |
| 381 | // FIXME: Combine w/ processSymbols64. Factor 64/32 datatype and such. |
| 382 | for (unsigned i = 0; i != SymtabLC->NumSymbolTableEntries; ++i) { |
| 383 | InMemoryStruct<macho::SymbolTableEntry> STE; |
| 384 | Obj->ReadSymbolTableEntry(SymtabLC->SymbolTableOffset, i, STE); |
| 385 | if (!STE) |
| 386 | return Error("unable to read symbol: '" + Twine(i) + "'"); |
| 387 | // Get the symbol name. |
| 388 | StringRef Name = Obj->getStringAtIndex(STE->StringIndex); |
| 389 | SymbolNames.push_back(Name); |
| 390 | |
| 391 | // FIXME: Check the symbol type and flags. |
| 392 | if (STE->Type != 0xF) // external, defined in this segment. |
| 393 | continue; |
| 394 | // Flags in the upper nibble we don't care about. |
| 395 | if ((STE->Flags & 0xf) != 0x0) |
| 396 | continue; |
| 397 | |
| 398 | // Remember the symbol. |
| 399 | uint32_t SectionID = SectionMap[STE->SectionIndex - 1]; |
| 400 | SymbolTable[Name] = SymbolLoc(SectionID, STE->Value); |
| 401 | |
| 402 | DEBUG(dbgs() << "Symbol: '" << Name << "' @ " |
| 403 | << (getSectionAddress(SectionID) + STE->Value) |
| 404 | << "\n"); |
| 405 | } |
| 406 | return false; |
| 407 | } |
| 408 | |
| 409 | bool RuntimeDyldMachO:: |
| 410 | processSymbols64(const MachOObject *Obj, |
| 411 | SmallVectorImpl<unsigned> &SectionMap, |
| 412 | SmallVectorImpl<StringRef> &SymbolNames, |
| 413 | const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC) { |
| 414 | for (unsigned i = 0; i != SymtabLC->NumSymbolTableEntries; ++i) { |
| 415 | InMemoryStruct<macho::Symbol64TableEntry> STE; |
| 416 | Obj->ReadSymbol64TableEntry(SymtabLC->SymbolTableOffset, i, STE); |
| 417 | if (!STE) |
| 418 | return Error("unable to read symbol: '" + Twine(i) + "'"); |
| 419 | // Get the symbol name. |
| 420 | StringRef Name = Obj->getStringAtIndex(STE->StringIndex); |
| 421 | SymbolNames.push_back(Name); |
| 422 | |
| 423 | // FIXME: Check the symbol type and flags. |
| 424 | if (STE->Type != 0xF) // external, defined in this segment. |
| 425 | continue; |
| 426 | // Flags in the upper nibble we don't care about. |
| 427 | if ((STE->Flags & 0xf) != 0x0) |
| 428 | continue; |
| 429 | |
| 430 | // Remember the symbol. |
| 431 | uint32_t SectionID = SectionMap[STE->SectionIndex - 1]; |
| 432 | SymbolTable[Name] = SymbolLoc(SectionID, STE->Value); |
| 433 | |
| 434 | DEBUG(dbgs() << "Symbol: '" << Name << "' @ " |
| 435 | << (getSectionAddress(SectionID) + STE->Value) |
| 436 | << "\n"); |
| 437 | } |
| 438 | return false; |
| 439 | } |
| 440 | |
| 441 | // resolveSymbol - Resolve any relocations to the specified symbol if |
| 442 | // we know where it lives. |
| 443 | void RuntimeDyldMachO::resolveSymbol(StringRef Name) { |
| 444 | StringMap<SymbolLoc>::const_iterator Loc = SymbolTable.find(Name); |
| 445 | if (Loc == SymbolTable.end()) |
| 446 | return; |
| 447 | |
| 448 | RelocationList &Relocs = UnresolvedRelocations[Name]; |
| 449 | DEBUG(dbgs() << "Resolving symbol '" << Name << "'\n"); |
| 450 | for (int i = 0, e = Relocs.size(); i != e; ++i) { |
| 451 | // Change the relocation to be section relative rather than symbol |
| 452 | // relative and move it to the resolved relocation list. |
| 453 | RelocationEntry Entry = Relocs[i]; |
| 454 | Entry.Addend += Loc->second.second; |
| 455 | Relocations[Loc->second.first].push_back(Entry); |
| 456 | } |
| 457 | // FIXME: Keep a worklist of the relocations we've added so that we can |
| 458 | // resolve more selectively later. |
| 459 | Relocs.clear(); |
| 460 | } |
| 461 | |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 462 | bool RuntimeDyldMachO::loadObject(MemoryBuffer *InputBuffer) { |
| 463 | // If the linker is in an error state, don't do anything. |
| 464 | if (hasError()) |
| 465 | return true; |
| 466 | // Load the Mach-O wrapper object. |
| 467 | std::string ErrorStr; |
| 468 | OwningPtr<MachOObject> Obj( |
| 469 | MachOObject::LoadFromBuffer(InputBuffer, &ErrorStr)); |
| 470 | if (!Obj) |
| 471 | return Error("unable to load object: '" + ErrorStr + "'"); |
| 472 | |
| 473 | // Get the CPU type information from the header. |
| 474 | const macho::Header &Header = Obj->getHeader(); |
| 475 | |
| 476 | // FIXME: Error checking that the loaded object is compatible with |
| 477 | // the system we're running on. |
| 478 | CPUType = Header.CPUType; |
| 479 | CPUSubtype = Header.CPUSubtype; |
| 480 | |
| 481 | // Validate that the load commands match what we expect. |
| 482 | const MachOObject::LoadCommandInfo *SegmentLCI = 0, *SymtabLCI = 0, |
| 483 | *DysymtabLCI = 0; |
| 484 | for (unsigned i = 0; i != Header.NumLoadCommands; ++i) { |
| 485 | const MachOObject::LoadCommandInfo &LCI = Obj->getLoadCommandInfo(i); |
| 486 | switch (LCI.Command.Type) { |
| 487 | case macho::LCT_Segment: |
| 488 | case macho::LCT_Segment64: |
| 489 | if (SegmentLCI) |
| 490 | return Error("unexpected input object (multiple segments)"); |
| 491 | SegmentLCI = &LCI; |
| 492 | break; |
| 493 | case macho::LCT_Symtab: |
| 494 | if (SymtabLCI) |
| 495 | return Error("unexpected input object (multiple symbol tables)"); |
| 496 | SymtabLCI = &LCI; |
| 497 | break; |
| 498 | case macho::LCT_Dysymtab: |
| 499 | if (DysymtabLCI) |
| 500 | return Error("unexpected input object (multiple symbol tables)"); |
| 501 | DysymtabLCI = &LCI; |
| 502 | break; |
| 503 | default: |
| 504 | return Error("unexpected input object (unexpected load command"); |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | if (!SymtabLCI) |
| 509 | return Error("no symbol table found in object"); |
| 510 | if (!SegmentLCI) |
Eli Bendersky | 1eb189b | 2012-01-06 07:49:17 +0000 | [diff] [blame] | 511 | return Error("no segments found in object"); |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 512 | |
| 513 | // Read and register the symbol table data. |
| 514 | InMemoryStruct<macho::SymtabLoadCommand> SymtabLC; |
| 515 | Obj->ReadSymtabLoadCommand(*SymtabLCI, SymtabLC); |
| 516 | if (!SymtabLC) |
| 517 | return Error("unable to load symbol table load command"); |
| 518 | Obj->RegisterStringTable(*SymtabLC); |
| 519 | |
| 520 | // Read the dynamic link-edit information, if present (not present in static |
| 521 | // objects). |
| 522 | if (DysymtabLCI) { |
| 523 | InMemoryStruct<macho::DysymtabLoadCommand> DysymtabLC; |
| 524 | Obj->ReadDysymtabLoadCommand(*DysymtabLCI, DysymtabLC); |
| 525 | if (!DysymtabLC) |
| 526 | return Error("unable to load dynamic link-exit load command"); |
| 527 | |
| 528 | // FIXME: We don't support anything interesting yet. |
| 529 | // if (DysymtabLC->LocalSymbolsIndex != 0) |
| 530 | // return Error("NOT YET IMPLEMENTED: local symbol entries"); |
| 531 | // if (DysymtabLC->ExternalSymbolsIndex != 0) |
| 532 | // return Error("NOT YET IMPLEMENTED: non-external symbol entries"); |
| 533 | // if (DysymtabLC->UndefinedSymbolsIndex != SymtabLC->NumSymbolTableEntries) |
| 534 | // return Error("NOT YET IMPLEMENTED: undefined symbol entries"); |
| 535 | } |
| 536 | |
| 537 | // Load the segment load command. |
| 538 | if (SegmentLCI->Command.Type == macho::LCT_Segment) { |
| 539 | if (loadSegment32(Obj.get(), SegmentLCI, SymtabLC)) |
| 540 | return true; |
| 541 | } else { |
| 542 | if (loadSegment64(Obj.get(), SegmentLCI, SymtabLC)) |
| 543 | return true; |
| 544 | } |
| 545 | |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 546 | // Assign the addresses of the sections from the object so that any |
| 547 | // relocations to them get set properly. |
| 548 | // FIXME: This is done directly from the client at the moment. We should |
| 549 | // default the values to the local storage, at least when the target arch |
| 550 | // is the same as the host arch. |
| 551 | |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 552 | return false; |
| 553 | } |
| 554 | |
| 555 | // Assign an address to a symbol name and resolve all the relocations |
| 556 | // associated with it. |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 557 | void RuntimeDyldMachO::reassignSectionAddress(unsigned SectionID, |
| 558 | uint64_t Addr) { |
| 559 | // The address to use for relocation resolution is not |
| 560 | // the address of the local section buffer. We must be doing |
| 561 | // a remote execution environment of some sort. Re-apply any |
| 562 | // relocations referencing this section with the given address. |
| 563 | // |
| 564 | // Addr is a uint64_t because we can't assume the pointer width |
| 565 | // of the target is the same as that of the host. Just use a generic |
| 566 | // "big enough" type. |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 567 | |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 568 | SectionLoadAddress[SectionID] = Addr; |
| 569 | |
| 570 | RelocationList &Relocs = Relocations[SectionID]; |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 571 | for (unsigned i = 0, e = Relocs.size(); i != e; ++i) { |
| 572 | RelocationEntry &RE = Relocs[i]; |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 573 | uint8_t *Target = (uint8_t*)Sections[RE.SectionID].base() + RE.Offset; |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 574 | bool isPCRel = (RE.Data >> 24) & 1; |
| 575 | unsigned Type = (RE.Data >> 28) & 0xf; |
| 576 | unsigned Size = 1 << ((RE.Data >> 25) & 3); |
| 577 | |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 578 | DEBUG(dbgs() << "Resolving relocation at Section #" << RE.SectionID |
| 579 | << " + " << RE.Offset << " (" << format("%p", Target) << ")" |
| 580 | << " from Section #" << SectionID << " (" << format("%p", Addr) << ")" |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 581 | << "(" << (isPCRel ? "pcrel" : "absolute") |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 582 | << ", type: " << Type << ", Size: " << Size << ", Addend: " |
| 583 | << RE.Addend << ").\n"); |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 584 | |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 585 | resolveRelocation(Target, Addr, isPCRel, Type, Size, RE.Addend); |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 586 | } |
| 587 | } |
| 588 | |
| 589 | bool RuntimeDyldMachO::isKnownFormat(const MemoryBuffer *InputBuffer) { |
| 590 | StringRef Magic = InputBuffer->getBuffer().slice(0, 4); |
| 591 | if (Magic == "\xFE\xED\xFA\xCE") return true; |
| 592 | if (Magic == "\xCE\xFA\xED\xFE") return true; |
| 593 | if (Magic == "\xFE\xED\xFA\xCF") return true; |
| 594 | if (Magic == "\xCF\xFA\xED\xFE") return true; |
| 595 | return false; |
| 596 | } |
| 597 | |
| 598 | } // end namespace llvm |