gpt4 book ai didi

c - gcc:使用 nostdlib 编译时出现段错误

转载 作者:太空宇宙 更新时间:2023-11-04 00:50:03 26 4
gpt4 key购买 nike

我正在试验入口点并遇到段错误。

程序.c:

int main() {
return 0;
}

编译和链接:

 gcc -Wall prog.c -nostdlib -c -o prog.o
ld prog.o -e main -o prog.out

对象转储:

Sections:
Idx Name Size VMA LMA File off Algn
0 .text 0000000b 00000000004000b0 00000000004000b0 000000b0 2**2
CONTENTS, ALLOC, LOAD, READONLY, CODE
1 .eh_frame 00000038 00000000004000c0 00000000004000c0 000000c0 2**3
CONTENTS, ALLOC, LOAD, READONLY, DATA
2 .comment 0000001c 0000000000000000 0000000000000000 000000f8 2**0
CONTENTS, READONLY

Disassembly of section .text:
00000000004000b0 <main>:
4000b0: 55 push %rbp
4000b1: 48 89 e5 mov %rsp,%rbp
4000b4: b8 00 00 00 00 mov $0x0,%eax
4000b9: 5d pop %rbp
4000ba: c3 retq

Valgrind 输出:

 Access not within mapped region at address 0x0

最佳答案

retq 从堆栈顶部获取返回地址并从那里执行...问题是根据 Linux 执行二进制文件的方式,参数的数量在堆栈上并且执行转移到地址 0x1(如果没有参数已给出)

用 gdb 设置一些虚拟参数(set args x y z)

你可以用调试信息(-g)编译和链接,然后使用gdb在retq指令(br *0x4000ba)上设置断点并运行程序

执行最后一条指令,观察SIGSEGV地址对应参数个数+1

程序应该通过系统调用退出,而不是 retq

http://eli.thegreenplace.net/2012/08/13/how-statically-linked-programs-run-on-linux/一些有用的背景信息

关于c - gcc:使用 nostdlib 编译时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22236840/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com