@@ -21,12 +21,16 @@ use bitflags::bitflags;
21
21
use xmas_elf:: dynamic:: * ;
22
22
use xmas_elf:: program:: ProgramIter ;
23
23
24
- pub use xmas_elf:: header:: Machine ;
24
+ pub use xmas_elf:: header:: { Header , Machine } ;
25
25
pub use xmas_elf:: program:: { Flags , ProgramHeader , ProgramHeader64 } ;
26
26
pub use xmas_elf:: sections:: { Rel , Rela } ;
27
27
pub use xmas_elf:: symbol_table:: { Entry , Entry64 } ;
28
28
pub use xmas_elf:: { P32 , P64 } ;
29
29
30
+ /// Required alignment for zero-copy reads provided to xmas_elf by the
31
+ /// zero crate.
32
+ pub ( crate ) const ALIGNMENT : usize = core:: mem:: align_of :: < Header > ( ) ;
33
+
30
34
/// An iterator over [`ProgramHeader`] whose type is `LOAD`.
31
35
pub type LoadableHeaders < ' a , ' b > = Filter < ProgramIter < ' a , ' b > , fn ( & ProgramHeader ) -> bool > ;
32
36
pub type PAddr = u64 ;
@@ -47,6 +51,7 @@ pub struct RelocationEntry {
47
51
pub enum ElfLoaderErr {
48
52
ElfParser { source : & ' static str } ,
49
53
OutOfMemory ,
54
+ UnalignedMemory ,
50
55
SymbolTableNotFound ,
51
56
UnsupportedElfFormat ,
52
57
UnsupportedElfVersion ,
@@ -69,6 +74,7 @@ impl fmt::Display for ElfLoaderErr {
69
74
match self {
70
75
ElfLoaderErr :: ElfParser { source } => write ! ( f, "Error in ELF parser: {}" , source) ,
71
76
ElfLoaderErr :: OutOfMemory => write ! ( f, "Out of memory" ) ,
77
+ ElfLoaderErr :: UnalignedMemory => write ! ( f, "Data must be aligned to {:?}" , ALIGNMENT ) ,
72
78
ElfLoaderErr :: SymbolTableNotFound => write ! ( f, "No symbol table in the ELF file" ) ,
73
79
ElfLoaderErr :: UnsupportedElfFormat => write ! ( f, "ELF format not supported" ) ,
74
80
ElfLoaderErr :: UnsupportedElfVersion => write ! ( f, "ELF version not supported" ) ,
@@ -166,6 +172,15 @@ pub trait ElfLoader {
166
172
}
167
173
}
168
174
175
+ /// Utility function to verify alignment.
176
+ ///
177
+ /// Note: this may be stabilized in the future as:
178
+ ///
179
+ /// [core::ptr::is_aligned_to](https://doc.rust-lang.org/core/primitive.pointer.html#method.is_aligned_to)
180
+ pub ( crate ) fn is_aligned_to ( ptr : usize , align : usize ) -> bool {
181
+ ptr & ( align - 1 ) == 0
182
+ }
183
+
169
184
#[ cfg( doctest) ]
170
185
mod test_readme {
171
186
macro_rules! external_doc_test {
0 commit comments