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