Annotation of qemu/roms/seabios/tools/layoutrom.py, revision 1.1.1.6

1.1       root        1: #!/usr/bin/env python
                      2: # Script to analyze code and arrange ld sections.
                      3: #
1.1.1.4   root        4: # Copyright (C) 2008-2010  Kevin O'Connor <[email protected]>
1.1       root        5: #
                      6: # This file may be distributed under the terms of the GNU GPLv3 license.
                      7: 
                      8: import sys
                      9: 
                     10: # LD script headers/trailers
                     11: COMMONHEADER = """
                     12: /* DO NOT EDIT!  This is an autogenerated file.  See tools/layoutrom.py. */
                     13: OUTPUT_FORMAT("elf32-i386")
                     14: OUTPUT_ARCH("i386")
                     15: SECTIONS
                     16: {
                     17: """
                     18: COMMONTRAILER = """
1.1.1.3   root       19: 
                     20:         /* Discard regular data sections to force a link error if
                     21:          * code attempts to access data not marked with VAR16 (or other
                     22:          * appropriate macro)
                     23:          */
                     24:         /DISCARD/ : {
                     25:                 *(.text*) *(.data*) *(.bss*) *(.rodata*)
                     26:                 *(COMMON) *(.discard*) *(.eh_frame)
                     27:                 }
1.1       root       28: }
                     29: """
                     30: 
                     31: 
                     32: ######################################################################
1.1.1.3   root       33: # Determine section locations
1.1       root       34: ######################################################################
                     35: 
1.1.1.3   root       36: # Align 'pos' to 'alignbytes' offset
                     37: def alignpos(pos, alignbytes):
                     38:     mask = alignbytes - 1
                     39:     return (pos + mask) & ~mask
                     40: 
                     41: # Determine the final addresses for a list of sections that end at an
1.1       root       42: # address.
1.1.1.4   root       43: def setSectionsStart(sections, endaddr, minalign=1):
1.1       root       44:     totspace = 0
1.1.1.4   root       45:     for section in sections:
                     46:         if section.align > minalign:
                     47:             minalign = section.align
                     48:         totspace = alignpos(totspace, section.align) + section.size
1.1.1.3   root       49:     startaddr = (endaddr - totspace) / minalign * minalign
                     50:     curaddr = startaddr
                     51:     # out = [(addr, sectioninfo), ...]
                     52:     out = []
1.1.1.4   root       53:     for section in sections:
                     54:         curaddr = alignpos(curaddr, section.align)
                     55:         section.finalloc = curaddr
                     56:         curaddr += section.size
                     57:     return startaddr
1.1       root       58: 
                     59: # The 16bit code can't exceed 64K of space.
1.1.1.3   root       60: BUILD_BIOS_ADDR = 0xf0000
                     61: BUILD_BIOS_SIZE = 0x10000
1.1       root       62: 
                     63: # Layout the 16bit code.  This ensures sections with fixed offset
                     64: # requirements are placed in the correct location.  It also places the
                     65: # 16bit code as high as possible in the f-segment.
1.1.1.3   root       66: def fitSections(sections, fillsections):
1.1.1.4   root       67:     # fixedsections = [(addr, section), ...]
1.1       root       68:     fixedsections = []
1.1.1.4   root       69:     for section in sections:
                     70:         if section.name.startswith('.fixedaddr.'):
                     71:             addr = int(section.name[11:], 16)
                     72:             section.finalloc = addr
                     73:             fixedsections.append((addr, section))
                     74:             if section.align != 1:
1.1       root       75:                 print "Error: Fixed section %s has non-zero alignment (%d)" % (
1.1.1.4   root       76:                     section.name, section.align)
1.1       root       77:                 sys.exit(1)
1.1.1.4   root       78:     fixedsections.sort()
                     79:     firstfixed = fixedsections[0][0]
1.1       root       80: 
                     81:     # Find freespace in fixed address area
1.1.1.4   root       82:     # fixedAddr = [(freespace, section), ...]
1.1       root       83:     fixedAddr = []
                     84:     for i in range(len(fixedsections)):
                     85:         fixedsectioninfo = fixedsections[i]
1.1.1.3   root       86:         addr, section = fixedsectioninfo
1.1       root       87:         if i == len(fixedsections) - 1:
1.1.1.3   root       88:             nextaddr = BUILD_BIOS_SIZE
1.1       root       89:         else:
                     90:             nextaddr = fixedsections[i+1][0]
