Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to export symbols in NASM

My function is not exported by NASM assembler and therefore I can not link it with my C program. I am using the export directive like the manual says, but it is not recognized. What is wrong? Here is how I do it:

[niko@dev1 test]$ cat ssefuncs.S 
use64
section .data
NEW_LINE_4_SSE  db  '1111111111111111'

section .text

export find_nl_sse

find_nl_sse:
    mov rax,NEW_LINE_4_SSE
    movntdqa xmm0,[esi]
    pcmpestri xmm0,[rax],0x0

    ret

[niko@dev1 test]$ nasm -f elf64 -o ssefuncs.o ssefuncs.S
ssefuncs.S:7: error: parser: instruction expected
[niko@dev1 test]$ 

If I omit the export, recompile the assembly and try to link, the resulting code won't link with my C program:

[niko@dev1 test]$ gcc -o bench3 ssefuncs.o bench3.o
bench3.o: In function `main':
/home/niko/quaztech/qstar/test/bench3.c:34: undefined reference to `find_nl_sse'
collect2: error: ld returned 1 exit status
[niko@dev1 test]$ 

I also tried to add the global directive but I get the same error. Why NASM documentation is so misleading?

like image 595
Nulik Avatar asked Feb 01 '16 16:02

Nulik


1 Answers

here is the correct way to define a label as being visible outside the current assembly unit.

global _main 
_main: 
  1. the global statement must be before the actual label
  2. the label must begin with a single underscore

a C file would reference the label as

extern _main
like image 181
user3629249 Avatar answered Oct 04 '22 13:10

user3629249