e4b3215f387f3dd87f8129bb33c0631cd4c1ff00
[seabios.git] / tools / defsyms.py
1 #!/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
9 import sys
10 import string
11
12 def printUsage():
13     print "Usage:\n   %s <output file>" % (sys.argv[0],)
14     sys.exit(1)
15
16 def main():
17     if len(sys.argv) != 2:
18         printUsage()
19     # Find symbols (that are valid)
20     syms = []
21     lines = sys.stdin.readlines()
22     for line in lines:
23         addr, type, sym = line.split()
24         if type not in 'Tt':
25             # 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))
32     # 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
44 // Auto generated file - please see defsyms.py.
45 // This file contains symbol offsets of a compiled binary.
46
47 """ % (guardstr, guardstr))
48     for sym, addr in syms:
49         f.write("#define OFFSET_%s 0x%s\n" % (sym, addr))
50     f.write("""
51 #endif // __OFFSET_AUTO_H__%s
52 """ % (guardstr,))
53
54 if __name__ == '__main__':
55     main()