aboutsummaryrefslogtreecommitdiff
path: root/bfd/elfcode.h
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 /bfd/elfcode.h
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 'bfd/elfcode.h')
-rw-r--r--bfd/elfcode.h36
1 files changed, 26 insertions, 10 deletions
diff --git a/bfd/elfcode.h b/bfd/elfcode.h
index e4e29f0..495e498 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)