2 # Script to arrange sections to ensure fixed offsets.
4 # Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net>
6 # This file may be distributed under the terms of the GNU GPLv3 license.
14 # Read in section names and sizes
15 # sections = [(size, align, name), ...]
17 for line in sys.stdin.readlines():
19 idx, name, size, vma, lma, fileoff, align = line.split()
20 if align[:3] != '2**':
22 sections.append((int(size, 16), 2**int(align[3:]), name))
26 doLayout(sections, outname)
28 def alignpos(pos, alignbytes):
30 return (pos + mask) & ~mask
34 def doLayout(sections, outname):
38 # fixedsections = [(addr, sectioninfo, extasectionslist), ...]
40 # canrelocate = [(sectioninfo, list), ...]
43 # Find desired sections.
44 for section in sections:
45 size, align, name = section
46 if name[:11] == '.fixedaddr.':
47 addr = int(name[11:], 16)
48 fixedsections.append((addr, section, []))
50 print "Error: Fixed section %s has non-zero alignment (%d)" % (
53 if name[:6] == '.text.':
54 textsections.append(section)
55 canrelocate.append((section, textsections))
56 if name[:17] == '.rodata.__func__.' or name == '.rodata.str1.1':
57 rodatasections.append(section)
58 #canrelocate.append((section, rodatasections))
59 if name[:8] == '.data16.':
60 datasections.append(section)
61 #canrelocate.append((section, datasections))
63 # Find freespace in fixed address area
65 # fixedAddr = [(freespace, sectioninfo), ...]
67 for i in range(len(fixedsections)):
68 fixedsectioninfo = fixedsections[i]
69 addr, section, extrasectionslist = fixedsectioninfo
70 if i == len(fixedsections) - 1:
73 nextaddr = fixedsections[i+1][0]
74 avail = nextaddr - addr - section[0]
75 fixedAddr.append((avail, fixedsectioninfo))
77 # Attempt to fit other sections into fixed area
81 for freespace, fixedsectioninfo in fixedAddr:
82 fixedaddr, fixedsection, extrasections = fixedsectioninfo
83 addpos = fixedaddr + fixedsection[0]
84 totalused += fixedsection[0]
85 nextfixedaddr = addpos + freespace
86 # print "Filling section %x uses %d, next=%x, available=%d" % (
87 # fixedaddr, fixedsection[0], nextfixedaddr, freespace)
90 for fixedaddrinfo in canrelocate:
91 fitsection, inlist = fixedaddrinfo
92 fitsize, fitalign, fitname = fitsection
93 if addpos + fitsize > nextfixedaddr:
94 # Can't fit and nothing else will fit.
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.
102 canfit = (fitnextaddr, fixedaddrinfo)
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)
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)
117 firstfixed = fixedsections[0][0]
118 total = MAXPOS-firstfixed
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)
123 # Write regular sections
124 output = open(outname, 'wb')
125 for section in textsections:
127 output.write("*(%s)\n" % (name,))
128 output.write("code16_rodata = . ;\n")
129 for section in rodatasections:
131 output.write("*(%s)\n" % (name,))
132 for section in datasections:
134 output.write("*(%s)\n" % (name,))
136 # Write fixed sections
137 output.write("freespace1_start = . ;\n")
139 for addr, section, extrasections in fixedsections:
141 output.write(". = ( 0x%x - code16_start ) ;\n" % (addr,))
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,))
150 if __name__ == '__main__':