From patchwork Wed Jun 28 23:16:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fangrui Song X-Patchwork-Id: 114109 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp9268586vqr; Wed, 28 Jun 2023 16:16:25 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5u7Rey7gOD8Jt5DcGeVU32vjfO6grUOf+ZjDdWDzBq54Rjw2e66KwQi+Qno2KRZ/DqVVHb X-Received: by 2002:a2e:8305:0:b0:2b6:a5f4:fdb2 with SMTP id a5-20020a2e8305000000b002b6a5f4fdb2mr6111344ljh.46.1687994184933; Wed, 28 Jun 2023 16:16:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687994184; cv=none; d=google.com; s=arc-20160816; b=TOxqBrdpXabxFpyq6W4nvvhwGwWXuCpGT1zCtdIoiACSEdUP/XwjDJAEmLPVUa8l2B cyzQu7dGTN+7kmhMIyT5CP/VQVsL9vkw6+wQpvjAqlJsY46CGrq1tsoohdFAWsS/qA7C uRXHnC3bet7OjTMaUCaLQqVeY+UAmRRp2iFhkijMngKsUVVTggKw23slhDYMEJTVAw5P c00KhSjFyxXIgQiw7aF1QkMwf2I6GTKR98OTSZ3e45XuEw251Df/XDvACDPG94ChV381 oSHlIdAwqyQsblF+U6+1CLE8QZ5mg38ZkejRqn6EvqDHA3MKbqI6E2mGonOah09sZDCj 6Tmg== 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=VgPOAhYHrQm9ZXuNxaZSaVwvwfbXTmPYWgBZOm/D+wY=; fh=iOdKEcvcyKr8F0Ow9/BX/FP1N9atFrJVI2WJ0/3sDz4=; b=hU2/r957pHYdCjRbKAiVDnAIzaAPxehoY8zI2g6R6b3uauAiqTtPJLkR4nWm19YgRx 6Oc0CgteZnmlGCla1h+d/iThr2QtZNkVvJvcxyd4slJEWoSJvmK+krTTtFcvJICm93gK NwgBP/RT5elDb25UEKnvn5jaEZJf9askL7Ql3HjDfKv1bjquVs/zz9gx4vm1lJfN58fa x/z1s16em6JWMD/Syh9TeygCU8yZd3znOCl11volBC2KlgB5TtWRPAPS5SlliNt4Dx5c iGGO0llpqXGkVkC6ogNBf+9yr66ylp6mssztJTc0pZ64ExYxnwSTGV6OkE50YWfJl/J0 2HKg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=Yif0bXjD; 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 sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id hb13-20020a170906b88d00b00991db3a72ddsi3737523ejb.628.2023.06.28.16.16.24 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Jun 2023 16:16: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=Yif0bXjD; 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 987743858C54 for ; Wed, 28 Jun 2023 23:16:23 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 987743858C54 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1687994183; bh=VgPOAhYHrQm9ZXuNxaZSaVwvwfbXTmPYWgBZOm/D+wY=; h=Date:Subject:To:Cc:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=Yif0bXjDp4AvnEUZoIgjmwxAhqis9odP3KqVE7CeMLbKlxKNuH2RPFJNG3tyEoc25 XIXTKRdvpCwGR8QB1R0kbjBQUkJjygoiYDZZQjgE3ztRkYodwCZFCfla5c+BrxpJrr jz1pL+ayQMbmPAtgDyLw1ZPQf64LsVVdjLEVNVTs= X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by sourceware.org (Postfix) with ESMTPS id 866503858D35 for ; Wed, 28 Jun 2023 23:16:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 866503858D35 Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-56ff7b4feefso642407b3.0 for ; Wed, 28 Jun 2023 16:16:14 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687994174; x=1690586174; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=VgPOAhYHrQm9ZXuNxaZSaVwvwfbXTmPYWgBZOm/D+wY=; b=QR2vrMu9HUFgrs8+8g52eHMgTdlofpa3XLy9sSkE8oBjcJGe8WZnv70FM9JE3BpaBn /vDrgrge6/g+WXy87BLFJVnpB1W9jRdDKz26wLm3NtNqOKg/CB5BGRIY8ZZQZGvTgQcP FpThY6feYCjqnEbMvnHkLcRbRQggAOIw1mLgIBGnho0eeKKpI+03r2n7+4PYFMZ3QUvA 2hCdyZ4hnwWaXSrS3dCGZF3b0jy4mehvnTCEbzamDjftcUs7v/WGxfXWhy07azQzhuqZ x7mU9a5dixEjoQbbG9gLL1UgO95aJS+6SpRYL1vp641O+Vwuoz4neRVXdJLB3QQ/jLu8 1h5w== X-Gm-Message-State: AC+VfDzh/xofngxE3tHuDVkcEPazA/ZYcLr1JmaFbSCLKqJ2mccZk0XX x1Nnf+k9MlN7oRdJrfnGhf2HlqVDp9zltPh7R72c5NRdTQ7AXd8Oq7krDlIcS5sPVbhbGwVr0/K uYBaaGgC/9TsiDkvZ+to9uDd/wEnlI6Ertyjbxlna7x/463J4lO37Pm2NSHB2/4SIVg== X-Received: from maskray.svl.corp.google.com ([2620:15c:2d3:205:48e2:9be7:271c:ecba]) (user=maskray job=sendgmr) by 2002:a81:e24a:0:b0:56d:2abf:f0c with SMTP id z10-20020a81e24a000000b0056d2abf0f0cmr14679511ywl.10.1687994173773; Wed, 28 Jun 2023 16:16:13 -0700 (PDT) Date: Wed, 28 Jun 2023 16:16:10 -0700 Mime-Version: 1.0 Message-ID: <20230628231610.220112-1-maskray@google.com> Subject: [PATCH v2] PR30592 objcopy: allow --set-section-flags to add or remove SHF_X86_64_LARGE To: binutils@sourceware.org Cc: Fangrui Song X-Spam-Status: No, score=-19.6 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?1769990190442041407?= X-GMAIL-MSGID: =?utf-8?q?1769990190442041407?= 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. bfd/ * bfd-in2.h: Define SEC_ELF_LARGE. * 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. 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 --- bfd/bfd-in2.h | 3 +++ bfd/elf.c | 10 ++++++++++ binutils/NEWS | 3 +++ binutils/doc/binutils.texi | 15 ++++++++------- binutils/objcopy.c | 3 ++- .../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 ++ 10 files changed, 69 insertions(+), 8 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 c4fa0c1f9bf..17f3da907e0 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 8f6d7d1adba..112e8c8e5ca 100644 --- a/bfd/elf.c +++ b/bfd/elf.c @@ -1034,6 +1034,10 @@ _bfd_elf_make_section_from_shdr (bfd *abfd, if ((hdr->sh_flags & SHF_EXCLUDE) != 0) flags |= SEC_EXCLUDE; + if (get_elf_backend_data (abfd)->elf_machine_code == EM_X86_64) + if ((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, @@ -3351,6 +3355,9 @@ 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 (asect->flags & SEC_ELF_LARGE) + if (get_elf_backend_data (abfd)->elf_machine_code == EM_X86_64) + 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 @@ -7940,6 +7947,9 @@ _bfd_elf_init_private_section_data (bfd *ibfd, elf_section_flags (osec) = (elf_section_flags (isec) & (SHF_MASKOS | SHF_MASKPROC)); + 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/binutils/NEWS b/binutils/NEWS index 00ef7c43610..1b61cce92e3 100644 --- a/binutils/NEWS +++ b/binutils/NEWS @@ -13,6 +13,9 @@ updates the ABI in an incompatible way: this includes removal of sframe_get_funcdesc_with_addr API, change in the behavior of sframe_fre_get_ra_offset and sframe_fre_get_fp_offset APIs. + +* objcopy's --set-section-flags now support "large" to set SHF_X86_64_LARGE + for ELF x86-64 objects. Changes in 2.40: diff --git a/binutils/doc/binutils.texi b/binutils/doc/binutils.texi index 8c14d1121d9..a497f9087a3 100644 --- a/binutils/doc/binutils.texi +++ b/binutils/doc/binutils.texi @@ -1740,13 +1740,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 414007780a8..40496f8f792 100644 --- a/binutils/objcopy.c +++ b/binutils/objcopy.c @@ -797,6 +797,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 { @@ -807,7 +808,7 @@ 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, 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 */