天天看點

編譯驅動子產品時,出現“stack protector enabled but no compiler support”[解決辦法]【轉】

寫驅動程式,編譯驅動子產品時,出現

“make[1]: Entering directory `/usr/src/linux-headers-2.6.32-5-amd64'

/usr/src/linux-headers-2.6.32-5-common/arch/x86/Makefile:81: stack protector enabled but no compiler support” - stack protector啟用,但編譯器不支援

解決方法1: (除去棧保護支援)

1. 修改 /usr/src/linux-header-xxx/目錄下的檔案.config,找到CONFIG_CC_STACKPROTECTOR,注釋掉

2. 同樣的辦法修改/usr/src/linux-header-xxx/include/config/auto.conf

解決方法2: (保留棧保護功能)

在/usr/src/linux-headers-2.6.32-5-common/arch/x86/Makefile中有

ifdef CONFIG_CC_STACKPROTECTOR

cc_has_sp := $(srctree)/scripts/gcc-x86_$(BITS)-has-stack-protector.sh

ifeq ($(shell $(CONFIG_SHELL) $(cc_has_sp) $(CC) $(biarch)),y)

stackp-y := -fstack-protector

KBUILD_CFLAGS += $(stackp-y)

else

$(warning stack protector enabled but no compiler support)

endif

判斷編譯器是否支援stack-protector的是/usr/src/linux-headers-2.6.32-5-common/scripts/gcc-x86_$(BITS)-has-stack-protector.sh檔案(針對32/64位機器,有不同的檔案)

點選(此處)折疊或打開

#!/bin/sh

echo "" | $* -S -xc -c -O0 -fstack-protector - -o - 2> /dev/null | grep -q "%gs"

if [ "$?" -eq "0" ] ; then

echo y

echo n

fi

這個檔案中判斷gcc是否支援fstack-protector的方法是,檢視""生成的支援棧保護的彙編碼中是否含有"%gs"。大家可以通過實驗來觀察差別,而這個檔案中的判斷與實際的相反。故将這兩個檔案中的y和n互換位置即可。

實驗:  Debian6.0.5/Linux 2.6.32-5-amd64/gcc 4.4.5

源代碼: (test_stack_protector.c)

    int foo(void) { char X[200]; return 3; }

編譯結果:

(1)  gcc -S -fstack-protector -o stack test_stack_protector.c

stack:

------------------------------------------------------------

.file"test_stack_protector.c"

.text

.globl foo

.typefoo, @function

foo:

pushl %ebp

movl %esp, %ebp

subl $216, %esp

movl %gs:20, %eax

movl %eax, -12(%ebp)

xorl %eax, %eax

movl $3, %eax

movl -12(%ebp), %edx

xorl %gs:20, %edx

je .L3

call __stack_chk_fail

.L3:

leave

ret

.sizefoo, .-foo

.ident"GCC: (Debian 4.4.5-8) 4.4.5"

.section.note.GNU-stack,"",@progbits

(2)   gcc -S -fno-stack-protector -o nostack test_stack_protector.c 

nostack:

subl $208, %esp

【新浪微網誌】 張昺華--sky

【twitter】 @sky2030_

【facebook】 張昺華 zhangbinghua

本文版權歸作者和部落格園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接配接,否則保留追究法律責任的權利.

繼續閱讀