flat assembler
Message board for the users of flat assembler.

Index > Linux > using fasm under linux 64-bit

Thread Post new topic Reply to topic
system error

Joined: 01 Sep 2013
Posts: 670
system error 11 Oct 2014, 17:07
I just bought a pc with linux mint 17 (64-bit) installed. I am familiar with linux but not with linux 32/64-bit with fasm. So i decided to give it a try. here are some questions;

1. how come this instruction "sub rsp,8" leads to 16 byte-alignment? I don't quite get it. What if I have no locals to push? Do I still need to align it to 16-byte? I heard this thing that we need to align our procedures to 16-byte in 64-bit programming.

2. Where's FASM 64-bit procs for linux? I saw proc32 in the dynamic folder/example but no 64-bit version. Frankly I never used proc64 on windows either (if such thing ever exists) Razz

3. Why do we need these 2 lines;
interpreter '/lib64/ld-linux-x86-64.so.2'
needed 'libc.so.6'

I think I have more questions but maybe later. I am busy exploring my first ever 64-bit toy Very Happy
Post 11 Oct 2014, 17:07
View user's profile Send private message Reply with quote

Joined: 09 Oct 2009
Posts: 394
Location: Australia
redsock 11 Oct 2014, 20:34
Your question #1 doesn't really make sense, rsp is basically just another general purpose register, so its value after a "sub rsp, 8" depends on what it was beforehand Smile

As for your second and third question, see the below example. Compile with: fasm example.asm && ld -o example example.o

Then run with: gdb ./example

        format ELF64

        db 1, 2, 3              ; intentional misalignment
public maligned
        push    rbp
        mov     rbp, rsp        ; framepointers to make gdb happy
        int3                    ; breakpoint

        leave                   ; restore stackframe

public _start
        sub     rsp, 3          ; intentionally misalign the stack

        int3                    ; breakpoint

        call    maligned        ; jump off into misaligned function

        mov     eax, 60         ; exit
        xor     edi, edi        ; return code

Hope that helps Smile you'll note that doing an: nm example

will show that both maligned and _start functions are both not aligned by 16.
Post 11 Oct 2014, 20:34
View user's profile Send private message Reply with quote

Joined: 18 Feb 2013
Posts: 161
gens 11 Oct 2014, 21:28
2. i made myself a macro
macro fcall routine, op1, op2, op3, op4, op5, op6 {
if op6 eq
else if op6 eqtype rax+3
lea r9, [op6]
mov r9, op6
end if

if op5 eq
else if op5 eqtype rax+3
lea r8, [op5]
mov r8, op5
end if

if op4 eq
else if op4 eqtype rax+3
lea rcx, [op4]
mov rcx, op4
end if

if op3 eq
else if op3 eqtype rax+3
lea rdx, [op3]
mov rdx, op3
end if

if op2 eq
else if op2 eq rsi
else if op2 eqtype rax+3
lea rsi, [op2]
mov rsi, op2
end if

if op1 eq
else if op1 eqtype rax+3
lea rdi, [op1]
mov rdi, op1
end if

call [routine]

it's not perfect by any stretch
it doesn't do floats, that in the amd64 abi go in xmm registers
it doesn't do more then 6 parameters ( every over 6 should go on the stack )
and if you do something like "fcall call, rsi, rdi" it will first put mov rsi, rdi then mov rdi, rsi making them bout what was in rdi

the first 2 things i could easily put it, but the 3'rd problem gets complicated
note that the kernel calling convention is slightly different

edit: on more thing
if you do "fcall call, rdi" it will make it mov rdi, rdi
i should put that in too

3. is the info too put in the elf header
interpreter (linker/loader) tells what to use to fill the... forgot what the section is called, anyway it's for shared library call addresses
needed tells the the linker what shared libraries to check for calls
import tells what functions to declare in the section that i cant remember the name of

you can only have one import declaration, so use \ to skip newlines Smile
Post 11 Oct 2014, 21:28
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-2023, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.