1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
//! Section-header related ELF types
//!
//! This module contains section-header views used while scanning ELF sections.
use super::{
layout::{ElfLayout, NativeElfLayout},
raw::ElfShdrRaw,
symbol::ElfSectionIndex,
};
use crate::entity::EntityRef;
use bitflags::bitflags;
use core::fmt::{self, Display};
use elf::abi::*;
/// Semantic wrapper for the ELF `sh_type` field.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(transparent)]
pub struct ElfSectionType(u32);
impl ElfSectionType {
/// `SHT_NULL`: inactive section header.
pub const NULL: Self = Self(SHT_NULL);
/// `SHT_PROGBITS`: program-defined data.
pub const PROGBITS: Self = Self(SHT_PROGBITS);
/// `SHT_SYMTAB`: full symbol table.
pub const SYMTAB: Self = Self(SHT_SYMTAB);
/// `SHT_STRTAB`: string table.
pub const STRTAB: Self = Self(SHT_STRTAB);
/// `SHT_RELA`: explicit-addend relocation section.
pub const RELA: Self = Self(SHT_RELA);
/// `SHT_HASH`: System V symbol hash table.
pub const HASH: Self = Self(SHT_HASH);
/// `SHT_DYNAMIC`: dynamic section.
pub const DYNAMIC: Self = Self(SHT_DYNAMIC);
/// `SHT_NOTE`: note section.
pub const NOTE: Self = Self(SHT_NOTE);
/// `SHT_NOBITS`: allocated data with no file bytes.
pub const NOBITS: Self = Self(SHT_NOBITS);
/// `SHT_REL`: implicit-addend relocation section.
pub const REL: Self = Self(SHT_REL);
/// `SHT_SHLIB`: reserved section type.
pub const SHLIB: Self = Self(SHT_SHLIB);
/// `SHT_DYNSYM`: dynamic symbol table.
pub const DYNSYM: Self = Self(SHT_DYNSYM);
/// `SHT_INIT_ARRAY`: initialization function array.
pub const INIT_ARRAY: Self = Self(SHT_INIT_ARRAY);
/// `SHT_FINI_ARRAY`: finalization function array.
pub const FINI_ARRAY: Self = Self(SHT_FINI_ARRAY);
/// `SHT_PREINIT_ARRAY`: pre-initialization function array.
pub const PREINIT_ARRAY: Self = Self(SHT_PREINIT_ARRAY);
/// `SHT_GROUP`: section group.
pub const GROUP: Self = Self(SHT_GROUP);
/// `SHT_SYMTAB_SHNDX`: extended symbol section indices.
pub const SYMTAB_SHNDX: Self = Self(SHT_SYMTAB_SHNDX);
/// Creates a section type wrapper from a raw `sh_type` value.
#[inline]
pub const fn new(raw: u32) -> Self {
Self(raw)
}
/// Returns the raw `sh_type` value.
#[inline]
pub const fn raw(self) -> u32 {
self.0
}
}
impl From<u32> for ElfSectionType {
#[inline]
fn from(value: u32) -> Self {
Self::new(value)
}
}
impl From<ElfSectionType> for u32 {
#[inline]
fn from(value: ElfSectionType) -> Self {
value.raw()
}
}
impl Display for ElfSectionType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.0 {
SHT_NULL => f.write_str("SHT_NULL"),
SHT_PROGBITS => f.write_str("SHT_PROGBITS"),
SHT_SYMTAB => f.write_str("SHT_SYMTAB"),
SHT_STRTAB => f.write_str("SHT_STRTAB"),
SHT_RELA => f.write_str("SHT_RELA"),
SHT_HASH => f.write_str("SHT_HASH"),
SHT_DYNAMIC => f.write_str("SHT_DYNAMIC"),
SHT_NOTE => f.write_str("SHT_NOTE"),
SHT_NOBITS => f.write_str("SHT_NOBITS"),
SHT_REL => f.write_str("SHT_REL"),
SHT_SHLIB => f.write_str("SHT_SHLIB"),
SHT_DYNSYM => f.write_str("SHT_DYNSYM"),
SHT_INIT_ARRAY => f.write_str("SHT_INIT_ARRAY"),
SHT_FINI_ARRAY => f.write_str("SHT_FINI_ARRAY"),
SHT_PREINIT_ARRAY => f.write_str("SHT_PREINIT_ARRAY"),
SHT_GROUP => f.write_str("SHT_GROUP"),
SHT_SYMTAB_SHNDX => f.write_str("SHT_SYMTAB_SHNDX"),
raw => write!(f, "unknown ELF section type {raw}"),
}
}
}
bitflags! {
/// Bitflags wrapper for the ELF `sh_flags` field.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct ElfSectionFlags: u64 {
/// Section is writable at runtime.
const WRITE = SHF_WRITE as u64;
/// Section occupies memory at runtime.
const ALLOC = SHF_ALLOC as u64;
/// Section contains executable instructions.
const EXECINSTR = SHF_EXECINSTR as u64;
/// Section contains thread-local storage.
const TLS = SHF_TLS as u64;
}
}
/// Stable identifier for a real section-header table entry.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
pub struct ElfSectionId(usize);
impl ElfSectionId {
/// Creates a section id from a zero-based section-header table index.
#[inline]
pub const fn new(index: usize) -> Self {
Self(index)
}
/// Returns the zero-based section-header table index.
#[inline]
pub const fn index(self) -> usize {
self.0
}
/// Converts a symbol `st_shndx` value into a section id when it names a
/// real section-header table entry.
#[inline]
pub const fn from_symbol_shndx(index: ElfSectionIndex) -> Option<Self> {
if index.is_undef() || index.is_abs() || index.is_common() || index.is_xindex() {
None
} else {
Some(Self::new(index.index()))
}
}
}
impl EntityRef for ElfSectionId {
#[inline]
fn new(index: usize) -> Self {
Self(index)
}
#[inline]
fn index(self) -> usize {
self.0
}
}
impl Display for ElfSectionId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "ELF section {}", self.0)
}
}
/// ELF section header describing sections of the ELF file.
#[derive(Debug)]
#[repr(transparent)]
pub struct ElfShdr<L: ElfLayout = NativeElfLayout> {
shdr: L::Shdr,
}
impl<L: ElfLayout> ElfShdr<L> {
/// Returns the parsed ELF section type of this header.
#[inline]
pub fn section_type(&self) -> ElfSectionType {
ElfSectionType::new(self.shdr.sh_type())
}
/// Returns the section name index (`sh_name`) field.
#[inline]
pub fn sh_name(&self) -> u32 {
self.shdr.sh_name()
}
/// Returns the parsed ELF section flags of this header.
#[inline]
pub fn flags(&self) -> ElfSectionFlags {
ElfSectionFlags::from_bits_retain(self.shdr.sh_flags())
}
/// Returns the section address (`sh_addr`) as a native-sized value.
#[inline]
pub fn sh_addr(&self) -> usize {
self.shdr.sh_addr()
}
/// Returns the section file offset (`sh_offset`) as a native-sized value.
#[inline]
pub fn sh_offset(&self) -> usize {
self.shdr.sh_offset()
}
/// Returns the section size (`sh_size`) as a native-sized value.
#[inline]
pub fn sh_size(&self) -> usize {
self.shdr.sh_size()
}
/// Returns the section link (`sh_link`) field.
#[inline]
pub fn sh_link(&self) -> u32 {
self.shdr.sh_link()
}
/// Returns the section info (`sh_info`) field.
#[inline]
pub fn sh_info(&self) -> u32 {
self.shdr.sh_info()
}
/// Returns the section alignment (`sh_addralign`) as a native-sized value.
#[inline]
pub fn sh_addralign(&self) -> usize {
self.shdr.sh_addralign()
}
/// Returns the section entry size (`sh_entsize`) as a native-sized value.
#[inline]
pub fn sh_entsize(&self) -> usize {
self.shdr.sh_entsize()
}
/// Updates the section name index (`sh_name`) field.
#[inline]
pub fn set_sh_name(&mut self, name: u32) {
self.shdr.set_sh_name(name);
}
/// Updates the section type (`sh_type`) field.
#[inline]
pub fn set_section_type(&mut self, ty: ElfSectionType) {
self.shdr.set_sh_type(ty.raw());
}
/// Updates the section flags (`sh_flags`) field.
#[inline]
pub fn set_flags(&mut self, flags: ElfSectionFlags) {
self.shdr.set_sh_flags(flags.bits());
}
/// Updates the section address (`sh_addr`) field.
#[inline]
pub fn set_sh_addr(&mut self, addr: usize) {
self.shdr.set_sh_addr(addr);
}
/// Adds an offset to the section address (`sh_addr`) field.
#[inline]
pub fn add_sh_addr(&mut self, delta: usize) {
self.shdr.add_sh_addr(delta);
}
/// Updates the section file offset (`sh_offset`) field.
#[inline]
pub fn set_sh_offset(&mut self, offset: usize) {
self.shdr.set_sh_offset(offset);
}
/// Updates the section size (`sh_size`) field.
#[inline]
pub fn set_sh_size(&mut self, size: usize) {
self.shdr.set_sh_size(size);
}
/// Updates the section link (`sh_link`) field.
#[inline]
pub fn set_sh_link(&mut self, link: u32) {
self.shdr.set_sh_link(link);
}
/// Updates the section info (`sh_info`) field.
#[inline]
pub fn set_sh_info(&mut self, info: u32) {
self.shdr.set_sh_info(info);
}
/// Updates the section alignment (`sh_addralign`) field.
#[inline]
pub fn set_sh_addralign(&mut self, addralign: usize) {
self.shdr.set_sh_addralign(addralign);
}
/// Updates the section entry size (`sh_entsize`) field.
#[inline]
pub fn set_sh_entsize(&mut self, entsize: usize) {
self.shdr.set_sh_entsize(entsize);
}
/// Creates a new ELF section header with the specified parameters.
///
/// # Arguments
/// * `sh_name` - Section name string table index
/// * `sh_type` - Section type (e.g., PROGBITS, SYMTAB, etc.)
/// * `sh_flags` - Section flags (e.g., WRITE, ALLOC, EXECINSTR)
/// * `sh_addr` - Address where section should be loaded
/// * `sh_offset` - Offset of section in file
/// * `sh_size` - Size of section in bytes
/// * `sh_link` - Link to another section (interpretation depends on section type)
/// * `sh_info` - Extra information (interpretation depends on section type)
/// * `sh_addralign` - Address alignment constraint
/// * `sh_entsize` - Size of each entry if section contains a table
#[cfg(feature = "object")]
// Mirrors the ELF section-header field list.
#[allow(clippy::too_many_arguments)]
pub(crate) fn new(
sh_name: u32,
sh_type: ElfSectionType,
sh_flags: ElfSectionFlags,
sh_addr: usize,
sh_offset: usize,
sh_size: usize,
sh_link: u32,
sh_info: u32,
sh_addralign: usize,
sh_entsize: usize,
) -> Self {
let mut shdr: L::Shdr = unsafe { core::mem::zeroed() };
shdr.set_sh_name(sh_name);
shdr.set_sh_type(sh_type.raw());
shdr.set_sh_flags(sh_flags.bits());
shdr.set_sh_addr(sh_addr);
shdr.set_sh_offset(sh_offset);
shdr.set_sh_size(sh_size);
shdr.set_sh_link(sh_link);
shdr.set_sh_info(sh_info);
shdr.set_sh_addralign(sh_addralign);
shdr.set_sh_entsize(sh_entsize);
Self { shdr }
}
}