22
33use crate :: { TagHeader , TagTrait , TagType } ;
44use core:: fmt:: { Debug , Formatter } ;
5+ use core:: marker:: { PhantomData , PhantomPinned } ;
56use core:: mem;
67use core:: str:: Utf8Error ;
78#[ cfg( feature = "builder" ) ]
@@ -34,21 +35,18 @@ impl ElfSectionsTag {
3435 }
3536
3637 /// Get an iterator of loaded ELF sections.
37- pub ( crate ) const fn sections ( & self ) -> ElfSectionIter {
38+ pub ( crate ) fn sections ( & self ) -> ElfSectionIter {
3839 let string_section_offset = ( self . shndx * self . entry_size ) as isize ;
3940 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 _ } ;
4142 ElfSectionIter {
42- current_section : self . first_section ( ) ,
43+ current_section : self . sections . as_ptr ( ) ,
4344 remaining_sections : self . number_of_sections ,
4445 entry_size : self . entry_size ,
4546 string_section : string_section_ptr,
47+ _phantom_data : PhantomData :: default ( ) ,
4648 }
4749 }
48-
49- const fn first_section ( & self ) -> * const u8 {
50- & ( self . sections [ 0 ] ) as * const _
51- }
5250}
5351
5452impl TagTrait for ElfSectionsTag {
@@ -75,23 +73,24 @@ impl Debug for ElfSectionsTag {
7573
7674/// An iterator over some ELF sections.
7775#[ derive( Clone ) ]
78- /// TODO make this memory safe with lifetime capture.
79- pub struct ElfSectionIter {
76+ pub struct ElfSectionIter < ' a > {
8077 current_section : * const u8 ,
8178 remaining_sections : u32 ,
8279 entry_size : u32 ,
8380 string_section : * const u8 ,
81+ _phantom_data : PhantomData < & ' a ( ) > ,
8482}
8583
86- impl Iterator for ElfSectionIter {
87- type Item = ElfSection ;
84+ impl < ' a > Iterator for ElfSectionIter < ' a > {
85+ type Item = ElfSection < ' a > ;
8886
89- fn next ( & mut self ) -> Option < ElfSection > {
87+ fn next ( & mut self ) -> Option < ElfSection < ' a > > {
9088 while self . remaining_sections != 0 {
9189 let section = ElfSection {
9290 inner : self . current_section ,
9391 string_section : self . string_section ,
9492 entry_size : self . entry_size ,
93+ _phantom : PhantomData :: default ( ) ,
9594 } ;
9695
9796 self . current_section = unsafe { self . current_section . offset ( self . entry_size as isize ) } ;
@@ -105,7 +104,7 @@ impl Iterator for ElfSectionIter {
105104 }
106105}
107106
108- impl Debug for ElfSectionIter {
107+ impl < ' a > Debug for ElfSectionIter < ' a > {
109108 fn fmt ( & self , f : & mut Formatter < ' _ > ) -> core:: fmt:: Result {
110109 let mut debug = f. debug_list ( ) ;
111110 self . clone ( ) . for_each ( |ref e| {
@@ -115,23 +114,13 @@ impl Debug for ElfSectionIter {
115114 }
116115}
117116
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-
129117/// A single generic ELF Section.
130118#[ derive( Copy , Clone , Debug , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
131- pub struct ElfSection {
119+ pub struct ElfSection < ' a > {
132120 inner : * const u8 ,
133121 string_section : * const u8 ,
134122 entry_size : u32 ,
123+ _phantom : PhantomData < & ' a ( ) > ,
135124}
136125
137126#[ derive( Clone , Copy , Debug ) ]
@@ -164,7 +153,7 @@ struct ElfSectionInner64 {
164153 entry_size : u64 ,
165154}
166155
167- impl ElfSection {
156+ impl < ' a > ElfSection < ' a > {
168157 /// Get the section type as a `ElfSectionType` enum variant.
169158 #[ must_use]
170159 pub fn section_type ( & self ) -> ElfSectionType {
0 commit comments