object/write/elf/
object.rs

1use alloc::vec::Vec;
2
3use crate::write::elf::writer::*;
4use crate::write::string::StringId;
5use crate::write::*;
6use crate::{elf, pod};
7
8#[derive(Clone, Copy)]
9struct ComdatOffsets {
10    offset: usize,
11    str_id: StringId,
12}
13
14#[derive(Clone, Copy)]
15struct SectionOffsets {
16    index: SectionIndex,
17    offset: usize,
18    str_id: StringId,
19    reloc_offset: usize,
20    reloc_str_id: Option<StringId>,
21}
22
23#[derive(Default, Clone, Copy)]
24struct SymbolOffsets {
25    index: SymbolIndex,
26    str_id: Option<StringId>,
27}
28
29// Public methods.
30impl<'a> Object<'a> {
31    /// Add a property with a u32 value to the ELF ".note.gnu.property" section.
32    ///
33    /// Requires `feature = "elf"`.
34    pub fn add_elf_gnu_property_u32(&mut self, property: u32, value: u32) {
35        if self.format != BinaryFormat::Elf {
36            return;
37        }
38
39        let align = if self.elf_is_64() { 8 } else { 4 };
40        let mut data = Vec::with_capacity(32);
41        let n_name = b"GNU\0";
42        data.extend_from_slice(pod::bytes_of(&elf::NoteHeader32 {
43            n_namesz: U32::new(self.endian, n_name.len() as u32),
44            n_descsz: U32::new(self.endian, util::align(3 * 4, align) as u32),
45            n_type: U32::new(self.endian, elf::NT_GNU_PROPERTY_TYPE_0),
46        }));
47        data.extend_from_slice(n_name);
48        // This happens to already be aligned correctly.
49        debug_assert_eq!(util::align(data.len(), align), data.len());
50        data.extend_from_slice(pod::bytes_of(&U32::new(self.endian, property)));
51        // Value size
52        data.extend_from_slice(pod::bytes_of(&U32::new(self.endian, 4)));
53        data.extend_from_slice(pod::bytes_of(&U32::new(self.endian, value)));
54        util::write_align(&mut data, align);
55
56        let section = self.section_id(StandardSection::GnuProperty);
57        self.append_section_data(section, &data, align as u64);
58    }
59}
60
61// Private methods.
62impl<'a> Object<'a> {
63    pub(crate) fn elf_section_info(
64        &self,
65        section: StandardSection,
66    ) -> (&'static [u8], &'static [u8], SectionKind, SectionFlags) {
67        match section {
68            StandardSection::Text => (&[], &b".text"[..], SectionKind::Text, SectionFlags::None),
69            StandardSection::Data => (&[], &b".data"[..], SectionKind::Data, SectionFlags::None),
70            StandardSection::ReadOnlyData | StandardSection::ReadOnlyString => (
71                &[],
72                &b".rodata"[..],
73                SectionKind::ReadOnlyData,
74                SectionFlags::None,
75            ),
76            StandardSection::ReadOnlyDataWithRel => (
77                &[],
78                b".data.rel.ro",
79                SectionKind::ReadOnlyDataWithRel,
80                SectionFlags::None,
81            ),
82            StandardSection::UninitializedData => (
83                &[],
84                &b".bss"[..],
85                SectionKind::UninitializedData,
86                SectionFlags::None,
87            ),
88            StandardSection::Tls => (&[], &b".tdata"[..], SectionKind::Tls, SectionFlags::None),
89            StandardSection::UninitializedTls => (
90                &[],
91                &b".tbss"[..],
92                SectionKind::UninitializedTls,
93                SectionFlags::None,
94            ),
95            StandardSection::TlsVariables => {
96                // Unsupported section.
97                (&[], &[], SectionKind::TlsVariables, SectionFlags::None)
98            }
99            StandardSection::Common => {
100                // Unsupported section.
101                (&[], &[], SectionKind::Common, SectionFlags::None)
102            }
103            StandardSection::GnuProperty => (
104                &[],
105                &b".note.gnu.property"[..],
106                SectionKind::Note,
107                SectionFlags::Elf {
108                    sh_flags: u64::from(elf::SHF_ALLOC),
109                },
110            ),
111        }
112    }
113
114    pub(crate) fn elf_subsection_name(&self, section: &[u8], value: &[u8]) -> Vec<u8> {
115        let mut name = section.to_vec();
116        if !value.is_empty() {
117            name.push(b'.');
118            name.extend_from_slice(value);
119        }
120        name
121    }
122
123    pub(crate) fn elf_section_flags(&self, section: &Section<'_>) -> SectionFlags {
124        let sh_flags = match section.kind {
125            SectionKind::Text => elf::SHF_ALLOC | elf::SHF_EXECINSTR,
126            SectionKind::Data | SectionKind::ReadOnlyDataWithRel => elf::SHF_ALLOC | elf::SHF_WRITE,
127            SectionKind::Tls => elf::SHF_ALLOC | elf::SHF_WRITE | elf::SHF_TLS,
128            SectionKind::UninitializedData => elf::SHF_ALLOC | elf::SHF_WRITE,
129            SectionKind::UninitializedTls => elf::SHF_ALLOC | elf::SHF_WRITE | elf::SHF_TLS,
130            SectionKind::ReadOnlyData => elf::SHF_ALLOC,
131            SectionKind::ReadOnlyString => elf::SHF_ALLOC | elf::SHF_STRINGS | elf::SHF_MERGE,
132            SectionKind::OtherString | SectionKind::DebugString => {
133                elf::SHF_STRINGS | elf::SHF_MERGE
134            }
135            SectionKind::Other
136            | SectionKind::Debug
137            | SectionKind::Metadata
138            | SectionKind::Linker
139            | SectionKind::Note
140            | SectionKind::Elf(_) => 0,
141            SectionKind::Unknown | SectionKind::Common | SectionKind::TlsVariables => {
142                return SectionFlags::None;
143            }
144        }
145        .into();
146        SectionFlags::Elf { sh_flags }
147    }
148
149    pub(crate) fn elf_symbol_flags(&self, symbol: &Symbol) -> SymbolFlags<SectionId, SymbolId> {
150        let st_type = match symbol.kind {
151            SymbolKind::Text => {
152                if symbol.is_undefined() {
153                    elf::STT_NOTYPE
154                } else {
155                    elf::STT_FUNC
156                }
157            }
158            SymbolKind::Data => {
159                if symbol.is_undefined() {
160                    elf::STT_NOTYPE
161                } else if symbol.is_common() {
162                    elf::STT_COMMON
163                } else {
164                    elf::STT_OBJECT
165                }
166            }
167            SymbolKind::Section => elf::STT_SECTION,
168            SymbolKind::File => elf::STT_FILE,
169            SymbolKind::Tls => elf::STT_TLS,
170            SymbolKind::Label => elf::STT_NOTYPE,
171            SymbolKind::Unknown => {
172                if symbol.is_undefined() {
173                    elf::STT_NOTYPE
174                } else {
175                    return SymbolFlags::None;
176                }
177            }
178        };
179        let st_bind = if symbol.weak {
180            elf::STB_WEAK
181        } else if symbol.is_undefined() {
182            elf::STB_GLOBAL
183        } else if symbol.is_local() {
184            elf::STB_LOCAL
185        } else {
186            elf::STB_GLOBAL
187        };
188        let st_info = (st_bind << 4) + st_type;
189        let st_other = if symbol.scope == SymbolScope::Linkage {
190            elf::STV_HIDDEN
191        } else {
192            elf::STV_DEFAULT
193        };
194        SymbolFlags::Elf { st_info, st_other }
195    }
196
197    fn elf_has_relocation_addend(&self) -> Result<bool> {
198        Ok(match self.architecture {
199            Architecture::Aarch64 => true,
200            Architecture::Aarch64_Ilp32 => true,
201            Architecture::Alpha => true,
202            Architecture::Arm => false,
203            Architecture::Avr => true,
204            Architecture::Bpf => false,
205            Architecture::Csky => true,
206            Architecture::E2K32 => true,
207            Architecture::E2K64 => true,
208            Architecture::I386 => false,
209            Architecture::X86_64 => true,
210            Architecture::X86_64_X32 => true,
211            Architecture::Hppa => false,
212            Architecture::Hexagon => true,
213            Architecture::LoongArch32 => true,
214            Architecture::LoongArch64 => true,
215            Architecture::M68k => true,
216            Architecture::Mips => false,
217            Architecture::Mips64 => true,
218            Architecture::Mips64_N32 => true,
219            Architecture::Msp430 => true,
220            Architecture::PowerPc => true,
221            Architecture::PowerPc64 => true,
222            Architecture::Riscv64 => true,
223            Architecture::Riscv32 => true,
224            Architecture::S390x => true,
225            Architecture::Sbf => false,
226            Architecture::Sharc => true,
227            Architecture::Sparc => true,
228            Architecture::Sparc32Plus => true,
229            Architecture::Sparc64 => true,
230            Architecture::SuperH => false,
231            Architecture::Xtensa => true,
232            _ => {
233                return Err(Error(format!(
234                    "unimplemented architecture {:?}",
235                    self.architecture
236                )));
237            }
238        })
239    }
240
241    pub(crate) fn elf_translate_relocation(&mut self, reloc: &mut Relocation) -> Result<()> {
242        use RelocationEncoding as E;
243        use RelocationKind as K;
244
245        let (kind, encoding, size) = if let RelocationFlags::Generic {
246            kind,
247            encoding,
248            size,
249        } = reloc.flags
250        {
251            (kind, encoding, size)
252        } else {
253            return Ok(());
254        };
255
256        let unsupported_reloc = || Err(Error(format!("unimplemented ELF relocation {:?}", reloc)));
257        let r_type = match self.architecture {
258            Architecture::Aarch64 => match (kind, encoding, size) {
259                (K::Absolute, E::Generic, 64) => elf::R_AARCH64_ABS64,
260                (K::Absolute, E::Generic, 32) => elf::R_AARCH64_ABS32,
261                (K::Absolute, E::Generic, 16) => elf::R_AARCH64_ABS16,
262                (K::Relative, E::Generic, 64) => elf::R_AARCH64_PREL64,
263                (K::Relative, E::Generic, 32) => elf::R_AARCH64_PREL32,
264                (K::Relative, E::Generic, 16) => elf::R_AARCH64_PREL16,
265                (K::Relative, E::AArch64Call, 26) => elf::R_AARCH64_CALL26,
266                (K::PltRelative, E::AArch64Call, 26) => elf::R_AARCH64_CALL26,
267                _ => return unsupported_reloc(),
268            },
269            Architecture::Aarch64_Ilp32 => match (kind, encoding, size) {
270                (K::Absolute, E::Generic, 32) => elf::R_AARCH64_P32_ABS32,
271                _ => return unsupported_reloc(),
272            },
273            Architecture::Alpha => match (kind, encoding, size) {
274                // Absolute
275                (K::Absolute, _, 32) => elf::R_ALPHA_REFLONG,
276                (K::Absolute, _, 64) => elf::R_ALPHA_REFQUAD,
277                // Relative to the PC
278                (K::Relative, _, 16) => elf::R_ALPHA_SREL16,
279                (K::Relative, _, 32) => elf::R_ALPHA_SREL32,
280                (K::Relative, _, 64) => elf::R_ALPHA_SREL64,
281                _ => return unsupported_reloc(),
282            },
283            Architecture::Arm => match (kind, encoding, size) {
284                (K::Absolute, _, 32) => elf::R_ARM_ABS32,
285                _ => return unsupported_reloc(),
286            },
287            Architecture::Avr => match (kind, encoding, size) {
288                (K::Absolute, _, 32) => elf::R_AVR_32,
289                (K::Absolute, _, 16) => elf::R_AVR_16,
290                _ => return unsupported_reloc(),
291            },
292            Architecture::Bpf => match (kind, encoding, size) {
293                (K::Absolute, _, 64) => elf::R_BPF_64_64,
294                (K::Absolute, _, 32) => elf::R_BPF_64_32,
295                _ => return unsupported_reloc(),
296            },
297            Architecture::Csky => match (kind, encoding, size) {
298                (K::Absolute, _, 32) => elf::R_CKCORE_ADDR32,
299                (K::Relative, E::Generic, 32) => elf::R_CKCORE_PCREL32,
300                _ => return unsupported_reloc(),
301            },
302            Architecture::I386 => match (kind, size) {
303                (K::Absolute, 32) => elf::R_386_32,
304                (K::Relative, 32) => elf::R_386_PC32,
305                (K::Got, 32) => elf::R_386_GOT32,
306                (K::PltRelative, 32) => elf::R_386_PLT32,
307                (K::GotBaseOffset, 32) => elf::R_386_GOTOFF,
308                (K::GotBaseRelative, 32) => elf::R_386_GOTPC,
309                (K::Absolute, 16) => elf::R_386_16,
310                (K::Relative, 16) => elf::R_386_PC16,
311                (K::Absolute, 8) => elf::R_386_8,
312                (K::Relative, 8) => elf::R_386_PC8,
313                _ => return unsupported_reloc(),
314            },
315            Architecture::E2K32 | Architecture::E2K64 => match (kind, encoding, size) {
316                (K::Absolute, E::Generic, 32) => elf::R_E2K_32_ABS,
317                (K::Absolute, E::E2KLit, 64) => elf::R_E2K_64_ABS_LIT,
318                (K::Absolute, E::Generic, 64) => elf::R_E2K_64_ABS,
319                (K::Relative, E::E2KDisp, 28) => elf::R_E2K_DISP,
320                (K::Got, _, 32) => elf::R_E2K_GOT,
321                _ => return unsupported_reloc(),
322            },
323            Architecture::X86_64 | Architecture::X86_64_X32 => match (kind, encoding, size) {
324                (K::Absolute, E::Generic, 64) => elf::R_X86_64_64,
325                (K::Relative, E::X86Branch, 32) => elf::R_X86_64_PLT32,
326                (K::Relative, _, 32) => elf::R_X86_64_PC32,
327                (K::Got, _, 32) => elf::R_X86_64_GOT32,
328                (K::PltRelative, _, 32) => elf::R_X86_64_PLT32,
329                (K::GotRelative, _, 32) => elf::R_X86_64_GOTPCREL,
330                (K::Absolute, E::Generic, 32) => elf::R_X86_64_32,
331                (K::Absolute, E::X86Signed, 32) => elf::R_X86_64_32S,
332                (K::Absolute, _, 16) => elf::R_X86_64_16,
333                (K::Relative, _, 16) => elf::R_X86_64_PC16,
334                (K::Absolute, _, 8) => elf::R_X86_64_8,
335                (K::Relative, _, 8) => elf::R_X86_64_PC8,
336                _ => return unsupported_reloc(),
337            },
338            Architecture::Hppa => match (kind, encoding, size) {
339                (K::Absolute, _, 32) => elf::R_PARISC_DIR32,
340                (K::Relative, _, 32) => elf::R_PARISC_PCREL32,
341                _ => return unsupported_reloc(),
342            },
343            Architecture::Hexagon => match (kind, encoding, size) {
344                (K::Absolute, _, 32) => elf::R_HEX_32,
345                _ => return unsupported_reloc(),
346            },
347            Architecture::LoongArch32 | Architecture::LoongArch64 => match (kind, encoding, size) {
348                (K::Absolute, _, 32) => elf::R_LARCH_32,
349                (K::Absolute, _, 64) => elf::R_LARCH_64,
350                (K::Relative, _, 32) => elf::R_LARCH_32_PCREL,
351                (K::Relative, _, 64) => elf::R_LARCH_64_PCREL,
352                (K::Relative, E::LoongArchBranch, 16) => elf::R_LARCH_B16,
353                (K::PltRelative, E::LoongArchBranch, 16) => elf::R_LARCH_B16,
354                (K::Relative, E::LoongArchBranch, 21) => elf::R_LARCH_B21,
355                (K::PltRelative, E::LoongArchBranch, 21) => elf::R_LARCH_B21,
356                (K::Relative, E::LoongArchBranch, 26) => elf::R_LARCH_B26,
357                (K::PltRelative, E::LoongArchBranch, 26) => elf::R_LARCH_B26,
358                _ => return unsupported_reloc(),
359            },
360            Architecture::M68k => match (kind, encoding, size) {
361                (K::Absolute, _, 8) => elf::R_68K_8,
362                (K::Absolute, _, 16) => elf::R_68K_16,
363                (K::Absolute, _, 32) => elf::R_68K_32,
364                (K::Relative, _, 8) => elf::R_68K_PC8,
365                (K::Relative, _, 16) => elf::R_68K_PC16,
366                (K::Relative, _, 32) => elf::R_68K_PC32,
367                (K::GotRelative, _, 8) => elf::R_68K_GOT8,
368                (K::GotRelative, _, 16) => elf::R_68K_GOT16,
369                (K::GotRelative, _, 32) => elf::R_68K_GOT32,
370                (K::Got, _, 8) => elf::R_68K_GOT8O,
371                (K::Got, _, 16) => elf::R_68K_GOT16O,
372                (K::Got, _, 32) => elf::R_68K_GOT32O,
373                (K::PltRelative, _, 8) => elf::R_68K_PLT8,
374                (K::PltRelative, _, 16) => elf::R_68K_PLT16,
375                (K::PltRelative, _, 32) => elf::R_68K_PLT32,
376                _ => return unsupported_reloc(),
377            },
378            Architecture::Mips | Architecture::Mips64 | Architecture::Mips64_N32 => {
379                match (kind, encoding, size) {
380                    (K::Absolute, _, 16) => elf::R_MIPS_16,
381                    (K::Absolute, _, 32) => elf::R_MIPS_32,
382                    (K::Absolute, _, 64) => elf::R_MIPS_64,
383                    _ => return unsupported_reloc(),
384                }
385            }
386            Architecture::Msp430 => match (kind, encoding, size) {
387                (K::Absolute, _, 32) => elf::R_MSP430_32,
388                (K::Absolute, _, 16) => elf::R_MSP430_16_BYTE,
389                _ => return unsupported_reloc(),
390            },
391            Architecture::PowerPc => match (kind, encoding, size) {
392                (K::Absolute, _, 32) => elf::R_PPC_ADDR32,
393                _ => return unsupported_reloc(),
394            },
395            Architecture::PowerPc64 => match (kind, encoding, size) {
396                (K::Absolute, _, 32) => elf::R_PPC64_ADDR32,
397                (K::Absolute, _, 64) => elf::R_PPC64_ADDR64,
398                _ => return unsupported_reloc(),
399            },
400            Architecture::Riscv32 | Architecture::Riscv64 => match (kind, encoding, size) {
401                (K::Absolute, _, 32) => elf::R_RISCV_32,
402                (K::Absolute, _, 64) => elf::R_RISCV_64,
403                (K::Relative, E::Generic, 32) => elf::R_RISCV_32_PCREL,
404                _ => return unsupported_reloc(),
405            },
406            Architecture::S390x => match (kind, encoding, size) {
407                (K::Absolute, E::Generic, 8) => elf::R_390_8,
408                (K::Absolute, E::Generic, 16) => elf::R_390_16,
409                (K::Absolute, E::Generic, 32) => elf::R_390_32,
410                (K::Absolute, E::Generic, 64) => elf::R_390_64,
411                (K::Relative, E::Generic, 16) => elf::R_390_PC16,
412                (K::Relative, E::Generic, 32) => elf::R_390_PC32,
413                (K::Relative, E::Generic, 64) => elf::R_390_PC64,
414                (K::Relative, E::S390xDbl, 16) => elf::R_390_PC16DBL,
415                (K::Relative, E::S390xDbl, 32) => elf::R_390_PC32DBL,
416                (K::PltRelative, E::S390xDbl, 16) => elf::R_390_PLT16DBL,
417                (K::PltRelative, E::S390xDbl, 32) => elf::R_390_PLT32DBL,
418                (K::Got, E::Generic, 16) => elf::R_390_GOT16,
419                (K::Got, E::Generic, 32) => elf::R_390_GOT32,
420                (K::Got, E::Generic, 64) => elf::R_390_GOT64,
421                (K::GotRelative, E::S390xDbl, 32) => elf::R_390_GOTENT,
422                (K::GotBaseOffset, E::Generic, 16) => elf::R_390_GOTOFF16,
423                (K::GotBaseOffset, E::Generic, 32) => elf::R_390_GOTOFF32,
424                (K::GotBaseOffset, E::Generic, 64) => elf::R_390_GOTOFF64,
425                (K::GotBaseRelative, E::Generic, 64) => elf::R_390_GOTPC,
426                (K::GotBaseRelative, E::S390xDbl, 32) => elf::R_390_GOTPCDBL,
427                _ => return unsupported_reloc(),
428            },
429            Architecture::Sbf => match (kind, encoding, size) {
430                (K::Absolute, _, 64) => elf::R_SBF_64_64,
431                (K::Absolute, _, 32) => elf::R_SBF_64_32,
432                _ => return unsupported_reloc(),
433            },
434            Architecture::Sharc => match (kind, encoding, size) {
435                (K::Absolute, E::SharcTypeA, 32) => elf::R_SHARC_ADDR32_V3,
436                (K::Absolute, E::Generic, 32) => elf::R_SHARC_ADDR_VAR_V3,
437                (K::Relative, E::SharcTypeA, 24) => elf::R_SHARC_PCRLONG_V3,
438                (K::Relative, E::SharcTypeA, 6) => elf::R_SHARC_PCRSHORT_V3,
439                (K::Relative, E::SharcTypeB, 6) => elf::R_SHARC_PCRSHORT_V3,
440                (K::Absolute, E::Generic, 16) => elf::R_SHARC_ADDR_VAR16_V3,
441                (K::Absolute, E::SharcTypeA, 16) => elf::R_SHARC_DATA16_V3,
442                (K::Absolute, E::SharcTypeB, 16) => elf::R_SHARC_DATA16_VISA_V3,
443                (K::Absolute, E::SharcTypeA, 24) => elf::R_SHARC_ADDR24_V3,
444                (K::Absolute, E::SharcTypeA, 6) => elf::R_SHARC_DATA6_V3,
445                (K::Absolute, E::SharcTypeB, 6) => elf::R_SHARC_DATA6_VISA_V3,
446                (K::Absolute, E::SharcTypeB, 7) => elf::R_SHARC_DATA7_VISA_V3,
447                _ => return unsupported_reloc(),
448            },
449            Architecture::Sparc | Architecture::Sparc32Plus => match (kind, encoding, size) {
450                // TODO: use R_SPARC_32 if aligned.
451                (K::Absolute, _, 32) => elf::R_SPARC_UA32,
452                _ => return unsupported_reloc(),
453            },
454            Architecture::Sparc64 => match (kind, encoding, size) {
455                // TODO: use R_SPARC_32/R_SPARC_64 if aligned.
456                (K::Absolute, _, 32) => elf::R_SPARC_UA32,
457                (K::Absolute, _, 64) => elf::R_SPARC_UA64,
458                _ => return unsupported_reloc(),
459            },
460            Architecture::SuperH => match (kind, encoding, size) {
461                (K::Absolute, _, 32) => elf::R_SH_DIR32,
462                (K::Relative, _, 32) => elf::R_SH_REL32,
463                _ => return unsupported_reloc(),
464            },
465            Architecture::Xtensa => match (kind, encoding, size) {
466                (K::Absolute, _, 32) => elf::R_XTENSA_32,
467                (K::Relative, E::Generic, 32) => elf::R_XTENSA_32_PCREL,
468                _ => return unsupported_reloc(),
469            },
470            _ => {
471                return Err(Error(format!(
472                    "unimplemented architecture {:?}",
473                    self.architecture
474                )));
475            }
476        };
477        reloc.flags = RelocationFlags::Elf { r_type };
478        Ok(())
479    }
480
481    pub(crate) fn elf_adjust_addend(&mut self, _relocation: &mut Relocation) -> Result<bool> {
482        // Determine whether the addend is stored in the relocation or the data.
483        let implicit = !self.elf_has_relocation_addend()?;
484        Ok(implicit)
485    }
486
487    pub(crate) fn elf_relocation_size(&self, reloc: &Relocation) -> Result<u8> {
488        let r_type = if let RelocationFlags::Elf { r_type } = reloc.flags {
489            r_type
490        } else {
491            return Err(Error("invalid relocation flags".into()));
492        };
493        // This only needs to support architectures that use implicit addends.
494        let size = match self.architecture {
495            Architecture::Arm => match r_type {
496                elf::R_ARM_ABS16 => Some(16),
497                elf::R_ARM_ABS32 | elf::R_ARM_REL32 => Some(32),
498                _ => None,
499            },
500            Architecture::Bpf => match r_type {
501                elf::R_BPF_64_32 => Some(32),
502                elf::R_BPF_64_64 => Some(64),
503                _ => None,
504            },
505            Architecture::I386 => match r_type {
506                elf::R_386_8 | elf::R_386_PC8 => Some(8),
507                elf::R_386_16 | elf::R_386_PC16 => Some(16),
508                elf::R_386_32
509                | elf::R_386_PC32
510                | elf::R_386_GOT32
511                | elf::R_386_PLT32
512                | elf::R_386_GOTOFF
513                | elf::R_386_GOTPC => Some(32),
514                _ => None,
515            },
516            Architecture::Mips => match r_type {
517                elf::R_MIPS_16 => Some(16),
518                elf::R_MIPS_32 => Some(32),
519                elf::R_MIPS_64 => Some(64),
520                _ => None,
521            },
522            Architecture::Sbf => match r_type {
523                elf::R_SBF_64_32 => Some(32),
524                elf::R_SBF_64_64 => Some(64),
525                _ => None,
526            },
527            _ => {
528                return Err(Error(format!(
529                    "unimplemented architecture {:?}",
530                    self.architecture
531                )));
532            }
533        };
534        size.ok_or_else(|| Error(format!("unsupported relocation for size {:?}", reloc)))
535    }
536
537    pub(crate) fn elf_is_64(&self) -> bool {
538        match self.architecture.address_size().unwrap() {
539            AddressSize::U8 | AddressSize::U16 | AddressSize::U32 => false,
540            AddressSize::U64 => true,
541        }
542    }
543
544    pub(crate) fn elf_write(&self, buffer: &mut dyn WritableBuffer) -> Result<()> {
545        // Create reloc section header names so we can reference them.
546        let is_rela = self.elf_has_relocation_addend()?;
547        let reloc_names: Vec<_> = self
548            .sections
549            .iter()
550            .map(|section| {
551                let mut reloc_name = Vec::with_capacity(
552                    if is_rela { ".rela".len() } else { ".rel".len() } + section.name.len(),
553                );
554                if !section.relocations.is_empty() {
555                    reloc_name.extend_from_slice(if is_rela {
556                        &b".rela"[..]
557                    } else {
558                        &b".rel"[..]
559                    });
560                    reloc_name.extend_from_slice(&section.name);
561                }
562                reloc_name
563            })
564            .collect();
565
566        // Start calculating offsets of everything.
567        let mut writer = Writer::new(self.endian, self.elf_is_64(), buffer);
568        writer.reserve_file_header();
569
570        // Calculate size of section data.
571        let mut comdat_offsets = Vec::with_capacity(self.comdats.len());
572        for comdat in &self.comdats {
573            if comdat.kind != ComdatKind::Any {
574                return Err(Error(format!(
575                    "unsupported COMDAT symbol `{}` kind {:?}",
576                    self.symbols[comdat.symbol.0].name().unwrap_or(""),
577                    comdat.kind
578                )));
579            }
580
581            writer.reserve_section_index();
582            let offset = writer.reserve_comdat(comdat.sections.len());
583            let str_id = writer.add_section_name(b".group");
584            comdat_offsets.push(ComdatOffsets { offset, str_id });
585        }
586        let mut section_offsets = Vec::with_capacity(self.sections.len());
587        for (section, reloc_name) in self.sections.iter().zip(reloc_names.iter()) {
588            let index = writer.reserve_section_index();
589            let offset = writer.reserve(section.data.len(), section.align as usize);
590            let str_id = writer.add_section_name(&section.name);
591            let mut reloc_str_id = None;
592            if !section.relocations.is_empty() {
593                writer.reserve_section_index();
594                reloc_str_id = Some(writer.add_section_name(reloc_name));
595            }
596            section_offsets.push(SectionOffsets {
597                index,
598                offset,
599                str_id,
600                // Relocation data is reserved later.
601                reloc_offset: 0,
602                reloc_str_id,
603            });
604        }
605
606        // Calculate index of symbols and add symbol strings to strtab.
607        let mut symbol_offsets = vec![SymbolOffsets::default(); self.symbols.len()];
608        writer.reserve_null_symbol_index();
609        // Local symbols must come before global.
610        for (index, symbol) in self.symbols.iter().enumerate() {
611            if symbol.is_local() {
612                let section_index = symbol.section.id().map(|s| section_offsets[s.0].index);
613                symbol_offsets[index].index = writer.reserve_symbol_index(section_index);
614            }
615        }
616        let symtab_num_local = writer.symbol_count();
617        for (index, symbol) in self.symbols.iter().enumerate() {
618            if !symbol.is_local() {
619                let section_index = symbol.section.id().map(|s| section_offsets[s.0].index);
620                symbol_offsets[index].index = writer.reserve_symbol_index(section_index);
621            }
622        }
623        for (index, symbol) in self.symbols.iter().enumerate() {
624            if symbol.kind != SymbolKind::Section && !symbol.name.is_empty() {
625                symbol_offsets[index].str_id = Some(writer.add_string(&symbol.name));
626            }
627        }
628
629        // Calculate size of symbols.
630        writer.reserve_symtab_section_index();
631        writer.reserve_symtab();
632        if writer.symtab_shndx_needed() {
633            writer.reserve_symtab_shndx_section_index();
634        }
635        writer.reserve_symtab_shndx();
636        writer.reserve_strtab_section_index();
637        writer.reserve_strtab();
638
639        // Calculate size of relocations.
640        for (index, section) in self.sections.iter().enumerate() {
641            let count = section.relocations.len();
642            if count != 0 {
643                section_offsets[index].reloc_offset = writer.reserve_relocations(count, is_rela);
644            }
645        }
646
647        // Calculate size of section headers.
648        writer.reserve_shstrtab_section_index();
649        writer.reserve_shstrtab();
650        writer.reserve_section_headers();
651
652        // Start writing.
653        let e_type = elf::ET_REL;
654        let e_machine = match (self.architecture, self.sub_architecture) {
655            (Architecture::Aarch64, None) => elf::EM_AARCH64,
656            (Architecture::Aarch64_Ilp32, None) => elf::EM_AARCH64,
657            (Architecture::Alpha, None) => elf::EM_ALPHA,
658            (Architecture::Arm, None) => elf::EM_ARM,
659            (Architecture::Avr, None) => elf::EM_AVR,
660            (Architecture::Bpf, None) => elf::EM_BPF,
661            (Architecture::Csky, None) => elf::EM_CSKY,
662            (Architecture::E2K32, None) => elf::EM_MCST_ELBRUS,
663            (Architecture::E2K64, None) => elf::EM_MCST_ELBRUS,
664            (Architecture::I386, None) => elf::EM_386,
665            (Architecture::X86_64, None) => elf::EM_X86_64,
666            (Architecture::X86_64_X32, None) => elf::EM_X86_64,
667            (Architecture::Hppa, None) => elf::EM_PARISC,
668            (Architecture::Hexagon, None) => elf::EM_HEXAGON,
669            (Architecture::LoongArch32, None) => elf::EM_LOONGARCH,
670            (Architecture::LoongArch64, None) => elf::EM_LOONGARCH,
671            (Architecture::M68k, None) => elf::EM_68K,
672            (Architecture::Mips, None) => elf::EM_MIPS,
673            (Architecture::Mips64, None) => elf::EM_MIPS,
674            (Architecture::Mips64_N32, None) => elf::EM_MIPS,
675            (Architecture::Msp430, None) => elf::EM_MSP430,
676            (Architecture::PowerPc, None) => elf::EM_PPC,
677            (Architecture::PowerPc64, None) => elf::EM_PPC64,
678            (Architecture::Riscv32, None) => elf::EM_RISCV,
679            (Architecture::Riscv64, None) => elf::EM_RISCV,
680            (Architecture::S390x, None) => elf::EM_S390,
681            (Architecture::Sbf, None) => elf::EM_SBF,
682            (Architecture::Sharc, None) => elf::EM_SHARC,
683            (Architecture::Sparc, None) => elf::EM_SPARC,
684            (Architecture::Sparc32Plus, None) => elf::EM_SPARC32PLUS,
685            (Architecture::Sparc64, None) => elf::EM_SPARCV9,
686            (Architecture::SuperH, None) => elf::EM_SH,
687            (Architecture::Xtensa, None) => elf::EM_XTENSA,
688            _ => {
689                return Err(Error(format!(
690                    "unimplemented architecture {:?} with sub-architecture {:?}",
691                    self.architecture, self.sub_architecture
692                )));
693            }
694        };
695        let (os_abi, abi_version, mut e_flags) = if let FileFlags::Elf {
696            os_abi,
697            abi_version,
698            e_flags,
699        } = self.flags
700        {
701            (os_abi, abi_version, e_flags)
702        } else {
703            (elf::ELFOSABI_NONE, 0, 0)
704        };
705
706        if self.architecture == Architecture::Mips64_N32 {
707            e_flags |= elf::EF_MIPS_ABI2;
708        }
709
710        writer.write_file_header(&FileHeader {
711            os_abi,
712            abi_version,
713            e_type,
714            e_machine,
715            e_entry: 0,
716            e_flags,
717        })?;
718
719        // Write section data.
720        for comdat in &self.comdats {
721            writer.write_comdat_header();
722            for section in &comdat.sections {
723                writer.write_comdat_entry(section_offsets[section.0].index);
724            }
725        }
726        for (index, section) in self.sections.iter().enumerate() {
727            writer.write_align(section.align as usize);
728            debug_assert_eq!(section_offsets[index].offset, writer.len());
729            writer.write(&section.data);
730        }
731
732        // Write symbols.
733        writer.write_null_symbol();
734        let mut write_symbol = |index: usize, symbol: &Symbol| -> Result<()> {
735            let SymbolFlags::Elf { st_info, st_other } = self.symbol_flags(symbol) else {
736                return Err(Error(format!(
737                    "unimplemented symbol `{}` kind {:?}",
738                    symbol.name().unwrap_or(""),
739                    symbol.kind
740                )));
741            };
742            let (st_shndx, section) = match symbol.section {
743                SymbolSection::None => {
744                    debug_assert_eq!(symbol.kind, SymbolKind::File);
745                    (elf::SHN_ABS, None)
746                }
747                SymbolSection::Undefined => (elf::SHN_UNDEF, None),
748                SymbolSection::Absolute => (elf::SHN_ABS, None),
749                SymbolSection::Common => (elf::SHN_COMMON, None),
750                SymbolSection::Section(id) => (0, Some(section_offsets[id.0].index)),
751            };
752            writer.write_symbol(&Sym {
753                name: symbol_offsets[index].str_id,
754                section,
755                st_info,
756                st_other,
757                st_shndx,
758                st_value: symbol.value,
759                st_size: symbol.size,
760            });
761            Ok(())
762        };
763        for (index, symbol) in self.symbols.iter().enumerate() {
764            if symbol.is_local() {
765                write_symbol(index, symbol)?;
766            }
767        }
768        for (index, symbol) in self.symbols.iter().enumerate() {
769            if !symbol.is_local() {
770                write_symbol(index, symbol)?;
771            }
772        }
773        writer.write_symtab_shndx();
774        writer.write_strtab();
775
776        // Write relocations.
777        for (index, section) in self.sections.iter().enumerate() {
778            if !section.relocations.is_empty() {
779                writer.write_align_relocation();
780                debug_assert_eq!(section_offsets[index].reloc_offset, writer.len());
781                for reloc in &section.relocations {
782                    let r_type = if let RelocationFlags::Elf { r_type } = reloc.flags {
783                        r_type
784                    } else {
785                        return Err(Error("invalid relocation flags".into()));
786                    };
787                    let r_sym = symbol_offsets[reloc.symbol.0].index.0;
788                    writer.write_relocation(
789                        is_rela,
790                        &Rel {
791                            r_offset: reloc.offset,
792                            r_sym,
793                            r_type,
794                            r_addend: reloc.addend,
795                        },
796                    );
797                }
798            }
799        }
800
801        writer.write_shstrtab();
802
803        // Write section headers.
804        writer.write_null_section_header();
805
806        let symtab_index = writer.symtab_index();
807        for (comdat, comdat_offset) in self.comdats.iter().zip(comdat_offsets.iter()) {
808            writer.write_comdat_section_header(
809                comdat_offset.str_id,
810                symtab_index,
811                symbol_offsets[comdat.symbol.0].index,
812                comdat_offset.offset,
813                comdat.sections.len(),
814            );
815        }
816        for (index, section) in self.sections.iter().enumerate() {
817            let sh_type = match section.kind {
818                SectionKind::UninitializedData | SectionKind::UninitializedTls => elf::SHT_NOBITS,
819                SectionKind::Note => elf::SHT_NOTE,
820                SectionKind::Elf(sh_type) => sh_type,
821                _ => elf::SHT_PROGBITS,
822            };
823            let SectionFlags::Elf { sh_flags } = self.section_flags(section) else {
824                return Err(Error(format!(
825                    "unimplemented section `{}` kind {:?}",
826                    section.name().unwrap_or(""),
827                    section.kind
828                )));
829            };
830            // TODO: not sure if this is correct, maybe user should determine this
831            let sh_entsize = match section.kind {
832                SectionKind::ReadOnlyString | SectionKind::OtherString => 1,
833                _ => 0,
834            };
835            writer.write_section_header(&SectionHeader {
836                name: Some(section_offsets[index].str_id),
837                sh_type,
838                sh_flags,
839                sh_addr: 0,
840                sh_offset: section_offsets[index].offset as u64,
841                sh_size: section.size,
842                sh_link: 0,
843                sh_info: 0,
844                sh_addralign: section.align,
845                sh_entsize,
846            });
847
848            if !section.relocations.is_empty() {
849                writer.write_relocation_section_header(
850                    section_offsets[index].reloc_str_id.unwrap(),
851                    section_offsets[index].index,
852                    symtab_index,
853                    section_offsets[index].reloc_offset,
854                    section.relocations.len(),
855                    is_rela,
856                );
857            }
858        }
859
860        writer.write_symtab_section_header(symtab_num_local);
861        writer.write_symtab_shndx_section_header();
862        writer.write_strtab_section_header();
863        writer.write_shstrtab_section_header();
864
865        debug_assert_eq!(writer.reserved_len(), writer.len());
866
867        Ok(())
868    }
869}