From 6f12fafdc4102a9b8c3ffee060cab48f50f4c37d Mon Sep 17 00:00:00 2001 From: norly Date: Sat, 23 Feb 2013 15:36:30 +0000 Subject: [PATCH] Remove ELFU_BOOL --- include/libelfu/analysis.h | 2 +- include/libelfu/types.h | 6 +----- src/analysis/segment-contains-section.c | 17 +++++------------ src/lookup/first-section-in-segment.c | 2 +- src/lookup/last-section-in-segment.c | 17 ++++++++++------- src/printing/sections.c | 11 +++++++---- src/printing/segments.c | 10 +++++++--- 7 files changed, 32 insertions(+), 33 deletions(-) diff --git a/include/libelfu/analysis.h b/include/libelfu/analysis.h index 80ec251..f3d4ddb 100644 --- a/include/libelfu/analysis.h +++ b/include/libelfu/analysis.h @@ -6,6 +6,6 @@ #include -ELFU_BOOL elfu_segmentContainsSection(GElf_Phdr *phdr, Elf_Scn *scn); +int elfu_segmentContainsSection(GElf_Phdr *phdr, GElf_Shdr *shdr); #endif diff --git a/include/libelfu/types.h b/include/libelfu/types.h index 6ffdd84..a59efca 100644 --- a/include/libelfu/types.h +++ b/include/libelfu/types.h @@ -1,10 +1,6 @@ #ifndef __LIBELFU_TYPES_H__ #define __LIBELFU_TYPES_H__ -typedef enum { - ELFU_ERROR = -1, - ELFU_FALSE = 0, - ELFU_TRUE = 1 -} ELFU_BOOL; + #endif diff --git a/src/analysis/segment-contains-section.c b/src/analysis/segment-contains-section.c index dc3269d..298cf72 100644 --- a/src/analysis/segment-contains-section.c +++ b/src/analysis/segment-contains-section.c @@ -4,23 +4,16 @@ #include -ELFU_BOOL elfu_segmentContainsSection(GElf_Phdr *phdr, Elf_Scn *scn) +int elfu_segmentContainsSection(GElf_Phdr *phdr, GElf_Shdr *shdr) { - GElf_Shdr shdr; - - - if (gelf_getshdr(scn, &shdr) != &shdr) { - return ELFU_ERROR; - } - - size_t secStart = shdr.sh_offset; - size_t secEnd = shdr.sh_offset + shdr.sh_size; + size_t secStart = shdr->sh_offset; + size_t secEnd = shdr->sh_offset + shdr->sh_size; size_t segStart = phdr->p_offset; size_t segEnd = phdr->p_offset + phdr->p_memsz; if (secStart < segStart || secEnd > segEnd) { - return ELFU_FALSE; + return 0; } - return ELFU_TRUE; + return 1; } diff --git a/src/lookup/first-section-in-segment.c b/src/lookup/first-section-in-segment.c index 833563d..58065a5 100644 --- a/src/lookup/first-section-in-segment.c +++ b/src/lookup/first-section-in-segment.c @@ -25,7 +25,7 @@ Elf_Scn* elfu_firstSectionInSegment(Elf *e, GElf_Phdr *phdr) } if (shdr.sh_offset == phdr->p_offset - && elfu_segmentContainsSection(phdr, scn) == ELFU_TRUE) { + && elfu_segmentContainsSection(phdr, &shdr)) { return scn; } diff --git a/src/lookup/last-section-in-segment.c b/src/lookup/last-section-in-segment.c index 964e854..53323f6 100644 --- a/src/lookup/last-section-in-segment.c +++ b/src/lookup/last-section-in-segment.c @@ -1,3 +1,4 @@ +#include #include #include @@ -21,22 +22,24 @@ Elf_Scn* elfu_lastSectionInSegment(Elf *e, GElf_Phdr *phdr) scn = elf_getscn(e, 1); while (scn) { - if (elfu_segmentContainsSection(phdr, scn) == ELFU_TRUE) { + GElf_Shdr shdr; + + if (gelf_getshdr(scn, &shdr) != &shdr) { + fprintf(stderr, "gelf_getshdr() failed: %s\n", elf_errmsg(-1)); + continue; + } + + if (elfu_segmentContainsSection(phdr, &shdr)) { if (!last) { last = scn; } else { GElf_Shdr shdrOld; - GElf_Shdr shdrNew; if (gelf_getshdr(last, &shdrOld) != &shdrOld) { continue; } - if (gelf_getshdr(scn, &shdrNew) != &shdrNew) { - continue; - } - - if (shdrNew.sh_offset + shdrNew.sh_size + if (shdr.sh_offset + shdr.sh_size > shdrOld.sh_offset + shdrOld.sh_size) { // TODO: Check (leftover space in memory image) < (p_align) last = scn; diff --git a/src/printing/sections.c b/src/printing/sections.c index 6241a73..be7a907 100644 --- a/src/printing/sections.c +++ b/src/printing/sections.c @@ -10,25 +10,28 @@ void printSegmentsWithSection(Elf *e, Elf_Scn *scn) { GElf_Phdr phdr; + GElf_Shdr shdr; int i; size_t n; + if (gelf_getshdr(scn, &shdr) != &shdr) { + fprintf(stderr, "gelf_getshdr() failed: %s\n", elf_errmsg(-1)); + return; + } + if (elf_getphdrnum(e, &n)) { fprintf(stderr, "elf_getphdrnum() failed: %s\n", elf_errmsg(-1)); return; } for (i = 0; i < n; i++) { - ELFU_BOOL isInSeg; - if (gelf_getphdr(e, i, &phdr) != &phdr) { fprintf(stderr, "getphdr() failed for #%d: %s\n", i, elf_errmsg(-1)); continue; } - isInSeg = elfu_segmentContainsSection(&phdr, scn); - if (isInSeg == ELFU_TRUE) { + if (elfu_segmentContainsSection(&phdr, &shdr)) { printf(" %d %s\n", i, segmentTypeStr(phdr.p_type)); } } diff --git a/src/printing/segments.c b/src/printing/segments.c index 6d8cac7..1f431d6 100644 --- a/src/printing/segments.c +++ b/src/printing/segments.c @@ -26,10 +26,14 @@ void printSectionsInSegment(Elf *e, GElf_Phdr *phdr) scn = elf_getscn(e, 0); while (scn) { - ELFU_BOOL isInSeg; + GElf_Shdr shdr; - isInSeg = elfu_segmentContainsSection(phdr, scn); - if (isInSeg == ELFU_TRUE) { + if (gelf_getshdr(scn, &shdr) != &shdr) { + fprintf(stderr, "gelf_getshdr() failed: %s\n", elf_errmsg(-1)); + continue; + } + + if (elfu_segmentContainsSection(phdr, &shdr)) { printf(" %10u %s\n", elf_ndxscn(scn), elfu_sectionName(e, scn)); } -- 2.30.2