Implement memory management TODOs
[centaur.git] / src / libelfu / modelops / reladd.c
index 6ab54e06096c24ae18e82f9c013b5ffd0a44d54b..4bc6e40aef84fc0a1a0a9ddae29f4b4a3c4549f1 100644 (file)
@@ -36,28 +36,6 @@ static ElfuScn* cloneScn(ElfuScn *ms)
 }
 
 
-static int appendData(ElfuScn *ms, void *buf, size_t len)
-{
-  char *newbuf;
-
-  assert(ms);
-  assert(ms->shdr.sh_type != SHT_NOBITS);
-  assert(ms->databuf);
-
-  newbuf = realloc(ms->databuf, ms->shdr.sh_size + len);
-  if (!newbuf) {
-    ELFU_WARN("appendData: malloc() failed for newbuf.\n");
-    return 1;
-  }
-
-  ms->databuf = newbuf;
-  memcpy(newbuf + ms->shdr.sh_size, buf, len);
-  ms->shdr.sh_size += len;
-  assert(ms->shdr.sh_size == ms->shdr.sh_size);
-
-  return 0;
-}
-
 
 static ElfuScn* insertSection(ElfuElf *me, ElfuElf *mrel, ElfuScn *oldscn)
 {
@@ -66,7 +44,13 @@ static ElfuScn* insertSection(ElfuElf *me, ElfuElf *mrel, ElfuScn *oldscn)
   GElf_Off injOffset;
   ElfuPhdr *injPhdr;
 
-  if (oldscn->shdr.sh_flags & SHF_ALLOC) {
+  if (!(oldscn->shdr.sh_flags & SHF_ALLOC)) {
+    ELFU_WARN("insertSection: Skipping non-memory section %s (type %d flags %u).\n",
+              elfu_mScnName(mrel, oldscn),
+              oldscn->shdr.sh_type,
+              (unsigned)oldscn->shdr.sh_flags);
+    goto ERROR;
+  } else {
     newscn = cloneScn(oldscn);
     if (!newscn) {
       return NULL;
@@ -105,13 +89,14 @@ static ElfuScn* insertSection(ElfuElf *me, ElfuElf *mrel, ElfuScn *oldscn)
     newscn->shdr.sh_addr = injAddr;
     newscn->shdr.sh_offset = injOffset;
 
+    /* Insert section in child list, ordered by memory address */
     if (CIRCLEQ_EMPTY(&injPhdr->childScnList)
-        || CIRCLEQ_LAST(&injPhdr->childScnList)->shdr.sh_offset < injOffset) {
+        || CIRCLEQ_LAST(&injPhdr->childScnList)->shdr.sh_addr < injAddr) {
       CIRCLEQ_INSERT_TAIL(&injPhdr->childScnList, newscn, elemChildScn);
     } else {
       ElfuScn *ms;
       CIRCLEQ_FOREACH(ms, &injPhdr->childScnList, elemChildScn) {
-        if (injOffset < ms->shdr.sh_offset) {
+        if (injAddr < ms->shdr.sh_addr) {
           CIRCLEQ_INSERT_BEFORE(&injPhdr->childScnList, ms, newscn, elemChildScn);
           break;
         }
@@ -121,7 +106,6 @@ static ElfuScn* insertSection(ElfuElf *me, ElfuElf *mrel, ElfuScn *oldscn)
 
     /* Inject name */
     if (me->shstrtab) {
-      char *newname;
       size_t newnamelen;
 
       newnamelen = strlen("reladd") + 1;
@@ -129,36 +113,24 @@ static ElfuScn* insertSection(ElfuElf *me, ElfuElf *mrel, ElfuScn *oldscn)
         newnamelen += strlen(elfu_mScnName(mrel, oldscn));
       }
 
-      newname = malloc(newnamelen);
+      char newname[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;
-        }
+      newscn->shdr.sh_name = me->shstrtab->shdr.sh_size;
 
-        free(newname);
+      if (elfu_mScnAppendData(me->shstrtab, newname, newnamelen)) {
+        newscn->shdr.sh_name = 0;
       }
     }
 
     return newscn;
-  } else {
-      ELFU_WARN("insertSection: Skipping non-memory section %s (type %d flags %u).\n",
-                elfu_mScnName(mrel, oldscn),
-                oldscn->shdr.sh_type,
-                (unsigned)oldscn->shdr.sh_flags);
-      goto ERROR;
   }
 
   ERROR:
   if (newscn) {
-    // TODO: Destroy newscn
+    elfu_mScnDestroy(newscn);
   }
   return NULL;
 }
@@ -201,23 +173,19 @@ static void* subScnAdd2(ElfuElf *mrel, ElfuScn *ms, void *aux1, void *aux2)
   switch(ms->shdr.sh_type) {
     case SHT_NULL: /* 0 */
     case SHT_PROGBITS: /* 1 */
+    case SHT_SYMTAB: /* 2 */
     case SHT_STRTAB: /* 3 */
     case SHT_NOBITS: /* 8 */
       break;
 
-
+    case SHT_RELA: /* 4 */
     case SHT_REL: /* 9 */
       /* Relocate. */
-      elfu_mRelocate(me, elfu_mScnByOldscn(me, ms->infoptr), ms);
+      if (elfu_mRelocate(me, elfu_mScnByOldscn(me, ms->infoptr), ms)) {
+        return (void*)-1;
+      }
       break;
 
-    case SHT_RELA: /* 4 */
-      // TODO: Needs a parser
-      //elfu_mRelocate(elfu_mScnByOldscn(me, ms->infoptr), ms);
-
-    case SHT_SYMTAB: /* 2 */
-      /* Merge with the existing table. Take care of string tables also. */
-
     /* The next section types either do not occur in .o files, or are
      * not strictly necessary to process here. */
     case SHT_NOTE: /* 7 */
@@ -265,8 +233,9 @@ static void insertSymClone(ElfuElf *me, const ElfuScn *oldmsst, const ElfuSym *o
     }
   }
 
-  // TODO: Allocate symtab if none present
-  assert(me->symtab);
+  /* If we don't have a symbol table, create one so we have somewhere to
+   * write our new symbols to. */
+  elfu_mSymtabAddGlobalDymtabIfNotPresent(me);
 
   /* Allocate memory for the cloned symbol */
   newsym = malloc(sizeof(*newsym));
@@ -329,11 +298,13 @@ static void mergeSymtab(ElfuElf *me, const ElfuElf *mrel)
   CIRCLEQ_FOREACH(sym, &mrel->symtab->symtab.syms, elem) {
     insertSymClone(me, mrel->symtab, sym);
   }
+
+  elfu_mSymtabFlatten(me);
 }
 
 
 
-void elfu_mReladd(ElfuElf *me, const ElfuElf *mrel)
+int elfu_mReladd(ElfuElf *me, const ElfuElf *mrel)
 {
   assert(me);
   assert(mrel);
@@ -344,8 +315,13 @@ void elfu_mReladd(ElfuElf *me, const ElfuElf *mrel)
   mergeSymtab(me, mrel);
 
   /* Do relocations and other stuff */
-  elfu_mScnForall((ElfuElf*)mrel, subScnAdd2, me, NULL);
+  if (elfu_mScnForall((ElfuElf*)mrel, subScnAdd2, me, NULL)) {
+    ELFU_WARN("elfu_mReladd: Reladd aborted. Target model is unclean.\n");
+    return -1;
+  }
 
   /* Re-layout to accommodate new contents */
   elfu_mLayoutAuto(me);
+
+  return 0;
 }