pub fn write(object: &Object) -> Result<Vec<u8>, WriteError>Expand description
Writes an object as an ELF file and returns the bytes.
The object is validated first. The output is deterministic.
§Errors
WriteError::Invalidif the object breaks a rule of the model.WriteError::ReservedSectionNamefor a relocatable object with a section named.note.GNU-stack, which the writer emits itself.- For executables:
WriteError::SegmentsSharePage,WriteError::NoRoomForHeaders, andWriteError::Unsupportedfor thread-local sections. WriteError::TooLargeif a table outgrows ELF’s 32-bit fields.
§Examples
use object_lang::{Architecture, Object, Section, SectionKind, Symbol};
let mut obj = Object::relocatable(Architecture::X86_64);
let text = obj.add_section(Section::new(".text", SectionKind::Text).with_data(vec![0xc3]));
obj.add_symbol(Symbol::function("ret", text, 0, 1));
let bytes = object_lang::elf::write(&obj)?;
assert_eq!(&bytes[..4], b"\x7fELF");
assert_eq!(object_lang::elf::read(&bytes)?, obj);