开发者

How does linkage and name mangling work?

开发者 https://www.devze.com 2023-03-16 13:40 出处:网络
Lets take this code sample //header struct A { }; struct B { }; struct C { }; extern C c; //code A myfunc(B&b){ A a; return a; }

Lets take this code sample

//header
struct A { };
struct B { };
struct C { };
extern C c;

//code
A myfunc(B&b){ A a; return a; }
void myfunc(B&b, C&c){}
C c;

Lets do this line by line starting from the code section. When the compiler sees the first myfunc method it does not care about A or B because its use is internal. Each c++ file will know what it takes in, what it returns. Although there needs to be a name for each of the two overload so how is that chosen and how does the linker know which means what? Next is C c; I once had a bug were the linker wouldnt reconize thus allow me access to C in other C++ files. It was because that cpp didnt know c was extern and i had to mark it as extern in the head开发者_Go百科er before i could link successfully. Now i am not sure if the class type has any involvement with the linker and the variable C. I dont know how RTTI will be involved but i do know C needs to be visible by other files.

How does the linker work and name mangling and such.


We first need to understand where compilation ends and linking begins. Compilation involves taking a compilation unit (a C or C++ source file) and turning it into an object file. Simplistically, this involves generating snippets of machine code for each function as well as a symbol table for all functions and static (global) variables. Placeholders are used for any symbols needed by the compilation unit that are external to the said compilation unit.

The linker is then responsible for loading all the object files and resolving all the place-holder symbols with real addresses (or offsets for machine independent code). This is placed into various sections that can be read by the operating system's dynamic loader when loading an executable.

So for the specifics. In order to avoid errors during linking, the compiler requires you to declare all external symbols that will be used by the current compilation unit. For global variables one must use the extern keyword, for functions this is optional.

All functions and global variables defined in a compilation unit have external linkage (i.e., can be referenced by other compilation units) unless one declares that with the static keyword (or the unnamed namespace in C++). In C++, the vtable will also have a symbol needed for linkage.

Now in C++, since functions can be overloaded, the parameters also form part of the function name. Since machine-code is just addresses and registers, extra information needs to be added to the function name in the symbols table. This extra parameter information comes in the form of a mangled name and ensures that the linker links to the correct version of an overloaded function.

If you really are interested in the gory details take a look at the ELF file format (PDF) used extensively on Linux. Windows has a different format but the principles can be expected to be the same. Name mangling on the Itanuim (and ARM) platforms can be found here.


http://en.wikipedia.org/wiki/Name_mangling

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号