2 # Script to analyze code and arrange ld sections.
4 # Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net>
6 # This file may be distributed under the terms of the GNU GPLv3 license.
10 # Align 'pos' to 'alignbytes' offset
11 def alignpos(pos, alignbytes):
13 return (pos + mask) & ~mask
15 # LD script headers/trailers
17 /* DO NOT EDIT! This is an autogenerated file. See tools/layoutrom.py. */
18 OUTPUT_FORMAT("elf32-i386")
28 ######################################################################
29 # 16bit fixed address section fitting
30 ######################################################################
32 # Get the maximum start position for a list of sections that end at an
34 def getSectionsStart(sections, endaddr, minalign=1):
36 for size, align, name in sections:
39 totspace = alignpos(totspace, align) + size
40 return (endaddr - totspace) / minalign * minalign
42 # Write LD script includes for the given sections
43 def outSections(file, sections):
44 for size, align, name in sections:
45 file.write("*(%s)\n" % (name,))
47 # The 16bit code can't exceed 64K of space.
50 # Layout the 16bit code. This ensures sections with fixed offset
51 # requirements are placed in the correct location. It also places the
52 # 16bit code as high as possible in the f-segment.
53 def doLayout16(sections, outname):
57 # fixedsections = [(addr, sectioninfo, extasectionslist), ...]
59 # canrelocate = [(sectioninfo, list), ...]
62 # Find desired sections.
63 for section in sections:
64 size, align, name = section
65 if name[:11] == '.fixedaddr.':
66 addr = int(name[11:], 16)
67 fixedsections.append((addr, section, []))
69 print "Error: Fixed section %s has non-zero alignment (%d)" % (
72 if name[:6] == '.text.':
73 textsections.append(section)
74 canrelocate.append((section, textsections))
75 if name[:17] == '.rodata.__func__.' or name == '.rodata.str1.1':
76 rodatasections.append(section)
77 #canrelocate.append((section, rodatasections))
78 if name[:8] == '.data16.':
79 datasections.append(section)
80 #canrelocate.append((section, datasections))
82 # Find freespace in fixed address area
84 # fixedAddr = [(freespace, sectioninfo), ...]
86 for i in range(len(fixedsections)):
87 fixedsectioninfo = fixedsections[i]
88 addr, section, extrasectionslist = fixedsectioninfo
89 if i == len(fixedsections) - 1:
92 nextaddr = fixedsections[i+1][0]
93 avail = nextaddr - addr - section[0]
94 fixedAddr.append((avail, fixedsectioninfo))
96 # Attempt to fit other sections into fixed area
100 for freespace, fixedsectioninfo in fixedAddr:
101 fixedaddr, fixedsection, extrasections = fixedsectioninfo
102 addpos = fixedaddr + fixedsection[0]
103 totalused += fixedsection[0]
104 nextfixedaddr = addpos + freespace
105 # print "Filling section %x uses %d, next=%x, available=%d" % (
106 # fixedaddr, fixedsection[0], nextfixedaddr, freespace)
109 for fixedaddrinfo in canrelocate:
110 fitsection, inlist = fixedaddrinfo
111 fitsize, fitalign, fitname = fitsection
112 if addpos + fitsize > nextfixedaddr:
113 # Can't fit and nothing else will fit.
115 fitnextaddr = alignpos(addpos, fitalign) + fitsize
116 # print "Test %s - %x vs %x" % (
117 # fitname, fitnextaddr, nextfixedaddr)
118 if fitnextaddr > nextfixedaddr:
119 # This item can't fit.
121 canfit = (fitnextaddr, fixedaddrinfo)
124 # Found a section that can fit.
125 fitnextaddr, fixedaddrinfo = canfit
126 canrelocate.remove(fixedaddrinfo)
127 fitsection, inlist = fixedaddrinfo
128 inlist.remove(fitsection)
129 extrasections.append(fitsection)
131 totalused += fitsection[0]
132 # print " Adding %s (size %d align %d) pos=%x avail=%d" % (
133 # fitsection[2], fitsection[0], fitsection[1]
134 # , fitnextaddr, nextfixedaddr - fitnextaddr)
135 firstfixed = fixedsections[0][0]
138 total = MAXPOS-firstfixed
139 slack = total - totalused
140 print ("Fixed space: 0x%x-0x%x total: %d slack: %d"
141 " Percent slack: %.1f%%" % (
142 firstfixed, MAXPOS, total, slack,
143 (float(slack) / total) * 100.0))
145 # Find overall start position
146 start16 = getSectionsStart(
147 textsections + rodatasections + datasections, firstfixed)
150 output = open(outname, 'wb')
151 output.write(COMMONHEADER + """
153 code16_start = ABSOLUTE(.) ;
157 # Write regular sections
158 outSections(output, textsections)
159 output.write("code16_rodata = . ;\n")
160 outSections(output, rodatasections)
161 outSections(output, datasections)
163 # Write fixed sections
164 for addr, section, extrasections in fixedsections:
166 output.write(". = ( 0x%x - code16_start ) ;\n" % (addr,))
167 output.write("*(%s)\n" % (name,))
168 for extrasection in extrasections:
169 output.write("*(%s)\n" % (extrasection[2],))
173 code16_end = ABSOLUTE(.) ;
176 /* Discard regular data sections to force a link error if
177 * 16bit code attempts to access data not marked with VAR16
179 /DISCARD/ : { *(.text*) *(.rodata*) *(.data*) *(.bss*) *(COMMON) }
185 ######################################################################
186 # 32bit section outputting
187 ######################################################################
189 # Return the subset of sections with a given name prefix
190 def getSectionsPrefix(sections, prefix):
193 for size, align, name in sections:
194 if name[:lp] == prefix:
195 out.append((size, align, name))
198 # Layout the 32bit code. This places the code as high as possible.
199 def doLayout32(sections, outname, start16):
201 # Find sections to output
202 textsections = getSectionsPrefix(sections, '.text.')
203 rodatasections = getSectionsPrefix(sections, '.rodata')
204 datasections = getSectionsPrefix(sections, '.data.')
205 bsssections = getSectionsPrefix(sections, '.bss.')
206 start32 = getSectionsStart(
207 textsections + rodatasections + datasections + bsssections, start16, 512)
210 output = open(outname, 'wb')
211 output.write(COMMONHEADER + """
213 code32_start = ABSOLUTE(.) ;
216 outSections(output, textsections)
217 output.write("code32_rodata = . ;\n")
218 outSections(output, rodatasections)
219 outSections(output, datasections)
220 outSections(output, bsssections)
223 freespace_start = . ;
224 code32_end = ABSOLUTE(.) ;
229 ######################################################################
230 # Section garbage collection
231 ######################################################################
233 # Note required section, and recursively set all referenced sections
235 def keepsection(name, pri, alt):
237 # Already kept - nothing to do.
240 relocs = pri[2].get(name)
243 # Keep all sections that this section points to
244 for symbol in relocs:
245 addr, section = pri[1].get(symbol, (None, None))
246 if (section is not None and '*' not in section
247 and section[:9] != '.discard.'):
248 keepsection(section, pri, alt)
250 # Not in primary sections - it may be a cross 16/32 reference
251 addr, section = alt[1].get(symbol, (None, None))
252 if section is not None and '*' not in section:
253 keepsection(section, alt, pri)
255 # Determine which sections are actually referenced and need to be
256 # placed into the output file.
257 def gc(info16, info32):
258 # pri = (sections, symbols, relocs, keep sections)
259 pri = (info16[0], info16[1], info16[2], [])
260 alt = (info32[0], info32[1], info32[2], [])
261 # Start by keeping sections that are globally visible.
262 for size, align, section in info16[0]:
263 if section[:11] == '.fixedaddr.' or '.export.' in section:
264 keepsection(section, pri, alt)
265 # Return sections found.
267 for info in info16[0]:
268 size, align, section = info
269 if section not in pri[3]:
270 # print "gc16", section
272 sections16.append(info)
274 for info in info32[0]:
275 size, align, section = info
276 if section not in alt[3]:
277 # print "gc32", section
279 sections32.append(info)
280 return sections16, sections32
283 ######################################################################
284 # Startup and input parsing
285 ######################################################################
287 # Read in output from objdump
288 def parseObjDump(file):
289 # sections = [(size, align, section), ...]
291 # symbols[symbol] = section
293 # relocs[section] = [symbol, ...]
297 for line in file.readlines():
299 if line == 'Sections:':
302 if line == 'SYMBOL TABLE:':
305 if line[:24] == 'RELOCATION RECORDS FOR [':
307 relocsection = line[24:-2]
310 if state == 'section':
312 idx, name, size, vma, lma, fileoff, align = line.split()
313 if align[:3] != '2**':
315 sections.append((int(size, 16), 2**int(align[3:]), name))
319 if state == 'symbol':
321 section, off, symbol = line[17:].split()
323 addr = int(line[:8], 16)
324 symbols[symbol] = addr, section
330 off, type, symbol = line.split()
332 relocs.setdefault(relocsection, []).append(symbol)
335 return sections, symbols, relocs
339 in16, in32, out16, out32 = sys.argv[1:]
341 infile16 = open(in16, 'rb')
342 infile32 = open(in32, 'rb')
344 info16 = parseObjDump(infile16)
345 info32 = parseObjDump(infile32)
347 sections16, sections32 = gc(info16, info32)
349 start16 = doLayout16(sections16, out16)
350 doLayout32(sections32, out32, start16)
352 if __name__ == '__main__':