splat icon indicating copy to clipboard operation
splat copied to clipboard

Symbols for section end do not include subalign padding

Open PartyPlanner64 opened this issue 4 years ago • 1 comments

I am finding that linker symbols like _TEXT_END and _DATA_END do not include any subalign padding. If a game needs the aligned/padded values, these can't be obtained with the current linker script format.

Consider the following example:

.overlays_overlay71 0x801059A0 : AT(overlays_overlay71_ROM_START) SUBALIGN(16)
    {
        overlays_overlay71_TEXT_START = .;
        build/src/overlays/overlay71/overlay71_main.c.o(.text);
        build/src/overlays/overlay71/overlay71.c.o(.text);
        overlays_overlay71_TEXT_END = .;
        overlays_overlay71_DATA_START = .;
        overlays_overlay71_overlay71_main_c = .;
        build/src/overlays/overlay71/overlay71_main.c.o(.data);
        overlays_overlay71_DATA_END = .;
        overlays_overlay71_BSS_START = .;
        overlays_overlay71_BSS_END = .;
    }

Corresponding .map file:

.overlays_overlay71
                0x00000000801059a0      0x160 load address 0x000000000031b3b0
                0x00000000801059a0                overlays_overlay71_TEXT_START = .
 build/src/overlays/overlay71/overlay71_main.c.o(.text)
 .text          0x00000000801059a0       0x28 build/src/overlays/overlay71/overlay71_main.c.o
                0x00000000801059a0                func_801059A0_31B3B0
 build/src/overlays/overlay71/overlay71.c.o(.text)
 *fill*         0x00000000801059c8        0x8 
 .text          0x00000000801059d0      0x108 build/src/overlays/overlay71/overlay71.c.o
                0x00000000801059d0                func_801059D0_31B3E0
                0x0000000080105a44                func_80105A44_31B454
                0x0000000080105a6c                func_80105A6C_31B47C
                0x0000000080105a98                func_80105A98_31B4A8
                0x0000000080105ad8                overlays_overlay71_TEXT_END = .
                0x0000000080105ad8                overlays_overlay71_DATA_START = .
                0x0000000080105ad8                overlays_overlay71_overlay71_main_c = .
 build/src/overlays/overlay71/overlay71_main.c.o(.data)
 *fill*         0x0000000080105ad8        0x8 
 .data          0x0000000080105ae0       0x20 build/src/overlays/overlay71/overlay71_main.c.o
                0x0000000080105ae0                overlay71_entrypoints

What appears to happen is that the SUBALIGN takes effect after overlays_overlay71_TEXT_END and others are assigned. They get the non-aligned values as a result.

For my purposes, I would get the values I need if we add . = ALIGN(16) before we assign the _END variables (substitute 16 for whatever is configured). This causes the alignment to take effect before the variables are assigned.

.overlays_overlay71 0x801059A0 : AT(overlays_overlay71_ROM_START) SUBALIGN(16)
    {
        overlays_overlay71_TEXT_START = .;
        build/src/overlays/overlay71/overlay71_main.c.o(.text);
        build/src/overlays/overlay71/overlay71.c.o(.text);
        . = ALIGN(16);
        overlays_overlay71_TEXT_END = .;
        overlays_overlay71_DATA_START = .;
        overlays_overlay71_overlay71_main_c = .;
        build/src/overlays/overlay71/overlay71_main.c.o(.data);
        overlays_overlay71_DATA_END = .;
        overlays_overlay71_BSS_START = .;
        overlays_overlay71_BSS_END = .;
    }

Another solution could be to assign all the _ENDs as overlays_overlay71_TEXT_END = ALIGN(16); for example.

I suppose it could be argued either way, some other use cases might want unaligned. Could expose a separate set of _END_ALIGNED variables to satisfy either, or perhaps configure this with an option if it's too verbose.

PartyPlanner64 avatar Sep 11 '21 05:09 PartyPlanner64

Ahh, that's annoying. I wonder why we haven't hit this issue in paper mario. I can't say working on splat has been at the top of my list lately, but at some point I'll come back around and try to reproduce / fix this. Alternatively, if you are interested in contributing, I'd gladly help point you in the right direction to tackle this stuff. lmk!

ethteck avatar Sep 11 '21 17:09 ethteck

This now works as expected and can be configured with the segment-level align: option (see changelog)

ethteck avatar Sep 12 '22 18:09 ethteck