编译与反编译GCC常⽤指令
从源代码转变为可执⾏代码的过程,具体可分为 4 个过程,分别为预处理(Preprocessing)、编译(Compilation)、汇编(Asmbly)链接(Linking)
⼀. GCC常⽤编译命令选项
@localhost train]$ gcc --help
Usage: gcc
Options:
-pass-exit-codes Exit with highest error code from a pha
--help Display this information
--target-help Display target specific command line options
-
-help={common|optimizers|params|target|warnings|[^]{joined|parate|undocumented}}[,...] Display specific types of command line options
(U '-v --help' to display command line options of sub-process)
--version Display compiler version information
-dumpspecs Display all of the built in spec strings
-dumpversion Display the version of the compiler
-dumpmachine Display the compiler's target processor
-print-arch-dirs Display the directories in the compiler's arch path
-print-libgcc-file-name Display the name of the compiler's companion library
-print-file-name=<lib> Display the full path to library <lib>
-print-prog-name=<prog> Display the full path to compiler component <prog>
-print-multiarch Display the target's normalized GNU triplet, ud as
a component in the library path
-print-multi-directory Display the root directory for versions of libgcc
-print-multi-lib Display the mapping between command line options and
multiple library arch directories
-print-multi-os-directory Display the relative path to OS libraries
-print-sysroot Display the target libraries directory
-print-sysroot-headers-suffix Display the sysroot suffix ud to find headerslogo设计图片
-Wa,<options> Pass comma-parated <options> on to the asmbler
-Wp,<options> Pass comma-parated <options> on to the preprocessor
-Wl,<options> Pass comma-parated <options> on to the linker
-Xasmbler <arg> Pass <arg> on to the asmbler
-
Xpreprocessor <arg> Pass <arg> on to the preprocessor
-Xlinker <arg> Pass <arg> on to the linker
-save-temps Do not delete intermediate files
-save-temps=<arg> Do not delete intermediate files
-no-canonical-prefixes Do not canonicalize paths when building relative
prefixes to other gcc components
-pipe U pipes rather than intermediate files
-time Time the execution of each subprocess
-specs=<file> Override built-in specs with the contents of <file>
-std=<standard> Assume that the input sources are for <standard>
--sysroot=<directory> U <directory> as the root directory for headers
and libraries
-B <directory> Add <directory> to the compiler's arch paths
-v Display the programs invoked by the compiler
-### Like -v but options quoted and commands not executed
-E Preprocess only; do not compile, asmble or link
-S Compile only; do not asmble or link
-c Compile and asmble, but do not link
-o <file> Place the output into <file>
-pie Create a position independent executable
-shared Create a shared library
-x <language> Specify the language of the following input files
Permissible languages include: c c++ asmbler none
'none' means revert to the default behavior of
guessing the language bad on the file's extension
指令格式为 gcc [options] file…
[options] 指令
file 操作⽂件
假设源程序⽂件名为test.c。
1. ⽆选项编译链接 按照默认输出
默认情况下,gcc 指令会⼀⽓呵成,直接将源代码历经这 4 个过程转变为可执⾏代码,且不会保留各个阶段产⽣的中间⽂件
⽤法:#gcc test.c
作⽤:将test.c预处理、汇编、编译并链接形成可执⾏⽂件。这⾥未指定输出⽂件,默认输出为a.out。
扩展名在Linux中和Windows下意义不同
2. 选项 -o Place the output into
⽤法:#gcc test.c -o test
作⽤:将test.c预处理、汇编、编译并链接形成可执⾏⽂件test。-o选项⽤来指定输出⽂件的⽂件名。
3. 选项 -E Preprocess only; do not compile, asmble or link
⽤法:#gcc -E test.c -o test.i
作⽤:将test.c预处理输出test.i⽂件。
#gcc -E test.c 即可预处理test.c⽂件
使⽤ -o 把预处理结果输出为test.i⽂件
所谓预处理操作,主要是处理那些源⽂件和头⽂件中以 # 开头的命令(⽐如 #include、#define、#ifdef 等),并删除程序中所有的注释// 和/* … */。
值得注意的是,默认情况下 gcc -E 指令只会将预处理操作的结果输出到屏幕上,并不会⾃动保存到某个⽂件。因此该指令往往会和 -o 选项连⽤,将结果导⼊到指令的⽂件中
4. 选项 -S -S Compile only; do not asmble or link
⽤法:#gcc -S test.i
作⽤:将预处理输出⽂件test.i汇编成test.s⽂件。
编译是整个程序构建的核⼼部分,也是最复杂的部分之⼀。所谓编译,简单理解就是将预处理得到的程序代码,经过⼀系列的词法分析、语法分析、语义分析以及优化,加⼯为当前机器⽀持的汇编代码。
-S 选项的功能是令 GCC 编译器将指定⽂件处理⾄编译阶段结束。这也就意味着,gcc -S 指令可以操作预处理后的 .i ⽂件,也可以操作源代码⽂件
5. 选项 -c -c Compile and asmble, but do not link
⽤法:#gcc -c test.s
作⽤:将汇编输出⽂件test.s编译输出test.o⽂件。
-c 会进⾏编译并且进⾏汇编,-o把结果输出为 ⽂件
gcc 指令添加 -c 选项(注意是⼩写字母 c),即可让 GCC 编译器将指定⽂件加⼯⾄汇编阶段,并⽣成相应的⽬标⽂件。
c 选项只是令 GCC 编译器将指定⽂件加⼯⾄汇编阶段,但不执⾏链接操作
6. ⽆选项链接
⽤法:#gcc test.o -o test
作⽤:将编译输出⽂件test.o链接成最终可执⾏⽂件test。
7. 选项-O羊与狼
⽤法:#gcc -O1 test.c -o test
作⽤:使⽤编译优化级别1编译程序。级别为1~3,级别越⼤优化效果越好,但编译时间越长。
8. gcc main.c -o main.out -lm
数学库的⽂件名是 libm.a。前缀lib和后缀.a是标准的,m是基本名称,GCC 会在-l选项后紧跟着的基本名称的基础上⾃动添加这些前缀、后缀,本例中,基本名称为 m。
链接器把多个⼆进制的⽬标⽂件(object file)链接成⼀个单独的可执⾏⽂件。在链接过程中,它必须把符号(变量名、函数名等⼀些列标识符)⽤对应的数据的内存地址(变量地址、函数地址等)替代,以完成程序中多个模块的外部引⽤。
标准库的⼤部分函数通常放在⽂件 libc.a 中(⽂件名后缀.a代表“achieve”,译为“获取”),或者放在⽤于共享的动态链接⽂件 libc.so 中(⽂件名后缀.so代表“share object”,译为“共享对象”)。这些链接库⼀般位于 /lib/ 或 /usr/lib/,或者位于 GCC 默认搜索的其他⽬录。
多源⽂件的编译⽅法
如果有多个源⽂件,基本上有两种编译⽅法:
秋怎么组词[假设有两个源⽂件为test.c和testfun.c]
1. 多个⽂件⼀起编译
⽤法:#gcc testfun.c test.c -o test
作⽤:将testfun.c和test.c分别编译后链接成test可执⾏⽂件。
2. 分别编译各个源⽂件,之后对编译后输出的⽬标⽂件链接。
⽤法:
#gcc -c testfun.c //将testfun.c编译成testfun.o
牛虻小说
#gcc -c test.c //将test.c编译成test.o
#gcc -o testfun.o test.o -o test //将testfun.o和test.o链接成test
常⽤选项
-E:只进⾏预处理,不编译
-S:只编译,不汇编
-c:只编译、汇编,不链接
-
g:包含调试信息
-I:指定include包含⽂件的搜索⽬录
-o:输出成指定⽂件名
⾼级选项
-v:详细输出编译过程中所采⽤的每⼀个选项
-C:预处理时保留注释信息
惠普打印机怎么复印-ggdb:在可执⾏⽂件中包含可供GDB使⽤的调试信息
-fverbo-asm:在编译成汇编语⾔时,把C变量的名称作为汇编语⾔中的注释
-save-temps:⾃动输出预处理⽂件、汇编⽂件、对象⽂件,编译正常进⾏
-fsyntax-only:只测试源⽂件语法是否正确,不会进⾏任何编译操作
-ffreestanding:编译成独⽴程序,⽽⾮宿主程序
语⾔标准
-ansi:ANSI标准
-std=c99:C99标准
-std=gnu89:ISO/IEC 9899:1990 以及GNU扩充
-std=gnu99:ISO/IEC 9899:1999 以及GNU扩充
-trigraphs:⽀持ISO C三字符组
出错提⽰
-w:忽略所有警告
-Werror:不区分警告和错误,遇到任何警告都停⽌编译
-Wall:开启⼤部分警告提⽰
-Wshadow:某语句块作⽤域变量与更⼤作⽤域的另⼀变量同名时发出警告(此警告未包含在-Wall选项中,需单独开启)
-Wextra:对所有合法但值得怀疑的表达式发出警告
优化选项
-O0:关闭所有优化选项
-O1:第⼀级别优化,使⽤此选项可使可执⾏⽂件更⼩、运⾏更快,并不会增加太多编译时间,可以简写为-O -O2:第⼆级别优化,采⽤了⼏乎所有的优化技术,使⽤此选项会延长编译时间
-O3:第三级别优化,在-O2的基础上增加了产⽣inline函数、使⽤寄存器等优化技术
-Os:此选项类似于-O2,作⽤是优化所占⽤的空间,但不会进⾏性能优化,常⽤于⽣成最终版本
objdump 反汇编
Usage: objdump <option(s)> <file(s)>
Display information from object <file(s)>.
At least one of the following switches must be given:
-a,--archive-headers Display archive header information
怀不上孩子怎么办
-f,--file-headers Display the contents of the overall file header
-p,--private-headers Display object format specific file header contents
-P,--private= Display object format specific contents
-h,--headers Display the contents of the ction headers
-x,--all-headers Display the contents of all headers
-d,--disasmble Display asmbler contents of executable ctions
-D,--disasmble-all Display asmbler contents of all ctions
-S,--source Intermix source code with disasmbly
-s,--full-contents Display the full contents of all ctions requested
-g,--debugging Display debug information in object file
-e,--debugging-tags Display debug information using ctags style
-
G,--stabs Display (in raw form) any STABS info in the file
-W or
--dwarf[=rawline,=decodedline,=info,=abbrev,=pubnames,=aranges,=macro,=frames,
=frames-interp,=str,=loc,=Ranges,=pubtypes,
=gdb_index,=trace_info,=trace_abbrev,=trace_aranges,
=addr,=cu_index]
Display DWARF info in the file
-t,--syms Display the contents of the symbol table(s)
-T,--dynamic-syms Display the contents of the dynamic symbol table
-r,--reloc Display the relocation entries in the file
-R,--dynamic-reloc Display the dynamic relocation entries in the file
@<file> Read options from <file>
-v,--version Display this program's version number
-i, --info List object formats and architectures supported
杀了夏明翰-H, --help Display this information
The following switches are optional:
-b, --target=BFDNAME Specify the target object format as BFDNAME
-m, --architecture=MACHINE Specify the target architecture as MACHINE
-j, --ction=NAME Only display information for ction NAME
-M, --disasmbler-options=OPT Pass text OPT on to the disasmbler
-EB --endian=big Assume big endian format when disasmbling
-EL --endian=little Assume little endian format when disasmbling
-
牙疼吃什么食物
-file-start-context Include context from start of file (with -S)
-I, --include=DIR Add DIR to arch list for source files
-l, --line-numbers Include line numbers and filenames in output
-F, --file-offts Include file offts when displaying information
-C, --demangle[=STYLE] Decode mangled/procesd symbol names
The STYLE, if specified, can be `auto', `gnu',
`lucid', `arm', `hp', `edg', `gnu-v3', `java'
or `gnat'
-w,--wide Format output for more than 80 columns
-z,--disasmble-zeroes Do not skip blocks of zeroes when disasmbling
--start-address=ADDR Only process data who address is >= ADDR
-
-stop-address=ADDR Only process data who address is <= ADDR
--prefix-address Print complete address alongside disasmbly
--show-raw-insn Display hex alongside symbolic disasmbly
--insn-width=WIDTH Display WIDTH bytes on a single line for-d
--adjust-vma=OFFSET Add OFFSET to all displayed ction address
--special-syms Include special symbols in symbol dumps
--prefix=PREFIX Add PREFIX to absolute paths for-S
--prefix-strip=LEVEL Strip initial directory names for-S
--dwarf-depth=N Do not display DIEs at depth N or greater