Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / seabios / scripts / layoutrom.py
1 #!/usr/bin/env python
2 # Script to analyze code and arrange ld sections.
3 #
4 # Copyright (C) 2008-2014  Kevin O'Connor <kevin@koconnor.net>
5 #
6 # This file may be distributed under the terms of the GNU GPLv3 license.
7
8 import operator
9 import sys
10
11 # LD script headers/trailers
12 COMMONHEADER = """
13 /* DO NOT EDIT!  This is an autogenerated file.  See scripts/layoutrom.py. */
14 OUTPUT_FORMAT("elf32-i386")
15 OUTPUT_ARCH("i386")
16 SECTIONS
17 {
18 """
19 COMMONTRAILER = """
20
21         /* Discard regular data sections to force a link error if
22          * code attempts to access data not marked with VAR16 (or other
23          * appropriate macro)
24          */
25         /DISCARD/ : {
26                 *(.text*) *(.data*) *(.bss*) *(.rodata*)
27                 *(COMMON) *(.discard*) *(.eh_frame) *(.note*)
28                 }
29 }
30 """
31
32
33 ######################################################################
34 # Determine section locations
35 ######################################################################
36
37 # Align 'pos' to 'alignbytes' offset
38 def alignpos(pos, alignbytes):
39     mask = alignbytes - 1
40     return (pos + mask) & ~mask
41
42 # Determine the final addresses for a list of sections that end at an
43 # address.
44 def setSectionsStart(sections, endaddr, minalign=1, segoffset=0):
45     totspace = 0
46     for section in sections:
47         if section.align > minalign:
48             minalign = section.align
49         totspace = alignpos(totspace, section.align) + section.size
50     startaddr = int((endaddr - totspace) / minalign) * minalign
51     curaddr = startaddr
52     for section in sections:
53         curaddr = alignpos(curaddr, section.align)
54         section.finalloc = curaddr
55         section.finalsegloc = curaddr - segoffset
56         curaddr += section.size
57     return startaddr, minalign
58
59 # The 16bit code can't exceed 64K of space.
60 BUILD_BIOS_ADDR = 0xf0000
61 BUILD_BIOS_SIZE = 0x10000
62 BUILD_ROM_START = 0xc0000
63 BUILD_LOWRAM_END = 0xa0000
64 # Space to reserve in f-segment for dynamic allocations
65 BUILD_MIN_BIOSTABLE = 2048
66
67 # Layout the 16bit code.  This ensures sections with fixed offset
68 # requirements are placed in the correct location.  It also places the
69 # 16bit code as high as possible in the f-segment.
70 def fitSections(sections, fillsections):
71     # fixedsections = [(addr, section), ...]
72     fixedsections = []
73     for section in sections:
74         if section.name.startswith('.fixedaddr.'):
75             addr = int(section.name[11:], 16)
76             section.finalloc = addr + BUILD_BIOS_ADDR
77             section.finalsegloc = addr
78             fixedsections.append((addr, section))
79             if section.align != 1:
80                 print("Error: Fixed section %s has non-zero alignment (%d)" % (
81                     section.name, section.align))
82                 sys.exit(1)
83     fixedsections.sort(key=operator.itemgetter(0))
84     firstfixed = fixedsections[0][0]
85
86     # Find freespace in fixed address area
87     # fixedAddr = [(freespace, section), ...]
88     fixedAddr = []
89     for i in range(len(fixedsections)):
90         fixedsectioninfo = fixedsections[i]
91         addr, section = fixedsectioninfo
92         if i == len(fixedsections) - 1:
93             nextaddr = BUILD_BIOS_SIZE
94         else:
95             nextaddr = fixedsections[i+1][0]
96         avail = nextaddr - addr - section.size
97         fixedAddr.append((avail, section))
98     fixedAddr.sort(key=operator.itemgetter(0))
99
100     # Attempt to fit other sections into fixed area
101     canrelocate = [(section.size, section.align, section.name, section)
102                    for section in fillsections]
103     canrelocate.sort()
104     canrelocate = [section for size, align, name, section in canrelocate]
105     totalused = 0
106     for freespace, fixedsection in fixedAddr:
107         addpos = fixedsection.finalsegloc + fixedsection.size
108         totalused += fixedsection.size
109         nextfixedaddr = addpos + freespace
110 #        print("Filling section %x uses %d, next=%x, available=%d" % (
111 #            fixedsection.finalloc, fixedsection.size, nextfixedaddr, freespace))
112         while 1:
113             canfit = None
114             for fitsection in canrelocate:
115                 if addpos + fitsection.size > nextfixedaddr:
116                     # Can't fit and nothing else will fit.
117                     break
118                 fitnextaddr = alignpos(addpos, fitsection.align) + fitsection.size
119 #                print("Test %s - %x vs %x" % (
120 #                    fitsection.name, fitnextaddr, nextfixedaddr))
121                 if fitnextaddr > nextfixedaddr:
122                     # This item can't fit.
123                     continue
124                 canfit = (fitnextaddr, fitsection)
125             if canfit is None:
126                 break
127             # Found a section that can fit.
128             fitnextaddr, fitsection = canfit
129             canrelocate.remove(fitsection)
130             fitsection.finalloc = addpos + BUILD_BIOS_ADDR
131             fitsection.finalsegloc = addpos
132             addpos = fitnextaddr
133             totalused += fitsection.size
134 #            print("    Adding %s (size %d align %d) pos=%x avail=%d" % (
135 #                fitsection[2], fitsection[0], fitsection[1]
136 #                , fitnextaddr, nextfixedaddr - fitnextaddr))
137
138     # Report stats
139     total = BUILD_BIOS_SIZE-firstfixed
140     slack = total - totalused
141     print ("Fixed space: 0x%x-0x%x  total: %d  slack: %d"
142            "  Percent slack: %.1f%%" % (
143             firstfixed, BUILD_BIOS_SIZE, total, slack,
144             (float(slack) / total) * 100.0))
145
146     return firstfixed + BUILD_BIOS_ADDR
147
148 # Return the subset of sections with a given category
149 def getSectionsCategory(sections, category):
150     return [section for section in sections if section.category == category]
151
152 # Return the subset of sections with a given fileid
153 def getSectionsFileid(sections, fileid):
154     return [section for section in sections if section.fileid == fileid]
155
156 # Return the subset of sections with a given name prefix
157 def getSectionsPrefix(sections, prefix):
158     return [section for section in sections
159             if section.name.startswith(prefix)]
160
161 # The sections (and associated information) to be placed in output rom
162 class LayoutInfo:
163     sections = None
164     genreloc = None
165     sec32init_start = sec32init_end = sec32init_align = None
166     sec32low_start = sec32low_end = None
167     zonelow_base = final_sec32low_start = None
168     zonefseg_start = zonefseg_end = None
169     final_readonly_start = None
170     varlowsyms = entrysym = None
171
172 # Determine final memory addresses for sections
173 def doLayout(sections, config, genreloc):
174     li = LayoutInfo()
175     li.sections = sections
176     li.genreloc = genreloc
177     # Determine 16bit positions
178     sections16 = getSectionsCategory(sections, '16')
179     textsections = getSectionsPrefix(sections16, '.text.')
180     rodatasections = getSectionsPrefix(sections16, '.rodata')
181     datasections = getSectionsPrefix(sections16, '.data16.')
182     fixedsections = getSectionsCategory(sections, 'fixed')
183
184     firstfixed = fitSections(fixedsections, textsections)
185     remsections = [s for s in textsections+rodatasections+datasections
186                    if s.finalloc is None]
187     sec16_start, sec16_align = setSectionsStart(
188         remsections, firstfixed, segoffset=BUILD_BIOS_ADDR)
189
190     # Determine 32seg positions
191     sections32seg = getSectionsCategory(sections, '32seg')
192     textsections = getSectionsPrefix(sections32seg, '.text.')
193     rodatasections = getSectionsPrefix(sections32seg, '.rodata')
194     datasections = getSectionsPrefix(sections32seg, '.data32seg.')
195
196     sec32seg_start, sec32seg_align = setSectionsStart(
197         textsections + rodatasections + datasections, sec16_start
198         , segoffset=BUILD_BIOS_ADDR)
199
200     # Determine 32bit "fseg memory" data positions
201     sections32textfseg = getSectionsCategory(sections, '32textfseg')
202     sec32textfseg_start, sec32textfseg_align = setSectionsStart(
203         sections32textfseg, sec32seg_start, 16)
204
205     sections32fseg = getSectionsCategory(sections, '32fseg')
206     sec32fseg_start, sec32fseg_align = setSectionsStart(
207         sections32fseg, sec32textfseg_start, 16
208         , segoffset=BUILD_BIOS_ADDR)
209
210     # Determine 32flat runtime positions
211     sections32flat = getSectionsCategory(sections, '32flat')
212     textsections = getSectionsPrefix(sections32flat, '.text.')
213     rodatasections = getSectionsPrefix(sections32flat, '.rodata')
214     datasections = getSectionsPrefix(sections32flat, '.data.')
215     bsssections = getSectionsPrefix(sections32flat, '.bss.')
216
217     sec32flat_start, sec32flat_align = setSectionsStart(
218         textsections + rodatasections + datasections + bsssections
219         , sec32fseg_start, 16)
220
221     # Determine 32flat init positions
222     sections32init = getSectionsCategory(sections, '32init')
223     init32_textsections = getSectionsPrefix(sections32init, '.text.')
224     init32_rodatasections = getSectionsPrefix(sections32init, '.rodata')
225     init32_datasections = getSectionsPrefix(sections32init, '.data.')
226     init32_bsssections = getSectionsPrefix(sections32init, '.bss.')
227
228     sec32init_start, sec32init_align = setSectionsStart(
229         init32_textsections + init32_rodatasections
230         + init32_datasections + init32_bsssections
231         , sec32flat_start, 16)
232
233     # Determine location of ZoneFSeg memory.
234     zonefseg_end = sec32flat_start
235     if not genreloc:
236         zonefseg_end = sec32init_start
237     zonefseg_start = BUILD_BIOS_ADDR
238     if zonefseg_start + BUILD_MIN_BIOSTABLE > zonefseg_end:
239         # Not enough ZoneFSeg space - force a minimum space.
240         zonefseg_end = sec32fseg_start
241         zonefseg_start = zonefseg_end - BUILD_MIN_BIOSTABLE
242         sec32flat_start, sec32flat_align = setSectionsStart(
243             textsections + rodatasections + datasections + bsssections
244             , zonefseg_start, 16)
245         sec32init_start, sec32init_align = setSectionsStart(
246             init32_textsections + init32_rodatasections
247             + init32_datasections + init32_bsssections
248             , sec32flat_start, 16)
249     li.sec32init_start = sec32init_start
250     li.sec32init_end = sec32flat_start
251     li.sec32init_align = sec32init_align
252     final_readonly_start = min(BUILD_BIOS_ADDR, sec32flat_start)
253     if not genreloc:
254         final_readonly_start = min(BUILD_BIOS_ADDR, sec32init_start)
255     li.zonefseg_start = zonefseg_start
256     li.zonefseg_end = zonefseg_end
257     li.final_readonly_start = final_readonly_start
258
259     # Determine "low memory" data positions
260     sections32low = getSectionsCategory(sections, '32low')
261     sec32low_end = sec32init_start
262     if config.get('CONFIG_MALLOC_UPPERMEMORY'):
263         final_sec32low_end = final_readonly_start
264         zonelow_base = final_sec32low_end - 64*1024
265         zonelow_base = max(BUILD_ROM_START, alignpos(zonelow_base, 2*1024))
266     else:
267         final_sec32low_end = BUILD_LOWRAM_END
268         zonelow_base = final_sec32low_end - 64*1024
269     relocdelta = final_sec32low_end - sec32low_end
270     li.sec32low_start, li.sec32low_align = setSectionsStart(
271         sections32low, sec32low_end, 16
272         , segoffset=zonelow_base - relocdelta)
273     li.sec32low_end = sec32low_end
274     li.zonelow_base = zonelow_base
275     li.final_sec32low_start = li.sec32low_start + relocdelta
276
277     # Print statistics
278     size16 = BUILD_BIOS_ADDR + BUILD_BIOS_SIZE - sec16_start
279     size32seg = sec16_start - sec32seg_start
280     size32textfseg = sec32seg_start - sec32textfseg_start
281     size32fseg = sec32textfseg_start - sec32fseg_start
282     size32flat = sec32fseg_start - sec32flat_start
283     size32init = sec32flat_start - sec32init_start
284     sizelow = li.sec32low_end - li.sec32low_start
285     print("16bit size:           %d" % size16)
286     print("32bit segmented size: %d" % size32seg)
287     print("32bit flat size:      %d" % (size32flat + size32textfseg))
288     print("32bit flat init size: %d" % size32init)
289     print("Lowmem size:          %d" % sizelow)
290     print("f-segment var size:   %d" % size32fseg)
291     return li
292
293
294 ######################################################################
295 # Linker script output
296 ######################################################################
297
298 # Write LD script includes for the given cross references
299 def outXRefs(sections, useseg=0, exportsyms=[], forcedelta=0):
300     xrefs = dict([(symbol.name, symbol) for symbol in exportsyms])
301     out = ""
302     for section in sections:
303         for reloc in section.relocs:
304             symbol = reloc.symbol
305             if (symbol.section is not None
306                 and (symbol.section.fileid != section.fileid
307                      or symbol.name != reloc.symbolname)):
308                 xrefs[reloc.symbolname] = symbol
309     for symbolname, symbol in xrefs.items():
310         loc = symbol.section.finalloc
311         if useseg:
312             loc = symbol.section.finalsegloc
313         out += "%s = 0x%x ;\n" % (symbolname, loc + forcedelta + symbol.offset)
314     return out
315
316 # Write LD script includes for the given sections
317 def outSections(sections, useseg=0):
318     out = ""
319     for section in sections:
320         loc = section.finalloc
321         if useseg:
322             loc = section.finalsegloc
323         out += "%s 0x%x : { *(%s) }\n" % (section.name, loc, section.name)
324     return out
325
326 # Write LD script includes for the given sections using relative offsets
327 def outRelSections(sections, startsym, useseg=0):
328     sections = [(section.finalloc, section) for section in sections
329                 if section.finalloc is not None]
330     sections.sort(key=operator.itemgetter(0))
331     out = ""
332     for addr, section in sections:
333         loc = section.finalloc
334         if useseg:
335             loc = section.finalsegloc
336         out += ". = ( 0x%x - %s ) ;\n" % (loc, startsym)
337         if section.name in ('.rodata.str1.1', '.rodata'):
338             out += "_rodata%s = . ;\n" % (section.fileid,)
339         out += "*%s.*(%s)\n" % (section.fileid, section.name)
340     return out
341
342 # Build linker script output for a list of relocations.
343 def strRelocs(outname, outrel, relocs):
344     relocs.sort()
345     return ("        %s_start = ABSOLUTE(.) ;\n" % (outname,)
346             + "".join(["LONG(0x%x - %s)\n" % (pos, outrel)
347                        for pos in relocs])
348             + "        %s_end = ABSOLUTE(.) ;\n" % (outname,))
349
350 # Find relocations to the given sections
351 def getRelocs(sections, tosection, type=None):
352     return [section.finalloc + reloc.offset
353             for section in sections
354                 for reloc in section.relocs
355                     if (reloc.symbol.section in tosection
356                         and (type is None or reloc.type == type))]
357
358 # Output the linker scripts for all required sections.
359 def writeLinkerScripts(li, out16, out32seg, out32flat):
360     # Write 16bit linker script
361     filesections16 = getSectionsFileid(li.sections, '16')
362     out = outXRefs(filesections16, useseg=1) + """
363     zonelow_base = 0x%x ;
364     _zonelow_seg = 0x%x ;
365
366 %s
367 """ % (li.zonelow_base,
368        int(li.zonelow_base / 16),
369        outSections(filesections16, useseg=1))
370     outfile = open(out16, 'w')
371     outfile.write(COMMONHEADER + out + COMMONTRAILER)
372     outfile.close()
373
374     # Write 32seg linker script
375     filesections32seg = getSectionsFileid(li.sections, '32seg')
376     out = (outXRefs(filesections32seg, useseg=1)
377            + outSections(filesections32seg, useseg=1))
378     outfile = open(out32seg, 'w')
379     outfile.write(COMMONHEADER + out + COMMONTRAILER)
380     outfile.close()
381
382     # Write 32flat linker script
383     sec32all_start = li.sec32low_start
384     relocstr = ""
385     if li.genreloc:
386         # Generate relocations
387         initsections = dict([
388             (s, 1) for s in getSectionsCategory(li.sections, '32init')])
389         noninitsections = dict([(s, 1) for s in li.sections
390                                 if s not in initsections])
391         absrelocs = getRelocs(initsections, initsections, type='R_386_32')
392         relrelocs = getRelocs(initsections, noninitsections, type='R_386_PC32')
393         initrelocs = getRelocs(noninitsections, initsections)
394         relocstr = (strRelocs("_reloc_abs", "code32init_start", absrelocs)
395                     + strRelocs("_reloc_rel", "code32init_start", relrelocs)
396                     + strRelocs("_reloc_init", "code32flat_start", initrelocs))
397         numrelocs = len(absrelocs + relrelocs + initrelocs)
398         sec32all_start -= numrelocs * 4
399     filesections32flat = getSectionsFileid(li.sections, '32flat')
400     out = outXRefs([], exportsyms=li.varlowsyms
401                    , forcedelta=li.final_sec32low_start-li.sec32low_start)
402     out += outXRefs(filesections32flat, exportsyms=[li.entrysym]) + """
403     _reloc_min_align = 0x%x ;
404     zonefseg_start = 0x%x ;
405     zonefseg_end = 0x%x ;
406     zonelow_base = 0x%x ;
407     final_varlow_start = 0x%x ;
408     final_readonly_start = 0x%x ;
409     varlow_start = 0x%x ;
410     varlow_end = 0x%x ;
411     code32init_start = 0x%x ;
412     code32init_end = 0x%x ;
413
414     code32flat_start = 0x%x ;
415     .text code32flat_start : {
416 %s
417 %s
418         code32flat_end = ABSOLUTE(.) ;
419     } :text
420 """ % (li.sec32init_align,
421        li.zonefseg_start,
422        li.zonefseg_end,
423        li.zonelow_base,
424        li.final_sec32low_start,
425        li.final_readonly_start,
426        li.sec32low_start,
427        li.sec32low_end,
428        li.sec32init_start,
429        li.sec32init_end,
430        sec32all_start,
431        relocstr,
432        outRelSections(li.sections, 'code32flat_start'))
433     out = COMMONHEADER + out + COMMONTRAILER + """
434 ENTRY(%s)
435 PHDRS
436 {
437         text PT_LOAD AT ( code32flat_start ) ;
438 }
439 """ % (li.entrysym.name,)
440     outfile = open(out32flat, 'w')
441     outfile.write(out)
442     outfile.close()
443
444
445 ######################################################################
446 # Detection of unused sections and init sections
447 ######################################################################
448
449 # Visit all sections reachable from a given set of start sections
450 def findReachable(anchorsections, checkreloc, data):
451     anchorsections = dict([(section, []) for section in anchorsections])
452     pending = list(anchorsections)
453     while pending:
454         section = pending.pop()
455         for reloc in section.relocs:
456             chain = anchorsections[section] + [section.name]
457             if not checkreloc(reloc, section, data, chain):
458                 continue
459             nextsection = reloc.symbol.section
460             if nextsection not in anchorsections:
461                 anchorsections[nextsection] = chain
462                 pending.append(nextsection)
463     return anchorsections
464
465 # Find "runtime" sections (ie, not init only sections).
466 def checkRuntime(reloc, rsection, data, chain):
467     section = reloc.symbol.section
468     if section is None or '.init.' in section.name:
469         return 0
470     if '.data.varinit.' in section.name:
471         print("ERROR: %s is VARVERIFY32INIT but used from %s" % (
472             section.name, chain))
473         sys.exit(1)
474     return 1
475
476 # Find and keep the section associated with a symbol (if available).
477 def checkKeepSym(reloc, syms, fileid, isxref):
478     symbolname = reloc.symbolname
479     mustbecfunc = symbolname.startswith('_cfunc')
480     if mustbecfunc:
481         symprefix = '_cfunc' + fileid + '_'
482         if not symbolname.startswith(symprefix):
483             return 0
484         symbolname = symbolname[len(symprefix):]
485     symbol = syms.get(symbolname)
486     if (symbol is None or symbol.section is None
487         or symbol.section.name.startswith('.discard.')):
488         return 0
489     isdestcfunc = (symbol.section.name.startswith('.text.')
490                    and not symbol.section.name.startswith('.text.asm.'))
491     if ((mustbecfunc and not isdestcfunc)
492         or (not mustbecfunc and isdestcfunc and isxref)):
493         return 0
494
495     reloc.symbol = symbol
496     return 1
497
498 # Resolve a relocation and check if it should be kept in the final binary.
499 def checkKeep(reloc, section, symbols, chain):
500     ret = checkKeepSym(reloc, symbols[section.fileid], section.fileid, 0)
501     if ret:
502         return ret
503     # Not in primary sections - it may be a cross 16/32 reference
504     for fileid in ('16', '32seg', '32flat'):
505         if fileid != section.fileid:
506             ret = checkKeepSym(reloc, symbols[fileid], fileid, 1)
507             if ret:
508                 return ret
509     return 0
510
511
512 ######################################################################
513 # Startup and input parsing
514 ######################################################################
515
516 class Section:
517     name = size = alignment = fileid = relocs = None
518     finalloc = finalsegloc = category = None
519 class Reloc:
520     offset = type = symbolname = symbol = None
521 class Symbol:
522     name = offset = section = None
523
524 # Read in output from objdump
525 def parseObjDump(file, fileid):
526     # sections = [section, ...]
527     sections = []
528     sectionmap = {}
529     # symbols[symbolname] = symbol
530     symbols = {}
531
532     state = None
533     for line in file.readlines():
534         line = line.rstrip()
535         if line == 'Sections:':
536             state = 'section'
537             continue
538         if line == 'SYMBOL TABLE:':
539             state = 'symbol'
540             continue
541         if line.startswith('RELOCATION RECORDS FOR ['):
542             sectionname = line[24:-2]
543             if sectionname.startswith('.debug_'):
544                 # Skip debugging sections (to reduce parsing time)
545                 state = None
546                 continue
547             state = 'reloc'
548             relocsection = sectionmap[sectionname]
549             continue
550
551         if state == 'section':
552             try:
553                 idx, name, size, vma, lma, fileoff, align = line.split()
554                 if align[:3] != '2**':
555                     continue
556                 section = Section()
557                 section.name = name
558                 section.size = int(size, 16)
559                 section.align = 2**int(align[3:])
560                 section.fileid = fileid
561                 section.relocs = []
562                 sections.append(section)
563                 sectionmap[name] = section
564             except ValueError:
565                 pass
566             continue
567         if state == 'symbol':
568             try:
569                 parts = line[17:].split()
570                 if len(parts) == 3:
571                     sectionname, size, name = parts
572                 elif len(parts) == 4 and parts[2] == '.hidden':
573                     sectionname, size, hidden, name = parts
574                 else:
575                     continue
576                 symbol = Symbol()
577                 symbol.size = int(size, 16)
578                 symbol.offset = int(line[:8], 16)
579                 symbol.name = name
580                 symbol.section = sectionmap.get(sectionname)
581                 symbols[name] = symbol
582             except ValueError:
583                 pass
584             continue
585         if state == 'reloc':
586             try:
587                 off, type, symbolname = line.split()
588                 reloc = Reloc()
589                 reloc.offset = int(off, 16)
590                 reloc.type = type
591                 reloc.symbolname = symbolname
592                 reloc.symbol = symbols.get(symbolname)
593                 if reloc.symbol is None:
594                     # Some binutils (2.20.1) give section name instead
595                     # of a symbol - create a dummy symbol.
596                     reloc.symbol = symbol = Symbol()
597                     symbol.size = 0
598                     symbol.offset = 0
599                     symbol.name = symbolname
600                     symbol.section = sectionmap.get(symbolname)
601                     symbols[symbolname] = symbol
602                 relocsection.relocs.append(reloc)
603             except ValueError:
604                 pass
605     return sections, symbols
606
607 # Parser for constants in simple C header files.
608 def scanconfig(file):
609     f = open(file, 'r')
610     opts = {}
611     for l in f.readlines():
612         parts = l.split()
613         if len(parts) != 3:
614             continue
615         if parts[0] != '#define':
616             continue
617         value = parts[2]
618         if value.isdigit() or (value.startswith('0x') and value[2:].isdigit()):
619             value = int(value, 0)
620         opts[parts[1]] = value
621     return opts
622
623 def main():
624     # Get output name
625     in16, in32seg, in32flat, cfgfile, out16, out32seg, out32flat = sys.argv[1:]
626
627     # Read in the objdump information
628     infile16 = open(in16, 'r')
629     infile32seg = open(in32seg, 'r')
630     infile32flat = open(in32flat, 'r')
631
632     # infoX = (sections, symbols)
633     info16 = parseObjDump(infile16, '16')
634     info32seg = parseObjDump(infile32seg, '32seg')
635     info32flat = parseObjDump(infile32flat, '32flat')
636
637     # Read kconfig config file
638     config = scanconfig(cfgfile)
639
640     # Figure out which sections to keep.
641     allsections = info16[0] + info32seg[0] + info32flat[0]
642     symbols = {'16': info16[1], '32seg': info32seg[1], '32flat': info32flat[1]}
643     if config.get('CONFIG_COREBOOT'):
644         entrysym = symbols['16'].get('entry_elf')
645     elif config.get('CONFIG_CSM'):
646         entrysym = symbols['16'].get('entry_csm')
647     else:
648         entrysym = symbols['16'].get('reset_vector')
649     anchorsections = [entrysym.section] + [
650         section for section in allsections
651         if section.name.startswith('.fixedaddr.')]
652     keepsections = findReachable(anchorsections, checkKeep, symbols)
653     sections = [section for section in allsections if section in keepsections]
654
655     # Separate 32bit flat into runtime, init, and special variable parts
656     anchorsections = [
657         section for section in sections
658         if ('.data.varlow.' in section.name or '.data.varfseg.' in section.name
659             or '.fixedaddr.' in section.name or '.runtime.' in section.name)]
660     runtimesections = findReachable(anchorsections, checkRuntime, None)
661     for section in sections:
662         if section.name.startswith('.data.varlow.'):
663             section.category = '32low'
664         elif section.name.startswith('.data.varfseg.'):
665             section.category = '32fseg'
666         elif section.name.startswith('.text.32fseg.'):
667             section.category = '32textfseg'
668         elif section.name.startswith('.fixedaddr.'):
669             section.category = 'fixed'
670         elif section.fileid == '32flat' and section not in runtimesections:
671             section.category = '32init'
672         else:
673             section.category = section.fileid
674
675     # Determine the final memory locations of each kept section.
676     genreloc = '_reloc_abs_start' in symbols['32flat']
677     li = doLayout(sections, config, genreloc)
678
679     # Exported symbols
680     li.varlowsyms = [symbol for symbol in symbols['32flat'].values()
681                      if (symbol.section is not None
682                          and symbol.section.finalloc is not None
683                          and '.data.varlow.' in symbol.section.name
684                          and symbol.name != symbol.section.name)]
685     li.entrysym = entrysym
686
687     # Write out linker script files.
688     writeLinkerScripts(li, out16, out32seg, out32flat)
689
690 if __name__ == '__main__':
691     main()