flat assembler
Message board for the users of flat assembler.

Index > Main > link two object files(in diff lang), error on execute

Author
Thread Post new topic Reply to topic
othername



Joined: 08 Apr 2005
Posts: 26
Location: Lithuania
othername 15 Apr 2005, 22:25
hi, have a problem. First file(in asm) calls function writen in C(second).
I compile them like an objects, then link them:
-----------
fasm first.asm
gcc -c second.c
ld first.o second.o -o outputfile -lc
---------------
what is the problem, when i execute outputfile i have a message "no such file"?

Code:
format ELF
public _start
section '.text' executable 
_start:
extrn hello
    push        message
    call        hello
    mov         eax,1
    xor         ebx,ebx
    int         0x80
section '.data' writeable
message db "hello world", 0xA, 0

    

and second file in C(sorry for that)
Code:
#include <stdio.h>
int hello(const char* mesg)
{
    char *msg = (char *)mesg;
    for(; *msg!='0'; msg++)
        write(1, msg, 1);
    return 1
}
    

_________________
Sorry for my bad English Sad
Post 15 Apr 2005, 22:25
View user's profile Send private message Reply with quote
crc



Joined: 21 Jun 2003
Posts: 637
Location: Penndel, PA [USA]
crc 16 Apr 2005, 17:48
Try this:

Code:
format ELF
public main
section '.text' executable
main:
extrn hello
    push        message
    call        hello
    mov         eax,1
    xor         ebx,ebx
    int         0x80
section '.data' writeable
message db "hello world", 0xA, 0    


and link them by doing:

Code:
gcc first.o second.o -o outputfile    


Just using 'ld' doesn't get the C runtime stuff in place; without that Linux can't properly identify and load the C library.
Post 16 Apr 2005, 17:48
View user's profile Send private message Visit poster's website Reply with quote
othername



Joined: 08 Apr 2005
Posts: 26
Location: Lithuania
othername 16 Apr 2005, 19:09
Works!
But why it must be main insted _start?

_________________
Sorry for my bad English Sad
Post 16 Apr 2005, 19:09
View user's profile Send private message Reply with quote
crc



Joined: 21 Jun 2003
Posts: 637
Location: Penndel, PA [USA]
crc 16 Apr 2005, 22:52
The C runtime header provides _start, which sets up argc/argv among other things. The provided _start then jumps to main, which is the main entry point for the program in a C-based world.
Post 16 Apr 2005, 22:52
View user's profile Send private message Visit poster's website Reply with quote
Endre



Joined: 29 Dec 2003
Posts: 215
Location: Budapest, Hungary
Endre 17 Apr 2005, 11:17
Just one more thing: when a function returns it does not destroy the argument space you have created with push on the stack before calling to it. So you have to do it manually:
Code:
    push    message
    call    hello
    add     esp, 4 ; restore the stack - practically pop message but more general
                   ; even in case of multiple arguments
    mov     ebx, eax ; use return value for exit value
    mov     eax, 1 ; exit follows
    int     0x80    

But you can also exploit this behavior:
Code:
    push    message
    call    hello ; message on the stack
    call    hello ; message still on the stack
    add     esp, 4 ; restore the stack
    mov     ebx, eax ; use return value for exit value
    mov     eax, 1 ; exit follows
    int     0x80    
Post 17 Apr 2005, 11:17
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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.