Struct Section

Source
pub struct Section {
    pub endian: Endian,
    /* private fields */
}
Expand description

A section is a sequence of bytes, constructed by appending bytes to the end.

Sections have a convenient and flexible set of member functions for appending data in various formats: big-endian and little-endian signed and unsigned values of different sizes, and raw blocks of bytes.

There are methods for appending each of u8, u16, u32, and u64 in each of big, little, or the Section’s default endianness. The method names are {D,L,B}{8,16,32,64} for each variant, so D16 writes a u16 in the Section’s default endianness, and L64 writes a u64 in little-endian byte order.

Each of these methods also accepts a Label to write non-constant values.

See the module-level documentation for examples.

Fields§

§endian: Endian

The current endianness of the writer.

This sets the behavior of the D appending functions.

Implementations§

Source§

impl Section

Source

pub fn new() -> Section

Construct a Section with platform-default endianness.

Source

pub fn with_endian(endian: Endian) -> Section

Construct a Section with endian endianness.

Source

pub fn size(&self) -> u64

Return the current size of the section.

Source

pub fn final_size(&self) -> Label

Return a Label that will resolve to the final size of the section.

This label is undefined until get_contents is called.

Source

pub fn get_contents(self) -> Option<Vec<u8>>

Get the contents of this section as a slice of bytes.

Consumes the section. If there were still undefined labels, return None.

Source

pub fn start(&self) -> Label

Return a label representing the start of the section.

It is up to the user whether this label represents the section’s position in an object file, the section’s address in memory, or what have you; some applications may need both, in which case this simple-minded interface won’t be enough. This class only provides a single start label, for use with the here and mark methods.

Source

pub fn here(&self) -> Label

A label representing the point at which the next appended item will appear in the section, relative to start.

Source

pub fn set_start_const(self, value: u64) -> Section

Set the value of start to value.

Source

pub fn mark(self, label: &Label) -> Section

Set label to here, and return this section.

Source

pub fn append_bytes(self, data: &[u8]) -> Section

Append data to the end of this section, and return this section.

Source

pub fn append_section<S: Into<Section>>(self, section: S) -> Section

Append section’s contents to the end of this section.

Any Labels that were appended to section will not be resolved until this section is finalized. Return this section.

Source

pub fn append_repeated(self, byte: u8, count: usize) -> Section

Append count copies of byte to the end of this section.

Return this section.

Source

pub fn align(self, alignment: u64) -> Section

Jump to the next location aligned on an alignment-byte boundary, relative to the start of the section.

Fill the gap with zeroes. alignment must be a power of two. Return this section.

Source

pub fn D8<'a, T: ToLabelOrNum<'a, u8>>(self, byte: T) -> Section

Append byte with the Section’s default endianness.

byte may be a Label or a u8. Return this section.

Source

pub fn L8<'a, T: ToLabelOrNum<'a, u8>>(self, byte: T) -> Section

Append byte as little-endian (identical to D8).

Return this section.

Source

pub fn B8<'a, T: ToLabelOrNum<'a, u8>>(self, byte: T) -> Section

Append byte as big-endian (identical to D8).

Return this section.

Source

pub fn D16<'a, T: ToLabelOrNum<'a, u16>>(self, word: T) -> Section

Append word with the Section’s default endianness.

word may be a Label or a u16. Return this section.

Source

pub fn L16<'a, T: ToLabelOrNum<'a, u16>>(self, word: T) -> Section

Append word as little-endian.

word may be a Label or a u16. Return this section.

Source

pub fn B16<'a, T: ToLabelOrNum<'a, u16>>(self, word: T) -> Section

Append word as big-endian.

word may be a Label or a u16. Return this section.

Source

pub fn D32<'a, T: ToLabelOrNum<'a, u32>>(self, dword: T) -> Section

Append dword with the Section’s default endianness.

dword may be a Label or a u32. Return this section.

Source

pub fn L32<'a, T: ToLabelOrNum<'a, u32>>(self, dword: T) -> Section

Append dword as little-endian.

dword may be a Label or a u32. Return this section.

Source

pub fn B32<'a, T: ToLabelOrNum<'a, u32>>(self, dword: T) -> Section

Append dword as big-endian.

dword may be a Label or a u32. Return this section.

Source

pub fn D64<'a, T: ToLabelOrNum<'a, u64>>(self, qword: T) -> Section

Append qword with the Section’s default endianness.

qword may be a Label or a u32. Return this section.

Source

pub fn L64<'a, T: ToLabelOrNum<'a, u64>>(self, qword: T) -> Section

Append qword as little-endian.

qword may be a Label or a u32. Return this section.

Source

pub fn B64<'a, T: ToLabelOrNum<'a, u64>>(self, qword: T) -> Section

Append qword as big-endian.

qword may be a Label or a u32. Return this section.

Trait Implementations§

Source§

impl Default for Section

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Freeze for Section

§

impl !RefUnwindSafe for Section

§

impl !Send for Section

§

impl !Sync for Section

§

impl Unpin for Section

§

impl !UnwindSafe for Section

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.