Page 1 of 1

BareBones C++ - linking removes my kmain function

Posted: Wed Feb 11, 2015 11:58 am
by sebihepp
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

Re: BareBones C++ - linking removes my kmain function

Posted: Wed Feb 11, 2015 12:48 pm
by Combuster
Check your file modified timestamps.

Re: BareBones C++ - linking removes my kmain function

Posted: Wed Feb 11, 2015 1:10 pm
by sebihepp
Wow, you were right. the resulting file wasn't updated since yesterday. Now I will investigate why...

EDIT: Got it. I used the parameter -print-libgcc-file-name, but with it, it only prints the filename and doesn't link.

By the way, I get a warning while linking now: .bss section is changed to PROGBITS. I googled it and found out, that this means, that the .bss section isn't uninitialized anymore, and contains now data. This results in a larger elf-file. Now I have to find out, which section from the source files contains data and is written in the .bss section...

Thank you all very much.