blob: cf4f5c7dd648f10b48e9efa1a0ad8b90b8f74b49 [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
11import os
12import re
13import subprocess
14import sys
15
16def Main():
17 parser = argparse.ArgumentParser()
18 parser.add_argument("--tool_prefix", required=True)
19 parser.add_argument("--input", required=True)
20 parser.add_argument("--output", required=True)
21 args = parser.parse_args()
22 raw = subprocess.check_output([
23 "{}objdump".format(args.tool_prefix),
24 "--disassemble-all",
25 "--section",
26 ".rodata",
27 args.input])
28 lines = raw.split('\n')
29 with open(args.output, 'w') as header:
30 header.write('#pragma once\n\n')
31 for n in range(len(lines)):
32 # Find a defined offset
33 match = re.match('.+DEFINE_OFFSET__([^>]+)', lines[n])
34 if not match:
35 continue
36 name = match.group(1)
37 # The next line tells the offset
38 if "..." in lines[n + 1]:
39 offset = 0
40 else:
41 offset = re.match('.+\.[\S]+\s+(.+)$', lines[n + 1]).group(1)
42 # Write the offset to the header
43 header.write("#define {} {}\n".format(name, offset))
44 return 0
45
46if __name__ == "__main__":
47 sys.exit(Main())