Rev 288 | Rev 297 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 288 | Rev 293 | ||
---|---|---|---|
Line 2... | Line 2... | ||
2 | 2 | ||
3 | import sys |
3 | import sys |
4 | import struct |
4 | import struct |
5 | import re |
5 | import re |
6 | 6 | ||
7 | symline = re.compile(r'(0x[a-f0-9]+)\s+([^\s]+)$') |
- | |
8 | fileline = re.compile(r'[^\s]+\s+0x[a-f0-9]+\s+0x[a-f0-9]+\s+([^\s]+\.o)$') |
- | |
9 | - | ||
10 | MAXSTRING=63 |
7 | MAXSTRING=63 |
11 | symtabfmt = "<Q%ds" % (MAXSTRING+1) |
8 | symtabfmt = "<Q%ds" % (MAXSTRING+1) |
12 | 9 | ||
13 | def read_symbols(inp): |
- | |
14 | while 1: |
- | |
15 | line = inp.readline() |
- | |
16 | if not line: |
- | |
17 | return |
- | |
18 | if 'memory map' in line: |
- | |
19 | break |
- | |
20 | 10 | ||
21 | symtable = {} |
- | |
22 | filename = '' |
11 | objline = re.compile(r'([0-9a-f]+)\s+[lg]\s+F\s+\.text\s+[0-9a-f]+\s+(.*)$') |
23 | while 1: |
- | |
24 | line = inp.readline() |
12 | fileexp = re.compile(r'([^\s]+):\s+file format') |
25 | if not line.strip(): |
13 | def read_obdump(inp): |
26 | continue |
14 | result = {} |
27 | if line[0] not in (' ','.'): |
15 | fname = '' |
28 | break |
16 | for line in inp: |
29 | line = line.strip() |
17 | line = line.strip() |
30 | # Search for file name |
- | |
31 | res = fileline.match(line) |
18 | res = objline.match(line) |
32 | if res: |
19 | if res: |
33 | filename = res.group(1) |
20 | result.setdefault(fname,[]).append((int(res.group(1),16), |
34 | # Search for symbols |
21 | res.group(2))) |
35 | res = symline.match(line) |
22 | res = fileexp.match(line) |
36 | if res: |
23 | if res: |
37 | symtable[int(res.group(1),16)] = filename + ':' + res.group(2) |
- | |
38 | return symtable |
- | |
39 | - | ||
40 | def generate(inp, out): |
- | |
41 | symtab = read_symbols(inp) |
- | |
42 | if not symtab: |
- | |
43 | print "Bad kernel.map format, empty." |
- | |
44 | sys.exit(1) |
24 | fname = res.group(1) |
45 | addrs = symtab.keys() |
- | |
46 | addrs.sort() |
- | |
47 | for addr in addrs: |
- | |
48 | # Do not write address 0, it indicates end of data |
- | |
49 | if addr == 0: |
- | |
50 | continue |
25 | continue |
- | 26 | ||
- | 27 | return result |
|
- | 28 | ||
- | 29 | startfile = re.compile(r'\.text\s+(0x[0-9a-f]+)\s+0x[0-9a-f]+\s+(.*)$') |
|
- | 30 | def generate(kmapf, obmapf, out): |
|
- | 31 | obdump = read_obdump(obmapf) |
|
- | 32 | ||
- | 33 | def sorter(x,y): |
|
- | 34 | return cmp(x[0],y[0]) |
|
- | 35 | ||
- | 36 | for line in kmapf: |
|
- | 37 | line = line.strip() |
|
- | 38 | res = startfile.match(line) |
|
- | 39 | if res and obdump.has_key(res.group(2)): |
|
- | 40 | offset = int(res.group(1),16) |
|
- | 41 | fname = res.group(2) |
|
- | 42 | symbols = obdump[fname] |
|
- | 43 | symbols.sort(sorter) |
|
- | 44 | for addr,symbol in symbols: |
|
- | 45 | value = fname + ':' + symbol |
|
51 | data = struct.pack(symtabfmt,addr,symtab[addr][:MAXSTRING]) |
46 | data = struct.pack(symtabfmt,addr+offset,value[:MAXSTRING]) |
52 | out.write(data) |
47 | out.write(data) |
- | 48 | ||
53 | out.write(struct.pack(symtabfmt,0,'')) |
49 | out.write(struct.pack(symtabfmt,0,'')) |
54 | 50 | ||
55 | def main(): |
51 | def main(): |
56 | if len(sys.argv) != 3: |
52 | if len(sys.argv) != 4: |
57 | print "Usage: %s <kernel.map> <output.bin>" % sys.argv[0] |
53 | print "Usage: %s <kernel.map> <nm dump> <output.bin>" % sys.argv[0] |
58 | sys.exit(1) |
54 | sys.exit(1) |
59 | 55 | ||
60 | inp = open(sys.argv[1],'r') |
56 | kmapf = open(sys.argv[1],'r') |
- | 57 | obmapf = open(sys.argv[2],'r') |
|
61 | out = open(sys.argv[2],'w') |
58 | out = open(sys.argv[3],'w') |
62 | generate(inp,out) |
59 | generate(kmapf,obmapf,out) |
63 | inp.close() |
60 | kmapf.close() |
- | 61 | obmapf.close() |
|
64 | out.close() |
62 | out.close() |
65 | 63 | ||
66 | if __name__ == '__main__': |
64 | if __name__ == '__main__': |
67 | main() |
65 | main() |