Andrew Scull | a158e91 | 2018-07-16 11:32:13 +0100 | [diff] [blame^] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | """Extract embedded offsets from an object file. |
| 4 | |
| 5 | We let the compiler calculate the offsets it is going to use and have those |
| 6 | emitted into and object file. This is the next pass which extracts those offsets |
| 7 | and stores them in a header file for the assembly to include and use. |
| 8 | """ |
| 9 | |
| 10 | import argparse |
| 11 | import os |
| 12 | import re |
| 13 | import subprocess |
| 14 | import sys |
| 15 | |
| 16 | def 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 | |
| 46 | if __name__ == "__main__": |
| 47 | sys.exit(Main()) |