BareBones C++ - linking removes my kmain function
Posted: Wed Feb 11, 2015 11:58 am
Hello,
It has been a while since my last actions on OS development. Now I am back and followed the BareBones Tutorial, to get in again.
I have a strange problem. I use GRUB2 and the multiboot standard for loading my kernel. For now, my kernel consists of only 2 files: boot.s and main.cpp
boot.s is nearly a copy and paste variant from the Tutorial. Only difference is in naming the stack and his section, and the kernel_main, which I called kmain. The disassembler shows the correct code for boot.o.
My main.cpp differs more from the tutorial. I print a Hello World completely within kmain, declared the videomemory as volatile to prevent the compiler optimize my entire function away, and return at the end. Disassembler also shows the correct code.
My problem begins after linking, as the resulting elf-file doesn't call my kmain function. If I disassemble, the entire code from main.o is gone, as well as the "call kmain". Even the sections from main.o are empty. First I thought of the optimizations and used -g and -O0 for linking, but the problem still exists.
There are no error messages, not even warnings. I use my built crosscompiler (binutils and gcc (c/c++)) and I didn't forget the libgcc. My linker script looks exactly like the one in the tutorial without the comments. The result after linking works with grub2 and bochs, it only lacks of the code from main.cpp/main.o. I also use gcc for linking, not ld.
I am now at a point, where I have absolutely no clue, why this happens, and how to solve this issue. I mean, a linker shouldn't remove an instruction without any message, should it? Perhaps someone can bring me on the right track again...
Best regards
Sebihepp
It has been a while since my last actions on OS development. Now I am back and followed the BareBones Tutorial, to get in again.
I have a strange problem. I use GRUB2 and the multiboot standard for loading my kernel. For now, my kernel consists of only 2 files: boot.s and main.cpp
boot.s is nearly a copy and paste variant from the Tutorial. Only difference is in naming the stack and his section, and the kernel_main, which I called kmain. The disassembler shows the correct code for boot.o.
My main.cpp differs more from the tutorial. I print a Hello World completely within kmain, declared the videomemory as volatile to prevent the compiler optimize my entire function away, and return at the end. Disassembler also shows the correct code.
My problem begins after linking, as the resulting elf-file doesn't call my kmain function. If I disassemble, the entire code from main.o is gone, as well as the "call kmain". Even the sections from main.o are empty. First I thought of the optimizations and used -g and -O0 for linking, but the problem still exists.
There are no error messages, not even warnings. I use my built crosscompiler (binutils and gcc (c/c++)) and I didn't forget the libgcc. My linker script looks exactly like the one in the tutorial without the comments. The result after linking works with grub2 and bochs, it only lacks of the code from main.cpp/main.o. I also use gcc for linking, not ld.
I am now at a point, where I have absolutely no clue, why this happens, and how to solve this issue. I mean, a linker shouldn't remove an instruction without any message, should it? Perhaps someone can bring me on the right track again...
Best regards
Sebihepp