Move ELF-related files together
authornorly <ny-git@enpas.org>
Sat, 23 Feb 2013 15:41:35 +0000 (15:41 +0000)
committernorly <ny-git@enpas.org>
Sat, 23 Feb 2013 15:42:42 +0000 (15:42 +0000)
14 files changed:
src/analysis/segment-contains-section.c [deleted file]
src/elfops/first-section-in-segment.c [new file with mode: 0644]
src/elfops/last-section-in-segment.c [new file with mode: 0644]
src/elfops/phdr.c [new file with mode: 0644]
src/elfops/scnSize.c [new file with mode: 0644]
src/elfops/section-by-name.c [new file with mode: 0644]
src/elfops/section-name.c [new file with mode: 0644]
src/elfops/segment-contains-section.c [new file with mode: 0644]
src/fixup/phdr.c [deleted file]
src/lookup/first-section-in-segment.c [deleted file]
src/lookup/last-section-in-segment.c [deleted file]
src/lookup/scnSize.c [deleted file]
src/lookup/section-by-name.c [deleted file]
src/lookup/section-name.c [deleted file]

diff --git a/src/analysis/segment-contains-section.c b/src/analysis/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/elfops/first-section-in-segment.c b/src/elfops/first-section-in-segment.c
new file mode 100644 (file)
index 0000000..58065a5
--- /dev/null
@@ -0,0 +1,36 @@
+#include <stdlib.h>
+
+#include <libelf.h>
+#include <gelf.h>
+
+#include <libelfu/libelfu.h>
+
+
+/*
+ * Returns the section that starts at the same point in the file as
+ * the segment AND is wholly contained in the memory image.
+ *
+ * If no section fits, NULL is returned.
+ */
+Elf_Scn* elfu_firstSectionInSegment(Elf *e, GElf_Phdr *phdr)
+{
+  Elf_Scn *scn;
+
+  scn = elf_getscn(e, 1);
+  while (scn) {
+    GElf_Shdr shdr;
+
+    if (gelf_getshdr(scn, &shdr) != &shdr) {
+      return NULL;
+    }
+
+    if (shdr.sh_offset == phdr->p_offset
+        && elfu_segmentContainsSection(phdr, &shdr)) {
+      return scn;
+    }
+
+    scn = elf_nextscn(e, scn);
+  }
+
+  return NULL;
+}
diff --git a/src/elfops/last-section-in-segment.c b/src/elfops/last-section-in-segment.c
new file mode 100644 (file)
index 0000000..53323f6
--- /dev/null
@@ -0,0 +1,54 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <libelf.h>
+#include <gelf.h>
+
+#include <libelfu/libelfu.h>
+
+
+/*
+ * Returns the first section that  is contained in the segment and
+ * ends as close to its memory image of as possible (the "last"
+ * section in the segment).
+ *
+ * If no section fits, NULL is returned.
+ */
+Elf_Scn* elfu_lastSectionInSegment(Elf *e, GElf_Phdr *phdr)
+{
+  Elf_Scn *last = NULL;
+  Elf_Scn *scn;
+
+
+  scn = elf_getscn(e, 1);
+  while (scn) {
+    GElf_Shdr shdr;
+
+    if (gelf_getshdr(scn, &shdr) != &shdr) {
+      fprintf(stderr, "gelf_getshdr() failed: %s\n", elf_errmsg(-1));
+      continue;
+    }
+
+    if (elfu_segmentContainsSection(phdr, &shdr)) {
+      if (!last) {
+        last = scn;
+      } else {
+        GElf_Shdr shdrOld;
+
+        if (gelf_getshdr(last, &shdrOld) != &shdrOld) {
+          continue;
+        }
+
+        if (shdr.sh_offset + shdr.sh_size
+            > shdrOld.sh_offset + shdrOld.sh_size) {
+          // TODO: Check (leftover space in memory image) < (p_align)
+          last = scn;
+        }
+      }
+    }
+
+    scn = elf_nextscn(e, scn);
+  }
+
+  return last;
+}
diff --git a/src/elfops/phdr.c b/src/elfops/phdr.c
new file mode 100644 (file)
index 0000000..ccdc021
--- /dev/null
@@ -0,0 +1,41 @@
+#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
new file mode 100644 (file)
index 0000000..2c65ca1
--- /dev/null
@@ -0,0 +1,13 @@
+#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;
+}
diff --git a/src/elfops/section-by-name.c b/src/elfops/section-by-name.c
new file mode 100644 (file)
index 0000000..1697140
--- /dev/null
@@ -0,0 +1,39 @@
+#include <string.h>
+
+#include <libelf.h>
+#include <gelf.h>
+
+#include <libelfu/libelfu.h>
+
+
+Elf_Scn* elfu_sectionByName(Elf *e, char *name)
+{
+  size_t shstrndx;
+  Elf_Scn *scn;
+
+  if (elf_getshdrstrndx(e, &shstrndx) != 0) {
+    return NULL;
+  }
+
+  scn = elf_getscn(e, 1);
+  while (scn) {
+    GElf_Shdr shdr;
+    char *curname;
+
+    if (gelf_getshdr(scn, &shdr) != &shdr) {
+      return NULL;
+    }
+
+    /* elf_strptr returns NULL if there was an error */
+    curname = elf_strptr(e, shstrndx, shdr.sh_name);
+
+    /* strcmp... but we really have no bounds on the lengths here */
+    if (!strcmp(curname, name)) {
+      return scn;
+    }
+
+    scn = elf_nextscn(e, scn);
+  }
+
+  return NULL;
+}
diff --git a/src/elfops/section-name.c b/src/elfops/section-name.c
new file mode 100644 (file)
index 0000000..1365eb6
--- /dev/null
@@ -0,0 +1,24 @@
+#include <stdlib.h>
+
+#include <libelf.h>
+#include <gelf.h>
+
+#include <libelfu/libelfu.h>
+
+
+char* elfu_sectionName(Elf *e, Elf_Scn *scn)
+{
+  size_t shstrndx;
+  GElf_Shdr shdr;
+
+  if (elf_getshdrstrndx(e, &shstrndx) != 0) {
+    return NULL;
+  }
+
+  if (gelf_getshdr(scn, &shdr) != &shdr) {
+    return NULL;
+  }
+
+  /* elf_strptr returns NULL if there was an error */
+  return elf_strptr(e, shstrndx, shdr.sh_name);
+}
diff --git a/src/elfops/segment-contains-section.c b/src/elfops/segment-contains-section.c
new file mode 100644 (file)
index 0000000..298cf72
--- /dev/null
@@ -0,0 +1,19 @@
+#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/fixup/phdr.c b/src/fixup/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/lookup/first-section-in-segment.c b/src/lookup/first-section-in-segment.c
deleted file mode 100644 (file)
index 58065a5..0000000
+++ /dev/null
@@ -1,36 +0,0 @@
-#include <stdlib.h>
-
-#include <libelf.h>
-#include <gelf.h>
-
-#include <libelfu/libelfu.h>
-
-
-/*
- * Returns the section that starts at the same point in the file as
- * the segment AND is wholly contained in the memory image.
- *
- * If no section fits, NULL is returned.
- */
-Elf_Scn* elfu_firstSectionInSegment(Elf *e, GElf_Phdr *phdr)
-{
-  Elf_Scn *scn;
-
-  scn = elf_getscn(e, 1);
-  while (scn) {
-    GElf_Shdr shdr;
-
-    if (gelf_getshdr(scn, &shdr) != &shdr) {
-      return NULL;
-    }
-
-    if (shdr.sh_offset == phdr->p_offset
-        && elfu_segmentContainsSection(phdr, &shdr)) {
-      return scn;
-    }
-
-    scn = elf_nextscn(e, scn);
-  }
-
-  return NULL;
-}
diff --git a/src/lookup/last-section-in-segment.c b/src/lookup/last-section-in-segment.c
deleted file mode 100644 (file)
index 53323f6..0000000
+++ /dev/null
@@ -1,54 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-
-#include <libelf.h>
-#include <gelf.h>
-
-#include <libelfu/libelfu.h>
-
-
-/*
- * Returns the first section that  is contained in the segment and
- * ends as close to its memory image of as possible (the "last"
- * section in the segment).
- *
- * If no section fits, NULL is returned.
- */
-Elf_Scn* elfu_lastSectionInSegment(Elf *e, GElf_Phdr *phdr)
-{
-  Elf_Scn *last = NULL;
-  Elf_Scn *scn;
-
-
-  scn = elf_getscn(e, 1);
-  while (scn) {
-    GElf_Shdr shdr;
-
-    if (gelf_getshdr(scn, &shdr) != &shdr) {
-      fprintf(stderr, "gelf_getshdr() failed: %s\n", elf_errmsg(-1));
-      continue;
-    }
-
-    if (elfu_segmentContainsSection(phdr, &shdr)) {
-      if (!last) {
-        last = scn;
-      } else {
-        GElf_Shdr shdrOld;
-
-        if (gelf_getshdr(last, &shdrOld) != &shdrOld) {
-          continue;
-        }
-
-        if (shdr.sh_offset + shdr.sh_size
-            > shdrOld.sh_offset + shdrOld.sh_size) {
-          // TODO: Check (leftover space in memory image) < (p_align)
-          last = scn;
-        }
-      }
-    }
-
-    scn = elf_nextscn(e, scn);
-  }
-
-  return last;
-}
diff --git a/src/lookup/scnSize.c b/src/lookup/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;
-}
diff --git a/src/lookup/section-by-name.c b/src/lookup/section-by-name.c
deleted file mode 100644 (file)
index 1697140..0000000
+++ /dev/null
@@ -1,39 +0,0 @@
-#include <string.h>
-
-#include <libelf.h>
-#include <gelf.h>
-
-#include <libelfu/libelfu.h>
-
-
-Elf_Scn* elfu_sectionByName(Elf *e, char *name)
-{
-  size_t shstrndx;
-  Elf_Scn *scn;
-
-  if (elf_getshdrstrndx(e, &shstrndx) != 0) {
-    return NULL;
-  }
-
-  scn = elf_getscn(e, 1);
-  while (scn) {
-    GElf_Shdr shdr;
-    char *curname;
-
-    if (gelf_getshdr(scn, &shdr) != &shdr) {
-      return NULL;
-    }
-
-    /* elf_strptr returns NULL if there was an error */
-    curname = elf_strptr(e, shstrndx, shdr.sh_name);
-
-    /* strcmp... but we really have no bounds on the lengths here */
-    if (!strcmp(curname, name)) {
-      return scn;
-    }
-
-    scn = elf_nextscn(e, scn);
-  }
-
-  return NULL;
-}
diff --git a/src/lookup/section-name.c b/src/lookup/section-name.c
deleted file mode 100644 (file)
index 1365eb6..0000000
+++ /dev/null
@@ -1,24 +0,0 @@
-#include <stdlib.h>
-
-#include <libelf.h>
-#include <gelf.h>
-
-#include <libelfu/libelfu.h>
-
-
-char* elfu_sectionName(Elf *e, Elf_Scn *scn)
-{
-  size_t shstrndx;
-  GElf_Shdr shdr;
-
-  if (elf_getshdrstrndx(e, &shstrndx) != 0) {
-    return NULL;
-  }
-
-  if (gelf_getshdr(scn, &shdr) != &shdr) {
-    return NULL;
-  }
-
-  /* elf_strptr returns NULL if there was an error */
-  return elf_strptr(e, shstrndx, shdr.sh_name);
-}