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