1.1.1.4   root       91:         avail = nextaddr - addr - section.size
                     92:         fixedAddr.append((avail, section))
                     93:     fixedAddr.sort()
1.1       root       94: 
                     95:     # Attempt to fit other sections into fixed area
1.1.1.4   root       96:     canrelocate = [(section.size, section.align, section.name, section)
                     97:                    for section in fillsections]
1.1       root       98:     canrelocate.sort()
1.1.1.4   root       99:     canrelocate = [section for size, align, name, section in canrelocate]
1.1       root      100:     totalused = 0
1.1.1.4   root      101:     for freespace, fixedsection in fixedAddr:
                    102:         addpos = fixedsection.finalloc + fixedsection.size
                    103:         totalused += fixedsection.size
1.1       root      104:         nextfixedaddr = addpos + freespace
                    105: #        print "Filling section %x uses %d, next=%x, available=%d" % (
1.1.1.4   root      106: #            fixedsection.finalloc, fixedsection.size, nextfixedaddr, freespace)
1.1       root      107:         while 1:
                    108:             canfit = None
1.1.1.3   root      109:             for fitsection in canrelocate:
1.1.1.4   root      110:                 if addpos + fitsection.size > nextfixedaddr:
1.1       root      111:                     # Can't fit and nothing else will fit.
                    112:                     break
1.1.1.4   root      113:                 fitnextaddr = alignpos(addpos, fitsection.align) + fitsection.size
1.1       root      114: #                print "Test %s - %x vs %x" % (
1.1.1.4   root      115: #                    fitsection.name, fitnextaddr, nextfixedaddr)
1.1       root      116:                 if fitnextaddr > nextfixedaddr:
                    117:                     # This item can't fit.
                    118:                     continue
1.1.1.3   root      119:                 canfit = (fitnextaddr, fitsection)
1.1       root      120:             if canfit is None:
                    121:                 break
                    122:             # Found a section that can fit.
1.1.1.3   root      123:             fitnextaddr, fitsection = canfit
                    124:             canrelocate.remove(fitsection)
1.1.1.4   root      125:             fitsection.finalloc = addpos
1.1       root      126:             addpos = fitnextaddr
1.1.1.4   root      127:             totalused += fitsection.size
1.1       root      128: #            print "    Adding %s (size %d align %d) pos=%x avail=%d" % (
                    129: #                fitsection[2], fitsection[0], fitsection[1]
                    130: #                , fitnextaddr, nextfixedaddr - fitnextaddr)
                    131: 
                    132:     # Report stats
1.1.1.3   root      133:     total = BUILD_BIOS_SIZE-firstfixed
1.1       root      134:     slack = total - totalused
                    135:     print ("Fixed space: 0x%x-0x%x  total: %d  slack: %d"
                    136:            "  Percent slack: %.1f%%" % (
1.1.1.3   root      137:             firstfixed, BUILD_BIOS_SIZE, total, slack,
1.1       root      138:             (float(slack) / total) * 100.0))
                    139: 
1.1.1.4   root      140:     return firstfixed
1.1       root      141: 
1.1.1.4   root      142: # Return the subset of sections with a given name prefix
                    143: def getSectionsPrefix(sections, category, prefix):
                    144:     return [section for section in sections
                    145:             if section.category == category and section.name.startswith(prefix)]
                    146: 
                    147: def doLayout(sections):
1.1.1.3   root      148:     # Determine 16bit positions
1.1.1.4   root      149:     textsections = getSectionsPrefix(sections, '16', '.text.')
1.1.1.6 ! root      150:     rodatasections = (
        !           151:         getSectionsPrefix(sections, '16', '.rodata.str1.1')
        !           152:         + getSectionsPrefix(sections, '16', '.rodata.__func__.')
        !           153:         + getSectionsPrefix(sections, '16', '.rodata.__PRETTY_FUNCTION__.'))
1.1.1.4   root      154:     datasections = getSectionsPrefix(sections, '16', '.data16.')
                    155:     fixedsections = getSectionsPrefix(sections, '16', '.fixedaddr.')
                    156: 
                    157:     firstfixed = fitSections(fixedsections, textsections)
                    158:     remsections = [s for s in textsections+rodatasections+datasections
                    159:                    if s.finalloc is None]
                    160:     code16_start = setSectionsStart(remsections, firstfixed)
