flat assembler
Message board for the users of flat assembler.

Index > OS Construction > Relocatable executables

Author
Thread Post new topic Reply to topic
Artlav



Joined: 23 Dec 2004
Posts: 188
Location: Moscow, Russia
Artlav 05 Aug 2006, 13:07
The problem is:

OS loads the image of the executable at some random address in the memory.
But many instructions in the executable code uses absolute addressing, like in "mov esi,msg"
So, if the program is not loaded at the specified addess, it won't run properly.

On x86 it can be solved by using segments, like in DOS, or pagination to map the program at predefined address.
But what if other CPU doesn't support any of them (like some of the ARM's)?

How the interprocess commnications and shared objects is handled (x86)?
dll's and shared o's is somehow relocated entirely to the address, unknown at compile time.
So, is relocating executables/libraryes accomplished by compiling them position-indepedent, or by some sort of big relocation table?

I tryed reading ELF manual, but found nothing definit on the subject.




Question summary:
OS of my design uses flat memory model, applications is loaded at random memory address. Is there a way to compile the applications (with fasm) in such a way, that it will be possible to make them run properly at initially unknown base address?

The program example below:

Code:
use32

start:
jmp begin

;###############################################################################

begin:

mov     esi,msg
mov     eax,111
int     32

ret

msg     db "Hello world! (Aprom PE/AE)",0
    


Last edited by Artlav on 05 Aug 2006, 20:44; edited 1 time in total
Post 05 Aug 2006, 13:07
View user's profile Send private message Visit poster's website Reply with quote
Octavio



Joined: 21 Jun 2003
Posts: 366
Location: Spain
Octavio 05 Aug 2006, 13:45
Object file formats like elf relocatable contains the information required
for loading the code at any adress. The Os read the file ,make relocations and jmps to the code, thats all.
Post 05 Aug 2006, 13:45
View user's profile Send private message Visit poster's website Reply with quote
Artlav



Joined: 23 Dec 2004
Posts: 188
Location: Moscow, Russia
Artlav 05 Aug 2006, 14:16
Well, ive already tryed to do it with ELF.
The problem is that

Code:
format  ELF executable
entry   start

section readable executable

use32


start:
jmp begin

;###############################################################################

begin:

mov     esi,msg
mov     eax,111
int     32

ret


msg     db "Hello world! (Aprom PE/AE)",0
    



and the same without mov


Code:
format  ELF executable
entry   start

section readable executable

use32


start:
jmp begin

;###############################################################################

begin:

;mov    esi,msg
nop
nop
nop
nop
nop
mov     eax,111
int     32

ret


msg     db "Hello world! (Aprom PE/AE)",0
    


produces exactly the same ELF file, with only diffirence in place of the mov opcode.

Maybe iam missing something about ELF?
Post 05 Aug 2006, 14:16
View user's profile Send private message Visit poster's website Reply with quote
Dex4u



Joined: 08 Feb 2005
Posts: 1601
Location: web
Dex4u 05 Aug 2006, 19:56
You use a simple relocte able bin file like this:
Code:
org 0use32jmp  start;maybe a simple header herestart:mov eax,[MyVar1 + ebx] ; you need to do thismov [MyVar2 + ebx],edx; same with thismov esi,MyStringadd esi,ebx ;  you need to do thiscall print ; this is ok like thisjmp LetsGo ;this is ok like this; some more code here, maybeLetsGo:         retprint:;print code hereret;DataMyString: db 'hello world!',13,0MyVar1 rd 1MyVar2 rd 1    

This can be loaded any where in memory, you just need to load ebx with the load address at load time and keep ebx untouched.

There is a better way, "0x4e71" came up with for Dex4u OS.
Post 05 Aug 2006, 19:56
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8351
Location: Kraków, Poland
Tomasz Grysztar 05 Aug 2006, 20:30
"format ELF executable" is not an object, is an executable - and it doesn't contains relocations.
Use pure "format ELF" to get relocatable object.

