Leandro Pereira | f5a8d49 | 2017-07-24 10:19:49 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # |
| 3 | # Copyright (c) 2017 Intel Corporation. |
| 4 | # |
| 5 | # SPDX-License-Identifier: Apache-2.0 |
| 6 | # |
| 7 | |
| 8 | from elftools.elf.elffile import ELFFile |
| 9 | from elftools.elf.sections import SymbolTableSection |
| 10 | import argparse |
| 11 | import sys |
| 12 | |
Anas Nashif | 7256553 | 2017-12-12 08:19:25 -0500 | [diff] [blame] | 13 | |
Leandro Pereira | f5a8d49 | 2017-07-24 10:19:49 -0700 | [diff] [blame] | 14 | def get_symbol_table(obj): |
| 15 | for section in obj.iter_sections(): |
| 16 | if isinstance(section, SymbolTableSection): |
| 17 | return section |
| 18 | |
| 19 | raise LookupError("Could not find symbol table") |
| 20 | |
Anas Nashif | 7256553 | 2017-12-12 08:19:25 -0500 | [diff] [blame] | 21 | |
Leandro Pereira | f5a8d49 | 2017-07-24 10:19:49 -0700 | [diff] [blame] | 22 | def gen_offset_header(input_name, input_file, output_file): |
Anas Nashif | 08ed560 | 2017-08-01 14:33:09 -0400 | [diff] [blame] | 23 | include_guard = "__GEN_OFFSETS_H__" |
Leandro Pereira | f5a8d49 | 2017-07-24 10:19:49 -0700 | [diff] [blame] | 24 | output_file.write("""/* THIS FILE IS AUTO GENERATED. PLEASE DO NOT EDIT. |
| 25 | * |
| 26 | * This header file provides macros for the offsets of various structure |
| 27 | * members. These offset macros are primarily intended to be used in |
| 28 | * assembly code. |
| 29 | */ |
| 30 | |
| 31 | #ifndef %s |
| 32 | #define %s\n\n""" % (include_guard, include_guard)) |
| 33 | |
| 34 | obj = ELFFile(input_file) |
| 35 | for sym in get_symbol_table(obj).iter_symbols(): |
| 36 | if isinstance(sym.name, bytes): |
| 37 | sym.name = str(sym.name, 'ascii') |
| 38 | |
| 39 | if not sym.name.endswith(('_OFFSET', '_SIZEOF')): |
| 40 | continue |
| 41 | if sym.entry['st_shndx'] != 'SHN_ABS': |
| 42 | continue |
| 43 | if sym.entry['st_info']['bind'] != 'STB_GLOBAL': |
| 44 | continue |
| 45 | |
Anas Nashif | 7256553 | 2017-12-12 08:19:25 -0500 | [diff] [blame] | 46 | output_file.write( |
| 47 | "#define %s 0x%x\n" % |
| 48 | (sym.name, sym.entry['st_value'])) |
Leandro Pereira | f5a8d49 | 2017-07-24 10:19:49 -0700 | [diff] [blame] | 49 | |
| 50 | output_file.write("\n#endif /* %s */\n" % include_guard) |
| 51 | |
| 52 | return 0 |
| 53 | |
Anas Nashif | 7256553 | 2017-12-12 08:19:25 -0500 | [diff] [blame] | 54 | |
Leandro Pereira | f5a8d49 | 2017-07-24 10:19:49 -0700 | [diff] [blame] | 55 | if __name__ == '__main__': |
| 56 | parser = argparse.ArgumentParser( |
Anas Nashif | 7256553 | 2017-12-12 08:19:25 -0500 | [diff] [blame] | 57 | formatter_class=argparse.RawDescriptionHelpFormatter) |
Leandro Pereira | f5a8d49 | 2017-07-24 10:19:49 -0700 | [diff] [blame] | 58 | |
Anas Nashif | 7256553 | 2017-12-12 08:19:25 -0500 | [diff] [blame] | 59 | parser.add_argument( |
| 60 | "-i", |
| 61 | "--input", |
| 62 | required=True, |
| 63 | help="Input object file") |
| 64 | parser.add_argument( |
| 65 | "-o", |
| 66 | "--output", |
| 67 | required=True, |
| 68 | help="Output header file") |
Leandro Pereira | f5a8d49 | 2017-07-24 10:19:49 -0700 | [diff] [blame] | 69 | |
| 70 | args = parser.parse_args() |
| 71 | |
| 72 | input_file = open(args.input, 'rb') |
| 73 | output_file = open(args.output, 'w') |
| 74 | |
| 75 | ret = gen_offset_header(args.input, input_file, output_file) |
| 76 | sys.exit(ret) |