blob: 23e711aedfe71fc0752023e4797dc6334eb8ceb8 [file] [log] [blame]
Andrew Sculla158e912018-07-16 11:32:13 +01001#!/usr/bin/env python
2
3"""Extract embedded offsets from an object file.
4
5We let the compiler calculate the offsets it is going to use and have those
6emitted into and object file. This is the next pass which extracts those offsets
7and stores them in a header file for the assembly to include and use.
8"""
9
10import argparse
Andrew Sculla158e912018-07-16 11:32:13 +010011import re
12import subprocess
13import sys
14
15def Main():
16 parser = argparse.ArgumentParser()
17 parser.add_argument("--tool_prefix", required=True)
18 parser.add_argument("--input", required=True)
19 parser.add_argument("--output", required=True)
20 args = parser.parse_args()
21 raw = subprocess.check_output([
22 "{}objdump".format(args.tool_prefix),
23 "--disassemble-all",
24 "--section",
25 ".rodata",
26 args.input])
27 lines = raw.split('\n')
28 with open(args.output, 'w') as header:
29 header.write('#pragma once\n\n')
30 for n in range(len(lines)):
31 # Find a defined offset
32 match = re.match('.+DEFINE_OFFSET__([^>]+)', lines[n])
33 if not match:
34 continue
35 name = match.group(1)
36 # The next line tells the offset
37 if "..." in lines[n + 1]:
38 offset = 0
39 else:
40 offset = re.match('.+\.[\S]+\s+(.+)$', lines[n + 1]).group(1)
41 # Write the offset to the header
42 header.write("#define {} {}\n".format(name, offset))
43 return 0
44
45if __name__ == "__main__":
46 sys.exit(Main())