一、查看 .rodata
c
ubuntu@aaa $ readelf -x .rodata a.out
Hex dump of section '.rodata':
0x00002000 01000200 00000000 02000000 00000000 ................
0x00002010 20204465 72697665 6420636c 61737320 Derived class
0x00002020 6f626a65 63742069 73206372 65617465 object is create
0x00002030 6420616e 64206675 6e637469 6f6e2066 d and function f
0x00002040 28292069 73206361 6c6c6564 2e000000 () is called....
0x00002050 37446572 69766564 00344261 736500 7Derived.4Base.
ubuntu@aaa $
二、grep vtable没有结果
ubuntu@aaa$ readelf -x .rodata a.out |grep vtalbe
ubuntu@aaa $
三、虚函数除了声明,还需要定义
c
ubuntu@aa $ g++ polymorph.cpp
/usr/bin/ld: /tmp/ccstK1jB.o:
warning:
relocation against `_ZTV4Base' in read-only section `.text._ZN4BaseC2Ev[_ZN4BaseC5Ev]'
/usr/bin/ld: /tmp/ccstK1jB.o:
in function `Base::Base()':
polymorph.cpp:
(.text._ZN4BaseC2Ev[_ZN4BaseC5Ev]+0xf):
undefined reference to `vtable for Base'
/usr/bin/ld: /tmp/ccstK1jB.o:
in function `Derived::Derived()':
polymorph.cpp:
(.text._ZN7DerivedC2Ev[_ZN7DerivedC5Ev]+0x1f):
undefined reference to `vtable for Derived'
/usr/bin/ld: warning:
creating DT_TEXTREL in a PIE
collect2: error: ld returned 1 exit status
ubuntu@aa $