diff options
author | norly <ny-git@enpas.org> | 2013-05-31 19:01:40 +0100 |
---|---|---|
committer | norly <ny-git@enpas.org> | 2013-06-01 01:04:57 +0100 |
commit | 3afdca277b88f6b1ae9da81fe704a6a3844106d6 (patch) | |
tree | 214915ac8e0da4e00fe46d6ef142516ccfb58c52 /src | |
parent | 93156d059749102d3dfef793f00c63c8c1c27ee0 (diff) |
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".
Diffstat (limited to 'src')
-rw-r--r-- | src/model/reladd.c | 54 |
1 files 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 <libelfu/libelfu.h> +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 |