1.ATT汇编
1.1 基础知识
-
Intel中,第一个操作数是目的操作数,第二个操作数是源操作数。
-
AT&T 中,第一个数是源操作数,第二个数是目的操作数
-
AT&T 的操作码后面有一个后缀,指出操作码的大小
- “l”表示长整数(32 位),
- “w”表示字(16 位),
- “b”表示字节(8 位)
-
Intel 的语法中,在内存单元操作数的前面加上byte ptr、word ptr 和dword ptr,
- “dword”对应“long”
-
"#"井号开头的是注释行
-
"."点开始的指令一般都是伪指令
-
"$"美元符号修饰立即数,十六进制立即数前冠以“0x”
-
"%"修饰寄存器
-
地址或偏移 (%基址或偏移量寄存器, %索引寄存器, 比例因子)
- 这种寻址方式常常用在访问数据结构数组中某个特定元素内的一个字段,其中,base 为数组的起始地址,scale 为每个数组元素的大小,index 为下标。如果数组元素还是一个结构,则disp 为具体字段在结构中的位移。
-
AT&T 的语法中,一个节由.section 关键词来标识
- .section .data:包含程序已初始化的数据,也就是说,包含具有初值的那些变量
- .section .bss:包含程序还未初始化的数据,也就是说,包含没有初值的那些变量
- .section .text :包含程序的代码,它是只读节,而.data 和.bss 是读/写节。
1.2 常用指令
- cmp S2,S1 计算S1 – S2,并根据执行结果修改标志位
- mov S,D 将s放入d中
- je Label 根据ZF标志进行条件跳转,如果发生跳转将跳转至label处
- lea S,D 执行D = &S,将s的地址放入d中
- nopl 用于在指令流中插入空闲nop指令,使指令之后的访问可以对齐
- cmpxchg source, destination 将目的寄存器与eax寄存器的值进行比较
- 如果相等则将源操作数放入目的操作数
- 如果不等,将目的操作数放入eax寄存器
- set
2.寄存器
2.1 intel架构寄存器作用
对于 Intel 架构,共有16个64位通用寄存器
- %rax 作为函数返回值使用。
- %rsp 栈指针寄存器,指向栈顶,%rbp 栈基地址寄存器,保存当前帧的栈底地址。
- %rdi,%rsi,%rdx,%rcx,%r8,%r9 用作函数参数,依次对应第1参数,第2参数。。。
- 其余被标识为 “miscellaneous registers” 的寄存器,属于通用性更为广泛的寄存器,编译器或汇编程序可以根据需要存储任何数据。
- %rip 指令指针寄存器,用于存放下一个要执行的指令地址(专用的寄存器 )
3.c++执行过程中的内存使用
-
在C++中,内存分成5个区,他们分别是堆、栈、自由存储区、全局/静态存储区和常量存储区。
-
栈,在执行函数时,函数内局部变量的存储单元都可以在栈上创建,函数执行结束时这些存储单元自动被释放。栈内存分配运算内置于处理器的指令集中,效率很高,但是分配的内存容量有限。
-
堆,就是那些由new分配的内存块,他们的释放编译器不去管,由我们的应用程序去控制,一般一个new就要对应一个delete。如果程序员没有释放掉,那么在程序结束后,操作系统会自动回收。
-
自由存储区,就是那些由malloc等分配的内存块,他和堆是十分相似的,不过它是用free来结束自己的生命的。
-
全局/静态存储区,全局变量和静态变量被分配到同一块内存中,在以前的C语言中,全局变量又分为初始化的和未初始化的,在C++里面没有这个区分了,他们共同占用同一块内存区。
-
常量存储区,里面存放的是常量,不允许修改。
-
4.objdump使用
- 使用objdump在Linux环境下可以将二进制可执行文件转换为汇编文件
4.1 反汇编选项
- 至少选择其中一个进行反汇编:
- -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=OPT,OPT… Display object format specific contents
- -h, --[section-]headers Display the contents of the section headers
- -x, --all-headers Display the contents of all headers
- -d, --disassemble Display assembler contents of executable sections
- -D, --disassemble-all Display assembler contents of all sections
- -S, --source Intermix source code with disassembly
- -s, --full-contents Display the full contents of all sections 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[lLiaprmfFsoRt] 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 @ Read options from
- -v, --version Display this program’s version number
- -i, --info List object formats and architectures supported
- -H, --help Display this information
- 下面的是可选反汇编选项:
- -b, --target=BFDNAME Specify the target object format as BFDNAME
- -m, --architecture=MACHINE Specify the target architecture as MACHINE
- -j, --section=NAME Only display information for section NAME
- -M, --disassembler-options=OPT Pass text OPT on to the disassembler
- -EB --endian=big Assume big endian format when disassembling
- -EL --endian=little Assume little endian format when disassembling
- –file-start-context Include context from start of file (with -S)
- -I, --include=DIR Add DIR to search list for source files
- -l, --line-numbers Include line numbers and filenames in output
- -F, --file-offsets Include file offsets when displaying information
- -C, --demangle[=STYLE] Decode mangled/processed 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, --disassemble-zeroes Do not skip blocks of zeroes when disassembling
- –start-address=ADDR Only process data whose address is >= ADDR
- –stop-address=ADDR Only process data whose address is <= ADDR
- –prefix-addresses Print complete address alongside disassembly
- –[no-]show-raw-insn Display hex alongside symbolic disassembly
- –insn-width=WIDTH Display WIDTH bytes on a single line for -d
- –adjust-vma=OFFSET Add OFFSET to all displayed section addresses
- –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
- –dwarf-start=N Display DIEs starting with N, at the same depth or deeper
- –dwarf-check Make additional dwarf internal consistency checks.
- 使用-M选项在i386/x86-64机器上指定特别选项,多个选项之间使用逗号隔开
- x86-64 Disassemble in 64bit mode
- i386 Disassemble in 32bit mode
- i8086 Disassemble in 16bit mode
- att Display instruction in AT&T syntax
- intel Display instruction in Intel syntax
- att-mnemonic Display instruction in AT&T mnemonic
- intel-mnemonic Display instruction in Intel mnemonic
- addr64 Assume 64bit address size
- addr32 Assume 32bit address size
- addr16 Assume 16bit address size
- data32 Assume 32bit data size
- data16 Assume 16bit data size
- suffix Always display instruction suffix in AT&T syntax