Separate PLT lookup
[centaur.git] / src / libelfu / modelops / reladd.c
index 8c56d4ff7faa2af178b047598c7c74f46ae8e5db..bf3b8caab64ed0dc5ba8322281906003e9e365d8 100644 (file)
@@ -5,6 +5,37 @@
 #include <libelfu/libelfu.h>
 
 
+static ElfuScn* cloneScn(ElfuScn *ms)
+{
+  ElfuScn *newscn;
+
+  assert(ms);
+
+  newscn = elfu_mScnAlloc();
+  if (!newscn) {
+    return NULL;
+  }
+
+  newscn->shdr = ms->shdr;
+
+  if (ms->databuf) {
+    void *newbuf = malloc(ms->shdr.sh_size);
+    if (!newbuf) {
+      ELFU_WARN("cloneScn: Could not allocate memory for new data buffer.\n");
+      free(newscn);
+      return NULL;
+    }
+
+    memcpy(newbuf, ms->databuf, ms->shdr.sh_size);
+    newscn->databuf = newbuf;
+  }
+
+  newscn->oldptr = ms;
+
+  return newscn;
+}
+
+
 static int appendData(ElfuScn *ms, void *buf, size_t len)
 {
   char *newbuf;
@@ -35,8 +66,14 @@ static ElfuScn* insertSection(ElfuElf *me, ElfuElf *mrel, ElfuScn *oldscn)
   GElf_Off injOffset;
   ElfuPhdr *injPhdr;
 
-  if (oldscn->shdr.sh_flags & SHF_ALLOC) {
-    newscn = elfu_mCloneScn(oldscn);
+  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;
     }
@@ -74,13 +111,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;
         }
@@ -117,12 +155,6 @@ static ElfuScn* insertSection(ElfuElf *me, ElfuElf *mrel, ElfuScn *oldscn)
     }
 
     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:
@@ -170,23 +202,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 */
@@ -302,7 +330,7 @@ static void mergeSymtab(ElfuElf *me, const ElfuElf *mrel)
 
 
 
-void elfu_mReladd(ElfuElf *me, const ElfuElf *mrel)
+int elfu_mReladd(ElfuElf *me, const ElfuElf *mrel)
 {
   assert(me);
   assert(mrel);
@@ -313,8 +341,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;
 }