blob: e4b3215f387f3dd87f8129bb33c0631cd4c1ff00 [file] [log] [blame]
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05001#!/usr/bin/env python
2# Simple script to convert the output from 'nm' to a C style header
3# file with defined offsets.
4#
5# Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net>
6#
7# This file may be distributed under the terms of the GNU GPLv3 license.
8
9import sys
10import string
11
Kevin O'Connor596cf602008-03-01 10:11:55 -050012def printUsage():
13 print "Usage:\n %s <output file>" % (sys.argv[0],)
14 sys.exit(1)
15
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050016def main():
Kevin O'Connor596cf602008-03-01 10:11:55 -050017 if len(sys.argv) != 2:
18 printUsage()
19 # Find symbols (that are valid)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050020 syms = []
21 lines = sys.stdin.readlines()
22 for line in lines:
23 addr, type, sym = line.split()
Kevin O'Connor410680b2008-03-02 20:48:35 -050024 if type not in 'Tt':
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050025 # Only interested in global symbols in text segment
26 continue
27 for c in sym:
28 if c not in string.letters + string.digits + '_':
29 break
30 else:
31 syms.append((sym, addr))
Kevin O'Connor596cf602008-03-01 10:11:55 -050032 # Build guard string
33 guardstr = ''
34 for c in sys.argv[1]:
35 if c not in string.letters + string.digits + '_':
36 guardstr += '_'
37 else:
38 guardstr += c
39 # Generate header
40 f = open(sys.argv[1], 'wb')
41 f.write("""
42#ifndef __OFFSET_AUTO_H__%s
43#define __OFFSET_AUTO_H__%s
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050044// Auto generated file - please see defsyms.py.
45// This file contains symbol offsets of a compiled binary.
Kevin O'Connor596cf602008-03-01 10:11:55 -050046
47""" % (guardstr, guardstr))
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050048 for sym, addr in syms:
Kevin O'Connor596cf602008-03-01 10:11:55 -050049 f.write("#define OFFSET_%s 0x%s\n" % (sym, addr))
50 f.write("""
51#endif // __OFFSET_AUTO_H__%s
52""" % (guardstr,))
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050053
54if __name__ == '__main__':
55 main()