5 #include <libelfu/libelfu.h>
11 static void printUsage(char *progname)
13 printf("Usage: %s -i <inputfile> [OPTIONS]\n", progname);
15 "Options, executed in order given:\n"
16 " -h, --help Print this help message\n"
18 " -i, --input infile Load new ELF file (must be first command)\n"
20 " -c, --check Do a few sanity checks and print any errors\n"
21 " -d, --dump Dump current model state (debug only)\n"
22 " --reladd obj.o Insert object file contents\n"
23 " --detour from,to Write a jump to <to> at <from>\n"
24 " -o, --output outfile Where to write the modified ELF file to\n"
31 int main(int argc, char **argv)
33 ELFHandles hIn = { 0 };
34 int exitval = EXIT_SUCCESS;
35 char *progname = argv[0];
40 static struct option long_options[] = {
45 {"output", 1, 0, 'o'},
46 {"reladd", 1, 0, 10001},
47 {"detour", 1, 0, 10002},
58 /* Is libelf alive and well? */
59 if (elf_version(EV_CURRENT) == EV_NONE) {
60 fprintf(stderr, "libelf init error: %s\n", elf_errmsg(-1));
64 /* Parse and and execute user commands */
65 while ((c = getopt_long(argc, argv, "hcdi:o:",
66 long_options, &option_index)) != -1) {
75 printf("Checking model validity...\n");
91 printf("Opening input file %s.\n", optarg);
92 openElf(&hIn, optarg, ELF_C_READ);
94 printf("Error: Failed to open input file. Aborting.\n");
95 exitval = EXIT_FAILURE;
99 me = elfu_mFromElf(hIn.e);
103 printf("Error: Failed to load model, aborting.\n");
111 ELFHandles hOut = { 0 };
113 printf("Writing modified file to %s.\n", optarg);
115 openElf(&hOut, optarg, ELF_C_WRITE);
117 printf("Failed to open output file. Aborting.\n");
119 exitval = EXIT_FAILURE;
123 elfu_mToElf(me, hOut.e);
125 if (elf_update(hOut.e, ELF_C_WRITE) < 0) {
126 fprintf(stderr, "elf_update() failed: %s\n", elf_errmsg(-1));
135 ELFHandles hRel = { 0 };
136 ElfuElf *mrel = NULL;
138 openElf(&hRel, optarg, ELF_C_READ);
140 printf("--reladd: Failed to open file for --reladd, aborting.\n");
145 mrel = elfu_mFromElf(hRel.e);
148 printf("--reladd: Failed to load model for --reladd, aborting.\n");
151 printf("--reladd: Injecting %s...\n", optarg);
152 if (elfu_mCheck(mrel)) {
153 printf("--reladd: Check for input file failed.\n");
156 if (elfu_mReladd(me, mrel)) {
157 printf("--reladd: Failed.\n");
160 printf("--reladd: Injected %s.\n", optarg);
172 strtok_r(optarg, ",", &second);
173 printf("--detour: From '%s' to '%s'\n", optarg, second);
176 from = strtoul(optarg, NULL, 0);
178 from = elfu_mSymtabLookupAddrByName(me, me->symtab, optarg);
181 printf("--detour: Cannot parse argument 1, aborting.\n");
184 printf("--detour: From %x\n", (unsigned)from);
186 to = strtoul(second, NULL, 0);
188 to = elfu_mSymtabLookupAddrByName(me, me->symtab, second);
191 printf("--detour: Cannot parse argument 2, aborting.\n");
194 printf("--detour: To %x\n", (unsigned)to);
196 elfu_mDetour(me, from, to);
201 printUsage(progname);
206 while (optind < argc) {
214 elfu_mElfDestroy(me);
225 printf("Error: No input file opened. Aborting.\n");
228 exitval = EXIT_FAILURE;