gcc编译⼀个hello.c的⼀个过程
输⼊命令查看编译过程
参考⾃《ctf竞赛权威指南 PWN篇》
gcc hello.c -save-temps --verbose
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/8/lto-wrapper  ##这个是 Link Time Optimization,看这篇⽂章:blog.csdn/qq_36287943 /article/details/103681797
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Uos 8.3.0.3-3+rebuild' --with-bugurl=file:///usr/share/doc/gcc-8/README.Bugs --enable-languages=c, ada,c++,go,brig,d,fortran,objc,obj-c++ --prefix=/usr --with-gcc-major-version-only --program-suffix=-8 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocal e=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-lib mpx --enable-plugin --with-system-zlib --with-target-system-zlib --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m 64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none --without-cuda-driver --enable-checking=rel ease --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 8.3.0 (Uos 8.3.0.3-3+rebuild)
COLLECT_GCC_OPTIONS='-save-temps''-v''-mtune=generic''-march=x86-64'
/usr/lib/gcc/x86_64-linux-gnu/8/cc1 -E -quiet -v -imultiarch x86_64-linux-gnu hello.c -mtune=generic -
march=x86-64 -fpch-preprocess -o hello.i
ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu"
#include "..." search starts here:
#include <...> search starts here:
/usr/lib/gcc/x86_64-linux-gnu/8/include
/usr/local/include
/usr/lib/gcc/x86_64-linux-gnu/8/include-fixed
/usr/x86_64-linux-gnu/include
/usr/include/x86_64-linux-gnu
/usr/include
End of search list.
>下⾯开始编译阶段第⼀、⼆阶段
COLLECT_GCC_OPTIONS='-save-temps''-v''-mtune=generic''-march=x86-64'
> (cc1 是编译器)  c⽂件  --(预处理,针对预处理指令-->  i⽂件  --(编译--> s⽂件
/usr/lib/gcc/x86_64-linux-gnu/8/cc1 -fpreprocessed hello.i -quiet -dumpbase hello.c -mtune=generic -march=x86-64 -auxbase hello -version -o hello.s
GNU C17 (Uos 8.3.0.3-3+rebuild) version 8.3.0 (x86_64-linux-gnu)
compiled by GNU C version 8.3.0, GMP version 6.1.2, MPFR version 4.0.2, MPC version 1.1.0, isl version isl-0.20-GMP
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
linux下gcc编译的四个步骤GNU C17 (Uos 8.3.0.3-3+rebuild) version 8.3.0 (x86_64-linux-gnu)
compiled by GNU C version 8.3.0, GMP version 6.1.2, MPFR version 4.0.2, MPC version 1.1.0, isl version isl-0.20-GMP
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: b0aa96b3fb90562a90ec754591a6a020
COLLECT_GCC_OPTIONS='-save-temps''-v''-mtune=generic''-march=x86-64'
####(as 是汇编器  s⽂件  ---->  o⽂件
as -v --64 -o hello.o hello.s
GNU assembler version 2.31.1 (x86_64-linux-gnu) using BFD version (GNU Binutils for Uos) 2.31.1
COMPILER_PATH=/usr/lib/gcc/x86_64-linux-gnu/8/:/usr/lib/gcc/x86_64-linux-gnu/8/:/usr/lib/gcc/x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/8/:/usr/lib/g cc/x86_64-linux-gnu/
LIBRARY_PATH=/usr/lib/gcc/x86_64-linux-gnu/8/:/usr/lib/gcc/x86_64-linux-gnu/8/../../../../x86_64-linux-gnu/lib/../lib/:/usr/lib/gcc/x86_64-linux-gnu/8/../../../x8 6_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/8/../../../../lib/:/lib/x86_64-linux-gnu/:/lib/../lib/:/usr/lib/x86_64-linux-gnu/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-linux-g nu/8/../../../../x86_64-linux-gnu/lib/:/usr/lib/gcc/x86_64-linux-gnu/8/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-save-temps''-v''-mtune=generic''-march=x86-64'
> collect2是ld命令的封装,将CRT库中的⽬标⽂件(-lgcc、-lgcc-s 后⾯) o⽂件和动态链接库⽂件(-dynamic-linker后⾯)的o⽂件链接到hello
/usr/lib/gcc/x86_64-linux-gnu/8/collect2
-plugin /usr/lib/gcc/x86_64-linux-gnu/8/liblto_plugin.so -plugin-opt=/usr/lib/gcc/x86_64-linux-gnu/8/lto-wrapper -plugin-opt=-s -plugin-opt =-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lc -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s
--build-id --eh-frame-hdr -m elf_x86_64 --hash-style=gnu
-dynamic-linker /lib64/ld-linux-x86-64.so.2 /usr/lib/gcc/x86_64-linux-gnu/8/../../../../x86_64-linux-gnu/lib/../lib/crt1.o /usr/lib/gcc/x86_64-linux-gnu/8/../../../../x 86_64-linux-gnu/lib/../lib/crti.o /usr/lib/gcc/x86_64-linux-gnu/8/crtbegin.o -L/usr/lib/gcc/x86_64-linux-gnu/8 -L/usr/lib/gcc/x86_64-linux-gnu/8/../../../../x86_64-l inux-gnu/lib/../lib -L/usr/lib/gcc/x86_64-linux-gnu/8/../../../x86_64-linux-gnu -L/usr/lib/gcc/x86_64-linux-gnu/8/../../../../lib -L/lib/x86_64-linux-gnu -L/lib/../lib -L/ usr/lib/x86_64-l
inux-gnu -L/usr/lib/../lib -L/usr/lib/gcc/x86_64-linux-gnu/8/../../../../x86_64-linux-gnu/lib -L/usr/lib/gcc/x86_64-linux-gnu/8/../../.. hello.o
-lgcc --push-state --as-needed -lgcc_s --pop-state -lc -lgcc --push-state --as-needed -lgcc_s --pop-state /usr/lib/gcc/x86_64-linux-gnu/8/crtend.o /usr/lib/gc c/x86_64-linux-gnu/8/../../../../x86_64-linux-gnu/lib/../lib/crtn.o
COLLECT_GCC_OPTIONS='-save-temps''-v''-mtune=generic' '-march=x86-64
得到的⽂件为
hello.o、a.out ⽂件 均为 ELF 64-bit LSB ralocatable,x84-64
hello.s⽂件 gcc默认为AT & T 格式的汇编语⾔,可以指定为intel格式的 。
AT & T 格式的:
.file "hello.c"
.text
.
section .rodata
.LC0:
.string "hello world"
.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
movl $.LC0, %edi
movl $0, %eax
call printf
movl $0, %eax
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.
LFE0:
.size main, .-main
.ident "GCC: (Uos 8.3.0.3-3+rebuild) 8.3.0"
.section .note.GNU-stack,"",@progbits
输⼊这个
gcc -S hello.i -o hello_intel.s -masm=intel
产⽣intel格式的:
.file "hello.c"
.intel_syntax noprefix
.text
.section .rodata
.
LC0:
.string "hello world"
.text
.globl main
.type main, @function
main:
.LFB0:
.cfi_startproc
push rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
mov rbp, rsp
.cfi_def_cfa_register 6
mov edi, OFFSET FLAT:.LC0
mov eax, 0
call printf
mov eax, 0
pop rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE0:
.
size main, .-main
.ident "GCC: (Uos 8.3.0.3-3+rebuild) 8.3.0"
.section .note.GNU-stack,"",@progbits
hello.i ⽂件
# 1"hello.c"
# 1"<built-in>"
# 1"<command-line>"
# 31"<command-line>"
# 1"/usr/x86_64-linux-gnu/include/stdc-predef.h"13
# 32"<command-line>"2
# 1"hello.c"
# 1"/usr/x86_64-linux-gnu/include/stdio.h"13
# 27"/usr/x86_64-linux-gnu/include/stdio.h"3
# 1"/usr/x86_64-linux-gnu/include/bits/libc-header-start.h"13 # 33"/usr/x86_64-linux-gnu/include/bits/libc-header-start.h"3 # 1"/usr/x86_64-linux-gnu/include/features.h"13
# 424"/usr/x86_64-linux-gnu/include/features.h"3
# 1"/usr/x86_64-linux-gnu/include/sys/cdefs.h"13
# 442"/usr/x86_64-linux-gnu/include/sys/cdefs.h"3
# 1"/usr/x86_64-linux-gnu/include/bits/wordsize.h"13
# 443"/usr/x86_64-linux-gnu/include/sys/cdefs.h"23
# 1"/usr/x86_64-linux-gnu/include/bits/long-double.h"13
# 444"/usr/x86_64-linux-gnu/include/sys/cdefs.h"23
# 425"/usr/x86_64-linux-gnu/include/features.h"23
# 448"/usr/x86_64-linux-gnu/include/features.h"3
# 1"/usr/x86_64-linux-gnu/include/gnu/stubs.h"13
# 10"/usr/x86_64-linux-gnu/include/gnu/stubs.h"3
# 1"/usr/x86_64-linux-gnu/include/gnu/stubs-64.h"13
# 11"/usr/x86_64-linux-gnu/include/gnu/stubs.h"23
# 449"/usr/x86_64-linux-gnu/include/features.h"23
# 34"/usr/x86_64-linux-gnu/include/bits/libc-header-start.h"23 # 28"/usr/x86_64-linux-gnu/include/stdio.h"23
# 1"/usr/lib/gcc/x86_64-linux-gnu/8/include/stddef.h"134 # 216"/usr/lib/gcc/x86_64-linux-gnu/8/include/stddef.h"34
# 216"/usr/lib/gcc/x86_64-linux-gnu/8/include/stddef.h"34 typedef long unsigned int size_t;
# 34"/usr/x86_64-linux-gnu/include/stdio.h"23
# 1"/usr/lib/gcc/x86_64-linux-gnu/8/include/stdarg.h"134 # 40"/usr/lib/gcc/x86_64-linux-gnu/8/include/stdarg.h"34 typedef __builtin_va_list __gnuc_va_list;
# 37"/usr/x86_64-linux-gnu/include/stdio.h"23
# 1"/usr/x86_64-linux-gnu/include/bits/types.h"13
# 27"/usr/x86_64-linux-gnu/include/bits/types.h"3
# 1"/usr/x86_64-linux-gnu/include/bits/wordsize.h"13
# 28"/usr/x86_64-linux-gnu/include/bits/types.h"23
typedef unsigned char __u_char;
typedef unsigned short int __u_short;
typedef unsigned int __u_int;
typedef unsigned long int __u_long;
typedef signed char __int8_t;
typedef unsigned char __uint8_t;
typedef signed short int __int16_t;
typedef unsigned short int __uint16_t;
typedef signed int __int32_t;
typedef unsigned int __uint32_t;
typedef signed long int __int64_t;
typedef unsigned long int __uint64_t;
typedef __int8_t __int_least8_t;
typedef __uint8_t __uint_least8_t;
typedef __int16_t __int_least16_t;
typedef __uint16_t __uint_least16_t;
typedef __int32_t __int_least32_t;
typedef __uint32_t __uint_least32_t;
typedef __int64_t __int_least64_t;
typedef __uint64_t __uint_least64_t;
typedef long int __quad_t;
typedef unsigned long int __u_quad_t;
typedef long int __intmax_t;
typedef unsigned long int __uintmax_t;
# 140"/usr/x86_64-linux-gnu/include/bits/types.h"3
# 1"/usr/x86_64-linux-gnu/include/bits/typesizes.h"13
# 141"/usr/x86_64-linux-gnu/include/bits/types.h"23
typedef unsigned long int __dev_t;
typedef unsigned int __uid_t;
typedef unsigned int __gid_t;
typedef unsigned long int __ino_t;
typedef unsigned long int __ino64_t;
typedef unsigned int __mode_t;
typedef unsigned long int __nlink_t;
typedef long int __off_t;
typedef long int __off64_t;
typedef int __pid_t;
typedef struct{int __val[2];} __fsid_t;
typedef long int __clock_t;
typedef unsigned long int __rlim_t;
typedef unsigned long int __rlim64_t;
typedef unsigned int __id_t;
typedef long int __time_t;
typedef unsigned int __useconds_t;
typedef long int __suseconds_t;
typedef int __daddr_t;
typedef int __key_t;
typedef int __clockid_t;
typedef void* __timer_t;
typedef long int __blksize_t;
typedef long int __blkcnt_t;
typedef long int __blkcnt64_t;
typedef unsigned long int __fsblkcnt_t;
typedef unsigned long int __fsblkcnt64_t;
typedef unsigned long int __fsfilcnt_t;
typedef unsigned long int __fsfilcnt64_t;
typedef long int __fsword_t;
typedef long int __ssize_t;
typedef long int __syscall_slong_t;
typedef unsigned long int __syscall_ulong_t;
typedef __off64_t __loff_t;
typedef char*__caddr_t;
typedef long int __intptr_t;

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。

发表评论