This is an archive of the discontinued LLVM Phabricator instance.

[COFF] Fix /export:foo=bar when bar is a weak alias
ClosedPublic

Authored by rnk on Jun 6 2019, 2:41 PM.

Details

Summary

When handling exports from the command line or from .def files, the
linker does a "fuzzy" string lookup to allow finding mangled symbols.
However, when the symbol is re-exported under a new name, the linker has
to transfer the decorations from the exported symbol over to the new
name. This is implemented by taking the mangled symbol that was found in
the object and replacing the original symbol name with the export name.

Before this patch, LLD implemented the fuzzy search by adding an
undefined symbol with the unmangled name, and then during symbol
resolution, checking if similar mangled symbols had been added after the
last round of symbol resolution. If so, LLD makes the original symbol a
weak alias of the mangled symbol. Later, to get the original symbol
name, LLD would look through the weak alias and forward it on to the
import library writer, which copies the symbol decorations. This
approach doesn't work when bar is itself a weak alias, as is the case in
asan. It's especially bad when the aliasee of bar contains the string
"bar", consider "bar_default". In this case, we would end up exporting
the symbol "foo_default" when we should've exported just "foo".

To fix this, don't look through weak aliases to find the mangled name.
Save the mangled name earlier during fuzzy symbol lookup.

Fixes PR42074

Event Timeline

rnk created this revision.Jun 6 2019, 2:41 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 6 2019, 2:41 PM
mstorsjo accepted this revision.Jun 6 2019, 9:11 PM

Looks good and thought through. I had a few nits though.

lld/COFF/SymbolTable.cpp
475

This regex is a little misleading/incomplete, as it can also match by skipping the first char of Prefix. (I guess that can't be expressed in regex syntax easily...)

lld/test/COFF/entry-mangled.test
5

Is this -verbose needed? I see no checks for its contents.

This revision is now accepted and ready to land.Jun 6 2019, 9:11 PM
ruiu accepted this revision.Jun 6 2019, 11:34 PM

LGTM

lld/COFF/SymbolTable.cpp
494

I wondered why you needed to call getSymWithPrefix first instead of scanning the entire symbol table. Looks like this is just a performance optimization; find candidates first and then try to find a match from them. Can you add a comment about that?

llvm/include/llvm/Object/COFFImportFile.h
72–87

Thank you for doing this!

rnk marked 5 inline comments as done.Jun 7 2019, 2:56 PM

Thanks!

lld/test/COFF/entry-mangled.test
5

Oops, it was for debugging.

llvm/include/llvm/Object/COFFImportFile.h
72–87

Hopefully it's correct. :)

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