Clean up main.c a bit
[centaur.git] / src / main.c
index 1e06d2998bca0642143bbb2deee1a51aaab14ed5..eda76e517d4798dc3431c2c8f45990b48c0ec73a 100644 (file)
@@ -1,10 +1,6 @@
 #include <stdio.h>
 #include <stdlib.h>
-
 #include <getopt.h>
-#include <libelf.h>
-#include <gelf.h>
-
 #include <libelfu/libelfu.h>
 
 #include "elfhandle.h"
@@ -18,6 +14,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) {
@@ -36,67 +33,65 @@ 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. */
+  me = elfu_mFromElf(hIn.e);
+  closeElf(&hIn);
+  if (!me) {
+    printf("Failed to load model, aborting.\n");
+    goto EXIT;
   }
 
+  elfu_mCheck(me);
 
-  /* Now that we have a (hopefully) sane environment, execute commands */
-  if (opts.printHeader) {
-    printHeader(hIn.e);
-  }
+  //elfu_mDumpElf(me);
 
-  if (opts.printSegments) {
-    printSegments(hIn.e);
-  }
+  /* Perform requested transformations on the memory model on-the-fly. */
+  if (opts.fnReladd) {
+    ELFHandles hRel = { 0 };
+    ElfuElf *mrel = NULL;
 
-  if (opts.printSections) {
-    printSections(hIn.e);
+    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 (!mrel) {
+        printf("--reladd: Failed to load model for --reladd, skipping operation.\n");
+      } else {
+        elfu_mCheck(mrel);
+        elfu_mReladd(me, mrel);
+        printf("--reladd: Injected %s.\n", opts.fnReladd);
+      }
+    }
   }
 
+  //elfu_mDumpElf(me);
 
-  /* Copy the input ELF to the output file if the latter is specified */
+  /* Copy the input ELF to the output file if one is specified. */
   if (opts.fnOutput) {
-    ElfuElf *me;
-
-    me = elfu_mFromElf(hIn.e);
+    printf("Writing modified file to %s.\n", opts.fnOutput);
+    elfu_mCheck(me);
 
-    if (me) {
-      printf("Model successfully loaded.\n");
-      elfu_mCheck(me);
-      printf("Input model checked.\n");
 
-      if (opts.insertBeforeSz) {
-        elfu_mInsertBefore(me, opts.insertBeforeOffs, opts.insertBeforeSz);
-      }
-
-      if (opts.insertAfterSz) {
-        elfu_mInsertAfter(me, opts.insertAfterOffs, opts.insertAfterSz);
-      }
-
-      elfu_mCheck(me);
-      printf("Output model checked.\n");
-      elfu_mToElf(me, hOut.e);
-      printf("Model converted to ELF, ready to be written.\n");
-    } else {
-      printf("Failed to load model.\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);
 
-
-EXIT:
-  if (hOut.e) {
     if (elf_update(hOut.e, ELF_C_WRITE) < 0) {
       fprintf(stderr, "elf_update() failed: %s\n", elf_errmsg(-1));
     }
     closeElf(&hOut);
   }
 
+
+
+EXIT:
   if (hIn.e) {
     closeElf(&hIn);
   }