Clean up ELF-related files and functions
authornorly <ny-git@enpas.org>
Sat, 23 Feb 2013 15:59:12 +0000 (15:59 +0000)
committernorly <ny-git@enpas.org>
Sat, 23 Feb 2013 15:59:12 +0000 (15:59 +0000)
15 files changed:
include/libelfu/elfops.h
include/libelfu/generic.h [new file with mode: 0644]
include/libelfu/libelfu.h
src/elfops/phdr-contains-section.c [new file with mode: 0644]
src/elfops/phdr-fixup-selfref.c [new file with mode: 0644]
src/elfops/phdr.c [deleted file]
src/elfops/scnSize.c [deleted file]
src/elfops/section-by-name.c
src/elfops/section-in-segment.c
src/elfops/section-name.c
src/elfops/segment-contains-section.c [deleted file]
src/generic/scnSize.c [new file with mode: 0644]
src/model/check.c
src/printing/sections.c
src/printing/segments.c

index a57069d30b8f6152b27fd073347533a212f5214e..25575a102434c0647085169b34a6237cbcecaaf3 100644 (file)
@@ -6,15 +6,14 @@
 
 #include <libelfu/types.h>
 
-size_t elfu_scnSizeFile(const GElf_Shdr *shdr);
 
-char* elfu_sectionName(Elf *e, Elf_Scn *scn);
-Elf_Scn* elfu_sectionByName(Elf *e, char *name);
+   char* elfu_eScnName(Elf *e, Elf_Scn *scn);
+Elf_Scn* elfu_eScnByName(Elf *e, char *name);
 
-int elfu_segmentContainsSection(GElf_Phdr *phdr, GElf_Shdr *shdr);
-Elf_Scn* elfu_firstSectionInSegment(Elf *e, GElf_Phdr *phdr);
-Elf_Scn* elfu_lastSectionInSegment(Elf *e, GElf_Phdr *phdr);
+     int elfu_ePhdrContainsScn(GElf_Phdr *phdr, GElf_Shdr *shdr);
+Elf_Scn* elfu_eScnFirstInSegment(Elf *e, GElf_Phdr *phdr);
+Elf_Scn* elfu_eScnLastInSegment(Elf *e, GElf_Phdr *phdr);
 
-void elfu_fixupPhdrSelfRef(Elf *e);
+void elfu_ePhdrFixupSelfRef(Elf *e);
 
 #endif
diff --git a/include/libelfu/generic.h b/include/libelfu/generic.h
new file mode 100644 (file)
index 0000000..9d9aefb
--- /dev/null
@@ -0,0 +1,9 @@
+#ifndef __LIBELFU_GENERIC_H__
+#define __LIBELFU_GENERIC_H__
+
+#include <gelf.h>
+
+
+size_t elfu_gScnSizeFile(const GElf_Shdr *shdr);
+
+#endif
index 961235fbd50886ad6aa8d11c5358ccab23d46c09..3c62aad52126815718e5ffaa06fea11f582cfb37 100644 (file)
@@ -4,6 +4,7 @@
 
 #include <libelfu/types.h>
 
+#include <libelfu/generic.h>
 #include <libelfu/elfops.h>
 #include <libelfu/model.h>
 
