From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by sourceware.org (Postfix) with ESMTP id 8EE72384A02F for ; Fri, 8 Jan 2021 08:10:13 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 8EE72384A02F Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-485-h9kogY6nOJWslw2b7G0ucg-1; Fri, 08 Jan 2021 03:10:10 -0500 X-MC-Unique: h9kogY6nOJWslw2b7G0ucg-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 427E1190A7A3 for ; Fri, 8 Jan 2021 08:10:09 +0000 (UTC) Received: from hostfoo.redhat.com (ovpn-112-148.ams2.redhat.com [10.36.112.148]) by smtp.corp.redhat.com (Postfix) with ESMTP id B31635D9E2 for ; Fri, 8 Jan 2021 08:10:08 +0000 (UTC) From: tbaeder@redhat.com To: elfutils-devel@sourceware.org Subject: [PATCH 2/2] elf-from-memory: Refactor to get rid of nested function Date: Fri, 8 Jan 2021 09:09:56 +0100 Message-Id: <20210108080956.2201985-3-tbaeder@redhat.com> In-Reply-To: <20210108080956.2201985-1-tbaeder@redhat.com> References: <20210108080956.2201985-1-tbaeder@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-14.7 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: elfutils-devel@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Elfutils-devel mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 08 Jan 2021 08:10:14 -0000 From: Timm Bäder Try to unify the 32/64 bit code paths and get rid of the nested handle_segment() this way. Signed-off-by: Timm Bäder --- libdwfl/elf-from-memory.c | 115 +++++++++++++++++--------------------- 1 file changed, 50 insertions(+), 65 deletions(-) diff --git a/libdwfl/elf-from-memory.c b/libdwfl/elf-from-memory.c index 933ab864..a0ef0014 100644 --- a/libdwfl/elf-from-memory.c +++ b/libdwfl/elf-from-memory.c @@ -292,80 +292,65 @@ elf_from_remote_memory (GElf_Addr ehdr_vma, goto no_memory; } - switch (ehdr.e32.e_ident[EI_CLASS]) + for (uint_fast16_t i = 0; i < phnum; ++i) { - /* Reads the given segment. Returns true if reading fails, - false otherwise. */ - inline bool handle_segment (GElf_Addr vaddr, GElf_Off offset, - GElf_Xword filesz) - { - GElf_Off start = offset & -pagesize; - GElf_Off end = (offset + filesz + pagesize - 1) & -pagesize; - if (end > (GElf_Off) contents_size) - end = contents_size; - nread = (*read_memory) (arg, buffer + start, - (loadbase + vaddr) & -pagesize, - end - start, end - start); - return nread <= 0; - } + GElf_Word type = class32 ? (*p32)[i].p_type : (*p64)[i].p_type; - case ELFCLASS32: - for (uint_fast16_t i = 0; i < phnum; ++i) - if ((*p32)[i].p_type == PT_LOAD) - if (handle_segment ((*p32)[i].p_vaddr, (*p32)[i].p_offset, - (*p32)[i].p_filesz)) - goto read_error; - - /* If the segments visible in memory didn't include the section - headers, then clear them from the file header. */ - if (contents_size < shdrs_end) - { - ehdr.e32.e_shoff = 0; - ehdr.e32.e_shnum = 0; - ehdr.e32.e_shstrndx = 0; - } + if (type != PT_LOAD) + continue; + + GElf_Addr vaddr = class32 ? (*p32)[i].p_vaddr : (*p64)[i].p_vaddr; + GElf_Off offset = class32 ? (*p32)[i].p_offset : (*p64)[i].p_offset; + GElf_Xword filesz = class32 ? (*p32)[i].p_filesz : (*p64)[i].p_filesz; + + GElf_Off start = offset & -pagesize; + GElf_Off end = (offset + filesz + pagesize - 1) & -pagesize; + if (end > (GElf_Off) contents_size) + end = contents_size; + nread = (*read_memory) (arg, buffer + start, + (loadbase + vaddr) & -pagesize, + end - start, end - start); + if (nread <= 0) + goto read_error; + } + + /* If the segments visible in memory didn't include the section + headers, then clear them from the file header. */ + if (contents_size < shdrs_end) + { + if (class32) + { + ehdr.e32.e_shoff = 0; + ehdr.e32.e_shnum = 0; + ehdr.e32.e_shstrndx = 0; + } + else + { + ehdr.e64.e_shoff = 0; + ehdr.e64.e_shnum = 0; + ehdr.e64.e_shstrndx = 0; + } + } - /* This will normally have been in the first PT_LOAD segment. But it - conceivably could be missing, and we might have just changed it. */ - xlatefrom.d_type = xlateto.d_type = ELF_T_EHDR; + /* This will normally have been in the first PT_LOAD segment. But it + conceivably could be missing, and we might have just changed it. */ + xlatefrom.d_type = xlateto.d_type = ELF_T_EHDR; + xlateto.d_buf = buffer; + if (class32) + { xlatefrom.d_size = xlateto.d_size = sizeof ehdr.e32; xlatefrom.d_buf = &ehdr.e32; - xlateto.d_buf = buffer; if (elf32_xlatetof (&xlateto, &xlatefrom, - ehdr.e32.e_ident[EI_DATA]) == NULL) - goto libelf_error; - break; - - case ELFCLASS64: - for (uint_fast16_t i = 0; i < phnum; ++i) - if ((*p64)[i].p_type == PT_LOAD) - if (handle_segment ((*p64)[i].p_vaddr, (*p64)[i].p_offset, - (*p64)[i].p_filesz)) - goto read_error; - - /* If the segments visible in memory didn't include the section - headers, then clear them from the file header. */ - if (contents_size < shdrs_end) - { - ehdr.e64.e_shoff = 0; - ehdr.e64.e_shnum = 0; - ehdr.e64.e_shstrndx = 0; - } - - /* This will normally have been in the first PT_LOAD segment. But it - conceivably could be missing, and we might have just changed it. */ - xlatefrom.d_type = xlateto.d_type = ELF_T_EHDR; + ehdr.e32.e_ident[EI_DATA]) == NULL) + goto libelf_error; + } + else + { xlatefrom.d_size = xlateto.d_size = sizeof ehdr.e64; xlatefrom.d_buf = &ehdr.e64; - xlateto.d_buf = buffer; if (elf64_xlatetof (&xlateto, &xlatefrom, - ehdr.e64.e_ident[EI_DATA]) == NULL) - goto libelf_error; - break; - - default: - abort (); - break; + ehdr.e64.e_ident[EI_DATA]) == NULL) + goto libelf_error; } free (phdrsp); -- 2.26.2