how to make your own "executable" files in your own OS

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.
User avatar
Nekrose483
Posts: 7
Joined: Sun Nov 01, 2009 5:00 pm
Location: Deutchland

how to make your own "executable" files in your own OS

Post by Nekrose483 »

so, i wrote the bootloader and the kernel... its pretty nice.. comming along well and its all in ASM. thing is.. i want to be able to compile my own "exe" i'm very unsure in how to implement this. Is there a tutorial or did i miss some information on the site? i know about the types of executable files... like a.out, ELF, PE, EXE. I was wondering how to make my own. like if i was to name it ".XSO" (for my OS) how would i go about implementing that... and what tools would i need?

Thank you very much in advance. i'm still in highschool and i recently just turned 18.. i'm one of those people who have dreamed of making their OS, but am set apart becuase.. I'M ACTUALLY DOING IT. heh. so, just a note: i've never taken any classes and my knowledge of computers is definatly limited, but is still expanding. I'm not a total beginner. heh. I know i have hope. and i KNOW writing an OS isnt easy. i just need the help along the way...

thanks in advance,

Nekrose
–Nekrose
User avatar
NickJohnson
Member
Member
Posts: 1249
Joined: Tue Mar 24, 2009 8:11 pm
Location: Sunnyvale, California

Re: how to make your own "executable" files in your own OS

Post by NickJohnson »

An executable format needs a few different things to work well. First, it at least needs to store the binary data (mostly machine code) that needs to be loaded into memory for the program to run, as well as the the position at which it should be loaded. It also should have a table that stores the names of the functions in the binary, known as a symbol table, so multiple object files in the format can be linked together. There are also a lot of other things, like dynamic linking and relocation information, that can be added too.

However, I would suggest using a popular executable format like ELF if you can - there are already many good tools that run on existing operating systems that manipulate ELF, and ELF files are very extensible and not very hard to write a loader for (at least for static binaries). I don't know much about PE, but I expect the same stuff applies. IMO, the only reasons you should write an executable format are the need of a novel feature and, of course, for the same reason as you would write an OS - to gain experience.
User avatar
AndrewAPrice
Member
Member
Posts: 2303
Joined: Mon Jun 05, 2006 11:00 pm
Location: USA (and Australia)

Re: how to make your own "executable" files in your own OS

Post by AndrewAPrice »

Is there a reason that you wish to do so when you have a platter to choose from? (A bad question, I know, because people can ask the same about making an operating system.)

If you're using GAS then it would be just writing your own file format's backend into binutils then you'll get to use all the tools provided with your new format.

Otherwise, you could link to a plain binary or ELF and write then write a tool which extracts the executable data and repackages it in your own format.
but am set apart becuase.. I'M ACTUALLY DOING IT
No need to brag because that's why we're all here. :)
My OS is Perception.
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Re: how to make your own "executable" files in your own OS

Post by neon »

You would need to create a specialized toolset that can output to your file format. I would personally recommend using another format like Elf or PE though - creating your own custom executable format can be hard if you do not yet know what it is that you want. Unless there is a reason that a special format is needed right now, I would use an existing format.
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
User avatar
AndrewAPrice
Member
Member
Posts: 2303
Joined: Mon Jun 05, 2006 11:00 pm
Location: USA (and Australia)

Re: how to make your own "executable" files in your own OS

Post by AndrewAPrice »

PE and ELF are both extremely simple to parse. It's basically a matter of reading a few bits in the header (e.g. the architecture, how many sections there are in there, the point at where to start executing), then loop through each section and copy it into memory. For dynamic linking, there is usually an import/export table you loop through which says the label, the file in it's in, and where to store the address once it's loaded into memory.

The .NET format (which is actually embedded as code inside a PE, but if you strip away the PE headers I believe it should be classified as it's own format) is a little different since it's more domain-specific. It goes into detail over the access privileges of members and the like.
My OS is Perception.
User avatar
Nekrose483
Posts: 7
Joined: Sun Nov 01, 2009 5:00 pm
Location: Deutchland

Re: how to make your own "executable" files in your own OS

Post by Nekrose483 »

MessiahAndrw wrote:No need to brag because that's why we're all here. :)
true, true. ^___^

and thank you very much for the help. i'm actually making this more of a class project.. so its not that i NEED to make my own format. its just showing that i CAN. you know? not really usefull for anything. but its kinda cool. you know? heh. thanks very much though ^___^
–Nekrose
User avatar
AndrewAPrice
Member
Member
Posts: 2303
Joined: Mon Jun 05, 2006 11:00 pm
Location: USA (and Australia)

Re: how to make your own "executable" files in your own OS

Post by AndrewAPrice »

Nekrose483 wrote:and thank you very much for the help. i'm actually making this more of a class project.. so its not that i NEED to make my own format. its just showing that i CAN. you know? not really usefull for anything. but its kinda cool. you know? heh. thanks very much though ^___^
The fact that your OS will have it's own set of system calls and interfaces for communicating with drivers automatically makes your applications binary incompatible with other operating systems (even though you're using COFF, a.out, PE, ELF, flat binary, whatever). So in a way you can consider it 'your own format'.

