Inserting section names, first part shotgunparser
authornorly <ny-git@enpas.org>
Sun, 26 May 2013 11:52:47 +0000 (12:52 +0100)
committernorly <ny-git@enpas.org>
Sun, 26 May 2013 11:52:47 +0000 (12:52 +0100)
src/model/insert.c
src/model/reladd.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
+}
index 41d7cfba004a4a2bb146fbee058f772db8561dbc..ae7bf522fad244139bf07381afaf87ea3a4f9a4e 100644 (file)
@@ -6,6 +6,7 @@
 
 typedef enum Destsegment {
   DS_UNKNOWN,
+  DS_OTHER,
   DS_TEXT,
   DS_DATA,
 } Destsegment;
@@ -14,10 +15,8 @@ typedef enum Destsegment {
 static Destsegment destsegment(ElfuScn *ms)
 {
   if (!(ms->shdr.sh_flags & SHF_ALLOC)) {
-    return DS_UNKNOWN;
-  }
-
-  if (!(ms->shdr.sh_flags & SHF_WRITE)
+    return DS_OTHER;
+  } else if (!(ms->shdr.sh_flags & SHF_WRITE)
       && (ms->shdr.sh_flags & SHF_EXECINSTR)) {
     return DS_TEXT;
   } else if ((ms->shdr.sh_flags & SHF_WRITE)
@@ -150,8 +149,7 @@ static ElfuScn* insertSection(ElfuElf *me, ElfuElf *mrel, ElfuScn *ms)
         newscn->shdr.sh_offset = injOffset;
 
         /* Inject name */
-        // TODO
-        newscn->shdr.sh_name = 0;
+        newscn->shdr.sh_name = elfu_mInsertScnName(me, "RELADD", elfu_mScnName(mrel, ms));
 
         // TODO: Relocate