Cleaner error handling with ELFU_WARN and ELFU_WARNELF
authornorly <ny-git@enpas.org>
Fri, 24 May 2013 00:07:45 +0000 (01:07 +0100)
committernorly <ny-git@enpas.org>
Fri, 24 May 2013 00:08:56 +0000 (01:08 +0100)
include/libelfu/debug.h [new file with mode: 0644]
include/libelfu/libelfu.h
src/elfops/phdr-fixup-selfref.c
src/elfops/section-in-segment.c
src/model/check.c
src/model/expandNobits.c
src/model/fromFile.c
src/model/toFile.c

diff --git a/include/libelfu/debug.h b/include/libelfu/debug.h
new file mode 100644 (file)
index 0000000..4c82345
--- /dev/null
@@ -0,0 +1,11 @@
+#ifndef __LIBELFU_DEBUG_H_
+#define __LIBELFU_DEBUG_H_
+
+#include <stdio.h>
+
+
+#define ELFU_WARN(...) do { fprintf(stderr, __VA_ARGS__); } while(0)
+
+#define ELFU_WARNELF(function_name) ELFU_WARN(function_name "() failed: %s\n", elf_errmsg(-1))
+
+#endif
index 1fc613ad097231e5d852d77235236fc0922fde09..cc55282f906fe8b6a721655ae532f63cb28edcad 100644 (file)
@@ -9,5 +9,6 @@
 #include <libelfu/modeltypes.h>
 #include <libelfu/modelops.h>
 
+#include <libelfu/debug.h>
 
 #endif
index d84e83b8f2d38716ef12a58308d1d77c6eade553..0182844caca6b818234c51aa055774948332f8d7 100644 (file)
@@ -1,27 +1,28 @@
-#include <stdio.h>
-
 #include <libelf/libelf.h>
 #include <libelf/gelf.h>
 
+#include <libelfu/debug.h>
+
+
 void elfu_ePhdrFixupSelfRef(Elf *e)
 {
   GElf_Ehdr ehdr;
   size_t i, n;
 
   if (!gelf_getehdr(e, &ehdr)) {
-    fprintf(stderr, "gelf_getehdr() failed: %s.", elf_errmsg(-1));
+    ELFU_WARNELF("gelf_getehdr");
     return;
   }
 
   if (elf_getphdrnum(e, &n)) {
-    fprintf(stderr, "elf_getphdrnum() failed: %s\n", elf_errmsg(-1));
+    ELFU_WARNELF("elf_getphdrnum");
   }
 
   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));
+      ELFU_WARN("gelf_getphdr() failed for #%d: %s\n", i, elf_errmsg(-1));
       continue;
     }
 
@@ -31,7 +32,7 @@ void elfu_ePhdrFixupSelfRef(Elf *e)
       phdr.p_memsz = phdr.p_filesz;
 
       if (!gelf_update_phdr (e, i, &phdr)) {
-        fprintf(stderr, "gelf_update_ehdr() failed: %s\n", elf_errmsg(-1));
+        ELFU_WARNELF("gelf_update_ehdr");
       }
     }
   }
index 1ac9b845732627820e9a72795f428d465b249266..143c03f7a823336be60d556b198a784f2206d85a 100644 (file)
@@ -1,4 +1,3 @@
-#include <stdio.h>
 #include <stdlib.h>
 
 #include <libelf/libelf.h>
@@ -56,7 +55,7 @@ Elf_Scn* elfu_eScnLastInSegment(Elf *e, GElf_Phdr *phdr)
     GElf_Shdr shdr;
 
     if (gelf_getshdr(scn, &shdr) != &shdr) {
-      fprintf(stderr, "gelf_getshdr() failed: %s\n", elf_errmsg(-1));
+      ELFU_WARNELF("gelf_getshdr");
       continue;
     }
 
index c490d5d74df0dcafd9e0d2e553cdfd92a84db5cd..2c8c67ebf757ab38060975e8409317c7938cd01d 100644 (file)
@@ -1,5 +1,4 @@
 #include <assert.h>
-#include <stdio.h>
 #include <stdlib.h>
 #include <sys/types.h>
 #include <libelfu/libelfu.h>
@@ -54,7 +53,7 @@ int elfu_mCheck(ElfuElf *me)
   numSecs = elfu_mCountScns(me);
   sortedSecs = malloc(numSecs * sizeof(*sortedSecs));
   if (!sortedSecs) {
-    fprintf(stderr, "elfu_check: Failed to allocate memory.\n");
+    ELFU_WARN("elfu_check: Failed to allocate memory.\n");
   }
 
   i = 0;
