flat assembler
Message board for the users of flat assembler.
Index
> Linux > How to compile a file with extrn? |
Author |
|
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? |
|||
31 Jan 2017, 16:34 |
|
zhak 31 Jan 2017, 18:54
There's an example included in fasm package: libcdemo or elfexe/dynamic
|
|||
31 Jan 2017, 18:54 |
|
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. |
|||
01 Feb 2017, 02:01 |
|
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.
|
|||
01 Feb 2017, 02:10 |
|
jorido 01 Feb 2017, 04:25
Thanks, how about static linking?
|
|||
01 Feb 2017, 04:25 |
|
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) |
|||
01 Feb 2017, 13:52 |
|
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 |
|||
01 Feb 2017, 14:50 |
|
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. |
|||
01 Feb 2017, 15:38 |
|
jorido 01 Feb 2017, 16:05
So why not ld?
|
|||
01 Feb 2017, 16:05 |
|
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? |
|||
01 Feb 2017, 16:19 |
|
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? |
|||
01 Feb 2017, 18:15 |
|
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 |
|||
01 Feb 2017, 18:46 |
|
jorido 01 Feb 2017, 18:58
thanks.
|
|||
01 Feb 2017, 18:58 |
|
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. |
|||
30 Jul 2018, 20:01 |
|
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.
|
|||
30 Jul 2018, 21:15 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.