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