elf-from-memory: Restructure code to get rid of nested handle_segment()
authorTimm Bäder <tbaeder@redhat.com>
Fri, 8 Jan 2021 08:09:55 +0000 (09:09 +0100)
committerMark Wielaard <mark@klomp.org>
Thu, 28 Jan 2021 21:03:40 +0000 (22:03 +0100)
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 <tbaeder@redhat.com>
libdwfl/ChangeLog
libdwfl/elf-from-memory.c

index f9f6f01..f17eafe 100644 (file)
@@ -1,3 +1,10 @@
+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,
+       found_base, segments_end and segments_end_mem directly.
+
 2020-12-16  Dmitry V. Levin  <ldv@altlinux.org>
 
        * argp-std.c (_): Remove.
index c54c1b9..933ab86 100644 (file)
@@ -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