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 89B80384A01A for ; Fri, 8 Jan 2021 08:10:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 89B80384A01A 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-413-88XY2uFcPa24eQiB5Zn3dQ-1; Fri, 08 Jan 2021 03:10:09 -0500 X-MC-Unique: 88XY2uFcPa24eQiB5Zn3dQ-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 6298A5185 for ; Fri, 8 Jan 2021 08:10:08 +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 D38975D9DE for ; Fri, 8 Jan 2021 08:10:07 +0000 (UTC) From: tbaeder@redhat.com To: elfutils-devel@sourceware.org Subject: [PATCH 1/2] elf-from-memory: Restructure code to get rid of nested handle_segment() Date: Fri, 8 Jan 2021 09:09:55 +0100 Message-Id: <20210108080956.2201985-2-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.6 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:12 -0000 From: Timm Bäder Use one loop for both 32 and 64 bit case. This allows for only one call site of the old handle_segment(), which we can then inline into the for loop. Signed-off-by: Timm Bäder --- libdwfl/elf-from-memory.c | 81 ++++++++++++++++----------------------- 1 file changed, 34 insertions(+), 47 deletions(-) diff --git a/libdwfl/elf-from-memory.c b/libdwfl/elf-from-memory.c index c54c1b99..933ab864 100644 --- a/libdwfl/elf-from-memory.c +++ b/libdwfl/elf-from-memory.c @@ -223,61 +223,48 @@ elf_from_remote_memory (GElf_Addr ehdr_vma, bool found_base = false; Elf32_Phdr (*p32)[phnum] = phdrsp; Elf64_Phdr (*p64)[phnum] = phdrsp; - switch (ehdr.e32.e_ident[EI_CLASS]) + + if (class32) { - /* Sanity checks segments and calculates segment_end, - segments_end, segments_end_mem and loadbase (if not - found_base yet). Returns true if sanity checking failed, - false otherwise. */ - inline bool handle_segment (GElf_Addr vaddr, GElf_Off offset, - GElf_Xword filesz, GElf_Xword memsz) - { - /* Sanity check the segment load aligns with the pagesize. */ - if (((vaddr - offset) & (pagesize - 1)) != 0) - return true; + if (! elf32_xlatetom (&xlateto, &xlatefrom, ehdr.e32.e_ident[EI_DATA])) + goto libelf_error; + } + else + { + if (! elf64_xlatetom (&xlateto, &xlatefrom, ehdr.e64.e_ident[EI_DATA])) + goto libelf_error; + } - GElf_Off segment_end = ((offset + filesz + pagesize - 1) - & -pagesize); + for (uint_fast16_t i = 0; i < phnum; ++i) + { + GElf_Word type = class32 ? (*p32)[i].p_type : (*p64)[i].p_type; - if (segment_end > (GElf_Off) contents_size) - contents_size = segment_end; + if (type != PT_LOAD) + continue; - if (!found_base && (offset & -pagesize) == 0) - { - loadbase = ehdr_vma - (vaddr & -pagesize); - found_base = true; - } + GElf_Addr vaddr = class32 ? (*p32)[i].p_vaddr : (*p64)[i].p_vaddr; + GElf_Xword memsz = class32 ? (*p32)[i].p_memsz : (*p64)[i].p_memsz; + GElf_Off offset = class32 ? (*p32)[i].p_offset : (*p64)[i].p_offset; + GElf_Xword filesz = class32 ? (*p32)[i].p_filesz : (*p64)[i].p_filesz; - segments_end = offset + filesz; - segments_end_mem = offset + memsz; - return false; - } + /* Sanity check the segment load aligns with the pagesize. */ + if (((vaddr - offset) & (pagesize - 1)) != 0) + goto bad_elf; - case ELFCLASS32: - if (elf32_xlatetom (&xlateto, &xlatefrom, - ehdr.e32.e_ident[EI_DATA]) == NULL) - goto libelf_error; - 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, (*p32)[i].p_memsz)) - goto bad_elf; - break; + GElf_Off segment_end = ((offset + filesz + pagesize - 1) + & -pagesize); - case ELFCLASS64: - if (elf64_xlatetom (&xlateto, &xlatefrom, - ehdr.e64.e_ident[EI_DATA]) == NULL) - goto libelf_error; - 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, (*p64)[i].p_memsz)) - goto bad_elf; - break; + if (segment_end > (GElf_Off) contents_size) + contents_size = segment_end; - default: - abort (); - break; + if (!found_base && (offset & -pagesize) == 0) + { + loadbase = ehdr_vma - (vaddr & -pagesize); + found_base = true; + } + + segments_end = offset + filesz; + segments_end_mem = offset + memsz; } /* Trim the last segment so we don't bother with zeros in the last page -- 2.26.2