[PATCH v3] objcopy: Add ELF header e_flags option in objcopy.
H.J. Lu
hjl.tools@gmail.com
Sun Apr 17 22:58:20 GMT 2022
On Sun, Apr 17, 2022, 1:25 AM liuzhensong <liuzhensong@loongson.cn> wrote:
> Specify flags when copying binary to ELF.
> The flags is treated as an absolute number to be
> stored in the e_flags field of the ELF header.
>
> Usage:
> objcopy -I binary -O target --elf-eflags=val bin_file elf_file
>
> binutils/
> * objcopy.c
> * doc/binutils.texi:Add the new objcopy option.
> ---
> binutils/doc/binutils.texi | 7 +++++++
> binutils/objcopy.c | 16 ++++++++++++++++
> 2 files changed, 23 insertions(+)
>
> diff --git a/binutils/doc/binutils.texi b/binutils/doc/binutils.texi
> index 2c234c682aa..65230775b29 100644
> --- a/binutils/doc/binutils.texi
> +++ b/binutils/doc/binutils.texi
> @@ -1326,6 +1326,7 @@ objcopy [@option{-F}
> @var{bfdname}|@option{--target=}@var{bfdname}]
> [@option{--merge-notes}]
> [@option{--no-merge-notes}]
> [@option{--verilog-data-width=@var{val}}]
> + [@option{--elf-eflags=}@var{value}]
> [@option{-v}|@option{--verbose}]
> [@option{-V}|@option{--version}]
> [@option{--help}] [@option{--info}]
> @@ -2187,6 +2188,12 @@ For Verilog output, this options controls the
> number of bytes
> converted for each output data element. The input target controls the
> endianness of the conversion.
>
> +@item --elf-eflags=@var{value}
> +Meaningful only for ELF output. Use @var{value} as e_flags of the
> +output. This is useful for changing the e_flags of an existing binary.
> +The @var{value} is treated as an absolute number to be stored in the
> +e_flags field of the ELF header.
> +
> @item -v
> @itemx --verbose
> Verbose output: list all object files modified. In the case of
> diff --git a/binutils/objcopy.c b/binutils/objcopy.c
> index 6fb31c8cac7..3c1eaf5ec1a 100644
> --- a/binutils/objcopy.c
> +++ b/binutils/objcopy.c
> @@ -303,6 +303,10 @@ enum long_section_name_handling
> This is also the only behaviour for 'strip'. */
> static enum long_section_name_handling long_section_names = KEEP;
>
> +/* Specify ELF header e_flags? */
> +static bool elf_eflags_set = false;
> +static unsigned int elf_eflags = 0;
> +
> /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
> enum command_line_switch
> {
> @@ -321,6 +325,7 @@ enum command_line_switch
> OPTION_DEBUGGING,
> OPTION_DECOMPRESS_DEBUG_SECTIONS,
> OPTION_DUMP_SECTION,
> + OPTION_ELF_EFLAGS,
> OPTION_ELF_STT_COMMON,
> OPTION_EXTRACT_DWO,
> OPTION_EXTRACT_SYMBOL,
> @@ -443,6 +448,7 @@ static struct option copy_options[] =
> {"discard-all", no_argument, 0, 'x'},
> {"discard-locals", no_argument, 0, 'X'},
> {"dump-section", required_argument, 0, OPTION_DUMP_SECTION},
> + {"elf-eflags", required_argument, 0, OPTION_ELF_EFLAGS},
> {"elf-stt-common", required_argument, 0, OPTION_ELF_STT_COMMON},
> {"enable-deterministic-archives", no_argument, 0, 'D'},
> {"extract-dwo", no_argument, 0, OPTION_EXTRACT_DWO},
> @@ -684,6 +690,7 @@ copy_usage (FILE *stream, int exit_status)
> --elf-stt-common=[yes|no] Generate ELF common symbols with
> STT_COMMON\n\
> type\n\
> --verilog-data-width <number> Specifies data width, in bytes, for
> verilog output\n\
> + --elf-eflags=<value> Set e_flags to <value> for ELF
> output\n\
> -M --merge-notes Remove redundant entries in note
> sections\n\
> --no-merge-notes Do not attempt to remove redundant
> notes (default)\n\
> -v --verbose List all object files modified\n\
> @@ -3496,6 +3503,10 @@ copy_object (bfd *ibfd, bfd *obfd, const
> bfd_arch_info_type *input_arch)
> }
> }
>
> + if (bfd_get_flavour (obfd) == bfd_target_elf_flavour
> + && elf_eflags_set)
> + elf_elfheader (obfd)->e_flags = elf_eflags;
> +
> return true;
> }
>
> @@ -5863,6 +5874,11 @@ copy_main (int argc, char *argv[])
> fatal (_("verilog data width must be at least 1 byte"));
> break;
>
> + case OPTION_ELF_EFLAGS:
> + elf_eflags_set = true;
> + elf_eflags = parse_vma (optarg, "--elf-eflags");
> + break;
> +
> case 0:
> /* We've been given a long option. */
> break;
> --
> 2.31.1
>
Should we extend elfedit instead?
H.J.
More information about the Binutils
mailing list