gbdev / gbdev/rgbds

Feature request: anonymous sections

Open
#1,991 6 comments 0 reactions 0 assignees View on GitHub
breaking enhancement rgbasm rgblink
Dominant language
C++
Stars
1.6k
Forks
193
Avg merge
20h 32m
Merged PRs (30d)
29

Description

As discussed in #1120, and more recently in Discord `#asm-rgbds`, anonymous sections are a desirable feature that currently gets handled with macros. However, such macros are hard to write reliably, since `\@` is not unique across translation units, and we ended up rejecting #1460 as an alternative to `__FILE__`.

The best we can currently do is to manually define a unique symbol per translation unit, which can be done automatically with build system cooperation, e.g. @ISSOtm's Makefile example:

```make
obj/%.o: src/%.c
rgbasm ${ASFLAGS} $< -o $@ -DUNIT_NAME='$*' # or ${*F} if you want just the file name.
```

```asm
MACRO ANON_SECTION
SECTION "ANON {UNIT_NAME} \@", \#
ENDM
```

This issue proposes to allow just omitting the section name. The resulting anonymous section would have an internal unique identifier instead. And the linker knows which section comes from which translation unit, so it could avoid conflicts.

```asm
SECTION ROM0[$100]
jp EntryPoint

SECTION ROM0[$38]
CrashRst::
jp _CrashHandler

SECTION WRAM0, ALIGN[256]
wBuffer:: ds 512
```

Of course, anonymous sections can't be `UNION` or `FRAGMENT`, and can't be referred to from a linker script, but if you need any of those, you just give it a name.

This would require an update to the [object file format](https://rgbds.gbdev.io/docs/rgbds.5#Sections), so it would have to wait for v2.0. (We could, for example, leave the `STRING Name` as an empty null-terminated string, and set one of the unused `BYTE Type` bits to indicate it's an anonymous section, which RGBLINK would then be responsible for tracking.)

Contributor guide

Open the contributing guide

Research direction

Start with the Sections section of the rgbds.5 object-file documentation and the assembler and RGBLINK handling of SECTION records. Determine how anonymous sections should be represented for v2.0, including the stated UNION, FRAGMENT, and linker-script limits. Done means the design is agreed and the assembler, object format, and linker behavior are specified consistently.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.