3 use File
::Temp qw
'tempdir';
6 my $dir = tempdir
(CLEANUP
=> 1);
8 my ($cpu, $ar, $as, $nm, $objcopy, %replace);
9 GetOptions
('cpu=s'=>\
$cpu, 'ar=s'=>\
$ar, 'as=s'=>\
$as,'nm=s'=>\
$nm, 'objcopy=s'=>\
$objcopy, 'replace=s'=>\
%replace);
12 # 1) import lib to create
14 # 3...) extra objects to add
16 $_ = File
::Spec
->rel2abs($_) for @ARGV;
21 open my $nm_fd, '-|', $nm, '-Apg', '--defined-only', $inpdll;
26 my $is_x86_64 = ($cpu eq 'x86_64' ?
1 : 0);
27 # FIXME? Do other (non-32 bit) arches on Windows still use symbol prefixes?
32 my ($fn, $type, $sym) = /^$inpdll:(.*?):\S+\s+(\S)\s+(\S+)$/o;
34 $text{$fn} = $sym if $type eq 'T';
35 $import{$fn} = $sym if $type eq 'I';
38 close $nm_fd or exit 1;
40 for my $sym (keys %replace) {
42 my $_sym = $sym_prefix . $sym;
43 if (!defined($fn = $symfile{$_sym})) {
47 my $imp_sym = '__imp_' . $sym_prefix . $replace{$sym};
48 $import{$fn} = $imp_sym;
51 for my $f (keys %text) {
52 my $imp_sym = delete $import{$f};
53 my $glob_sym = $text{$f};
54 if (!defined $imp_sym) {
56 } elsif ($imp_sym eq '__imp_' . $sym_prefix) {
60 open my $as_fd, '|-', $as, '-o', "$dir/t-$f", "-";
78 close $as_fd or exit 1;
82 chdir $dir or die "$0: couldn't cd to $dir - $!\n";
83 system $ar, 'x', $inpdll;
86 for my $f (keys %text) {
90 system $objcopy, '-R', '.text', $f and exit 1;
91 system $objcopy, '-R', '.bss', '-R', '.data', "t-$f" and exit 1;
95 # Enable deterministic archives for reproducible builds.
97 $opts .= 'D' if ($ENV{'SOURCE_DATE_EPOCH'} != '');
100 system $ar, $opts, $libdll, glob('*.o'), @ARGV;
105 chdir '/tmp'; # Allow $dir directory removal on Windows