]>
git.proxmox.com Git - rustc.git/blob - vendor/object-0.20.0/src/read/traits.rs
1 use alloc
::borrow
::Cow
;
4 self, Architecture
, CompressedData
, FileFlags
, Relocation
, Result
, SectionFlags
, SectionIndex
,
5 SectionKind
, Symbol
, SymbolIndex
, SymbolMap
,
10 pub trait Object
<'data
, 'file
>: read
::private
::Sealed
{
11 /// A segment in the object file.
12 type Segment
: ObjectSegment
<'data
>;
14 /// An iterator over the segments in the object file.
15 type SegmentIterator
: Iterator
<Item
= Self::Segment
>;
17 /// A section in the object file.
18 type Section
: ObjectSection
<'data
>;
20 /// An iterator over the sections in the object file.
21 type SectionIterator
: Iterator
<Item
= Self::Section
>;
23 /// An iterator over the symbols in the object file.
24 type SymbolIterator
: Iterator
<Item
= (SymbolIndex
, Symbol
<'data
>)>;
26 /// Get the architecture type of the file.
27 fn architecture(&self) -> Architecture
;
29 /// Get the endianness of the file.
31 fn endianness(&self) -> Endianness
{
32 if self.is_little_endian() {
39 /// Return true if the file is little endian, false if it is big endian.
40 fn is_little_endian(&self) -> bool
;
42 /// Return true if the file can contain 64-bit addresses.
43 fn is_64(&self) -> bool
;
45 /// Get an iterator over the segments in the file.
46 fn segments(&'file
self) -> Self::SegmentIterator
;
48 /// Get the entry point address of the binary
49 fn entry(&'file
self) -> u64;
51 /// Get the section named `section_name`, if such a section exists.
53 /// If `section_name` starts with a '.' then it is treated as a system section name,
54 /// and is compared using the conventions specific to the object file format. This
56 /// - if ".text" is requested for a Mach-O object file, then the actual
57 /// section name that is searched for is "__text".
58 /// - if ".debug_info" is requested for an ELF object file, then
59 /// ".zdebug_info" may be returned (and similarly for other debug sections).
61 /// For some object files, multiple segments may contain sections with the same
62 /// name. In this case, the first matching section will be used.
64 /// This method skips over sections with invalid names.
65 fn section_by_name(&'file
self, section_name
: &str) -> Option
<Self::Section
>;
67 /// Get the section at the given index.
69 /// The meaning of the index depends on the object file.
71 /// For some object files, this requires iterating through all sections.
73 /// Returns an error if the index is invalid.
74 fn section_by_index(&'file
self, index
: SectionIndex
) -> Result
<Self::Section
>;
76 /// Get an iterator over the sections in the file.
77 fn sections(&'file
self) -> Self::SectionIterator
;
79 /// Get the debugging symbol at the given index.
81 /// The meaning of the index depends on the object file.
83 /// Returns an error if the index is invalid.
84 fn symbol_by_index(&self, index
: SymbolIndex
) -> Result
<Symbol
<'data
>>;
86 /// Get an iterator over the debugging symbols in the file.
88 /// This may skip over symbols that are malformed or unsupported.
89 fn symbols(&'file
self) -> Self::SymbolIterator
;
91 /// Get the data for the given symbol.
93 /// This may iterate over segments.
95 /// Returns `Ok(None)` for undefined symbols or if the data could not be found.
96 fn symbol_data(&'file
self, symbol
: &Symbol
<'data
>) -> Result
<Option
<&'data
[u8]>> {
97 if symbol
.is_undefined() {
100 let address
= symbol
.address();
101 let size
= symbol
.size();
102 if let Some(index
) = symbol
.section_index() {
103 let section
= self.section_by_index(index
)?
;
104 section
.data_range(address
, size
)
106 for segment
in self.segments() {
107 if let Some(data
) = segment
.data_range(address
, size
)?
{
108 return Ok(Some(data
));
115 /// Get an iterator over the dynamic linking symbols in the file.
117 /// This may skip over symbols that are malformed or unsupported.
118 fn dynamic_symbols(&'file
self) -> Self::SymbolIterator
;
120 /// Construct a map from addresses to symbols.
121 fn symbol_map(&self) -> SymbolMap
<'data
>;
123 /// Return true if the file contains debug information sections, false if not.
124 fn has_debug_symbols(&self) -> bool
;
126 /// The UUID from a Mach-O `LC_UUID` load command.
128 fn mach_uuid(&self) -> Result
<Option
<[u8; 16]>> {
132 /// The build ID from an ELF `NT_GNU_BUILD_ID` note.
134 fn build_id(&self) -> Result
<Option
<&'data
[u8]>> {
138 /// The filename and CRC from a `.gnu_debuglink` section.
140 fn gnu_debuglink(&self) -> Result
<Option
<(&'data
[u8], u32)>> {
144 /// File flags that are specific to each file format.
145 fn flags(&self) -> FileFlags
;
148 /// A loadable segment defined in an object file.
150 /// For ELF, this is a program header with type `PT_LOAD`.
151 /// For Mach-O, this is a load command with type `LC_SEGMENT` or `LC_SEGMENT_64`.
152 pub trait ObjectSegment
<'data
>: read
::private
::Sealed
{
153 /// Returns the virtual address of the segment.
154 fn address(&self) -> u64;
156 /// Returns the size of the segment in memory.
157 fn size(&self) -> u64;
159 /// Returns the alignment of the segment in memory.
160 fn align(&self) -> u64;
162 /// Returns the offset and size of the segment in the file.
163 fn file_range(&self) -> (u64, u64);
165 /// Returns a reference to the file contents of the segment.
167 /// The length of this data may be different from the size of the
168 /// segment in memory.
169 fn data(&self) -> Result
<&'data
[u8]>;
171 /// Return the segment data in the given range.
173 /// Returns `Ok(None)` if the segment does not contain the given range.
174 fn data_range(&self, address
: u64, size
: u64) -> Result
<Option
<&'data
[u8]>>;
176 /// Returns the name of the segment.
177 fn name(&self) -> Result
<Option
<&str>>;
180 /// A section defined in an object file.
181 pub trait ObjectSection
<'data
>: read
::private
::Sealed
{
182 /// An iterator over the relocations for a section.
184 /// The first field in the item tuple is the section offset
185 /// that the relocation applies to.
186 type RelocationIterator
: Iterator
<Item
= (u64, Relocation
)>;
188 /// Returns the section index.
189 fn index(&self) -> SectionIndex
;
191 /// Returns the address of the section.
192 fn address(&self) -> u64;
194 /// Returns the size of the section in memory.
195 fn size(&self) -> u64;
197 /// Returns the alignment of the section in memory.
198 fn align(&self) -> u64;
200 /// Returns offset and size of on-disk segment (if any).
201 fn file_range(&self) -> Option
<(u64, u64)>;
203 /// Returns the raw contents of the section.
205 /// The length of this data may be different from the size of the
206 /// section in memory.
208 /// This does not do any decompression.
209 fn data(&self) -> Result
<&'data
[u8]>;
211 /// Return the raw contents of the section data in the given range.
213 /// This does not do any decompression.
215 /// Returns `Ok(None)` if the section does not contain the given range.
216 fn data_range(&self, address
: u64, size
: u64) -> Result
<Option
<&'data
[u8]>>;
218 /// Returns the potentially compressed contents of the section,
219 /// along with information about the compression.
220 fn compressed_data(&self) -> Result
<CompressedData
<'data
>>;
222 /// Returns the uncompressed contents of the section.
224 /// The length of this data may be different from the size of the
225 /// section in memory.
227 /// If no compression is detected, then returns the data unchanged.
228 /// Returns `Err` if decompression fails.
229 fn uncompressed_data(&self) -> Result
<Cow
<'data
, [u8]>> {
230 self.compressed_data()?
.decompress()
233 /// Returns the name of the section.
234 fn name(&self) -> Result
<&str>;
236 /// Returns the name of the segment for this section.
237 fn segment_name(&self) -> Result
<Option
<&str>>;
239 /// Return the kind of this section.
240 fn kind(&self) -> SectionKind
;
242 /// Get the relocations for this section.
243 fn relocations(&self) -> Self::RelocationIterator
;
245 /// Section flags that are specific to each file format.
246 fn flags(&self) -> SectionFlags
;