Merge {first,last}-section-in-segment.c
authornorly <ny-git@enpas.org>
Sat, 23 Feb 2013 15:47:53 +0000 (15:47 +0000)
committernorly <ny-git@enpas.org>
Sat, 23 Feb 2013 15:47:53 +0000 (15:47 +0000)
src/elfops/first-section-in-segment.c [deleted file]
src/elfops/last-section-in-segment.c [deleted file]
src/elfops/section-in-segment.c [new file with mode: 0644]

diff --git a/src/elfops/first-section-in-segment.c b/src/elfops/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/elfops/last-section-in-segment.c b/src/elfops/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/elfops/section-in-segment.c b/src/elfops/section-in-segment.c
new file mode 100644 (file)
index 0000000..e12720f
--- /dev/null
@@ -0,0 +1,85 @@
+#include <stdio.h>
+#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;
+}
+
+
+
+/*
+ * 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;
+}