Skip to content

Instantly share code, notes, and snippets.

@Leandros
Last active October 14, 2016 13:40
Show Gist options
  • Save Leandros/3a7fa4a415fd3e630ff407d4ba02a88e to your computer and use it in GitHub Desktop.
Save Leandros/3a7fa4a415fd3e630ff407d4ba02a88e to your computer and use it in GitHub Desktop.
Define function by objectcode
/*
* !!! Linux Version !!!
*
* Compile with: gcc -o foo foo_linux.c
*
* $ ./foo
* foo(5) = 10
*/
#include <stdio.h>
unsigned char __foo[] __attribute__((section(".text"))) = {
0x8d, 0x04, 0x3f, 0xc3
};
int (*foo)(int) = (int (*)(int))__foo;
int main(void)
{
printf("foo(5) = %d\n", foo(5));
return 0;
}
/*
* !!! macOS Version !!!
*
* Compile with: gcc -o foo foo_mac.c
*
* $ ./foo
* foo(5) = 10
*/
#include <stdio.h>
unsigned char __foo[] __attribute__((section("__TEXT,__text"))) = {
0x55, 0x48, 0x89, 0xe5, 0x8d, 0x04, 0x3f, 0x5d, 0xc3
};
int (*foo)(int) = (int (*)(int))__foo;
int main(void)
{
printf("foo(5) = %d\n", foo(5));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment