Gcc canaries: undefined link to __stack_chk_guard

I am trying to enable the generation of gcc 's canaries', but I am getting undefined reference to __stack_chk_guard.

From gcc man about canaries:

-mstack-protector-guard=guard
       Generate stack protection code using canary at guard.  Supported locations are global for
       global canary or tls for per-thread canary in the TLS block (the default).  This option
       has effect only when -fstack-protector or -fstack-protector-all is specified.

   These -m switches are supported in addition to the above on x86-64 processors in 64-bit
   environments.

      

I ran this test program:

#define VALUE 2048
int    main()
{
  char arr[VALUE];
  int  i;

  for (i = 0; i < VALUE + 15; i++) // "i < VALUE + 15" is to test if canaries works but the code doesn't compile anymore with "i < 10" 
    arr[i] = '0';
  return 0;
}

      

As the gcc man says, my compilation line is:

gcc main.c -fstack-protector-all -mstack-protector-guard=global

      

But I am getting the following error:

/tmp/ccXxxxVd.o: In function `main':
main.c:(.text+0xe): undefined reference to `__stack_chk_guard'
main.c:(.text+0x51): undefined reference to `__stack_chk_guard'
collect2: error: ld returned 1 exit status

      

How do I remove this error?

EDIT:

  • OS: ubuntu 14.10 utopic
  • architecture: x86-64
  • environments: 64-bit
+3


source to share


1 answer


It looks like the parameter -mstack-protector-guard

is only for backward compatibility with how the past stack protector worked. Previously, the canary was in a global variable. He later switched to TLS. It looks like the operating system / libc you are using either remote or never had a global canary, so only TLS works.



Don't touch the option -mstack-protector-guard

and everything should work. The default should be fine if you are using -fstack-protector-all

.

+2


source







All Articles