I had once also written (for a Maverick's request) a tool COFF2REL that was converting fasm-produced COFF object into simple flat binary with an auxiliary file containing addresses of relocations. I don't know where is it now, though.
Post 05 Aug 2006, 20:30
View user's profile Send private message Visit poster's website Reply with quote
Artlav



Joined: 23 Dec 2004
Posts: 188
Location: Moscow, Russia
Artlav 05 Aug 2006, 20:33
Heh, that's the kind of code i was testing the OS with, only using
call bg
bg:
pop ebx
in the beginning.

After exausting the ELF manual i got into EXE PE one.
It appears that PE format supports full relocations, fasm can generate it, and it's exe output is quite plain, so no big deal to make exe->aex1 converter.

So, the problem is closed, atlease for now.

Anyway, please clarify "There is a better way, "0x4e71" came up with for Dex4u OS.".
Post 05 Aug 2006, 20:33
View user's profile Send private message Visit poster's website Reply with quote
Artlav



Joined: 23 Dec 2004
Posts: 188
Location: Moscow, Russia
Artlav 05 Aug 2006, 20:40
Like that?
Code:
format  ELF
use32

start:
jmp begin
;###############################################################################
begin:

mov     esi,msg
mov     eax,111
int     32

ret

msg     db "Hello world! (Aprom PE/AE)",0
    


That produces an object file...
Or do you mean something like linking programs at load-time?
Post 05 Aug 2006, 20:40
View user's profile Send private message Visit poster's website Reply with quote
Dex4u



Joined: 08 Feb 2005
Posts: 1601
Location: web
Dex4u 05 Aug 2006, 22:59
Artlav wrote:

Anyway, please clarify "There is a better way, "0x4e71" came up with for Dex4u OS.".

You can get info and test prog here: http://jas2o.forthworks.com/dexforum/index.php?topic=108.30
note: its based on the PE file format.
Post 05 Aug 2006, 22:59
View user's profile Send private message Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3175
Location: Denmark
f0dder 06 Aug 2006, 07:58
I think ELF executables generally aren't relocatable, but that ELF shared libraries generated with GCC/LD use "-fPIC", which produces position-independent code. I dunno if the ELF format supports relocations.

In my own kernel, I use PE executables... they support relocations, and I can use the nice Visual C++ compiler which has decent optimizations.
Post 06 Aug 2006, 07:58
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8351
Location: Kraków, Poland
Tomasz Grysztar 06 Aug 2006, 11:13
For executables you're right (look here for some information on PIC), but the linkable objects always need to be relocatable, and so are the ELF ones.

Artlav: Yes, this is the right way to produce linkable (and thus relocatable) object file. Even the "use32" is actually redundant here. You can also try "format COFF" if that format would be easier for you to handle.

I'll post here if I find my old COFF2REL tool somewhere. Unfortunately the Maverick who perhaps was the only one who had a copy seems to be no longer available (if anyone knows what happened to him, please let me know, BTW).
Post 06 Aug 2006, 11:13
View user's profile Send private message Visit poster's website Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3175
Location: Denmark
f0dder 06 Aug 2006, 18:07
I could try firing off an email to Maverick... I think he got tired of boards and had real life to attend to as well.

Tomasz Grysztar wrote:

but the linkable objects always need to be relocatable, and so are the ELF ones.

Doh yes, of course, I wasn't thinking clearly Smile - I wonder if the ELF *loader* supports relocations, though? Ie, if a shared library could be non-PIC and have relocs applied instead...

I used to think wasting a register for GOT instead of using relocs was a bad idea, but this means somewhat smaller executables. Also, in the case of a DLL that can't be loaded to it's preferred base address, thus needing relocations, those code pages will be dirty and nonshareable - that won't happen with -fPIC code on ELF systems.
Post 06 Aug 2006, 18:07
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8351
Location: Kraków, Poland
Tomasz Grysztar 06 Aug 2006, 18:11
I tried e-mailing him a few times in recent years, but each time it was returned saying the quota on his inbox was exceeded.
Post 06 Aug 2006, 18:11
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8351
Location: Kraków, Poland
Tomasz Grysztar 06 Aug 2006, 20:14
Thanks f0dder, with your help Maverick and I got in touch again. He just sent me a copy of COFF2REL and I'm attaching it here before he's able to post himself.

I think you may find this tool useful. It should be also easy to adapt it to output some your own relocatable format.


Description: The COFF extractor - extracts the code and relocations from COFF .flat section into the flat binary files.
Download
Filename: COFF2REL.RAR
Filesize: 17.69 KB
Downloaded: 217 Time(s)

Post 06 Aug 2006, 20:14
View user's profile Send private message Visit poster's website Reply with quote
Artlav



Joined: 23 Dec 2004
Posts: 188
Location: Moscow, Russia
Artlav 07 Aug 2006, 04:55
Thank you all for help.

Ive currently stopped on PE to aex1 converter as the simpliest solution, but COFF2REL may be of use too.

If anyone is interested, ive attached current version of Aprom PE.


Description: Aprom PEAE (060807)
Download
Filename: ape.img.gz
Filesize: 429.82 KB
Downloaded: 207 Time(s)

Post 07 Aug 2006, 04:55
View user's profile Send private message Visit poster's website Reply with quote
Maverick



Joined: 07 Aug 2006
Posts: 251
Location: Citizen of the Universe
Maverick 07 Aug 2006, 06:54
Hello Wink

_________________
Greets,
Fabio
Post 07 Aug 2006, 06:54
View user's profile Send private message Visit poster's website Reply with quote
pelaillo
Missing in inaction


Joined: 19 Jun 2003
Posts: 878
Location: Colombia
pelaillo 07 Aug 2006, 19:38
Maverick, nice to "see" you again.

I hope you are going to keep in touch helping us here Smile
Post 07 Aug 2006, 19:38
View user's profile Send private message Yahoo Messenger Reply with quote
Maverick



Joined: 07 Aug 2006
Posts: 251
Location: Citizen of the Universe
Maverick 08 Aug 2006, 06:03

¡Hola pelaillo, mi viejo amigo! Smile

I'll keep an eye open, that's for sure, but free time is the most limited resource in the universe right now. ;P

¡Hasta la vista!
--
FabI/O
Post 08 Aug 2006, 06:03
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.