rgbds icon indicating copy to clipboard operation
rgbds copied to clipboard

[Feature proposal] Weak sections

Open aaaaaa123456789 opened this issue 3 years ago • 0 comments

[Note: proposal taken from the debate in gbdev's #asm on 2022-03-02.]

Fixing some data across many banks is easy. For instance, consider placing the current bank number at the end of each ROMX bank:

for ___bank, 1, $80
SECTION "Bank ID {d:___bank}", ROMX[$7fff], BANK[___bank]
  db ___bank
endr

However, this has the side effect of extending the ROM to add banks that contain nothing but this fixed data, which will most likely be irrelevant for those otherwise unused banks. Therefore, a new section type could be created (like SECTION UNION and SECTION FRAGMENT), which for this proposal I'll call SECTION WEAK:

for ___bank, 1, $100
SECTION WEAK "Bank ID {d:___bank}", ROMX[$7fff], BANK[___bank]
  db ___bank
endr

Semantics:

  • After linking and placing all sections, if the last bank of any section type contains only weak sections, discard them all and repeat this check until it fails.
  • Sections discarded this way don't emit any data or symbols, nor they should show up in map files.
  • If a symbol defined by a section discarded this way is referenced from a non-discarded section, a link error occurs. (Symbols from a weak section referenced in another section shouldn't be resolved at assembly time, if possible.)
  • A weak section whose placement isn't fully defined (both bank and address) at link time, either through attributes or through a linker script, should raise a link warning, enabled by default. A weak section whose section type is unbanked should also raise this warning.
  • If a weak section's bank number isn't fully defined at link time, and its section type is banked, it is always discarded. This is equivalent to placing it in a new bank and discarding that bank. (Note that this will raise the warning described in the previous bullet point.)
  • As WEAK is a kind of section, it doesn't combine with UNION and FRAGMENT. This is intentional. (If this combination would be desirable, WEAK could be made an attribute, like BANK or ALIGN.)
  • It is possible to define weak sections for section types that don't belong in ROM ("no output section types" borrowing the terminology from #524). This is simply to make the semantics simpler (no special cases) and to avoid blocking #524. Of course, a no output weak section can only output symbols, but it can also reserve space in each bank, which may be useful for some strange use cases.

aaaaaa123456789 avatar Mar 02 '22 23:03 aaaaaa123456789