1.1.1.3   root      161: 
                    162:     # Determine 32seg positions
1.1.1.4   root      163:     textsections = getSectionsPrefix(sections, '32seg', '.text.')
1.1.1.6 ! root      164:     rodatasections = (
        !           165:         getSectionsPrefix(sections, '32seg', '.rodata.str1.1')
        !           166:         + getSectionsPrefix(sections, '32seg', '.rodata.__func__.')
        !           167:         + getSectionsPrefix(sections, '32seg', '.rodata.__PRETTY_FUNCTION__.'))
1.1.1.4   root      168:     datasections = getSectionsPrefix(sections, '32seg', '.data32seg.')
1.1.1.3   root      169: 
1.1.1.4   root      170:     code32seg_start = setSectionsStart(
1.1.1.3   root      171:         textsections + rodatasections + datasections, code16_start)
                    172: 
1.1.1.4   root      173:     # Determine 32flat runtime positions
                    174:     textsections = getSectionsPrefix(sections, '32flat', '.text.')
                    175:     rodatasections = getSectionsPrefix(sections, '32flat', '.rodata')
                    176:     datasections = getSectionsPrefix(sections, '32flat', '.data.')
                    177:     bsssections = getSectionsPrefix(sections, '32flat', '.bss.')
1.1.1.3   root      178: 
1.1.1.4   root      179:     code32flat_start = setSectionsStart(
1.1.1.3   root      180:         textsections + rodatasections + datasections + bsssections
                    181:         , code32seg_start + BUILD_BIOS_ADDR, 16)
                    182: 
1.1.1.4   root      183:     # Determine 32flat init positions
                    184:     textsections = getSectionsPrefix(sections, '32init', '.text.')
                    185:     rodatasections = getSectionsPrefix(sections, '32init', '.rodata')
                    186:     datasections = getSectionsPrefix(sections, '32init', '.data.')
                    187:     bsssections = getSectionsPrefix(sections, '32init', '.bss.')
                    188: 
                    189:     code32init_start = setSectionsStart(
                    190:         textsections + rodatasections + datasections + bsssections
                    191:         , code32flat_start, 16)
                    192: 
1.1.1.3   root      193:     # Print statistics
                    194:     size16 = BUILD_BIOS_SIZE - code16_start
                    195:     size32seg = code16_start - code32seg_start
                    196:     size32flat = code32seg_start + BUILD_BIOS_ADDR - code32flat_start
1.1.1.4   root      197:     size32init = code32flat_start - code32init_start
1.1.1.3   root      198:     print "16bit size:           %d" % size16
                    199:     print "32bit segmented size: %d" % size32seg
                    200:     print "32bit flat size:      %d" % size32flat
1.1.1.4   root      201:     print "32bit flat init size: %d" % size32init
1.1       root      202: 
                    203: 
                    204: ######################################################################
1.1.1.3   root      205: # Linker script output
1.1       root      206: ######################################################################
                    207: 
1.1.1.3   root      208: # Write LD script includes for the given cross references
1.1.1.4   root      209: def outXRefs(sections):
                    210:     xrefs = {}
1.1.1.3   root      211:     out = ""
1.1.1.4   root      212:     for section in sections:
                    213:         for reloc in section.relocs:
                    214:             symbol = reloc.symbol
                    215:             if (symbol.section is None
                    216:                 or (symbol.section.fileid == section.fileid
                    217:                     and symbol.name == reloc.symbolname)
                    218:                 or reloc.symbolname in xrefs):
                    219:                 continue
                    220:             xrefs[reloc.symbolname] = 1
                    221:             addr = symbol.section.finalloc + symbol.offset
                    222:             if (section.fileid == '32flat'
                    223:                 and symbol.section.fileid in ('16', '32seg')):
                    224:                 addr += BUILD_BIOS_ADDR
                    225:             out += "%s = 0x%x ;\n" % (reloc.symbolname, addr)
1.1.1.3   root      226:     return out
                    227: 
                    228: # Write LD script includes for the given sections using relative offsets
