【问题标题】:musl-gcc: undefined reference to __memcpy_chkmusl-gcc:对 __memcpy_chk 的未定义引用
【发布时间】:2019-08-10 06:53:07
【问题描述】:

我需要针对 musl-libc 编译一个 C 程序以使其在嵌入式设备上运行。但是,我无法编译该程序。源代码取决于我传递给链接器的几个库,如下所示:

/usr/local/musl/bin/musl-gcc app.c -o app -I../lib -lzlog -lfilter

这是我得到的输出:

/usr/local/musl/lib/libzlog.a(category.o): In function `strcpy':
/usr/include/x86_64-linux-gnu/bits/string3.h:110: undefined reference to `__memcpy_chk'
/usr/local/musl/lib/libzlog.a(conf.o): In function `strcpy':
/usr/include/x86_64-linux-gnu/bits/string3.h:110: undefined reference to `__strcpy_chk'
/usr/include/x86_64-linux-gnu/bits/string3.h:110: undefined reference to `__strcpy_chk'
/usr/include/x86_64-linux-gnu/bits/string3.h:110: undefined reference to `__strcpy_chk'
/usr/include/x86_64-linux-gnu/bits/string3.h:110: undefined reference to `__strcpy_chk'
/usr/local/musl/lib/libzlog.a(event.o): In function `sprintf':
/usr/include/x86_64-linux-gnu/bits/stdio2.h:33: undefined reference to `__sprintf_chk'
/usr/include/x86_64-linux-gnu/bits/stdio2.h:33: undefined reference to `__sprintf_chk'
/usr/local/musl/lib/libzlog.a(format.o): In function `memcpy':
/usr/include/x86_64-linux-gnu/bits/string3.h:53: undefined reference to `__memcpy_chk'
/usr/local/musl/lib/libzlog.a(record.o): In function `strcpy':
/usr/include/x86_64-linux-gnu/bits/string3.h:110: undefined reference to `__memcpy_chk'
/usr/local/musl/lib/libzlog.a(rotater.o): In function `snprintf':
/usr/include/x86_64-linux-gnu/bits/stdio2.h:64: undefined reference to `__snprintf_chk'
/usr/include/x86_64-linux-gnu/bits/stdio2.h:64: undefined reference to `__snprintf_chk'
/usr/include/x86_64-linux-gnu/bits/stdio2.h:64: undefined reference to `__snprintf_chk'
/usr/include/x86_64-linux-gnu/bits/stdio2.h:64: undefined reference to `__snprintf_chk'
/usr/include/x86_64-linux-gnu/bits/stdio2.h:64: undefined reference to `__snprintf_chk'
/usr/local/musl/lib/libzlog.a(rule.o): In function `syslog':
/usr/include/x86_64-linux-gnu/bits/syslog.h:31: undefined reference to `__syslog_chk'
/usr/local/musl/lib/libzlog.a(rule.o): In function `strcpy':
/usr/include/x86_64-linux-gnu/bits/string3.h:110: undefined reference to `__strcpy_chk'
/usr/local/musl/lib/libzlog.a(rule.o): In function `memcpy':
/usr/include/x86_64-linux-gnu/bits/string3.h:53: undefined reference to `__memcpy_chk'
/usr/local/musl/lib/libzlog.a(spec.o): In function `sprintf':
/usr/include/x86_64-linux-gnu/bits/stdio2.h:33: undefined reference to `__sprintf_chk'
/usr/local/musl/lib/libzlog.a(zc_profile.o): In function `vfprintf':
/usr/include/x86_64-linux-gnu/bits/stdio2.h:127: undefined reference to `__vfprintf_chk'
/usr/local/musl/lib/libzlog.a(zc_profile.o): In function `fprintf':
/usr/include/x86_64-linux-gnu/bits/stdio2.h:97: undefined reference to `__fprintf_chk'
/usr/include/x86_64-linux-gnu/bits/stdio2.h:97: undefined reference to `__fprintf_chk'
/usr/local/musl/lib/libzlog.a(zc_util.o): In function `snprintf':
/usr/include/x86_64-linux-gnu/bits/stdio2.h:64: undefined reference to `__snprintf_chk'
/usr/local/musl/lib/libzlog.a(buf.o): In function `strcpy':
/usr/include/x86_64-linux-gnu/bits/string3.h:110: undefined reference to `__memcpy_chk'
/usr/local/musl/lib/libzlog.a(buf.o): In function `vsnprintf':
/usr/include/x86_64-linux-gnu/bits/stdio2.h:77: undefined reference to `__vsnprintf_chk'
/usr/include/x86_64-linux-gnu/bits/stdio2.h:77: undefined reference to `__vsnprintf_chk'
/usr/include/x86_64-linux-gnu/bits/stdio2.h:77: undefined reference to `__vsnprintf_chk'
collect2: error: ld returned 1 exit status

与 gcc 相同的命令可以正常工作。 这些函数不是用musl实现的吗?

【问题讨论】:

  • 关于:-I../lib 这是告诉编译器头文件的位置,而不是库的位置。要告诉链接器库所在的位置,请使用-L../lib 注意大写“L”而不是大写“I”`
  • 你当然是对的。设置有点奇怪。相关库不需要头文件。该库位于链接器的标准查找路径中。

标签: c gcc musl


【解决方案1】:

您问题中的包含路径都是 glibc 文件,因此看起来您尝试链接的库是使用 glibc 构建的。这有时可以工作,但有一些限制。在您的情况下,它是使用 _FORTIFY_SOURCE 的 glibc 版本构建的,它使用 glibc 中目前在 musl 中不可用的符号(通常在 musl 上使用的 _FORTIFY_SOURCE 实现的工作方式不同)。长期以来,这项工作一直在长期议程上,但不是优先事项;如果可以,最好针对 musl 重建库。

【讨论】:

  • @Antti 你的意思是来自 musl 的作者本人?
  • 当然,就是这样。我可以得到源代码并针对 musl 编译它。工作正常。
【解决方案2】:

也许这与你的情况无关,但我在 linux 下使用 MinGW 进行交叉编译时遇到了同样的__memcpy_chk 问题。

通过将-lssp 标志添加到链接器来修复这个未定义的引用。如果你有configure,那么你可以做LDFLAGS="-lssp" ./configure ...

【讨论】:

    猜你喜欢
    • 2018-05-07
    • 2017-10-26
    • 1970-01-01
    • 1970-01-01
    • 2015-08-23
    • 1970-01-01
    • 2014-04-21
    • 2012-12-12
    • 2023-04-02
    相关资源
    最近更新 更多