1

I'm using GCC 4.7.2 from devtoolset package on CentOS 5 (CentOS 5 because we need compatibility with old Linuxes, and GCC 4.7 because it optimizes better than 4.4).

There is one symbols that prevents my binaries from running on RHEL4: __stack_chk_fail@GLIBC_2.4. It is included only in some C++ programs and the -fno-stack-protector flag doesn't help.

Here is minimal program that reproduces the problem (but the same using stdio.h works):

#include <iostream>
int main(int argc, char *argv[]) {
    for(int i=0; i < argc; i++)
        std::cout << " " << argv[i];
    return 0;
}

When compiled with optimization (-O/-O2) it references __stack_chk_fail.

$ g++ -fno-stack-protector -O2 foo.cc
$ readelf -s a.out | grep chk
    15: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND __stack_chk_fail@GLIBC_2.4 (5)
   105: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND __stack_chk_fail@@GLIBC_2

Any idea why this symbol gets here and how to get rid of it?

Just in case, full readelf output and gcc -v is here.

Edit: This problem may be specific to Red Hat Developer Toolset 1.1. With default CentOS compiler __stack_chk_fail is not referenced.

2 Answers 2

2

This symbol comes from libstdc++_nonshared.a.

Unlike GCC from the distro, GCC from devtoolset has non-shared part of libstdc++. libstdc++.so in GCC 4.7 is a linker script that uses libstdc++ from GCC 4.1 and extra functions linked statically:

$ cat /opt/centos/devtoolset-1.1/root/usr/lib/gcc/i386-CentOS-linux/4.7.2/libstdc++.so
/* GNU ld script
   Use the shared library, but some functions are only in
   the static library, so try that secondarily.  */
OUTPUT_FORMAT(elf32-i386)
INPUT ( /usr/lib/libstdc++.so.6 -lstdc++_nonshared )

After recompiling libstdc++_nonshared.a with disabled stack protector the final program can be run on RHEL4.

0

It comes with GLibC. Build against an older GLibC.

You can tell GCC 4.7 to emit optimized assembly code and then assemble and link that on an older system

1
  • 1
    well, the symbol is defined in glibc, as can be seen from versioning @GLIBC_2.4, but why is it referenced in this simple program, regardless of -fno-stack-protector? Moving assembly around can be done for this minimal example, but the real project has thousands of files and complex build system.
    – marcin
    Sep 12, 2013 at 0:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .