This is an archive of the discontinued LLVM Phabricator instance.

[lld]: Fix RISC-V relaxation bug
AbandonedPublic

Authored by MaskRay on May 9 2023, 12:38 PM.

Details

Summary

When multiple symbol names alias to the same symbol (which is the case
when we use --wrap), the RISC-V relaxation steps are executed multiple
times for the same symbol, causing erroneous addresses being assigned to
symbols. This commit fixes this by only operating on unique symbols
(addressed by the symbol pointers).

Signed-off-by: Kishore Ganesh <kishoreganesh@rivosinc.com>

Diff Detail

Event Timeline

kishore-rivos created this revision.May 9 2023, 12:38 PM
Herald added a project: Restricted Project. · View Herald Transcript
kishore-rivos requested review of this revision.May 9 2023, 12:38 PM
reames added a subscriber: reames.May 12 2023, 8:08 AM
reames added inline comments.
lld/ELF/Arch/RISCV.cpp
506

It looks like you're only using existance in the map, so this can be a DenseSet<Symbol *>

532

It looks like symMap is only used in the scope of this loop, so you can declare it locally here.

539

I suspect we should really be checking for some property of the symbol here, but am not sufficiently knowledgeable of ELF minutia to know which one. Maybe @MaskRay or @jrtc27 might have a suggestion here?

When multiple symbol names alias to the same symbol (which is the case when we use --wrap),

This is the uncommon in wrap.s, where foo and __wrap_foo are defined in one file. This is not how --wrap is used in the wild, but if we identify a bug, we should fix it.

the RISC-V relaxation steps are executed multiple times for the same symbol, causing erroneous addresses being assigned to symbols.

A relocatable object file (%t2) indeed has multiple instances of the same symbol. Can you describe how this leads to incorrect addresses?

For riscv-wrap.s, the %t3 is identical without or with this patch.

lld/test/ELF/Inputs/riscv-wrap.s
3

Omit __real_foo. Having a .globl for an undefined symbol is a no-op, but not conventional.

Avoid .protected if STV_PROTECTED doesn't lead to behavior differences.

Add a comment before .align 5.

nop below is not indented.

lld/test/ELF/riscv-wrap.s
2

The test case copied from wrap.s needs some polishing. You may find some recent tests that use a IMHO cleaner style:

  • .o for relocatable object files. %t.o and %t to make the primary relocatable object file name match the linker output file name.
  • prefer split-file when it makes the test more readable.

This seems to have been (accidentally) fixed by D149735: symbol values are not calculated anymore by repeated subtraction (which caused this bug) but always overwritten with their new value (which fixes this).

@kishore-rivos: this patch is based on an old LLVM version where D149735 wasn't landed yet. Could you try updating your local copy?

It could still make sense to filter-out symbol aliases for performance reasons I suppose.

asb added a comment.May 16 2023, 6:23 AM

It would likely be worth separating out the tests from this patch to ensure the bug isn't reintroduced in any future refactorings.

It would likely be worth separating out the tests from this patch to ensure the bug isn't reintroduced in any future refactorings.

Agreed. @jobnoorman could I ask you to reduce a test here? @kishore-rivos is an intern who has finished his internship. I can take a shot at it, but I'm not really a linker person and I think you have a much better chance of getting a clean test on first attempt.

It would likely be worth separating out the tests from this patch to ensure the bug isn't reintroduced in any future refactorings.

Agreed. @jobnoorman could I ask you to reduce a test here? @kishore-rivos is an intern who has finished his internship. I can take a shot at it, but I'm not really a linker person and I think you have a much better chance of getting a clean test on first attempt.

Sure! D150940

MaskRay commandeered this revision.May 31 2023, 7:49 AM
MaskRay abandoned this revision.
MaskRay edited reviewers, added: kishore-rivos; removed: MaskRay.

Thanks for the report. Obsoleted by D151768