1use std::io::{self, ErrorKind, Read, Seek, SeekFrom};
5
6pub(super) const COVMAP_SECTION: &[u8] = b"__llvm_covmap";
8
9pub(super) const ELF_IDENT: &[u8] = b"\x7fELF\x02\x01";
11pub(super) const ELF_HEADER_LEN: usize = 64;
12pub(super) const E_SHOFF: usize = 0x28;
13pub(super) const E_SHENTSIZE: usize = 0x3a;
14pub(super) const E_SHNUM: usize = 0x3c;
15pub(super) const E_SHSTRNDX: usize = 0x3e;
16pub(super) const SH_NAME: usize = 0x00;
17pub(super) const SH_OFFSET: usize = 0x18;
18pub(super) const SH_SIZE: usize = 0x20;
19pub(super) const SH_LINK: usize = 0x28;
20pub(super) const SHN_XINDEX: usize = 0xffff;
22pub(super) const MAX_TABLE_LEN: u64 = 16 << 20;
24
25pub(super) fn elf_has_section(file: &mut (impl Read + Seek), name: &[u8]) -> io::Result<bool> {
29 let mut header = [0; ELF_HEADER_LEN];
30 if !fill(file, &mut header)? {
31 return Ok(false);
32 }
33 let Some(sections) = SectionTable::parse(&header) else {
34 return Ok(false);
35 };
36 let Some(sections) = resolve_extended(file, sections)? else {
37 return Ok(false);
38 };
39 let Some(table) = read_region(file, sections.offset, sections.len())? else {
40 return Ok(false);
41 };
42 let Some(names_entry) = sections.entry(&table, sections.names_index) else {
43 return Ok(false);
44 };
45 let (Some(names_offset), Some(names_len)) = (
46 le_bytes(names_entry, SH_OFFSET).map(u64::from_le_bytes),
47 le_bytes(names_entry, SH_SIZE).map(u64::from_le_bytes),
48 ) else {
49 return Ok(false);
50 };
51 let Some(names) = read_region(file, names_offset, usize::try_from(names_len).ok())? else {
52 return Ok(false);
53 };
54 Ok((0..sections.count)
55 .filter_map(|index| sections.entry(&table, index))
56 .filter_map(|entry| le_bytes(entry, SH_NAME).map(u32::from_le_bytes))
57 .any(|name_offset| section_name(&names, name_offset) == Some(name)))
58}
59
60fn resolve_extended(
63 file: &mut (impl Read + Seek),
64 sections: SectionTable,
65) -> io::Result<Option<SectionTable>> {
66 if sections.count != 0 && sections.names_index != SHN_XINDEX {
67 return Ok(Some(sections));
68 }
69 if sections.offset == 0 {
70 return Ok(None);
71 }
72 let Some(first) = read_region(file, sections.offset, Some(sections.entry_len))? else {
73 return Ok(None);
74 };
75 let count = match sections.count {
76 0 => le_bytes(&first, SH_SIZE)
77 .map(u64::from_le_bytes)
78 .and_then(|size| usize::try_from(size).ok()),
79 count => Some(count),
80 };
81 let names_index = match sections.names_index {
82 SHN_XINDEX => le_bytes(&first, SH_LINK)
83 .map(u32::from_le_bytes)
84 .and_then(|link| usize::try_from(link).ok()),
85 index => Some(index),
86 };
87 Ok(count
88 .zip(names_index)
89 .map(|(count, names_index)| SectionTable {
90 count,
91 names_index,
92 ..sections
93 }))
94}
95
96struct SectionTable {
98 offset: u64,
99 entry_len: usize,
100 count: usize,
101 names_index: usize,
102}
103
104impl SectionTable {
105 fn parse(header: &[u8]) -> Option<Self> {
107 if header.get(..ELF_IDENT.len())? != ELF_IDENT {
108 return None;
109 }
110 Some(Self {
111 offset: le_bytes(header, E_SHOFF).map(u64::from_le_bytes)?,
112 entry_len: le_bytes(header, E_SHENTSIZE)
113 .map(u16::from_le_bytes)?
114 .into(),
115 count: le_bytes(header, E_SHNUM).map(u16::from_le_bytes)?.into(),
116 names_index: le_bytes(header, E_SHSTRNDX).map(u16::from_le_bytes)?.into(),
117 })
118 }
119
120 fn len(&self) -> Option<usize> {
121 self.entry_len.checked_mul(self.count)
122 }
123
124 fn entry<'a>(&self, table: &'a [u8], index: usize) -> Option<&'a [u8]> {
125 let start = self.entry_len.checked_mul(index)?;
126 table.get(start..start.checked_add(self.entry_len)?)
127 }
128}
129
130fn read_region(
133 file: &mut (impl Read + Seek),
134 offset: u64,
135 len: Option<usize>,
136) -> io::Result<Option<Vec<u8>>> {
137 let Some(len) = len.filter(|len| u64::try_from(*len).is_ok_and(|len| len <= MAX_TABLE_LEN))
138 else {
139 return Ok(None);
140 };
141 file.seek(SeekFrom::Start(offset))?;
142 let mut region = vec![0; len];
143 Ok(fill(file, &mut region)?.then_some(region))
144}
145
146fn fill(file: &mut impl Read, buf: &mut [u8]) -> io::Result<bool> {
148 match file.read_exact(buf) {
149 Ok(()) => Ok(true),
150 Err(err) if err.kind() == ErrorKind::UnexpectedEof => Ok(false),
151 Err(err) => Err(err),
152 }
153}
154
155fn section_name(names: &[u8], offset: u32) -> Option<&[u8]> {
156 names
157 .get(usize::try_from(offset).ok()?..)?
158 .split(|byte| *byte == 0)
159 .next()
160}
161
162fn le_bytes<const N: usize>(bytes: &[u8], at: usize) -> Option<[u8; N]> {
163 bytes.get(at..at.checked_add(N)?)?.try_into().ok()
164}
165
166#[cfg(test)]
167mod tests;