llvm.org GIT mirror llvm / 956ca72
Clarify getRelocationAddress x getRelocationOffset a bit. getRelocationAddress is for dynamic libraries and executables, getRelocationOffset for relocatable objects. Mark the getRelocationAddress of COFF and MachO as not implemented yet. Add a test of ELF's. llvm-readobj -r now prints the same values as readelf -r. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@180259 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 7 years ago
11 changed file(s) with 61 addition(s) and 65 deletion(s). Raw diff Collapse all Expand all
606606 mutable const char *dt_soname;
607607
608608 private:
609 uint64_t getROffset(DataRefImpl Rel) const;
610
609611 // Records for each version index the corresponding Verdef or Vernaux entry.
610612 // This is filled the first time LoadVersionMap() is called.
611613 class VersionMapEntry : public PointerIntPair {
15201522 template
15211523 error_code ELFObjectFile::getRelocationAddress(DataRefImpl Rel,
15221524 uint64_t &Result) const {
1523 uint64_t offset;
1525 assert((Header->e_type == ELF::ET_EXEC || Header->e_type == ELF::ET_DYN) &&
1526 "Only executable and shared objects files have addresses");
1527 Result = getROffset(Rel);
1528 return object_error::success;
1529 }
1530
1531 template
1532 error_code ELFObjectFile::getRelocationOffset(DataRefImpl Rel,
1533 uint64_t &Result) const {
1534 assert(Header->e_type == ELF::ET_REL &&
1535 "Only relocatable object files have relocation offsets");
1536 Result = getROffset(Rel);
1537 return object_error::success;
1538 }
1539
1540 template
1541 uint64_t ELFObjectFile::getROffset(DataRefImpl Rel) const {
15241542 const Elf_Shdr *sec = getSection(Rel.w.b);
15251543 switch (sec->sh_type) {
1526 default :
1527 report_fatal_error("Invalid section type in Rel!");
1528 case ELF::SHT_REL : {
1529 offset = getRel(Rel)->r_offset;
1530 break;
1531 }
1532 case ELF::SHT_RELA : {
1533 offset = getRela(Rel)->r_offset;
1534 break;
1535 }
1536 }
1537
1538 Result = offset;
1539 return object_error::success;
1540 }
1541
1542 template
1543 error_code ELFObjectFile::getRelocationOffset(DataRefImpl Rel,
1544 uint64_t &Result) const {
1545 uint64_t offset;
1546 const Elf_Shdr *sec = getSection(Rel.w.b);
1547 switch (sec->sh_type) {
1548 default :
1549 report_fatal_error("Invalid section type in Rel!");
1550 case ELF::SHT_REL : {
1551 offset = getRel(Rel)->r_offset;
1552 break;
1553 }
1554 case ELF::SHT_RELA : {
1555 offset = getRela(Rel)->r_offset;
1556 break;
1557 }
1558 }
1559
1560 Result = offset - sec->sh_addr;
1561 return object_error::success;
1544 default:
1545 report_fatal_error("Invalid section type in Rel!");
1546 case ELF::SHT_REL:
1547 return getRel(Rel)->r_offset;
1548 case ELF::SHT_RELA:
1549 return getRela(Rel)->r_offset;
1550 }
15621551 }
15631552
15641553 template
132132 int64_t Addend;
133133 R.getAdditionalInfo(Addend);
134134 uint64_t Address;
135 R.getAddress(Address);
135 R.getOffset(Address);
136136 return RelocToApply(Value + Addend - Address, 4);
137137 }
138138
150150 int64_t Addend;
151151 R.getAdditionalInfo(Addend);
152152 uint64_t Address;
153 R.getAddress(Address);
153 R.getOffset(Address);
154154 return RelocToApply(Value + Addend - Address, 4);
155155 }
156156 RelocToApply visitELF_X86_64_32(RelocationRef R, uint64_t Value) {
571571 reloc_e = i->end_relocations();
572572 reloc_i != reloc_e; reloc_i.increment(ec)) {
573573 uint64_t Address;
574 reloc_i->getAddress(Address);
574 reloc_i->getOffset(Address);
575575 uint64_t Type;
576576 reloc_i->getType(Type);
577577 uint64_t SymAddr = 0;
704704 }
705705 error_code COFFObjectFile::getRelocationAddress(DataRefImpl Rel,
706706 uint64_t &Res) const {
707 Res = toRel(Rel)->VirtualAddress;
708 return object_error::success;
707 report_fatal_error("getRelocationAddress not implemented in COFFObjectFile");
709708 }
710709 error_code COFFObjectFile::getRelocationOffset(DataRefImpl Rel,
711710 uint64_t &Res) const {
788788
789789 error_code
790790 MachOObjectFile::getRelocationAddress(DataRefImpl Rel, uint64_t &Res) const {
791 uint64_t SectAddress;
792 DataRefImpl Sec;
793 Sec.d.a = Rel.d.b;
794 if (is64Bit()) {
795 macho::Section64 Sect = getSection64(Sec);
796 SectAddress = Sect.Address;
797 } else {
798 macho::Section Sect = getSection(Sec);
799 SectAddress = Sect.Address;
800 }
801
802 macho::RelocationEntry RE = getRelocation(Rel);
803 uint64_t RelAddr = getAnyRelocationAddress(RE);
804 Res = SectAddress + RelAddr;
805 return object_error::success;
791 report_fatal_error("getRelocationAddress not implemented in MachOObjectFile");
806792 }
807793
808794 error_code MachOObjectFile::getRelocationOffset(DataRefImpl Rel,
0 RUN: llvm-readobj -r -expand-relocs %p/Inputs/hello-world.elf-x86-64 \
1 RUN: | FileCheck %s
2
3 // CHECK: Relocations [
4 // CHECK: Section (11) .plt {
5 // CHECK-NEXT: Relocation {
6 // CHECK-NEXT: Offset: 0x4018F8
7 // CHECK-NEXT: Type: R_X86_64_JUMP_SLOT (7)
8 // CHECK-NEXT: Symbol: __libc_start_main
9 // CHECK-NEXT: Info: 0x0
10 // CHECK-NEXT: }
11 // CHECK-NEXT: Relocation {
12 // CHECK-NEXT: Offset: 0x401900
13 // CHECK-NEXT: Type: R_X86_64_JUMP_SLOT (7)
14 // CHECK-NEXT: Symbol: puts
15 // CHECK-NEXT: Info: 0x0
16 // CHECK-NEXT: }
17 // CHECK-NEXT: }
342342 for (relocation_iterator RI = Sections[SectIdx].begin_relocations(),
343343 RE = Sections[SectIdx].end_relocations(); RI != RE; RI.increment(ec)) {
344344 uint64_t RelocOffset, SectionAddress;
345 RI->getAddress(RelocOffset);
345 RI->getOffset(RelocOffset);
346346 Sections[SectIdx].getAddress(SectionAddress);
347347 RelocOffset -= SectionAddress;
348348
185185
186186 bool llvm::RelocAddressLess(RelocationRef a, RelocationRef b) {
187187 uint64_t a_addr, b_addr;
188 if (error(a.getAddress(a_addr))) return false;
189 if (error(b.getAddress(b_addr))) return false;
188 if (error(a.getOffset(a_addr))) return false;
189 if (error(b.getOffset(b_addr))) return false;
190190 return a_addr < b_addr;
191191 }
192192
377377 if (error(rel_cur->getHidden(hidden))) goto skip_print_rel;
378378 if (hidden) goto skip_print_rel;
379379
380 if (error(rel_cur->getAddress(addr))) goto skip_print_rel;
380 if (error(rel_cur->getOffset(addr))) goto skip_print_rel;
381381 // Stop when rel_cur's address is past the current instruction.
382382 if (addr >= Index + Size) break;
383383 if (error(rel_cur->getTypeName(name))) goto skip_print_rel;
416416 if (error(ri->getHidden(hidden))) continue;
417417 if (hidden) continue;
418418 if (error(ri->getTypeName(relocname))) continue;
419 if (error(ri->getAddress(address))) continue;
419 if (error(ri->getOffset(address))) continue;
420420 if (error(ri->getValueString(valuestr))) continue;
421421 outs() << address << " " << relocname << " " << valuestr << "\n";
422422 }
581581 int64_t Info;
582582 StringRef SymbolName;
583583 SymbolRef Symbol;
584 if (error(RelI->getOffset(Offset))) return;
584 if (Obj->getElfHeader()->e_type == ELF::ET_REL){
585 if (error(RelI->getOffset(Offset))) return;
586 } else {
587 if (error(RelI->getAddress(Offset))) return;
588 }
585589 if (error(RelI->getType(RelocType))) return;
586590 if (error(RelI->getTypeName(RelocName))) return;
587591 if (error(RelI->getAdditionalInfo(Info))) return;
142142
143143 bool relocAddressLess(RelocationRef a, RelocationRef b) {
144144 uint64_t a_addr, b_addr;
145 if (error(a.getAddress(a_addr))) return false;
146 if (error(b.getAddress(b_addr))) return false;
145 if (error(a.getOffset(a_addr))) return false;
146 if (error(b.getOffset(b_addr))) return false;
147147 return a_addr < b_addr;
148148 }
149149