-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgnu_linker_map_parser.py
81 lines (67 loc) · 2.83 KB
/
gnu_linker_map_parser.py
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
import re
import yaml
from section import Section
class GNULinkerMapParser:
"""
Parse a GNU linker map file and convert it to a yaml file for further processing
"""
def __init__(self, input_filename, output_filename):
self.sections = []
self.subsections = []
self.input_filename = input_filename
self.output_filename = output_filename
def parse(self):
with open(self.input_filename, 'r', encoding='utf8') as file:
file_iterator = iter(file)
prev_line = next(file_iterator)
for line in file_iterator:
self.process_areas(prev_line)
multiple_line = prev_line + line
self.process_sections(multiple_line)
prev_line = line
my_dict = {'map': []}
for section in self.sections:
my_dict['map'].append({
'type': 'area',
'address': section.address,
'size': section.size,
'id': section.id,
'flags': section.flags
})
for subsection in self.subsections:
my_dict['map'].append({
'type': 'section',
'parent': subsection.parent,
'address': subsection.address,
'size': subsection.size,
'id': subsection.id,
'flags': subsection.flags
})
with open(self.output_filename, 'w', encoding='utf8') as file:
yaml_string = yaml.dump(my_dict)
file.write(yaml_string)
def process_areas(self, line):
pattern = r'([.][a-z]{1,})[ ]{1,}(0x[a-fA-F0-9]{1,})[ ]{1,}(0x[a-fA-F0-9]{1,})\n'
p = re.compile(pattern)
result = p.search(line)
if result is not None:
self.sections.append(Section(parent=None,
id=result.group(1),
address=int(result.group(2), 0),
size=int(result.group(3), 0),
_type='area'
)
)
def process_sections(self, line):
pattern = r'\s(.[^.]+).([^. \n]+)[\n\r]\s+(0x[0-9a-fA-F]{16})\s+' \
r'(0x[0-9a-fA-F]+)\s+[^\n]+[\n\r]{1}'
p = re.compile(pattern)
result = p.search(line)
if result is not None:
self.subsections.append(Section(parent=result.group(1),
id=result.group(2),
address=int(result.group(3), 0),
size=int(result.group(4), 0),
_type='section'
)
)