diff options
-rw-r--r-- | llvm/include/llvm/Object/ELFObjectFile.h | 25 | ||||
-rw-r--r-- | llvm/test/Object/objdump-reloc-shared.test | 5 | ||||
-rw-r--r-- | llvm/test/tools/llvm-objdump/X86/Inputs/relocations-in-nonrelocatable.elf-x86_64 | bin | 9731 -> 0 bytes | |||
-rw-r--r-- | llvm/test/tools/llvm-objdump/X86/relocations-in-nonrelocatable.test | 9 | ||||
-rw-r--r-- | llvm/tools/llvm-objdump/llvm-objdump.cpp | 4 |
5 files changed, 20 insertions, 23 deletions
diff --git a/llvm/include/llvm/Object/ELFObjectFile.h b/llvm/include/llvm/Object/ELFObjectFile.h index 5ee90ee9ea7..b01fa1da4b3 100644 --- a/llvm/include/llvm/Object/ELFObjectFile.h +++ b/llvm/include/llvm/Object/ELFObjectFile.h @@ -640,6 +640,9 @@ ELFObjectFile<ELFT>::section_rel_end(DataRefImpl Sec) const { template <class ELFT> section_iterator ELFObjectFile<ELFT>::getRelocatedSection(DataRefImpl Sec) const { + if (EF.getHeader()->e_type != ELF::ET_REL) + return section_end(); + const Elf_Shdr *EShdr = getSection(Sec); uintX_t Type = EShdr->sh_type; if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA) @@ -679,20 +682,14 @@ ELFObjectFile<ELFT>::getRelocationSymbol(DataRefImpl Rel) const { } template <class ELFT> -uint64_t ELFObjectFile<ELFT>::getRelocationOffset(DataRefImpl Rel) const {
- const Elf_Shdr *sec = getRelSection(Rel);
- uint64_t Offset = sec->sh_type == ELF::SHT_REL ?
- getRel(Rel)->r_offset :
- getRela(Rel)->r_offset;
- if (EF.getHeader()->e_type == ELF::ET_EXEC ||
- EF.getHeader()->e_type == ELF::ET_DYN) {
- // For an executable file or a shared object, the value is the virtual
- // address of the storage unit affected by the relocation.
- auto SectionIter = getRelocatedSection(toDRI(sec));
- if (SectionIter != section_end())
- Offset -= SectionIter->getAddress();
- }
- return Offset; +uint64_t ELFObjectFile<ELFT>::getRelocationOffset(DataRefImpl Rel) const { + assert(EF.getHeader()->e_type == ELF::ET_REL && + "Only relocatable object files have relocation offsets"); + const Elf_Shdr *sec = getRelSection(Rel); + if (sec->sh_type == ELF::SHT_REL) + return getRel(Rel)->r_offset; + + return getRela(Rel)->r_offset; } template <class ELFT> diff --git a/llvm/test/Object/objdump-reloc-shared.test b/llvm/test/Object/objdump-reloc-shared.test new file mode 100644 index 00000000000..d899ffb6087 --- /dev/null +++ b/llvm/test/Object/objdump-reloc-shared.test @@ -0,0 +1,5 @@ +RUN: llvm-objdump -r %p/Inputs/elf-reloc-no-sym.x86_64 \ +RUN: | FileCheck %s + +; CHECK: elf-reloc-no-sym.x86_64: file format ELF64-x86-64 +; CHECK-NOT: {{.}} diff --git a/llvm/test/tools/llvm-objdump/X86/Inputs/relocations-in-nonrelocatable.elf-x86_64 b/llvm/test/tools/llvm-objdump/X86/Inputs/relocations-in-nonrelocatable.elf-x86_64 Binary files differdeleted file mode 100644 index bb706c948cb..00000000000 --- a/llvm/test/tools/llvm-objdump/X86/Inputs/relocations-in-nonrelocatable.elf-x86_64 +++ /dev/null diff --git a/llvm/test/tools/llvm-objdump/X86/relocations-in-nonrelocatable.test b/llvm/test/tools/llvm-objdump/X86/relocations-in-nonrelocatable.test deleted file mode 100644 index 4860b91dd6a..00000000000 --- a/llvm/test/tools/llvm-objdump/X86/relocations-in-nonrelocatable.test +++ /dev/null @@ -1,9 +0,0 @@ -// This test checks that relocation in nonrelocatable files are printed
-// RUN: llvm-objdump -r %p/Inputs/relocations-in-nonrelocatable.elf-x86_64 | FileCheck %s
-
-// (main.c)
-// void g(void){}
-// int main(void) { g(); };
-// gcc main.c -o main -Wl,--emit-relocs
-
-CHECK: 00000000000000f8 R_X86_64_PC32 g-4-P
\ No newline at end of file diff --git a/llvm/tools/llvm-objdump/llvm-objdump.cpp b/llvm/tools/llvm-objdump/llvm-objdump.cpp index 7f49efd8f84..f436183f065 100644 --- a/llvm/tools/llvm-objdump/llvm-objdump.cpp +++ b/llvm/tools/llvm-objdump/llvm-objdump.cpp @@ -1188,6 +1188,10 @@ static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) { void llvm::PrintRelocations(const ObjectFile *Obj) { StringRef Fmt = Obj->getBytesInAddress() > 4 ? "%016" PRIx64 : "%08" PRIx64; + // Regular objdump doesn't print relocations in non-relocatable object + // files. + if (!Obj->isRelocatableObject()) + return; for (const SectionRef &Section : ToolSectionFilter(*Obj)) { if (Section.relocation_begin() == Section.relocation_end()) |