2016年12月30日 星期五

c++ exception handling 的實作 (2) - 使用 g++ 5.4.0

binary hacks 繁體中文版 item 38, 39, 40, 41 是用 gcc 3.4.4 講解, 用目前的 gcc 5.4.0 (20161231) 編譯的執行檔會直接 segmentfault, 無法在目前的系統上執行有點可惜, 我希望這段程式碼可以使用 gcc 5.4.0 編譯測試, 來試試看吧!

你可能有興趣:
c++ exception handling (1) - 原理篇

挑戰自己, 先來編譯 gcc 5.4.0, glibc 2.23。

env:
x64 debian 64 bit

系統上的 glibc 是使用 dwarf 的方式處理 exception handle 編譯出來的, 要以 static link 編譯 a.cpp 就出了問題, 得自己編譯以 sjlj 處理 exception handle 的 glibc。

dwarf 呼叫的是 _Unwind_RaiseException, sjlj 的版本呼叫的則是 _Unwind_SjLj_RaiseException, symbol 不一樣。

編譯 gcc 5.4.0
../gcc-5.4.0/configure --enable-languages=c,c++ --enable-sjlj-exceptions  --disable-nls
make
make install

編譯 glibc 2.23
../glibc-2.23/configure --disable-nls --disable-sanity-checks
make
make install

編譯 glibc 時會遇到 ldconfig, sln 無法使用 static 編譯的問題, 我用 touch 建立這 2 個檔案, 還有 libgcc.so 找不到的問題, 我修改了路徑以及加入 /usr/local/lib/gcc/x86_64-unknown-linux-gnu/5.4.0/libgcc.a, 成功編譯出呼叫 sjlj 系列函式的 glibc。

修改 libgcc 的路徑, 補上紅色那段, 移出原本的 -lgcc
 1 
 3 gcc -nostdlib -nostartfiles /usr/local/lib64/libgcc_s.so -o /media/descent/usbhd/glibc-build/debug/pcprofiledump    -Wl,-z,combreloc -Wl,-z,relro -Wl,--hash-style=both /media/descent/usbhd/glibc-build/csu/crt1.o /media/descent/usbhd/glibc-build/csu/crti.o `gcc  --print-file-name=crtbegin.o` /media/descent/usbhd/glibc-build/debug/pcprofiledump.o  -Wl,-dynamic-linker=/usr/local/lib/ld-linux-x86-64.so.2 -Wl,-rpath-link=/media/descent/usbhd/glibc-build:/media/descent/usbhd/glibc-build/math:/media/descent/usbhd/glibc-build/elf:/media/descent/usbhd/glibc-build/dlfcn:/media/descent/usbhd/glibc-build/nss:/media/descent/usbhd/glibc-build/nis:/media/descent/usbhd/glibc-build/rt:/media/descent/usbhd/glibc-build/resolv:/media/descent/usbhd/glibc-build/crypt:/media/descent/usbhd/glibc-build/mathvec:/media/descent/usbhd/glibc-build/nptl /media/descent/usbhd/glibc-build/libc.so.6 /media/descent/usbhd/glibc-build/libc_nonshared.a -Wl,--as-needed /media/descent/usbhd/glibc-build/elf/ld.so -Wl,--no-as-needed  `gcc  --print-file-name=crtend.o` /media/descent/usbhd/glibc-build/csu/crtn.o
 4 
 5 gcc -nostdlib -nostartfiles /usr/local/lib64/libgcc_s.so -o /media/descent/usbhd/glibc-build/login/utmpdump    -Wl,-z,combreloc -Wl,-z,relro -Wl,--hash-style=both /media/descent/usbhd/glibc-build/csu/crt1.o /media/descent/usbhd/glibc-build/csu/crti.o `gcc  --print-file-name=crtbegin.o` /media/descent/usbhd/glibc-build/login/utmpdump.o  -Wl,-dynamic-linker=/usr/local/lib/ld-linux-x86-64.so.2 -Wl,-rpath-link=/media/descent/usbhd/glibc-build:/media/descent/usbhd/glibc-build/math:/media/descent/usbhd/glibc-build/elf:/media/descent/usbhd/glibc-build/dlfcn:/media/descent/usbhd/glibc-build/nss:/media/descent/usbhd/glibc-build/nis:/media/descent/usbhd/glibc-build/rt:/media/descent/usbhd/glibc-build/resolv:/media/descent/usbhd/glibc-build/crypt:/media/descent/usbhd/glibc-build/mathvec:/media/descent/usbhd/glibc-build/nptl /media/descent/usbhd/glibc-build/libc.so.6 /media/descent/usbhd/glibc-build/libc_nonshared.a -Wl,--as-needed /media/descent/usbhd/glibc-build/elf/ld.so -Wl,--no-as-needed `gcc  --print-file-name=crtend.o` /media/descent/usbhd/glibc-build/csu/crtn.o
 6 
 7 gcc -nostdlib -nostartfiles /usr/local/lib64/libgcc_s.so -o /media/descent/usbhd/glibc-build/elf/sprof    -Wl,-z,combreloc -Wl,-z,relro -Wl,--hash-style=both /media/descent/usbhd/glibc-build/csu/crt1.o /media/descent/usbhd/glibc-build/csu/crti.o `gcc  --print-file-name=crtbegin.o` /media/descent/usbhd/glibc-build/elf/sprof.o /media/descent/usbhd/glibc-build/dlfcn/libdl.so.2  -Wl,-dynamic-linker=/usr/local/lib/ld-linux-x86-64.so.2 -Wl,-rpath-link=/media/descent/usbhd/glibc-build:/media/descent/usbhd/glibc-build/math:/media/descent/usbhd/glibc-build/elf:/media/descent/usbhd/glibc-build/dlfcn:/media/descent/usbhd/glibc-build/nss:/media/descent/usbhd/glibc-build/nis:/media/descent/usbhd/glibc-build/rt:/media/descent/usbhd/glibc-build/resolv:/media/descent/usbhd/glibc-build/crypt:/media/descent/usbhd/glibc-build/mathvec:/media/descent/usbhd/glibc-build/nptl /media/descent/usbhd/glibc-build/libc.so.6 /media/descent/usbhd/glibc-build/libc_nonshared.a -Wl,--as-needed /media/descent/usbhd/glibc-build/elf/ld.so -Wl,--no-as-needed `gcc  --print-file-name=crtend.o` /media/descent/usbhd/glibc-build/csu/crtn.o
 8 
 9 
