function_pointer.c
#include <stdio.h>
typedef int (init_fnc_t)(void);//1
int printf1(void);
int printf2(void);
int printf3(void);
init_fnc_t *init_sequence[]={//2
printf1,
printf2,
printf3,
NULL,
};
int main()
{
init_fnc_t **init_fnc_ptr;//3
for (init_fnc_ptr = init_sequence; *init_fnc_ptr; ++init_fnc_ptr) {//4
if ((*init_fnc_ptr)() != 0) {//5
printf("error\n");
}
printf("%x###%x\n", init_fnc_ptr, *init_fnc_ptr);
}
return 0;
}
int printf1(void)
{
printf("1111111111111111\n");
return 0;
}
int printf2(void)
{
printf("22222222222222222\n");
return 0;
}
int printf3(void)
{
printf("33333333333333333\n");
return 0;
}
cc function_pointer.c
./a.out
1111111111111111
804a018###8048474
22222222222222222
804a01c###804848d
33333333333333333
804a020###80484a6
objdump -j .text -SI a.out > function_pointer.dis
a.out: file format elf32-i386
Disassembly of section .text:
08048360 <_start>:
8048360: 31 ed xor %ebp,%ebp
8048362: 5e pop %esi
8048363: 89 e1 mov %esp,%ecx
8048365: 83 e4 f0 and $0xfffffff0,%esp
8048368: 50 push %eax
8048369: 54 push %esp
804836a: 52 push %edx
804836b: 68 c0 84 04 08 push $0x80484c0
8048370: 68 d0 84 04 08 push $0x80484d0
8048375: 51 push %ecx
8048376: 56 push %esi
8048377: 68 14 84 04 08 push $0x8048414
804837c: e8 af ff ff ff call 8048330 <__libc_start_main@plt>
8048381: f4 hlt
8048382: 90 nop
8048383: 90 nop
8048384: 90 nop
8048385: 90 nop
8048386: 90 nop
8048387: 90 nop
8048388: 90 nop
8048389: 90 nop
804838a: 90 nop
804838b: 90 nop
804838c: 90 nop
804838d: 90 nop
804838e: 90 nop
804838f: 90 nop
08048390 <__do_global_dtors_aux>:
8048390: 55 push %ebp
8048391: 89 e5 mov %esp,%ebp
8048393: 53 push %ebx
8048394: 83 ec 04 sub $0x4,%esp
8048397: 80 3d 28 a0 04 08 00 cmpb $0x0,0x804a028
804839e: 75 3f jne 80483df <__do_global_dtors_aux+0x4f>
80483a0: a1 2c a0 04 08 mov 0x804a02c,%eax
80483a5: bb 18 9f 04 08 mov $0x8049f18,%ebx
80483aa: 81 eb 14 9f 04 08 sub $0x8049f14,%ebx
80483b0: c1 fb 02 sar $0x2,%ebx
80483b3: 83 eb 01 sub $0x1,%ebx
80483b6: 39 d8 cmp %ebx,%eax
80483b8: 73 1e jae 80483d8 <__do_global_dtors_aux+0x48>
80483ba: 8d b6 00 00 00 00 lea 0x0(%esi),%esi
80483c0: 83 c0 01 add $0x1,%eax
80483c3: a3 2c a0 04 08 mov %eax,0x804a02c
80483c8: ff 14 85 14 9f 04 08 call *0x8049f14(,%eax,4)
80483cf: a1 2c a0 04 08 mov 0x804a02c,%eax
80483d4: 39 d8 cmp %ebx,%eax
80483d6: 72 e8 jb 80483c0 <__do_global_dtors_aux+0x30>
80483d8: c6 05 28 a0 04 08 01 movb $0x1,0x804a028
80483df: 83 c4 04 add $0x4,%esp
80483e2: 5b pop %ebx
80483e3: 5d pop %ebp
80483e4: c3 ret
80483e5: 8d 74 26 00 lea 0x0(%esi,%eiz,1),%esi
80483e9: 8d bc 27 00 00 00 00 lea 0x0(%edi,%eiz,1),%edi
080483f0 <frame_dummy>:
80483f0: 55 push %ebp
80483f1: 89 e5 mov %esp,%ebp
80483f3: 83 ec 18 sub $0x18,%esp
80483f6: a1 1c 9f 04 08 mov 0x8049f1c,%eax
80483fb: 85 c0 test %eax,%eax
80483fd: 74 12 je 8048411 <frame_dummy+0x21>
80483ff: b8 00 00 00 00 mov $0x0,%eax
8048404: 85 c0 test %eax,%eax
8048406: 74 09 je 8048411 <frame_dummy+0x21>
8048408: c7 04 24 1c 9f 04 08 movl $0x8049f1c,(%esp)
804840f: ff d0 call *%eax
8048411: c9 leave
8048412: c3 ret
8048413: 90 nop
08048414 <main>:
8048414: 55 push %ebp
8048415: 89 e5 mov %esp,%ebp
8048417: 83 e4 f0 and $0xfffffff0,%esp
804841a: 83 ec 20 sub $0x20,%esp
804841d: c7 44 24 1c 18 a0 04 movl $0x804a018,0x1c(%esp)
8048424: 08
8048425: eb 3c jmp 8048463 <main+0x4f>
8048427: 8b 44 24 1c mov 0x1c(%esp),%eax
804842b: 8b 00 mov (%eax),%eax
804842d: ff d0 call *%eax
804842f: 85 c0 test %eax,%eax
8048431: 74 0c je 804843f <main+0x2b>
8048433: c7 04 24 80 85 04 08 movl $0x8048580,(%esp)
804843a: e8 11 ff ff ff call 8048350 <puts@plt>
804843f: 8b 44 24 1c mov 0x1c(%esp),%eax
8048443: 8b 10 mov (%eax),%edx
8048445: b8 86 85 04 08 mov $0x8048586,%eax
804844a: 89 54 24 08 mov %edx,0x8(%esp)
804844e: 8b 54 24 1c mov 0x1c(%esp),%edx
8048452: 89 54 24 04 mov %edx,0x4(%esp)
8048456: 89 04 24 mov %eax,(%esp)
8048459: e8 e2 fe ff ff call 8048340 <printf@plt>
804845e: 83 44 24 1c 04 addl $0x4,0x1c(%esp)
8048463: 8b 44 24 1c mov 0x1c(%esp),%eax
8048467: 8b 00 mov (%eax),%eax
8048469: 85 c0 test %eax,%eax
804846b: 75 ba jne 8048427 <main+0x13>
804846d: b8 00 00 00 00 mov $0x0,%eax
8048472: c9 leave
8048473: c3 ret
08048474 <printf1>: //a
8048474: 55 push %ebp
8048475: 89 e5 mov %esp,%ebp
8048477: 83 ec 18 sub $0x18,%esp
804847a: c7 04 24 8f 85 04 08 movl $0x804858f,(%esp)
8048481: e8 ca fe ff ff call 8048350 <puts@plt>
8048486: b8 00 00 00 00 mov $0x0,%eax
804848b: c9 leave
804848c: c3 ret
0804848d <printf2>: //b
804848d: 55 push %ebp
804848e: 89 e5 mov %esp,%ebp
8048490: 83 ec 18 sub $0x18,%esp
8048493: c7 04 24 a0 85 04 08 movl $0x80485a0,(%esp)
804849a: e8 b1 fe ff ff call 8048350 <puts@plt>
804849f: b8 00 00 00 00 mov $0x0,%eax
80484a4: c9 leave
80484a5: c3 ret
080484a6 <printf3>: //c
80484a6: 55 push %ebp
80484a7: 89 e5 mov %esp,%ebp
80484a9: 83 ec 18 sub $0x18,%esp
80484ac: c7 04 24 b2 85 04 08 movl $0x80485b2,(%esp)
80484b3: e8 98 fe ff ff call 8048350 <puts@plt>
80484b8: b8 00 00 00 00 mov $0x0,%eax
80484bd: c9 leave
80484be: c3 ret
80484bf: 90 nop
080484c0 <__libc_csu_fini>:
80484c0: 55 push %ebp
80484c1: 89 e5 mov %esp,%ebp
80484c3: 5d pop %ebp
80484c4: c3 ret
80484c5: 8d 74 26 00 lea 0x0(%esi,%eiz,1),%esi
80484c9: 8d bc 27 00 00 00 00 lea 0x0(%edi,%eiz,1),%edi
080484d0 <__libc_csu_init>:
80484d0: 55 push %ebp
80484d1: 89 e5 mov %esp,%ebp
80484d3: 57 push %edi
80484d4: 56 push %esi
80484d5: 53 push %ebx
80484d6: e8 4f 00 00 00 call 804852a <__i686.get_pc_thunk.bx>
80484db: 81 c3 19 1b 00 00 add $0x1b19,%ebx
80484e1: 83 ec 1c sub $0x1c,%esp
80484e4: e8 f7 fd ff ff call 80482e0 <_init>
80484e9: 8d bb 18 ff ff ff lea -0xe8(%ebx),%edi
80484ef: 8d 83 18 ff ff ff lea -0xe8(%ebx),%eax
80484f5: 29 c7 sub %eax,%edi
80484f7: c1 ff 02 sar $0x2,%edi
80484fa: 85 ff test %edi,%edi
80484fc: 74 24 je 8048522 <__libc_csu_init+0x52>
80484fe: 31 f6 xor %esi,%esi
8048500: 8b 45 10 mov 0x10(%ebp),%eax
8048503: 89 44 24 08 mov %eax,0x8(%esp)
8048507: 8b 45 0c mov 0xc(%ebp),%eax
804850a: 89 44 24 04 mov %eax,0x4(%esp)
804850e: 8b 45 08 mov 0x8(%ebp),%eax
8048511: 89 04 24 mov %eax,(%esp)
8048514: ff 94 b3 18 ff ff ff call *-0xe8(%ebx,%esi,4)
804851b: 83 c6 01 add $0x1,%esi
804851e: 39 fe cmp %edi,%esi
8048520: 72 de jb 8048500 <__libc_csu_init+0x30>
8048522: 83 c4 1c add $0x1c,%esp
8048525: 5b pop %ebx
8048526: 5e pop %esi
8048527: 5f pop %edi
8048528: 5d pop %ebp
8048529: c3 ret
0804852a <__i686.get_pc_thunk.bx>:
804852a: 8b 1c 24 mov (%esp),%ebx
804852d: c3 ret
804852e: 90 nop
804852f: 90 nop
08048530 <__do_global_ctors_aux>:
8048530: 55 push %ebp
8048531: 89 e5 mov %esp,%ebp
8048533: 53 push %ebx
8048534: 83 ec 04 sub $0x4,%esp
8048537: a1 0c 9f 04 08 mov 0x8049f0c,%eax
804853c: 83 f8 ff cmp $0xffffffff,%eax
804853f: 74 13 je 8048554 <__do_global_ctors_aux+0x24>
8048541: bb 0c 9f 04 08 mov $0x8049f0c,%ebx
8048546: 66 90 xchg %ax,%ax
8048548: 83 eb 04 sub $0x4,%ebx
804854b: ff d0 call *%eax
804854d: 8b 03 mov (%ebx),%eax
804854f: 83 f8 ff cmp $0xffffffff,%eax
8048552: 75 f4 jne 8048548 <__do_global_ctors_aux+0x18>
8048554: 83 c4 04 add $0x4,%esp
8048557: 5b pop %ebx
8048558: 5d pop %ebp
8048559: c3 ret
804855a: 90 nop
804855b: 90 nop
注意注释的1,2,3,4,5和反汇编a,b,c和程序输出,特别注意for (init_fnc_ptr = init_sequence; *init_fnc_ptr; ++init_fnc_ptr)中init_fnc_ptr = init_sequence而不是init_fnc_ptr = init_sequence[0]