From 7df905954106fe2302df36d96e4952b736817298 Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Thu, 3 Apr 2014 23:54:35 +0000 Subject: [PATCH] Implement getRelocationAddress for MachO and ET_REL elf files. With that, fix the symbolizer to work with any ELF file. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@205588 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Object/ELFObjectFile.h | 15 ++++++-- lib/MC/MCObjectSymbolizer.cpp | 36 ++---------------- lib/Object/MachOObjectFile.cpp | 10 ++++- .../Object/Inputs/relocation-dynamic.elf-i386 | Bin 0 -> 1504 bytes .../Inputs/relocation-relocatable.elf-i386 | Bin 0 -> 772 bytes .../X86/objdump-disassembly-symbolic.test | 20 ++++++++++ 6 files changed, 44 insertions(+), 37 deletions(-) create mode 100755 test/Object/Inputs/relocation-dynamic.elf-i386 create mode 100644 test/Object/Inputs/relocation-relocatable.elf-i386 diff --git a/include/llvm/Object/ELFObjectFile.h b/include/llvm/Object/ELFObjectFile.h index 256f3c7630c..302cabaf8ea 100644 --- a/include/llvm/Object/ELFObjectFile.h +++ b/include/llvm/Object/ELFObjectFile.h @@ -555,10 +555,17 @@ ELFObjectFile::getRelocationSymbol(DataRefImpl Rel) const { template error_code ELFObjectFile::getRelocationAddress(DataRefImpl Rel, uint64_t &Result) const { - assert((EF.getHeader()->e_type == ELF::ET_EXEC || - EF.getHeader()->e_type == ELF::ET_DYN) && - "Only executable and shared objects files have relocation addresses"); - Result = getROffset(Rel); + uint64_t ROffset = getROffset(Rel); + const Elf_Ehdr *Header = EF.getHeader(); + + if (Header->e_type == ELF::ET_REL) { + const Elf_Shdr *RelocationSec = getRelSection(Rel); + const Elf_Shdr *RelocatedSec = EF.getSection(RelocationSec->sh_info); + Result = ROffset + RelocatedSec->sh_addr; + } else { + Result = ROffset; + } + return object_error::success; } diff --git a/lib/MC/MCObjectSymbolizer.cpp b/lib/MC/MCObjectSymbolizer.cpp index ba80d156532..52d66b8d406 100644 --- a/lib/MC/MCObjectSymbolizer.cpp +++ b/lib/MC/MCObjectSymbolizer.cpp @@ -257,40 +257,12 @@ void MCObjectSymbolizer::buildSectionList() { void MCObjectSymbolizer::buildRelocationByAddrMap() { for (const SectionRef &Section : Obj->sections()) { - section_iterator RelSecI = Section.getRelocatedSection(); - if (RelSecI == Obj->section_end()) - continue; - - uint64_t StartAddr; RelSecI->getAddress(StartAddr); - uint64_t Size; RelSecI->getSize(Size); - bool RequiredForExec; - RelSecI->isRequiredForExecution(RequiredForExec); - if (RequiredForExec == false || Size == 0) - continue; for (const RelocationRef &Reloc : Section.relocations()) { - // FIXME: libObject is inconsistent regarding error handling. The - // overwhelming majority of methods always return object_error::success, - // and assert for simple errors.. Here, ELFObjectFile::getRelocationOffset - // asserts when the file type isn't ET_REL. - // This workaround handles x86-64 elf, the only one that has a relocinfo. - uint64_t Offset; - if (Obj->isELF()) { - const ELF64LEObjectFile *ELFObj = dyn_cast(Obj); - if (ELFObj == 0) - break; - if (ELFObj->getELFFile()->getHeader()->e_type == ELF::ET_REL) { - Reloc.getOffset(Offset); - Offset += StartAddr; - } else { - Reloc.getAddress(Offset); - } - } else { - Reloc.getOffset(Offset); - Offset += StartAddr; - } + uint64_t Address; + Reloc.getAddress(Address); // At a specific address, only keep the first relocation. - if (AddrToReloc.find(Offset) == AddrToReloc.end()) - AddrToReloc[Offset] = Reloc; + if (AddrToReloc.find(Address) == AddrToReloc.end()) + AddrToReloc[Address] = Reloc; } } } diff --git a/lib/Object/MachOObjectFile.cpp b/lib/Object/MachOObjectFile.cpp index dd732aefb0d..7ce62eb2a44 100644 --- a/lib/Object/MachOObjectFile.cpp +++ b/lib/Object/MachOObjectFile.cpp @@ -784,7 +784,15 @@ void MachOObjectFile::moveRelocationNext(DataRefImpl &Rel) const { error_code MachOObjectFile::getRelocationAddress(DataRefImpl Rel, uint64_t &Res) const { - report_fatal_error("getRelocationAddress not implemented in MachOObjectFile"); + MachO::any_relocation_info RE = getRelocation(Rel); + uint64_t Offset = getAnyRelocationAddress(RE); + + DataRefImpl Sec; + Sec.d.a = Rel.d.a; + uint64_t SecAddress; + getSectionAddress(Sec, SecAddress); + Res = SecAddress + Offset; + return object_error::success; } error_code MachOObjectFile::getRelocationOffset(DataRefImpl Rel, diff --git a/test/Object/Inputs/relocation-dynamic.elf-i386 b/test/Object/Inputs/relocation-dynamic.elf-i386 new file mode 100755 index 0000000000000000000000000000000000000000..1548f13f72320d805bb6383be965ac05bcd40ae8 GIT binary patch literal 1504 zcmb7^O>0w85QZoH@~alDs33mam9#>SNen283rTAgwV>$2jp=Q3+Z38aa*tv`us^|t zKfpy@=tBBq{0lBzx)cG$Wd`KMKPL4zKW{w#?wf>; zO1M926sMlO;rp|yV%SoM)N`S(&jtS>*~+uQt#W?$XGZwB5pEmSvQ2Ab&D5vXXsqRV zBll^~%hXY&yhl!Ir^fIdq>Dn0^iAv+kul*DTE-v0`u%?RG1Dir53*ApwVx6W3*`MK z91~(si;6oceo5f%ruZ3Yzh${zznRP}uRNPiYVL-sCe?D~TBTa4CUZ}+W^ym} z1N*}Dd2;i{k)hTl+w5*PlZvZUaE^$|`y&(j?)1fu>`Ir$rb6$> zcsb;JXXX;W89PTD`vTaPC~+oBGO)MAu}7e+bM%uBTo9NyaqKDJonjnwg#-3>PY1xR zM!|0(XdG#^o3wj>LS_Li|B-{JnCA9ue{_436O!AGjD0wGc;bFNRzy$h4eSBax1MJk^5_8C<1tWU5iGG1Tb*N3BKY5-P6AcH06VD&s z+;zRaI#Q7r^}>TN3M0?I)no56Hf20G>=LQ$B{lJg(acMnh&R_y%IIf9Y8;!G)F{hH zC9^cuQ;tn%Mk_87WF}4?<)}+>mlfz`#VXN5$NB*2bBF!4TKu~~w8?zcYMR#Hv)pBb zot99|vfn;Ouy&cQ#~Q2w!mwXZc;$o~#<9gRO7#B*r0y0Aq$ae`UBR&?Iv}@L*%^~J zX1Iek5A=)v!Rz~dux^86VzQrb!Fs$&U%AAAe}5Nvy#hF@FU+l8uZ+3WD()7n#bcXq gsQ#un_qxqVo{Y|nO6Z;@^uW2Ie|{kRiv~CR3zb(oumAu6 literal 0 HcmV?d00001 diff --git a/test/Object/X86/objdump-disassembly-symbolic.test b/test/Object/X86/objdump-disassembly-symbolic.test index 858653e95eb..95a5fc8e70a 100644 --- a/test/Object/X86/objdump-disassembly-symbolic.test +++ b/test/Object/X86/objdump-disassembly-symbolic.test @@ -46,3 +46,23 @@ MACHO-STUBS-x86-64: 1faa: e8 09 00 00 00 MACHO-STUBS-x86-64: 1faf: 8b 44 24 04 movl 4(%rsp), %eax MACHO-STUBS-x86-64: 1fb3: 48 83 c4 08 addq $8, %rsp MACHO-STUBS-x86-64: 1fb7: c3 ret + + +RUN: llvm-objdump -d -symbolize %p/../Inputs/relocation-relocatable.elf-i386 \ +RUN: | FileCheck %s -check-prefix ELF-i386-REL + +ELF-i386-REL: Disassembly of section .text: +ELF-i386-REL-NEXT: f: +ELF-i386-REL-NEXT: 0: e9 fc ff ff ff jmp h +ELF-i386-REL: g: +ELF-i386-REL-NEXT: 5: e9 fc ff ff ff jmp f + + +RUN: llvm-objdump -d -symbolize %p/../Inputs/relocation-dynamic.elf-i386 \ +RUN: | FileCheck %s -check-prefix ELF-i386-DYN + +ELF-i386-DYN: Disassembly of section .text: +ELF-i386-DYN-NEXT: f: +ELF-i386-DYN-NEXT: 1a4: e9 fc ff ff ff jmp h +ELF-i386-DYN: g: +ELF-i386-DYN-NEXT: 1a9: e9 fc ff ff ff jmp f