Page 1 of 1
Local static array is causing linker errors for memcpy
Posted: Tue Sep 23, 2014 12:27 am
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.
Re: Local static array is causing linker errors for memcpy
Posted: Tue Sep 23, 2014 1:23 am
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.
Re: Local static array is causing linker errors for memcpy
Posted: Tue Sep 23, 2014 1:29 am
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.
Re: Local static array is causing linker errors for memcpy
Posted: Tue Sep 23, 2014 1:38 am
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?
Re: Local static array is causing linker errors for memcpy
Posted: Tue Sep 23, 2014 1:55 am
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.
Re: Local static array is causing linker errors for memcpy
Posted: Tue Sep 23, 2014 2:40 am
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.
Re: Local static array is causing linker errors for memcpy
Posted: Tue Sep 23, 2014 3:04 am
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.
Re: Local static array is causing linker errors for memcpy
Posted: Tue Sep 23, 2014 3:36 am
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?
Re: Local static array is causing linker errors for memcpy
Posted: Tue Sep 23, 2014 3:42 am
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
Re: Local static array is causing linker errors for memcpy
Posted: Tue Sep 23, 2014 3:51 am
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?