Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit a5eed4d

Browse files
committed
Make gendef.pl emit DATA annotations for global variables.
This should make the MSVC build act more like builds for other platforms, i.e. backend global variables will be automatically available to loadable libraries without need for explicit PGDLLIMPORT marking. Craig Ringer
1 parent 7a98d32 commit a5eed4d

File tree

1 file changed

+154
-51
lines changed

1 file changed

+154
-51
lines changed

src/tools/msvc/gendef.pl

+154-51
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,175 @@
11
my @def;
22

3+
use warnings;
4+
use strict;
5+
use 5.8.0;
6+
use List::Util qw(max);
7+
38
#
49
# Script that generates a .DEF file for all objects in a directory
510
#
611
# src/tools/msvc/gendef.pl
712
#
813

9-
die "Usage: gendef.pl <modulepath> <platform>\n"
10-
unless (($ARGV[0] =~ /\\([^\\]+$)/)
11-
&& ($ARGV[1] == 'Win32' || $ARGV[1] == 'x64'));
14+
sub dumpsyms
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+
sub extract_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+
next unless defined($pieces[0]) and $pieces[0] =~ /^[A-F0-9]{3,}$/;
75+
# Skip blank symbol names
76+
next unless $pieces[6];
77+
# Skip externs used from another compilation unit
78+
next if ($pieces[2] eq "UNDEF");
79+
# Skip static symbols
80+
next unless ($pieces[4] eq "External");
81+
# Skip some more MSVC-generated crud
82+
next if $pieces[6] =~ /^@/;
83+
next if $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+
next if $pieces[6] =~ /^__real/;
88+
next if $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+
next if $pieces[6] =~ /^__imp/;
93+
# More under-documented internal crud
94+
next if $pieces[6] =~ /NULL_THUNK_DATA$/;
95+
next if $pieces[6] =~ /^__IMPORT_DESCRIPTOR/;
96+
next if $pieces[6] =~ /^__NULL_IMPORT/;
97+
# Skip string literals
98+
next if $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+
sub writedef
109+
{
110+
my ($deffile, $platform, $def) = @_;
111+
open(DEF, ">$deffile") || die "Could not write to $deffile\n";
112+
print DEF "EXPORTS\n";
113+
foreach my $f (sort keys %{$def})
114+
{
115+
my $isdata = $def->{$f} eq 'data';
116+
# Strip the leading underscore for win32, but not x64
117+
$f =~ s/^_//
118+
unless ($platform eq "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+
sub usage {
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'));
12143
my $defname = uc $1;
144+
my $deffile = "$ARGV[0]/$defname.def";
13145
my $platform = $ARGV[1];
14146

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+
)
16153
{
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);
19156
}
20157

21158
print "Generating $defname.DEF from directory $ARGV[0], platform $platform\n";
22159

160+
my %def = ();
161+
23162
while (<$ARGV[0]/*.obj>)
24163
{
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-
next unless $pieces[0] =~ /^[A-F0-9]{3,}$/;
36-
next unless $pieces[6];
37-
next if ($pieces[2] eq "UNDEF");
38-
next unless ($pieces[4] eq "External");
39-
next if $pieces[6] =~ /^@/;
40-
next if $pieces[6] =~ /^\(/;
41-
next if $pieces[6] =~ /^__real/;
42-
next if $pieces[6] =~ /^__imp/;
43-
next if $pieces[6] =~ /^__xmm/;
44-
next if $pieces[6] =~ /NULL_THUNK_DATA$/;
45-
next if $pieces[6] =~ /^__IMPORT_DESCRIPTOR/;
46-
next if $pieces[6] =~ /^__NULL_IMPORT/;
47-
next if $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);
53170
}
54171
print "\n";
55172

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-
foreach my $f (sort @def)
61-
{
62-
next if ($f eq $last);
63-
$last = $f;
64-
$f =~ s/^_//
65-
unless ($platform eq "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";

0 commit comments

Comments
 (0)