Skip to content

test: Add test for weak ref to as-needed shared library#1672

Merged
davidlattimore merged 3 commits intowild-linker:mainfrom
PritamP20:fix/as-needed-weak-shared-lib
Mar 12, 2026
Merged

test: Add test for weak ref to as-needed shared library#1672
davidlattimore merged 3 commits intowild-linker:mainfrom
PritamP20:fix/as-needed-weak-shared-lib

Conversation

@PritamP20
Copy link
Contributor

@PritamP20 PritamP20 commented Mar 9, 2026

The original intent of this PR was to fix an issue where --as-needed shared libraries were not being activated when only a weak symbol referenced them.

Initially, this seemed like a divergence from GNU ld and lld, so the change attempted to activate the shared library in that case.

After further investigation and testing with GNU ld and lld, it turned out that both of them do not activate --as-needed shared libraries for weak references. The behavior expected by the mold test (as-needed-weak.sh) is therefore mold-specific. Wild already behaves the same as GNU ld and lld.

Changes in this PR

  • Mark as-needed-weak.sh as mold-specific in mold_skip_tests.toml.
  • Add a Wild integration test (as-needed-weak.c) to verify the expected behavior: a weak reference should not activate a --as-needed shared library.
  • The test ensures the library is not added to DT_NEEDED, matching GNU ld and lld behavior.

Result

Wild’s behavior now has explicit test coverage confirming that weak references do not activate --as-needed shared libraries, consistent with GNU ld and lld.

// file got loaded. TODO: If the file is a non-archived object, or possibly even if
// it's an archived object that we've already decided to load, then we could skip
// this.
// The symbol is weak and the target is an archive (not a shared library). We
Copy link
Member

Choose a reason for hiding this comment

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

It might not be an archive. It could also be a regular object file right?

Copy link
Contributor Author

@PritamP20 PritamP20 Mar 9, 2026

Choose a reason for hiding this comment

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

Yeah, it can be a regular object file too I'll fix the comment. The main thing this block handles is the case where a weak reference to a shared library wasn't activating it for --as-needed, so it was getting dropped from DT_NEEDED

@davidlattimore
Copy link
Member

Do the tests pass for you locally? If they do, then you might want to look at your test-config.toml or copy test-config-ci.toml if you don't have one.

@PritamP20
Copy link
Contributor Author

I think I understand why my earlier fix was breaking the tests. When try_request_file_id activates a --as-needed DSO for a weak ref, the file becomes loaded. Then in canonicalise_undefined_symbols, the ignore_if_loaded check treats the symbol as defined and skips the ValueFlags::DYNAMIC path. This causes the symbol to keep the DSO’s version metadata (e.g. gmon_start@unversioned) instead of being emitted as a plain undefined dynamic symbol like GNU ld does

@PritamP20
Copy link
Contributor Author

Hi @davidlattimore,

I'm working on fixing the as-needed-weak.sh mold test. I added logic in resolve_symbol to call try_request_file_id for --as-needed DSOs when a weak reference from a regular object file resolves to one during an executable build.

But this introduces a regression trivial_main_c__ and similar integration tests start failing because wild now pulls libgcc_s.so.1 into DT_NEEDED for weak CRT references like _ITM_deregisterTMCloneTable from crtbeginS.o, which GNU ld doesn’t do.

I tried guarding with symbol_db.get(name, allow_dynamic=false).is_none() so we skip activation if a static alternative exists, but on Alpine musl CI there’s no static provider for those symbols in libgcc.a, so the guard doesn’t help.

Not sure if as-needed-weak.sh is testing mold-specific behavior that differs from GNU ld. If that’s the case, should the test remain skipped, or is there a better way to distinguish the fn1 case (user weak ref should activate DSO) from the _ITM_* case (CRT weak ref should not activate)?

@davidlattimore
Copy link
Member

Usually in cases like this, it's necessary to do some experiments. I'd suggest writing a test using our own testing framework. We probably want to do that anyway, since we want our test suite to be reasonably complete when run without the external test suites. That'll make it easier to run with GNU ld and / or LLD so that you can confirm if they have the same behaviour. You could also try running the mold test with GNU ld to see if it passes.

