aboutsummaryrefslogtreecommitdiff
path: root/ld/ldlang.c
AgeCommit message (Collapse)AuthorFilesLines
2024-05-10Add --section-ordering command line option to the bfd linker.Nick Clifton1-23/+58
2024-02-28Fix implementation of SUBALIGN.Vladislav Belov1-5/+8
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-18ld: Print 0 size in B and not in GBTorbjörn SVENSSON1-1/+3
When using --print-memory-usage, the printed size can be zero and in that case, the unit should be B and not GB. ld/ * ldlang.c (lang_print_memory_size) Print 0 B instead of 0 GB. * testsuite/ld-scripts/print-memory-usage-1.l: Validate emplty region. * testsuite/ld-scripts/print-memory-usage-1.t: Define empty region. Signed-off-by: Torbjörn SVENSSON <torbjorn.svensson@foss.st.com>
2023-11-01ld: Support input section description keyword: REVERSENick Clifton1-8/+36
PR 27565 * ldlex.l: Add REVERSE. * ldgram.y: Allow REVERSE to be used wherever a sorting command can be used. * ld.h (struct wildcard_spec): Add 'reversed' field. * ldlang.h (lang_wild_statement_struct): Add 'filenames_reversed' field. * ldlang.c (compare_sections): Add reversed parameter. (wild_sort): Reverse the comparison if requested. (print_wild_statement): Handle the reversed field. * ld.texi: Document the new feature. * NEWS: Mention the new feature. * testsuite/ld-scripts/sort-file-reversed-1.d: New test driver. * testsuite/ld-scripts/sort-file-reversed-1.t: New test source. * testsuite/ld-scripts/sort-file-reversed-2.t: New test source. * testsuite/ld-scripts/sort-file-reversed-2.d: New test driver. * testsuite/ld-scripts/sort-sections-reversed-1.d: New test driver. * testsuite/ld-scripts/sort-sections-reversed-1.t: New test source. * testsuite/ld-scripts/sort-sections-reversed-2.t: New test source. * testsuite/ld-scripts/sort-sections-reversed-2.d: New test driver. * testsuite/ld-scripts/sort-sections-reversed-3.d: New test driver. * testsuite/ld-scripts/sort-sections-reversed-3.t: New test source.
2023-10-09Fix: A potential null_pointer_deference bugNick Clifton1-0/+3
PR 30954 * ldlang.c (map_input_to_output_sections): Check that os is non NULL before using it.
2023-09-27Fix: Output section type does not been applied to section forced output by ↵Hsinyuan Xavier1-2/+18
`. = .` assignment PR 30875 * ldlang.c (get_os_init_flag): New function. (exp_init_os, map_input_to_output_sections): Use it.
2023-08-02Revert "2.41 Release sources"Sam James1-69/+72
This reverts commit 675b9d612cc59446e84e2c6d89b45500cb603a8d. See https://sourceware.org/pipermail/binutils/2023-August/128761.html.
2023-08-022.41 Release sourcesbinutils-2_41-releaseNick Clifton1-72/+69
2023-07-18PR 30632 - ld segfaults if linker script includes a STARTUP line.Pter Chubb1-3/+9
2023-07-17Support NEXT_SECTION in ALIGNOF and SIZEOFAlan Modra1-69/+72
This patch is aimed at making __bss_start properly aligned with the first of any bss-style sections following. Most of the work here involves keeping track of the last output section seen when processing the linker script. You can almost align __bss_start properly by using ${RELOCATING+. = ALIGN(${DATA_SDATA-${NO_SMALL_DATA-ALIGNOF(.${SBSS_NAME}) != 0 ? ALIGNOF(.${SBSS_NAME}) : }}${BSS_PLT+ALIGNOF(.plt) != 0 ? ALIGNOF(.plt) : }ALIGNOF(.${BSS_NAME}));} and changing every place that defines NO_SMALL_DATA to use " ", but having two .plt sections (marked SPECIAL) on some backends foils that idea. The problem is that you only want to pick up the following .plt, not the one preceeding __bss_start in the data segment if the backend decides that is the proper .plt section. Perhaps that could be fixed too, but I decided instead to extend the linker script a little. THIS_SECTION and PREV_SECTION could easily be added too. * ld.texi (ALIGNOF, SIZEOF): Update and mention NEXT_SECTION. * ldexp.c (output_section_find): New function. (fold_name <ALIGNOF, SIZEOF>): Use output_section_find. (exp_fold_tree): Add os parameter. Adjust all calls. (exp_fold_tree_no_dot, exp_get_vma, exp_get_power): Likewise. * ldexp.h (struct ldexp_control): Add last_os. (exp_fold_tree, exp_fold_tree_no_dot): Update prototypes. (exp_get_vma, exp_get_power): Likewise. * ldlang.c: Pass last output section to expression folder calls throughout file. (open_input_bfds): Add os parameter to track last os seen. (lang_size_sections_1): Rename output_section_statement param to current_os. Track last os. (lang_do_assignments_1): Track last os. * scripttempl/arclinux.sc: Align to ALIGNOF NEXT_SECTION before defining __bss_start. * scripttempl/elf.sc: Likewise. * scripttempl/elf64bpf.sc: Likewise. * scripttempl/elf64hppa.sc: Likewise. * scripttempl/elf_chaos.sc: Likewise. * scripttempl/elfarc.sc: Likewise. * scripttempl/elfd10v.sc: Likewise. * scripttempl/elfxtensa.sc: Likewise. * scripttempl/epiphany_4x4.sc: Likewise. * scripttempl/iq2000.sc: Likewise. * scripttempl/mep.sc: Likewise. * scripttempl/nds32elf.sc: Likewise. * scripttempl/xstormy16.sc: Likewise. * testsuite/ld-x86-64/pe-x86-64-5.od: Update expected __bss_start. * testsuite/ld-x86-64/pe-x86-64-5.rd: Likewise.
2023-06-29ELF: Discard non-alloc sections without section headerH.J. Lu1-0/+4
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-29ELF: Strip section header in ELF objectsKaylee Blake1-0/+4
Section header isn't mandatory on ELF executable nor shared library. This patch adds a new linker option, -z nosectionheader, to omit ELF section header, a new objcopy and strip option, --strip-section-headers, to remove ELF section headers. bfd/ 2023-06-06 H.J. Lu <hongjiu.lu@intel.com> Kaylee Blake <klkblake@gmail.com> PR ld/25617 * bfd.c (BFD_NO_SECTION_HEADER): New. (BFD_FLAGS_SAVED): Add BFD_NO_SECTION_HEADER. (BFD_FLAGS_FOR_BFD_USE_MASK): Likewise. * elfcode.h (elf_swap_ehdr_out): Omit section header with BFD_NO_SECTION_HEADER. (elf_write_shdrs_and_ehdr): Likewise. * elfxx-target.h (TARGET_BIG_SYM): Add BFD_NO_SECTION_HEADER to object_flags. (TARGET_LITTLE_SYM): Likewise. * bfd-in2.h: Regenerated. binutils/ 2023-06-06 H.J. Lu <hongjiu.lu@intel.com> PR ld/25617 * NEWS: Mention --strip-section-headers for objcopy and strip. * objcopy.c (strip_section_headers): New. (command_line_switch): Add OPTION_STRIP_SECTION_HEADERS. (strip_options): Add --strip-section-headers. (copy_options): Likewise. (copy_usage): Add --strip-section-headers. (strip_usage): Likewise. (copy_object): Handle --strip-section-headers for ELF files. (strip_main): Handle OPTION_STRIP_SECTION_HEADERS. (copy_main): Likewise. * doc/binutils.texi: Document --strip-section-headers for objcopy and strip. ld/ 2023-06-06 H.J. Lu <hongjiu.lu@intel.com> Kaylee Blake <klkblake@gmail.com> PR ld/25617 * NEWS: Mention -z nosectionheader. * emultempl/elf.em: Support -z sectionheader and -z nosectionheader. * ld.h (ld_config_type): Add no_section_header. * ld.texi: Document -z sectionheader and -z nosectionheader. * ldlang.c (ldlang_open_output): Handle config.no_section_header. * lexsup.c (parse_args): Enable --strip-all with -z nosectionheader. Disallow -r with -z nosectionheader. (elf_static_list_options): Add -z sectionheader and -z nosectionheader.
2023-06-28section-match: Check parent archive name as wellMichael Matz1-2/+13
rewriting the section matching routines lost a special case of matching: section statements of the form NAME(section-glob) normally match against NAME being an object file, but like in the exclude list we happened to accept archive names as NAME (undocumented). The documented way to specify (all) archive members is by using e.g. lib.a:(section-glob) (that does work also with the prefix tree matcher). But I intended to not actually change behaviour with the prefix tree implementation. So, let's also implement checking against archive names with a similar FIXME comment we already have in walk_wild_file_in_exclude_list. PR 30590 ld/ * ldlang.c (walk_wild_section_match): Also look at archive parents for a name match.
2023-06-14Add --remap-inputs option to the BFD linker.Nick Clifton1-0/+7
PR 30374 * ldfile.c (struct input_remap): New structure. (ldfile_add_remap): New function. (ldfile_remap_input_free): New function. (ldfile_add_remap_file): New function. (ldfile_possibly_remap_input): New function. (ldfile_print_input_remaps): New function. * ldfile.h: Add prototypes for new functions. * ldlang.c (new_afile): Call ldfile_possibly_remap_input. (lang_finish): Call ldfile_remap_input_free. (lang_map): Call ldfile_print_input_remaps. * ldlex.h (OPTION_REMAP_INPUTS, OPTION_REMAP_INPUTS_FILE): Define. * lexsup.c (ld_options): Add --remap-inputs-file and --remap-inputs. (parse_args): Handle new options. * NEWS: Mention the new feature. * ld.texi: Document the new options. * testsuite/ld-misc/input-remap.exp: New test driver. * testsuite/ld-misc/remaps.r: New file: Expected linker output. * testsuite/ld-misc/remaps.txt: New file. Input remaps file.
2023-05-10Add linker option to include local symbols in the linker map.Nick Clifton1-2/+86
PR 16566 * ldlang.c (ld_is_local_symbol): New function. (print_input_section): Add code to display local symbols in the section. * ldlex.h (enum option_values): Add OPTION_PRINT_MAP_LOCALS and OPTION_PRINT_MAP_LOCALS. * lexsup.c (ld_options[]): Add entries for --print-map-locals and --no-print-map-locals. * NEWS: Mention the new feature. * ld.h (struct ld_config_type): Add print_map_locals field. * ld.texi: Document the new command line option. * testsuite/ld-scripts/sizeof.s: Add a local symbol. * testsuite/ld-scripts/map-locals.d: New test control file. * testsuite/ld-scripts/map-address.exp: Run the new test.
2023-04-27Fix PR30358, performance with --sort-sectionMichael Matz1-31/+61
since af31506c we only use the binary tree when section sorting is required. While its unbalanced and hence can degrade to a linear list it should otherwise have been equivalent to the old code relying on insertion sort. Unfortunately it was not. The old code directly used lang_add_section to populate the sorted list, the new code first populates the tree and only then does lang_add_section on the sorted result. In the testcase we have very many linkonce section groups, and hence lang_add_section won't actually insert anything for most of them. That limited the to-be-sorted list length previously. The tree-sorting code OTOH first created a tree of all candidates sections, including those that wouldn't be inserted by lang_add_section, hence increasing the size of the sorting problem. In the testcase the chain length went from about 1500 to 106000, and in the degenerated case (as in the testcase) that goes in quadratically. This splits out most of the early-out code from lang_add_section to its own function and uses the latter to avoid inserting into the tree. This refactoring slightly changes the order of early-out tests (the ones based on section flags is now done last, and only in lang_add_section). The new function is not a pure predicate: it can give warnings and it might change output_section, like the old early-out code did. I have also added a skip-warning case in the first discard case, whose non-existence seemed to have been an oversight. PR 30358 * ldlang.c (wont_add_section_p): Split out from ... (lang_add_section): ... here. (output_section_callback_sort): Use wont_add_section_p to not always add sections to the sort tree.
2023-04-25section-select: Fix performance problem (PR30367)Michael Matz1-4/+12
when using many wild-statements with non-wildcard filenames we were running into quadraticness via repeatedly using lookup_name on a long list of loaded files. I've originally retained using lookup_name because that preserved existing behaviour most obviously. In particular in matching wild-statements when using a non-wildcard filename it matches against local_sym_name, not the filename member. If the wildspec would have an archive-spec or a wildcard it would use the filename member, though. Also it would load the named file (and ignore it, as being not equal to the currently considered input-statement). Rewrite this to not use lookup_name but retain the comparison against local_sym_name with a comment to that effect. PR 30367 * ldlang.c (walk_wild_section_match): Don't use lookup_name but directly compare spec and local_sym_name.
2023-04-20PR30343 infrastructureAlan Modra1-8/+8
Make ldemul_before_plugin_all_symbols_read more useful. * ldlang.c (lang_process): Move call to ldemul_before_plugin_all_symbols_read outside BFD_SUPPORTS_PLUGINS. Allow backends to add to gc_sym_list before handling entry sym. * ldelf.c (ldelf_before_plugin_all_symbols_read): Test lto_plugin_active.
2023-03-15Add --enable-linker-version option to bfd linker to add an entry in the ↵Nick Clifton1-2/+26
.comment section. PR 30187 * NEWS: Mention the new feature. * ld.texi: Document the new feature. * ldgram.y: Handle LINKER_VERSION token. * ldlang.c (lang_add_version): New function. (enable_linker_version): New global variable. * ldlang.h (land_add_version): Prototype. (enable_linker_version): Export. * ldlex.h (OPTION_ENABLE_LINKER_VERSION): Define. (OPTION_DISABLE_LINKER_VERSION): Define. * ldlex.l (LINKER_VERSION): Add token. * lexsup.c (ld_options): Add --enable-linker-version and --disable-linker-version. (parse_args): Handle the new options. * scripttempl/arclinux.sc: Remove stabs and comment sections and replace with inclusion of misc-sections.sc * scripttempl/avr.sc: Likewise. * scripttempl/dlx.sc: Likewise. * scripttempl/elf.sc: Likewise. * scripttempl/elf32cr16.sc: Likewise. * scripttempl/elf32crx.sc: Likewise. * scripttempl/elf32msp430.sc: Likewise. * scripttempl/elf64bpf.sc: Likewise. * scripttempl/elf64hppa.sc: Likewise. * scripttempl/elf_chaos.sc: Likewise. * scripttempl/elfarc.sc: Likewise. * scripttempl/elfarcv2.sc: Likewise. * scripttempl/elfd10v.sc: Likewise. * scripttempl/elfd30v.sc: Likewise. * scripttempl/elfm68hc11.sc: Likewise. * scripttempl/elfm68hc12.sc: Likewise. * scripttempl/elfm9s12z.sc: Likewise. * scripttempl/elfmicroblaze.sc: Likewise. * scripttempl/elfxgate.sc: Likewise. * scripttempl/elfxtensa.sc: Likewise. * scripttempl/epiphany_4x4.sc: Likewise. * scripttempl/ft32.sc: Likewise. * scripttempl/ip2k.sc: Likewise. * scripttempl/iq2000.sc: Likewise. * scripttempl/mep.sc: Likewise. * scripttempl/nds32elf.sc: Likewise. * scripttempl/pru.sc: Likewise. * scripttempl/v850.sc: Likewise. * scripttempl/v850_rh850.sc: Likewise. * scripttempl/visium.sc: Likewise. * scripttempl/xstormy16.sc: Likewise. * scripttempl/z80.sc: Likewise. * testsuite/ld-scripts/script.exp: Run new tests. * scripttempl/misc-sections.sc: New file. * testsuite/ld-scripts/ld-version-2.d: New file. * testsuite/ld-scripts/ld-version.d: New file. * testsuite/ld-scripts/ld-version.t: New file.
2023-03-10Revert ld ASCII supportAlan Modra1-53/+16
Revert "Prevent the ASCII linker script directive from generating huge amounts of padding if the size expression is not a constant." This reverts commit adbe951fc95943016325af08d677f18e8c177ac1. Revert "ld test asciz and ascii fails" This reverts the ascii.d part of commit 5f497256bee624f0fa470949aa41534093bc5b25. Revert "Add support for the ASCII directive inside linker scripts." This mostly reverts commit 9fe129a4105bb59398f73ce96938a94f19265b79 leaving the asciz.d and asciz.t changes in place.
2023-03-10Revert ld DIGEST supportAlan Modra1-8/+0
This is a hopefully temporary reversion of new ld features for embedded processors by Ulf Samuelsson, plus some followup patches. Squashed together from the following: Revert "lddigest 32-bit support and gcc-4 compile errors" This reverts commit d7ee19be87110a8f5342cec6e323d83d01c641d1. Revert "ld: Use correct types for crc64 calculations" This reverts commit 9a534b9f8e3d0f3cdb5a20f19ff165693fbb84d2. Revert "Re: DIGEST: testsuite" This reverts commit c8e85484d8a0fe9f7b88e00a6b9ae63bcb53ba32. Revert "Regen potfiles" This reverts commit 4d98c966f8bf305ab25badd34cb295631873cf7c. Revert "DIGEST: Makefile.*" This reverts commit 78ef6ab03f56ce83a606d974bb8a9f34b5d6e0b7. Revert "DIGEST: calculation" This reverts commit 5243990191e683d5066d3dd622c76deaba0bf15c. Revert "DIGEST: ldlang.*: add timestamp" This reverts commit bd9466d4aa277a469a9d8b12f0a6e6fa51678e36. Revert "DIGEST: ldmain.c" This reverts commit c8f8653fa7eeb3dc0769ac23039eadb5c5f09dff. Revert "DIGEST: ldgram.y" This reverts commit d73c01be2669e9c5267fab669a269f95a32048c9. Revert "DIGEST: ldlex.l" This reverts commit 48b5163a9dd5759cc87171331bbd6e902c547b5a. Revert "DIGEST: testsuite" This reverts commit a4135d1a4886400ea29af2da782dd8dd40ccad23. Revert "DIGEST: Documentation" This reverts commit 3ec28966c3e4c63704212778f96c517cbf2e0090. Revert "DIGEST: NEWS" This reverts commit 099bf2927d446424e8585a60cf4ce63209999aa2. Revert "DIGEST: LICENSING" This reverts commit 5c8a0c6654fb55926985edf3b360b62d4f20691d.
2023-03-07DIGEST: ldlang.*: add timestampUlf Samuelsson1-0/+8
Signed-off-by: Ulf Samuelsson <ulf@emagii.com>
2023-02-24PR30155, ld segfault in _bfd_nearby_sectionAlan Modra1-13/+52
The segfault was a symptom of messing with the absolute section next field, confusing bfd_section_removed_from_list in linker.c:fix_syms. That's not all that was going wrong. The INSERT list of output sections was being inserted into itself, ie. lost from the main list of linker statements. PR 30155 * ldlang.c (process_insert_statements): Handle pathological case of the insert script being inserted before the first output section statement in the default script. (output_prev_sec_find): Don't test section owner here. (insert_os_after): Change parameter to a list union pointer. (lang_insert_orphan): Test section owner here and adjust insert_os_after call.
2023-02-16Add support for the ASCII directive inside linker scripts.Nick Clifton1-16/+53
* ldlex.l: Add ASCII token. * ldgram.y: Add parsing of the ASCII command. * ldlang.c (lang_add_string): Add maximum size parameter. Move escape character handling code into separate function. * ldlang.h (lang_add_string): Update prototype. * NEWS: Mention the new feature. * ld.texi (Output Section Data): Document the new directives. * testsuite/ld-scripts/asciz.t: Adjust to work on more architectures and to test more aspects of the ASCIZ directive. * testsuite/ld-scripts/asciz.d: Adjust to match the changes to the test linker script. * testsuite/ld-scripts/ascii.d: New test driver. * testsuite/ld-scripts/ascii.s: New test assembler source. * testsuite/ld-scripts/ascii.t: New test script. * testsuite/ld-scripts/script.exp: Run the new test.
2023-02-14ASCIZ Command for output sectionUlf Samuelsson1-0/+83
Adds a new directive to the linker script syntax: ASCIZ. This inserts a zero-terminated string into the output at the place where it is used.
2023-02-13Fix PR30079: abort on mingwMichael Matz1-1/+2
the early-out in wild_sort is not enough, it might still be that filenames are equal _and_ the wildcard list doesn't specify a sort order either. Don't call compare_section then. Tested on all targets.
2023-01-31Re: Another fix for EFI generation with LTO enabledAlan Modra1-0/+2
Revert 1c66b8a03989 and instead fix the broken list pointer. PR 29998 * pe-dll.c (build_filler_bfd): Revert last change. * ldlang.c (lang_process): When rescanning archives for lto, fix file_chain.tail pointer if the insert point happens to be at the end of the list.
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-21enable-non-contiguous-regions warningsAlan Modra1-8/+30
The warning about discarded sections in elf_link_input_bfd doesn't belong there since the code is dealing with symbols. Multiple symbols in a discarded section will result in multiple identical warnings about the section. Move the warning to a new function in ldlang.c. The patch also tidies the warning quoting of section and file names, consistently using `%pA' and `%pB'. I'm no stickler for one style of section and file name quoting, but they ought to be consistent within a warning, eg. see the first one fixed in ldlang.c, and when a warning is emitted for multiple targets they all ought to use exactly the same format string to reduce translation work. elf64-ppc.c loses the build_one_stub errors since we won't get there before hitting the fatal errors in size_one_stub. bfd/ * elflink.c (elf_link_input_bfd): Don't warn here about discarded sections. * elf32-arm.c (arm_build_one_stub): Use consistent style in --enable-non-contiguous-regions error. * elf32-csky.c (csky_build_one_stub): Likewise. * elf32-hppa.c (hppa_build_one_stub): Likewise. * elf32-m68hc11.c (m68hc11_elf_build_one_stub): Likewise. * elf32-m68hc12.c (m68hc12_elf_build_one_stub): Likewise. * elf32-metag.c (metag_build_one_stub): Likewise. * elf32-nios2.c (nios2_build_one_stub): Likewise. * elfnn-aarch64.c (aarch64_build_one_stub): Likewise. * xcofflink.c (xcoff_build_one_stub): Likewise. * elf64-ppc.c (ppc_size_one_stub): Likewise. (ppc_build_one_stub): Delete dead code. ld/ * ldlang.c (lang_add_section): Use consistent style in --enable-non-contiguous-regions warnings. (size_input_section): Likewise. (warn_non_contiguous_discards): New function. (lang_process): Call it. * testsuite/ld-arm/non-contiguous-arm.d: Update. * testsuite/ld-arm/non-contiguous-arm4.d: Update. * testsuite/ld-arm/non-contiguous-arm7.d: Add --enable-non-contiguous-regions-warnings. * testsuite/ld-arm/non-contiguous-arm7.err: New. * testsuite/ld-powerpc/non-contiguous-powerpc.d: Update. * testsuite/ld-powerpc/non-contiguous-powerpc64.d: Update.
2022-12-05Prevent an illegal memory access when comparing the prefix of a section name ↵Nick Clifton1-5/+21
regexp. PR 29849 * ldlang.c (spec_match): Check that there is sufficient length in the target name to match the spec's prefix.
2022-11-30section-select: Fix exclude-file-3Michael Matz1-3/+4
this testcase wasn't correctly testing everything, it passed, even though sections from an excluded file were included. Fixing this reveals a problem in the new section selector. This fixes that as well.
2022-11-30section-select: Remove unused codeMichael Matz1-507/+1
walk_wild_file, hence walk_wild_section and walk_wild_section_handler aren't called with the prefix tree. Hence initialization of the latter and all potential special cases for it aren't used anymore. That also removes the need to handler_data[] and some associated helper functions. So, remove all of that.
2022-11-30section-select: Implement a prefix-treeMichael Matz1-32/+316
Now that we have a list of potentially matching sections per wild statement we can actually pre-fill that one by going once over all input sections and match their names against a prefix-tree that points to the potentially matching wild statements. So instead of looking at all sections names for each glob for each wild statement we now look at the sections only once and then only check against those globs that have a possibility of a match at all (usually only one or two). This pushes the whole section selection off the profiles.
2022-11-30section-select: Completely rebuild matchesMichael Matz1-0/+22
The check_relocs callback (and others) might have created new section behind our back and some of them (e.g. on powerpc the "linker stubs" .got) need to come in front of all others, despite being created late (a symptom would be "TOC opt*" failing on powerpc). This resets all section matches before updating for newly created sections (i.e. completely rebuilds the matches).
2022-11-30section-select: Lazily resolve section matchesMichael Matz1-1/+87
and remember the results. Before this the order of section matching is basically: foreach script-wild-stmt S foreach pattern P of S foreach inputfile I foreach section S of I match S against P if match: do action for S And this process is done three or four times: for each top-level call to walk_wild() or wild(), that is: check_input_sections, lang_gc_sections, lang_find_relro_sections and of course map_input_to_output_sections. So we iterate over all sections of all files many many times (for each glob). Reality is a bit more complicated (some special glob types don't need the full iteration over all sections, only over all files), but that's the gist of it. For future work this shuffles the whole ordering a bit by lazily doing the matching process and memoizing results, trading a little memory for a 75% speedup of the overall section selection process. This lazy resolution introduces a problem with sections added late that's corrected in the next patch.
2022-11-28Only use wild_sort_fastMichael Matz1-177/+125
there's no reason why the tree-based variant can't always be used when sorting is required, it merely needs to also support filename sorting and have a fast path for insertion at end (aka rightmost tree leaf). The filename sorting isn't tested anywhere and the only scripttempl that uses it is avr (for 'SORT(*)(.ctors)'), and I believe even there it was a mistake. Either way, this adds a testcase for filename sorting as well. Then the non-BST based sorting can be simplified to only support the fast case of no sorting required at all (at the same time renaming the two variants to _sort and _nosort).
2022-11-28Special case more simple patternsMichael Matz1-32/+93
fnmatch is slow, so avoiding it in more cases is good. This implements a more generic version of match_simple_wild which needs some pre-processing of patterns. In particular it supports patterns of the form PREFIX*SUFFIX (where all parts are optional), i.e. a super set of what's handled now. Most section matchers of this form and hence don't need any calls to fnmatch anymore. We retain the implementation of match_simple_wild for the filename matchers (they aren't called often enough to matter).
2022-08-04Don't use BFD_VMA_FMT in binutilsAlan Modra1-6/+6
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-04Re: Get rid of fprintf_vma and sprintf_vmaAlan Modra1-90/+44
Commit f493c2174e messed the formatting in linker map files, particularly for 32-bit builds where a number of tests using map files regressed. I should have noticed the BFD64 conditional printing of spaces to line up output due to the original %V printing hex vmas with 16 digits when BFD64 and 8 digits when not. Besides that, it is nicer to print 32-bit vmas for 32-bit targets. So change %V back to be target dependent, now using bfd_sprintf_vma. Since minfo doesn't return the number of chars printed, that means some places that currently use %V must instead sprintf to a buffer in order to find the length printed. * ldmisc.h (print_spaces): Declare. (print_space): Change to a macro. * ldmisc.c (vfinfo): Use bfd_sprintf_vma for %V. Tidy %W case. (print_space): Delete. (print_spaces): New function. * emultempl/aix.em (print_symbol): Use print_spaces. * ldctor.c (ldctor_build_sets): Likewise. * ldmain.c (add_archive_element): Likewise. * ldlang.c (print_one_symbol, lang_print_asneeded): Likewise. (print_output_section_statement, print_data_statement): Likewise. (print_reloc_statement, print_padding_statement): Likewise. (print_assignment): Likewise. Also replace %V printing of vmas with printing to a buffer in order to properly format output. (print_input_section, lang_one_common): Likewise.
2022-08-03Fix a conflict between the linker's need to rename some PE format input ↵Alan Modra1-21/+22
libraries and the BFD library's file caching mechanism. PR 29389 bfd * bfd.c (BFD_CLOSED_BY_CACHE): New bfd flag. * cache.c (bfd_cache_delete): Set BFD_CLOSED_BY_DELETE on the closed bfd. (bfd_cache_lookup_worker): Clear BFD_CLOSED_BY_DELETE on the newly reopened bfd. * opncls.c (bfd_set_filename): Refuse to change the name of a bfd that has been closed by bfd_cache_delete. Mark changed bfds as uncacheable. * bfd-in2.h: Regenerate. ld * ldlang.h (lang_input_statement_struct): Add sort_key field. * emultempl/pe.em (after_open): If multiple import libraries refer to the same bfd, store their names in the sort_key field. * emultempl/pep.em (after_open): Likewise. * ldlang.c (sort_filename): New function. Returns the filename to be used when sorting input files. (wild_sort): Use the sort_filename function.
2022-08-01Get rid of fprintf_vma and sprintf_vmaAlan Modra1-17/+2
These two macros print either a 16 digit hex number or an 8 digit hex number. Unfortunately they depend on both target and host, which means that the output for 32-bit targets may be either 8 or 16 hex digits. Replace them in most cases with code that prints a bfd_vma using PRIx64. In some cases, deliberately lose the leading zeros. This change some output, notably in base/offset fields of m68k disassembly which I think looks better that way, and in error messages. I've kept leading zeros in symbol dumps (objdump -t) and in PE header dumps. bfd/ * bfd-in.h (fprintf_vma, sprintf_vma, printf_vma): Delete. * bfd-in2.h: Regenerate. * bfd.c (bfd_sprintf_vma): Don't use sprintf_vma. (bfd_fprintf_vma): Don't use fprintf_vma. * coff-rs6000.c (xcoff_reloc_type_tls): Don't use sprintf_vma. Instead use PRIx64 to print bfd_vma values. (xcoff_ppc_relocate_section): Likewise. * cofflink.c (_bfd_coff_write_global_sym): Likewise. * mmo.c (mmo_write_symbols_and_terminator): Likewise. * srec.c (srec_write_symbols): Likewise. * elf32-xtensa.c (print_r_reloc): Similarly for fprintf_vma. * pei-x86_64.c (pex64_dump_xdata): Likewise. (pex64_bfd_print_pdata_section): Likewise. * som.c (som_print_symbol): Likewise. * ecoff.c (_bfd_ecoff_print_symbol): Use bfd_fprintf_vma. opcodes/ * dis-buf.c (perror_memory, generic_print_address): Don't use sprintf_vma. Instead use PRIx64 to print bfd_vma values. * i386-dis.c (print_operand_value, print_displacement): Likewise. * m68k-dis.c (print_base, print_indexed): Likewise. * ns32k-dis.c (print_insn_arg): Likewise. * ia64-gen.c (_opcode_int64_low, _opcode_int64_high): Delete. (opcode_fprintf_vma): Delete. (print_main_table): Use PRIx64 to print opcode. binutils/ * od-macho.c: Replace all uses of printf_vma with bfd_printf_vma. * objcopy.c (copy_object): Don't use sprintf_vma. Instead use PRIx64 to print bfd_vma values. (copy_main): Likewise. * readelf.c (CHECK_ENTSIZE_VALUES): Likewise. (dynamic_section_mips_val): Likewise. (print_vma): Don't use printf_vma. Instead use PRIx64 to print bfd_vma values. (dump_ia64_vms_dynamic_fixups): Likewise. (process_version_sections): Likewise. * rddbg.c (stab_context): Likewise. gas/ * config/tc-i386.c (offset_in_range): Don't use sprintf_vma. Instead use PRIx64 to print bfd_vma values. (md_assemble): Likewise. * config/tc-mips.c (load_register, macro): Likewise. * messages.c (as_internal_value_out_of_range): Likewise. * read.c (emit_expr_with_reloc): Likewise. * config/tc-ia64.c (note_register_values): Don't use fprintf_vma. Instead use PRIx64 to print bfd_vma values. (print_dependency): Likewise. * listing.c (list_symbol_table): Use bfd_sprintf_vma. * symbols.c (print_symbol_value_1): Use %p to print pointers. (print_binary): Likewise. (print_expr_1): Use PRIx64 to print bfd_vma values. * write.c (print_fixup): Use %p to print pointers. Don't use fprintf_vma. * testsuite/gas/all/overflow.l: Update expected output. * testsuite/gas/m68k/mcf-mov3q.d: Likewise. * testsuite/gas/m68k/operands.d: Likewise. * testsuite/gas/s12z/truncated.d: Likewise. ld/ * deffilep.y (def_file_print): Don't use fprintf_vma. Instead use PRIx64 to print bfd_vma values. * emultempl/armelf.em (gld${EMULATION_NAME}_finish): Don't use sprintf_vma. Instead use PRIx64 to print bfd_vma values. * emultempl/pe.em (gld${EMULATION_NAME}_finish): Likewise. * ldlang.c (lang_map): Use %V to print region origin. (lang_one_common): Don't use sprintf_vma. * ldmisc.c (vfinfo): Don't use fprintf_vma or sprintf_vma. * pe-dll.c (pe_dll_generate_def_file): Likewise. gdb/ * remote.c (remote_target::trace_set_readonly_regions): Replace uses of sprintf_vma with bfd_sprintf_vma.
2022-07-25Re: PowerPC64 .branch_lt addressAlan Modra1-1/+0
On seeing PR29369 my suspicion was naturally on a recent powerpc64 change, commit 0ab80031430e. Without a reproducer, I spent time wondering what could have gone wrong, and while I doubt this patch would have fixed the PR, there are some improvements that can be made to cater for user silliness. I also noticed that when -z relro -z now sections are created out of order, with .got before .plt in the section headers but .got is laid out at a higher address. That's due to the address expression for .branch_lt referencing SIZEOF(.got) and so calling init_os (which creates a bfd section) for .got before the .plt section is created. Fix that by ignoring SIZEOF in exp_init_os. Unlike ADDR and LOADADDR which need to reference section vma and lma respectively, SIZEOF can and does cope with a missing bfd section by returning zero for its size, which of course is correct. PR 29369 * ldlang.c (exp_init_os): Don't create a bfd section for SIZEOF. * emulparams/elf64ppc.sh (OTHER_RELRO_SECTIONS_2): Revise .branch_lt address to take into account possible user sections with alignment larger than 8 bytes.
2022-06-27-z relro relaxation and ld script SIZEOFAlan Modra1-1/+0
A number of targets use assignments like: . = DATA_SEGMENT_RELRO_END (SIZEOF (.got.plt) >= 12 ? 12 : 0, .); (from i386) in linker scripts to put the end of the relro segment past the header in .got.plt. Examination of testcases like those edited by this patch instead sees the end of the relro segment being placed at the start of .got.plt. For the i386 pie1 test: [ 9] .got.plt PROGBITS 00002000 001000 00000c 04 WA 0 0 4 GNU_RELRO 0x000f90 0x00001f90 0x00001f90 0x00070 0x00070 R 0x1 A map file shows: .dynamic 0x0000000000001f90 0x70 *(.dynamic) .dynamic 0x0000000000001f90 0x70 tmpdir/pie1.o 0x0000000000001f90 _DYNAMIC .got 0x0000000000002000 0x0 *(.got) .got 0x0000000000002000 0x0 tmpdir/pie1.o *(.igot) 0x0000000000002ff4 . = DATA_SEGMENT_RELRO_END (., (SIZEOF (.got.plt) >= 0xc)?0xc:0x0) .got.plt 0x0000000000002000 0xc *(.got.plt) .got.plt 0x0000000000002000 0xc tmpdir/pie1.o 0x0000000000002000 _GLOBAL_OFFSET_TABLE_ The DATA_SEGMENT_RELRO_END value in the map file is weird too. All of this is triggered by SIZEOF (.got.plt) being evaluated wrongly as zero. Fix it by taking into account the action of lang_reset_memory_regions during relaxation. * ldexp.c (fold_name <SIZEOF>): Use rawsize if size has been reset. * ldlang.c (lang_size_sections_1): Don't reset processed_vma here. * testsuite/ld-i386/pie1.d: Adjust to suit. * testsuite/ld-x86-64/pr20830a.d: Likewise. * testsuite/ld-x86-64/pr20830b.d: Likewise. * testsuite/ld-x86-64/pr21038a.d: Likewise. * testsuite/ld-x86-64/pr21038b.d: Likewise. * testsuite/ld-x86-64/pr21038c.d: Likewise.
2022-06-16Always free matching vector from bfd_check_format_matchesAlan Modra1-1/+6
At least one place calling list_matching_formats failed to free the "matching" vector from bfd_check_format_matches afterwards. Fix that by calling free inside list_matching_formats. binutils/ * bucomm.c (list_matching_formats): Free arg. * addr2line.c (process_file): Adjust to suit. * ar.c (open_inarch, ranlib_touch): Likewise. * coffdump.c (main): Likewise. * nm.c (display_archive, display_file): Likewise. * objcopy.c (copy_file): Likewise. * objdump.c (display_object_bfd): Likewise. * size.c (display_bfd): Likewise. * srconv.c (main): Likewise. ld/ * ldlang.c (load_symbols): Free "matching".
2022-04-13ubsan: member access within null pointer of unionAlan Modra1-1/+1
Add some nonsense to cover "undefined behaviour". * ldlang.c (section_for_dot): Avoid UB.
2022-03-09ld: Add a before_plugin_all_symbols_read hookH.J. Lu1-0/+2
Add a before_plugin_all_symbols_read hook to load symbol references from DT_NEEDED entries, included from --copy-dt-needed-entries, before reading plugin symbols to properly resolve plugin symbol references. bfd/ PR ld/28849 * elf-bfd.h (elf_link_hash_table): Add handling_dt_needed. * elflink.c (_bfd_elf_merge_symbol): Don't set non_ir_ref_dynamic before plugin 'all symbols read' hook is called. ld/ PR ld/28849 * ldelf.c (ldelf_handle_dt_needed): New function. (ldelf_before_plugin_all_symbols_read): Likewise. (ldelf_after_open): Call ldelf_handle_dt_needed. * ldelf.h (ldelf_before_plugin_all_symbols_read): New. * ldemul.c (ldemul_before_plugin_all_symbols_read): Likewise. * ldemul.h (ldemul_before_plugin_all_symbols_read): Likewise. (ld_emulation_xfer_struct): Add before_plugin_all_symbols_read. * ldlang.c (lang_process): Call ldemul_before_plugin_all_symbols_read before calling plugin_call_all_symbols_read. * emultempl/elf.em (gld${EMULATION_NAME}_before_plugin_all_symbols_read): New. (LDEMUL_BEFORE_PLUGIN_ALL_SYMBOLS_READ): New. * emultempl/emulation.em (ld_${EMULATION_NAME}_emulation): Initialize the before_plugin_all_symbols_read field. * testsuite/ld-plugin/lto.exp: Run PR ld/28849 tests. * testsuite/ld-plugin/pr28849.d: New file. * testsuite/ld-plugin/pr28849a.c: Likewise. * testsuite/ld-plugin/pr28849b.c: Likewise.
2022-02-16ld: Support customized output section typeFangrui Song1-6/+49
bfd/ PR ld/28841 * bfd-in2.h (struct bfd_section): Add type. (discarded_section): Add field. * elf.c (elf_fake_sections): Handle bfd_section::type. * section.c (BFD_FAKE_SECTION): Add field. * mri.c (mri_draw_tree): Update function call. ld/ PR ld/28841 * ld.texi: Document new output section type. * ldlex.l: Add new token TYPE. * ldgram.y: Handle TYPE=exp. * ldlang.h: Add type_section to list of section types. * ldlang.c (lang_add_section): Handle type_section. (map_input_to_output_sections): Handle type_section. * testsuite/ld-scripts/output-section-types.t: Add tests. * testsuite/ld-scripts/output-section-types.d: Update.
2022-02-13Don't pass around expld.dataseg pointerAlan Modra1-14/+16
The better to see any code that accesses expld.dataseg. * ldexp.c (fold_segment_end): Remove seg parameter. Adjust calls. (fold_segment_align, fold_segment_relro_end): Likewise. * ldlang.c (lang_size_segment): Likewise. (lang_size_relro_segment_1, lang_find_relro_sections_1): Likewise.
2022-02-13PR28824, relro security issuesAlan Modra1-7/+6
Background ========== There are constraints on layout of binaries to meet demand paging and memory protection requirements. Demand paged binaries must have file offset mod pagesize equal to vma mod pagesize. Memory protection (executable, read, write status) can only change at page boundaries. The linker's MAXPAGESIZE variable gives the page size for these layout constraints. In a typical basic executable with two memory segments, text (RE) and data (RW), the data segment must start on a different page to the last text segment page. For example, with 64k pages and a small executable of 48k text and 1k data, the text segment might start at address 0x10000 and data at 0x20000 for a total of two 64k memory pages. Demand paging would require the image on disk to be 64k+1k in size. We can do better than that. If the data segment instead starts at 0x2c000 (the end of the text segment plus one 64k page) then there are still only two memory pages, but the disk image is now smaller, 48k+1k in size. This is why the linker normally starts the data segment at the end of the text segment plus one page. That simple heuristic isn't ideal in all cases. Changing our simple example to one with 64k-1 text size, following that heuristic would result in data starting at 0x2ffff. Now we have two 64k memory data pages for a data segment of 1k! If the data segment instead started at 0x30000 we'd get a single data segment page at the cost of 1 byte extra in the disk image, which is likely a good trade-off. So the linker does adjust the simple heuristic. Just how much disk image size increase is allowed is controlled by the linker's COMMONPAGESIZE variable. A PT_GNU_RELRO segment overlays the initial part of the data segment, saying that those pages should be made read-only after relocation by the dynamic loader. Page granularity for memory protection means that the end of the relro segment must be at a page boundary. The problem =========== Unfortunately most targets currently only align the end of the relro segment to COMMONPAGESIZE. That results in only partial relro protection if an executable is running with MAXPAGESIZE pages, since any part of the relro segment past the last MAXPAGESIZE boundary can't be made read-only without also affecting sections past the end of the relro segment. I believe this problem arose because x86 always runs with 4k (COMMPAGESIZE) memory pages, and therefore using a larger MAXPAGESIZE on x86 is for reasons other than the demand paging and memory page protection boundary requirements. The solution ============ Always end the relro segment on a MAXPAGESIZE boundary, except for x86. Note that the relro segment, comprising of sections at the start of the data segment, is sized according to how those sections are laid out. That means the start of the relro segment is fixed relative to its end. Which also means the start of the data segment must be at a fixed address mod MAXPAGESIZE. So for relro the linker can't play games with the start of the data segment to save disk space. At least, not without introducing gaps between the relro sections. In fact, because the linker was starting layout using its simple heuristic of starting the data segment at the end of the text segment plus one page, it was sometimes introducing page gaps for no reason. See pr28743. PR 28824 PR 28734 * ldexp.c (fold_segment_align): When relro, don't adjust up by offset within page. Set relropagesize. (fold_segment_relro_end): Align to relropagesize. * ldexp.h (seg_align_type): Rename pagesize to commonpagesize. Add relropagesize. Comment. * ldlang.c (lang_size_segment): Adjust to suit field renaming. (lang_size_relro_segment_1): Align relro_end using relropagesize.