• Aucun résultat trouvé

Mangling Names

Dans le document For Mary (Page 144-147)

Both C++ member functions and Java methods can be overloaded by specifying different data types in the parameter list. For example, the following three lines are prototypes for entirely different functions:

int *cold(long);

int *cold(struct schold *);

int *cold(long, char *);

The compiler has no problem determining which one you call because the argument types are distinct. The only problem that arises is from the linker, because linkers blindly match the names referenced in one module with the names defined in another module without regard to their type. The solution is to have the compiler change the names in such a way that the argument information is not lost and the linker is able to match them up. The process of changing the names is calledmangling.

A mangled name is made up from the following pieces of information, in this order:

1. The base name of the function 2. A pair of underscore characters

3. A possibly zero-length list of codes indicating any function qualifiers, such asconst

4. The number of characters in the name of the class of which the function is a member

5. The name of the class

6. A list of codes indicating the data types of the parameters

For example, the functionvoid cname::fname(void)is encoded as

fname__5cname. The functionint cname::stname(long frim) constis encoded asstname__C5cnamel, whereCindicates the function isconstand the trailingl (ell) indicates a single parameter of typelong. A constructor is encoded by omitting the function name. For example, the constructorcname::cname(signed char)is encoded as__5cnameSc, where theScpair indicates asigned charparameter.

The codes for the various types and qualifiers are listed in Table 5-3. The meanings of some of the codes depend on how and where they are used in the encoding string, but with the entries in the table and a little practice you will be able to demangle the names in object files well enough to match the names with the source.

USINGTHECOMPILERCOLLECTION

Code

Letter Meaning

<number> The number of characters in a custom data type name. For example, the functionMph::pdq(char, drip, double)encodes as pdq__3Mphc4dripd. Optionally, the number can be preceded by the letterG—that is,pdq__3Mph4dripis equivalent to pdq_3MphG4drip.

A An array. In C++ the arrays always decay to pointers, so this type is never actually seen. In Java, an array is encoded as a pointer to aJArraytype.

b A C++booldata type or a Javabooleandata type.

c The C++chardata type or the Javabytedata type.

C A modifier indicating aconstparameter type or member function.

d Thedoubledata type.

e Extra arguments of unknown types. For example, the function Mph::pdq(int,...)encodesas pdq__3Mphie.

f Thefloatdata type.

G See <number>.

H A template function.

i Theintdata type.

I A special integer data type containing a nonstandard number of bits. For example, the functionMph::pdq(int, int60_t, char) with a 60-bit integer type as its second argument will be encoded aspdq__3MphiI_3C_c. A hexadecimal number surrounded by underscore characters is used to specify the number of bits in the integer. The hexadecimal number may not be delimited by underscore characters if the surrounding characters are not ambiguous.

J The C++complexdata type.

l (ell) The C++longdata type.

L The name of a local class.

Table 5-3. Code Letters Used in Name Mangling

USINGTHECOMPILERCOLLECTION Code

Letter Meaning

p A pointer. It is always followed by an indicator of the pointer type.

Same asP.

P A pointer. It is always followed by an indicator of the pointer type.

Same asp.

Q A qualified name, such as arises from a nested class.

r The C++long doubledata type.

R A C++ reference. It is always followed by an indicator of the type being referenced. For example, the functionMph::pdq(ostream&) is encoded aspdq__3MphR7ostream.

s Theshortdata type.

S IfSis used to precede the name of a class, it impliesstatic. For example,Mph::pdq(void) staticis encodedpdq__S3Mph. If Sis used to precede achardata type indicator, it impliessigned.

For example, the functionMph::pdq(signed char)is encoded pdq__3MphSc.

t A C++ template instantiation.

T A back reference to a previously seen parameter type.

u The type qualifier for a restricted pointer.

U A modifier indicating an unsigned integer data type. It is also used as a modifier for a class or namespace name to indicate Unicode encoding.

v Thevoiddata type.

V A modifier indicating avolatiledata type.

w The C++whcar_tdata type or the Javachardata type.

x The C++long longdata type or the Javalongdata type.

X A template type parameter.

Y A template constant parameter.

Table 5-3. Code Letters Used in Name Mangling (continued)

A demangler namedc++filtis part of thebinutilspackage. You can enter a mangled name on the command line, and it will present you with a demangled version of the name, as shown in the following example:

$ c++filt pdq__3MphiUsJde

Mph::pdq(int, unsigned short, __complex double, ...)

Thec++filtutility is capable of demangling more that one scheme. The scheme will vary from one platform to another and from one language to another. Among the options that can be set by using the-soption arelucid,arm,hp, andedg. Two of the language-soptions arejavafor Java andgnatfor Ada.

The mangling schemes used by GCC for C++ and Java, while compatible with one another, are not compatible with other compilers. Each compiler uses its own mangling scheme, but this is not altogether bad. Each compiler also uses its own scheme for laying out classes, implementing multiple inheritance, and in the technique for handling virtual functions. If a compatible mangling scheme were used, it would be possible to link your GCC object with modules and libraries produced by other compilers, but the programs still would not run.

Linkage

Some things appear in the object file that are not strictly a part of the executable code, but they can be important for certain optimizations and for resolving references. Some of this type of information are categorized asvague linkagebecause they are something other than the normal (and simpler) process of associating a specific name with a specific address. The following is a description of the C++ vague linkage items.

Dans le document For Mary (Page 144-147)