Rev 1787 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
268 | palkovsky | 1 | #!/usr/bin/env python |
2 | |||
3 | import sys |
||
4 | import struct |
||
5 | import re |
||
6 | |||
270 | palkovsky | 7 | MAXSTRING=63 |
8 | symtabfmt = "<Q%ds" % (MAXSTRING+1) |
||
9 | |||
268 | palkovsky | 10 | |
332 | palkovsky | 11 | funcline = re.compile(r'([0-9a-f]+)\s+[lg]\s+.\s+\.text\s+([0-9a-f]+)\s+(.*)$') |
297 | palkovsky | 12 | bssline = re.compile(r'([0-9a-f]+)\s+[lg]\s+[a-zA-Z]\s+\.bss\s+([0-9a-f]+)\s+(.*)$') |
13 | dataline = re.compile(r'([0-9a-f]+)\s+[lg]\s+[a-zA-Z]\s+\.data\s+([0-9a-f]+)\s+(.*)$') |
||
293 | palkovsky | 14 | fileexp = re.compile(r'([^\s]+):\s+file format') |
15 | def read_obdump(inp): |
||
297 | palkovsky | 16 | funcs = {} |
17 | data = {} |
||
18 | bss ={} |
||
293 | palkovsky | 19 | fname = '' |
20 | for line in inp: |
||
268 | palkovsky | 21 | line = line.strip() |
297 | palkovsky | 22 | res = funcline.match(line) |
270 | palkovsky | 23 | if res: |
297 | palkovsky | 24 | funcs.setdefault(fname,[]).append((int(res.group(1),16), |
25 | res.group(3))) |
||
26 | continue |
||
27 | res = bssline.match(line) |
||
28 | if res: |
||
29 | start = int(res.group(1),16) |
||
30 | end = int(res.group(2),16) |
||
31 | if end: |
||
32 | bss.setdefault(fname,[]).append((start,res.group(3))) |
||
33 | res = dataline.match(line) |
||
34 | if res: |
||
35 | start = int(res.group(1),16) |
||
36 | end = int(res.group(2),16) |
||
37 | if end: |
||
38 | data.setdefault(fname,[]).append((start,res.group(3))) |
||
293 | palkovsky | 39 | res = fileexp.match(line) |
268 | palkovsky | 40 | if res: |
293 | palkovsky | 41 | fname = res.group(1) |
42 | continue |
||
43 | |||
297 | palkovsky | 44 | return { |
45 | 'text' : funcs, |
||
46 | 'bss' : bss, |
||
47 | 'data' : data |
||
48 | } |
||
49 | |||
50 | startfile = re.compile(r'\.(text|bss|data)\s+(0x[0-9a-f]+)\s+0x[0-9a-f]+\s+(.*)$') |
||
293 | palkovsky | 51 | def generate(kmapf, obmapf, out): |
297 | palkovsky | 52 | obdump = read_obdump(obmapf) |
293 | palkovsky | 53 | |
54 | def sorter(x,y): |
||
55 | return cmp(x[0],y[0]) |
||
56 | |||
57 | for line in kmapf: |
||
58 | line = line.strip() |
||
59 | res = startfile.match(line) |
||
297 | palkovsky | 60 | |
61 | if res and obdump[res.group(1)].has_key(res.group(3)): |
||
62 | offset = int(res.group(2),16) |
||
63 | fname = res.group(3) |
||
64 | symbols = obdump[res.group(1)][fname] |
||
293 | palkovsky | 65 | symbols.sort(sorter) |
297 | palkovsky | 66 | for addr,symbol in symbols: |
293 | palkovsky | 67 | value = fname + ':' + symbol |
68 | data = struct.pack(symtabfmt,addr+offset,value[:MAXSTRING]) |
||
69 | out.write(data) |
||
70 | |||
268 | palkovsky | 71 | out.write(struct.pack(symtabfmt,0,'')) |
72 | |||
73 | def main(): |
||
293 | palkovsky | 74 | if len(sys.argv) != 4: |
75 | print "Usage: %s <kernel.map> <nm dump> <output.bin>" % sys.argv[0] |
||
268 | palkovsky | 76 | sys.exit(1) |
77 | |||
293 | palkovsky | 78 | kmapf = open(sys.argv[1],'r') |
79 | obmapf = open(sys.argv[2],'r') |
||
80 | out = open(sys.argv[3],'w') |
||
81 | generate(kmapf,obmapf,out) |
||
82 | kmapf.close() |
||
83 | obmapf.close() |
||
268 | palkovsky | 84 | out.close() |
85 | |||
86 | if __name__ == '__main__': |
||
87 | main() |