elf_loader 0.15.0

A no_std-friendly ELF loader, runtime linker, and JIT linker for Rust.
//! LoongArch 64-bit ELF relocation numbering.

use elf::abi::EM_LOONGARCH;

use crate::arch::ArchKind;
use crate::elf::{Elf64Layout, ElfMachine, ElfRela, ElfRelocationType};
use crate::relocation::RelocationArch;

const R_LARCH_64: u32 = 2;
const R_LARCH_RELATIVE: u32 = 3;
const R_LARCH_COPY: u32 = 4;
const R_LARCH_JUMP_SLOT: u32 = 5;
const R_LARCH_TLS_DTPMOD64: u32 = 7;
const R_LARCH_TLS_DTPREL64: u32 = 9;
const R_LARCH_TLS_TPREL64: u32 = 11;
const R_LARCH_IRELATIVE: u32 = 12;

/// LoongArch 64-bit architecture marker.
#[derive(Debug, Clone, Copy, Default)]
pub struct LoongArch64Arch;

impl RelocationArch for LoongArch64Arch {
    const KIND: ArchKind = ArchKind::LoongArch64;
    const MACHINE: ElfMachine = ElfMachine::new(EM_LOONGARCH);
    type Layout = Elf64Layout;
    type Relocation = ElfRela<Self::Layout>;

    const NONE: ElfRelocationType = ElfRelocationType::new(0);
    const RELATIVE: ElfRelocationType = ElfRelocationType::new(R_LARCH_RELATIVE);
    const GOT: ElfRelocationType = ElfRelocationType::new(R_LARCH_64);
    const SYMBOLIC: ElfRelocationType = ElfRelocationType::new(R_LARCH_64);
    const JUMP_SLOT: ElfRelocationType = ElfRelocationType::new(R_LARCH_JUMP_SLOT);
    const IRELATIVE: ElfRelocationType = ElfRelocationType::new(R_LARCH_IRELATIVE);
    const COPY: ElfRelocationType = ElfRelocationType::new(R_LARCH_COPY);

    const DTPMOD: ElfRelocationType = ElfRelocationType::new(R_LARCH_TLS_DTPMOD64);
    const DTPOFF: ElfRelocationType = ElfRelocationType::new(R_LARCH_TLS_DTPREL64);
    const TPOFF: ElfRelocationType = ElfRelocationType::new(R_LARCH_TLS_TPREL64);
    // LoongArch 64-bit does not define a TLSDESC relocation.
    const TLSDESC: Option<ElfRelocationType> = None;

    // `true` only when this ZST is the host's relocation backend.
    const SUPPORTS_NATIVE_RUNTIME: bool = cfg!(target_arch = "loongarch64");

    #[inline]
    fn rel_type_to_str(r_type: ElfRelocationType) -> &'static str {
        match r_type.raw() {
            R_LARCH_64 => "R_LARCH_64",
            R_LARCH_RELATIVE => "R_LARCH_RELATIVE",
            R_LARCH_COPY => "R_LARCH_COPY",
            R_LARCH_JUMP_SLOT => "R_LARCH_JUMP_SLOT",
            R_LARCH_TLS_DTPMOD64 => "R_LARCH_TLS_DTPMOD64",
            R_LARCH_TLS_DTPREL64 => "R_LARCH_TLS_DTPREL64",
            R_LARCH_IRELATIVE => "R_LARCH_IRELATIVE",
            _ => "UNKNOWN",
        }
    }
}

impl crate::relocation::RelocationValueProvider for LoongArch64Arch {}
impl crate::linker::scan::GotPltTarget for LoongArch64Arch {}