如何修正“在丢弃部分中定义”链接器错误?

时间:2021-12-05 20:26:37

My program compiles fine without -flto but with -flto I get this error:

我的程序在没有-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

I tried moving the memcpy.o to different positions to try different link orders but the error is always the same. I've seen that this is a common problem but none of the answeres to previous questions applies. I don't have a broken boost installed or using different compiler versions to compile. I'm building a bare-metal kernel so there isn't any outside library involed other than libgcc.

我试着移动memcpy。o以不同的位置尝试不同的链接顺序,但错误总是相同的。我发现这是一个常见的问题,但对之前问题的回答都不适用。我没有安装坏的boost,也没有使用不同的编译器版本进行编译。我正在构建一个裸机内核,因此除了libgcc之外没有任何外部库。

Anyone an idea what is going on there?

有人知道这是怎么回事吗?

1 个解决方案

#1


5  

This seems to be a compiler bug that was fixed in gcc-4.7 and reapeared in gcc-4.8 (gcc bugreport for 4.6, reapearance in 4.8). A quick workaround is to mark the function used:

这似乎是一个在gcc-4.7中修复的编译器错误,并在gcc-4.8中重新分配(gcc bugreport为4.6,重新分配为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;
}

That stops the optimizer from discarding the function. Thanks to Richard Biener for suggesting that.

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

#1


5  

This seems to be a compiler bug that was fixed in gcc-4.7 and reapeared in gcc-4.8 (gcc bugreport for 4.6, reapearance in 4.8). A quick workaround is to mark the function used:

这似乎是一个在gcc-4.7中修复的编译器错误,并在gcc-4.8中重新分配(gcc bugreport为4.6,重新分配为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;
}

That stops the optimizer from discarding the function. Thanks to Richard Biener for suggesting that.

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