flat assembler
Message board for the users of flat assembler.

flat assembler > Compiler Internals > Segmentation fault question

Author
Thread Post new topic Reply to topic
Azagaros



Joined: 18 Jan 2013
Posts: 26
Code:
format ELF64
section '.data'

strUsage: 
    db 'program filename -o filename', 0x00



section '.text' executable

extrn 'puts' as _puts
puts = PLT _puts

public main
main:
    ; rdi is count of arguments
    ; rsi **arg
    dec rdi
    jz .usageInfo
    mov rbx, 0x01
 
    
.getNextArg:
    ; save the registers 
    push rdi
    push rsi
    push rbx
    sub rsp, 24
    
    mov rdx, [rsi+rbx*8]    ;move current argument into rdx
    mov al, byte[rdx]       ;move first two to ax and switch option pointed at
;    cmp al, '-'  ; this is where the segmentation fault occurs.. which means it is not cleanly formed
;    je ProcessArg  ;it is the jump not the compare after further checking...
    cmp byte[rdx], 0x00
    je .toNextArg
    mov rdi, rdx
    call puts
    
    add rsp, 24
    pop rbx
    pop rsi
    pop rdi
    
;    add rsi, 0x08
.toNextArg:
    inc rbx
    dec rdi
    jnz .getNextArg
    jmp .exit
  
.usageInfo:
    push rdi
    push rsi
    sub rsp, 16
    
    mov rdi, strUsage
    call puts
    
    add rsp, 16
    pop rsi
    pop rdi
    
.exit:
    xor rax, rax
    ret

ProcessArg:
    push rcx
    push rbx
    push rdi
    push rsi
    sub rsp, 32
    
    mov ax, word[rdx]
    mov di, ax
    call puts
    
    add rsp, 32
    pop rsi
    pop rdi
    pop rbx
    pop rcx
    ret
    


I know the code compiles. It is 64 bit Linux and that compare "cmp al, '-'" is the segmentation fault that I have traced back to. Something is not right in the code generation and everything I have read says I have put in a legal set of commands for 64 bit.


Last edited by Azagaros on 27 Jun 2018, 12:18; edited 3 times in total
Post 27 Jun 2018, 10:51
View user's profile Send private message ICQ Number Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 15984
Location: Qo'noS
You probably need to keep the stack aligned to a multiple of 8 byte.
Code:
    sub rsp, 0x0C  ;<--- not a multiple of 8 bytes    
Also the first "je .toNextArg" leaves the stack unbalanced with no matching "add rsp,0xc"
Post 27 Jun 2018, 11:05
View user's profile Send private message Visit poster's website Reply with quote
Azagaros



Joined: 18 Jan 2013
Posts: 26
It was a Gnu assembler example, that I tried sample code from and it worked. That example used register size for 8 byte registers was 4, which I found odd.

i have changed the 12 to 24 and still no change.
Post 27 Jun 2018, 11:14
View user's profile Send private message ICQ Number Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 15984
Location: Qo'noS
You still have an unbalanced stack from the first "je .toNextArg". You adjust the stack downwards and then never adjust it back up when the jump is made.

Also, I'm not clear on why you adjust the stack like that. It isn't fastcall or any normal call method for 64-bit code.
Post 27 Jun 2018, 12:02
View user's profile Send private message Visit poster's website Reply with quote
Azagaros



Joined: 18 Jan 2013
Posts: 26
I am starting to find you an idiot at this point. You do not not remove the comments on the offending code. It is the call to the function outside the main function that is the issue and not the local jump. I remove the jump to the function and it works as expected. In other words you are not running the code. 3*8 is 24 last time checked, if all general registers are 8 bytes wide. The code I am testing from has nothing in the function but calling label and return statement... It is not making the call to the function.
Post 27 Jun 2018, 12:24
View user's profile Send private message ICQ Number 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-2018, Tomasz Grysztar.

Powered by rwasa.