undefined reference to `memcpy'?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
tsdnz
Member
Member
Posts: 333
Joined: Sun Jun 16, 2013 4:09 am

undefined reference to `memcpy'?

Post 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]);
Last edited by tsdnz on Mon Feb 01, 2016 12:43 am, edited 2 times in total.
tsdnz
Member
Member
Posts: 333
Joined: Sun Jun 16, 2013 4:09 am

Re: undefined reference to `memcpy'?

Post by tsdnz »

It has something to do with the

Code: Select all

sizeof(TestCode)
FallenAvatar
Member
Member
Posts: 283
Joined: Mon Jan 03, 2011 6:58 pm

Re: undefined reference to `memcpy'?

Post by FallenAvatar »

1) No where near enough info to help you.
2) Are you compiling with "-fno-built-ins"?

- Monk
tsdnz
Member
Member
Posts: 333
Joined: Sun Jun 16, 2013 4:09 am

Re: undefined reference to `memcpy'?

Post 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 
tsdnz
Member
Member
Posts: 333
Joined: Sun Jun 16, 2013 4:09 am

Re: undefined reference to `memcpy'?

Post 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
Boris
Member
Member
Posts: 145
Joined: Sat Nov 07, 2015 3:12 pm

Re: undefined reference to `memcpy'?

Post 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
mallard
Member
Member
Posts: 280
Joined: Tue May 13, 2014 3:02 am
Location: Private, UK

Re: undefined reference to `memcpy'?

Post 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.
Image
Post Reply