Index: include/llvm/Object/ELFObjectFile.h =================================================================== --- include/llvm/Object/ELFObjectFile.h +++ include/llvm/Object/ELFObjectFile.h @@ -771,7 +771,7 @@ } } for (const Elf_Shdr &Sec : *SectionsOrErr) { - if (is_contained(Offsets, Sec.sh_offset)) + if (is_contained(Offsets, Sec.sh_addr)) Res.emplace_back(toDRI(&Sec), this); } return Res; Index: test/tools/llvm-objdump/dynamic-reloc-dyn.test =================================================================== --- /dev/null +++ test/tools/llvm-objdump/dynamic-reloc-dyn.test @@ -0,0 +1,54 @@ +# RUN: yaml2obj %s > %t.dyn +# RUN: llvm-objdump -R %t.dyn | FileCheck %s +# +# RUN: sed -e 's/ET_DYN/ET_EXEC/' %s | yaml2obj > %t.exec +# RUN: llvm-objdump -R %t.exec | FileCheck %s +# +# CHECK: DYNAMIC RELOCATION RECORDS +# CHECK-NEXT: {{[0-9a-f]+}} R_X86_64_GLOB_DAT x + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_DYN + Machine: EM_X86_64 +Sections: + - Name: .rela.dyn + Type: SHT_RELA + Info: .got + Flags: [ SHF_ALLOC ] + Address: 0x00000000000002D8 + AddressAlign: 0x0000000000000008 + EntSize: 0x0000000000000018 + Relocations: + - Offset: 0x00000000000020D0 + Symbol: x + Type: R_X86_64_GLOB_DAT + - Name: .dynamic + Type: SHT_DYNAMIC + Flags: [ SHF_WRITE, SHF_ALLOC ] + Address: 0x0000000000002000 + Link: .dynstr + AddressAlign: 0x0000000000000008 + EntSize: 0x0000000000000010 + Entries: + - Tag: DT_RELA + Value: 0x00000000000002D8 + - Tag: DT_NULL + Value: 0x0000000000000000 + - Name: .got + Type: SHT_PROGBITS + Flags: [ SHF_WRITE, SHF_ALLOC ] + Address: 0x00000000000020D0 + AddressAlign: 0x0000000000000008 + Content: '0000000000000000' +Symbols: + - Name: x + Type: STT_OBJECT + Binding: STB_GLOBAL +DynamicSymbols: + - Name: x + Type: STT_OBJECT + Binding: STB_GLOBAL +... Index: test/tools/llvm-objdump/dynamic-reloc-static.test =================================================================== --- /dev/null +++ test/tools/llvm-objdump/dynamic-reloc-static.test @@ -0,0 +1,11 @@ +# RUN: yaml2obj %s > %t +# RUN: not llvm-objdump -R %t 2>&1 | FileCheck %s +# CHECK: error: not a dynamic object + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_EXEC + Machine: EM_X86_64 +... Index: tools/llvm-objdump/llvm-objdump.cpp =================================================================== --- tools/llvm-objdump/llvm-objdump.cpp +++ tools/llvm-objdump/llvm-objdump.cpp @@ -1524,13 +1524,21 @@ } } +static bool isDynamicObject(const ELFObjectFileBase &Elf) { + return (Elf.getEType() == ELF::ET_DYN || Elf.getEType() == ELF::ET_EXEC) && + std::any_of(Elf.sections().begin(), Elf.sections().end(), + [](const ELFSectionRef Sec) { + return Sec.getType() == ELF::SHT_DYNAMIC; + }); +} + void llvm::printDynamicRelocations(const ObjectFile *Obj) { // For the moment, this option is for ELF only if (!Obj->isELF()) return; const auto *Elf = dyn_cast(Obj); - if (!Elf || Elf->getEType() != ELF::ET_DYN) { + if (!Elf || !isDynamicObject(*Elf)) { error("not a dynamic object"); return; }