tools/reorder/mapaddrs.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     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/. */
     5 /*
     7   A program that reverse-maps a binary stream of address references to
     8   function names.
    10  */
    12 #include <fstream>
    13 #include <unistd.h>
    14 #include <fcntl.h>
    15 #include <stdio.h>
    17 #include "elf_symbol_table.h"
    19 #define _GNU_SOURCE
    20 #include <getopt.h>
    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;
    29     while ((cb = read(fd, buf, sizeof buf)) > 0) {
    30         if (cb % sizeof buf[0])
    31             fprintf(stderr, "unaligned read\n");
    33         unsigned int *addr = buf;
    34         unsigned int *limit = buf + (cb / 4);
    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     }
    43     return 0;
    44 }
    46 static struct option long_options[] = {
    47     { "exe", required_argument, 0, 'e' },
    48     { 0,     0,                 0, 0   }
    49 };
    51 static void
    52 usage()
    53 {
    54     cerr << "usage: mapaddrs --exe=exe file ..." << endl;
    55 }
    57 int
    58 main(int argc, char *argv[])
    59 {
    60     elf_symbol_table table;
    62     while (1) {
    63         int option_index = 0;
    64         int c = getopt_long(argc, argv, "e:", long_options, &option_index);
    66         if (c < 0)
    67             break;
    69         switch (c) {
    70         case 'e':
    71             table.init(optarg);
    72             break;
    74         default:
    75             usage();
    76             return 1;
    77         }
    78     }
    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             }
    91             map_addrs(fd, table);
    92             close(fd);
    93         } while (++optind < argc);
    94     }
    96     return 0;
    97 }

mercurial