2009-04-12 11:19:52 +08:00
|
|
|
#!/usr/bin/perl
|
|
|
|
use strict;
|
|
|
|
use File::Temp qw'tempdir';
|
|
|
|
use File::Spec;
|
|
|
|
use Getopt::Long;
|
|
|
|
my $dir = tempdir(CLEANUP => 1);
|
|
|
|
|
2013-04-23 17:44:36 +08:00
|
|
|
my ($cpu, $ar, $as, $nm, $objcopy, %replace);
|
|
|
|
GetOptions('cpu=s'=>\$cpu, 'ar=s'=>\$ar, 'as=s'=>\$as,'nm=s'=>\$nm, 'objcopy=s'=>\$objcopy, 'replace=s'=>\%replace);
|
2009-04-12 11:19:52 +08:00
|
|
|
|
|
|
|
# Args::
|
|
|
|
# 1) import lib to create
|
|
|
|
# 2) input dll
|
|
|
|
# 3...) extra objects to add
|
|
|
|
|
|
|
|
$_ = File::Spec->rel2abs($_) for @ARGV;
|
|
|
|
|
|
|
|
my $libdll = shift;
|
|
|
|
my $inpdll = shift;
|
|
|
|
|
|
|
|
open my $nm_fd, '-|', $nm, '-Apg', '--defined-only', $inpdll;
|
|
|
|
my %text = ();
|
|
|
|
my %import = ();
|
|
|
|
my %symfile = ();
|
2013-04-23 17:44:36 +08:00
|
|
|
|
2022-07-29 02:05:55 +08:00
|
|
|
my $is_x86_64 = ($cpu eq 'x86_64' ? 1 : 0);
|
|
|
|
# FIXME? Do other (non-32 bit) arches on Windows still use symbol prefixes?
|
|
|
|
my $sym_prefix = '';
|
2013-04-23 17:44:36 +08:00
|
|
|
|
2009-04-12 11:19:52 +08:00
|
|
|
while (<$nm_fd>) {
|
|
|
|
chomp;
|
|
|
|
my ($fn, $type, $sym) = /^$inpdll:(.*?):\S+\s+(\S)\s+(\S+)$/o;
|
|
|
|
next unless $fn;
|
|
|
|
$text{$fn} = $sym if $type eq 'T';
|
|
|
|
$import{$fn} = $sym if $type eq 'I';
|
|
|
|
$symfile{$sym} = $fn;
|
|
|
|
}
|
|
|
|
close $nm_fd or exit 1;
|
|
|
|
|
|
|
|
for my $sym (keys %replace) {
|
|
|
|
my $fn;
|
2013-04-23 17:44:36 +08:00
|
|
|
my $_sym = $sym_prefix . $sym;
|
2009-04-12 11:19:52 +08:00
|
|
|
if (!defined($fn = $symfile{$_sym})) {
|
|
|
|
$fn = "$sym.o";
|
|
|
|
$text{$fn} = $_sym;
|
|
|
|
}
|
2013-04-23 17:44:36 +08:00
|
|
|
my $imp_sym = '__imp_' . $sym_prefix . $replace{$sym};
|
2009-04-12 11:19:52 +08:00
|
|
|
$import{$fn} = $imp_sym;
|
|
|
|
}
|
|
|
|
|
|
|
|
for my $f (keys %text) {
|
|
|
|
my $imp_sym = delete $import{$f};
|
|
|
|
my $glob_sym = $text{$f};
|
|
|
|
if (!defined $imp_sym) {
|
|
|
|
delete $text{$f};
|
2013-04-23 17:44:36 +08:00
|
|
|
} elsif ($imp_sym eq '__imp_' . $sym_prefix) {
|
2009-04-12 11:19:52 +08:00
|
|
|
$text{$f} = 0;
|
|
|
|
} else {
|
|
|
|
$text{$f} = 1;
|
|
|
|
open my $as_fd, '|-', $as, '-o', "$dir/t-$f", "-";
|
2022-07-29 02:05:55 +08:00
|
|
|
if ($is_x86_64) {
|
2013-04-23 17:44:36 +08:00
|
|
|
print $as_fd <<EOF;
|
|
|
|
.text
|
|
|
|
.extern $imp_sym
|
|
|
|
.global $glob_sym
|
|
|
|
$glob_sym:
|
|
|
|
jmp *$imp_sym(%rip)
|
|
|
|
EOF
|
|
|
|
} else {
|
|
|
|
print $as_fd <<EOF;
|
2009-04-19 01:23:45 +08:00
|
|
|
.text
|
2009-04-12 11:19:52 +08:00
|
|
|
.extern $imp_sym
|
|
|
|
.global $glob_sym
|
|
|
|
$glob_sym:
|
|
|
|
jmp *$imp_sym
|
|
|
|
EOF
|
2013-04-23 17:44:36 +08:00
|
|
|
}
|
2009-04-12 11:19:52 +08:00
|
|
|
close $as_fd or exit 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
chdir $dir or die "$0: couldn't cd to $dir - $!\n";
|
|
|
|
system $ar, 'x', $inpdll;
|
|
|
|
exit 1 if $?;
|
|
|
|
|
|
|
|
for my $f (keys %text) {
|
|
|
|
if (!$text{$f}) {
|
|
|
|
unlink $f;
|
|
|
|
} else {
|
|
|
|
system $objcopy, '-R', '.text', $f and exit 1;
|
|
|
|
system $objcopy, '-R', '.bss', '-R', '.data', "t-$f" and exit 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unlink $libdll;
|
2009-04-12 13:11:08 +08:00
|
|
|
system $ar, 'crus', $libdll, glob('*.o'), @ARGV;
|
|
|
|
unlink glob('*.o');
|
|
|
|
exit 1 if $?;
|
2011-02-12 02:00:55 +08:00
|
|
|
|
|
|
|
END {
|
|
|
|
chdir '/tmp'; # Allow $dir directory removal on Windows
|
|
|
|
}
|