objdump的選項-S、-l十分方便。如果二進制文件中帶有調(diào)試信息,可以將源代碼、文件名和行號與匯編代碼對應(yīng)顯示。
在OSX上,對應(yīng)的工具是otool。與“objdump -Sl”能力接近的命令是otool -tV。
看一看insert sort算法的反編譯。插入排序的一種實現(xiàn):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
#include <stdio.h>
void insert_sort(int* L, int size) {
int tmp, i, j;
for (i = 1; i < size; ++i) {
tmp = L[i];
for (j = i-1; j >= 0 && L[j] > tmp; --j) {
L[j+1] = L[j];
}
L[j+1] = tmp;
}
}
void print_array(int* L, int size) {
printf("array: ");
for (int i = 0; i < size; ++i) {
printf("%d ", L[i]);
}
printf("\n");
}
int main (int argc, char** argv) {
int L[] = {18, 7, 5, 8, 99};
int size = sizeof(L) / sizeof(int);
insert_sort(L, size);
print_array(L, size);
return 0;
}
|
這個實現(xiàn)中沒有全局變量,數(shù)據(jù)段(Data Section)應(yīng)該沒有內(nèi)容。我們用"otool -dV insertsort"這個命令,只顯示Data Section, 驗證一下:
- oliverluan@localhost:~/Documents/Opt/insertsort$ otool -dV insertsort
- insertsort:
如果把L數(shù)據(jù)和size變量改寫成全局變量:
- oliverluan@localhost:~/Documents/Opt/insertsort$ gcc -g insertsort_global.c -o insertsort_global
- oliverluan@localhost:~/Documents/Opt/insertsort$ otool -dV insertsort_global
- insertsort_global:
- (__DATA,__data) section
- 0000000100001020 12 00 00 00 07 00 00 00 05 00 00 00 08 00 00 00
- 0000000100001030 63 00 00 00 05 00 00 00
看一看Text Section: otool -tV insertsort:
- oliverluan@localhost:~/Documents/Opt/insertsort$ otool -tV insertsort
- insertsort:
- (__TEXT,__text) section
- _insert_sort:
- 0000000100000dd0 pushq %rbp
- 0000000100000dd1 movq %rsp, %rbp
- 0000000100000dd4 movq %rdi, 0xfffffffffffffff8(%rbp)
- 0000000100000dd8 movl %esi, 0xfffffffffffffff4(%rbp)
- 0000000100000ddb movl $0x1, 0xffffffffffffffec(%rbp)
- 0000000100000de2 movl 0xffffffffffffffec(%rbp), %eax
- 0000000100000de5 cmpl 0xfffffffffffffff4(%rbp), %eax
- 0000000100000de8 jge 0x100000e94
- 0000000100000dee movslq 0xffffffffffffffec(%rbp), %rax
- 0000000100000df2 movq 0xfffffffffffffff8(%rbp), %rcx
- 0000000100000df6 movl (%rcx,%rax,4), %edx
- 0000000100000df9 movl %edx, 0xfffffffffffffff0(%rbp)
- 0000000100000dfc movl 0xffffffffffffffec(%rbp), %edx
- 0000000100000dff subl $0x1, %edx
- 0000000100000e05 movl %edx, 0xffffffffffffffe8(%rbp)
- 0000000100000e08 movb $0x0, %al
- 0000000100000e0a cmpl $0x0, 0xffffffffffffffe8(%rbp)
- 0000000100000e11 movb %al, 0xffffffffffffffe7(%rbp)
- 0000000100000e14 jl 0x100000e30
- 0000000100000e1a movslq 0xffffffffffffffe8(%rbp), %rax
- 0000000100000e1e movq 0xfffffffffffffff8(%rbp), %rcx
- 0000000100000e22 movl (%rcx,%rax,4), %edx
- 0000000100000e25 cmpl 0xfffffffffffffff0(%rbp), %edx
- 0000000100000e28 setg %sil
- 0000000100000e2c movb %sil, 0xffffffffffffffe7(%rbp)
- 0000000100000e30 movb 0xffffffffffffffe7(%rbp), %al
- 0000000100000e33 testb $0x1, %al
- 0000000100000e35 jne 0x100000e40
- 0000000100000e3b jmpq 0x100000e6e
- 0000000100000e40 movslq 0xffffffffffffffe8(%rbp), %rax
- 0000000100000e44 movq 0xfffffffffffffff8(%rbp), %rcx
- 0000000100000e48 movl (%rcx,%rax,4), %edx
- 0000000100000e4b movl 0xffffffffffffffe8(%rbp), %esi
- 0000000100000e4e addl $0x1, %esi
- 0000000100000e54 movslq %esi, %rax
- 0000000100000e57 movq 0xfffffffffffffff8(%rbp), %rcx
- 0000000100000e5b movl %edx, (%rcx,%rax,4)
- 0000000100000e5e movl 0xffffffffffffffe8(%rbp), %eax
- 0000000100000e61 addl $0xffffffff, %eax
- 0000000100000e66 movl %eax, 0xffffffffffffffe8(%rbp)
- 0000000100000e69 jmpq 0x100000e08
- 0000000100000e6e movl 0xfffffffffffffff0(%rbp), %eax
- 0000000100000e71 movl 0xffffffffffffffe8(%rbp), %ecx
- 0000000100000e74 addl $0x1, %ecx
- 0000000100000e7a movslq %ecx, %rdx
- 0000000100000e7d movq 0xfffffffffffffff8(%rbp), %rsi
- 0000000100000e81 movl %eax, (%rsi,%rdx,4)
- 0000000100000e84 movl 0xffffffffffffffec(%rbp), %eax
- 0000000100000e87 addl $0x1, %eax
- 0000000100000e8c movl %eax, 0xffffffffffffffec(%rbp)
- 0000000100000e8f jmpq 0x100000de2
- 0000000100000e94 popq %rbp
- 0000000100000e95 ret
- 0000000100000e96 nopw %cs:(%rax,%rax)
- _print_array:
- 0000000100000ea0 pushq %rbp
- 0000000100000ea1 movq %rsp, %rbp
- 0000000100000ea4 subq $0x20, %rsp
- 0000000100000ea8 leaq 0xdb(%rip), %rax ## literal pool for: array:
- 0000000100000eaf movq %rdi, 0xfffffffffffffff8(%rbp)
- 0000000100000eb3 movl %esi, 0xfffffffffffffff4(%rbp)
- 0000000100000eb6 movq %rax, %rdi
- 0000000100000eb9 movb $0x0, %al
- 0000000100000ebb callq 0x100000f68 ## symbol stub for: _printf
- 0000000100000ec0 movl $0x0, 0xfffffffffffffff0(%rbp)
- 0000000100000ec7 movl %eax, 0xffffffffffffffec(%rbp)
- 0000000100000eca movl 0xfffffffffffffff0(%rbp), %eax
- 0000000100000ecd cmpl 0xfffffffffffffff4(%rbp), %eax
- 0000000100000ed0 jge 0x100000f02
- 0000000100000ed6 leaq 0xb5(%rip), %rdi ## literal pool for: %d
- 0000000100000edd movslq 0xfffffffffffffff0(%rbp), %rax
- 0000000100000ee1 movq 0xfffffffffffffff8(%rbp), %rcx
- 0000000100000ee5 movl (%rcx,%rax,4), %esi
- 0000000100000ee8 movb $0x0, %al
- 0000000100000eea callq 0x100000f68 ## symbol stub for: _printf
- 0000000100000eef movl %eax, 0xffffffffffffffe8(%rbp)
- 0000000100000ef2 movl 0xfffffffffffffff0(%rbp), %eax
- 0000000100000ef5 addl $0x1, %eax
- 0000000100000efa movl %eax, 0xfffffffffffffff0(%rbp)
- 0000000100000efd jmpq 0x100000eca
- 0000000100000f02 leaq 0x8d(%rip), %rdi ## literal pool for:
-
- 0000000100000f09 movb $0x0, %al
- 0000000100000f0b callq 0x100000f68 ## symbol stub for: _printf
- 0000000100000f10 movl %eax, 0xffffffffffffffe4(%rbp)
- 0000000100000f13 addq $0x20, %rsp
- 0000000100000f17 popq %rbp
- 0000000100000f18 ret
- 0000000100000f19 nopl (%rax)
- _main:
- 0000000100000f20 pushq %rbp
- 0000000100000f21 movq %rsp, %rbp
- 0000000100000f24 subq $0x10, %rsp
- 0000000100000f28 leaq _L(%rip), %rax
- 0000000100000f2f movl $0x0, 0xfffffffffffffffc(%rbp)
- 0000000100000f36 movl %edi, 0xfffffffffffffff8(%rbp)
- 0000000100000f39 movq %rsi, 0xfffffffffffffff0(%rbp)
- 0000000100000f3d movl _size(%rip), %esi
- 0000000100000f43 movq %rax, %rdi
- 0000000100000f46 callq _insert_sort
- 0000000100000f4b leaq _L(%rip), %rdi
- 0000000100000f52 movl _size(%rip), %esi
- 0000000100000f58 callq _print_array
- 0000000100000f5d movl $0x0, %eax
- 0000000100000f62 addq $0x10, %rsp
- 0000000100000f66 popq %rbp
- 0000000100000f67 ret
另外,可以用-l選項查看load commands:
符號表的查看使用nm -px insertsort。-p 原始順序,不做symbol字母或者數(shù)字排序。-x 16進制表示
- oliverluan@localhost:~/Documents/Opt/insertsort$ nm -px insertsort
- 0000000100001020 0f 09 0000 00000000000000a9 _L
- 0000000100000000 0f 01 0010 00000000000000ac __mh_execute_header
- 0000000100000dd0 0f 01 0000 00000000000000c0 _insert_sort
- 0000000100000f20 0f 01 0000 00000000000000cd _main
- 0000000100000ea0 0f 01 0000 00000000000000d3 _print_array
- 0000000100001034 0f 09 0000 00000000000000e0 _size
- 0000000000000000 01 00 0100 00000000000000e6 _printf
- 0000000000000000 01 00 0100 00000000000000ee dyld_stub_binder
|