summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/ChangeLog7
-rw-r--r--src/strip.c43
-rw-r--r--src/unstrip.c21
3 files changed, 66 insertions, 5 deletions
diff --git a/src/ChangeLog b/src/ChangeLog
index a093a737..524c81ac 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,5 +1,12 @@
2018-09-13 Mark Wielaard <[email protected]>
+ * strip.c (handle_elf): Check against shstrndx, not e_shstrndx.
+ Explicitly set shdrstrndx for debug file.
+ * unstrip.c (copy_elf): Explicitly copy shstrndx.
+ (find_alloc_sections_prelink): Document shnum usage.
+
+2018-09-13 Mark Wielaard <[email protected]>
+
* elflint.c (check_elf_header): Use shnum instead of e_shnum for all
checks.
(check_symtab): Use shstrndx instead of e_shstrndx to get section
diff --git a/src/strip.c b/src/strip.c
index dc712364..4a3db1b5 100644
--- a/src/strip.c
+++ b/src/strip.c
@@ -820,7 +820,7 @@ handle_elf (int fd, Elf *elf, const char *prefix, const char *fname,
: (ebl_section_strip_p (ebl, &shdr_info[cnt].shdr,
shdr_info[cnt].name, remove_comment,
remove_debug)
- || cnt == ehdr->e_shstrndx
+ || cnt == shstrndx
|| section_name_matches (remove_secs, shdr_info[cnt].name)))
{
/* The user might want to explicitly keep this one. */
@@ -1081,7 +1081,7 @@ handle_elf (int fd, Elf *elf, const char *prefix, const char *fname,
&& shdr_info[cnt].debug_data == NULL
&& shdr_info[cnt].shdr.sh_type != SHT_NOTE
&& shdr_info[cnt].shdr.sh_type != SHT_GROUP
- && cnt != ehdr->e_shstrndx);
+ && cnt != shstrndx);
/* Set the section header in the new file. */
GElf_Shdr debugshdr = shdr_info[cnt].shdr;
@@ -1134,7 +1134,42 @@ handle_elf (int fd, Elf *elf, const char *prefix, const char *fname,
debugehdr->e_version = ehdr->e_version;
debugehdr->e_entry = ehdr->e_entry;
debugehdr->e_flags = ehdr->e_flags;
- debugehdr->e_shstrndx = ehdr->e_shstrndx;
+
+ size_t shdrstrndx;
+ if (elf_getshdrstrndx (elf, &shdrstrndx) < 0)
+ {
+ error (0, 0, gettext ("%s: error while getting shdrstrndx: %s"),
+ fname, elf_errmsg (-1));
+ result = 1;
+ goto fail_close;
+ }
+
+ if (shstrndx < SHN_LORESERVE)
+ debugehdr->e_shstrndx = shdrstrndx;
+ else
+ {
+ debugehdr->e_shstrndx = SHN_XINDEX;
+ Elf_Scn *scn0 = elf_getscn (debugelf, 0);
+ GElf_Shdr shdr0_mem;
+ GElf_Shdr *shdr0 = gelf_getshdr (scn0, &shdr0_mem);
+ if (shdr0 == NULL)
+ {
+ error (0, 0, gettext ("%s: error getting zero section: %s"),
+ debug_fname, elf_errmsg (-1));
+ result = 1;
+ goto fail_close;
+ }
+
+ shdr0->sh_link = shdrstrndx;
+ if (gelf_update_shdr (scn0, shdr0) == 0)
+ {
+ error (0, 0, gettext ("%s: error while updating zero section: %s"),
+ debug_fname, elf_errmsg (-1));
+ result = 1;
+ goto fail_close;
+ }
+
+ }
if (unlikely (gelf_update_ehdr (debugelf, debugehdr) == 0))
{
@@ -1187,7 +1222,7 @@ handle_elf (int fd, Elf *elf, const char *prefix, const char *fname,
the .shstrtab section which we would add again. */
bool removing_sections = !(cnt == idx
|| (cnt == idx + 1
- && shdr_info[ehdr->e_shstrndx].idx == 0));
+ && shdr_info[shstrndx].idx == 0));
if (output_fname == NULL && !removing_sections)
goto fail_close;
diff --git a/src/unstrip.c b/src/unstrip.c
index ec46c952..e6f09478 100644
--- a/src/unstrip.c
+++ b/src/unstrip.c
@@ -239,8 +239,27 @@ copy_elf (Elf *outelf, Elf *inelf)
ELF_CHECK (gelf_newehdr (outelf, gelf_getclass (inelf)),
_("cannot create ELF header: %s"));
+ size_t shstrndx;
+ ELF_CHECK (elf_getshdrstrndx (inelf, &shstrndx) == 0,
+ _("cannot get shdrstrndx:%s"));
+
GElf_Ehdr ehdr_mem;
GElf_Ehdr *ehdr = gelf_getehdr (inelf, &ehdr_mem);
+ if (shstrndx < SHN_LORESERVE)
+ ehdr->e_shstrndx = shstrndx;
+ else
+ {
+ ehdr->e_shstrndx = SHN_XINDEX;
+ Elf_Scn *scn0 = elf_getscn (outelf, 0);
+ GElf_Shdr shdr0_mem;
+ GElf_Shdr *shdr0 = gelf_getshdr (scn0, &shdr0_mem);
+ ELF_CHECK (shdr0 != NULL,
+ _("cannot get new zero section: %s"));
+ shdr0->sh_link = shstrndx;
+ ELF_CHECK (gelf_update_shdr (scn0, shdr0),
+ _("cannot update new zero section: %s"));
+ }
+
ELF_CHECK (gelf_update_ehdr (outelf, ehdr),
_("cannot copy ELF header: %s"));
@@ -1025,7 +1044,7 @@ find_alloc_sections_prelink (Elf *debug, Elf_Data *debug_shstrtab,
_("cannot read '.gnu.prelink_undo' section: %s"));
uint_fast16_t phnum;
- uint_fast16_t shnum;
+ uint_fast16_t shnum; /* prelink doesn't handle > SHN_LORESERVE. */
if (ehdr.e32.e_ident[EI_CLASS] == ELFCLASS32)
{
phnum = ehdr.e32.e_phnum;