对“__stack_chk_fail_local”的未定义引用

undefined reference to '__stack_chk_fail_local'

已经有人问了一个问题:undefined reference to `__stack_chk_fail' 但这个问题是对 __stack_chk_fail 而不是 __stack_chk_fail_local 的未定义引用。这是我收到的链接器错误,我已经搜索过了,但我不知道如何修复它。这是确切的错误:

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

这是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));

}

一些其他信息: 我将 gcc 编译器和 ld 链接器与我自己的链接器文件一起使用, 我正在使用一个名为 GlobalDescriptorTable 的 class 和构造函数,错误来自构造函数。

编辑:我也在关注 YouTube 教程

__stack_chk_fail_local 是 glibc 的一个函数,它基本上调用 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 ();
}

在我的安装中,此函数是 libc_nonshared.a 的一部分,后者是一个包含共享库中不存在的一些函数的库。

你可以通过阅读libc.so看到这些信息,我的可以在/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 ) )

思路是编译一个简单的测试:

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

使用命令行 gcc -o exec x.c -Wl,-Map,x.map 并查看它是否编译(您将收到警告)。如果它编译,问题可能来自你的链接器脚本 and/or 你的命令行,否则你的安装已损坏或不完整。

在第二种情况下,如果您使用 Ubuntu,您可能需要重新安装 libc6-dev。