2c6268c6cd9e5a56ae0f1aba511fa60a59f9e219
[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    * Printing will have to be reimplemented based on the memory model.
43    */
44   if (opts.printHeader) {
45     printHeader(hIn.e);
46   }
47
48   if (opts.printSegments) {
49     printSegments(hIn.e);
50   }
51
52   if (opts.printSections) {
53     printSections(hIn.e);
54   }
55
56
57   me = elfu_mFromElf(hIn.e);
58   if (me) {
59     closeElf(&hIn);
60     printf("Model successfully loaded.\n");
61     elfu_mCheck(me);
62     printf("Input model checked.\n");
63   } else {
64     printf("Failed to load model, aborting.\n");
65     goto EXIT;
66   }
67
68
69   /* Copy the input ELF to the output file if the latter is specified.
70    * Perform requested transformations on the memory model on-the-fly. */
71   if (!opts.fnOutput) {
72     printf("No output file specified - no further operations performed.\n");
73   } else {
74     if (opts.expandNobitsOffs) {
75       elfu_mExpandNobits(me, opts.expandNobitsOffs);
76     }
77
78     if (opts.insertBeforeSz) {
79       elfu_mInsertSpaceBefore(me, opts.insertBeforeOffs, opts.insertBeforeSz);
80     }
81
82     if (opts.insertAfterSz) {
83       elfu_mInsertSpaceAfter(me, opts.insertAfterOffs, opts.insertAfterSz);
84     }
85
86     if (opts.fnReladd) {
87       ELFHandles hRel = { 0 };
88       ElfuElf *mrel = NULL;
89
90       openElf(&hRel, opts.fnReladd, ELF_C_READ);
91       if (!hRel.e) {
92         printf("--reladd: Failed to open file for --reladd, skipping operation.\n");
93       } else {
94         mrel = elfu_mFromElf(hRel.e);
95         closeElf(&hRel);
96         if (!me) {
97           printf("--reladd: Failed to load model for --reladd, skipping operation.\n");
98         } else {
99           printf("--reladd: Model successfully loaded.\n");
100           elfu_mCheck(mrel);
101           printf("--reladd: Input model checked.\n");
102           elfu_mReladd(me, mrel);
103         }
104       }
105
106     }
107
108     elfu_mCheck(me);
109     printf("Output model checked.\n");
110
111
112     openElf(&hOut, opts.fnOutput, ELF_C_WRITE);
113     if (!hOut.e) {
114       printf("Failed to open output file. Aborting.\n");
115       exitval = EXIT_FAILURE;
116       goto EXIT;
117     }
118
119     elfu_mToElf(me, hOut.e);
120     printf("Model converted to ELF, ready to be written.\n");
121   }
122
123
124
125 EXIT:
126   if (hOut.e) {
127     if (elf_update(hOut.e, ELF_C_WRITE) < 0) {
128       fprintf(stderr, "elf_update() failed: %s\n", elf_errmsg(-1));
129     }
130     closeElf(&hOut);
131   }
132
133   if (hIn.e) {
134     closeElf(&hIn);
135   }
136
137   return (exitval);
138 }