aboutsummaryrefslogtreecommitdiff
path: root/bfd/elf.c
AgeCommit message (Collapse)AuthorFilesLines
2023-08-21aarch64/sme2: Teach binutils/BFD about the NT_ARM_ZT register setLuis Machado1-0/+37
The Scalable Matrix Extension v2 (SME2) defines a new register, ZT0, that the Linux Kernel handles through a new NT_ARM_ZT register set. Teach binutils/BFD about it so that gdb can make use of it for reading and writing core files. This also enables readelf/objdump to show the correct identification for the NT_ARM_ZT register set. Validated under Fast Models.
2023-08-21aarch64/sme: Core file supportEzra Sitorus1-0/+58
Add required code to support core file dumps with NT_ARM_ZA and NT_ARM_SSVE register sets in them. These new register sets are dumped when SME is supported.
2023-08-09Rename bfd_bread and bfd_bwriteAlan Modra1-9/+7
These were renamed from bfd_read and bfd_write back in 2001 when they lost an unnecessary parameter. Rename them back, and get rid of a few casts that are only needed without prototyped functions (K&R C).
2023-06-30Fix used-before-initialized warnings when compiling elf.c with Clang-16.Nick Clifton1-2/+4
2023-06-29bfd: Improve nm and objdump without section headerH.J. Lu1-58/+671
When there is no section header in an executable or shared library, we reconstruct dynamic symbol table from the PT_DYNAMIC segment, which contains DT_HASH/DT_GNU_HASH/DT_MIPS_XHASH, DT_STRTAB, DT_SYMTAB, DT_STRSZ, and DT_SYMENT entries, to improve nm and objdump. For DT_HASH, the number of dynamic symbol table entries equals the number of chains. For DT_GNU_HASH/DT_MIPS_XHASH, only defined symbols with non-STB_LOCAL indings are in hash table. Since DT_GNU_HASH/DT_MIPS_XHASH place all symbols with STB_LOCAL binding before symbols with other bindings and all undefined symbols defined ones in dynamic symbol table, the highest symbol index in DT_GNU_HASH/DT_MIPS_XHASH is the highest dynamic symbol table index. We can also get symbol version from DT_VERSYM, DT_VERDEF and DT_VERNEED entries. dt_symtab, dt_versym, dt_verdef, dt_verneed, dt_symtab_count, dt_verdef_count, dt_verneed_count and dt_strtab are added to elf_obj_tdata to store dynamic symbol table information. PR ld/25617 * elf-bfd.h (elf_obj_tdata): Add dt_symtab, dt_verdef, dt_verneed, dt_symtab_count, dt_verdef_count, dt_verneed_count and dt_strtab. (elf_use_dt_symtab_p): New. (_bfd_elf_get_dynamic_symbols): Likewise. (_bfd_elf_get_section_from_dynamic_symbol): Likewise. * elf.c (bfd_elf_get_elf_syms): Use dynamic symbol table if neeeded. (_bfd_elf_get_dynamic_symtab_upper_bound): Likewise. (_bfd_elf_slurp_version_tables): Likewise. (offset_from_vma): New function. (get_hash_table_data): Likewise. (_bfd_elf_get_dynamic_symbols): Likewise. (_bfd_elf_get_section_from_dynamic_symbol): Likewise. (_bfd_elf_get_symbol_version_name): Likewise. * elfcode.h (elf_object_p): Call _bfd_elf_get_dynamic_symbols to reconstruct dynamic symbol table from PT_DYNAMIC segment if there is no section header. (elf_slurp_symbol_table): Use dynamic symbol table if neeeded. Don't free isymbuf when dynamic symbol table is used. * elflink.c (elf_link_is_defined_archive_symbol): Return wrong format error when dynamic symbol table is used. (elf_link_add_object_symbols): Likewise.
2023-06-29ELF: Discard non-alloc sections without section headerH.J. Lu1-3/+10
Discard non-alloc sections when section headers are stripped. bfd/ PR ld/25617 * elf.c (_bfd_elf_assign_file_positions_for_non_load): Skip non-load sections without section header. (_bfd_elf_write_object_contents): Don't set the sh_name field without section header. Write out the .shstrtab section only if its sh_offset field isn't -1. binutils/ PR ld/25617 * objcopy.c (is_strip_section_1): Remove non-alloc sections for --strip-section-headers. ld/ PR ld/25617 * ldlang.c (lang_discard_section_p): Discard non-alloc sections if we are stripping section headers.
2023-06-14Re: bfd/elf.c strtab memory leakAlan Modra1-9/+9
There are other places that leak the strtab. * elf.c (_bfd_elf_compute_section_file_positions): Free strtab on error paths.
2023-06-07_bfd_free_cached_infoAlan Modra1-7/+8
doc/bfdint.texi and comments in the aout and som code about this function are just wrong, and its name is not very apt. Better would be _bfd_mostly_destroy, and we certainly should not be saying anything about the possibility of later recreating anything lost by this function. What's more, if _bfd_free_cached_info is called when creating an archive map to reduce memory usage by throwing away symbols, the target _close_and_cleanup function won't have access to tdata or section bfd_user_data to tidy memory. This means most of the target _close_and_cleanup function won't do anything, and therefore sometimes will result in memory leaks. This patch fixes the documentation problems and moves most of the target _close_and_cleanup code to target _bfd_free_cached_info. Another notable change is that bfd_generic_bfd_free_cached_info is now defined as _bfd_free_cached_info rather than _bfd_bool_bfd_true, ie. the default now frees objalloc memory.
2023-06-07bfd/elf.c strtab memory leakAlan Modra1-1/+5
* elf.c (_bfd_elf_compute_section_file_positions): Free strtab on set_group_contents failure return path.
2023-06-05ELF: Don't warn an empty PT_LOAD with the program headersH.J. Lu1-0/+2
When rewriting the program headers, don't warn an empty PT_LOAD with the program headers. bfd/ PR binutils/30508 * elf.c (rewrite_elf_program_header): Don't warn if an empty PT_LOAD contains the program headers. ld/ PR binutils/30508 * testsuite/ld-elf/pr30508.d: New file. * testsuite/ld-elf/pr30508.s: Likewise.
2023-05-03Move bfd_elf_bfd_from_remote_memory to opncls.cAlan Modra1-25/+0
bfd_elf_bfd_from_remote_memory is just a wrapper, and the function could be implemented for other formats. Move it to opncls.c because it acts a little like some of the other bfd_open* routines. Also give it the usual FUNCTION etc. comment so prototypes and docs are handled automatically. * elf.c (bfd_elf_bfd_from_remote_memory): Move to.. * opncls.c: ..here, add FUNCTION comment. * bfd-in.h (bfd_elf_bfd_from_remote_memory): Delete prototype. * bfd-in2.h: Regenerate.
2023-04-12PR30326, uninitialised value in objdump compare_relocsAlan Modra1-7/+10
This is a fuzzing PR, with a testcase involving a SHF_ALLOC and SHF_COMPRESSED SHT_RELA section, ie. a compressed dynamic reloc section. BFD doesn't handle compressed relocation sections, with most of the code reading relocs using sh_size (often no bfd section is created) but in the case of SHF_ALLOC dynamic relocs we had some code using the bfd section size. This led to a mismatch, sh_size is compressed, size is uncompressed, and from that some uninitialised memory. Consistently using sh_size is enough to fix this PR, but I've also added tests to exclude SHF_COMPRESSED reloc sections from consideration. PR 30362 * elf.c (bfd_section_from_shdr): Exclude reloc sections with SHF_COMPRESSED flag from normal reloc processing. (_bfd_elf_get_dynamic_reloc_upper_bound): Similarly exclude SHF_COMPRESSED sections from consideration. Use sh_size when sizing to match slurp_relocs. (_bfd_elf_canonicalize_dynamic_reloc): Likewise. (_bfd_elf_get_synthetic_symtab): Use NUM_SHDR_ENTRIES to size plt relocs. * elf32-arm.c (elf32_arm_get_synthetic_symtab): Likewise. * elf32-ppc.c (ppc_elf_get_synthetic_symtab): Likewise. * elf64-ppc.c (ppc64_elf_get_synthetic_symtab): Likewise. * elfxx-mips.c (_bfd_mips_elf_get_synthetic_symtab): Likewise.
2023-04-11bfd: optimize bfd_elf_hashNathan Sidwell1-20/+11
The bfd_elf_hash loop is taken straight from the sysV document, but it is poorly optimized. This refactoring removes about 5 x86 insns from the 15 insn loop. 1) The if (..) is meaningless -- we're xoring with that value, and of course xor 0 is a nop. On x86 (at least) we actually compute the xor'd value and then cmov. Removing the if test removes the cmov. 2) The 'h ^ g' to clear the top 4 bits is not needed, as those 4 bits will be shifted out in the next iteration. All we need to do is sink a mask of those 4 bits out of the loop. 3) anding with 0xf0 after shifting by 24 bits can allow betterin encoding on RISC ISAs than masking with '0xf0 << 24' before shifting. RISC ISAs often require materializing larger constants. bfd/ * elf.c (bfd_elf_hash): Refactor to optimize loop. (bfd_elf_gnu_hash): Refactor to use 32-bit type.
2023-03-30Fix an illegal memory access when an accessing a zer0-lengthverdef table.Nick Clifton1-0/+5
PR 30285 * elf.c (_bfd_elf_slurp_version_tables): Fail if no version definitions are allocated.
2023-03-30Setting sh_link for SHT_REL/SHT_RELAAlan Modra1-14/+16
It's wrong to have an alloc reloc section trying to use a non-alloc symbol table. * elf.c (assign_section_numbers <SHT_REL, SHT_RELA>): Correct comment. Always set sh_link to .dynsym for alloc reloc sections and to .symtab for non-alloc.
2023-03-24Tidy dwarf1 cached section contentsAlan Modra1-0/+1
* dwarf1.c (_bfd_dwarf1_cleanup_debug_info): New function. * libbfd-in.h (_bfd_dwarf1_cleanup_debug_info): Declare. * elf.c (_bfd_elf_close_and_cleanup): Call it. * elf-bfd.h (struct elf_obj_tdata): Make dwarf1_find_line_info a void*. * libbfd.h: Regenerate.
2023-03-19rewrite_elf_program_header and want_p_paddr_set_to_zeroAlan Modra1-7/+8
Layout in rewrite_elf_program_header is really done by lma, even if program headers are going to have their p_paddr forced to zero. Thus when not matching against an existing segment, don't try to use a "vma" from elf_segment_map. * elf.c (is_contained_by): Replace "bed" param with "use_vaddr". (IS_SECTION_IN_INPUT_SEGMENT): Adjust is_contained_by call. (rewrite_elf_program_header): Always match against lma in calls to is_contained_by using new maps.
2023-03-16readelf: add support for QNT_STACK note subsectionsClément Chigot1-9/+4
QNX provides some .note subsections. QNT_STACK is the one controling the stack allocation. bfd/ChangeLog: * elf.c (BFD_QNT_CORE_INFO): Delete. (BFD_QNT_CORE_STATUS): Likewise. (BFD_QNT_CORE_GREG): Likewise. (BFD_QNT_CORE_FPREG): Likewise. (elfcore_grok_nto_note): Replace BFD_QNT_* by QNT_*. binutils/ChangeLog: * readelf.c (get_qnx_elfcore_note_type): New function. (print_qnx_note): New function. (process_note): Add support for QNX support. include/ChangeLog: * elf/common.h (QNT_DEBUG_FULLPATH): New define. (QNT_DEBUG_RELOC): New define. (QNT_STACK): New define. (QNT_GENERATOR): New define. (QNT_DEFAULT_LIB): New define. (QNT_CORE_SYSINFO): New define. (QNT_CORE_INFO): New define. (QNT_CORE_STATUS): New define. (QNT_CORE_GREG): New define. (QNT_CORE_FPREG): New define. (QNT_LINK_MAP): New define.
2023-03-02Don't write zeros to a gap in the output fileAlan Modra1-20/+6
Writing out zeros is counterproductive if a file system supports sparse files. A very large gap need not take much actual disk space, but it usually will if zeros are written. memory_bseek also supports not writing out zeros in a gap. * elf.c (write_zeros): Delete. (assign_file_positions_for_load_sections): Don't call write_zeros. Comment.
2023-02-23Test SEC_HAS_CONTENTS before reading section contentsAlan Modra1-1/+1
bfd_malloc_and_get_section does size sanity checking before allocating memory and reading contents. These size checks are not done for bss style sections, because they typically don't occupy file space and thus can't be compared against file size. However, if you are expecting to look at something other than a whole lot of zeros, don't allow fuzzers to avoid the size checking. * cofflink.c (process_embedded_commands): Don't look at sections without SEC_HAS_CONTENTS set. * cpu-arm.c (bfd_arm_update_notes): Likewise. (bfd_arm_get_mach_from_notes): Likewise. * elf-eh-frame.c (_bfd_elf_parse_eh_frame): Likewise. * elf-hppa.h (elf_hppa_sort_unwind): Likewise. * elf-m10300.c (mn10300_elf_relax_section): Likewise. * elf-sframe.c (_bfd_elf_parse_sframe): Likewise. * elf.c (_bfd_elf_print_private_bfd_data): Likewise. * elf32-arm.c (bfd_elf32_arm_process_before_allocation): Likewise. * elf32-avr.c (avr_elf32_load_property_records): Likewise. * elf32-ppc.c (_bfd_elf_ppc_set_arch): Likewise. (ppc_elf_get_synthetic_symtab, ppc_elf_relax_section): Likewise. * elf64-ppc.c (ppc64_elf_get_synthetic_symtab): Likewise. (opd_entry_value, ppc64_elf_edit_opd, ppc64_elf_edit_toc): Likewise. * elf64-x86-64.c (elf_x86_64_get_synthetic_symtab): Likewise. * elflink.c (elf_link_add_object_symbols): Likewise. (bfd_elf_get_bfd_needed_list): Likewise. * elfnn-aarch64.c (get_plt_type): Likewise. * elfxx-mips.c (_bfd_mips_elf_get_synthetic_symtab): Likewise. * linker.c (_bfd_handle_already_linked): Likewise. * opncls.c (bfd_get_debug_link_info_1): Likewise. (bfd_get_alt_debug_link_info, get_build_id): Likewise. * peXXigen.c (pe_print_idata, pe_print_pdata): Likewise. (_bfd_XX_print_ce_compressed_pdata, pe_print_reloc): Likewise. * pei-x86_64.c (pex64_bfd_print_pdata_section): Likewise. * stabs.c (_bfd_link_section_stabs): Likewise. (_bfd_discard_section_stabs): Likewise. * xcofflink.c (_bfd_xcoff_get_dynamic_symtab_upper_bound): Likewise. (_bfd_xcoff_canonicalize_dynamic_symtab): Likewise. (_bfd_xcoff_get_dynamic_reloc_upper_bound): Likewise. (_bfd_xcoff_canonicalize_dynamic_reloc): Likewise. (xcoff_link_add_dynamic_symbols): Likewise. (xcoff_link_check_dynamic_ar_symbols): Likewise. (bfd_xcoff_build_dynamic_sections): Likewise.
2023-01-01Update year range in copyright notice of binutils filesAlan Modra1-1/+1
The newer update-copyright.py fixes file encoding too, removing cr/lf on binutils/bfdtest2.c and ld/testsuite/ld-cygwin/exe-export.exp, and embedded cr in binutils/testsuite/binutils-all/ar.exp string match.
2022-12-17asan: elf.c:12621:18: applying zero offset to null pointerAlan Modra1-1/+1
That's this line in elf_parse_notes: while (p < buf + size) * elf.c (_bfd_elf_make_section_from_shdr): Don't call elf_parse_notes when sh_size is zero.
2022-12-07_bfd_elf_slurp_secondary_reloc_section sanity checkAlan Modra1-3/+14
* elf.c (_bfd_elf_slurp_secondary_reloc_section): Sanity check section header against file size. Avoid overflow in reloc_count.
2022-12-07Compression tidy and fixesAlan Modra1-2/+2
Tidies: - Move stuff from bfd-in.h and libbfd.c to compress.c - Delete COMPRESS_DEBUG from enum compressed_debug_section_type - Move compress_debug field out of link_info to ld_config. Fixes: - Correct test in bfd_convert_section_setup to use obfd flags, not ibfd. - Apply bfd_applicable_file_flags to compression bfd flags added by gas and ld to the output bfd. bfd/ * bfd-in.h (enum compressed_debug_section_type), (struct compressed_type_tuple), (bfd_get_compression_algorithm), (bfd_get_compression_algorithm_name), * libbfd.c (compressed_debug_section_names), (bfd_get_compression_algorithm), (bfd_get_compression_algorithm_name): Move.. * compress.c: ..to here, deleting COMPRESS_DEBUG from enum compressed_debug_section_type. (bfd_convert_section_setup): Test obfd flags not ibfd for compression flags. * elf.c (elf_fake_sections): Replace link_info->compress_debug test with abfd->flags test. * bfd-in2.h: Regenerate. binutils/ * objcopy.c (copy_file): Tidy setting of bfd compress flags. Expand comment. gas/ * write.c (compress_debug): Test bfd compress flags rather than flag_compress_debug. (write_object_file): Apply bfd_applicable_file_flags to compress debug flags added to output bfd. include/ * bfdlink.h (struct bfd_link_info): Delete compress_debug. ld/ * ld.h (ld_config_type): Add compress_debug. * emultempl/elf.em: Replace references to link_info.compress_debug with config.compress_debug. * lexsup.c (elf_static_list_options): Likewise. * ldmain.c (main): Likewise. Apply bfd_applicable_file_flags to compress debug flags added to output bfd.
2022-12-06Get rid of SEC_ELF_COMPRESSAlan Modra1-76/+55
This flag also isn't needed, except for some sanity checks which we can omit. * elf.c (elf_fake_sections): Don't set SEC_ELF_COMPRESS for compressed debug sections, just leave sh_name as -1. (assign_file_positions_for_non_load_sections), (assign_file_positions_except_relocs): Decide whether a section needs compressing and thus should not have its file offset set by looking at sh_name. (_bfd_elf_assign_file_positions_for_non_load): Similarly decide which sections need compressing. * elflink.c (bfd_elf_final_link): Don't test SEC_ELF_COMPRESS. * merge.c (_bfd_write_merged_section): Likewise. * section.c (SEC_ELF_COMPRESS): Don't define. (SEC_ELF_PURECODE): Renumber. * bfd-in2.h: Regenerate.
2022-12-06Get rid of SEC_ELF_RENAMEAlan Modra1-73/+22
SEC_ELF_RENAME is a flag used to effect section name changes when compressing/decompressing zlib-gnu debug sections. This can be accomplished more directly in one of the objcopy specific bfd functions. Renaming for ld input is simplified too. Ld input object files always have BFD_DECOMPRESS set. bfd/ * compress.c (bfd_convert_section_size): Rename to.. (bfd_convert_section_setup): ..this. Handle objcopy renaming of compressed/decompressed debug sections. * elf.c (_bfd_elf_make_section_from_shdr): Only rename zdebug input for linker. (elf_fake_sections): Don't handle renaming of debug sections for objcopy here. * section.c (SEC_ELF_RENAME): Delete. * bfd-in2.h: Regenerate. binutils/ * objcopy.c (setup_section): Call bfd_convert_section_setup. Don't call bfd_convert_section_size.
2022-12-06Compression header enumAlan Modra1-3/+3
Define an enum instead of using ELFCOMPRESS_ZLIB and ELFCOMPRESS_ZSTD in bfd and binutils, and move some functions from bfd.c to compress.c. When looking at the COFF/PE debug compression support, I wondered about extending it to support zstd. I likely won't do that, but the compression header ch_type field isn't just ELF specific if these headers are to be used in COFF/PE too. bfd/ * bfd.c (bfd_update_compression_header), (bfd_check_compression_header, bfd_get_compression_header_size), (bfd_convert_section_size, bfd_convert_section_contents): Move to.. * compress.c: ..here. (enum compression_type): New. Use it throughout file. * elf.c (_bfd_elf_make_section_from_shdr): Replace uses of ELFCOMPRESS_ZLIB and ELFCOMPRESS_ZSTD with ch_compress_zlib and ch_compress_zstd. * bfd-in2.h: Regenerate. binutils/ * readelf.c (process_section_headers, dump_section_as_strings), (dump_section_as_bytes, load_specific_debug_section): Replace uses of ELFCOMPRESS_ZLIB and ELFCOMPRESS_ZSTD with ch_compress_zlib and ch_compress_zstd.
2022-12-04Renaming .debug to .zdebug and vice versaAlan Modra1-30/+4
Move a couple of elf.c functions to compress.c. * compress.c (bfd_debug_name_to_zdebug): New inline function. (bfd_zdebug_name_to_debug): Likewise. * elf.c (convert_debug_to_zdebug, convert_zdebug_to_debug): Delete. (_bfd_elf_make_section_from_shdr, elf_fake_sections), (_bfd_elf_assign_file_positions_for_non_load): Adjust to suit. * coffgen.c (make_a_section_from_file): Use new inlines here.
2022-11-15bfd: linker: merge .sframe sectionsIndu Bhagat1-0/+32
The linker merges all the input .sframe sections. When merging, the linker verifies that all the input .sframe sections have the same abi/arch. The linker uses libsframe library to perform key actions on the .sframe sections - decode, read, and create output data. This implies buildsystem changes to make and install libsframe before libbfd. The linker places the output .sframe section in a new segment of its own: PT_GNU_SFRAME. A new segment is not added, however, if the generated .sframe section is empty. When a section is discarded from the final link, the corresponding entries in the .sframe section for those functions are also deleted. The linker sorts the SFrame FDEs on start address by default and sets the SFRAME_F_FDE_SORTED flag in the .sframe section. This patch also adds support for generation of SFrame unwind information for the .plt* sections on x86_64. SFrame unwind info is generated for IBT enabled PLT, lazy/non-lazy PLT. The existing linker option --no-ld-generated-unwind-info has been adapted to include the control of whether .sframe unwind information will be generated for the linker generated sections like PLT. Changes to the linker script have been made as necessary. ChangeLog: * Makefile.def: Add install dependency on libsframe for libbfd. * Makefile.in: Regenerated. * bfd/Makefile.am: Add elf-sframe.c * bfd/Makefile.in: Regenerated. * bfd/bfd-in2.h (SEC_INFO_TYPE_SFRAME): Regenerated. * bfd/configure: Regenerate. * bfd/configure.ac: Add elf-sframe.lo. * bfd/elf-bfd.h (struct sframe_func_bfdinfo): New struct. (struct sframe_dec_info): Likewise. (struct sframe_enc_info): Likewise. (struct elf_link_hash_table): New member for encoded .sframe object. (struct output_elf_obj_tdata): New member. (elf_sframe): New access macro. (_bfd_elf_set_section_sframe): New declaration. * bfd/elf.c (get_segment_type): Handle new segment PT_GNU_SFRAME. (bfd_section_from_phdr): Likewise. (get_program_header_size): Likewise. (_bfd_elf_map_sections_to_segments): Likewise. * bfd/elf64-x86-64.c (elf_x86_64_link_setup_gnu_properties): Add contents to the .sframe sections or .plt* entries. * bfd/elflink.c (elf_section_ignore_discarded_relocs): Handle SEC_INFO_TYPE_SFRAME. (_bfd_elf_default_action_discarded): Handle .sframe section. (elf_link_input_bfd): Merge .sframe section. (bfd_elf_final_link): Write the output .sframe section. (bfd_elf_discard_info): Handle discarding .sframe section. * bfd/elfxx-x86.c (_bfd_x86_elf_size_dynamic_sections): Create .sframe section for .plt and .plt.sec. (_bfd_x86_elf_finish_dynamic_sections): Handle .sframe from .plt* sections. * bfd/elfxx-x86.h (PLT_SFRAME_FDE_START_OFFSET): New definition. (SFRAME_PLT0_MAX_NUM_FRES): Likewise. (SFRAME_PLTN_MAX_NUM_FRES): Likewise. (struct elf_x86_sframe_plt): New structure. (struct elf_x86_link_hash_table): New member. (struct elf_x86_init_table): New members for .sframe creation. * bfd/section.c: Add new definition SEC_INFO_TYPE_SFRAME. * binutils/readelf.c (get_segment_type): Handle new segment PT_GNU_SFRAME. * ld/ld.texi: Update documentation for --no-ld-generated-unwind-info. * ld/scripttempl/elf.sc: Support .sframe sections. * ld/Makefile.am (TESTSFRAMELIB): Use it. (check-DEJAGNU): Likewise. * ld/Makefile.in: Regenerated. * ld/configure.ac (TESTSFRAMELIB): Set to the .so or .a like TESTBFDLIB. * ld/configure: Regenerated. * bfd/elf-sframe.c: New file. include/ChangeLog: * elf/common.h (PT_GNU_SFRAME): New definition. * elf/internal.h (struct elf_segment_map): Handle new segment type PT_GNU_SFRAME. ld/testsuite/ChangeLog: * ld/testsuite/ld-bootstrap/bootstrap.exp: Add SFRAMELIB. * ld/testsuite/ld-aarch64/aarch64-elf.exp: Add new test sframe-simple-1. * ld/testsuite/ld-aarch64/sframe-bar.s: New file. * ld/testsuite/ld-aarch64/sframe-foo.s: Likewise. * ld/testsuite/ld-aarch64/sframe-simple-1.d: Likewise. * ld/testsuite/ld-sframe/sframe-empty.d: New test. * ld/testsuite/ld-sframe/sframe-empty.s: New file. * ld/testsuite/ld-sframe/sframe.exp: New testsuite. * ld/testsuite/ld-x86-64/sframe-bar.s: New file. * ld/testsuite/ld-x86-64/sframe-foo.s: Likewise. * ld/testsuite/ld-x86-64/sframe-simple-1.d: Likewise. * ld/testsuite/ld-x86-64/sframe-plt-1.d: Likewise. * ld/testsuite/ld-x86-64/sframe-simple-1.d: Likewise. * ld/testsuite/ld-x86-64/x86-64.exp: Add new tests - sframe-simple-1, sframe-plt-1. * ld/testsuite/lib/ld-lib.exp: Add new proc to check if assembler supports SFrame section. * ld/testsuite/ld-sframe/discard.d: New file. * ld/testsuite/ld-sframe/discard.ld: Likewise. * ld/testsuite/ld-sframe/discard.s: Likewise.
2022-10-26Correct ELF reloc size sanity checkAlan Modra1-6/+11
The external reloc size check was wrong. Here asect is the code/data section, not the reloc section. So using this_hdr gave the size of the code/data section. * elf.c (_bfd_elf_get_reloc_upper_bound): Properly get external size from reloc headers.
2022-10-19Fix an illegal memory access when parsing an ELF file containing corrupt ↵Nick Clifton1-1/+3
symbol version information. PR 29699 * elf.c (_bfd_elf_slurp_version_tables): Fail if the sh_info field of the section header is zero.
2022-10-04Support objcopy changing compression to or from zstdAlan Modra1-45/+48
Commit 2cac01e3ffff lacked support for objcopy changing compression style. Add that support, which meant a rewrite of bfd_compress_section_contents. In the process I've fixed some memory leaks. * compress.c (bfd_is_section_compressed_info): Rename from bfd_is_section_compressed_with_header and add ch_type param to return compression header ch_type field. Update all callers. (decompress_section_contents): Remove buffer and size params. Rewrite. Update callers. (bfd_init_section_compress_status): Free contents on failure. (bfd_compress_section): Likewise. * elf.c (_bfd_elf_make_section_from_shdr): Support objcopy changing between any of the three compression schemes. Report "unable to compress/decompress" rather than "unable to initialize compress/decompress status" on compress/decompress failures. * bfd-in2.h: Regenerate.
2022-10-04Re: compress .gnu.debuglto_.debug_* sections if requestedAlan Modra1-3/+4
Enable zlib-gnu compression for .gnu.debuglto_.debug_*. This differs from zlib-gnu for .debug_* where the name is changed to .zdebug_*. The name change isn't really needed. bfd/ * elf.c (elf_fake_sections): Replace "." with ".z" in debug section names only when name was ".d*", ie. ".debug_*". (_bfd_elf_assign_file_positions_for_non_load): Likewise. gas/ * write.c (compress_debug): Compress .gnu.debuglto_.debug_* for zlib-gnu too. Compress .gnu.linkonce.wi.*.
2022-10-04compress .gnu.debuglto_.debug_* sections if requestedMartin Liska1-4/+3
Right now, when using LTO, the intermediate object files do contain debug info in sections starting with .gnu.debuglto_ prefix and are not compressed when --compress-debug-sections is used. It's a mistake and we can save quite some disk space. The following example comes from tramp3d when the corresponding LTO sections are compressed with zlib: $ bloaty tramp3d-v4-v2.o -- tramp3d-v4.o FILE SIZE VM SIZE -------------- -------------- +83% +10 [ = ] 0 [Unmapped] -68.0% -441 [ = ] 0 .gnu.debuglto_.debug_line -52.3% -759 [ = ] 0 .gnu.debuglto_.debug_line_str -62.4% -3.24Ki [ = ] 0 .gnu.debuglto_.debug_abbrev -64.8% -1.12Mi [ = ] 0 .gnu.debuglto_.debug_info -88.8% -4.58Mi [ = ] 0 .gnu.debuglto_.debug_str -27.7% -5.70Mi [ = ] 0 TOTAL bfd/ChangeLog: * elf.c (_bfd_elf_make_section_from_shdr): Compress all debug info sections. gas/ChangeLog: * write.c (compress_debug): Compress also ".gnu.debuglto_.debug_" if the compression algorithm is different from zlib-gnu.
2022-09-26binutils, gdb: support zstd compressed debug sectionsFangrui Song1-0/+12
PR29397 PR29563: Add new configure option --with-zstd which defaults to auto. If pkgconfig/libzstd.pc is found, define HAVE_ZSTD and support zstd compressed debug sections for most tools. * bfd: for addr2line, objdump --dwarf, gdb, etc * gas: support --compress-debug-sections=zstd * ld: support ELFCOMPRESS_ZSTD input and --compress-debug-sections=zstd * objcopy: support ELFCOMPRESS_ZSTD input for --decompress-debug-sections and --compress-debug-sections=zstd * gdb: support ELFCOMPRESS_ZSTD input. The bfd change references zstd symbols, so gdb has to link against -lzstd in this patch. If zstd is not supported, ELFCOMPRESS_ZSTD input triggers an error. We can avoid HAVE_ZSTD if binutils-gdb imports zstd/ like zlib/, but this is too heavyweight, so don't do it for now. ``` % ld/ld-new a.o ld/ld-new: a.o: section .debug_abbrev is compressed with zstd, but BFD is not built with zstd support ... % ld/ld-new a.o --compress-debug-sections=zstd ld/ld-new: --compress-debug-sections=zstd: ld is not built with zstd support % binutils/objcopy --compress-debug-sections=zstd a.o b.o binutils/objcopy: --compress-debug-sections=zstd: binutils is not built with zstd support % binutils/objcopy b.o --decompress-debug-sections binutils/objcopy: zstd.o: section .debug_abbrev is compressed with zstd, but BFD is not built with zstd support ... ```
2022-09-26PR29613, use of uninitialized value in objcopyAlan Modra1-8/+9
PR 29613 * elf.c (_bfd_elf_write_secondary_reloc_section): Trim sh_size back to relocs written. Use better types for vars.
2022-09-26stab nearest_line bfd_malloc_and_get_sectionAlan Modra1-0/+1
bfd_malloc_and_get_section performs some sanity checks on the section size before allocating memory. This patch avails the stab nearest_line code of that sanity checking, and tidies up memory afterward. * coffgen.c (_bfd_coff_close_and_cleanup): Call _bfd_stab_cleanup. * elf.c (_bfd_elf_close_and_cleanup): Likewise. * syms.c (_bfd_stab_section_find_nearest_line): Set *pinfo earlier. Use bfd_malloc_and_get_section. Free malloc'd buffers on failure. Malloc indextable. (_bfd_stab_cleanup): New function. * libbfd-in.h (_bfd_stab_cleanup): Declare. * libbfd.h: Regnerate.
2022-09-23Don't attempt to compress bss sectionsAlan Modra1-1/+2
It doesn't make sense to try to compress a section without contents since those sections take no space on disk. Compression can only increase the disk image size. * coffgen.c (make_a_section_from_file): Exclude !SEC_HAS_CONTENTS sections from compression and decompression. * elf.c (_bfd_elf_make_section_from_shdr): Likewise.
2022-09-21PR29566, objdump -p considers an empty .gnu.version_r invalidAlan Modra1-5/+6
Allow and ignore an empty section. PR 29566 * elf.c (bfd_section_from_shdr): Don't set elf_dynverdef or elf_dynverref for empty sections. (_bfd_elf_slurp_version_tables): Remove now redundant tests.
2022-09-09Support debuginfo files with empty group sections.Nick Clifton1-1/+5
PR 29532 bfd * elf.c (setup_group): Do not return false if there is no group information available. bionutils* objcopy.c (setup_section): Leave group sections intact when creating separate debuginfo files.
2022-09-06bfd: Add bfd_find_nearest_line_with_altAaron Merey1-5/+27
bfd_find_nearest_line_with_alt functions like bfd_find_nearest_line with the addition of a parameter for specifying the filename of a supplementary debug file such as one referenced by .gnu_debugaltlink or .debug_sup. This patch focuses on implementing bfd_find_nearest_line_with_alt support for ELF/DWARF2 .gnu_debugaltlink. For other targets this function simply sets the invalid_operation bfd_error.
2022-08-23SHT_RELR sh_link and sh_infoAlan Modra1-0/+1
I don't think it makes any sense for a SHT_RELR section to specify a symbol table with sh_link. SHT_RELR relocations don't use symbols. There is no real need to specify sh_info either, SHT_RELR is not for relocatable object files. Anyway, fuzzers of course don't restrict themselves to even half-sensible objects. So they found a hole in objcopy using a non-alloc SHT_RELR in an ET_EXEC. In that case BFD set up the SHT_RELR section as if it were a SHT_REL against the sh_info target section. When it came to reading in the target section relocs, the count was horribly wrong which caused a buffer overflow. * elf.c (bfd_section_from_shdr <SHT_RELR>): Always just make a normal section, don't treat it as a reloc section.
2022-08-23Re: bfd_elf_set_group_contents assertionAlan Modra1-5/+19
Further to commit 7744e3278b9f. * elf.c (bfd_elf_set_group_contents): Restrict loc in loop writing contents, and add another assertion.
2022-08-17bfd_elf_set_group_contents assertionAlan Modra1-1/+6
objcopy of broken SHT_GROUP sections shouldn't write garbage. * elf.c (bfd_elf_set_group_contents): If number of entries is unexpected, fill out section with zeros.
2022-08-17elf.c tidyAlan Modra1-138/+160
Swap params of is_note, so they are section, segment like others used in rewrite_elf_program_header. Whitespace fixes, plus wrapping of overlong lines.
2022-08-16PR29495, rewrite_elf_program_header loopingAlan Modra1-65/+75
This patch, in order of significance: 1) Replaces some macros with inline functions. 2) Those inline functions catch and avoid arithmetic overflows when comparing addresses. 3) When assigning sections to segments (IS_SECTION_IN_INPUT_SEGMENT) use bed->want_p_paddr_set_to_zero to decide whether lma vs p_paddr or vma vs p_vaddr should be tested. When remapping, use the same test, and use is_note rather than the more restrictive IS_COREFILE_NOTE. It's important that the later tests not be more restrictive. If they are it can lead to the situation triggered by the testcases, where a section seemingly didn't fit and thus needed a new mapping. It didn't fit the new mapping either, and this repeated until memory exhausted. PR 29495 * elf.c (SEGMENT_END, SECTION_SIZE, IS_CONTAINED_BY_VMA): Delete. (IS_CONTAINED_BY_LMA, IS_NOTE, IS_COREFILE_NOTE): Delete. (segment_size, segment_end, section_size): New inline function. (is_contained_by, is_note): Likewise. (rewrite_elf_program_header): Use new functions.
2022-08-05asan: ppc64_elf_get_synthetic_symtab heap buffer overflowAlan Modra1-8/+3
Fuzzed input files with sizes of .dynamic not a multiple of dynamic tag size can result in reading past the end of the buffer with the current simple checks. Fix that, and use the same check in other files that process input object .dynamic section. (There is no need for buffer overflow checks in the linker's generated .dynamic section.) * elf32-ppc.c (ppc_elf_get_synthetic_symtab): Sanity check .dynamic content buffer reads. * elf64-ppc.c (ppc64_elf_get_synthetic_symtab): Likewise. * elf64-ia64-vms.c (elf64_vms_link_add_object_symbols): Likewise. * elf.c (_bfd_elf_print_private_bfd_data): Simplify .dynamic buffer sanity checks. * elflink.c (elf_link_add_object_symbols): Avoid possible UB subtracting sizeof_dyn from pointer.
2022-08-04Don't use BFD_VMA_FMT in binutilsAlan Modra1-1/+1
BFD_VMA_FMT can't be used in format strings that need to be translated, because the translation won't work when the type of bfd_vma differs from the machine used to compile .pot files. We've known about this for a long time, but patches slip through review. So just get rid of BFD_VMA_FMT, instead using the appropriate PRId64, PRIu64, PRIx64 or PRIo64 and SCN variants for scanf. The patch is mostly mechanical, the only thing requiring any thought is casts needed to preserve PRId64 output from bfd_vma values, or to preserve one of the unsigned output formats from bfd_signed_vma values.
2022-08-03elf: Reset alignment for each PT_LOAD segmentH.J. Lu1-3/+4
Reset alignment for each PT_LOAD segment to avoid using alignment from the previous PT_LOAD segment. bfd/ PR ld/29435 * elf.c (assign_file_positions_for_load_sections): Reset alignment for each PT_LOAD segment. ld/ PR ld/29435 * testsuite/ld-elf/pr29435.d: New file. * testsuite/ld-elf/pr29435.s: Likewise.
2022-08-02ELF: emit symbol table when there are relocationsJan Beulich1-2/+4
Even when there are no symbols (e.g. all relocations being against absolute values), a symbol table (with just the first placeholder entry) needs to be emitted. Otherwise tools like objdump won't properly process the relocations. The respective checks in assign_section_numbers() and _bfd_elf_compute_section_file_positions() support also this view. Oddly enough so far HAS_RELOC was only set when reading in an object file, but not when generating one anew; the flag would only have been cleared when no relocations were found (anymore). While there also amend the affected function's leading comment to also mention gas.