1.1.1.4   root      229: def outRelSections(sections, startsym):
1.1.1.3   root      230:     out = ""
1.1.1.4   root      231:     for section in sections:
                    232:         out += ". = ( 0x%x - %s ) ;\n" % (section.finalloc, startsym)
                    233:         if section.name == '.rodata.str1.1':
1.1.1.3   root      234:             out += "_rodata = . ;\n"
1.1.1.4   root      235:         out += "*(%s)\n" % (section.name,)
1.1       root      236:     return out
                    237: 
1.1.1.4   root      238: def getSectionsFile(sections, fileid, defaddr=0):
                    239:     sections = [(section.finalloc, section)
                    240:                 for section in sections if section.fileid == fileid]
                    241:     sections.sort()
                    242:     sections = [section for addr, section in sections]
                    243:     pos = defaddr
                    244:     if sections:
                    245:         pos = sections[0].finalloc
                    246:     return sections, pos
1.1.1.3   root      247: 
1.1.1.4   root      248: # Layout the 32bit segmented code.  This places the code as high as possible.
                    249: def writeLinkerScripts(sections, entrysym, genreloc, out16, out32seg, out32flat):
1.1.1.3   root      250:     # Write 16bit linker script
1.1.1.4   root      251:     sections16, code16_start = getSectionsFile(sections, '16')
1.1.1.3   root      252:     output = open(out16, 'wb')
1.1.1.4   root      253:     output.write(COMMONHEADER + outXRefs(sections16) + """
1.1.1.3   root      254:     code16_start = 0x%x ;
                    255:     .text16 code16_start : {
                    256: """ % (code16_start)
1.1.1.4   root      257:                  + outRelSections(sections16, 'code16_start')
1.1.1.3   root      258:                  + """
                    259:     }
                    260: """
                    261:                  + COMMONTRAILER)
                    262:     output.close()
                    263: 
                    264:     # Write 32seg linker script
1.1.1.4   root      265:     sections32seg, code32seg_start = getSectionsFile(
                    266:         sections, '32seg', code16_start)
1.1.1.3   root      267:     output = open(out32seg, 'wb')
1.1.1.4   root      268:     output.write(COMMONHEADER + outXRefs(sections32seg) + """
1.1.1.3   root      269:     code32seg_start = 0x%x ;
                    270:     .text32seg code32seg_start : {
                    271: """ % (code32seg_start)
1.1.1.4   root      272:                  + outRelSections(sections32seg, 'code32seg_start')
1.1.1.3   root      273:                  + """
                    274:     }
                    275: """
                    276:                  + COMMONTRAILER)
                    277:     output.close()
                    278: 
                    279:     # Write 32flat linker script
1.1.1.4   root      280:     sections32flat, code32flat_start = getSectionsFile(
                    281:         sections, '32flat', code32seg_start)
                    282:     relocstr = ""
                    283:     relocminalign = 0
                    284:     if genreloc:
                    285:         # Generate relocations
                    286:         relocstr, size, relocminalign = genRelocs(sections)
                    287:         code32flat_start -= size
1.1.1.3   root      288:     output = open(out32flat, 'wb')
                    289:     output.write(COMMONHEADER
1.1.1.4   root      290:                  + outXRefs(sections32flat) + """
                    291:     %s = 0x%x ;
                    292:     _reloc_min_align = 0x%x ;
1.1.1.3   root      293:     code32flat_start = 0x%x ;
                    294:     .text code32flat_start : {
1.1.1.4   root      295: """ % (entrysym.name,
                    296:        entrysym.section.finalloc + entrysym.offset + BUILD_BIOS_ADDR,
                    297:        relocminalign, code32flat_start)
                    298:                  + relocstr
                    299:                  + """
                    300:         code32init_start = ABSOLUTE(.) ;
                    301: """
                    302:                  + outRelSections(getSectionsPrefix(sections32flat, '32init', '')
                    303:                                   , 'code32flat_start')
                    304:                  + """
                    305:         code32init_end = ABSOLUTE(.) ;
                    306: """
                    307:                  + outRelSections(getSectionsPrefix(sections32flat, '32flat', '')
                    308:                                   , 'code32flat_start')
1.1.1.3   root      309:                  + """
                    310:         . = ( 0x%x - code32flat_start ) ;
                    311:         *(.text32seg)
                    312:         . = ( 0x%x - code32flat_start ) ;
                    313:         *(.text16)
                    314:         code32flat_end = ABSOLUTE(.) ;
                    315:     } :text
                    316: """ % (code32seg_start + BUILD_BIOS_ADDR, code16_start + BUILD_BIOS_ADDR)
                    317:                  + COMMONTRAILER
                    318:                  + """
1.1.1.4   root      319: ENTRY(%s)
1.1.1.3   root      320: PHDRS
                    321: {
                    322:         text PT_LOAD AT ( code32flat_start ) ;
                    323: }
1.1.1.4   root      324: """ % (entrysym.name,))
1.1.1.3   root      325:     output.close()
1.1       root      326: 
                    327: 
                    328: ######################################################################
