C++ name mangling

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  _Z13NewDecryptStrPcj is 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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值