Page 1 of 1

undefined reference to `memcpy'?

Posted: Sun Jan 31, 2016 10:35 pm
by tsdnz
Edit: Solved.
Edit: Not Solved, I was in debug mode.:

Hi all, I am not sure why this error is displayed.
I have memcpy in my program as below.

Code: Select all

extern "C" void* memcpy(void *dest, const void *src, size_t len);
This error happens in the following code, using the struct.

Code: Select all

template<QWORD Size> struct QuickCopy
{
	BYTE Data[Size];

	static FIL void Copy(QWORD Destination, QWORD Source);
	static FIL void Copy(void* Destination, void* Source);
};
If I switch the commented lines below it works fine.

Code: Select all

template<QWORD Size> void QuickCopy<Size>::Copy(QWORD Destination, QWORD Source)
{
	//memcpy((void*)Destination, (void*)Source, Size);
	*(QuickCopy<Size>*)Destination = *(QuickCopy<Size>*)Source;
}
template<QWORD Size> void QuickCopy<Size>::Copy(void* Destination, void* Source)
{
	//memcpy((void*)Destination, (void*)Source, Size);
	*(QuickCopy<Size>*)Destination = *(QuickCopy<Size>*)Source;
}
For some reason when optimization is full it cannot find memcpy.

This works.

Code: Select all

QuickCopy<2048>::Copy(1, 0x12345687);
But this does not

Code: Select all

QuickCopy<(QWORD)sizeof(TestCode)>::Copy((QWORD)Account->Code, (QWORD)&TestCode[0]);

Re: undefined reference to `memcpy'?

Posted: Sun Jan 31, 2016 10:40 pm
by tsdnz
It has something to do with the

Code: Select all

sizeof(TestCode)

Re: undefined reference to `memcpy'?

Posted: Mon Feb 01, 2016 12:13 am
by FallenAvatar
1) No where near enough info to help you.
2) Are you compiling with "-fno-built-ins"?

- Monk

Re: undefined reference to `memcpy'?

Posted: Mon Feb 01, 2016 12:26 am
by tsdnz
Compiler:

Code: Select all

-fno-use-cxa-atexit -ffast-math -fno-exceptions -mcmodel=large -fno-rtti -ffreestanding -mno-red-zone -Wall -Wextra -nostdlib -m64 -std=c++11 -msse3 -msse2 -mmmx -msse -mno-3dnow
Linker:

Code: Select all

-nostdlib -fno-use-linker-plugin 

Re: undefined reference to `memcpy'?

Posted: Mon Feb 01, 2016 12:36 am
by tsdnz
tjmonk15 wrote:1) No where near enough info to help you.
2) Are you compiling with "-fno-built-ins"?

- Monk
I am now, cheers that worked. Oops, sorry I was in debug mode, not working, the correct command line is:

Code: Select all

$ x86_64-elf-g++ -c DatabaseServer.cpp -o DatabaseServer.o -fno-builtin -fno-builtin-function -fno-use-cxa-atexit -ffast-math -fno-exceptions -mcmodel=large -fno-rtti -ffreestanding -mno-red-zone -Ofast -Wall -Wextra -nostdlib -m64 -std=c++11 -msse3 -msse2 -mmmx -msse -mno-3dnow -fstrength-reduce -fomit-frame-pointer -finline-functions -fno-tree-loop-distribute-patterns -funroll-loops

Re: undefined reference to `memcpy'?

Posted: Fri Feb 05, 2016 11:49 am
by Boris
A simple explanation : in c++ assignments are done through copy constructors or operators= .
By default, the compiler will cook some memory copy method.
You should implement yours . Given the size of your template, you could copy your data 8byte per 8 byte, 4 byte per 4 byte.. depending if you use uint64..uint8 copy

Re: undefined reference to `memcpy'?

Posted: Sat Feb 06, 2016 8:30 am
by mallard
From: https://gcc.gnu.org/onlinedocs/gcc/Standards.html
GCC requires the freestanding environment provide memcpy, memmove, memset and memcmp.
i.e. GCC may emit calls to those functions even when they're not explicitly called by the source code.