Inserting section names, first part
[centaur.git] / src / model / insert.c
index b18aa8aed80ade86b52d5fb4a6df8274c142b646..15157062a500b8d3df726609dbfac9135087e060 100644 (file)
@@ -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
+}