From patchwork Fri Jul 7 05:43:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fangrui Song X-Patchwork-Id: 116978 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9f45:0:b0:3ea:f831:8777 with SMTP id v5csp3041964vqx; Thu, 6 Jul 2023 22:43:24 -0700 (PDT) X-Google-Smtp-Source: APBJJlEKJNjngVNVfmWSJO53EpGBwiSbLa5SeWH++y5DXjsL517MpfH0FDeDRtfz2LgfGTEtHm10 X-Received: by 2002:a05:6402:2789:b0:51e:2e87:2416 with SMTP id b9-20020a056402278900b0051e2e872416mr3554900ede.5.1688708604227; Thu, 06 Jul 2023 22:43:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688708604; cv=none; d=google.com; s=arc-20160816; b=orOlUjhHGhO7T+/BkUlW7lirURquyOaHAi1ZTwuBfnJM1NBHxAVoeECLD8twQ3wUR7 qNlzQ7v4YsqgWRszmyazmlYbMydvhPKzhuGdPn/2rtUyBeNHTOGsA4LBXWrjF+H4nJe0 oIRWw8uOXfBqGwsbyYQeVcXDN8l/D3zNT2O6Kd+ZssXKxCPdqXZIdnRGEYB910QPp5qM gL/R2ApunE+2KFfxWyCMsLhH/9+G7yxePPbuiGE932CxOUDhcGzKs2vAAH1aYbcxNhNd yRdi/Plyn00ZFDks06uAT5cXXHZnlBAJhSXBeKWFJb69tRfyx6TGyaszp448ManWOD7/ fVfg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:cc:to:subject :message-id:mime-version:date:dmarc-filter:delivered-to :dkim-signature:dkim-filter; bh=cOsL/d1pwTOV2pOG5GSvupptBBNuusIqpoWcgcIXU7E=; fh=ffcRW9L3u7x+5Lpny738WHntCUkf88jPj4NdzgonHD4=; b=LHzpCQTtO0WyVcQEXyVVBRg20JD4ZDuEdi9W+9gzgAAGxWGOllI6KbwK1F7Qpmx5fa iwDhZhBI1z6EEPAs10N3DxaL4ydAAmVJT2ws16U856nn0cZAkewIZMOy+IVqxrDQvMWf htK8HtYuzbGqk9vGunlfWGTxtlS9rlyY6RaAnvULCR1RKT2iEd7/HlS7VS8m2WgUCxWq GIBL9Hc9azI4uy8o6LhOFhr1A4S5zRpiXj8Cc0BV4PwF/wX4LrPDaHd5u5AyraN0t0aT 4ySgVHyZVyYUeIKItUDKBlBOUSvIbDB02tbSvz396Q/h0A+9XeeUfaCKFiA+TAPnl8yV bAjA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=mYyk3YqY; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="binutils-bounces+ouuuleilei=gmail.com@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=sourceware.org Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id x21-20020aa7d395000000b0051dd27b0843si1794703edq.44.2023.07.06.22.43.23 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 Jul 2023 22:43:24 -0700 (PDT) Received-SPF: pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=mYyk3YqY; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="binutils-bounces+ouuuleilei=gmail.com@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3D133385800A for ; Fri, 7 Jul 2023 05:43:20 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3D133385800A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1688708600; bh=cOsL/d1pwTOV2pOG5GSvupptBBNuusIqpoWcgcIXU7E=; h=Date:Subject:To:Cc:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=mYyk3YqYRJ4G51/ACd1Ku6HFfmDqAoIKcXWlXrCJzeA3m1Tnr7hd+BO3uVOaOXHen iPnLUx5ZT5Vn68ro1HzZlcvU5vkww+iOwmS/vnpFp7KshT9mtqTs5mDg+9FxRfydJN JHsq3oO7kD9cTWHa4nDEMywPbePRMR7EggrqGgPg= X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by sourceware.org (Postfix) with ESMTPS id AFF973858D38 for ; Fri, 7 Jul 2023 05:43:10 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org AFF973858D38 Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-c5cea5773e8so1766412276.1 for ; Thu, 06 Jul 2023 22:43:10 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688708590; x=1691300590; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=cOsL/d1pwTOV2pOG5GSvupptBBNuusIqpoWcgcIXU7E=; b=Gg4On21q+RAosqIy43pCmQ/lzvpEbDyYmjlolmdiN1LaSosucyD2oErCQp8RZ84PCL 2IXBQ3lcP/qskmlWPrsgEczgt73CNsjDGJ45wDVSjmylRFdz292b+9JI+Nv5Lx6+Sw8h DZjhjTYrtMg/SAtXOb4upTaJsnM1bk72IKgPKbrocrk/L8EkKBJnvwAEyDFI+kLyU4ZD 7I08ewF5YF42CUIie5TqfRGONtLfvH/CD11YGcvSHEJEGYrtdQWXyLYgYHEWK8dB5z8q 5ttOvZBFUNH+8Uuecx8A7PmWlUotcqgEbxU+0pu2NmeNDnH2vit9lScI5jz0oi2g99qx dGpQ== X-Gm-Message-State: ABy/qLYzQKMOQaUOyEsaVyp1iHDfehKkCYKOypsjTIsJdxu1mDzmgtJ8 zwcWzoKWCxnMETSlJWYl4lpbcTb0iqeJpnwfMIHVHoz388TLBF3K/DK/uePrys/QMSgG6JjOoi7 HQ4DLYEg32lmzglOL/NdHgpj6of94OBh33LSfNylWP4k9nLxHNhFpW1naCN2BRUoSmg== X-Received: from maskray.svl.corp.google.com ([2620:15c:2d3:205:2d5e:47ed:cba0:11f]) (user=maskray job=sendgmr) by 2002:a25:b092:0:b0:c62:5700:6520 with SMTP id f18-20020a25b092000000b00c6257006520mr37752ybj.3.1688708589799; Thu, 06 Jul 2023 22:43:09 -0700 (PDT) Date: Thu, 6 Jul 2023 22:43:06 -0700 Mime-Version: 1.0 Message-ID: <20230707054306.2810080-1-maskray@google.com> Subject: [PATCH v3] PR30592 objcopy: allow --set-section-flags to add or remove SHF_X86_64_LARGE To: binutils@sourceware.org, Jan Beulich Cc: Fangrui Song X-Spam-Status: No, score=-19.7 required=5.0 tests=BAYES_00, DKIMWL_WL_MED, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org 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-Patchwork-Original-From: Fangrui Song via Binutils From: Fangrui Song Reply-To: Fangrui Song Errors-To: binutils-bounces+ouuuleilei=gmail.com@sourceware.org Sender: "Binutils" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1770739313532199849?= X-GMAIL-MSGID: =?utf-8?q?1770739313532199849?= For example, objcopy --set-section-flags .data=alloc,large will add SHF_X86_64_LARGE to the .data section. Omitting "large" will drop the SHF_X86_64_LARGE flag. The bfd_section flag is named generically, SEC_ELF_LARGE, in case other processors want to follow SHF_X86_64_LARGE. SEC_ELF_LARGE has the same value as SEC_TIC54X_CLINK and SEC_MEP_VLIW. bfd/ * section.c: Define SEC_ELF_LARGE. * bfd-in2.h: Regenerate. * elf.c (_bfd_elf_make_section_from_shdr): Check SHF_X86_64_LARGE. (elf_fake_sections): Check SEC_ELF_LARGE. (_bfd_elf_init_private_section_data): Drop SHF_X86_64_LARGE for x86-64 so that objcopy --set-section-flags without 'large' clears the section flag. binutils/ * NEWS: Mention the new feature for objcopy. * doc/binutils.texi: Mention "large". * objcopy.c (parse_flags): Parse "large". * testsuite/binutils-all/x86-64/large-sections.d: New. * testsuite/binutils-all/x86-64/large-sections.s: New. * testsuite/binutils-all/x86-64/large-sections-2.d: New. * testsuite/binutils-all/x86-64/large-sections-2.s: New. include/ * elf/common.h: Define SHF_X86_64_LARGE to be used by elf.c. --- Changes from v1: * Add an entry to binutils/NEWS * Adjust doc/binutils.texi wording * Guard a SEC_ELF_LARGE branch with EM_X86_64 check Changes from v2: * Address Jan's comments https://sourceware.org/pipermail/binutils/2023-July/128303.html --- bfd/bfd-in2.h | 3 +++ bfd/elf.c | 12 +++++++++++- bfd/section.c | 3 +++ binutils/NEWS | 3 +++ binutils/doc/binutils.texi | 15 ++++++++------- binutils/objcopy.c | 4 +++- .../binutils-all/x86-64/large-sections-2.d | 15 +++++++++++++++ .../binutils-all/x86-64/large-sections-2.s | 4 ++++ .../binutils-all/x86-64/large-sections.d | 14 ++++++++++++++ .../binutils-all/x86-64/large-sections.s | 8 ++++++++ include/elf/common.h | 2 ++ 11 files changed, 74 insertions(+), 9 deletions(-) create mode 100644 binutils/testsuite/binutils-all/x86-64/large-sections-2.d create mode 100644 binutils/testsuite/binutils-all/x86-64/large-sections-2.s create mode 100644 binutils/testsuite/binutils-all/x86-64/large-sections.d create mode 100644 binutils/testsuite/binutils-all/x86-64/large-sections.s diff --git a/bfd/bfd-in2.h b/bfd/bfd-in2.h index b34c8ef9fc9..03767efb260 100644 --- a/bfd/bfd-in2.h +++ b/bfd/bfd-in2.h @@ -633,6 +633,9 @@ typedef struct bfd_section /* This section contains vliw code. This is for Toshiba MeP only. */ #define SEC_MEP_VLIW 0x20000000 + /* This section has the SHF_X86_64_LARGE flag. This is ELF x86-64 only. */ +#define SEC_ELF_LARGE 0x20000000 + /* All symbols, sizes and relocations in this section are octets instead of bytes. Required for DWARF debug sections as DWARF information is organized in octets, not bytes. */ diff --git a/bfd/elf.c b/bfd/elf.c index d38e0afff2e..fe41798e413 100644 --- a/bfd/elf.c +++ b/bfd/elf.c @@ -1045,6 +1045,11 @@ _bfd_elf_make_section_from_shdr (bfd *abfd, if ((hdr->sh_flags & SHF_EXCLUDE) != 0) flags |= SEC_EXCLUDE; + bed = get_elf_backend_data (abfd); + if (bed->elf_machine_code == EM_X86_64 && + (hdr->sh_flags & SHF_X86_64_LARGE) != 0) + flags |= SEC_ELF_LARGE; + switch (elf_elfheader (abfd)->e_ident[EI_OSABI]) { /* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE, @@ -1104,7 +1109,6 @@ _bfd_elf_make_section_from_shdr (bfd *abfd, if (!bfd_set_section_flags (newsect, flags)) return false; - bed = get_elf_backend_data (abfd); if (bed->elf_backend_section_flags) if (!bed->elf_backend_section_flags (hdr)) return false; @@ -3912,6 +3916,8 @@ elf_fake_sections (bfd *abfd, asection *asect, void *fsarg) } if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE) this_hdr->sh_flags |= SHF_EXCLUDE; + if (bed->elf_machine_code == EM_X86_64 && (asect->flags & SEC_ELF_LARGE) != 0) + this_hdr->sh_flags |= SHF_X86_64_LARGE; /* If the section has relocs, set up a section header for the SHT_REL[A] section. If two relocation sections are required for @@ -8508,6 +8514,10 @@ _bfd_elf_init_private_section_data (bfd *ibfd, elf_section_flags (osec) = (elf_section_flags (isec) & (SHF_MASKOS | SHF_MASKPROC)); + /* objcopy --set-section-flags without "large" drops SHF_X86_64_LARGE. */ + if (get_elf_backend_data (ibfd)->elf_machine_code == EM_X86_64) + elf_section_flags (osec) = (elf_section_flags (isec) & ~SHF_X86_64_LARGE); + /* Copy sh_info from input for mbind section. */ if ((elf_tdata (ibfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0 && elf_section_flags (isec) & SHF_GNU_MBIND) diff --git a/bfd/section.c b/bfd/section.c index 73770294e56..b7c771b4fb8 100644 --- a/bfd/section.c +++ b/bfd/section.c @@ -367,6 +367,9 @@ CODE_FRAGMENT . {* This section contains vliw code. This is for Toshiba MeP only. *} .#define SEC_MEP_VLIW 0x20000000 . +. {* This section has the SHF_X86_64_LARGE flag. This is ELF x86-64 only. *} +.#define SEC_ELF_LARGE 0x20000000 +. . {* All symbols, sizes and relocations in this section are octets . instead of bytes. Required for DWARF debug sections as DWARF . information is organized in octets, not bytes. *} diff --git a/binutils/NEWS b/binutils/NEWS index 563062c6904..cad877b943a 100644 --- a/binutils/NEWS +++ b/binutils/NEWS @@ -1,5 +1,8 @@ -*- text -*- +* objcopy's --set-section-flags now support "large" to set SHF_X86_64_LARGE + for ELF x86-64 objects. + Changes in 2.41: * The MIPS port now supports the Sony Interactive Entertainment Allegrex diff --git a/binutils/doc/binutils.texi b/binutils/doc/binutils.texi index 8314cb57562..309bedf6110 100644 --- a/binutils/doc/binutils.texi +++ b/binutils/doc/binutils.texi @@ -1745,13 +1745,14 @@ Set the flags for any sections matching @var{sectionpattern}. The @var{flags} argument is a comma separated string of flag names. The recognized names are @samp{alloc}, @samp{contents}, @samp{load}, @samp{noload}, @samp{readonly}, @samp{code}, @samp{data}, @samp{rom}, -@samp{exclude}, @samp{share}, and @samp{debug}. You can set the -@samp{contents} flag for a section which does not have contents, but it -is not meaningful to clear the @samp{contents} flag of a section which -does have contents--just remove the section instead. Not all flags are -meaningful for all object file formats. In particular the -@samp{share} flag is only meaningful for COFF format files and not for -ELF format files. +@samp{exclude}, @samp{share}, @samp{debug}, and @samp{large}. +You can set the @samp{contents} flag for a section which does not have +contents, but it is not meaningful to clear the @samp{contents} flag of a +section which does have contents--just remove the section instead. Not all +flags are meaningful for all object file formats. In particular the +@samp{share} flag is only meaningful for COFF format files and not for ELF +format files. The ELF x86-64 specific flag @samp{large} corresponds to +SHF_X86_64_LARGE. @item --set-section-alignment @var{sectionpattern}=@var{align} Set the alignment for any sections matching @var{sectionpattern}. diff --git a/binutils/objcopy.c b/binutils/objcopy.c index 3569b890c7d..cb0d44ec0f4 100644 --- a/binutils/objcopy.c +++ b/binutils/objcopy.c @@ -803,6 +803,7 @@ parse_flags (const char *s) PARSE_FLAG ("contents", SEC_HAS_CONTENTS); PARSE_FLAG ("merge", SEC_MERGE); PARSE_FLAG ("strings", SEC_STRINGS); + PARSE_FLAG ("large", SEC_ELF_LARGE); #undef PARSE_FLAG else { @@ -813,7 +814,8 @@ parse_flags (const char *s) copy[len] = '\0'; non_fatal (_("unrecognized section flag `%s'"), copy); fatal (_("supported flags: %s"), - "alloc, load, noload, readonly, debug, code, data, rom, exclude, share, contents, merge, strings"); + "alloc, load, noload, readonly, debug, code, data, rom, exclude, " + "share, contents, merge, strings, (ELF x86-64 specific) large"); } s = snext; diff --git a/binutils/testsuite/binutils-all/x86-64/large-sections-2.d b/binutils/testsuite/binutils-all/x86-64/large-sections-2.d new file mode 100644 index 00000000000..29ace42cc9e --- /dev/null +++ b/binutils/testsuite/binutils-all/x86-64/large-sections-2.d @@ -0,0 +1,15 @@ +#source: large-sections.s +#PROG: objcopy +#as: --64 +#objcopy: --set-section-flags .ldata=alloc +#readelf: -S -W + +#... + \[[ 0-9]+\] \.text.*[ \t]+PROGBITS[ \t0-9a-f]+AX[ \t]+.* +#... + \[[ 0-9]+\] \.data.*[ \t]+PROGBITS[ \t0-9a-f]+WA[ \t]+.* +#... + \[[ 0-9]+\] \.ltext.*[ \t]+PROGBITS[ \t0-9a-f]+AXl[ \t]+.* +#... + \[[ 0-9]+\] \.ldata.*[ \t]+PROGBITS[ \t0-9a-f]+WA[ \t]+.* +#pass diff --git a/binutils/testsuite/binutils-all/x86-64/large-sections-2.s b/binutils/testsuite/binutils-all/x86-64/large-sections-2.s new file mode 100644 index 00000000000..6f31aa93701 --- /dev/null +++ b/binutils/testsuite/binutils-all/x86-64/large-sections-2.s @@ -0,0 +1,4 @@ + .section .text, "axl" + nop + .section .data, "awl" + .byte 1 diff --git a/binutils/testsuite/binutils-all/x86-64/large-sections.d b/binutils/testsuite/binutils-all/x86-64/large-sections.d new file mode 100644 index 00000000000..5d945e46ba3 --- /dev/null +++ b/binutils/testsuite/binutils-all/x86-64/large-sections.d @@ -0,0 +1,14 @@ +#PROG: objcopy +#as: --64 +#objcopy: --set-section-flags .text=alloc,readonly,code,large --set-section-flags .data=alloc,large +#readelf: -S -W + +#... + \[[ 0-9]+\] \.text.*[ \t]+PROGBITS[ \t0-9a-f]+AXl[ \t]+.* +#... + \[[ 0-9]+\] \.data.*[ \t]+PROGBITS[ \t0-9a-f]+WAl[ \t]+.* +#... + \[[ 0-9]+\] \.ltext.*[ \t]+PROGBITS[ \t0-9a-f]+AXl[ \t]+.* +#... + \[[ 0-9]+\] \.ldata.*[ \t]+PROGBITS[ \t0-9a-f]+WAl[ \t]+.* +#pass diff --git a/binutils/testsuite/binutils-all/x86-64/large-sections.s b/binutils/testsuite/binutils-all/x86-64/large-sections.s new file mode 100644 index 00000000000..072e456a1ed --- /dev/null +++ b/binutils/testsuite/binutils-all/x86-64/large-sections.s @@ -0,0 +1,8 @@ + .section .text, "ax" + nop + .section .data, "aw" + .byte 1 + .section .ltext, "axl" + nop + .section .ldata, "awl" + .byte 1 diff --git a/include/elf/common.h b/include/elf/common.h index ffa6b60bd2b..1397d60402e 100644 --- a/include/elf/common.h +++ b/include/elf/common.h @@ -588,6 +588,8 @@ #define SHF_GNU_MBIND 0x01000000 /* Mbind section. */ +#define SHF_X86_64_LARGE 0x10000000 + /* Compression types. */ #define ELFCOMPRESS_ZLIB 1 /* Compressed with zlib. */ #define ELFCOMPRESS_ZSTD 2 /* Compressed with zstd */