llvm.org GIT mirror llvm / 59a8b5a
Revert my last two commits while I debug what is wrong in a big endian host. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@179303 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 7 years ago
5 changed file(s) with 91 addition(s) and 132 deletion(s). Raw diff Collapse all Expand all
111111 MachOInt32 Reserved3;
112112 };
113113
114 struct MachOInt24 {
115 uint8_t bytes[3];
116 operator uint32_t() const {
117 return (bytes[2] << 24) | (bytes[1] << 16) | bytes[0];
118 }
119 };
120
121114 template
122115 struct RelocationEntry {
123116 LLVM_MACHOB_IMPORT_TYPES(TargetEndianness)
124 MachOInt32 Address;
125 MachOInt24 SymbolNum;
126 unsigned PCRel : 1;
127 unsigned Length : 2;
128 unsigned External : 1;
129 unsigned Type : 4;
130 };
131
132 template
133 struct ScatteredRelocationEntry {
134 LLVM_MACHOB_IMPORT_TYPES(TargetEndianness)
135 unsigned Address : 24;
136 unsigned Type : 4;
137 unsigned Length : 2;
138 unsigned PCRel : 1;
139 unsigned Scattered : 1;
140 MachOInt32 Value;
117 MachOInt32 Word0;
118 MachOInt32 Word1;
141119 };
142120
143121 template
227205 SymbolTableEntryBase;
228206 typedef MachOFormat::SymtabLoadCommand SymtabLoadCommand;
229207 typedef MachOFormat::RelocationEntry RelocationEntry;
230 typedef MachOFormat::ScatteredRelocationEntry
231 ScatteredRelocationEntry;
232208 typedef MachOFormat::SectionBase SectionBase;
233209 typedef MachOFormat::LoadCommand LoadCommand;
234210 typedef MachOFormat::Header Header;
266242 const Header *getHeader() const;
267243 unsigned getHeaderSize() const;
268244 StringRef getData(size_t Offset, size_t Size) const;
269 const RelocationEntry *getRelocation(DataRefImpl Rel) const;
270 bool isScattered(const RelocationEntry *RE) const;
271 bool isPCRel(const RelocationEntry *RE) const;
272 unsigned getLength(const RelocationEntry *RE) const;
273 unsigned getType(const RelocationEntry *RE) const;
274245
275246 static inline bool classof(const Binary *v) {
276247 return v->isMachO();
507478 const Section *Sect = getSection(Sections[Rel.d.b]);
508479 uint64_t SectAddress = Sect->Address;
509480 const RelocationEntry *RE = getRelocation(Rel);
481 unsigned Arch = getArch();
482 bool isScattered = (Arch != Triple::x86_64) &&
483 (RE->Word0 & macho::RF_Scattered);
510484
511485 uint64_t RelAddr;
512 if (isScattered(RE))
513 RelAddr = RE->Address & 0xFFFFFF;
486 if (isScattered)
487 RelAddr = RE->Word0 & 0xFFFFFF;
514488 else
515 RelAddr = RE->Address;
489 RelAddr = RE->Word0;
516490
517491 Res = SectAddress + RelAddr;
518492 return object_error::success;
523497 MachOObjectFile::getRelocationOffset(DataRefImpl Rel,
524498 uint64_t &Res) const {
525499 const RelocationEntry *RE = getRelocation(Rel);
526 if (isScattered(RE))
527 Res = RE->Address & 0xFFFFFF;
500
501 unsigned Arch = getArch();
502 bool isScattered = (Arch != Triple::x86_64) &&
503 (RE->Word0 & macho::RF_Scattered);
504 if (isScattered)
505 Res = RE->Word0 & 0xFFFFFF;
528506 else
529 Res = RE->Address;
507 Res = RE->Word0;
530508 return object_error::success;
531509 }
532510
535513 MachOObjectFile::getRelocationSymbol(DataRefImpl Rel,
536514 SymbolRef &Res) const {
537515 const RelocationEntry *RE = getRelocation(Rel);
538 uint32_t SymbolIdx = RE->SymbolNum;
539 bool isExtern = RE->External;
516 uint32_t SymbolIdx = RE->Word1 & 0xffffff;
517 bool isExtern = (RE->Word1 >> 27) & 1;
540518
541519 DataRefImpl Sym;
542520 moveToNextSymbol(Sym);
556534 error_code MachOObjectFile::getRelocationType(DataRefImpl Rel,
557535 uint64_t &Res) const {
558536 const RelocationEntry *RE = getRelocation(Rel);
559 Res = getType(RE);
537 Res = RE->Word0;
538 Res <<= 32;
539 Res |= RE->Word1;
560540 return object_error::success;
561541 }
562542
569549 const RelocationEntry *RE = getRelocation(Rel);
570550
571551 unsigned Arch = getArch();
572
573 unsigned r_type = getType(RE);
552 bool isScattered = (Arch != Triple::x86_64) &&
553 (RE->Word0 & macho::RF_Scattered);
554
555 unsigned r_type;
556 if (isScattered)
557 r_type = (RE->Word0 >> 24) & 0xF;
558 else
559 r_type = (RE->Word1 >> 28) & 0xF;
574560
575561 switch (Arch) {
576562 case Triple::x86: {
663649 const RelocationEntry *RE = getRelocation(Rel);
664650
665651 unsigned Arch = getArch();
666 bool IsScattered = isScattered(RE);
652 bool isScattered = (Arch != Triple::x86_64) &&
653 (RE->Word0 & macho::RF_Scattered);
667654
668655 std::string fmtbuf;
669656 raw_string_ostream fmt(fmtbuf);
670657
671 unsigned Type = getType(RE);
672 bool IsPCRel = isPCRel(RE);
658 unsigned Type;
659 if (isScattered)
660 Type = (RE->Word0 >> 24) & 0xF;
661 else
662 Type = (RE->Word1 >> 28) & 0xF;
663
664 bool isPCRel;
665 if (isScattered)
666 isPCRel = ((RE->Word0 >> 30) & 1);
667 else
668 isPCRel = ((RE->Word1 >> 24) & 1);
673669
674670 // Determine any addends that should be displayed with the relocation.
675671 // These require decoding the relocation type, which is triple-specific.
676672
677673 // X86_64 has entirely custom relocation types.
678674 if (Arch == Triple::x86_64) {
679 bool isPCRel = RE->PCRel;
675 bool isPCRel = ((RE->Word1 >> 24) & 1);
680676
681677 switch (Type) {
682678 case macho::RIT_X86_64_GOTLoad: // X86_64_RELOC_GOT_LOAD
694690 // X86_64_SUBTRACTOR must be followed by a relocation of type
695691 // X86_64_RELOC_UNSIGNED.
696692 // NOTE: Scattered relocations don't exist on x86_64.
697 unsigned RType = RENext->Type;
693 unsigned RType = (RENext->Word1 >> 28) & 0xF;
698694 if (RType != 0)
699695 report_fatal_error("Expected X86_64_RELOC_UNSIGNED after "
700696 "X86_64_RELOC_SUBTRACTOR.");
741737 // X86 sect diff's must be followed by a relocation of type
742738 // GENERIC_RELOC_PAIR.
743739 bool isNextScattered = (Arch != Triple::x86_64) &&
744 (RENext->Address & macho::RF_Scattered);
740 (RENext->Word0 & macho::RF_Scattered);
745741 unsigned RType;
746742 if (isNextScattered)
747 RType = (RENext->Address >> 24) & 0xF;
743 RType = (RENext->Word0 >> 24) & 0xF;
748744 else
749 RType = RENext->Type;
745 RType = (RENext->Word1 >> 28) & 0xF;
750746 if (RType != 1)
751747 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
752748 "GENERIC_RELOC_SECTDIFF.");
770766 // X86 sect diff's must be followed by a relocation of type
771767 // GENERIC_RELOC_PAIR.
772768 bool isNextScattered = (Arch != Triple::x86_64) &&
773 (RENext->Address & macho::RF_Scattered);
769 (RENext->Word0 & macho::RF_Scattered);
774770 unsigned RType;
775771 if (isNextScattered)
776 RType = (RENext->Address >> 24) & 0xF;
772 RType = (RENext->Word0 >> 24) & 0xF;
777773 else
778 RType = RENext->Type;
774 RType = (RENext->Word1 >> 28) & 0xF;
779775 if (RType != 1)
780776 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
781777 "GENERIC_RELOC_LOCAL_SECTDIFF.");
788784 case macho::RIT_Generic_TLV: {
789785 printRelocationTargetName(RE, fmt);
790786 fmt << "@TLV";
791 if (IsPCRel) fmt << "P";
787 if (isPCRel) fmt << "P";
792788 break;
793789 }
794790 default:
801797 // Half relocations steal a bit from the length field to encode
802798 // whether this is an upper16 or a lower16 relocation.
803799 bool isUpper;
804 if (IsScattered)
805 isUpper = (RE->Address >> 28) & 1;
800 if (isScattered)
801 isUpper = (RE->Word0 >> 28) & 1;
806802 else
807 isUpper = (RE->Length >> 1) & 1;
803 isUpper = (RE->Word1 >> 25) & 1;
808804
809805 if (isUpper)
810806 fmt << ":upper16:(";
819815 // ARM half relocs must be followed by a relocation of type
820816 // ARM_RELOC_PAIR.
821817 bool isNextScattered = (Arch != Triple::x86_64) &&
822 (RENext->Address & macho::RF_Scattered);
818 (RENext->Word0 & macho::RF_Scattered);
823819 unsigned RType;
824820 if (isNextScattered)
825 RType = (RENext->Address >> 24) & 0xF;
821 RType = (RENext->Word0 >> 24) & 0xF;
826822 else
827 RType = RENext->Type;
823 RType = (RENext->Word1 >> 28) & 0xF;
828824
829825 if (RType != 1)
830826 report_fatal_error("Expected ARM_RELOC_PAIR after "
863859 MachOObjectFile::getRelocationHidden(DataRefImpl Rel,
864860 bool &Result) const {
865861 const RelocationEntry *RE = getRelocation(Rel);
862
866863 unsigned Arch = getArch();
867 unsigned Type = getType(RE);
864 bool isScattered = (Arch != Triple::x86_64) &&
865 (RE->Word0 & macho::RF_Scattered);
866 unsigned Type;
867 if (isScattered)
868 Type = (RE->Word0 >> 24) & 0xF;
869 else
870 Type = (RE->Word1 >> 28) & 0xF;
868871
869872 Result = false;
870873
880883 RelPrev.d.a--;
881884 const RelocationEntry *REPrev = getRelocation(RelPrev);
882885
883 unsigned PrevType = REPrev->Type;
886 unsigned PrevType = (REPrev->Word1 >> 28) & 0xF;
884887
885888 if (PrevType == macho::RIT_X86_64_Subtractor) Result = true;
886889 }
7979 return ObjectFile::getData().substr(Offset, Size);
8080 }
8181
82 const MachOObjectFileBase::RelocationEntry *
83 MachOObjectFileBase::getRelocation(DataRefImpl Rel) const {
84 if (const MachOObjectFile32Le *O = dyn_cast(this))
85 return O->getRelocation(Rel);
86 const MachOObjectFile64Le *O = dyn_cast(this);
87 return O->getRelocation(Rel);
88 }
89
90 bool MachOObjectFileBase::isScattered(const RelocationEntry *RE) const {
91 unsigned Arch = getArch();
92 return (Arch != Triple::x86_64) && (RE->Address & macho::RF_Scattered);
93 }
94
95 bool MachOObjectFileBase::isPCRel(const RelocationEntry *RE) const {
96 if (isScattered(RE)) {
97 const ScatteredRelocationEntry *SRE =
98 reinterpret_cast(RE);
99 return SRE->PCRel;
100 }
101 return RE->PCRel;
102 }
103
104 unsigned MachOObjectFileBase::getLength(const RelocationEntry *RE) const {
105 if (isScattered(RE)) {
106 const ScatteredRelocationEntry *SRE =
107 reinterpret_cast(RE);
108 return SRE->Length;
109 }
110 return RE->Length;
111 }
112
113 unsigned MachOObjectFileBase::getType(const RelocationEntry *RE) const {
114 if (isScattered(RE)) {
115 const ScatteredRelocationEntry *SRE =
116 reinterpret_cast(RE);
117 return SRE->Type;
118 }
119 return RE->Type;
120 }
121
12282 ObjectFile *ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer) {
12383 StringRef Magic = Buffer->getBuffer().slice(0, 4);
12484 error_code ec;
474434 void
475435 MachOObjectFileBase::printRelocationTargetName(const RelocationEntry *RE,
476436 raw_string_ostream &fmt) const {
437 unsigned Arch = getArch();
438 bool isScattered = (Arch != Triple::x86_64) &&
439 (RE->Word0 & macho::RF_Scattered);
440
477441 // Target of a scattered relocation is an address. In the interest of
478442 // generating pretty output, scan through the symbol table looking for a
479443 // symbol that aligns with that address. If we find one, print it.
480444 // Otherwise, we just print the hex address of the target.
481 if (isScattered(RE)) {
482 uint32_t Val = RE->SymbolNum;
445 if (isScattered) {
446 uint32_t Val = RE->Word1;
483447
484448 error_code ec;
485449 for (symbol_iterator SI = begin_symbols(), SE = end_symbols(); SI != SE;
521485 }
522486
523487 StringRef S;
524 bool isExtern = RE->External;
525 uint32_t Val = RE->Address;
488 bool isExtern = (RE->Word1 >> 27) & 1;
489 uint32_t Val = RE->Word1 & 0xFFFFFF;
526490
527491 if (isExtern) {
528492 symbol_iterator SI = begin_symbols();
2525
2626 MACHO-I386: Relocations [
2727 MACHO-I386-NEXT: Section __text {
28 MACHO-I386-NEXT: 0x18 1 2 1 GENERIC_RELOC_VANILLA 0 _SomeOtherFunction
29 MACHO-I386-NEXT: 0x13 1 2 1 GENERIC_RELOC_VANILLA 0 _puts
30 MACHO-I386-NEXT: 0xB 0 2 n/a GENERIC_RELOC_LOCAL_SECTDIFF 1 _main
31 MACHO-I386-NEXT: 0x0 0 2 n/a GENERIC_RELOC_PAIR 1 _main
28 MACHO-I386-NEXT: 0x18 GENERIC_RELOC_VANILLA _SomeOtherFunction 0x0
29 MACHO-I386-NEXT: 0x13 GENERIC_RELOC_VANILLA _puts 0x0
30 MACHO-I386-NEXT: 0xB GENERIC_RELOC_LOCAL_SECTDIFF _main 0x{{[0-9A-F]+}}
31 MACHO-I386-NEXT: 0x0 GENERIC_RELOC_PAIR _main 0x{{[0-9A-F]+}}
3232 MACHO-I386-NEXT: }
3333 MACHO-I386-NEXT: ]
3434
3535 MACHO-X86-64: Relocations [
3636 MACHO-X86-64-NEXT: Section __text {
37 MACHO-X86-64-NEXT: 0xE 1 2 1 X86_64_RELOC_BRANCH 0 _SomeOtherFunction
38 MACHO-X86-64-NEXT: 0x9 1 2 1 X86_64_RELOC_BRANCH 0 _puts
39 MACHO-X86-64-NEXT: 0x4 1 2 1 X86_64_RELOC_SIGNED 0 L_.str
37 MACHO-X86-64-NEXT: 0xE X86_64_RELOC_BRANCH _SomeOtherFunction 0x0
38 MACHO-X86-64-NEXT: 0x9 X86_64_RELOC_BRANCH _puts 0x0
39 MACHO-X86-64-NEXT: 0x4 X86_64_RELOC_SIGNED L_.str 0x0
4040 MACHO-X86-64-NEXT: }
4141 MACHO-X86-64-NEXT:]
152152 MACHO-I386-NEXT: Reserved1: 0x0
153153 MACHO-I386-NEXT: Reserved2: 0x0
154154 MACHO-I386-NEXT: Relocations [
155 MACHO-I386-NEXT: 0x18 1 2 1 GENERIC_RELOC_VANILLA 0 _SomeOtherFunction
156 MACHO-I386-NEXT: 0x13 1 2 1 GENERIC_RELOC_VANILLA 0 _puts
157 MACHO-I386-NEXT: 0xB 0 2 n/a GENERIC_RELOC_LOCAL_SECTDIFF 1 _main
158 MACHO-I386-NEXT: 0x0 0 2 n/a GENERIC_RELOC_PAIR 1 _main
155 MACHO-I386-NEXT: 0x18 GENERIC_RELOC_VANILLA _SomeOtherFunction 0x0
156 MACHO-I386-NEXT: 0x13 GENERIC_RELOC_VANILLA _puts 0x0
157 MACHO-I386-NEXT: 0xB GENERIC_RELOC_LOCAL_SECTDIFF _main 0x{{[0-9A-F]+}}
158 MACHO-I386-NEXT: 0x0 GENERIC_RELOC_PAIR _main 0x{{[0-9A-F]+}}
159159 MACHO-I386-NEXT: ]
160160 MACHO-I386-NEXT: Symbols [
161161 MACHO-I386-NEXT: Symbol {
195195 MACHO-X86-64-NEXT: Reserved1: 0x0
196196 MACHO-X86-64-NEXT: Reserved2: 0x0
197197 MACHO-X86-64-NEXT: Relocations [
198 MACHO-X86-64-NEXT: 0xE 1 2 1 X86_64_RELOC_BRANCH 0 _SomeOtherFunction
199 MACHO-X86-64-NEXT: 0x9 1 2 1 X86_64_RELOC_BRANCH 0 _puts
200 MACHO-X86-64-NEXT: 0x4 1 2 1 X86_64_RELOC_SIGNED 0 L_.str
198 MACHO-X86-64-NEXT: 0xE X86_64_RELOC_BRANCH _SomeOtherFunction 0x0
199 MACHO-X86-64-NEXT: 0x9 X86_64_RELOC_BRANCH _puts 0x0
200 MACHO-X86-64-NEXT: 0x4 X86_64_RELOC_SIGNED L_.str 0x0
201201 MACHO-X86-64-NEXT: ]
202202 MACHO-X86-64-NEXT: Symbols [
203203 MACHO-X86-64-NEXT: Symbol {
329329 relocation_iterator RelI) {
330330 uint64_t Offset;
331331 SmallString<32> RelocName;
332 int64_t Info;
332333 StringRef SymbolName;
333334 SymbolRef Symbol;
334335 if (error(RelI->getOffset(Offset))) return;
335336 if (error(RelI->getTypeName(RelocName))) return;
337 if (error(RelI->getAdditionalInfo(Info))) return;
336338 if (error(RelI->getSymbol(Symbol))) return;
337339 if (error(Symbol.getName(SymbolName))) return;
338340
339 DataRefImpl DR = RelI->getRawDataRefImpl();
340 const MachOObjectFileBase::RelocationEntry *RE = Obj->getRelocation(DR);
341 bool IsScattered = Obj->isScattered(RE);
342
343341 raw_ostream& OS = W.startLine();
344342 OS << W.hex(Offset)
345 << " " << Obj->isPCRel(RE)
346 << " " << Obj->getLength(RE);
347 if (IsScattered)
348 OS << " n/a";
349 else
350 OS << " " << RE->External;
351 OS << " " << RelocName
352 << " " << IsScattered
343 << " " << RelocName
353344 << " " << (SymbolName.size() > 0 ? SymbolName : "-")
345 << " " << W.hex(Info)
354346 << "\n";
355347 }
356348