汇编代码strdup.共享库中的Malloc调用 [英] Assembly coding strdup. Malloc calling in shared library
问题描述
我有一个问题,调用malloc时无法编译我的strdup. 当我不调用malloc时,它将完美地编译我的共享库,因此,如果有人可以帮助我,那就太好了!
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 !
这是我的代码:
BITS 64
DEFAULT REL
global my_strdup:function
extern malloc
my_strdup:
[...]
call malloc
我以此编译:
I compile with this:
$> nasm -f elf64 my_strdup.S
$> nasm -f elf64 my_strdup.S
$> gcc -shared -o libmy.so my_strdup.o
$> 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:针对未定义符号`重新定位R_X86_64_PC32创建共享库时,不能使用malloc @@ GLIBC_2.2.5';用-fPIC重新编译 /usr/lib64/gcc/x86_64-suse-linux/4.8/../../../../x86_64-suse-linux/bin/ld:最终链接失败:值错误 collect2:错误:ld返回1退出状态
/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
我有此错误.我不明白!我有一台64位计算机. 这是我第一次尝试组装.
I have this error. I don't understand it ! I have a 64bit computer. It is the first time I try Assembly.
谢谢!
推荐答案
创建一个从另一个共享库调用函数的共享库对于第一个汇编程序可能不是最好的方法;)
Creating a shared library that calls function from another shared library might not be the best thing for a first assembly program ;)
也就是说,这就是 nasm手册对此必须说:
That said, here is what the nasm manual has to say about this:
使用wrt ..plt引用过程名称会导致链接器 为该符号建立一个过程链接表条目,然后 reference给出了PLT条目的地址.您只能在 通常会产生PC相对重定位的上下文(即 作为CALL或JMP的目标),因为ELF不包含重定位 键入以完全引用PLT条目.
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.
所以,您需要的是call malloc wrt ..plt
.
这篇关于汇编代码strdup.共享库中的Malloc调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!