flat assembler
Message board for the users of flat assembler.

flat assembler > Linux > IS there will be support for x86_x32

Author
Thread Post new topic Reply to topic
Melissa



Joined: 12 Apr 2012
Posts: 60
Linux since kernel 3.4 has support for 32 bit elf with 64 bit instructions
called x86_x32.

gcc has parameter -mx32 and gas --x32 for that format.
Is there will be similar thing for fasm?
Post 02 Feb 2014, 19:25
View user's profile Send private message Reply with quote
gens



Joined: 18 Feb 2013
Posts: 159
afaik x32 is just an ABI

http://lwn.net/Articles/456731/
and the project site https://sites.google.com/site/x32abi/

didnt look much at it, but it seems like its just 32bit pointers (and structs and connected things) in 64bit mode

i also looked at my local kernel source... well just checked quick
and cant find x32 syscall list, but it should be there somewhere (again, didnt look hard)

ofc, you can use 32bit calls to the kernel no problem
and to shared lib's, with a little problem (linking)
so as i understand it should not be a problem

(thinking about it maybe general memory access is bit different, but i know only to do 64bit so:))
Post 03 Feb 2014, 16:22
View user's profile Send private message Reply with quote
Melissa



Joined: 12 Apr 2012
Posts: 60
No it's not just an aBI. It's distinct elf file type,
and wont link with 64 bit elfs. Also there are versions of libraries
specific to this format which are linked when used -mx32 flag.
File type is recognized as :
ELF 32-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 3.4.0,
Post 04 Feb 2014, 23:02
View user's profile Send private message Reply with quote
gens



Joined: 18 Feb 2013
Posts: 159
Post 05 Feb 2014, 21:10
View user's profile Send private message Reply with quote
HaHaAnonymous



Joined: 02 Dec 2012
Posts: 1181
Location: Unknown
Quote:

Linux since kernel 3.4 has support for 32 bit elf with 64 bit instructions

I do not know what is the point... because:
1.32bit only machines will not run 64bit code.
2.If this is to be run on 32bit OS and the machine supports 64bit then it does not make sense to install a 32bit OS to run 64bit code.

Seems confusing for now.

But it works if you want to run 32bit code in 64bit mode, you can alternate between 32/64bit code as you wish:
Code:
format ELF64 executable 3

entry woof

use32

align 16
woof:
          mov          eax,$04
          mov          ebx,$01
          mov          ecx,str0
          mov          edx,_str0
          int          $80
use64
          and          edi,$00
          mov          eax,$3C
          syscall
          ; force exception on modified kernels that returns after exit (i.e. fake exit)
          ud2

segment readable

align 8
str0:
        db 'Woof, woof! :D',$0A
_str0 = $-str0
    
Post 22 Mar 2015, 20:02
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 16508
Location: M87*
A working example of x32
Code:
USE_LIBC        = 1
IS_DYNAMIC      = 1

if IS_DYNAMIC
        format ELF dynamic 3 at 1 shl 32 - 0x10000
else
        format ELF executable 3 at 1 shl 32 - 0x10000
end if

entry start

segment gnustack
segment executable

use64

start:
        lea     edi,[maps]
        mov     esi,O_RDONLY
        xor     edx,edx
if USE_LIBC
        call    [open]
else
        mov     eax,SYS_OPEN
        syscall
end if
        mov     edx,0x1000
        sub     esp,edx
        mov     edi,eax
        mov     eax,SYS_READ
        mov     esi,esp
        syscall
        mov     edx,eax
        mov     eax,SYS_WRITE
        mov     edi,STD_OUTPUT
        mov     esi,esp
        syscall
        mov     eax,SYS_EXIT
        xor     edi,edi
        syscall

SYS_x32         = 0x40000000

SYS_READ        = SYS_x32 or 0
SYS_WRITE       = SYS_x32 or 1
SYS_OPEN        = SYS_x32 or 2
SYS_EXIT        = SYS_x32 or 60
STD_INPUT       = 0
STD_OUTPUT      = 1
O_RDONLY        = 0
PROT_READ       = 0x1

