[lld] [LLD] [COFF] Handle manually defined __imp_ pointers in LTO (PR #70777)

Martin Storsjö via llvm-commits llvm-commits at lists.llvm.org
Sat Nov 18 05:32:19 PST 2023


mstorsjo wrote:

> This patch forces `__imp_sym` to be retained in the output. Am I right that this could be relaxed to when `sym` is referenced?

Yes, that's correct.

> But that would introduce some complexity as `__imp_sym` `sym->isUsedInRegularObj = true;` needs to be done after all input files are parsed.

Indeed - something like what I do in #71376 could perhaps help here as well - but this case is a bit trickier. When LLD looks at the bitcode object files, it sees one with an undefined `__imp_sym` and one with a defined `__imp_sym` - i.e. a perfectly normal situation, nothing to do about that. But when the LTO engine compiles it, it sees one defined `__imp_sym` with no references within the set of bitcode files, and one undefined `sym` with a dllimport attribute.

In practice I don't think this affects the overall end result much, because this kind of trickery with manually defined `__imp_` symbols is very rare in normal code; it's used a bunch within mingw-w64-crt, but also there, the link would mostly include those object files that actually are needed. But perhaps it does end up retaining a handful of `__imp_` pointers that might not be needed. I would think that MSVC like environments almost never use this construct.

Ideally, the optimal condition would be to only mark `isUsedInRegularObj` on any defined bitcode symbol with an `__imp_` prefix, if any other of the bitcode input files has an undefined `__imp_sym` symbol. I'll see if I get time to take another shot at this.


https://github.com/llvm/llvm-project/pull/70777


More information about the llvm-commits mailing list