5

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?

3
  • I heave two ideas: 1. try moving export find_nl_sse before section .text also try global instead of export. 2. in asm name it _find_nl_sse and in C find_nl_sse. Feb 1, 2016 at 17:10
  • 2
    global and export do different thinigs.
    – fuz
    Feb 1, 2016 at 17:15
  • @MarinovIván , it doesn't help. same error after moving export / global out of the .text section
    – Nulik
    Feb 1, 2016 at 19:12

1 Answer 1

9

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
5
  • correct, just I can't use _main, I have the "main" symbol in my C program. But it works. Thanks. "Before label" and removing "export" I think were the keys to compile successfuly
    – Nulik
    Feb 1, 2016 at 23:14
  • As for the underscore, when using ELF objects the leading underscore is not needed unless you explicitly override it with -fleading-underscore Feb 2, 2016 at 2:54
  • @MichaelPetch But on a mac, you always need the underscore.
    – JustinCB
    Aug 5, 2018 at 17:53
  • @JustinC.B.: I said ELF,not MACHO. ELF doesn't require the underscores. If you use an x86 ELF cross compiler on MacOS you don't need an underscore. If you generate MACHO objects (32 or 64-bit) then you do.' It should be pointed out the OP was using ELF. Aug 5, 2018 at 17:55
  • @MichaelPetch That's reasonable, I was just pointing out that if you're using MACHO you actually need the underscore.
    – JustinCB
    Aug 5, 2018 at 18:16

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.