-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpacking.pl
146 lines (109 loc) · 2.84 KB
/
packing.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env perl
#
# Calculate structure offsets by directly asking
# the currently configured compiler.
#
# This will be moved to Win32::API::Packing (or similar)
#
# $Id$
use strict;
use warnings;
use Data::Dumper;
use ExtUtils::CBuilder;
use File::Temp;
sub build_packing_code {
my ($struct) = @_;
if (!$struct || ref $struct ne 'ARRAY') {
return;
}
# "void *" fails with MSVC with "unknown size of void *" error
my $ptr_type = 'char *';
my $code = <<END_OF_C;
#include <stdio.h>
/* Structure declaration */
struct s1 {
%s
};
struct s1 ts;
int main() {
/* Start of struct pointer */
${ptr_type}start = ($ptr_type) &ts;
/* Moving "member" pointer */
${ptr_type}p;
/* Output struct member offsets */
%s
return 0;
}
END_OF_C
my @struct_decl = ();
my @struct_output = ();
for my $member (@{$struct}) {
push @struct_decl, qq{\t$member;};
my ($type, $name) = split m{\s+}, $member, 2;
$type =~ s{^\s*}{};
$name =~ s{\s*$}{};
push @struct_output,
qq{\tp = ($ptr_type) &ts.$name;},
qq{\tprintf("struct.$name\\t%d\\n", p - start);},
q{};
}
$code = sprintf($code, join("\n", @struct_decl), join("\n", @struct_output));
return $code;
}
sub compile_code {
my ($code) = @_;
my $fh = File::Temp->new(SUFFIX => '.c', UNLINK => 0);
print $fh $code;
close $fh;
my $fname = $fh->filename();
my $exe_file = "$fname.exe";
# Use ExtUtils::CBuilder ???
my $cc = 'gcc';
my $cc_cmd = qq{$cc $fname -o $exe_file};
my $status = system($cc_cmd);
$status >>= 8;
if ($status != 0) {
die "Temp file $fname didn't compile: $!\n";
}
unlink $fname;
return $exe_file;
}
sub compile_code_eucb {
my ($code) = @_;
my $fh = File::Temp->new(SUFFIX => '.c', UNLINK => 0);
print $fh $code;
close $fh;
my $fname = $fh->filename();
my $exe_file = "$fname.exe";
my $eucb = ExtUtils::CBuilder->new();
my $obj_file = $eucb->compile(source => $fname);
$exe_file = $eucb->link_executable(
objects => $obj_file,
exe_file => $exe_file
);
unlink $fname;
return $exe_file;
}
sub parse_packing_output {
my ($exe_file) = @_;
my @output = qx{$exe_file};
if (!@output) {
die "Couldn't get any packing info from $exe_file: $!\n";
}
my @struct;
for (@output) {
next if m{^\s*$};
chomp;
my ($member, $offset) = split "\t", $_, 2;
push @struct, {name => $member, offset => $offset};
}
return \@struct;
}
my $struct = ['int i', 'char c1', 'char c2', 'char c3', 'int j',];
my $code = build_packing_code($struct);
my $exe = compile_code_eucb($code);
if (!$exe) {
die "Code didn't compile. Too bad.\n";
}
my $packing = parse_packing_output($exe);
print Dumper($packing);