Remove old and ugly printing functions
[centaur.git] / src / main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 #include <getopt.h>
5 #include <libelf/libelf.h>
6 #include <libelf/gelf.h>
7
8 #include <libelfu/libelfu.h>
9
10 #include "elfhandle.h"
11 #include "options.h"
12 #include "printing.h"
13
14
15 int main(int argc, char **argv)
16 {
17   CLIOpts opts = { 0 };
18   ELFHandles hIn = { 0 };
19   ELFHandles hOut = { 0 };
20   int exitval = EXIT_SUCCESS;
21   ElfuElf *me;
22
23   /* Is libelf alive and well? */
24   if (elf_version(EV_CURRENT) == EV_NONE) {
25     fprintf(stderr, "libelf init error: %s\n", elf_errmsg(-1));
26   }
27
28
29   /* Parse and validate user input */
30   parseOptions(&opts, argc, argv);
31
32
33   /* Open input/output files */
34   openElf(&hIn, opts.fnInput, ELF_C_READ);
35   if (!hIn.e) {
36     exitval = EXIT_FAILURE;
37     goto EXIT;
38   }
39
40
41   /* Now that we have a (hopefully) sane environment, execute commands. */
42   me = elfu_mFromElf(hIn.e);
43   if (me) {
44     closeElf(&hIn);
45     printf("Model successfully loaded.\n");
46
47     elfu_mDumpElf(me);
48
49     elfu_mCheck(me);
50     printf("Input model checked.\n");
51   } else {
52     printf("Failed to load model, aborting.\n");
53     goto EXIT;
54   }
55
56
57   /* Copy the input ELF to the output file if the latter is specified.
58    * Perform requested transformations on the memory model on-the-fly. */
59   if (!opts.fnOutput) {
60     printf("No output file specified - no further operations performed.\n");
61   } else {
62     if (opts.expandNobitsOffs) {
63       elfu_mExpandNobits(me, opts.expandNobitsOffs);
64     }
65
66     if (opts.insertBeforeSz) {
67       elfu_mInsertSpaceBefore(me, opts.insertBeforeOffs, opts.insertBeforeSz);
68     }
69
70     if (opts.insertAfterSz) {
71       elfu_mInsertSpaceAfter(me, opts.insertAfterOffs, opts.insertAfterSz);
72     }
73
74     if (opts.fnReladd) {
75       ELFHandles hRel = { 0 };
76       ElfuElf *mrel = NULL;
77
78       openElf(&hRel, opts.fnReladd, ELF_C_READ);
79       if (!hRel.e) {
80         printf("--reladd: Failed to open file for --reladd, skipping operation.\n");
81       } else {
82         mrel = elfu_mFromElf(hRel.e);
83         closeElf(&hRel);
84         if (!me) {
85           printf("--reladd: Failed to load model for --reladd, skipping operation.\n");
86         } else {
87           printf("--reladd: Model successfully loaded.\n");
88           elfu_mCheck(mrel);
89           printf("--reladd: Input model checked.\n");
90           elfu_mReladd(me, mrel);
91         }
92       }
93
94     }
95
96     elfu_mCheck(me);
97     printf("Output model checked.\n");
98
99
100     openElf(&hOut, opts.fnOutput, ELF_C_WRITE);
101     if (!hOut.e) {
102       printf("Failed to open output file. Aborting.\n");
103       exitval = EXIT_FAILURE;
104       goto EXIT;
105     }
106
107     elfu_mToElf(me, hOut.e);
108     printf("Model converted to ELF, ready to be written.\n");
109   }
110
111
112
113 EXIT:
114   if (hOut.e) {
115     if (elf_update(hOut.e, ELF_C_WRITE) < 0) {
116       fprintf(stderr, "elf_update() failed: %s\n", elf_errmsg(-1));
117     }
118     closeElf(&hOut);
119   }
120
121   if (hIn.e) {
122     closeElf(&hIn);
123   }
124
125   return (exitval);
126 }