-
-
Save tilkinsc/22f9021548a50b53928c6de7243c5152 to your computer and use it in GitHub Desktop.
Include binary file with gcc
This file contains hidden or 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> | |
#define STR2(x) #x | |
#define STR(x) STR2(x) | |
#define INCBIN(name, file) \ | |
__asm__(".section .rodata\n" \ | |
".global incbin_" STR(name) "_start\n" \ | |
".type incbin_" STR(name) "_start, @object\n" \ | |
".balign 16\n" \ | |
"incbin_" STR(name) "_start:\n" \ | |
".incbin \"" file "\"\n" \ | |
\ | |
".global incbin_" STR(name) "_end\n" \ | |
".type incbin_" STR(name) "_end, @object\n" \ | |
".balign 1\n" \ | |
"incbin_" STR(name) "_end:\n" \ | |
".byte 0\n" \ | |
); \ | |
extern const __attribute__((aligned(16))) void* incbin_ ## name ## _start; \ | |
extern const void* incbin_ ## name ## _end; \ | |
INCBIN(foobar, "binary.bin"); | |
int main() | |
{ | |
printf("start = %p\n", &incbin_foobar_start); | |
printf("end = %p\n", &incbin_foobar_end); | |
printf("size = %zu\n", (char*)&incbin_foobar_end - (char*)&incbin_foobar_start); | |
printf("first byte = 0x%02x\n", ((unsigned char*)&incbin_foobar_start)[0]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment