From: norly Date: Mon, 17 Jun 2013 22:34:30 +0000 (+0100) Subject: C90 compliance, except variadic macros and TODOs X-Git-Url: https://git.enpas.org/?p=centaur.git;a=commitdiff_plain;h=f88e1ad7d9db8a41abecc795200f21138af65c74 C90 compliance, except variadic macros and TODOs Also removed a lot of dead code from the early days. --- diff --git a/Makefile b/Makefile index 6418179..c8dee76 100644 --- a/Makefile +++ b/Makefile @@ -14,7 +14,7 @@ SOURCES := $(shell find $(SRCDIR)/ -iname "*.c") OBJS := $(patsubst %.c, $(BUILDDIR)/%.o, $(SOURCES)) INCLUDES := $(patsubst %, -I%, $(INCLUDEDIR) $(SRCDIR)) $(shell pkg-config --cflags-only-I $(LIBRARIES)) -CFLAGS := -g -Wall $(shell pkg-config --cflags-only-other $(LIBRARIES)) +CFLAGS := -g -Wall -pedantic -Wno-variadic-macros $(shell pkg-config --cflags-only-other $(LIBRARIES)) LDFLAGS := $(shell pkg-config --libs $(LIBRARIES)) diff --git a/include/libelfu/elfops.h b/include/libelfu/elfops.h index 73c7aec..48ef380 100644 --- a/include/libelfu/elfops.h +++ b/include/libelfu/elfops.h @@ -9,12 +9,5 @@ int elfu_eCheck(Elf *e); - char* elfu_eScnName(Elf *e, Elf_Scn *scn); -Elf_Scn* elfu_eScnByName(Elf *e, char *name); - -Elf_Scn* elfu_eScnFirstInSegment(Elf *e, GElf_Phdr *phdr); -Elf_Scn* elfu_eScnLastInSegment(Elf *e, GElf_Phdr *phdr); - -void elfu_ePhdrFixupSelfRef(Elf *e); #endif diff --git a/src/elfops/phdr-fixup-selfref.c b/src/elfops/phdr-fixup-selfref.c deleted file mode 100644 index c961ceb..0000000 --- a/src/elfops/phdr-fixup-selfref.c +++ /dev/null @@ -1,39 +0,0 @@ -#include - - -void elfu_ePhdrFixupSelfRef(Elf *e) -{ - GElf_Ehdr ehdr; - size_t i, n; - - if (!gelf_getehdr(e, &ehdr)) { - ELFU_WARNELF("gelf_getehdr"); - return; - } - - if (elf_getphdrnum(e, &n)) { - ELFU_WARNELF("elf_getphdrnum"); - } - - for (i = 0; i < n; i++) { - GElf_Phdr phdr; - - if (gelf_getphdr(e, i, &phdr) != &phdr) { - ELFU_WARN("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)) { - ELFU_WARNELF("gelf_update_ehdr"); - } - } - } - - /* Tell libelf that phdrs have changed */ - elf_flagphdr(e, ELF_C_SET, ELF_F_DIRTY); -} diff --git a/src/elfops/section-by-name.c b/src/elfops/section-by-name.c deleted file mode 100644 index 8bb93a3..0000000 --- a/src/elfops/section-by-name.c +++ /dev/null @@ -1,35 +0,0 @@ -#include -#include - - -Elf_Scn* elfu_eScnByName(Elf *e, char *name) -{ - size_t shstrndx; - Elf_Scn *scn; - - if (elf_getshdrstrndx(e, &shstrndx) != 0) { - return NULL; - } - - scn = elf_getscn(e, 1); - while (scn) { - GElf_Shdr shdr; - char *curname; - - if (gelf_getshdr(scn, &shdr) != &shdr) { - return NULL; - } - - /* elf_strptr returns NULL if there was an error */ - curname = elf_strptr(e, shstrndx, shdr.sh_name); - - /* strcmp... but we really have no bounds on the lengths here */ - if (!strcmp(curname, name)) { - return scn; - } - - scn = elf_nextscn(e, scn); - } - - return NULL; -} diff --git a/src/elfops/section-in-segment.c b/src/elfops/section-in-segment.c deleted file mode 100644 index 7e6206b..0000000 --- a/src/elfops/section-in-segment.c +++ /dev/null @@ -1,80 +0,0 @@ -#include -#include - - -/* - * Returns the section that starts at the same point in the file as - * the segment AND is wholly contained in the memory image. - * - * If no section fits, NULL is returned. - */ -Elf_Scn* elfu_eScnFirstInSegment(Elf *e, GElf_Phdr *phdr) -{ - Elf_Scn *scn; - - scn = elf_getscn(e, 1); - while (scn) { - GElf_Shdr shdr; - - if (gelf_getshdr(scn, &shdr) != &shdr) { - return NULL; - } - - if (shdr.sh_offset == phdr->p_offset - && PHDR_CONTAINS_SCN_IN_MEMORY(phdr, &shdr)) { - return scn; - } - - scn = elf_nextscn(e, scn); - } - - return NULL; -} - - - -/* - * Returns the first section that is contained in the segment and - * ends as close to its memory image of as possible (the "last" - * section in the segment). - * - * If no section fits, NULL is returned. - */ -Elf_Scn* elfu_eScnLastInSegment(Elf *e, GElf_Phdr *phdr) -{ - Elf_Scn *last = NULL; - Elf_Scn *scn; - - - scn = elf_getscn(e, 1); - while (scn) { - GElf_Shdr shdr; - - if (gelf_getshdr(scn, &shdr) != &shdr) { - ELFU_WARNELF("gelf_getshdr"); - continue; - } - - if (PHDR_CONTAINS_SCN_IN_MEMORY(phdr, &shdr)) { - if (!last) { - last = scn; - } else { - GElf_Shdr shdrOld; - - if (gelf_getshdr(last, &shdrOld) != &shdrOld) { - continue; - } - - if (shdr.sh_offset + shdr.sh_size - > shdrOld.sh_offset + shdrOld.sh_size) { - // TODO: Check (leftover space in memory image) < (p_align) - last = scn; - } - } - } - - scn = elf_nextscn(e, scn); - } - - return last; -} diff --git a/src/elfops/section-name.c b/src/elfops/section-name.c deleted file mode 100644 index 4832db9..0000000 --- a/src/elfops/section-name.c +++ /dev/null @@ -1,20 +0,0 @@ -#include -#include - - -char* elfu_eScnName(Elf *e, Elf_Scn *scn) -{ - size_t shstrndx; - GElf_Shdr shdr; - - if (elf_getshdrstrndx(e, &shstrndx) != 0) { - return NULL; - } - - if (gelf_getshdr(scn, &shdr) != &shdr) { - return NULL; - } - - /* elf_strptr returns NULL if there was an error */ - return elf_strptr(e, shstrndx, shdr.sh_name); -} diff --git a/src/modelops/dump.c b/src/modelops/dump.c index 27556f9..b2e172e 100644 --- a/src/modelops/dump.c +++ b/src/modelops/dump.c @@ -99,25 +99,25 @@ void elfu_mDumpPhdr(ElfuElf *me, ElfuPhdr *mp) assert(me); assert(mp); - ELFU_INFO("%12s %8jx %8jx %8jx %8jx %8jx %8jx %8jx %8jx\n", + ELFU_INFO("%12s %8x %8x %8x %8x %8x %8x %8x %8x\n", segmentTypeStr(mp->phdr.p_type), - (uintmax_t) mp->phdr.p_type, - (uintmax_t) mp->phdr.p_offset, - (uintmax_t) mp->phdr.p_vaddr, - (uintmax_t) mp->phdr.p_paddr, - (uintmax_t) mp->phdr.p_filesz, - (uintmax_t) mp->phdr.p_memsz, - (uintmax_t) mp->phdr.p_flags, - (uintmax_t) mp->phdr.p_align); + (unsigned)mp->phdr.p_type, + (unsigned)mp->phdr.p_offset, + (unsigned)mp->phdr.p_vaddr, + (unsigned)mp->phdr.p_paddr, + (unsigned)mp->phdr.p_filesz, + (unsigned)mp->phdr.p_memsz, + (unsigned)mp->phdr.p_flags, + (unsigned)mp->phdr.p_align); if (!CIRCLEQ_EMPTY(&mp->childPhdrList)) { ElfuPhdr *mpc; ELFU_INFO(" -> Child PHDRs:\n"); CIRCLEQ_FOREACH(mpc, &mp->childPhdrList, elemChildPhdr) { - ELFU_INFO(" * %-8s @ %8jx\n", + ELFU_INFO(" * %-8s @ %8x\n", segmentTypeStr(mpc->phdr.p_type), - mpc->phdr.p_vaddr); + (unsigned)mpc->phdr.p_vaddr); } } @@ -126,9 +126,9 @@ void elfu_mDumpPhdr(ElfuElf *me, ElfuPhdr *mp) ELFU_INFO(" -> Child sections:\n"); CIRCLEQ_FOREACH(msc, &mp->childScnList, elemChildScn) { - ELFU_INFO(" * %-17s @ %8jx\n", + ELFU_INFO(" * %-17s @ %8x\n", elfu_mScnName(me, msc), - msc->shdr.sh_addr); + (unsigned)msc->shdr.sh_addr); } } } @@ -146,15 +146,15 @@ void elfu_mDumpScn(ElfuElf *me, ElfuScn *ms) linkstr = ms->linkptr ? elfu_mScnName(me, ms->linkptr) : ""; infostr = ms->infoptr ? elfu_mScnName(me, ms->infoptr) : ""; - ELFU_INFO("%-17s %-15s %8jx %9jx %8jx %2jx %2jx %2jd %-17s %-17s\n", + ELFU_INFO("%-17s %-15s %8x %9x %8x %2x %2x %2d %-17s %-17s\n", namestr, typestr, - ms->shdr.sh_addr, - ms->shdr.sh_offset, - ms->shdr.sh_size, - ms->shdr.sh_entsize, - ms->shdr.sh_flags, - ms->shdr.sh_addralign, + (unsigned)ms->shdr.sh_addr, + (unsigned)ms->shdr.sh_offset, + (unsigned)ms->shdr.sh_size, + (unsigned)ms->shdr.sh_entsize, + (unsigned)ms->shdr.sh_flags, + (unsigned)ms->shdr.sh_addralign, linkstr, infostr); } @@ -172,9 +172,9 @@ void elfu_mDumpEhdr(ElfuElf *me) ELFU_INFO(" e_type %8x\n", me->ehdr.e_type); ELFU_INFO(" e_machine %8x\n", me->ehdr.e_machine); ELFU_INFO(" e_version %8x\n", me->ehdr.e_version); - ELFU_INFO(" e_entry %8jx\n", me->ehdr.e_entry); - ELFU_INFO(" e_phoff %8jx\n", me->ehdr.e_phoff); - ELFU_INFO(" e_shoff %8jx\n", me->ehdr.e_shoff); + ELFU_INFO(" e_entry %8x\n", (unsigned)me->ehdr.e_entry); + ELFU_INFO(" e_phoff %8x\n", (unsigned)me->ehdr.e_phoff); + ELFU_INFO(" e_shoff %8x\n", (unsigned)me->ehdr.e_shoff); ELFU_INFO(" e_flags %8x\n", me->ehdr.e_flags); ELFU_INFO(" e_ehsize %8x\n", me->ehdr.e_ehsize); ELFU_INFO(" e_phentsize %8x\n", me->ehdr.e_phentsize); @@ -224,9 +224,9 @@ void elfu_mDumpElf(ElfuElf *me) ELFU_INFO("Orphaned sections:\n"); CIRCLEQ_FOREACH(ms, &me->orphanScnList, elemChildScn) { - ELFU_INFO(" * %-17s @ %8jx\n", + ELFU_INFO(" * %-17s @ %8x\n", elfu_mScnName(me, ms), - ms->shdr.sh_addr); + (unsigned)ms->shdr.sh_addr); } ELFU_INFO("\n"); diff --git a/src/modelops/fromFile.c b/src/modelops/fromFile.c index bc089bf..dd8b462 100644 --- a/src/modelops/fromFile.c +++ b/src/modelops/fromFile.c @@ -52,7 +52,7 @@ static void parseSymtab(ElfuElf *me, ElfuScn *ms, ElfuScn**origScnArr) CIRCLEQ_INSERT_TAIL(&ms->symtab.syms, newsym, elem); } } else { - // Unknown elfclass + /* Unknown elfclass */ assert(0); } @@ -126,11 +126,14 @@ static void parseRelatab64(ElfuScn *ms) static int cmpScnOffs(const void *ms1, const void *ms2) { + ElfuScn *s1 = *(ElfuScn**)ms1; + ElfuScn *s2 = *(ElfuScn**)ms2; + assert(ms1); assert(ms2); - ElfuScn *s1 = *(ElfuScn**)ms1; - ElfuScn *s2 = *(ElfuScn**)ms2; + s1 = *(ElfuScn**)ms1; + s2 = *(ElfuScn**)ms2; assert(s1); assert(s2); @@ -230,7 +233,7 @@ static ElfuScn* modelFromSection(Elf_Scn *scn) ms->data.d_buf = malloc(ms->shdr.sh_size); if (!ms->data.d_buf) { - ELFU_WARN("modelFromSection: malloc() failed for data buffer (%jx bytes).\n", ms->shdr.sh_size); + ELFU_WARN("modelFromSection: malloc() failed for data buffer (%x bytes).\n", (unsigned)ms->shdr.sh_size); goto ERROR; } @@ -246,7 +249,7 @@ static ElfuScn* modelFromSection(Elf_Scn *scn) if (data->d_off + data->d_size > ms->shdr.sh_size) { 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); + memcpy((char*)ms->data.d_buf + data->d_off, data->d_buf, data->d_size); } data = elf_rawdata(scn, data); @@ -427,12 +430,14 @@ ElfuElf* elfu_mFromElf(Elf *e) if (me->elfclass == ELFCLASS32) { parseReltab32(ms); } else if (me->elfclass == ELFCLASS64) { - // Not used on x86-64 + /* Not used on x86-64 */ + assert(0); } break; case SHT_RELA: if (me->elfclass == ELFCLASS32) { - // TODO + /* Not used on x86-32 */ + assert(0); } else if (me->elfclass == ELFCLASS64) { parseRelatab64(ms); } diff --git a/src/modelops/layout.c b/src/modelops/layout.c index e4b3fb1..8abc766 100644 --- a/src/modelops/layout.c +++ b/src/modelops/layout.c @@ -115,7 +115,7 @@ GElf_Addr elfu_mLayoutGetSpaceInPhdr(ElfuElf *me, GElf_Word size, GElf_Off endAddr = OFFS_END(last->phdr.p_vaddr, last->phdr.p_filesz); ElfuScn *ms; - ELFU_INFO("Expanding NOBITS at address 0x%jx...\n", endAddr); + ELFU_INFO("Expanding NOBITS at address 0x%x...\n", (unsigned)endAddr); CIRCLEQ_FOREACH(ms, &last->childScnList, elemChildScn) { if (ms->shdr.sh_offset == endOff) { @@ -124,7 +124,8 @@ GElf_Addr elfu_mLayoutGetSpaceInPhdr(ElfuElf *me, GElf_Word size, ms->data.d_buf = malloc(ms->shdr.sh_size); memset(ms->data.d_buf, '\0', ms->shdr.sh_size); if (!ms->data.d_buf) { - ELFU_WARN("mExpandNobits: Could not allocate %jd bytes for NOBITS expansion. Data may be inconsistent.\n", ms->shdr.sh_size); + ELFU_WARN("mExpandNobits: Could not allocate %u bytes for NOBITS expansion. Data may be inconsistent.\n", + (unsigned)ms->shdr.sh_size); assert(0); goto ERROR; } @@ -227,11 +228,14 @@ GElf_Addr elfu_mLayoutGetSpaceInPhdr(ElfuElf *me, GElf_Word size, static int cmpPhdrOffs(const void *mp1, const void *mp2) { + ElfuPhdr *p1; + ElfuPhdr *p2; + assert(mp1); assert(mp2); - ElfuPhdr *p1 = *(ElfuPhdr**)mp1; - ElfuPhdr *p2 = *(ElfuPhdr**)mp2; + p1 = *(ElfuPhdr**)mp1; + p2 = *(ElfuPhdr**)mp2; assert(p1); assert(p2); diff --git a/src/modelops/reladd.c b/src/modelops/reladd.c index fa8ffff..bc909e4 100644 --- a/src/modelops/reladd.c +++ b/src/modelops/reladd.c @@ -7,7 +7,7 @@ static int appendData(ElfuScn *ms, void *buf, size_t len) { - void *newbuf; + char *newbuf; assert(ms); assert(ms->shdr.sh_type != SHT_NOBITS); @@ -67,9 +67,9 @@ static ElfuScn* insertSection(ElfuElf *me, ElfuElf *mrel, ElfuScn *oldscn) goto ERROR; } - ELFU_INFO("Inserting %s at address 0x%jx...\n", + ELFU_INFO("Inserting %s at address 0x%x...\n", elfu_mScnName(mrel, oldscn), - injAddr); + (unsigned)injAddr); injOffset = injAddr - injPhdr->phdr.p_vaddr + injPhdr->phdr.p_offset; @@ -120,10 +120,10 @@ static ElfuScn* insertSection(ElfuElf *me, ElfuElf *mrel, ElfuScn *oldscn) return newscn; } else { - ELFU_WARN("insertSection: Skipping non-memory section %s (type %d flags %jd).\n", + ELFU_WARN("insertSection: Skipping non-memory section %s (type %d flags %u).\n", elfu_mScnName(mrel, oldscn), oldscn->shdr.sh_type, - oldscn->shdr.sh_flags); + (unsigned)oldscn->shdr.sh_flags); goto ERROR; } @@ -137,10 +137,10 @@ static ElfuScn* insertSection(ElfuElf *me, ElfuElf *mrel, ElfuScn *oldscn) static void* subScnAdd1(ElfuElf *mrel, ElfuScn *ms, void *aux1, void *aux2) { - (void)aux2; + ElfuScn *newscn; ElfuElf *me = (ElfuElf*)aux1; + (void)aux2; - ElfuScn *newscn; switch(ms->shdr.sh_type) { case SHT_PROGBITS: /* 1 */ @@ -166,9 +166,8 @@ static void* subScnAdd1(ElfuElf *mrel, ElfuScn *ms, void *aux1, void *aux2) static void* subScnAdd2(ElfuElf *mrel, ElfuScn *ms, void *aux1, void *aux2) { - (void)aux2; ElfuElf *me = (ElfuElf*)aux1; - (void)me; + (void)aux2; switch(ms->shdr.sh_type) { case SHT_NULL: /* 0 */ @@ -217,7 +216,7 @@ static void* subScnAdd2(ElfuElf *mrel, ElfuScn *ms, void *aux1, void *aux2) static void insertSymClone(ElfuElf *me, const ElfuScn *oldmsst, const ElfuSym *oldsym) { GElf_Xword newsize; - void *newbuf; + char *newbuf; ElfuScn *newscn = NULL; ElfuSym *newsym; char *oldsymname; diff --git a/src/modelops/relocate.c b/src/modelops/relocate.c index 90a319b..eefed02 100644 --- a/src/modelops/relocate.c +++ b/src/modelops/relocate.c @@ -13,9 +13,9 @@ void elfu_mRelocate(ElfuElf *metarget, ElfuScn *mstarget, ElfuScn *msrt) assert(mstarget); assert(msrt); - ELFU_DEBUG("Relocating in section of type %d size %jx\n", + ELFU_DEBUG("Relocating in section of type %u size %x\n", mstarget->shdr.sh_type, - mstarget->shdr.sh_size); + (unsigned)mstarget->shdr.sh_size); CIRCLEQ_FOREACH(rel, &msrt->reltab.rels, elem) { Elf32_Word *dest32 = (Elf32_Word*)(((char*)(mstarget->data.d_buf)) + rel->offset); @@ -41,12 +41,13 @@ void elfu_mRelocate(ElfuElf *metarget, ElfuScn *mstarget, ElfuScn *msrt) ELFU_DEBUG("Skipping relocation: Unknown type %d\n", rel->type); } } else if (metarget->elfclass == ELFCLASS64) { - /* x86-64 only uses RELA with explicit addend. */ - assert(rel->addendUsed); Elf64_Word a64 = rel->addend; Elf64_Addr p64 = mstarget->shdr.sh_addr + rel->offset; Elf64_Addr s64 = elfu_mSymtabLookupVal(metarget, msrt->linkptr, rel->sym); + /* x86-64 only uses RELA with explicit addend. */ + assert(rel->addendUsed); + switch(rel->type) { case R_X86_64_NONE: ELFU_DEBUG("Skipping relocation: R_386_NONE\n"); diff --git a/src/modelops/section.c b/src/modelops/section.c index a96377c..2675126 100644 --- a/src/modelops/section.c +++ b/src/modelops/section.c @@ -146,11 +146,14 @@ static void* subScnsToArray(ElfuElf *me, ElfuScn *ms, void *aux1, void *aux2) static int cmpScnOffs(const void *ms1, const void *ms2) { + ElfuScn *s1; + ElfuScn *s2; + assert(ms1); assert(ms2); - ElfuScn *s1 = *(ElfuScn**)ms1; - ElfuScn *s2 = *(ElfuScn**)ms2; + s1 = *(ElfuScn**)ms1; + s2 = *(ElfuScn**)ms2; assert(s1); assert(s2); @@ -167,12 +170,12 @@ static int cmpScnOffs(const void *ms1, const void *ms2) ElfuScn** elfu_mScnSortedByOffset(ElfuElf *me, size_t *count) { - assert(me); - size_t numSecs; ElfuScn **sortedSecs; size_t i; + assert(me); + /* Sort sections by offset in file */ numSecs = elfu_mScnCount(me); sortedSecs = malloc(numSecs * sizeof(*sortedSecs)); diff --git a/src/modelops/symtab.c b/src/modelops/symtab.c index a7c1485..e62871f 100644 --- a/src/modelops/symtab.c +++ b/src/modelops/symtab.c @@ -76,7 +76,7 @@ static GElf_Word pltLookupVal(ElfuElf *me, char *name) * from the start of the PLT, where j is the PLT entry and 16 is * the number of bytes the machine code in a PLT entry take. */ GElf_Addr addr = plt->shdr.sh_addr + (16 * j); - ELFU_DEBUG("dynsymLookupVal: Guessing symbol '%s' is in destination memory at %jx (PLT entry #%d).\n", name, addr, j); + ELFU_DEBUG("dynsymLookupVal: Guessing symbol '%s' is in destination memory at %x (PLT entry #%u).\n", name, (unsigned)addr, j); return addr; } } @@ -215,7 +215,7 @@ void elfu_mSymtabFlatten(ElfuElf *me) i++; } } else { - // Unknown elfclass + /* Unknown elfclass */ assert(0); } diff --git a/src/modelops/toFile.c b/src/modelops/toFile.c index 1fec8b0..368f12a 100644 --- a/src/modelops/toFile.c +++ b/src/modelops/toFile.c @@ -34,10 +34,10 @@ static void modelToPhdrs(ElfuElf *me, Elf *e) static void* modelToSection(ElfuElf *me, ElfuScn *ms, void *aux1, void *aux2) { + Elf_Scn *scnOut; + Elf *e = (Elf*)aux1; (void) me; (void) aux2; - Elf *e = (Elf*)aux1; - Elf_Scn *scnOut; scnOut = elf_newscn(e); if (!scnOut) {