Assembly coding strdup. Malloc calling in shared l

2019-07-20 20:25发布

问题:

I have a problem, I can't compile my strdup while calling malloc. When I don't call malloc, it compiles my shared library perfectly, so if someone could help me that would be great !

here is my code:


BITS 64
DEFAULT REL
        global my_strdup:function
        extern malloc
my_strdup:

[...]

    call malloc

I compile with this:

$> nasm -f elf64 my_strdup.S

$> gcc -shared -o libmy.so my_strdup.o

/usr/lib64/gcc/x86_64-suse-linux/4.8/../../../../x86_64-suse-linux/bin/ld: my_strdup.o: relocation R_X86_64_PC32 against undefined symbol `malloc@@GLIBC_2.2.5' can not be used when making a shared object; recompile with -fPIC /usr/lib64/gcc/x86_64-suse-linux/4.8/../../../../x86_64-suse-linux/bin/ld: final link failed: Bad value collect2: error: ld returned 1 exit status

I have this error. I don't understand it ! I have a 64bit computer. It is the first time I try Assembly.

Thanks !

回答1:

Creating a shared library that calls function from another shared library might not be the best thing for a first assembly program ;)

That said, here is what the nasm manual has to say about this:

Referring to a procedure name using wrt ..plt causes the linker to build a procedure linkage table entry for the symbol, and the reference gives the address of the PLT entry. You can only use this in contexts which would generate a PC-relative relocation normally (i.e. as the destination for CALL or JMP), since ELF contains no relocation type to refer to PLT entries absolutely.

So, what you need is call malloc wrt ..plt.