summaryrefslogtreecommitdiff
path: root/src/elfucli.c
blob: f6bac3fcd7314469f05d6d16b018154293f762e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/* This file is part of centaur.
 *
 * centaur is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License 2 as
 * published by the Free Software Foundation.

 * centaur is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with centaur.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <libelfu/libelfu.h>

#include "elfhandle.h"



static void printUsage(char *progname)
{
  printf("Usage: %s -i <inputfile> [OPTIONS]\n", progname);
  printf("\n"
         "Options, executed in order given:\n"
         "  -h, --help                     Print this help message\n"
         "\n"
         "  -i, --input         infile     Load new ELF file (must be first command)\n"
         "\n"
         "  -c, --check                    Do a few sanity checks and print any errors\n"
         "  -d, --dump                     Dump current model state (debug only)\n"
         "      --reladd        obj.o      Insert object file contents\n"
         "      --detour        from,to    Write a jump to <to> at <from>\n"
         "  -o, --output        outfile    Where to write the modified ELF file to\n"
         "\n");
}




int main(int argc, char **argv)
{
  ELFHandles hIn = { 0 };
  int exitval = EXIT_SUCCESS;
  char *progname = argv[0];
  int c;
  int option_index = 0;
  ElfuElf *me = NULL;

  static struct option long_options[] = {
    {"help", 0, 0, 'h'},
    {"check", 0, 0, 'c'},
    {"dump", 0, 0, 'd'},
    {"input", 1, 0, 'i'},
    {"output", 1, 0, 'o'},
    {"reladd", 1, 0, 10001},
    {"detour", 1, 0, 10002},
    {NULL, 0, NULL, 0}
  };


  if (argc < 3) {
    printUsage(progname);
    goto EXIT;
  }


  /* Is libelf alive and well? */
  if (elf_version(EV_CURRENT) == EV_NONE) {
    fprintf(stderr, "libelf init error: %s\n", elf_errmsg(-1));
  }


  /* Parse and and execute user commands */
  while ((c = getopt_long(argc, argv, "hcdi:o:",
                          long_options, &option_index)) != -1) {
    switch (c) {
      case 'h':
        printUsage(progname);
        goto EXIT;
      case 'c':
        if (!me) {
          goto ERR_NO_INPUT;
        } else {
          printf("Checking model validity...\n");
          elfu_mCheck(me);
        }
        break;
      case 'd':
        if (!me) {
          goto ERR_NO_INPUT;
        } else {
          elfu_mDumpElf(me);
        }
        break;
      case 'i':
        if (me) {
          elfu_mElfDestroy(me);
        }

        printf("Opening input file %s.\n", optarg);
        openElf(&hIn, optarg, ELF_C_READ);
        if (!hIn.e) {
          printf("Error: Failed to open input file. Aborting.\n");
          exitval = EXIT_FAILURE;
          goto EXIT;
        }

        me = elfu_mFromElf(hIn.e);
        closeElf(&hIn);

        if (!me) {
          printf("Error: Failed to load model, aborting.\n");
          goto EXIT;
        }
        break;
      case 'o':
        if (!me) {
          goto ERR_NO_INPUT;
        } else {
          ELFHandles hOut = { 0 };

          printf("Writing modified file to %s.\n", optarg);

          openElf(&hOut, optarg, ELF_C_WRITE);
          if (!hOut.e) {
            printf("Failed to open output file. Aborting.\n");
            closeElf(&hOut);
            exitval = EXIT_FAILURE;
            goto EXIT;
          }

          elfu_mToElf(me, hOut.e);

          if (elf_update(hOut.e, ELF_C_WRITE) < 0) {
            fprintf(stderr, "elf_update() failed: %s\n", elf_errmsg(-1));
          }
          closeElf(&hOut);
        }
        break;
      case 10001:
        if (!me) {
          goto ERR_NO_INPUT;
        } else {
          ELFHandles hRel = { 0 };
          ElfuElf *mrel = NULL;

          openElf(&hRel, optarg, ELF_C_READ);
          if (!hRel.e) {
            printf("--reladd: Failed to open file for --reladd, aborting.\n");
            closeElf(&hRel);
            goto ERR;
          }

          mrel = elfu_mFromElf(hRel.e);
          closeElf(&hRel);
          if (!mrel) {
            printf("--reladd: Failed to load model for --reladd, aborting.\n");
            goto ERR;
          } else {
            printf("--reladd: Injecting %s...\n", optarg);
            if (elfu_mCheck(mrel)) {
              printf("--reladd: Check for input file failed.\n");
              elfu_mElfDestroy(mrel);
              goto ERR;
            }
            if (elfu_mReladd(me, mrel)) {
              printf("--reladd: Failed.\n");
              elfu_mElfDestroy(mrel);
              goto ERR;
            }
            printf("--reladd: Injected %s.\n", optarg);
            elfu_mElfDestroy(mrel);
          }
        }
        break;
      case 10002:
        if (!me) {
          goto ERR_NO_INPUT;
        } else {
          GElf_Addr from;
          GElf_Addr to;
          char *second;

          strtok_r(optarg, ",", &second);
          printf("--detour: From '%s' to '%s'\n", optarg, second);


          from = strtoul(optarg, NULL, 0);
          if (from == 0) {
            from = elfu_mSymtabLookupAddrByName(me, me->symtab, optarg);
          }
          if (from == 0) {
            printf("--detour: Cannot parse argument 1, aborting.\n");
            goto ERR;
          }
          printf("--detour: From %x\n", (unsigned)from);

          to = strtoul(second, NULL, 0);
          if (to == 0) {
            to = elfu_mSymtabLookupAddrByName(me, me->symtab, second);
          }
          if (to == 0) {
            printf("--detour: Cannot parse argument 2, aborting.\n");
            goto ERR;
          }
          printf("--detour: To %x\n", (unsigned)to);

          elfu_mDetour(me, from, to);
        }
        break;
      case '?':
      default:
        printUsage(progname);
        goto EXIT;
    }
  }

  while (optind < argc) {
    optind++;
  }



EXIT:
  if (me) {
    elfu_mElfDestroy(me);
  }

  if (hIn.e) {
    closeElf(&hIn);
  }

  return (exitval);


ERR_NO_INPUT:
  printf("Error: No input file opened. Aborting.\n");

ERR:
  exitval = EXIT_FAILURE;
  goto EXIT;
}