Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I embed the contents of a binary file in an executable on Mac OS X?

My command-line program's build process generates a binary file (over 500KB) that currently has to be referenced by path from argv. I would like to embed this file in the executable instead.

On Linux, it appears possible to use objcopy to make an object file from a binary file:

objcopy --input binary --output elf32-i386 --binary-architecture i386 myfile.dat myfile.o

However, the OS X developer toolchain doesn't include an objcopy command. Short of installing binutils, what are the possibilities?

I build my project from Xcode and the file is generated with a custom build rule.

like image 907
zneak Avatar asked Jan 06 '16 19:01

zneak


1 Answers

During the link phase, pass the arguments -sectcreate <segname> <sectname> <file> to the linker. If you're driving the linker via an invocation of the compiler, which is pretty common, you'd pass it as -Wl,-sectcreate,<segname>,<sectname>,<file>.

You'd make up segment and section names.

You would use the getsectdata() function along with _dyld_get_image_vmaddr_slide() to get a pointer to the data at runtime.

like image 147
Ken Thomases Avatar answered Sep 17 '22 21:09

Ken Thomases