@PritamP20
Copy link
Contributor Author

@davidlattimore sure I will experiment with all the edge scenario which are possible and let you know where i land with it

@PritamP20
Copy link
Contributor Author

PritamP20 commented Mar 12, 2026

I re-ran the test with GNU ld and lld to double check. Both of them fail the mold test as well, so it seems this behaviour is mold-specific, Wild might already be behaving correctly here

I will add a small Wild test to confirm the expected behaviour.

@davidlattimore
Copy link
Member

Thanks for investigating. It'd be useful to also update mold_skip_tests.toml. e.g. add a comment on that test that says something like "Mold-specific behaviour". It's fine to do it in the same PR as adding the new test since they're pretty related.

@PritamP20 PritamP20 force-pushed the fix/as-needed-weak-shared-lib branch from 17e7b8d to b1e4071 Compare March 12, 2026 02:22
@davidlattimore davidlattimore changed the title fix: activate --as-needed shared libraries for weak symbol references test: Add test for weak ref to as-needed shared library Mar 12, 2026
Copy link
Member

@davidlattimore davidlattimore left a comment

Choose a reason for hiding this comment

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

Thanks. Always good to see our test coverage improve :)

Copy link
Member

@davidlattimore davidlattimore left a comment

Choose a reason for hiding this comment

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

Looks like there's a compilation error with the merge

//#Object:runtime.c
//#Mode:unspecified
//#Shared:template(--push-state --as-needed $O --pop-state):as-needed-weak-lib.c
// GNU ld drops PT_INTERP when no shared libs are needed, producing a static
Copy link
Member

Choose a reason for hiding this comment

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

If you wanted, you could add the following instead:

//#Shared:force-dynamic-linking.c
//#DiffIgnore:.dynamic.DT_NEEDED

@lapla-cogito
Copy link
Member

Also, could you update the PR's description? This would be useful for others to review later.

@davidlattimore
Copy link
Member

davidlattimore commented Mar 12, 2026

Also, could you update the PR's description? This would be useful for others to review later.

I already changed the PR title. Unless there's further issues with the title?

@lapla-cogito
Copy link
Member

I don't see any issues with the title itself, but having the title and description content significantly differ would only cause confusion, which is why I'd like Pritam to update the content.

@davidlattimore
Copy link
Member

Oh, right. Sorry, you did say description. I often just delete the description from the commit message when I merge, so I didn't pay any attention to that.

@PritamP20
Copy link
Contributor Author

@davidlattimore @lapla-cogito Thanks for the review and feedback. I will update the PR description so that it matches the changes and conclusions made.

@PritamP20 PritamP20 force-pushed the fix/as-needed-weak-shared-lib branch from a2dea4e to c91a302 Compare March 12, 2026 05:59
@davidlattimore
Copy link
Member

Sorry, you possibly need //#Mode:dynamic too. See other tests that use force-dynamic-linking.c.

@PritamP20
Copy link
Contributor Author

@davidlattimore yeah, I figured it out. I'm testing it locally first before pushing the update.

@PritamP20 PritamP20 force-pushed the fix/as-needed-weak-shared-lib branch from 4cbed0f to 4eb63b0 Compare March 12, 2026 07:27
@davidlattimore
Copy link
Member

For the error:

section.got
  wild Section missing
  ld OK

It's fine to just diffignore it. Wild doesn't emit the GOT if it isn't needed, so differences like this are currently pretty common.

@PritamP20 PritamP20 force-pushed the fix/as-needed-weak-shared-lib branch 2 times, most recently from e25958a to a7a8eb7 Compare March 12, 2026 09:58
@PritamP20 PritamP20 force-pushed the fix/as-needed-weak-shared-lib branch from 35bf737 to 0b07001 Compare March 12, 2026 10:58
@davidlattimore davidlattimore merged commit 42c35f1 into wild-linker:main Mar 12, 2026
22 checks passed
@PritamP20
Copy link
Contributor Author

@lapla-cogito I have updated the PR description, can you review it once

@lapla-cogito
Copy link
Member

Thanks! Looks good.

@PritamP20 PritamP20 deleted the fix/as-needed-weak-shared-lib branch March 12, 2026 13:42
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