Search code examples
c++linker

undefined reference to '__stack_chk_fail_local'


there is already a question asked: undefined reference to `__stack_chk_fail' but that question is undefined reference to __stack_chk_fail and not __stack_chk_fail_local. this Is a linker error i am getting and i have searched but i don't know how to fix it. This is the exact error:

ld: binaries/GlobalDescriptorTable.o: in function 
`GlobalDescriptorTable::GlobalDescriptorTable()':
GlobalDescriptorTable.cpp:(.text+0xa6): undefined reference to `__stack_chk_fail_local'

and this is (a part of)the code in GlobalDescriptorTable.cpp:

GlobalDescriptorTable::GlobalDescriptorTable()
: nullSegmentSelector(0,0,0),
unusedSegmentSelector(0,0,0),
codeSegmentSelector(0,64*1024*1024,0x9A),
dataSegmentSelector(0,64*1024*1024,0x92)
{
    uint32 i[2];
    i[0] = (uint32)this;
    i[1] = sizeof(GlobalDescriptorTable) << 16;

    //lgdt is an assembly instruction that stands for load global descriptor table
    asm volatile("lgdt (%0)": :"p" (((uint8 *) i)+2));

}

Some other information: i am using gcc compiler and ld linker with my own linker file, I am using a class called GlobalDescriptorTable with a constructor and the error is coming from the constructor.

edit: also i am following a youtube tutorial


Solution

  • __stack_chk_fail_local is a function frim glibc which basically call stack_chk_fail.

    /* On some architectures, this helps needless PIC pointer setup
       that would be needed just for the __stack_chk_fail call.  */
    
    void __attribute__ ((noreturn)) attribute_hidden
    __stack_chk_fail_local (void)
    {
      __stack_chk_fail ();
    }
    

    In my Installation, this function is part of libc_nonshared.a which is a library which contains some functions not present in the shared library.

    You can see these information by reading libc.so, mine can be found at /usr/lib/x86_64-linux-gnu/libc.so.

    /* GNU ld script
       Use the shared library, but some functions are only in
       the static library, so try that secondarily.  */
    OUTPUT_FORMAT(elf64-x86-64)
    GROUP ( /lib/x86_64-linux-gnu/libc.so.6 /usr/lib/x86_64-linux-gnu/libc_nonshared.a  AS_NEEDED ( /lib/x86_64-linux-gnu/ld-linux-x86-64.so.2 ) )
    

    The idea is to compile a simple test:

    int main()
    {
      __stack_chk_fail_local();
      return 0;
    }
    

    With the command line gcc -o exec x.c -Wl,-Map,x.map and see if it compile (you will get a warning). If it compiles , the problem is probably coming from your linker script and/or your command lines, otherwise your installation is corrupted or incomplete.

    In the second case you need probably to reinstall libc6-dev if you are using Ubuntu for example.