/// The index of an ELF section. #[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] pubstruct SectionIndex(pub u32);
/// The index of an ELF symbol. #[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] pubstruct SymbolIndex(pub u32);
/// A helper for writing ELF files. /// /// Writing uses a two phase approach. The first phase builds up all of the information /// that may need to be known ahead of time: /// - build string tables /// - reserve section indices /// - reserve symbol indices /// - reserve file ranges for headers and sections /// /// Some of the information has ordering requirements. For example, strings must be added /// to string tables before reserving the file range for the string table. Symbol indices /// must be reserved after reserving the section indices they reference. There are debug /// asserts to check some of these requirements. /// /// The second phase writes everything out in order. Thus the caller must ensure writing /// is in the same order that file ranges were reserved. There are debug asserts to assist /// with checking this. #[allow(missing_debug_implementations)] pubstruct Writer<'a> {
endian: Endianness,
is_64: bool,
is_mips64el: bool,
elf_align: usize,
/// Get the file class that will be written. fn class(&self) -> Class {
Class { is_64: self.is_64 }
}
/// Return the current file length that has been reserved. pubfn reserved_len(&self) -> usize { self.len
}
/// Return the current file length that has been written. #[allow(clippy::len_without_is_empty)] pubfn len(&self) -> usize { self.buffer.len()
}
/// Reserve a file range with the given size and starting alignment. /// /// Returns the aligned offset of the start of the range. /// /// `align_start` must be a power of two. pubfn reserve(&mutself, len: usize, align_start: usize) -> usize { if align_start > 1 { self.len = util::align(self.len, align_start);
} let offset = self.len; self.len += len;
offset
}
/// Write data. /// /// This is typically used to write section data. pubfn write(&mutself, data: &[u8]) { self.buffer.write_bytes(data);
}
/// Reserve the file range up to the given file offset. pubfn reserve_until(&mutself, offset: usize) {
debug_assert!(self.len <= offset); self.len = offset;
}
/// Write padding up to the given file offset. pubfn pad_until(&mutself, offset: usize) {
debug_assert!(self.buffer.len() <= offset); self.buffer.resize(offset);
}
/// Reserve the range for the file header. /// /// This must be at the start of the file. pubfn reserve_file_header(&mutself) {
debug_assert_eq!(self.len, 0); self.reserve(self.class().file_header_size(), 1);
}
/// Write the file header. /// /// This must be at the start of the file. /// /// Fields that can be derived from known information are automatically set by this function. pubfn write_file_header(&mutself, header: &FileHeader) -> Result<()> {
debug_assert_eq!(self.buffer.len(), 0);
let e_ehsize = self.class().file_header_size() as u16;
let e_phoff = self.segment_offset as u64; let e_phentsize = ifself.segment_num == 0 { 0
} else { self.class().program_header_size() as u16
}; // TODO: overflow let e_phnum = self.segment_num as u16;
let e_shoff = self.section_offset as u64; let e_shentsize = ifself.section_num == 0 { 0
} else { self.class().section_header_size() as u16
}; let e_shnum = ifself.section_num >= elf::SHN_LORESERVE.into() { 0
} else { self.section_num as u16
}; let e_shstrndx = ifself.shstrtab_index.0 >= elf::SHN_LORESERVE.into() {
elf::SHN_XINDEX
} else { self.shstrtab_index.0as u16
};
/// Reserve the range for the program headers. pubfn reserve_program_headers(&mutself, num: u32) {
debug_assert_eq!(self.segment_offset, 0); if num == 0 { return;
} self.segment_num = num; self.segment_offset = self.reserve(
num as usize * self.class().program_header_size(), self.elf_align,
);
}
/// Write alignment padding bytes prior to the program headers. pubfn write_align_program_headers(&mutself) { ifself.segment_offset == 0 { return;
}
util::write_align(self.buffer, self.elf_align);
debug_assert_eq!(self.segment_offset, self.buffer.len());
}
/// Write a program header. pubfn write_program_header(&mutself, header: &ProgramHeader) { let endian = self.endian; ifself.is_64 { let header = elf::ProgramHeader64 {
p_type: U32::new(endian, header.p_type),
p_flags: U32::new(endian, header.p_flags),
p_offset: U64::new(endian, header.p_offset),
p_vaddr: U64::new(endian, header.p_vaddr),
p_paddr: U64::new(endian, header.p_paddr),
p_filesz: U64::new(endian, header.p_filesz),
p_memsz: U64::new(endian, header.p_memsz),
p_align: U64::new(endian, header.p_align),
}; self.buffer.write(&header);
} else { let header = elf::ProgramHeader32 {
p_type: U32::new(endian, header.p_type),
p_offset: U32::new(endian, header.p_offset as u32),
p_vaddr: U32::new(endian, header.p_vaddr as u32),
p_paddr: U32::new(endian, header.p_paddr as u32),
p_filesz: U32::new(endian, header.p_filesz as u32),
p_memsz: U32::new(endian, header.p_memsz as u32),
p_flags: U32::new(endian, header.p_flags),
p_align: U32::new(endian, header.p_align as u32),
}; self.buffer.write(&header);
}
}
/// Reserve the section index for the null section header. /// /// The null section header is usually automatically reserved, /// but this can be used to force an empty section table. /// /// This must be called before [`Self::reserve_section_headers`]. pubfn reserve_null_section_index(&mutself) -> SectionIndex {
debug_assert_eq!(self.section_num, 0); ifself.section_num == 0 { self.section_num = 1;
}
SectionIndex(0)
}
/// Reserve a section table index. /// /// Automatically also reserves the null section header if required. /// /// This must be called before [`Self::reserve_section_headers`]. pubfn reserve_section_index(&mutself) -> SectionIndex {
debug_assert_eq!(self.section_offset, 0); ifself.section_num == 0 { self.section_num = 1;
} let index = self.section_num; self.section_num += 1;
SectionIndex(index)
}
/// Reserve the range for the section headers. /// /// This function does nothing if no sections were reserved. /// This must be called after [`Self::reserve_section_index`] /// and other functions that reserve section indices. pubfn reserve_section_headers(&mutself) {
debug_assert_eq!(self.section_offset, 0); ifself.section_num == 0 { return;
} self.section_offset = self.reserve( self.section_num as usize * self.class().section_header_size(), self.elf_align,
);
}
/// Write the null section header. /// /// This must be the first section header that is written. /// This function does nothing if no sections were reserved. pubfn write_null_section_header(&mutself) { ifself.section_num == 0 { return;
}
util::write_align(self.buffer, self.elf_align);
debug_assert_eq!(self.section_offset, self.buffer.len()); self.write_section_header(&SectionHeader {
name: None,
sh_type: 0,
sh_flags: 0,
sh_addr: 0,
sh_offset: 0,
sh_size: ifself.section_num >= elf::SHN_LORESERVE.into() { self.section_num.into()
} else { 0
},
sh_link: ifself.shstrtab_index.0 >= elf::SHN_LORESERVE.into() { self.shstrtab_index.0
} else { 0
}, // TODO: e_phnum overflow
sh_info: 0,
sh_addralign: 0,
sh_entsize: 0,
});
}
/// Add a section name to the section header string table. /// /// This will be stored in the `.shstrtab` section. /// /// This must be called before [`Self::reserve_shstrtab`]. pubfn add_section_name(&mutself, name: &'a [u8]) -> StringId {
debug_assert_eq!(self.shstrtab_offset, 0); self.shstrtab.add(name)
}
/// Reserve the range for the section header string table. /// /// This range is used for a section named `.shstrtab`. /// /// This function does nothing if no sections were reserved. /// This must be called after [`Self::add_section_name`]. /// and other functions that reserve section names and indices. pubfn reserve_shstrtab(&mutself) {
debug_assert_eq!(self.shstrtab_offset, 0); ifself.section_num == 0 { return;
} // Start with null section name. self.shstrtab_data = vec![0]; self.shstrtab.write(1, &mutself.shstrtab_data); self.shstrtab_offset = self.reserve(self.shstrtab_data.len(), 1);
}
/// Write the section header string table. /// /// This function does nothing if the section was not reserved. pubfn write_shstrtab(&mutself) { ifself.shstrtab_offset == 0 { return;
}
debug_assert_eq!(self.shstrtab_offset, self.buffer.len()); self.buffer.write_bytes(&self.shstrtab_data);
}
/// Reserve the section index for the section header string table. /// /// This must be called before [`Self::reserve_shstrtab`] /// and [`Self::reserve_section_headers`]. pubfn reserve_shstrtab_section_index(&mutself) -> SectionIndex { self.reserve_shstrtab_section_index_with_name(&b".shstrtab"[..])
}
/// Reserve the section index for the section header string table. /// /// This must be called before [`Self::reserve_shstrtab`] /// and [`Self::reserve_section_headers`]. pubfn reserve_shstrtab_section_index_with_name(&mutself, name: &'a [u8]) -> SectionIndex {
debug_assert_eq!(self.shstrtab_index, SectionIndex(0)); self.shstrtab_str_id = Some(self.add_section_name(name)); self.shstrtab_index = self.reserve_section_index(); self.shstrtab_index
}
/// Write the section header for the section header string table. /// /// This function does nothing if the section index was not reserved. pubfn write_shstrtab_section_header(&mutself) { ifself.shstrtab_index == SectionIndex(0) { return;
} self.write_section_header(&SectionHeader {
name: self.shstrtab_str_id,
sh_type: elf::SHT_STRTAB,
sh_flags: 0,
sh_addr: 0,
sh_offset: self.shstrtab_offset as u64,
sh_size: self.shstrtab_data.len() as u64,
sh_link: 0,
sh_info: 0,
sh_addralign: 1,
sh_entsize: 0,
});
}
/// Add a string to the string table. /// /// This will be stored in the `.strtab` section. /// /// This must be called before [`Self::reserve_strtab`]. pubfn add_string(&mutself, name: &'a [u8]) -> StringId {
debug_assert_eq!(self.strtab_offset, 0); self.need_strtab = true; self.strtab.add(name)
}
/// Return true if `.strtab` is needed. pubfn strtab_needed(&self) -> bool { self.need_strtab
}
/// Require the string table even if no strings were added. pubfn require_strtab(&mutself) { self.need_strtab = true;
}
/// Reserve the range for the string table. /// /// This range is used for a section named `.strtab`. /// /// This function does nothing if a string table is not required. /// This must be called after [`Self::add_string`]. pubfn reserve_strtab(&mutself) {
debug_assert_eq!(self.strtab_offset, 0); if !self.need_strtab { return;
} // Start with null string. self.strtab_data = vec![0]; self.strtab.write(1, &mutself.strtab_data); self.strtab_offset = self.reserve(self.strtab_data.len(), 1);
}
/// Write the string table. /// /// This function does nothing if the section was not reserved. pubfn write_strtab(&mutself) { ifself.strtab_offset == 0 { return;
}
debug_assert_eq!(self.strtab_offset, self.buffer.len()); self.buffer.write_bytes(&self.strtab_data);
}
/// Reserve the section index for the string table. /// /// You should check [`Self::strtab_needed`] before calling this /// unless you have other means of knowing if this section is needed. /// /// This must be called before [`Self::reserve_section_headers`]. pubfn reserve_strtab_section_index(&mutself) -> SectionIndex { self.reserve_strtab_section_index_with_name(&b".strtab"[..])
}
/// Reserve the section index for the string table. /// /// You should check [`Self::strtab_needed`] before calling this /// unless you have other means of knowing if this section is needed. /// /// This must be called before [`Self::reserve_section_headers`]. pubfn reserve_strtab_section_index_with_name(&mutself, name: &<span style='color:blue'>'a [u8]) -> SectionIndex {
debug_assert_eq!(self.strtab_index, SectionIndex(0)); self.strtab_str_id = Some(self.add_section_name(name)); self.strtab_index = self.reserve_section_index(); self.strtab_index
}
/// Write the section header for the string table. /// /// This function does nothing if the section index was not reserved. pubfn write_strtab_section_header(&mutself) { ifself.strtab_index == SectionIndex(0) { return;
} self.write_section_header(&SectionHeader {
name: self.strtab_str_id,
sh_type: elf::SHT_STRTAB,
sh_flags: 0,
sh_addr: 0,
sh_offset: self.strtab_offset as u64,
sh_size: self.strtab_data.len() as u64,
sh_link: 0,
sh_info: 0,
sh_addralign: 1,
sh_entsize: 0,
});
}
/// Reserve the null symbol table entry. /// /// This will be stored in the `.symtab` section. /// /// The null symbol table entry is usually automatically reserved, /// but this can be used to force an empty symbol table. /// /// This must be called before [`Self::reserve_symtab`]. pubfn reserve_null_symbol_index(&mutself) -> SymbolIndex {
debug_assert_eq!(self.symtab_offset, 0);
debug_assert_eq!(self.symtab_num, 0); self.symtab_num = 1; // The symtab must link to a strtab. self.need_strtab = true;
SymbolIndex(0)
}
/// Reserve a symbol table entry. /// /// This will be stored in the `.symtab` section. /// /// `section_index` is used to determine whether `.symtab_shndx` is required. /// /// Automatically also reserves the null symbol if required. /// Callers may assume that the returned indices will be sequential /// starting at 1. /// /// This must be called before [`Self::reserve_symtab`] and /// [`Self::reserve_symtab_shndx`]. pubfn reserve_symbol_index(&mutself, section_index: Option<SectionIndex>) -> SymbolIndex {
debug_assert_eq!(self.symtab_offset, 0);
debug_assert_eq!(self.symtab_shndx_offset, 0); ifself.symtab_num == 0 { self.symtab_num = 1; // The symtab must link to a strtab. self.need_strtab = true;
} let index = self.symtab_num; self.symtab_num += 1; iflet Some(section_index) = section_index { if section_index.0 >= elf::SHN_LORESERVE.into() { self.need_symtab_shndx = true;
}
}
SymbolIndex(index)
}
/// Return the number of reserved symbol table entries. /// /// Includes the null symbol. pubfn symbol_count(&self) -> u32 { self.symtab_num
}
/// Reserve the range for the symbol table. /// /// This range is used for a section named `.symtab`. /// This function does nothing if no symbols were reserved. /// This must be called after [`Self::reserve_symbol_index`]. pubfn reserve_symtab(&mutself) {
debug_assert_eq!(self.symtab_offset, 0); ifself.symtab_num == 0 { return;
} self.symtab_offset = self.reserve( self.symtab_num as usize * self.class().sym_size(), self.elf_align,
);
}
/// Write the null symbol. /// /// This must be the first symbol that is written. /// This function does nothing if no symbols were reserved. pubfn write_null_symbol(&mutself) { ifself.symtab_num == 0 { return;
}
util::write_align(self.buffer, self.elf_align);
debug_assert_eq!(self.symtab_offset, self.buffer.len()); ifself.is_64 { self.buffer.write(&elf::Sym64::<Endianness>::default());
} else { self.buffer.write(&elf::Sym32::<Endianness>::default());
}
/// Reserve the section index for the symbol table. /// /// This must be called before [`Self::reserve_section_headers`]. pubfn reserve_symtab_section_index(&mutself) -> SectionIndex { self.reserve_symtab_section_index_with_name(&b".symtab"[..])
}
/// Reserve the section index for the symbol table. /// /// This must be called before [`Self::reserve_section_headers`]. pubfn reserve_symtab_section_index_with_name(&mutself, name: &<span style='color:blue'>'a [u8]) -> SectionIndex {
debug_assert_eq!(self.symtab_index, SectionIndex(0)); self.symtab_str_id = Some(self.add_section_name(name)); self.symtab_index = self.reserve_section_index(); self.symtab_index
}
/// Return the section index of the symbol table. pubfn symtab_index(&mutself) -> SectionIndex { self.symtab_index
}
/// Write the section header for the symbol table. /// /// This function does nothing if the section index was not reserved. pubfn write_symtab_section_header(&mutself, num_local: u32) { ifself.symtab_index == SectionIndex(0) { return;
} self.write_section_header(&SectionHeader {
name: self.symtab_str_id,
sh_type: elf::SHT_SYMTAB,
sh_flags: 0,
sh_addr: 0,
sh_offset: self.symtab_offset as u64,
sh_size: self.symtab_num as u64 * self.class().sym_size() as u64,
sh_link: self.strtab_index.0,
sh_info: num_local,
sh_addralign: self.elf_align as u64,
sh_entsize: self.class().sym_size() as u64,
});
}
/// Return true if `.symtab_shndx` is needed. pubfn symtab_shndx_needed(&self) -> bool { self.need_symtab_shndx
}
/// Require the extended section indices for the symbol table even /// if no section indices are too large. pubfn require_symtab_shndx(&mutself) { self.need_symtab_shndx = true;
}
/// Reserve the range for the extended section indices for the symbol table. /// /// This range is used for a section named `.symtab_shndx`. /// This also reserves a section index. /// /// This function does nothing if extended section indices are not needed. /// This must be called after [`Self::reserve_symbol_index`]. pubfn reserve_symtab_shndx(&mutself) {
debug_assert_eq!(self.symtab_shndx_offset, 0); if !self.need_symtab_shndx { return;
} self.symtab_shndx_offset = self.reserve(self.symtab_num as usize * 4, ALIGN_SYMTAB_SHNDX); self.symtab_shndx_data.reserve(self.symtab_num as usize * 4);
}
/// Write the extended section indices for the symbol table. /// /// This function does nothing if the section was not reserved. pubfn write_symtab_shndx(&mutself) { ifself.symtab_shndx_offset == 0 { return;
}
util::write_align(self.buffer, ALIGN_SYMTAB_SHNDX);
debug_assert_eq!(self.symtab_shndx_offset, self.buffer.len());
debug_assert_eq!(self.symtab_num as usize * 4, self.symtab_shndx_data.len()); self.buffer.write_bytes(&self.symtab_shndx_data);
}
/// Reserve the section index for the extended section indices symbol table. /// /// You should check [`Self::symtab_shndx_needed`] before calling this /// unless you have other means of knowing if this section is needed. /// /// This must be called before [`Self::reserve_section_headers`]. pubfn reserve_symtab_shndx_section_index(&mutself) -> SectionIndex { self.reserve_symtab_shndx_section_index_with_name(&b".symtab_shndx"[..])
}
/// Reserve the section index for the extended section indices symbol table. /// /// You should check [`Self::symtab_shndx_needed`] before calling this /// unless you have other means of knowing if this section is needed. /// /// This must be called before [`Self::reserve_section_headers`]. pubfn reserve_symtab_shndx_section_index_with_name(&mutself, name: &'a [u8]) -> SectionIndex {
debug_assert!(self.symtab_shndx_str_id.is_none()); self.symtab_shndx_str_id = Some(self.add_section_name(name)); self.reserve_section_index()
}
/// Write the section header for the extended section indices for the symbol table. /// /// This function does nothing if the section index was not reserved. pubfn write_symtab_shndx_section_header(&mutself) { ifself.symtab_shndx_str_id.is_none() { return;
} let sh_size = ifself.symtab_shndx_offset == 0 { 0
} else {
(self.symtab_num * 4) as u64
}; self.write_section_header(&SectionHeader {
name: self.symtab_shndx_str_id,
sh_type: elf::SHT_SYMTAB_SHNDX,
sh_flags: 0,
sh_addr: 0,
sh_offset: self.symtab_shndx_offset as u64,
sh_size,
sh_link: self.symtab_index.0,
sh_info: 0,
sh_addralign: ALIGN_SYMTAB_SHNDX as u64,
sh_entsize: 4,
});
}
/// Add a string to the dynamic string table. /// /// This will be stored in the `.dynstr` section. /// /// This must be called before [`Self::reserve_dynstr`]. pubfn add_dynamic_string(&mutself, name: &'a [u8]) -> StringId {
debug_assert_eq!(self.dynstr_offset, 0); self.need_dynstr = true; self.dynstr.add(name)
}
/// Get a string that was previously added to the dynamic string table. /// /// Panics if the string was not added. pubfn get_dynamic_string(&self, name: &>'a [u8]) -> StringId { self.dynstr.get_id(name)
}
/// Return true if `.dynstr` is needed. pubfn dynstr_needed(&self) -> bool { self.need_dynstr
}
/// Require the dynamic string table even if no strings were added. pubfn require_dynstr(&mutself) { self.need_dynstr = true;
}
/// Reserve the range for the dynamic string table. /// /// This range is used for a section named `.dynstr`. /// /// This function does nothing if no dynamic strings were defined. /// This must be called after [`Self::add_dynamic_string`]. pubfn reserve_dynstr(&mutself) -> usize {
debug_assert_eq!(self.dynstr_offset, 0); if !self.need_dynstr { return0;
} // Start with null string. self.dynstr_data = vec![0]; self.dynstr.write(1, &mutself.dynstr_data); self.dynstr_offset = self.reserve(self.dynstr_data.len(), 1); self.dynstr_offset
}
/// Return the size of the dynamic string table. /// /// This must be called after [`Self::reserve_dynstr`]. pubfn dynstr_len(&mutself) -> usize {
debug_assert_ne!(self.dynstr_offset, 0); self.dynstr_data.len()
}
/// Write the dynamic string table. /// /// This function does nothing if the section was not reserved. pubfn write_dynstr(&mutself) { ifself.dynstr_offset == 0 { return;
}
debug_assert_eq!(self.dynstr_offset, self.buffer.len()); self.buffer.write_bytes(&self.dynstr_data);
}
/// Reserve the section index for the dynamic string table. /// /// You should check [`Self::dynstr_needed`] before calling this /// unless you have other means of knowing if this section is needed. /// /// This must be called before [`Self::reserve_section_headers`]. pubfn reserve_dynstr_section_index(&mutself) -> SectionIndex { self.reserve_dynstr_section_index_with_name(&b".dynstr"[..])
}
/// Reserve the section index for the dynamic string table. /// /// You should check [`Self::dynstr_needed`] before calling this /// unless you have other means of knowing if this section is needed. /// /// This must be called before [`Self::reserve_section_headers`]. pubfn reserve_dynstr_section_index_with_name(&mutself, name: &<span style='color:blue'>'a [u8]) -> SectionIndex {
debug_assert_eq!(self.dynstr_index, SectionIndex(0)); self.dynstr_str_id = Some(self.add_section_name(name)); self.dynstr_index = self.reserve_section_index(); self.dynstr_index
}
/// Return the section index of the dynamic string table. pubfn dynstr_index(&mutself) -> SectionIndex { self.dynstr_index
}
/// Write the section header for the dynamic string table. /// /// This function does nothing if the section index was not reserved. pubfn write_dynstr_section_header(&mutself, sh_addr: u64) { ifself.dynstr_index == SectionIndex(0) { return;
} self.write_section_header(&SectionHeader {
name: self.dynstr_str_id,
sh_type: elf::SHT_STRTAB,
sh_flags: elf::SHF_ALLOC.into(),
sh_addr,
sh_offset: self.dynstr_offset as u64,
sh_size: self.dynstr_data.len() as u64,
sh_link: 0,
sh_info: 0,
sh_addralign: 1,
sh_entsize: 0,
});
}
/// Reserve the null dynamic symbol table entry. /// /// This will be stored in the `.dynsym` section. /// /// The null dynamic symbol table entry is usually automatically reserved, /// but this can be used to force an empty dynamic symbol table. /// /// This must be called before [`Self::reserve_dynsym`]. pubfn reserve_null_dynamic_symbol_index(&mutself) -> SymbolIndex {
debug_assert_eq!(self.dynsym_offset, 0);
debug_assert_eq!(self.dynsym_num, 0); self.dynsym_num = 1;
SymbolIndex(0)
}
/// Reserve a dynamic symbol table entry. /// /// This will be stored in the `.dynsym` section. /// /// Automatically also reserves the null symbol if required. /// Callers may assume that the returned indices will be sequential /// starting at 1. /// /// This must be called before [`Self::reserve_dynsym`]. pubfn reserve_dynamic_symbol_index(&mutself) -> SymbolIndex {
debug_assert_eq!(self.dynsym_offset, 0); ifself.dynsym_num == 0 { self.dynsym_num = 1;
} let index = self.dynsym_num; self.dynsym_num += 1;
SymbolIndex(index)
}
/// Return the number of reserved dynamic symbols. /// /// Includes the null symbol. pubfn dynamic_symbol_count(&mutself) -> u32 { self.dynsym_num
}
/// Reserve the range for the dynamic symbol table. /// /// This range is used for a section named `.dynsym`. /// /// This function does nothing if no dynamic symbols were reserved. /// This must be called after [`Self::reserve_dynamic_symbol_index`]. pubfn reserve_dynsym(&mutself) -> usize {
debug_assert_eq!(self.dynsym_offset, 0); ifself.dynsym_num == 0 { return0;
} self.dynsym_offset = self.reserve( self.dynsym_num as usize * self.class().sym_size(), self.elf_align,
); self.dynsym_offset
}
/// Write the null dynamic symbol. /// /// This must be the first dynamic symbol that is written. /// This function does nothing if no dynamic symbols were reserved. pubfn write_null_dynamic_symbol(&mutself) { ifself.dynsym_num == 0 { return;
}
util::write_align(self.buffer, self.elf_align);
debug_assert_eq!(self.dynsym_offset, self.buffer.len()); ifself.is_64 { self.buffer.write(&elf::Sym64::<Endianness>::default());
} else { self.buffer.write(&elf::Sym32::<Endianness>::default());
}
}
/// Write a dynamic symbol. pubfn write_dynamic_symbol(&mutself, sym: &Sym) { let st_name = iflet Some(name) = sym.name { self.dynstr.get_offset(name) as u32
} else { 0
};
let st_shndx = iflet Some(section) = sym.section { if section.0 >= elf::SHN_LORESERVE as u32 { // TODO: we don't actually write out .dynsym_shndx yet. // This is unlikely to be needed though.
elf::SHN_XINDEX
} else {
section.0as u16
}
} else {
sym.st_shndx
};
/// Reserve the section index for the dynamic symbol table. /// /// This must be called before [`Self::reserve_section_headers`]. pubfn reserve_dynsym_section_index(&mutself) -> SectionIndex { self.reserve_dynsym_section_index_with_name(&b".dynsym"[..])
}
/// Reserve the section index for the dynamic symbol table. /// /// This must be called before [`Self::reserve_section_headers`]. pubfn reserve_dynsym_section_index_with_name(&mutself, name: &<span style='color:blue'>'a [u8]) -> SectionIndex {
debug_assert_eq!(self.dynsym_index, SectionIndex(0)); self.dynsym_str_id = Some(self.add_section_name(name)); self.dynsym_index = self.reserve_section_index(); self.dynsym_index
}
/// Return the section index of the dynamic symbol table. pubfn dynsym_index(&mutself) -> SectionIndex { self.dynsym_index
}
/// Write the section header for the dynamic symbol table. /// /// This function does nothing if the section index was not reserved. pubfn write_dynsym_section_header(&mutself, sh_addr: u64, num_local: u32) { ifself.dynsym_index == SectionIndex(0) { return;
} self.write_section_header(&SectionHeader {
name: self.dynsym_str_id,
sh_type: elf::SHT_DYNSYM,
sh_flags: elf::SHF_ALLOC.into(),
sh_addr,
sh_offset: self.dynsym_offset as u64,
sh_size: self.dynsym_num as u64 * self.class().sym_size() as u64,
sh_link: self.dynstr_index.0,
sh_info: num_local,
sh_addralign: self.elf_align as u64,
sh_entsize: self.class().sym_size() as u64,
});
}
/// Reserve the range for the `.dynamic` section. /// /// This function does nothing if `dynamic_num` is zero. pubfn reserve_dynamic(&mutself, dynamic_num: usize) -> usize {
debug_assert_eq!(self.dynamic_offset, 0); if dynamic_num == 0 { return0;
} self.dynamic_num = dynamic_num; self.dynamic_offset = self.reserve_dynamics(dynamic_num); self.dynamic_offset
}
/// Write alignment padding bytes prior to the `.dynamic` section. /// /// This function does nothing if the section was not reserved. pubfn write_align_dynamic(&mutself) { ifself.dynamic_offset == 0 { return;
}
util::write_align(self.buffer, self.elf_align);
debug_assert_eq!(self.dynamic_offset, self.buffer.len());
}
/// Reserve a file range for the given number of dynamic entries. /// /// Returns the offset of the range. pubfn reserve_dynamics(&mutself, dynamic_num: usize) -> usize { self.reserve(dynamic_num * self.class().dyn_size(), self.elf_align)
}
/// Write a dynamic string entry. pubfn write_dynamic_string(&mutself, tag: u32, id: StringId) { self.write_dynamic(tag, self.dynstr.get_offset(id) as u64);
}
/// Write a dynamic value entry. pubfn write_dynamic(&mutself, d_tag: u32, d_val: u64) { let endian = self.endian; ifself.is_64 { let d = elf::Dyn64 {
d_tag: U64::new(endian, d_tag.into()),
d_val: U64::new(endian, d_val),
}; self.buffer.write(&d);
} else { let d = elf::Dyn32 {
d_tag: U32::new(endian, d_tag),
d_val: U32::new(endian, d_val as u32),
}; self.buffer.write(&d);
}
}
/// Reserve the section index for the dynamic table. pubfn reserve_dynamic_section_index(&mutself) -> SectionIndex {
debug_assert!(self.dynamic_str_id.is_none()); self.dynamic_str_id = Some(self.add_section_name(&b".dynamic"[..])); self.reserve_section_index()
}
/// Write the section header for the dynamic table. /// /// This function does nothing if the section index was not reserved. pubfn write_dynamic_section_header(&mutself, sh_addr: u64) { ifself.dynamic_str_id.is_none() { return;
} self.write_section_header(&SectionHeader {
name: self.dynamic_str_id,
sh_type: elf::SHT_DYNAMIC,
sh_flags: (elf::SHF_WRITE | elf::SHF_ALLOC).into(),
sh_addr,
sh_offset: self.dynamic_offset as u64,
sh_size: (self.dynamic_num * self.class().dyn_size()) as u64,
sh_link: self.dynstr_index.0,
sh_info: 0,
sh_addralign: self.elf_align as u64,
sh_entsize: self.class().dyn_size() as u64,
});
}
/// Reserve a file range for a SysV hash section. /// /// `symbol_count` is the number of symbols in the hash, /// not the total number of symbols. pubfn reserve_hash(&mutself, bucket_count: u32, chain_count: u32) -> usize { self.hash_size = self.class().hash_size(bucket_count, chain_count); self.hash_offset = self.reserve(self.hash_size, ALIGN_HASH); self.hash_offset
}
/// Write a SysV hash section. /// /// `chain_count` is the number of symbols in the hash. /// The argument to `hash` will be in the range `0..chain_count`. pubfn write_hash<F>(&mutself, bucket_count: u32, chain_count: u32, hash: F) where
F: Fn(u32) -> Option<u32>,
{ letmut buckets = vec![U32::new(self.endian, 0); bucket_count as usize]; letmut chains = vec![U32::new(self.endian, 0); chain_count as usize]; for i in0..chain_count { iflet Some(hash) = hash(i) { let bucket = hash % bucket_count;
chains[i as usize] = buckets[bucket as usize];
buckets[bucket as usize] = U32::new(self.endian, i);
}
}
/// Reserve the section index for the SysV hash table. pubfn reserve_hash_section_index(&mutself) -> SectionIndex { self.reserve_hash_section_index_with_name(&b".hash"[..])
}
/// Reserve the section index for the SysV hash table. pubfn reserve_hash_section_index_with_name(&mutself, name: &an style='color:blue'>'a [u8]) -> SectionIndex {
debug_assert!(self.hash_str_id.is_none()); self.hash_str_id = Some(self.add_section_name(name)); self.reserve_section_index()
}
/// Write the section header for the SysV hash table. /// /// This function does nothing if the section index was not reserved. pubfn write_hash_section_header(&mutself, sh_addr: u64) { ifself.hash_str_id.is_none() { return;
} self.write_section_header(&SectionHeader {
name: self.hash_str_id,
sh_type: elf::SHT_HASH,
sh_flags: elf::SHF_ALLOC.into(),
sh_addr,
sh_offset: self.hash_offset as u64,
sh_size: self.hash_size as u64,
sh_link: self.dynsym_index.0,
sh_info: 0,
sh_addralign: ALIGN_HASH as u64,
sh_entsize: 4,
});
}
/// Reserve a file range for a GNU hash section. /// /// `symbol_count` is the number of symbols in the hash, /// not the total number of symbols. pubfn reserve_gnu_hash(
&mutself,
bloom_count: u32,
bucket_count: u32,
symbol_count: u32,
) -> usize { self.gnu_hash_size = self
.class()
.gnu_hash_size(bloom_count, bucket_count, symbol_count); self.gnu_hash_offset = self.reserve(self.gnu_hash_size, self.elf_align); self.gnu_hash_offset
}
/// Write a GNU hash section. /// /// `symbol_count` is the number of symbols in the hash. /// The argument to `hash` will be in the range `0..symbol_count`. /// /// This requires that symbols are already sorted by bucket. pubfn write_gnu_hash<F>(
&mutself,
symbol_base: u32,
bloom_shift: u32,
bloom_count: u32,
bucket_count: u32,
symbol_count: u32,
hash: F,
) where
F: Fn(u32) -> u32,
{
util::write_align(self.buffer, self.elf_align);
debug_assert_eq!(self.gnu_hash_offset, self.buffer.len()); self.buffer.write(&elf::GnuHashHeader {
bucket_count: U32::new(self.endian, bucket_count),
symbol_base: U32::new(self.endian, symbol_base),
bloom_count: U32::new(self.endian, bloom_count),
bloom_shift: U32::new(self.endian, bloom_shift),
});
// Calculate and write bloom filter. ifself.is_64 { letmut bloom_filters = vec![0; bloom_count as usize]; for i in0..symbol_count { let h = hash(i);
bloom_filters[((h / 64) & (bloom_count - 1)) as usize] |= 1 << (h % 64) | 1 << ((h >> bloom_shift) % 64);
} for bloom_filter in bloom_filters { self.buffer.write(&U64::new(self.endian, bloom_filter));
}
} else { letmut bloom_filters = vec![0; bloom_count as usize]; for i in0..symbol_count { let h = hash(i);
bloom_filters[((h / 32) & (bloom_count - 1)) as usize] |= 1 << (h % 32) | 1 << ((h >> bloom_shift) % 32);
} for bloom_filter in bloom_filters { self.buffer.write(&U32::new(self.endian, bloom_filter));
}
}
// Write buckets. // // This requires that symbols are already sorted by bucket. letmut bucket = 0; for i in0..symbol_count { let symbol_bucket = hash(i) % bucket_count; while bucket < symbol_bucket { self.buffer.write(&U32::new(self.endian, 0));
bucket += 1;
} if bucket == symbol_bucket { self.buffer.write(&U32::new(self.endian, symbol_base + i));
bucket += 1;
}
} while bucket < bucket_count { self.buffer.write(&U32::new(self.endian, 0));
bucket += 1;
}
// Write hash values. for i in0..symbol_count { letmut h = hash(i); if i == symbol_count - 1 || h % bucket_count != hash(i + 1) % bucket_count {
h |= 1;
} else {
h &= !1;
} self.buffer.write(&U32::new(self.endian, h));
}
}
/// Reserve the section index for the GNU hash table. pubfn reserve_gnu_hash_section_index(&mutself) -> SectionIndex { self.reserve_gnu_hash_section_index_with_name(&b".gnu.hash"[..])
}
/// Reserve the section index for the GNU hash table. pubfn reserve_gnu_hash_section_index_with_name(&mutself, name: &'a [u8]) -> SectionIndex {
debug_assert!(self.gnu_hash_str_id.is_none()); self.gnu_hash_str_id = Some(self.add_section_name(name)); self.reserve_section_index()
}
/// Write the section header for the GNU hash table. /// /// This function does nothing if the section index was not reserved. pubfn write_gnu_hash_section_header(&mutself, sh_addr: u64) { ifself.gnu_hash_str_id.is_none() { return;
} self.write_section_header(&SectionHeader {
name: self.gnu_hash_str_id,
sh_type: elf::SHT_GNU_HASH,
sh_flags: elf::SHF_ALLOC.into(),
sh_addr,
sh_offset: self.gnu_hash_offset as u64,
sh_size: self.gnu_hash_size as u64,
sh_link: self.dynsym_index.0,
sh_info: 0,
sh_addralign: self.elf_align as u64,
sh_entsize: ifself.is_64 { 0 } else { 4 },
});
}
/// Reserve the range for the `.gnu.version` section. /// /// This function does nothing if no dynamic symbols were reserved. pubfn reserve_gnu_versym(&mutself) -> usize {
debug_assert_eq!(self.gnu_versym_offset, 0); ifself.dynsym_num == 0 { return0;
} self.gnu_versym_offset = self.reserve(self.dynsym_num as usize * 2, ALIGN_GNU_VERSYM); self.gnu_versym_offset
}
/// Write the null symbol version entry. /// /// This must be the first symbol version that is written. /// This function does nothing if no dynamic symbols were reserved. pubfn write_null_gnu_versym(&mutself) { ifself.dynsym_num == 0 { return;
}
util::write_align(self.buffer, ALIGN_GNU_VERSYM);
debug_assert_eq!(self.gnu_versym_offset, self.buffer.len()); self.write_gnu_versym(0);
}
/// Write a symbol version entry. pubfn write_gnu_versym(&mutself, versym: u16) { self.buffer.write(&U16::new(self.endian, versym));
}
/// Reserve the section index for the `.gnu.version` section. pubfn reserve_gnu_versym_section_index(&mutself) -> SectionIndex { self.reserve_gnu_versym_section_index_with_name(&b".gnu.version"[..])
}
/// Reserve the section index for the `.gnu.version` section. pubfn reserve_gnu_versym_section_index_with_name(&mutself, name: &'a [u8]) -> SectionIndex {
debug_assert!(self.gnu_versym_str_id.is_none()); self.gnu_versym_str_id = Some(self.add_section_name(name)); self.reserve_section_index()
}
/// Write the section header for the `.gnu.version` section. /// /// This function does nothing if the section index was not reserved. pubfn write_gnu_versym_section_header(&mutself, sh_addr: u64) { ifself.gnu_versym_str_id.is_none() { return;
} self.write_section_header(&SectionHeader {
name: self.gnu_versym_str_id,
sh_type: elf::SHT_GNU_VERSYM,
sh_flags: elf::SHF_ALLOC.into(),
sh_addr,
sh_offset: self.gnu_versym_offset as u64,
sh_size: self.class().gnu_versym_size(self.dynsym_num as usize) as u64,
sh_link: self.dynsym_index.0,
sh_info: 0,
sh_addralign: ALIGN_GNU_VERSYM as u64,
sh_entsize: 2,
});
}
/// Reserve the range for the `.gnu.version_d` section. pubfn reserve_gnu_verdef(&mutself, verdef_count: usize, verdaux_count: usize) -> usize {
debug_assert_eq!(self.gnu_verdef_offset, 0); if verdef_count == 0 { return0;
} self.gnu_verdef_size = self.class().gnu_verdef_size(verdef_count, verdaux_count); self.gnu_verdef_offset = self.reserve(self.gnu_verdef_size, ALIGN_GNU_VERDEF); self.gnu_verdef_count = verdef_count as u16; self.gnu_verdef_remaining = self.gnu_verdef_count; self.gnu_verdef_offset
}
/// Write a version definition entry that shares the names of the next definition. /// /// This is typically useful when there are only two versions (including the base) /// and they have the same name. pubfn write_gnu_verdef_shared(&mutself, verdef: &Verdef) {
debug_assert_ne!(self.gnu_verdef_remaining, 0); self.gnu_verdef_remaining -= 1;
debug_assert_ne!(self.gnu_verdef_remaining, 0); let vd_next = mem::size_of::<elf::Verdef<Endianness>>() as u32;
debug_assert_ne!(verdef.aux_count, 0); self.gnu_verdaux_remaining = 0; let vd_aux = 2 * mem::size_of::<elf::Verdef<Endianness>>() as u32;
/// Write a version definition auxiliary entry. pubfn write_gnu_verdaux(&mutself, name: StringId) {
debug_assert_ne!(self.gnu_verdaux_remaining, 0); self.gnu_verdaux_remaining -= 1; let vda_next = ifself.gnu_verdaux_remaining == 0 { 0
} else {
mem::size_of::<elf::Verdaux<Endianness>>() as u32
}; self.buffer.write(&elf::Verdaux {
vda_name: U32::new(self.endian, self.dynstr.get_offset(name) as u32),
vda_next: U32::new(self.endian, vda_next),
});
}
/// Reserve the section index for the `.gnu.version_d` section. pubfn reserve_gnu_verdef_section_index(&mutself) -> SectionIndex { self.reserve_gnu_verdef_section_index_with_name(&b".gnu.version_d"[..])
}
/// Reserve the section index for the `.gnu.version_d` section. pubfn reserve_gnu_verdef_section_index_with_name(&mutself, name: &'a [u8]) -> SectionIndex {
debug_assert!(self.gnu_verdef_str_id.is_none()); self.gnu_verdef_str_id = Some(self.add_section_name(name)); self.reserve_section_index()
}
/// Write the section header for the `.gnu.version_d` section. /// /// This function does nothing if the section index was not reserved. pubfn write_gnu_verdef_section_header(&mutself, sh_addr: u64) { ifself.gnu_verdef_str_id.is_none() { return;
} self.write_section_header(&SectionHeader {
name: self.gnu_verdef_str_id,
sh_type: elf::SHT_GNU_VERDEF,
sh_flags: elf::SHF_ALLOC.into(),
sh_addr,
sh_offset: self.gnu_verdef_offset as u64,
sh_size: self.gnu_verdef_size as u64,
sh_link: self.dynstr_index.0,
sh_info: self.gnu_verdef_count.into(),
sh_addralign: ALIGN_GNU_VERDEF as u64,
sh_entsize: 0,
});
}
/// Reserve the range for the `.gnu.version_r` section. pubfn reserve_gnu_verneed(&mutself, verneed_count: usize, vernaux_count: usize) -> usize {
debug_assert_eq!(self.gnu_verneed_offset, 0); if verneed_count == 0 { return0;
} self.gnu_verneed_size = self.class().gnu_verneed_size(verneed_count, vernaux_count); self.gnu_verneed_offset = self.reserve(self.gnu_verneed_size, ALIGN_GNU_VERNEED); self.gnu_verneed_count = verneed_count as u16; self.gnu_verneed_remaining = self.gnu_verneed_count; self.gnu_verneed_offset
}
/// Write a version need auxiliary entry. pubfn write_gnu_vernaux(&mutself, vernaux: &Vernaux) {
debug_assert_ne!(self.gnu_vernaux_remaining, 0); self.gnu_vernaux_remaining -= 1; let vna_next = ifself.gnu_vernaux_remaining == 0 { 0
} else {
mem::size_of::<elf::Vernaux<Endianness>>() as u32
}; self.buffer.write(&elf::Vernaux {
vna_hash: U32::new(self.endian, elf::hash(self.dynstr.get_string(vernaux.name))),
vna_flags: U16::new(self.endian, vernaux.flags),
vna_other: U16::new(self.endian, vernaux.index),
vna_name: U32::new(self.endian, self.dynstr.get_offset(vernaux.name) as u32),
vna_next: U32::new(self.endian, vna_next),
});
}
/// Reserve the section index for the `.gnu.version_r` section. pubfn reserve_gnu_verneed_section_index(&mutself) -> SectionIndex { self.reserve_gnu_verneed_section_index_with_name(&b".gnu.version_r"[..])
}
/// Reserve the section index for the `.gnu.version_r` section. pubfn reserve_gnu_verneed_section_index_with_name(&mutself, name: &'a [u8]) -> SectionIndex {
debug_assert!(self.gnu_verneed_str_id.is_none()); self.gnu_verneed_str_id = Some(self.add_section_name(name)); self.reserve_section_index()
}
/// Write the section header for the `.gnu.version_r` section. /// /// This function does nothing if the section index was not reserved. pubfn write_gnu_verneed_section_header(&mutself, sh_addr: u64) { ifself.gnu_verneed_str_id.is_none() { return;
} self.write_section_header(&SectionHeader {
name: self.gnu_verneed_str_id,
sh_type: elf::SHT_GNU_VERNEED,
sh_flags: elf::SHF_ALLOC.into(),
sh_addr,
sh_offset: self.gnu_verneed_offset as u64,
sh_size: self.gnu_verneed_size as u64,
sh_link: self.dynstr_index.0,
sh_info: self.gnu_verneed_count.into(),
sh_addralign: ALIGN_GNU_VERNEED as u64,
sh_entsize: 0,
});
}
/// Reserve the section index for the `.gnu.attributes` section. pubfn reserve_gnu_attributes_section_index(&mutself) -> SectionIndex { self.reserve_gnu_attributes_section_index_with_name(&b".gnu.attributes"[..])
}
/// Reserve the section index for the `.gnu.attributes` section. pubfn reserve_gnu_attributes_section_index_with_name(
&mutself,
name: &'a [u8],
) -> SectionIndex {
debug_assert!(self.gnu_attributes_str_id.is_none()); self.gnu_attributes_str_id = Some(self.add_section_name(name)); self.reserve_section_index()
}
/// Reserve the range for the `.gnu.attributes` section. pubfn reserve_gnu_attributes(&mutself, gnu_attributes_size: usize) -> usize {
debug_assert_eq!(self.gnu_attributes_offset, 0); if gnu_attributes_size == 0 { return0;
} self.gnu_attributes_size = gnu_attributes_size; self.gnu_attributes_offset = self.reserve(self.gnu_attributes_size, self.elf_align); self.gnu_attributes_offset
}
/// Write the section header for the `.gnu.attributes` section. /// /// This function does nothing if the section index was not reserved. pubfn write_gnu_attributes_section_header(&mutself) { ifself.gnu_attributes_str_id.is_none() { return;
} self.write_section_header(&SectionHeader {
name: self.gnu_attributes_str_id,
sh_type: elf::SHT_GNU_ATTRIBUTES,
sh_flags: 0,
sh_addr: 0,
sh_offset: self.gnu_attributes_offset as u64,
sh_size: self.gnu_attributes_size as u64,
sh_link: self.dynstr_index.0,
sh_info: 0, // TODO
sh_addralign: self.elf_align as u64,
sh_entsize: 0,
});
}
/// Write the data for the `.gnu.attributes` section. pubfn write_gnu_attributes(&mutself, data: &[u8]) { ifself.gnu_attributes_offset == 0 { return;
}
util::write_align(self.buffer, self.elf_align);
debug_assert_eq!(self.gnu_attributes_offset, self.buffer.len()); self.buffer.write_bytes(data);
}
/// Reserve a file range for the given number of relocations. /// /// Returns the offset of the range. pubfn reserve_relocations(&mutself, count: usize, is_rela: bool) -> usize { self.reserve(count * self.class().rel_size(is_rela), self.elf_align)
}
/// Write alignment padding bytes prior to a relocation section. pubfn write_align_relocation(&mutself) {
util::write_align(self.buffer, self.elf_align);
}
/// Write a relocation. pubfn write_relocation(&mutself, is_rela: bool, rel: &Rel) { let endian = self.endian; ifself.is_64 { if is_rela { let rel = elf::Rela64 {
r_offset: U64::new(endian, rel.r_offset),
r_info: elf::Rela64::r_info(endian, self.is_mips64el, rel.r_sym, rel.r_type),
r_addend: I64::new(endian, rel.r_addend),
}; self.buffer.write(&rel);
} else { let rel = elf::Rel64 {
r_offset: U64::new(endian, rel.r_offset),
r_info: elf::Rel64::r_info(endian, rel.r_sym, rel.r_type),
}; self.buffer.write(&rel);
}
} else { if is_rela { let rel = elf::Rela32 {
r_offset: U32::new(endian, rel.r_offset as u32),
r_info: elf::Rel32::r_info(endian, rel.r_sym, rel.r_type as u8),
r_addend: I32::new(endian, rel.r_addend as i32),
}; self.buffer.write(&rel);
} else { let rel = elf::Rel32 {
r_offset: U32::new(endian, rel.r_offset as u32),
r_info: elf::Rel32::r_info(endian, rel.r_sym, rel.r_type as u8),
}; self.buffer.write(&rel);
}
}
}
/// Write the section header for a relocation section. /// /// `section` is the index of the section the relocations apply to, /// or 0 if none. /// /// `symtab` is the index of the symbol table the relocations refer to, /// or 0 if none. /// /// `offset` is the file offset of the relocations. pubfn write_relocation_section_header(
&mutself,
name: StringId,
section: SectionIndex,
symtab: SectionIndex,
offset: usize,
count: usize,
is_rela: bool,
) { self.write_section_header(&SectionHeader {
name: Some(name),
sh_type: if is_rela { elf::SHT_RELA } else { elf::SHT_REL },
sh_flags: elf::SHF_INFO_LINK.into(),
sh_addr: 0,
sh_offset: offset as u64,
sh_size: (count * self.class().rel_size(is_rela)) as u64,
sh_link: symtab.0,
sh_info: section.0,
sh_addralign: self.elf_align as u64,
sh_entsize: self.class().rel_size(is_rela) as u64,
});
}
/// Reserve a file range for a COMDAT section. /// /// `count` is the number of sections in the COMDAT group. /// /// Returns the offset of the range. pubfn reserve_comdat(&mutself, count: usize) -> usize { self.reserve((count + 1) * 4, 4)
}
/// Write `GRP_COMDAT` at the start of the COMDAT section. pubfn write_comdat_header(&mutself) {
util::write_align(self.buffer, 4); self.buffer.write(&U32::new(self.endian, elf::GRP_COMDAT));
}
/// Write an entry in a COMDAT section. pubfn write_comdat_entry(&mutself, entry: SectionIndex) { self.buffer.write(&U32::new(self.endian, entry.0));
}
/// Return a helper for writing an attributes section. pubfn attributes_writer(&self) -> AttributesWriter {
AttributesWriter::new(self.endian)
}
}
/// A helper for writing an attributes section. /// /// Attributes have a variable length encoding, so it is awkward to write them in a /// single pass. Instead, we build the entire attributes section data in memory, using /// placeholders for unknown lengths that are filled in later. #[allow(missing_debug_implementations)] pubstruct AttributesWriter {
endian: Endianness,
data: Vec<u8>,
subsection_offset: usize,
subsubsection_offset: usize,
}
impl AttributesWriter { /// Create a new `AttributesWriter` for the given endianness. pubfn new(endian: Endianness) -> Self {
AttributesWriter {
endian,
data: vec![0x41],
subsection_offset: 0,
subsubsection_offset: 0,
}
}
/// Start a new subsection with the given vendor name. pubfn start_subsection(&mutself, vendor: &[u8]) {
debug_assert_eq!(self.subsection_offset, 0);
debug_assert_eq!(self.subsubsection_offset, 0); self.subsection_offset = self.data.len(); self.data.extend_from_slice(&[0; 4]); self.data.extend_from_slice(vendor); self.data.push(0);
}
/// End the subsection. /// /// The subsection length is automatically calculated and written. pubfn end_subsection(&mutself) {
debug_assert_ne!(self.subsection_offset, 0);
debug_assert_eq!(self.subsubsection_offset, 0); let length = self.data.len() - self.subsection_offset; self.data[self.subsection_offset..][..4]
.copy_from_slice(pod::bytes_of(&U32::new(self.endian, length as u32))); self.subsection_offset = 0;
}
/// Start a new sub-subsection with the given tag. pubfn start_subsubsection(&mutself, tag: u8) {
debug_assert_ne!(self.subsection_offset, 0);
debug_assert_eq!(self.subsubsection_offset, 0); self.subsubsection_offset = self.data.len(); self.data.push(tag); self.data.extend_from_slice(&[0; 4]);
}
/// Write a section or symbol index to the sub-subsection. /// /// The user must also call this function to write the terminating 0 index. pubfn write_subsubsection_index(&mutself, index: u32) {
debug_assert_ne!(self.subsection_offset, 0);
debug_assert_ne!(self.subsubsection_offset, 0);
util::write_uleb128(&mutself.data, u64::from(index));
}
/// Write raw index data to the sub-subsection. /// /// The terminating 0 index is automatically written. pubfn write_subsubsection_indices(&mutself, indices: &[u8]) {
debug_assert_ne!(self.subsection_offset, 0);
debug_assert_ne!(self.subsubsection_offset, 0); self.data.extend_from_slice(indices); self.data.push(0);
}
/// Write an attribute tag to the sub-subsection. pubfn write_attribute_tag(&mutself, tag: u64) {
debug_assert_ne!(self.subsection_offset, 0);
debug_assert_ne!(self.subsubsection_offset, 0);
util::write_uleb128(&mutself.data, tag);
}
/// Write an attribute integer value to the sub-subsection. pubfn write_attribute_integer(&mutself, value: u64) {
debug_assert_ne!(self.subsection_offset, 0);
debug_assert_ne!(self.subsubsection_offset, 0);
util::write_uleb128(&mutself.data, value);
}
/// Write an attribute string value to the sub-subsection. /// /// The value must not include the null terminator. pubfn write_attribute_string(&mutself, value: &[u8]) {
debug_assert_ne!(self.subsection_offset, 0);
debug_assert_ne!(self.subsubsection_offset, 0); self.data.extend_from_slice(value); self.data.push(0);
}
/// Write raw attribute data to the sub-subsection. pubfn write_subsubsection_attributes(&mutself, attributes: &[u8]) {
debug_assert_ne!(self.subsection_offset, 0);
debug_assert_ne!(self.subsubsection_offset, 0); self.data.extend_from_slice(attributes);
}
/// End the sub-subsection. /// /// The sub-subsection length is automatically calculated and written. pubfn end_subsubsection(&mutself) {
debug_assert_ne!(self.subsection_offset, 0);
debug_assert_ne!(self.subsubsection_offset, 0); let length = self.data.len() - self.subsubsection_offset; self.data[self.subsubsection_offset + 1..][..4]
.copy_from_slice(pod::bytes_of(&U32::new(self.endian, length as u32))); self.subsubsection_offset = 0;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.