Object file injection, first part
[centaur.git] / src / main.c
index 9791feb477820de5b70c27866f3eb57620d8a622..2c6268c6cd9e5a56ae0f1aba511fa60a59f9e219 100644 (file)
@@ -2,8 +2,10 @@
 #include <stdlib.h>
 
 #include <getopt.h>
-#include <libelf.h>
-#include <gelf.h>
+#include <libelf/libelf.h>
+#include <libelf/gelf.h>
+
+#include <libelfu/libelfu.h>
 
 #include "elfhandle.h"
 #include "options.h"
@@ -16,6 +18,7 @@ int main(int argc, char **argv)
   ELFHandles hIn = { 0 };
   ELFHandles hOut = { 0 };
   int exitval = EXIT_SUCCESS;
+  ElfuElf *me;
 
   /* Is libelf alive and well? */
   if (elf_version(EV_CURRENT) == EV_NONE) {
@@ -34,16 +37,10 @@ int main(int argc, char **argv)
     goto EXIT;
   }
 
-  if (opts.fnOutput) {
-    openElf(&hOut, opts.fnOutput, ELF_C_WRITE);
-    if (!hOut.e) {
-      exitval = EXIT_FAILURE;
-      goto EXIT;
-    }
-  }
 
-
-  /* Now that we have a (hopefully) sane environment, execute commands */
+  /* Now that we have a (hopefully) sane environment, execute commands.
+   * Printing will have to be reimplemented based on the memory model.
+   */
   if (opts.printHeader) {
     printHeader(hIn.e);
   }
@@ -57,6 +54,73 @@ int main(int argc, char **argv)
   }
 
 
+  me = elfu_mFromElf(hIn.e);
+  if (me) {
+    closeElf(&hIn);
+    printf("Model successfully loaded.\n");
+    elfu_mCheck(me);
+    printf("Input model checked.\n");
+  } else {
+    printf("Failed to load model, aborting.\n");
+    goto EXIT;
+  }
+
+
+  /* Copy the input ELF to the output file if the latter is specified.
+   * Perform requested transformations on the memory model on-the-fly. */
+  if (!opts.fnOutput) {
+    printf("No output file specified - no further operations performed.\n");
+  } else {
+    if (opts.expandNobitsOffs) {
+      elfu_mExpandNobits(me, opts.expandNobitsOffs);
+    }
+
+    if (opts.insertBeforeSz) {
+      elfu_mInsertSpaceBefore(me, opts.insertBeforeOffs, opts.insertBeforeSz);
+    }
+
+    if (opts.insertAfterSz) {
+      elfu_mInsertSpaceAfter(me, opts.insertAfterOffs, opts.insertAfterSz);
+    }
+
+    if (opts.fnReladd) {
+      ELFHandles hRel = { 0 };
+      ElfuElf *mrel = NULL;
+
+      openElf(&hRel, opts.fnReladd, ELF_C_READ);
+      if (!hRel.e) {
+        printf("--reladd: Failed to open file for --reladd, skipping operation.\n");
+      } else {
+        mrel = elfu_mFromElf(hRel.e);
+        closeElf(&hRel);
+        if (!me) {
+          printf("--reladd: Failed to load model for --reladd, skipping operation.\n");
+        } else {
+          printf("--reladd: Model successfully loaded.\n");
+          elfu_mCheck(mrel);
+          printf("--reladd: Input model checked.\n");
+          elfu_mReladd(me, mrel);
+        }
+      }
+
+    }
+
+    elfu_mCheck(me);
+    printf("Output model checked.\n");
+
+
+    openElf(&hOut, opts.fnOutput, ELF_C_WRITE);
+    if (!hOut.e) {
+      printf("Failed to open output file. Aborting.\n");
+      exitval = EXIT_FAILURE;
+      goto EXIT;
+    }
+
+    elfu_mToElf(me, hOut.e);
+    printf("Model converted to ELF, ready to be written.\n");
+  }
+
+
 
 EXIT:
   if (hOut.e) {