Booting PE files - and a lot more

Programming, for all ages and all languages.
Post Reply
rudimk
Posts: 15
Joined: Mon Apr 26, 2010 3:33 am
Location: New Delhi, India

Booting PE files - and a lot more

Post 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
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Booting PE files - and a lot more

Post 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.
Every good solution is obvious once you've found it.
rudimk
Posts: 15
Joined: Mon Apr 26, 2010 3:33 am
Location: New Delhi, India

Re: Booting PE files - and a lot more

Post 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!
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Booting PE files - and a lot more

Post 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.
Every good solution is obvious once you've found it.
rudimk
Posts: 15
Joined: Mon Apr 26, 2010 3:33 am
Location: New Delhi, India

Re: Booting PE files - and a lot more

Post 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
Gigasoft
Member
Member
Posts: 855
Joined: Sat Nov 21, 2009 5:11 pm

Re: Booting PE files - and a lot more

Post 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.
rudimk
Posts: 15
Joined: Mon Apr 26, 2010 3:33 am
Location: New Delhi, India

Re: Booting PE files - and a lot more

Post 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!
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: Booting PE files - and a lot more

Post 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.
"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 ]
technik3k
Member
Member
Posts: 31
Joined: Thu Jan 14, 2010 5:35 pm

Re: Booting PE files - and a lot more

Post 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.
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Re: Booting PE files - and a lot more

Post 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.
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
User avatar
bewing
Member
Member
Posts: 1401
Joined: Wed Feb 07, 2007 1:45 pm
Location: Eugene, OR, US

Re: Booting PE files - and a lot more

Post 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.
rudimk
Posts: 15
Joined: Mon Apr 26, 2010 3:33 am
Location: New Delhi, India

Re: Booting PE files - and a lot more

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