编译驱动模块时,出现“stack protector enabled but no compiler support”[解决办法]【转】

转自:http://blog.chinaunix.net/uid-26847859-id-3297170.html

原文地址:编译驱动模块时,出现“stack protector enabled but no compiler support”[解决办法] 作者:cjunsking

写驱动程序,编译驱动模块时,出现

“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中有

 

 

  1. ifdef CONFIG_CC_STACKPROTECTOR
  2. cc_has_sp := $(srctree)/scripts/gcc-x86_$(BITS)-has-stack-protector.sh
  3. ifeq ($(shell $(CONFIG_SHELL) $(cc_has_sp) $(CC) $(biarch)),y)
  4. stackp-y := -fstack-protector
  5. KBUILD_CFLAGS += $(stackp-y)
  6. else
  7. $(warning stack protector enabled but no compiler support)
  8. endif
  9. endif

 

判断编译器是否支持stack-protector的是/usr/src/linux-headers-2.6.32-5-common/scripts/gcc-x86_$(BITS)-has-stack-protector.sh文件(针对32/64位机器,有不同的文件)

 

 

点击(此处)折叠或打开

  1. #!/bin/sh
  2. echo "" | $* -S -xc -c -O0 -fstack-protector - -o - 2> /dev/null | grep -q "%gs"
  3. if [ "$?" -eq "0" ] ; then
  4. echo y
  5. else
  6. echo n
  7. 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:

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

 

  1. .file"test_stack_protector.c"
  2. .text
  3. .globl foo
  4. .typefoo, @function
  5. foo:
  6. pushl %ebp
  7. movl %esp, %ebp
  8. subl $216, %esp
  9. movl %gs:20, %eax
  10. movl %eax, -12(%ebp)
  11. xorl %eax, %eax
  12. movl $3, %eax
  13. movl -12(%ebp), %edx
  14. xorl %gs:20, %edx
  15. je .L3
  16. call __stack_chk_fail
  17. .L3:
  18. leave
  19. ret
  20. .sizefoo, .-foo
  21. .ident"GCC: (Debian 4.4.5-8) 4.4.5"
  22. .section.note.GNU-stack,"",@progbits

 

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

nostack:

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

 

    1. .file"test_stack_protector.c"
    2. .text
    3. .globl foo
    4. .typefoo, @function
    5. foo:
    6. pushl %ebp
    7. movl %esp, %ebp
    8. subl $208, %esp
    9. movl $3, %eax
    10. leave
    11. ret
    12. .sizefoo, .-foo
    13. .ident"GCC: (Debian 4.4.5-8) 4.4.5"
    14. .section.note.GNU-stack,"",@progbits
原文地址:https://www.cnblogs.com/sky-heaven/p/6215461.html