GPLv2 release
[centaur.git] / src / libelfu / modelops / reladd.c
index bf3b8caab64ed0dc5ba8322281906003e9e365d8..fb6b10f2b87a998cecc901a0f23157c4ccee0bae 100644 (file)
@@ -1,3 +1,18 @@
+/* This file is part of centaur.
+ *
+ * centaur is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License 2 as
+ * published by the Free Software Foundation.
+
+ * centaur is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+
+ * You should have received a copy of the GNU General Public License
+ * along with centaur.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
 #include <assert.h>
 #include <stdlib.h>
 #include <string.h>
@@ -36,28 +51,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)
 {
@@ -128,7 +121,6 @@ static ElfuScn* insertSection(ElfuElf *me, ElfuElf *mrel, ElfuScn *oldscn)
 
     /* Inject name */
     if (me->shstrtab) {
-      char *newname;
       size_t newnamelen;
 
       newnamelen = strlen("reladd") + 1;
@@ -136,21 +128,15 @@ 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;
       }
     }
 
@@ -159,7 +145,7 @@ static ElfuScn* insertSection(ElfuElf *me, ElfuElf *mrel, ElfuScn *oldscn)
 
   ERROR:
   if (newscn) {
-    // TODO: Destroy newscn
+    elfu_mScnDestroy(newscn);
   }
   return NULL;
 }
@@ -262,8 +248,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));
@@ -326,6 +313,8 @@ static void mergeSymtab(ElfuElf *me, const ElfuElf *mrel)
   CIRCLEQ_FOREACH(sym, &mrel->symtab->symtab.syms, elem) {
     insertSymClone(me, mrel->symtab, sym);
   }
+
+  elfu_mSymtabFlatten(me);
 }