[PATCH] D15965: Add support for dumping relocations in non-relocatable files

Rafael EspĂ­ndola via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 14 14:18:13 PST 2016


Please add a comment saying how the binary was created.

The assert is correct. Whatever is calling getRelocationOffset should
instead be trying to get the address.

Testing locally I see that objdump prints relocations that were copied
by  --emit-reloc but not dynamic relocations.

Given

void g(void);
void f(void) {
  g();
}

objudump on x86_64 will print

--------------------------------------------

RELOCATION RECORDS FOR [.text]:
OFFSET           TYPE              VALUE
0000000000000005 R_X86_64_PLT32    g-0x0000000000000004


RELOCATION RECORDS FOR [.eh_frame]:
OFFSET           TYPE              VALUE
0000000000000020 R_X86_64_PC32     .text+0x0000000000000270
-------------------------------------------


readelf prints

-------------------------------------------------

Relocation section '.rela.plt' at offset 0x238 contains 1 entries:
    Offset             Info             Type               Symbol's
Value  Symbol's Name + Addend
00000000000013e8  0000000200000007 R_X86_64_JUMP_SLOT     0000000000000000 g + 0

Relocation section '.rela.text' at offset 0x428 contains 1 entries:
    Offset             Info             Type               Symbol's
Value  Symbol's Name + Addend
0000000000000275  0000000600000004 R_X86_64_PLT32         0000000000000000 g - 4

Relocation section '.rela.eh_frame' at offset 0x440 contains 1 entries:
    Offset             Info             Type               Symbol's
Value  Symbol's Name + Addend
00000000000002a0  0000000100000002 R_X86_64_PC32
0000000000000270 .text + 270
----------------------------------------------------------------


Note that:

* The relocations from .rela.plt are missing in objdump.
* The relocations in the .so have addresses. What objdump does is map
address to section and subtract the section address to get an offset.

Please investigate exactly which relocations are skipped by objdump.

Please make sure llvm-objdump skips the same relocations and prints
offsets like objdump.

Cheers,
Rafael




On 7 January 2016 at 15:22, Colin LeMahieu <colinl at codeaurora.org> wrote:
> colinl created this revision.
> colinl added a reviewer: rafael.
> colinl added a subscriber: llvm-commits.
> colinl set the repository for this revision to rL LLVM.
>
> Relocations can be in non-relocatable files by linking with --emit-relocs.  This removes the associated violated assertion in ELFObjectFile and removes the short-circuit return in llvm-objdump.  The comment says objdump doesn't print out these relocations though this isn't correct.  Using the associated test file it's seen GNU objdump indeed prints out these relocations.
>
> Repository:
>   rL LLVM
>
> http://reviews.llvm.org/D15965
>
> Files:
>   include/llvm/Object/ELFObjectFile.h
>   test/tools/llvm-objdump/Inputs/relocations-in-nonrelocatable.elf-hexagon
>   test/tools/llvm-objdump/relocations-in-nonrelocatable.test
>   tools/llvm-objdump/llvm-objdump.cpp
>
> Index: tools/llvm-objdump/llvm-objdump.cpp
> ===================================================================
> --- tools/llvm-objdump/llvm-objdump.cpp
> +++ tools/llvm-objdump/llvm-objdump.cpp
> @@ -1154,11 +1154,6 @@
>  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())
>        continue;
> Index: test/tools/llvm-objdump/relocations-in-nonrelocatable.test
> ===================================================================
> --- test/tools/llvm-objdump/relocations-in-nonrelocatable.test
> +++ test/tools/llvm-objdump/relocations-in-nonrelocatable.test
> @@ -0,0 +1,4 @@
> +// This test checks that relocation in nonrelocatable files are printed
> +// RUN: llvm-objdump -r %p/Inputs/relocations-in-nonrelocatable.elf-hexagon | FileCheck %s
> +
> +CHECK: 00000080 R_HEX_HI16 hexagon_pre_main
> Index: include/llvm/Object/ELFObjectFile.h
> ===================================================================
> --- include/llvm/Object/ELFObjectFile.h
> +++ include/llvm/Object/ELFObjectFile.h
> @@ -683,8 +683,6 @@
>
>  template <class ELFT>
>  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;
>
>


More information about the llvm-commits mailing list