Since the final location of the "varlow" variables are known at build time, link the final locations into the binary during the build. The 16bit code was already done at link time - update the build so the 32bit code is also done at link time.
Signed-off-by: Kevin O'Connor kevin@koconnor.net --- src/pmm.c | 34 +++++++++++++++-------------- src/post.c | 8 +------ src/types.h | 2 +- src/util.h | 2 +- tools/layoutrom.py | 63 +++++++++++++++++++++++++++++++----------------------- 5 files changed, 57 insertions(+), 52 deletions(-)
diff --git a/src/pmm.c b/src/pmm.c index 8944bae..0bd30b2 100644 --- a/src/pmm.c +++ b/src/pmm.c @@ -223,7 +223,7 @@ void malloc_preinit(void) { ASSERT32FLAT(); - dprintf(3, "malloc setup\n"); + dprintf(3, "malloc preinit\n");
dprintf(1, "Ram Size=0x%08x (0x%016llx high)\n", RamSize, RamSizeOver4G);
@@ -288,29 +288,31 @@ csm_malloc_preinit(u32 low_pmm, u32 low_pmm_size, u32 hi_pmm, u32 hi_pmm_size)
// Update pointers after code relocation. void -malloc_fixupreloc_init(void) +malloc_init(void) { ASSERT32FLAT(); - if (!CONFIG_RELOCATE_INIT) - return; - dprintf(3, "malloc fixup reloc\n"); - - int i; - for (i=0; i<ARRAY_SIZE(Zones); i++) { - struct zone_s *zone = Zones[i]; - if (zone->info) - zone->info->pprev = &zone->info; + dprintf(3, "malloc init\n"); + + if (CONFIG_RELOCATE_INIT) { + // Fixup malloc pointers after relocation + int i; + for (i=0; i<ARRAY_SIZE(Zones); i++) { + struct zone_s *zone = Zones[i]; + if (zone->info) + zone->info->pprev = &zone->info; + } }
// Move low-memory initial variable content to new location. extern u8 varlow_start[], varlow_end[], final_varlow_start[]; memmove(final_varlow_start, varlow_start, varlow_end - varlow_start);
- // Add space free'd during relocation in f-segment to ZoneFSeg - extern u8 code32init_end[]; - if ((u32)code32init_end > BUILD_BIOS_ADDR) { - memset((void*)BUILD_BIOS_ADDR, 0, (u32)code32init_end - BUILD_BIOS_ADDR); - addSpace(&ZoneFSeg, (void*)BUILD_BIOS_ADDR, code32init_end); + // Add space available in f-segment to ZoneFSeg + extern u8 final_code32flat_start[]; + if ((u32)final_code32flat_start > BUILD_BIOS_ADDR) { + memset((void*)BUILD_BIOS_ADDR, 0 + , (u32)final_code32flat_start - BUILD_BIOS_ADDR); + addSpace(&ZoneFSeg, (void*)BUILD_BIOS_ADDR, final_code32flat_start); } }
diff --git a/src/post.c b/src/post.c index 7f096de..44bb0b0 100644 --- a/src/post.c +++ b/src/post.c @@ -105,7 +105,7 @@ void interface_init(void) { // Running at new code address - do code relocation fixups - malloc_fixupreloc_init(); + malloc_init();
// Setup romfile items. qemu_cfg_init(); @@ -264,8 +264,6 @@ reloc_preinit(void *f, void *arg) extern u32 _reloc_rel_start[], _reloc_rel_end[]; extern u32 _reloc_init_start[], _reloc_init_end[]; extern u8 code32init_start[], code32init_end[]; - extern u32 _reloc_varlow_start[], _reloc_varlow_end[]; - extern u8 varlow_start[], varlow_end[], final_varlow_start[];
// Allocate space for init code. u32 initsize = code32init_end - code32init_start; @@ -275,10 +273,6 @@ reloc_preinit(void *f, void *arg) panic("No space for init relocation.\n");
// Copy code and update relocs (init absolute, init relative, and runtime) - dprintf(1, "Relocating low data from %p to %p (size %d)\n" - , varlow_start, final_varlow_start, varlow_end - varlow_start); - updateRelocs(code32flat_start, _reloc_varlow_start, _reloc_varlow_end - , final_varlow_start - varlow_start); dprintf(1, "Relocating init from %p to %p (size %d)\n" , code32init_start, codedest, initsize); s32 delta = codedest - (void*)code32init_start; diff --git a/src/types.h b/src/types.h index 4df1476..03104a2 100644 --- a/src/types.h +++ b/src/types.h @@ -90,7 +90,7 @@ extern void __force_link_error__only_in_16bit(void) __noreturn; # define VAR16 __section(".discard.var16." UNIQSEC) # define VAR16FIXED(addr) VAR16 __VISIBLE __weak # define VAR32SEG __section(".discard.var32seg." UNIQSEC) -# define VARLOW __section(".data.varlow." UNIQSEC) __VISIBLE +# define VARLOW __section(".data.varlow." UNIQSEC) __VISIBLE __weak # define VARFSEG __section(".data.varfseg." UNIQSEC) __VISIBLE # define ASM16(code) # define ASM32FLAT(code) __ASM(code) diff --git a/src/util.h b/src/util.h index 5729a2f..9303b5a 100644 --- a/src/util.h +++ b/src/util.h @@ -371,7 +371,7 @@ int rom_confirm(u32 size); void csm_malloc_preinit(u32 low_pmm, u32 low_pmm_size, u32 hi_pmm, u32 hi_pmm_size); void malloc_preinit(void); -void malloc_fixupreloc_init(void); +void malloc_init(void); void malloc_prepboot(void); void *pmm_malloc(struct zone_s *zone, u32 handle, u32 size, u32 align); int pmm_free(void *data); diff --git a/tools/layoutrom.py b/tools/layoutrom.py index 2543c58..284ce99 100755 --- a/tools/layoutrom.py +++ b/tools/layoutrom.py @@ -152,17 +152,20 @@ def getSectionsPrefix(sections, prefix):
# The sections (and associated information) to be placed in output rom class LayoutInfo: + genreloc = None sections16 = sec16_start = sec16_align = None sections32seg = sec32seg_start = sec32seg_align = None sections32flat = sec32flat_start = sec32flat_align = None sections32init = sec32init_start = sec32init_align = None sections32low = sec32low_start = sec32low_align = None sections32fseg = sec32fseg_start = sec32fseg_align = None - zonelow_base = final_sec32low_start = None + final_code32flat_start = final_sec32low_start = zonelow_base = None + exportsyms = varlowsyms = None
# Determine final memory addresses for sections def doLayout(sections, genreloc): li = LayoutInfo() + li.genreloc = genreloc # Determine 16bit positions li.sections16 = getSectionsCategory(sections, '16') textsections = getSectionsPrefix(li.sections16, '.text.') @@ -221,19 +224,20 @@ def doLayout(sections, genreloc): textsections + rodatasections + datasections + bsssections , li.sec32flat_start, 16)
- # Determine "low memory" data positions - li.sections32low = getSectionsCategory(sections, '32low') if genreloc: - sec32low_top = li.sec32init_start - final_sec32low_top = min(BUILD_BIOS_ADDR, li.sec32flat_start) + li.final_code32flat_start = li.sec32flat_start else: - sec32low_top = min(BUILD_BIOS_ADDR, li.sec32init_start) - final_sec32low_top = sec32low_top - relocdelta = final_sec32low_top - sec32low_top - zonelow_base = final_sec32low_top - 64*1024 + li.final_code32flat_start = li.sec32init_start + + # Determine "low memory" data positions + li.sections32low = getSectionsCategory(sections, '32low') + sec32low_end = li.sec32init_start + final_sec32low_start = min(BUILD_BIOS_ADDR, li.final_code32flat_start) + relocdelta = final_sec32low_start - sec32low_end + zonelow_base = final_sec32low_start - 64*1024 li.zonelow_base = max(BUILD_ROM_START, alignpos(zonelow_base, 2*1024)) li.sec32low_start, li.sec32low_align = setSectionsStart( - li.sections32low, sec32low_top, 16 + li.sections32low, sec32low_end, 16 , segoffset=li.zonelow_base - relocdelta) li.final_sec32low_start = li.sec32low_start + relocdelta
@@ -243,7 +247,7 @@ def doLayout(sections, genreloc): size32fseg = li.sec32seg_start - li.sec32fseg_start size32flat = li.sec32fseg_start - li.sec32flat_start size32init = li.sec32flat_start - li.sec32init_start - sizelow = sec32low_top - li.sec32low_start + sizelow = sec32low_end - li.sec32low_start print "16bit size: %d" % size16 print "32bit segmented size: %d" % size32seg print "32bit flat size: %d" % size32flat @@ -258,7 +262,7 @@ def doLayout(sections, genreloc): ######################################################################
# Write LD script includes for the given cross references -def outXRefs(sections, useseg=0, exportsyms=[]): +def outXRefs(sections, useseg=0, exportsyms=[], forcedelta=0): xrefs = dict([(symbol.name, symbol) for symbol in exportsyms]) out = "" for section in sections: @@ -272,7 +276,7 @@ def outXRefs(sections, useseg=0, exportsyms=[]): loc = symbol.section.finalloc if useseg: loc = symbol.section.finalsegloc - out += "%s = 0x%x ;\n" % (symbolname, loc + symbol.offset) + out += "%s = 0x%x ;\n" % (symbolname, loc + forcedelta + symbol.offset) return out
# Write LD script includes for the given sections using relative offsets @@ -319,7 +323,7 @@ def getSectionsStart(sections, defaddr=0): if section.finalloc is not None] or [defaddr])
# Output the linker scripts for all required sections. -def writeLinkerScripts(li, exportsyms, genreloc, out16, out32seg, out32flat): +def writeLinkerScripts(li, out16, out32seg, out32flat): # Write 16bit linker script out = outXRefs(li.sections16, useseg=1) + """ zonelow_base = 0x%x ; @@ -350,11 +354,10 @@ def writeLinkerScripts(li, exportsyms, genreloc, out16, out32seg, out32flat): outfile.close()
# Write 32flat linker script - sections32all = (li.sections32flat + li.sections32init + li.sections32low - + li.sections32fseg) + sections32all = (li.sections32flat + li.sections32init + li.sections32fseg) sec32all_start = li.sec32low_start relocstr = "" - if genreloc: + if li.genreloc: # Generate relocations absrelocs = getRelocs( li.sections32init, type='R_386_32', category='32init') @@ -363,16 +366,17 @@ def writeLinkerScripts(li, exportsyms, genreloc, out16, out32seg, out32flat): initrelocs = getRelocs( li.sections32flat + li.sections32low + li.sections16 + li.sections32seg + li.sections32fseg, category='32init') - lowrelocs = getRelocs(sections32all, category='32low') relocstr = (strRelocs("_reloc_abs", "code32init_start", absrelocs) + strRelocs("_reloc_rel", "code32init_start", relrelocs) - + strRelocs("_reloc_init", "code32flat_start", initrelocs) - + strRelocs("_reloc_varlow", "code32flat_start", lowrelocs)) - numrelocs = len(absrelocs + relrelocs + initrelocs + lowrelocs) + + strRelocs("_reloc_init", "code32flat_start", initrelocs)) + numrelocs = len(absrelocs + relrelocs + initrelocs) sec32all_start -= numrelocs * 4 - out = outXRefs(sections32all, exportsyms=exportsyms) + """ + out = outXRefs(li.sections32low, exportsyms=li.varlowsyms + , forcedelta=li.final_sec32low_start-li.sec32low_start) + out += outXRefs(sections32all, exportsyms=li.exportsyms) + """ _reloc_min_align = 0x%x ; zonelow_base = 0x%x ; + final_code32flat_start = 0x%x ; final_varlow_start = 0x%x ;
code32flat_start = 0x%x ; @@ -394,6 +398,7 @@ def writeLinkerScripts(li, exportsyms, genreloc, out16, out32seg, out32flat): } :text """ % (li.sec32init_align, li.zonelow_base, + li.final_code32flat_start, li.final_sec32low_start, sec32all_start, relocstr, @@ -634,13 +639,17 @@ def main(): li = doLayout(sections, genreloc)
# Exported symbols - exportsyms = [symbol for symbol in info16[1].values() - if (symbol.section is not None - and '.export.' in symbol.section.name - and symbol.name != symbol.section.name)] + li.exportsyms = [symbol for symbol in info16[1].values() + if (symbol.section is not None + and '.export.' in symbol.section.name + and symbol.name != symbol.section.name)] + li.varlowsyms = [symbol for symbol in info32flat[1].values() + if (symbol.section is not None + and '.data.varlow.' in symbol.section.name + and symbol.name != symbol.section.name)]
# Write out linker script files. - writeLinkerScripts(li, exportsyms, genreloc, out16, out32seg, out32flat) + writeLinkerScripts(li, out16, out32seg, out32flat)
if __name__ == '__main__': main()