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
//! Section-header related ELF types
//!
//! This module contains section-header views used while scanning ELF sections.
use super::defs::{ElfLayout, ElfSectionFlags, ElfSectionType, ElfShdrRaw, NativeElfLayout};
/// 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 address (`sh_addr`) field.
#[inline]
#[cfg(feature = "object")]
pub(crate) 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]
#[cfg(feature = "object")]
pub(crate) fn add_sh_addr(&mut self, delta: usize) {
self.shdr.add_sh_addr(delta);
}
/// 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")]
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 }
}
}
impl<L: ElfLayout> ElfShdr<L> {
/// Returns a reference to the section content as a slice of the specified type.
///
/// This method provides safe access to section data by interpreting the section
/// as a contiguous array of elements of type `T`. The section must contain a table
/// of fixed-size entries for this to be meaningful.
///
/// # Safety
/// The caller must ensure that the section actually contains valid data of type `T`
/// and that the alignment and size constraints are met.
///
/// # Panics
/// Panics in debug builds if the element size doesn't match the section's entry size,
/// if the section size is not divisible by the entry size, or if the address is not
/// properly aligned.
#[cfg(feature = "object")]
pub(crate) fn content<T>(&self) -> &'static [T] {
self.content_mut()
}
/// Returns a mutable reference to the section content as a slice of the specified type.
///
/// This method provides mutable access to section data. Use with caution as it allows
/// modification of the underlying ELF data.
///
/// # Safety
/// The caller must ensure that the section actually contains valid data of type `T`
/// and that the alignment and size constraints are met. Modifying section data may
/// corrupt the ELF file or cause runtime errors.
///
/// # Panics
/// Panics in debug builds if the element size doesn't match the section's entry size,
/// if the section size is not divisible by the entry size, or if the address is not
/// properly aligned.
#[cfg(feature = "object")]
pub(crate) fn content_mut<T>(&self) -> &'static mut [T] {
let start = self.sh_addr();
let len = self.sh_size() / self.sh_entsize();
debug_assert!(core::mem::size_of::<T>() == self.sh_entsize());
debug_assert!(self.sh_size().is_multiple_of(self.sh_entsize()));
debug_assert!(self.sh_addr().is_multiple_of(self.sh_addralign()));
unsafe { core::slice::from_raw_parts_mut(start as *mut T, len) }
}
}