1.1.1.4   root      329: # Detection of init code
                    330: ######################################################################
                    331: 
                    332: # Determine init section relocations
                    333: def genRelocs(sections):
                    334:     absrelocs = []
                    335:     relrelocs = []
                    336:     initrelocs = []
                    337:     minalign = 16
                    338:     for section in sections:
                    339:         if section.category == '32init' and section.align > minalign:
                    340:             minalign = section.align
                    341:         for reloc in section.relocs:
                    342:             symbol = reloc.symbol
                    343:             if symbol.section is None:
                    344:                 continue
                    345:             relocpos = section.finalloc + reloc.offset
                    346:             if (reloc.type == 'R_386_32' and section.category == '32init'
                    347:                 and symbol.section.category == '32init'):
                    348:                 # Absolute relocation
                    349:                 absrelocs.append(relocpos)
                    350:             elif (reloc.type == 'R_386_PC32' and section.category == '32init'
                    351:                   and symbol.section.category != '32init'):
                    352:                 # Relative relocation
                    353:                 relrelocs.append(relocpos)
                    354:             elif (section.category != '32init'
                    355:                   and symbol.section.category == '32init'):
                    356:                 # Relocation to the init section
                    357:                 if section.fileid in ('16', '32seg'):
                    358:                     relocpos += BUILD_BIOS_ADDR
                    359:                 initrelocs.append(relocpos)
                    360:     absrelocs.sort()
                    361:     relrelocs.sort()
                    362:     initrelocs.sort()
                    363:     out = ("        _reloc_abs_start = ABSOLUTE(.) ;\n"
                    364:            + "".join(["LONG(0x%x - code32init_start)\n" % (pos,)
                    365:                       for pos in absrelocs])
                    366:            + "        _reloc_abs_end = ABSOLUTE(.) ;\n"
                    367:            + "        _reloc_rel_start = ABSOLUTE(.) ;\n"
                    368:            + "".join(["LONG(0x%x - code32init_start)\n" % (pos,)
                    369:                       for pos in relrelocs])
                    370:            + "        _reloc_rel_end = ABSOLUTE(.) ;\n"
                    371:            + "        _reloc_init_start = ABSOLUTE(.) ;\n"
                    372:            + "".join(["LONG(0x%x - code32flat_start)\n" % (pos,)
                    373:                       for pos in initrelocs])
                    374:            + "        _reloc_init_end = ABSOLUTE(.) ;\n")
                    375:     return out, len(absrelocs + relrelocs + initrelocs) * 4, minalign
                    376: 
                    377: def markRuntime(section, sections):
                    378:     if (section is None or not section.keep or section.category is not None
                    379:         or '.init.' in section.name or section.fileid != '32flat'):
                    380:         return
                    381:     section.category = '32flat'
                    382:     # Recursively mark all sections this section points to
                    383:     for reloc in section.relocs:
                    384:         markRuntime(reloc.symbol.section, sections)
                    385: 
                    386: def findInit(sections):
                    387:     # Recursively find and mark all "runtime" sections.
                    388:     for section in sections:
                    389:         if '.runtime.' in section.name or '.export.' in section.name:
                    390:             markRuntime(section, sections)
                    391:     for section in sections:
                    392:         if section.category is not None:
                    393:             continue
                    394:         if section.fileid == '32flat':
                    395:             section.category = '32init'
                    396:         else:
                    397:             section.category = section.fileid
                    398: 
                    399: 
                    400: ######################################################################
1.1       root      401: # Section garbage collection
                    402: ######################################################################
                    403: 
