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