-
Notifications
You must be signed in to change notification settings - Fork 70
/
ghidra2stubs
executable file
·69 lines (59 loc) · 1.17 KB
/
ghidra2stubs
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
#!/usr/bin/perl
# Generate a linker script from a Ghidra XML file
use warnings;
use strict;
print <<"";
#define NSTUB(addr,name) \\
.global name; \\
.extern name; \\
name = addr
.text
my %types;
my %names;
while(<>)
{
if (my ($addr,$type) = m/
<DEFINED_DATA \s+
ADDRESS="([0-9a-f]+)" \s+
DATATYPE="(.*?)"
/msgx)
{
$types{hex $addr} = $type;
next;
}
if (my ($addr,$name) = m/
<SYMBOL \s+
ADDRESS="([0-9a-f]+)" \s+
NAME="(.*?)" \s+
.*
TYPE="global"
/msgx)
{
# fixup any names
$name =~ s/[ -]/_/msgx;
$name = "_ADC" if $name eq "ADC";
$addr = hex $addr;
# if the type isn't defined, it is probably a function
# and needs to be converted to Thumb
$addr += 1 unless exists $types{$addr};
push @{$names{$name}}, $addr;
}
}
for my $name (sort { $names{$a}->[0] <=> $names{$b}->[0] } keys %names)
{
# ignore some common ones
next if $name =~ /^case/;
next if $name =~ /^switch/;
next if $name =~ /^nop/;
next if $name =~ /^return/;
my $addrs = $names{$name};
if (@$addrs == 1)
{
printf "NSTUB(0x%08x, %s)\n", $addrs->[0], $name;
next;
}
for my $addr (@$addrs)
{
printf "NSTUB(0x%08x, %s_%05x)\n", $addr, $name, $addr & 0xFFFFF;
}
}