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