static int appendData(ElfuScn *ms, void *buf, size_t len)
{
- void *newbuf;
+ char *newbuf;
assert(ms);
assert(ms->shdr.sh_type != SHT_NOBITS);
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;
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;
}
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 */
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 */
case SHT_REL: /* 9 */
/* Relocate. */
- elfu_mRelocate32(me, elfu_mScnByOldscn(me, ms->infoptr), ms);
+ elfu_mRelocate(me, elfu_mScnByOldscn(me, ms->infoptr), ms);
break;
case SHT_RELA: /* 4 */
}
-void elfu_mReladd(ElfuElf *me, ElfuElf *mrel)
+
+static void insertSymClone(ElfuElf *me, const ElfuScn *oldmsst, const ElfuSym *oldsym)
+{
+ GElf_Xword newsize;
+ char *newbuf;
+ ElfuScn *newscn = NULL;
+ ElfuSym *newsym;
+ char *oldsymname;
+
+ assert(me);
+ assert(oldmsst);
+ assert(oldsym);
+
+ /* If the old symbol pointed to a section, find its clone in the target */
+ if (oldsym->scnptr) {
+ newscn = elfu_mScnByOldscn(me, oldsym->scnptr);
+
+ /* If we didn't copy the section referenced, we won't
+ * copy this symbol either */
+ if (!newscn) {
+ return;
+ }
+ }
+
+ // TODO: Allocate symtab if none present
+ assert(me->symtab);
+
+ /* Allocate memory for the cloned symbol */
+ newsym = malloc(sizeof(*newsym));
+ if (!newsym) {
+ ELFU_WARN("insertSymClone: malloc() failed for newsym.\n");
+ goto ERROR;
+ }
+
+ oldsymname = ELFU_SYMSTR(oldmsst, oldsym->name);
+
+ /* 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);
+ if (!newbuf) {
+ ELFU_WARN("insertSymClone: realloc() failed for strtab.\n");
+ goto ERROR;
+ }
+
+ me->symtab->linkptr->data.d_buf = 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;
+
+
+ /* Copy all other fields */
+ newsym->scnptr = newscn;
+ newsym->shndx = oldsym->shndx; /* If scnptr == NULL, this becomes relevant */
+ newsym->bind = oldsym->bind;
+ newsym->other = oldsym->other;
+ newsym->size = oldsym->size;
+ newsym->type = oldsym->type;
+ newsym->value = oldsym->value;
+
+ /* In executables, symbol addresses need to be in memory */
+ if (newscn) {
+ newsym->value += newscn->shdr.sh_addr;
+ }
+
+ /* Insert symbol */
+ CIRCLEQ_INSERT_TAIL(&me->symtab->symtab.syms, newsym, elem);
+
+ return;
+
+ ERROR:
+ if (newsym) {
+ free(newsym);
+ }
+}
+
+static void mergeSymtab(ElfuElf *me, const ElfuElf *mrel)
+{
+ ElfuSym *sym;
+
+ assert(me);
+ assert(mrel);
+
+ CIRCLEQ_FOREACH(sym, &mrel->symtab->symtab.syms, elem) {
+ insertSymClone(me, mrel->symtab, sym);
+ }
+}
+
+
+
+void elfu_mReladd(ElfuElf *me, const ElfuElf *mrel)
{
assert(me);
assert(mrel);
/* For each section in object file, guess how to insert it */
- elfu_mScnForall(mrel, subScnAdd1, me, NULL);
+ elfu_mScnForall((ElfuElf*)mrel, subScnAdd1, me, NULL);
+
+ mergeSymtab(me, mrel);
/* Do relocations and other stuff */
- elfu_mScnForall(mrel, subScnAdd2, me, NULL);
+ elfu_mScnForall((ElfuElf*)mrel, subScnAdd2, me, NULL);
/* Re-layout to accommodate new contents */
elfu_mLayoutAuto(me);