Insert section names (with fixed prefix)
authornorly <ny-git@enpas.org>
Fri, 31 May 2013 18:01:40 +0000 (19:01 +0100)
committernorly <ny-git@enpas.org>
Sat, 1 Jun 2013 00:04:57 +0000 (01:04 +0100)
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".

src/model/reladd.c

index 2de03dca43965c6b82a1311e26faea7856110eb4..632c7c368ecb9d605747fdfac86fbe62d88a76c9 100644 (file)
@@ -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