aboutsummaryrefslogtreecommitdiff
path: root/binutils/readelf.c
AgeCommit message (Collapse)AuthorFilesLines
12 daysreadelf: tidy dump_relr_relocationsAlan Modra1-5/+12
A comment in display_relocations said "RELRS has been freed by dump_relr_relocations". Except that hadn't happened on all return paths. Tidy that by freeing relrs allocated in dump_relr_relocations in that function, and relrs allocated in display_relocation in that function. * readelf.c (dump_relr_relocations): Only free relrs allocated in this function. (display_relocations): Free relrs here, on error return paths too.
12 daysCorrect -Ue output for nm, objdump and readelfAndrew C Aitchison1-2/+2
..., like PR33360 does for strings. Signed-off-by: Andrew C Aitchison <github@aitchison.me.uk>
2025-09-07elf: Define more SHT_SUNW_XXXH.J. Lu1-20/+44
binutils/ * readelf.c (get_solaris_section_type): Use SHT_SUNW_XXX and add SHT_SUNW_phname and SHT_SUNW_symtabnsort. include/ * elf/common.h (SHT_SUNW_symtabnsort): New. (SHT_SUNW_ancillary): Likewise. (SHT_SUNW_capchain): Likewise. (SHT_SUNW_capinfo): Likewise. (SHT_SUNW_symsort): Likewise. (SHT_SUNW_tlssort): Likewise. (SHT_SUNW_LDYNSYM): Likewise. (SHT_SUNW_dof): Likewise. (SHT_SUNW_cap): Likewise. (SHT_SUNW_SIGNATURE): Likewise. (SHT_SUNW_ANNOTATE): Likewise. (SHT_SUNW_DEBUGSTR): Likewise. (SHT_SUNW_DEBUG): Likewise. (SHT_SUNW_verdef): Likewise. (SHT_SUNW_verneed): Likewise. (SHT_SUNW_versym): Likewise. Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
2025-09-05readelf: Handle 0 sh_entsize of GOT sectionsH.J. Lu1-0/+19
Gold, lld and mold set sh_entsize to 0 on .got and .got.plt sections. If sh_entsize of GOT sections is 0, assume 8 for ELFCLASS64 and 4 for ELFCLASS32, except for x32 which uses 8. PR binutils/33368 * readelf.c (process_got_section_contents): Handle 0 sh_entsize of GOT sections. Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
2025-09-03readelf: Don't change the symbol tableH.J. Lu1-19/+32
1. Update dump_relr_relocations not to change the symbol table. 2. Update count_relr_relocations and dump_relr_relocations not to retrieve the same data twice. binutils/ PR binutils/33328 * readelf.c (count_relr_relocations): Add an argument to return the retrieved data and remove the FIXME in comments. (dump_relr_relocations): Add an argument for the retrieved data. Retrieve the data if needed. Don't change the symbol table and remove the FIXME in comments. (display_relocations): Get the data from count_relr_relocations and pass it to dump_relr_relocations call. (process_relocs): Pass NULL to dump_relr_relocations. ld/ PR binutils/33328 * testsuite/ld-i386/dt-relr-2.d: New file. * testsuite/ld-i386/dt-relr-2.s: Likewise. * testsuite/ld-x86-64/dt-relr-2-x32.d: Likewise. * testsuite/ld-x86-64/dt-relr-2.d: Likewise. * testsuite/ld-x86-64/dt-relr-2.s: Likewise. * testsuite/ld-i386/i386.exp: Run dt-relr-2. * testsuite/ld-x86-64/x86-64.exp: Run dt-relr-2 and dt-relr-2-x32. Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
2025-09-03readelf: Run --got-contents only on ET_DYN/ET_EXEC filesH.J. Lu1-0/+9
Update "readelf --got-contents" to skip on non-ET_DYN/ET_EXEC files. * readelf.c (process_got_section_contents): Skip if not ET_DYN/ET_EXEC files. * testsuite/binutils-all/readelf-got.d: Removed. * testsuite/binutils-all/readelf.exp: Don't run readelf-got. Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
2025-09-03readelf: Call qsort only if all_relocations_count > 1H.J. Lu1-2/+3
Don't call qsort if all_relocations_count < 1. PR binutils/33351 * readelf.c (process_got_section_contents): Call qsort only if all_relocations_count > 1. Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
2025-09-02readelf: Report if no GOT section foundH.J. Lu1-0/+13
Update "readelf --got-contents" to report if no GOT section found. * readelf.c (process_got_section_contents): Report if no GOT section found. * testsuite/binutils-all/readelf-got.d: New file. * testsuite/binutils-all/readelf.exp: Run readelf-got. Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
2025-08-29readelf: Add --got-contents optionH.J. Lu1-214/+790
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>
2025-08-27readelf: Update "-D -r" to dump DT_RELRH.J. Lu1-46/+60
1. Update dump_relr_relocations to take relr_size, relr_entsize and relr_offset, instead of a pointer to Elf_Internal_Shdr, as function arguments. 2. Update process_relocs to call dump_relr_relocations if relocation type is reltype_relr. binutils/ PR binutils/33328 * readelf.c (dump_relr_relocations): Replace a pointer to Elf_Internal_Shdr argument with relr_size, relr_entsize and relr_offset. (display_relocations): Update the dump_relr_relocations call. (process_relocs): Call dump_relr_relocations if relocation type is reltype_relr. ld/ PR binutils/33328 * testsuite/ld-i386/dt-relr-1c.d: New file. * testsuite/ld-i386/dt-relr-1d.d: Likewise. * testsuite/ld-x86-64/dt-relr-1c-x32.d: Likewise. * testsuite/ld-x86-64/dt-relr-1c.d: Likewise. * testsuite/ld-x86-64/dt-relr-1d-x32.d: Likewise. * testsuite/ld-x86-64/dt-relr-1d.d: Likewise. * testsuite/ld-i386/i386.exp: Run PR binutils/33328 tests. * testsuite/ld-x86-64/x86-64.exp: Likewise. Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
2025-07-25readelf: objdump: sframe: fix dumping with section nameIndu Bhagat1-0/+3
Fix PR binutils/33186 - No SFrame dump if section name is not .sframe When the section name is not ".sframe", ensure that readelf and objdump are able to dump a section of type SHT_GNU_SFRAME and not fail if the user specifies the new section name. For objdump, in dump_dwarf_section (), use the match string of ".sframe" to find the corresponding debug_displays[] item for SFrame section. Doing this ensures that any call to dump_dwarf_section () with the section pointing to the SFrame section (with name possibly different from ".sframe") will successfully dump the SFrame section. If the SFrame section is named anything but ".sframe", and user does not specify the name of the SFrame section either, the documented behaviour is that the default section name is assumed to be ".sframe". So the following (albeit counter intuitive) is expected at this time: $ readelf -S sort | grep sframe [NN] .sframe2 GNU_SFRAME 0000000000NNNNNN 0000NNNN (Note section name .sframe2). $ objdump --sframe sort sort: file format elf64-x86-64 No .sframe section present (Similarly for readelf as well). For objdump, set dump_sframe_section_name to ".sframe" if user specifies no section name. In the error checking done in dump_sframe_section, add the case when user specifies a valid section name but one that does not contain SFrame section data. For sections generated with Binutils >= 2.45, this can be checked with section type of SHT_GNU_SFRAME. Previously these sections were SHT_PROGBITS with name ".sframe". Similar changes in readelf. Add a test each for objdump and readelf to dump a renamed section. Use gas_sframe_check to limit the execution of these tests only when a gas supporting SFrame format is present. binutils/ PR binutils/33186 * objdump.c (dump_dwarf_section): Set match to ".sframe" which corresponds to the name in the debug_displays[] entry for SFrame section. (dump_sframe_section): Check if the user specified section name contains SFrame data. (main): Set default section name to ".sframe". * readelf.c (display_debug_section): Adjust checks to find the debug_diplays[] item for the input arg SFrame section. Use id instead of i, as it is more readable. binutils/testsuite/ PR binutils/33186 * binutils-all/x86-64/objdump-sframe-01.d: New test. * binutils-all/x86-64/readelf-sframe-01.d: New test. * binutils-all/x86-64/sframe-func.s: New test.
2025-07-17readelf v850 notesAlan Modra1-36/+24
This patch was prompted by a clang warning: readelf.c:23317:37: warning: pointer comparison always evaluates to false [-Wtautological-compare] 23317 | || inote.namedata + inote.namesz < inote.namedata) | ^ Use the same sanity checks as used by process_notes_at. (See commit 5396a86e4396). While we are at it, print all the v850 note info in one place to properly handle error conditions.
2025-07-12ld/aarch64elf: add support for DT_AARCH64_MEMTAG_STACK dynamic tagIndu Bhagat1-0/+1
Add new command line option -z memtag-stack for aarch64 elf. This option instructs the linker to generate the necessary dynamic tag DT_AARCH64_MEMTAG_STACK, which the dynamic loader can then use to protect the stack memory with PROT_MTE. Linker issues an 'unrecognized option' error when -z memtag-stack is specified for non-aarch64 based emulations. readelf displays the dynamic tag when present: $ readelf -d <exectutable> Dynamic section at offset 0xfdd8 contains XX entries: Tag Type Name/Value 0x0000000000000001 (NEEDED) Shared library: [libc.so.6] 0x000000000000000c (INIT) 0x400520 0x000000000000000d (FINI) 0x400b64 0x0000000000000019 (INIT_ARRAY) 0x41fdc8 ... ... ... 0x000000007000000c (AARCH64_MEMTAG_STACK) 0x1 ... ... ... ChangeLog: * bfd/elfnn-aarch64.c (elfNN_aarch64_late_size_sections): Emit DT_AARCH64_MEMTAG_STACK dynamic tag. * bfd/elfxx-aarch64.h (struct aarch64_memtag_opts): Add new member for tracking whether stack access uses MTE insns. * binutils/readelf.c (get_aarch64_dynamic_type): Handle DT_AARCH64_MEMTAG_STACK. * ld/emultempl/aarch64elf.em: Add new command line option. * ld/ld.texi: Add documentation for -z memtag-stack. * ld/testsuite/ld-aarch64/aarch64-elf.exp: Add new test. * ld/testsuite/ld-aarch64/dt-memtag-stack.d: New test. include/ChangeLog: * elf/aarch64.h (DT_AARCH64_MEMTAG_STACK): New definition.
2025-07-12ld/aarch64elf: add support for DT_AARCH64_MEMTAG_MODE dynamic tagIndu Bhagat1-2/+4
Add new command line option -z memtag-mode=<mode> to aarch64 elf, where <mode> can be one of none, sync, or async. For mode of sync or async, a DT_AARCH64_MEMTAG_MODE dynamic tag with a value of 0 or 1 respectively is emitted. readelf displays the dynamic tag when present: $ readelf -d <exectutable> Dynamic section at offset 0xfdd8 contains XX entries: Tag Type Name/Value 0x0000000000000001 (NEEDED) Shared library: [libc.so.6] 0x000000000000000c (INIT) 0x400520 0x000000000000000d (FINI) 0x400b64 0x0000000000000019 (INIT_ARRAY) 0x41fdc8 ... ... ... 0x0000000070000009 (AARCH64_MEMTAG_MODE) 0x1 ... ... ... Note that this patch doesn't add support for the "asymm" MTE mode, which is an Armv8.7 extension. ChangeLog: * bfd/elfnn-aarch64.c (struct elf_aarch64_link_hash_table): Add new member for memtag properties. (bfd_elfNN_aarch64_set_options): New argument to pass memtag properties. (elfNN_aarch64_late_size_sections): Emit DT_AARCH64_MEMTAG_MODE dynamic tag. * bfd/elfxx-aarch64.h: New definition for the various memtag properties. * binutils/readelf.c (get_aarch64_dynamic_type): Handle DT_AARCH64_MEMTAG_MODE. * ld/emultempl/aarch64elf.em: Likewise. * ld/ld.texi: Add documentation for the new option -z memtag-mode. * ld/testsuite/ld-aarch64/aarch64-elf.exp: New test. * ld/testsuite/ld-aarch64/dt-memtag.d: New test. * ld/testsuite/ld-aarch64/dt-memtag-mode.s: New test. include/ChangeLog: * elf/aarch64.h (DT_AARCH64_MEMTAG_MODE): New definition.
2025-07-12binutils: generalize init_dwarf_regnames_by_* functionsJose E. Marchesi1-1/+1
This patch renames the functions: init_dwarf_regnames_by_elf_machine_code init_dwarf_regnames_by_bfd_arch_and_mach to init_dwarf_by_elf_machine_code init_dwarf_by_bfd_arch_and_mach The idea is to start using these functions to perform general architecture/machine specific initializations beyond register names. Regtested in x86_64-linux-gnu and aarch64-linux-gnu targets. binutils/ChangeLog: * dwarf.c (init_dwarf_regnames_by_elf_machine_code): Rename to init_dwarf_by_elf_machine_code. (init_dwarf_regnames_by_bfd_arch_and_mach): Rename to init_dwarf_by_bfd_arch_and_mach. * dwarf.h: Adjust prototypes accordingly. * readelf.c (process_file_header): Adjust call to init_dwarf_regnames_by_elf_machine_code accordingly. * objdump.c (dump_dwarf): Adjust call to init_dwarf_regnames_by_bfd_arch_and_mach accordingly.
2025-07-11gas: elf: binutils: add new section type SHT_GNU_SFRAMEIndu Bhagat1-0/+2
So far, SFrame sections were of type SHT_PROGBITS. As per ELF specification, SHT_PROGBITS indicates that the section holds information defined by the program, whose format and meaning are determined solely by the program. On the linker side, SHT_PROGBITS should be reserved for the simple "cat contents after applying relocs" semantics. Currently, the only way to know that a section contains SFrame stack trace data is if consumer checks for section name. Such a check for section name is not quite conformant to ELF principles. Some of this was discussed here https://sourceware.org/pipermail/binutils/2025-March/140181.html With this change, the SFrame sections generated by gas, ld will have section type set to SHT_GNU_SFRAME. The new section type is defined in the SHT_LOOS/SHT_HIOS space. The SFrame parsing routine _bfd_elf_parse_sframe () now admits sections only when the the section type is SHT_GNU_SFRAME. No special handling / validation is done at the moment for the case of manual creation of SFrame sections via obj_elf_section (). Add function level comments for now to add a note about this. Although the default handling for (sh_type >= SHT_LOOS && sh_type <= SHT_HIOS) is sufficient when SHT_GNU_SFRAME is in that range, it makes sense to add it as a case of its own. bfd/ * elf-sframe.c (_bfd_elf_parse_sframe): Check if section type is SHT_GNU_SFRAME. (_bfd_elf_set_section_sframe): Set SHT_GNU_SFRAME for output SFrame section. * elflink.c (obj_elf_section): Use section type for check instead of section name. * elfxx-x86.c: Set SHT_GNU_SFRAME for SFrame sections for .plt* sections. * elf.c (bfd_section_from_shdr): Add case for SHT_GNU_SFRAME. binutils/ * readelf.c (get_os_specific_section_type_name): Add SHT_GNU_SFRAME. gas/ * NEWS: Announce emitted SFrame sections have SHT_GNU_SFRAME set. * config/obj-elf.c (obj_elf_attach_to_group): Add comments to indicate no special handling for SFrame yet. * dw2gencfi.c (cfi_finish): Set SHT_GNU_SFRAME for emitted SFrame section. ld/ * NEWS: Announce emitted SFrame sections have SHT_GNU_SFRAME set. gas/testsuite/ * gas/cfi-sframe/cfi-sframe.exp: Add new test. * gas/cfi-sframe/cfi-sframe-common-1b.d: New test. * gas/cfi-sframe/cfi-sframe-common-1b.s: New test. include/ * elf/common.h (SHT_GNU_SFRAME): Add new section type for SFrame stack trace information. libsframe/doc/ * sframe-spec.texi: Add expected ELF section type.
2025-07-06objdump, readelf: sframe: apply relocations before textual dumpIndu Bhagat1-40/+10
PR libsframe/32589 - function start address is zero in SFrame section dump Currently, readelf and objdump display the SFrame sections in ET_REL object files with function start addresses of each function as 0. This makes it difficult to correlate SFrame stack trace information with the individual functions in the object file. For objdump, use the dump_dwarf () interface to dump SFrame section. Similarly, for readelf, use the display_debug_section () interface to dump SFrame section. These existing interfaces (for DWARF debug sections) already support relocating the section contents before dumping, so lets use them for SFrame sections as well. When adding a new entry for SFrame in debug_option_table[], use char 'nil' and the option name of "sframe-internal-only". This is done so that there is no additional (unnecessary) user-exposed ways of dumping SFrame sections. Additionally, we explicitly disallow the "sframe-internal-only" from external/user input in --dwarf (objdump). Similarly, "sframe-internal-only" is explicitly matched and disallowed from --debug-dump (readelf). For objdump and readelf, we continue to keep the same error messaging as earlier: $ objdump --sframe=sframe bubble_sort.o ... No sframe section present $ objdump --sframe=.sfram bubble_sort.o ... No .sfram section present $ objdump --sframe=sframe-internal-only sort ... No sframe-internal-only section present Similarly for readelf: $ readelf --sframe= bubble_sort.o readelf: Error: Section name must be provided $ readelf --sframe=.sfram bubble_sort.o readelf: Warning: Section '.sfram' was not dumped because it does not exist $ readelf --sframe=sframe bubble_sort.o readelf: Warning: Section 'sframe' was not dumped because it does not exist PS: Note how this patch adds a new entry to debug_displays[] with a relocate value set to FALSE. This will be set to TRUE in a subsequent patch ("bfd: gas: ld: libsframe: emit func start addr field as an offset from FDE") when fixes are made to emit the value of the 'sfde_func_start_address' field in the new encoding SFRAME_F_FDE_FUNC_START_PCREL across gas and ld. binutils/ * dwarf.c (display_sframe): New definition. (dwarf_select_sections_all): Enable SFrame section too. (struct dwarf_section_display): Add entry for SFrame section. * dwarf.h (enum dwarf_section_display_enum): Add enumerator for SFrame. * objdump.c (dump_section_sframe): Remove. (dump_sframe_section): Add new definition. (dump_bfd): Use dump_sframe_section. * binutils/readelf.c (dump_section_as_sframe): Remove.
2025-06-25readelf: invalid error message triggered when last tag is an empty stringMatthieu Longo1-2/+6
Disclaimer: this issue cannot occur with Object Attributes v1 (OAv1) while using the GNU binutils because a value of '\0' (empty string) for a tag with a string value is considered as the default value for the attribute, and consequently is eliminated by gas from the output object file during the serialization. An empty string is a valid value for a NTBS tag in both OAv1 and OAv2 [1] cases. However, contrarily to OAv1, a OAv2 subsection can be required and so, tags in this subsection might have to be present even if the value is the default. To comply with this requirement, the OAv2 serializer won't drop the default values. In the case where a NTBS tag has the value '\0' and is last in the object attributes section, the current code in readelf used for dumping the object attributes incorrectly detects an overflow, and prints out an error message for a corrupted string tag. This patch fixes the detection of the overflow so that it now accept an empty string in the last tag of the object attributes section. It also fixes the previous tests for the empty NTBS case and the non-null terminated string one. The fix was also tested in the context of OAv2's patch series [1] where the issue was originally detected. No regression was found. [1]: https://inbox.sourceware.org/binutils/20250509151319.88725-1-matthieu .longo@arm.com/
2025-06-24RISC-V: Add GNU_PROPERTY_RISCV_FEATURE_1_CFI_SS and ↵Kito Cheng1-0/+39
GNU_PROPERTY_RISCV_FEATURE_1_CFI_LP_UNLABELED This patch adds two new GNU properties for RISC-V: GNU_PROPERTY_RISCV_FEATURE_1_CFI_SS and GNU_PROPERTY_RISCV_FEATURE_1_CFI_LP_UNLABELED. We only add readelf and define the properties in this patch. Ref: https://github.com/riscv-non-isa/riscv-elf-psabi-doc/pull/417
2025-03-04clean-up readelf: simplify and flatten body of process_attributesMatthieu Longo1-164/+153
- use find_section_by_type() instead of a for-loop. - reindent the whole function accordingly. - move declaration of variables nearer from their usage. - prune else branch by using a goto in the error case. diff --git a/binutils/readelf.c b/binutils/readelf.c index 6d3ec65a8a1..878012da8f0 100644 --- a/binutils/readelf.c +++ b/binutils/readelf.c @@ -19268,42 +19268,32 @@ process_attributes (Filedata * filedata, unsigned char * (* display_pub_attribute) (unsigned char *, const unsigned char * const), unsigned char * (* display_proc_gnu_attribute) (unsigned char *, unsigned int, const unsigned char * const)) { - Elf_Internal_Shdr * sect; - unsigned i; - bool res = true; - /* Find the section header so that we get the size. */ - for (i = 0, sect = filedata->section_headers; - i < filedata->file_header.e_shnum; - i++, sect++) - { - unsigned char * contents; - unsigned char * p; + Elf_Internal_Shdr * sect = find_section_by_type (filedata, proc_type); + if (sect == NULL) + sect = find_section_by_type (filedata, SHT_GNU_ATTRIBUTES); - if (sect->sh_type != proc_type && sect->sh_type != SHT_GNU_ATTRIBUTES) - continue; + if (sect == NULL) + /* No section, exit without error. */ + return true; - contents = (unsigned char *) get_data (NULL, filedata, sect->sh_offset, 1, - sect->sh_size, _("attributes")); + unsigned char * contents = (unsigned char *) + get_data (NULL, filedata, sect->sh_offset, 1, sect->sh_size, _("attributes")); if (contents == NULL) - { - res = false; - continue; - } + return false; - p = contents; + bool res = true; + unsigned char * p = contents; /* The first character is the version of the attributes. Currently only version 1, (aka 'A') is recognised here. */ if (*p != 'A') { printf (_("Unknown attributes version '%c'(%d) - expecting 'A'\n"), *p, *p); res = false; + goto free_data; } - else - { - uint64_t section_len; - section_len = sect->sh_size - 1; + uint64_t section_len = sect->sh_size - 1; p++; while (section_len > 0) @@ -19456,10 +19446,9 @@ process_attributes (Filedata * filedata, attr_len = 0; } } - } +free_data: free (contents); - } return res; }
2025-03-04clean-up: fix conflicting symbol with unknown from bfd/elf-bfd.hMatthieu Longo1-8/+8
2025-03-04clean-up: fix annoying spaces in binutils/readelf.cMatthieu Longo1-27/+27
2025-02-28readelf, objdump: fix ctf dict leakNick Alcock1-1/+4
ctf_archive_next returns an opened dict, which must be closed by the caller. Thanks to Alan Modra for spotting this. binutils/ * objdump.c (dump_ctf): Close dict. * readelf.c (dump_section_as_ctf): Likewise.
2025-01-16Have readelf sanitize the program interpreter string before displaying it.Nick Clifton1-41/+68
2025-01-14elf: Add GNU_PROPERTY_MEMORY_SEAL gnu propertyAdhemerval Zanella1-0/+6
The GNU_PROPERTY_MEMORY_SEAL gnu property is a way to mark binaries to be memory sealed by the loader, to avoid further changes of PT_LOAD segments (such as unmapping or change permission flags). This is done along with Linux kernel (the mseal syscall [1]), and C runtime supports to instruct the kernel on the correct time during program startup (for instance, after RELRO handling). This support is added along the glibc support to handle the new gnu property [2]. This is a opt-in security features, like other security hardening ones like NX-stack or RELRO. The new property is ignored if present on ET_REL objects, and only added on ET_EXEC/ET_DYN if the linker option is used. A gnu property is used instead of DT_FLAGS_1 flag to allow memory sealing to work with ET_EXEC without PT_DYNAMIC support (at least on glibc some ports still do no support static-pie). [1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=8be7258aad44b5e25977a98db136f677fa6f4370 [2] https://sourceware.org/pipermail/libc-alpha/2024-September/160291.html Change-Id: Id47fadabecd24be0e83cff45653f7ce9a900ecf4
2025-01-14ld: Add LTO and none-LTO output support for ld -rH.J. Lu1-0/+1
Link with mixed IR/non-IR objects * 2 kinds of object files o non-IR object file has * non-IR sections o IR object file has * IR sections * non-IR sections * The output of "ld -r" with mixed IR/non-IR objects should work with: o Compilers/linkers with IR support. o Compilers/linkers without IR support. * Add the mixed object file which has o IR sections o non-IR sections: * Object codes from IR sections. * Object codes from non-IR object files. o Object-only section: * With section name ".gnu_object_only" and SHT_GNU_OBJECT_ONLY type on ELF: https://gitlab.com/x86-psABIs/Linux-ABI #define SHT_GNU_OBJECT_ONLY 0x6ffffff8 /* Object only */ * Contain non-IR object file. * Input is discarded after link. * Linker action: o Classify each input object file: * If there is a ".gnu_object_only" section, it is a mixed object file. * If there is a IR section, it is an IR object file. * Otherwise, it is a non-IR object file. o Relocatable non-IR link: * Prepare for an object-only output. * Prepare for a regular output. * For each mixed object file: * Add IR and non-IR sections to the regular output. * For object-only section: * Extract object only file. * Add it to the object-only output. * Discard object-only section. * For each IR object file: * Add IR and non-IR sections to the regular output. * For each non-IR object file: * Add non-IR sections to the regular output. * Add non-IR sections to the object-only output. * Final output: * If there are IR objects, non-IR objects and the object-only output isn't empty: * Put the object-only output into the object-only section. * Add the object-only section to the regular output. * Remove the object-only output. o Normal link and relocatable IR link: * Prepare for output. * IR link: * For each mixed object file: * Compile and add IR sections to the output. * Discard non-IR sections. * Object-only section: * Extract object only file. * Add it to the output. * Discard object-only section. * For each IR object file: * Compile and add IR sections to the output. * Discard non-IR sections. * For each non-IR object file: * Add non-IR sections to the output. * Non-IR link: * For each mixed object file: * Add non-IR sections to the output. * Discard IR sections and object-only section. * For each IR object file: * Add non-IR sections to the output. * Discard IR sections. * For each non-IR object file: * Add non-IR sections to the output. This is useful for Linux kernel build with LTO. bfd/ PR ld/12291 PR ld/12430 PR ld/13298 * bfd.c (bfd_lto_object_type): Add lto_mixed_object. (bfd): Add object_only_section. (bfd_group_signature): New. * elf.c (special_sections_g): Add .gnu_object_only. * format.c: Include "plugin-api.h" and "plugin.h" if BFD_SUPPORTS_PLUGINS is defined. (bfd_set_lto_type): Set type to lto_mixed_object for GNU_OBJECT_ONLY_SECTION_NAME section. (bfd_check_format_matches): Don't check the plugin target twice if the plugin target is explicitly specified. * opncls.c (bfd_extract_object_only_section): New. * plugin.c (bfd_plugin_fake_text_section): New. (bfd_plugin_fake_data_section): Likewise. (bfd_plugin_fake_bss_section): Likewise. (bfd_plugin_fake_common_section): Likewise. (bfd_plugin_get_symbols_in_object_only): Likewise. * plugin.c (add_symbols): Call bfd_plugin_get_symbols_in_object_only and count plugin_data->object_only_nsyms. (bfd_plugin_get_symtab_upper_bound): Count plugin_data->object_only_nsyms. bfd_plugin_get_symbols_in_object_only and add symbols from object only section. (bfd_plugin_canonicalize_symtab): Remove fake_section, fake_data_section, fake_bss_section and fake_common_section. Set udata.p to NULL. Use bfd_plugin_fake_text_section, bfd_plugin_fake_data_section, bfd_plugin_fake_bss_section and bfd_plugin_fake_common_section. Set udata.p to NULL. * plugin.h (plugin_data_struct): Add object_only_nsyms and object_only_syms. * section.c (GNU_OBJECT_ONLY_SECTION_NAME): New. * bfd-in2.h: Regenerated. binutils/ PR ld/12291 PR ld/12430 PR ld/13298 * objcopy.c (group_signature): Removed. (is_strip_section): Replace group_signature with bfd_group_signature. (setup_section): Likewise. * readelf.c (get_os_specific_section_type_name): Handle SHT_GNU_OBJECT_ONLY. gas/ PR ld/12291 PR ld/12430 PR ld/13298 * testsuite/gas/elf/section9.s: Add the .gnu_object_only test. * testsuite/gas/elf/section9.d: Updated. include/ PR ld/12291 PR ld/12430 PR ld/13298 * elf/common.h (SHT_GNU_OBJECT_ONLY): New. ld/ PR ld/12291 PR ld/12430 PR ld/13298 * ld.h (ld_config_type): Add emit_gnu_object_only and emitting_gnu_object_only. * ldelf.c (orphan_init_done): Make it file scope. (ldelf_place_orphan): Rename hold to orig_hold. Initialize hold from orig_hold at run-time. (ldelf_finish): New. * ldelf.h (ldelf_finish): New. * ldexp.c (ldexp_init): Take a bfd_boolean argument to supprt object-only output. (ldexp_finish): Likewise. * ldexp.h (ldexp_init): Take a bfd_boolean argument. (ldexp_finish): Likewise. * ldfile.c (ldfile_try_open_bfd): Call cmdline_check_object_only_section. * ldlang.c: Include "ldwrite.h" and elf-bfd.h. * ldlang.c (cmdline_object_only_file_list): New. (cmdline_object_only_archive_list): Likewise. (cmdline_temp_object_only_list): Likewise. (cmdline_lists_init): Likewise. (cmdline_list_new): Likewise. (cmdline_list_append): Likewise. (print_cmdline_list): Likewise. (cmdline_on_object_only_archive_list_p): Likewise. (cmdline_object_only_list_append): Likewise. (cmdline_get_object_only_input_files): Likewise. (cmdline_arg): Likewise. (setup_section): Likewise. (copy_section): Likewise. (cmdline_fopen_temp): Likewise. (cmdline_add_object_only_section): Likewise. (cmdline_emit_object_only_section): Likewise. (cmdline_extract_object_only_section): Likewise. (cmdline_check_object_only_section): Likewise. (cmdline_remove_object_only_files): Likewise. (lang_init): Take a bfd_boolean argument to supprt object-only output. Call cmdline_lists_init. (load_symbols): Call cmdline_on_object_only_archive_list_p to check if an archive member should be loaded. (lang_process): Handle object-only link. * ldlang.h (lang_init): Take a bfd_boolean argument. (cmdline_enum_type): New. (cmdline_header_type): Likewise. (cmdline_file_type): Likewise. (cmdline_bfd_type): Likewise. (cmdline_union_type): Likewise. (cmdline_list_type): Likewise. (cmdline_emit_object_only_section): Likewise. (cmdline_check_object_only_section): Likewise. (cmdline_remove_object_only_files): Likewise. * ldmain.c (main): Call xatexit with cmdline_remove_object_only_files. Pass FALSE to lang_init, ldexp_init and ldexp_finish. Use ld_parse_linker_script. Set link_info.output_bfd to NULL after close. Call cmdline_emit_object_only_section if needed. (add_archive_element): Call cmdline_check_object_only_section. (ld_parse_linker_script): New. * ldmain.h (ld_parse_linker_script): New. * plugin.c (plugin_maybe_claim): Call cmdline_check_object_only_section on claimed IR files. * scripttempl/elf.sc: Also discard .gnu_object_only sections. * scripttempl/elf64hppa.sc: Likewise. * scripttempl/elfxtensa.sc: Likewise. * scripttempl/mep.sc: Likewise. * scripttempl/pe.sc: Likewise. * scripttempl/pep.sc: Likewise. * emultempl/aarch64elf.em (gld${EMULATION_NAME}_finish): Replace finish_default with ldelf_finish. * emultempl/alphaelf.em (alpha_finish): Likewise. * emultempl/avrelf.em (avr_finish): Likewise. * emultempl/elf.em (ld_${EMULATION_NAME}_emulation): Likewise. * emultempl/ppc32elf.em (ppc_finish): Likewise. * emultempl/ppc64elf.em (gld${EMULATION_NAME}_finish): Likewise. * emultempl/spuelf.em (gld${EMULATION_NAME}_finish): Likewise. * testsuite/ld-plugin/lto-10.out: New file. * testsuite/ld-plugin/lto-10a.c: Likewise. * testsuite/ld-plugin/lto-10b.c: Likewise. * testsuite/ld-plugin/lto-10r.d: Likewise. * testsuite/ld-plugin/lto-4.out: Likewise. * testsuite/ld-plugin/lto-4a.c: Likewise. * testsuite/ld-plugin/lto-4b.c: Likewise. * testsuite/ld-plugin/lto-4c.c: Likewise. * testsuite/ld-plugin/lto-4r-a.d: Likewise. * testsuite/ld-plugin/lto-4r-b.d: Likewise. * testsuite/ld-plugin/lto-4r-c.d: Likewise. * testsuite/ld-plugin/lto-4r-d.d: Likewise. * testsuite/ld-plugin/lto.exp (lto_link_tests): Prepare for "LTO 4[acd]", "lto-4r-[abcd]" and "LTO 10" tests. (lto_run_tests): Add "LTO 4[acd]" and "LTO 10" tests. Build liblto-4.a. Run "lto-4r-[abcd]" tests. Run lto-10r and create tmpdir/lto-10.o. Add test for nm on mixed LTO/non-LTO object. Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
2025-01-01readelf memory leaksAlan Modra1-11/+21
This fixes multiple readelf memory leaks: - The check functions used to validate separate debug info files opened and read file data but didn't release the memory nor close the file. - A string table was being re-read into a buffer, leaking the old contents. - Decompressed section contents leaked. * dwarf.c (check_gnu_debuglink): Always call close_debug_file. (check_gnu_debugaltlink): Likewise. * readelf.c (process_section_headers): Don't read string_table again if we already have it. (maybe_expand_or_relocate_section): Add decomp_buf param to return new uncompressed buffer. (dump_section_as_strings, filedata->string_table): Free any uncompressed buffer. (process_file): Call close_debug_file rather than freeing various filedata components.
2025-01-01Update year range in copyright notice of binutils filesAlan Modra1-1/+1
2024-12-02aarch64: GCS feature check in GNU note properties for input objectsSrinath Parvathaneni1-0/+4
This patch adds support for Guarded Control Stack in AArch64 linker. This patch implements the following: 1) Defines GNU_PROPERTY_AARCH64_FEATURE_1_GCS bit for GCS in GNU_PROPERTY_AARCH64_FEATURE_1_AND macro. 2) Adds readelf support to read and print the GCS feature in GNU properties in AArch64. Displaying notes found in: .note.gnu.property [ ]+Owner[ ]+Data size[ ]+Description GNU 0x00000010 NT_GNU_PROPERTY_TYPE_0 Properties: AArch64 feature: GCS 3) Adds support for the "-z gcs" linker option and document all the values allowed with this option (-z gcs[=always|never|implicit]) where "-z gcs" is equivalent to "-z gcs=always". When '-z gcs' option is omitted from the command line, it defaults to "implicit" and relies on the GCS feature marking in GNU properties. 4) Adds support for the "-z gcs-report" linker option and document all the values allowed with this option (-z gcs-report[=none|warning|error]) where "-z gcs-report" is equivalent to "-z gcs-report=warning". When this option is omitted from the command line, it defaults to "warning". The ABI changes adding GNU_PROPERTY_AARCH64_FEATURE_1_GCS to the GNU property GNU_PROPERTY_AARCH64_FEATURE_1_AND is merged into main and can be found in [1]. [1] https://github.com/ARM-software/abi-aa/blob/main/sysvabi64/sysvabi64.rst Co-authored-by: Matthieu Longo <matthieu.longo@arm.com> Co-authored-by: Yury Khrustalev <yury.khrustalev@arm.com>
2024-10-24Replace uses of asprintf with xasprintfAlan Modra1-13/+6
xasprintf has a nicer interface and behaves like xmalloc as far as memory is concerned, ie. no need to check a return status and the program exits with an error on OOM. binutils/ * dwarf.c (load_debug_sup_file): Replace asprintf with xasprintf. * nm.c (get_elf_symbol_type, get_coff_symbol_type): Likewise. * objdump.c (dump_ctf_indent_lines): Likewise. * readelf.c (display_lto_symtab, dump_ctf_indent_lines): Likewise. * windres.c (main): Likewise. * configure.ac: Remove asprintf from AC_CHECK_DECLS. * config.in: Regenerate. * configure: Regenerate. gas/ * config/tc-kvx.c (kvx_emit_single_noop): Simplify. * config/tc-riscv.c (md_assemblef): Replace asprintf with xasprintf. * read.c (s_nop, do_s_func): Likewise. * stabs.c (stabs_generate_asm_func): Likewise. (stabs_generate_asm_endfunc): Likewise. * configure.ac: Remove asprintf from AC_CHECK_DECLS. * config.in: Regenerate. * configure: Regenerate. ld/ * ldlang.c (lang_leave_overlay_section): Replace xmalloc+sprintf with xasprintf. Localise vars. * lexsup.c (parse_args): Replace asprintf with xasprintf. * pe-dll.c (make_head, make_tail, make_one): Likewise. (make_singleton_name_thunk, make_import_fixup_entry): Likewise. (make_runtime_pseudo_reloc): Likewise. (pe_create_runtime_relocator_reference): Likewise. * configure.ac: Remove asprintf from AC_CHECK_DECLS. * config.in: Regenerate. * configure: Regenerate.
2024-07-26Fix "Untranslated plural in readelf.c"Nick Clifton1-9/+10
PR 32002
2024-05-29readelf: Use section names for displaying RELR relocsSzabolcs Nagy1-8/+24
In some cases using section names instead of symbol names for displaying an address is more useful. If the symbol falls outside the section where the address is then likely it is not useful to display the address relative to. And if symbols are stripped from a binary then printing the section that contains the address is more useful than printing <no sym>.
2024-05-29readelf: Fix symbol display for RELR relocsSzabolcs Nagy1-17/+125
Filter symbols before binary searching for the right symbol to display for a given address, such that only displayable symbols are present and at most one per address. The current logic does not handle multiple symbols for the same address well if some of them are empty, the selected symbol is not stable with respect to an unrelated symbol table change and on aarch64 often mapping symbols are displayed which is not useful. Filtering solves these problems at the cost of a linear scan of the sorted symbol table. The heuristic to select the best symbol likely could be improved, this patch aims to improve symbol display for RELR without complex logic such that the output is useful and stable for ld tests.
2024-05-28Add new ELF section and segment types to readelf.Nick Clifton1-234/+383
2024-05-20readelf: add pretty printing for FDO Dlopen Metadata noteLuca Boccassi1-0/+7
2024-05-14arm: binutils: drop Maverick support.Richard Earnshaw1-4/+0
Remove the decoding of the Maverick flag from readelf.
2024-04-24Update readelf's display of RELR sections to include the number of locations ↵Nick Clifton1-20/+115
relocated
2024-04-16Simplify readelf's RELR relocation display.Fangrui Song1-17/+3
2024-04-11Improve readelf's display of RELR relocs.Nick Clifton1-129/+285
2024-04-11Add -j/--display-section option to readelf.Nick Clifton1-133/+238
2024-03-25bfd: make _bfd_section_size_insane part of the public APIAndrew Burgess1-1/+1
If a BFD user is making use of a function like bfd_get_section_contents to read a section into a pre-allocated buffer, then that BFD user might also want to make use of _bfd_section_size_insane prior to allocating the buffer they intend to use in order to validate that the buffer size that plan to allocate is sane. This commit makes _bfd_section_size_insane public, by renaming it to bfd_section_size_insane. I've updated the existing uses within bfd/, I don't believe this function is used outside of bfd/ currently. One place that I plan to make use of this function is in gdb/gdb_bfd.c, in the function gdb_bfd_get_full_section_contents. This change isn't included in this commit, but will come later if/when this has been merged into bfd. There should be no change in behaviour after this commit. bfd/ * bfd-in2.h (bfd_section_size_insane): Add declaration. * compress.c (bfd_get_full_section_contents): Update for new name of _bfd_section_size_insane. (bfd_init_section_compress_status): Likewise. * dwarf2.c (read_section): Likewise. (_bfd_dwarf2_slurp_debug_info): Likewise. * libbfd.h (_bfd_section_size_insane): Remove declaration. * section.c (_bfd_section_size_insane): Rename to ... (bfd_section_size_insane): ... this. binutils/ * readelf.c (uncompress_section_contents): Update comment to account for new name of _bfd_section_size_insane.
2024-03-19Remove redunant test of ELF size in core note decoder.Nick Clifton1-7/+0
PR 31469
2024-02-12Add support to readelf for the PT_OPENBSD_SYSCALLS segment type.Frederic Cambus1-0/+1
binutils * readelf.c (get_segment_type): Handle PT_OPENBSD_SYSCALLS segment type. include * elf/common.h (PT_OPENBSD_SYSCALLS): Define.
2024-01-19Update readelf's and objdump's debug frame displaying feature to include the ↵Nick Clifton1-0/+2
contents of the .eh_frame_hdr section, if present.
2024-01-04Update year range in copyright notice of binutils filesAlan Modra1-1/+1
Adds two new external authors to etc/update-copyright.py to cover bfd/ax_tls.m4, and adds gprofng to dirs handled automatically, then updates copyright messages as follows: 1) Update cgen/utils.scm emitted copyrights. 2) Run "etc/update-copyright.py --this-year" with an extra external author I haven't committed, 'Kalray SA.', to cover gas testsuite files (which should have their copyright message removed). 3) Build with --enable-maintainer-mode --enable-cgen-maint=yes. 4) Check out */po/*.pot which we don't update frequently.
2023-12-28x86: Add NT_X86_SHSTK noteSchimpe, Christina1-0/+2
Define NT_X86_SHSTK which is the note for x86 Shadow Stack (SHSTK) to support Intel SHSTK in Linux kernel. For now only userspace shadow stack and kernel IBT are supported by the linux kernel. This note should be used instead of NT_X86_CET introduced in the commit "x86: Add NT_X86_CET note", as it is outdated and only used by old binutils versions.
2023-11-14Improve objdump's handling of compressed sections.Nick Clifton1-146/+84
PR 31062 * objdump.c (decompressed_dumps): New local variable. (usage): Mention the -z/--decompress option. (long_options): Add --decompress. (dump_section_header): Add "COMPRESSED" to the Flags field of any compressed section. (dump_section): Warn users when dumping a compressed section. (display_any_bfd): Decompress the section if decompressed_dumps is true. (main): Handle the -z/--decompress option. * NEWS: Mention the new feature. * doc/binutils.texi: Document the new feature. * testsuite/binutils-all/objdump.s: Update expected output. * testsuite/binutils-all/objdump.exp: Add test of -Z -s. * testsuite/binutils-all/objdump.Zs: New file. * readelf.c (maybe_expand_or_relocate_section): New function. Contains common code found in dump functions. Adds a note message if a compressed section is not being decompressed. (dump_section_as_strings): Use new function. (dump_section_as_bytes): Likewise.
2023-11-10bfd, binutils: add gfx11 amdgpu architecturesSimon Marchi1-0/+3
Teach bfd and readelf about some recent gfx11 architectures. This code is taken from the rocgdb 5.7.x branch [1]. [1] https://github.com/rocm-Developer-Tools/rocgdb/tree/rocm-5.7.x bfd/ChangeLog: * archures.c (bfd_mach_amdgcn_gfx1100, bfd_mach_amdgcn_gfx1101, bfd_mach_amdgcn_gfx1102): New. * bfd-in2.h (bfd_mach_amdgcn_gfx1100, bfd_mach_amdgcn_gfx1101, bfd_mach_amdgcn_gfx1102): New. * cpu-amdgcn.c (arch_info_struct): Add entries for bfd_mach_amdgcn_gfx1100, bfd_mach_amdgcn_gfx1101, bfd_mach_amdgcn_gfx1102. binutils/ChangeLog: * readelf.c (decode_AMDGPU_machine_flags): Handle gfx1100, gfx1101, gfx1102. include/ChangeLog: * elf/amdgpu.h (EF_AMDGPU_MACH_AMDGCN_GFX1100, EF_AMDGPU_MACH_AMDGCN_GFX1101, EF_AMDGPU_MACH_AMDGCN_GFX1102): New. Change-Id: I95a8a62942e359781a1c9fa2079950fbcf2a78b8 Co-Authored-By: Laurent Morichetti <laurent.morichetti@amd.com> Cc: Lancelot Six <lancelot.six@amd.com>
2023-11-10MIPS: Change all E_MIPS_* to EF_MIPS_*Ying Huang1-38/+38
2023-10-20bfd: microblaze: Add 32_NONE reloc typeNeal Frager1-0/+4
This patch adds the R_MICROBLAZE_32_NONE relocation type. This is a 32-bit reloc that stores the 32-bit pc relative value in two words (with an imm instruction). Add test case to gas test suite. Signed-off-by: Neal Frager <neal.frager@amd.com> Signed-off-by: Michael J. Eager <eager@eagercon.com>