1use alloc::fmt;
2use alloc::vec::Vec;
3use core::fmt::Debug;
4use core::slice;
5
6use crate::elf;
7use crate::endian::{self, Endianness};
8use crate::pod::Pod;
9use crate::read::{
10 self, Error, ReadRef, Relocation, RelocationEncoding, RelocationFlags, RelocationKind,
11 RelocationTarget, SectionIndex, SymbolIndex,
12};
13
14use super::{ElfFile, FileHeader, SectionHeader, SectionTable};
15
16#[derive(Debug, Default)]
18pub struct RelocationSections {
19 relocations: Vec<usize>,
20}
21
22impl RelocationSections {
23 pub fn parse<'data, Elf: FileHeader, R: ReadRef<'data>>(
27 endian: Elf::Endian,
28 sections: &SectionTable<'data, Elf, R>,
29 symbol_section: SectionIndex,
30 ) -> read::Result<Self> {
31 let mut relocations = vec![0; sections.len()];
32 for (index, section) in sections.iter().enumerate().rev() {
33 let sh_type = section.sh_type(endian);
34 if sh_type == elf::SHT_REL || sh_type == elf::SHT_RELA {
35 let sh_link = section.link(endian);
38 if sh_link != symbol_section {
39 continue;
40 }
41
42 let sh_info = section.info_link(endian);
43 if sh_info == SectionIndex(0) {
44 continue;
46 }
47 if sh_info.0 >= relocations.len() {
48 return Err(Error("Invalid ELF sh_info for relocation section"));
49 }
50
51 let sh_info_type = sections.section(sh_info)?.sh_type(endian);
54 if sh_info_type == elf::SHT_REL || sh_info_type == elf::SHT_RELA {
55 return Err(Error("Unsupported ELF sh_info for relocation section"));
56 }
57
58 let next = relocations[sh_info.0];
60 relocations[sh_info.0] = index;
61 relocations[index] = next;
62 }
63 }
64 Ok(Self { relocations })
65 }
66
67 pub fn get(&self, index: SectionIndex) -> Option<SectionIndex> {
72 self.relocations
73 .get(index.0)
74 .cloned()
75 .filter(|x| *x != 0)
76 .map(SectionIndex)
77 }
78}
79
80pub(super) enum ElfRelaIterator<'data, Elf: FileHeader> {
81 Rel(slice::Iter<'data, Elf::Rel>),
82 Rela(slice::Iter<'data, Elf::Rela>),
83}
84
85impl<'data, Elf: FileHeader> ElfRelaIterator<'data, Elf> {
86 fn is_rel(&self) -> bool {
87 match self {
88 ElfRelaIterator::Rel(_) => true,
89 ElfRelaIterator::Rela(_) => false,
90 }
91 }
92}
93
94impl<'data, Elf: FileHeader> Iterator for ElfRelaIterator<'data, Elf> {
95 type Item = Elf::Rela;
96
97 fn next(&mut self) -> Option<Self::Item> {
98 match self {
99 ElfRelaIterator::Rel(ref mut i) => i.next().cloned().map(Self::Item::from),
100 ElfRelaIterator::Rela(ref mut i) => i.next().cloned(),
101 }
102 }
103}
104
105pub type ElfDynamicRelocationIterator32<'data, 'file, Endian = Endianness, R = &'data [u8]> =
107 ElfDynamicRelocationIterator<'data, 'file, elf::FileHeader32<Endian>, R>;
108pub type ElfDynamicRelocationIterator64<'data, 'file, Endian = Endianness, R = &'data [u8]> =
110 ElfDynamicRelocationIterator<'data, 'file, elf::FileHeader64<Endian>, R>;
111
112pub struct ElfDynamicRelocationIterator<'data, 'file, Elf, R = &'data [u8]>
114where
115 Elf: FileHeader,
116 R: ReadRef<'data>,
117{
118 pub(super) section_index: SectionIndex,
120 pub(super) file: &'file ElfFile<'data, Elf, R>,
121 pub(super) relocations: Option<ElfRelaIterator<'data, Elf>>,
122}
123
124impl<'data, 'file, Elf, R> Iterator for ElfDynamicRelocationIterator<'data, 'file, Elf, R>
125where
126 Elf: FileHeader,
127 R: ReadRef<'data>,
128{
129 type Item = (u64, Relocation);
130
131 fn next(&mut self) -> Option<Self::Item> {
132 let endian = self.file.endian;
133 loop {
134 if let Some(ref mut relocations) = self.relocations {
135 if let Some(reloc) = relocations.next() {
136 let relocation =
137 parse_relocation(self.file.header, endian, reloc, relocations.is_rel());
138 return Some((reloc.r_offset(endian).into(), relocation));
139 }
140 self.relocations = None;
141 }
142
143 let section = self.file.sections.section(self.section_index).ok()?;
144 self.section_index.0 += 1;
145
146 if section.link(endian) != self.file.dynamic_symbols.section() {
147 continue;
148 }
149
150 match section.sh_type(endian) {
151 elf::SHT_REL => {
152 if let Ok(relocations) = section.data_as_array(endian, self.file.data) {
153 self.relocations = Some(ElfRelaIterator::Rel(relocations.iter()));
154 }
155 }
156 elf::SHT_RELA => {
157 if let Ok(relocations) = section.data_as_array(endian, self.file.data) {
158 self.relocations = Some(ElfRelaIterator::Rela(relocations.iter()));
159 }
160 }
161 _ => {}
162 }
163 }
164 }
165}
166
167impl<'data, 'file, Elf, R> fmt::Debug for ElfDynamicRelocationIterator<'data, 'file, Elf, R>
168where
169 Elf: FileHeader,
170 R: ReadRef<'data>,
171{
172 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
173 f.debug_struct("ElfDynamicRelocationIterator").finish()
174 }
175}
176
177pub type ElfSectionRelocationIterator32<'data, 'file, Endian = Endianness, R = &'data [u8]> =
179 ElfSectionRelocationIterator<'data, 'file, elf::FileHeader32<Endian>, R>;
180pub type ElfSectionRelocationIterator64<'data, 'file, Endian = Endianness, R = &'data [u8]> =
182 ElfSectionRelocationIterator<'data, 'file, elf::FileHeader64<Endian>, R>;
183
184pub struct ElfSectionRelocationIterator<'data, 'file, Elf, R = &'data [u8]>
186where
187 Elf: FileHeader,
188 R: ReadRef<'data>,
189{
190 pub(super) section_index: SectionIndex,
192 pub(super) file: &'file ElfFile<'data, Elf, R>,
193 pub(super) relocations: Option<ElfRelaIterator<'data, Elf>>,
194}
195
196impl<'data, 'file, Elf, R> Iterator for ElfSectionRelocationIterator<'data, 'file, Elf, R>
197where
198 Elf: FileHeader,
199 R: ReadRef<'data>,
200{
201 type Item = (u64, Relocation);
202
203 fn next(&mut self) -> Option<Self::Item> {
204 let endian = self.file.endian;
205 loop {
206 if let Some(ref mut relocations) = self.relocations {
207 if let Some(reloc) = relocations.next() {
208 let relocation =
209 parse_relocation(self.file.header, endian, reloc, relocations.is_rel());
210 return Some((reloc.r_offset(endian).into(), relocation));
211 }
212 self.relocations = None;
213 }
214 self.section_index = self.file.relocations.get(self.section_index)?;
215 let section = self.file.sections.section(self.section_index).unwrap();
217 match section.sh_type(endian) {
218 elf::SHT_REL => {
219 if let Ok(relocations) = section.data_as_array(endian, self.file.data) {
220 self.relocations = Some(ElfRelaIterator::Rel(relocations.iter()));
221 }
222 }
223 elf::SHT_RELA => {
224 if let Ok(relocations) = section.data_as_array(endian, self.file.data) {
225 self.relocations = Some(ElfRelaIterator::Rela(relocations.iter()));
226 }
227 }
228 _ => {}
229 }
230 }
231 }
232}
233
234impl<'data, 'file, Elf, R> fmt::Debug for ElfSectionRelocationIterator<'data, 'file, Elf, R>
235where
236 Elf: FileHeader,
237 R: ReadRef<'data>,
238{
239 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
240 f.debug_struct("ElfSectionRelocationIterator").finish()
241 }
242}
243
244fn parse_relocation<Elf: FileHeader>(
245 header: &Elf,
246 endian: Elf::Endian,
247 reloc: Elf::Rela,
248 implicit_addend: bool,
249) -> Relocation {
250 use RelocationEncoding as E;
251 use RelocationKind as K;
252
253 let is_mips64el = header.is_mips64el(endian);
254 let r_type = reloc.r_type(endian, is_mips64el);
255 let flags = RelocationFlags::Elf { r_type };
256 let g = E::Generic;
257 let unknown = (K::Unknown, E::Generic, 0);
258 let (kind, encoding, size) = match header.e_machine(endian) {
259 elf::EM_AARCH64 => {
260 if header.is_type_64() {
261 match r_type {
262 elf::R_AARCH64_ABS64 => (K::Absolute, g, 64),
263 elf::R_AARCH64_ABS32 => (K::Absolute, g, 32),
264 elf::R_AARCH64_ABS16 => (K::Absolute, g, 16),
265 elf::R_AARCH64_PREL64 => (K::Relative, g, 64),
266 elf::R_AARCH64_PREL32 => (K::Relative, g, 32),
267 elf::R_AARCH64_PREL16 => (K::Relative, g, 16),
268 elf::R_AARCH64_CALL26 => (K::PltRelative, E::AArch64Call, 26),
269 _ => unknown,
270 }
271 } else {
272 match r_type {
273 elf::R_AARCH64_P32_ABS32 => (K::Absolute, g, 32),
274 _ => unknown,
275 }
276 }
277 }
278 elf::EM_ARM => match r_type {
279 elf::R_ARM_ABS32 => (K::Absolute, g, 32),
280 _ => unknown,
281 },
282 elf::EM_AVR => match r_type {
283 elf::R_AVR_32 => (K::Absolute, g, 32),
284 elf::R_AVR_16 => (K::Absolute, g, 16),
285 _ => unknown,
286 },
287 elf::EM_BPF => match r_type {
288 elf::R_BPF_64_64 => (K::Absolute, g, 64),
289 elf::R_BPF_64_32 => (K::Absolute, g, 32),
290 _ => unknown,
291 },
292 elf::EM_CSKY => match r_type {
293 elf::R_CKCORE_ADDR32 => (K::Absolute, g, 32),
294 elf::R_CKCORE_PCREL32 => (K::Relative, g, 32),
295 _ => unknown,
296 },
297 elf::EM_MCST_ELBRUS => match r_type {
298 elf::R_E2K_32_ABS => (K::Absolute, g, 32),
299 elf::R_E2K_64_ABS => (K::Absolute, g, 64),
300 elf::R_E2K_64_ABS_LIT => (K::Absolute, E::E2KLit, 64),
301 elf::R_E2K_DISP => (K::Relative, E::E2KDisp, 28),
302 elf::R_E2K_GOT => (K::Got, g, 32),
303 _ => unknown,
304 },
305 elf::EM_386 => match r_type {
306 elf::R_386_32 => (K::Absolute, g, 32),
307 elf::R_386_PC32 => (K::Relative, g, 32),
308 elf::R_386_GOT32 => (K::Got, g, 32),
309 elf::R_386_PLT32 => (K::PltRelative, g, 32),
310 elf::R_386_GOTOFF => (K::GotBaseOffset, g, 32),
311 elf::R_386_GOTPC => (K::GotBaseRelative, g, 32),
312 elf::R_386_16 => (K::Absolute, g, 16),
313 elf::R_386_PC16 => (K::Relative, g, 16),
314 elf::R_386_8 => (K::Absolute, g, 8),
315 elf::R_386_PC8 => (K::Relative, g, 8),
316 _ => unknown,
317 },
318 elf::EM_X86_64 => match r_type {
319 elf::R_X86_64_64 => (K::Absolute, g, 64),
320 elf::R_X86_64_PC32 => (K::Relative, g, 32),
321 elf::R_X86_64_GOT32 => (K::Got, g, 32),
322 elf::R_X86_64_PLT32 => (K::PltRelative, g, 32),
323 elf::R_X86_64_GOTPCREL => (K::GotRelative, g, 32),
324 elf::R_X86_64_32 => (K::Absolute, g, 32),
325 elf::R_X86_64_32S => (K::Absolute, E::X86Signed, 32),
326 elf::R_X86_64_16 => (K::Absolute, g, 16),
327 elf::R_X86_64_PC16 => (K::Relative, g, 16),
328 elf::R_X86_64_8 => (K::Absolute, g, 8),
329 elf::R_X86_64_PC8 => (K::Relative, g, 8),
330 _ => unknown,
331 },
332 elf::EM_HEXAGON => match r_type {
333 elf::R_HEX_32 => (K::Absolute, g, 32),
334 _ => unknown,
335 },
336 elf::EM_LOONGARCH => match r_type {
337 elf::R_LARCH_32 => (K::Absolute, g, 32),
338 elf::R_LARCH_64 => (K::Absolute, g, 64),
339 elf::R_LARCH_32_PCREL => (K::Relative, g, 32),
340 elf::R_LARCH_64_PCREL => (K::Relative, g, 64),
341 elf::R_LARCH_B16 => (K::Relative, E::LoongArchBranch, 16),
342 elf::R_LARCH_B21 => (K::Relative, E::LoongArchBranch, 21),
343 elf::R_LARCH_B26 => (K::Relative, E::LoongArchBranch, 26),
344 _ => unknown,
345 },
346 elf::EM_MIPS => match r_type {
347 elf::R_MIPS_16 => (K::Absolute, g, 16),
348 elf::R_MIPS_32 => (K::Absolute, g, 32),
349 elf::R_MIPS_64 => (K::Absolute, g, 64),
350 _ => unknown,
351 },
352 elf::EM_MSP430 => match r_type {
353 elf::R_MSP430_32 => (K::Absolute, g, 32),
354 elf::R_MSP430_16_BYTE => (K::Absolute, g, 16),
355 _ => unknown,
356 },
357 elf::EM_PPC => match r_type {
358 elf::R_PPC_ADDR32 => (K::Absolute, g, 32),
359 _ => unknown,
360 },
361 elf::EM_PPC64 => match r_type {
362 elf::R_PPC64_ADDR32 => (K::Absolute, g, 32),
363 elf::R_PPC64_ADDR64 => (K::Absolute, g, 64),
364 _ => unknown,
365 },
366 elf::EM_RISCV => match r_type {
367 elf::R_RISCV_32 => (K::Absolute, g, 32),
368 elf::R_RISCV_64 => (K::Absolute, g, 64),
369 _ => unknown,
370 },
371 elf::EM_S390 => match r_type {
372 elf::R_390_8 => (K::Absolute, g, 8),
373 elf::R_390_16 => (K::Absolute, g, 16),
374 elf::R_390_32 => (K::Absolute, g, 32),
375 elf::R_390_64 => (K::Absolute, g, 64),
376 elf::R_390_PC16 => (K::Relative, g, 16),
377 elf::R_390_PC32 => (K::Relative, g, 32),
378 elf::R_390_PC64 => (K::Relative, g, 64),
379 elf::R_390_PC16DBL => (K::Relative, E::S390xDbl, 16),
380 elf::R_390_PC32DBL => (K::Relative, E::S390xDbl, 32),
381 elf::R_390_PLT16DBL => (K::PltRelative, E::S390xDbl, 16),
382 elf::R_390_PLT32DBL => (K::PltRelative, E::S390xDbl, 32),
383 elf::R_390_GOT16 => (K::Got, g, 16),
384 elf::R_390_GOT32 => (K::Got, g, 32),
385 elf::R_390_GOT64 => (K::Got, g, 64),
386 elf::R_390_GOTENT => (K::GotRelative, E::S390xDbl, 32),
387 elf::R_390_GOTOFF16 => (K::GotBaseOffset, g, 16),
388 elf::R_390_GOTOFF32 => (K::GotBaseOffset, g, 32),
389 elf::R_390_GOTOFF64 => (K::GotBaseOffset, g, 64),
390 elf::R_390_GOTPC => (K::GotBaseRelative, g, 64),
391 elf::R_390_GOTPCDBL => (K::GotBaseRelative, E::S390xDbl, 32),
392 _ => unknown,
393 },
394 elf::EM_SBF => match r_type {
395 elf::R_SBF_64_64 => (K::Absolute, g, 64),
396 elf::R_SBF_64_32 => (K::Absolute, g, 32),
397 _ => unknown,
398 },
399 elf::EM_SHARC => match r_type {
400 elf::R_SHARC_ADDR24_V3 => (K::Absolute, E::SharcTypeA, 24),
401 elf::R_SHARC_ADDR32_V3 => (K::Absolute, E::SharcTypeA, 32),
402 elf::R_SHARC_ADDR_VAR_V3 => (K::Absolute, E::Generic, 32),
403 elf::R_SHARC_PCRSHORT_V3 => (K::Relative, E::SharcTypeA, 6),
404 elf::R_SHARC_PCRLONG_V3 => (K::Relative, E::SharcTypeA, 24),
405 elf::R_SHARC_DATA6_V3 => (K::Absolute, E::SharcTypeA, 6),
406 elf::R_SHARC_DATA16_V3 => (K::Absolute, E::SharcTypeA, 16),
407 elf::R_SHARC_DATA6_VISA_V3 => (K::Absolute, E::SharcTypeB, 6),
408 elf::R_SHARC_DATA7_VISA_V3 => (K::Absolute, E::SharcTypeB, 7),
409 elf::R_SHARC_DATA16_VISA_V3 => (K::Absolute, E::SharcTypeB, 16),
410 elf::R_SHARC_PCR6_VISA_V3 => (K::Relative, E::SharcTypeB, 16),
411 elf::R_SHARC_ADDR_VAR16_V3 => (K::Absolute, E::Generic, 16),
412 _ => unknown,
413 },
414 elf::EM_SPARC | elf::EM_SPARC32PLUS | elf::EM_SPARCV9 => match r_type {
415 elf::R_SPARC_32 | elf::R_SPARC_UA32 => (K::Absolute, g, 32),
416 elf::R_SPARC_64 | elf::R_SPARC_UA64 => (K::Absolute, g, 64),
417 _ => unknown,
418 },
419 elf::EM_XTENSA => match r_type {
420 elf::R_XTENSA_32 => (K::Absolute, g, 32),
421 elf::R_XTENSA_32_PCREL => (K::Relative, g, 32),
422 _ => unknown,
423 },
424 _ => unknown,
425 };
426 let target = match reloc.symbol(endian, is_mips64el) {
427 None => RelocationTarget::Absolute,
428 Some(symbol) => RelocationTarget::Symbol(symbol),
429 };
430 Relocation {
431 kind,
432 encoding,
433 size,
434 target,
435 addend: reloc.r_addend(endian).into(),
436 implicit_addend,
437 flags,
438 }
439}
440
441#[allow(missing_docs)]
443pub trait Rel: Debug + Pod + Clone {
444 type Word: Into<u64>;
445 type Sword: Into<i64>;
446 type Endian: endian::Endian;
447
448 fn r_offset(&self, endian: Self::Endian) -> Self::Word;
449 fn r_info(&self, endian: Self::Endian) -> Self::Word;
450 fn r_sym(&self, endian: Self::Endian) -> u32;
451 fn r_type(&self, endian: Self::Endian) -> u32;
452
453 fn symbol(&self, endian: Self::Endian) -> Option<SymbolIndex> {
457 let sym = self.r_sym(endian);
458 if sym == 0 {
459 None
460 } else {
461 Some(SymbolIndex(sym as usize))
462 }
463 }
464}
465
466impl<Endian: endian::Endian> Rel for elf::Rel32<Endian> {
467 type Word = u32;
468 type Sword = i32;
469 type Endian = Endian;
470
471 #[inline]
472 fn r_offset(&self, endian: Self::Endian) -> Self::Word {
473 self.r_offset.get(endian)
474 }
475
476 #[inline]
477 fn r_info(&self, endian: Self::Endian) -> Self::Word {
478 self.r_info.get(endian)
479 }
480
481 #[inline]
482 fn r_sym(&self, endian: Self::Endian) -> u32 {
483 self.r_sym(endian)
484 }
485
486 #[inline]
487 fn r_type(&self, endian: Self::Endian) -> u32 {
488 self.r_type(endian)
489 }
490}
491
492impl<Endian: endian::Endian> Rel for elf::Rel64<Endian> {
493 type Word = u64;
494 type Sword = i64;
495 type Endian = Endian;
496
497 #[inline]
498 fn r_offset(&self, endian: Self::Endian) -> Self::Word {
499 self.r_offset.get(endian)
500 }
501
502 #[inline]
503 fn r_info(&self, endian: Self::Endian) -> Self::Word {
504 self.r_info.get(endian)
505 }
506
507 #[inline]
508 fn r_sym(&self, endian: Self::Endian) -> u32 {
509 self.r_sym(endian)
510 }
511
512 #[inline]
513 fn r_type(&self, endian: Self::Endian) -> u32 {
514 self.r_type(endian)
515 }
516}
517
518#[allow(missing_docs)]
520pub trait Rela: Debug + Pod + Clone {
521 type Word: Into<u64>;
522 type Sword: Into<i64>;
523 type Endian: endian::Endian;
524
525 fn r_offset(&self, endian: Self::Endian) -> Self::Word;
526 fn r_info(&self, endian: Self::Endian, is_mips64el: bool) -> Self::Word;
527 fn r_addend(&self, endian: Self::Endian) -> Self::Sword;
528 fn r_sym(&self, endian: Self::Endian, is_mips64el: bool) -> u32;
529 fn r_type(&self, endian: Self::Endian, is_mips64el: bool) -> u32;
530
531 fn symbol(&self, endian: Self::Endian, is_mips64el: bool) -> Option<SymbolIndex> {
535 let sym = self.r_sym(endian, is_mips64el);
536 if sym == 0 {
537 None
538 } else {
539 Some(SymbolIndex(sym as usize))
540 }
541 }
542}
543
544impl<Endian: endian::Endian> Rela for elf::Rela32<Endian> {
545 type Word = u32;
546 type Sword = i32;
547 type Endian = Endian;
548
549 #[inline]
550 fn r_offset(&self, endian: Self::Endian) -> Self::Word {
551 self.r_offset.get(endian)
552 }
553
554 #[inline]
555 fn r_info(&self, endian: Self::Endian, _is_mips64el: bool) -> Self::Word {
556 self.r_info.get(endian)
557 }
558
559 #[inline]
560 fn r_addend(&self, endian: Self::Endian) -> Self::Sword {
561 self.r_addend.get(endian)
562 }
563
564 #[inline]
565 fn r_sym(&self, endian: Self::Endian, _is_mips64el: bool) -> u32 {
566 self.r_sym(endian)
567 }
568
569 #[inline]
570 fn r_type(&self, endian: Self::Endian, _is_mips64el: bool) -> u32 {
571 self.r_type(endian)
572 }
573}
574
575impl<Endian: endian::Endian> Rela for elf::Rela64<Endian> {
576 type Word = u64;
577 type Sword = i64;
578 type Endian = Endian;
579
580 #[inline]
581 fn r_offset(&self, endian: Self::Endian) -> Self::Word {
582 self.r_offset.get(endian)
583 }
584
585 #[inline]
586 fn r_info(&self, endian: Self::Endian, is_mips64el: bool) -> Self::Word {
587 self.get_r_info(endian, is_mips64el)
588 }
589
590 #[inline]
591 fn r_addend(&self, endian: Self::Endian) -> Self::Sword {
592 self.r_addend.get(endian)
593 }
594
595 #[inline]
596 fn r_sym(&self, endian: Self::Endian, is_mips64el: bool) -> u32 {
597 self.r_sym(endian, is_mips64el)
598 }
599
600 #[inline]
601 fn r_type(&self, endian: Self::Endian, is_mips64el: bool) -> u32 {
602 self.r_type(endian, is_mips64el)
603 }
604}