This is an archive of the discontinued LLVM Phabricator instance.

[COFF] Aliases resolve directly to defined external targets
ClosedPublic

Authored by epastor on Sep 25 2020, 7:36 AM.

Details

Summary

Avoid introducing unnecessary indirection for weak-external references.

We only need to introduce ".weak.<SYMBOL>.default" when referencing a
symbol that is defined, but not external.

Diff Detail

Event Timeline

epastor created this revision.Sep 25 2020, 7:36 AM
Herald added a project: Restricted Project. · View Herald TranscriptSep 25 2020, 7:36 AM
epastor requested review of this revision.Sep 25 2020, 7:36 AM

This does seem to produce the right results for all the cases that I can come to think of right now, so I guess it's good, but I'd like to think about it for another day or two, if that's ok.

I tested this with an input file like this here:

        .globl uniquename
uniquename:
        ret

        .globl weakundef

        .globl func
func:
        call weakundef
        ret

        .weak aliasname
aliasname = func

        .weak secondalias
secondalias = aliasname

        .weak weakundef

        .weak weakfunc 
        .globl weakfunc
weakfunc:
        ret

        .weak undefalias
undefalias = undefsym

Maybe it'd be good to add more testcases out of these, in addition to the one added here (with verbose comments in the testcase about what's tested and what the expected outcome is), if some of the cases aren't exercised by the current tests. At least the second-level alias and alias against an undefined symbol (undefalias) are untested I think, and I don't see a case that really matches the weakfunc case either - so it'd be good to have those covered as well.

mstorsjo added inline comments.Sep 25 2020, 3:52 PM
llvm/lib/MC/WinCOFFObjectWriter.cpp
359–360

Can you come up with a scenario where you hit this else/return nullptr? Because in all of my testcases, I either hit the "return GetOr...", or return early at the first check (!Symbol.isVariable()).

I.e. it's hard to get a feel for whether this is the right condition here, as long as one could just as well remove the if statement altogether.

epastor updated this revision to Diff 294699.Sep 28 2020, 7:40 AM

Add test demonstrating hitting the else/return case

epastor updated this revision to Diff 294707.Sep 28 2020, 7:57 AM

Add test for a weak function definition

I tested this with an input file like this here:
...

Maybe it'd be good to add more testcases out of these, in addition to the one added here (with verbose comments in the testcase about what's tested and what the expected outcome is), if some of the cases aren't exercised by the current tests. At least the second-level alias and alias against an undefined symbol (undefalias) are untested I think, and I don't see a case that really matches the weakfunc case either - so it'd be good to have those covered as well.

I've added a new test file covering (I think) all of your proposed cases except the weakfunc case. That one, I added to the weak.s test file instead.

llvm/lib/MC/WinCOFFObjectWriter.cpp
359–360

This scenario comes up when making a weak reference to a local label.

I've just added a new test file for most of the alias cases I could think of; the definition of t2 there (aliasee: proc2) triggers this scenario.

mstorsjo accepted this revision.Sep 28 2020, 12:58 PM

LGTM

Awesome, thanks for finding all the relevant cases here. I've tested it with my own testsuite for GNU style weak symbols, and it seems to still be working.

llvm/test/MC/COFF/weak-alias-labels.s
88

Out of curiosity - how does the t4 case differ from t1? Only in the fact that bar is defined further ahead in the file?

This revision is now accepted and ready to land.Sep 28 2020, 12:58 PM
epastor marked an inline comment as done.Sep 28 2020, 1:08 PM
epastor added inline comments.
llvm/test/MC/COFF/weak-alias-labels.s
88

That was the idea, yes - making sure that (effective) forward declarations still worked properly.

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