Subversion Repositories HelenOS

Rev

Rev 270 | Rev 293 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 270 Rev 288
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
symline = re.compile(r'(0x[a-f0-9]+)\s+([^\s]+)$')
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)$')
8
fileline = re.compile(r'[^\s]+\s+0x[a-f0-9]+\s+0x[a-f0-9]+\s+([^\s]+\.o)$')
9
 
9
 
10
MAXSTRING=63
10
MAXSTRING=63
11
symtabfmt = "<Q%ds" % (MAXSTRING+1)
11
symtabfmt = "<Q%ds" % (MAXSTRING+1)
12
 
12
 
13
def read_symbols(inp):
13
def read_symbols(inp):
14
    while 1:
14
    while 1:
15
        line = inp.readline()
15
        line = inp.readline()
16
        if not line:
16
        if not line:
17
            return
17
            return
18
        if 'memory map' in line:
18
        if 'memory map' in line:
19
            break        
19
            break        
20
 
20
 
21
    symtable = {}
21
    symtable = {}
22
    filename = ''
22
    filename = ''
23
    while 1:
23
    while 1:
24
        line = inp.readline()
24
        line = inp.readline()
25
        if not line.strip():
25
        if not line.strip():
26
            continue
26
            continue
27
        if line[0] not in (' ','.'):
27
        if line[0] not in (' ','.'):
28
            break
28
            break
29
        line = line.strip()
29
        line = line.strip()
30
        # Search for file name
30
        # Search for file name
31
        res = fileline.match(line)
31
        res = fileline.match(line)
32
        if res:
32
        if res:
33
            filename = res.group(1)
33
            filename = res.group(1)
34
        # Search for symbols
34
        # Search for symbols
35
        res = symline.match(line)
35
        res = symline.match(line)
36
        if res:
36
        if res:
37
            symtable[int(res.group(1),16)] = filename + ':' + res.group(2)
37
            symtable[int(res.group(1),16)] = filename + ':' + res.group(2)
38
    return symtable
38
    return symtable
39
   
39
   
40
def generate(inp, out):
40
def generate(inp, out):
41
    symtab = read_symbols(inp)
41
    symtab = read_symbols(inp)
42
    if not symtab:
42
    if not symtab:
43
        print "Bad kernel.map format, empty."
43
        print "Bad kernel.map format, empty."
44
        sys.exit(1)
44
        sys.exit(1)
45
    addrs = symtab.keys()
45
    addrs = symtab.keys()
46
    addrs.sort()
46
    addrs.sort()
47
    for addr in addrs:
47
    for addr in addrs:
48
        # Do not write address 0, it indicates end of data
48
        # Do not write address 0, it indicates end of data
49
        if addr == 0:
49
        if addr == 0:
50
            continue
50
            continue
51
        data = struct.pack(symtabfmt,addr,symtab[addr][:MAXSTRING])
51
        data = struct.pack(symtabfmt,addr,symtab[addr][:MAXSTRING])
52
        out.write(data)
52
        out.write(data)
53
    out.write(struct.pack(symtabfmt,0,''))
53
    out.write(struct.pack(symtabfmt,0,''))
54
 
54
 
55
def main():
55
def main():
56
    if len(sys.argv) != 3:
56
    if len(sys.argv) != 3:
57
        print "Usage: %s <kernel.map> <output.bin>" % sys.argv[0]
57
        print "Usage: %s <kernel.map> <output.bin>" % sys.argv[0]
58
        sys.exit(1)
58
        sys.exit(1)
59
 
59
 
60
    inp = open(sys.argv[1],'r')
60
    inp = open(sys.argv[1],'r')
61
    out = open(sys.argv[2],'w')
61
    out = open(sys.argv[2],'w')
62
    generate(inp,out)
62
    generate(inp,out)
63
    inp.close()
63
    inp.close()
64
    out.close()
64
    out.close()
65
 
65
 
66
if __name__ == '__main__':
66
if __name__ == '__main__':
67
    main()
67
    main()
68
 
68