BareBones C++ - linking removes my kmain function

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
sebihepp
Member
Member
Posts: 190
Joined: Tue Aug 26, 2008 11:24 am
GitHub: https://github.com/sebihepp

BareBones C++ - linking removes my kmain function

Post 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
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: BareBones C++ - linking removes my kmain function

Post by Combuster »

Check your file modified timestamps.
"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 ]
sebihepp
Member
Member
Posts: 190
Joined: Tue Aug 26, 2008 11:24 am
GitHub: https://github.com/sebihepp

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

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