As Iwillnotexist Idonotexist pointed outIwillnotexist Idonotexist pointed out, GCC does support nested functions in C, using the exact syntax I've shown above.
URL Rewriter Bot
As Iwillnotexist Idonotexist pointed out, GCC does support nested functions in C, using the exact syntax I've shown above.
As Iwillnotexist Idonotexist pointed out, GCC does support nested functions in C, using the exact syntax I've shown above.
This would be equivalent to something more verbose like this:
struct func_a_ctx { int a1, a5; }; void func_b(struct func_a_ctx *ctx, int p1, int p2) { /* Use variables from func_a() */ printf("a1=%d a5=%d\n", ctx->a1, ctx->a5); } void func_a(void) { int a2=2, a3=3, a4=4; struct func_a_ctx ctx = { .a1 = 0x1001, .a5 = 0x1005, }; func_b(&ctx, 1, 2); } This would be equivalent to something more verbose like this:
struct func_a_ctx { int a1, a5; }; void func_b(struct func_a_ctx *ctx, int p1, int p2) { /* Use variables from func_a() */ printf("a1=%d a5=%d\n", ctx->a1, ctx->a5); } void func_a(void) { int a2=2, a3=3, a4=4; struct func_a_ctx ctx = { .a1 = 0x1001, .a5 = 0x1005, }; func_b(&ctx, 1, 2); } 00000000004004dc <func_b.2172>: 4004dc: 55 push rbp 4004dd: 48 89 e5 mov rbp,rsp 4004e0: 48 83 ec 10 sub rsp,0x10 4004e4: 89 7d fc mov DWORD PTR [rbp-0x4],edi 4004e7: 89 75 f8 mov DWORD PTR [rbp-0x8],esi 4004ea: 4c 89 d0 mov rax,r10 ; ptr to parentcalling function "shared" vars 4004ed: 8b 48 04 mov ecx,DWORD PTR [rax+0x4] 4004f0: 8b 00 mov eax,DWORD PTR [rax] 4004f2: 89 c2 mov edx,eax 4004f4: 89 ce mov esi,ecx 4004f6: bf 10 06 40 00 mov edi,0x400610 4004fb: b8 00 00 00 00 mov eax,0x0 400500: e8 ab fe ff ff call 4003b0 <printf@plt> 400505: c9 leave 400506: c3 ret 0000000000400507 <func_a>: 400507: 55 push rbp 400508: 48 89 e5 mov rbp,rsp 40050b: 48 83 ec 20 sub rsp,0x20 40050f: c7 45 e4 01 10 00 00 mov DWORD PTR [rbp-0x1c],0x1001 400516: c7 45 fc 02 00 00 00 mov DWORD PTR [rbp-0x4],0x2 40051d: c7 45 f8 03 00 00 00 mov DWORD PTR [rbp-0x8],0x3 400524: c7 45 f4 04 00 00 00 mov DWORD PTR [rbp-0xc],0x4 40052b: c7 45 e0 05 10 00 00 mov DWORD PTR [rbp-0x20],0x1005 400532: 48 8d 45 e0 lea rax,[rbp-0x20] 400536: 49 89 c2 ; Pass a, b to the nested function 400536: mov r10,rax ; Pass ; in r10 ! 400539: be 02 00 00 00 mov esi,0x2 40053e: bf 01 00 00 00 mov edi,0x1 400543: e8 94 ff ff ff call 4004dc <func_b.2172> 400548: c9 leave 400549: c3 ret Output from objdump --no-show-raw-insn -d -Mintel
00000000004004dc <func_b.2172>: 4004dc: 55 push rbp 4004dd: 48 89 e5 mov rbp,rsp 4004e0: 48 83 ec 10 sub rsp,0x10 4004e4: 89 7d fc mov DWORD PTR [rbp-0x4],edi 4004e7: 89 75 f8 mov DWORD PTR [rbp-0x8],esi 4004ea: 4c 89 d0 mov rax,r10 ; ptr to parent vars 4004ed: 8b 48 04 mov ecx,DWORD PTR [rax+0x4] 4004f0: 8b 00 mov eax,DWORD PTR [rax] 4004f2: 89 c2 mov edx,eax 4004f4: 89 ce mov esi,ecx 4004f6: bf 10 06 40 00 mov edi,0x400610 4004fb: b8 00 00 00 00 mov eax,0x0 400500: e8 ab fe ff ff call 4003b0 <printf@plt> 400505: c9 leave 400506: c3 ret 0000000000400507 <func_a>: 400507: 55 push rbp 400508: 48 89 e5 mov rbp,rsp 40050b: 48 83 ec 20 sub rsp,0x20 40050f: c7 45 e4 01 10 00 00 mov DWORD PTR [rbp-0x1c],0x1001 400516: c7 45 fc 02 00 00 00 mov DWORD PTR [rbp-0x4],0x2 40051d: c7 45 f8 03 00 00 00 mov DWORD PTR [rbp-0x8],0x3 400524: c7 45 f4 04 00 00 00 mov DWORD PTR [rbp-0xc],0x4 40052b: c7 45 e0 05 10 00 00 mov DWORD PTR [rbp-0x20],0x1005 400532: 48 8d 45 e0 lea rax,[rbp-0x20] 400536: 49 89 c2 mov r10,rax ; Pass in r10 ! 400539: be 02 00 00 00 mov esi,0x2 40053e: bf 01 00 00 00 mov edi,0x1 400543: e8 94 ff ff ff call 4004dc <func_b.2172> 400548: c9 leave 400549: c3 ret 00000000004004dc <func_b.2172>: 4004dc: push rbp 4004dd: mov rbp,rsp 4004e0: sub rsp,0x10 4004e4: mov DWORD PTR [rbp-0x4],edi 4004e7: mov DWORD PTR [rbp-0x8],esi 4004ea: mov rax,r10 ; ptr to calling function "shared" vars 4004ed: mov ecx,DWORD PTR [rax+0x4] 4004f0: mov eax,DWORD PTR [rax] 4004f2: mov edx,eax 4004f4: mov esi,ecx 4004f6: mov edi,0x400610 4004fb: mov eax,0x0 400500: call 4003b0 <printf@plt> 400505: leave 400506: ret 0000000000400507 <func_a>: 400507: push rbp 400508: mov rbp,rsp 40050b: sub rsp,0x20 40050f: mov DWORD PTR [rbp-0x1c],0x1001 400516: mov DWORD PTR [rbp-0x4],0x2 40051d: mov DWORD PTR [rbp-0x8],0x3 400524: mov DWORD PTR [rbp-0xc],0x4 40052b: mov DWORD PTR [rbp-0x20],0x1005 400532: lea rax,[rbp-0x20] ; Pass a, b to the nested function 400536: mov r10,rax ; in r10 ! 400539: mov esi,0x2 40053e: mov edi,0x1 400543: call 4004dc <func_b.2172> 400548: leave 400549: ret Output from objdump --no-show-raw-insn -d -Mintel
Loading