]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commit
readelf: Add --got-contents option
authorH.J. Lu <hjl.tools@gmail.com>
Thu, 28 Aug 2025 21:15:18 +0000 (14:15 -0700)
committerH.J. Lu <hjl.tools@gmail.com>
Fri, 29 Aug 2025 13:17:19 +0000 (06:17 -0700)
commit81e90cf63a10ad11772c2437c8f2a88f1a00c739
tree6fa5f8b893bf61d2c2d28a744eaedc8035b5185b
parentdeefd61637c6b99fc9f0fb40fce0f6eb4bb10414
readelf: Add --got-contents option

Add --got-contents option to readelf, inspired by the -G option on Solaris,
to display contents of GOT sections:

$ readelf --got-contents libfoo.so

Global Offset Table '.got' contains 1 entry:
 Index:    Address       Reloc         Sym. Name + Addend/Value
     0: 000000200340 R_X86_64_GLOB_DAT foo + 0

Global Offset Table '.got.plt' contains 4 entries:
 Index:    Address       Reloc         Sym. Name + Addend/Value
     0: 000000200348                   200220
     1: 000000200350                   0
     2: 000000200358                   0
     3: 000000200360 R_X86_64_JUMP_SLO bar + 0

When --got-content options are used:

1. Allocate an array, all_relocations, to hold all relocations.
2. Update dump_relr_relocations and dump_relocations to scan relocations
and cache them in all_relocations.  Don't display relocations if not
requested.
3. Add process_got_section_contents to display contents of GOT sections
by matching the GOT entry offset against the cached relocations.
4. Update process_mips_specific to only display the GOT related contents
for --got-contents.

binutils/

* NEWS: Mention "readelf --got-contents".
* readelf.c (do_got_section_contents): New.
(elf_relocation): Likewise.
(all_relocations_root): Likewise.
(all_relocations): Likewise.
(all_relocations_count): Likewise.
(update_all_relocations): Likewise.
(dump_relr_relocations): Add a bool argument to indicate if
relocations should be displayed.  Populate all_relocations if
do_got_section_contents is true.
(dump_relocations): Likewise.
(long_option_values): Add OPTION_GOT_CONTENTS.
(options): Add --got-contents.
(usage): Show --got-contents.
(parse_args): Support --got-contents.
(display_relocations): Add a bool argument, dump_reloc, to
indicate if relocations should be displayed.  Call
update_all_relocations.  Pass dump_reloc to dump_relr_relocations
and dump_relocations.
(process_relocs): Check do_got_section_contents.  Handle
do_got_section_contents for dynamic relocations.
(process_section_contents): Pass true to display_relocations.
(process_mips_specific): Add a bool argument, dump_got, to
indicate if only the GOT related contents should be displayed.
Display all MIPS specific information if dump_got is false.
(elf_relocation_cmp): New.
(display_elf_relocation_at): Likewise.
(process_got_section_contents): Likewise.
(process_object): Call process_got_section_contents.
* doc/binutils.texi: Document --got-contents.

ld/

* testsuite/ld-i386/binutils.exp: New file.
* testsuite/ld-i386/got-1.s: Likewise.
* testsuite/ld-i386/libgot-1a.rd: Likewise.
* testsuite/ld-i386/libgot-1b.rd: Likewise.
* testsuite/ld-i386/libgot-1c.rd: Likewise.
* testsuite/ld-i386/libgot-1d.rd: Likewise.
* testsuite/ld-x86-64/binutils.exp: Likewise.
* testsuite/ld-x86-64/got-1.s: Likewise.
* testsuite/ld-x86-64/libgot-1a-x32.rd: Likewise.
* testsuite/ld-x86-64/libgot-1a.rd: Likewise.
* testsuite/ld-x86-64/libgot-1b-x32.rd: Likewise.
* testsuite/ld-x86-64/libgot-1b.rd: Likewise.
* testsuite/ld-x86-64/libgot-1c-x32.rd: Likewise.
* testsuite/ld-x86-64/libgot-1c.rd: Likewise.
* testsuite/ld-x86-64/libgot-1d-x32.rd: Likewise.
* testsuite/ld-x86-64/libgot-1d.rd: Likewise.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
19 files changed:
binutils/NEWS
binutils/doc/binutils.texi
binutils/readelf.c
ld/testsuite/ld-i386/binutils.exp [new file with mode: 0644]
ld/testsuite/ld-i386/got-1.s [new file with mode: 0644]
ld/testsuite/ld-i386/libgot-1a.rd [new file with mode: 0644]
ld/testsuite/ld-i386/libgot-1b.rd [new file with mode: 0644]
ld/testsuite/ld-i386/libgot-1c.rd [new file with mode: 0644]
ld/testsuite/ld-i386/libgot-1d.rd [new file with mode: 0644]
ld/testsuite/ld-x86-64/binutils.exp [new file with mode: 0644]
ld/testsuite/ld-x86-64/got-1.s [new file with mode: 0644]
ld/testsuite/ld-x86-64/libgot-1a-x32.rd [new file with mode: 0644]
ld/testsuite/ld-x86-64/libgot-1a.rd [new file with mode: 0644]
ld/testsuite/ld-x86-64/libgot-1b-x32.rd [new file with mode: 0644]
ld/testsuite/ld-x86-64/libgot-1b.rd [new file with mode: 0644]
ld/testsuite/ld-x86-64/libgot-1c-x32.rd [new file with mode: 0644]
ld/testsuite/ld-x86-64/libgot-1c.rd [new file with mode: 0644]
ld/testsuite/ld-x86-64/libgot-1d-x32.rd [new file with mode: 0644]
ld/testsuite/ld-x86-64/libgot-1d.rd [new file with mode: 0644]