Kevin O'Connor | 202024a | 2009-01-17 10:41:28 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # Script to arrange sections to ensure fixed offsets. |
| 3 | # |
| 4 | # Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net> |
| 5 | # |
| 6 | # This file may be distributed under the terms of the GNU GPLv3 license. |
| 7 | |
| 8 | import sys |
| 9 | |
| 10 | def main(): |
Kevin O'Connor | 711ddc6 | 2009-01-17 15:17:34 -0500 | [diff] [blame] | 11 | # Get output name |
| 12 | outname = sys.argv[1] |
Kevin O'Connor | 202024a | 2009-01-17 10:41:28 -0500 | [diff] [blame] | 13 | |
Kevin O'Connor | 711ddc6 | 2009-01-17 15:17:34 -0500 | [diff] [blame] | 14 | # Read in section names and sizes |
| 15 | # sections = [(size, align, name), ...] |
Kevin O'Connor | 202024a | 2009-01-17 10:41:28 -0500 | [diff] [blame] | 16 | sections = [] |
| 17 | for line in sys.stdin.readlines(): |
| 18 | try: |
| 19 | idx, name, size, vma, lma, fileoff, align = line.split() |
| 20 | if align[:3] != '2**': |
| 21 | continue |
Kevin O'Connor | 711ddc6 | 2009-01-17 15:17:34 -0500 | [diff] [blame] | 22 | sections.append((int(size, 16), 2**int(align[3:]), name)) |
Kevin O'Connor | 202024a | 2009-01-17 10:41:28 -0500 | [diff] [blame] | 23 | except: |
| 24 | pass |
| 25 | |
Kevin O'Connor | 711ddc6 | 2009-01-17 15:17:34 -0500 | [diff] [blame] | 26 | doLayout(sections, outname) |
| 27 | |
| 28 | def alignpos(pos, alignbytes): |
| 29 | mask = alignbytes - 1 |
| 30 | return (pos + mask) & ~mask |
| 31 | |
Kevin O'Connor | db802ad | 2009-01-17 19:35:10 -0500 | [diff] [blame] | 32 | MAXPOS = 0x10000 |
| 33 | |
Kevin O'Connor | 711ddc6 | 2009-01-17 15:17:34 -0500 | [diff] [blame] | 34 | def doLayout(sections, outname): |
Kevin O'Connor | 202024a | 2009-01-17 10:41:28 -0500 | [diff] [blame] | 35 | textsections = [] |
| 36 | rodatasections = [] |
| 37 | datasections = [] |
Kevin O'Connor | 711ddc6 | 2009-01-17 15:17:34 -0500 | [diff] [blame] | 38 | # fixedsections = [(addr, sectioninfo, extasectionslist), ...] |
| 39 | fixedsections = [] |
| 40 | # canrelocate = [(sectioninfo, list), ...] |
| 41 | canrelocate = [] |
Kevin O'Connor | 202024a | 2009-01-17 10:41:28 -0500 | [diff] [blame] | 42 | |
| 43 | # Find desired sections. |
| 44 | for section in sections: |
Kevin O'Connor | 711ddc6 | 2009-01-17 15:17:34 -0500 | [diff] [blame] | 45 | size, align, name = section |
Kevin O'Connor | 202024a | 2009-01-17 10:41:28 -0500 | [diff] [blame] | 46 | if name[:11] == '.fixedaddr.': |
| 47 | addr = int(name[11:], 16) |
Kevin O'Connor | 711ddc6 | 2009-01-17 15:17:34 -0500 | [diff] [blame] | 48 | fixedsections.append((addr, section, [])) |
| 49 | if align != 1: |
| 50 | print "Error: Fixed section %s has non-zero alignment (%d)" % ( |
| 51 | name, align) |
| 52 | sys.exit(1) |
Kevin O'Connor | 202024a | 2009-01-17 10:41:28 -0500 | [diff] [blame] | 53 | if name[:6] == '.text.': |
| 54 | textsections.append(section) |
Kevin O'Connor | 711ddc6 | 2009-01-17 15:17:34 -0500 | [diff] [blame] | 55 | canrelocate.append((section, textsections)) |
Kevin O'Connor | 202024a | 2009-01-17 10:41:28 -0500 | [diff] [blame] | 56 | if name[:17] == '.rodata.__func__.' or name == '.rodata.str1.1': |
| 57 | rodatasections.append(section) |
Kevin O'Connor | 711ddc6 | 2009-01-17 15:17:34 -0500 | [diff] [blame] | 58 | #canrelocate.append((section, rodatasections)) |
Kevin O'Connor | 202024a | 2009-01-17 10:41:28 -0500 | [diff] [blame] | 59 | if name[:8] == '.data16.': |
| 60 | datasections.append(section) |
Kevin O'Connor | 711ddc6 | 2009-01-17 15:17:34 -0500 | [diff] [blame] | 61 | #canrelocate.append((section, datasections)) |
| 62 | |
| 63 | # Find freespace in fixed address area |
| 64 | fixedsections.sort() |
| 65 | # fixedAddr = [(freespace, sectioninfo), ...] |
| 66 | fixedAddr = [] |
| 67 | for i in range(len(fixedsections)): |
| 68 | fixedsectioninfo = fixedsections[i] |
| 69 | addr, section, extrasectionslist = fixedsectioninfo |
| 70 | if i == len(fixedsections) - 1: |
Kevin O'Connor | db802ad | 2009-01-17 19:35:10 -0500 | [diff] [blame] | 71 | nextaddr = MAXPOS |
Kevin O'Connor | 711ddc6 | 2009-01-17 15:17:34 -0500 | [diff] [blame] | 72 | else: |
| 73 | nextaddr = fixedsections[i+1][0] |
| 74 | avail = nextaddr - addr - section[0] |
| 75 | fixedAddr.append((avail, fixedsectioninfo)) |
| 76 | |
| 77 | # Attempt to fit other sections into fixed area |
| 78 | fixedAddr.sort() |
| 79 | canrelocate.sort() |
Kevin O'Connor | 76f0bed | 2009-01-19 13:00:42 -0500 | [diff] [blame] | 80 | totalused = 0 |
Kevin O'Connor | 711ddc6 | 2009-01-17 15:17:34 -0500 | [diff] [blame] | 81 | for freespace, fixedsectioninfo in fixedAddr: |
| 82 | fixedaddr, fixedsection, extrasections = fixedsectioninfo |
| 83 | addpos = fixedaddr + fixedsection[0] |
Kevin O'Connor | 76f0bed | 2009-01-19 13:00:42 -0500 | [diff] [blame] | 84 | totalused += fixedsection[0] |
Kevin O'Connor | 711ddc6 | 2009-01-17 15:17:34 -0500 | [diff] [blame] | 85 | nextfixedaddr = addpos + freespace |
| 86 | # print "Filling section %x uses %d, next=%x, available=%d" % ( |
| 87 | # fixedaddr, fixedsection[0], nextfixedaddr, freespace) |
| 88 | while 1: |
| 89 | canfit = None |
| 90 | for fixedaddrinfo in canrelocate: |
| 91 | fitsection, inlist = fixedaddrinfo |
Kevin O'Connor | db802ad | 2009-01-17 19:35:10 -0500 | [diff] [blame] | 92 | fitsize, fitalign, fitname = fitsection |
| 93 | if addpos + fitsize > nextfixedaddr: |
| 94 | # Can't fit and nothing else will fit. |
Kevin O'Connor | 711ddc6 | 2009-01-17 15:17:34 -0500 | [diff] [blame] | 95 | break |
Kevin O'Connor | db802ad | 2009-01-17 19:35:10 -0500 | [diff] [blame] | 96 | fitnextaddr = alignpos(addpos, fitalign) + fitsize |
| 97 | # print "Test %s - %x vs %x" % ( |
| 98 | # fitname, fitnextaddr, nextfixedaddr) |
| 99 | if fitnextaddr > nextfixedaddr: |
| 100 | # This item can't fit. |
| 101 | continue |
Kevin O'Connor | 711ddc6 | 2009-01-17 15:17:34 -0500 | [diff] [blame] | 102 | canfit = (fitnextaddr, fixedaddrinfo) |
| 103 | if canfit is None: |
| 104 | break |
| 105 | # Found a section that can fit. |
| 106 | fitnextaddr, fixedaddrinfo = canfit |
| 107 | canrelocate.remove(fixedaddrinfo) |
| 108 | fitsection, inlist = fixedaddrinfo |
| 109 | inlist.remove(fitsection) |
| 110 | extrasections.append(fitsection) |
| 111 | addpos = fitnextaddr |
Kevin O'Connor | 76f0bed | 2009-01-19 13:00:42 -0500 | [diff] [blame] | 112 | totalused += fitsection[0] |
| 113 | # print " Adding %s (size %d align %d) pos=%x avail=%d" % ( |
| 114 | # fitsection[2], fitsection[0], fitsection[1] |
| 115 | # , fitnextaddr, nextfixedaddr - fitnextaddr) |
Kevin O'Connor | 202024a | 2009-01-17 10:41:28 -0500 | [diff] [blame] | 116 | |
Kevin O'Connor | db802ad | 2009-01-17 19:35:10 -0500 | [diff] [blame] | 117 | firstfixed = fixedsections[0][0] |
| 118 | total = MAXPOS-firstfixed |
Kevin O'Connor | db802ad | 2009-01-17 19:35:10 -0500 | [diff] [blame] | 119 | print "Fixed space: 0x%x-0x%x total: %d used: %d Percent used: %.1f%%" % ( |
| 120 | firstfixed, MAXPOS, total, totalused, |
| 121 | (float(totalused) / total) * 100.0) |
| 122 | |
Kevin O'Connor | 202024a | 2009-01-17 10:41:28 -0500 | [diff] [blame] | 123 | # Write regular sections |
Kevin O'Connor | 711ddc6 | 2009-01-17 15:17:34 -0500 | [diff] [blame] | 124 | output = open(outname, 'wb') |
Kevin O'Connor | 202024a | 2009-01-17 10:41:28 -0500 | [diff] [blame] | 125 | for section in textsections: |
Kevin O'Connor | 711ddc6 | 2009-01-17 15:17:34 -0500 | [diff] [blame] | 126 | name = section[2] |
| 127 | output.write("*(%s)\n" % (name,)) |
| 128 | output.write("code16_rodata = . ;\n") |
Kevin O'Connor | 202024a | 2009-01-17 10:41:28 -0500 | [diff] [blame] | 129 | for section in rodatasections: |
Kevin O'Connor | 711ddc6 | 2009-01-17 15:17:34 -0500 | [diff] [blame] | 130 | name = section[2] |
| 131 | output.write("*(%s)\n" % (name,)) |
Kevin O'Connor | 202024a | 2009-01-17 10:41:28 -0500 | [diff] [blame] | 132 | for section in datasections: |
Kevin O'Connor | 711ddc6 | 2009-01-17 15:17:34 -0500 | [diff] [blame] | 133 | name = section[2] |
| 134 | output.write("*(%s)\n" % (name,)) |
Kevin O'Connor | 202024a | 2009-01-17 10:41:28 -0500 | [diff] [blame] | 135 | |
| 136 | # Write fixed sections |
Kevin O'Connor | 711ddc6 | 2009-01-17 15:17:34 -0500 | [diff] [blame] | 137 | output.write("freespace1_start = . ;\n") |
Kevin O'Connor | 202024a | 2009-01-17 10:41:28 -0500 | [diff] [blame] | 138 | first = 1 |
Kevin O'Connor | 711ddc6 | 2009-01-17 15:17:34 -0500 | [diff] [blame] | 139 | for addr, section, extrasections in fixedsections: |
| 140 | name = section[2] |
| 141 | output.write(". = ( 0x%x - code16_start ) ;\n" % (addr,)) |
Kevin O'Connor | 202024a | 2009-01-17 10:41:28 -0500 | [diff] [blame] | 142 | if first: |
| 143 | first = 0 |
Kevin O'Connor | 711ddc6 | 2009-01-17 15:17:34 -0500 | [diff] [blame] | 144 | output.write("freespace1_end = . ;\n") |
| 145 | output.write("*(%s)\n" % (name,)) |
| 146 | for extrasection in extrasections: |
| 147 | name = extrasection[2] |
| 148 | output.write("*(%s)\n" % (name,)) |
Kevin O'Connor | 202024a | 2009-01-17 10:41:28 -0500 | [diff] [blame] | 149 | |
| 150 | if __name__ == '__main__': |
| 151 | main() |