summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornorly <ny-git@enpas.org>2013-05-24 01:07:45 +0100
committernorly <ny-git@enpas.org>2013-05-24 01:08:56 +0100
commitbac8986954899053330a952cb6e40d04ec1225b8 (patch)
tree0359885c1cb848b54489f8d8421d5c09edeea592
parent2e60e5b7733fbb99009421ca84a5e3cdc49e7fa7 (diff)
Cleaner error handling with ELFU_WARN and ELFU_WARNELF
-rw-r--r--include/libelfu/debug.h11
-rw-r--r--include/libelfu/libelfu.h1
-rw-r--r--src/elfops/phdr-fixup-selfref.c13
-rw-r--r--src/elfops/section-in-segment.c3
-rw-r--r--src/model/check.c17
-rw-r--r--src/model/expandNobits.c5
-rw-r--r--src/model/fromFile.c15
-rw-r--r--src/model/toFile.c15
8 files changed, 44 insertions, 36 deletions
diff --git a/include/libelfu/debug.h b/include/libelfu/debug.h
new file mode 100644
index 0000000..4c82345
--- /dev/null
+++ b/include/libelfu/debug.h
@@ -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
diff --git a/include/libelfu/libelfu.h b/include/libelfu/libelfu.h
index 1fc613a..cc55282 100644
--- a/include/libelfu/libelfu.h
+++ b/include/libelfu/libelfu.h
@@ -9,5 +9,6 @@
#include <libelfu/modeltypes.h>
#include <libelfu/modelops.h>
+#include <libelfu/debug.h>
#endif
diff --git a/src/elfops/phdr-fixup-selfref.c b/src/elfops/phdr-fixup-selfref.c
index d84e83b..0182844 100644
--- a/src/elfops/phdr-fixup-selfref.c
+++ b/src/elfops/phdr-fixup-selfref.c
@@ -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");
}
}
}
diff --git a/src/elfops/section-in-segment.c b/src/elfops/section-in-segment.c
index 1ac9b84..143c03f 100644
--- a/src/elfops/section-in-segment.c
+++ b/src/elfops/section-in-segment.c
@@ -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;
}
diff --git a/src/model/check.c b/src/model/check.c
index c490d5d..2c8c67e 100644
--- a/src/model/check.c
+++ b/src/model/check.c
@@ -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]));
}
}
diff --git a/src/model/expandNobits.c b/src/model/expandNobits.c
index 648ee21..d12990e 100644
--- a/src/model/expandNobits.c
+++ b/src/model/expandNobits.c
@@ -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;
diff --git a/src/model/fromFile.c b/src/model/fromFile.c
index 6e79867..fee6ec0 100644
--- a/src/model/fromFile.c
+++ b/src/model/fromFile.c
@@ -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;
}
diff --git a/src/model/toFile.c b/src/model/toFile.c
index b675e55..729a60e 100644
--- a/src/model/toFile.c
+++ b/src/model/toFile.c
@@ -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");
}