From patchwork Tue May 30 17:12:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "H.J. Lu" X-Patchwork-Id: 101020 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp2334694vqr; Tue, 30 May 2023 10:13:33 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5p8Nz/VXxofAc7Von0GsfH/ErN7bGML8dpOWAPvzwkt6eL9zyqe0bdDXSpCbX8Igf9VFac X-Received: by 2002:aa7:ca4b:0:b0:50c:358:1eba with SMTP id j11-20020aa7ca4b000000b0050c03581ebamr1887009edt.35.1685466813093; Tue, 30 May 2023 10:13:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685466813; cv=none; d=google.com; s=arc-20160816; b=bI9tzCk5HvzC3sdxfw8W/mgvGPefq8JB94jLYopF5eUloLMJk6YJaOaCMDJ2CAMymV gn+eJrSynyIkXst3ImUaCCBohrkxGi2a6E1BhoaQX7PgeP+QMGQ/BrHd0oIq8umxSK0I CJWiiQKF7qr3Zqg30T/zPle0QlFT+/CggFISS2ONt5VKSepVCIRtlHi2Bg7gM5CVNgz+ 66iVdjopdKDtUNWT/cE+jPWn4wD27BaQL7Wn9FMkwxpgum5X/nV27xBKcixhQJqInRtN nVzlqtirmF5/1/oQPbugBgBdfhWKFL7WWi1h35aeIVX2E83AZ7rpszMMCgDUKz3FNfSK WuBg== 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 :content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:dmarc-filter:delivered-to :dkim-signature:dkim-filter; bh=UPPZsmIfo4uNh7rx2C3B268rJb215AXBTeAuFkp6//k=; b=oxAVxWwoOi4LT06BvNvVYl2e9jrk7J4ciZcoci50nnrOgYCin0EnSizGMpEQM9Z6R5 epS2SgPxDyeDot6xgNgw/cuDZexLcEkSbLKS5O+/TMw6wwj3vE64r/qZRXD4aJrsDHth dY354vXzWsKZ4jD7B0h/6WrROv9JP8S8dtTJkyMRPWNSJuyZay6SQ2pYRDn4iir1c/Zg Ih4DK2IV5W1axXNIKYqSD2q5rtF0b4FxJDUrB2FQbU7Yv9Ut/a9O0cLt/JtpluebfmiW oNiQ12+rvAoJp2vo8MBXnXaJaRN3rvnyC6YHLIcdcueaTVobNNyn/1rqmKeJj9KzJvFb LvSw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=n504rqqy; 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 p15-20020aa7d30f000000b005048bfe57f1si295553edq.28.2023.05.30.10.13.32 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 May 2023 10:13:33 -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=n504rqqy; 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 8AD663856968 for ; Tue, 30 May 2023 17:13:25 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8AD663856968 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1685466805; bh=UPPZsmIfo4uNh7rx2C3B268rJb215AXBTeAuFkp6//k=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=n504rqqyCYHsZmC2ZTYc58h7MYYHdpu+XG0HVtDOOVT8wUT+YPjMhi5o7q3wmlidO bFJCdOTnUVL9D3gKGRh2e2wF9ibZE+625RNmLqLY12PRefEBj8b5pC2i92oj89rxJI 4jtYBjM+mIRhRn4ODP2HxsXt+8f3Xm4h2YHOu6xQ= X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail-pl1-x62b.google.com (mail-pl1-x62b.google.com [IPv6:2607:f8b0:4864:20::62b]) by sourceware.org (Postfix) with ESMTPS id 7C5C33857355 for ; Tue, 30 May 2023 17:13:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7C5C33857355 Received: by mail-pl1-x62b.google.com with SMTP id d9443c01a7336-1b011cffe7fso31048885ad.1 for ; Tue, 30 May 2023 10:13:14 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685466792; x=1688058792; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=UPPZsmIfo4uNh7rx2C3B268rJb215AXBTeAuFkp6//k=; b=PUFlUmHoYJF20nMNXuE7S5IUpQAM1PpwaQEm2NTpublcU/daNp6SA805WRHZXlxc5m qeBQ1xAtNZlj49MinKkz9XZ4Wf6xMImyh1X22l/viE+mYkwDUvU7cnkucCqS1rZmpnwD DrH94gPKz05+mCJO6olfEbSKnjwJAj2ygHMEX1Erl74ilpUjhZ6iwGLwn6edFVSLkv3d Dku/NEhFUeqJADnjyuVVd2z8HTgV9OC6nIp/V+DmdgQ6p55pC7sjutxzTDkEHZ+h5zkC XlUXTqCZ1BX9F6kWI2CzTifv1CFjuRuO1R69IYD1Zf0M/tGeyLvvkhDB53/UOZaDL6HQ rroA== X-Gm-Message-State: AC+VfDwaqXMKlEXMYn9gn9Ry28SHZ+D4JOUVfXsgaruKdzeiCArUSOkk YM0DMMkgaqPOuJnYAcMXZK/uZv/xM8M= X-Received: by 2002:a17:903:1c6:b0:1b0:2743:10f4 with SMTP id e6-20020a17090301c600b001b0274310f4mr2694507plh.6.1685466792217; Tue, 30 May 2023 10:13:12 -0700 (PDT) Received: from gnu-cfl-3.localdomain ([172.59.160.123]) by smtp.gmail.com with ESMTPSA id m10-20020a1709026bca00b001add2ba4459sm10448218plt.32.2023.05.30.10.13.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 May 2023 10:13:11 -0700 (PDT) Received: from gnu-cfl-3.. (localhost [IPv6:::1]) by gnu-cfl-3.localdomain (Postfix) with ESMTP id 76D527401DE; Tue, 30 May 2023 10:13:10 -0700 (PDT) To: binutils@sourceware.org Cc: Kaylee Blake Subject: [PATCH 1/6] ELF: Strip section header in ELF objects Date: Tue, 30 May 2023 10:12:47 -0700 Message-Id: <20230530171252.269552-2-hjl.tools@gmail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230530171252.269552-1-hjl.tools@gmail.com> References: <20230530171252.269552-1-hjl.tools@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-3024.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE 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: "H.J. Lu via Binutils" From: "H.J. Lu" Reply-To: "H.J. Lu" 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?1767339971566522018?= X-GMAIL-MSGID: =?utf-8?q?1767340048845989772?= From: Kaylee Blake Section header isn't mandatory on ELF executable nor shared library. This patch adds a new linker option, -z nosectionheader, to omit ELF section header, an new objcopy and strip option, --strip-sections, to remove ELF section header. bfd/ 2020-03-XX H.J. Lu Kaylee Blake PR ld/25617 * bfd.c (BFD_NO_SECTION_HEADER): New. (BFD_FLAGS_SAVED): Add BFD_NO_SECTION_HEADER. (BFD_FLAGS_FOR_BFD_USE_MASK): Likewise. * elfcode.h (elf_swap_ehdr_out): Omit section header with BFD_NO_SECTION_HEADER. (elf_write_shdrs_and_ehdr): Likewise. * elfxx-target.h (TARGET_BIG_SYM): Add BFD_NO_SECTION_HEADER to object_flags. (TARGET_LITTLE_SYM): Likewise. * bfd-in2.h: Regenerated. binutils/ 2020-03-XX H.J. Lu PR ld/25617 * NEWS: Mention --strip-sections for objcopy and strip. * doc/binutils.texi: Document --strip-sections for objcopy and strip. * objcopy.c (remove_section_header): New. (command_line_switch): Add OPTION_REMOVE_SECTION_HEADER. (strip_options): Add --strip-sections. (copy_options): Likewise. (copy_usage): Add --strip-sections. (strip_usage): Likewise. (copy_object): Handle --strip-sections for ELF files. (strip_main): Handle OPTION_REMOVE_SECTION_HEADER. (copy_main): Likewise. ld/ 2020-03-XX H.J. Lu Kaylee Blake PR ld/25617 * NEWS: Mention -z nosectionheader. * emultempl/elf.em: Support -z sectionheader and -z nosectionheader. * ld.h (ld_config_type): Add no_section_header. * ld.texi: Document -z sectionheader and -z nosectionheader. * ldlang.c (ldlang_open_output): Handle config.no_section_header. * lexsup.c (parse_args): Enable --strip-all with -z nosectionheader. (elf_static_list_options): Add -z sectionheader and -z nosectionheader. --- bfd/bfd-in2.h | 6 +++++- bfd/bfd.c | 6 +++++- bfd/elfcode.h | 36 ++++++++++++++++++++++++++---------- bfd/elfxx-target.h | 4 ++-- binutils/NEWS | 5 ++++- binutils/doc/binutils.texi | 10 ++++++++++ binutils/objcopy.c | 33 +++++++++++++++++++++++++++++++-- ld/NEWS | 3 +++ ld/emultempl/elf.em | 4 ++++ ld/ld.h | 3 +++ ld/ld.texi | 5 +++++ ld/ldlang.c | 4 ++++ ld/lexsup.c | 8 ++++++++ 13 files changed, 110 insertions(+), 17 deletions(-) diff --git a/bfd/bfd-in2.h b/bfd/bfd-in2.h index 6f8a8fd114d..a84a3c24f2a 100644 --- a/bfd/bfd-in2.h +++ b/bfd/bfd-in2.h @@ -2048,11 +2048,15 @@ struct bfd /* Compress sections in this BFD with SHF_COMPRESSED zstd. */ #define BFD_COMPRESS_ZSTD 0x400000 + /* Don't generate ELF section header. */ +#define BFD_NO_SECTION_HEADER 0x800000 + /* Flags bits which are for BFD use only. */ #define BFD_FLAGS_FOR_BFD_USE_MASK \ (BFD_IN_MEMORY | BFD_COMPRESS | BFD_DECOMPRESS | BFD_LINKER_CREATED \ | BFD_PLUGIN | BFD_TRADITIONAL_FORMAT | BFD_DETERMINISTIC_OUTPUT \ - | BFD_COMPRESS_GABI | BFD_CONVERT_ELF_COMMON | BFD_USE_ELF_STT_COMMON) + | BFD_COMPRESS_GABI | BFD_CONVERT_ELF_COMMON | BFD_USE_ELF_STT_COMMON \ + | BFD_NO_SECTION_HEADER) /* The format which belongs to the BFD. (object, core, etc.) */ ENUM_BITFIELD (bfd_format) format : 3; diff --git a/bfd/bfd.c b/bfd/bfd.c index 71debd73733..014b7a90a4d 100644 --- a/bfd/bfd.c +++ b/bfd/bfd.c @@ -192,11 +192,15 @@ CODE_FRAGMENT . {* Compress sections in this BFD with SHF_COMPRESSED zstd. *} .#define BFD_COMPRESS_ZSTD 0x400000 . +. {* Don't generate ELF section header. *} +.#define BFD_NO_SECTION_HEADER 0x800000 +. . {* Flags bits which are for BFD use only. *} .#define BFD_FLAGS_FOR_BFD_USE_MASK \ . (BFD_IN_MEMORY | BFD_COMPRESS | BFD_DECOMPRESS | BFD_LINKER_CREATED \ . | BFD_PLUGIN | BFD_TRADITIONAL_FORMAT | BFD_DETERMINISTIC_OUTPUT \ -. | BFD_COMPRESS_GABI | BFD_CONVERT_ELF_COMMON | BFD_USE_ELF_STT_COMMON) +. | BFD_COMPRESS_GABI | BFD_CONVERT_ELF_COMMON | BFD_USE_ELF_STT_COMMON \ +. | BFD_NO_SECTION_HEADER) . . {* The format which belongs to the BFD. (object, core, etc.) *} . ENUM_BITFIELD (bfd_format) format : 3; diff --git a/bfd/elfcode.h b/bfd/elfcode.h index e4e29f0dc57..790d1dd3b04 100644 --- a/bfd/elfcode.h +++ b/bfd/elfcode.h @@ -266,6 +266,7 @@ elf_swap_ehdr_out (bfd *abfd, { unsigned int tmp; int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma; + bool no_section_header = (abfd->flags & BFD_NO_SECTION_HEADER) != 0; memcpy (dst->e_ident, src->e_ident, EI_NIDENT); /* note that all elements of dst are *arrays of unsigned char* already... */ H_PUT_16 (abfd, src->e_type, dst->e_type); @@ -276,7 +277,10 @@ elf_swap_ehdr_out (bfd *abfd, else H_PUT_WORD (abfd, src->e_entry, dst->e_entry); H_PUT_WORD (abfd, src->e_phoff, dst->e_phoff); - H_PUT_WORD (abfd, src->e_shoff, dst->e_shoff); + if (no_section_header) + H_PUT_WORD (abfd, 0, dst->e_shoff); + else + H_PUT_WORD (abfd, src->e_shoff, dst->e_shoff); H_PUT_32 (abfd, src->e_flags, dst->e_flags); H_PUT_16 (abfd, src->e_ehsize, dst->e_ehsize); H_PUT_16 (abfd, src->e_phentsize, dst->e_phentsize); @@ -284,15 +288,24 @@ elf_swap_ehdr_out (bfd *abfd, if (tmp > PN_XNUM) tmp = PN_XNUM; H_PUT_16 (abfd, tmp, dst->e_phnum); - H_PUT_16 (abfd, src->e_shentsize, dst->e_shentsize); - tmp = src->e_shnum; - if (tmp >= (SHN_LORESERVE & 0xffff)) - tmp = SHN_UNDEF; - H_PUT_16 (abfd, tmp, dst->e_shnum); - tmp = src->e_shstrndx; - if (tmp >= (SHN_LORESERVE & 0xffff)) - tmp = SHN_XINDEX & 0xffff; - H_PUT_16 (abfd, tmp, dst->e_shstrndx); + if (no_section_header) + { + H_PUT_16 (abfd, 0, dst->e_shentsize); + H_PUT_16 (abfd, 0, dst->e_shnum); + H_PUT_16 (abfd, 0, dst->e_shstrndx); + } + else + { + H_PUT_16 (abfd, src->e_shentsize, dst->e_shentsize); + tmp = src->e_shnum; + if (tmp >= (SHN_LORESERVE & 0xffff)) + tmp = SHN_UNDEF; + H_PUT_16 (abfd, tmp, dst->e_shnum); + tmp = src->e_shstrndx; + if (tmp >= (SHN_LORESERVE & 0xffff)) + tmp = SHN_XINDEX & 0xffff; + H_PUT_16 (abfd, tmp, dst->e_shstrndx); + } } /* Translate an ELF section header table entry in external format into an @@ -1077,6 +1090,9 @@ elf_write_shdrs_and_ehdr (bfd *abfd) || bfd_bwrite (&x_ehdr, amt, abfd) != amt) return false; + if ((abfd->flags & BFD_NO_SECTION_HEADER) != 0) + return true; + /* Some fields in the first section header handle overflow of ehdr fields. */ if (i_ehdrp->e_phnum >= PN_XNUM) diff --git a/bfd/elfxx-target.h b/bfd/elfxx-target.h index 9bcbdfb27dd..20191fae294 100644 --- a/bfd/elfxx-target.h +++ b/bfd/elfxx-target.h @@ -994,7 +994,7 @@ const bfd_target TARGET_BIG_SYM = (HAS_RELOC | EXEC_P | HAS_LINENO | HAS_DEBUG | HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT | D_PAGED | BFD_COMPRESS | BFD_DECOMPRESS | BFD_COMPRESS_GABI | BFD_COMPRESS_ZSTD | BFD_CONVERT_ELF_COMMON - | BFD_USE_ELF_STT_COMMON), + | BFD_USE_ELF_STT_COMMON | BFD_NO_SECTION_HEADER), /* section_flags: mask of all section flags */ (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_READONLY @@ -1099,7 +1099,7 @@ const bfd_target TARGET_LITTLE_SYM = (HAS_RELOC | EXEC_P | HAS_LINENO | HAS_DEBUG | HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT | D_PAGED | BFD_COMPRESS | BFD_DECOMPRESS | BFD_COMPRESS_GABI | BFD_COMPRESS_ZSTD | BFD_CONVERT_ELF_COMMON - | BFD_USE_ELF_STT_COMMON), + | BFD_USE_ELF_STT_COMMON | BFD_NO_SECTION_HEADER), /* section_flags: mask of all section flags */ (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_READONLY diff --git a/binutils/NEWS b/binutils/NEWS index b29f36bf1d4..515a768722a 100644 --- a/binutils/NEWS +++ b/binutils/NEWS @@ -2,7 +2,10 @@ * Objdump's --private option can now be used on PE format files to display the fields in the file header and section headers. - + +* Add command-line option, --strip-sections, to objcopy and strip to + remove ELF section header from ELF file. + Changes in 2.40: * Objdump has a new command line option --show-all-symbols which will make it diff --git a/binutils/doc/binutils.texi b/binutils/doc/binutils.texi index 8c14d1121d9..793902a848e 100644 --- a/binutils/doc/binutils.texi +++ b/binutils/doc/binutils.texi @@ -1278,6 +1278,7 @@ objcopy [@option{-F} @var{bfdname}|@option{--target=}@var{bfdname}] [@option{-R} @var{sectionpattern}|@option{--remove-section=}@var{sectionpattern}] [@option{--keep-section=}@var{sectionpattern}] [@option{--remove-relocations=}@var{sectionpattern}] + [@option{--strip-sections}] [@option{-p}|@option{--preserve-dates}] [@option{-D}|@option{--enable-deterministic-archives}] [@option{-U}|@option{--disable-deterministic-archives}] @@ -1494,6 +1495,10 @@ will remove all relocations for sections matching the pattern '.text.*', but will not remove relocations for the section '.text.foo'. +@item --strip-sections +Strip section header This option is specific to ELF files. +Implies @option{--strip-all} and @option{--merge-notes}. + @item -S @itemx --strip-all Do not copy relocation and symbol information from the source file. @@ -3484,6 +3489,7 @@ strip [@option{-F} @var{bfdname} |@option{--target=}@var{bfdname}] [@option{-R} @var{sectionname} |@option{--remove-section=}@var{sectionname}] [@option{--keep-section=}@var{sectionpattern}] [@option{--remove-relocations=}@var{sectionpattern}] + [@option{--strip-sections}] [@option{-o} @var{file}] [@option{-p}|@option{--preserve-dates}] [@option{-D}|@option{--enable-deterministic-archives}] [@option{-U}|@option{--disable-deterministic-archives}] @@ -3586,6 +3592,10 @@ will remove all relocations for sections matching the pattern '.text.*', but will not remove relocations for the section '.text.foo'. +@item --strip-sections +Strip section header. This option is specific to ELF files. Implies +@option{--strip-all} and @option{--merge-notes}. + @item -s @itemx --strip-all Remove all symbols. diff --git a/binutils/objcopy.c b/binutils/objcopy.c index 49d54bff9b2..28d94d470d1 100644 --- a/binutils/objcopy.c +++ b/binutils/objcopy.c @@ -97,6 +97,7 @@ static int deterministic = -1; /* Enable deterministic archives. */ static int status = 0; /* Exit status. */ static bool merge_notes = false; /* Merge note sections. */ +static bool strip_sections = false; /* Strip section header. */ typedef struct merged_note_section { @@ -355,6 +356,7 @@ enum command_line_switch OPTION_REDEFINE_SYMS, OPTION_REMOVE_LEADING_CHAR, OPTION_REMOVE_RELOCS, + OPTION_STRIP_SECTIONS, OPTION_RENAME_SECTION, OPTION_REVERSE_BYTES, OPTION_PE_SECTION_ALIGNMENT, @@ -403,6 +405,7 @@ static struct option strip_options[] = {"preserve-dates", no_argument, 0, 'p'}, {"remove-section", required_argument, 0, 'R'}, {"remove-relocations", required_argument, 0, OPTION_REMOVE_RELOCS}, + {"strip-sections", no_argument, 0, OPTION_STRIP_SECTIONS}, {"strip-all", no_argument, 0, 's'}, {"strip-debug", no_argument, 0, 'S'}, {"strip-dwo", no_argument, 0, OPTION_STRIP_DWO}, @@ -492,6 +495,7 @@ static struct option copy_options[] = {"remove-leading-char", no_argument, 0, OPTION_REMOVE_LEADING_CHAR}, {"remove-section", required_argument, 0, 'R'}, {"remove-relocations", required_argument, 0, OPTION_REMOVE_RELOCS}, + {"strip-sections", no_argument, 0, OPTION_STRIP_SECTIONS}, {"rename-section", required_argument, 0, OPTION_RENAME_SECTION}, {"reverse-bytes", required_argument, 0, OPTION_REVERSE_BYTES}, {"section-alignment", required_argument, 0, OPTION_PE_SECTION_ALIGNMENT}, @@ -592,6 +596,7 @@ copy_usage (FILE *stream, int exit_status) --add-gnu-debuglink= Add section .gnu_debuglink linking to \n\ -R --remove-section Remove section from the output\n\ --remove-relocations Remove relocations from section \n\ + --strip-sections Strip section header from the output\n\ -S --strip-all Remove all symbol and relocation information\n\ -g --strip-debug Remove all debugging symbols & sections\n\ --strip-dwo Remove all DWO sections\n\ @@ -730,6 +735,7 @@ strip_usage (FILE *stream, int exit_status) fprintf (stream, _("\ -R --remove-section= Also remove section from the output\n\ --remove-relocations Remove relocations from section \n\ + --strip-sections Strip section header from the output\n\ -s --strip-all Remove all symbol and relocation information\n\ -g -S -d --strip-debug Remove all debugging symbols & sections\n\ --strip-dwo Remove all DWO sections\n\ @@ -2695,7 +2701,16 @@ copy_object (bfd *ibfd, bfd *obfd, const bfd_arch_info_type *input_arch) necessary. */ VerilogDataEndianness = ibfd->xvec->byteorder; - if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour) + if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour) + { + if (strip_sections) + { + ibfd->flags |= BFD_NO_SECTION_HEADER; + strip_symbols = STRIP_ALL; + merge_notes = true; + } + } + else { if ((do_debug_sections & compress) != 0 && do_debug_sections != compress) @@ -2712,6 +2727,13 @@ copy_object (bfd *ibfd, bfd *obfd, const bfd_arch_info_type *input_arch) bfd_get_archive_filename (ibfd)); return false; } + + if (strip_sections) + { + non_fatal (_("--strip_sections is unsupported on `%s'"), + bfd_get_archive_filename (ibfd)); + return false; + } } if (verbose) @@ -3456,7 +3478,7 @@ copy_object (bfd *ibfd, bfd *obfd, const bfd_arch_info_type *input_arch) free (merged); } } - else if (merge_notes && ! is_strip) + else if (merge_notes && ! is_strip && ! strip_sections) non_fatal (_("%s: Could not find any mergeable note sections"), bfd_get_filename (ibfd)); @@ -4752,6 +4774,9 @@ strip_main (int argc, char *argv[]) case OPTION_REMOVE_RELOCS: handle_remove_relocations_option (optarg); break; + case OPTION_STRIP_SECTIONS: + strip_sections = true; + break; case 's': strip_symbols = STRIP_ALL; break; @@ -5221,6 +5246,10 @@ copy_main (int argc, char *argv[]) handle_remove_relocations_option (optarg); break; + case OPTION_STRIP_SECTIONS: + strip_sections = true; + break; + case 'S': strip_symbols = STRIP_ALL; break; diff --git a/ld/NEWS b/ld/NEWS index 9920d0209b8..0a471107516 100644 --- a/ld/NEWS +++ b/ld/NEWS @@ -10,6 +10,9 @@ * The linker script syntax has a new command for output sections: ASCIZ "string" This will insert a zero-terminated string at the current location. +* Add command-line option, -z nosectionheader, to omit ELF section + header. + Changes in 2.40: * The linker has a new command line option to suppress the generation of any diff --git a/ld/emultempl/elf.em b/ld/emultempl/elf.em index e367299146f..3f67f8b09d9 100644 --- a/ld/emultempl/elf.em +++ b/ld/emultempl/elf.em @@ -807,6 +807,10 @@ fragment <flags |= BFD_TRADITIONAL_FORMAT; else link_info.output_bfd->flags &= ~BFD_TRADITIONAL_FORMAT; + if (config.no_section_header) + link_info.output_bfd->flags |= BFD_NO_SECTION_HEADER; + else + link_info.output_bfd->flags &= ~BFD_NO_SECTION_HEADER; break; case lang_target_statement_enum: diff --git a/ld/lexsup.c b/ld/lexsup.c index 6090921bda4..37479189154 100644 --- a/ld/lexsup.c +++ b/ld/lexsup.c @@ -1971,6 +1971,10 @@ parse_args (unsigned argc, char **argv) break; } + /* -z nosectionheader implies --strip-all. */ + if (config.no_section_header) + link_info.strip = strip_all; + if (!bfd_link_dll (&link_info)) { if (command_line.filter_shlib) @@ -2245,6 +2249,10 @@ elf_static_list_options (FILE *file) fprintf (file, _("\ -z start-stop-visibility=V Set visibility of built-in __start/__stop symbols\n\ to DEFAULT, PROTECTED, HIDDEN or INTERNAL\n")); + fprintf (file, _("\ + -z sectionheader Generate section header (default)\n")); + fprintf (file, _("\ + -z nosectionheader Do not generate section header\n")); } static void