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