#include <stdio.h>
/*
* 直接函数调用
*/
void func() ;
int main()
{
int a;
func();
a = 0x66;
func();
return 0;
}
void func()
{
int a;
a++;
}
#if 0
gcc -o 01-direct-call 01-direct-call.c
strip 01-direct-call
1.确定程序入口
readelf -h 01-direct-call
ELF Header:
Magic: 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
Class: ELF64
Data: 2's complement, little endian
Version: 1 (current)
OS/ABI: UNIX - System V
ABI Version: 0
Type: DYN (Shared object file)
Machine: Advanced Micro Devices X86-64
Version: 0x1
Entry point address: 0x1040
Start of program headers: 64 (bytes into file)
Start of section headers: 12600 (bytes into file)
Flags: 0x0
Size of this header: 64 (bytes)
Size of program headers: 56 (bytes)
Number of program headers: 13
Size of section headers: 64 (bytes)
Number of section headers: 27
Section header string table index: 26
0x1040, 只是 <.text> 的开始地址
2.gdb 01-direct-call
(gdb) disassemble 0x1040 // strip 后无法使用disassemble命令
No function contains specified address.
(gdb) x/20i 0x1040 // 但可以使用x命令查看
0x1040: endbr64
0x1044: xor %ebp,%ebp
0x1046: mov %rdx,%r9
0x1049: pop %rsi
0x104a: mov %rsp,%rdx
0x104d: and $0xfffffffffffffff0,%rsp
0x1051: push %rax
0x1052: push %rsp
0x1053: lea 0x186(%rip),%r8 # 0x11e0
0x105a: lea 0x10f(%rip),%rcx # 0x1170
0x1061: lea 0xc1(%rip),%rdi # 0x1129 这才是main地址
0x1068: callq *0x2f72(%rip) # 0x3fe0
0x106e: hlt
0x106f: nop
0x1070: lea 0x2f99(%rip),%rdi # 0x4010
0x1077: lea 0x2f92(%rip),%rax # 0x4010
0x107e: cmp %rdi,%rax
0x1081: je 0x1098
0x1083: mov 0x2f4e(%rip),%rax # 0x3fd8
0x108a: test %rax,%rax
(gdb) x/20i 0x1129 // main
0x1129: endbr64
0x112d: push %rbp
0x112e: mov %rsp,%rbp
0x1131: sub $0x10,%rsp
0x1135: mov $0x0,%eax
0x113a: callq 0x1157
0x113f: movl $0x66,-0x4(%rbp)
0x1146: mov $0x0,%eax
0x114b: callq 0x1157
0x1150: mov $0x0,%eax
0x1155: leaveq
0x1156: retq
0x1157: endbr64
0x115b: push %rbp
0x115c: mov %rsp,%rbp
0x115f: addl $0x1,-0x4(%rbp)
0x1163: nop
0x1164: pop %rbp
0x1165: retq
0x1166: nopw %cs:0x0(%rax,%rax,1)
(gdb) x/20i 0x1157 // func
0x1157: endbr64
0x115b: push %rbp
0x115c: mov %rsp,%rbp
0x115f: addl $0x1,-0x4(%rbp)
0x1163: nop
0x1164: pop %rbp
0x1165: retq
0x1166: nopw %cs:0x0(%rax,%rax,1)
0x1170: endbr64
0x1174: push %r15
0x1176: lea 0x2c73(%rip),%r15 # 0x3df0
0x117d: push %r14
0x117f: mov %rdx,%r14
0x1182: push %r13
0x1184: mov %rsi,%r13
0x1187: push %r12
0x1189: mov %edi,%r12d
0x118c: push %rbp
0x118d: lea 0x2c64(%rip),%rbp # 0x3df8
0x1194: push %rbx
(gdb) b *0x1040
Note: breakpoint 1 also set at pc 0x1040.
Breakpoint 2 at 0x1040
(gdb) b *0x1129
Breakpoint 3 at 0x1129
(gdb) b *0x1157
Breakpoint 4 at 0x1157
(gdb) b *main
No symbol table is loaded. Use the "file" command.
(gdb) r
(gdb) c
#endif