5 #include <libelf/libelf.h>
6 #include <libelf/gelf.h>
8 #include <libelfu/libelfu.h>
10 #include "elfhandle.h"
15 int main(int argc, char **argv)
18 ELFHandles hIn = { 0 };
19 ELFHandles hOut = { 0 };
20 int exitval = EXIT_SUCCESS;
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));
29 /* Parse and validate user input */
30 parseOptions(&opts, argc, argv);
33 /* Open input/output files */
34 openElf(&hIn, opts.fnInput, ELF_C_READ);
36 exitval = EXIT_FAILURE;
41 /* Now that we have a (hopefully) sane environment, execute commands.
42 * Printing will have to be reimplemented based on the memory model.
44 if (opts.printHeader) {
48 if (opts.printSegments) {
52 if (opts.printSections) {
57 me = elfu_mFromElf(hIn.e);
60 printf("Model successfully loaded.\n");
62 printf("Input model checked.\n");
64 printf("Failed to load model, aborting.\n");
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. */
72 printf("No output file specified - no further operations performed.\n");
74 if (opts.expandNobitsOffs) {
75 elfu_mExpandNobits(me, opts.expandNobitsOffs);
78 if (opts.insertBeforeSz) {
79 elfu_mInsertSpaceBefore(me, opts.insertBeforeOffs, opts.insertBeforeSz);
82 if (opts.insertAfterSz) {
83 elfu_mInsertSpaceAfter(me, opts.insertAfterOffs, opts.insertAfterSz);
87 ELFHandles hRel = { 0 };
90 openElf(&hRel, opts.fnReladd, ELF_C_READ);
92 printf("--reladd: Failed to open file for --reladd, skipping operation.\n");
94 mrel = elfu_mFromElf(hRel.e);
97 printf("--reladd: Failed to load model for --reladd, skipping operation.\n");
99 printf("--reladd: Model successfully loaded.\n");
101 printf("--reladd: Input model checked.\n");
102 elfu_mReladd(me, mrel);
109 printf("Output model checked.\n");
112 openElf(&hOut, opts.fnOutput, ELF_C_WRITE);
114 printf("Failed to open output file. Aborting.\n");
115 exitval = EXIT_FAILURE;
119 elfu_mToElf(me, hOut.e);
120 printf("Model converted to ELF, ready to be written.\n");
127 if (elf_update(hOut.e, ELF_C_WRITE) < 0) {
128 fprintf(stderr, "elf_update() failed: %s\n", elf_errmsg(-1));