Rev 532 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 532 | Rev 534 | ||
---|---|---|---|
Line -... | Line 1... | ||
- | 1 | #!/usr/bin/env python |
|
- | 2 | ||
- | 3 | import sys |
|
- | 4 | import struct |
|
- | 5 | import re |
|
- | 6 | ||
- | 7 | MAXSTRING=63 |
|
- | 8 | symtabfmt = "<Q%ds" % (MAXSTRING+1) |
|
- | 9 | ||
- | 10 | ||
- | 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+(.*)$') |
|
- | 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') |
|
- | 15 | def read_obdump(inp): |
|
- | 16 | funcs = {} |
|
- | 17 | data = {} |
|
- | 18 | bss ={} |
|
- | 19 | fname = '' |
|
- | 20 | for line in inp: |
|
- | 21 | line = line.strip() |
|
- | 22 | res = funcline.match(line) |
|
- | 23 | if res: |
|
- | 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))) |
|
- | 39 | res = fileexp.match(line) |
|
- | 40 | if res: |
|
- | 41 | fname = res.group(1) |
|
- | 42 | continue |
|
- | 43 | ||
- | 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+(.*)$') |
|
- | 51 | def generate(kmapf, obmapf, out): |
|
- | 52 | obdump = read_obdump(obmapf) |
|
- | 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) |
|
- | 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] |
|
- | 65 | symbols.sort(sorter) |
|
- | 66 | for addr,symbol in symbols: |
|
- | 67 | value = fname + ':' + symbol |
|
- | 68 | data = struct.pack(symtabfmt,addr+offset,value[:MAXSTRING]) |
|
- | 69 | out.write(data) |
|
- | 70 | ||
- | 71 | out.write(struct.pack(symtabfmt,0,'')) |
|
- | 72 | ||
- | 73 | def main(): |
|
- | 74 | if len(sys.argv) != 4: |
|
- | 75 | print "Usage: %s <kernel.map> <nm dump> <output.bin>" % sys.argv[0] |
|
- | 76 | sys.exit(1) |
|
- | 77 | ||
- | 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() |
|
- | 84 | out.close() |
|
- | 85 | ||
- | 86 | if __name__ == '__main__': |
|
- | 87 | main() |