summaryrefslogtreecommitdiff
path: root/libdwfl
diff options
context:
space:
mode:
authorTimm Bäder <tbaeder@redhat.com>2021-01-08 09:09:56 +0100
committerMark Wielaard <mark@klomp.org>2021-01-28 23:57:06 +0100
commit9c15e1e06eb088d6b632403a41a9e47b91f58b5f (patch)
treef49301a9e43762b14a6e1ae817549fc5ac1fdb9b /libdwfl
parent67a912f0b81047d42881be3c40c8404c2711711c (diff)
downloadelfutils-9c15e1e06eb088d6b632403a41a9e47b91f58b5f.tar.gz
elf-from-memory: Refactor to get rid of nested function
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 <tbaeder@redhat.com>
Diffstat (limited to 'libdwfl')
-rw-r--r--libdwfl/ChangeLog6
-rw-r--r--libdwfl/elf-from-memory.c115
2 files changed, 56 insertions, 65 deletions
diff --git a/libdwfl/ChangeLog b/libdwfl/ChangeLog
index f17eafe7..5058f5f8 100644
--- a/libdwfl/ChangeLog
+++ b/libdwfl/ChangeLog
@@ -1,5 +1,11 @@
2021-01-08 Timm Bäder <tbaeder@redhat.com>
+ * elf-from-memory.c (elf_from_remote_memory): Add for loop over
+ switch. inline handle_segment call, set vaddr, offset and filesz
+ directly based on class.
+
+2021-01-08 Timm Bäder <tbaeder@redhat.com>
+
* 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,
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);