[PATCH 1/7] ELF: Omit section header on ELF objects

Alan Modra amodra@gmail.com
Fri Mar 20 07:11:31 GMT 2020


On Mon, Mar 09, 2020 at 05:12:18PM -0700, H.J. Lu wrote:
> From: Kaylee Blake <klkblake@gmail.com>
> 
> 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 when building an executable or shared library, an
> objcopy and strip option, --remove-section-header, to remove ELF
> section header from an executable or shared library.
> 
> bfd/
> 
> 2020-03-XX  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 on
> 	non-relocatable output 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  <hongjiu.lu@intel.com>
> 
> 	PR ld/25617
> 	* NEWS: Mention --remove-section-header for objcopy and strip.
> 	* doc/binutils.texi: Document --remove-section-header for objcopy
> 	and strip.
> 	* objcopy.c (remove_section_header): New.
> 	(command_line_switch): Add OPTION_REMOVE_SECTION_HEADER.
> 	(strip_options): Add --remove-section-header.
> 	(copy_options): Likewise.
> 	(copy_usage): Add --remove-section-header.
> 	(strip_usage): Likewise.
> 	(copy_object): Renamed to ...
> 	(copy_object_1): This.  Issue a warning for
> 	--remove-section-header on non-ELF targets.
> 	(copy_object): New.
> 	(strip_main): Handle OPTION_REMOVE_SECTION_HEADER.
> 	(copy_main): Likewise.
> 
> ld/
> 
> 2020-03-XX  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): Disallow -z nosectionheader with -r.
> 	(elf_static_list_options): Add -z sectionheader and
> 	-z nosectionheader.

This looks mostly OK to me.  The only nit I have to pick is the
copy_object_1 extraction from copy_object.  That seems unnecessary to
me since it looks like --remove-section-header on a non-ELF object
results in an error and the output file being deleted.  So you could
do without the new function and just set strip_symbols and merge_notes
when decoding the new option.  Also, I wouldn't restrict the operation
to (EXEC_P | DYNAMIC) objects.  Someone might find a reason to use
--remove-section-header on a relocatable object.  Allow people to
shoot themselves in the foot if they so desire..  (Unless supporting
--remove-section-header on a relocatable object requires more work.)

-- 
Alan Modra
Australia Development Lab, IBM



More information about the Binutils mailing list