-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_lds.py
More file actions
72 lines (61 loc) · 1.92 KB
/
generate_lds.py
File metadata and controls
72 lines (61 loc) · 1.92 KB
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
import sys
import imp
import os
MEMORY_LAYPUT = '''
MEMORY
{
ram : ORIGIN = 0x%x, LENGTH = 0x%x
}
'''
LDS_FORMAT1 = '''
SECTIONS
{
. = 0x%(text_addr)x;
.text : { *(.text) }
.data : { *(.data) }
%(other_sections)s
}
'''
LDS_FORMAT2 = '''
SECTIONS
{
.text : { *(.text) } > ram
.data : { *(.data) } > ram
%(other_sections)s
}
'''
HOOK_SECTION_FORMAT = '''
. = 0x%(hook_addr)x;
.%(hook_section_name)s = .;
'''
def generate_lds(symbols, hook_sections, memory_layout=None):
'''
param symbols: a dict of {symbol : address}
param hook_sections: a dict of {hook_addr : hook_section_name}
param (optional) memory_layout: a tuple of (orig, length)
returns: ld script
'''
other_sections = '\n\n'.join(HOOK_SECTION_FORMAT % {'hook_addr' : hook_addr,
'hook_section_name' : hook_section_name} \
for hook_section_name, hook_addr in hook_sections.iteritems())
if memory_layout is None:
lds = LDS_FORMAT1 % {'text_addr' : symbols['my_text_address'],
'other_sections' : other_sections}
else:
lds = MEMORY_LAYPUT % memory_layout
lds += LDS_FORMAT2 % {'other_sections' : other_sections}
symbols_text = '\n'.join('%s = 0x%x;' % (sym_name, sym_addr) for sym_name, sym_addr in symbols.iteritems())
lds = symbols_text + lds
return lds
def main(config_file_name, output_lds_file_name):
'''
param config_file_name: a python file with globals of symbols
param output_lds_file_name: where to save the ld script
'''
config = imp.load_source(os.path.basename(config_file_name).split('.py')[0],
config_file_name)
lds = generate_lds(config.symbols, config.hook_sections)
with open(output_lds_file_name, 'wb') as lds_file:
lds_file.write(lds)
if __name__ == '__main__':
main(*sys.argv[1:])