Skip to content

Instantly share code, notes, and snippets.

@larsks
Created April 14, 2023 00:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save larsks/82450c9e7d130602d82ecb00a741afae to your computer and use it in GitHub Desktop.
Save larsks/82450c9e7d130602d82ecb00a741afae to your computer and use it in GitHub Desktop.
Embedding binary data in C programs
#include <stdint.h>
#include <stdio.h>
extern uint8_t rom_start[], rom_end[];
extern uint16_t rom_size;
int main() {
printf("rom_size: 0x%X\n", rom_size);
for (int i = 0; i < rom_size; i++) {
printf("%02x: %02x\n", i, *(rom_start + i));
}
return 0;
}
CFLAGS = -g
example: example.o rom.o
$(CC) -o $@ $^
rom.o: rom.S rom.bin
rom.bin:
echo -n -e '\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00' > $@
clean:
rm -f example *.o rom.bin
.section ".rodata"
.global rom_start
.global rom_end
.global rom_size
rom_start:
.incbin "rom.bin"
rom_end:
rom_size: .int rom_end - rom_start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment