I am currently wrapping my head around function binding and what binding actually is C++. Function binding is assigning an address to function? Does only refer to when the function is called? or does binding occur when function the function is called?
Here is the program
#include <iostream>
int one() { return 1; }
int main()
{
// 1. Does binding occur here when the function pointer is assigned the address of the function
int (*one_ptr)() = one;
// 2. or does binding occur here when the function is called
one_ptr();
}
Does binding occur when the function pointer is assigned the address of the function:
int (*one_ptr)() = one;
or does the binding occur when the function is called:
one_ptr();
Here is the relevant objdump of the program:
0000000000001169 <_Z3onev>:
1169: f3 0f 1e fa endbr64
116d: 55 push rbp
116e: 48 89 e5 mov rbp,rsp
1171: b8 01 00 00 00 mov eax,0x1
1176: 5d pop rbp
1177: c3 ret
0000000000001178 <main>:
1178: f3 0f 1e fa endbr64
117c: 55 push rbp
117d: 48 89 e5 mov rbp,rsp
1180: 48 83 ec 10 sub rsp,0x10
1184: 48 8d 05 de ff ff ff lea rax,[rip+0xffffffffffffffde] # 1169 <_Z3onev>
118b: 48 89 45 f8 mov QWORD PTR [rbp-0x8],rax
118f: 48 8b 45 f8 mov rax,QWORD PTR [rbp-0x8]
1193: ff d0 call rax
1195: b8 00 00 00 00 mov eax,0x0
119a: c9 leave
119b: c3 ret
This is the assembly version of the function pointer being declared and initialized
lea rax,[rip+0xffffffffffffffde] # 1169 <_Z3onev>
mov QWORD PTR [rbp-0x8],rax
Here, relative rip addressing is used to assign the address of the function to the local variable. The address of the function is stored in rax
as we can see here
lea rax,[rip+0xffffffffffffffde] # 1169 <_Z3onev>
So calling rax
makes sense. It is an indirect function call (I believe).
call rax
So, is the function is bound to 00000001169
, the address of one()
? And in this case, it's static bound because the objdump is able to determine the address of the function could be determined at compile time.