Executables

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
Edward
Posts: 13
Joined: Wed Oct 18, 2006 1:12 pm

Executables

Post by Edward »

I was thinking...

when I use assembly language to create a binary file I use the 'org' directive to give the initial offset of the image.

for example in a '*.com' dos executable I would use org 100h because it is loaded at (some segment):100h

in a single tasking enviroment this is fine because there is only one task at any one time.

However in a protected mode enviroment segmentation works differantly so I was wondering in pmode, how do you run for example, two instances of the same application at once, each instance must be loaded at the same offset so if you are going to create segments with base 0 and limit 4 Gi how do you run both.

I undestand that you can run two proceses at the same offsets but with differant segments by changing the base address of the segments. But I undestand OSs like Linux don't do this.

Sorry about the lack of clarity. Edward.
Otter
Member
Member
Posts: 75
Joined: Sun Dec 31, 2006 11:56 am
Location: Germany

Post by Otter »

OSs like Linux use paging, that means that processes have their own address space. Because of that, none of the instances of your program can see another in memory and all of them can use the same virtual base adress.

OSs without paging cannot start binary executables, because they need relocation informations.
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

Post by earlz »

I'm thinking they use paging but not completely for sure...

for instance(in theory) task A is run 2 times, we'll call them process B and C, when process B's time share is gone, you do something to map addresses to another address(via paging) we then switch to C at the same address but we switched the page tables(or whatever) so that where B is loaded is actually somewhere else and such...bad explanation..
Edward
Posts: 13
Joined: Wed Oct 18, 2006 1:12 pm

Post by Edward »

with pageing would you create two page directories.

Then have the first two entries in those pointing to some identity mapped kernel space. but in the one for process A (assembled for offset 0xc0000000) you map say, the physical address 0x10000000 to 0xc0000000 and in the one for process B you map physical address 0x20000000 to 0xc0000000 and when you perform a task switch you change page girectories. to that both processes appear at 0xc0000000.

Is this right?
User avatar
Dex
Member
Member
Posts: 1444
Joined: Fri Jan 27, 2006 12:00 am
Contact:

Post by Dex »

You need to load a relocatable file format eg: PE: in the header is relocatable info, so you would use ORG 0, some address do not need fixing eg: jump 10 bytes from here, other do, so the offset is stored in the header, you jump to that offset in the program, get what at that address and add the load address (eg: you could load two of the same files one to 2MB and one to 3MB).
This is done when you load a file.

This is a simple example, read about relocatable file format for more info.
Ready4Dis
Member
Member
Posts: 571
Joined: Sat Nov 18, 2006 9:11 am

Post by Ready4Dis »

Paging can insure all apps run from a specific location, however I recommend looking into a relocateable format as well, for example: You want a process to load a shared library at a random address, the kernel wants to load drivers into kernel space so it doesn't have to do a page table switch each time a driver function is called. There are many formats that you can use, a.out, coff,elf,pe, they all have minor differences,so find the one that fits your needs (or make your own). I am working on a custom relocation format, I have a formatter that can load either coff or a.out files and convert them (it also links them together as well). I need to make major changes though, I want to keep symbol information sometimes, which I currently don't (for things like loading a shared library!).
Edward
Posts: 13
Joined: Wed Oct 18, 2006 1:12 pm

Post by Edward »

Which format would You sujest. PE sounds good but does M$ have patents on it?
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

Post by earlz »

no patents on PE...yet
I find it funny they call it Portable Executable format, yet nothing but windows uses it(and I think only on the PC)
User avatar
Brynet-Inc
Member
Member
Posts: 2426
Joined: Tue Oct 17, 2006 9:29 pm
Libera.chat IRC: brynet
Location: Canada
Contact:

Post by Brynet-Inc »

hckr83 wrote:no patents on PE...yet
I find it funny they call it Portable Executable format, yet nothing but windows uses it(and I think only on the PC)
Indeed, But considering what they did to FAT..

It might be best to avoid Microsoft :P

ELF or OpenBSD's a.out is what I'd use..

PE is in itself just an extended COFF format.
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

Post by earlz »

PE is in itself just an extended COFF format.
no, PE is COFF, just PE is branched out to COFF, EXE, and DLL
Edward
Posts: 13
Joined: Wed Oct 18, 2006 1:12 pm

Post by Edward »

I'm currently using FASM. when I am creating ELF files, I decided to try ELF; everyone seems very keen on it, should I use:

'ELF' or 'ELF executable'

to produce sutible 'relocatable files'?

The former produces a file that is described by readelf as 'relocatable' and contains relocation information.

The later is used in the FASM source for linux and seems to create an executable that does not contain relocation information.

Thank you, ED
Ready4Dis
Member
Member
Posts: 571
Joined: Sat Nov 18, 2006 9:11 am

Post by Ready4Dis »

If you want relocation, use the first, if you are linking to a specific location, use the second. Executeable means that all linking information has been patched, if you are using the same address with seperate memory spaces, for example: Every application starts at 0xC0000000, so you can link it executeable at that location, and it will relocate all if it's jumps/variable access', etc to that location. If each application doesnt have a static address and you want it to be able to be loaded anywhere in memory, you must compile it non-executeable, and do the relocations when you load the program from disk using whatever address you moved it to as the base.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Post by Candy »

hckr83 wrote:
PE is in itself just an extended COFF format.
no, PE is COFF, just PE is branched out to COFF, EXE, and DLL
Actually, no, PE is a further development of COFF. EXE and DLL files are specific extensions given to a particular instantiation of either PE or COFF (namely, the executable one).
Post Reply