-
Notifications
You must be signed in to change notification settings - Fork 7
/
elfcat.c
336 lines (297 loc) · 8.69 KB
/
elfcat.c
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/* The MIT License (MIT)
Copyright (c) 2015-2016 Gabriel Corona
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <getopt.h>
#include <libelf.h>
#include <gelf.h>
// Genetic dump:
static int dump_zero(off_t size)
{
size_t buffer_size = 64*1024;
void* buffer = calloc(1, buffer_size);
while(size) {
size_t count = buffer_size < size ? buffer_size : size;
size_t wcount = write(STDOUT_FILENO, buffer, count);
if (wcount == -1) {
fprintf(stderr, "Could not write\n");
free(buffer);
return EXIT_FAILURE;
}
size -= wcount;
}
free(buffer);
return EXIT_SUCCESS;
}
static int dump_data(int fd, off_t offset, off_t size)
{
size_t buffer_size = 64*1024;
void* buffer = malloc(buffer_size);
off_t start = lseek(fd, 0, SEEK_CUR);
if (start == -1) {
fprintf(stderr, "Could not seek\n");
goto err_free;
}
if (lseek(fd, offset, SEEK_SET) == -1) {
fprintf(stderr, "Could not seek\n");
goto err_lseek;
}
while(size) {
size_t available_size = buffer_size < size ? buffer_size : size;
size_t rcount = read(fd, buffer, available_size);
if (rcount == -1) {
fprintf(stderr, "Could not read data\n");
goto err_lseek;
}
size -= rcount;
char* data = buffer;
while(rcount) {
size_t wcount = write(STDOUT_FILENO, data, rcount);
if (wcount == -1) {
fprintf(stderr, "Could not write data\n");
goto err_lseek;
}
rcount -= wcount;
data += wcount;
}
}
if (lseek(fd, start, SEEK_SET) == -1) {
fprintf(stderr, "Could not seek\n");
goto err_free;
}
free(buffer);
return EXIT_SUCCESS;
err_lseek:
lseek(fd, start, SEEK_SET);
err_free:
free(buffer);
return EXIT_FAILURE;
}
// Program entries:
static int dump_program_by_index(Elf* elf, int fd, size_t index)
{
GElf_Phdr phdr;
if (gelf_getphdr(elf, index, &phdr) != &phdr) {
fprintf(stderr, "Program entry not found\n");
return EXIT_FAILURE;
}
int res = dump_data(fd, phdr.p_offset, phdr.p_filesz);
if (res) {
fprintf(stderr, "Could not dump data from file\n");
return EXIT_FAILURE;
}
return dump_zero(phdr.p_memsz - phdr.p_filesz);
}
// Sections:
static int dump_section(int fd, Elf_Scn* scn)
{
GElf_Shdr shdr;
if (gelf_getshdr(scn , &shdr) != &shdr) {
fprintf(stderr, "Could not get section data\n");
return EXIT_FAILURE;
}
if (shdr.sh_type == SHT_NOBITS)
return dump_zero(shdr.sh_size);
return dump_data(fd, shdr.sh_offset, shdr.sh_size);
}
static int dump_section_by_index(Elf* elf, int fd, size_t index)
{
Elf_Scn* scn = elf_getscn(elf, index);
if (scn == NULL) {
fprintf(stderr, "Section not found\n");
return EXIT_FAILURE;
}
return dump_section(fd, scn);
}
static int dump_section_by_name(Elf* elf, int fd, const char* section_name)
{
Elf_Scn* scn = NULL;
GElf_Shdr shdr;
size_t shstrndx;
if (elf_getshdrstrndx(elf, &shstrndx) != 0) {
fprintf(stderr, "Could not find section name section\n");
return EXIT_FAILURE;
}
while ((scn = elf_nextscn(elf, scn)) != NULL) {
if (gelf_getshdr(scn , &shdr) != &shdr) {
fprintf(stderr, "Could not get section data\n");
return EXIT_FAILURE;
}
const char* scn_name = elf_strptr(elf, shstrndx , shdr.sh_name);
if (scn_name == NULL) {
fprintf(stderr, "Could not get section name\n");
return EXIT_FAILURE;
}
if (strcmp(scn_name, section_name) == 0)
return dump_section(fd, scn);
}
fprintf(stderr, "Section not found\n");
return EXIT_FAILURE;
}
// CLI:
enum cli_option {
CLI_OPTION_HELP = 500,
CLI_OPTION_SECTION_NAME = 501,
CLI_OPTION_SECTION_INDEX = 502,
CLI_OPTION_PROGRAM_INDEX = 503,
CLI_OPTION_VERSION = 504,
};
struct cli_action {
enum cli_option option;
const char* argument;
};
static int process_file(
char* elf_file_name, struct cli_action* actions, size_t actions_count)
{
int res = EXIT_SUCCESS;
int elf_fd = -1;
Elf* elf = NULL;
elf_fd = open(elf_file_name, O_RDONLY);
if (elf_fd == -1) {
fprintf(stderr, "Could not open file %s\n", elf_file_name);
res = EXIT_FAILURE;
goto on_failure;
}
elf = elf_begin(elf_fd, ELF_C_READ, NULL);
if (elf == NULL) {
fprintf(stderr, "Could read file %s as ELF\n (%s)",
elf_file_name, elf_errmsg(elf_errno()));
res = EXIT_FAILURE;
goto on_failure;
}
// Do each action:
for (size_t i = 0; i < actions_count ; ++i) {
switch (actions[i].option) {
case CLI_OPTION_SECTION_NAME:
if (dump_section_by_name(elf, elf_fd, actions[i].argument))
res = EXIT_FAILURE;
break;
case CLI_OPTION_SECTION_INDEX:
if (dump_section_by_index(elf, elf_fd, atoll(actions[i].argument)))
res = EXIT_FAILURE;
break;
case CLI_OPTION_PROGRAM_INDEX:
if (dump_program_by_index(elf, elf_fd, atoll(actions[i].argument)))
return EXIT_FAILURE;
break;
default:
fputs("Unexpected action\n", stderr);
abort();
break;
}
}
on_failure:
if (elf)
elf_end(elf);
if (elf_fd != -1)
close(elf_fd);
return res;
}
static int show_help(void)
{
fprintf(stderr, "elfcat [options] [elf-files]\n"
" --help This help\n"
" --version Version\n"
" --section-name .text Dump a section with a given name\n"
" --section-index 3 Dump a section with a given index\n"
" --program-index 3 Dump a program header with a given index\n");
return EXIT_SUCCESS;
}
static int show_version(void)
{
fprintf(stderr, PACKAGE_STRING "\n"
"Copyright 2015-2016 Gabriel Corona\n"
"MIT License\n");
return EXIT_SUCCESS;
}
int main(int argc, char** argv)
{
size_t actions_reserved = 5;
struct cli_action* actions;
size_t actions_count = 0;
actions = malloc(sizeof(struct cli_action) * actions_reserved);
if (actions == NULL) {
fputs("Allocation error\n", stderr);
abort();
}
// CLI processing:
while (1) {
static struct option long_options[] = {
{"help", no_argument, NULL, CLI_OPTION_HELP},
{"version", no_argument, NULL, CLI_OPTION_VERSION},
{"section-name", required_argument, NULL, CLI_OPTION_SECTION_NAME},
{"section-index", required_argument, NULL, CLI_OPTION_SECTION_INDEX},
{"program-index", required_argument, NULL, CLI_OPTION_PROGRAM_INDEX},
{0, 0, 0, 0}
};
int option = getopt_long(argc, argv, "", long_options, NULL);
if (option == -1)
break;
switch (option) {
case '?':
exit(EXIT_FAILURE);
break;
case CLI_OPTION_HELP:
return show_help();
case CLI_OPTION_VERSION:
return show_version();
case CLI_OPTION_SECTION_NAME:
case CLI_OPTION_SECTION_INDEX:
case CLI_OPTION_PROGRAM_INDEX:
if (actions_reserved == actions_count) {
actions_reserved = actions_reserved + actions_reserved / 2;
actions = realloc(actions, sizeof(struct cli_action) * actions_reserved);
if (actions == NULL) {
fputs("Allocation error\n", stderr);
abort();
}
}
actions[actions_count].option = option;
actions[actions_count].argument = optarg;
actions_count++;
break;
default:
fputs("?\n", stderr);
abort();
break;
}
}
// Initialize libelf:
if (elf_version(EV_CURRENT) == EV_NONE) {
fprintf(stderr, "Could not initialize libelf\n");
return EXIT_FAILURE;
}
// Process each file:
int res = EXIT_SUCCESS;
while (optind < argc)
if (process_file(argv[optind++], actions, actions_count))
res = EXIT_FAILURE;
free(actions);
return res;
}