What is name mangling, and how does it work?

2020-01-24 04:14发布

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.

10条回答
疯言疯语
2楼-- · 2020-01-24 04:30

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.

查看更多
狗以群分
3楼-- · 2020-01-24 04:35

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.

查看更多
ら.Afraid
4楼-- · 2020-01-24 04:36

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 of List.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.)

查看更多
不美不萌又怎样
5楼-- · 2020-01-24 04:37

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

  1. By changing number of Arguments.
  2. List item By having different types of argument.

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.

class ABC
{       
 public:
  void fun(long a, long b) {}
  void fun(float a, float b) {} 
  void fun(int a, float b) {}   
};
int main()
{
 ABC obj;
 obj.fun(1l,2l);
 obj.fun(1,2.3f);
 obj.fun(3.2f,4.2f);
 return 0;
}

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:

ayadav@gateway1:~$ nm ./a.out |grep fun
000000000040058c W _ZN3ABC3funEff
00000000004005a0 W _ZN3ABC3funEif
000000000040057a W _ZN3ABC3funEll
  • ABC is command string for class name
  • fun is common string for function name
  • ff two float->f type of arguments
  • ll two long->l typeof arguments
  • if first integer argument->i and one float->f argument
查看更多
女痞
6楼-- · 2020-01-24 04:40

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

    program test
    end program 

    subroutine foo()
    end subroutine

    subroutine b_ar()
    end subroutine
    subroutine b_a_r()
    end subroutine

Produces the following mangled symbols:

0000000000400806 g     F .text  0000000000000006              b_ar__
0000000000400800 g     F .text  0000000000000006              foo_
000000000040080c g     F .text  0000000000000006              b_a_r__

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.

查看更多
Lonely孤独者°
7楼-- · 2020-01-24 04:45

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:

class Class1
 {
        public:
            int val;
            ...
  };

val becomes something like:

  // a possible member name mangling
     val__11Class1
查看更多
登录 后发表回答