What is name mangling, and how does it work?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-17 03:36:09

问题


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.


回答1:


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




回答2:


Simply put, name-mangling is a process by which compilers changes the names of identifiers in your source code in order to aid the linker in disambiguating between those identifiers.

Wikipedia has a wonderful article on this subject with several great examples.




回答3:


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.




回答4:


In python, name-mangling is a system by which class variables have different names inside and outside the class. The programmer "activates" it by putting two underscores at the start of the variable name.

For example, I can define a simple class with some members:

>>> class Foo(object):
...  def __init__(self):
...   self.x = 3
...   self._y = 4
...   self.__z = 5
... 

In python practice, a variable name starting with an underscore is "internal" and not part of the class interface, and so programmers should not rely on it. However, it is still visible:

>>> f = Foo()
>>> f.x
3
>>> f._y
4

A variable name starting with two underscores is still public, but it is name-mangled and thus harder to access:

>>> f.__z  
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Foo' object has no attribute '__z'

If we know how the name-mangling works, however, we can get at it:

>>> f._Foo__z
5

i.e. the classname is prepended to the variable name with an extra underscore.

Python has no concept of 'private' versus 'public' members; everything is public. Name-mangling is the strongest-possible signal a programmer can send that the variable should not be accessed from outside the class.




回答5:


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



回答6:


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.




回答7:


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



回答8:


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.




回答9:


All previous answers are correct but here is the Python perspective/reasoning with example.

Definition

When an variable in a class has a prefix of __ (i.e. two underscore) & does not have a suffix of __ (i.e. two underscore or more) then it's considered private identfier. Python interpreter converts any private identifier and it mangles the name to _class__identfier

Example:
MyClassName --> _myClassName
__variable --> __variable

Why

This is needed because to avoid problems that could be caused by overriding attributes. In other words, in order to override, the Python interpreter has to be able to build distinct id for child method versus parent method and using __ (double underscore) enable python to do this. In below example, without __help this code would not work.

class Parent:
    def __init__(self):
       self.__help("will take child to school")
    def help(self, activities):
        print("parent",activities)

    __help = help   # private copy of original help() method

class Child(Parent):
    def help(self, activities, days):   # notice this has 3 arguments and overrides the Parent.help()
        self.activities = activities
        self.days = days
        print ("child will do",self.activities, self.days)


# the goal was to extend and override the Parent class to list the child activities too
print ("list parent & child responsibilities")
c = Child()
c.help("laundry","Saturdays")



回答10:


the answers here are awesome so this is just an addition from my little experience: i use name mangling in order to know , what tools ( gcc / vs /...) and how parameters passed into the stack and what calling convention i'm dealing with, and that based on the name so for example if see _main i know it's a Cdecl the same for others



来源:https://stackoverflow.com/questions/1314743/what-is-name-mangling-and-how-does-it-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!