PHDR self-reference fixup function model-cleanup
authornorly <ny-git@enpas.org>
Mon, 11 Feb 2013 01:10:27 +0000 (01:10 +0000)
committernorly <ny-git@enpas.org>
Mon, 11 Feb 2013 01:24:36 +0000 (01:24 +0000)
include/libelfu/fixup.h [new file with mode: 0644]
include/libelfu/libelfu.h
src/fixup/phdr.c [new file with mode: 0644]

diff --git a/include/libelfu/fixup.h b/include/libelfu/fixup.h
new file mode 100644 (file)
index 0000000..6fb6620
--- /dev/null
@@ -0,0 +1,9 @@
+#ifndef __LIBELFU_FIXUP_H__
+#define __LIBELFU_FIXUP_H__
+
+#include <libelf.h>
+#include <gelf.h>
+
+void elfu_fixupPhdrSelfRef(Elf *e);
+
+#endif
index 28a1e9af0b31eb1b2c2a70f222cf9ac6f1680733..271b0ce8a1c77cc0ce801927c3178170db4f482d 100644 (file)
@@ -5,6 +5,7 @@
 #include <libelfu/types.h>
 
 #include <libelfu/analysis.h>
+#include <libelfu/fixup.h>
 #include <libelfu/lookup.h>
 #include <libelfu/model.h>
 
diff --git a/src/fixup/phdr.c b/src/fixup/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);
+}