10 gcc -nostdlib -nostartfiles /usr/local/lib64/libgcc_s.so -o /media/descent/usbhd/glibc-build/elf/pldd    -Wl,-z,combreloc -Wl,-z,relro -Wl,--hash-style=both /media/descent/usbhd/glibc-build/csu/crt1.o /media/descent/usbhd/glibc-build/csu/crti.o `gcc  --print-file-name=crtbegin.o` /media/descent/usbhd/glibc-build/elf/pldd.o /media/descent/usbhd/glibc-build/elf/xmalloc.o  -Wl,-dynamic-linker=/usr/local/lib/ld-linux-x86-64.so.2 -Wl,-rpath-link=/media/descent/usbhd/glibc-build:/media/descent/usbhd/glibc-build/math:/media/descent/usbhd/glibc-build/elf:/media/descent/usbhd/glibc-build/dlfcn:/media/descent/usbhd/glibc-build/nss:/media/descent/usbhd/glibc-build/nis:/media/descent/usbhd/glibc-build/rt:/media/descent/usbhd/glibc-build/resolv:/media/descent/usbhd/glibc-build/crypt:/media/descent/usbhd/glibc-build/mathvec:/media/descent/usbhd/glibc-build/nptl /media/descent/usbhd/glibc-build/libc.so.6 /media/descent/usbhd/glibc-build/libc_nonshared.a -Wl,--as-needed /media/descent/usbhd/glibc-build/elf/ld.so -Wl,--no-as-needed `gcc  --print-file-name=crtend.o` /media/descent/usbhd/glibc-build/csu/crtn.o
11 #!/bin/sh
12 gcc   -shared -static-libgcc -Wl,-O1  -Wl,-z,defs -Wl,-dynamic-linker=/usr/local/lib/ld-linux-x86-64.so.2  -B/media/descent/usbhd/glibc-build/csu/  -Wl,--version-script=/media/descent/usbhd/glibc-build/libc.map -Wl,-soname=libc.so.6 -Wl,-z,combreloc -Wl,-z,relro -Wl,--hash-style=both -nostdlib -nostartfiles -e __libc_main -L/media/descent/usbhd/glibc-build -L/media/descent/usbhd/glibc-build/math -L/media/descent/usbhd/glibc-build/elf -L/media/descent/usbhd/glibc-build/dlfcn -L/media/descent/usbhd/glibc-build/nss -L/media/descent/usbhd/glibc-build/nis -L/media/descent/usbhd/glibc-build/rt -L/media/descent/usbhd/glibc-build/resolv -L/media/descent/usbhd/glibc-build/crypt -L/media/descent/usbhd/glibc-build/mathvec -L/media/descent/usbhd/glibc-build/nptl -Wl,-rpath-link=/media/descent/usbhd/glibc-build:/media/descent/usbhd/glibc-build/math:/media/descent/usbhd/glibc-build/elf:/media/descent/usbhd/glibc-build/dlfcn:/media/descent/usbhd/glibc-build/nss:/media/descent/usbhd/glibc-build/nis:/media/descent/usbhd/glibc-build/rt:/media/descent/usbhd/glibc-build/resolv:/media/descent/usbhd/glibc-build/crypt:/media/descent/usbhd/glibc-build/mathvec:/media/descent/usbhd/glibc-build/nptl /usr/local/lib64/libgcc_s.so -o /media/descent/usbhd/glibc-build/libc.so -T /media/descent/usbhd/glibc-build/shlib.lds /media/descent/usbhd/glibc-build/csu/abi-note.o /media/descent/usbhd/glibc-build/elf/soinit.os /media/descent/usbhd/glibc-build/libc_pic.os /media/descent/usbhd/glibc-build/elf/sofini.os /media/descent/usbhd/glibc-build/elf/interp.os /media/descent/usbhd/glibc-build/elf/ld.so 
13 
14 #!/bin/sh
15 gcc   -shared -static-libgcc -Wl,-O1  -Wl,-z,defs -Wl,-dynamic-linker=/usr/local/lib/ld-linux-x86-64.so.2  -B/media/descent/usbhd/glibc-build/csu/  -Wl,--version-script=/media/descent/usbhd/glibc-build/libc.map -Wl,-soname=libc.so.6 -Wl,-z,combreloc -Wl,-z,relro -Wl,--hash-style=both -nostdlib -nostartfiles -e __libc_main -L/media/descent/usbhd/glibc-build -L/media/descent/usbhd/glibc-build/math -L/media/descent/usbhd/glibc-build/elf -L/media/descent/usbhd/glibc-build/dlfcn -L/media/descent/usbhd/glibc-build/nss -L/media/descent/usbhd/glibc-build/nis -L/media/descent/usbhd/glibc-build/rt -L/media/descent/usbhd/glibc-build/resolv -L/media/descent/usbhd/glibc-build/crypt -L/media/descent/usbhd/glibc-build/mathvec -L/media/descent/usbhd/glibc-build/nptl -Wl,-rpath-link=/media/descent/usbhd/glibc-build:/media/descent/usbhd/glibc-build/math:/media/descent/usbhd/glibc-build/elf:/media/descent/usbhd/glibc-build/dlfcn:/media/descent/usbhd/glibc-build/nss:/media/descent/usbhd/glibc-build/nis:/media/descent/usbhd/glibc-build/rt:/media/descent/usbhd/glibc-build/resolv:/media/descent/usbhd/glibc-build/crypt:/media/descent/usbhd/glibc-build/mathvec:/media/descent/usbhd/glibc-build/nptl /usr/local/lib64/libgcc_s.so -o /media/descent/usbhd/glibc-build/linkobj/libc.so -T /media/descent/usbhd/glibc-build/shlib.lds /media/descent/usbhd/glibc-build/csu/abi-note.o /media/descent/usbhd/glibc-build/elf/soinit.os -Wl,--whole-archive /media/descent/usbhd/glibc-build/linkobj/libc_pic.a -Wl,--no-whole-archive /media/descent/usbhd/glibc-build/elf/sofini.os /media/descent/usbhd/glibc-build/elf/interp.os /media/descent/usbhd/glibc-build/elf/ld.so 
16 
17 gcc -nostdlib -nostartfiles /usr/local/lib64/libgcc_s.so -o /media/descent/usbhd/glibc-build/iconv/iconvconfig    -Wl,-z,combreloc -Wl,-z,relro -Wl,--hash-style=both /media/descent/usbhd/glibc-build/csu/crt1.o /media/descent/usbhd/glibc-build/csu/crti.o `gcc  --print-file-name=crtbegin.o` /media/descent/usbhd/glibc-build/iconv/iconvconfig.o /media/descent/usbhd/glibc-build/iconv/strtab.o /media/descent/usbhd/glibc-build/iconv/xmalloc.o /media/descent/usbhd/glibc-build/iconv/hash-string.o  -Wl,-dynamic-linker=/usr/local/lib/ld-linux-x86-64.so.2 -Wl,-rpath-link=/media/descent/usbhd/glibc-build:/media/descent/usbhd/glibc-build/math:/media/descent/usbhd/glibc-build/elf:/media/descent/usbhd/glibc-build/dlfcn:/media/descent/usbhd/glibc-build/nss:/media/descent/usbhd/glibc-build/nis:/media/descent/usbhd/glibc-build/rt:/media/descent/usbhd/glibc-build/resolv:/media/descent/usbhd/glibc-build/crypt:/media/descent/usbhd/glibc-build/mathvec:/media/descent/usbhd/glibc-build/nptl /media/descent/usbhd/glibc-build/libc.so.6 /media/descent/usbhd/glibc-build/libc_nonshared.a -Wl,--as-needed /media/descent/usbhd/glibc-build/elf/ld.so -Wl,--no-as-needed  `gcc  --print-file-name=crtend.o` /media/descent/usbhd/glibc-build/csu/crtn.o
18 
19 gcc -nostdlib -nostartfiles /usr/local/lib64/libgcc_s.so -o /media/descent/usbhd/glibc-build/iconv/iconv_prog    -Wl,-z,combreloc -Wl,-z,relro -Wl,--hash-style=both /media/descent/usbhd/glibc-build/csu/crt1.o /media/descent/usbhd/glibc-build/csu/crti.o `gcc  --print-file-name=crtbegin.o` /media/descent/usbhd/glibc-build/iconv/iconv_prog.o /media/descent/usbhd/glibc-build/iconv/iconv_charmap.o /media/descent/usbhd/glibc-build/iconv/charmap.o /media/descent/usbhd/glibc-build/iconv/charmap-dir.o /media/descent/usbhd/glibc-build/iconv/linereader.o /media/descent/usbhd/glibc-build/iconv/dummy-repertoire.o /media/descent/usbhd/glibc-build/iconv/simple-hash.o /media/descent/usbhd/glibc-build/iconv/xstrdup.o /media/descent/usbhd/glibc-build/iconv/xmalloc.o  -Wl,-dynamic-linker=/usr/local/lib/ld-linux-x86-64.so.2 -Wl,-rpath-link=/media/descent/usbhd/glibc-build:/media/descent/usbhd/glibc-build/math:/media/descent/usbhd/glibc-build/elf:/media/descent/usbhd/glibc-build/dlfcn:/media/descent/usbhd/glibc-build/nss:/media/descent/usbhd/glibc-build/nis:/media/descent/usbhd/glibc-build/rt:/media/descent/usbhd/glibc-build/resolv:/media/descent/usbhd/glibc-build/crypt:/media/descent/usbhd/glibc-build/mathvec:/media/descent/usbhd/glibc-build/nptl /media/descent/usbhd/glibc-build/libc.so.6 /media/descent/usbhd/glibc-build/libc_nonshared.a -Wl,--as-needed /media/descent/usbhd/glibc-build/elf/ld.so -Wl,--no-as-needed `gcc  --print-file-name=crtend.o` /media/descent/usbhd/glibc-build/csu/crtn.o
20 
21 gcc -nostdlib -nostartfiles -o /media/descent/usbhd/glibc-build/locale/localedef    -Wl,-z,combreloc -Wl,-z,relro -Wl,--hash-style=both /media/descent/usbhd/glibc-build/csu/crt1.o /media/descent/usbhd/glibc-build/csu/crti.o `gcc  --print-file-name=crtbegin.o` /media/descent/usbhd/glibc-build/locale/localedef.o /media/descent/usbhd/glibc-build/locale/ld-ctype.o /media/descent/usbhd/glibc-build/locale/ld-messages.o /media/descent/usbhd/glibc-build/locale/ld-monetary.o /media/descent/usbhd/glibc-build/locale/ld-numeric.o /media/descent/usbhd/glibc-build/locale/ld-time.o /media/descent/usbhd/glibc-build/locale/ld-paper.o /media/descent/usbhd/glibc-build/locale/ld-name.o /media/descent/usbhd/glibc-build/locale/ld-address.o /media/descent/usbhd/glibc-build/locale/ld-telephone.o /media/descent/usbhd/glibc-build/locale/ld-measurement.o /media/descent/usbhd/glibc-build/locale/ld-identification.o /media/descent/usbhd/glibc-build/locale/ld-collate.o /media/descent/usbhd/glibc-build/locale/charmap.o /media/descent/usbhd/glibc-build/locale/linereader.o /media/descent/usbhd/glibc-build/locale/locfile.o /media/descent/usbhd/glibc-build/locale/repertoire.o /media/descent/usbhd/glibc-build/locale/locarchive.o /media/descent/usbhd/glibc-build/locale/md5.o /media/descent/usbhd/glibc-build/locale/charmap-dir.o /media/descent/usbhd/glibc-build/locale/simple-hash.o /media/descent/usbhd/glibc-build/locale/xmalloc.o /media/descent/usbhd/glibc-build/locale/xstrdup.o  -Wl,-dynamic-linker=/usr/local/lib/ld-linux-x86-64.so.2 -Wl,-rpath-link=/media/descent/usbhd/glibc-build:/media/descent/usbhd/glibc-build/math:/media/descent/usbhd/glibc-build/elf:/media/descent/usbhd/glibc-build/dlfcn:/media/descent/usbhd/glibc-build/nss:/media/descent/usbhd/glibc-build/nis:/media/descent/usbhd/glibc-build/rt:/media/descent/usbhd/glibc-build/resolv:/media/descent/usbhd/glibc-build/crypt:/media/descent/usbhd/glibc-build/mathvec:/media/descent/usbhd/glibc-build/nptl /media/descent/usbhd/glibc-build/libc.so.6 /media/descent/usbhd/glibc-build/libc_nonshared.a -Wl,--as-needed /media/descent/usbhd/glibc-build/elf/ld.so -Wl,--no-as-needed -lgcc  `gcc  --print-file-name=crtend.o` /media/descent/usbhd/glibc-build/csu/crtn.o

某些檔案編不起來, 直接 touch 建立這些檔案
touch /media/descent/usbhd/glibc-build/locale/localedef
touch /media/descent/usbhd/glibc-build/locale/locale
touch /media/descent/usbhd/glibc-build/catgets/gencat
touch /media/descent/usbhd/glibc-build/timezone/zic
touch /media/descent/usbhd/glibc-build/timezone/zdump
touch /media/descent/usbhd/glibc-build/posix/getconf
touch /media/descent/usbhd/glibc-build/io/pwd

touch /media/descent/usbhd/glibc-build/nss/getent
touch /media/descent/usbhd/glibc-build/nss/makedb
touch /media/descent/usbhd/glibc-build/sunrpc/rpcgen
touch /media/descent/usbhd/glibc-build/nscd/nscd
touch /media/descent/usbhd/glibc-build/elf/sln
touch /media/descent/usbhd/glibc-build/elf/ldconfig

sln, ldconfig 在 make install 時
sln, ldconfig 需要修改執行權限
chmod 755 sln
chmod 755 ldconfig

其內容改成
cat ldconfig
#!/bin/sh
exit 0

cat sln
#!/bin/sh
exit 0

以 dynamic link 編譯 a.cpp 時, 該注意的地方
dynamic link libgcc 時
使用 ldd 查看 so
descent@deb:eh_impl$ /usr/bin/ldd a
linux-vdso.so.1 (0x00007ffe1b261000)
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f549386f000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f549356b000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f5493354000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f5492fb6000)
/lib64/ld-linux-x86-64.so.2 (0x00005636522ad000)

