From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1039) id 3A3C23858D35; Thu, 29 Jun 2023 17:36:13 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3A3C23858D35 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: H.J. Lu To: bfd-cvs@sourceware.org Subject: [binutils-gdb] ELF: Strip section header in ELF objects X-Act-Checkin: binutils-gdb X-Git-Author: Kaylee Blake X-Git-Refname: refs/heads/master X-Git-Oldrev: fa2b501cdcbb7e9e8437f1dfb4b1e939dcb07264 X-Git-Newrev: 96cc7918c18cdb7bd0331d836fa7f05341732580 Message-Id: <20230629173613.3A3C23858D35@sourceware.org> Date: Thu, 29 Jun 2023 17:36:13 +0000 (GMT) X-BeenThere: binutils-cvs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Binutils-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 29 Jun 2023 17:36:13 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D96cc7918c18c= db7bd0331d836fa7f05341732580 commit 96cc7918c18cdb7bd0331d836fa7f05341732580 Author: Kaylee Blake Date: Wed Mar 4 19:18:07 2020 +1030 ELF: Strip section header in ELF objects =20 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, a new objcopy and strip option, --strip-section-headers, to remove ELF section headers. =20 bfd/ =20 2023-06-06 H.J. Lu Kaylee Blake =20 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. =20 binutils/ =20 2023-06-06 H.J. Lu =20 PR ld/25617 * NEWS: Mention --strip-section-headers for objcopy and strip. * objcopy.c (strip_section_headers): New. (command_line_switch): Add OPTION_STRIP_SECTION_HEADERS. (strip_options): Add --strip-section-headers. (copy_options): Likewise. (copy_usage): Add --strip-section-headers. (strip_usage): Likewise. (copy_object): Handle --strip-section-headers for ELF files. (strip_main): Handle OPTION_STRIP_SECTION_HEADERS. (copy_main): Likewise. * doc/binutils.texi: Document --strip-section-headers for objco= py and strip. =20 ld/ =20 2023-06-06 H.J. Lu Kaylee Blake =20 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. Disallow -r with -z nosectionheader. (elf_static_list_options): Add -z sectionheader and -z nosectionheader. Diff: --- bfd/bfd-in2.h | 6 +++++- bfd/bfd.c | 6 +++++- bfd/elfcode.h | 36 ++++++++++++++++++++++++++---------- bfd/elfxx-target.h | 4 ++-- binutils/NEWS | 3 +++ 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 | 13 +++++++++++++ 13 files changed, 114 insertions(+), 16 deletions(-) diff --git a/bfd/bfd-in2.h b/bfd/bfd-in2.h index c4fa0c1f9bf..b34c8ef9fc9 100644 --- a/bfd/bfd-in2.h +++ b/bfd/bfd-in2.h @@ -2049,11 +2049,15 @@ struct bfd /* Compress sections in this BFD with SHF_COMPRESSED zstd. */ #define BFD_COMPRESS_ZSTD 0x400000 =20 + /* 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) =20 /* 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 804acabf621..e43a388ac72 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..495e498838d 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 =3D get_elf_backend_data (abfd)->sign_extend_vma; + bool no_section_header =3D (abfd->flags & BFD_NO_SECTION_HEADER) !=3D 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 =3D PN_XNUM; H_PUT_16 (abfd, tmp, dst->e_phnum); - H_PUT_16 (abfd, src->e_shentsize, dst->e_shentsize); - tmp =3D src->e_shnum; - if (tmp >=3D (SHN_LORESERVE & 0xffff)) - tmp =3D SHN_UNDEF; - H_PUT_16 (abfd, tmp, dst->e_shnum); - tmp =3D src->e_shstrndx; - if (tmp >=3D (SHN_LORESERVE & 0xffff)) - tmp =3D 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 =3D src->e_shnum; + if (tmp >=3D (SHN_LORESERVE & 0xffff)) + tmp =3D SHN_UNDEF; + H_PUT_16 (abfd, tmp, dst->e_shnum); + tmp =3D src->e_shstrndx; + if (tmp >=3D (SHN_LORESERVE & 0xffff)) + tmp =3D SHN_XINDEX & 0xffff; + H_PUT_16 (abfd, tmp, dst->e_shstrndx); + } } =20 /* 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) !=3D amt) return false; =20 + if ((abfd->flags & BFD_NO_SECTION_HEADER) !=3D 0) + return true; + /* Some fields in the first section header handle overflow of ehdr fields. */ if (i_ehdrp->e_phnum >=3D PN_XNUM) diff --git a/bfd/elfxx-target.h b/bfd/elfxx-target.h index 8eabad4bd3f..f8553006385 100644 --- a/bfd/elfxx-target.h +++ b/bfd/elfxx-target.h @@ -994,7 +994,7 @@ const bfd_target TARGET_BIG_SYM =3D (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), =20 /* 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 =3D (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), =20 /* 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 00ef7c43610..3aea8755339 100644 --- a/binutils/NEWS +++ b/binutils/NEWS @@ -14,6 +14,9 @@ sframe_get_funcdesc_with_addr API, change in the behavior of sframe_fre_get_ra_offset and sframe_fre_get_fp_offset APIs. =20 +* Add command-line option, --strip-section-headers, to objcopy and strip to + remove ELF section header from ELF file. + Changes in 2.40: =20 * 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..8314cb57562 100644 --- a/binutils/doc/binutils.texi +++ b/binutils/doc/binutils.texi @@ -1278,6 +1278,7 @@ objcopy [@option{-F} @var{bfdname}|@option{--target= =3D}@var{bfdname}] [@option{-R} @var{sectionpattern}|@option{--remove-section=3D}@var= {sectionpattern}] [@option{--keep-section=3D}@var{sectionpattern}] [@option{--remove-relocations=3D}@var{sectionpattern}] + [@option{--strip-section-headers}] [@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 th= e pattern '.text.*', but will not remove relocations for the section '.text.foo'. =20 +@item --strip-section-headers +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=3D= }@var{bfdname}] [@option{-R} @var{sectionname} |@option{--remove-section=3D}@var{sec= tionname}] [@option{--keep-section=3D}@var{sectionpattern}] [@option{--remove-relocations=3D}@var{sectionpattern}] + [@option{--strip-section-headers}] [@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 th= e pattern '.text.*', but will not remove relocations for the section '.text.foo'. =20 +@item --strip-section-headers +Strip section headers. 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 414007780a8..206d80bd0f1 100644 --- a/binutils/objcopy.c +++ b/binutils/objcopy.c @@ -97,6 +97,7 @@ static int deterministic =3D -1; /* Enable deterministic= archives. */ static int status =3D 0; /* Exit status. */ =20 static bool merge_notes =3D false; /* Merge note sections. */ +static bool strip_section_headers =3D false;/* Strip section headers. */ =20 typedef struct merged_note_section { @@ -365,6 +366,7 @@ enum command_line_switch OPTION_SREC_LEN, OPTION_STACK, OPTION_STRIP_DWO, + OPTION_STRIP_SECTION_HEADERS, OPTION_STRIP_SYMBOLS, OPTION_STRIP_UNNEEDED, OPTION_STRIP_UNNEEDED_SYMBOL, @@ -403,6 +405,7 @@ static struct option strip_options[] =3D {"preserve-dates", no_argument, 0, 'p'}, {"remove-section", required_argument, 0, 'R'}, {"remove-relocations", required_argument, 0, OPTION_REMOVE_RELOCS}, + {"strip-section-headers", no_argument, 0, OPTION_STRIP_SECTION_HEADERS}, {"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[] =3D {"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-section-headers", no_argument, 0, OPTION_STRIP_SECTION_HEADERS}, {"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=3D Add section .gnu_debuglink linking to= \n\ -R --remove-section Remove section from the output\n\ --remove-relocations Remove relocations from section \= n\ + --strip-section-headers Strip section header from the ou= tput\n\ -S --strip-all Remove all symbol and relocation inform= ation\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=3D Also remove section from the o= utput\n\ --remove-relocations Remove relocations from section \= n\ + --strip-section-headers Strip section headers from the output\n\ -s --strip-all Remove all symbol and relocation inform= ation\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_in= fo_type *input_arch) necessary. */ VerilogDataEndianness =3D ibfd->xvec->byteorder; =20 - if (bfd_get_flavour (ibfd) !=3D bfd_target_elf_flavour) + if (bfd_get_flavour (ibfd) =3D=3D bfd_target_elf_flavour) + { + if (strip_section_headers) + { + ibfd->flags |=3D BFD_NO_SECTION_HEADER; + strip_symbols =3D STRIP_ALL; + merge_notes =3D true; + } + } + else { if ((do_debug_sections & compress) !=3D 0 && do_debug_sections !=3D compress) @@ -2712,6 +2727,13 @@ copy_object (bfd *ibfd, bfd *obfd, const bfd_arch_in= fo_type *input_arch) bfd_get_archive_filename (ibfd)); return false; } + + if (strip_section_headers) + { + non_fatal (_("--strip_section_headers is unsupported on `%s'"), + bfd_get_archive_filename (ibfd)); + return false; + } } =20 if (verbose) @@ -3456,7 +3478,7 @@ copy_object (bfd *ibfd, bfd *obfd, const bfd_arch_inf= o_type *input_arch) free (merged); } } - else if (merge_notes && ! is_strip) + else if (merge_notes && ! is_strip && ! strip_section_headers) non_fatal (_("%s: Could not find any mergeable note sections"), bfd_get_filename (ibfd)); =20 @@ -4759,6 +4781,9 @@ strip_main (int argc, char *argv[]) case OPTION_REMOVE_RELOCS: handle_remove_relocations_option (optarg); break; + case OPTION_STRIP_SECTION_HEADERS: + strip_section_headers =3D true; + break; case 's': strip_symbols =3D STRIP_ALL; break; @@ -5228,6 +5253,10 @@ copy_main (int argc, char *argv[]) handle_remove_relocations_option (optarg); break; =20 + case OPTION_STRIP_SECTION_HEADERS: + strip_section_headers =3D true; + break; + case 'S': strip_symbols =3D STRIP_ALL; break; diff --git a/ld/NEWS b/ld/NEWS index 4dee2301158..dbeea1267a1 100644 --- a/ld/NEWS +++ b/ld/NEWS @@ -15,6 +15,9 @@ * The linker script syntax has a new command for output sections: ASCIZ "s= tring" This will insert a zero-terminated string at the current location. =20 +* Add command-line option, -z nosectionheader, to omit ELF section + header. + Changes in 2.40: =20 * The linker has a new command line option to suppress the generation of a= ny 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 |=3D BFD_TRADITIONAL_FORMAT; else link_info.output_bfd->flags &=3D ~BFD_TRADITIONAL_FORMAT; + if (config.no_section_header) + link_info.output_bfd->flags |=3D BFD_NO_SECTION_HEADER; + else + link_info.output_bfd->flags &=3D ~BFD_NO_SECTION_HEADER; break; =20 case lang_target_statement_enum: diff --git a/ld/lexsup.c b/ld/lexsup.c index 4e20e938a49..fe8722313fe 100644 --- a/ld/lexsup.c +++ b/ld/lexsup.c @@ -1998,6 +1998,15 @@ parse_args (unsigned argc, char **argv) break; } =20 + /* -z nosectionheader implies --strip-all. */ + if (config.no_section_header) + { + if (bfd_link_relocatable (&link_info)) + einfo (_("%F%P: -r and -z nosectionheader may not be used together\n")); + + link_info.strip =3D strip_all; + } + if (!bfd_link_dll (&link_info)) { if (command_line.filter_shlib) @@ -2272,6 +2281,10 @@ elf_static_list_options (FILE *file) fprintf (file, _("\ -z start-stop-visibility=3DV 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")); } =20 static void