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