4

Suppose I've written a foo<T> function (I have a full signature with namespaces), but never mind that right now); and suppose there is no other function overloading it (in the relevant namespace it's in). Now let's place ourselves at runtime. Suppose I have the string "foo", and for some type MyType, I have typeid(MyType) (from the <memory> header).

Can I somehow obtain the symbol name for foo<MyType>?

Second version of this question: Now suppose I have the full signature of foo as a string, instead of just the name; and drop the assumption about no overloads.

Notes:

  • No, I'm not asking about the symbol itself, just the name. That would be an interesting question for another time.
  • Answers which depend on foo<T> coming from a shared library are relevant, although I don't think it should matter just for the symbol name.
  • I don't care about performance here, I'll do whatever it takes. Help me Obi Wan, you're my last hope etc. So, RTTI, compiling with weird flags, whatever.
  • Platform-dependent answers are also relevant: GNU/Linux with kernel version >= 3.x , an x86_64 CPU , gcc >= 4.8 .
einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • Possible duplicate of [Determining to which function a pointer is pointing in C?](http://stackoverflow.com/questions/33215374/determining-to-which-function-a-pointer-is-pointing-in-c) – Marcus Müller Jan 19 '16 at 22:56
  • This is the same for C and C++, unless you extensively use RTTI, which is, as far as I can tell, a rather uncommon choice (both due to negative side effects and not really being all that helpful). – Marcus Müller Jan 19 '16 at 22:57
  • @MarcusMüller: I don't see how that's related, actually. – einpoklum Jan 19 '16 at 22:57
  • C and C++ are non-reflecting languages. There's no information what your method/function is called like after it gets compiled. – Marcus Müller Jan 19 '16 at 22:58
  • Do you mean the mangled name? – Alan Stokes Jan 19 '16 at 22:58
  • 1
    @MarcusMüller: I know they're non-reflecting languages, otherwise I wouldn't be interested in things like symbols, which exist after we've gone from language code to compiled form... – einpoklum Jan 19 '16 at 22:59
  • The answer is likely to be platform dependent at best, and will depend on the signature of `foo`. – Alan Stokes Jan 19 '16 at 22:59
  • Have you had a look at what the symbol names look like on your platform, for different types for `MyTime`? You could probably deduce how to construct them. – Alan Stokes Jan 19 '16 at 23:02
  • @AlanStokes: I've seen that there's a lot of mangled gibberishy text there. But if it's a deterministic function of the signature, then I guess what I'd need is someone to point me at some library which does this mangling for me. – einpoklum Jan 19 '16 at 23:05
  • I don't think that there's a platform independent answer to this. Maybe you'll need to specify which platform you want an answer for? – Damyan Jan 19 '16 at 23:11
  • I suppose you can try using Clang's mangler, but I think that requires building an AST first. – T.C. Jan 19 '16 at 23:14
  • In the absence of a shared library, does `foo` even _have_ a symbol name? Is it even a symbol? Code is just a series of assembly instructions which in general have no 1:1 relation to the original sources. What exactly are you naming, then? – MSalters Jan 20 '16 at 12:06
  • One thing: [See potentially related question](http://stackoverflow.com/questions/10127982/why-is-name-mangling-not-standardized). Another thing: do you actually have `foo` in your code? Otherwise there really is no runtime symbol for this, since templates are only created when used (unlike with some generic languages) – grek40 Jan 20 '16 at 12:08

1 Answers1

1

No, you can't.

To get the mangled name of an instantiated function template, you need in the simplest case the following information:

  • The fully qualified name of the function (you said you only have "foo", what if the function is in a namespace?)
  • The types of all template type arguments (the mangled name of the type may suffice, if the mangling scheme for the function name embeds the type name directly; otherwise you'd need the full type name, potentially recursively into all template arguments of the type).
  • The types of all function arguments (same caveat applies).

This is assuming that you don't have template template parameters, or non-type parameters. It gets a lot more complicated when you have those, since it may require mangled forms of entire expression trees. It also assumes that you're not dealing with partial or full explicit specialization, which is even more complicated. And it is finally assuming that your function doesn't have any special decoration due to compiler-specific extensions (e.g. __stdcall in 32-bit Windows environments). Oh, and some ABIs may encode the return type of the function as well.

Since according to your premise you only have the function name (not clear on whether it is fully qualified) and the type_id objects of the template arguments (which may work as a source of the mangled type name, but do not on all platforms), you have insufficient information to recreate the mangled name.

This leaves the option of obtaining a list of all compiled symbols from your binary (if such is available) and searching for a most likely candidate, which is of course error-prone.

Sebastian Redl
  • 69,373
  • 8
  • 123
  • 157
  • I meant I do have the information you listed, I just didn't provide an example of all that. – einpoklum Apr 25 '18 at 12:52
  • 1
    @einpoklum Re: second version. Unfortunately the full signature of the function as a string is still insufficient, since the mangling of the types in the signature depends on the definition of the types. Typedefs are looked through, and classes and enums may have different manglings. (In the case of the MS compiler, `struct` and `class` trigger different mangling, which is actually not standards-conforming.) – Sebastian Redl Apr 25 '18 at 13:12