flat assembler
Message board for the users of flat assembler.

Index > Linux > How to call GTK 3 functions with FASM?

Author
Thread Post new topic Reply to topic
BPS



Joined: 22 Mar 2016
Posts: 3
BPS 22 Mar 2016, 08:58
Hello Smile

I'm currently learning assembler (FASM) for fun, so I started to rewrite some of my C programs.

I have program which uses GTK, it creates status icon:

main.c
=====
Code:
...
int main(void)
{
gtk_init(0, 0);
// Status icon
GtkStatusIcon *status_icon = gtk_status_icon_new_from_icon_name("exit");
g_signal_connect(status_icon, "button-press-event", G_CALLBACK(on_button_press), 0);

// main loop
gtk_main();
return 0;
}
    


and now I'm trying to translate above to assembler. Right now I got this:

main.asm
========
Code:
...
main:
push 0
push 0
call gtk_init
add esp, 8

push status_icon_stock
call gtk_status_icon_new_from_icon_name
add esp, 4
mov eax, [status_icon]

push 0
push 0
push 0
push on_button_press
push button_press_event
push [status_icon]
call g_signal_connect_data
add esp, 24

call gtk_main

status_icon dd 0
exit_dialog dd 0
vbox dd 0
exit_button dd 0
status_icon_stock db 'exit', 0
clicked_str db 'clicked', 0
application_exit_stock db 'application-exit', 0
button_press_event db 'button-press-event', 0
    


But when I try to run it I get error saying that status_icon is NULL.

Why my C code is working and assembly is not? Am I missing something? mov eax, [status_icon] does not put valid pointer in status_code variable?

Full code can be viewed here: https://github.com/4544fa8d/i3-exit

thanks Smile
Post 22 Mar 2016, 08:58
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20486
Location: In your JS exploiting you and your system
revolution 22 Mar 2016, 09:23
I think you forgot to save the handle:
Code:
;...
call gtk_status_icon_new_from_icon_name
mov [status_icon],eax ;<--- save the handle here
add esp, 4
;...    
Post 22 Mar 2016, 09:23
View user's profile Send private message Visit poster's website Reply with quote
BPS



Joined: 22 Mar 2016
Posts: 3
BPS 22 Mar 2016, 15:26
Och, I switched places of source and destination, I don't know how could have I missed it Very Happy.

Thanks

Now I moved forward and have another error:

Code:
on_button_press:
        ...
        cmp eax, GTK_RESPONSE_CLOSE
        jne return

        mov eax, 11             ;sys_execve
        mov ebx, dbus_filename
        mov ecx, dbus_shutdown_args
        mov edx, 0
        int 80h

        call gtk_main_quit

        return:
                push [exit_dialog]
                call gtk_widget_destroy
                add esp, 4
                ; int 03h
                ret             ; <- Segmentation fault (core dumped)
                int 03h

main:
        ...
        push 0
        push 0
        push 0
        push on_button_press
        push button_press_event
        push [status_icon]
        call g_signal_connect_data
        add esp, 24

        call gtk_main
    


Breakpoint show that "segmentation fault" on ret instruction in my "on_button" function.
How can I return from on_button function?

Full updated code is at: https://github.com/4544fa8d/i3-exit

thanks Smile
Post 22 Mar 2016, 15:26
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20486
Location: In your JS exploiting you and your system
revolution 23 Mar 2016, 05:14
So I expect you have some stack corruption in there somewhere. You didn't show all the code* so I can only guess.

* I see the github link. Not going there, sorry.
Post 23 Mar 2016, 05:14
View user's profile Send private message Visit poster's website Reply with quote
BPS



Joined: 22 Mar 2016
Posts: 3
BPS 23 Mar 2016, 10:22
Laughing

Code:
format ELF