@@ -71,9 +70,9 @@ int elfu_mCheck(ElfuElf *me)
   for (i = 0; i < numSecs - 1; i++) {
     if (sortedSecs[i]->shdr.sh_offset + elfu_gScnSizeFile(&sortedSecs[i]->shdr)
         > sortedSecs[i+1]->shdr.sh_offset) {
-      fprintf(stderr, "elfu_check: Found overlapping sections: %s and %s.\n",
-                      elfu_mScnName(me, sortedSecs[i]),
-                      elfu_mScnName(me, sortedSecs[i+1]));
+      ELFU_WARN("elfu_check: Found overlapping sections: %s and %s.\n",
+                elfu_mScnName(me, sortedSecs[i]),
+                elfu_mScnName(me, sortedSecs[i+1]));
     }
   }
 
@@ -81,8 +80,8 @@ int elfu_mCheck(ElfuElf *me)
   /* Check for sections overlapping with EHDR */
   for (i = 0; i < numSecs; i++) {
     if (sortedSecs[i]->shdr.sh_offset < me->ehdr.e_ehsize) {
-      fprintf(stderr, "elfu_check: Found section overlapping with EHDR: %s.\n",
-                      elfu_mScnName(me, sortedSecs[i]));
+      ELFU_WARN("elfu_check: Found section overlapping with EHDR: %s.\n",
+                elfu_mScnName(me, sortedSecs[i]));
     }
   }
 
@@ -93,8 +92,8 @@ int elfu_mCheck(ElfuElf *me)
                       elfu_gScnSizeFile(&sortedSecs[i]->shdr),
                       me->ehdr.e_phoff,
                       me->ehdr.e_phentsize * me->ehdr.e_phnum)) {
-      fprintf(stderr, "elfu_check: Found section overlapping with PHDRs: %s.\n",
-                      elfu_mScnName(me, sortedSecs[i]));
+      ELFU_WARN("elfu_check: Found section overlapping with PHDRs: %s.\n",
+                elfu_mScnName(me, sortedSecs[i]));
     }
   }
 
index 648ee210bcdb9706ce8d54b6dd3969bf03a14c91..d12990e87a995c65c9d7ee9f21cc1acf6ee4528f 100644 (file)
@@ -1,5 +1,4 @@
 #include <assert.h>
-#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <sys/types.h>
@@ -33,7 +32,7 @@ void elfu_mExpandNobits(ElfuElf *me, GElf_Off off)
          * to be filled. This means that it relies on the NOBITS area
          * being actually 0 bytes, and the expansion would ruin it.
          */
