1use core::fmt::Debug;
2use core::{iter, slice, str};
3
4use crate::elf;
5use crate::endian::{self, Endianness, U32Bytes};
6use crate::pod::{self, Pod};
7use crate::read::{
8 self, gnu_compression, CompressedData, CompressedFileRange, CompressionFormat, Error,
9 ObjectSection, ReadError, ReadRef, RelocationMap, SectionFlags, SectionIndex, SectionKind,
10 StringTable,
11};
12
13use super::{
14 AttributesSection, CompressionHeader, ElfFile, ElfSectionRelocationIterator, FileHeader,
15 GnuHashTable, HashTable, NoteIterator, RelocationSections, RelrIterator, SymbolTable,
16 VerdefIterator, VerneedIterator, VersionTable,
17};
18
19#[derive(Debug, Clone, Copy)]
25pub struct SectionTable<'data, Elf: FileHeader, R = &'data [u8]>
26where
27 R: ReadRef<'data>,
28{
29 sections: &'data [Elf::SectionHeader],
30 strings: StringTable<'data, R>,
31}
32
33impl<'data, Elf: FileHeader, R: ReadRef<'data>> Default for SectionTable<'data, Elf, R> {
34 fn default() -> Self {
35 SectionTable {
36 sections: &[],
37 strings: StringTable::default(),
38 }
39 }
40}
41
42impl<'data, Elf: FileHeader, R: ReadRef<'data>> SectionTable<'data, Elf, R> {
43 #[inline]
45 pub fn new(sections: &'data [Elf::SectionHeader], strings: StringTable<'data, R>) -> Self {
46 SectionTable { sections, strings }
47 }
48
49 #[inline]
53 pub fn iter(&self) -> slice::Iter<'data, Elf::SectionHeader> {
54 self.sections.iter()
55 }
56
57 #[inline]
61 pub fn enumerate(&self) -> impl Iterator<Item = (SectionIndex, &'data Elf::SectionHeader)> {
62 self.sections
63 .iter()
64 .enumerate()
65 .map(|(i, section)| (SectionIndex(i), section))
66 }
67
68 #[inline]
70 pub fn is_empty(&self) -> bool {
71 self.sections.is_empty()
72 }
73
74 #[inline]
76 pub fn len(&self) -> usize {
77 self.sections.len()
78 }
79
80 pub fn section(&self, index: SectionIndex) -> read::Result<&'data Elf::SectionHeader> {
84 if index == SectionIndex(0) {
85 return Err(read::Error("Invalid ELF section index"));
86 }
87 self.sections
88 .get(index.0)
89 .read_error("Invalid ELF section index")
90 }
91
92 pub fn section_by_name(
96 &self,
97 endian: Elf::Endian,
98 name: &[u8],
99 ) -> Option<(SectionIndex, &'data Elf::SectionHeader)> {
100 self.enumerate()
101 .find(|(_, section)| self.section_name(endian, section) == Ok(name))
102 }
103
104 pub fn section_name(
106 &self,
107 endian: Elf::Endian,
108 section: &Elf::SectionHeader,
109 ) -> read::Result<&'data [u8]> {
110 section.name(endian, self.strings)
111 }
112
113 #[inline]
118 pub fn strings(
119 &self,
120 endian: Elf::Endian,
121 data: R,
122 index: SectionIndex,
123 ) -> read::Result<StringTable<'data, R>> {
124 if index == SectionIndex(0) {
125 return Ok(StringTable::default());
126 }
127 self.section(index)?
128 .strings(endian, data)?
129 .read_error("Invalid ELF string section type")
130 }
131
132 #[inline]
136 pub fn symbols(
137 &self,
138 endian: Elf::Endian,
139 data: R,
140 sh_type: u32,
141 ) -> read::Result<SymbolTable<'data, Elf, R>> {
142 debug_assert!(sh_type == elf::SHT_DYNSYM || sh_type == elf::SHT_SYMTAB);
143
144 let (index, section) = match self.enumerate().find(|s| s.1.sh_type(endian) == sh_type) {
145 Some(s) => s,
146 None => return Ok(SymbolTable::default()),
147 };
148
149 SymbolTable::parse(endian, data, self, index, section)
150 }
151
152 #[inline]
156 pub fn symbol_table_by_index(
157 &self,
158 endian: Elf::Endian,
159 data: R,
160 index: SectionIndex,
161 ) -> read::Result<SymbolTable<'data, Elf, R>> {
162 let section = self.section(index)?;
163 match section.sh_type(endian) {
164 elf::SHT_DYNSYM | elf::SHT_SYMTAB => {}
165 _ => return Err(Error("Invalid ELF symbol table section type")),
166 }
167 SymbolTable::parse(endian, data, self, index, section)
168 }
169
170 #[inline]
172 pub fn relocation_sections(
173 &self,
174 endian: Elf::Endian,
175 symbol_section: SectionIndex,
176 ) -> read::Result<RelocationSections> {
177 RelocationSections::parse(endian, self, symbol_section)
178 }
179
180 pub fn dynamic(
187 &self,
188 endian: Elf::Endian,
189 data: R,
190 ) -> read::Result<Option<(&'data [Elf::Dyn], SectionIndex)>> {
191 for section in self.sections {
192 if let Some(dynamic) = section.dynamic(endian, data)? {
193 return Ok(Some(dynamic));
194 }
195 }
196 Ok(None)
197 }
198
199 pub fn hash_header(
204 &self,
205 endian: Elf::Endian,
206 data: R,
207 ) -> read::Result<Option<&'data elf::HashHeader<Elf::Endian>>> {
208 for section in self.sections {
209 if let Some(hash) = section.hash_header(endian, data)? {
210 return Ok(Some(hash));
211 }
212 }
213 Ok(None)
214 }
215
216 pub fn hash(
223 &self,
224 endian: Elf::Endian,
225 data: R,
226 ) -> read::Result<Option<(HashTable<'data, Elf>, SectionIndex)>> {
227 for section in self.sections {
228 if let Some(hash) = section.hash(endian, data)? {
229 return Ok(Some(hash));
230 }
231 }
232 Ok(None)
233 }
234
235 pub fn gnu_hash_header(
240 &self,
241 endian: Elf::Endian,
242 data: R,
243 ) -> read::Result<Option<&'data elf::GnuHashHeader<Elf::Endian>>> {
244 for section in self.sections {
245 if let Some(hash) = section.gnu_hash_header(endian, data)? {
246 return Ok(Some(hash));
247 }
248 }
249 Ok(None)
250 }
251
252 pub fn gnu_hash(
259 &self,
260 endian: Elf::Endian,
261 data: R,
262 ) -> read::Result<Option<(GnuHashTable<'data, Elf>, SectionIndex)>> {
263 for section in self.sections {
264 if let Some(hash) = section.gnu_hash(endian, data)? {
265 return Ok(Some(hash));
266 }
267 }
268 Ok(None)
269 }
270
271 pub fn gnu_versym(
278 &self,
279 endian: Elf::Endian,
280 data: R,
281 ) -> read::Result<Option<(&'data [elf::Versym<Elf::Endian>], SectionIndex)>> {
282 for section in self.sections {
283 if let Some(syms) = section.gnu_versym(endian, data)? {
284 return Ok(Some(syms));
285 }
286 }
287 Ok(None)
288 }
289
290 pub fn gnu_verdef(
297 &self,
298 endian: Elf::Endian,
299 data: R,
300 ) -> read::Result<Option<(VerdefIterator<'data, Elf>, SectionIndex)>> {
301 for section in self.sections {
302 if let Some(defs) = section.gnu_verdef(endian, data)? {
303 return Ok(Some(defs));
304 }
305 }
306 Ok(None)
307 }
308
309 pub fn gnu_verneed(
316 &self,
317 endian: Elf::Endian,
318 data: R,
319 ) -> read::Result<Option<(VerneedIterator<'data, Elf>, SectionIndex)>> {
320 for section in self.sections {
321 if let Some(needs) = section.gnu_verneed(endian, data)? {
322 return Ok(Some(needs));
323 }
324 }
325 Ok(None)
326 }
327
328 pub fn versions(
333 &self,
334 endian: Elf::Endian,
335 data: R,
336 ) -> read::Result<Option<VersionTable<'data, Elf>>> {
337 let (versyms, link) = match self.gnu_versym(endian, data)? {
338 Some(val) => val,
339 None => return Ok(None),
340 };
341 let strings = self.symbol_table_by_index(endian, data, link)?.strings();
342 let verdefs = self.gnu_verdef(endian, data)?.map(|x| x.0);
344 let verneeds = self.gnu_verneed(endian, data)?.map(|x| x.0);
345 VersionTable::parse(endian, versyms, verdefs, verneeds, strings).map(Some)
346 }
347}
348
349pub type ElfSectionIterator32<'data, 'file, Endian = Endianness, R = &'data [u8]> =
351 ElfSectionIterator<'data, 'file, elf::FileHeader32<Endian>, R>;
352pub type ElfSectionIterator64<'data, 'file, Endian = Endianness, R = &'data [u8]> =
354 ElfSectionIterator<'data, 'file, elf::FileHeader64<Endian>, R>;
355
356#[derive(Debug)]
358pub struct ElfSectionIterator<'data, 'file, Elf, R = &'data [u8]>
359where
360 Elf: FileHeader,
361 R: ReadRef<'data>,
362{
363 file: &'file ElfFile<'data, Elf, R>,
364 iter: iter::Enumerate<slice::Iter<'data, Elf::SectionHeader>>,
365}
366
367impl<'data, 'file, Elf, R> ElfSectionIterator<'data, 'file, Elf, R>
368where
369 Elf: FileHeader,
370 R: ReadRef<'data>,
371{
372 pub(super) fn new(file: &'file ElfFile<'data, Elf, R>) -> Self {
373 let mut iter = file.sections.iter().enumerate();
374 iter.next(); ElfSectionIterator { file, iter }
376 }
377}
378
379impl<'data, 'file, Elf, R> Iterator for ElfSectionIterator<'data, 'file, Elf, R>
380where
381 Elf: FileHeader,
382 R: ReadRef<'data>,
383{
384 type Item = ElfSection<'data, 'file, Elf, R>;
385
386 fn next(&mut self) -> Option<Self::Item> {
387 self.iter.next().map(|(index, section)| ElfSection {
388 index: SectionIndex(index),
389 file: self.file,
390 section,
391 })
392 }
393}
394
395pub type ElfSection32<'data, 'file, Endian = Endianness, R = &'data [u8]> =
397 ElfSection<'data, 'file, elf::FileHeader32<Endian>, R>;
398pub type ElfSection64<'data, 'file, Endian = Endianness, R = &'data [u8]> =
400 ElfSection<'data, 'file, elf::FileHeader64<Endian>, R>;
401
402#[derive(Debug)]
406pub struct ElfSection<'data, 'file, Elf, R = &'data [u8]>
407where
408 Elf: FileHeader,
409 R: ReadRef<'data>,
410{
411 pub(super) file: &'file ElfFile<'data, Elf, R>,
412 pub(super) index: SectionIndex,
413 pub(super) section: &'data Elf::SectionHeader,
414}
415
416impl<'data, 'file, Elf: FileHeader, R: ReadRef<'data>> ElfSection<'data, 'file, Elf, R> {
417 pub fn elf_file(&self) -> &'file ElfFile<'data, Elf, R> {
419 self.file
420 }
421
422 pub fn elf_section_header(&self) -> &'data Elf::SectionHeader {
424 self.section
425 }
426
427 pub fn elf_relocation_section_index(&self) -> read::Result<Option<SectionIndex>> {
432 let Some(relocation_index) = self.file.relocations.get(self.index) else {
433 return Ok(None);
434 };
435 if self.file.relocations.get(relocation_index).is_some() {
436 return Err(Error(
437 "Unsupported ELF section with multiple relocation sections",
438 ));
439 }
440 Ok(Some(relocation_index))
441 }
442
443 pub fn elf_relocation_section(&self) -> read::Result<Option<&'data Elf::SectionHeader>> {
448 let Some(relocation_index) = self.elf_relocation_section_index()? else {
449 return Ok(None);
450 };
451 self.file.sections.section(relocation_index).map(Some)
452 }
453
454 pub fn elf_linked_rel(&self) -> read::Result<&'data [Elf::Rel]> {
459 let Some(relocation_section) = self.elf_relocation_section()? else {
460 return Ok(&[]);
461 };
462 let Some((rel, _)) = relocation_section.rel(self.file.endian, self.file.data)? else {
464 return Ok(&[]);
465 };
466 Ok(rel)
467 }
468
469 pub fn elf_linked_rela(&self) -> read::Result<&'data [Elf::Rela]> {
474 let Some(relocation_section) = self.elf_relocation_section()? else {
475 return Ok(&[]);
476 };
477 let Some((rela, _)) = relocation_section.rela(self.file.endian, self.file.data)? else {
479 return Ok(&[]);
480 };
481 Ok(rela)
482 }
483
484 fn bytes(&self) -> read::Result<&'data [u8]> {
485 self.section
486 .data(self.file.endian, self.file.data)
487 .read_error("Invalid ELF section size or offset")
488 }
489
490 fn maybe_compressed(&self) -> read::Result<Option<CompressedFileRange>> {
491 let endian = self.file.endian;
492 if let Some((header, offset, compressed_size)) =
493 self.section.compression(endian, self.file.data)?
494 {
495 let format = match header.ch_type(endian) {
496 elf::ELFCOMPRESS_ZLIB => CompressionFormat::Zlib,
497 elf::ELFCOMPRESS_ZSTD => CompressionFormat::Zstandard,
498 _ => return Err(Error("Unsupported ELF compression type")),
499 };
500 let uncompressed_size = header.ch_size(endian).into();
501 Ok(Some(CompressedFileRange {
502 format,
503 offset,
504 compressed_size,
505 uncompressed_size,
506 }))
507 } else {
508 Ok(None)
509 }
510 }
511
512 fn maybe_compressed_gnu(&self) -> read::Result<Option<CompressedFileRange>> {
514 if !self
515 .name()
516 .map_or(false, |name| name.starts_with(".zdebug_"))
517 {
518 return Ok(None);
519 }
520 let (section_offset, section_size) = self
521 .file_range()
522 .read_error("Invalid ELF GNU compressed section type")?;
523 gnu_compression::compressed_file_range(self.file.data, section_offset, section_size)
524 .map(Some)
525 }
526}
527
528impl<'data, 'file, Elf, R> read::private::Sealed for ElfSection<'data, 'file, Elf, R>
529where
530 Elf: FileHeader,
531 R: ReadRef<'data>,
532{
533}
534
535impl<'data, 'file, Elf, R> ObjectSection<'data> for ElfSection<'data, 'file, Elf, R>
536where
537 Elf: FileHeader,
538 R: ReadRef<'data>,
539{
540 type RelocationIterator = ElfSectionRelocationIterator<'data, 'file, Elf, R>;
541
542 #[inline]
543 fn index(&self) -> SectionIndex {
544 self.index
545 }
546
547 #[inline]
548 fn address(&self) -> u64 {
549 self.section.sh_addr(self.file.endian).into()
550 }
551
552 #[inline]
553 fn size(&self) -> u64 {
554 self.section.sh_size(self.file.endian).into()
555 }
556
557 #[inline]
558 fn align(&self) -> u64 {
559 self.section.sh_addralign(self.file.endian).into()
560 }
561
562 #[inline]
563 fn file_range(&self) -> Option<(u64, u64)> {
564 self.section.file_range(self.file.endian)
565 }
566
567 #[inline]
568 fn data(&self) -> read::Result<&'data [u8]> {
569 self.bytes()
570 }
571
572 fn data_range(&self, address: u64, size: u64) -> read::Result<Option<&'data [u8]>> {
573 Ok(read::util::data_range(
574 self.bytes()?,
575 self.address(),
576 address,
577 size,
578 ))
579 }
580
581 fn compressed_file_range(&self) -> read::Result<CompressedFileRange> {
582 Ok(if let Some(data) = self.maybe_compressed()? {
583 data
584 } else if let Some(data) = self.maybe_compressed_gnu()? {
585 data
586 } else {
587 CompressedFileRange::none(self.file_range())
588 })
589 }
590
591 fn compressed_data(&self) -> read::Result<CompressedData<'data>> {
592 self.compressed_file_range()?.data(self.file.data)
593 }
594
595 fn name_bytes(&self) -> read::Result<&'data [u8]> {
596 self.file
597 .sections
598 .section_name(self.file.endian, self.section)
599 }
600
601 fn name(&self) -> read::Result<&'data str> {
602 let name = self.name_bytes()?;
603 str::from_utf8(name)
604 .ok()
605 .read_error("Non UTF-8 ELF section name")
606 }
607
608 #[inline]
609 fn segment_name_bytes(&self) -> read::Result<Option<&[u8]>> {
610 Ok(None)
611 }
612
613 #[inline]
614 fn segment_name(&self) -> read::Result<Option<&str>> {
615 Ok(None)
616 }
617
618 fn kind(&self) -> SectionKind {
619 let flags = self.section.sh_flags(self.file.endian).into();
620 let sh_type = self.section.sh_type(self.file.endian);
621 match sh_type {
622 elf::SHT_PROGBITS => {
623 if flags & u64::from(elf::SHF_ALLOC) != 0 {
624 if flags & u64::from(elf::SHF_EXECINSTR) != 0 {
625 SectionKind::Text
626 } else if flags & u64::from(elf::SHF_TLS) != 0 {
627 SectionKind::Tls
628 } else if flags & u64::from(elf::SHF_WRITE) != 0 {
629 SectionKind::Data
630 } else if flags & u64::from(elf::SHF_STRINGS) != 0 {
631 SectionKind::ReadOnlyString
632 } else {
633 SectionKind::ReadOnlyData
634 }
635 } else if flags & u64::from(elf::SHF_STRINGS) != 0 {
636 SectionKind::OtherString
637 } else {
638 SectionKind::Other
639 }
640 }
641 elf::SHT_NOBITS => {
642 if flags & u64::from(elf::SHF_TLS) != 0 {
643 SectionKind::UninitializedTls
644 } else {
645 SectionKind::UninitializedData
646 }
647 }
648 elf::SHT_NOTE => SectionKind::Note,
649 elf::SHT_NULL
650 | elf::SHT_SYMTAB
651 | elf::SHT_STRTAB
652 | elf::SHT_RELA
653 | elf::SHT_HASH
654 | elf::SHT_DYNAMIC
655 | elf::SHT_REL
656 | elf::SHT_DYNSYM
657 | elf::SHT_GROUP
658 | elf::SHT_SYMTAB_SHNDX
659 | elf::SHT_RELR => SectionKind::Metadata,
660 _ => SectionKind::Elf(sh_type),
661 }
662 }
663
664 fn relocations(&self) -> ElfSectionRelocationIterator<'data, 'file, Elf, R> {
665 ElfSectionRelocationIterator {
666 section_index: self.index,
667 file: self.file,
668 relocations: None,
669 }
670 }
671
672 fn relocation_map(&self) -> read::Result<RelocationMap> {
673 RelocationMap::new(self.file, self)
674 }
675
676 fn flags(&self) -> SectionFlags {
677 SectionFlags::Elf {
678 sh_flags: self.section.sh_flags(self.file.endian).into(),
679 }
680 }
681}
682
683#[allow(missing_docs)]
685pub trait SectionHeader: Debug + Pod {
686 type Elf: FileHeader<SectionHeader = Self, Endian = Self::Endian, Word = Self::Word>;
687 type Word: Into<u64>;
688 type Endian: endian::Endian;
689
690 fn sh_name(&self, endian: Self::Endian) -> u32;
691 fn sh_type(&self, endian: Self::Endian) -> u32;
692 fn sh_flags(&self, endian: Self::Endian) -> Self::Word;
693 fn sh_addr(&self, endian: Self::Endian) -> Self::Word;
694 fn sh_offset(&self, endian: Self::Endian) -> Self::Word;
695 fn sh_size(&self, endian: Self::Endian) -> Self::Word;
696 fn sh_link(&self, endian: Self::Endian) -> u32;
697 fn sh_info(&self, endian: Self::Endian) -> u32;
698 fn sh_addralign(&self, endian: Self::Endian) -> Self::Word;
699 fn sh_entsize(&self, endian: Self::Endian) -> Self::Word;
700
701 fn name<'data, R: ReadRef<'data>>(
703 &self,
704 endian: Self::Endian,
705 strings: StringTable<'data, R>,
706 ) -> read::Result<&'data [u8]> {
707 strings
708 .get(self.sh_name(endian))
709 .read_error("Invalid ELF section name offset")
710 }
711
712 fn link(&self, endian: Self::Endian) -> SectionIndex {
716 SectionIndex(self.sh_link(endian) as usize)
717 }
718
719 fn has_info_link(&self, endian: Self::Endian) -> bool {
721 self.sh_flags(endian).into() & u64::from(elf::SHF_INFO_LINK) != 0
722 }
723
724 fn info_link(&self, endian: Self::Endian) -> SectionIndex {
729 SectionIndex(self.sh_info(endian) as usize)
730 }
731
732 fn file_range(&self, endian: Self::Endian) -> Option<(u64, u64)> {
736 if self.sh_type(endian) == elf::SHT_NOBITS {
737 None
738 } else {
739 Some((self.sh_offset(endian).into(), self.sh_size(endian).into()))
740 }
741 }
742
743 fn data<'data, R: ReadRef<'data>>(
748 &self,
749 endian: Self::Endian,
750 data: R,
751 ) -> read::Result<&'data [u8]> {
752 if let Some((offset, size)) = self.file_range(endian) {
753 data.read_bytes_at(offset, size)
754 .read_error("Invalid ELF section size or offset")
755 } else {
756 Ok(&[])
757 }
758 }
759
760 fn data_as_array<'data, T: Pod, R: ReadRef<'data>>(
766 &self,
767 endian: Self::Endian,
768 data: R,
769 ) -> read::Result<&'data [T]> {
770 pod::slice_from_all_bytes(self.data(endian, data)?)
771 .read_error("Invalid ELF section size or offset")
772 }
773
774 fn strings<'data, R: ReadRef<'data>>(
779 &self,
780 endian: Self::Endian,
781 data: R,
782 ) -> read::Result<Option<StringTable<'data, R>>> {
783 if self.sh_type(endian) != elf::SHT_STRTAB {
784 return Ok(None);
785 }
786 let str_offset = self.sh_offset(endian).into();
787 let str_size = self.sh_size(endian).into();
788 let str_end = str_offset
789 .checked_add(str_size)
790 .read_error("Invalid ELF string section offset or size")?;
791 Ok(Some(StringTable::new(data, str_offset, str_end)))
792 }
793
794 fn symbols<'data, R: ReadRef<'data>>(
804 &self,
805 endian: Self::Endian,
806 data: R,
807 sections: &SectionTable<'data, Self::Elf, R>,
808 section_index: SectionIndex,
809 ) -> read::Result<Option<SymbolTable<'data, Self::Elf, R>>> {
810 let sh_type = self.sh_type(endian);
811 if sh_type != elf::SHT_SYMTAB && sh_type != elf::SHT_DYNSYM {
812 return Ok(None);
813 }
814 SymbolTable::parse(endian, data, sections, section_index, self).map(Some)
815 }
816
817 fn rel<'data, R: ReadRef<'data>>(
824 &self,
825 endian: Self::Endian,
826 data: R,
827 ) -> read::Result<Option<(&'data [<Self::Elf as FileHeader>::Rel], SectionIndex)>> {
828 if self.sh_type(endian) != elf::SHT_REL {
829 return Ok(None);
830 }
831 let rel = self
832 .data_as_array(endian, data)
833 .read_error("Invalid ELF relocation section offset or size")?;
834 Ok(Some((rel, self.link(endian))))
835 }
836
837 fn rela<'data, R: ReadRef<'data>>(
844 &self,
845 endian: Self::Endian,
846 data: R,
847 ) -> read::Result<Option<(&'data [<Self::Elf as FileHeader>::Rela], SectionIndex)>> {
848 if self.sh_type(endian) != elf::SHT_RELA {
849 return Ok(None);
850 }
851 let rela = self
852 .data_as_array(endian, data)
853 .read_error("Invalid ELF relocation section offset or size")?;
854 Ok(Some((rela, self.link(endian))))
855 }
856
857 fn relr<'data, R: ReadRef<'data>>(
862 &self,
863 endian: Self::Endian,
864 data: R,
865 ) -> read::Result<Option<RelrIterator<'data, Self::Elf>>> {
866 if self.sh_type(endian) != elf::SHT_RELR {
867 return Ok(None);
868 }
869 let data = self
870 .data_as_array(endian, data)
871 .read_error("Invalid ELF relocation section offset or size")?;
872 let relrs = RelrIterator::new(endian, data);
873 Ok(Some(relrs))
874 }
875
876 fn dynamic<'data, R: ReadRef<'data>>(
883 &self,
884 endian: Self::Endian,
885 data: R,
886 ) -> read::Result<Option<(&'data [<Self::Elf as FileHeader>::Dyn], SectionIndex)>> {
887 if self.sh_type(endian) != elf::SHT_DYNAMIC {
888 return Ok(None);
889 }
890 let dynamic = self
891 .data_as_array(endian, data)
892 .read_error("Invalid ELF dynamic section offset or size")?;
893 Ok(Some((dynamic, self.link(endian))))
894 }
895
896 fn notes<'data, R: ReadRef<'data>>(
901 &self,
902 endian: Self::Endian,
903 data: R,
904 ) -> read::Result<Option<NoteIterator<'data, Self::Elf>>> {
905 if self.sh_type(endian) != elf::SHT_NOTE {
906 return Ok(None);
907 }
908 let data = self
909 .data(endian, data)
910 .read_error("Invalid ELF note section offset or size")?;
911 let notes = NoteIterator::new(endian, self.sh_addralign(endian), data)?;
912 Ok(Some(notes))
913 }
914
915 fn group<'data, R: ReadRef<'data>>(
923 &self,
924 endian: Self::Endian,
925 data: R,
926 ) -> read::Result<Option<(u32, &'data [U32Bytes<Self::Endian>])>> {
927 if self.sh_type(endian) != elf::SHT_GROUP {
928 return Ok(None);
929 }
930 let msg = "Invalid ELF group section offset or size";
931 let data = self.data(endian, data).read_error(msg)?;
932 let (flag, data) = pod::from_bytes::<U32Bytes<_>>(data).read_error(msg)?;
933 let sections = pod::slice_from_all_bytes(data).read_error(msg)?;
934 Ok(Some((flag.get(endian), sections)))
935 }
936
937 fn hash_header<'data, R: ReadRef<'data>>(
942 &self,
943 endian: Self::Endian,
944 data: R,
945 ) -> read::Result<Option<&'data elf::HashHeader<Self::Endian>>> {
946 if self.sh_type(endian) != elf::SHT_HASH {
947 return Ok(None);
948 }
949 let data = self
950 .data(endian, data)
951 .read_error("Invalid ELF hash section offset or size")?;
952 let header = data
953 .read_at::<elf::HashHeader<Self::Endian>>(0)
954 .read_error("Invalid hash header")?;
955 Ok(Some(header))
956 }
957
958 fn hash<'data, R: ReadRef<'data>>(
965 &self,
966 endian: Self::Endian,
967 data: R,
968 ) -> read::Result<Option<(HashTable<'data, Self::Elf>, SectionIndex)>> {
969 if self.sh_type(endian) != elf::SHT_HASH {
970 return Ok(None);
971 }
972 let data = self
973 .data(endian, data)
974 .read_error("Invalid ELF hash section offset or size")?;
975 let hash = HashTable::parse(endian, data)?;
976 Ok(Some((hash, self.link(endian))))
977 }
978
979 fn gnu_hash_header<'data, R: ReadRef<'data>>(
984 &self,
985 endian: Self::Endian,
986 data: R,
987 ) -> read::Result<Option<&'data elf::GnuHashHeader<Self::Endian>>> {
988 if self.sh_type(endian) != elf::SHT_GNU_HASH {
989 return Ok(None);
990 }
991 let data = self
992 .data(endian, data)
993 .read_error("Invalid ELF GNU hash section offset or size")?;
994 let header = data
995 .read_at::<elf::GnuHashHeader<Self::Endian>>(0)
996 .read_error("Invalid GNU hash header")?;
997 Ok(Some(header))
998 }
999
1000 fn gnu_hash<'data, R: ReadRef<'data>>(
1007 &self,
1008 endian: Self::Endian,
1009 data: R,
1010 ) -> read::Result<Option<(GnuHashTable<'data, Self::Elf>, SectionIndex)>> {
1011 if self.sh_type(endian) != elf::SHT_GNU_HASH {
1012 return Ok(None);
1013 }
1014 let data = self
1015 .data(endian, data)
1016 .read_error("Invalid ELF GNU hash section offset or size")?;
1017 let hash = GnuHashTable::parse(endian, data)?;
1018 Ok(Some((hash, self.link(endian))))
1019 }
1020
1021 fn gnu_versym<'data, R: ReadRef<'data>>(
1028 &self,
1029 endian: Self::Endian,
1030 data: R,
1031 ) -> read::Result<Option<(&'data [elf::Versym<Self::Endian>], SectionIndex)>> {
1032 if self.sh_type(endian) != elf::SHT_GNU_VERSYM {
1033 return Ok(None);
1034 }
1035 let versym = self
1036 .data_as_array(endian, data)
1037 .read_error("Invalid ELF GNU versym section offset or size")?;
1038 Ok(Some((versym, self.link(endian))))
1039 }
1040
1041 fn gnu_verdef<'data, R: ReadRef<'data>>(
1048 &self,
1049 endian: Self::Endian,
1050 data: R,
1051 ) -> read::Result<Option<(VerdefIterator<'data, Self::Elf>, SectionIndex)>> {
1052 if self.sh_type(endian) != elf::SHT_GNU_VERDEF {
1053 return Ok(None);
1054 }
1055 let verdef = self
1056 .data(endian, data)
1057 .read_error("Invalid ELF GNU verdef section offset or size")?;
1058 Ok(Some((
1059 VerdefIterator::new(endian, verdef),
1060 self.link(endian),
1061 )))
1062 }
1063
1064 fn gnu_verneed<'data, R: ReadRef<'data>>(
1071 &self,
1072 endian: Self::Endian,
1073 data: R,
1074 ) -> read::Result<Option<(VerneedIterator<'data, Self::Elf>, SectionIndex)>> {
1075 if self.sh_type(endian) != elf::SHT_GNU_VERNEED {
1076 return Ok(None);
1077 }
1078 let verneed = self
1079 .data(endian, data)
1080 .read_error("Invalid ELF GNU verneed section offset or size")?;
1081 Ok(Some((
1082 VerneedIterator::new(endian, verneed),
1083 self.link(endian),
1084 )))
1085 }
1086
1087 fn gnu_attributes<'data, R: ReadRef<'data>>(
1092 &self,
1093 endian: Self::Endian,
1094 data: R,
1095 ) -> read::Result<Option<AttributesSection<'data, Self::Elf>>> {
1096 if self.sh_type(endian) != elf::SHT_GNU_ATTRIBUTES {
1097 return Ok(None);
1098 }
1099 self.attributes(endian, data).map(Some)
1100 }
1101
1102 fn attributes<'data, R: ReadRef<'data>>(
1109 &self,
1110 endian: Self::Endian,
1111 data: R,
1112 ) -> read::Result<AttributesSection<'data, Self::Elf>> {
1113 let data = self.data(endian, data)?;
1114 AttributesSection::new(endian, data)
1115 }
1116
1117 fn compression<'data, R: ReadRef<'data>>(
1125 &self,
1126 endian: Self::Endian,
1127 data: R,
1128 ) -> read::Result<
1129 Option<(
1130 &'data <Self::Elf as FileHeader>::CompressionHeader,
1131 u64,
1132 u64,
1133 )>,
1134 > {
1135 if (self.sh_flags(endian).into() & u64::from(elf::SHF_COMPRESSED)) == 0 {
1136 return Ok(None);
1137 }
1138 let (section_offset, section_size) = self
1139 .file_range(endian)
1140 .read_error("Invalid ELF compressed section type")?;
1141 let mut offset = section_offset;
1142 let header = data
1143 .read::<<Self::Elf as FileHeader>::CompressionHeader>(&mut offset)
1144 .read_error("Invalid ELF compressed section offset")?;
1145 let compressed_size = section_size
1146 .checked_sub(offset - section_offset)
1147 .read_error("Invalid ELF compressed section size")?;
1148 Ok(Some((header, offset, compressed_size)))
1149 }
1150}
1151
1152impl<Endian: endian::Endian> SectionHeader for elf::SectionHeader32<Endian> {
1153 type Elf = elf::FileHeader32<Endian>;
1154 type Word = u32;
1155 type Endian = Endian;
1156
1157 #[inline]
1158 fn sh_name(&self, endian: Self::Endian) -> u32 {
1159 self.sh_name.get(endian)
1160 }
1161
1162 #[inline]
1163 fn sh_type(&self, endian: Self::Endian) -> u32 {
1164 self.sh_type.get(endian)
1165 }
1166
1167 #[inline]
1168 fn sh_flags(&self, endian: Self::Endian) -> Self::Word {
1169 self.sh_flags.get(endian)
1170 }
1171
1172 #[inline]
1173 fn sh_addr(&self, endian: Self::Endian) -> Self::Word {
1174 self.sh_addr.get(endian)
1175 }
1176
1177 #[inline]
1178 fn sh_offset(&self, endian: Self::Endian) -> Self::Word {
1179 self.sh_offset.get(endian)
1180 }
1181
1182 #[inline]
1183 fn sh_size(&self, endian: Self::Endian) -> Self::Word {
1184 self.sh_size.get(endian)
1185 }
1186
1187 #[inline]
1188 fn sh_link(&self, endian: Self::Endian) -> u32 {
1189 self.sh_link.get(endian)
1190 }
1191
1192 #[inline]
1193 fn sh_info(&self, endian: Self::Endian) -> u32 {
1194 self.sh_info.get(endian)
1195 }
1196
1197 #[inline]
1198 fn sh_addralign(&self, endian: Self::Endian) -> Self::Word {
1199 self.sh_addralign.get(endian)
1200 }
1201
1202 #[inline]
1203 fn sh_entsize(&self, endian: Self::Endian) -> Self::Word {
1204 self.sh_entsize.get(endian)
1205 }
1206}
1207
1208impl<Endian: endian::Endian> SectionHeader for elf::SectionHeader64<Endian> {
1209 type Word = u64;
1210 type Endian = Endian;
1211 type Elf = elf::FileHeader64<Endian>;
1212
1213 #[inline]
1214 fn sh_name(&self, endian: Self::Endian) -> u32 {
1215 self.sh_name.get(endian)
1216 }
1217
1218 #[inline]
1219 fn sh_type(&self, endian: Self::Endian) -> u32 {
1220 self.sh_type.get(endian)
1221 }
1222
1223 #[inline]
1224 fn sh_flags(&self, endian: Self::Endian) -> Self::Word {
1225 self.sh_flags.get(endian)
1226 }
1227
1228 #[inline]
1229 fn sh_addr(&self, endian: Self::Endian) -> Self::Word {
1230 self.sh_addr.get(endian)
1231 }
1232
1233 #[inline]
1234 fn sh_offset(&self, endian: Self::Endian) -> Self::Word {
1235 self.sh_offset.get(endian)
1236 }
1237
1238 #[inline]
1239 fn sh_size(&self, endian: Self::Endian) -> Self::Word {
1240 self.sh_size.get(endian)
1241 }
1242
1243 #[inline]
1244 fn sh_link(&self, endian: Self::Endian) -> u32 {
1245 self.sh_link.get(endian)
1246 }
1247
1248 #[inline]
1249 fn sh_info(&self, endian: Self::Endian) -> u32 {
1250 self.sh_info.get(endian)
1251 }
1252
1253 #[inline]
1254 fn sh_addralign(&self, endian: Self::Endian) -> Self::Word {
1255 self.sh_addralign.get(endian)
1256 }
1257
1258 #[inline]
1259 fn sh_entsize(&self, endian: Self::Endian) -> Self::Word {
1260 self.sh_entsize.get(endian)
1261 }
1262}