diff --git a/src/elfops/phdr-contains-section.c b/src/elfops/phdr-contains-section.c
new file mode 100644 (file)
index 0000000..6b57019
--- /dev/null
@@ -0,0 +1,19 @@
+#include <libelf.h>
+#include <gelf.h>
+
+#include <libelfu/libelfu.h>
+
+
+int elfu_ePhdrContainsScn(GElf_Phdr *phdr, GElf_Shdr *shdr)
+{
+  size_t secStart = shdr->sh_offset;
+  size_t secEnd   = shdr->sh_offset + shdr->sh_size;
+  size_t segStart = phdr->p_offset;
+  size_t segEnd   = phdr->p_offset + phdr->p_memsz;
+
+  if (secStart < segStart || secEnd > segEnd) {
+    return 0;
+  }
+
+  return 1;
+}
diff --git a/src/elfops/phdr-fixup-selfref.c b/src/elfops/phdr-fixup-selfref.c
new file mode 100644 (file)
index 0000000..e79f584
--- /dev/null
@@ -0,0 +1,41 @@
+#include <stdio.h>
+
+#include <libelf.h>
+#include <gelf.h>
+
+void elfu_ePhdrFixupSelfRef(Elf *e)
+{
+  GElf_Ehdr ehdr;
+  size_t i, n;
+
+  if (!gelf_getehdr(e, &ehdr)) {
+    fprintf(stderr, "gelf_getehdr() failed: %s.", elf_errmsg(-1));
+    return;
+  }
+
+  if (elf_getphdrnum(e, &n)) {
+    fprintf(stderr, "elf_getphdrnum() failed: %s\n", elf_errmsg(-1));
+  }
+
+  for (i = 0; i < n; i++) {
+    GElf_Phdr phdr;
+
+    if (gelf_getphdr(e, i, &phdr) != &phdr) {
+      fprintf(stderr, "gelf_getphdr() failed for #%d: %s\n", i, elf_errmsg(-1));
+      continue;
+    }
+
+    if (phdr.p_type == PT_PHDR) {
+      phdr.p_offset = ehdr.e_phoff;
+      phdr.p_filesz = elf32_fsize(ELF_T_PHDR, n, EV_CURRENT);
+      phdr.p_memsz = phdr.p_filesz;
+
+      if (!gelf_update_phdr (e, i, &phdr)) {
+        fprintf(stderr, "gelf_update_ehdr() failed: %s\n", elf_errmsg(-1));
+      }
+    }
+  }
+
+  /* Tell libelf that phdrs have changed */
+  elf_flagphdr(e, ELF_C_SET, ELF_F_DIRTY);
+}
diff --git a/src/elfops/phdr.c b/src/elfops/phdr.c
deleted file mode 100644 (file)
index ccdc021..0000000
+++ /dev/null
@@ -1,41 +0,0 @@
-#include <stdio.h>
-
-#include <libelf.h>
-#include <gelf.h>
-
-void elfu_fixupPhdrSelfRef(Elf *e)
-{
-  GElf_Ehdr ehdr;
-  size_t i, n;
-
-  if (!gelf_getehdr(e, &ehdr)) {
-    fprintf(stderr, "gelf_getehdr() failed: %s.", elf_errmsg(-1));
-    return;
-  }
-
-  if (elf_getphdrnum(e, &n)) {
-    fprintf(stderr, "elf_getphdrnum() failed: %s\n", elf_errmsg(-1));
-  }
-
-  for (i = 0; i < n; i++) {
-    GElf_Phdr phdr;
-
-    if (gelf_getphdr(e, i, &phdr) != &phdr) {
-      fprintf(stderr, "gelf_getphdr() failed for #%d: %s\n", i, elf_errmsg(-1));
-      continue;
-    }
-
-    if (phdr.p_type == PT_PHDR) {
-      phdr.p_offset = ehdr.e_phoff;
-      phdr.p_filesz = elf32_fsize(ELF_T_PHDR, n, EV_CURRENT);
-      phdr.p_memsz = phdr.p_filesz;
-
-      if (!gelf_update_phdr (e, i, &phdr)) {
-        fprintf(stderr, "gelf_update_ehdr() failed: %s\n", elf_errmsg(-1));
-      }
-    }
-  }
-
-  /* Tell libelf that phdrs have changed */
-  elf_flagphdr(e, ELF_C_SET, ELF_F_DIRTY);
-}
diff --git a/src/elfops/scnSize.c b/src/elfops/scnSize.c
deleted file mode 100644 (file)
index 2c65ca1..0000000
+++ /dev/null
@@ -1,13 +0,0 @@
-#include <assert.h>
-#include <sys/types.h>
-#include <gelf.h>
-#include <libelfu/libelfu.h>
-
-
-
-size_t elfu_scnSizeFile(const GElf_Shdr *shdr)
-{
-  assert(shdr);
-
-  return shdr->sh_type == SHT_NOBITS ? 0 : shdr->sh_size;
-}
index 16971405474053a02586f193b988ab8693aa40ec..3352b14d69cc46679ca82c7876a7fd4cdc381201 100644 (file)
@@ -6,7 +6,7 @@
 #include <libelfu/libelfu.h>
 
 