/lib/x86_64-linux-gnu/libgcc_s.so.1 沒有執行我們編譯的那個,

export LD_LIBRARY_PATH=/usr/local/lib64/ # x64 64 bit environment
export LD_LIBRARY_PATH=/usr/local/lib/ # x86 32 bit environment

再一次 ldd
descent@deb:eh_impl$ /usr/bin/ldd a
linux-vdso.so.1 (0x00007fff905b2000)
libstdc++.so.6 => /usr/local/lib64/libstdc++.so.6 (0x00007f463eb5e000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f463e843000)
libgcc_s.so.1 => /usr/local/lib64/libgcc_s.so.1 (0x00007f463e631000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f463e293000)
/lib64/ld-linux-x86-64.so.2 (0x000055bb0e8f5000)

注意 libgcc_s.so.1 path。

static link a.cpp 需要重新編譯 glibc, 所以才編譯了 glibc-2.23, 為什麼需要使用 static link 呢? 因為使用 gdb 時, libgcc.so 的行號對應似乎有問題, 所以才想要使用 static link, 不過由於有 2 套 glibc, 我不知道怎麼讓 gcc 使用我自己編的那個, 就有了很蠢 d.sh 那個方法, -lunwind 也可以拿掉。

而和 gcc 3.4.4 搭配的 glibc 是 2.3.5, 2.23 無法使用 gcc 3.4.4 編譯, 在目前的系統上因為有些工具太新而不能編譯, 例如: sed, awk, make, binutility ...

d.sh for static link
1 #!/bin/sh
2 /usr/local/libexec/gcc/x86_64-unknown-linux-gnu/5.4.0/collect2 -plugin /usr/local/libexec/gcc/x86_64-unknown-linux-gnu/5.4.0/liblto_plugin.so -plugin-opt=/usr/local/libexec/gcc/x86_64-unknown-linux-gnu/5.4.0/lto-wrapper -plugin-opt=-fresolution=/tmp/ccnKcDbO.res -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_eh -plugin-opt=-pass-through=-lc -m elf_x86_64 -static -o a /usr/local/lib/crt1.o /usr/local/lib/crti.o /usr/local/lib/gcc/x86_64-unknown-linux-gnu/5.4.0/crtbeginT.o -L/usr/local/lib/gcc/x86_64-unknown-linux-gnu/5.4.0 -L/usr/local/lib/gcc/x86_64-unknown-linux-gnu/5.4.0/../../../../lib64 -L/lib/x86_64-linux-gnu -L/lib/../lib64 -L/usr/local/lib -L/usr/local/lib/gcc/x86_64-unknown-linux-gnu/5.4.0/../../.. a.o -lstdc++ -lm --start-group -lunwind -lgcc -lgcc_eh -lc --end-group /usr/local/lib/gcc/x86_64-unknown-linux-gnu/5.4.0/crtend.o /usr/local/lib/crtn.o

一開始沒有修改 lsda, 執行下去馬上就 segmentfault, 所以該啟動反組譯工程了。

有了自行編譯的 glibc 之後, 就可以使用 gdb debug 這個 static link 的檔案, 方便理解整個來龍去脈, 遺憾的就是 lsda 裡頭的格式我還是不了解, 但我還是可以把 lsda 的格式填對, 怎麼做呢?

list 1 是 .gcc_except_table, 就是那個 lsda, 可是 L7 不是一個值, 而是一個需要計算才知道的值, 我要怎麼填呢?

list 1. g++ -S a.cpp
 1 .LFE1042:
 2         .section        .gcc_except_table
 3         .align 4
 4 .LLSDA1042:
 5         .byte   0xff
 6         .byte   0x3
 7         .uleb128 .LLSDATT1042-.LLSDATTD1042
 8 .LLSDATTD1042:
 9         .byte   0x1
10         .uleb128 .LLSDACSE1042-.LLSDACSB1042
11 .LLSDACSB1042:
12         .uleb128 0
13         .uleb128 0x1
14         .uleb128 0x1
15         .uleb128 0
16 .LLSDACSE1042:
17         .byte   0x1
18         .byte   0
19         .align 4
20         .long   _ZTIi
21 .LLSDATT1042:
22         .text

靠 objdump -D a, list 2 L1329 開始的 ff, 03, 0x0d, 0x01, 4, 0, 1, 1, 0, 1, 0 不就是答案了嗎?

list 2. objdump -D a
1322 Disassembly of section .gcc_except_table:
1323
1324 00000000004010e4 <.gcc_except_table>:
1325   4010e4:   ff                      (bad)
1326   4010e5:   ff 01                   incl   (%rcx)
1327   4010e7:   02 00                   add    (%rax),%al
1328   4010e9:   00 00                   add    %al,(%rax)
1329   4010eb:   00 ff                   add    %bh,%bh
1330   4010ed:   03 0d 01 04 00 01       add    0x1000401(%rip),%ecx        # 14014f4 <_end+0xdff30c>
1331   4010f3:   01 00                   add    %eax,(%rax)
1332   4010f5:   01 00                   add    %eax,(%rax)
1333   4010f7:   00 d0                   add    %dl,%al
1334   4010f9:   21 60 00                and    %esp,0x0(%rax)
1335
1336 Disassembly of section .init_array:

把這串神秘的數字換掉 binary hack 的範例程式, 就可以使用 g++ 5.4.0 來編譯這個程式, 並且成功的執行 exception handle, 爽阿!

完整 source code 請參考 https://github.com/descent/eh_impl, 支援 gcc 3.4.4 以及 gcc 5.4.0。

沒有留言:

張貼留言

使用 google 的 reCAPTCHA 驗證碼, 總算可以輕鬆留言了。

我實在受不了 spam 了, 又不想讓大家的眼睛花掉, 只好放棄匿名留言。這是沒辦法中的辦法了。留言的朋友需要有 google 帳號。