GPLv2 release
[centaur.git] / src / libelfu / modelops / toFile.c
index ff01390b27c1c293a7d19a2f53cc644a03a93eb3..98f6cf7f12426606ed6f352a4c5069f4684ce2b9 100644 (file)
@@ -1,37 +1,40 @@
+/* This file is part of centaur.
+ *
+ * centaur is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License 2 as
+ * published by the Free Software Foundation.
+
+ * centaur is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+
+ * You should have received a copy of the GNU General Public License
+ * along with centaur.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
 #include <assert.h>
 #include <stdlib.h>
 #include <string.h>
 #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;
-
-  /* Count PHDRs */
-  i = 0;
-  CIRCLEQ_FOREACH(mp, &me->phdrList, elem) {
-    i++;
-  }
+  size_t *i = (size_t*)aux1;
+  Elf *e = (Elf*)aux2;
 
-  if (!gelf_newphdr(e, i)) {
-    ELFU_WARNELF("gelf_newphdr");
+  if (!gelf_update_phdr (e, *i, &mp->phdr)) {
+    ELFU_WARNELF("gelf_update_phdr");
   }
 
-  /* 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 +85,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 +119,15 @@ 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);
+
+  elfu_eReorderPhdrs(e);
 
 
+  /* Done */
   elf_flagelf(e, ELF_C_SET, ELF_F_DIRTY);
 }