From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 87315 invoked by alias); 26 Oct 2018 21:51:03 -0000 Mailing-List: contact elfutils-devel-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Post: List-Help: List-Subscribe: Sender: elfutils-devel-owner@sourceware.org Received: (qmail 87216 invoked by uid 89); 26 Oct 2018 21:51:02 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Checked: by ClamAV 0.100.1 on sourceware.org X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,GIT_PATCH_3,RCVD_IN_DNSWL_NONE,SPF_PASS autolearn=ham version=3.3.2 spammy=H*Ad:U*mark, refuse, Hx-languages-length:4344, readelfc X-Spam-Status: No, score=-26.9 required=5.0 tests=BAYES_00,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,GIT_PATCH_3,RCVD_IN_DNSWL_NONE,SPF_PASS autolearn=ham version=3.3.2 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on sourceware.org X-Spam-Level: X-HELO: gnu.wildebeest.org Received: from wildebeest.demon.nl (HELO gnu.wildebeest.org) (212.238.236.112) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 26 Oct 2018 21:51:01 +0000 Received: from tarox.wildebeest.org (tarox.wildebeest.org [172.31.17.39]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by gnu.wildebeest.org (Postfix) with ESMTPSA id CE8DB315F8DB; Fri, 26 Oct 2018 23:50:58 +0200 (CEST) Received: by tarox.wildebeest.org (Postfix, from userid 1000) id C99B046765F7; Fri, 26 Oct 2018 23:50:58 +0200 (CEST) From: Mark Wielaard To: elfutils-devel@sourceware.org Cc: Mark Wielaard Subject: [PATCH 1/4] strip: Always copy over any phdrs if there are any. Date: Fri, 26 Oct 2018 21:51:00 -0000 Message-Id: <1540590644-22290-2-git-send-email-mark@klomp.org> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1540590644-22290-1-git-send-email-mark@klomp.org> References: <1540590644-22290-1-git-send-email-mark@klomp.org> X-Spam-Flag: NO X-IsSubscribed: yes X-SW-Source: 2018-q4/txt/msg00084.txt.bz2 Ignore the type of ELF file, just copy over any phdrs if the original file contained any. Also refuse to move around any allocated sections based on whether there are any phdrs instead of on ELF file type. Signed-off-by: Mark Wielaard --- src/ChangeLog | 6 +++++ src/strip.c | 70 +++++++++++++++++++++++++++++++++++------------------------ 2 files changed, 48 insertions(+), 28 deletions(-) diff --git a/src/ChangeLog b/src/ChangeLog index 602312e..5a57e8f 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,9 @@ +2018-10-24 Mark Wielaard + + * strip.c (handle_elf): Always copy over phdrs if there are any + and check phnum instead of e_type to determine whether to move + allocated sections. + 2018-10-16 Mark Wielaard * readelf.c (print_debug_frame_section): Make sure readp is never diff --git a/src/strip.c b/src/strip.c index fdebc5e..cb479de 100644 --- a/src/strip.c +++ b/src/strip.c @@ -588,49 +588,63 @@ handle_elf (int fd, Elf *elf, const char *prefix, const char *fname, else newelf = elf_clone (elf, ELF_C_EMPTY); - if (unlikely (gelf_newehdr (newelf, gelf_getclass (elf)) == 0) - || (ehdr->e_type != ET_REL - && unlikely (gelf_newphdr (newelf, phnum) == 0))) + if (unlikely (gelf_newehdr (newelf, gelf_getclass (elf)) == 0)) { - error (0, 0, gettext ("cannot create new file '%s': %s"), + error (0, 0, gettext ("cannot create new ehdr for file '%s': %s"), output_fname ?: fname, elf_errmsg (-1)); goto fail; } /* Copy over the old program header if needed. */ - if (ehdr->e_type != ET_REL) - for (cnt = 0; cnt < phnum; ++cnt) - { - GElf_Phdr phdr_mem; - GElf_Phdr *phdr = gelf_getphdr (elf, cnt, &phdr_mem); - if (phdr == NULL - || unlikely (gelf_update_phdr (newelf, cnt, phdr) == 0)) - INTERNAL_ERROR (fname); - } + if (phnum > 0) + { + if (unlikely (gelf_newphdr (newelf, phnum) == 0)) + { + error (0, 0, gettext ("cannot create new phdr for file '%s': %s"), + output_fname ?: fname, elf_errmsg (-1)); + goto fail; + } + + for (cnt = 0; cnt < phnum; ++cnt) + { + GElf_Phdr phdr_mem; + GElf_Phdr *phdr = gelf_getphdr (elf, cnt, &phdr_mem); + if (phdr == NULL + || unlikely (gelf_update_phdr (newelf, cnt, phdr) == 0)) + INTERNAL_ERROR (fname); + } + } if (debug_fname != NULL) { /* Also create an ELF descriptor for the debug file */ debugelf = elf_begin (debug_fd, ELF_C_WRITE_MMAP, NULL); - if (unlikely (gelf_newehdr (debugelf, gelf_getclass (elf)) == 0) - || (ehdr->e_type != ET_REL - && unlikely (gelf_newphdr (debugelf, phnum) == 0))) + if (unlikely (gelf_newehdr (debugelf, gelf_getclass (elf)) == 0)) { - error (0, 0, gettext ("cannot create new file '%s': %s"), + error (0, 0, gettext ("cannot create new ehdr for file '%s': %s"), debug_fname, elf_errmsg (-1)); goto fail_close; } /* Copy over the old program header if needed. */ - if (ehdr->e_type != ET_REL) - for (cnt = 0; cnt < phnum; ++cnt) - { - GElf_Phdr phdr_mem; - GElf_Phdr *phdr = gelf_getphdr (elf, cnt, &phdr_mem); - if (phdr == NULL - || unlikely (gelf_update_phdr (debugelf, cnt, phdr) == 0)) - INTERNAL_ERROR (fname); - } + if (phnum > 0) + { + if (unlikely (gelf_newphdr (debugelf, phnum) == 0)) + { + error (0, 0, gettext ("cannot create new phdr for file '%s': %s"), + debug_fname, elf_errmsg (-1)); + goto fail_close; + } + + for (cnt = 0; cnt < phnum; ++cnt) + { + GElf_Phdr phdr_mem; + GElf_Phdr *phdr = gelf_getphdr (elf, cnt, &phdr_mem); + if (phdr == NULL + || unlikely (gelf_update_phdr (debugelf, cnt, phdr) == 0)) + INTERNAL_ERROR (fname); + } + } } /* Number of sections. */ @@ -738,7 +752,7 @@ handle_elf (int fd, Elf *elf, const char *prefix, const char *fname, to keep the layout of all allocated sections as similar as possible to the original file. In relocatable object files everything can be moved. */ - if (ehdr->e_type == ET_REL + if (phnum == 0 || (shdr_info[cnt].shdr.sh_flags & SHF_ALLOC) == 0) shdr_info[cnt].shdr.sh_offset = 0; @@ -2328,7 +2342,7 @@ while computing checksum for debug information")); /* The ELF library better follows our layout when this is not a relocatable object file. */ elf_flagelf (newelf, ELF_C_SET, - (ehdr->e_type != ET_REL ? ELF_F_LAYOUT : 0) + (phnum > 0 ? ELF_F_LAYOUT : 0) | (permissive ? ELF_F_PERMISSIVE : 0)); /* Finally write the file. */ -- 1.8.3.1