![](/img/trans.png)
[英]Difference between -ffreestanding and -nostdlib when compiling with gcc
[英]gcc: segmentation fault when compiling with nostdlib
我正在試驗入口點,並遇到了段錯誤。
程序:
int main() {
return 0;
}
編譯並鏈接到:
gcc -Wall prog.c -nostdlib -c -o prog.o
ld prog.o -e main -o prog.out
objdump:
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設置一些虛擬參數(設置args xyz)
您可以編譯並鏈接調試信息(-g),然后使用gdb在retq指令(br * 0x4000ba)上設置斷點並運行程序
執行最后一條指令,並觀察SIGSEGV地址對應於參數數量+ 1
該程序應通過系統調用而不是retq退出
有關一些有用的背景信息,請參見http://eli.thegreenplace.net/2012/08/13/how-statically-linked-programs-run-on-linux/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.