1########################################################################
2#
3# Copyright (c) 2010, Secure Endpoints Inc.
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9#
10# - Redistributions of source code must retain the above copyright
11#   notice, this list of conditions and the following disclaimer.
12#
13# - Redistributions in binary form must reproduce the above copyright
14#   notice, this list of conditions and the following disclaimer in
15#   the documentation and/or other materials provided with the
16#   distribution.
17#
18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29# POSSIBILITY OF SUCH DAMAGE.
30#
31
32my $show_module_name = 1;
33my $use_indent = 1;
34my $strip_leading_underscore = 0;
35
36# Dump all symbols for the given object file that are defined and have
37# external scope.
38
39sub dump_symbols_for_file($)
40{
41    $fn = shift;
42
43    print STDERR "Opening dump of object [$fn]\n";
44
45    open(SP, '-|', "dumpbin /symbols \"".$fn."\"") or die "Can't open pipe for $fn";
46
47  LINE:
48    while (<SP>) {
49	# 008 00000000 SECT3  notype ()    External     | _encode_AccessDescription
50
51	/^[[:xdigit:]]{3,}\s[[:xdigit:]]{8,}\s(\w+)\s+\w*\s+(\(\)|  )\s+(\w+)\s+\|\s+([0-9a-zA-Z\@\_]+)$/ && do {
52	    my ($section, $type, $visibility, $symbol) = ($1, $2, $3, $4);
53
54	    if ($section ne "UNDEF" && $visibility eq "External") {
55		print $fn if $show_module_name;
56		print "\t" if $use_indent || $show_module_name;
57
58		if ($strip_leading_underscore && $symbol =~ /_(.*)/) {
59		    $symbol = $1;
60		}
61		if ($strip_leading_underscore && $symbol =~ /(.*)\@.*$/) {
62		    $symbol = $1;
63		}
64		print $symbol;
65                if ($type ne "()") {
66                    print "\tDATA";
67                }
68		print "\n";
69	    }
70	};
71    }
72
73    close SP;
74}
75
76sub use_response_file($)
77{
78    $fn = shift;
79
80    open (RF, '<', $fn) or die "Can't open response file $fn";
81
82    while (<RF>) {
83	/(\S+)/ && do {
84	    dump_symbols_for_file($1);
85	}
86    }
87    close RF;
88}
89
90for (@ARGV) {
91    ARG: {
92	/^-q$/ && do {
93	    $show_module_name = 0;
94	    last ARG;
95	};
96
97	/^-1$/ && do {
98	    $use_indent = 0;
99	    last ARG;
100	};
101
102	/^-u$/ && do {
103	    $strip_leading_underscore = 1;
104	    last ARG;
105	};
106
107	/^@(.*)$/ && do {
108	    use_response_file($1);
109	    last ARG;
110	};
111
112	dump_symbols_for_file($_);
113    }
114}
115