From: norly Date: Sat, 23 Feb 2013 15:59:12 +0000 (+0000) Subject: Clean up ELF-related files and functions X-Git-Url: https://git.enpas.org/?p=centaur.git;a=commitdiff_plain;h=6e4cbafe0876ff14ff03a14ab80951167ef67e06 Clean up ELF-related files and functions --- diff --git a/include/libelfu/elfops.h b/include/libelfu/elfops.h index a57069d..25575a1 100644 --- a/include/libelfu/elfops.h +++ b/include/libelfu/elfops.h @@ -6,15 +6,14 @@ #include -size_t elfu_scnSizeFile(const GElf_Shdr *shdr); -char* elfu_sectionName(Elf *e, Elf_Scn *scn); -Elf_Scn* elfu_sectionByName(Elf *e, char *name); + char* elfu_eScnName(Elf *e, Elf_Scn *scn); +Elf_Scn* elfu_eScnByName(Elf *e, char *name); -int elfu_segmentContainsSection(GElf_Phdr *phdr, GElf_Shdr *shdr); -Elf_Scn* elfu_firstSectionInSegment(Elf *e, GElf_Phdr *phdr); -Elf_Scn* elfu_lastSectionInSegment(Elf *e, GElf_Phdr *phdr); + int elfu_ePhdrContainsScn(GElf_Phdr *phdr, GElf_Shdr *shdr); +Elf_Scn* elfu_eScnFirstInSegment(Elf *e, GElf_Phdr *phdr); +Elf_Scn* elfu_eScnLastInSegment(Elf *e, GElf_Phdr *phdr); -void elfu_fixupPhdrSelfRef(Elf *e); +void elfu_ePhdrFixupSelfRef(Elf *e); #endif diff --git a/include/libelfu/generic.h b/include/libelfu/generic.h new file mode 100644 index 0000000..9d9aefb --- /dev/null +++ b/include/libelfu/generic.h @@ -0,0 +1,9 @@ +#ifndef __LIBELFU_GENERIC_H__ +#define __LIBELFU_GENERIC_H__ + +#include + + +size_t elfu_gScnSizeFile(const GElf_Shdr *shdr); + +#endif diff --git a/include/libelfu/libelfu.h b/include/libelfu/libelfu.h index 961235f..3c62aad 100644 --- a/include/libelfu/libelfu.h +++ b/include/libelfu/libelfu.h @@ -4,6 +4,7 @@ #include +#include #include #include diff --git a/src/elfops/phdr-contains-section.c b/src/elfops/phdr-contains-section.c new file mode 100644 index 0000000..6b57019 --- /dev/null +++ b/src/elfops/phdr-contains-section.c @@ -0,0 +1,19 @@ +#include +#include + +#include + + +int elfu_ePhdrContainsScn(GElf_Phdr *phdr, GElf_Shdr *shdr) +{ + 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 0; + } + + return 1; +} diff --git a/src/elfops/phdr-fixup-selfref.c b/src/elfops/phdr-fixup-selfref.c new file mode 100644 index 0000000..e79f584 --- /dev/null +++ b/src/elfops/phdr-fixup-selfref.c @@ -0,0 +1,41 @@ +#include + +#include +#include + +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)); + 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); +} diff --git a/src/elfops/phdr.c b/src/elfops/phdr.c deleted file mode 100644 index ccdc021..0000000 --- a/src/elfops/phdr.c +++ /dev/null @@ -1,41 +0,0 @@ -#include - -#include -#include - -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); -} diff --git a/src/elfops/scnSize.c b/src/elfops/scnSize.c deleted file mode 100644 index 2c65ca1..0000000 --- a/src/elfops/scnSize.c +++ /dev/null @@ -1,13 +0,0 @@ -#include -#include -#include -#include - - - -size_t elfu_scnSizeFile(const GElf_Shdr *shdr) -{ - assert(shdr); - - return shdr->sh_type == SHT_NOBITS ? 0 : shdr->sh_size; -} diff --git a/src/elfops/section-by-name.c b/src/elfops/section-by-name.c index 1697140..3352b14 100644 --- a/src/elfops/section-by-name.c +++ b/src/elfops/section-by-name.c @@ -6,7 +6,7 @@ #include -Elf_Scn* elfu_sectionByName(Elf *e, char *name) +Elf_Scn* elfu_eScnByName(Elf *e, char *name) { size_t shstrndx; Elf_Scn *scn; diff --git a/src/elfops/section-in-segment.c b/src/elfops/section-in-segment.c index e12720f..cb62514 100644 --- a/src/elfops/section-in-segment.c +++ b/src/elfops/section-in-segment.c @@ -13,7 +13,7 @@ * * If no section fits, NULL is returned. */ -Elf_Scn* elfu_firstSectionInSegment(Elf *e, GElf_Phdr *phdr) +Elf_Scn* elfu_eScnFirstInSegment(Elf *e, GElf_Phdr *phdr) { Elf_Scn *scn; @@ -26,7 +26,7 @@ Elf_Scn* elfu_firstSectionInSegment(Elf *e, GElf_Phdr *phdr) } if (shdr.sh_offset == phdr->p_offset - && elfu_segmentContainsSection(phdr, &shdr)) { + && elfu_ePhdrContainsScn(phdr, &shdr)) { return scn; } @@ -45,7 +45,7 @@ Elf_Scn* elfu_firstSectionInSegment(Elf *e, GElf_Phdr *phdr) * * If no section fits, NULL is returned. */ -Elf_Scn* elfu_lastSectionInSegment(Elf *e, GElf_Phdr *phdr) +Elf_Scn* elfu_eScnLastInSegment(Elf *e, GElf_Phdr *phdr) { Elf_Scn *last = NULL; Elf_Scn *scn; @@ -60,7 +60,7 @@ Elf_Scn* elfu_lastSectionInSegment(Elf *e, GElf_Phdr *phdr) continue; } - if (elfu_segmentContainsSection(phdr, &shdr)) { + if (elfu_ePhdrContainsScn(phdr, &shdr)) { if (!last) { last = scn; } else { diff --git a/src/elfops/section-name.c b/src/elfops/section-name.c index 1365eb6..e77efee 100644 --- a/src/elfops/section-name.c +++ b/src/elfops/section-name.c @@ -6,7 +6,7 @@ #include -char* elfu_sectionName(Elf *e, Elf_Scn *scn) +char* elfu_eScnName(Elf *e, Elf_Scn *scn) { size_t shstrndx; GElf_Shdr shdr; diff --git a/src/elfops/segment-contains-section.c b/src/elfops/segment-contains-section.c deleted file mode 100644 index 298cf72..0000000 --- a/src/elfops/segment-contains-section.c +++ /dev/null @@ -1,19 +0,0 @@ -#include -#include - -#include - - -int elfu_segmentContainsSection(GElf_Phdr *phdr, GElf_Shdr *shdr) -{ - 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 0; - } - - return 1; -} diff --git a/src/generic/scnSize.c b/src/generic/scnSize.c new file mode 100644 index 0000000..e4cf1c1 --- /dev/null +++ b/src/generic/scnSize.c @@ -0,0 +1,13 @@ +#include +#include +#include +#include + + + +size_t elfu_gScnSizeFile(const GElf_Shdr *shdr) +{ + assert(shdr); + + return shdr->sh_type == SHT_NOBITS ? 0 : shdr->sh_size; +} diff --git a/src/model/check.c b/src/model/check.c index cd45f2d..dd5e8eb 100644 --- a/src/model/check.c +++ b/src/model/check.c @@ -69,7 +69,7 @@ int elfu_modelCheck(ElfuElf *me) /* Check for overlapping sections */ for (i = 0; i < numSecs - 1; i++) { - if (sortedSecs[i]->shdr.sh_offset + elfu_scnSizeFile(&sortedSecs[i]->shdr) + 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_modelScnName(me, sortedSecs[i]), @@ -90,7 +90,7 @@ int elfu_modelCheck(ElfuElf *me) /* Check for sections overlapping with PHDRs */ for (i = 0; i < numSecs; i++) { if (isOverlapping(sortedSecs[i]->shdr.sh_offset, - elfu_scnSizeFile(&sortedSecs[i]->shdr), + 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", diff --git a/src/printing/sections.c b/src/printing/sections.c index be7a907..e06f3b7 100644 --- a/src/printing/sections.c +++ b/src/printing/sections.c @@ -31,7 +31,7 @@ void printSegmentsWithSection(Elf *e, Elf_Scn *scn) continue; } - if (elfu_segmentContainsSection(&phdr, &shdr)) { + if (elfu_ePhdrContainsScn(&phdr, &shdr)) { printf(" %d %s\n", i, segmentTypeStr(phdr.p_type)); } } @@ -42,7 +42,7 @@ void printSection(Elf *e, Elf_Scn *scn) { printf(" %jd: %s\n", (uintmax_t) elf_ndxscn(scn), - elfu_sectionName(e, scn)); + elfu_eScnName(e, scn)); } diff --git a/src/printing/segments.c b/src/printing/segments.c index 1f431d6..74eeb83 100644 --- a/src/printing/segments.c +++ b/src/printing/segments.c @@ -33,8 +33,8 @@ void printSectionsInSegment(Elf *e, GElf_Phdr *phdr) continue; } - if (elfu_segmentContainsSection(phdr, &shdr)) { - printf(" %10u %s\n", elf_ndxscn(scn), elfu_sectionName(e, scn)); + if (elfu_ePhdrContainsScn(phdr, &shdr)) { + printf(" %10u %s\n", elf_ndxscn(scn), elfu_eScnName(e, scn)); } scn = elf_nextscn(e, scn);