This is an archive of the discontinued LLVM Phabricator instance.

lld-link: Also demangle undefined dllimported symbols.
ClosedPublic

Authored by thakis on Sep 15 2018, 5:39 PM.

Details

Summary

dllimported symbols go through an import stub that's called __imp_ followed by the name the stub points to. Make that work.

Follow-up to https://reviews.llvm.org/D52104

Diff Detail

Repository
rL LLVM

Event Timeline

thakis created this revision.Sep 15 2018, 5:39 PM
thakis updated this revision to Diff 165672.Sep 15 2018, 5:40 PM

fix case on variable

ruiu accepted this revision.Sep 15 2018, 5:48 PM

LGTM

Common/Strings.cpp
41 ↗(On Diff #165672)

Is "imp_" name mangling really only for C++ symbol names? I wonder if it's wrong to demangle imp_f as "__declspec(dllimport) f".

This revision is now accepted and ready to land.Sep 15 2018, 5:48 PM
ruiu added a comment.Sep 15 2018, 6:15 PM

Oh sorry I didn't intend to LGTM this yet.

Thanks!

Common/Strings.cpp
41 ↗(On Diff #165672)

__imp_ is also used for C names. But as far as I can tell, for C names a prefix of __imp_ doesn't necessarily mean that this is an import thunk for whatever follows it: the thunk for __declspec(dllimport) fand the name of a regular function void _imp__f() both get the name __imp__f: https://godbolt.org/z/sbObpp So while this is likely the right thing to do in C, it would only be a heuristic there.

If you want, I can add this for C too -- we might want to also don't strip the leading _that the compiler adds for C functions and chop off the @correction suffix that's added for some calling conventions if we wan to "demangle" C names. Currently we only demangle C++ names, so I only added this for C++ symbols.

ruiu added a comment.Sep 17 2018, 8:16 AM

LGTM. I'm convinced that it makes sense to demangle only C++ names.

Common/Strings.cpp
43 ↗(On Diff #165672)

Maybe this comment is too detailed for a first-time reader of this code? I'd just say that we demangle only C++ names.

thakis marked an inline comment as done.Sep 17 2018, 9:32 AM

Thanks! Changed the comment; landing.

Common/Strings.cpp
41 ↗(On Diff #165672)

Somewhat entertainingly, the following program links and runs fine (with cl and link; 32-bit, for 64-bit it needs to be called __imp_f since no _ are inserted by the compiler there):

#include <stdio.h>

// Note: Declared only, not defined.
__declspec(dllimport) int f();

void g(void) { printf("hi\n"); }
void (*_imp__f)(void) = &g;

int main() { f(); }
This revision was automatically updated to reflect the committed changes.