Local static array is causing linker errors for 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
goku420
Member
Member
Posts: 51
Joined: Wed Jul 10, 2013 9:11 am

Local static array is causing linker errors for memcpy

Post by goku420 »

I have followed the guide on local static variables and they work for primitive types (like int.) However, when I use an array of my Panel objects, it's giving undefined reference to memcpy, even though I don't use memcpy in the function. Therefore I'm assuming it's attempting to use some sort of built-in memcpy to initialize the array. Does anyone have any solution for this?

Code: Select all

void Terminal::resizepanel(int column_factor, int row_factor)
{
	auto panelPtr = &panels[panelindex];
	static const Panel initial_panels[3] { panels[0], panels[1], panels[2] };
Edit: Note, it's definitely the static keyword making the linker errors happening.
User avatar
b.zaar
Member
Member
Posts: 294
Joined: Wed May 21, 2008 4:33 am
Location: Mars MTC +6:00
Contact:

Re: Local static array is causing linker errors for memcpy

Post by b.zaar »

The static keyword moves the initial_panel from the stack to the data section. This would mean initializing it may use the internal memcpy.

A simple fix would be to create a small memcpy function.
"God! Not Unix" - Richard Stallman

Website: venom Dev
OS project: venom OS
Hexadecimal Editor: hexed
goku420
Member
Member
Posts: 51
Joined: Wed Jul 10, 2013 9:11 am

Re: Local static array is causing linker errors for memcpy

Post by goku420 »

b.zaar wrote:The static keyword moves the initial_panel from the stack to the data section. This would mean initializing it may use the internal memcpy.

A simple fix would be to create a small memcpy function.
I do have a memcpy function.
alexfru
Member
Member
Posts: 1112
Joined: Tue Mar 04, 2014 5:27 am

Re: Local static array is causing linker errors for memcpy

Post by alexfru »

remyabel wrote:I do have a memcpy function.
Have you thought of making use of the information contained in the error message? What is the message?
User avatar
iansjack
Member
Member
Posts: 4709
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Local static array is causing linker errors for memcpy

Post by iansjack »

remyabel wrote:
b.zaar wrote:The static keyword moves the initial_panel from the stack to the data section. This would mean initializing it may use the internal memcpy.

A simple fix would be to create a small memcpy function.
I do have a memcpy function.
That doesn't make sense. If you have a memcpy function, and you are including it in the object files to be linked, how are you getting an error that it is undefined? There's something wrong with your toolset.
alexfru
Member
Member
Posts: 1112
Joined: Tue Mar 04, 2014 5:27 am

Re: Local static array is causing linker errors for memcpy

Post by alexfru »

iansjack wrote:That doesn't make sense. If you have a memcpy function, and you are including it in the object files to be linked, how are you getting an error that it is undefined? There's something wrong with your toolset.
That or the name of that function could differ from memcpy, but we haven't been shown the actual error.
goku420
Member
Member
Posts: 51
Joined: Wed Jul 10, 2013 9:11 am

Re: Local static array is causing linker errors for memcpy

Post by goku420 »

alexfru wrote:
iansjack wrote:That doesn't make sense. If you have a memcpy function, and you are including it in the object files to be linked, how are you getting an error that it is undefined? There's something wrong with your toolset.
That or the name of that function could differ from memcpy, but we haven't been shown the actual error.
Sorry for the late reply. The error message is what you'd expect:

Code: Select all

terminal.o: In function `Terminal::resizepanel(int, int)':
terminal.cpp:(.text+0x7d9): undefined reference to `memcpy'
terminal.cpp:(.text+0x7f3): undefined reference to `memcpy'
terminal.cpp:(.text+0x80d): undefined reference to `memcpy'
The declaration of memcpy looks like:

Code: Select all

void* memcpy(void* dest, const void* src, uint32_t len);
Note that I have changed uint32_t to size_t but size_t is typedefd to uint32_t anyway. I compared it to the C library prototype just to make sure.

And yes the object files are getting linked in otherwise I wouldn't be able to use the other functions defined in the same translation unit. I don't get undefined reference errors when using memcpy. Like I told you, it appears to be trying to use another memcpy due to the static keyword.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Local static array is causing linker errors for memcpy

Post by Combuster »

What would be the entire linker command line, which file in there should contain the memcpy, and have you dumped all symbols from that file?
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
goku420
Member
Member
Posts: 51
Joined: Wed Jul 10, 2013 9:11 am

Re: Local static array is causing linker errors for memcpy

Post by goku420 »

Combuster wrote:What would be the entire linker command line, which file in there should contain the memcpy, and have you dumped all symbols from that file?
Objdump:

Code: Select all

objdump -t -C cstring.o

00000000 g     F .text	00000037 memcpy(void*, void const*, unsigned long)
cstring.hpp:

Code: Select all

void* memcpy(void* dest, const void* src, size_t len);
cstring.cpp:

Code: Select all

void* memcpy(void* dest, const void* src, size_t len) {
	uint8_t* ptrd = (uint8_t*) dest;
	const uint8_t* ptrs = (uint8_t*) src;
	for (; len != 0; len--)
		*ptrd++ = *ptrs++;
	return dest;
}
Linker command line:

Code: Select all

../../i686-elf-4.9.0-Linux-x86_64/bin//i686-elf-g++ -T linker.ld -ffreestanding -O2 -nostdlib -o myos.bin \
            boot.o gdt_asm.o idt_asm.o keyboard.o vsprintf.o kernel.o printf.o cstring.o gdt.o svga.o multiboot.o terminal.o parse.o font.o scanf.o idt.o -lgcc
alexfru
Member
Member
Posts: 1112
Joined: Tue Mar 04, 2014 5:27 am

Re: Local static array is causing linker errors for memcpy

Post by alexfru »

remyabel wrote: Sorry for the late reply. The error message is what you'd expect:

Code: Select all

terminal.o: In function `Terminal::resizepanel(int, int)':
terminal.cpp:(.text+0x7d9): undefined reference to `memcpy'
terminal.cpp:(.text+0x7f3): undefined reference to `memcpy'
terminal.cpp:(.text+0x80d): undefined reference to `memcpy'
The declaration of memcpy looks like:

Code: Select all

void* memcpy(void* dest, const void* src, uint32_t len);
Since you appear to be compiling C++ code and memcpy() is supposed to be a C function and C-callable function, it is important that it have the proper prototype in the eyes of the C++ compiler:

Code: Select all

extern "C"
{
  void* memcpy(void* dest, const void* src, uint32_t len);
}
Do you have that?
Post Reply