extrn gtk_init
extrn gtk_main
extrn gtk_button_set_always_show_image
extrn gtk_status_icon_new_from_icon_name
extrn gtk_window_set_default_size
extrn gtk_image_new_from_icon_name
extrn gtk_dialog_run
extrn gtk_main_quit
extrn gtk_button_set_image
extrn gtk_widget_destroy
extrn gtk_widget_show_all
extrn gtk_dialog_response
extrn gtk_button_new
extrn gtk_dialog_get_content_area
extrn gtk_dialog_new
extrn gtk_box_pack_start
extrn g_signal_connect_data

sys_execve = 11

GTK_RESPONSE_CLOSE = -7
GTK_ICON_SIZE_DIALOG = 6

public main

on_exit_button:
        push GTK_RESPONSE_CLOSE
        push [exit_dialog]
        call gtk_dialog_response
        add esp, 8
        ret

on_button_press:
        call gtk_dialog_new
        mov [exit_dialog], eax

        push [exit_dialog]
        call gtk_dialog_get_content_area
        add esp, 4
        mov [vbox], eax

        push 300
        push 300
        push [exit_dialog]
        call gtk_window_set_default_size
        add esp, 12

        call gtk_button_new
        mov [exit_button], eax

        push 1
        push [exit_button]
        call gtk_button_set_always_show_image
        add esp, 8

        push GTK_ICON_SIZE_DIALOG
        push application_exit_stock
        call gtk_image_new_from_icon_name
        add esp, 8

        push eax
        push [exit_button]
        call gtk_button_set_image
        add esp, 8

        push 0
        push 0
        push 0
        push on_exit_button
        push clicked_str
        push [exit_button]
        call g_signal_connect_data
        add esp, 24

        push 5
        push 1
        push 1
        push [exit_button]
        push [vbox]
        call gtk_box_pack_start
        add esp, 20

        push [vbox]
        call gtk_widget_show_all
        add esp, 4

        push [exit_dialog]
        call gtk_dialog_run
        add esp, 4

        cmp eax, GTK_RESPONSE_CLOSE
        jne return

        mov eax, 11             ;sys_execve
        mov ebx, dbus_filename
        mov ecx, dbus_shutdown_args
        mov edx, 0
        int 80h

        call gtk_main_quit

        return:
                push [exit_dialog]
                call gtk_widget_destroy
                add esp, 4
                ; int 03h
                ret             ; <- Segmentation fault (core dumped)
                int 03h

main:
        push 0
        push 0
        call gtk_init
        add esp, 8

        push status_icon_stock
        call gtk_status_icon_new_from_icon_name
        add esp, 4
        mov [status_icon], eax

        push 0
        push 0
        push 0
        push on_button_press
        push button_press_event
        push [status_icon]
        call g_signal_connect_data
        add esp, 24

        call gtk_main

status_icon dd 0
exit_dialog dd 0
vbox dd 0
exit_button dd 0
status_icon_stock db 'exit', 0
clicked_str db 'clicked', 0
application_exit_stock db 'application-exit', 0
button_press_event db 'button-press-event', 0
dbus_filename db 'dbus-send', 0
dbus_shutdown_args db '--system --print-reply --dest=\"org.freedesktop.ConsoleKit\" /org/freedesktop/ConsoleKit/Manager org.freedesktop.ConsoleKit.Manager.Stop', 0

    
Post 23 Mar 2016, 10:22
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20486
Location: In your JS exploiting you and your system
revolution 25 Mar 2016, 04:51
At first glance there does not appear to be anything obvious wrong.

You can try a few things though. Check that the stack pointer the same upon entry and just before the ret. And/or, simplify the entire procedure to just a ret (or maybe some minimal instructions) and see if that exhibits the same problem.
Post 25 Mar 2016, 04:51
View user's profile Send private message Visit poster's website Reply with quote
petelomax



Joined: 19 Jan 2013
Posts: 11
Location: London
petelomax 07 Apr 2016, 18:12
hasty post deleted [after posting I saw dbus_shudown_args had already been fixed]
Post 07 Apr 2016, 18:12
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-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.