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