| 1 |
//! Symbol versioning |
| 2 |
//! |
| 3 |
//! Implementation of the GNU symbol versioning extension according to |
| 4 |
//! [LSB Core Specification - Symbol Versioning][lsb-symver]. |
| 5 |
//! |
| 6 |
//! # Examples |
| 7 |
//! |
| 8 |
//! List the dependencies of an ELF file that have [version needed][lsb-verneed] information along |
| 9 |
//! with the versions needed for each dependency. |
| 10 |
//! ```rust |
| 11 |
//! use goblin::error::Error; |
| 12 |
//! |
| 13 |
//! pub fn show_verneed(bytes: &[u8]) -> Result<(), Error> { |
| 14 |
//! let binary = goblin::elf::Elf::parse(&bytes)?; |
| 15 |
//! |
| 16 |
//! if let Some(verneed) = binary.verneed { |
| 17 |
//! for need_file in verneed.iter() { |
| 18 |
//! println!( |
| 19 |
//! "Depend on {:?} with version(s):", |
| 20 |
//! binary.dynstrtab.get_at(need_file.vn_file) |
| 21 |
//! ); |
| 22 |
//! for need_ver in need_file.iter() { |
| 23 |
//! println!("{:?}", binary.dynstrtab.get_at(need_ver.vna_name)); |
| 24 |
//! } |
| 25 |
//! } |
| 26 |
//! } |
| 27 |
//! |
| 28 |
//! Ok(()) |
| 29 |
//! } |
| 30 |
//! ``` |
| 31 |
//! |
| 32 |
//! List the [version defined][lsb-verdef] information of an ELF file, effectively listing the version |
| 33 |
//! defined by this ELF file. |
| 34 |
//! ```rust |
| 35 |
//! use goblin::error::Error; |
| 36 |
//! |
| 37 |
//! pub fn show_verdef(bytes: &[u8]) -> Result<(), Error> { |
| 38 |
//! let binary = goblin::elf::Elf::parse(&bytes)?; |
| 39 |
//! |
| 40 |
//! if let Some(verdef) = &binary.verdef { |
| 41 |
//! for def in verdef.iter() { |
| 42 |
//! for (n, aux) in def.iter().enumerate() { |
| 43 |
//! let name = binary.dynstrtab.get_at(aux.vda_name); |
| 44 |
//! match n { |
| 45 |
//! 0 => print!("Name: {:?}", name), |
| 46 |
//! 1 => print!(" Parent: {:?}", name), |
| 47 |
//! _ => print!(", {:?}", name), |
| 48 |
//! } |
| 49 |
//! } |
| 50 |
//! print!("\n"); |
| 51 |
//! } |
| 52 |
//! } |
| 53 |
//! |
| 54 |
//! Ok(()) |
| 55 |
//! } |
| 56 |
//! ``` |
| 57 |
//! |
| 58 |
//! [lsb-symver]: https://refspecs.linuxbase.org/LSB_5.0.0/LSB-Core-generic/LSB-Core-generic/symversion.html |
| 59 |
//! [lsb-verneed]: https://refspecs.linuxbase.org/LSB_5.0.0/LSB-Core-generic/LSB-Core-generic/symversion.html#SYMVERRQMTS |
| 60 |
//! [lsb-verdef]: https://refspecs.linuxbase.org/LSB_5.0.0/LSB-Core-generic/LSB-Core-generic/symversion.html#SYMVERDEFS |
| 61 |
|
| 62 |
use crate::container; |
| 63 |
use crate::elf::section_header::{SectionHeader, SHT_GNU_VERDEF, SHT_GNU_VERNEED, SHT_GNU_VERSYM}; |
| 64 |
use crate::error::Result; |
| 65 |
use core::iter::FusedIterator; |
| 66 |
use scroll::Pread; |
| 67 |
|
| 68 |
/****************** |
| 69 |
* ELF Constants * |
| 70 |
******************/ |
| 71 |
|
| 72 |
// Versym constants. |
| 73 |
|
| 74 |
/// Constant describing a local symbol, see [`Versym::is_local`]. |
| 75 |
pub const VER_NDX_LOCAL: u16 = 0; |
| 76 |
/// Constant describing a global symbol, see [`Versym::is_global`]. |
| 77 |
pub const VER_NDX_GLOBAL: u16 = 1; |
| 78 |
/// Bitmask to check hidden bit, see [`Versym::is_hidden`]. |
| 79 |
pub const VERSYM_HIDDEN: u16 = 0x8000; |
| 80 |
/// Bitmask to get version information, see [`Versym::version`]. |
| 81 |
pub const VERSYM_VERSION: u16 = 0x7fff; |
| 82 |
|
| 83 |
// Verdef constants. |
| 84 |
|
| 85 |
/// Bitmask to check `base` flag in [`Verdef::vd_flags`]. |
| 86 |
pub const VER_FLG_BASE: u16 = 0x1; |
| 87 |
/// Bitmask to check `weak` flag in [`Verdef::vd_flags`]. |
| 88 |
pub const VER_FLG_WEAK: u16 = 0x2; |
| 89 |
/// Bitmask to check `info` flag in [`Verdef::vd_flags`]. |
| 90 |
pub const VER_FLG_INFO: u16 = 0x4; |
| 91 |
|
| 92 |
/******************** |
| 93 |
* ELF Structures * |
| 94 |
********************/ |
| 95 |
|
| 96 |
/// An ELF `Symbol Version` entry. |
| 97 |
/// |
| 98 |
/// https://refspecs.linuxbase.org/LSB_5.0.0/LSB-Core-generic/LSB-Core-generic/symversion.html#SYMVERTBL |
| 99 |
#[repr(C)] |
| 100 |
#[derive(Debug, Pread)] |
| 101 |
struct ElfVersym { |
| 102 |
vs_val: u16, |
| 103 |
} |
| 104 |
|
| 105 |
/// An ELF `Version Definition` entry Elfxx_Verdef. |
| 106 |
/// |
| 107 |
/// https://refspecs.linuxbase.org/LSB_5.0.0/LSB-Core-generic/LSB-Core-generic/symversion.html#VERDEFENTRIES |
| 108 |
#[repr(C)] |
| 109 |
#[derive(Debug, Pread)] |
| 110 |
struct ElfVerdef { |
| 111 |
vd_version: u16, |
| 112 |
vd_flags: u16, |
| 113 |
vd_ndx: u16, |
| 114 |
vd_cnt: u16, |
| 115 |
vd_hash: u32, |
| 116 |
vd_aux: u32, |
| 117 |
vd_next: u32, |
| 118 |
} |
| 119 |
|
| 120 |
/// An ELF `Version Definition Auxiliary` entry Elfxx_Verdaux. |
| 121 |
/// |
| 122 |
/// https://refspecs.linuxbase.org/LSB_5.0.0/LSB-Core-generic/LSB-Core-generic/symversion.html#VERDEFEXTS |
| 123 |
#[repr(C)] |
| 124 |
#[derive(Debug, Pread)] |
| 125 |
struct ElfVerdaux { |
| 126 |
vda_name: u32, |
| 127 |
vda_next: u32, |
| 128 |
} |
| 129 |
|
| 130 |
/// An ELF `Version Need` entry Elfxx_Verneed. |
| 131 |
/// |
| 132 |
/// https://refspecs.linuxbase.org/LSB_5.0.0/LSB-Core-generic/LSB-Core-generic/symversion.html#VERNEEDFIG |
| 133 |
#[repr(C)] |
| 134 |
#[derive(Debug, Pread)] |
| 135 |
struct ElfVerneed { |
| 136 |
vn_version: u16, |
| 137 |
vn_cnt: u16, |
| 138 |
vn_file: u32, |
| 139 |
vn_aux: u32, |
| 140 |
vn_next: u32, |
| 141 |
} |
| 142 |
|
| 143 |
/// An ELF `Version Need Auxiliary` entry Elfxx_Vernaux. |
| 144 |
/// |
| 145 |
/// https://refspecs.linuxbase.org/LSB_5.0.0/LSB-Core-generic/LSB-Core-generic/symversion.html#VERNEEDEXTFIG |
| 146 |
#[repr(C)] |
| 147 |
#[derive(Debug, Pread)] |
| 148 |
struct ElfVernaux { |
| 149 |
vna_hash: u32, |
| 150 |
vna_flags: u16, |
| 151 |
vna_other: u16, |
| 152 |
vna_name: u32, |
| 153 |
vna_next: u32, |
| 154 |
} |
| 155 |
|
| 156 |
/******************** |
| 157 |
* Symbol Version * |
| 158 |
********************/ |
| 159 |
|
| 160 |
/// Helper struct to iterate over [Symbol Version][Versym] entries. |
| 161 |
#[derive(Debug)] |
| 162 |
pub struct VersymSection<'a> { |
| 163 |
bytes: &'a [u8], |
| 164 |
ctx: container::Ctx, |
| 165 |
} |
| 166 |
|
| 167 |
impl<'a> VersymSection<'a> { |
| 168 |
pub fn parse( |
| 169 |
bytes: &'a [u8], |
| 170 |
shdrs: &[SectionHeader], |
| 171 |
ctx: container::Ctx, |
| 172 |
) -> Result<Option<VersymSection<'a>>> { |
| 173 |
// Get fields needed from optional `symbol version` section. |
| 174 |
let (offset, size) = |
| 175 |
if let Some(shdr) = shdrs.iter().find(|shdr| shdr.sh_type == SHT_GNU_VERSYM) { |
| 176 |
(shdr.sh_offset as usize, shdr.sh_size as usize) |
| 177 |
} else { |
| 178 |
return Ok(None); |
| 179 |
}; |
| 180 |
|
| 181 |
// Get a slice of bytes of the `version definition` section content. |
| 182 |
let bytes: &'a [u8] = bytes.pread_with(offset, size)?; |
| 183 |
|
| 184 |
Ok(Some(VersymSection { bytes, ctx })) |
| 185 |
} |
| 186 |
|
| 187 |
/// Get an iterator over the [`Versym`] entries. |
| 188 |
#[inline] |
| 189 |
pub fn iter(&'a self) -> VersymIter<'a> { |
| 190 |
self.into_iter() |
| 191 |
} |
| 192 |
|
| 193 |
/// True if there are no [`Versym`] entries. |
| 194 |
#[inline] |
| 195 |
pub fn is_empty(&self) -> bool { |
| 196 |
self.bytes.is_empty() |
| 197 |
} |
| 198 |
|
| 199 |
/// Number of [`Versym`] entries. |
| 200 |
#[inline] |
| 201 |
pub fn len(&self) -> usize { |
| 202 |
let entsize = core::mem::size_of::<ElfVersym>(); |
| 203 |
|
| 204 |
self.bytes.len() / entsize |
| 205 |
} |
| 206 |
|
| 207 |
/// Get [`Versym`] entry at index. |
| 208 |
#[inline] |
| 209 |
pub fn get_at(&self, idx: usize) -> Option<Versym> { |
| 210 |
let entsize = core::mem::size_of::<ElfVersym>(); |
| 211 |
let offset = idx.checked_mul(entsize)?; |
| 212 |
|
| 213 |
self.bytes |
| 214 |
.pread_with::<ElfVersym>(offset, self.ctx.le) |
| 215 |
.ok() |
| 216 |
.map(Versym::from) |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
impl<'a> IntoIterator for &'_ VersymSection<'a> { |
| 221 |
type Item = <VersymIter<'a> as Iterator>::Item; |
| 222 |
type IntoIter = VersymIter<'a>; |
| 223 |
|
| 224 |
fn into_iter(self) -> Self::IntoIter { |
| 225 |
VersymIter { |
| 226 |
bytes: self.bytes, |
| 227 |
offset: 0, |
| 228 |
ctx: self.ctx, |
| 229 |
} |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
/// Iterator over the [`Versym`] entries from the [`SHT_GNU_VERSYM`] section. |
| 234 |
pub struct VersymIter<'a> { |
| 235 |
bytes: &'a [u8], |
| 236 |
offset: usize, |
| 237 |
ctx: container::Ctx, |
| 238 |
} |
| 239 |
|
| 240 |
impl<'a> Iterator for VersymIter<'a> { |
| 241 |
type Item = Versym; |
| 242 |
|
| 243 |
fn next(&mut self) -> Option<Self::Item> { |
| 244 |
if self.offset >= self.bytes.len() { |
| 245 |
None |
| 246 |
} else { |
| 247 |
self.bytes |
| 248 |
.gread_with::<ElfVersym>(&mut self.offset, self.ctx.le) |
| 249 |
.ok() |
| 250 |
.map(Versym::from) |
| 251 |
.or_else(|| { |
| 252 |
// self.bytes are not a multiple of ElfVersym. |
| 253 |
// Adjust offset to continue yielding Nones. |
| 254 |
self.offset = self.bytes.len(); |
| 255 |
None |
| 256 |
}) |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
#[inline] |
| 261 |
fn size_hint(&self) -> (usize, Option<usize>) { |
| 262 |
let len = (self.bytes.len() - self.offset) / core::mem::size_of::<Self::Item>(); |
| 263 |
(len, Some(len)) |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
impl ExactSizeIterator for VersymIter<'_> {} |
| 268 |
|
| 269 |
impl FusedIterator for VersymIter<'_> {} |
| 270 |
|
| 271 |
/// An ELF [Symbol Version][lsb-versym] entry. |
| 272 |
/// |
| 273 |
/// [lsb-versym]: https://refspecs.linuxbase.org/LSB_5.0.0/LSB-Core-generic/LSB-Core-generic/symversion.html#SYMVERTBL |
| 274 |
#[derive(Debug)] |
| 275 |
pub struct Versym { |
| 276 |
pub vs_val: u16, |
| 277 |
} |
| 278 |
|
| 279 |
impl Versym { |
| 280 |
/// Returns true if the symbol is local and not available outside the object according to |
| 281 |
/// [`VER_NDX_LOCAL`]. |
| 282 |
#[inline] |
| 283 |
pub fn is_local(&self) -> bool { |
| 284 |
self.vs_val == VER_NDX_LOCAL |
| 285 |
} |
| 286 |
|
| 287 |
/// Returns true if the symbol is defined in this object and globally available according |
| 288 |
/// to [`VER_NDX_GLOBAL`]. |
| 289 |
#[inline] |
| 290 |
pub fn is_global(&self) -> bool { |
| 291 |
self.vs_val == VER_NDX_GLOBAL |
| 292 |
} |
| 293 |
|
| 294 |
/// Returns true if the `hidden` bit is set according to the [`VERSYM_HIDDEN`] bitmask. |
| 295 |
#[inline] |
| 296 |
pub fn is_hidden(&self) -> bool { |
| 297 |
(self.vs_val & VERSYM_HIDDEN) == VERSYM_HIDDEN |
| 298 |
} |
| 299 |
|
| 300 |
/// Returns the symbol version index according to the [`VERSYM_VERSION`] bitmask. |
| 301 |
#[inline] |
| 302 |
pub fn version(&self) -> u16 { |
| 303 |
self.vs_val & VERSYM_VERSION |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
impl From<ElfVersym> for Versym { |
| 308 |
fn from(ElfVersym { vs_val }: ElfVersym) -> Self { |
| 309 |
Versym { vs_val } |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
/************************ |
| 314 |
* Version Definition * |
| 315 |
************************/ |
| 316 |
|
| 317 |
/// Helper struct to iterate over [Version Definition][Verdef] and [Version Definition |
| 318 |
/// Auxiliary][Verdaux] entries. |
| 319 |
#[derive(Debug)] |
| 320 |
pub struct VerdefSection<'a> { |
| 321 |
/// String table used to resolve version strings. |
| 322 |
bytes: &'a [u8], |
| 323 |
count: usize, |
| 324 |
ctx: container::Ctx, |
| 325 |
} |
| 326 |
|
| 327 |
impl<'a> VerdefSection<'a> { |
| 328 |
pub fn parse( |
| 329 |
bytes: &'a [u8], |
| 330 |
shdrs: &[SectionHeader], |
| 331 |
ctx: container::Ctx, |
| 332 |
) -> Result<Option<VerdefSection<'a>>> { |
| 333 |
// Get fields needed from optional `version definition` section. |
| 334 |
let (offset, size, count) = |
| 335 |
if let Some(shdr) = shdrs.iter().find(|shdr| shdr.sh_type == SHT_GNU_VERDEF) { |
| 336 |
( |
| 337 |
shdr.sh_offset as usize, |
| 338 |
shdr.sh_size as usize, |
| 339 |
shdr.sh_info as usize, // Encodes the number of ElfVerdef entries. |
| 340 |
) |
| 341 |
} else { |
| 342 |
return Ok(None); |
| 343 |
}; |
| 344 |
|
| 345 |
// Get a slice of bytes of the `version definition` section content. |
| 346 |
let bytes: &'a [u8] = bytes.pread_with(offset, size)?; |
| 347 |
|
| 348 |
Ok(Some(VerdefSection { bytes, count, ctx })) |
| 349 |
} |
| 350 |
|
| 351 |
/// Get an iterator over the [`Verdef`] entries. |
| 352 |
#[inline] |
| 353 |
pub fn iter(&'a self) -> VerdefIter<'a> { |
| 354 |
self.into_iter() |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
impl<'a> IntoIterator for &'_ VerdefSection<'a> { |
| 359 |
type Item = <VerdefIter<'a> as Iterator>::Item; |
| 360 |
type IntoIter = VerdefIter<'a>; |
| 361 |
|
| 362 |
#[inline] |
| 363 |
fn into_iter(self) -> Self::IntoIter { |
| 364 |
VerdefIter { |
| 365 |
bytes: self.bytes, |
| 366 |
count: self.count, |
| 367 |
index: 0, |
| 368 |
offset: 0, |
| 369 |
ctx: self.ctx, |
| 370 |
} |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
/// Iterator over the [`Verdef`] entries from the [`SHT_GNU_VERDEF`] section. |
| 375 |
pub struct VerdefIter<'a> { |
| 376 |
bytes: &'a [u8], |
| 377 |
count: usize, |
| 378 |
index: usize, |
| 379 |
offset: usize, |
| 380 |
ctx: container::Ctx, |
| 381 |
} |
| 382 |
|
| 383 |
impl<'a> Iterator for VerdefIter<'a> { |
| 384 |
type Item = Verdef<'a>; |
| 385 |
|
| 386 |
fn next(&mut self) -> Option<Self::Item> { |
| 387 |
if self.index >= self.count { |
| 388 |
None |
| 389 |
} else { |
| 390 |
self.index += 1; |
| 391 |
|
| 392 |
let do_next = |iter: &mut Self| { |
| 393 |
let ElfVerdef { |
| 394 |
vd_version, |
| 395 |
vd_flags, |
| 396 |
vd_ndx, |
| 397 |
vd_cnt, |
| 398 |
vd_hash, |
| 399 |
vd_aux, |
| 400 |
vd_next, |
| 401 |
} = iter.bytes.pread_with(iter.offset, iter.ctx.le).ok()?; |
| 402 |
|
| 403 |
// Validate offset to first ElfVerdaux entry. |
| 404 |
let offset = iter.offset.checked_add(vd_aux as usize)?; |
| 405 |
|
| 406 |
// Validate if offset is valid index into bytes slice. |
| 407 |
if offset >= iter.bytes.len() { |
| 408 |
return None; |
| 409 |
} |
| 410 |
|
| 411 |
// Get a slice of bytes starting with the first ElfVerdaux entry. |
| 412 |
let bytes: &'a [u8] = &iter.bytes[offset..]; |
| 413 |
|
| 414 |
// Bump the offset to the next ElfVerdef entry. |
| 415 |
iter.offset = iter.offset.checked_add(vd_next as usize)?; |
| 416 |
|
| 417 |
// Start yielding None on the next call if there is no next offset. |
| 418 |
if vd_next == 0 { |
| 419 |
iter.index = iter.count; |
| 420 |
} |
| 421 |
|
| 422 |
Some(Verdef { |
| 423 |
vd_version, |
| 424 |
vd_flags, |
| 425 |
vd_ndx, |
| 426 |
vd_cnt, |
| 427 |
vd_hash, |
| 428 |
vd_aux, |
| 429 |
vd_next, |
| 430 |
bytes, |
| 431 |
ctx: iter.ctx, |
| 432 |
}) |
| 433 |
}; |
| 434 |
|
| 435 |
do_next(self).or_else(|| { |
| 436 |
// Adjust current index to count in case of an error. |
| 437 |
self.index = self.count; |
| 438 |
None |
| 439 |
}) |
| 440 |
} |
| 441 |
} |
| 442 |
|
| 443 |
#[inline] |
| 444 |
fn size_hint(&self) -> (usize, Option<usize>) { |
| 445 |
let len = self.count - self.index; |
| 446 |
(0, Some(len)) |
| 447 |
} |
| 448 |
} |
| 449 |
|
| 450 |
impl ExactSizeIterator for VerdefIter<'_> {} |
| 451 |
|
| 452 |
impl FusedIterator for VerdefIter<'_> {} |
| 453 |
|
| 454 |
/// An ELF [Version Definition][lsb-verdef] entry . |
| 455 |
/// |
| 456 |
/// [lsb-verdef]: https://refspecs.linuxbase.org/LSB_5.0.0/LSB-Core-generic/LSB-Core-generic/symversion.html#VERDEFENTRIES |
| 457 |
#[derive(Debug)] |
| 458 |
pub struct Verdef<'a> { |
| 459 |
/// Version revision. This field shall be set to 1. |
| 460 |
pub vd_version: u16, |
| 461 |
/// Version information flag bitmask. |
| 462 |
pub vd_flags: u16, |
| 463 |
/// Version index numeric value referencing the SHT_GNU_versym section. |
| 464 |
pub vd_ndx: u16, |
| 465 |
/// Number of associated verdaux array entries. |
| 466 |
pub vd_cnt: u16, |
| 467 |
/// Version name hash value (ELF hash function). |
| 468 |
pub vd_hash: u32, |
| 469 |
/// Offset in bytes to a corresponding entry in an array of Elfxx_Verdaux structures. |
| 470 |
pub vd_aux: u32, |
| 471 |
/// Offset to the next verdef entry, in bytes. |
| 472 |
pub vd_next: u32, |
| 473 |
|
| 474 |
bytes: &'a [u8], |
| 475 |
ctx: container::Ctx, |
| 476 |
} |
| 477 |
|
| 478 |
impl<'a> Verdef<'a> { |
| 479 |
/// Get an iterator over the [`Verdaux`] entries of this [`Verdef`] entry. |
| 480 |
#[inline] |
| 481 |
pub fn iter(&'a self) -> VerdauxIter<'a> { |
| 482 |
self.into_iter() |
| 483 |
} |
| 484 |
} |
| 485 |
|
| 486 |
impl<'a> IntoIterator for &'_ Verdef<'a> { |
| 487 |
type Item = <VerdauxIter<'a> as Iterator>::Item; |
| 488 |
type IntoIter = VerdauxIter<'a>; |
| 489 |
|
| 490 |
fn into_iter(self) -> Self::IntoIter { |
| 491 |
VerdauxIter { |
| 492 |
bytes: self.bytes, |
| 493 |
count: self.vd_cnt, |
| 494 |
index: 0, |
| 495 |
offset: 0, |
| 496 |
ctx: self.ctx, |
| 497 |
} |
| 498 |
} |
| 499 |
} |
| 500 |
|
| 501 |
/// Iterator over the [`Verdaux`] entries for an specific [`Verdef`] entry. |
| 502 |
pub struct VerdauxIter<'a> { |
| 503 |
bytes: &'a [u8], |
| 504 |
count: u16, |
| 505 |
index: u16, |
| 506 |
offset: usize, |
| 507 |
ctx: container::Ctx, |
| 508 |
} |
| 509 |
|
| 510 |
impl<'a> Iterator for VerdauxIter<'a> { |
| 511 |
type Item = Verdaux; |
| 512 |
|
| 513 |
fn next(&mut self) -> Option<Self::Item> { |
| 514 |
if self.index >= self.count { |
| 515 |
None |
| 516 |
} else { |
| 517 |
self.index += 1; |
| 518 |
|
| 519 |
let do_next = |iter: &mut Self| { |
| 520 |
let ElfVerdaux { vda_name, vda_next } = |
| 521 |
iter.bytes.pread_with(iter.offset, iter.ctx.le).ok()?; |
| 522 |
|
| 523 |
// Bump the offset to the next ElfVerdaux entry. |
| 524 |
iter.offset = iter.offset.checked_add(vda_next as usize)?; |
| 525 |
|
| 526 |
// Start yielding None on the next call if there is no next offset. |
| 527 |
if vda_next == 0 { |
| 528 |
iter.index = iter.count; |
| 529 |
} |
| 530 |
|
| 531 |
Some(Verdaux { |
| 532 |
vda_name: vda_name as usize, |
| 533 |
vda_next, |
| 534 |
}) |
| 535 |
}; |
| 536 |
|
| 537 |
do_next(self).or_else(|| { |
| 538 |
// Adjust current index to count in case of an error. |
| 539 |
self.index = self.count; |
| 540 |
None |
| 541 |
}) |
| 542 |
} |
| 543 |
} |
| 544 |
|
| 545 |
#[inline] |
| 546 |
fn size_hint(&self) -> (usize, Option<usize>) { |
| 547 |
let len = usize::from(self.count - self.index); |
| 548 |
(0, Some(len)) |
| 549 |
} |
| 550 |
} |
| 551 |
|
| 552 |
impl ExactSizeIterator for VerdauxIter<'_> {} |
| 553 |
|
| 554 |
impl FusedIterator for VerdauxIter<'_> {} |
| 555 |
|
| 556 |
/// An ELF [Version Definition Auxiliary][lsb-verdaux] entry. |
| 557 |
/// |
| 558 |
/// [lsb-verdaux]: https://refspecs.linuxbase.org/LSB_5.0.0/LSB-Core-generic/LSB-Core-generic/symversion.html#VERDEFEXTS |
| 559 |
#[derive(Debug)] |
| 560 |
pub struct Verdaux { |
| 561 |
/// Offset to the version or dependency name string in the section header, in bytes. |
| 562 |
pub vda_name: usize, |
| 563 |
/// Offset to the next verdaux entry, in bytes. |
| 564 |
pub vda_next: u32, |
| 565 |
} |
| 566 |
|
| 567 |
/************************** |
| 568 |
* Version Requirements * |
| 569 |
**************************/ |
| 570 |
|
| 571 |
/// Helper struct to iterate over [Version Needed][Verneed] and [Version Needed |
| 572 |
/// Auxiliary][Vernaux] entries. |
| 573 |
#[derive(Debug)] |
| 574 |
pub struct VerneedSection<'a> { |
| 575 |
bytes: &'a [u8], |
| 576 |
count: usize, |
| 577 |
ctx: container::Ctx, |
| 578 |
} |
| 579 |
|
| 580 |
impl<'a> VerneedSection<'a> { |
| 581 |
/// Try to parse the optional [`SHT_GNU_VERNEED`] section. |
| 582 |
pub fn parse( |
| 583 |
bytes: &'a [u8], |
| 584 |
shdrs: &[SectionHeader], |
| 585 |
ctx: container::Ctx, |
| 586 |
) -> Result<Option<VerneedSection<'a>>> { |
| 587 |
// Get fields needed from optional `version needed` section. |
| 588 |
let (offset, size, count) = |
| 589 |
if let Some(shdr) = shdrs.iter().find(|shdr| shdr.sh_type == SHT_GNU_VERNEED) { |
| 590 |
( |
| 591 |
shdr.sh_offset as usize, |
| 592 |
shdr.sh_size as usize, |
| 593 |
shdr.sh_info as usize, // Encodes the number of ElfVerneed entries. |
| 594 |
) |
| 595 |
} else { |
| 596 |
return Ok(None); |
| 597 |
}; |
| 598 |
|
| 599 |
// Get a slice of bytes of the `version needed` section content. |
| 600 |
let bytes: &'a [u8] = bytes.pread_with(offset, size)?; |
| 601 |
|
| 602 |
Ok(Some(VerneedSection { bytes, count, ctx })) |
| 603 |
} |
| 604 |
|
| 605 |
/// Get an iterator over the [`Verneed`] entries. |
| 606 |
#[inline] |
| 607 |
pub fn iter(&'a self) -> VerneedIter<'a> { |
| 608 |
self.into_iter() |
| 609 |
} |
| 610 |
} |
| 611 |
|
| 612 |
impl<'a> IntoIterator for &'_ VerneedSection<'a> { |
| 613 |
type Item = <VerneedIter<'a> as Iterator>::Item; |
| 614 |
type IntoIter = VerneedIter<'a>; |
| 615 |
|
| 616 |
#[inline] |
| 617 |
fn into_iter(self) -> Self::IntoIter { |
| 618 |
VerneedIter { |
| 619 |
bytes: self.bytes, |
| 620 |
count: self.count, |
| 621 |
index: 0, |
| 622 |
offset: 0, |
| 623 |
ctx: self.ctx, |
| 624 |
} |
| 625 |
} |
| 626 |
} |
| 627 |
|
| 628 |
/// Iterator over the [`Verneed`] entries from the [`SHT_GNU_VERNEED`] section. |
| 629 |
pub struct VerneedIter<'a> { |
| 630 |
bytes: &'a [u8], |
| 631 |
count: usize, |
| 632 |
index: usize, |
| 633 |
offset: usize, |
| 634 |
ctx: container::Ctx, |
| 635 |
} |
| 636 |
|
| 637 |
impl<'a> Iterator for VerneedIter<'a> { |
| 638 |
type Item = Verneed<'a>; |
| 639 |
|
| 640 |
fn next(&mut self) -> Option<Self::Item> { |
| 641 |
if self.index >= self.count { |
| 642 |
None |
| 643 |
} else { |
| 644 |
self.index += 1; |
| 645 |
|
| 646 |
let do_next = |iter: &mut Self| { |
| 647 |
let ElfVerneed { |
| 648 |
vn_version, |
| 649 |
vn_cnt, |
| 650 |
vn_file, |
| 651 |
vn_aux, |
| 652 |
vn_next, |
| 653 |
} = iter.bytes.pread_with(iter.offset, iter.ctx.le).ok()?; |
| 654 |
|
| 655 |
// Validate offset to first ElfVernaux entry. |
| 656 |
let offset = iter.offset.checked_add(vn_aux as usize)?; |
| 657 |
|
| 658 |
// Validate if offset is valid index into bytes slice. |
| 659 |
if offset >= iter.bytes.len() { |
| 660 |
return None; |
| 661 |
} |
| 662 |
|
| 663 |
// Get a slice of bytes starting with the first ElfVernaux entry. |
| 664 |
let bytes: &'a [u8] = &iter.bytes[offset..]; |
| 665 |
|
| 666 |
// Bump the offset to the next ElfVerneed entry. |
| 667 |
iter.offset = iter.offset.checked_add(vn_next as usize)?; |
| 668 |
|
| 669 |
// Start yielding None on the next call if there is no next offset. |
| 670 |
if vn_next == 0 { |
| 671 |
iter.index = iter.count; |
| 672 |
} |
| 673 |
|
| 674 |
Some(Verneed { |
| 675 |
vn_version, |
| 676 |
vn_cnt, |
| 677 |
vn_file: vn_file as usize, |
| 678 |
vn_aux, |
| 679 |
vn_next, |
| 680 |
bytes, |
| 681 |
ctx: iter.ctx, |
| 682 |
}) |
| 683 |
}; |
| 684 |
|
| 685 |
do_next(self).or_else(|| { |
| 686 |
// Adjust current index to count in case of an error. |
| 687 |
self.index = self.count; |
| 688 |
None |
| 689 |
}) |
| 690 |
} |
| 691 |
} |
| 692 |
|
| 693 |
#[inline] |
| 694 |
fn size_hint(&self) -> (usize, Option<usize>) { |
| 695 |
let len = self.count - self.index; |
| 696 |
(0, Some(len)) |
| 697 |
} |
| 698 |
} |
| 699 |
|
| 700 |
impl ExactSizeIterator for VerneedIter<'_> {} |
| 701 |
|
| 702 |
impl FusedIterator for VerneedIter<'_> {} |
| 703 |
|
| 704 |
/// An ELF [Version Need][lsb-verneed] entry . |
| 705 |
/// |
| 706 |
/// [lsb-verneed]: https://refspecs.linuxbase.org/LSB_5.0.0/LSB-Core-generic/LSB-Core-generic/symversion.html#VERNEEDFIG |
| 707 |
#[derive(Debug)] |
| 708 |
pub struct Verneed<'a> { |
| 709 |
/// Version of structure. This value is currently set to 1, and will be reset if the versioning |
| 710 |
/// implementation is incompatibly altered. |
| 711 |
pub vn_version: u16, |
| 712 |
/// Number of associated verneed array entries. |
| 713 |
pub vn_cnt: u16, |
| 714 |
/// Offset to the file name string in the section header, in bytes. |
| 715 |
pub vn_file: usize, |
| 716 |
/// Offset to a corresponding entry in the vernaux array, in bytes. |
| 717 |
pub vn_aux: u32, |
| 718 |
/// Offset to the next verneed entry, in bytes. |
| 719 |
pub vn_next: u32, |
| 720 |
|
| 721 |
bytes: &'a [u8], |
| 722 |
ctx: container::Ctx, |
| 723 |
} |
| 724 |
|
| 725 |
impl<'a> Verneed<'a> { |
| 726 |
/// Get an iterator over the [`Vernaux`] entries of this [`Verneed`] entry. |
| 727 |
#[inline] |
| 728 |
pub fn iter(&'a self) -> VernauxIter<'a> { |
| 729 |
self.into_iter() |
| 730 |
} |
| 731 |
} |
| 732 |
|
| 733 |
impl<'a> IntoIterator for &'_ Verneed<'a> { |
| 734 |
type Item = <VernauxIter<'a> as Iterator>::Item; |
| 735 |
type IntoIter = VernauxIter<'a>; |
| 736 |
|
| 737 |
#[inline] |
| 738 |
fn into_iter(self) -> Self::IntoIter { |
| 739 |
VernauxIter { |
| 740 |
bytes: self.bytes, |
| 741 |
count: self.vn_cnt, |
| 742 |
index: 0, |
| 743 |
offset: 0, |
| 744 |
ctx: self.ctx, |
| 745 |
} |
| 746 |
} |
| 747 |
} |
| 748 |
|
| 749 |
/// Iterator over the [`Vernaux`] entries for an specific [`Verneed`] entry. |
| 750 |
pub struct VernauxIter<'a> { |
| 751 |
bytes: &'a [u8], |
| 752 |
count: u16, |
| 753 |
index: u16, |
| 754 |
offset: usize, |
| 755 |
ctx: container::Ctx, |
| 756 |
} |
| 757 |
|
| 758 |
impl<'a> Iterator for VernauxIter<'a> { |
| 759 |
type Item = Vernaux; |
| 760 |
|
| 761 |
fn next(&mut self) -> Option<Self::Item> { |
| 762 |
if self.index >= self.count { |
| 763 |
None |
| 764 |
} else { |
| 765 |
self.index += 1; |
| 766 |
|
| 767 |
let do_next = |iter: &mut Self| { |
| 768 |
let ElfVernaux { |
| 769 |
vna_hash, |
| 770 |
vna_flags, |
| 771 |
vna_other, |
| 772 |
vna_name, |
| 773 |
vna_next, |
| 774 |
} = iter.bytes.pread_with(iter.offset, iter.ctx.le).ok()?; |
| 775 |
|
| 776 |
// Bump the offset to the next ElfVernaux entry. |
| 777 |
iter.offset = iter.offset.checked_add(vna_next as usize)?; |
| 778 |
|
| 779 |
// Start yielding None on the next call if there is no next offset. |
| 780 |
if vna_next == 0 { |
| 781 |
iter.index = iter.count; |
| 782 |
} |
| 783 |
|
| 784 |
Some(Vernaux { |
| 785 |
vna_hash, |
| 786 |
vna_flags, |
| 787 |
vna_other, |
| 788 |
vna_name: vna_name as usize, |
| 789 |
vna_next, |
| 790 |
}) |
| 791 |
}; |
| 792 |
|
| 793 |
do_next(self).or_else(|| { |
| 794 |
// Adjust current index to count in case of an error. |
| 795 |
self.index = self.count; |
| 796 |
None |
| 797 |
}) |
| 798 |
} |
| 799 |
} |
| 800 |
|
| 801 |
#[inline] |
| 802 |
fn size_hint(&self) -> (usize, Option<usize>) { |
| 803 |
let len = usize::from(self.count - self.index); |
| 804 |
(0, Some(len)) |
| 805 |
} |
| 806 |
} |
| 807 |
|
| 808 |
impl ExactSizeIterator for VernauxIter<'_> {} |
| 809 |
|
| 810 |
impl FusedIterator for VernauxIter<'_> {} |
| 811 |
|
| 812 |
/// An ELF [Version Need Auxiliary][lsb-vernaux] entry. |
| 813 |
/// |
| 814 |
/// [lsb-vernaux]: https://refspecs.linuxbase.org/LSB_5.0.0/LSB-Core-generic/LSB-Core-generic/symversion.html#VERNEEDEXTFIG |
| 815 |
#[derive(Debug)] |
| 816 |
pub struct Vernaux { |
| 817 |
/// Dependency name hash value (ELF hash function). |
| 818 |
pub vna_hash: u32, |
| 819 |
/// Dependency information flag bitmask. |
| 820 |
pub vna_flags: u16, |
| 821 |
/// Object file version identifier used in the .gnu.version symbol version array. Bit number 15 |
| 822 |
/// controls whether or not the object is hidden; if this bit is set, the object cannot be used |
| 823 |
/// and the static linker will ignore the symbol's presence in the object. |
| 824 |
pub vna_other: u16, |
| 825 |
/// Offset to the dependency name string in the section header, in bytes. |
| 826 |
pub vna_name: usize, |
| 827 |
/// Offset to the next vernaux entry, in bytes. |
| 828 |
pub vna_next: u32, |
| 829 |
} |
| 830 |
|
| 831 |
#[cfg(test)] |
| 832 |
mod test { |
| 833 |
use super::{ElfVerdaux, ElfVerdef, ElfVernaux, ElfVerneed, ElfVersym}; |
| 834 |
use super::{Versym, VERSYM_HIDDEN, VER_NDX_GLOBAL, VER_NDX_LOCAL}; |
| 835 |
use core::mem::size_of; |
| 836 |
|
| 837 |
#[test] |
| 838 |
fn check_size() { |
| 839 |
assert_eq!(2, size_of::<ElfVersym>()); |
| 840 |
assert_eq!(20, size_of::<ElfVerdef>()); |
| 841 |
assert_eq!(8, size_of::<ElfVerdaux>()); |
| 842 |
assert_eq!(16, size_of::<ElfVerneed>()); |
| 843 |
assert_eq!(16, size_of::<ElfVernaux>()); |
| 844 |
} |
| 845 |
|
| 846 |
#[test] |
| 847 |
fn check_versym() { |
| 848 |
let local = Versym { |
| 849 |
vs_val: VER_NDX_LOCAL, |
| 850 |
}; |
| 851 |
assert_eq!(true, local.is_local()); |
| 852 |
assert_eq!(false, local.is_global()); |
| 853 |
assert_eq!(false, local.is_hidden()); |
| 854 |
assert_eq!(VER_NDX_LOCAL, local.version()); |
| 855 |
|
| 856 |
let global = Versym { |
| 857 |
vs_val: VER_NDX_GLOBAL, |
| 858 |
}; |
| 859 |
assert_eq!(false, global.is_local()); |
| 860 |
assert_eq!(true, global.is_global()); |
| 861 |
assert_eq!(false, global.is_hidden()); |
| 862 |
assert_eq!(VER_NDX_GLOBAL, global.version()); |
| 863 |
|
| 864 |
let hidden = Versym { |
| 865 |
vs_val: VERSYM_HIDDEN, |
| 866 |
}; |
| 867 |
assert_eq!(false, hidden.is_local()); |
| 868 |
assert_eq!(false, hidden.is_global()); |
| 869 |
assert_eq!(true, hidden.is_hidden()); |
| 870 |
assert_eq!(0, hidden.version()); |
| 871 |
|
| 872 |
let hidden = Versym { |
| 873 |
vs_val: VERSYM_HIDDEN | 0x123, |
| 874 |
}; |
| 875 |
assert_eq!(false, hidden.is_local()); |
| 876 |
assert_eq!(false, hidden.is_global()); |
| 877 |
assert_eq!(true, hidden.is_hidden()); |
| 878 |
assert_eq!(0x123, hidden.version()); |
| 879 |
} |
| 880 |
} |