This is an archive of the discontinued LLVM Phabricator instance.

[coff] for /msvclto, pass archive members with prevailing symbols first
ClosedPublic

Authored by inglorion on Apr 20 2017, 3:17 PM.

Details

Summary

When using /msvclto, lld and MSVC's linker both do their own symbol resolution. This can cause them to select different archive members, which can result in undefined references. This change avoids that situation by extracting archive members that are selected by lld and passing those to link.exe before any archives, so that MSVC's uses those objects for symbol resolution instead of different archive members.

Event Timeline

inglorion created this revision.Apr 20 2017, 3:17 PM
pcc added inline comments.Apr 20 2017, 3:52 PM
COFF/Driver.cpp
156

Instead of handling this here, I think you can handle it entirely within the /msvclto implementation by creating temporary files for all members of Symtab.ObjectFiles that have a ParentName.

inglorion added inline comments.Apr 20 2017, 5:56 PM
COFF/Driver.cpp
156

I like that idea, but I'm not sure how I would write the object file to disk. Its MemoryBufferRef is protected. I could make it public or create a public accessor. Or I could use the ParentName to try and locate the archive and find the object file in there...but that feels kind of brittle. I'm not sure I like either implementation better than what I've already written and tested. If you have a better suggestion, or prefer one of the alternatives I presented, I'll be happy to implement that, though.

pcc added inline comments.Apr 20 2017, 6:06 PM
COFF/Driver.cpp
156

I would just make it public. Generally we make things public when there is a need to do so. (For example, the MB in the ELF linker's InputFile is public so that we can pass it to a DWARF parser among other things.)

pcc added inline comments.Apr 20 2017, 6:10 PM
COFF/Driver.cpp
161

FWIW, it doesn't look like these temporary files are ever deleted.

inglorion updated this revision to Diff 96071.Apr 20 2017, 6:22 PM

@pcc's suggestions (thanks!)

pcc added inline comments.Apr 20 2017, 6:32 PM
COFF/Driver.cpp
529

Can't you s/EarlyObjects/Temps/ here and remove the code dealing with EarlyObjects?

530

Don't you need to append a newline here?

test/COFF/msvclto-order.ll
12

CHECK-NOT: lld-msvclto-order-b?

inglorion updated this revision to Diff 96196.Apr 21 2017, 11:57 AM
inglorion marked 3 inline comments as done.

Good call on the unnecessary vector and the missing newline! Fixed.

pcc accepted this revision.Apr 21 2017, 12:24 PM

LGTM

test/COFF/msvclto-order.ll
12

Please address

This revision is now accepted and ready to land.Apr 21 2017, 12:24 PM
inglorion added inline comments.Apr 21 2017, 1:01 PM
test/COFF/msvclto-order.ll
12

We actually still pass the library, so that check would fail. We could add some code to not pass libraries, but I'm not sure that's always correct, so I decided to not make that effort and just keep the library path in the list of things to be passed to MSVC's linker, even if we don't expect any objects in that library to be used in the link.

inglorion added inline comments.Apr 21 2017, 1:03 PM
test/COFF/msvclto-order.ll
12

After in person conversion, I realize you meant to check for the absence of the other object file, not the other library. Will do.

inglorion updated this revision to Diff 96214.Apr 21 2017, 1:05 PM

check that msvclto-order-b.obj is not passed to link.exe

pcc added a comment.Apr 21 2017, 1:21 PM

Still LGTM

This revision was automatically updated to reflect the committed changes.