tuhdo / os01

Bootstrap yourself to write an OS from scratch. A book for self-learner.
11.9k stars 701 forks source link

Use a cross compiler for OS #122

Open shakram02 opened 5 years ago

shakram02 commented 5 years ago

Hello, I"m very grateful that I found your book, it has lots of information laid nicely, I followed it to the end (I hope it gets complete one day!) and completed the missing information online without much trouble.

I just want to note that I guess GCC won't be friendly with the OS binary as it inserts calls to some of its functions (I don't remember the exact name of the function) like .text.***_ax or something like that which get the OS code into an undefined state. (I followed the code with gdb)

Guys on the osdev wiki mandated that we have a cross compiler build specifically for our target, when I did that and compiled the OS and retraced the code it worked as expected and contained to extra call.

Thank you so much for your efforts writing the book, I appreciate them

tuhdo commented 5 years ago

I'm glad you enjoy the book! I'm happy to hear it.

For cross compilers, indeed it is recommended that way. However, to reduce the complexity of already complex learning project, I decided to use a native compiler since code generated is x86 anyway. The code sections like .text.***_ax are filtered by a linker script. That is, you use a linker script and objcopy utility to discard unused code sections and assemble object files into a final binary image however you want. You can also tell gcc to not generate PIE (Position-independent Executable) code to strip more code sections for a simpler binary output. I always want to include more advance exercises on linker script and manual crafting of binary image, but haven't found the time to do so.

shakram02 commented 5 years ago

I couldn't actually get it working with the linker script, the first issue was that I had to flip the order on PHDRS command (code comes first then headers), as it gave me an error that PHDR segment not covered by LOAD segment I knew that it wasn't correct to just flip the entries

after that I had a problem that I couldn't get physical and virtual memory addresses and the offset to be correct (even when I used the recipe in the book), I just kep trying in other ways until I got things barely working (I got shifted debug symbols at the very end)

I couldn't solve that problem, but in my opinion building a cross-compiler on Linux wasn't that complicated, to be honest, I was just scared of stepping into doing it just because it seemed complex, but using the steps ok OSDev made things easier, I just needed to modify the Makefiles after that (which was a bit tricky for me)

I also felt that linker scripts aren't documents and I couldn't really search about what's wrong when anything happened (I just kept randomly trying until things work), so that why I suggest that maybe using a cross compiler is a better option

but surely I'm not saying that was annoying, I'm grateful that those problems got me into the cross compiler thing, thanks :pray: