Page 1 of 1

Booting PE files - and a lot more

Posted: Tue Jul 13, 2010 5:26 am
by rudimk
Hey there!

Okay. Got some things to ask :P

First. Suppose I use FreeBASIC's compiler for Windows, to write a small bare-bones kernel.
But - I get a PE file.
Now, I know it's possible to boot a PE file, but how does one do that?
Is it possible to use GRUB? Or GRUB4DOS?
Or can it be done with a custom bootloader?
And do you know of any tutorial, which can walk me through the process?

Second. The PE kernel is ready, and it's booted fine.
But, I want to link it with the OSKit libraries, which are ELF libraries, if I'm right.
So is it possible to link the PE kernel with those ELF libraries?
If yes, again, how?

Third. The ELF libraries are linked just fine.
Now, when I want to write a small program, which runs in userspace inside my PE kernel, does that program have to be a PE file, or can it be an ELF as well?

Thanks :D

Re: Booting PE files - and a lot more

Posted: Tue Jul 13, 2010 6:15 am
by Solar
Please read FreeBASIC and FreeBasic Barebones in our Wiki...

...which is incidentially linked in the FORUM RULES sticky thread in the OS Development subboard, which would have been more appropriate for your clearly OS-related questions, as well as prominently linked in the header ("The OSDev.org Wiki - Got a question? Search this first!").

The keyword I used to find those two Wiki pages for you was "FreeBasic". There is also an article on PE format.

Re: Booting PE files - and a lot more

Posted: Tue Jul 13, 2010 7:07 am
by rudimk
Hey, Solar!
What's up?

Okay. Yeah, I read all of that.
I know for one, that I can use C libraries in FreeBASIC code, but the thing is - is there no issue when it comes to linking an ELF library to a PE file?

As for the PE booting part...again, the wiki wasn't exactly illuminating..but i'll survive :P

Thanks!

Re: Booting PE files - and a lot more

Posted: Tue Jul 13, 2010 7:27 am
by Solar
rudimk wrote:Yeah, I read all of that.
No you didn't. The FB Barebones tells you how to get ELF binaries out of your FB compiler.

Re: Booting PE files - and a lot more

Posted: Tue Jul 13, 2010 7:31 am
by rudimk
Solar wrote:
rudimk wrote:Yeah, I read all of that.
No you didn't. The FB Barebones tells you how to get ELF binaries out of your FB compiler.
I did :P
I don't want ELF binaries.
I'd like to boot PEs, just for a change!
Yeah, I could simply do a cross compiler, and copy the the ld, ar and as binaries to the platform folders.
But like I mentioned before, I don't want ELFs.

So there :D

Re: Booting PE files - and a lot more

Posted: Tue Jul 13, 2010 8:28 am
by Gigasoft
Then you usually can't use GRUB. But loading a PE file isn't particularly difficult. Just remember that your loader probably won't fit in 510 bytes, so it must be able to load the rest of itself. It should locate the remainder of itself on the disk and load it to address 7dfeh. Then, load the PE file and other needed files, set up protected mode and paging, and copy each section to the correct address. The first 510 bytes of the loader, along with the boot signature, should be written to the boot sector, and the rest of it to whereever you want. You don't need a tutorial for this, as all this is straightforward to figure out on your own.

The format you use for application programs and drivers, is of course completely unrelated to the format used for the system image.

Re: Booting PE files - and a lot more

Posted: Tue Jul 13, 2010 9:26 am
by rudimk
Gigasoft wrote:Then you usually can't use GRUB. But loading a PE file isn't particularly difficult. Just remember that your loader probably won't fit in 510 bytes, so it must be able to load the rest of itself. It should locate the remainder of itself on the disk and load it to address 7dfeh. Then, load the PE file and other needed files, set up protected mode and paging, and copy each section to the correct address. The first 510 bytes of the loader, along with the boot signature, should be written to the boot sector, and the rest of it to whereever you want. You don't need a tutorial for this, as all this is straightforward to figure out on your own.

