From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from loongson.cn (mail.loongson.cn [114.242.206.163]) by sourceware.org (Postfix) with ESMTP id D1C7F3857C48 for ; Sun, 17 Apr 2022 07:33:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org D1C7F3857C48 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=loongson.cn Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=loongson.cn Received: from [10.20.4.152] (unknown [10.20.4.152]) by mail.loongson.cn (Coremail) with SMTP id AQAAf9AxuszYwltiwmAmAA--.48706S3; Sun, 17 Apr 2022 15:33:44 +0800 (CST) Subject: Re: [PATCH v2] objcopy: Add elf header e_flags option in objcopy. To: WANG Xuerui , binutils@sourceware.org References: <20220415094132.284598-1-liuzhensong@loongson.cn> From: liuzhensong Message-ID: Date: Sun, 17 Apr 2022 15:33:44 +0800 User-Agent: Mozilla/5.0 (X11; Linux mips64; rv:68.0) Gecko/20100101 Thunderbird/68.7.0 MIME-Version: 1.0 In-Reply-To: Content-Language: en-US X-CM-TRANSID: AQAAf9AxuszYwltiwmAmAA--.48706S3 X-Coremail-Antispam: 1UD129KBjvJXoW3GFyUWrWfZryxWrWkWryUWrg_yoW7uFWrpw 4rGF15JFy5Jry8X3ZrJF1UWryrZw48X3yUArn8J3WDAr1rXryjqr10gF1Y9F1UAF40y3W5 A3W7ZFyUuw47Xr7anT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUvjb7Iv0xC_Kw4lb4IE77IF4wAFF20E14v26r1j6r4UM7CY07I2 0VC2zVCF04k26cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28lY4IEw2IIxxk0rw A2F7IY1VAKz4vEj48ve4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Jr0_JF4l84ACjcxK6xII jxv20xvEc7CjxVAFwI0_Jr0_Gr1l84ACjcxK6I8E87Iv67AKxVWxJVW8Jr1l84ACjcxK6I 8E87Iv6xkF7I0E14v26r4UJVWxJr1le2I262IYc4CY6c8Ij28IcVAaY2xG8wAv7VC0I7IY x2IY67AKxVWUJVWUGwAv7VC2z280aVAFwI0_Jr0_Gr1lOx8S6xCaFVCjc4AY6r1j6r4UM4 x0Y48IcVAKI48JMx8GjcxK6IxK0xIIj40E5I8CrwCYjI0SjxkI62AI1cAE67vIY487MxkI ecxEwVCm-wCF04k20xvY0x0EwIxGrwCFx2IqxVCFs4IE7xkEbVWUJVW8JwC20s026c02F4 0E14v26r106r1rMI8I3I0E7480Y4vE14v26r106r1rMI8E67AF67kF1VAFwI0_Jrv_JF1l IxkGc2Ij64vIr41lIxAIcVC0I7IYx2IY67AKxVWUJVWUCwCI42IY6xIIjxv20xvEc7CjxV AFwI0_Jr0_Gr1lIxAIcVCF04k26cxKx2IYs7xG6rW3Jr0E3s1lIxAIcVC2z280aVAFwI0_ Jr0_Gr1lIxAIcVC2z280aVCY1x0267AKxVWUJVW8JbIYCTnIWIevJa73UjIFyTuYvjxUqW xRDUUUU X-CM-SenderInfo: holx6xphqv003j6o00pqjv00gofq/ X-Spam-Status: No, score=-13.0 required=5.0 tests=BAYES_00, BODY_8BITS, GIT_PATCH_0, HTML_MESSAGE, KAM_DMARC_STATUS, NICE_REPLY_A, SPF_HELO_PASS, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit X-Content-Filtered-By: Mailman/MimeDel 2.1.29 X-BeenThere: binutils@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Binutils mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 17 Apr 2022 07:33:50 -0000 On 2022/4/15 下午6:06, WANG Xuerui wrote: > Hi, > > On 4/15/22 17:41, liuzhensong wrote: >> Specify flags when copying binary to elf file. >> This flags will be written to the e_flags of the elf header. >> >> usage: >>    objcopy -I binary -O target --alt-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..b36aad05c98 100644 >> --- a/binutils/doc/binutils.texi >> +++ b/binutils/doc/binutils.texi >> @@ -1302,6 +1302,7 @@ objcopy [@option{-F} >> @var{bfdname}|@option{--target=}@var{bfdname}] >>           [@option{--weaken-symbols=}@var{filename}] >>           [@option{--add-symbol} >> @var{name}=[@var{section}:]@var{value}[,@var{flags}]] >>           [@option{--alt-machine-code=}@var{index}] >> +        [@option{--alt-elf-eflags=}@var{value}] >>           [@option{--prefix-symbols=}@var{string}] >>           [@option{--prefix-sections=}@var{string}] >>           [@option{--prefix-alloc-sections=}@var{string}] >> @@ -1942,6 +1943,12 @@ being used.  For ELF based architectures if >> the @var{index} >>   alternative does not exist then the value is treated as an absolute >>   number to be stored in the e_machine field of the ELF header. >>   +@item --alt-elf-eflags=@var{value} >> +If the output file has alternate ELF header e_flags, use the >> @var{vaule} > > Typo; "value". > >> +instead of the default one.  This is useful in case objcopy a binary to >> +an ELF to specify the e_flags. The value is treated as an absolute >> +number to be stored in the e_flags field of the ELF header. > > Actually the whole description seems inaccurate (which field is the > alternate to e_flags? what's e_flags's "default" value?), and the > English is broken. > > By reading the implementation it's clear the value specified here > would get into any output file that is ELF. So no need to call it > "alt"; a name like "--elf-eflags" would be enough. > > Something like this would be better IMO: > > "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 --writable-text >>   Mark the output text as writable.  This option isn't meaningful for >> all >>   object file formats. >> diff --git a/binutils/objcopy.c b/binutils/objcopy.c >> index 6fb31c8cac7..032caf565d1 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; >>   +/* Use alternative elf header e_flags?  */ >> +static bool alt_elf_eflags_set = false; >> +static unsigned int alt_elf_eflags = 0; >> + >>   /* 150 isn't special; it's just an arbitrary non-ASCII char value.  */ >>   enum command_line_switch >>   { >> @@ -310,6 +314,7 @@ enum command_line_switch >>     OPTION_ADD_GNU_DEBUGLINK, >>     OPTION_ADD_SYMBOL, >>     OPTION_ALT_MACH_CODE, >> +  OPTION_ALT_ELF_EFLAGS, >>     OPTION_CHANGE_ADDRESSES, >>     OPTION_CHANGE_LEADING_CHAR, >>     OPTION_CHANGE_SECTION_ADDRESS, >> @@ -427,6 +432,7 @@ static struct option copy_options[] = >>     {"adjust-vma", required_argument, 0, OPTION_CHANGE_ADDRESSES}, >>     {"adjust-warnings", no_argument, 0, OPTION_CHANGE_WARNINGS}, >>     {"alt-machine-code", required_argument, 0, OPTION_ALT_MACH_CODE}, >> +  {"alt-elf-eflags", required_argument, 0, OPTION_ALT_ELF_EFLAGS}, >>     {"binary-architecture", required_argument, 0, 'B'}, >>     {"byte", required_argument, 0, 'b'}, >>     {"change-addresses", required_argument, 0, OPTION_CHANGE_ADDRESSES}, >> @@ -660,6 +666,7 @@ copy_usage (FILE *stream, int exit_status) >>        --weaken-symbols        -W for all symbols listed in >> \n\ >>        --add-symbol =[
:][,] Add a symbol\n\ >>        --alt-machine-code     Use the target's 'th >> alternative machine\n\ >> +     --alt-elf-eflags=      Use the alternative elf header >> e_flags\n\ >>        --writable-text               Mark the output text as writable\n\ >>        --readonly-text               Make the output text write >> protected\n\ >>        --pure                        Mark the output file as demand >> paged\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 >> +      && alt_elf_eflags_set) >> +    elf_elfheader (obfd)->e_flags = alt_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_ALT_ELF_EFLAGS: >> +      alt_elf_eflags_set = true; >> +      alt_elf_eflags = parse_vma (optarg, "--alt-elf-eflags"); >> +      break; >> + >>       case 0: >>         /* We've been given a long option.  */ >>         break; Thank you for the review, it looks more appropriate. I will send a v3 with the changes. -- liuzhensong