40parser = optparse.OptionParser()
41parser.add_option(
"--nm", type=
"string", help=
"Path to `nm` tool")
42parser.add_option(
"--binary", type=
"string")
43parser.add_option(
"--output", type=
"string")
44options = parser.parse_args()[0]
47 raise Exception(
'--nm not specified')
48binary = options.binary
50 raise Exception(
'--binary not specified')
51output = options.output
53 raise Exception(
'--output not specified')
56 [nm,
"--demangle",
"--numeric-sort",
"--print-size", binary],
57 stdout=subprocess.PIPE,
58 stderr=subprocess.STDOUT)
59nm_output, _ = p.communicate()
60nm_lines = nm_output.decode(
'utf-8').split(
'\n')
61regex = re.compile(
"([0-9A-Za-z]+) ([0-9A-Za-z]+) (t|T|w|W) (.*)")
67 offset =
int(m.group(1), 16)
68 if offset > 0x100000000:
71 size =
int(m.group(2), 16)
72 name = m.group(4).split(
"(")[0]
73 if name ==
"__mh_execute_header":
76 symbols.append(
Symbol(offset, size, name.encode(
'utf-8')))
79 raise Exception(binary +
" has no symbols")
81stream = open(output,
"wb")
82stream.write(struct.pack(
"I",
len(symbols)))
85 stream.write(struct.pack(
"I", symbol.offset))
86 stream.write(struct.pack(
"I", symbol.size))
87 stream.write(struct.pack(
"I", nameOffset))
88 nameOffset +=
len(symbol.name)
91 stream.write(symbol.name)
def __init__(self, offset, size, name)