|
1 | 1 | my@def;
|
2 | 2 |
|
| 3 | +use warnings; |
| 4 | +use strict; |
| 5 | +use 5.8.0; |
| 6 | +use List::Utilqw(max); |
| 7 | + |
3 | 8 | #
|
4 | 9 | # Script that generates a .DEF file for all objects in a directory
|
5 | 10 | #
|
6 | 11 | # src/tools/msvc/gendef.pl
|
7 | 12 | #
|
8 | 13 |
|
9 |
| -die"Usage: gendef.pl <modulepath> <platform>\n" |
10 |
| -unless (($ARGV[0] =~/\\([^\\]+$)/) |
11 |
| -&& ($ARGV[1] =='Win32' ||$ARGV[1] =='x64')); |
| 14 | +subdumpsyms |
| 15 | +{ |
| 16 | +my ($objfile,$symfile) =@_; |
| 17 | +system("dumpbin /symbols /out:symbols.out$_ >NUL") |
| 18 | + &&die"Could not call dumpbin"; |
| 19 | +rename("symbols.out",$symfile); |
| 20 | +} |
| 21 | + |
| 22 | +# Given a symbol file path, loops over its contents |
| 23 | +# and returns a list of symbols of interest as a dictionary |
| 24 | +# of 'symbolname' -> symtype, where symtype is: |
| 25 | +# |
| 26 | +# 0 a CODE symbol, left undecorated in the .DEF |
| 27 | +# 1 A DATA symbol, i.e. global var export |
| 28 | +# |
| 29 | +subextract_syms |
| 30 | +{ |
| 31 | +my ($symfile,$def) =@_; |
| 32 | +open(F,"<$symfile") ||die"Could not open$symfile for$_\n"; |
| 33 | +while (<F>) |
| 34 | + { |
| 35 | +# Expected symbol lines look like: |
| 36 | +# |
| 37 | +# 0 1 2 3 4 5 6 |
| 38 | +# IDX SYMBOL SECT SYMTYPE SYMSTATIC SYMNAME |
| 39 | +# ------------------------------------------------------------------------ |
| 40 | +# 02E 00000130 SECTA notype External | _standbyState |
| 41 | +# 02F 00000009 SECT9 notype Static | _LocalRecoveryInProgress |
| 42 | +# 064 00000020 SECTC notype () Static | _XLogCheckBuffer |
| 43 | +# 065 00000000 UNDEF notype () External | _BufferGetTag |
| 44 | +# |
| 45 | +# See http://msdn.microsoft.com/en-us/library/b842y285.aspx |
| 46 | +# |
| 47 | +# We're not interested in the symbol index or offset. |
| 48 | +# |
| 49 | +# SECT[ION] is only examined to see whether the symbol is defined in a |
| 50 | +# COFF section of the local object file; if UNDEF, it's a symbol to be |
| 51 | +# resolved at link time from another object so we can't export it. |
| 52 | +# |
| 53 | +# SYMTYPE is always notype for C symbols as there's no typeinfo and no |
| 54 | +# way to get the symbol type from name (de)mangling. However, we care |
| 55 | +# if "notype" is suffixed by "()" or not. The presence of () means the |
| 56 | +# symbol is a function, the absence means it isn't. |
| 57 | +# |
| 58 | +# SYMSTATIC indicates whether it's a compilation-unit local "static" |
| 59 | +# symbol ("Static"), or whether it's available for use from other |
| 60 | +# compilation units ("External"). We export all symbols that aren't |
| 61 | +# static as part of the whole program DLL interface to produce UNIX-like |
| 62 | +# default linkage. |
| 63 | +# |
| 64 | +# SYMNAME is, obviously, the symbol name. The leading underscore |
| 65 | +# indicates that the _cdecl calling convention is used. See |
| 66 | +# http://www.unixwiz.net/techtips/win32-callconv.html |
| 67 | +# http://www.codeproject.com/Articles/1388/Calling-Conventions-Demystified |
| 68 | +# |
| 69 | +s/notype\(\)/func/g; |
| 70 | +s/notype/data/g; |
| 71 | + |
| 72 | +my@pieces =split; |
| 73 | +# Skip file and section headers and other non-symbol entries |
| 74 | +nextunlessdefined($pieces[0])and$pieces[0] =~/^[A-F0-9]{3,}$/; |
| 75 | +# Skip blank symbol names |
| 76 | +nextunless$pieces[6]; |
| 77 | +# Skip externs used from another compilation unit |
| 78 | +nextif ($pieces[2]eq"UNDEF"); |
| 79 | +# Skip static symbols |
| 80 | +nextunless ($pieces[4]eq"External"); |
| 81 | +# Skip some more MSVC-generated crud |
| 82 | +nextif$pieces[6] =~/^@/; |
| 83 | +nextif$pieces[6] =~/^\(/; |
| 84 | +# __real and __xmm are out-of-line floating point literals and |
| 85 | +# (for __xmm) their SIMD equivalents. They shouldn't be part |
| 86 | +# of the DLL interface. |
| 87 | +nextif$pieces[6] =~/^__real/; |
| 88 | +nextif$pieces[6] =~/^__xmm/; |
| 89 | +# __imp entries are imports from other DLLs, eg __imp__malloc . |
| 90 | +# (We should never have one of these that hasn't already been skipped |
| 91 | +# by the UNDEF test above, though). |
| 92 | +nextif$pieces[6] =~/^__imp/; |
| 93 | +# More under-documented internal crud |
| 94 | +nextif$pieces[6] =~/NULL_THUNK_DATA$/; |
| 95 | +nextif$pieces[6] =~/^__IMPORT_DESCRIPTOR/; |
| 96 | +nextif$pieces[6] =~/^__NULL_IMPORT/; |
| 97 | +# Skip string literals |
| 98 | +nextif$pieces[6] =~/^\?\?_C/; |
| 99 | + |
| 100 | +# We assume that if a symbol is defined as data, then as a function, |
| 101 | +# the linker will reject the binary anyway. So it's OK to just pick |
| 102 | +# whatever came last. |
| 103 | +$def->{$pieces[6]} =$pieces[3]; |
| 104 | + } |
| 105 | +close(F); |
| 106 | +} |
| 107 | + |
| 108 | +subwritedef |
| 109 | +{ |
| 110 | +my ($deffile,$platform,$def) =@_; |
| 111 | +open(DEF,">$deffile") ||die"Could not write to$deffile\n"; |
| 112 | +print DEF"EXPORTS\n"; |
| 113 | +foreachmy$f (sortkeys %{$def}) |
| 114 | + { |
| 115 | +my$isdata =$def->{$f}eq'data'; |
| 116 | +# Strip the leading underscore for win32, but not x64 |
| 117 | +$f =~s/^_// |
| 118 | +unless ($platformeq"x64"); |
| 119 | + |
| 120 | +# Emit just the name if it's a function symbol, or emit the name |
| 121 | +# decorated with the DATA option for variables. |
| 122 | +if ($isdata) { |
| 123 | +print DEF"$f DATA\n"; |
| 124 | + }else { |
| 125 | +print DEF"$f\n"; |
| 126 | + } |
| 127 | + } |
| 128 | +close(DEF); |
| 129 | +} |
| 130 | + |
| 131 | + |
| 132 | +subusage { |
| 133 | +die ("Usage: gendef.pl <modulepath> <platform>\n" |
| 134 | + ." modulepath: path to dir with obj files, no trailing slash" |
| 135 | + ." platform: Win32 | x64"); |
| 136 | +} |
| 137 | + |
| 138 | +usage() |
| 139 | +unless |
| 140 | +scalar(@ARGV) == 2 |
| 141 | + && (($ARGV[0] =~/\\([^\\]+$)/) |
| 142 | + && ($ARGV[1]eq'Win32' ||$ARGV[1]eq'x64')); |
12 | 143 | my$defname =uc$1;
|
| 144 | +my$deffile ="$ARGV[0]/$defname.def"; |
13 | 145 | my$platform =$ARGV[1];
|
14 | 146 |
|
15 |
| -if (-f"$ARGV[0]/$defname.def") |
| 147 | +# if the def file exists and is newer than all input object files, skip |
| 148 | +# its creation |
| 149 | +if ( |
| 150 | +-f$deffile |
| 151 | + && (-M$deffile > max(map {-M } <$ARGV[0]/*.obj> )) |
| 152 | + ) |
16 | 153 | {
|
17 |
| -print"Not re-generating$defname.DEF, file already exists.\n"; |
18 |
| -exit(0); |
| 154 | +print"Not re-generating$defname.DEF, file already exists.\n"; |
| 155 | +exit(0); |
19 | 156 | }
|
20 | 157 |
|
21 | 158 | print"Generating$defname.DEF from directory$ARGV[0], platform$platform\n";
|
22 | 159 |
|
| 160 | +my%def = (); |
| 161 | + |
23 | 162 | while (<$ARGV[0]/*.obj>)
|
24 | 163 | {
|
25 |
| -my$symfile =$_; |
26 |
| -$symfile =~s/\.obj$/.sym/i; |
27 |
| -print"."; |
28 |
| -system("dumpbin /symbols /out:symbols.out$_ >NUL") |
29 |
| - &&die"Could not call dumpbin"; |
30 |
| -open(F,"<symbols.out") ||die"Could not open symbols.out for$_\n"; |
31 |
| -while (<F>) |
32 |
| -{ |
33 |
| -s/\(\)//g; |
34 |
| -my@pieces =split; |
35 |
| -nextunless$pieces[0] =~/^[A-F0-9]{3,}$/; |
36 |
| -nextunless$pieces[6]; |
37 |
| -nextif ($pieces[2]eq"UNDEF"); |
38 |
| -nextunless ($pieces[4]eq"External"); |
39 |
| -nextif$pieces[6] =~/^@/; |
40 |
| -nextif$pieces[6] =~/^\(/; |
41 |
| -nextif$pieces[6] =~/^__real/; |
42 |
| -nextif$pieces[6] =~/^__imp/; |
43 |
| -nextif$pieces[6] =~/^__xmm/; |
44 |
| -nextif$pieces[6] =~/NULL_THUNK_DATA$/; |
45 |
| -nextif$pieces[6] =~/^__IMPORT_DESCRIPTOR/; |
46 |
| -nextif$pieces[6] =~/^__NULL_IMPORT/; |
47 |
| -nextif$pieces[6] =~/^\?\?_C/; |
48 |
| - |
49 |
| -push@def,$pieces[6]; |
50 |
| -} |
51 |
| -close(F); |
52 |
| -rename("symbols.out",$symfile); |
| 164 | +my$objfile =$_; |
| 165 | +my$symfile =$objfile; |
| 166 | +$symfile =~s/\.obj$/.sym/i; |
| 167 | + dumpsyms($objfile,$symfile); |
| 168 | +print"."; |
| 169 | + extract_syms($symfile, \%def); |
53 | 170 | }
|
54 | 171 | print"\n";
|
55 | 172 |
|
56 |
| -open(DEF,">$ARGV[0]/$defname.def") ||die"Could not write to$defname\n"; |
57 |
| -print DEF"EXPORTS\n"; |
58 |
| -my$i = 0; |
59 |
| -my$last =""; |
60 |
| -foreachmy$f (sort@def) |
61 |
| -{ |
62 |
| -nextif ($feq$last); |
63 |
| -$last =$f; |
64 |
| -$f =~s/^_// |
65 |
| -unless ($platformeq"x64");# win64 has new format of exports |
66 |
| -$i++; |
67 |
| - |
68 |
| -# print DEF " $f \@ $i\n"; # ordinaled exports? |
69 |
| -print DEF"$f\n"; |
70 |
| -} |
71 |
| -close(DEF); |
72 |
| -print"Generated$i symbols\n"; |
| 173 | +writedef($deffile,$platform, \%def); |
| 174 | + |
| 175 | +print"Generated" .scalar(keys(%def)) ." symbols\n"; |