blob: 82152fbb42fc8a83badb6adf25d1b3db4de0b5a4 [file] [log] [blame]
Leandro Pereiraf5a8d492017-07-24 10:19:49 -07001#!/usr/bin/env python3
2#
3# Copyright (c) 2017 Intel Corporation.
4#
5# SPDX-License-Identifier: Apache-2.0
6#
7
David B. Kinder17299f02019-05-31 15:39:39 -07008"""
9This script scans a specified object file and generates a header file
10that defined macros for the offsets of various found structure members
11(particularly symbols ending with ``_OFFSET`` or ``_SIZEOF``), primarily
12intended for use in assembly code.
13"""
14
Leandro Pereiraf5a8d492017-07-24 10:19:49 -070015from elftools.elf.elffile import ELFFile
16from elftools.elf.sections import SymbolTableSection
17import argparse
18import sys
19
Anas Nashif72565532017-12-12 08:19:25 -050020
Leandro Pereiraf5a8d492017-07-24 10:19:49 -070021def get_symbol_table(obj):
22 for section in obj.iter_sections():
23 if isinstance(section, SymbolTableSection):
24 return section
25
26 raise LookupError("Could not find symbol table")
27
Anas Nashif72565532017-12-12 08:19:25 -050028
Leandro Pereiraf5a8d492017-07-24 10:19:49 -070029def gen_offset_header(input_name, input_file, output_file):
Anas Nashif08ed5602017-08-01 14:33:09 -040030 include_guard = "__GEN_OFFSETS_H__"
Leandro Pereiraf5a8d492017-07-24 10:19:49 -070031 output_file.write("""/* THIS FILE IS AUTO GENERATED. PLEASE DO NOT EDIT.
32 *
33 * This header file provides macros for the offsets of various structure
34 * members. These offset macros are primarily intended to be used in
35 * assembly code.
36 */
37
38#ifndef %s
39#define %s\n\n""" % (include_guard, include_guard))
40
41 obj = ELFFile(input_file)
42 for sym in get_symbol_table(obj).iter_symbols():
43 if isinstance(sym.name, bytes):
44 sym.name = str(sym.name, 'ascii')
45
46 if not sym.name.endswith(('_OFFSET', '_SIZEOF')):
47 continue
48 if sym.entry['st_shndx'] != 'SHN_ABS':
49 continue
50 if sym.entry['st_info']['bind'] != 'STB_GLOBAL':
51 continue
52
Anas Nashif72565532017-12-12 08:19:25 -050053 output_file.write(
54 "#define %s 0x%x\n" %
55 (sym.name, sym.entry['st_value']))
Leandro Pereiraf5a8d492017-07-24 10:19:49 -070056
57 output_file.write("\n#endif /* %s */\n" % include_guard)
58
59 return 0
60
Anas Nashif72565532017-12-12 08:19:25 -050061
Leandro Pereiraf5a8d492017-07-24 10:19:49 -070062if __name__ == '__main__':
63 parser = argparse.ArgumentParser(
Anas Nashif72565532017-12-12 08:19:25 -050064 formatter_class=argparse.RawDescriptionHelpFormatter)
Leandro Pereiraf5a8d492017-07-24 10:19:49 -070065
Anas Nashif72565532017-12-12 08:19:25 -050066 parser.add_argument(
67 "-i",
68 "--input",
69 required=True,
70 help="Input object file")
71 parser.add_argument(
72 "-o",
73 "--output",
74 required=True,
75 help="Output header file")
Leandro Pereiraf5a8d492017-07-24 10:19:49 -070076
77 args = parser.parse_args()
78
79 input_file = open(args.input, 'rb')
80 output_file = open(args.output, 'w')
81
82 ret = gen_offset_header(args.input, input_file, output_file)
83 sys.exit(ret)