-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathremake-elf
executable file
·100 lines (83 loc) · 1.81 KB
/
remake-elf
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
#!/usr/bin/perl
#
# Re-generate an ELF file at the specified offset
# with the symbols provided from the IDA Pro dump.
#
# (c) 2009 Trammell Hudson <[email protected]>
# Based on stubs_2_elf.S by chr
#
use warnings;
use strict;
use Getopt::Long;
use File::Temp qw/ tempfile tempdir /;
my $base = 0;
my $output = "a.out";
my $cc = $ENV{CC} || "arm-elf-gcc";
my $relative = 0;
GetOptions(
"b|base=o" => \$base,
"o|output=s" => \$output,
"c|cc=s" => \$cc,
"r|relative+" => \$relative,
) or die "$0: Unknown option\n";
my $image = shift
or die "$0: Image must be specified!\n";
my $syms = shift;
# Convert the base back into hex notation
$base = sprintf "0x%08x", $base;
my $dir = tempdir( CLEANUP => 1 );
my ($fh,$asm_file) = tempfile( DIR => $dir, SUFFIX => ".S" );
sub _ { local $_ = shift; s/^\| //msg; return $_ }
#
# Output the preamble of the temporary .S file
#
print $fh _(<<"");
| /*
| * Generated file from $image.
| * Do not edit!
| */
| .text
| .org 0
| .align 2
| .globl _start
| here = .
| #define NSTUB(addr,name) name = here + addr - $base
| _start:
| .text
unless( $syms )
{
warn "$0: No syms specified (ignored)\n";
} else {
open SYMS, "<", $syms
or die "$0: Unable to open syms file '$syms': $!\n";
#
# Parse the symbol dump from IDA Pro and output
# code into the .S file to define stubs for each of the
# entities.
#
while( <SYMS> )
{
my ($addr,$name) = /^\s*0001:([0-9A-Fa-f]+)\s+([^\s]+)\s*$/
or next;
$addr = hex($addr);
$addr += hex($base) if $relative;
printf $fh "NSTUB( 0x%08x, %s )\n", $addr, $name;
}
}
#
# Include the ROM dump into the ELF file
#
print $fh _(<<"");
| blob:
| .incbin "$image"
| end:
close SYMS;
close $fh;
#
# Call $CC to compile the assembly file
#
system $cc,
"-Wl,-N,-Ttext,$base",
"-nostdlib",
"-o" => $output,
$asm_file;