Back for some “old school reasons”
I’ve not been a low level C programmer for a long time, but today I was having some fun in the middle of my weekend project of porting an “old” windows CE/KOBO app (LK8000) to iOS and I found something amazing in their code.
Back in the days it was quite common to store data files in the binary executable, I used to append zip files to my exe (zip stores the header at the end of file so I could just unzip my exe and get the content from there) or include binary data in my exe generating C code from a binary dump (you know char my file[] ={0x00…} etc. But today for the first time I saw something amazing :D and I like it so much that I need to share it and use it.
Basically to include multiple resources they simply use an assembly macro that bind the content of the resource file directly inside the .data section. Cool ! I’m amused because there are tons of script that generate a code block but they didn’t use any of them, they just went hard-core and I love it <3
https://github.com/LK8000/LK8000/blob/master/Common/Source/Resource/resource_data.S
.altmacro .macro binfile p q .globl \p&_begin .globl \p&_end .globl \p&_size \p&_begin: .incbin \q \p&_end: /* Put a “.byte 0” here if you know your data is text and you wish to use \p&_begin as a C string. It doesn’t hurt to leave it here even for binary data since it is not counted in \p_&size */ .byte 0 \p&_size: .int (\p&_end - \p&_begin) /* add alias for “_” decorated symname for avoid link error. */ .globl _\p&_begin .set _\p&_begin, \p&_begin .globl _\p&_end .set _\p&_end, \p&_end .globl _\p&_size .set _\p&_size, \p&_size
.endm
.section .rodata
binfile IDR_RASTER_EGM96S “../../Data/Bitmaps/egm96s.dem”
then a C macro create the variables declaration and they just access the data as a ptr*
I love it, that’s it :D of course, llvm didn’t like it, this is my fixed version
.macro binfile .globl $0_begin .globl $0_end .globl $0_size $0_begin: .incbin $1 $0_end: /* Put a “.byte 0” here if you know your data is text and you wish to use \p&_begin as a C string. It doesn’t hurt to leave it here even for binary data since it is not counted in \p_&size */ .byte 0 $0_size: .int ($0_end - $0_begin) /* add alias for “_” decorated symname for avoid link error. */ .globl _$0_begin .set _$0_begin, $0_begin .globl _$0_end .set _$0_end, $0_end .globl _$0_size .set _$0_size, $0_size .endm
.data
binfile IDR_RASTER_EGM96S,“../../Data/Bitmaps/egm96s.dem”














