How to fix the "Defined in the dropped section" error?

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

% 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 memcpy.o to different positions to try different link orders, but the error is always the same. I saw that this is a common problem, but none of the answers to previous questions apply. I don’t have a broken forced level installed or different versions of the compiler are used for compilation. I am building a kernel without a kernel, so there is no external library other than libgcc.

Does anyone know what is going on there?

+7
c ++ g ++ linker-errors
source share
1 answer

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

 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; } 

This stops the optimizer from abandoning the function. Thanks to Richard Biener for suggesting that.

+6
source share

All Articles