From 3afdca277b88f6b1ae9da81fe704a6a3844106d6 Mon Sep 17 00:00:00 2001 From: norly Date: Fri, 31 May 2013 19:01:40 +0100 Subject: [PATCH] Insert section names (with fixed prefix) Ideally, the filename of the original .o file being inserted would be used as a prefix for the sections copied over. However for now that prefix is simply "reladd". --- src/model/reladd.c | 54 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 52 insertions(+), 2 deletions(-) diff --git a/src/model/reladd.c b/src/model/reladd.c index 2de03dc..632c7c3 100644 --- a/src/model/reladd.c +++ b/src/model/reladd.c @@ -6,6 +6,30 @@ #include +static int appendData(ElfuScn *ms, void *buf, size_t len) +{ + void *newbuf; + + assert(ms); + assert(ms->shdr.sh_type != SHT_NOBITS); + assert(ms->data.d_buf); + + newbuf = realloc(ms->data.d_buf, ms->shdr.sh_size + len); + if (!newbuf) { + ELFU_WARN("appendData: malloc() failed for newbuf.\n"); + return 1; + } + + ms->data.d_buf = 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); + + return 0; +} + + static ElfuScn* insertSection(ElfuElf *me, ElfuElf *mrel, ElfuScn *oldscn) { ElfuScn *newscn = NULL; @@ -54,9 +78,35 @@ static ElfuScn* insertSection(ElfuElf *me, ElfuElf *mrel, ElfuScn *oldscn) } } + /* Inject name */ - // TODO - newscn->shdr.sh_name = 0; + if (me->shstrtab) { + char *newname; + size_t newnamelen; + + newnamelen = strlen("reladd") + 1; + if (elfu_mScnName(mrel, oldscn)) { + newnamelen += strlen(elfu_mScnName(mrel, oldscn)); + } + + newname = malloc(newnamelen); + strcpy(newname, "reladd"); + strcat(newname, elfu_mScnName(mrel, oldscn)); + + if (!newname) { + ELFU_WARN("insertSection: malloc() failed for newname. Leaving section name empty.\n"); + newscn->shdr.sh_name = 0; + } else { + size_t offset = me->shstrtab->shdr.sh_size; + + if (!appendData(me->shstrtab, newname, newnamelen)) { + newscn->shdr.sh_name = offset; + } + + free(newname); + } + } + // TODO: Relocate -- 2.30.2