|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 /* |
|
6 |
|
7 A program that reverse-maps a binary stream of address references to |
|
8 function names. |
|
9 |
|
10 */ |
|
11 |
|
12 #include <fstream> |
|
13 #include <unistd.h> |
|
14 #include <fcntl.h> |
|
15 #include <stdio.h> |
|
16 |
|
17 #include "elf_symbol_table.h" |
|
18 |
|
19 #define _GNU_SOURCE |
|
20 #include <getopt.h> |
|
21 |
|
22 static int |
|
23 map_addrs(int fd, elf_symbol_table &table) |
|
24 { |
|
25 // Read the binary addresses from stdin. |
|
26 unsigned int buf[128]; |
|
27 ssize_t cb; |
|
28 |
|
29 while ((cb = read(fd, buf, sizeof buf)) > 0) { |
|
30 if (cb % sizeof buf[0]) |
|
31 fprintf(stderr, "unaligned read\n"); |
|
32 |
|
33 unsigned int *addr = buf; |
|
34 unsigned int *limit = buf + (cb / 4); |
|
35 |
|
36 for (; addr < limit; ++addr) { |
|
37 const Elf32_Sym *sym = table.lookup(*addr); |
|
38 if (sym) |
|
39 cout << table.get_symbol_name(sym) << endl; |
|
40 } |
|
41 } |
|
42 |
|
43 return 0; |
|
44 } |
|
45 |
|
46 static struct option long_options[] = { |
|
47 { "exe", required_argument, 0, 'e' }, |
|
48 { 0, 0, 0, 0 } |
|
49 }; |
|
50 |
|
51 static void |
|
52 usage() |
|
53 { |
|
54 cerr << "usage: mapaddrs --exe=exe file ..." << endl; |
|
55 } |
|
56 |
|
57 int |
|
58 main(int argc, char *argv[]) |
|
59 { |
|
60 elf_symbol_table table; |
|
61 |
|
62 while (1) { |
|
63 int option_index = 0; |
|
64 int c = getopt_long(argc, argv, "e:", long_options, &option_index); |
|
65 |
|
66 if (c < 0) |
|
67 break; |
|
68 |
|
69 switch (c) { |
|
70 case 'e': |
|
71 table.init(optarg); |
|
72 break; |
|
73 |
|
74 default: |
|
75 usage(); |
|
76 return 1; |
|
77 } |
|
78 } |
|
79 |
|
80 if (optind >= argc) { |
|
81 map_addrs(STDIN_FILENO, table); |
|
82 } |
|
83 else { |
|
84 do { |
|
85 int fd = open(argv[optind], O_RDONLY); |
|
86 if (fd < 0) { |
|
87 perror(argv[optind]); |
|
88 return 1; |
|
89 } |
|
90 |
|
91 map_addrs(fd, table); |
|
92 close(fd); |
|
93 } while (++optind < argc); |
|
94 } |
|
95 |
|
96 return 0; |
|
97 } |