From 5d00dc2e4345796d79424a1ac19e0902009ade36 Mon Sep 17 00:00:00 2001 From: norly Date: Sun, 26 May 2013 12:52:47 +0100 Subject: [PATCH] Inserting section names, first part --- src/model/insert.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++ src/model/reladd.c | 10 ++++------ 2 files changed, 53 insertions(+), 6 deletions(-) diff --git a/src/model/insert.c b/src/model/insert.c index b18aa8a..1515706 100644 --- a/src/model/insert.c +++ b/src/model/insert.c @@ -287,3 +287,52 @@ void elfu_mInsertScnInChainAfter(ElfuElf *me, ElfuScn *oldscn, ElfuScn *newscn) CIRCLEQ_INSERT_AFTER(&me->scnList, oldscn, newscn, elem); } + + + + + +/* Insert a name into the section string table (.shstrtab) */ +Elf32_Word elfu_mInsertScnName(ElfuElf *me, char *prefix, char *name) +{ + void *newstrlen; + char *newstr; + void *newbuf; + + assert(me); + + if (!name || !me->shstrtab) { + return 0; + } + + assert(shstrtab->data.d_buf); + + newstrlen = strlen(name) + 1; + if (prefix) { + newstrlen += strlen(prefix); + } + + newbuf = realloc(me->shstrtab->data.d_buf, me->shstrtab->shdr.sh_size + newstrlen); + if (!newbuf) { + ELFU_WARN("mInsertScnName: malloc failed for newbuf.\n"); + free(newstr); + return 0; + } + + me->shstrtab->data.d_buf = newbuf; + + newstr = &newbuf[me->shstrtab->shdr.sh_size]; + if (prefix) { + strcpy(newstr, prefix); + newstr += strlen(prefix); + } + strcpy(newstr, name); + + me->shstrtab->shdr.sh_size += newstrlen; + + /* Delete section and reinsert it at the end to allow for the larger size */ + CIRCLEQ_REMOVE(me->scnList, me->shstrtab, elem); + CIRCLEQ_INSERT_TAIL(me->scnList, me->shstrtab, elem); + + // TODO: Move this section to the end and move following SHDR/PHDR tables +} diff --git a/src/model/reladd.c b/src/model/reladd.c index 41d7cfb..ae7bf52 100644 --- a/src/model/reladd.c +++ b/src/model/reladd.c @@ -6,6 +6,7 @@ typedef enum Destsegment { DS_UNKNOWN, + DS_OTHER, DS_TEXT, DS_DATA, } Destsegment; @@ -14,10 +15,8 @@ typedef enum Destsegment { static Destsegment destsegment(ElfuScn *ms) { if (!(ms->shdr.sh_flags & SHF_ALLOC)) { - return DS_UNKNOWN; - } - - if (!(ms->shdr.sh_flags & SHF_WRITE) + return DS_OTHER; + } else if (!(ms->shdr.sh_flags & SHF_WRITE) && (ms->shdr.sh_flags & SHF_EXECINSTR)) { return DS_TEXT; } else if ((ms->shdr.sh_flags & SHF_WRITE) @@ -150,8 +149,7 @@ static ElfuScn* insertSection(ElfuElf *me, ElfuElf *mrel, ElfuScn *ms) newscn->shdr.sh_offset = injOffset; /* Inject name */ - // TODO - newscn->shdr.sh_name = 0; + newscn->shdr.sh_name = elfu_mInsertScnName(me, "RELADD", elfu_mScnName(mrel, ms)); // TODO: Relocate -- 2.30.2