summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/libelfu/model/phdr.c (renamed from src/libelfu/modelops/phdr.c)0
-rw-r--r--src/libelfu/model/section.c (renamed from src/libelfu/modelops/section.c)29
-rw-r--r--src/libelfu/model/symtab.c (renamed from src/libelfu/modelops/symtab.c)26
-rw-r--r--src/libelfu/modelops/clone.c18
-rw-r--r--src/libelfu/modelops/detour.c2
-rw-r--r--src/libelfu/modelops/fromFile.c53
-rw-r--r--src/libelfu/modelops/layout.c12
-rw-r--r--src/libelfu/modelops/reladd.c21
-rw-r--r--src/libelfu/modelops/relocate.c4
-rw-r--r--src/libelfu/modelops/toFile.c14
10 files changed, 82 insertions, 97 deletions
diff --git a/src/libelfu/modelops/phdr.c b/src/libelfu/model/phdr.c
index d26eb77..d26eb77 100644
--- a/src/libelfu/modelops/phdr.c
+++ b/src/libelfu/model/phdr.c
diff --git a/src/libelfu/modelops/section.c b/src/libelfu/model/section.c
index 2675126..c93c5c8 100644
--- a/src/libelfu/modelops/section.c
+++ b/src/libelfu/model/section.c
@@ -1,5 +1,6 @@
#include <assert.h>
#include <stdlib.h>
+#include <string.h>
#include <libelfu/libelfu.h>
@@ -124,11 +125,11 @@ char* elfu_mScnName(ElfuElf *me, ElfuScn *ms)
return NULL;
}
- if (!me->shstrtab->data.d_buf) {
+ if (!me->shstrtab->databuf) {
return NULL;
}
- return &((char*)me->shstrtab->data.d_buf)[ms->shdr.sh_name];
+ return &me->shstrtab->databuf[ms->shdr.sh_name];
}
@@ -194,3 +195,27 @@ ElfuScn** elfu_mScnSortedByOffset(ElfuElf *me, size_t *count)
return sortedSecs;
}
+
+
+
+/*
+ * Allocation, destruction
+ */
+
+ElfuScn* elfu_mScnAlloc()
+{
+ ElfuScn *ms;
+
+ ms = malloc(sizeof(ElfuScn));
+ if (!ms) {
+ ELFU_WARN("mScnCreate: malloc() failed for ElfuScn.\n");
+ return NULL;
+ }
+
+ memset(ms, 0, sizeof(*ms));
+
+ CIRCLEQ_INIT(&ms->symtab.syms);
+ CIRCLEQ_INIT(&ms->reltab.rels);
+
+ return ms;
+}
diff --git a/src/libelfu/modelops/symtab.c b/src/libelfu/model/symtab.c
index ef8443f..165e00e 100644
--- a/src/libelfu/modelops/symtab.c
+++ b/src/libelfu/model/symtab.c
@@ -210,19 +210,18 @@ void elfu_mSymtabFlatten(ElfuElf *me)
size_t newsize = (numsyms + 1) * sizeof(Elf32_Sym);
size_t i;
- if (me->symtab->data.d_buf) {
- free(me->symtab->data.d_buf);
+ if (me->symtab->databuf) {
+ free(me->symtab->databuf);
}
- me->symtab->data.d_buf = malloc(newsize);
- assert(me->symtab->data.d_buf);
+ me->symtab->databuf = malloc(newsize);
+ assert(me->symtab->databuf);
- me->symtab->data.d_size = newsize;
me->symtab->shdr.sh_size = newsize;
- memset(me->symtab->data.d_buf, 0, newsize);
+ memset(me->symtab->databuf, 0, newsize);
i = 1;
CIRCLEQ_FOREACH(sym, &me->symtab->symtab.syms, elem) {
- Elf32_Sym *es = ((Elf32_Sym*)me->symtab->data.d_buf) + i;
+ Elf32_Sym *es = ((Elf32_Sym*)me->symtab->databuf) + i;
es->st_name = sym->name;
es->st_value = sym->value;
@@ -237,19 +236,18 @@ void elfu_mSymtabFlatten(ElfuElf *me)
size_t newsize = (numsyms + 1) * sizeof(Elf64_Sym);
size_t i;
- if (me->symtab->data.d_buf) {
- free(me->symtab->data.d_buf);
+ if (me->symtab->databuf) {
+ free(me->symtab->databuf);
}
- me->symtab->data.d_buf = malloc(newsize);
- assert(me->symtab->data.d_buf);
+ me->symtab->databuf = malloc(newsize);
+ assert(me->symtab->databuf);
- me->symtab->data.d_size = newsize;
me->symtab->shdr.sh_size = newsize;
- memset(me->symtab->data.d_buf, 0, newsize);
+ memset(me->symtab->databuf, 0, newsize);
i = 1;
CIRCLEQ_FOREACH(sym, &me->symtab->symtab.syms, elem) {
- Elf64_Sym *es = ((Elf64_Sym*)me->symtab->data.d_buf) + i;
+ Elf64_Sym *es = ((Elf64_Sym*)me->symtab->databuf) + i;
es->st_name = sym->name;
es->st_value = sym->value;
diff --git a/src/libelfu/modelops/clone.c b/src/libelfu/modelops/clone.c
index 8f92919..8b9b0bc 100644
--- a/src/libelfu/modelops/clone.c
+++ b/src/libelfu/modelops/clone.c
@@ -9,33 +9,27 @@ ElfuScn* elfu_mCloneScn(ElfuScn *ms)
assert(ms);
- newscn = malloc(sizeof(ElfuScn));
+ newscn = elfu_mScnAlloc();
if (!newscn) {
ELFU_WARN("elfu_nCloneScn: Could not allocate memory for new ElfuScn.\n");
return NULL;
}
newscn->shdr = ms->shdr;
- newscn->data = ms->data;
- if (ms->data.d_buf) {
- void *newbuf = malloc(ms->data.d_size);
+
+ if (ms->databuf) {
+ void *newbuf = malloc(ms->shdr.sh_size);
if (!newbuf) {
ELFU_WARN("elfu_nCloneScn: Could not allocate memory for new data buffer.\n");
free(newscn);
return NULL;
}
- memcpy(newbuf, ms->data.d_buf, ms->data.d_size);
- newscn->data.d_buf = newbuf;
+ memcpy(newbuf, ms->databuf, ms->shdr.sh_size);
+ newscn->databuf = newbuf;
}
- newscn->linkptr = NULL;
- newscn->infoptr = NULL;
-
newscn->oldptr = ms;
- CIRCLEQ_INIT(&ms->symtab.syms);
- CIRCLEQ_INIT(&ms->reltab.rels);
-
return newscn;
}
diff --git a/src/libelfu/modelops/detour.c b/src/libelfu/modelops/detour.c
index 075d945..86ac46b 100644
--- a/src/libelfu/modelops/detour.c
+++ b/src/libelfu/modelops/detour.c
@@ -47,5 +47,5 @@ void elfu_mDetour(ElfuElf *me, GElf_Addr from, GElf_Addr to)
(unsigned)to);
*(Elf32_Word*)(detourcode + 1) = to - from - 5;
- memcpy((char*)ms->data.d_buf + scnoffset, detourcode, 5);
+ memcpy(ms->databuf + scnoffset, detourcode, 5);
}
diff --git a/src/libelfu/modelops/fromFile.c b/src/libelfu/modelops/fromFile.c
index dd8b462..9c25e29 100644
--- a/src/libelfu/modelops/fromFile.c
+++ b/src/libelfu/modelops/fromFile.c
@@ -11,13 +11,13 @@ static void parseSymtab(ElfuElf *me, ElfuScn *ms, ElfuScn**origScnArr)
size_t i;
assert(ms);
- assert(ms->data.d_buf);
+ assert(ms->databuf);
assert(origScnArr);
/* Parse symbols from their elfclass-specific format */
if (me->elfclass == ELFCLASS32) {
for (i = 1; (i + 1) * sizeof(Elf32_Sym) <= ms->shdr.sh_size; i++) {
- Elf32_Sym *cursym = ((Elf32_Sym*)ms->data.d_buf) + i;
+ Elf32_Sym *cursym = ((Elf32_Sym*)ms->databuf) + i;
ElfuSym *newsym = malloc(sizeof(*sym));
assert(newsym);
@@ -29,13 +29,11 @@ static void parseSymtab(ElfuElf *me, ElfuScn *ms, ElfuScn**origScnArr)
newsym->other = cursym->st_other;
newsym->shndx = cursym->st_shndx;
-
-
CIRCLEQ_INSERT_TAIL(&ms->symtab.syms, newsym, elem);
}
} else if (me->elfclass == ELFCLASS64) {
for (i = 1; (i + 1) * sizeof(Elf64_Sym) <= ms->shdr.sh_size; i++) {
- Elf64_Sym *cursym = ((Elf64_Sym*)ms->data.d_buf) + i;
+ Elf64_Sym *cursym = ((Elf64_Sym*)ms->databuf) + i;
ElfuSym *newsym = malloc(sizeof(*sym));
assert(newsym);
@@ -47,8 +45,6 @@ static void parseSymtab(ElfuElf *me, ElfuScn *ms, ElfuScn**origScnArr)
newsym->other = cursym->st_other;
newsym->shndx = cursym->st_shndx;
-
-
CIRCLEQ_INSERT_TAIL(&ms->symtab.syms, newsym, elem);
}
} else {
@@ -77,11 +73,11 @@ static void parseReltab32(ElfuScn *ms)
size_t i;
assert(ms);
- assert(ms->data.d_buf);
+ assert(ms->databuf);
for (i = 0; (i + 1) * sizeof(Elf32_Rel) <= ms->shdr.sh_size; i++) {
- Elf32_Rel *currel = &(((Elf32_Rel*)ms->data.d_buf)[i]);
+ Elf32_Rel *currel = &(((Elf32_Rel*)ms->databuf)[i]);
ElfuRel *rel;
rel = malloc(sizeof(*rel));
@@ -103,11 +99,11 @@ static void parseRelatab64(ElfuScn *ms)
size_t i;
assert(ms);
- assert(ms->data.d_buf);
+ assert(ms->databuf);
for (i = 0; (i + 1) * sizeof(Elf64_Rela) <= ms->shdr.sh_size; i++) {
- Elf64_Rela *currel = &(((Elf64_Rela*)ms->data.d_buf)[i]);
+ Elf64_Rela *currel = &(((Elf64_Rela*)ms->databuf)[i]);
ElfuRel *rel;
rel = malloc(sizeof(*rel));
@@ -210,29 +206,20 @@ static ElfuScn* modelFromSection(Elf_Scn *scn)
assert(scn);
- ms = malloc(sizeof(ElfuScn));
+ ms = elfu_mScnAlloc();
if (!ms) {
- ELFU_WARN("modelFromSection: malloc() failed for ElfuScn.\n");
goto ERROR;
}
-
+ /* Copy SHDR */
assert(gelf_getshdr(scn, &ms->shdr) == &ms->shdr);
-
/* Copy each data part in source segment */
- ms->data.d_align = 1;
- ms->data.d_buf = NULL;
- ms->data.d_off = 0;
- ms->data.d_type = ELF_T_BYTE;
- ms->data.d_size = ms->shdr.sh_size;
- ms->data.d_version = elf_version(EV_NONE);
- if (ms->shdr.sh_type != SHT_NOBITS
- && ms->shdr.sh_size > 0) {
+ if (SCNFILESIZE(&ms->shdr) > 0) {
Elf_Data *data;
- ms->data.d_buf = malloc(ms->shdr.sh_size);
- if (!ms->data.d_buf) {
+ ms->databuf = malloc(ms->shdr.sh_size);
+ if (!ms->databuf) {
ELFU_WARN("modelFromSection: malloc() failed for data buffer (%x bytes).\n", (unsigned)ms->shdr.sh_size);
goto ERROR;
}
@@ -241,30 +228,20 @@ static ElfuScn* modelFromSection(Elf_Scn *scn)
data = elf_rawdata(scn, NULL);
assert(data);
- ms->data.d_align = data->d_align;
- ms->data.d_type = data->d_type;
- ms->data.d_version = data->d_version;
+ /* elf_rawdata() always returns ELF_T_BYTE */
+ assert(data->d_type == ELF_T_BYTE);
while (data) {
if (data->d_off + data->d_size > ms->shdr.sh_size) {
ELFU_WARN("modelFromSection: libelf delivered a bogus data blob. Skipping\n");
} else {
- memcpy((char*)ms->data.d_buf + data->d_off, data->d_buf, data->d_size);
+ memcpy((char*)ms->databuf + data->d_off, data->d_buf, data->d_size);
}
data = elf_rawdata(scn, data);
}
}
- ms->linkptr = NULL;
- ms->infoptr = NULL;
-
- ms->oldptr = NULL;
-
- CIRCLEQ_INIT(&ms->symtab.syms);
- CIRCLEQ_INIT(&ms->reltab.rels);
-
-
return ms;
ERROR:
diff --git a/src/libelfu/modelops/layout.c b/src/libelfu/modelops/layout.c
index 8abc766..65a713d 100644
--- a/src/libelfu/modelops/layout.c
+++ b/src/libelfu/modelops/layout.c
@@ -121,21 +121,15 @@ GElf_Addr elfu_mLayoutGetSpaceInPhdr(ElfuElf *me, GElf_Word size,
if (ms->shdr.sh_offset == endOff) {
assert(ms->shdr.sh_type == SHT_NOBITS);
assert(ms->shdr.sh_size == nobitsize);
- ms->data.d_buf = malloc(ms->shdr.sh_size);
- memset(ms->data.d_buf, '\0', ms->shdr.sh_size);
- if (!ms->data.d_buf) {
+ ms->databuf = malloc(ms->shdr.sh_size);
+ memset(ms->databuf, '\0', ms->shdr.sh_size);
+ if (!ms->databuf) {
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;
}
- ms->data.d_align = 1;
- ms->data.d_off = 0;
- ms->data.d_type = ELF_T_BYTE;
- ms->data.d_size = ms->shdr.sh_size;
- ms->data.d_version = elf_version(EV_NONE);
-
ms->shdr.sh_type = SHT_PROGBITS;
ms->shdr.sh_addr = endAddr;
}
diff --git a/src/libelfu/modelops/reladd.c b/src/libelfu/modelops/reladd.c
index bc909e4..8c56d4f 100644
--- a/src/libelfu/modelops/reladd.c
+++ b/src/libelfu/modelops/reladd.c
@@ -11,19 +11,18 @@ static int appendData(ElfuScn *ms, void *buf, size_t len)
assert(ms);
assert(ms->shdr.sh_type != SHT_NOBITS);
- assert(ms->data.d_buf);
+ assert(ms->databuf);
- newbuf = realloc(ms->data.d_buf, ms->shdr.sh_size + len);
+ newbuf = realloc(ms->databuf, ms->shdr.sh_size + len);
if (!newbuf) {
ELFU_WARN("appendData: malloc() failed for newbuf.\n");
return 1;
}
- ms->data.d_buf = newbuf;
+ ms->databuf = newbuf;
memcpy(newbuf + ms->shdr.sh_size, buf, len);
ms->shdr.sh_size += len;
- ms->data.d_size += len;
- assert(ms->shdr.sh_size == ms->data.d_size);
+ assert(ms->shdr.sh_size == ms->shdr.sh_size);
return 0;
}
@@ -45,13 +44,12 @@ static ElfuScn* insertSection(ElfuElf *me, ElfuElf *mrel, ElfuScn *oldscn)
if (newscn->shdr.sh_type == SHT_NOBITS) {
/* Expand this to SHT_PROGBITS, then insert as such. */
- assert(!newscn->data.d_buf);
+ assert(!newscn->databuf);
- newscn->data.d_buf = malloc(newscn->shdr.sh_size);
- if (!newscn->data.d_buf) {
+ newscn->databuf = malloc(newscn->shdr.sh_size);
+ if (!newscn->databuf) {
goto ERROR;
}
- newscn->data.d_size = newscn->shdr.sh_size;
newscn->shdr.sh_type = SHT_PROGBITS;
}
@@ -250,19 +248,18 @@ static void insertSymClone(ElfuElf *me, const ElfuScn *oldmsst, const ElfuSym *o
/* Expand .strtab, append symbol name, link newsym to it */
newsize = me->symtab->linkptr->shdr.sh_size + strlen(oldsymname) + 1;
- newbuf = realloc(me->symtab->linkptr->data.d_buf, newsize);
+ newbuf = realloc(me->symtab->linkptr->databuf, newsize);
if (!newbuf) {
ELFU_WARN("insertSymClone: realloc() failed for strtab.\n");
goto ERROR;
}
- me->symtab->linkptr->data.d_buf = newbuf;
+ me->symtab->linkptr->databuf = newbuf;
newsym->name = me->symtab->linkptr->shdr.sh_size;
strcpy(newbuf + newsym->name, oldsymname);
- me->symtab->linkptr->data.d_size = newsize;
me->symtab->linkptr->shdr.sh_size = newsize;
diff --git a/src/libelfu/modelops/relocate.c b/src/libelfu/modelops/relocate.c
index eefed02..2eada04 100644
--- a/src/libelfu/modelops/relocate.c
+++ b/src/libelfu/modelops/relocate.c
@@ -18,8 +18,8 @@ void elfu_mRelocate(ElfuElf *metarget, ElfuScn *mstarget, ElfuScn *msrt)
(unsigned)mstarget->shdr.sh_size);
CIRCLEQ_FOREACH(rel, &msrt->reltab.rels, elem) {
- Elf32_Word *dest32 = (Elf32_Word*)(((char*)(mstarget->data.d_buf)) + rel->offset);
- Elf64_Word *dest64 = (Elf64_Word*)(((char*)(mstarget->data.d_buf)) + rel->offset);
+ Elf32_Word *dest32 = (Elf32_Word*)(mstarget->databuf + rel->offset);
+ Elf64_Word *dest64 = (Elf64_Word*)(mstarget->databuf + rel->offset);
if (metarget->elfclass == ELFCLASS32) {
diff --git a/src/libelfu/modelops/toFile.c b/src/libelfu/modelops/toFile.c
index 368f12a..ff01390 100644
--- a/src/libelfu/modelops/toFile.c
+++ b/src/libelfu/modelops/toFile.c
@@ -59,18 +59,18 @@ static void* modelToSection(ElfuElf *me, ElfuScn *ms, void *aux1, void *aux2)
/* Data */
- if (ms->data.d_buf) {
+ if (ms->databuf) {
Elf_Data *dataOut = elf_newdata(scnOut);
if (!dataOut) {
ELFU_WARNELF("elf_newdata");
}
- dataOut->d_align = ms->data.d_align;
- dataOut->d_buf = ms->data.d_buf;
- dataOut->d_off = ms->data.d_off;
- dataOut->d_type = ms->data.d_type;
- dataOut->d_size = ms->data.d_size;
- dataOut->d_version = ms->data.d_version;
+ dataOut->d_align = 1;
+ dataOut->d_buf = ms->databuf;
+ dataOut->d_off = 0;
+ dataOut->d_type = ELF_T_BYTE;
+ dataOut->d_size = ms->shdr.sh_size;
+ dataOut->d_version = elf_version(EV_NONE);
}
return NULL;