-        fprintf(stderr, "mExpandNobits: Found PHDR spanning expansion offset. Aborting.\n");
+        ELFU_WARN("mExpandNobits: Found PHDR spanning expansion offset. Aborting.\n");
         return;
       }
     } else {
@@ -101,7 +100,7 @@ void elfu_mExpandNobits(ElfuElf *me, GElf_Off off)
         ms->data.d_buf = malloc(ms->shdr.sh_size);
         memset(ms->data.d_buf, '\0', ms->shdr.sh_size);
         if (!ms->data.d_buf) {
-          fprintf(stderr, "mExpandNobits: Could not allocate %jd bytes for NOBITS expansion.\n", ms->shdr.sh_size);
+          ELFU_WARN("mExpandNobits: Could not allocate %jd bytes for NOBITS expansion.\n", ms->shdr.sh_size);
         }
 
         ms->data.d_align = 1;
index 6e79867feff007b3d14e1231f010562ad73c0ed7..fee6ec03fabd4cc08ddef6572757c42d821c7bed 100644 (file)
@@ -1,5 +1,4 @@
 #include <assert.h>
-#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <sys/types.h>
@@ -35,7 +34,7 @@ static ElfuScn* modelFromSection(Elf_Scn *scn)
 
 
   if (gelf_getshdr(scn, &ms->shdr) != &ms->shdr) {
-    fprintf(stderr, "gelf_getshdr() failed: %s\n", elf_errmsg(-1));
+    ELFU_WARN("gelf_getshdr() failed: %s\n", elf_errmsg(-1));
     goto out;
   }
 
@@ -53,7 +52,7 @@ static ElfuScn* modelFromSection(Elf_Scn *scn)
 
     ms->data.d_buf = malloc(ms->shdr.sh_size);
     if (!ms->data.d_buf) {
-      fprintf(stderr, "modelFromSection: Could not allocate data buffer (%jx bytes).\n", ms->shdr.sh_size);
+      ELFU_WARN("modelFromSection: Could not allocate data buffer (%jx bytes).\n", ms->shdr.sh_size);
       goto out;
     }
 
@@ -68,7 +67,7 @@ static ElfuScn* modelFromSection(Elf_Scn *scn)
 
     while (data) {
       if (data->d_off + data->d_size > ms->shdr.sh_size) {
-        fprintf(stderr, "modelFromSection: libelf delivered a bogus data blob. Skipping\n");
+        ELFU_WARN("modelFromSection: libelf delivered a bogus data blob. Skipping\n");
       } else {
         memcpy(ms->data.d_buf + data->d_off, data->d_buf, data->d_size);
       }
@@ -113,11 +112,11 @@ ElfuElf* elfu_mFromElf(Elf *e)
    */
   me->elfclass = gelf_getclass(e);
   if (me->elfclass == ELFCLASSNONE) {
-    fprintf(stderr, "getclass() failed: %s\n", elf_errmsg(-1));
+    ELFU_WARNELF("getclass");
   }
 
   if (!gelf_getehdr(e, &me->ehdr)) {
-    fprintf(stderr, "gelf_getehdr() failed: %s\n", elf_errmsg(-1));
+    ELFU_WARNELF("gelf_getehdr");
     goto out;
   }
 
@@ -149,7 +148,7 @@ ElfuElf* elfu_mFromElf(Elf *e)
    * Segments
    */
   if (elf_getphdrnum(e, &n)) {
-    fprintf(stderr, "elf_getphdrnum() failed: %s\n", elf_errmsg(-1));
+    ELFU_WARNELF("elf_getphdrnum");
   }
 
   for (i = 0; i < n; i++) {
@@ -157,7 +156,7 @@ ElfuElf* elfu_mFromElf(Elf *e)
     ElfuPhdr *mp;
 
     if (gelf_getphdr(e, i, &phdr) != &phdr) {
-      fprintf(stderr, "gelf_getphdr() failed for #%d: %s\n", i, elf_errmsg(-1));
+      ELFU_WARN("gelf_getphdr() failed for #%d: %s\n", i, elf_errmsg(-1));
       break;
     }
 
index b675e55d2b64fc1493468fb33681077cb9ac65f3..729a60e9a5313a6f5ddf1e9233751b4e01a724e0 100644 (file)
@@ -1,4 +1,3 @@
-#include <stdio.h>
 #include <stdlib.h>
 #include <libelf/libelf.h>
 #include <libelf/gelf.h>
@@ -18,14 +17,14 @@ static void modelToPhdrs(ElfuElf *me, Elf *e)
   }
 
   if (!gelf_newphdr(e, i)) {
-    fprintf(stderr, "gelf_newphdr() failed: %s\n", elf_errmsg(-1));
+    ELFU_WARNELF("gelf_newphdr");
   }
 
   /* Copy PHDRs */
   i = 0;
   CIRCLEQ_FOREACH(mp, &me->phdrList, elem) {
     if (!gelf_update_phdr (e, i, &mp->phdr)) {
-      fprintf(stderr, "gelf_update_phdr() failed: %s\n", elf_errmsg(-1));
+      ELFU_WARNELF("gelf_update_phdr");
     }
 
     i++;
@@ -40,14 +39,14 @@ static void modelToSection(ElfuScn *ms, Elf *e)
 
   scnOut = elf_newscn(e);
   if (!scnOut) {
-    fprintf(stderr, "elf_newscn() failed: %s\n", elf_errmsg(-1));
+    ELFU_WARNELF("elf_newscn");
     return;
   }
 
 
   /* SHDR */
   if (!gelf_update_shdr(scnOut, &ms->shdr)) {
-    fprintf(stderr, "gelf_update_shdr() failed: %s\n", elf_errmsg(-1));
+    ELFU_WARNELF("gelf_update_shdr");
   }
 
 
@@ -55,7 +54,7 @@ static void modelToSection(ElfuScn *ms, Elf *e)
   if (ms->data.d_buf) {
     Elf_Data *dataOut = elf_newdata(scnOut);
     if (!dataOut) {
-      fprintf(stderr, "elf_newdata() failed: %s\n", elf_errmsg(-1));
+      ELFU_WARNELF("elf_newdata");
     }
 
     dataOut->d_align = ms->data.d_align;
@@ -82,11 +81,11 @@ void elfu_mToElf(ElfuElf *me, Elf *e)
 
   /* EHDR */
   if (!gelf_newehdr(e, me->elfclass)) {
-    fprintf(stderr, "gelf_newehdr() failed: %s\n", elf_errmsg(-1));
+    ELFU_WARNELF("gelf_newehdr");
   }
 
   if (!gelf_update_ehdr(e, &me->ehdr)) {
-    fprintf(stderr, "gelf_update_ehdr() failed: %s\n", elf_errmsg(-1));
+    ELFU_WARNELF("gelf_update_ehdr");
   }