9791feb477820de5b70c27866f3eb57620d8a622
[centaur.git] / src / main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 #include <getopt.h>
5 #include <libelf.h>
6 #include <gelf.h>
7
8 #include "elfhandle.h"
9 #include "options.h"
10 #include "printing.h"
11
12
13 int main(int argc, char **argv)
14 {
15   CLIOpts opts = { 0 };
16   ELFHandles hIn = { 0 };
17   ELFHandles hOut = { 0 };
18   int exitval = EXIT_SUCCESS;
19
20   /* Is libelf alive and well? */
21   if (elf_version(EV_CURRENT) == EV_NONE) {
22     fprintf(stderr, "libelf init error: %s\n", elf_errmsg(-1));
23   }
24
25
26   /* Parse and validate user input */
27   parseOptions(&opts, argc, argv);
28
29
30   /* Open input/output files */
31   openElf(&hIn, opts.fnInput, ELF_C_READ);
32   if (!hIn.e) {
33     exitval = EXIT_FAILURE;
34     goto EXIT;
35   }
36
37   if (opts.fnOutput) {
38     openElf(&hOut, opts.fnOutput, ELF_C_WRITE);
39     if (!hOut.e) {
40       exitval = EXIT_FAILURE;
41       goto EXIT;
42     }
43   }
44
45
46   /* Now that we have a (hopefully) sane environment, execute commands */
47   if (opts.printHeader) {
48     printHeader(hIn.e);
49   }
50
51   if (opts.printSegments) {
52     printSegments(hIn.e);
53   }
54
55   if (opts.printSections) {
56     printSections(hIn.e);
57   }
58
59
60
61 EXIT:
62   if (hOut.e) {
63     if (elf_update(hOut.e, ELF_C_WRITE) < 0) {
64       fprintf(stderr, "elf_update() failed: %s\n", elf_errmsg(-1));
65     }
66     closeElf(&hOut);
67   }
68
69   if (hIn.e) {
70     closeElf(&hIn);
71   }
72
73   return (exitval);
74 }