Skip to main content

executable

Function executable 

Source
pub fn executable(
    arch: Architecture,
    code: &[u8],
) -> Result<Vec<u8>, WriteError>
Expand description

Builds a static executable from machine code: the one-call path to a program the OS can run.

code becomes a .text section at code_address, 16-byte aligned, and execution starts at its first byte, where a global _start symbol marks it. The code must not need relocations or data of its own beyond what it carries; for anything more, build an executable Object and write() it.

§Errors

WriteError::Invalid with EntryNotExecutable if code is empty.

§Examples

exit(42) on Linux x86-64, hand-assembled:

use object_lang::Architecture;

let code = [
    0xbf, 0x2a, 0x00, 0x00, 0x00, // mov edi, 42
    0xb8, 0x3c, 0x00, 0x00, 0x00, // mov eax, 60 (exit)
    0x0f, 0x05, //                   syscall
];
let program = object_lang::elf::executable(Architecture::X86_64, &code)?;
// Write `program` to a file, mark it executable, run it: it exits with status 42.
assert_eq!(&program[..4], b"\x7fELF");