C++ name mangling

来源:互联网 发布:大螺距螺纹编程实例 编辑:程序博客网 时间:2024/06/05 09:25

a couple days ago, the boss asked me to convert a C++ project from X86 to X86_64. I thought it is easy for me, just do two the following steps:

  • 1. replace all of the x86 libraries (SO)to their counterparts of x86_64
  • 2. change the compile option from -m32 to  -m 64

After that, I tried to make the project. it ended with the following error: libcommonldap.so: undefined reference to `NewDecryptStr(char*, unsigned int)

It is a usual link error. Using objdump to check if this symbol is there.

[root@centos src]# objdump -t output/libsupport.so  | grep NewD
00000000000123af g     F .text 00000000000000e7              _Z13NewDecryptStrPcm

Yes, it is there. but why ld can not find it? I konw the symbol  _Z13NewDecryptStrPcjis what I wanted. The wired name is caused by c++ name mangling. But why the ld does not recognize it? What the ld really wants? Using objdump again to check:

[root@centos src]# objdump -t output/libcommonldap.so  | grep NewD
0000000000000000         *UND*0000000000000000             _Z13NewDecryptStrPcj


I got it. The symbol the ld is looking for is one letter different with the one exported from libsupport.so. But the defination  is exactlly the same as follows
int NewDecryptStr(char *sEncryptedPassword, const size_t sizeof_sEncryptedPassword)

Why g++ generates differents name for the same defination? It confused the whole afternoon.
The next day I thought if I coud decode the symbol to defination to check what relly happend. So I googled it and found this tool:c++filt
I decoded the two symbols as follows:

[root@centos commonsource]# c++filt  _Z13NewDecryptStrPcm
NewDecryptStr(char*, unsigned long)
[root@centos commonsource]# c++filt  _Z13NewDecryptStrPcj
NewDecryptStr(char*, unsigned int)
[root@centos commonsource]#


It is clear that the difinations are different. the size_t mapped to different types. finally, I found a typedef 

#ifndef size_t#define size_t unsigned int#endif

It is true for x86, but for x86_64, size_t should be ULONG. This is the reason



原创粉丝点击