1.1.1.4   root      404: CFUNCPREFIX = [('_cfunc16_', 0), ('_cfunc32seg_', 1), ('_cfunc32flat_', 2)]
                    405: 
1.1.1.2   root      406: # Find and keep the section associated with a symbol (if available).
1.1.1.4   root      407: def keepsymbol(reloc, infos, pos, isxref):
                    408:     symbolname = reloc.symbolname
                    409:     mustbecfunc = 0
                    410:     for symprefix, needpos in CFUNCPREFIX:
                    411:         if symbolname.startswith(symprefix):
                    412:             if needpos != pos:
                    413:                 return -1
                    414:             symbolname = symbolname[len(symprefix):]
                    415:             mustbecfunc = 1
                    416:             break
                    417:     symbol = infos[pos][1].get(symbolname)
                    418:     if (symbol is None or symbol.section is None
                    419:         or symbol.section.name.startswith('.discard.')):
1.1.1.2   root      420:         return -1
1.1.1.4   root      421:     isdestcfunc = (symbol.section.name.startswith('.text.')
                    422:                    and not symbol.section.name.startswith('.text.asm.'))
                    423:     if ((mustbecfunc and not isdestcfunc)
                    424:         or (not mustbecfunc and isdestcfunc and isxref)):
                    425:         return -1
                    426: 
                    427:     reloc.symbol = symbol
                    428:     keepsection(symbol.section, infos, pos)
1.1.1.2   root      429:     return 0
                    430: 
1.1       root      431: # Note required section, and recursively set all referenced sections
                    432: # as required.
1.1.1.4   root      433: def keepsection(section, infos, pos=0):
                    434:     if section.keep:
1.1       root      435:         # Already kept - nothing to do.
                    436:         return
1.1.1.4   root      437:     section.keep = 1
1.1       root      438:     # Keep all sections that this section points to
1.1.1.4   root      439:     for reloc in section.relocs:
                    440:         ret = keepsymbol(reloc, infos, pos, 0)
1.1.1.2   root      441:         if not ret:
1.1       root      442:             continue
                    443:         # Not in primary sections - it may be a cross 16/32 reference
1.1.1.4   root      444:         ret = keepsymbol(reloc, infos, (pos+1)%3, 1)
1.1.1.2   root      445:         if not ret:
                    446:             continue
1.1.1.4   root      447:         ret = keepsymbol(reloc, infos, (pos+2)%3, 1)
1.1.1.2   root      448:         if not ret:
                    449:             continue
1.1       root      450: 
                    451: # Determine which sections are actually referenced and need to be
                    452: # placed into the output file.
1.1.1.2   root      453: def gc(info16, info32seg, info32flat):
1.1.1.4   root      454:     # infos = ((sections16, symbols16), (sect32seg, sym32seg)
                    455:     #          , (sect32flat, sym32flat))
                    456:     infos = (info16, info32seg, info32flat)
1.1       root      457:     # Start by keeping sections that are globally visible.
1.1.1.4   root      458:     for section in info16[0]:
                    459:         if section.name.startswith('.fixedaddr.') or '.export.' in section.name:
1.1.1.2   root      460:             keepsection(section, infos)
1.1.1.4   root      461:     return [section for section in info16[0]+info32seg[0]+info32flat[0]
                    462:             if section.keep]
1.1       root      463: 
                    464: 
                    465: ######################################################################
                    466: # Startup and input parsing
                    467: ######################################################################
                    468: 
1.1.1.4   root      469: class Section:
                    470:     name = size = alignment = fileid = relocs = None
                    471:     finalloc = category = keep = None
                    472: class Reloc:
                    473:     offset = type = symbolname = symbol = None
                    474: class Symbol:
                    475:     name = offset = section = None
                    476: 
1.1       root      477: # Read in output from objdump
1.1.1.4   root      478: def parseObjDump(file, fileid):
                    479:     # sections = [section, ...]
1.1       root      480:     sections = []
1.1.1.4   root      481:     sectionmap = {}
                    482:     # symbols[symbolname] = symbol
1.1       root      483:     symbols = {}
                    484: 
                    485:     state = None
                    486:     for line in file.readlines():
                    487:         line = line.rstrip()
                    488:         if line == 'Sections:':
                    489:             state = 'section'
                    490:             continue
                    491:         if line == 'SYMBOL TABLE:':
                    492:             state = 'symbol'
                    493:             continue
