aboutsummaryrefslogtreecommitdiff
path: root/binutils/objcopy.c
AgeCommit message (Collapse)AuthorFilesLines
12 daysobjcopy: when an invalid bfd target string is used as a target option, print ↵Nick Clifton1-3/+12
an error message that references the target string, not the file being copied
2025-08-27objcopy "Unable to recognise the format of the input file"Alan Modra1-9/+12
This bogus error comes up when trying something like objcopy -O binary .../binutils/testsuite/binutils-all/tek2.obj xxx This is an annoying message, as HJ said in https://sourceware.org/pipermail/binutils/2002-August/021354.html and removed it for some cases, eg. I can make it go away by specifying -I tekhex. The message is also untrue, as objcopy does in fact know the format of the input file. I think the message should be limited to ELF input files that are being handled by the elf64-little, elf64-big, elf32-little or elf32-big targets, due to libbfd being compiled with limited target support. I'm also changing the message a litle. * objcopy.c (copy_object): Change "Unable to recognise format" message to "Unable to recognise architecture" and only report this error for ELF objects lacking their proper target support. * testsuite/binutils-all/x86-64/x86-64.exp: Update to suit.
2025-08-27Revert part of commit 8e885fece150Alan Modra1-11/+6
After commit 5e83077d552e we no longer choose a "plugin" target in objcopy so ibfd->target_defaults can again be used to test whether the user supplied a target. PR 33230 * objcopy.c (copy_file): Revert change adding a target_defaulted variable and passing down to.. (copy_archive, copy_object): ..here. Remove target_defaulted parameter. Use ibfd->target_defaulted.
2025-08-18Limit BFD_SUPPORTS_PLUGINS check to plugin.h and targets.cH.J. Lu1-20/+5
Minimize the BFD_SUPPORTS_PLUGINS check to make code more readable and maintainable by: 1. Update bfd/plugin.h to define plugin functions as static inline if BFD_SUPPORTS_PLUGINS is 0. 2. Remove BFD_SUPPORTS_PLUGINS check from all bfd and binutils files except plugin.h and targets.c. 3. Replace the remaining BFD_SUPPORTS_PLUGINS checks with a function so that plugin availability is checked at run time. bfd/ * archive.c: Include plugin.h unconditionally. (_bfd_compute_and_write_armap): Remove the BFD_SUPPORTS_PLUGINS check. * bfd-in.h (bfd_plugin_enabled): New. * bfd-in2.h: Regenerated. * elflink.c: Include plugin.h unconditionally. (elf_link_is_defined_archive_symbol): Remove the BFD_SUPPORTS_PLUGINS check. * format.c: Include plugin.h unconditionally. (bfd_set_lto_type): Remove the BFD_SUPPORTS_PLUGINS check. (bfd_check_format_matches): Replace the BFD_SUPPORTS_PLUGINS check with the bfd_plugin_enabled call. Replace plugin_vec with bfd_plugin_vec. Remove the BFD_SUPPORTS_PLUGINS check. * plugin.c (bfd_plugin_target_p): Removed. * plugin.h (bfd_plugin_vec): New. (bfd_plugin_target_p): Likewise. (bfd_plugin_set_program_name): New. Static inline function if BFD_SUPPORTS_PLUGINS is 0. (bfd_plugin_open_input): Likewise. (bfd_plugin_set_plugin): Likewise. (bfd_link_plugin_object_p): Likewise. (register_ld_plugin_object_p): Likewise. (bfd_plugin_close_file_descriptor): Likewise. (bfd_plugin_vec): Likewise. (bfd_plugin_target_p): Likewise. * xtensa-dynconfig.c (xtensa_load_config): Replace the BFD_SUPPORTS_PLUGINS check with the bfd_plugin_enabled call. ar/ * ar.c: Include plugin.h unconditionally. (plugin_target): Removed. (usage): Replace the BFD_SUPPORTS_PLUGINS check with the bfd_plugin_enabled call. (ranlib_usage): Likewise. (decode_options): Likewise. (ranlib_main): Likewise. (main): Call bfd_plugin_set_program_name unconditionally. * nm.c: Include plugin.h unconditionally. (plugin_target): Removed. (usage): Replace the BFD_SUPPORTS_PLUGINS check with the bfd_plugin_enabled call. (filter_symbols): Remove the BFD_SUPPORTS_PLUGINS check. (display_rel_file): Likewise. (main): Call bfd_plugin_set_program_name unconditionally. Replace the BFD_SUPPORTS_PLUGINS check with the bfd_plugin_enabled call. * objcopy.c: Include plugin.h unconditionally. (strip_usage): Replace the BFD_SUPPORTS_PLUGINS check with the bfd_plugin_enabled call. (copy_archive): Remove the BFD_SUPPORTS_PLUGINS check. Replace BFD_SUPPORTS_PLUGINS with the bfd_plugin_enabled call. (copy_file): Likewise. (strip_main): Likewise. ld/ * ldfile.c: Include plugin.h unconditionally. (ldfile_try_open_bfd): Remove the BFD_SUPPORTS_PLUGINS check. * ldlang.c: Include plugin.h unconditionally. (plugin_insert): Remove the BFD_SUPPORTS_PLUGINS check. (plugin_undefs): Likewise. (open_input_bfds): Likewise. (lang_check): Likewise. (lang_gc_sections): Likewise. (find_next_input_statement): Likewise. (lang_process): Likewise. * ldlang.h (lang_input_statement_flags): Likewise. * ldlex.h (option_values): Likewise. * ldmain.c: Include plugin.h unconditionally. (ld_cleanup): Remove the BFD_SUPPORTS_PLUGINS check. (main): Likewise. (add_archive_element): Likewise. * lexsup.c: Include plugin.h unconditionally. (ld_options): Remove the BFD_SUPPORTS_PLUGINS check. (parse_args): Replace the BFD_SUPPORTS_PLUGINS check with the bfd_plugin_enabled call. Remove the BFD_SUPPORTS_PLUGINS check. (help): Append " (ignored)" to plugin options if bfd_plugin_enabled return false. * libdep_plugin.c: Remove the BFD_SUPPORTS_PLUGINS check. * plugin.c: Likewise. * testplug.c: Likewise. * testplug2.c: Likewise. * testplug3.c: Likewise. * testplug4.c: Likewise. Co-Authored-By: Alan Modra <amodra@gmail.com> Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
2025-08-18Don't choose plugin target in binutils/Alan Modra1-8/+2
Instead make bfd_check_format try the plugin target first when the user hasn't supplied a target. bfd/ * format.c (bfd_check_format_matches): Try for a plugin target match first. * targets.c (bfd_find_target): Don't specially treat "plugin". binutils/ * ar.c (plugin_target): Delete. (open_inarch): Don't set target of archive elements. (replace_members): Use target rather than plugin_target when opening replacement or additional files. * arsup.c (plugin_target): Delete. Replace all uses with NULL. (ar_open): Don't set element target. * bucomm.h (set_plugin_target): Delete. * nm.c (plugin_target): Delete. (display_archive): Don't set element target. (display_file): Alway use target when opening file. * objcopy.c (copy_archive): Don't use plugin target for output elements. * NEWS: Mention stricter target checking.
2025-08-15objcopy.c: Re-indent slim LTO IR commentH.J. Lu1-1/+1
Re-indent slim LTO IR comment in commit 9b383903e73cd01f2fbe9728d0c31fea765ba8d6 Author: H.J. Lu <hjl.tools@gmail.com> Date: Tue Aug 12 12:02:08 2025 -0700 strip: Treat slim GCC/LLVM IR objects the same PR binutils/33271 * objcopy.c (copy_file): Re-indent slim LTO IR comment. Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
2025-08-14strip: Treat slim GCC/LLVM IR objects the sameH.J. Lu1-23/+9
Slim LLVM IR object is a standalone file whose first 4 bytes are 'B', 'C', 0xc0, 0xde. GCC IR object is regular ELF object with sections whose names start with .gnu.lto_.* or .gnu.debuglto_.*. GCC IR object uses a .gnu.lto_.lto.<some_hash> section to encode the LTO bytecode information: struct lto_section { int16_t major_version; int16_t minor_version; unsigned char slim_object; /* Flags is a private field that is not defined publicly. */ uint16_t flags; }; In slim GCC IR object, the slim_object field is non-zero. Strip should treat slim GCC/LLVM IR objects the same. Since strip won't change slim LLVM IR objects, it should leave slim GCC IR object unchanged even when asked to remove all IR objects: 1. Set the lto_type field to lto_slim_ir_object for slim LLVM IR object. 2. Always copy slim IR object as unknown object. bfd/ PR binutils/33271 * format.c (bfd_set_lto_type): Set the lto_type field to lto_slim_ir_object for slim LLVM IR object. binutils/ PR binutils/33271 * objcopy.c (lto_sections_removed): Removed. (copy_archive): Always copy slim IR object as unknown object. (copy_file): Likewise. (strip_main): Updated. ld/ PR binutils/33271 * testsuite/ld-plugin/lto-binutils.exp: Don't check if fat IR is available when running slim IR tests. * testsuite/ld-plugin/strip-1a-s-all.nd: Expect full symbol list. Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
2025-08-14objcopy/strip of IR files and is_strip_inputAlan Modra1-94/+85
This tidies objcopy/strip handling of IR objects, in the process of removing the unnecessary is_strip_input flag. The first thing I noticed when looking at is_strip_input code was that the abfd->my_archive test in bfd_check_format_matches meant that plugins were disabled when reading archive elements. We can instead disable plugins by setting bfd_no_plugin, so there doesn't seem to be a need for is_strip_input in objcopy.c:copy_archive. This isn't exactly the same, because bfd_no_plugin prevents the plugin target recognising archive elements in the bfd_check_format_matches loop over all targets as well as just the first !target_defaulted test. But that turns out to be fine. IR code is handled in copy_archive as for other unknown format files. In fact, the only need for the plugin target when copying archives is when reading symbols for the archive map. I've made that plain by moving the plugin target override and commenting on why it is really needed. So on to plain object files. Here, IR code is also copied unchanged, so there doesn't seem a need for the plugin target there either. It isn't quite so simple though, because the objcopy/strip code handling object files wants to verify the format of the object file. Allowing objcopy/strip to copy unknown format files would be a change in behaviour (and results in mmix testsuite fails, ld-mmix/b-badfil1 and others). However, always excluding the plugin target results in a fail of tests added in commit c2729c37f10a. So I've enabled a plugin format check only for files that are otherwise unrecognised, and commented why this is done. I question the need to objcopy LLVM bytecode files. bfd/ * bfd.c (struct bfd<is_strip_input>): Delete. * format.c (bfd_check_format_matches): Delete is_strip_input special case code. * bfd-in2.h: Regenerate. binutils/ * objcopy.c (copy_archive): Don't set is_strip_input. Always set bfd_plugin_no when reading elements. Enable plugins when opening copied elements. (check_format_object): Delete. (copy_file): Don't enable plugin target here. Don't set is_strip_input. Set bfd_plugin_no. Move bfd_core handling code earlier to remove goto. Enable plugin for llvm bytecode. Copy slim IR files as unknown objects.
2025-08-07Move struct plugin_data_struct to plugin.cAlan Modra1-1/+2
It isn't needed anywhere except plugin.c. The typedef can disappear. Also make a forward declaraion for ld_plugin_input_file in plugin.h so that this header can be used without first including plugin-api.h. bfd/ * plugin.h (struct ld_plugin_input_file): Forward declare. (struct plugin_data_struct): Move to.. * plugin.c: ..here. (add_symbols): Size plugin_data without using type. * archive.c: Don't include plugin-api.h. * elflink.c: Likewise. * format.c: Likewise. binutils/ * ar.c: Don't include plugin-api.h or ansidecl.h. Only include plugin.h when BFD_SUPPORTS_PLUGINS. * nm.c: Don't include plugin-api.h. Only include plugin.h when BFD_SUPPORTS_PLUGINS. * objcopy.c: Likewise. ld/ * ldfile.c: Don't include plugin-api.h. * ldmain.c: Likewise.
2025-08-06strip: Don't treat fat IR objects as plugin objectH.J. Lu1-1/+12
Fat IR objects contains both regular sections and IR sections. After commit 717a38e9a02109fcbcb18bb2ec3aa251e2ad0a0d Author: H.J. Lu <hjl.tools@gmail.com> Date: Sun May 4 05:12:46 2025 +0800 strip: Add GCC LTO IR support "strip --strip-debug" no longer strips debug sections in fat IR objects since fat IR objects are recognized as plugin object and copied as unknown objects. Add a is_strip_input field to bfd to indicate called from strip. Update bfd_check_format_matches not to treat archive member nor standalone fat IR object as IR object so that strip can remove debug and IR sections in fat IR object. For archive member, it is copied as an unknown object if the plugin target is in use or it is a slim IR object. For standalone fat IR object, it is copied as non-IR object. bfd/ PR binutils/33246 * archive.c: Include "plugin-api.h" and "plugin.h" if plugin is enabled. (_bfd_compute_and_write_armap): Don't complain plugin is needed when the plugin target is in use. * bfd-in2.h: Regenerated. * bfd.c (bfd): Add is_strip_input. * format.c (bfd_set_lto_type): If there is .llvm.lto section, set LTO type to lto_fat_ir_object. (bfd_check_format_matches): Don't set LTO type when setting format. When called from strip, don't treat archive member nor standalone fat IR object as an IR object. * plugin.c (bfd_plugin_get_symbols_in_object_only): Copy LTO type derived from input sections. nm/ PR binutils/33246 * nm.c (filter_symbols): Don't complain plugin is needed when the plugin target is in use. (display_rel_file): Likewise. * objcopy.c (copy_archive): Set the BFD is_strip_input field of archive member to 1 to indicate called from strip. Also copy slim IR archive member as unknown object. (copy_file): Set the BFD is_strip_input field of input bfd to 1 to indicate called from strip. (strip_main): Keep .gnu.debuglto_* sections unless all GCC LTO sections will be removed. ld/ PR binutils/33246 * testsuite/ld-plugin/lto-binutils.exp (run_pr33246_test): New. Run binutils/33246 tests with GCC and Clang. * testsuite/ld-plugin/pr33246.c: New file. Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
2025-08-06Remove bfd_check_format_ltoAlan Modra1-19/+25
Tidy changes to bfd_check_format_matches made by commit 9b854f169df9 which added a bfd_plugin_specified_p test and commit f752be8f916e which added an lto_sections_removed arg. Both of these changes are unnecessary if plugin_format is set to bfd_plugin_no before calling bfd_check_format. bfd_plugin_no will prevent the plugin object_p function from returning a match (and in the first case from a segfault when loading plugins while a plugin is running). The plugin object_p function already protected itself from recursive calls by setting bfd_plugin_no before loading a plugin, but commit 9b854f169df9 opened new bfds so they were unprotected. It isn't strictly necessary to test for bfd_plugin_no in bfd_check_format_matches but I kept the check to minimise functional changes. Close inspection of the patch will notice I've added an is_linker_input test too. That also isn't strictly necessary, I think, but the match_count test was for the linker. See commit 999d6dff80fa. PR 12291 PR 12430 PR 13298 PR 33198 bfd/ * format.c (bfd_check_format_lto): Revert to bfd_check_format. (bfd_check_format_matches_lto): Revert to bfd_check_format_matches. Correct comments. Manage both the lto_sections_removed and bfd_plugin_specified_p cases by testing for bfd_plugin_no. * plugin.c (bfd_plugin_get_symbols_in_object_only): Set plugin_format to bfd_plugin_no before checking new bfds. (try_load_plugin): Comment setting bfd_plugin_no. (bfd_plugin_specified_p): Delete. * plugin.h (bfd_plugin_specified_p): Delete. * bfd-in2.h: Regenerate. binutils/ * objcopy.c (copy_archive): Replace bfd_check_format_lto calls with bfd_check_format using plugin_format set to bfd_plugin_no. (check_format_object): New function. (copy_file): Use it.
2025-07-31strip: Treat "default" output_target as unspecifiedH.J. Lu1-2/+4
Treat output target as unspecified if it is set to "default". binutils/ PR binutils/33230 * objcopy.c (copy_file): Treat "default" output_target as unspecified. binutils/testsuite/ PR binutils/33230 * binutils-all/x86-64/x86-64.exp (run_pr33230_test): New. Run binutils/33230 tests with readelf if supported. Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
2025-07-30strip: Don't check target_defaulted in input BFDH.J. Lu1-6/+11
The target_defaulted field in BFD is set to true if the target isn't specified. After commit 717a38e9a02109fcbcb18bb2ec3aa251e2ad0a0d Author: H.J. Lu <hjl.tools@gmail.com> Date: Sun May 4 05:12:46 2025 +0800 strip: Add GCC LTO IR support the target is set to "plugin" if BFD supports plugin when the target isn't specified nor default. Update strip to check the input target, instead of the target_defaulted field in input BFD. PR binutils/33230 * objcopy.c (copy_object): Add a bool argument, target_defaulted, to indicate if the input target isn't specified nor default. Check it instead of ibfd->target_defaulted. (copy_archive): Add a bool argument, target_defaulted, and pass it to copy_object. (copy_file): Set target_defaulted to true if the input target isn't specified and pass it to copy_archive and copy_object. * testsuite/binutils-all/x86-64/pr33230.obj.bz2: New file. * testsuite/binutils-all/x86-64/x86-64.exp: Run PR binutils/33230 tests. Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
2025-07-24strip: Properly handle LLVM IR bitcodeH.J. Lu1-10/+30
commit 717a38e9a02109fcbcb18bb2ec3aa251e2ad0a0d Author: H.J. Lu <hjl.tools@gmail.com> Date: Sun May 4 05:12:46 2025 +0800 strip: Add GCC LTO IR support added "-R .gnu.lto_.*" to strip to remove all GCC LTO sections. When "-R .gnu.lto_.*" is used, the plugin target is ignored so that all LTO sections are stripped as the regular sections. It works for the slim GCC LTO IR since the GCC LTO IR is stored in the regular sections. When the plugin target is ignored, the GCC LTO IR can be recognized as the normal object files. But it doesn't work for the slim LLVM IR which is stored in a standalone file. 1. Add bfd_check_format_matches_lto and bfd_check_format_lto to take an argument, lto_sections_removed, to indicate if all LTO sections should be removed. 2. Update strip to always enable the plugin target so that the plugin target is enabled when checking for bfd_archive. 3. Update strip to ignore the plugin target for bfd_object when all LTO sections should be removed. If the object is unknown, copy it as an unknown file without any messages. 4. Treat the "-R .llvm.lto" strip option as removing all LTO sections. bfd/ PR binutils/33198 * format.c (bfd_check_format_lto): New function. (bfd_check_format): Call bfd_check_format_matches_lto. (bfd_check_format_matches): Renamed to ... (bfd_check_format_matches_lto): This. Add an argument, lto_sections_removed, to indicate if all LTO sections should be removed and don't match the plugin target if lto_sections_removed is true. (bfd_check_format_matches): Call bfd_check_format_matches_lto. * bfd-in2.h: Regenerated. binutils/ PR binutils/33198 * objcopy.c (copy_archive): Call bfd_check_format_lto, instead of bfd_check_format, and pass lto_sections_removed. Remove the non-fatal message on unknown element since it will be copied as an unknown file. (copy_file): Don't check lto_sections_removed when enabling LTO plugin in strip. (copy_file): Ignore the plugin target first if all LTO sections should be removed. Try with the plugin target next if ignoring the plugin target failed to match the format. (strip_main): Also set lto_sections_removed for -R .llvm.lto. * testsuite/binutils-all/x86-64/pr33198.c: New file. * testsuite/binutils-all/x86-64/x86-64.exp (run_pr33198_test): New. Run binutils/33198 tests. * testsuite/lib/binutils-common.exp (llvm_plug_opt): New. (CLANG_FOR_TARGET): New. Set to "clang" for native build if "clang -v" reports "clang version". Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
2025-07-17binutils: drop unused note_size, contents, old variablesSam James1-8/+1
GCC trunk recently had improvements to its -Wunused-but-set-variable which picked up that contents and hence note_size & old aren't used at all in the end. * objcopy.c (merge_gnu_build_notes): Drop unused 'note_size', 'contents', and 'old' variables.
2025-07-09Merge init_private_section_data with copy_private_section_dataAlan Modra1-1/+1
init_private_section_data is used by the linker and is a special case of copy_private_section_data that copies a reduced set of section data from input to output. Merge the two functions, adding a link_info param to copy_private_section_data and remove init_private_section_data.
2025-06-23objcopy: Don't extend the output section sizeH.J. Lu1-0/+6
Since the output section contents are copied from the input, don't extend the output section size beyond the input section size. PR binutils/33049 * objcopy.c (copy_section): Don't extend the output section size beyond the input section size. Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
2025-06-15objcopy: Correctly check archive element for LTO IRH.J. Lu1-1/+1
commit 717a38e9a02109fcbcb18bb2ec3aa251e2ad0a0d Author: H.J. Lu <hjl.tools@gmail.com> Date: Sun May 4 05:12:46 2025 +0800 strip: Add GCC LTO IR support added: @@ -3744,6 +3768,12 @@ copy_archive (bfd *ibfd, bfd *obfd, const char *output_target, goto cleanup_and_exit; } +#if BFD_SUPPORTS_PLUGINS + /* Copy LTO IR file as unknown object. */ + if (bfd_plugin_target_p (ibfd->xvec)) ^^^^ A typo, should be this_element. + ok_object = false; + else +#endif if (ok_object) { ok = copy_object (this_element, output_element, input_arch); to check if the archive element is a LTO IR file. "ibfd" is the archive BFD. "this_element" should be used to check for LTO IR in the archive element. Fix it by replacing "ibfd" with "this_element". PR binutils/33078 * objcopy.c (copy_archive): Correctly check archive element for LTO IR. * testsuite/binutils-all/objcopy.exp (strip_test_archive): New. Run strip_test_archive. Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
2025-05-14strip: Add GCC LTO IR supportH.J. Lu1-22/+93
Add GCC LTO IR support to strip by copying GCC LTO IR input as unknown object file. Don't enable LTO plugin in strip unless all LTO sections should be removed, assuming all LTO sections will be removed with -R .gnu.lto_.*. Add linker LTO tests for strip with --strip-unneeded and GCC LTO IR inputs. binutils/ PR binutils/21479 * objcopy.c: Include "plugin-api.h" and "plugin.h". (lto_sections_removed): New. (command_line_switch): Add OPTION_PLUGIN. (strip_options): Likewise. (strip_usage): Display "--plugin NAME". (copy_unknown_file): New function. (copy_unknown_object): Call copy_unknown_file. (copy_archive): Copy input LTO IR member as unknown object. (copy_file): Set input target to "plugin" for strip if it is unset unless all LTO sections should be removed. Copy input LTO IR file as unknown file. (strip_main): Call bfd_plugin_set_program_name. Handle OPTION_PLUGIN. Set lto_sections_removed to true if all GCC LTO sections should be removed. * doc/binutils.texi: Document --plugin for strip. ld/ PR binutils/21479 * testsuite/ld-plugin/lto-binutils.exp: New file. * testsuite/ld-plugin/strip-1a-fat.c: Likewise. * testsuite/ld-plugin/strip-1a-fat.rd: Likewise. * testsuite/ld-plugin/strip-1b-fat.c: Likewise. * testsuite/ld-plugin/strip-1b-fat.rd: Likewise. * testsuite/ld-plugin/strip-1a.c: Likewise. * testsuite/ld-plugin/strip-1b.c: Likewise. * testsuite/lib/ld-lib.exp (run_cc_link_tests): Add optional trailing ld options. Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
2025-04-04objcopy: also check --file-alignment option argumentJan Beulich1-0/+5
... to be a power of two, just like --section-alignment does.
2025-04-04objcopy: constrain --section-alignment to PE binaries againJan Beulich1-41/+3
PR binutils/32732 The --set-section-alignment option is what ought to be used on object files; --section-alignment should be affecting PE binaries only, and only the value stored in the header. Sections don't individually have alignment recorded there; see 6f8f6017a0c4 ("PR27567, Linking PE files adds alignment section flags to executables"). Undo the core part of 121a3f4b4f4a ("Update objcopy's --section-alignment option so that it sets the alignment flag on..."), which includes removing the testcase again, while leaving all secondary changes in place. (Note that the testcase did fail anyway for i?86-interix, with objdump saying "option -P/--private not supported by this file".)
2025-04-04ar/objcopy: harmonize .exe suffix strippingJan Beulich1-1/+2
With it only being the tail of the name which wants checking, using lbasename() isn't helpful. Mirror what objcopy.c:main() does to ar.c, merely chaning the plain int of the local variable to size_t.
2025-04-04binutils: properly split objcopy and stripJan Beulich1-0/+4
By not linking the exact same object file twice, in particular strip can benefit quite a bit from the compiler eliminating dead code.
2025-03-21strip: don't corrupt PE binary's section/file alignmentJan Beulich1-2/+2
Section and file alignment are supposed to remain unaltered when PE binaries are stripped. While this is the case when they're strip-ed individually, passing multiple such files to strip would reset the two values to their defaults in all but the first of those binaries.
2025-01-14ld: Add LTO and none-LTO output support for ld -rH.J. Lu1-30/+2
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-01Close elements of output archiveAlan Modra1-4/+1
When cleaning up an archive, close all its elements. This fixes a number of ar memory leaks. bfd/ * archive.c (_bfd_archive_close_and_cleanup): Close elements of an archive open for writing. binutils/ * objcopy.c (copy_archive): Don't close output archive elements here. * dlltool.c (gen_lib_file): Likewise. ld/ * pe-dll.c (pe_dll_generate_implib): Don't close output archive elements here.
2025-01-01Update year range in copyright notice of binutils filesAlan Modra1-1/+1
2024-12-14Delete asection.symbol_ptr_ptrAlan Modra1-1/+1
This field is always set to point to asection.symbol, and no code ever changes it from its initial value. With one exception. elfxx-mips.c creates two sections with separate pointers to their symbols, and uses those as asection.symbol_ptr_ptr. Those pointers aren't modified, so they disappear in this patch too.
2024-10-08Revised "Don't return (null) from bfd_elf_sym_name"Alan Modra1-5/+5
Commit 68bbe1183379 results in a lot of follow up work, much of which likely is still to be done. (And yes, since this is all for corrupted or fuzzed object files, a whole lot of work doesn't much benefit anyone. It was a bad idea to put NULL in asymbol->name.) So I'm changing the approach to instead put a unique empty string for symbols with a corrupted st_name. An empty string won't require much work to ensure nm, objcopy, objdump etc. won't crash, since these tools already must work with unnamed local symbols. The unique empty string is called bfd_symbol_error_name. This patch uses that name string for corrupted symbols in the ELF and COFF backends. Such symbols are displayed by nm and objdump as the translated string "<corrupt>", which is what the COFF backend used to put directly into corrupted symbols. ie. it's the way I should have written the original patch, plus a few tides and cleanups I retained from the reverted patches.
2024-10-08Revert "objcopy fixes for commit 68bbe1183379"Alan Modra1-11/+5
This reverts commit ef166f451fbc2c7b251a251ab23cd35b36c5ee23.
2024-10-05objcopy fixes for commit 68bbe1183379Alan Modra1-5/+11
* objcopy.c (is_specified_symbol): Handle NULL name. (filter_symbols): Drop syms with a NULL name.
2024-07-20PR31999 strip [.gnu.build.attributes]: failedAlan Modra1-0/+1
PR 31999 * objcopy.c (merge_gnu_build_notes): Always set *new_size.
2024-07-06objcopy bfd_map_over_sections and global statusAlan Modra1-142/+139
This patch started life as a relatively simple change to fix some unimportant objcopy memory leaks, but expanded into a larger patch when I was annoyed by the awkwardness of passing data when using bfd_map_over_sections. A simple loop over sections is much more convenient, and we really don't need the abstraction layer. Sections in a list isn't going to disappear any time soon. The patch also removes use of the global "status" variable by all but the top-level functions called from main. * objcopy.c (filter_symbols): Return success as a bool. Pass symcount as a pointer, updated on return. (merge_gnu_build_notes): Similarly return a bool and add newsize param with updated smaller section size. (setup_bfd_headers): Return bool success rather than setting "status" on failure. (setup_section): Likewise. (copy_relocations_in_section, copy_section): Likewise, and adjust params. (mark_symbols_used_in_relocations): Likewise, and free memory on failure path. Don't call bfd_fatal. (get_sections): Delete function. (copy_object): Don't use bfd_map_over_sections, instead use a loop allowing easy detection of failure status. Free memory on error paths. (copy_archive): Return bool success rather than setting "status" on failure. (copy_file): Set "status" here. * testsuite/binutils-all/strip-13.d: Adjust to suit.
2024-07-01PR31941 objcopy --globalize-symbolAlan Modra1-0/+1
I think FILE symbols are special, and I can't see why anyone would want them to be made global. The fact that no one has reported this bug since commit 7b4a0685e80a in 2005 supports that claim. PR 31941 * objcopy.c (filter_symbols): Don't allow BSF_FILE symbols to be made global.
2024-07-01objcopy: Allow making symbol global and weak on same invocationMarcus Nilsson1-15/+16
Previously objcopy had to be run twice in order to make a local symbol weak, first once to globalize it, and once again to mark it as weak. * objcopy.c (filter_symbols): Weaken symbols after making local/global changes. * testsuite/binutils-all/symbols-5.d, * testsuite/binutils-all/symbols-5.s: New test.
2024-04-26objcopy: check input flavor before setting PE/COFF section alignmentJan Beulich1-0/+1
coff_section_data() and elf_section_data() use the same underlying field. The pointer being non-NULL therefore isn't sufficient to know that pei_section_data() can validly be used on the incoming object. Apparently in 64-bit-host builds the resulting memory corruption is benign, whereas in 32-bit-host builds a segmentation fault occurs upon de-referencing pei_section_data()'s return value.
2024-04-23objcopy.c: Fix bfd_copy_private_symbol_data on 32-bit hostsH.J. Lu1-2/+2
Use long with bfd_copy_private_symbol_data to fix .../binutils/objcopy.c: In function ‘copy_object’: .../binutils/objcopy.c:3383:17: error: comparison of integer expressions of different signedness: ‘unsigned int’ and ‘long int’ [-Werror=sign-compare] 3383 | for (i = 0; i < symcount; i++) | ^ on 32-bit hosts. PR binutils/14493 * objcopy.c (copy_object): Use long with bfd_copy_private_symbol_data.
2024-04-23use copy_private_symbol_data in objcopyAlan Modra1-0/+7
osympp appearing twice here is not a bug. PR 14493 * objcopy.c (copy_object): Run the symbols through bfd_copy_private_symbol_data.
2024-04-11Re: Update objcopy's --section-alignment optionAlan Modra1-2/+6
ubsan: shift exponent 255 is too large for 64-bit type I should have known oss-fuzz wouldn't be satisfied so easily. The pef format allows quite silly section alignments in object files. * objcopy.c (setup_section): Limit shift exponent when checking vma and lma for alignment.
2024-04-04Re: Update objcopy's --section-alignment optionAlan Modra1-2/+2
ubsan: left shift of 1 by 31 places cannot be represented in type 'int' * objcopy.c (setup_section): Avoid undefined behaviour when checking vma and lma for alignment.
2024-04-02Update objcopy's --section-alignment option so that it sets the alignment ↵Nick Clifton1-9/+101
flag on PE sections. Add a check for aligned sections not matching their VMAs.
2024-01-26Fix: Stripping Rust static libraries fails because of overly zealous illegal ↵Nick Clifton1-12/+14
path check PR 31250 * objcopy.c (copy_archive): Improve the handling of archives that contain elements with invalid pathnames.
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-27PR31191, objcopy leaves temporary filesAlan Modra1-23/+24
Fix the ENOTDIR rmdir too. PR 31191 * objcopy.c (copy_archive): Localise uses of "l". Remove const from name_list.name. Unlink output element on bfd_close error, and NULL list->name to indicate file is removed. Adjust cleanup to prevent rmdir on non-existent file.
2023-12-05Handle "efi-app-riscv64" and similar targets in objcopy.Peter Jones1-0/+5
This adds the efi target name handling for riscv64 to objcopy. binutils: * binutils/objcopy.c: add riscv64 handling to convert_efi_target() Signed-off-by: Peter Jones <pjones@redhat.com> Reviewed-by: Palmer Dabbelt <palmer@rivosinc.com>
2023-10-23objcopy: fix typo in --heap and --stack parserClément Chigot1-2/+2
The help says that <reserve> and <commit> should be separated by a "," but the implementation is checking for ".". Having two numbers being separated by a "." could be confusing, thus adjust the implementation to match the help syntax. binutils/ChangeLog: * objcopy.c (copy_main): Set separator to "," between <reserve> and <commit> for --heap and --stack. * doc/binutils.texi: Add <commit> for --heap and --stack.
2023-10-16objcopy: Fix name of the field modified by pe_stack_reserve.Clément Chigot1-1/+1
2023-08-09Rename bfd_bread and bfd_bwriteAlan Modra1-3/+3
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-08-02Revert "2.41 Release sources"Sam James1-38/+70
This reverts commit 675b9d612cc59446e84e2c6d89b45500cb603a8d. See https://sourceware.org/pipermail/binutils/2023-August/128761.html.
2023-08-022.41 Release sourcesbinutils-2_41-releaseNick Clifton1-70/+38