gccshared-librariesnasmsymbolsld

How to export a symbol to a shared library in nasm?


I have the following:

BITS 64

__NR_getuid equ 102

global sys_getuid

section .text

sys_getuid:
    xor eax, eax
    mov al, __NR_getuid
    syscall
    ret

This is compiled with:

nasm -f elf64 -o getuid.o getuid.asm
ld -shared -o libgetuid.so getuid.o

However sys_getuid isn't exported. Witness:

#include <stdio.h>

extern int sys_getuid();

int main()
{
    printf("UID = %d\n", sys_getuid());
    return 0;
}
gcc -o junk -L. -lgetuid junk.c
/usr/bin/ld: /tmp/ccHOc4iR.o: in function `main':
junk.c:(.text+0xa): undefined reference to `sys_getuid'
collect2: error: ld returned 1 exit status

How do I export the symbol?

I clearly don't need any relocation information for this library, so I should be able to do this like so.


Solution

  • How do I export the symbol?

    The symbol is already exported:

    nm -D libgetuid.so
    0000000000001000 T sys_getuid
    

    As Michael Petch commented, your problem is incorrect link line. Using

    gcc junk.c -L. -lgetuid -Wl,-rpath=.
    

    allows your executable to link and run.

    I thought we got rid of that problem long ago to allow circular symbol resolution between libraries.

    Among the common UNIX linkers, only LLD allows "definitions before references" . Binutils GNU ld and gold do not.