[ab08b42] | 1 | #!/usr/bin/env python
|
---|
[3c80f2b] | 2 | #
|
---|
| 3 | # Copyright (c) 2006 Ondrej Palkovsky
|
---|
| 4 | # All rights reserved.
|
---|
| 5 | #
|
---|
| 6 | # Redistribution and use in source and binary forms, with or without
|
---|
| 7 | # modification, are permitted provided that the following conditions
|
---|
| 8 | # are met:
|
---|
| 9 | #
|
---|
| 10 | # - Redistributions of source code must retain the above copyright
|
---|
| 11 | # notice, this list of conditions and the following disclaimer.
|
---|
| 12 | # - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | # notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | # documentation and/or other materials provided with the distribution.
|
---|
| 15 | # - The name of the author may not be used to endorse or promote products
|
---|
| 16 | # derived from this software without specific prior written permission.
|
---|
| 17 | #
|
---|
| 18 | # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 28 | #
|
---|
| 29 |
|
---|
| 30 | """
|
---|
| 31 | Create binary symbol map out of linker map file
|
---|
| 32 | """
|
---|
[ab08b42] | 33 |
|
---|
| 34 | import sys
|
---|
| 35 | import struct
|
---|
| 36 | import re
|
---|
| 37 |
|
---|
[3c80f2b] | 38 | MAXSTRING = 63
|
---|
| 39 | symtabfmt = "<Q%ds" % (MAXSTRING + 1)
|
---|
[ab08b42] | 40 |
|
---|
[3156582] | 41 | funcline = re.compile(r'([0-9a-f]+)\s+[lg]\s+.\s+\.text\s+([0-9a-f]+)\s+(.*)$')
|
---|
[ff4c4f5] | 42 | bssline = re.compile(r'([0-9a-f]+)\s+[lg]\s+[a-zA-Z]\s+\.bss\s+([0-9a-f]+)\s+(.*)$')
|
---|
| 43 | dataline = re.compile(r'([0-9a-f]+)\s+[lg]\s+[a-zA-Z]\s+\.data\s+([0-9a-f]+)\s+(.*)$')
|
---|
[ae9624e] | 44 | fileexp = re.compile(r'([^\s]+):\s+file format')
|
---|
[ff4c4f5] | 45 | startfile = re.compile(r'\.(text|bss|data)\s+(0x[0-9a-f]+)\s+0x[0-9a-f]+\s+(.*)$')
|
---|
[ae9624e] | 46 |
|
---|
[3c80f2b] | 47 | def read_obdump(inp):
|
---|
| 48 | "Parse input"
|
---|
[a35b458] | 49 |
|
---|
[3c80f2b] | 50 | funcs = {}
|
---|
| 51 | data = {}
|
---|
| 52 | bss = {}
|
---|
| 53 | fname = ''
|
---|
[a35b458] | 54 |
|
---|
[3c80f2b] | 55 | for line in inp:
|
---|
| 56 | line = line.strip()
|
---|
| 57 | res = funcline.match(line)
|
---|
| 58 | if (res):
|
---|
| 59 | funcs.setdefault(fname, []).append((int(res.group(1), 16), res.group(3)))
|
---|
| 60 | continue
|
---|
[a35b458] | 61 |
|
---|
[3c80f2b] | 62 | res = bssline.match(line)
|
---|
| 63 | if (res):
|
---|
| 64 | start = int(res.group(1), 16)
|
---|
| 65 | end = int(res.group(2), 16)
|
---|
| 66 | if (end):
|
---|
| 67 | bss.setdefault(fname, []).append((start, res.group(3)))
|
---|
[a35b458] | 68 |
|
---|
[3c80f2b] | 69 | res = dataline.match(line)
|
---|
| 70 | if (res):
|
---|
| 71 | start = int(res.group(1), 16)
|
---|
| 72 | end = int(res.group(2), 16)
|
---|
| 73 | if (end):
|
---|
| 74 | data.setdefault(fname, []).append((start, res.group(3)))
|
---|
[a35b458] | 75 |
|
---|
[3c80f2b] | 76 | res = fileexp.match(line)
|
---|
| 77 | if (res):
|
---|
| 78 | fname = res.group(1)
|
---|
| 79 | continue
|
---|
[a35b458] | 80 |
|
---|
[4e9aaf5] | 81 | return {'text' : funcs, 'bss' : bss, 'data' : data}
|
---|
[ff4c4f5] | 82 |
|
---|
[3c80f2b] | 83 | def generate(kmapf, obmapf, out):
|
---|
| 84 | "Generate output file"
|
---|
[a35b458] | 85 |
|
---|
[3c80f2b] | 86 | obdump = read_obdump(obmapf)
|
---|
[a35b458] | 87 |
|
---|
[28f4adb] | 88 | def key_sorter(x):
|
---|
| 89 | return x[0]
|
---|
[a35b458] | 90 |
|
---|
[3c80f2b] | 91 | for line in kmapf:
|
---|
| 92 | line = line.strip()
|
---|
| 93 | res = startfile.match(line)
|
---|
[a35b458] | 94 |
|
---|
[28f4adb] | 95 | if ((res) and (res.group(3) in obdump[res.group(1)])):
|
---|
[3c80f2b] | 96 | offset = int(res.group(2), 16)
|
---|
| 97 | fname = res.group(3)
|
---|
| 98 | symbols = obdump[res.group(1)][fname]
|
---|
[28f4adb] | 99 | symbols.sort(key = key_sorter)
|
---|
[3c80f2b] | 100 | for addr, symbol in symbols:
|
---|
| 101 | value = fname + ':' + symbol
|
---|
[432f68a] | 102 | value_bytes = value.encode('ascii')
|
---|
| 103 | data = struct.pack(symtabfmt, addr + offset, value_bytes[:MAXSTRING])
|
---|
[3c80f2b] | 104 | out.write(data)
|
---|
[a35b458] | 105 |
|
---|
[432f68a] | 106 | out.write(struct.pack(symtabfmt, 0, b''))
|
---|
[ab08b42] | 107 |
|
---|
| 108 | def main():
|
---|
[3c80f2b] | 109 | if (len(sys.argv) != 4):
|
---|
[28f4adb] | 110 | print("Usage: %s <kernel.map> <nm dump> <output.bin>" % sys.argv[0])
|
---|
[3c80f2b] | 111 | return 1
|
---|
[a35b458] | 112 |
|
---|
[3c80f2b] | 113 | kmapf = open(sys.argv[1], 'r')
|
---|
| 114 | obmapf = open(sys.argv[2], 'r')
|
---|
[28f4adb] | 115 | out = open(sys.argv[3], 'wb')
|
---|
[a35b458] | 116 |
|
---|
[3c80f2b] | 117 | generate(kmapf, obmapf, out)
|
---|
[a35b458] | 118 |
|
---|
[3c80f2b] | 119 | kmapf.close()
|
---|
| 120 | obmapf.close()
|
---|
| 121 | out.close()
|
---|
[ab08b42] | 122 |
|
---|
| 123 | if __name__ == '__main__':
|
---|
[3c80f2b] | 124 | sys.exit(main())
|
---|