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
Implementations§
Source§impl Section
impl Section
Sourcepub fn with_endian(endian: Endian) -> Section
pub fn with_endian(endian: Endian) -> Section
Construct a Section
with endian
endianness.
Sourcepub fn final_size(&self) -> Label
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.
Sourcepub fn get_contents(self) -> Option<Vec<u8>>
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
.
Sourcepub fn start(&self) -> Label
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.
Sourcepub fn here(&self) -> Label
pub fn here(&self) -> Label
A label representing the point at which the next appended item will appear in the section, relative to start
.
Sourcepub fn set_start_const(self, value: u64) -> Section
pub fn set_start_const(self, value: u64) -> Section
Set the value of start
to value
.
Sourcepub fn append_bytes(self, data: &[u8]) -> Section
pub fn append_bytes(self, data: &[u8]) -> Section
Append data
to the end of this section, and return this section.
Sourcepub fn append_section<S: Into<Section>>(self, section: S) -> Section
pub fn append_section<S: Into<Section>>(self, section: S) -> Section
Append section
’s contents to the end of this section.
Any Label
s that were appended to section
will not be
resolved until this section is finalized.
Return this section.
Sourcepub fn append_repeated(self, byte: u8, count: usize) -> Section
pub fn append_repeated(self, byte: u8, count: usize) -> Section
Append count
copies of byte
to the end of this section.
Return this section.
Sourcepub fn align(self, alignment: u64) -> Section
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.
Sourcepub fn D8<'a, T: ToLabelOrNum<'a, u8>>(self, byte: T) -> Section
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.
Sourcepub fn L8<'a, T: ToLabelOrNum<'a, u8>>(self, byte: T) -> Section
pub fn L8<'a, T: ToLabelOrNum<'a, u8>>(self, byte: T) -> Section
Append byte
as little-endian (identical to D8
).
Return this section.
Sourcepub fn B8<'a, T: ToLabelOrNum<'a, u8>>(self, byte: T) -> Section
pub fn B8<'a, T: ToLabelOrNum<'a, u8>>(self, byte: T) -> Section
Append byte
as big-endian (identical to D8
).
Return this section.
Sourcepub fn D16<'a, T: ToLabelOrNum<'a, u16>>(self, word: T) -> Section
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.
Sourcepub fn L16<'a, T: ToLabelOrNum<'a, u16>>(self, word: T) -> Section
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.
Sourcepub fn B16<'a, T: ToLabelOrNum<'a, u16>>(self, word: T) -> Section
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.
Sourcepub fn D32<'a, T: ToLabelOrNum<'a, u32>>(self, dword: T) -> Section
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.
Sourcepub fn L32<'a, T: ToLabelOrNum<'a, u32>>(self, dword: T) -> Section
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.
Sourcepub fn B32<'a, T: ToLabelOrNum<'a, u32>>(self, dword: T) -> Section
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.
Sourcepub fn D64<'a, T: ToLabelOrNum<'a, u64>>(self, qword: T) -> Section
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.
Sourcepub fn L64<'a, T: ToLabelOrNum<'a, u64>>(self, qword: T) -> Section
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.
Sourcepub fn B64<'a, T: ToLabelOrNum<'a, u64>>(self, qword: T) -> Section
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.