Subversion Repositories HelenOS

Rev

Rev 268 | Rev 288 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. #!/usr/bin/env python
  2.  
  3. import sys
  4. import struct
  5. import re
  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
  11. symtabfmt = "<Q%ds" % (MAXSTRING+1)
  12.  
  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.  
  21.     symtable = {}
  22.     filename = ''
  23.     while 1:
  24.         line = inp.readline()
  25.         if not line.strip():
  26.             continue
  27.         if line[0] not in (' ','.'):
  28.             break
  29.         line = line.strip()
  30.         # Search for file name
  31.         res = fileline.match(line)
  32.         if res:
  33.             filename = res.group(1)
  34.         # Search for symbols
  35.         res = symline.match(line)
  36.         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)
  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
  51.         data = struct.pack(symtabfmt,addr,symtab[addr][:MAXSTRING])
  52.         out.write(data)
  53.     out.write(struct.pack(symtabfmt,0,''))
  54.  
  55. def main():
  56.     if len(sys.argv) != 3:
  57.         print "Usage: %s <kernel.map> <output.bin>" % sys.argv[0]
  58.         sys.exit(1)
  59.  
  60.     inp = open(sys.argv[1],'r')
  61.     out = open(sys.argv[2],'w')
  62.     generate(inp,out)
  63.     inp.close()
  64.     out.close()
  65.  
  66. if __name__ == '__main__':
  67.     main()
  68.