flat assembler
Message board for the users of flat assembler.

Index > Linux > How to compile a file with extrn?

Author
Thread Post new topic Reply to topic
jorido



Joined: 23 Jan 2017
Posts: 53
jorido 31 Jan 2017, 16:34
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: 501
Location: Belarus
zhak 31 Jan 2017, 18:54
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: 53
jorido 31 Jan 2017, 21:47
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: 670
system error 01 Feb 2017, 02:01
"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: 670
system error 01 Feb 2017, 02:10
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: 53
jorido 01 Feb 2017, 04:25
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: 670
system error 01 Feb 2017, 13:52
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: 53
jorido 01 Feb 2017, 14:50
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: 670
system error 01 Feb 2017, 15:38
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: 53
jorido 01 Feb 2017, 16:05
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: 670
system error 01 Feb 2017, 16:19
@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: 53
jorido 01 Feb 2017, 18:15
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: 670
system error 01 Feb 2017, 18:46
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: 53
jorido 01 Feb 2017, 18:58
thanks.
Post 01 Feb 2017, 18:58
View user's profile Send private message Reply with quote
moveax41h



Joined: 18 Feb 2018
Posts: 59
moveax41h 30 Jul 2018, 20:01
I got a question here for Linux. I'm getting an error from fasm saying this is not a legal instruction:
Code:
interpreter '/lib64/ld-linux-x86-64.so.2' 
needed 'libc.so.6' 
    


On Windows, I used to do something like
Code:
library 'kernel32.dll',
import 'printf', \
'scanf'
    

etc

I want to dynamically link those libc functions in on Linux.
Post 30 Jul 2018, 20:01
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20425
Location: In your JS exploiting you and your system
revolution 30 Jul 2018, 21:15
"interpreter" and "needed" are macros, not instructions or directives. You will need to have some other macro file included to process those two lines.
Post 30 Jul 2018, 21:15
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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.