This is an archive of the discontinued LLVM Phabricator instance.

[ELF] Do not report undefined weak references in shared libraries
ClosedPublic

Authored by ikudrin on Dec 3 2021, 7:40 AM.

Details

Summary

This fixes an issue introduced in D101996.

A weak reference in a shared library could be incorrectly reported if there is another library that has a strong reference to the same symbol.

Diff Detail

Event Timeline

ikudrin created this revision.Dec 3 2021, 7:40 AM
ikudrin requested review of this revision.Dec 3 2021, 7:40 AM
Herald added a project: Restricted Project. · View Herald TranscriptDec 3 2021, 7:40 AM
ikudrin edited the summary of this revision. (Show Details)Dec 3 2021, 7:40 AM

(I am in a trip and have limited Internet connection.)

This is a good candidate for 13.0.1

lld/ELF/InputFiles.cpp
1539

I just noticed that we did not (before this patch) have test coverage for && !s->isWeak().

If the binding check is tested here, Writer.cpp:1978 binding check may be deleted.

lld/test/ELF/allow-shlib-undefined-weak.s
2

While the suffix name is .s, it is customary to use # for RUN/CHECK lines to not render them as instructions which may look weird in some editors.

22

When a .so is linked into another output, specify -soname= to ensure the final output has a deterministic DT_NEEDED string. It doesn't matter for this test, but can avoid a problem in case the test gets more checks.

28

Checking just the diagnostic can easily go stale. Use | count 0 if the output is empty.

40

You can remove movl (%rax), %eax. The semantic will be to get the address, which still looks like a real world example.

MaskRay added inline comments.Dec 3 2021, 10:46 PM
lld/ELF/InputFiles.cpp
1539

!sym.isWeak() would be better if we consider STB_GNU_UNIQUE.

jrtc27 added inline comments.Dec 4 2021, 8:37 PM
lld/test/ELF/allow-shlib-undefined-weak.s
28

Might also be worth adding --no-allow-shlib-undefined in case the default ever changes; I meant to do that in what I posted but forgot

ikudrin updated this revision to Diff 392045.Dec 6 2021, 6:29 AM
ikudrin marked 5 inline comments as done.
  • sym.getBinding() == STB_GLOBAL -> sym.getBinding() != STB_WEAK
  • allow-shlib-undefined-weak.s -> *.test
  • Added -soname def.so
  • Added --no-allow-shlib-undefined
  • | FileCheck %s --allow-empty -> | count 0
  • Removed movl (%rax), %eax x 2.
lld/ELF/InputFiles.cpp
1539

If the binding check is tested here, Writer.cpp:1978 binding check may be deleted.

We probably cannot delete that check.

If we link exec that references symbols in a.so that in turn references symbols in b.so, and we include b.so in the link with --as-needed, the symbols defined in b.so will be demoted to undefined weak. Now, if we removed the check for !sym->isWeak() in Writer<ELFT>::finalizeSections(), that would produce false errors. We could avoid that by improving the check for allNeededIsKnown to consider shared libraries with isNeeded == false as unknown, but that would disable diagnostics for a.so entirely.

lld/test/ELF/allow-shlib-undefined-weak.s
2

It was supposed to be .test. Just forgot to rename.

MaskRay accepted this revision.Dec 6 2021, 10:03 AM

Looks great! This form should be safe in release/13.x

This revision is now accepted and ready to land.Dec 6 2021, 10:03 AM
jrtc27 added inline comments.Dec 6 2021, 10:13 AM
lld/test/ELF/allow-shlib-undefined-weak.test
1 ↗(On Diff #392045)

The trouble with a .test is then you don't get syntax highlighting for the assembly...

.s with commented-out REQUIRES/RUN is better IMO

ikudrin marked an inline comment as done.Dec 6 2021, 7:21 PM
ikudrin added inline comments.
lld/test/ELF/allow-shlib-undefined-weak.test
1 ↗(On Diff #392045)

The trouble with a .test is then you don't get syntax highlighting for the assembly...

.s with commented-out REQUIRES/RUN is better IMO

Makes sense, thanks.

This revision was automatically updated to reflect the committed changes.
ikudrin marked an inline comment as done.

Hi Tom! Can we add this to the next stable release?