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