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 | |
| 32 | def doLayout(sections, outname): |
Kevin O'Connor | 202024a | 2009-01-17 10:41:28 -0500 | [diff] [blame] | 33 | textsections = [] |
| 34 | rodatasections = [] |
| 35 | datasections = [] |
Kevin O'Connor | 711ddc6 | 2009-01-17 15:17:34 -0500 | [diff] [blame] | 36 | # fixedsections = [(addr, sectioninfo, extasectionslist), ...] |
| 37 | fixedsections = [] |
| 38 | # canrelocate = [(sectioninfo, list), ...] |
| 39 | canrelocate = [] |
Kevin O'Connor | 202024a | 2009-01-17 10:41:28 -0500 | [diff] [blame] | 40 | |
| 41 | # Find desired sections. |
| 42 | for section in sections: |
Kevin O'Connor | 711ddc6 | 2009-01-17 15:17:34 -0500 | [diff] [blame] | 43 | size, align, name = section |
Kevin O'Connor | 202024a | 2009-01-17 10:41:28 -0500 | [diff] [blame] | 44 | if name[:11] == '.fixedaddr.': |
| 45 | addr = int(name[11:], 16) |
Kevin O'Connor | 711ddc6 | 2009-01-17 15:17:34 -0500 | [diff] [blame] | 46 | fixedsections.append((addr, section, [])) |
| 47 | if align != 1: |
| 48 | print "Error: Fixed section %s has non-zero alignment (%d)" % ( |
| 49 | name, align) |
| 50 | sys.exit(1) |
Kevin O'Connor | 202024a | 2009-01-17 10:41:28 -0500 | [diff] [blame] | 51 | if name[:6] == '.text.': |
| 52 | textsections.append(section) |
Kevin O'Connor | 711ddc6 | 2009-01-17 15:17:34 -0500 | [diff] [blame] | 53 | canrelocate.append((section, textsections)) |
Kevin O'Connor | 202024a | 2009-01-17 10:41:28 -0500 | [diff] [blame] | 54 | if name[:17] == '.rodata.__func__.' or name == '.rodata.str1.1': |
| 55 | rodatasections.append(section) |
Kevin O'Connor | 711ddc6 | 2009-01-17 15:17:34 -0500 | [diff] [blame] | 56 | #canrelocate.append((section, rodatasections)) |
Kevin O'Connor | 202024a | 2009-01-17 10:41:28 -0500 | [diff] [blame] | 57 | if name[:8] == '.data16.': |
| 58 | datasections.append(section) |
Kevin O'Connor | 711ddc6 | 2009-01-17 15:17:34 -0500 | [diff] [blame] | 59 | #canrelocate.append((section, datasections)) |
| 60 | |
| 61 | # Find freespace in fixed address area |
| 62 | fixedsections.sort() |
| 63 | # fixedAddr = [(freespace, sectioninfo), ...] |
| 64 | fixedAddr = [] |
| 65 | for i in range(len(fixedsections)): |
| 66 | fixedsectioninfo = fixedsections[i] |
| 67 | addr, section, extrasectionslist = fixedsectioninfo |
| 68 | if i == len(fixedsections) - 1: |
| 69 | nextaddr = 0x10000 |
| 70 | else: |
| 71 | nextaddr = fixedsections[i+1][0] |
| 72 | avail = nextaddr - addr - section[0] |
| 73 | fixedAddr.append((avail, fixedsectioninfo)) |
| 74 | |
| 75 | # Attempt to fit other sections into fixed area |
| 76 | fixedAddr.sort() |
| 77 | canrelocate.sort() |
| 78 | for freespace, fixedsectioninfo in fixedAddr: |
| 79 | fixedaddr, fixedsection, extrasections = fixedsectioninfo |
| 80 | addpos = fixedaddr + fixedsection[0] |
| 81 | nextfixedaddr = addpos + freespace |
| 82 | # print "Filling section %x uses %d, next=%x, available=%d" % ( |
| 83 | # fixedaddr, fixedsection[0], nextfixedaddr, freespace) |
| 84 | while 1: |
| 85 | canfit = None |
| 86 | for fixedaddrinfo in canrelocate: |
| 87 | fitsection, inlist = fixedaddrinfo |
| 88 | fitnextaddr = alignpos(addpos, fitsection[1]) + fitsection[0] |
| 89 | # print "Test %s - %x vs %x" % ( |
| 90 | # fitsection[2], fitnextaddr, nextfixedaddr) |
| 91 | if fitnextaddr > nextfixedaddr: |
| 92 | # Can't fit. |
| 93 | break |
| 94 | canfit = (fitnextaddr, fixedaddrinfo) |
| 95 | if canfit is None: |
| 96 | break |
| 97 | # Found a section that can fit. |
| 98 | fitnextaddr, fixedaddrinfo = canfit |
| 99 | canrelocate.remove(fixedaddrinfo) |
| 100 | fitsection, inlist = fixedaddrinfo |
| 101 | inlist.remove(fitsection) |
| 102 | extrasections.append(fitsection) |
| 103 | addpos = fitnextaddr |
| 104 | # print " Adding %s (size %d align %d)" % ( |
| 105 | # fitsection[2], fitsection[0], fitsection[1]) |
Kevin O'Connor | 202024a | 2009-01-17 10:41:28 -0500 | [diff] [blame] | 106 | |
| 107 | # Write regular sections |
Kevin O'Connor | 711ddc6 | 2009-01-17 15:17:34 -0500 | [diff] [blame] | 108 | output = open(outname, 'wb') |
Kevin O'Connor | 202024a | 2009-01-17 10:41:28 -0500 | [diff] [blame] | 109 | for section in textsections: |
Kevin O'Connor | 711ddc6 | 2009-01-17 15:17:34 -0500 | [diff] [blame] | 110 | name = section[2] |
| 111 | output.write("*(%s)\n" % (name,)) |
| 112 | output.write("code16_rodata = . ;\n") |
Kevin O'Connor | 202024a | 2009-01-17 10:41:28 -0500 | [diff] [blame] | 113 | for section in rodatasections: |
Kevin O'Connor | 711ddc6 | 2009-01-17 15:17:34 -0500 | [diff] [blame] | 114 | name = section[2] |
| 115 | output.write("*(%s)\n" % (name,)) |
Kevin O'Connor | 202024a | 2009-01-17 10:41:28 -0500 | [diff] [blame] | 116 | for section in datasections: |
Kevin O'Connor | 711ddc6 | 2009-01-17 15:17:34 -0500 | [diff] [blame] | 117 | name = section[2] |
| 118 | output.write("*(%s)\n" % (name,)) |
Kevin O'Connor | 202024a | 2009-01-17 10:41:28 -0500 | [diff] [blame] | 119 | |
| 120 | # Write fixed sections |
Kevin O'Connor | 711ddc6 | 2009-01-17 15:17:34 -0500 | [diff] [blame] | 121 | output.write("freespace1_start = . ;\n") |
Kevin O'Connor | 202024a | 2009-01-17 10:41:28 -0500 | [diff] [blame] | 122 | first = 1 |
Kevin O'Connor | 711ddc6 | 2009-01-17 15:17:34 -0500 | [diff] [blame] | 123 | for addr, section, extrasections in fixedsections: |
| 124 | name = section[2] |
| 125 | output.write(". = ( 0x%x - code16_start ) ;\n" % (addr,)) |
Kevin O'Connor | 202024a | 2009-01-17 10:41:28 -0500 | [diff] [blame] | 126 | if first: |
| 127 | first = 0 |
Kevin O'Connor | 711ddc6 | 2009-01-17 15:17:34 -0500 | [diff] [blame] | 128 | output.write("freespace1_end = . ;\n") |
| 129 | output.write("*(%s)\n" % (name,)) |
| 130 | for extrasection in extrasections: |
| 131 | name = extrasection[2] |
| 132 | output.write("*(%s)\n" % (name,)) |
Kevin O'Connor | 202024a | 2009-01-17 10:41:28 -0500 | [diff] [blame] | 133 | |
| 134 | if __name__ == '__main__': |
| 135 | main() |