flat assembler
Message board for the users of flat assembler.
 Home   FAQ   Search   Register 
 Profile   Log in to check your private messages   Log in 
flat assembler > Linux > How to compile a file with extrn?

Author
Thread Post new topic Reply to topic
jorido



Joined: 23 Jan 2017
Posts: 35
How to compile a file with extrn?
Say, I want to use this http://man7.org/linux/man-pages/man3/socket.3p.html or this http://man7.org/linux/man-pages/man3/getaliasent_r.3.html function or both:


Code:

extrn socket
extrn getaliasent_r






How should I compile and link a source assembly file in this case?

And how do I know where those functions are defined so that I'll be able to link those files to my assembly file?
Post 31 Jan 2017, 16:34
View user's profile Send private message Reply with quote
zhak



Joined: 12 Apr 2005
Posts: 473
Location: Belarus
There's an example included in fasm package: libcdemo or elfexe/dynamic
Post 31 Jan 2017, 18:54
View user's profile Send private message Reply with quote
jorido



Joined: 23 Jan 2017
Posts: 35
This?

Code:

interpreter '/lib64/ld-linux-x86-64.so.2'
needed 'libc.so.6'
import printf,exit




But where is "extrn"? And what if my file format isn't "executable 3" either?
Post 31 Jan 2017, 21:47
View user's profile Send private message Reply with quote
system error



Joined: 01 Sep 2013
Posts: 477
"extrn" is for static linking
"import" is for dynamic linking

dynamic linking results in direct executable. Linking is done internally by FASM. Thus, "executable 3" is required.


Code:
format elf64 executable 3
include 'import64.inc'
entry main


interpreter '/lib64/ld-linux-x86-64.so.2' 
needed 'libc.so.6' 
import printf,exit


segment readable writeable
hello db 'Hello World',0ah,0


segment readable executable
main:

        mov rdi,hello
        call [printf]       ;dynamic linking to printf in memory


        mov rdi,0
        call [exit]



Didn't test it, but should work.
Post 01 Feb 2017, 02:01
View user's profile Send private message Reply with quote
system error



Joined: 01 Sep 2013
Posts: 477
I've read your other posts and I take it that you come from NASM background. Well, this is one area where NASM is not familiar with - dynamic linking. Get used to it and welcome to FASM.
Post 01 Feb 2017, 02:10
View user's profile Send private message Reply with quote
jorido



Joined: 23 Jan 2017
Posts: 35
Thanks, how about static linking?
Post 01 Feb 2017, 04:25
View user's profile Send private message Reply with quote
system error



Joined: 01 Sep 2013
Posts: 477
For static linking, the step is the same as with NASM. If you're looking for source comparison, here's the static version

Code:
format ELF64
public main

extrn printf

section '.data' writeable
hello db 'hello world',0ah,0

section '.code' executable
main:

        mov     rdi,hello
        call    printf
        ret

;link against: gcc -m64 yourfile.o -o yourfile



I didn't test this bcoz I don't have linux at this moment, but the code should work. Even if it doesn't, just pay attention to the structural differences (section, format, public etc)
Post 01 Feb 2017, 13:52
View user's profile Send private message Reply with quote
jorido



Joined: 23 Jan 2017
Posts: 35
Why not ld for linking?

Is this a correct way?

Code:

ld hello.o -dynamic-linker /lib64/ld-linux-x86-64.so.2 -lc -melf_x86_64 


Post 01 Feb 2017, 14:50
View user's profile Send private message Reply with quote
system error



Joined: 01 Sep 2013
Posts: 477
jorido

if you're linking to dynamic library, just use the first example.

if you're linking to static library, you can use pretty much anything you know of using.

asking people about static and extrn thingies and yet using --dynamic-linker switch is not obviously smart from source structure point of view, because dynamic and static sources are different at the source code level. (segment vs section, ELF64 vs ELF64 executable 3, public vs entry etc).

If you trying to force NASM's way of doing things upon FASM, then you'll not learn anything new today.
Post 01 Feb 2017, 15:38
View user's profile Send private message Reply with quote
jorido



Joined: 23 Jan 2017
Posts: 35
So why not ld?
Post 01 Feb 2017, 16:05
View user's profile Send private message Reply with quote
system error



Joined: 01 Sep 2013
Posts: 477
@jorido

If I use ld, then I cannot use "format ELF64 executable 3".

I don't need "ld" to create an executable directly via FASM to access "libc.so.6". Why should I?
Post 01 Feb 2017, 16:19
View user's profile Send private message Reply with quote
jorido



Joined: 23 Jan 2017
Posts: 35
Alright.
What if there were other "so" library, would you still be able to use gcc?

Also:
include 'import64.inc' error: file not found.

Where should I get it?
Post 01 Feb 2017, 18:15
View user's profile Send private message Reply with quote
system error



Joined: 01 Sep 2013
Posts: 477
import64.inc is in the /examples/elfexe/dynamic/

you'd probably need "elf.inc" as well. Just make sure you have these two available in a the same path every time you're compiling.

In the examples folder, there are also examples of using both ld and gcc if you wished to perform static linking instead (like you always do in NASM).

Good luck
Post 01 Feb 2017, 18:46
View user's profile Send private message Reply with quote
jorido



Joined: 23 Jan 2017
Posts: 35
thanks.
Post 01 Feb 2017, 18:58
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


Powered by phpBB © 2001-2005 phpBB Group.

Main index   Download   Documentation   Examples   Message board
Copyright © 2004-2016, Tomasz Grysztar.