Skip to content

[dylink] Restore --whole-archive when linking with -sSIDE_MODULE=1 - #28055

Merged
sbc100 merged 1 commit into
emscripten-core:mainfrom
sbc100:shared_no_whole_archive
Oct 27, 2026
Merged

sbc100 merged 1 commit into
emscripten-core:mainfrom
sbc100:shared_no_whole_archive

Conversation

@sbc100

@sbc100 sbc100 commented Oct 23, 2026

Copy link
Copy Markdown
Collaborator

In #25930, -shared was implemented by defaulting SIDE_MODULE to 1 (which implies LINKABLE=1) and then special-casing lld_flags in tools/building.py to avoid --whole-archive. That special case had the unintended regression of also disabling --whole-archive for explicit -sSIDE_MODULE=1 builds, while still running wasm-ld twice in phase_link for -shared due to LINKABLE=1.

Instead, default SIDE_MODULE to 2 when -shared is used so LINKABLE remains disabled and --whole-archive is applied whenever LINKABLE is set. This restores the pre-#25930 --whole-archive behavior for -sSIDE_MODULE=1 without enabling it for -shared.

To keep native linker semantics for -shared, avoid passing --no-export-dynamic when -shared is specified so default-visibility symbols in object files are still exported automatically (unlike explicit -sSIDE_MODULE=2, which continues to DCE unexported symbols).

Supersedes #27430.

Fixes: #27403

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the behavior of the -shared flag in Emscripten to better match native linker behavior regarding static archives and symbol exports, distinguishing it from -sSIDE_MODULE=1 and -sSIDE_MODULE=2. It updates documentation, changes the default SIDE_MODULE setting from 1 to 2 when linking shared libraries, and adds comprehensive tests. Feedback on the changes suggests adding a defensive check for cmdline.options in tools/building.py to avoid a potential AttributeError in environments where command-line options are not initialized.

Comment thread tools/building.py Resolved
@sbc100
sbc100 force-pushed the shared_no_whole_archive branch from bb2be82 to 203a550 Compare October 23, 2026 05:21
@sbc100
sbc100 requested review from hoodmane and kripken October 23, 2026 05:22
@hoodmane

Copy link
Copy Markdown
Collaborator

What happens if -shared and -sSIDE_MODULE=1/2 is passed? For compatibility with the way it's been up until now SIDE_MODULE should probably win.

@sbc100

sbc100 commented Oct 23, 2026

Copy link
Copy Markdown
Collaborator Author

What happens if -shared and -sSIDE_MODULE=1/2 is passed? For compatibility with the way it's been up until now SIDE_MODULE should probably win.

Sure, that sounds reasonable.

@sbc100
sbc100 force-pushed the shared_no_whole_archive branch 3 times, most recently from 849e531 to 6f27d59 Compare October 23, 2026 20:59
Comment thread tools/link.py

if options.shared and not settings.FAKE_DYLIBS:
default_setting('SIDE_MODULE', 1)
default_setting('SIDE_MODULE', 2)

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.

Doesn't this cause a large change in behavior? I mean, aside from the whole-archive stuff, can't things now break if they aren't exported and we dce them away?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

We export all dynamic symbols in options.shared mode (because we don't pass --no-export-dynamic like we do in -sSIDE_MODULE=2 mode).

I've tried to document this behaviour. Basically -shared works like it does on native platforms which is kind of half way between our =1 and =2 modes.

This change restores =1 the behaviour to before #25930 while keeping the -shared behaviour constant IIUC.

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 see, thanks. Well, no objections from me, but I am not an expert on our dynamic linking code - lgtm if @hoodmane approves

@sbc100

sbc100 commented Oct 26, 2026

Copy link
Copy Markdown
Collaborator Author

@hoodmane OK to land this?

In emscripten-core#25930, `-shared` was implemented by defaulting `SIDE_MODULE` to `1`
(which implies `LINKABLE=1`) and then special-casing `lld_flags` in
`tools/building.py` to avoid `--whole-archive`. That special case had
the unintended regression of also disabling `--whole-archive` for
explicit `-sSIDE_MODULE=1` builds, while still running `wasm-ld` twice
in `phase_link` for `-shared` due to `LINKABLE=1`.

Instead, default `SIDE_MODULE` to `2` when `-shared` is used so
`LINKABLE` remains disabled and `--whole-archive` is applied whenever
`LINKABLE` is set. This restores the pre-emscripten-core#25930 `--whole-archive`
behavior for `-sSIDE_MODULE=1` without enabling it for `-shared`.

To keep native linker semantics for `-shared`, avoid passing
`--no-export-dynamic` when `-shared` is specified so default-visibility
symbols in object files are still exported automatically (unlike
explicit `-sSIDE_MODULE=2`, which continues to DCE unexported symbols).

Supersedes emscripten-core#27430.

Fixes: emscripten-core#27403
@sbc100
sbc100 force-pushed the shared_no_whole_archive branch from 6f27d59 to 7cd837f Compare October 27, 2026 18:59

@hoodmane hoodmane left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Yeah makes sense to me. Thanks!

@sbc100
sbc100 enabled auto-merge (squash) October 27, 2026 19:27
@sbc100
sbc100 merged commit e9aeb39 into emscripten-core:main Oct 27, 2026
42 checks passed
@sbc100
sbc100 deleted the shared_no_whole_archive branch October 27, 2026 20:01
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.

Trouble with emcc 6.0.0 from #25930

3 participants