-Elf_Scn* elfu_sectionByName(Elf *e, char *name)
+Elf_Scn* elfu_eScnByName(Elf *e, char *name)
 {
   size_t shstrndx;
   Elf_Scn *scn;
index e12720ff0093e72e16914fe463da6a50504485ad..cb625141d8eaa5c2d3448f4dfb2fb47f67be38f4 100644 (file)
@@ -13,7 +13,7 @@
  *
  * If no section fits, NULL is returned.
  */
-Elf_Scn* elfu_firstSectionInSegment(Elf *e, GElf_Phdr *phdr)
+Elf_Scn* elfu_eScnFirstInSegment(Elf *e, GElf_Phdr *phdr)
 {
   Elf_Scn *scn;
 
@@ -26,7 +26,7 @@ Elf_Scn* elfu_firstSectionInSegment(Elf *e, GElf_Phdr *phdr)
     }
 
     if (shdr.sh_offset == phdr->p_offset
-        && elfu_segmentContainsSection(phdr, &shdr)) {
+        && elfu_ePhdrContainsScn(phdr, &shdr)) {
       return scn;
     }
 
@@ -45,7 +45,7 @@ Elf_Scn* elfu_firstSectionInSegment(Elf *e, GElf_Phdr *phdr)
  *
  * If no section fits, NULL is returned.
  */
-Elf_Scn* elfu_lastSectionInSegment(Elf *e, GElf_Phdr *phdr)
+Elf_Scn* elfu_eScnLastInSegment(Elf *e, GElf_Phdr *phdr)
 {
   Elf_Scn *last = NULL;
   Elf_Scn *scn;
@@ -60,7 +60,7 @@ Elf_Scn* elfu_lastSectionInSegment(Elf *e, GElf_Phdr *phdr)
       continue;
     }
 
-    if (elfu_segmentContainsSection(phdr, &shdr)) {
+    if (elfu_ePhdrContainsScn(phdr, &shdr)) {
       if (!last) {
         last = scn;
       } else {
index 1365eb6c25e09e784bc8bd3e41abb6e5bb5a3803..e77efeee174fa6bd70e67cc3a93f5f99204a9bef 100644 (file)
@@ -6,7 +6,7 @@
 #include <libelfu/libelfu.h>
 
 
-char* elfu_sectionName(Elf *e, Elf_Scn *scn)
+char* elfu_eScnName(Elf *e, Elf_Scn *scn)
 {
   size_t shstrndx;
   GElf_Shdr shdr;
diff --git a/src/elfops/segment-contains-section.c b/src/elfops/segment-contains-section.c
deleted file mode 100644 (file)
index 298cf72..0000000
+++ /dev/null
@@ -1,19 +0,0 @@
-#include <libelf.h>
-#include <gelf.h>
-
-#include <libelfu/libelfu.h>
-
-
-int elfu_segmentContainsSection(GElf_Phdr *phdr, GElf_Shdr *shdr)
-{
-  size_t secStart = shdr->sh_offset;
-  size_t secEnd   = shdr->sh_offset + shdr->sh_size;
-  size_t segStart = phdr->p_offset;
-  size_t segEnd   = phdr->p_offset + phdr->p_memsz;
-
-  if (secStart < segStart || secEnd > segEnd) {
-    return 0;
-  }
-
-  return 1;
-}
diff --git a/src/generic/scnSize.c b/src/generic/scnSize.c
new file mode 100644 (file)
index 0000000..e4cf1c1
--- /dev/null
@@ -0,0 +1,13 @@
+#include <assert.h>
+#include <sys/types.h>
+#include <gelf.h>
+#include <libelfu/libelfu.h>
+
+
+
+size_t elfu_gScnSizeFile(const GElf_Shdr *shdr)
+{
+  assert(shdr);
+
+  return shdr->sh_type == SHT_NOBITS ? 0 : shdr->sh_size;
+}
index cd45f2d3ff9b9d15696203f5a8f75015f94d7cc0..dd5e8eb90b0172a335d3d46625c6544ebc7f815f 100644 (file)
@@ -69,7 +69,7 @@ int elfu_modelCheck(ElfuElf *me)
 
   /* Check for overlapping sections */
   for (i = 0; i < numSecs - 1; i++) {
-    if (sortedSecs[i]->shdr.sh_offset + elfu_scnSizeFile(&sortedSecs[i]->shdr)
+    if (sortedSecs[i]->shdr.sh_offset + elfu_gScnSizeFile(&sortedSecs[i]->shdr)
         > sortedSecs[i+1]->shdr.sh_offset) {
       fprintf(stderr, "elfu_check: Found overlapping sections: %s and %s.\n",
                       elfu_modelScnName(me, sortedSecs[i]),
@@ -90,7 +90,7 @@ int elfu_modelCheck(ElfuElf *me)
   /* Check for sections overlapping with PHDRs */
   for (i = 0; i < numSecs; i++) {
     if (isOverlapping(sortedSecs[i]->shdr.sh_offset,
-                      elfu_scnSizeFile(&sortedSecs[i]->shdr),
+                      elfu_gScnSizeFile(&sortedSecs[i]->shdr),
                       me->ehdr.e_phoff,
                       me->ehdr.e_phentsize * me->ehdr.e_phnum)) {
       fprintf(stderr, "elfu_check: Found section overlapping with PHDRs: %s.\n",
index be7a9072ae05aa304cf6cb28b19b25a6e2cdfb32..e06f3b7904097450f0ca573cadaaf7f94b6d7d37 100644 (file)
@@ -31,7 +31,7 @@ void printSegmentsWithSection(Elf *e, Elf_Scn *scn)
       continue;
     }
 
-    if (elfu_segmentContainsSection(&phdr, &shdr)) {
+    if (elfu_ePhdrContainsScn(&phdr, &shdr)) {
       printf("     %d %s\n", i, segmentTypeStr(phdr.p_type));
     }
   }
@@ -42,7 +42,7 @@ void printSection(Elf *e, Elf_Scn *scn)
 {
   printf("  %jd: %s\n",
           (uintmax_t) elf_ndxscn(scn),
-          elfu_sectionName(e, scn));
+          elfu_eScnName(e, scn));
 }
 
 
index 1f431d673e85969d2c78eb31e7d9ce7ce79329b2..74eeb831ccc81e80da742e136fb75ddcefe9132c 100644 (file)
@@ -33,8 +33,8 @@ void printSectionsInSegment(Elf *e, GElf_Phdr *phdr)
       continue;
     }
 
-    if (elfu_segmentContainsSection(phdr, &shdr)) {
-      printf("       %10u %s\n", elf_ndxscn(scn), elfu_sectionName(e, scn));
+    if (elfu_ePhdrContainsScn(phdr, &shdr)) {
+      printf("       %10u %s\n", elf_ndxscn(scn), elfu_eScnName(e, scn));
     }
 
     scn = elf_nextscn(e, scn);