DT_NULL         = 0
DT_NEEDED       = 1
DT_STRTAB       = 5
DT_SYMTAB       = 6
DT_RELA         = 7
DT_RELASZ       = 8
DT_RELAENT      = 9
DT_STRSZ        = 10
DT_SYMENT       = 11
DT_BIND_NOW     = 24
DT_FLAGS        = 30
DT_FLAGS_1      = 0x6ffffffb
STB_GLOBAL      = 1
STT_FUNC        = 2
R_386_32        = 1
DF_BIND_NOW     = 0x00000008
DF_1_NOW        = 0x00000001
DF_1_PIE        = 0x08000000

macro Elf32_Sym name,value,size,bind,type,other,shndx {
        dd name+0
        dd value+0
        dd size+0
        db (bind+0) shl 4 + (type+0)
        db other+0
        dw shndx+0
}
macro Elf32_Rela offset,symbol,type,addend {
        dd offset+0
        dd (symbol+0) shl 8 + (type+0)
        dd addend+0
}
virtual at 0
        Elf32_Sym
        sizeof.Elf32_Sym = $
        Elf32_Rela
        sizeof.Elf32_Rela = $ - sizeof.Elf32_Sym
end virtual

if USE_LIBC
        segment interpreter readable
                        db '/libx32/ld-linux-x32.so.2'
                strtab:
                        db 0
                _libc   db 'libc.so.6',0
                _open   db 'open',0
                strsz   = $ - strtab

        maps:   db      '/proc/self/maps',0

        segment dynamic readable
                dd DT_NEEDED,_libc - strtab
                dd DT_STRTAB,rva strtab
                dd DT_STRSZ,strsz
                dd DT_SYMTAB,rva symtab
                dd DT_SYMENT,sizeof.Elf32_Sym
                dd DT_RELA,rva rela
                dd DT_RELASZ,relasz
                dd DT_RELAENT,sizeof.Elf32_Rela
                dd DT_BIND_NOW,1
                dd DT_FLAGS,DF_BIND_NOW
                dd DT_FLAGS_1,DF_1_NOW or DF_1_PIE
                dd DT_NULL,0
                symtab:
                        Elf32_Sym
                        Elf32_Sym _open - strtab,0,0,STB_GLOBAL,STT_FUNC,0,0
                rela:
                        Elf32_Rela open,1,R_386_32,0
                relasz  = $ - rela
                open dq 0
        segment readable writeable
else
        segment readable
        maps:   db      '/proc/self/maps',0
end if    
It needs to be patched to run correctly. Byte 0x12 changed to 0x3e to make it X86_64 machine type. Something like this will do
Code:
printf "\x3e" | dd of=<filename> obs=1 seek=18 count=1 conv=notrunc    
It has to use type RELA relocations for linking. And each dynamic link must be 64-bits wide (see the "open" link) due to the limitation of call in 64-bit mode.


Last edited by revolution on 20 Feb 2019, 21:55; edited 2 times in total
Post 20 Feb 2019, 09:08
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar
Assembly Artist


Joined: 16 Jun 2003
Posts: 7235
Location: Kraków, Poland
Note that my official x32 examples (fasmg only) are here: https://github.com/tgrysztar/fasmg/tree/master/packages/x86/examples/linux/x32
Post 20 Feb 2019, 09:55
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 16508
Location: M87*
HaHaAnonymous wrote:
I do not know what is the point... because:
1.32bit only machines will not run 64bit code.
2.If this is to be run on 32bit OS and the machine supports 64bit then it does not make sense to install a 32bit OS to run 64bit code.
It's meant to run on a 64-bit machine and a 64-bit OS, and permit using smaller address sizes to improve resource usage efficiency.

According to WP
Quote:
Though the x32 ABI limits the program to a virtual address space of 4 GiB, it also decreases the memory footprint of the program by making pointers smaller. This can allow it to run faster by fitting more data into cache. The best results during testing were with the 181.mcf SPEC CPU 2000 benchmark, in which the x32 ABI version was 40% faster than the x86-64 version. On average, x32 is 5–8% faster on the SPEC CPU integer benchmarks compared to x86-64. There is no speed advantage over x86-64 in the SPEC CPU floating-point benchmarks.
Post 20 Feb 2019, 11:34
View user's profile Send private message Visit poster's website Reply with quote
guignol



Joined: 06 Dec 2008
Posts: 472
And the tests on power usage?
Post 20 Feb 2019, 21:43
View user's profile Send private message 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-2019, Tomasz Grysztar.

Powered by rwasa.