FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
map-gcc-parser-python/map_parser.py at dev · flipperdevices/map-gcc-parser-python · GitHub
flipperdevices
/
map-gcc-parser-python
Public
Notifications
You must be signed in to change notification settings
Fork
2
Star
15
Code
Issues
1
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
map-gcc-parser-python
/
map_parser.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
229 lines (179 loc) · 7.1 KB
Breadcrumbs
map-gcc-parser-python
/
map_parser.py
Copy path
File metadata and controls
229 lines (179 loc) · 7.1 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import
sys
import
re
import
os
from
typing
import
TextIO
from
cxxfilt
import
demangle
class
Objectfile
:
def
__init__
(
self
,
section
:
str
,
offset
:
int
,
size
:
int
,
comment
:
str
):
self
.
section
=
section
.
strip
()
self
.
offset
=
offset
self
.
size
=
size
self
.
path
=
(
None
,
None
)
self
.
basepath
=
None
if
comment
:
self
.
path
=
re
.
match
(
r'^(.+?)(?:\(([^\)]+)\))?$'
,
comment
).
groups
()
self
.
basepath
=
os
.
path
.
basename
(
self
.
path
[
0
])
self
.
children
=
[]
def
__repr__
(
self
)
->
str
:
return
f'<Objectfile
{
self
.
section
}
{
self
.
offset
:x
}
{
self
.
size
:x
}
{
self
.
path
}
{
repr
(
self
.
children
)
}
>'
def
update_children_size
(
children
:
list
[
list
],
subsection_size
:
int
)
->
list
:
# set subsection size to an only child
if
len
(
children
)
==
1
:
children
[
0
][
1
]
=
subsection_size
return
children
rest_size
=
subsection_size
for
index
in
range
(
1
,
len
(
children
)):
if
rest_size
>
0
:
# current size = current address - previous child address
child_size
=
children
[
index
][
0
]
-
children
[
index
-
1
][
0
]
rest_size
-=
child_size
children
[
index
-
1
][
1
]
=
child_size
# if there is rest size, set it to the last child element
if
rest_size
>
0
:
children
[
-
1
][
1
]
=
rest_size
return
children
def
parse_sections
(
file_name
:
str
)
->
list
:
"""
Quick&Dirty parsing for GNU ld’s linker map output, needs LANG=C, because
some messages are localized.
"""
sections
=
[]
with
open
(
file_name
,
'r'
)
as
file
:
# skip until memory map is found
found
=
False
while
True
:
line
=
file
.
readline
()
if
not
line
:
break
if
line
.
strip
()
==
'Memory Configuration'
:
found
=
True
break
if
not
found
:
raise
Exception
(
f'Memory configuration is not found in the
{
input_file
}
'
)
# long section names result in a linebreak afterwards
sectionre
=
re
.
compile
(
'(?P<section>.+?|.{14,}
\n
)[ ]+0x(?P<offset>[0-9a-f]+)[ ]+0x(?P<size>[0-9a-f]+)(?:[ ]+(?P<comment>.+))?
\n
+'
,
re
.
I
)
subsectionre
=
re
.
compile
(
'[ ]{16}0x(?P<offset>[0-9a-f]+)[ ]+(?P<function>.+)
\n
+'
,
re
.
I
)
s
=
file
.
read
()
pos
=
0
while
True
:
m
=
sectionre
.
match
(
s
,
pos
)
if
not
m
:
# skip that line
try
:
nextpos
=
s
.
index
(
'
\n
'
,
pos
)
+
1
pos
=
nextpos
continue
except
ValueError
:
break
pos
=
m
.
end
()
section
=
m
.
group
(
'section'
)
v
=
m
.
group
(
'offset'
)
offset
=
int
(
v
,
16
)
if
v
is
not
None
else
None
v
=
m
.
group
(
'size'
)
size
=
int
(
v
,
16
)
if
v
is
not
None
else
None
comment
=
m
.
group
(
'comment'
)
if
section
!=
'*default*'
and
size
>
0
:
of
=
Objectfile
(
section
,
offset
,
size
,
comment
)
if
section
.
startswith
(
' '
):
children
=
[]
sections
[
-
1
].
children
.
append
(
of
)
while
True
:
m
=
subsectionre
.
match
(
s
,
pos
)
if
not
m
:
break
pos
=
m
.
end
()
offset
,
function
=
m
.
groups
()
offset
=
int
(
offset
,
16
)
if
sections
and
sections
[
-
1
].
children
:
children
.
append
([
offset
,
0
,
function
])
if
children
:
children
=
update_children_size
(
children
=
children
,
subsection_size
=
of
.
size
)
sections
[
-
1
].
children
[
-
1
].
children
.
extend
(
children
)
else
:
sections
.
append
(
of
)
return
sections
def
get_subsection_name
(
section_name
:
str
,
subsection
:
Objectfile
)
->
str
:
subsection_split_names
=
subsection
.
section
.
split
(
'.'
)
if
subsection
.
section
.
startswith
(
'.'
):
subsection_split_names
=
subsection_split_names
[
1
:]
return
f'.
{
subsection_split_names
[
1
]
}
'
if
len
(
subsection_split_names
)
>
2
else
section_name
def
write_subsection
(
section_name
:
str
,
subsection_name
:
str
,
address
:
str
,
size
:
int
,
demangled_name
:
str
,
module_name
:
str
,
file_name
:
str
,
mangled_name
:
str
,
write_file_object
:
TextIO
)
->
None
:
write_file_object
.
write
(
f'
{
section_name
}
\t
'
f'
{
subsection_name
}
\t
'
f'
{
address
}
\t
'
f'
{
size
}
\t
'
f'
{
demangled_name
}
\t
'
f'
{
module_name
}
\t
'
f'
{
file_name
}
\t
'
f'
{
mangled_name
}
\n
'
)
def
save_subsection
(
section_name
:
str
,
subsection
:
Objectfile
,
write_file_object
:
TextIO
)
->
None
:
subsection_name
=
get_subsection_name
(
section_name
,
subsection
)
module_name
=
subsection
.
path
[
0
]
file_name
=
subsection
.
path
[
1
]
if
not
file_name
:
file_name
,
module_name
=
module_name
,
''
if
not
subsection
.
children
:
address
=
f'
{
subsection
.
offset
:x
}
'
size
=
subsection
.
size
mangled_name
=
''
if
subsection
.
section
==
section_name
else
subsection
.
section
.
split
(
"."
)[
-
1
]
demangled_name
=
demangle
(
mangled_name
)
if
mangled_name
else
mangled_name
write_subsection
(
section_name
=
section_name
,
subsection_name
=
subsection_name
,
address
=
address
,
size
=
size
,
demangled_name
=
demangled_name
,
module_name
=
module_name
,
file_name
=
file_name
,
mangled_name
=
mangled_name
,
write_file_object
=
write_file_object
)
return
for
subsection_child
in
subsection
.
children
:
address
=
f'
{
subsection_child
[
0
]:x
}
'
size
=
subsection_child
[
1
]
mangled_name
=
subsection_child
[
2
]
demangled_name
=
demangle
(
mangled_name
)
write_subsection
(
section_name
=
section_name
,
subsection_name
=
subsection_name
,
address
=
address
,
size
=
size
,
demangled_name
=
demangled_name
,
module_name
=
module_name
,
file_name
=
file_name
,
mangled_name
=
mangled_name
,
write_file_object
=
write_file_object
)
def
save_section
(
section
:
Objectfile
,
write_file_object
:
TextIO
)
->
None
:
section_name
=
section
.
section
for
subsection
in
section
.
children
:
save_subsection
(
section_name
=
section_name
,
subsection
=
subsection
,
write_file_object
=
write_file_object
)
def
save_parsed_data
(
parsed_data
:
list
[
Objectfile
],
output_file_name
:
str
)
->
None
:
with
open
(
output_file_name
,
'w'
)
as
write_file_object
:
for
section
in
parsed_data
:
if
section
.
children
:
save_section
(
section
=
section
,
write_file_object
=
write_file_object
)
if
__name__
==
'__main__'
:
if
len
(
sys
.
argv
)
<
3
:
raise
Exception
(
f'Usage:
{
sys
.
argv
[
0
]
}
<input file> <output file>'
)
input_file
=
sys
.
argv
[
1
]
output_file
=
sys
.
argv
[
2
]
parsed_sections
=
parse_sections
(
input_file
)
if
parsed_sections
is
None
:
raise
Exception
(
f'Memory configuration is not
{
input_file
}
'
)
save_parsed_data
(
parsed_sections
,
output_file
)
Back
|
FazBrowse Home
|
New Git URL