objcopy: add option to specify custom prefix for symbol of binary input

When using --input-target=binary, objcopy currently derives symbol names
from a mangled version of the input file name.  This approach can lead to
unpredictable results, as the generated symbols depend on the file path and
working directory.

This patch introduces a new option:

  --binary-symbol-prefix <prefix> Use <prefix> as the base symbol name for
                                    the input file (default: derived from
                                    file name)

It allows specifying an explicit symbol prefix, while preserving the existing
behavior as a fallback.

Signed-off-by: Alon Bar-Lev <alon.barlev@gmail.com>
This commit is contained in:
Alon Bar-Lev
2025-10-29 12:54:40 +02:00
committed by Alan Modra
parent f190abc584
commit 6fa97b38ff
4 changed files with 79 additions and 4 deletions

View File

@@ -41,6 +41,8 @@
a start symbol, an end symbol, and an absolute length symbol. */ a start symbol, an end symbol, and an absolute length symbol. */
#define BIN_SYMS 3 #define BIN_SYMS 3
char *bfd_binary_symbol_prefix = NULL;
/* Create a binary object. Invoked via bfd_set_format. */ /* Create a binary object. Invoked via bfd_set_format. */
static bool static bool
@@ -122,18 +124,25 @@ static char *
mangle_name (bfd *abfd, char *suffix) mangle_name (bfd *abfd, char *suffix)
{ {
bfd_size_type size; bfd_size_type size;
const char *prefix;
const char *prefix_amend = "";
char *buf; char *buf;
char *p; char *p;
size = (strlen (bfd_get_filename (abfd)) prefix = bfd_binary_symbol_prefix;
+ strlen (suffix) if (prefix == NULL)
+ sizeof "_binary__"); {
prefix = "_binary_";
prefix_amend = bfd_get_filename (abfd);
}
size = strlen (prefix) + strlen (prefix_amend) + strlen (suffix) + 2;
buf = (char *) bfd_alloc (abfd, size); buf = (char *) bfd_alloc (abfd, size);
if (buf == NULL) if (buf == NULL)
return ""; return "";
sprintf (buf, "_binary_%s_%s", bfd_get_filename (abfd), suffix); sprintf (buf, "%s%s_%s", prefix, prefix_amend, suffix);
/* Change any non-alphanumeric characters to underscores. */ /* Change any non-alphanumeric characters to underscores. */
for (p = buf; *p; p++) for (p = buf; *p; p++)

View File

@@ -1315,6 +1315,7 @@ objcopy [@option{-F} @var{bfdname}|@option{--target=}@var{bfdname}]
[@option{--keep-global-symbols=}@var{filename}] [@option{--keep-global-symbols=}@var{filename}]
[@option{--localize-symbols=}@var{filename}] [@option{--localize-symbols=}@var{filename}]
[@option{--weaken-symbols=}@var{filename}] [@option{--weaken-symbols=}@var{filename}]
[@option{--binary-symbol-prefix}@var{string}]
[@option{--add-symbol} @var{name}=[@var{section}:]@var{value}[,@var{flags}]] [@option{--add-symbol} @var{name}=[@var{section}:]@var{value}[,@var{flags}]]
[@option{--alt-machine-code=}@var{index}] [@option{--alt-machine-code=}@var{index}]
[@option{--prefix-symbols=}@var{string}] [@option{--prefix-symbols=}@var{string}]
@@ -1554,6 +1555,20 @@ given more than once. Note - unique symbols are not converted.
@itemx --weaken-symbol=@var{symbolname} @itemx --weaken-symbol=@var{symbolname}
Make symbol @var{symbolname} weak. This option may be given more than once. Make symbol @var{symbolname} weak. This option may be given more than once.
@item --binary-symbol-prefix=@var{string}
When used with @option{--input-target=binary}, sets @var{string} as the
base name for the symbols generated for the input file. These symbols are:
@example
@var{string}_start
@var{string}_end
@var{string}_size
@end example
By default, the binary input handler derives the prefix symbol name from a
mangled version of the input file name and _binary prefix. This option
allows specifying it explicitly.
@item --globalize-symbol=@var{symbolname} @item --globalize-symbol=@var{symbolname}
Give symbol @var{symbolname} global scoping so that it is visible Give symbol @var{symbolname} global scoping so that it is visible
outside of the file in which it is defined. This option may be given outside of the file in which it is defined. This option may be given

View File

@@ -334,6 +334,7 @@ enum command_line_switch
OPTION_HEAP, OPTION_HEAP,
OPTION_IMAGE_BASE, OPTION_IMAGE_BASE,
OPTION_IMPURE, OPTION_IMPURE,
OPTION_BINARY_SYMBOL_PREFIX,
OPTION_INTERLEAVE_WIDTH, OPTION_INTERLEAVE_WIDTH,
OPTION_KEEPGLOBAL_SYMBOLS, OPTION_KEEPGLOBAL_SYMBOLS,
OPTION_KEEP_FILE_SYMBOLS, OPTION_KEEP_FILE_SYMBOLS,
@@ -465,6 +466,7 @@ static struct option copy_options[] =
{"info", no_argument, 0, OPTION_FORMATS_INFO}, {"info", no_argument, 0, OPTION_FORMATS_INFO},
{"input-format", required_argument, 0, 'I'}, /* Obsolete */ {"input-format", required_argument, 0, 'I'}, /* Obsolete */
{"input-target", required_argument, 0, 'I'}, {"input-target", required_argument, 0, 'I'},
{"binary-symbol-prefix", required_argument, 0, OPTION_BINARY_SYMBOL_PREFIX},
{"interleave", optional_argument, 0, 'i'}, {"interleave", optional_argument, 0, 'i'},
{"interleave-width", required_argument, 0, OPTION_INTERLEAVE_WIDTH}, {"interleave-width", required_argument, 0, OPTION_INTERLEAVE_WIDTH},
{"keep-file-symbols", no_argument, 0, OPTION_KEEP_FILE_SYMBOLS}, {"keep-file-symbols", no_argument, 0, OPTION_KEEP_FILE_SYMBOLS},
@@ -540,6 +542,11 @@ extern char *program_name;
extern int is_strip; extern int is_strip;
#endif #endif
/* The symbol prefix of a binary input blob.
* <p>_start, <p>_end, <p>_size
*/
extern char *bfd_binary_symbol_prefix;
/* The maximum length of an S record. This variable is defined in srec.c /* The maximum length of an S record. This variable is defined in srec.c
and can be modified by the --srec-len parameter. */ and can be modified by the --srec-len parameter. */
extern unsigned int _bfd_srec_len; extern unsigned int _bfd_srec_len;
@@ -678,6 +685,9 @@ copy_usage (FILE *stream, int exit_status)
--globalize-symbols <file> --globalize-symbol for all in <file>\n\ --globalize-symbols <file> --globalize-symbol for all in <file>\n\
--keep-global-symbols <file> -G for all symbols listed in <file>\n\ --keep-global-symbols <file> -G for all symbols listed in <file>\n\
--weaken-symbols <file> -W for all symbols listed in <file>\n\ --weaken-symbols <file> -W for all symbols listed in <file>\n\
--binary-symbol-prefix <prefix>\n\
Use <prefix> as the base symbol name for the input file\n\
(default: derived from file name)\n\
--add-symbol <name>=[<section>:]<value>[,<flags>] Add a symbol\n\ --add-symbol <name>=[<section>:]<value>[,<flags>] Add a symbol\n\
--alt-machine-code <index> Use the target's <index>'th alternative machine\n\ --alt-machine-code <index> Use the target's <index>'th alternative machine\n\
--writable-text Mark the output text as writable\n\ --writable-text Mark the output text as writable\n\
@@ -5395,6 +5405,10 @@ copy_main (int argc, char *argv[])
input_target = optarg; input_target = optarg;
break; break;
case OPTION_BINARY_SYMBOL_PREFIX:
bfd_binary_symbol_prefix = optarg;
break;
case 'O': case 'O':
case 'd': /* "destination" - 'O' is preferred */ case 'd': /* "destination" - 'O' is preferred */
output_target = optarg; output_target = optarg;

View File

@@ -1602,3 +1602,40 @@ proc objcopy_tek2bin {} {
} }
objcopy_tek2bin objcopy_tek2bin
# Test objcopy -I binary and --binary-symbol-prefix
proc binary_symbol {name file args symbol} {
global OBJCOPY
global NM
global NMFLAGS
set test "binary symbol ($name)"
# detect a valid target, take the first supported
set target [lindex [split [binutils_run $OBJCOPY "--info"] \n] 1]
set out tmpdir/binary_symbol.o
set got [binutils_run $OBJCOPY "-I binary -O $target $args $file $out"]
set expected [list "${symbol}_end" "${symbol}_size" "${symbol}_start"]
set exec_output [binutils_run $NM "-a $NMFLAGS $out"]
set exec_output [prune_warnings $exec_output]
set actual {}
while {[regexp {^[0-9a-fA-F]+?[ ]+[TtDdA] ([0-9a-zA-Z_]+)[\r\n]+(.*)$} ${exec_output} all s rest]} {
set actual [concat $actual $s]
set exec_output $rest
}
if {[lsort $expected] ne [lsort $actual]} {
send_log "expected: $expected, actual: $actual\n"
fail $test
return
}
pass $test
}
binary_symbol implicit $srcdir/$subdir/version.s "" _binary_[regsub -all {[^0-9a-zA-Z]} $srcdir/$subdir/version.s _]
binary_symbol explicit $srcdir/$subdir/version.s "--binary-symbol-prefix symbol1" symbol1