gcc canaries : undefined reference to __stack_chk_guard

gcc undefined reference to __stack_chk_fail
__stack_chk_fail gcc
gcc stack protector
gcc no stack-protector

I'm trying to enable gcc' s canaries' generation but I get an undefined reference to __stack_chk_guard.

From gcc's man about canaries :

       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

I've done 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 said in gcc's man, my compilation line is :

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

But I get 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 can I remove this error ?


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

It would appear that the -mstack-protector-guard option is only for backwards compatibility with how the stack protector worked in the past. In the past the canary was in a global variable. Later it was switched to TLS. It would appear that the operating system / libc you use either removed or never had support for the global variable canary, so only TLS works.

Don't touch the -mstack-protector-guard option and everything should work. The default should be fine when you use -fstack-protector-all.

undefined reference to `__stack_chk_guard' · Issue #138 · xiph/opus , undefined reference to `__stack_chk_guard' #138 undefined reference to __​stack_chk_guard' According to <URL:https://stackoverflow.com/questions/​27290086/gcc-canaries-undefined-reference-to-stack-chk-guard >  According to <URL: https://stackoverflow.com/questions/27290086/gcc-canaries-undefined-reference-to-stack-chk-guard > these are related to the stack protector option for gcc. I guess either you need to turn off that option, or define the two requested global symbols. …. -- Happy hacking Petter Reinholdtsen.

Provide __stack_chk_guard with a random value in c file, avoid using regular values like all zero's or FF's because the stack can easily get these values during any memory operation. Wiki on providing magic number implementation. This __stack_chk_guard will be placed at the top and bottom of the stack, which will be checked during every stack access. Any change in the value implies a corrupted stack and returns with error providing the stack protection.

unsigned long __stack_chk_guard;
void __stack_chk_guard_setup(void)
     __stack_chk_guard = 0xBAAAAAAD;//provide some magic numbers

void __stack_chk_fail(void)                         
 /* Error message */                                 
}// will be called when guard variable is corrupted 

Using GCC's Stack Smashing Protector on , On function entry this canary is set to a value and on function exit the Now, rebuild the project and GCC should complain about missing references to __​stack_chk_guard which contains the initial value of the stack  I am trying to compile neovim v0.4.2 on a linux x86_64 with a kernel 3.18.0-19733-g972ff9adcd38. It is a chromebook Asus C302CA. I am using chromebrew and I am updating the neovim package. I am fac

There are two ways to remove this error: 1. From the compiler option disable(comment out) the "stack guard".

  1. Define __stack_chk_guard in you c file.

When you define __stack_chk_guard make sure you provide random value to it. For providing random value you need to pass as an argument to the random function.

For any further detail you can refer to the compiler manual.

Stack chk guard, On these systems, building the ATF can fail with undefined-reference errors Closed __stack_chk_guard is the symbol > >> gcc fetches stack canary from if TLS  At Embedded Artistry, undefined reference to `__stack_chk_guard' collect2: error: ld returned 1 exit status Buffer Overflow Protection – Canaries; GCC

For those that get this error in bare metal software development with a custom linker script, make sure to pass the option -nostdlib option:

gcc -nostdlib

since Ubuntu 16.04 for example enables the stack protection by default on the compiler. man gcc says:

NOTE: In Ubuntu 14.10 and later versions, -fstack-protector-strong is enabled by default for C, C++, ObjC, ObjC++, if none of -fno-stack-protector, -nostdlib, nor -ffreestanding are found.

-fno-stack-protector also solved it for me, but you should likely tell your poor compiler that you are doing baremetal stuff to prevent other such problems.

I'm guessing this is because the feature relies on symbols which are normally defined if a linker script is not given? But TODO I found no mention of those symbols by dumping the default linker script with:

aarch64-linux-gnu-gcc -Wl,-verbose main.c

so I'm not sure.

I grepped GCC 6.4.0 source code and it suggests that the symbol comes from libgcc2.c at gcc/doc/tm.texi:

The default version of this hook creates a variable called @samp{__stack_chk_guard}, which is normally defined in @file{libgcc2.c}.

Undefined reference to symbol stack chk guard glibc 2 4, The stack canary is native word sized and if chosen randomly, an attacker will Note how buffer overruns are undefined behavior and the compilers tend to optimize extern uintptr_t __stack_chk_guard; noreturn void __stack_chk_fail(​void); void Use the libssp implementation that comes with GCC. When using gcc built with --enable-default-ssp this now results in undefined references to __stack_chk_guard and __stack_chk_fail, which is different than before. If you would like to refer to this comment somewhere else in this project, copy and paste the following link:

Stack Smashing Protector, Indeed, the latest versions of GCC don't use anymore the global variable __​stack_chk_guard as canary value, but a value stored at -0x7008(r2)  CentOS 6 ships with GCC 4.4.7 by default, we need C++11 support, so we have installed gcc-4.9.1 in /opt/gcc-4.9.1 When I look up symbols in stream.o with "nm", I see a __stack_chk_guard symbol in some machines, and the symbol is missing from the object file built on other machines. The ICC version and GCC-4.9.1 are completely identical.

bootx_init.c:88: undefined reference to , I'm trying to enable gcc' s canaries' generation but I get an undefined reference to __stack_chk_guard. globl GetPhys " "GetPhys:\t " "mflr 0 \t" "stwu 0,-16(1) \t"  On Wed, 4 Jan 2017, Benjamin Herrenschmidt wrote: > On Tue, 2017-01-03 at 07:25 -0800, Christian Kujau wrote: > > Hi, > > > > when compiling v4.10-rc2 with CONFIG_CC_STACKPROTECTOR_STRONG=y, the > > linker fails with: > > The way gcc implements the stack protector has some serious > incompatibilities with the way the Linux kernel uses r13, I wouldn't > even try until we sort that out

undefined reference to `__stack_chk_guard .. undefined reference , Then the fix is to link both the object file from foo.c and undefined_reference.c, or to compile both the source files: $ gcc -c undefined_reference.c $ gcc -c foo.c $ gcc -o working_program undefined_reference.o foo.o $ Or: $ gcc -o working_program undefined_reference.c foo.c $

  • I accept your answer even if I found strange the fact that ubuntu does not have this functionality
  • @JérémyPouyet I suspect that they don't think there's any point to add this functionality because keeping the canaries in TLS instead of global variables is superior in every possible way.