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