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