Last active
December 25, 2020 08:36
-
-
Save seisvelas/e88fe0114c1e10e9f6bf5a4130d94f92 to your computer and use it in GitHub Desktop.
Is a function pointer just the address of the label?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdint.h> | |
/* | |
Test whether a function pointer is really just | |
a pointer to the memory location of a label. | |
Which would make sense, only I never really looked | |
into function pointers, so I'm curious if this is | |
what they are. | |
If this doesn't work, I'll have to actually look it up | |
(like a little bitch) | |
*/ | |
int64_t self_label_address(void) { | |
asm( | |
"lea rax, QWORD [self_label_address]" | |
); | |
} | |
int main(void) { | |
int64_t rip = self_label_address(); | |
int64_t function_addr = (int64_t)self_label_address; | |
printf("RIP, in function: %ld\n", rip); | |
printf("label address: %ld\n", (long int)self_label_address); | |
printf("difference: %ld\n", rip - (int64_t)self_label_address); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So they are 8 bytes apart. In other words, one memory address away (since I'm on 64 bit Linux, an address is 8 bytes). Hmm. Why aren't they identical? From looking up how label addresses work, I think that it's like this:
Ie, if you use the label as a pointer, you get something further along than the label's actual location in memory. Here's the function in assembly that the compiler generates:
So I guess [self_label_address] is giving us the address of the
endbr64
instruction (since the two things in between are assembler directives, not 'real' instructions that would appear in the machine code). Hmm. This has been very educational!