[PATCH] strip: Don't check target_defaulted in input BFD

H.J. Lu hjl.tools@gmail.com
Thu Jul 31 00:44:45 GMT 2025


On Wed, Jul 30, 2025 at 5:16 PM Alan Modra <amodra@gmail.com> wrote:
>
> On Wed, Jul 30, 2025 at 09:02:51AM -0700, H.J. Lu wrote:
> > 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.  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.  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
> >       test.
> >
> > Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
> > ---
> >  binutils/objcopy.c                            |  16 ++++++++-----
> >  .../binutils-all/x86-64/pr33230.obj.bz2       | Bin 0 -> 411 bytes
> >  .../testsuite/binutils-all/x86-64/x86-64.exp  |  22 ++++++++++++++++++
> >  3 files changed, 32 insertions(+), 6 deletions(-)
> >  create mode 100644 binutils/testsuite/binutils-all/x86-64/pr33230.obj.bz2
> >
> > diff --git a/binutils/objcopy.c b/binutils/objcopy.c
> > index 905ce917708..eccb110f3a6 100644
> > --- a/binutils/objcopy.c
> > +++ b/binutils/objcopy.c
> > @@ -2661,7 +2661,8 @@ set_long_section_mode (bfd *output_bfd, bfd *input_bfd, enum long_section_name_h
> >     Returns TRUE upon success, FALSE otherwise.  */
> >
> >  static bool
> > -copy_object (bfd *ibfd, bfd *obfd, const bfd_arch_info_type *input_arch)
> > +copy_object (bfd *ibfd, bfd *obfd, const bfd_arch_info_type *input_arch,
> > +          bool target_defaulted)
> >  {
> >    bfd_vma start;
> >    long symcount;
> > @@ -2812,7 +2813,7 @@ copy_object (bfd *ibfd, bfd *obfd, const bfd_arch_info_type *input_arch)
> >        imach = 0;
> >      }
> >    if (!bfd_set_arch_mach (obfd, iarch, imach)
> > -      && (ibfd->target_defaulted
> > +      && (target_defaulted
> >         || bfd_get_arch (ibfd) != bfd_get_arch (obfd)))
> >      {
> >        if (bfd_get_arch (ibfd) == bfd_arch_unknown)
> > @@ -3615,7 +3616,8 @@ fail:
> >  static bool
> >  copy_archive (bfd *ibfd, bfd *obfd, const char *output_target,
> >             bool force_output_target,
> > -           const bfd_arch_info_type *input_arch)
> > +           const bfd_arch_info_type *input_arch,
> > +           bool target_defaulted)
> >  {
> >    struct name_list
> >      {
> > @@ -3772,7 +3774,8 @@ copy_archive (bfd *ibfd, bfd *obfd, const char *output_target,
> >  #endif
> >        if (ok_object)
> >       {
> > -       ok = copy_object (this_element, output_element, input_arch);
> > +       ok = copy_object (this_element, output_element, input_arch,
> > +                         target_defaulted);
> >
> >         if (!ok && bfd_get_arch (this_element) == bfd_arch_unknown)
> >           /* Try again as an unknown object file.  */
> > @@ -3872,6 +3875,7 @@ copy_file (const char *input_filename, const char *output_filename, int ofd,
> >    char **core_matching;
> >    off_t size = get_file_size (input_filename);
> >    const char *target = input_target;
> > +  bool target_defaulted = !input_target;
>
> This ought to be !input_target || strcmp (input_target, "default").

It should be "!input_target || strcmp (input_target, "default") == 0".

I am checking the enclosed patch with a test for --input-target=default.

Thanks.

> OK with that change.
>
> >
> >    if (size < 1)
> >      {
> > @@ -3979,7 +3983,7 @@ copy_file (const char *input_filename, const char *output_filename, int ofd,
> >       }
> >
> >        if (!copy_archive (ibfd, obfd, output_target, force_output_target,
> > -                      input_arch))
> > +                      input_arch, target_defaulted))
> >       status = 1;
> >      }
> >    else if (
> > @@ -4034,7 +4038,7 @@ copy_file (const char *input_filename, const char *output_filename, int ofd,
> >        else
> >  #endif
> >       {
> > -       if (! copy_object (ibfd, obfd, input_arch))
> > +       if (! copy_object (ibfd, obfd, input_arch, target_defaulted))
> >           status = 1;
> >
> >         /* PR 17512: file: 0f15796a.
> > diff --git a/binutils/testsuite/binutils-all/x86-64/pr33230.obj.bz2 b/binutils/testsuite/binutils-all/x86-64/pr33230.obj.bz2
> > new file mode 100644
> > index 0000000000000000000000000000000000000000..6309a2ad810bce05aea0ef594832ee2b974c87c2
> > GIT binary patch
> > literal 411
> > zcmV;M0c8F{T4*^jL0KkKS!8+Sk^li+|L*_({7vNNd=N{;8bH5q-f+MGKmb61KmY+i
> > zAONrdRtq92i5^oYsiuaS0000027uE*Xw5+s^%)u`sir}ufEqLa02%?H002m$Ca0)9
> > zAk#shXwyNU0NR0|27qW3&Q&S^)HcYH35Blu3_)HA)`6CZi~vSKkYJ@)SUQK6P)>9D
> > zF^y~~*G?x_1vd8WaOee5gP%uQK`XMsG0Bf+<eIG@BErL(v?ORj7J)CfU8FVxVp|Y_
> > zOCZ6v#chH!nhT90hIo5kz2|)K+E?dTEkd<^U(6r5`IAGO9N{Ch*6DHz>z2e9dqHN}
> > zDTtL;P{Ni@z>HvG$wP?DXiCJ28VRJS5wwbp$9gh}Cb*4^`j$`xt?@{9ZNj0%9TX`I
> > z;RB26zDJixQAmr$c@;#H6Jw_Zx7<&9$~aJc3HSjvc3lt$$A^xz^p*h$k3%s=Vh|lY
> > zJnCyBBs0i-<ugb`1%XqMqzomB>VhnV#RwND3<#?Y10YvgX|NP`NFhIe7ji{7P>^JK
> > F<dVJ@tX2R3
> >
> > literal 0
> > HcmV?d00001
> >
> > diff --git a/binutils/testsuite/binutils-all/x86-64/x86-64.exp b/binutils/testsuite/binutils-all/x86-64/x86-64.exp
> > index ab1aa50a9a1..7cad31a67e9 100644
> > --- a/binutils/testsuite/binutils-all/x86-64/x86-64.exp
> > +++ b/binutils/testsuite/binutils-all/x86-64/x86-64.exp
> > @@ -254,3 +254,25 @@ run_pr33198_test "" "-R .gnu.lto_* -R .gnu.debuglto_* -R .llvm.lto -N __gnu_lto_
> >  run_pr33198_test "-fat" "-R .gnu.lto_* -R .gnu.debuglto_* -R .llvm.lto -N __gnu_lto_v1"
> >  run_pr33198_test "" "-R .llvm.lto"
> >  run_pr33198_test "-fat" "-R .llvm.lto"
> > +
> > +set t $srcdir/$subdir/pr33230.obj.bz2
> > +# We need to strip the ".bz2", but can leave the dirname.
> > +set test $subdir/[file tail $t]
> > +set testname [file rootname $test]
> > +verbose $testname
> > +set obj tmpdir/pr33230.o
> > +if {[catch "system \"bzip2 -dc $t > $obj\""] != 0} {
> > +  untested "bzip2 -dc ($testname)"
> > +} else {
> > +    set cmd "$STRIP $obj -o ${obj}.strip"
> > +    send_log "$cmd\n"
> > +    verbose "$cmd" 1
> > +    catch "exec $cmd" got
> > +    if [regexp "Unable to recognise the format" $got] then {
> > +     pass "$testname"
> > +    } else {
> > +     send_log "$got\n"
> > +     verbose "$got" 1
> > +     fail "$testname"
> > +    }
> > +}
> > --
> > 2.50.1
>
> --
> Alan Modra



-- 
H.J.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: v2-0001-strip-Don-t-check-target_defaulted-in-input-BFD.patch
Type: text/x-patch
Size: 6294 bytes
Desc: not available
URL: <https://sourceware.org/pipermail/binutils/attachments/20250730/170def83/attachment.bin>


More information about the Binutils mailing list