The format you use for application programs and drivers, is of course completely unrelated to the format used for the system image.
Okay.
I see.
<comprehension dawns>
I guess I just need to stay up a couple of nights, and tweak my way around!
But linking my PE kernel to an ELF library - that's possible, right?
Because if it isn't, then I'd have to go the cross-compiler way, and end up with an ELF kernel..

Thanks!

Re: Booting PE files - and a lot more

Posted: Tue Jul 13, 2010 9:37 am
by Combuster
But linking my PE kernel to an ELF library - that's possible, right?
Common sense says that you can't turn a application into a library. Common sense also doesn't mix ELF and PE.

That and there are numerous ABI differences between windows-targeted freebasic, and the crosscompiler configuration.

Re: Booting PE files - and a lot more

Posted: Tue Jul 13, 2010 12:10 pm
by technik3k
Combuster wrote:
But linking my PE kernel to an ELF library - that's possible, right?
Common sense says that you can't turn a application into a library. Common sense also doesn't mix ELF and PE.

That and there are numerous ABI differences between windows-targeted freebasic, and the crosscompiler configuration.
Combuster is correct: you cannot just use external libraries unless your kernel itself has code that knows how to load them. Furthermore, if external library needs to communicate with kernel or other libraries you must have an ABI or IPC in place and all libraries must be compiled for those specific interfaces. You cannot just pull some binaries out of thin air and start using them. You will be better off just linking the code you need into your kernel.
rudimk wrote:I don't want ELF binaries.
I'd like to boot PEs, just for a change!
There must be a reason. Preferably a good one. I choose to have PE format because then I could debug my non hardware specific code under MSVS and run the kernel in simulation mode under Windows (see here).

However, I am not sure that my approach is suitable for you. Regardless how you compile your kernel, you still have to apply very strict linker rules and obey restrictions of the boot environment. For example, anything more complex then "Hello World" will require to setup your own GDT, IDT, handle Interrupts and Exceptions, manage memory, etc...

When all other things taken into consideration the difference between choosing ELF and PE format, given that link and boot instructions for both are already well documented, becomes negligible.

Re: Booting PE files - and a lot more

Posted: Tue Jul 13, 2010 2:18 pm
by neon
You can boot PE files with Grub via multiboot, personally I have only done it with C kernels though. It might be possible to boot this way if you can position the multiboot header correctly with FreeBASIC.

Re: Booting PE files - and a lot more

Posted: Tue Jul 13, 2010 8:02 pm
by bewing
As far as the OSkit libraries go -- it is not hard to build them as PE files. GCC under cygwin automatically builds all files as PE, not ELF.

There is no convenient way to link ELF and PE binaries together. It is probably doable, but you really shouldn't consider hacking it, as said above. Too error prone, if nothing else. It's much easier to build yourself the cross compiler. And, as I said above, even easier to use one that's already set up for you.

As for question #3 -- you get to choose your own executable/linkable format when you build your compiler in your own OS. If you insist that your kernel be PE, and you want your compiler to be able to build your kernel, then a PE output format must be available. But you can have multiple supported output formats in gcc and many other compilers.

Re: Booting PE files - and a lot more

Posted: Tue Jul 13, 2010 8:23 pm
by rudimk
bewing wrote:As far as the OSkit libraries go -- it is not hard to build them as PE files. GCC under cygwin automatically builds all files as PE, not ELF.

There is no convenient way to link ELF and PE binaries together. It is probably doable, but you really shouldn't consider hacking it, as said above. Too error prone, if nothing else. It's much easier to build yourself the cross compiler. And, as I said above, even easier to use one that's already set up for you.

As for question #3 -- you get to choose your own executable/linkable format when you build your compiler in your own OS. If you insist that your kernel be PE, and you want your compiler to be able to build your kernel, then a PE output format must be available. But you can have multiple supported output formats in gcc and many other compilers.
Hmm.
Guess you got a point.
Maybe hacking those libraries is going to be a not-so-good idea.
Ah, well. I'll build the cross compiler.
:P

I see.
Well, I'm still not sure about the self hosting thing - having the compiler inside the OS build the OS..Got eons of time for that!

Thanks a ton!