1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/tools/reorder/mapaddrs.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,97 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +/* 1.9 + 1.10 + A program that reverse-maps a binary stream of address references to 1.11 + function names. 1.12 + 1.13 + */ 1.14 + 1.15 +#include <fstream> 1.16 +#include <unistd.h> 1.17 +#include <fcntl.h> 1.18 +#include <stdio.h> 1.19 + 1.20 +#include "elf_symbol_table.h" 1.21 + 1.22 +#define _GNU_SOURCE 1.23 +#include <getopt.h> 1.24 + 1.25 +static int 1.26 +map_addrs(int fd, elf_symbol_table &table) 1.27 +{ 1.28 + // Read the binary addresses from stdin. 1.29 + unsigned int buf[128]; 1.30 + ssize_t cb; 1.31 + 1.32 + while ((cb = read(fd, buf, sizeof buf)) > 0) { 1.33 + if (cb % sizeof buf[0]) 1.34 + fprintf(stderr, "unaligned read\n"); 1.35 + 1.36 + unsigned int *addr = buf; 1.37 + unsigned int *limit = buf + (cb / 4); 1.38 + 1.39 + for (; addr < limit; ++addr) { 1.40 + const Elf32_Sym *sym = table.lookup(*addr); 1.41 + if (sym) 1.42 + cout << table.get_symbol_name(sym) << endl; 1.43 + } 1.44 + } 1.45 + 1.46 + return 0; 1.47 +} 1.48 + 1.49 +static struct option long_options[] = { 1.50 + { "exe", required_argument, 0, 'e' }, 1.51 + { 0, 0, 0, 0 } 1.52 +}; 1.53 + 1.54 +static void 1.55 +usage() 1.56 +{ 1.57 + cerr << "usage: mapaddrs --exe=exe file ..." << endl; 1.58 +} 1.59 + 1.60 +int 1.61 +main(int argc, char *argv[]) 1.62 +{ 1.63 + elf_symbol_table table; 1.64 + 1.65 + while (1) { 1.66 + int option_index = 0; 1.67 + int c = getopt_long(argc, argv, "e:", long_options, &option_index); 1.68 + 1.69 + if (c < 0) 1.70 + break; 1.71 + 1.72 + switch (c) { 1.73 + case 'e': 1.74 + table.init(optarg); 1.75 + break; 1.76 + 1.77 + default: 1.78 + usage(); 1.79 + return 1; 1.80 + } 1.81 + } 1.82 + 1.83 + if (optind >= argc) { 1.84 + map_addrs(STDIN_FILENO, table); 1.85 + } 1.86 + else { 1.87 + do { 1.88 + int fd = open(argv[optind], O_RDONLY); 1.89 + if (fd < 0) { 1.90 + perror(argv[optind]); 1.91 + return 1; 1.92 + } 1.93 + 1.94 + map_addrs(fd, table); 1.95 + close(fd); 1.96 + } while (++optind < argc); 1.97 + } 1.98 + 1.99 + return 0; 1.100 +}