This is an archive of the discontinued LLVM Phabricator instance.

CodeGen: Add two more conditions for adding symbols to the address-significance table.
ClosedPublic

Authored by pcc on Aug 23 2018, 4:52 PM.

Details

Summary

Firstly, require the symbol to be used within the module. If a
symbol is unused within a module, then by definition it cannot be
address-significant within that module. This condition is useful on all
platforms because it could make symbol tables smaller -- without this
change, emitting an address-significance table could cause otherwise
unused undefined symbols to be added to the object file.

But this change is necessary with COFF specifically in order to
preserve the property that an unreferenced undefined symbol in an IR
module does not result in a link failure. This is already the case for
ELF because ELF linkers only reject links with unresolved symbols if
there is a relocation to that symbol, but COFF linkers require all
undefined symbols to be resolved regardless of relocations. So if
a module contains an unreferenced undefined symbol, we need to make
sure not to add it to the address-significance table (and thus the
symbol table) in case it doesn't end up resolved at link time.

Secondly, do not add dllimport symbols to the table. These symbols
won't be able to be resolved because their definitions live in another
module and are accessed via the IAT, and the address-significance
table has no effect on other modules anyway. It wouldn't make sense
to add the IAT entry symbol to the address-significance table either
because the IAT entry isn't address-significant -- the generated code
never takes its address.

Diff Detail

Repository
rL LLVM

Event Timeline

pcc created this revision.Aug 23 2018, 4:52 PM
rnk added inline comments.Aug 24 2018, 12:56 PM
llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
1546 ↗(On Diff #162303)

Dllimport things aren't actually always imported, sometimes they can be resolved to symbols in the same image. MSVC will warn for this by default, but in my experience developers disable the warning, especially if they don't use Windows for development.

Can we end up with an incomplete table if we apply dllimport, resolve the symbol locally, and take the symbols address in that TU?

rnk added inline comments.Aug 24 2018, 12:59 PM
llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
1546 ↗(On Diff #162303)

To answer my own question, maybe we just don't worry about doing extra ICF on symbols like that. Basically, if you have code like this, you see this warning, and then disable it, and *then* you rely on the symbol's address being unique, you are out of luck.

These symbols tend to be the result of template instantiation anyway, which often have non-unique addresses with -fvisibility-inlines-hidden is set, which is pretty common.

smeenai added inline comments.
llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
1546 ↗(On Diff #162303)

Even if a dllimported symbol ends up being found in the same image, you'll still end up going through the __imp_ thunk, so the argument for the IAT entry never being address-significant should still apply?

pcc added inline comments.Aug 24 2018, 1:17 PM
llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
1546 ↗(On Diff #162303)

Either that or we could pessimise the case where code uses a locally-imported symbol. That is, we should set KeepUnique on a symbol's section when we create a DefinedLocalImport for it.

rnk accepted this revision.Aug 24 2018, 1:20 PM

lgtm

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
1546 ↗(On Diff #162303)

@smeenai I'm not sure exactly how it works, but if the code is taking the address of a locally imported symbol, I think it will load the correct, local address of the symbol.

@pcc I'm not sure it's worth the complexity. I probably shouldn't have brought up this corner case. Thinking more on it, I doubt any user will ever hit it.

This revision is now accepted and ready to land.Aug 24 2018, 1:20 PM
This revision was automatically updated to reflect the committed changes.