# 生成有詳細信息的匯編文件
## 例子
#include <stdio.h>
int main(void)
{
int i;
for (i = 0; i < 10; i++)
printf("%d ", i);
putchar ('\n');
return 0;
}
## 技巧
使用`-fverbose-asm`選項就可以生成帶有詳細信息的匯編文件:
$ gcc -S -fverbose-asm foo.c
$ cat foo.s
.file "foo.c"
# GNU C (Ubuntu/Linaro 4.6.3-1ubuntu5) version 4.6.3 (x86_64-linux-gnu)
# compiled by GNU C version 4.6.3, GMP version 5.0.2, MPFR version 3.1.0-p3, MPC version 0.9
# GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
# options passed: -imultilib . -imultiarch x86_64-linux-gnu foo.c
# -mtune=generic -march=x86-64 -fverbose-asm -fstack-protector
# options enabled: -fasynchronous-unwind-tables -fauto-inc-dec
# -fbranch-count-reg -fcommon -fdelete-null-pointer-checks -fdwarf2-cfi-asm
# -fearly-inlining -feliminate-unused-debug-types -ffunction-cse -fgcse-lm
# -fident -finline-functions-called-once -fira-share-save-slots
# -fira-share-spill-slots -fivopts -fkeep-static-consts
# -fleading-underscore -fmath-errno -fmerge-debug-strings
# -fmove-loop-invariants -fpeephole -fprefetch-loop-arrays
# -freg-struct-return -fsched-critical-path-heuristic
# -fsched-dep-count-heuristic -fsched-group-heuristic -fsched-interblock
# -fsched-last-insn-heuristic -fsched-rank-heuristic -fsched-spec
# -fsched-spec-insn-heuristic -fsched-stalled-insns-dep -fshow-column
# -fsigned-zeros -fsplit-ivs-in-unroller -fstack-protector
# -fstrict-volatile-bitfields -ftrapping-math -ftree-cselim -ftree-forwprop
# -ftree-loop-if-convert -ftree-loop-im -ftree-loop-ivcanon
# -ftree-loop-optimize -ftree-parallelize-loops= -ftree-phiprop -ftree-pta
# -ftree-reassoc -ftree-scev-cprop -ftree-slp-vectorize
# -ftree-vect-loop-version -funit-at-a-time -funwind-tables
# -fvect-cost-model -fverbose-asm -fzero-initialized-in-bss
# -m128bit-long-double -m64 -m80387 -maccumulate-outgoing-args
# -malign-stringops -mfancy-math-387 -mfp-ret-in-387 -mglibc -mieee-fp
# -mmmx -mno-sse4 -mpush-args -mred-zone -msse -msse2 -mtls-direct-seg-refs
# Compiler executable checksum: 75e879ed14f91af504f4150eadeaa0e6
.section .rodata
.LC0:
.string "%d "
.text
.globl main
.type main, @function
main:
.LFB0:
.cfi_startproc
pushq %rbp #
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp #,
.cfi_def_cfa_register 6
subq $16, %rsp #,
movl $0, -4(%rbp) #, i
jmp .L2 #
.L3:
movl $.LC0, %eax #, D.2049
movl -4(%rbp), %edx # i, tmp62
movl %edx, %esi # tmp62,
movq %rax, %rdi # D.2049,
movl $0, %eax #,
call printf #
addl $1, -4(%rbp) #, i
.L2:
cmpl $9, -4(%rbp) #, i
jle .L3 #,
movl $10, %edi #,
call putchar #
movl $0, %eax #, D.2050
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE0:
.size main, .-main
.ident "GCC: (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3"
.section .note.GNU-stack,"",@progbits
可以看到,在匯編文件中給出了gcc所使用的具體選項,以及匯編指令操作數所對應的源程序(或中間代碼)中的變量。
詳情參見[gcc手冊](https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#index-fverbose-asm-2614)
## 貢獻者
xmj
- 信息顯示
- 打印gcc預定義的宏信息
- 打印gcc執行的子命令
- 打印優化級別的對應選項
- 打印彩色診斷信息
- 打印頭文件搜索路徑
- 打印連接庫的具體路徑
- 預處理
- 生成沒有行號標記的預處理文件
- 在命令行中預定義宏
- 在命令行中取消宏定義
- 匯編
- 把選項傳給匯編器
- 生成有詳細信息的匯編文件
- 調試
- 利用Address Sanitizer工具檢查內存訪問錯誤
- 利用Thread Sanitizer工具檢查數據競爭的問題
- 連接
- 把選項傳給連接器
- 設置動態連接器
- 函數屬性
- 禁止函數被優化掉
- 強制函數inline
- 常見錯誤
- error: cast from ... to ... loses precision
- all warnings being treated as errors
- gdb無法調試gcc編譯的程序
- 其它
- 只做語法檢查
- 保存臨時文件
- 打開警告信息
- 指定語言類型
- 改變結構體成員的字節對齊