Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I embed data into a Mac OS X mach-o binary file's TEXT Section?

I have a Mac OS X command-line tool that would benefit from having some data embedded in the binary file itself.

I know mach-o files support multiple segments, some of which can be used for storing arbitrary data. But I can't find a command-line tool to do that.

While I know there are other, probably simpler ways (e.g. convert the data file into C source code and have it get linked in by gcc) to get the data into my binary this problem has piqued my interest. Anyone know the magic?

like image 247
schwa Avatar asked Oct 22 '09 02:10

schwa


People also ask

What is binary file in Mac?

MacBinary is a file format that combines the two forks of a classic Mac OS file into a single file, along with HFS's extended metadata. The resulting file is suitable for transmission over FTP, the World Wide Web, and electronic mail.

How do I read a binary file on a Mac?

The file is probably an archive, application, or other binary file encoded to protect the Mac's forked file structure. To decode a MacBinary document in Mac OS, Mac OS X, or Windows, use StuffIt Expander. Simply drag and drop the file onto the StuffIt Expander icon and it will decode the file.

Where are binary files stored on Mac?

By putting /usr/local/bin after /usr/bin and /bin , Mac upends the usual system. Normally, you can put something into /usr/local/bin (say a second Perl interpreter, compiled in some non-standard way), and then a regular user will hit the custom one rather than the system-wide one first. This is good.


1 Answers

The linker, ld, takes an argument -sectcreate.

From the man page,

 -sectcreate segname sectname file
             The section sectname in the segment segname is created from
             the contents of file file. The combination of segname and 
             sectname must be unique  there cannot already be a 
             section (segname,sectname) from any other input.

GCC also has a section attribute that can be applied to a variable to say that it belongs in a non-standard section.

like image 69
Ken Avatar answered Oct 02 '22 15:10

Ken