编译驱动模块时,出现“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
  • 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
  • else
  • 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:

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

     

  • .file"test_stack_protector.c"
  • .text
  • .globl foo
  • .typefoo, @function
  • foo:
  • pushl %ebp
  • movl %esp, %ebp
  • subl $208, %esp
  • movl $3, %eax
  • leave
  • ret
  • .sizefoo, .-foo
  • .ident"GCC: (Debian 4.4.5-8) 4.4.5"
  • .section.note.GNU-stack,"",@progbits