2
2
3
3
use crate :: { TagHeader , TagTrait , TagType } ;
4
4
use core:: fmt:: { Debug , Formatter } ;
5
+ use core:: marker:: { PhantomData , PhantomPinned } ;
5
6
use core:: mem;
6
7
use core:: str:: Utf8Error ;
7
8
#[ cfg( feature = "builder" ) ]
@@ -34,21 +35,18 @@ impl ElfSectionsTag {
34
35
}
35
36
36
37
/// Get an iterator of loaded ELF sections.
37
- pub ( crate ) const fn sections ( & self ) -> ElfSectionIter {
38
+ pub ( crate ) fn sections ( & self ) -> ElfSectionIter {
38
39
let string_section_offset = ( self . shndx * self . entry_size ) as isize ;
39
40
let string_section_ptr =
40
- unsafe { self . first_section ( ) . offset ( string_section_offset) as * const _ } ;
41
+ unsafe { self . sections . as_ptr ( ) . offset ( string_section_offset) as * const _ } ;
41
42
ElfSectionIter {
42
- current_section : self . first_section ( ) ,
43
+ current_section : self . sections . as_ptr ( ) ,
43
44
remaining_sections : self . number_of_sections ,
44
45
entry_size : self . entry_size ,
45
46
string_section : string_section_ptr,
47
+ _phantom_data : PhantomData :: default ( ) ,
46
48
}
47
49
}
48
-
49
- const fn first_section ( & self ) -> * const u8 {
50
- & ( self . sections [ 0 ] ) as * const _
51
- }
52
50
}
53
51
54
52
impl TagTrait for ElfSectionsTag {
@@ -75,23 +73,24 @@ impl Debug for ElfSectionsTag {
75
73
76
74
/// An iterator over some ELF sections.
77
75
#[ derive( Clone ) ]
78
- /// TODO make this memory safe with lifetime capture.
79
- pub struct ElfSectionIter {
76
+ pub struct ElfSectionIter < ' a > {
80
77
current_section : * const u8 ,
81
78
remaining_sections : u32 ,
82
79
entry_size : u32 ,
83
80
string_section : * const u8 ,
81
+ _phantom_data : PhantomData < & ' a ( ) > ,
84
82
}
85
83
86
- impl Iterator for ElfSectionIter {
87
- type Item = ElfSection ;
84
+ impl < ' a > Iterator for ElfSectionIter < ' a > {
85
+ type Item = ElfSection < ' a > ;
88
86
89
- fn next ( & mut self ) -> Option < ElfSection > {
87
+ fn next ( & mut self ) -> Option < ElfSection < ' a > > {
90
88
while self . remaining_sections != 0 {
91
89
let section = ElfSection {
92
90
inner : self . current_section ,
93
91
string_section : self . string_section ,
94
92
entry_size : self . entry_size ,
93
+ _phantom : PhantomData :: default ( ) ,
95
94
} ;
96
95
97
96
self . current_section = unsafe { self . current_section . offset ( self . entry_size as isize ) } ;
@@ -105,7 +104,7 @@ impl Iterator for ElfSectionIter {
105
104
}
106
105
}
107
106
108
- impl Debug for ElfSectionIter {
107
+ impl < ' a > Debug for ElfSectionIter < ' a > {
109
108
fn fmt ( & self , f : & mut Formatter < ' _ > ) -> core:: fmt:: Result {
110
109
let mut debug = f. debug_list ( ) ;
111
110
self . clone ( ) . for_each ( |ref e| {
@@ -115,23 +114,13 @@ impl Debug for ElfSectionIter {
115
114
}
116
115
}
117
116
118
- impl Default for ElfSectionIter {
119
- fn default ( ) -> Self {
120
- Self {
121
- current_section : core:: ptr:: null ( ) ,
122
- remaining_sections : 0 ,
123
- entry_size : 0 ,
124
- string_section : core:: ptr:: null ( ) ,
125
- }
126
- }
127
- }
128
-
129
117
/// A single generic ELF Section.
130
118
#[ derive( Copy , Clone , Debug , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
131
- pub struct ElfSection {
119
+ pub struct ElfSection < ' a > {
132
120
inner : * const u8 ,
133
121
string_section : * const u8 ,
134
122
entry_size : u32 ,
123
+ _phantom : PhantomData < & ' a ( ) > ,
135
124
}
136
125
137
126
#[ derive( Clone , Copy , Debug ) ]
@@ -164,7 +153,7 @@ struct ElfSectionInner64 {
164
153
entry_size : u64 ,
165
154
}
166
155
167
- impl ElfSection {
156
+ impl < ' a > ElfSection < ' a > {
168
157
/// Get the section type as a `ElfSectionType` enum variant.
169
158
#[ must_use]
170
159
pub fn section_type ( & self ) -> ElfSectionType {
0 commit comments