It's also perfectly reasonable to use your own file extension (.XSO) in order to distinguish between executables that will run under your operating system, and those which will run under another.
My OS is Perception.
User avatar
Firestryke31
Member
Member
Posts: 550
Joined: Sat Nov 29, 2008 1:07 pm
Location: Throw a dart at central Texas
Contact:

Re: how to make your own "executable" files in your own OS

Post by Firestryke31 »

I would say that, instead of writing your own exe format, choose between one of the following 3: PE (like Windows), ELF (like Linux), and Mach-O (like Mac OS X).

I've seen many ELF-using OSes, a couple of PE-using OSes, but the only Mach-O-using Os I've seen is OS X.
Owner of Fawkes Software.
Wierd Al wrote: You think your Commodore 64 is really neato,
What kind of chip you got in there, a Dorito?
jal
Member
Member
Posts: 1385
Joined: Wed Oct 31, 2007 9:09 am

Re: how to make your own "executable" files in your own OS

Post by jal »

Nekrose483 wrote:how would i go about implementing that... and what tools would i need?
Though I agree with everyone here that making your own executable format is not needed per se, and ELF is a good way to go, if you are really set on making one anyway then consider a few things:

1) What information do you need in the executable? Study existing formats (especially PE and ELF) to see what is included. Then put on paper what you need for your OS, and decide how you like to put that information in a file. You'll need at least the executable code and some ID to detect it as your OSes binary format. From that, it is all up to you and the memory management you devised for your OS, and what you allow for binaries (having DLLs or not, relocatable code, etc. etc.).

2) How to create the executable? An executable is created by a linker, a piece of software putting together the object files the compiler produces into something the OS can handle. You could create a linker yourself, but I'm tentively advising against it, as it is rather complex and specialistic, and will divert you from creating your OS for many months. It would be easier to take the output executable of a linker, e.g. PE or ELF (depending on your platform), and create a converter that converts the executable into the format you want. Most modern linkers can be provided a linker script in which you can instruct the linker to put the sections and all other stuff in the order you want, facilitating this a bit. So then you'd read and parse e.g. the ELF executable, and recreate the executable the way you want (e.g. with your XSO header). In it's simplest form this could mean replacing the ELF signature with your XSO one, although this would probably be considered a bit lame.


JAL
User avatar
f2
Member
Member
Posts: 311
Joined: Mon Jun 15, 2009 10:01 am
Location: France

Re: how to make your own "executable" files in your own OS

Post by f2 »

neon wrote: You would need to create a specialized toolset that can output to your file format.
That's what I did with my assembler: it can produce "HX" executables. "HX" is the executable format of my OS.
"Open source seems to embrace the dark side of human nature." - Ville Turjanmaa
User avatar
Dex
Member
Member
Posts: 1444
Joined: Fri Jan 27, 2006 12:00 am
Contact:

Re: how to make your own "executable" files in your own OS

Post by Dex »

It depends what type of OS your making, but if your OS is single-tasking and it only has one program loaded a say at 4mb, you could do something has simple as this

Code: Select all

use32
	ORG   0x400000				       ; where our program is loaded to
	jmp   start				       ; jump to the start of program.
	db    'XSO '				       ; We check for this, to make sure it a valid XSO file.

 ;----------------------------------------------------;
 ; Start of program.                                  ;
 ;----------------------------------------------------;
start:
; SOME CODE HERE
        ret
Then assemble as
fasm test.asm test.xso <enter>
load the file to 0x400000
then just
call 0x400000
Thats it, you could also do the same with multi-tasking and paging, by changing ORG 0x400000 to ORG 0x0
You could add much more to the header.
jal
Member
Member
Posts: 1385
Joined: Wed Oct 31, 2007 9:09 am

Re: how to make your own "executable" files in your own OS

Post by jal »

Dex wrote:Thats it, you could also do the same with multi-tasking and paging, by changing ORG 0x400000 to ORG 0x0
Or, you could page it in at 0x400000 anyway :).


JAL
User avatar
AndrewAPrice
Member
Member
Posts: 2303
Joined: Mon Jun 05, 2006 11:00 pm
Location: USA (and Australia)

Re: how to make your own "executable" files in your own OS

Post by AndrewAPrice »

jal wrote:
Dex wrote:Thats it, you could also do the same with multi-tasking and paging, by changing ORG 0x400000 to ORG 0x0
Or, you could page it in at 0x400000 anyway :).

JAL
Make the linker output all references to labels into a linked list in the header so you can reposition code during loading.
My OS is Perception.
User avatar
Nekrose483
Posts: 7
Joined: Sun Nov 01, 2009 5:00 pm
Location: Deutchland

Re: how to make your own "executable" files in your own OS

Post by Nekrose483 »

MessiahAndrw wrote:It's also perfectly reasonable to use your own file extension (.XSO) in order to distinguish between executables that will run under your operating system, and those which will run under another.
that's EXACTLY what i was thinking. ^_^
–Nekrose
User avatar
Nekrose483
Posts: 7
Joined: Sun Nov 01, 2009 5:00 pm
Location: Deutchland

Re: how to make your own "executable" files in your own OS

Post by Nekrose483 »

all of these are awesome ideas ^___^ i might stick with the ELF because, to me, it looks like the easyest to read and parse. and i will just change the header on it and stick it with a ".XSO" on the end. that seems the best route for now. thank you
–Nekrose
Post Reply