blob: 85bdc7dfd0bd227e522f73235aecf2738f4bbc9f [file] [log] [blame]
Kevin O'Connor202024a2009-01-17 10:41:28 -05001#!/usr/bin/env python
Kevin O'Connor5b8f8092009-09-20 19:47:45 -04002# Script to analyze code and arrange ld sections.
Kevin O'Connor202024a2009-01-17 10:41:28 -05003#
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
8import sys
9
Kevin O'Connor5b8f8092009-09-20 19:47:45 -040010# Align 'pos' to 'alignbytes' offset
Kevin O'Connor711ddc62009-01-17 15:17:34 -050011def alignpos(pos, alignbytes):
12 mask = alignbytes - 1
13 return (pos + mask) & ~mask
14
Kevin O'Connor5b8f8092009-09-20 19:47:45 -040015# LD script headers/trailers
16COMMONHEADER = """
17/* DO NOT EDIT! This is an autogenerated file. See tools/layoutrom.py. */
18OUTPUT_FORMAT("elf32-i386")
19OUTPUT_ARCH("i386")
20SECTIONS
21{
22"""
23COMMONTRAILER = """
24}
25"""
26
Kevin O'Connorc0693942009-06-10 21:56:01 -040027
28######################################################################
29# 16bit fixed address section fitting
30######################################################################
31
Kevin O'Connor5b8f8092009-09-20 19:47:45 -040032# Get the maximum start position for a list of sections that end at an
33# address.
34def getSectionsStart(sections, endaddr, minalign=1):
35 totspace = 0
36 for size, align, name in sections:
37 if align > minalign:
38 minalign = align
39 totspace = alignpos(totspace, align) + size
40 return (endaddr - totspace) / minalign * minalign
Kevin O'Connordb802ad2009-01-17 19:35:10 -050041
Kevin O'Connor5b8f8092009-09-20 19:47:45 -040042# Write LD script includes for the given sections
43def outSections(file, sections):
44 for size, align, name in sections:
45 file.write("*(%s)\n" % (name,))
46
47# The 16bit code can't exceed 64K of space.
48MAXPOS = 64*1024
49
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.
Kevin O'Connorc0693942009-06-10 21:56:01 -040053def doLayout16(sections, outname):
Kevin O'Connor202024a2009-01-17 10:41:28 -050054 textsections = []
55 rodatasections = []
56 datasections = []
Kevin O'Connor711ddc62009-01-17 15:17:34 -050057 # fixedsections = [(addr, sectioninfo, extasectionslist), ...]
58 fixedsections = []
59 # canrelocate = [(sectioninfo, list), ...]
60 canrelocate = []
Kevin O'Connor202024a2009-01-17 10:41:28 -050061
62 # Find desired sections.
63 for section in sections:
Kevin O'Connor711ddc62009-01-17 15:17:34 -050064 size, align, name = section
Kevin O'Connor202024a2009-01-17 10:41:28 -050065 if name[:11] == '.fixedaddr.':
66 addr = int(name[11:], 16)
Kevin O'Connor711ddc62009-01-17 15:17:34 -050067 fixedsections.append((addr, section, []))
68 if align != 1:
69 print "Error: Fixed section %s has non-zero alignment (%d)" % (
70 name, align)
71 sys.exit(1)
Kevin O'Connor202024a2009-01-17 10:41:28 -050072 if name[:6] == '.text.':
73 textsections.append(section)
Kevin O'Connor711ddc62009-01-17 15:17:34 -050074 canrelocate.append((section, textsections))
Kevin O'Connor202024a2009-01-17 10:41:28 -050075 if name[:17] == '.rodata.__func__.' or name == '.rodata.str1.1':
76 rodatasections.append(section)
Kevin O'Connor711ddc62009-01-17 15:17:34 -050077 #canrelocate.append((section, rodatasections))
Kevin O'Connor202024a2009-01-17 10:41:28 -050078 if name[:8] == '.data16.':
79 datasections.append(section)
Kevin O'Connor711ddc62009-01-17 15:17:34 -050080 #canrelocate.append((section, datasections))
81
82 # Find freespace in fixed address area
83 fixedsections.sort()
84 # fixedAddr = [(freespace, sectioninfo), ...]
85 fixedAddr = []
86 for i in range(len(fixedsections)):
87 fixedsectioninfo = fixedsections[i]
88 addr, section, extrasectionslist = fixedsectioninfo
89 if i == len(fixedsections) - 1:
Kevin O'Connordb802ad2009-01-17 19:35:10 -050090 nextaddr = MAXPOS
Kevin O'Connor711ddc62009-01-17 15:17:34 -050091 else:
92 nextaddr = fixedsections[i+1][0]
93 avail = nextaddr - addr - section[0]
94 fixedAddr.append((avail, fixedsectioninfo))
95
96 # Attempt to fit other sections into fixed area
97 fixedAddr.sort()
98 canrelocate.sort()
Kevin O'Connor76f0bed2009-01-19 13:00:42 -050099 totalused = 0
Kevin O'Connor711ddc62009-01-17 15:17:34 -0500100 for freespace, fixedsectioninfo in fixedAddr:
101 fixedaddr, fixedsection, extrasections = fixedsectioninfo
102 addpos = fixedaddr + fixedsection[0]
Kevin O'Connor76f0bed2009-01-19 13:00:42 -0500103 totalused += fixedsection[0]
Kevin O'Connor711ddc62009-01-17 15:17:34 -0500104 nextfixedaddr = addpos + freespace
105# print "Filling section %x uses %d, next=%x, available=%d" % (
106# fixedaddr, fixedsection[0], nextfixedaddr, freespace)
107 while 1:
108 canfit = None
109 for fixedaddrinfo in canrelocate:
110 fitsection, inlist = fixedaddrinfo
Kevin O'Connordb802ad2009-01-17 19:35:10 -0500111 fitsize, fitalign, fitname = fitsection
112 if addpos + fitsize > nextfixedaddr:
113 # Can't fit and nothing else will fit.
Kevin O'Connor711ddc62009-01-17 15:17:34 -0500114 break
Kevin O'Connordb802ad2009-01-17 19:35:10 -0500115 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.
120 continue
Kevin O'Connor711ddc62009-01-17 15:17:34 -0500121 canfit = (fitnextaddr, fixedaddrinfo)
122 if canfit is None:
123 break
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)
130 addpos = fitnextaddr
Kevin O'Connor76f0bed2009-01-19 13:00:42 -0500131 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)
Kevin O'Connordb802ad2009-01-17 19:35:10 -0500135 firstfixed = fixedsections[0][0]
Kevin O'Connorb1a0d3a2009-05-23 17:49:44 -0400136
Kevin O'Connorb1a0d3a2009-05-23 17:49:44 -0400137 # Report stats
Kevin O'Connordb802ad2009-01-17 19:35:10 -0500138 total = MAXPOS-firstfixed
Kevin O'Connorb1a0d3a2009-05-23 17:49:44 -0400139 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))
144
Kevin O'Connor2ceeec92009-12-19 11:03:40 -0500145 # Find start positions
146 text16_start = getSectionsStart(textsections, firstfixed)
147 data16_start = getSectionsStart(rodatasections + datasections, text16_start)
Kevin O'Connor5b8f8092009-09-20 19:47:45 -0400148
Kevin O'Connor2ceeec92009-12-19 11:03:40 -0500149 # Write header and regular sections
Kevin O'Connorb1a0d3a2009-05-23 17:49:44 -0400150 output = open(outname, 'wb')
Kevin O'Connor5b8f8092009-09-20 19:47:45 -0400151 output.write(COMMONHEADER + """
Kevin O'Connor2ceeec92009-12-19 11:03:40 -0500152 data16_start = 0x%x ;
153 .data16 data16_start : {
Kevin O'Connorb1a0d3a2009-05-23 17:49:44 -0400154 freespace_end = . ;
Kevin O'Connor2ceeec92009-12-19 11:03:40 -0500155""" % data16_start)
156 outSections(output, datasections)
Kevin O'Connor5b8f8092009-09-20 19:47:45 -0400157 output.write("code16_rodata = . ;\n")
158 outSections(output, rodatasections)
Kevin O'Connor2ceeec92009-12-19 11:03:40 -0500159 output.write("""
160 }
161
162 text16_start = 0x%x ;
163 .text16 text16_start : {
164""" % text16_start)
165 outSections(output, textsections)
Kevin O'Connor202024a2009-01-17 10:41:28 -0500166
167 # Write fixed sections
Kevin O'Connor711ddc62009-01-17 15:17:34 -0500168 for addr, section, extrasections in fixedsections:
169 name = section[2]
Kevin O'Connor2ceeec92009-12-19 11:03:40 -0500170 output.write(". = ( 0x%x - text16_start ) ;\n" % (addr,))
Kevin O'Connor711ddc62009-01-17 15:17:34 -0500171 output.write("*(%s)\n" % (name,))
172 for extrasection in extrasections:
Kevin O'Connorc0693942009-06-10 21:56:01 -0400173 output.write("*(%s)\n" % (extrasection[2],))
Kevin O'Connorb1a0d3a2009-05-23 17:49:44 -0400174
175 # Write trailer
176 output.write("""
Kevin O'Connor2ceeec92009-12-19 11:03:40 -0500177 text16_end = ABSOLUTE(.) ;
Kevin O'Connorb1a0d3a2009-05-23 17:49:44 -0400178 }
Kevin O'Connor5b8f8092009-09-20 19:47:45 -0400179
180 /* Discard regular data sections to force a link error if
181 * 16bit code attempts to access data not marked with VAR16
182 */
183 /DISCARD/ : { *(.text*) *(.rodata*) *(.data*) *(.bss*) *(COMMON) }
184""" + COMMONTRAILER)
185
Kevin O'Connor2ceeec92009-12-19 11:03:40 -0500186 return data16_start
Kevin O'Connorb1a0d3a2009-05-23 17:49:44 -0400187
Kevin O'Connor202024a2009-01-17 10:41:28 -0500188
Kevin O'Connorc0693942009-06-10 21:56:01 -0400189######################################################################
190# 32bit section outputting
191######################################################################
192
Kevin O'Connor5b8f8092009-09-20 19:47:45 -0400193# Return the subset of sections with a given name prefix
194def getSectionsPrefix(sections, prefix):
Kevin O'Connorc0693942009-06-10 21:56:01 -0400195 lp = len(prefix)
Kevin O'Connor5b8f8092009-09-20 19:47:45 -0400196 out = []
Kevin O'Connorc0693942009-06-10 21:56:01 -0400197 for size, align, name in sections:
198 if name[:lp] == prefix:
Kevin O'Connor5b8f8092009-09-20 19:47:45 -0400199 out.append((size, align, name))
200 return out
Kevin O'Connorc0693942009-06-10 21:56:01 -0400201
Kevin O'Connor5b8f8092009-09-20 19:47:45 -0400202# Layout the 32bit code. This places the code as high as possible.
203def doLayout32(sections, outname, start16):
204 start16 += 0xf0000
205 # Find sections to output
206 textsections = getSectionsPrefix(sections, '.text.')
207 rodatasections = getSectionsPrefix(sections, '.rodata')
208 datasections = getSectionsPrefix(sections, '.data.')
209 bsssections = getSectionsPrefix(sections, '.bss.')
210 start32 = getSectionsStart(
211 textsections + rodatasections + datasections + bsssections, start16, 512)
212
213 # Write sections
Kevin O'Connorc0693942009-06-10 21:56:01 -0400214 output = open(outname, 'wb')
Kevin O'Connor5b8f8092009-09-20 19:47:45 -0400215 output.write(COMMONHEADER + """
216 .text32 0x%x : {
217 code32_start = ABSOLUTE(.) ;
218""" % start32)
219
220 outSections(output, textsections)
Kevin O'Connorc0693942009-06-10 21:56:01 -0400221 output.write("code32_rodata = . ;\n")
Kevin O'Connor5b8f8092009-09-20 19:47:45 -0400222 outSections(output, rodatasections)
223 outSections(output, datasections)
224 outSections(output, bsssections)
225
226 output.write("""
227 freespace_start = . ;
228 code32_end = ABSOLUTE(.) ;
229 }
230""" + COMMONTRAILER)
Kevin O'Connorc0693942009-06-10 21:56:01 -0400231
232
233######################################################################
234# Section garbage collection
235######################################################################
236
Kevin O'Connor5b8f8092009-09-20 19:47:45 -0400237# Note required section, and recursively set all referenced sections
238# as required.
Kevin O'Connorc0693942009-06-10 21:56:01 -0400239def keepsection(name, pri, alt):
240 if name in pri[3]:
241 # Already kept - nothing to do.
242 return
243 pri[3].append(name)
244 relocs = pri[2].get(name)
245 if relocs is None:
246 return
247 # Keep all sections that this section points to
248 for symbol in relocs:
Kevin O'Connor5b8f8092009-09-20 19:47:45 -0400249 addr, section = pri[1].get(symbol, (None, None))
250 if (section is not None and '*' not in section
251 and section[:9] != '.discard.'):
Kevin O'Connorc0693942009-06-10 21:56:01 -0400252 keepsection(section, pri, alt)
253 continue
254 # Not in primary sections - it may be a cross 16/32 reference
Kevin O'Connor5b8f8092009-09-20 19:47:45 -0400255 addr, section = alt[1].get(symbol, (None, None))
256 if section is not None and '*' not in section:
Kevin O'Connorc0693942009-06-10 21:56:01 -0400257 keepsection(section, alt, pri)
258
Kevin O'Connor5b8f8092009-09-20 19:47:45 -0400259# Determine which sections are actually referenced and need to be
260# placed into the output file.
Kevin O'Connorc0693942009-06-10 21:56:01 -0400261def gc(info16, info32):
262 # pri = (sections, symbols, relocs, keep sections)
263 pri = (info16[0], info16[1], info16[2], [])
264 alt = (info32[0], info32[1], info32[2], [])
265 # Start by keeping sections that are globally visible.
266 for size, align, section in info16[0]:
267 if section[:11] == '.fixedaddr.' or '.export.' in section:
268 keepsection(section, pri, alt)
269 # Return sections found.
270 sections16 = []
271 for info in info16[0]:
272 size, align, section = info
273 if section not in pri[3]:
274# print "gc16", section
275 continue
276 sections16.append(info)
277 sections32 = []
278 for info in info32[0]:
279 size, align, section = info
280 if section not in alt[3]:
281# print "gc32", section
282 continue
283 sections32.append(info)
284 return sections16, sections32
285
286
287######################################################################
288# Startup and input parsing
289######################################################################
290
291# Read in output from objdump
292def parseObjDump(file):
293 # sections = [(size, align, section), ...]
294 sections = []
295 # symbols[symbol] = section
296 symbols = {}
297 # relocs[section] = [symbol, ...]
298 relocs = {}
299
300 state = None
301 for line in file.readlines():
302 line = line.rstrip()
303 if line == 'Sections:':
304 state = 'section'
305 continue
306 if line == 'SYMBOL TABLE:':
307 state = 'symbol'
308 continue
309 if line[:24] == 'RELOCATION RECORDS FOR [':
310 state = 'reloc'
311 relocsection = line[24:-2]
312 continue
313
314 if state == 'section':
315 try:
316 idx, name, size, vma, lma, fileoff, align = line.split()
317 if align[:3] != '2**':
318 continue
319 sections.append((int(size, 16), 2**int(align[3:]), name))
320 except:
321 pass
322 continue
323 if state == 'symbol':
324 try:
325 section, off, symbol = line[17:].split()
326 off = int(off, 16)
Kevin O'Connor5b8f8092009-09-20 19:47:45 -0400327 addr = int(line[:8], 16)
328 symbols[symbol] = addr, section
Kevin O'Connorc0693942009-06-10 21:56:01 -0400329 except:
330 pass
331 continue
332 if state == 'reloc':
333 try:
334 off, type, symbol = line.split()
335 off = int(off, 16)
336 relocs.setdefault(relocsection, []).append(symbol)
337 except:
338 pass
339 return sections, symbols, relocs
340
341def main():
342 # Get output name
343 in16, in32, out16, out32 = sys.argv[1:]
344
345 infile16 = open(in16, 'rb')
346 infile32 = open(in32, 'rb')
347
348 info16 = parseObjDump(infile16)
349 info32 = parseObjDump(infile32)
350
351 sections16, sections32 = gc(info16, info32)
352
Kevin O'Connor5b8f8092009-09-20 19:47:45 -0400353 start16 = doLayout16(sections16, out16)
354 doLayout32(sections32, out32, start16)
Kevin O'Connorc0693942009-06-10 21:56:01 -0400355
Kevin O'Connor202024a2009-01-17 10:41:28 -0500356if __name__ == '__main__':
357 main()