@@ -50,20 +50,22 @@ pub struct ElfSectionIter {
50
50
current_section : * const u8 ,
51
51
remaining_sections : u32 ,
52
52
entry_size : u32 ,
53
- string_section : * const ElfSectionInner ,
53
+ string_section : * const u8 ,
54
54
}
55
55
56
56
impl Iterator for ElfSectionIter {
57
57
type Item = ElfSection ;
58
+
58
59
fn next ( & mut self ) -> Option < ElfSection > {
59
60
if self . remaining_sections == 0 {
60
61
return None ;
61
62
}
62
63
63
64
loop {
64
65
let section = ElfSection {
65
- inner : self . current_section as * const ElfSectionInner ,
66
+ inner : self . current_section ,
66
67
string_section : self . string_section ,
68
+ entry_size : self . entry_size ,
67
69
} ;
68
70
69
71
self . current_section = unsafe { self . current_section . offset ( self . entry_size as isize ) } ;
@@ -77,14 +79,14 @@ impl Iterator for ElfSectionIter {
77
79
}
78
80
79
81
pub struct ElfSection {
80
- inner : * const ElfSectionInner ,
81
- string_section : * const ElfSectionInner ,
82
+ inner : * const u8 ,
83
+ string_section : * const u8 ,
84
+ entry_size : u32 ,
82
85
}
83
86
84
- #[ cfg( feature = "elf32" ) ]
85
87
#[ derive( Debug ) ]
86
88
#[ repr( C ) ]
87
- struct ElfSectionInner {
89
+ struct ElfSectionInner32 {
88
90
name_index : u32 ,
89
91
typ : u32 ,
90
92
flags : u32 ,
@@ -97,10 +99,9 @@ struct ElfSectionInner {
97
99
entry_size : u32 ,
98
100
}
99
101
100
- #[ cfg( not( feature = "elf32" ) ) ]
101
102
#[ derive( Debug ) ]
102
103
#[ repr( C ) ]
103
- struct ElfSectionInner {
104
+ struct ElfSectionInner64 {
104
105
name_index : u32 ,
105
106
typ : u32 ,
106
107
flags : u64 ,
@@ -115,7 +116,7 @@ struct ElfSectionInner {
115
116
116
117
impl ElfSection {
117
118
pub fn section_type ( & self ) -> ElfSectionType {
118
- match self . get ( ) . typ {
119
+ match self . get ( ) . typ ( ) {
119
120
0 => ElfSectionType :: Unused ,
120
121
1 => ElfSectionType :: ProgramSection ,
121
122
2 => ElfSectionType :: LinkerSymbolTable ,
@@ -135,14 +136,14 @@ impl ElfSection {
135
136
}
136
137
137
138
pub fn section_type_raw ( & self ) -> u32 {
138
- self . get ( ) . typ
139
+ self . get ( ) . typ ( )
139
140
}
140
141
141
142
pub fn name ( & self ) -> & str {
142
143
use core:: { str, slice} ;
143
144
144
145
let name_ptr = unsafe {
145
- self . string_table ( ) . offset ( self . get ( ) . name_index as isize )
146
+ self . string_table ( ) . offset ( self . get ( ) . name_index ( ) as isize )
146
147
} ;
147
148
let strlen = {
148
149
let mut len = 0 ;
@@ -156,31 +157,95 @@ impl ElfSection {
156
157
}
157
158
158
159
pub fn start_address ( & self ) -> usize {
159
- self . get ( ) . addr as usize
160
+ self . get ( ) . addr ( )
160
161
}
161
162
162
163
pub fn end_address ( & self ) -> usize {
163
- ( self . get ( ) . addr + self . get ( ) . size ) as usize
164
+ self . get ( ) . addr ( ) + self . get ( ) . size ( )
164
165
}
165
166
166
167
pub fn size ( & self ) -> usize {
167
- self . get ( ) . size as usize
168
+ self . get ( ) . size ( )
168
169
}
169
170
170
171
pub fn flags ( & self ) -> ElfSectionFlags {
171
- ElfSectionFlags :: from_bits_truncate ( self . get ( ) . flags )
172
+ ElfSectionFlags :: from_bits_truncate ( self . get ( ) . flags ( ) )
172
173
}
173
174
174
175
pub fn is_allocated ( & self ) -> bool {
175
176
self . flags ( ) . contains ( ELF_SECTION_ALLOCATED )
176
177
}
177
178
178
179
fn get ( & self ) -> & ElfSectionInner {
179
- unsafe { & * self . inner }
180
+ match self . entry_size {
181
+ 40 => unsafe { & * ( self . inner as * const ElfSectionInner32 ) } ,
182
+ 64 => unsafe { & * ( self . inner as * const ElfSectionInner64 ) } ,
183
+ _ => panic ! ( ) ,
184
+ }
180
185
}
181
186
182
187
unsafe fn string_table ( & self ) -> * const u8 {
183
- ( * self . string_section ) . addr as * const _
188
+ match self . entry_size {
189
+ 40 => ( * ( self . string_section as * const ElfSectionInner32 ) ) . addr as * const _ ,
190
+ 64 => ( * ( self . string_section as * const ElfSectionInner64 ) ) . addr as * const _ ,
191
+ _ => panic ! ( ) ,
192
+ }
193
+ }
194
+ }
195
+
196
+ trait ElfSectionInner {
197
+ fn name_index ( & self ) -> u32 ;
198
+
199
+ fn typ ( & self ) -> u32 ;
200
+
201
+ fn flags ( & self ) -> u32 ;
202
+
203
+ fn addr ( & self ) -> usize ;
204
+
205
+ fn size ( & self ) -> usize ;
206
+ }
207
+
208
+ impl ElfSectionInner for ElfSectionInner32 {
209
+ fn name_index ( & self ) -> u32 {
210
+ self . name_index
211
+ }
212
+
213
+ fn typ ( & self ) -> u32 {
214
+ self . typ
215
+ }
216
+
217
+ fn flags ( & self ) -> u32 {
218
+ self . flags
219
+ }
220
+
221
+ fn addr ( & self ) -> usize {
222
+ self . addr as usize
223
+ }
224
+
225
+ fn size ( & self ) -> usize {
226
+ self . size as usize
227
+ }
228
+ }
229
+
230
+ impl ElfSectionInner for ElfSectionInner64 {
231
+ fn name_index ( & self ) -> u32 {
232
+ self . name_index
233
+ }
234
+
235
+ fn typ ( & self ) -> u32 {
236
+ self . typ
237
+ }
238
+
239
+ fn flags ( & self ) -> u32 {
240
+ self . flags as u32
241
+ }
242
+
243
+ fn addr ( & self ) -> usize {
244
+ self . addr as usize
245
+ }
246
+
247
+ fn size ( & self ) -> usize {
248
+ self . size as usize
184
249
}
185
250
}
186
251
@@ -203,14 +268,8 @@ pub enum ElfSectionType {
203
268
ProcessorSpecific = 0x7000_0000 ,
204
269
}
205
270
206
- #[ cfg( feature = "elf32" ) ]
207
- type ElfSectionFlagsType = u32 ;
208
-
209
- #[ cfg( not( feature = "elf32" ) ) ]
210
- type ElfSectionFlagsType = u64 ;
211
-
212
271
bitflags ! {
213
- flags ElfSectionFlags : ElfSectionFlagsType {
272
+ flags ElfSectionFlags : u32 {
214
273
const ELF_SECTION_WRITABLE = 0x1 ,
215
274
const ELF_SECTION_ALLOCATED = 0x2 ,
216
275
const ELF_SECTION_EXECUTABLE = 0x4 ,
0 commit comments