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>
This commit is contained in:
H.J. Lu
2025-08-28 14:15:18 -07:00
parent deefd61637
commit 81e90cf63a
19 changed files with 1380 additions and 217 deletions

View File

@@ -1,5 +1,8 @@
-*- text -*-
* Add --got-contents option to readelf to display the contents of
Global Offset Table (GOT) sections.
* Internal changes to plugin support, and stricter target checking may result
in some errors being exposed in user options passed to the various binutils.
For example objcopy --target=TARGET now will only work if the input file is

View File

@@ -5076,6 +5076,7 @@ readelf [@option{-a}|@option{--all}]
[@option{--ctf-strings=}@var{section}]
[@option{--sframe=}@var{section}]
[@option{-I}|@option{--histogram}]
[@option{--got-contents}]
[@option{-v}|@option{--version}]
[@option{-W}|@option{--wide}]
[@option{-T}|@option{--silent-truncation}]
@@ -5111,7 +5112,8 @@ given.
Equivalent to specifying @option{--file-header},
@option{--program-headers}, @option{--sections}, @option{--symbols},
@option{--relocs}, @option{--dynamic}, @option{--notes},
@option{--version-info}, @option{--arch-specific}, @option{--unwind},
@option{--got-contents}, @option{--version-info},
@option{--arch-specific}, @option{--unwind},
@option{--section-groups} and @option{--histogram}.
Note - this option does not enable @option{--use-dynamic} itself, so
@@ -5387,6 +5389,14 @@ string table are used.
If either of @option{--ctf-symbols} or @option{--ctf-strings} is specified, the
other must be specified as well.
@item --got-contents
@cindex ELF section information
@cindex ELF reloc information
Displays the contents of the file's Global Offset Table (GOT) sections,
if it has any. For MIPS, this option is similar to
@option{--arch-specific}, but it only displays the GOT related contents
and it is ignored when @option{--arch-specific} is used.
@item -I
@itemx --histogram
Display a histogram of bucket list lengths when displaying the contents

File diff suppressed because it is too large Load Diff