c - 从程序集中调用 libc 函数
问题描述
我在程序集中定义了一个调用 libc 函数(swapcontext)的函数。我从我的 C 代码中调用该函数。为了创建可重现的示例,我使用“puts”代替:
富.S:
.globl foo
foo:
call puts
ret
测试.c:
void foo(char *str);
int main() {
foo("Hello World\n");
return 0;
}
编译:
gcc test.c foo.S -o test
这编译得很好。然而,反汇编结果二进制显示链接器没有插入有效的调用指令:
objdump -dR:
0000000000000671 <foo>:
671: e8 00 00 00 00 callq 676 <foo+0x5>
672: R_X86_64_PC32 puts@GLIBC_2.2.5-0x4
676: c3 retq
677: 66 0f 1f 84 00 00 00 nopw 0x0(%rax,%rax,1)
67e: 00 00
0000000000000530 <puts@plt>:
530: ff 25 9a 0a 20 00 jmpq *0x200a9a(%rip) # 200fd0 <puts@GLIBC_2.2.5>
536: 68 00 00 00 00 pushq $0x0
53b: e9 e0 ff ff ff jmpq 520 <.plt>
执行:
./test1: Symbol `puts' causes overflow in R_X86_64_PC32 relocation
Segmentation fault
任何想法为什么?
解决方案
对于您更新的完全独立的问题,它取代了您关于拆卸 a 的问题.o
:
半相关:函数指针局部变量的意外值提到了链接器在非 PIE 中为您转换对的引用(因为如果静态链接可以让您获得高效的代码),但在 PIE 中没有puts
。puts@plt
libc 被映射到距离主可执行文件超过 2GiB 的位置,因此 acall rel32
无法访问它。
另请参阅Can't call C standard library function on 64-bit Linux from assembly (yasm) code,其中显示了 AT&T 和 NASM 语法,用于通过 PLTcall puts@plt
或gcc -fno-plt
样式从 PIE 可执行文件调用 libc 函数call *puts@gotpcrel(%rip)
。
推荐阅读
- python - 电子邮件的python IMAP内容包含一个字符串
- unix - 如何在 unix 的列中找出大于 10 的间隙?
- java - javax.net.ssl.SSLHandshakeException:在 Raspberry Pi 上运行 java 程序时出错
- amazon-web-services - 同步 RDS DB 和 Cognito 的最佳方式
- docker - 如何强制 Kubernetes 加载 mysql db?
- gcc - Makefile 每次都会编译所有文件
- python - 如何返回基于属性值排序的类对象列表
- python - 使用 Python Multiprocessing Pool.map() 的问题在 Python 3.7.2 中变得难以处理,但在 3.6.2 中快速完成
- jquery - 在boostrap轮播中使用图像而不是幻灯片背景
- angular - firebase 公司用户登录注册