LOAD PHDRs at top level, others as children. mPhdrForall().
authornorly <ny-git@enpas.org>
Thu, 20 Jun 2013 22:47:01 +0000 (23:47 +0100)
committernorly <ny-git@enpas.org>
Thu, 20 Jun 2013 23:42:22 +0000 (00:42 +0100)
The reference binaries had to be updated as PHDRs are now reordered.

include/libelfu/modelops.h
src/libelfu/model/phdr.c
src/libelfu/modelops/fromFile.c
src/libelfu/modelops/toFile.c
tests/reference/putsmain32-cloned
tests/reference/putsmain32-with-puts-alternative32

index afe6b96947f4e4a6620761859eb20f4af95318cd..e5702f759ad1e1f208c5638320e3119cc1934346 100644 (file)
@@ -18,6 +18,8 @@ void elfu_mSymtabFlatten(ElfuElf *me);
 void elfu_mRelocate(ElfuElf *metarget, ElfuScn *mstarget, ElfuScn *msrt);
 
 
+typedef void* (PhdrHandlerFunc)(ElfuElf *me, ElfuPhdr *mp, void *aux1, void *aux2);
+    void* elfu_mPhdrForall(ElfuElf *me, PhdrHandlerFunc f, void *aux1, void *aux2);
    size_t elfu_mPhdrCount(ElfuElf *me);
      void elfu_mPhdrUpdateChildOffsets(ElfuPhdr *mp);
 ElfuPhdr* elfu_mPhdrAlloc();
index 6896845f482bd8aac3b5c97aea48255e119a0c2f..1159fbb56c3988790c7d5c66544d778955294f2d 100644 (file)
@@ -4,22 +4,62 @@
 #include <libelfu/libelfu.h>
 
 
-size_t elfu_mPhdrCount(ElfuElf *me)
+/* Meta-functions */
+
+void* elfu_mPhdrForall(ElfuElf *me, PhdrHandlerFunc f, void *aux1, void *aux2)
 {
   ElfuPhdr *mp;
+
+  CIRCLEQ_FOREACH(mp, &me->phdrList, elem) {
+    ElfuPhdr *mp2;
+    void *rv = f(me, mp, aux1, aux2);
+    if (rv) {
+      return rv;
+    }
+
+    CIRCLEQ_FOREACH(mp2, &mp->childPhdrList, elemChildPhdr) {
+      void *rv = f(me, mp2, aux1, aux2);
+      if (rv) {
+        return rv;
+      }
+    }
+  }
+
+  return NULL;
+}
+
+
+
+
+/* Counting */
+
+static void* subCounter(ElfuElf *me, ElfuPhdr *mp, void *aux1, void *aux2)
+{
+  size_t *i = (size_t*)aux1;
+  (void)aux2;
+
+  *i += 1;
+
+  /* Continue */
+  return NULL;
+}
+
+
+size_t elfu_mPhdrCount(ElfuElf *me)
+{
   size_t i = 0;
 
   assert(me);
 
-  CIRCLEQ_FOREACH(mp, &me->phdrList, elem) {
-    i++;
-  }
+  elfu_mPhdrForall(me, subCounter, &i, NULL);
 
   return i;
 }
 
 
 
+/* Layout update */
+
 void elfu_mPhdrUpdateChildOffsets(ElfuPhdr *mp)
 {
   ElfuScn *ms;
@@ -69,7 +109,8 @@ void elfu_mPhdrDestroy(ElfuPhdr* mp)
   assert(mp);
 
   CIRCLEQ_FOREACH(mp2, &mp->childPhdrList, elem) {
-    // TODO ?
+    CIRCLEQ_REMOVE(&mp->childPhdrList, mp2, elem);
+    elfu_mPhdrDestroy(mp2);
   }
 
   CIRCLEQ_FOREACH(ms, &mp->childScnList, elem) {
index e19df7b42f18e8a35416732aff8ba100695dcd2b..c4e8c4555d3382ef68a7588f42ffb5a905dc92ec 100644 (file)
@@ -340,6 +340,8 @@ ElfuElf* elfu_mFromElf(Elf *e)
 
         if (mp->phdr.p_vaddr <= mp2->phdr.p_vaddr
             && OFFS_END(mp2->phdr.p_vaddr, mp2->phdr.p_memsz) <= OFFS_END(mp->phdr.p_vaddr, mp->phdr.p_memsz)) {
+          /* Remove from the main list so only LOADs remain there */
+          CIRCLEQ_REMOVE(&me->phdrList, mp2, elem);
           CIRCLEQ_INSERT_TAIL(&mp->childPhdrList, mp2, elemChildPhdr);
         }
       }
index ff01390b27c1c293a7d19a2f53cc644a03a93eb3..b42bc08076cfa6bf1dea720eb58d83ad8236bb3e 100644 (file)
@@ -4,34 +4,22 @@
 #include <libelfu/libelfu.h>
 
 
-static void modelToPhdrs(ElfuElf *me, Elf *e)
+static void* modelToPhdr(ElfuElf *me, ElfuPhdr *mp, void *aux1, void *aux2)
 {
-  ElfuPhdr *mp;
-  size_t i;
+  size_t *i = (size_t*)aux1;
+  Elf *e = (Elf*)aux2;
 
-  /* Count PHDRs */
-  i = 0;
-  CIRCLEQ_FOREACH(mp, &me->phdrList, elem) {
-    i++;
+  if (!gelf_update_phdr (e, *i, &mp->phdr)) {
+    ELFU_WARNELF("gelf_update_phdr");
   }
 
-  if (!gelf_newphdr(e, i)) {
-    ELFU_WARNELF("gelf_newphdr");
-  }
-
-  /* Copy PHDRs */
-  i = 0;
-  CIRCLEQ_FOREACH(mp, &me->phdrList, elem) {
-    if (!gelf_update_phdr (e, i, &mp->phdr)) {
-      ELFU_WARNELF("gelf_update_phdr");
-    }
+  *i += 1;
 
-    i++;
-  }
+  /* Continue */
+  return NULL;
 }
 
 
-
 static void* modelToSection(ElfuElf *me, ElfuScn *ms, void *aux1, void *aux2)
 {
   Elf_Scn *scnOut;
@@ -82,6 +70,8 @@ static void* modelToSection(ElfuElf *me, ElfuScn *ms, void *aux1, void *aux2)
 
 void elfu_mToElf(ElfuElf *me, Elf *e)
 {
+  size_t i = 0;
+
   if (me->symtab) {
     elfu_mSymtabFlatten(me);
   }
@@ -114,8 +104,13 @@ void elfu_mToElf(ElfuElf *me, Elf *e)
 
 
   /* PHDRs */
-  modelToPhdrs(me, e);
+  if (!gelf_newphdr(e, elfu_mPhdrCount(me))) {
+    ELFU_WARNELF("gelf_newphdr");
+  }
+
+  elfu_mPhdrForall(me, modelToPhdr, &i, e);
 
 
+  /* Done */
   elf_flagelf(e, ELF_C_SET, ELF_F_DIRTY);
 }
index 58328b4f0cc4bbe596d38b57e00fbf0e4d754c32..973ea9c01bdb771776c0a2dfcddf8cc804f79e49 100755 (executable)
Binary files a/tests/reference/putsmain32-cloned and b/tests/reference/putsmain32-cloned differ
index 953ddad817253b60089f9767dd21e825a416099d..c820ef55851e56e8e9e9dc17e7cc2fcbed99103d 100755 (executable)
Binary files a/tests/reference/putsmain32-with-puts-alternative32 and b/tests/reference/putsmain32-with-puts-alternative32 differ