Skip to content

feat(elf): place __global_pointer$ at .sdata + 0x800 - #3027

Merged
davidlattimore merged 3 commits into
wild-linker:mainfrom
erscholtz:riscv64-gp-setup
Nov 24, 2026
Merged

davidlattimore merged 3 commits into
wild-linker:mainfrom
erscholtz:riscv64-gp-setup

Conversation

@erscholtz

@erscholtz erscholtz commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

In this PR the __global_pointer$ is moved from the start of .data to using the formula from LLD's default script:

.sdata + 0x800

To do this, new sections had to be inserted between .data and .bss, namely .sdata and .sbss. These new sections also impact the output sections of all supported ELF architecture, including RISCV.

Known issue: linker-script-region-lma fails on aarch64. This is due to the new .sdata and .sbss moving .bss over to another segment, crossing the test file size limit. Open to suggestions on skipping empty .sdata/.sbss there vs. limiting them to RISC-V.

Related to #2520: gp needs to be in the right place before relaxation work can start. Opening this PR to check if GNU ld's approach is the direction planned for wild; happy to change it if not.

@davidlattimore

davidlattimore commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

I'm unsure how essential it is for us to exactly match BFD's formula for this symbol. LLD seems to use a simpler formulation. One thing I did just notice, looking at the pre-existing code for this symbol is that the address in symtab is inconsistent with the value used by relocations which reference the symbol. edit: Your change appears to fix this, which is good, but it's possible that fix should be separate from changes to the formula used.

@erscholtz

Copy link
Copy Markdown
Contributor Author

Thanks for the quick reply, I can split the symtab/relocation fix into a separate PR and adjust this one to follow the simpler LLD formula for placing gp. Does this approach sound good?

@davidlattimore

Copy link
Copy Markdown
Member

Thanks, that sounds good.

@erscholtz erscholtz changed the title feat(elf): place __global_pointer$ using GNU ld's formula feat(elf): place __global_pointer$ using LLD's formula Nov 22, 2026
@erscholtz

Copy link
Copy Markdown
Contributor Author

I've switched gp placement over to LLD's formula: .sdata + 0x800, or the ELF header + 0x800 when there's no .sdata, and it's no longer defined for -shared and .sdata/.sbss don't get emitted when they are empty as well.

There are two CI failures I'm not sure how I should handle:

linker-script-region-lma fails on aarch64, loongarch64 and ppc64le. The script uses MEMORY regions and doesn't mention .sdata, so the empty .sdata gets placed outside the region. It never gets emitted, but it still costs a 64 KiB page and pushes the file over the size limit (adding .sdata to the script with > RAM makes it pass). One fix is changing how empty unlisted sections interact with regions, which would also cover any future built-in section, though it touches shared layout code. Limiting .sdata/.sbss to RISC-V is another option that I see.

riscv64/z-unique/exec and riscv64/tls-common/default fail on gp's section in .dynsym. With no .sdata, wild puts gp in section 1 (.interp), which is what LLD does too (I checked locally), but GNU ld, the reference for these tests, reports no section. An ignore in linker-diff would accept LLD's behaviour here, but I'm not sure if that's how you'd want to handle this case.

@davidlattimore

Copy link
Copy Markdown
Member

Regarding dealing with the empty .sdata section, perhaps #3068 will help?

@erscholtz

Copy link
Copy Markdown
Contributor Author

Hey David, that was the fix, thank you so much! I've synced the PR with the current main and the aarch64 CI tests now pass.

One thing to note: I also added in the diff-ignore from my previous message as a separate commit on top. With this commit all of the CI tests now pass. I just want to confirm if this is the direction you see wild going for that case, since I don't want to change what linker-diff ignores without a clear go ahead. If you'd rather handle it differently, I'm happy to revert that commit and take another approach.

@erscholtz
erscholtz marked this pull request as ready for review November 24, 2026 01:45

@davidlattimore davidlattimore left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Give how variable the section into which the symbol gets placed is, I think adding the diff-ignore as you've done is reasonble.

Comment thread libwild/src/layout.rs Outdated
// Like LLD, a section-offset symbol (e.g. RISC-V's __global_pointer$) whose section has no
// content is placed relative to the file header instead, so that it doesn't force an empty
// section to be emitted.
for definition in &mut self.internal_symbols.symbol_definitions {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about doing this here. It introduces a side-effect to determine_kept_sections. Also, the code here doesn't take into account secondary sections. I think both problems can be solved together. If you moved this code to a separate function that was run after determine_kept_sections returns, then it'd automatically end up taking secondary sections into account, since the code below keeps the primary section if one of its secondary sections is kept would already have run. You might need to change the loop immediately below this one so that SymbolPlacement::SectionStartOffset doesn't cause a symbol to be kept.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, will update here shortly.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a second function running right after determine_kept_sections for relocating symbols in sections not kept to the file header. Also updated the loop in determine_kept_sections to ignore SymbolPlacement::SectionStartOffset.

@davidlattimore davidlattimore left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks :)

@davidlattimore davidlattimore changed the title feat(elf): place __global_pointer$ using LLD's formula feat(elf): place __global_pointer$ at .sdata + 0x800 Nov 24, 2026
@davidlattimore
davidlattimore added this pull request to the merge queue Nov 24, 2026
Merged via the queue into wild-linker:main with commit 980c702 Nov 24, 2026
24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants