4 #include <libelfu/libelfu.h>
10 static void printUsage(char *progname)
12 printf("Usage: %s -i <inputfile> [OPTIONS]\n", progname);
14 "Options, executed in order given:\n"
15 " -h, --help Print this help message\n"
17 " -i, --input infile Load new ELF file (must be first command)\n"
19 " -c, --check Do a few sanity checks and print any errors\n"
20 " -d, --dump Dump current model state (debug only)\n"
21 " --reladd obj.o Insert object file contents\n"
22 " -o, --output outfile Where to write the modified ELF file to\n"
29 int main(int argc, char **argv)
31 ELFHandles hIn = { 0 };
32 int exitval = EXIT_SUCCESS;
33 char *progname = argv[0];
38 static struct option long_options[] = {
43 {"output", 1, 0, 'o'},
44 {"reladd", 1, 0, 10001},
55 /* Is libelf alive and well? */
56 if (elf_version(EV_CURRENT) == EV_NONE) {
57 fprintf(stderr, "libelf init error: %s\n", elf_errmsg(-1));
61 /* Parse and and execute user commands */
62 while ((c = getopt_long(argc, argv, "hcdi:o:",
63 long_options, &option_index)) != -1) {
72 printf("Checking model validity...\n");
84 printf("Opening input file %s.\n", optarg);
85 openElf(&hIn, optarg, ELF_C_READ);
87 printf("Error: Failed to open input file. Aborting.\n");
88 exitval = EXIT_FAILURE;
92 me = elfu_mFromElf(hIn.e);
96 printf("Error: Failed to load model, aborting.\n");
104 ELFHandles hOut = { 0 };
106 printf("Writing modified file to %s.\n", optarg);
108 openElf(&hOut, optarg, ELF_C_WRITE);
110 printf("Failed to open output file. Aborting.\n");
112 exitval = EXIT_FAILURE;
116 elfu_mToElf(me, hOut.e);
118 if (elf_update(hOut.e, ELF_C_WRITE) < 0) {
119 fprintf(stderr, "elf_update() failed: %s\n", elf_errmsg(-1));
128 ELFHandles hRel = { 0 };
129 ElfuElf *mrel = NULL;
131 openElf(&hRel, optarg, ELF_C_READ);
133 printf("--reladd: Failed to open file for --reladd, aborting.\n");
138 mrel = elfu_mFromElf(hRel.e);
141 printf("--reladd: Failed to load model for --reladd, aborting.\n");
144 printf("--reladd: Injecting %s...\n", optarg);
146 elfu_mReladd(me, mrel);
147 printf("--reladd: Injected %s.\n", optarg);
153 printUsage(progname);
158 while (optind < argc) {
173 printf("Error: No input file opened. Aborting.\n");
176 exitval = EXIT_FAILURE;