4 #include <libelfu/libelfu.h>
11 int main(int argc, char **argv)
14 ELFHandles hIn = { 0 };
15 ELFHandles hOut = { 0 };
16 int exitval = EXIT_SUCCESS;
19 /* Is libelf alive and well? */
20 if (elf_version(EV_CURRENT) == EV_NONE) {
21 fprintf(stderr, "libelf init error: %s\n", elf_errmsg(-1));
25 /* Parse and validate user input */
26 parseOptions(&opts, argc, argv);
29 /* Open input/output files */
30 openElf(&hIn, opts.fnInput, ELF_C_READ);
32 exitval = EXIT_FAILURE;
36 /* Now that we have a (hopefully) sane environment, execute commands. */
37 me = elfu_mFromElf(hIn.e);
40 printf("Failed to load model, aborting.\n");
48 /* Perform requested transformations on the memory model on-the-fly. */
50 ELFHandles hRel = { 0 };
53 openElf(&hRel, opts.fnReladd, ELF_C_READ);
55 printf("--reladd: Failed to open file for --reladd, skipping operation.\n");
57 mrel = elfu_mFromElf(hRel.e);
60 printf("--reladd: Failed to load model for --reladd, skipping operation.\n");
63 elfu_mReladd(me, mrel);
64 printf("--reladd: Injected %s.\n", opts.fnReladd);
71 /* Copy the input ELF to the output file if one is specified. */
73 printf("Writing modified file to %s.\n", opts.fnOutput);
77 openElf(&hOut, opts.fnOutput, ELF_C_WRITE);
79 printf("Failed to open output file. Aborting.\n");
80 exitval = EXIT_FAILURE;
84 elfu_mToElf(me, hOut.e);
86 if (elf_update(hOut.e, ELF_C_WRITE) < 0) {
87 fprintf(stderr, "elf_update() failed: %s\n", elf_errmsg(-1));