Redesign data structures, make basic reladd work.
[centaur.git] / src / main.c
index 34e757010e7d26c43fba6aac2c8076374805d073..a179fcd82a0097e57a0e960d31390d917757025f 100644 (file)
@@ -2,8 +2,8 @@
 #include <stdlib.h>
 
 #include <getopt.h>
-#include <libelf.h>
-#include <gelf.h>
+#include <libelf/libelf.h>
+#include <libelf/gelf.h>
 
 #include <libelfu/libelfu.h>
 
@@ -18,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) {
@@ -36,59 +37,63 @@ 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);
+  if (me) {
+    closeElf(&hIn);
+    printf("Model successfully loaded.\n");
 
-  /* 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);
-  }
-
-  if (opts.printSections) {
-    printSections(hIn.e);
+    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 */
-  if (opts.fnOutput) {
-    ElfuElf *me;
-
-    me = elfu_mFromElf(hIn.e);
-
-    if (me) {
-      printf("Model successfully loaded.\n");
-      elfu_mCheck(me);
-      printf("Input model checked.\n");
-
-      if (opts.expandNobitsOffs) {
-        elfu_mExpandNobits(me, opts.expandNobitsOffs);
+  /* 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.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);
+        }
       }
+    }
 
-      if (opts.insertBeforeSz) {
-        elfu_mInsertBefore(me, opts.insertBeforeOffs, opts.insertBeforeSz);
-      }
+    elfu_mCheck(me);
+    printf("Output model checked.\n");
 
-      if (opts.insertAfterSz) {
-        elfu_mInsertAfter(me, opts.insertAfterOffs, opts.insertAfterSz);
-      }
+    elfu_mDumpElf(me);
 
-      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);
+    printf("Model converted to ELF, ready to be written.\n");
   }