【问题标题】:How to fix "defined in discarded section" linker error?如何修复“在丢弃的部分中定义”链接器错误?
【发布时间】:2015-04-26 19:26:41
【问题描述】:

我的程序在没有 -flto 的情况下编译得很好,但使用 -flto 我得到了这个错误:

% arm-none-eabi-g++ --version
arm-none-eabi-g++ (4.8.3-9+11) 4.8.3 20140820 (release)
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

% arm-none-eabi-g++ -O2 -W -Wall -fPIE -flto -march=armv7-a -mfloat-abi=hard -mfpu=vfpv3-d16 -ffreestanding -nostdlib -std=gnu++11 -fno-exceptions -fno-rtti -c -o main.o main.cc

% arm-none-eabi-g++ -fPIE -nostdlib -O2 -flto boot.o memcpy.o font.o main.o -lgcc -Tlink-arm-eabi.ld -o kernel.elf
`memcpy' referenced in section `.text' of /tmp/ccYO5wE8.ltrans0.ltrans.o: defined in discarded section `.text' of memcpy.o (symbol from plugin)
collect2: error: ld returned 1 exit status

我尝试将 memcpy.o 移动到不同的位置以尝试不同的链接顺序,但错误始终相同。我已经看到这是一个常见问题,但之前的问题的答案都不适用。我没有安装损坏的 boost 或使用不同的编译器版本进行编译。我正在构建一个裸机内核,因此除了 libgcc 之外没有涉及任何外部库。

有人知道那里发生了什么吗?

【问题讨论】:

  • g++-4.9.2 出现同样的错误

标签: c++ g++ linker-errors


【解决方案1】:

这似乎是在 gcc-4.7 中修复并在 gcc-4.8 中重现的编译器错误(gcc bugreport for 4.6reapearance in 4.8)。一个快速的解决方法是标记使用的函数:

void * memcpy(void *dest, const void *src, sizte_t n) __attribute__((used));
void * memcpy(void *dest, const void *src, size_t n) {
    uint8_t *d = (uint8_t *)dest;
    uint8_t *s = (uint8_t *)src;
    while(n--) {
    *d++ = *s++;
    }
    return dest;
}

这会阻止优化器丢弃该函数。感谢 Richard Biener 的建议。

【讨论】:

    猜你喜欢
    • 2012-04-06
    • 2016-09-04
    • 2014-09-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多