Please explain what is name mangling, how it works, what problem it solves, and in which contexts and languages is used. Name mangling strategies (e.g. what name is chosen by the compiler and why) a plus.
相关问题
- Name for a method that has only side effects
- Best way to keep the user-interface up-to-date?
- should I write more descriptive function names or
- Open a new tab in firefox and keep ff in the backg
- How do I write a function to compare and rank many
相关文章
- Should client-server code be written in one “proje
- Algorithm for maximizing coverage of rectangular a
- Is there an existing solution for these particular
- What is Scope Creep? [closed]
- How can I modify .xfdl files? (Update #1)
- What does '_GLOBAL__sub_I_' mean in nm out
- What is the best algorithm to shuffle cards? [clos
- Hashtable and list side by side?
At the time that link editors were designed, languages such as C, FORTAN and COBOL did not have namespaces, classes, members of classes and such other things. Name mangling is required to support object-oriented features such as those with a link editor that does not support them. The fact that the link editor does not support the additional features is often missed; people imply it by saying that name mangling is required due to the link editor.
Since there is so much variation among language requirements to support what name mangling does, there is not a simple solution to the problem of how to support it in a link editor. Link editors are designed to work with output (object modules) from a variety of compilers and therefore must have a universal way to support names.
Name mangling is a means by which compilers modify the "compiled" name of an object, to make it different than what you specified in a consistent manner.
This allows a programming language the flexibility to provide the same name to multiple, compiled objects, and have a consistent way to lookup the appropriate object. For example, this allows multiple classes with the same name to exist in different namespaces (often by prepending the namespace into the class name, etc).
Operator and method overloading in many languages take this a step further - each method ends up with a "mangled" name in the compiled library in order to allow multiple methods on one type to exist with the same name.
In the programming language of your choice, if an identifier is exported from a separately compiled unit, it needs a name by which it is known at link time. Name mangling solves the problem of overloaded identifiers in programming languages. (An identifier is "overloaded" if the same name is used in more than one context or with more than one meaning.)
Some examples:
In C++, function or method
get
may be overloaded at multiple types.In Ada or Modula-3, function
get
may appear in multiple modules.Multiple types and multiple modules cover the usual contexts.
Typical strategies:
Map each type to a string and use the combined high-level identifier and "type string" as the link-time name. Common in C++ (especially easy since overloading is permitted only for functions/methods and only on argument types) and Ada (where you can overload result types as well).
If an identifier is used in more than one module or namespace, join the name of the module with the name of the identifier, e.g.,
List_get
instead ofList.get
.Depending on what characters are legal in link-time names, you may have to do additional mangling; for example, it may be necessary to use the underscore as an 'escape' character, so you can distinguish
List_my.get
->List__my_get
from
List.my_get
->List_my__get
(Admittedly this example is reaching, but as a compiler writer, I have to guarantee that distinct identifiers in the source code map to distinct link-time names. That's the whole reason and purpose for name mangling.)
Most of the object oriented language provide function overloading feature. Function Overloading If any class have multiple functions with same names but different parameters type & number then they are said to be overloaded. Function overloading allows you to use the same name for different functions.
Ways to overload a function
How function overloading is achieved with name mangling?
C++ compiler distinguishes between different functions when it generates object code – it changes names by adding information about arguments based on type and number of arguments. This technique of adding additional information to form function names is called Name Mangling. C++ standard doesn’t specify any particular technique for name mangling, so different compilers may append different information to function names. I have run the sample program on gcc4.8.4.
This program have 3 functions named fun with differ on based on number of arguments and their types. These functions name's are mangled as below:
In Fortran, name mangling is needed because the language is case insensitive, meaning that Foo, FOO, fOo, foo etc.. will all resolve to the same symbol, whose name must be normalized in some way. Different compilers implement mangling differently, and this a source of great trouble when interfacing with C or binary objects compiled with a different compiler. GNU g77/g95, for example, always adds a trailing underscore to the lowercased name, unless the name already contains one or more underscores. In this case, two underscores are added.
For example, the following routine
Produces the following mangled symbols:
In order to call Fortran code from C, the properly mangled routine name must be invoked (obviously keeping into account possible different mangling strategies to be truly compiler independent). To call C code from fortran, a C-written interface must export properly mangled names and forward the call to the C routine. This interface can then be called from Fortran.
Source:http://sickprogrammersarea.blogspot.in/2014/03/technical-interview-questions-on-c_6.html
Name mangling is the process used by C++ compilers give each function in your program a unique name. In C++, generally programs have at-least a few functions with the same name. Thus name mangling can be considered as an important aspect in C++.
Example: Commonly, member names are uniquely generated by concatenating the name of the member with that of the class e.g. given the declaration:
val becomes something like: