例如 Linux/i386 上的 glibc 将 cookie 存储在
%gs:0x14
。除了 __stack_chk_guard
符号之外,我还需要在其他平台上寻找 cookie 吗?
(这是
gcc -fstack-protector
生成的代码在函数序言中将值存储到堆栈上的位置,并在返回之前进行检查以防止堆栈崩溃)。
从 gcc 源定义 TARGET_THREAD_SSP_OFFSET
grep -B1
(或使用 google codesearch 在线执行此 grep http://www.google.com/codesearch?q=TARGET_THREAD_SSP_OFFSET&exact_package=http%3A%2F%2Fmosync.googlecode.com% 2Fsvn&hl=zh )
gcc4/trunk/gcc-4.4.3/gcc/config/sparc/linux.h
168: /* sparc glibc provides __stack_chk_guard in [%g7 + 0x14]. */
169: #define TARGET_THREAD_SSP_OFFSET 0x14
gcc4/trunk/gcc-4.4.3/gcc/config/sparc/linux64.h
302: sparc64 glibc provides it at [%g7 + 0x28]. */
303: #define TARGET_THREAD_SSP_OFFSET (TARGET_ARCH64 ? 0x28 : 0x14)
gcc4/trunk/gcc-4.4.3/gcc/config/s390/linux.h
98: s390x glibc provides it at 0x28(tp). */
99: #define TARGET_THREAD_SSP_OFFSET (TARGET_64BIT ? 0x28 : 0x14)
gcc4/trunk/gcc-4.4.3/gcc/config/i386/linux.h
214: /* i386 glibc provides __stack_chk_guard in %gs:0x14. */
215: #define TARGET_THREAD_SSP_OFFSET 0x14
gcc4/trunk/gcc-4.4.3/gcc/config/rs6000/linux.h
121: /* ppc32 glibc provides __stack_chk_guard in -0x7008(2). */
122: #define TARGET_THREAD_SSP_OFFSET -0x7008
gcc4/trunk/gcc-4.4.3/gcc/config/rs6000/linux64.h
525: ppc64 glibc provides it at -0x7010(13). */
526: #define TARGET_THREAD_SSP_OFFSET (TARGET_64BIT ? -0x7010 : -0x7008)
gcc4/trunk/gcc-4.4.3/gcc/config/i386/linux64.h
118: x86_64 glibc provides it in %fs:0x28. */
119: #define TARGET_THREAD_SSP_OFFSET (TARGET_64BIT ? 0x28 : 0x14)
#ifdef __i386__
# define STACK_CHK_GUARD \
({ uintptr_t x; asm ("movl %%gs:0x14, %0" : "=r" (x)); x; })
#elif defined __x86_64__
# define STACK_CHK_GUARD \
({ uintptr_t x; asm ("movq %%fs:0x28, %0" : "=r" (x)); x; })
#elif defined __powerpc64__
# define STACK_CHK_GUARD \
({ uintptr_t x; asm ("ld %0,-28688(13)" : "=r" (x)); x; })
#elif defined __powerpc__
# define STACK_CHK_GUARD \
({ uintptr_t x; asm ("lwz %0,-28680(2)" : "=r" (x)); x; })
#elif defined __sparc__ && defined __arch64__
# define STACK_CHK_GUARD \
({ uintptr_t x; asm ("ldx [%%g7+0x28], %0" : "=r" (x)); x; })
#elif defined __sparc__
# define STACK_CHK_GUARD \
({ uintptr_t x; asm ("ld [%%g7+0x14], %0" : "=r" (x)); x; })
#elif defined __s390x__
# define STACK_CHK_GUARD \
({ uintptr_t x; asm ("ear %0,%%a0; sllg %0,%0,32; ear %0,%%a1; lg %0,0x28(%0)" : "=a" (x)); x; })
#elif defined __s390__
# define STACK_CHK_GUARD \
({ uintptr_t x; asm ("ear %0,%%a0; l %0,0x14(%0)" : "=a" (x)); x; })
#elif defined __ia64__
# define STACK_CHK_GUARD \
({ uintptr_t x; asm ("adds %0 = -8, r13;; ld8 %0 = [%0]" : "=r" (x)); x; })
#else
extern uintptr_t __stack_chk_guard;
# define STACK_CHK_GUARD __stack_chk_guard
#endif
所以,似乎 gcc 和 glibc 总是对主要平台使用相同的位置,可以通过 STACK_CHK_GUARD 宏访问