Llvm-objcopy --add-symbol on MachO object file

I am trying to include a binary text blob inside a mach-o object file on an arm64 system.
I am using LLVM version 16.0.6
The initial command was:
llvm-objcopy -I binary -O mach-o-arm64 string_blob.txt string_blob.o
It seems objcopy only supports elf format (return: invalid output format).

I thought, I might be able to add a symbol on an already existing object file.

gcc -c -o empty.o empty.c
llvm-objcopy --add-section __TEXT,__CUSTOM=string_blob.txt empty.o empty_section.o
llvm-objcopy --add-symbol _binary_string_blob_txt_start=__TEXT,__CUSTOM:0 empty_symbols full_empty

add-section works as expected:
objdump -s new_empty.o return:

new_empty.o: file format mach-o arm64
Contents of section __TEXT,__CUSTOM:
0138 626c6f62 62792062 6c6f6262 7920626c blobby blobby bl
0148 6f626279 00 obby.

Regrettably it seems llvm-objcopy --add-symbol doesn’t work with mach-o format.

error: option is not supported for MachO

This is a bit new to me.
Is there a different way to achieve this? I saw some comments regarding mach-o support, Is it something that is planned?
I tried using gnu binutils without success, it breaks the file architecture and cannot be linked afterward.

I found this link wiki:embedding_resources_in_executables [Robin Gareus] claiming:
Due to the nature of OSX binaries, referencing the data-section in the c-code is not possible with a simple extern unsigned char . The linker does not know which architecture will be used and can not provide an address. The mach binary format which is used by OSX needs to be inspected at runtime when the architecture is known and map the relevant data after the application is started

Would it be possible to circumvent this issue if the file is a single architecture and throw error otherwise?

Mach-O objects are supported by llvm-objcopy, but not all options have been implemented for it. In some cases, it is because the options do not make sense. --add-symbol and non-ELF targets for the -O option are some examples, as you have discovered.

Paging @smeenai and @keith, who have been more involved with the Mach-O objcopy than me (there are others too, but I don’t know their discourse handles). Hopefully one of them can assist further.

1 Like