Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

undefined reference to `__stack_chk_fail'

Getting this error while compiling C++ code:

undefined reference to `__stack_chk_fail'

Options already tried:

  1. added -fno-stack-protector while compiling - did not work, error persists
  2. added a dummy implementation of void __stack_chk_fail(void) in my code. Still getting the same error.

Detailed Error:

/u/ac/alanger/gurobi/gurobi400/linux64/lib/libgurobi_c++.a(Env.o)(.text+0x1034): In function `GRBEnv::getPar/u/ac/alanger/gurobi/gurobi400/linux64/lib/libgurobi_c++.a(Env.o)(.text+0x1034): In function `GRBEnv::getParamInfo(GRB_StringParam, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)':
: undefined reference to `__stack_chk_fail'
amInfo(GRB_StringParam, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)':
: **undefined reference to `__stack_chk_fail'**

Earlier, I was getting 10's of such errors. Found out that there was a version mismatch between the gcc of the pre-compiled libraries I am using and the gcc version I was using to compile the code. Updated gcc and now I am getting only 2 of these errors.

Any help, please?

like image 524
Akhil Avatar asked Dec 20 '10 18:12

Akhil


People also ask

What is __ Stack_chk_fail?

__stack_chk_fail , a callback function that is invoked when a stack buffer overflow is detected. This function shall abort the function that called it with a message that a stack buffer overflow has been detected, and then halt the program via exit , abort , or a custom panic handler.

What does FNO stack protector do?

-fno-stack-protector disables stack protection. -fstack-protector enables stack protection for vulnerable functions that contain: A character array larger than 8 bytes. An 8-bit integer array larger than 8 bytes.


3 Answers

libgurobi_c++.a was compiled with -fno-stack-protector (obviously).

A few things come to mind:

  1. add -fstack-protector when linking. This will make sure that libssp gets linked.
  2. Manually link -lssp
  3. Make your dummy version of __stack_chk_fail(void) in it's own object file and and add this .o file to your linker command AFTER libgurobi_c++.a. GCC/G++ resolves symbols from left to right during linking so despite your code having the function defined, a copy of an object containing the __stack_chk_fail symbol needs to be on the linker line to the right of libgurobi_c++.a.
like image 108
gravitron Avatar answered Oct 10 '22 06:10

gravitron


In gentoo I had the same problem and i resolved creating 2 files. The first contain the option to be parsed by emerge and passed to gcc:

/etc/portage/env/nostackprotector.conf CFLAGS="-fno-stack-protector -O2" 

And the second tells which package should use this settings:

/etc/portage/package.env/nostackprotector x11-libs/vte nostackprotector.conf sys-libs/glibc nostackprotector.conf www-client/chromium nostackprotector.conf app-admin/sudo nostackprotector.conf 
like image 32
Alex Avatar answered Oct 10 '22 05:10

Alex


Just had the same issue: c++ code with an implementation of void __stack_chk_fail(void) showing several undefined reference to __stack_chk_fail errors when compiling.

My solution was to define __stack_chk_fail(void) as extern "C":

extern "C" {
__stack_chk_fail(void)
{
...
}
}

This suppressed the compilation error :)

Hope it helps!

like image 45
OneTwoSteph Avatar answered Oct 10 '22 07:10

OneTwoSteph