From: Timm Bäder Date: Fri, 8 Jan 2021 08:09:55 +0000 (+0100) Subject: elf-from-memory: Restructure code to get rid of nested handle_segment() X-Git-Tag: elfutils-0.183~17 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=67a912f0b81047d42881be3c40c8404c2711711c;p=thirdparty%2Felfutils.git elf-from-memory: Restructure code to get rid of nested handle_segment() 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 --- diff --git a/libdwfl/ChangeLog b/libdwfl/ChangeLog index f9f6f01f0..f17eafe78 100644 --- a/libdwfl/ChangeLog +++ b/libdwfl/ChangeLog @@ -1,3 +1,10 @@ +2021-01-08 Timm Bäder + + * elf-from-memory.c (elf_from_remote_memory): Use if instead of + switch on class. Set new vaddr, memsz, offset and filesz + variables. Inline handle_segment function check and set loadbase, + found_base, segments_end and segments_end_mem directly. + 2020-12-16 Dmitry V. Levin * argp-std.c (_): Remove. diff --git a/libdwfl/elf-from-memory.c b/libdwfl/elf-from-memory.c index c54c1b998..933ab8642 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