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