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*) |
Kevin O'Connor | 90ebed4 | 2012-06-21 20:54:53 -0400 | [diff] [blame] | 26 | *(COMMON) *(.discard*) *(.eh_frame) *(.note*) |
Kevin O'Connor | 9ba1dea | 2010-05-01 09:50:13 -0400 | [diff] [blame] | 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 | 46b8262 | 2012-05-13 12:10:30 -0400 | [diff] [blame] | 43 | def setSectionsStart(sections, endaddr, minalign=1, segoffset=0): |
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 |
Kevin O'Connor | 1a4885e | 2010-09-15 21:28:31 -0400 | [diff] [blame] | 51 | for section in sections: |
| 52 | curaddr = alignpos(curaddr, section.align) |
| 53 | section.finalloc = curaddr |
Kevin O'Connor | 46b8262 | 2012-05-13 12:10:30 -0400 | [diff] [blame] | 54 | section.finalsegloc = curaddr - segoffset |
Kevin O'Connor | 1a4885e | 2010-09-15 21:28:31 -0400 | [diff] [blame] | 55 | curaddr += section.size |
Kevin O'Connor | 46b8262 | 2012-05-13 12:10:30 -0400 | [diff] [blame] | 56 | return startaddr, minalign |
Kevin O'Connor | c069394 | 2009-06-10 21:56:01 -0400 | [diff] [blame] | 57 | |
Kevin O'Connor | 9ba1dea | 2010-05-01 09:50:13 -0400 | [diff] [blame] | 58 | # The 16bit code can't exceed 64K of space. |
| 59 | BUILD_BIOS_ADDR = 0xf0000 |
| 60 | BUILD_BIOS_SIZE = 0x10000 |
Kevin O'Connor | 46b8262 | 2012-05-13 12:10:30 -0400 | [diff] [blame] | 61 | BUILD_ROM_START = 0xc0000 |
Kevin O'Connor | 9ba1dea | 2010-05-01 09:50:13 -0400 | [diff] [blame] | 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) |
Kevin O'Connor | 46b8262 | 2012-05-13 12:10:30 -0400 | [diff] [blame] | 72 | section.finalloc = addr + BUILD_BIOS_ADDR |
| 73 | section.finalsegloc = addr |
Kevin O'Connor | 1a4885e | 2010-09-15 21:28:31 -0400 | [diff] [blame] | 74 | fixedsections.append((addr, section)) |
| 75 | if section.align != 1: |
Kevin O'Connor | 9ba1dea | 2010-05-01 09:50:13 -0400 | [diff] [blame] | 76 | print "Error: Fixed section %s has non-zero alignment (%d)" % ( |
Kevin O'Connor | 1a4885e | 2010-09-15 21:28:31 -0400 | [diff] [blame] | 77 | section.name, section.align) |
Kevin O'Connor | 9ba1dea | 2010-05-01 09:50:13 -0400 | [diff] [blame] | 78 | sys.exit(1) |
Kevin O'Connor | 1a4885e | 2010-09-15 21:28:31 -0400 | [diff] [blame] | 79 | fixedsections.sort() |
| 80 | firstfixed = fixedsections[0][0] |
Kevin O'Connor | 9ba1dea | 2010-05-01 09:50:13 -0400 | [diff] [blame] | 81 | |
| 82 | # Find freespace in fixed address area |
Kevin O'Connor | 1a4885e | 2010-09-15 21:28:31 -0400 | [diff] [blame] | 83 | # fixedAddr = [(freespace, section), ...] |
Kevin O'Connor | 9ba1dea | 2010-05-01 09:50:13 -0400 | [diff] [blame] | 84 | fixedAddr = [] |
| 85 | for i in range(len(fixedsections)): |
| 86 | fixedsectioninfo = fixedsections[i] |
| 87 | addr, section = fixedsectioninfo |
| 88 | if i == len(fixedsections) - 1: |
| 89 | nextaddr = BUILD_BIOS_SIZE |
| 90 | else: |
| 91 | nextaddr = fixedsections[i+1][0] |
Kevin O'Connor | 1a4885e | 2010-09-15 21:28:31 -0400 | [diff] [blame] | 92 | avail = nextaddr - addr - section.size |
| 93 | fixedAddr.append((avail, section)) |
| 94 | fixedAddr.sort() |
Kevin O'Connor | 9ba1dea | 2010-05-01 09:50:13 -0400 | [diff] [blame] | 95 | |
| 96 | # Attempt to fit other sections into fixed area |
Kevin O'Connor | 1a4885e | 2010-09-15 21:28:31 -0400 | [diff] [blame] | 97 | canrelocate = [(section.size, section.align, section.name, section) |
| 98 | for section in fillsections] |
Kevin O'Connor | 9ba1dea | 2010-05-01 09:50:13 -0400 | [diff] [blame] | 99 | canrelocate.sort() |
Kevin O'Connor | 1a4885e | 2010-09-15 21:28:31 -0400 | [diff] [blame] | 100 | canrelocate = [section for size, align, name, section in canrelocate] |
Kevin O'Connor | 9ba1dea | 2010-05-01 09:50:13 -0400 | [diff] [blame] | 101 | totalused = 0 |
Kevin O'Connor | 1a4885e | 2010-09-15 21:28:31 -0400 | [diff] [blame] | 102 | for freespace, fixedsection in fixedAddr: |
Kevin O'Connor | 46b8262 | 2012-05-13 12:10:30 -0400 | [diff] [blame] | 103 | addpos = fixedsection.finalsegloc + fixedsection.size |
Kevin O'Connor | 1a4885e | 2010-09-15 21:28:31 -0400 | [diff] [blame] | 104 | totalused += fixedsection.size |
Kevin O'Connor | 9ba1dea | 2010-05-01 09:50:13 -0400 | [diff] [blame] | 105 | nextfixedaddr = addpos + freespace |
| 106 | # print "Filling section %x uses %d, next=%x, available=%d" % ( |
Kevin O'Connor | 1a4885e | 2010-09-15 21:28:31 -0400 | [diff] [blame] | 107 | # fixedsection.finalloc, fixedsection.size, nextfixedaddr, freespace) |
Kevin O'Connor | 9ba1dea | 2010-05-01 09:50:13 -0400 | [diff] [blame] | 108 | while 1: |
| 109 | canfit = None |
| 110 | for fitsection in canrelocate: |
Kevin O'Connor | 1a4885e | 2010-09-15 21:28:31 -0400 | [diff] [blame] | 111 | if addpos + fitsection.size > nextfixedaddr: |
Kevin O'Connor | 9ba1dea | 2010-05-01 09:50:13 -0400 | [diff] [blame] | 112 | # Can't fit and nothing else will fit. |
| 113 | break |
Kevin O'Connor | 1a4885e | 2010-09-15 21:28:31 -0400 | [diff] [blame] | 114 | fitnextaddr = alignpos(addpos, fitsection.align) + fitsection.size |
Kevin O'Connor | 9ba1dea | 2010-05-01 09:50:13 -0400 | [diff] [blame] | 115 | # print "Test %s - %x vs %x" % ( |
Kevin O'Connor | 1a4885e | 2010-09-15 21:28:31 -0400 | [diff] [blame] | 116 | # fitsection.name, fitnextaddr, nextfixedaddr) |
Kevin O'Connor | 9ba1dea | 2010-05-01 09:50:13 -0400 | [diff] [blame] | 117 | if fitnextaddr > nextfixedaddr: |
| 118 | # This item can't fit. |
| 119 | continue |
| 120 | canfit = (fitnextaddr, fitsection) |
| 121 | if canfit is None: |
| 122 | break |
| 123 | # Found a section that can fit. |
| 124 | fitnextaddr, fitsection = canfit |
| 125 | canrelocate.remove(fitsection) |
Kevin O'Connor | 46b8262 | 2012-05-13 12:10:30 -0400 | [diff] [blame] | 126 | fitsection.finalloc = addpos + BUILD_BIOS_ADDR |
| 127 | fitsection.finalsegloc = addpos |
Kevin O'Connor | 9ba1dea | 2010-05-01 09:50:13 -0400 | [diff] [blame] | 128 | addpos = fitnextaddr |
Kevin O'Connor | 1a4885e | 2010-09-15 21:28:31 -0400 | [diff] [blame] | 129 | totalused += fitsection.size |
Kevin O'Connor | 9ba1dea | 2010-05-01 09:50:13 -0400 | [diff] [blame] | 130 | # print " Adding %s (size %d align %d) pos=%x avail=%d" % ( |
| 131 | # fitsection[2], fitsection[0], fitsection[1] |
| 132 | # , fitnextaddr, nextfixedaddr - fitnextaddr) |
Kevin O'Connor | 9ba1dea | 2010-05-01 09:50:13 -0400 | [diff] [blame] | 133 | |
| 134 | # Report stats |
| 135 | total = BUILD_BIOS_SIZE-firstfixed |
| 136 | slack = total - totalused |
| 137 | print ("Fixed space: 0x%x-0x%x total: %d slack: %d" |
| 138 | " Percent slack: %.1f%%" % ( |
| 139 | firstfixed, BUILD_BIOS_SIZE, total, slack, |
| 140 | (float(slack) / total) * 100.0)) |
| 141 | |
Kevin O'Connor | 46b8262 | 2012-05-13 12:10:30 -0400 | [diff] [blame] | 142 | return firstfixed + BUILD_BIOS_ADDR |
| 143 | |
| 144 | # Return the subset of sections with a given category |
| 145 | def getSectionsCategory(sections, category): |
| 146 | return [section for section in sections if section.category == category] |
Kevin O'Connor | 9ba1dea | 2010-05-01 09:50:13 -0400 | [diff] [blame] | 147 | |
Kevin O'Connor | 1a4885e | 2010-09-15 21:28:31 -0400 | [diff] [blame] | 148 | # Return the subset of sections with a given name prefix |
Kevin O'Connor | 46b8262 | 2012-05-13 12:10:30 -0400 | [diff] [blame] | 149 | def getSectionsPrefix(sections, prefix): |
Kevin O'Connor | 1a4885e | 2010-09-15 21:28:31 -0400 | [diff] [blame] | 150 | return [section for section in sections |
Kevin O'Connor | 46b8262 | 2012-05-13 12:10:30 -0400 | [diff] [blame] | 151 | if section.name.startswith(prefix)] |
Kevin O'Connor | 1a4885e | 2010-09-15 21:28:31 -0400 | [diff] [blame] | 152 | |
Kevin O'Connor | 46b8262 | 2012-05-13 12:10:30 -0400 | [diff] [blame] | 153 | # The sections (and associated information) to be placed in output rom |
| 154 | class LayoutInfo: |
| 155 | sections16 = sec16_start = sec16_align = None |
| 156 | sections32seg = sec32seg_start = sec32seg_align = None |
| 157 | sections32flat = sec32flat_start = sec32flat_align = None |
| 158 | sections32init = sec32init_start = sec32init_align = None |
| 159 | sections32low = sec32low_start = sec32low_align = None |
Kevin O'Connor | c924344 | 2013-02-17 13:58:28 -0500 | [diff] [blame^] | 160 | zonelow_base = final_sec32low_start = None |
Kevin O'Connor | 46b8262 | 2012-05-13 12:10:30 -0400 | [diff] [blame] | 161 | |
| 162 | # Determine final memory addresses for sections |
| 163 | def doLayout(sections, genreloc): |
| 164 | li = LayoutInfo() |
Kevin O'Connor | 9ba1dea | 2010-05-01 09:50:13 -0400 | [diff] [blame] | 165 | # Determine 16bit positions |
Kevin O'Connor | 46b8262 | 2012-05-13 12:10:30 -0400 | [diff] [blame] | 166 | li.sections16 = getSectionsCategory(sections, '16') |
| 167 | textsections = getSectionsPrefix(li.sections16, '.text.') |
Kevin O'Connor | 805ede2 | 2012-02-08 20:23:36 -0500 | [diff] [blame] | 168 | rodatasections = ( |
Kevin O'Connor | 46b8262 | 2012-05-13 12:10:30 -0400 | [diff] [blame] | 169 | getSectionsPrefix(li.sections16, '.rodata.str1.1') |
| 170 | + getSectionsPrefix(li.sections16, '.rodata.__func__.') |
| 171 | + getSectionsPrefix(li.sections16, '.rodata.__PRETTY_FUNCTION__.')) |
| 172 | datasections = getSectionsPrefix(li.sections16, '.data16.') |
| 173 | fixedsections = getSectionsPrefix(li.sections16, '.fixedaddr.') |
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 | firstfixed = fitSections(fixedsections, textsections) |
| 176 | remsections = [s for s in textsections+rodatasections+datasections |
| 177 | if s.finalloc is None] |
Kevin O'Connor | 46b8262 | 2012-05-13 12:10:30 -0400 | [diff] [blame] | 178 | li.sec16_start, li.sec16_align = setSectionsStart( |
| 179 | remsections, firstfixed, segoffset=BUILD_BIOS_ADDR) |
Kevin O'Connor | 9ba1dea | 2010-05-01 09:50:13 -0400 | [diff] [blame] | 180 | |
| 181 | # Determine 32seg positions |
Kevin O'Connor | 46b8262 | 2012-05-13 12:10:30 -0400 | [diff] [blame] | 182 | li.sections32seg = getSectionsCategory(sections, '32seg') |
| 183 | textsections = getSectionsPrefix(li.sections32seg, '.text.') |
Kevin O'Connor | 805ede2 | 2012-02-08 20:23:36 -0500 | [diff] [blame] | 184 | rodatasections = ( |
Kevin O'Connor | 46b8262 | 2012-05-13 12:10:30 -0400 | [diff] [blame] | 185 | getSectionsPrefix(li.sections32seg, '.rodata.str1.1') |
| 186 | + getSectionsPrefix(li.sections32seg, '.rodata.__func__.') |
| 187 | + getSectionsPrefix(li.sections32seg, '.rodata.__PRETTY_FUNCTION__.')) |
| 188 | datasections = getSectionsPrefix(li.sections32seg, '.data32seg.') |
Kevin O'Connor | 9ba1dea | 2010-05-01 09:50:13 -0400 | [diff] [blame] | 189 | |
Kevin O'Connor | 46b8262 | 2012-05-13 12:10:30 -0400 | [diff] [blame] | 190 | li.sec32seg_start, li.sec32seg_align = setSectionsStart( |
| 191 | textsections + rodatasections + datasections, li.sec16_start |
| 192 | , segoffset=BUILD_BIOS_ADDR) |
Kevin O'Connor | 9ba1dea | 2010-05-01 09:50:13 -0400 | [diff] [blame] | 193 | |
Kevin O'Connor | 1a4885e | 2010-09-15 21:28:31 -0400 | [diff] [blame] | 194 | # Determine 32flat runtime positions |
Kevin O'Connor | 46b8262 | 2012-05-13 12:10:30 -0400 | [diff] [blame] | 195 | li.sections32flat = getSectionsCategory(sections, '32flat') |
| 196 | textsections = getSectionsPrefix(li.sections32flat, '.text.') |
| 197 | rodatasections = getSectionsPrefix(li.sections32flat, '.rodata') |
| 198 | datasections = getSectionsPrefix(li.sections32flat, '.data.') |
| 199 | bsssections = getSectionsPrefix(li.sections32flat, '.bss.') |
Kevin O'Connor | 9ba1dea | 2010-05-01 09:50:13 -0400 | [diff] [blame] | 200 | |
Kevin O'Connor | 46b8262 | 2012-05-13 12:10:30 -0400 | [diff] [blame] | 201 | li.sec32flat_start, li.sec32flat_align = setSectionsStart( |
Kevin O'Connor | 9ba1dea | 2010-05-01 09:50:13 -0400 | [diff] [blame] | 202 | textsections + rodatasections + datasections + bsssections |
Kevin O'Connor | 46b8262 | 2012-05-13 12:10:30 -0400 | [diff] [blame] | 203 | , li.sec32seg_start, 16) |
Kevin O'Connor | 9ba1dea | 2010-05-01 09:50:13 -0400 | [diff] [blame] | 204 | |
Kevin O'Connor | d1b4f96 | 2010-09-15 21:38:16 -0400 | [diff] [blame] | 205 | # Determine 32flat init positions |
Kevin O'Connor | 46b8262 | 2012-05-13 12:10:30 -0400 | [diff] [blame] | 206 | li.sections32init = getSectionsCategory(sections, '32init') |
| 207 | textsections = getSectionsPrefix(li.sections32init, '.text.') |
| 208 | rodatasections = getSectionsPrefix(li.sections32init, '.rodata') |
| 209 | datasections = getSectionsPrefix(li.sections32init, '.data.') |
| 210 | bsssections = getSectionsPrefix(li.sections32init, '.bss.') |
Kevin O'Connor | d1b4f96 | 2010-09-15 21:38:16 -0400 | [diff] [blame] | 211 | |
Kevin O'Connor | 46b8262 | 2012-05-13 12:10:30 -0400 | [diff] [blame] | 212 | li.sec32init_start, li.sec32init_align = setSectionsStart( |
Kevin O'Connor | d1b4f96 | 2010-09-15 21:38:16 -0400 | [diff] [blame] | 213 | textsections + rodatasections + datasections + bsssections |
Kevin O'Connor | 46b8262 | 2012-05-13 12:10:30 -0400 | [diff] [blame] | 214 | , li.sec32flat_start, 16) |
| 215 | |
| 216 | # Determine "low memory" data positions |
| 217 | li.sections32low = getSectionsCategory(sections, '32low') |
| 218 | if genreloc: |
| 219 | sec32low_top = li.sec32init_start |
Kevin O'Connor | c91da7a | 2012-06-08 21:14:19 -0400 | [diff] [blame] | 220 | final_sec32low_top = min(BUILD_BIOS_ADDR, li.sec32flat_start) |
Kevin O'Connor | 46b8262 | 2012-05-13 12:10:30 -0400 | [diff] [blame] | 221 | else: |
| 222 | sec32low_top = min(BUILD_BIOS_ADDR, li.sec32init_start) |
Kevin O'Connor | c91da7a | 2012-06-08 21:14:19 -0400 | [diff] [blame] | 223 | final_sec32low_top = sec32low_top |
| 224 | relocdelta = final_sec32low_top - sec32low_top |
Kevin O'Connor | c924344 | 2013-02-17 13:58:28 -0500 | [diff] [blame^] | 225 | zonelow_base = final_sec32low_top - 64*1024 |
| 226 | li.zonelow_base = max(BUILD_ROM_START, alignpos(zonelow_base, 2*1024)) |
Kevin O'Connor | 46b8262 | 2012-05-13 12:10:30 -0400 | [diff] [blame] | 227 | li.sec32low_start, li.sec32low_align = setSectionsStart( |
Kevin O'Connor | c91da7a | 2012-06-08 21:14:19 -0400 | [diff] [blame] | 228 | li.sections32low, sec32low_top, 16 |
Kevin O'Connor | c924344 | 2013-02-17 13:58:28 -0500 | [diff] [blame^] | 229 | , segoffset=li.zonelow_base - relocdelta) |
Kevin O'Connor | c91da7a | 2012-06-08 21:14:19 -0400 | [diff] [blame] | 230 | li.final_sec32low_start = li.sec32low_start + relocdelta |
Kevin O'Connor | d1b4f96 | 2010-09-15 21:38:16 -0400 | [diff] [blame] | 231 | |
Kevin O'Connor | 9ba1dea | 2010-05-01 09:50:13 -0400 | [diff] [blame] | 232 | # Print statistics |
Kevin O'Connor | 46b8262 | 2012-05-13 12:10:30 -0400 | [diff] [blame] | 233 | size16 = BUILD_BIOS_ADDR + BUILD_BIOS_SIZE - li.sec16_start |
| 234 | size32seg = li.sec16_start - li.sec32seg_start |
| 235 | size32flat = li.sec32seg_start - li.sec32flat_start |
| 236 | size32init = li.sec32flat_start - li.sec32init_start |
| 237 | sizelow = sec32low_top - li.sec32low_start |
Kevin O'Connor | 9ba1dea | 2010-05-01 09:50:13 -0400 | [diff] [blame] | 238 | print "16bit size: %d" % size16 |
| 239 | print "32bit segmented size: %d" % size32seg |
| 240 | print "32bit flat size: %d" % size32flat |
Kevin O'Connor | d1b4f96 | 2010-09-15 21:38:16 -0400 | [diff] [blame] | 241 | print "32bit flat init size: %d" % size32init |
Kevin O'Connor | 46b8262 | 2012-05-13 12:10:30 -0400 | [diff] [blame] | 242 | print "Lowmem size: %d" % sizelow |
| 243 | return li |
Kevin O'Connor | 9ba1dea | 2010-05-01 09:50:13 -0400 | [diff] [blame] | 244 | |
Kevin O'Connor | 9ba1dea | 2010-05-01 09:50:13 -0400 | [diff] [blame] | 245 | |
| 246 | ###################################################################### |
| 247 | # Linker script output |
| 248 | ###################################################################### |
| 249 | |
| 250 | # Write LD script includes for the given cross references |
Kevin O'Connor | a3c48f5 | 2013-02-05 22:36:13 -0500 | [diff] [blame] | 251 | def outXRefs(sections, useseg=0, exportsyms=[]): |
| 252 | xrefs = dict([(symbol.name, symbol) for symbol in exportsyms]) |
Kevin O'Connor | 9ba1dea | 2010-05-01 09:50:13 -0400 | [diff] [blame] | 253 | out = "" |
Kevin O'Connor | 1a4885e | 2010-09-15 21:28:31 -0400 | [diff] [blame] | 254 | for section in sections: |
| 255 | for reloc in section.relocs: |
| 256 | symbol = reloc.symbol |
Kevin O'Connor | a3c48f5 | 2013-02-05 22:36:13 -0500 | [diff] [blame] | 257 | if (symbol.section is not None |
| 258 | and (symbol.section.fileid != section.fileid |
| 259 | or symbol.name != reloc.symbolname)): |
| 260 | xrefs[reloc.symbolname] = symbol |
| 261 | for symbolname, symbol in xrefs.items(): |
| 262 | loc = symbol.section.finalloc |
| 263 | if useseg: |
| 264 | loc = symbol.section.finalsegloc |
| 265 | out += "%s = 0x%x ;\n" % (symbolname, loc + symbol.offset) |
Kevin O'Connor | 9ba1dea | 2010-05-01 09:50:13 -0400 | [diff] [blame] | 266 | return out |
| 267 | |
| 268 | # Write LD script includes for the given sections using relative offsets |
Kevin O'Connor | 46b8262 | 2012-05-13 12:10:30 -0400 | [diff] [blame] | 269 | def outRelSections(sections, startsym, useseg=0): |
| 270 | sections = [(section.finalloc, section) for section in sections |
| 271 | if section.finalloc is not None] |
| 272 | sections.sort() |
Kevin O'Connor | 9ba1dea | 2010-05-01 09:50:13 -0400 | [diff] [blame] | 273 | out = "" |
Kevin O'Connor | 46b8262 | 2012-05-13 12:10:30 -0400 | [diff] [blame] | 274 | for addr, section in sections: |
| 275 | loc = section.finalloc |
| 276 | if useseg: |
| 277 | loc = section.finalsegloc |
| 278 | out += ". = ( 0x%x - %s ) ;\n" % (loc, startsym) |
Kevin O'Connor | 1a4885e | 2010-09-15 21:28:31 -0400 | [diff] [blame] | 279 | if section.name == '.rodata.str1.1': |
Kevin O'Connor | 9ba1dea | 2010-05-01 09:50:13 -0400 | [diff] [blame] | 280 | out += "_rodata = . ;\n" |
Kevin O'Connor | 1a4885e | 2010-09-15 21:28:31 -0400 | [diff] [blame] | 281 | out += "*(%s)\n" % (section.name,) |
Kevin O'Connor | 9ba1dea | 2010-05-01 09:50:13 -0400 | [diff] [blame] | 282 | return out |
| 283 | |
Kevin O'Connor | 46b8262 | 2012-05-13 12:10:30 -0400 | [diff] [blame] | 284 | # Build linker script output for a list of relocations. |
| 285 | def strRelocs(outname, outrel, relocs): |
| 286 | relocs.sort() |
| 287 | return (" %s_start = ABSOLUTE(.) ;\n" % (outname,) |
| 288 | + "".join(["LONG(0x%x - %s)\n" % (pos, outrel) |
| 289 | for pos in relocs]) |
| 290 | + " %s_end = ABSOLUTE(.) ;\n" % (outname,)) |
Kevin O'Connor | 871e0a0 | 2009-12-30 12:14:53 -0500 | [diff] [blame] | 291 | |
Kevin O'Connor | 46b8262 | 2012-05-13 12:10:30 -0400 | [diff] [blame] | 292 | # Find all relocations in the given sections with the given attributes |
| 293 | def getRelocs(sections, type=None, category=None, notcategory=None): |
| 294 | out = [] |
| 295 | for section in sections: |
| 296 | for reloc in section.relocs: |
| 297 | if reloc.symbol.section is None: |
| 298 | continue |
| 299 | destcategory = reloc.symbol.section.category |
| 300 | if ((type is None or reloc.type == type) |
| 301 | and (category is None or destcategory == category) |
| 302 | and (notcategory is None or destcategory != notcategory)): |
| 303 | out.append(section.finalloc + reloc.offset) |
| 304 | return out |
| 305 | |
| 306 | # Return the start address and minimum alignment for a set of sections |
| 307 | def getSectionsStart(sections, defaddr=0): |
| 308 | return min([section.finalloc for section in sections |
| 309 | if section.finalloc is not None] or [defaddr]) |
| 310 | |
| 311 | # Output the linker scripts for all required sections. |
Kevin O'Connor | a3c48f5 | 2013-02-05 22:36:13 -0500 | [diff] [blame] | 312 | def writeLinkerScripts(li, exportsyms, genreloc, out16, out32seg, out32flat): |
Kevin O'Connor | 9ba1dea | 2010-05-01 09:50:13 -0400 | [diff] [blame] | 313 | # Write 16bit linker script |
Kevin O'Connor | 46b8262 | 2012-05-13 12:10:30 -0400 | [diff] [blame] | 314 | out = outXRefs(li.sections16, useseg=1) + """ |
Kevin O'Connor | c924344 | 2013-02-17 13:58:28 -0500 | [diff] [blame^] | 315 | zonelow_base = 0x%x ; |
| 316 | _zonelow_seg = 0x%x ; |
Kevin O'Connor | 46b8262 | 2012-05-13 12:10:30 -0400 | [diff] [blame] | 317 | |
Kevin O'Connor | 9ba1dea | 2010-05-01 09:50:13 -0400 | [diff] [blame] | 318 | code16_start = 0x%x ; |
| 319 | .text16 code16_start : { |
Kevin O'Connor | 46b8262 | 2012-05-13 12:10:30 -0400 | [diff] [blame] | 320 | %s |
Kevin O'Connor | 9ba1dea | 2010-05-01 09:50:13 -0400 | [diff] [blame] | 321 | } |
Kevin O'Connor | c924344 | 2013-02-17 13:58:28 -0500 | [diff] [blame^] | 322 | """ % (li.zonelow_base, |
| 323 | li.zonelow_base / 16, |
Kevin O'Connor | 46b8262 | 2012-05-13 12:10:30 -0400 | [diff] [blame] | 324 | li.sec16_start - BUILD_BIOS_ADDR, |
| 325 | outRelSections(li.sections16, 'code16_start', useseg=1)) |
| 326 | outfile = open(out16, 'wb') |
| 327 | outfile.write(COMMONHEADER + out + COMMONTRAILER) |
| 328 | outfile.close() |
Kevin O'Connor | 871e0a0 | 2009-12-30 12:14:53 -0500 | [diff] [blame] | 329 | |
Kevin O'Connor | 9ba1dea | 2010-05-01 09:50:13 -0400 | [diff] [blame] | 330 | # Write 32seg linker script |
Kevin O'Connor | 46b8262 | 2012-05-13 12:10:30 -0400 | [diff] [blame] | 331 | out = outXRefs(li.sections32seg, useseg=1) + """ |
Kevin O'Connor | 9ba1dea | 2010-05-01 09:50:13 -0400 | [diff] [blame] | 332 | code32seg_start = 0x%x ; |
| 333 | .text32seg code32seg_start : { |
Kevin O'Connor | 46b8262 | 2012-05-13 12:10:30 -0400 | [diff] [blame] | 334 | %s |
Kevin O'Connor | 9ba1dea | 2010-05-01 09:50:13 -0400 | [diff] [blame] | 335 | } |
Kevin O'Connor | 46b8262 | 2012-05-13 12:10:30 -0400 | [diff] [blame] | 336 | """ % (li.sec32seg_start - BUILD_BIOS_ADDR, |
| 337 | outRelSections(li.sections32seg, 'code32seg_start', useseg=1)) |
| 338 | outfile = open(out32seg, 'wb') |
| 339 | outfile.write(COMMONHEADER + out + COMMONTRAILER) |
| 340 | outfile.close() |
Kevin O'Connor | 871e0a0 | 2009-12-30 12:14:53 -0500 | [diff] [blame] | 341 | |
Kevin O'Connor | 9ba1dea | 2010-05-01 09:50:13 -0400 | [diff] [blame] | 342 | # Write 32flat linker script |
Kevin O'Connor | 46b8262 | 2012-05-13 12:10:30 -0400 | [diff] [blame] | 343 | sections32all = li.sections32flat + li.sections32init + li.sections32low |
| 344 | sec32all_start = li.sec32low_start |
Kevin O'Connor | 402fd9c | 2010-09-15 00:26:19 -0400 | [diff] [blame] | 345 | relocstr = "" |
Kevin O'Connor | 402fd9c | 2010-09-15 00:26:19 -0400 | [diff] [blame] | 346 | if genreloc: |
| 347 | # Generate relocations |
Kevin O'Connor | 46b8262 | 2012-05-13 12:10:30 -0400 | [diff] [blame] | 348 | absrelocs = getRelocs( |
| 349 | li.sections32init, type='R_386_32', category='32init') |
| 350 | relrelocs = getRelocs( |
| 351 | li.sections32init, type='R_386_PC32', notcategory='32init') |
| 352 | initrelocs = getRelocs( |
| 353 | li.sections32flat + li.sections32low + li.sections16 |
| 354 | + li.sections32seg, category='32init') |
Kevin O'Connor | c91da7a | 2012-06-08 21:14:19 -0400 | [diff] [blame] | 355 | lowrelocs = getRelocs(sections32all, category='32low') |
Kevin O'Connor | 46b8262 | 2012-05-13 12:10:30 -0400 | [diff] [blame] | 356 | relocstr = (strRelocs("_reloc_abs", "code32init_start", absrelocs) |
| 357 | + strRelocs("_reloc_rel", "code32init_start", relrelocs) |
| 358 | + strRelocs("_reloc_init", "code32flat_start", initrelocs) |
Kevin O'Connor | c924344 | 2013-02-17 13:58:28 -0500 | [diff] [blame^] | 359 | + strRelocs("_reloc_varlow", "code32flat_start", lowrelocs)) |
Kevin O'Connor | 46b8262 | 2012-05-13 12:10:30 -0400 | [diff] [blame] | 360 | numrelocs = len(absrelocs + relrelocs + initrelocs + lowrelocs) |
| 361 | sec32all_start -= numrelocs * 4 |
Kevin O'Connor | a3c48f5 | 2013-02-05 22:36:13 -0500 | [diff] [blame] | 362 | out = outXRefs(sections32all, exportsyms=exportsyms) + """ |
Kevin O'Connor | 402fd9c | 2010-09-15 00:26:19 -0400 | [diff] [blame] | 363 | _reloc_min_align = 0x%x ; |
Kevin O'Connor | c924344 | 2013-02-17 13:58:28 -0500 | [diff] [blame^] | 364 | zonelow_base = 0x%x ; |
| 365 | final_varlow_start = 0x%x ; |
Kevin O'Connor | 46b8262 | 2012-05-13 12:10:30 -0400 | [diff] [blame] | 366 | |
Kevin O'Connor | 9ba1dea | 2010-05-01 09:50:13 -0400 | [diff] [blame] | 367 | code32flat_start = 0x%x ; |
| 368 | .text code32flat_start : { |
Kevin O'Connor | 46b8262 | 2012-05-13 12:10:30 -0400 | [diff] [blame] | 369 | %s |
Kevin O'Connor | c924344 | 2013-02-17 13:58:28 -0500 | [diff] [blame^] | 370 | varlow_start = ABSOLUTE(.) ; |
Kevin O'Connor | 46b8262 | 2012-05-13 12:10:30 -0400 | [diff] [blame] | 371 | %s |
Kevin O'Connor | c924344 | 2013-02-17 13:58:28 -0500 | [diff] [blame^] | 372 | varlow_end = ABSOLUTE(.) ; |
Kevin O'Connor | 402fd9c | 2010-09-15 00:26:19 -0400 | [diff] [blame] | 373 | code32init_start = ABSOLUTE(.) ; |
Kevin O'Connor | 46b8262 | 2012-05-13 12:10:30 -0400 | [diff] [blame] | 374 | %s |
Kevin O'Connor | d1b4f96 | 2010-09-15 21:38:16 -0400 | [diff] [blame] | 375 | code32init_end = ABSOLUTE(.) ; |
Kevin O'Connor | 46b8262 | 2012-05-13 12:10:30 -0400 | [diff] [blame] | 376 | %s |
Kevin O'Connor | 9ba1dea | 2010-05-01 09:50:13 -0400 | [diff] [blame] | 377 | . = ( 0x%x - code32flat_start ) ; |
| 378 | *(.text32seg) |
| 379 | . = ( 0x%x - code32flat_start ) ; |
| 380 | *(.text16) |
| 381 | code32flat_end = ABSOLUTE(.) ; |
| 382 | } :text |
Kevin O'Connor | a3c48f5 | 2013-02-05 22:36:13 -0500 | [diff] [blame] | 383 | """ % (li.sec32init_align, |
Kevin O'Connor | c924344 | 2013-02-17 13:58:28 -0500 | [diff] [blame^] | 384 | li.zonelow_base, |
Kevin O'Connor | c91da7a | 2012-06-08 21:14:19 -0400 | [diff] [blame] | 385 | li.final_sec32low_start, |
Kevin O'Connor | 46b8262 | 2012-05-13 12:10:30 -0400 | [diff] [blame] | 386 | sec32all_start, |
| 387 | relocstr, |
| 388 | outRelSections(li.sections32low, 'code32flat_start'), |
| 389 | outRelSections(li.sections32init, 'code32flat_start'), |
| 390 | outRelSections(li.sections32flat, 'code32flat_start'), |
| 391 | li.sec32seg_start, |
| 392 | li.sec16_start) |
| 393 | out = COMMONHEADER + out + COMMONTRAILER + """ |
Kevin O'Connor | a3c48f5 | 2013-02-05 22:36:13 -0500 | [diff] [blame] | 394 | ENTRY(entry_elf) |
Kevin O'Connor | 9ba1dea | 2010-05-01 09:50:13 -0400 | [diff] [blame] | 395 | PHDRS |
| 396 | { |
| 397 | text PT_LOAD AT ( code32flat_start ) ; |
| 398 | } |
Kevin O'Connor | a3c48f5 | 2013-02-05 22:36:13 -0500 | [diff] [blame] | 399 | """ |
Kevin O'Connor | 46b8262 | 2012-05-13 12:10:30 -0400 | [diff] [blame] | 400 | outfile = open(out32flat, 'wb') |
| 401 | outfile.write(out) |
| 402 | outfile.close() |
Kevin O'Connor | c069394 | 2009-06-10 21:56:01 -0400 | [diff] [blame] | 403 | |
| 404 | |
| 405 | ###################################################################### |
Kevin O'Connor | d1b4f96 | 2010-09-15 21:38:16 -0400 | [diff] [blame] | 406 | # Detection of init code |
| 407 | ###################################################################### |
| 408 | |
| 409 | def markRuntime(section, sections): |
| 410 | if (section is None or not section.keep or section.category is not None |
| 411 | or '.init.' in section.name or section.fileid != '32flat'): |
| 412 | return |
| 413 | section.category = '32flat' |
| 414 | # Recursively mark all sections this section points to |
| 415 | for reloc in section.relocs: |
| 416 | markRuntime(reloc.symbol.section, sections) |
| 417 | |
| 418 | def findInit(sections): |
| 419 | # Recursively find and mark all "runtime" sections. |
| 420 | for section in sections: |
Kevin O'Connor | c924344 | 2013-02-17 13:58:28 -0500 | [diff] [blame^] | 421 | if ('.data.varlow.' in section.name or '.runtime.' in section.name |
Kevin O'Connor | 46b8262 | 2012-05-13 12:10:30 -0400 | [diff] [blame] | 422 | or '.export.' in section.name): |
Kevin O'Connor | d1b4f96 | 2010-09-15 21:38:16 -0400 | [diff] [blame] | 423 | markRuntime(section, sections) |
| 424 | for section in sections: |
| 425 | if section.category is not None: |
| 426 | continue |
| 427 | if section.fileid == '32flat': |
| 428 | section.category = '32init' |
| 429 | else: |
| 430 | section.category = section.fileid |
| 431 | |
| 432 | |
| 433 | ###################################################################### |
Kevin O'Connor | c069394 | 2009-06-10 21:56:01 -0400 | [diff] [blame] | 434 | # Section garbage collection |
| 435 | ###################################################################### |
| 436 | |
Kevin O'Connor | f3fe3aa | 2010-12-05 12:38:33 -0500 | [diff] [blame] | 437 | CFUNCPREFIX = [('_cfunc16_', 0), ('_cfunc32seg_', 1), ('_cfunc32flat_', 2)] |
| 438 | |
Kevin O'Connor | fdca418 | 2010-01-01 12:46:54 -0500 | [diff] [blame] | 439 | # Find and keep the section associated with a symbol (if available). |
Kevin O'Connor | f3fe3aa | 2010-12-05 12:38:33 -0500 | [diff] [blame] | 440 | def keepsymbol(reloc, infos, pos, isxref): |
| 441 | symbolname = reloc.symbolname |
| 442 | mustbecfunc = 0 |
| 443 | for symprefix, needpos in CFUNCPREFIX: |
| 444 | if symbolname.startswith(symprefix): |
| 445 | if needpos != pos: |
| 446 | return -1 |
| 447 | symbolname = symbolname[len(symprefix):] |
| 448 | mustbecfunc = 1 |
| 449 | break |
Kevin O'Connor | 1a4885e | 2010-09-15 21:28:31 -0400 | [diff] [blame] | 450 | symbol = infos[pos][1].get(symbolname) |
| 451 | if (symbol is None or symbol.section is None |
| 452 | or symbol.section.name.startswith('.discard.')): |
Kevin O'Connor | fdca418 | 2010-01-01 12:46:54 -0500 | [diff] [blame] | 453 | return -1 |
Kevin O'Connor | f3fe3aa | 2010-12-05 12:38:33 -0500 | [diff] [blame] | 454 | isdestcfunc = (symbol.section.name.startswith('.text.') |
| 455 | and not symbol.section.name.startswith('.text.asm.')) |
| 456 | if ((mustbecfunc and not isdestcfunc) |
| 457 | or (not mustbecfunc and isdestcfunc and isxref)): |
| 458 | return -1 |
| 459 | |
Kevin O'Connor | 1a4885e | 2010-09-15 21:28:31 -0400 | [diff] [blame] | 460 | reloc.symbol = symbol |
| 461 | keepsection(symbol.section, infos, pos) |
Kevin O'Connor | fdca418 | 2010-01-01 12:46:54 -0500 | [diff] [blame] | 462 | return 0 |
| 463 | |
Kevin O'Connor | 5b8f809 | 2009-09-20 19:47:45 -0400 | [diff] [blame] | 464 | # Note required section, and recursively set all referenced sections |
| 465 | # as required. |
Kevin O'Connor | 1a4885e | 2010-09-15 21:28:31 -0400 | [diff] [blame] | 466 | def keepsection(section, infos, pos=0): |
| 467 | if section.keep: |
Kevin O'Connor | c069394 | 2009-06-10 21:56:01 -0400 | [diff] [blame] | 468 | # Already kept - nothing to do. |
| 469 | return |
Kevin O'Connor | 1a4885e | 2010-09-15 21:28:31 -0400 | [diff] [blame] | 470 | section.keep = 1 |
Kevin O'Connor | c069394 | 2009-06-10 21:56:01 -0400 | [diff] [blame] | 471 | # Keep all sections that this section points to |
Kevin O'Connor | 1a4885e | 2010-09-15 21:28:31 -0400 | [diff] [blame] | 472 | for reloc in section.relocs: |
Kevin O'Connor | f3fe3aa | 2010-12-05 12:38:33 -0500 | [diff] [blame] | 473 | ret = keepsymbol(reloc, infos, pos, 0) |
Kevin O'Connor | fdca418 | 2010-01-01 12:46:54 -0500 | [diff] [blame] | 474 | if not ret: |
Kevin O'Connor | c069394 | 2009-06-10 21:56:01 -0400 | [diff] [blame] | 475 | continue |
| 476 | # Not in primary sections - it may be a cross 16/32 reference |
Kevin O'Connor | f3fe3aa | 2010-12-05 12:38:33 -0500 | [diff] [blame] | 477 | ret = keepsymbol(reloc, infos, (pos+1)%3, 1) |
Kevin O'Connor | fdca418 | 2010-01-01 12:46:54 -0500 | [diff] [blame] | 478 | if not ret: |
Kevin O'Connor | 871e0a0 | 2009-12-30 12:14:53 -0500 | [diff] [blame] | 479 | continue |
Kevin O'Connor | f3fe3aa | 2010-12-05 12:38:33 -0500 | [diff] [blame] | 480 | ret = keepsymbol(reloc, infos, (pos+2)%3, 1) |
Kevin O'Connor | fdca418 | 2010-01-01 12:46:54 -0500 | [diff] [blame] | 481 | if not ret: |
| 482 | continue |
Kevin O'Connor | c069394 | 2009-06-10 21:56:01 -0400 | [diff] [blame] | 483 | |
Kevin O'Connor | 5b8f809 | 2009-09-20 19:47:45 -0400 | [diff] [blame] | 484 | # Determine which sections are actually referenced and need to be |
| 485 | # placed into the output file. |
Kevin O'Connor | 871e0a0 | 2009-12-30 12:14:53 -0500 | [diff] [blame] | 486 | def gc(info16, info32seg, info32flat): |
Kevin O'Connor | 1a4885e | 2010-09-15 21:28:31 -0400 | [diff] [blame] | 487 | # infos = ((sections16, symbols16), (sect32seg, sym32seg) |
| 488 | # , (sect32flat, sym32flat)) |
| 489 | infos = (info16, info32seg, info32flat) |
Kevin O'Connor | c069394 | 2009-06-10 21:56:01 -0400 | [diff] [blame] | 490 | # Start by keeping sections that are globally visible. |
Kevin O'Connor | 1a4885e | 2010-09-15 21:28:31 -0400 | [diff] [blame] | 491 | for section in info16[0]: |
| 492 | if section.name.startswith('.fixedaddr.') or '.export.' in section.name: |
Kevin O'Connor | 871e0a0 | 2009-12-30 12:14:53 -0500 | [diff] [blame] | 493 | keepsection(section, infos) |
Kevin O'Connor | 1a4885e | 2010-09-15 21:28:31 -0400 | [diff] [blame] | 494 | return [section for section in info16[0]+info32seg[0]+info32flat[0] |
| 495 | if section.keep] |
Kevin O'Connor | c069394 | 2009-06-10 21:56:01 -0400 | [diff] [blame] | 496 | |
| 497 | |
| 498 | ###################################################################### |
| 499 | # Startup and input parsing |
| 500 | ###################################################################### |
| 501 | |
Kevin O'Connor | 1a4885e | 2010-09-15 21:28:31 -0400 | [diff] [blame] | 502 | class Section: |
| 503 | name = size = alignment = fileid = relocs = None |
Kevin O'Connor | 46b8262 | 2012-05-13 12:10:30 -0400 | [diff] [blame] | 504 | finalloc = finalsegloc = category = keep = None |
Kevin O'Connor | 1a4885e | 2010-09-15 21:28:31 -0400 | [diff] [blame] | 505 | class Reloc: |
Kevin O'Connor | f3fe3aa | 2010-12-05 12:38:33 -0500 | [diff] [blame] | 506 | offset = type = symbolname = symbol = None |
Kevin O'Connor | 1a4885e | 2010-09-15 21:28:31 -0400 | [diff] [blame] | 507 | class Symbol: |
| 508 | name = offset = section = None |
| 509 | |
Kevin O'Connor | c069394 | 2009-06-10 21:56:01 -0400 | [diff] [blame] | 510 | # Read in output from objdump |
Kevin O'Connor | 1a4885e | 2010-09-15 21:28:31 -0400 | [diff] [blame] | 511 | def parseObjDump(file, fileid): |
| 512 | # sections = [section, ...] |
Kevin O'Connor | c069394 | 2009-06-10 21:56:01 -0400 | [diff] [blame] | 513 | sections = [] |
Kevin O'Connor | 1a4885e | 2010-09-15 21:28:31 -0400 | [diff] [blame] | 514 | sectionmap = {} |
| 515 | # symbols[symbolname] = symbol |
Kevin O'Connor | c069394 | 2009-06-10 21:56:01 -0400 | [diff] [blame] | 516 | symbols = {} |
Kevin O'Connor | c069394 | 2009-06-10 21:56:01 -0400 | [diff] [blame] | 517 | |
| 518 | state = None |
| 519 | for line in file.readlines(): |
| 520 | line = line.rstrip() |
| 521 | if line == 'Sections:': |
| 522 | state = 'section' |
| 523 | continue |
| 524 | if line == 'SYMBOL TABLE:': |
| 525 | state = 'symbol' |
| 526 | continue |
Kevin O'Connor | 6c2e781 | 2010-09-13 18:04:02 -0400 | [diff] [blame] | 527 | if line.startswith('RELOCATION RECORDS FOR ['): |
Kevin O'Connor | 1a4885e | 2010-09-15 21:28:31 -0400 | [diff] [blame] | 528 | sectionname = line[24:-2] |
| 529 | if sectionname.startswith('.debug_'): |
| 530 | # Skip debugging sections (to reduce parsing time) |
| 531 | state = None |
| 532 | continue |
Kevin O'Connor | c069394 | 2009-06-10 21:56:01 -0400 | [diff] [blame] | 533 | state = 'reloc' |
Kevin O'Connor | 1a4885e | 2010-09-15 21:28:31 -0400 | [diff] [blame] | 534 | relocsection = sectionmap[sectionname] |
Kevin O'Connor | c069394 | 2009-06-10 21:56:01 -0400 | [diff] [blame] | 535 | continue |
| 536 | |
| 537 | if state == 'section': |
| 538 | try: |
| 539 | idx, name, size, vma, lma, fileoff, align = line.split() |
| 540 | if align[:3] != '2**': |
| 541 | continue |
Kevin O'Connor | 1a4885e | 2010-09-15 21:28:31 -0400 | [diff] [blame] | 542 | section = Section() |
| 543 | section.name = name |
| 544 | section.size = int(size, 16) |
| 545 | section.align = 2**int(align[3:]) |
| 546 | section.fileid = fileid |
| 547 | section.relocs = [] |
| 548 | sections.append(section) |
| 549 | sectionmap[name] = section |
| 550 | except ValueError: |
Kevin O'Connor | c069394 | 2009-06-10 21:56:01 -0400 | [diff] [blame] | 551 | pass |
| 552 | continue |
| 553 | if state == 'symbol': |
| 554 | try: |
Kevin O'Connor | 90ebed4 | 2012-06-21 20:54:53 -0400 | [diff] [blame] | 555 | parts = line[17:].split() |
| 556 | if len(parts) == 3: |
| 557 | sectionname, size, name = parts |
| 558 | elif len(parts) == 4 and parts[2] == '.hidden': |
| 559 | sectionname, size, hidden, name = parts |
| 560 | else: |
| 561 | continue |
Kevin O'Connor | 1a4885e | 2010-09-15 21:28:31 -0400 | [diff] [blame] | 562 | symbol = Symbol() |
| 563 | symbol.size = int(size, 16) |
| 564 | symbol.offset = int(line[:8], 16) |
| 565 | symbol.name = name |
| 566 | symbol.section = sectionmap.get(sectionname) |
| 567 | symbols[name] = symbol |
| 568 | except ValueError: |
Kevin O'Connor | c069394 | 2009-06-10 21:56:01 -0400 | [diff] [blame] | 569 | pass |
| 570 | continue |
| 571 | if state == 'reloc': |
| 572 | try: |
Kevin O'Connor | 1a4885e | 2010-09-15 21:28:31 -0400 | [diff] [blame] | 573 | off, type, symbolname = line.split() |
| 574 | reloc = Reloc() |
| 575 | reloc.offset = int(off, 16) |
| 576 | reloc.type = type |
Kevin O'Connor | f3fe3aa | 2010-12-05 12:38:33 -0500 | [diff] [blame] | 577 | reloc.symbolname = symbolname |
Kevin O'Connor | 67863be | 2010-12-24 10:23:10 -0500 | [diff] [blame] | 578 | reloc.symbol = symbols.get(symbolname) |
| 579 | if reloc.symbol is None: |
| 580 | # Some binutils (2.20.1) give section name instead |
| 581 | # of a symbol - create a dummy symbol. |
| 582 | reloc.symbol = symbol = Symbol() |
| 583 | symbol.size = 0 |
| 584 | symbol.offset = 0 |
| 585 | symbol.name = symbolname |
| 586 | symbol.section = sectionmap.get(symbolname) |
| 587 | symbols[symbolname] = symbol |
Kevin O'Connor | 1a4885e | 2010-09-15 21:28:31 -0400 | [diff] [blame] | 588 | relocsection.relocs.append(reloc) |
| 589 | except ValueError: |
Kevin O'Connor | c069394 | 2009-06-10 21:56:01 -0400 | [diff] [blame] | 590 | pass |
Kevin O'Connor | 1a4885e | 2010-09-15 21:28:31 -0400 | [diff] [blame] | 591 | return sections, symbols |
Kevin O'Connor | c069394 | 2009-06-10 21:56:01 -0400 | [diff] [blame] | 592 | |
| 593 | def main(): |
| 594 | # Get output name |
Kevin O'Connor | 871e0a0 | 2009-12-30 12:14:53 -0500 | [diff] [blame] | 595 | in16, in32seg, in32flat, out16, out32seg, out32flat = sys.argv[1:] |
Kevin O'Connor | c069394 | 2009-06-10 21:56:01 -0400 | [diff] [blame] | 596 | |
Kevin O'Connor | 9ba1dea | 2010-05-01 09:50:13 -0400 | [diff] [blame] | 597 | # Read in the objdump information |
Kevin O'Connor | c069394 | 2009-06-10 21:56:01 -0400 | [diff] [blame] | 598 | infile16 = open(in16, 'rb') |
Kevin O'Connor | 871e0a0 | 2009-12-30 12:14:53 -0500 | [diff] [blame] | 599 | infile32seg = open(in32seg, 'rb') |
| 600 | infile32flat = open(in32flat, 'rb') |
Kevin O'Connor | c069394 | 2009-06-10 21:56:01 -0400 | [diff] [blame] | 601 | |
Kevin O'Connor | 1a4885e | 2010-09-15 21:28:31 -0400 | [diff] [blame] | 602 | # infoX = (sections, symbols) |
| 603 | info16 = parseObjDump(infile16, '16') |
| 604 | info32seg = parseObjDump(infile32seg, '32seg') |
| 605 | info32flat = parseObjDump(infile32flat, '32flat') |
Kevin O'Connor | c069394 | 2009-06-10 21:56:01 -0400 | [diff] [blame] | 606 | |
Kevin O'Connor | 9ba1dea | 2010-05-01 09:50:13 -0400 | [diff] [blame] | 607 | # Figure out which sections to keep. |
Kevin O'Connor | 1a4885e | 2010-09-15 21:28:31 -0400 | [diff] [blame] | 608 | sections = gc(info16, info32seg, info32flat) |
Kevin O'Connor | c069394 | 2009-06-10 21:56:01 -0400 | [diff] [blame] | 609 | |
Kevin O'Connor | d1b4f96 | 2010-09-15 21:38:16 -0400 | [diff] [blame] | 610 | # Separate 32bit flat into runtime and init parts |
| 611 | findInit(sections) |
| 612 | |
Kevin O'Connor | 46b8262 | 2012-05-13 12:10:30 -0400 | [diff] [blame] | 613 | # Note "low memory" parts |
Kevin O'Connor | c924344 | 2013-02-17 13:58:28 -0500 | [diff] [blame^] | 614 | for section in getSectionsPrefix(sections, '.data.varlow.'): |
Kevin O'Connor | 46b8262 | 2012-05-13 12:10:30 -0400 | [diff] [blame] | 615 | section.category = '32low' |
| 616 | |
Kevin O'Connor | 9ba1dea | 2010-05-01 09:50:13 -0400 | [diff] [blame] | 617 | # Determine the final memory locations of each kept section. |
Kevin O'Connor | 46b8262 | 2012-05-13 12:10:30 -0400 | [diff] [blame] | 618 | genreloc = '_reloc_abs_start' in info32flat[1] |
| 619 | li = doLayout(sections, genreloc) |
Kevin O'Connor | 9ba1dea | 2010-05-01 09:50:13 -0400 | [diff] [blame] | 620 | |
Kevin O'Connor | a3c48f5 | 2013-02-05 22:36:13 -0500 | [diff] [blame] | 621 | # Exported symbols |
| 622 | exportsyms = [symbol for symbol in info16[1].values() |
| 623 | if (symbol.section is not None |
| 624 | and '.export.' in symbol.section.name |
| 625 | and symbol.name != symbol.section.name)] |
| 626 | |
Kevin O'Connor | 9ba1dea | 2010-05-01 09:50:13 -0400 | [diff] [blame] | 627 | # Write out linker script files. |
Kevin O'Connor | a3c48f5 | 2013-02-05 22:36:13 -0500 | [diff] [blame] | 628 | writeLinkerScripts(li, exportsyms, genreloc, out16, out32seg, out32flat) |
Kevin O'Connor | c069394 | 2009-06-10 21:56:01 -0400 | [diff] [blame] | 629 | |
Kevin O'Connor | 202024a | 2009-01-17 10:41:28 -0500 | [diff] [blame] | 630 | if __name__ == '__main__': |
| 631 | main() |