Skip to content

Conversation

@mitsuhiko
Copy link
Owner

@mitsuhiko mitsuhiko commented Jul 22, 2023

Relates to the broken fix from #1. On my machine this makes the test pass again at least.

Refs https://bugs.chromium.org/p/chromium/issues/detail?id=1466674

Reading that issue it sounds like the correct fix would be section __DATA,__mod_init_func,mod_init_funcs, but a symbol starting with I.

@BertalanD
Copy link
Contributor

Hello @mitsuhiko !

LLD developer here that has been working on fixing that Chromium issue.

The __DATA_CONST segment isn't supposed to be used directly in object files; it's the linker's job to move certain sections there when targeting an OS version that supports it (macOS 10.15, iOS 13.0 and later, see -data_const in ld's manpage). The correct link_section value is therefore "__DATA,__mod_init_func".

gory details

It would actually work with __DATA_CONST too if mod_init_funcs was specified in the section directive, as it sets the S_MOD_INIT_FUNC_POINTERS flag. That is what LLD and Apple's linkers use to determine whether a section contains init pointers and needs to be processed accordingly. For __DATA,__mod_init_func, MC automatically recognizes that it needs to be set:

https://github.com/llvm/llvm-project/blob/02c7be5094fa3ffb3f118d9e2d69af435b7c8ef6/llvm/lib/MC/MCParser/DarwinAsmParser.cpp#L309-L312

When targeting macOS 13.0, iOS 13.4 or later, runtime relocations are emitted in the fixup chains format to allow page-in linking. As part of this the pointers (which need relocation) in __mod_init_func are transformed by the linker to __init_offsets, which contain relative offsets to the initializers (no relocs needed).

https://github.com/llvm/llvm-project/blob/02c7be5094fa3ffb3f118d9e2d69af435b7c8ef6/lld/MachO/InputSection.cpp#L57-L60

If the flag is not set, the section doesn't get transformed, so ends up as __mod_init_func in the binary; which is ignored by dyld if __init_offsets exists too.

Additionally, we fixed the issue where non-local symbols (i.e. whose assembly names that don't start with uppercase/lowercase L) in __mod_init_func caused a crash during linking: llvm/llvm-project#97156. So no changes are necessary in that front.

@mitsuhiko mitsuhiko closed this Aug 1, 2024
@mitsuhiko mitsuhiko deleted the feature/mod-init-funcs branch August 1, 2024 19:50
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.

3 participants