1.1.1.4   root      494:         if line.startswith('RELOCATION RECORDS FOR ['):
                    495:             sectionname = line[24:-2]
                    496:             if sectionname.startswith('.debug_'):
                    497:                 # Skip debugging sections (to reduce parsing time)
                    498:                 state = None
                    499:                 continue
1.1       root      500:             state = 'reloc'
1.1.1.4   root      501:             relocsection = sectionmap[sectionname]
1.1       root      502:             continue
                    503: 
                    504:         if state == 'section':
                    505:             try:
                    506:                 idx, name, size, vma, lma, fileoff, align = line.split()
                    507:                 if align[:3] != '2**':
                    508:                     continue
1.1.1.4   root      509:                 section = Section()
                    510:                 section.name = name
                    511:                 section.size = int(size, 16)
                    512:                 section.align = 2**int(align[3:])
                    513:                 section.fileid = fileid
                    514:                 section.relocs = []
                    515:                 sections.append(section)
                    516:                 sectionmap[name] = section
                    517:             except ValueError:
1.1       root      518:                 pass
                    519:             continue
                    520:         if state == 'symbol':
                    521:             try:
1.1.1.4   root      522:                 sectionname, size, name = line[17:].split()
                    523:                 symbol = Symbol()
                    524:                 symbol.size = int(size, 16)
                    525:                 symbol.offset = int(line[:8], 16)
                    526:                 symbol.name = name
                    527:                 symbol.section = sectionmap.get(sectionname)
                    528:                 symbols[name] = symbol
                    529:             except ValueError:
1.1       root      530:                 pass
                    531:             continue
                    532:         if state == 'reloc':
                    533:             try:
1.1.1.4   root      534:                 off, type, symbolname = line.split()
                    535:                 reloc = Reloc()
                    536:                 reloc.offset = int(off, 16)
                    537:                 reloc.type = type
                    538:                 reloc.symbolname = symbolname
                    539:                 reloc.symbol = symbols.get(symbolname)
                    540:                 if reloc.symbol is None:
                    541:                     # Some binutils (2.20.1) give section name instead
                    542:                     # of a symbol - create a dummy symbol.
                    543:                     reloc.symbol = symbol = Symbol()
                    544:                     symbol.size = 0
                    545:                     symbol.offset = 0
                    546:                     symbol.name = symbolname
                    547:                     symbol.section = sectionmap.get(symbolname)
                    548:                     symbols[symbolname] = symbol
                    549:                 relocsection.relocs.append(reloc)
                    550:             except ValueError:
1.1       root      551:                 pass
1.1.1.4   root      552:     return sections, symbols
1.1       root      553: 
                    554: def main():
                    555:     # Get output name
1.1.1.2   root      556:     in16, in32seg, in32flat, out16, out32seg, out32flat = sys.argv[1:]
1.1       root      557: 
1.1.1.3   root      558:     # Read in the objdump information
1.1       root      559:     infile16 = open(in16, 'rb')
1.1.1.2   root      560:     infile32seg = open(in32seg, 'rb')
                    561:     infile32flat = open(in32flat, 'rb')
1.1       root      562: 
1.1.1.4   root      563:     # infoX = (sections, symbols)
                    564:     info16 = parseObjDump(infile16, '16')
                    565:     info32seg = parseObjDump(infile32seg, '32seg')
                    566:     info32flat = parseObjDump(infile32flat, '32flat')
1.1       root      567: 
1.1.1.3   root      568:     # Figure out which sections to keep.
1.1.1.4   root      569:     sections = gc(info16, info32seg, info32flat)
                    570: 
                    571:     # Separate 32bit flat into runtime and init parts
                    572:     findInit(sections)
1.1.1.3   root      573: 
                    574:     # Determine the final memory locations of each kept section.
1.1.1.4   root      575:     doLayout(sections)
1.1.1.3   root      576: 
                    577:     # Write out linker script files.
1.1.1.5   root      578:     entrysym = info16[1]['entry_elf']
1.1.1.4   root      579:     genreloc = '_reloc_abs_start' in info32flat[1]
                    580:     writeLinkerScripts(sections, entrysym, genreloc, out16, out32seg, out32flat)
1.1       root      581: 
                    582: if __name__ == '__main__':
                    583:     main()

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.