+# Return the subset of sections with a given name prefix
+def getSectionsPrefix(sections, category, prefix):
+ return [section for section in sections
+ if section.category == category and section.name.startswith(prefix)]
+
+def doLayout(sections):
+ # Determine 16bit positions
+ textsections = getSectionsPrefix(sections, '16', '.text.')
+ rodatasections = (getSectionsPrefix(sections, '16', '.rodata.str1.1')
+ + getSectionsPrefix(sections, '16', '.rodata.__func__.'))
+ datasections = getSectionsPrefix(sections, '16', '.data16.')
+ fixedsections = getSectionsPrefix(sections, '16', '.fixedaddr.')
+
+ firstfixed = fitSections(fixedsections, textsections)
+ remsections = [s for s in textsections+rodatasections+datasections
+ if s.finalloc is None]
+ code16_start = setSectionsStart(remsections, firstfixed)
+
+ # Determine 32seg positions
+ textsections = getSectionsPrefix(sections, '32seg', '.text.')
+ rodatasections = (getSectionsPrefix(sections, '32seg', '.rodata.str1.1')
+ +getSectionsPrefix(sections, '32seg', '.rodata.__func__.'))
+ datasections = getSectionsPrefix(sections, '32seg', '.data32seg.')
+
+ code32seg_start = setSectionsStart(
+ textsections + rodatasections + datasections, code16_start)
+
+ # Determine 32flat runtime positions
+ textsections = getSectionsPrefix(sections, '32flat', '.text.')
+ rodatasections = getSectionsPrefix(sections, '32flat', '.rodata')
+ datasections = getSectionsPrefix(sections, '32flat', '.data.')
+ bsssections = getSectionsPrefix(sections, '32flat', '.bss.')
+
+ code32flat_start = setSectionsStart(
+ textsections + rodatasections + datasections + bsssections
+ , code32seg_start + BUILD_BIOS_ADDR, 16)
+
+ # Determine 32flat init positions
+ textsections = getSectionsPrefix(sections, '32init', '.text.')
+ rodatasections = getSectionsPrefix(sections, '32init', '.rodata')
+ datasections = getSectionsPrefix(sections, '32init', '.data.')
+ bsssections = getSectionsPrefix(sections, '32init', '.bss.')
+
+ code32init_start = setSectionsStart(
+ textsections + rodatasections + datasections + bsssections
+ , code32flat_start, 16)
+
+ # Print statistics
+ size16 = BUILD_BIOS_SIZE - code16_start
+ size32seg = code16_start - code32seg_start
+ size32flat = code32seg_start + BUILD_BIOS_ADDR - code32flat_start
+ size32init = code32flat_start - code32init_start
+ print "16bit size: %d" % size16
+ print "32bit segmented size: %d" % size32seg
+ print "32bit flat size: %d" % size32flat
+ print "32bit flat init size: %d" % size32init