[PATCH v2] strip: Treat "default" output_target as unspecified

Alan Modra amodra@gmail.com
Thu Jul 31 08:10:22 GMT 2025


On Wed, Jul 30, 2025 at 08:51:54PM -0700, H.J. Lu wrote:
> On Wed, Jul 30, 2025 at 8:49 PM H.J. Lu <hjl.tools@gmail.com> wrote:
> >
> > On Wed, Jul 30, 2025 at 7:35 PM Alan Modra <amodra@gmail.com> wrote:
> > >
> > > On Wed, Jul 30, 2025 at 06:49:08PM -0700, H.J. Lu wrote:
> > > > On Wed, Jul 30, 2025 at 6:27 PM Alan Modra <amodra@gmail.com> wrote:
> > > > > Yes, I typoed that.  The tests need some tweaking as they both fail
> > > > > with --enable-targets=all.
> > > > >
> > > >
> > > > Like this?
> > >
> > > That will do I guess.  It would be nicer to run readelf and check the
> > > machine if strip succeeded.
> >
> > Run readelf uncovered an output_target bug.   "default" output_target
> > should be treated as unspecified.   Here is the patch to fix it with tests.
> >
> > OK for master?
> 
> Here is the v2 patch to also cover archive.

OK.

> 
> -- 
> H.J.

> From e927b76ef79b60a28760b739b1adbf2efbfd3805 Mon Sep 17 00:00:00 2001
> From: "H.J. Lu" <hjl.tools@gmail.com>
> Date: Wed, 30 Jul 2025 20:34:46 -0700
> Subject: [PATCH v2] strip: Treat "default" output_target as unspecified
> 
> 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>
> ---
>  binutils/objcopy.c                            |  6 +-
>  .../testsuite/binutils-all/x86-64/x86-64.exp  | 76 ++++++++++++++-----
>  2 files changed, 60 insertions(+), 22 deletions(-)
> 
> diff --git a/binutils/objcopy.c b/binutils/objcopy.c
> index e90117af90b..5774711abe6 100644
> --- a/binutils/objcopy.c
> +++ b/binutils/objcopy.c
> @@ -3953,7 +3953,8 @@ copy_file (const char *input_filename, const char *output_filename, int ofd,
>  
>        /* bfd_get_target does not return the correct value until
>  	 bfd_check_format succeeds.  */
> -      if (output_target == NULL)
> +      if (output_target == NULL
> +	  || strcmp (output_target, "default") == 0)
>  	{
>  	  output_target = bfd_get_target (ibfd);
>  	  force_output_target = false;
> @@ -4008,7 +4009,8 @@ copy_file (const char *input_filename, const char *output_filename, int ofd,
>  
>        /* bfd_get_target does not return the correct value until
>  	 bfd_check_format succeeds.  */
> -      if (output_target == NULL)
> +      if (output_target == NULL
> +	  || strcmp (output_target, "default") == 0)
>  	output_target = bfd_get_target (ibfd);
>  
>        if (ofd >= 0)
> diff --git a/binutils/testsuite/binutils-all/x86-64/x86-64.exp b/binutils/testsuite/binutils-all/x86-64/x86-64.exp
> index a3fe6b13a7c..6d1b308eca6 100644
> --- a/binutils/testsuite/binutils-all/x86-64/x86-64.exp
> +++ b/binutils/testsuite/binutils-all/x86-64/x86-64.exp
> @@ -255,6 +255,56 @@ run_pr33198_test "-fat" "-R .gnu.lto_* -R .gnu.debuglto_* -R .llvm.lto -N __gnu_
>  run_pr33198_test "" "-R .llvm.lto"
>  run_pr33198_test "-fat" "-R .llvm.lto"
>  
> +proc run_pr33230_test { testname obj strip_flags run_readelf } {
> +    global srcdir
> +    global subdir
> +    global READELF
> +    global STRIP
> +
> +    append strip_flags " --strip-debug"
> +    append testname " ($strip_flags)"
> +
> +    set cmd "$STRIP $strip_flags $obj -o ${obj}.strip"
> +    send_log "$cmd\n"
> +    verbose "$cmd" 1
> +    catch "exec $cmd" got
> +    if { $run_readelf == "yes" } {
> +	if ![string match "" $got] then {
> +	    send_log "$got\n"
> +	    verbose "$got" 1
> +	    fail "$testname (${obj})"
> +	    return
> +	}
> +	set cmd "$READELF -h $obj"
> +	send_log "$cmd\n"
> +	verbose "$cmd" 1
> +	catch "exec $cmd" got
> +	if { ![regexp "Machine: +AArch64" $got] } then {
> +	    send_log "$got\n"
> +	    verbose "$got" 1
> +	    fail "$testname (${obj})"
> +	    return
> +	}
> +	set cmd "$READELF -h ${obj}.strip"
> +	send_log "$cmd\n"
> +	verbose "$cmd" 1
> +	catch "exec $cmd" got
> +	if { ![regexp "Machine: +AArch64" $got] } then {
> +	    send_log "$got\n"
> +	    verbose "$got" 1
> +	    fail "$testname (${obj}.strip)"
> +	    return
> +	}
> +    } elseif { ![regexp "Unable to recognise the format" $got] } then {
> +	send_log "$got\n"
> +	verbose "$got" 1
> +	fail "$testname"
> +	return
> +    }
> +
> +    pass "$testname"
> +}
> +
>  set t $srcdir/$subdir/pr33230.obj.bz2
>  # We need to strip the ".bz2", but can leave the dirname.
>  set test $subdir/[file tail $t]
> @@ -264,26 +314,12 @@ 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"
> -    }
> -    set cmd "$STRIP --input-target=default $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"
> +    catch "exec $STRIP --help" got
> +    if { [regexp "elf64-littleaarch64" $got] } {
> +	set run_readelf "yes"
>      } else {
> -	send_log "$got\n"
> -	verbose "$got" 1
> -	fail "$testname (--input-target=default)"
> +	set run_readelf "no"
>      }
> +    run_pr33230_test "$testname" $obj "" $run_readelf
> +    run_pr33230_test "$testname" $obj "--input-target=default" $run_readelf
>  }
> -- 
> 2.50.1
> 


-- 
Alan Modra


More information about the Binutils mailing list