aboutsummaryrefslogtreecommitdiff
path: root/ld
diff options
context:
space:
mode:
authorKaylee Blake <klkblake@gmail.com>2020-03-04 19:18:07 +1030
committerH.J. Lu <hjl.tools@gmail.com>2023-06-29 10:29:46 -0700
commit96cc7918c18cdb7bd0331d836fa7f05341732580 (patch)
tree906a911fb1b123f4ae238f8504671b1aeecbbe89 /ld
parentfa2b501cdcbb7e9e8437f1dfb4b1e939dcb07264 (diff)
downloadbinutils-96cc7918c18cdb7bd0331d836fa7f05341732580.zip
binutils-96cc7918c18cdb7bd0331d836fa7f05341732580.tar.gz
binutils-96cc7918c18cdb7bd0331d836fa7f05341732580.tar.bz2
ELF: Strip section header in ELF objects
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. bfd/ 2023-06-06 H.J. Lu <hongjiu.lu@intel.com> Kaylee Blake <klkblake@gmail.com> 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/ 2023-06-06 H.J. Lu <hongjiu.lu@intel.com> 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 objcopy and strip. ld/ 2023-06-06 H.J. Lu <hongjiu.lu@intel.com> Kaylee Blake <klkblake@gmail.com> 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.
Diffstat (limited to 'ld')
-rw-r--r--ld/NEWS3
-rw-r--r--ld/emultempl/elf.em4
-rw-r--r--ld/ld.h3
-rw-r--r--ld/ld.texi5
-rw-r--r--ld/ldlang.c4
-rw-r--r--ld/lexsup.c13
6 files changed, 32 insertions, 0 deletions
diff --git a/ld/NEWS b/ld/NEWS
index 4dee230..dbeea12 100644
--- a/ld/NEWS
+++ b/ld/NEWS
@@ -15,6 +15,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 e367299..3f67f8b 100644
--- a/ld/emultempl/elf.em
+++ b/ld/emultempl/elf.em
@@ -807,6 +807,10 @@ fragment <<EOF
"must be default, internal, hidden, or protected"),
optarg);
}
+ else if (strcmp (optarg, "sectionheader") == 0)
+ config.no_section_header = false;
+ else if (strcmp (optarg, "nosectionheader") == 0)
+ config.no_section_header = true;
EOF
if test x"$GENERATE_SHLIB_SCRIPT" = xyes; then
diff --git a/ld/ld.h b/ld/ld.h
index 1882a28..a0f8a15 100644
--- a/ld/ld.h
+++ b/ld/ld.h
@@ -280,6 +280,9 @@ typedef struct
/* If set, code and non-code sections should never be in one segment. */
bool separate_code;
+ /* If set, generation of ELF section header should be suppressed. */
+ bool no_section_header;
+
/* The rpath separation character. Usually ':'. */
char rpath_separator;
diff --git a/ld/ld.texi b/ld/ld.texi
index a007ae7..aa8b1aa 100644
--- a/ld/ld.texi
+++ b/ld/ld.texi
@@ -1544,6 +1544,11 @@ Don't create an ELF @code{PT_GNU_RELRO} segment if @samp{norelro}.
Report dynamic relative relocations generated by linker. Supported for
Linux/i386 and Linux/x86_64.
+@item sectionheader
+@itemx nosectionheader
+Generate section header. Don't generate section header if
+@samp{nosectionheader} is used. @option{sectionheader} is the default.
+
@item separate-code
@itemx noseparate-code
Create separate code @code{PT_LOAD} segment header in the object. This
diff --git a/ld/ldlang.c b/ld/ldlang.c
index e359a89..f4a8b72 100644
--- a/ld/ldlang.c
+++ b/ld/ldlang.c
@@ -3434,6 +3434,10 @@ ldlang_open_output (lang_statement_union_type *statement)
link_info.output_bfd->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 4e20e93..fe87223 100644
--- a/ld/lexsup.c
+++ b/ld/lexsup.c
@@ -1998,6 +1998,15 @@ parse_args (unsigned argc, char **argv)
break;
}
+ /* -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 = 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=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