flat assembler
Message board for the users of flat assembler.

Index > Linux > Import 64bit functions from 32bit program?

Author
Thread Post new topic Reply to topic
JohnFound



Joined: 16 Jun 2003
Posts: 3500
Location: Bulgaria
JohnFound
Is it possible to dynamically link 32bit program to 64bit shared libraries in Linux64?
Of course the code that calls imported functions should be 64bit. But the other code of the program is 32bit.
So the question is also "Is it possible to mix 32 and 64 bit code in one program?

_________________
Tox ID: 48C0321ADDB2FE5F644BB5E3D58B0D58C35E5BCBC81D7CD333633FEDF1047914A534256478D9
Post 30 Sep 2012, 07:21
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17287
Location: In your JS exploiting you and your system
revolution
It is possible, but you need support from the OS to switch modes. Also be aware that the addressing would likely need to all be below the 4G limit and the page tables would need to match. I have no idea if Linux supports such a scheme as this:

http://board.flatassembler.net/topic.php?p=140791#140791
Post 30 Sep 2012, 08:14
View user's profile Send private message Visit poster's website Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3500
Location: Bulgaria
JohnFound
Maybe I have to explain more detailed what I actually need. It is known that 64bit Linux can run 32 bit applications natively but only if they does not use shared libraries. 32bit system calls are kernel supported.
There is a 32bit compatibility package that contains all needed 32bit libraries but it have to be installed. Otherwise, LD linker will not be able to find needed libraries and will fail to load application.

On the other hand, in FreshLib, because of its architecture, I have wrapper functions for all needed functions imported from the shared libraries.
So, I can make Linux64 layer that to import directly from 64bit libraries and this way to allow FreshLib applications to run on 64bit Linux without installing 32bit compatibility package.

Another way is to implement all needed functionality using only system calls, what is possible, but not practical in this moment.
Post 30 Sep 2012, 09:09
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17287
Location: In your JS exploiting you and your system
revolution
How would your asm file look? Like this?
Code:
use64

start64:
    ;wrapper code for 64-bit shared library access
      jmp     seg32:start32

shared_library_func_1:
 call    shared_library_func
 retf

finished:
   mov     rax,something
       int     0x80

use32

start32:
       call    seg64:shared_library_func_1
     ;...
        call    seg64:finished    
Post 30 Sep 2012, 11:21
View user's profile Send private message Visit poster's website Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3500
Location: Bulgaria
JohnFound
Well, I have no experience in 64 bit programming. Maybe something like:
Code:
use32

proc GetMem, .size
begin
; switch to 64bit , call libc64 "alloc"
   use64
;something more???

   invoke64 alloc, [.size]

   use32
; switch back to 32bit and return the address in 32bit (if possible)
   return
endp


start:
    stdcall GetMem, 1024
    
    stdcall FreeMem, eax
    stdcall Terminate, 0    
Post 30 Sep 2012, 12:28
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17287
Location: In your JS exploiting you and your system
revolution
You have to get cooperation from the OS to switch operating mode. You can't do it only with user mode code. You have to either change the current segment descriptor or select another descriptor that is already prepared.

Also, if you allocate memory in 64-bit mode then how do you ensure it is accessible in 32-bit mode? The memory regions might be very different.
Post 30 Sep 2012, 12:45
View user's profile Send private message Visit poster's website Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3500
Location: Bulgaria
JohnFound
Yes, I am sure that this is what I have to do. Maybe Linux64 has some syscall about changing modes?
Post 30 Sep 2012, 12:54
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
Sorry I can´t give a thorough look into this so this link may not be helpful: http://en.wikipedia.org/wiki/X32_ABI (also notice that perhaps this is not here yet)
Post 30 Sep 2012, 13:06
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17287
Location: In your JS exploiting you and your system
revolution
LocoDelAssembly wrote:
Sorry I can´t give a thorough look into this so this link may not be helpful: http://en.wikipedia.org/wiki/X32_ABI (also notice that perhaps this is not here yet)
That is still running 64-bit code but appears to be using a coding method to reduce code-size/instruction-count if it can be assumed that all pointers and offsets are <=32 bits. This is probably something we do automatically as assembly programmers anyhow so there is no benefit to us.
Post 30 Sep 2012, 13:24
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-2020, Tomasz Grysztar. Also on YouTube, Twitter.

Website powered by rwasa.