1 /* This file is part of centaur.
3 * centaur is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License 2 as
5 * published by the Free Software Foundation.
7 * centaur is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with centaur. If not, see <http://www.gnu.org/licenses/>.
20 #include <libelfu/libelfu.h>
22 #include "elfhandle.h"
26 static void printUsage(char *progname)
28 printf("Usage: %s -i <inputfile> [OPTIONS]\n", progname);
30 "Options, executed in order given:\n"
31 " -h, --help Print this help message\n"
33 " -i, --input infile Load new ELF file (must be first command)\n"
35 " -c, --check Do a few sanity checks and print any errors\n"
36 " -d, --dump Dump current model state (debug only)\n"
37 " --reladd obj.o Insert object file contents\n"
38 " --detour from,to Write a jump to <to> at <from>\n"
39 " -o, --output outfile Where to write the modified ELF file to\n"
46 int main(int argc, char **argv)
48 ELFHandles hIn = { 0 };
49 int exitval = EXIT_SUCCESS;
50 char *progname = argv[0];
55 static struct option long_options[] = {
60 {"output", 1, 0, 'o'},
61 {"reladd", 1, 0, 10001},
62 {"detour", 1, 0, 10002},
73 /* Is libelf alive and well? */
74 if (elf_version(EV_CURRENT) == EV_NONE) {
75 fprintf(stderr, "libelf init error: %s\n", elf_errmsg(-1));
79 /* Parse and and execute user commands */
80 while ((c = getopt_long(argc, argv, "hcdi:o:",
81 long_options, &option_index)) != -1) {
90 printf("Checking model validity...\n");
103 elfu_mElfDestroy(me);
106 printf("Opening input file %s.\n", optarg);
107 openElf(&hIn, optarg, ELF_C_READ);
109 printf("Error: Failed to open input file. Aborting.\n");
110 exitval = EXIT_FAILURE;
114 me = elfu_mFromElf(hIn.e);
118 printf("Error: Failed to load model, aborting.\n");
126 ELFHandles hOut = { 0 };
128 printf("Writing modified file to %s.\n", optarg);
130 openElf(&hOut, optarg, ELF_C_WRITE);
132 printf("Failed to open output file. Aborting.\n");
134 exitval = EXIT_FAILURE;
138 elfu_mToElf(me, hOut.e);
140 if (elf_update(hOut.e, ELF_C_WRITE) < 0) {
141 fprintf(stderr, "elf_update() failed: %s\n", elf_errmsg(-1));
150 ELFHandles hRel = { 0 };
151 ElfuElf *mrel = NULL;
153 openElf(&hRel, optarg, ELF_C_READ);
155 printf("--reladd: Failed to open file for --reladd, aborting.\n");
160 mrel = elfu_mFromElf(hRel.e);
163 printf("--reladd: Failed to load model for --reladd, aborting.\n");
166 printf("--reladd: Injecting %s...\n", optarg);
167 if (elfu_mCheck(mrel)) {
168 printf("--reladd: Check for input file failed.\n");
169 elfu_mElfDestroy(mrel);
172 if (elfu_mReladd(me, mrel)) {
173 printf("--reladd: Failed.\n");
174 elfu_mElfDestroy(mrel);
177 printf("--reladd: Injected %s.\n", optarg);
178 elfu_mElfDestroy(mrel);
190 strtok_r(optarg, ",", &second);
191 printf("--detour: From '%s' to '%s'\n", optarg, second);
194 from = strtoul(optarg, NULL, 0);
196 from = elfu_mSymtabLookupAddrByName(me, me->symtab, optarg);
199 printf("--detour: Cannot parse argument 1, aborting.\n");
202 printf("--detour: From %x\n", (unsigned)from);
204 to = strtoul(second, NULL, 0);
206 to = elfu_mSymtabLookupAddrByName(me, me->symtab, second);
209 printf("--detour: Cannot parse argument 2, aborting.\n");
212 printf("--detour: To %x\n", (unsigned)to);
214 elfu_mDetour(me, from, to);
219 printUsage(progname);
224 while (optind < argc) {
232 elfu_mElfDestroy(me);
243 printf("Error: No input file opened. Aborting.\n");
246 exitval = EXIT_FAILURE;