sui is a injection tool for executable formats (ELF, PE, Mach-O) that allows you to embed files into existing binary and extract them at runtime.

cargo add libsui

github.com/littledivy/sui

$ cp $(command -v deno) .
$ echo "Hello, World!" > hello.txt
$ sui ./deno _SUI hello.txt ./deno_new
$ readelf -n ./deno_new

Displaying notes found in:
  Owner                Data size 	Description
  _SUI                 0x0000000a	Unknown note type: (0x00000000)
   description data: 73 6f 6d 65 20 74 65 78 74 0a

API Reference

Inject into existing Mach-O binary:

let executable = std::fs::read("executable")?;
libsui::inject_into_macho(&executable, "__CUSTOM", "__custom", "Hello, World!", |data| {
  std::fs::write("executable", data);
  Ok(())
})?;

Extract data at runtime:

let data = libsui::find_section("_CUSTOM")?;

Design

Windows

For PE executables, the resources are added into the .rsrc section, with the RT_RCDATA (raw data) type. The build-time equivalent is adding the binary data as a resource in the usual manner, such as the Resource Compiler, and marking it as RT_RCDATA. The run-time lookup uses the FindResource and LoadResource APIs.

macOS

For Mach-O executables, the resources are added as sections inside a new segment. The build-time equivalent of embedding binary data with this approach uses a linker flag: -sectcreate,__FOO,__foo,content.txt

Linux

For ELF executables, the resources are added as notes. The build-time equivalent is to use a linker script.