Path: blob/main/cranelift/codegen/src/binemit/mod.rs
1693 views
//! Binary machine code emission.1//!2//! The `binemit` module contains code for translating Cranelift's intermediate representation into3//! binary machine code.45use core::fmt;6#[cfg(feature = "enable-serde")]7use serde_derive::{Deserialize, Serialize};89/// Offset in bytes from the beginning of the function.10///11/// Cranelift can be used as a cross compiler, so we don't want to use a type like `usize` which12/// depends on the *host* platform, not the *target* platform.13pub type CodeOffset = u32;1415/// Addend to add to the symbol value.16pub type Addend = i64;1718/// Relocation kinds for every ISA19#[derive(Copy, Clone, Debug, PartialEq, Eq)]20#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]21pub enum Reloc {22/// absolute 4-byte23Abs4,24/// absolute 8-byte25Abs8,26/// x86 PC-relative 4-byte27X86PCRel4,28/// x86 call to PC-relative 4-byte29X86CallPCRel4,30/// x86 call to PLT-relative 4-byte31X86CallPLTRel4,32/// x86 GOT PC-relative 4-byte33X86GOTPCRel4,34/// The 32-bit offset of the target from the beginning of its section.35/// Equivalent to `IMAGE_REL_AMD64_SECREL`.36/// See: [PE Format](https://docs.microsoft.com/en-us/windows/win32/debug/pe-format)37X86SecRel,38/// Arm32 call target39Arm32Call,40/// Arm64 call target. Encoded as bottom 26 bits of instruction. This41/// value is sign-extended, multiplied by 4, and added to the PC of42/// the call instruction to form the destination address.43Arm64Call,44/// s390x PC-relative 4-byte offset45S390xPCRel32Dbl,46/// s390x PC-relative 4-byte offset to PLT47S390xPLTRel32Dbl,4849/// Elf x86_64 32 bit signed PC relative offset to two GOT entries for GD symbol.50ElfX86_64TlsGd,5152/// Mach-O x86_64 32 bit signed PC relative offset to a `__thread_vars` entry.53MachOX86_64Tlv,5455/// Mach-O Aarch64 TLS56/// PC-relative distance to the page of the TLVP slot.57MachOAarch64TlsAdrPage21,5859/// Mach-O Aarch64 TLS60/// Offset within page of TLVP slot.61MachOAarch64TlsAdrPageOff12,6263/// Aarch64 TLSDESC Adr Page2164/// This is equivalent to `R_AARCH64_TLSDESC_ADR_PAGE21` in the [aaelf64](https://github.com/ARM-software/abi-aa/blob/2bcab1e3b22d55170c563c3c7940134089176746/aaelf64/aaelf64.rst#57105thread-local-storage-descriptors)65Aarch64TlsDescAdrPage21,6667/// Aarch64 TLSDESC Ld64 Lo1268/// This is equivalent to `R_AARCH64_TLSDESC_LD64_LO12` in the [aaelf64](https://github.com/ARM-software/abi-aa/blob/2bcab1e3b22d55170c563c3c7940134089176746/aaelf64/aaelf64.rst#57105thread-local-storage-descriptors)69Aarch64TlsDescLd64Lo12,7071/// Aarch64 TLSDESC Add Lo1272/// This is equivalent to `R_AARCH64_TLSGD_ADD_LO12` in the [aaelf64](https://github.com/ARM-software/abi-aa/blob/2bcab1e3b22d55170c563c3c7940134089176746/aaelf64/aaelf64.rst#57105thread-local-storage-descriptors)73Aarch64TlsDescAddLo12,7475/// Aarch64 TLSDESC Call76/// This is equivalent to `R_AARCH64_TLSDESC_CALL` in the [aaelf64](https://github.com/ARM-software/abi-aa/blob/2bcab1e3b22d55170c563c3c7940134089176746/aaelf64/aaelf64.rst#57105thread-local-storage-descriptors)77Aarch64TlsDescCall,7879/// AArch64 GOT Page80/// Set the immediate value of an ADRP to bits 32:12 of X; check that –2^32 <= X < 2^3281/// This is equivalent to `R_AARCH64_ADR_GOT_PAGE` (311) in the [aaelf64](https://github.com/ARM-software/abi-aa/blob/2bcab1e3b22d55170c563c3c7940134089176746/aaelf64/aaelf64.rst#static-aarch64-relocations)82Aarch64AdrGotPage21,8384/// Equivalent of `R_AARCH64_ADR_PREL_PG_HI21`.85Aarch64AdrPrelPgHi21,86/// Equivalent of `R_AARCH64_ADD_ABS_LO12_NC`.87Aarch64AddAbsLo12Nc,8889/// AArch64 GOT Low bits9091/// Set the LD/ST immediate field to bits 11:3 of X. No overflow check; check that X&7 = 092/// This is equivalent to `R_AARCH64_LD64_GOT_LO12_NC` (312) in the [aaelf64](https://github.com/ARM-software/abi-aa/blob/2bcab1e3b22d55170c563c3c7940134089176746/aaelf64/aaelf64.rst#static-aarch64-relocations)93Aarch64Ld64GotLo12Nc,9495/// RISC-V Call PLT: 32-bit PC-relative function call, macros call, tail (PIC)96///97/// Despite having PLT in the name, this relocation is also used for normal calls.98/// The non-PLT version of this relocation has been deprecated.99///100/// This is the `R_RISCV_CALL_PLT` relocation from the RISC-V ELF psABI document.101/// <https://github.com/riscv-non-isa/riscv-elf-psabi-doc/blob/master/riscv-elf.adoc#procedure-calls>102RiscvCallPlt,103104/// RISC-V TLS GD: High 20 bits of 32-bit PC-relative TLS GD GOT reference,105///106/// This is the `R_RISCV_TLS_GD_HI20` relocation from the RISC-V ELF psABI document.107/// <https://github.com/riscv-non-isa/riscv-elf-psabi-doc/blob/master/riscv-elf.adoc#global-dynamic>108RiscvTlsGdHi20,109110/// Low 12 bits of a 32-bit PC-relative relocation (I-Type instruction)111///112/// This is the `R_RISCV_PCREL_LO12_I` relocation from the RISC-V ELF psABI document.113/// <https://github.com/riscv-non-isa/riscv-elf-psabi-doc/blob/master/riscv-elf.adoc#pc-relative-symbol-addresses>114RiscvPCRelLo12I,115116/// High 20 bits of a 32-bit PC-relative GOT offset relocation117///118/// This is the `R_RISCV_GOT_HI20` relocation from the RISC-V ELF psABI document.119/// <https://github.com/riscv-non-isa/riscv-elf-psabi-doc/blob/master/riscv-elf.adoc#pc-relative-symbol-addresses>120RiscvGotHi20,121122/// High 20 bits of a 32-bit PC-relative offset relocation123///124/// This is the `R_RISCV_PCREL_HI20` relocation from the RISC-V ELF psABI document.125/// <https://github.com/riscv-non-isa/riscv-elf-psabi-doc/blob/master/riscv-elf.adoc#pc-relative-symbol-addresses>126RiscvPCRelHi20,127128/// s390x TLS GD64 - 64-bit offset of tls_index for GD symbol in GOT129S390xTlsGd64,130/// s390x TLS GDCall - marker to enable optimization of TLS calls131S390xTlsGdCall,132133/// Pulley - a relocation which is a pc-relative offset.134PulleyPcRel,135136/// Pulley - call a host function indirectly where the embedder resolving137/// this relocation needs to fill the 8-bit immediate that's part of the138/// `call_indirect_host` opcode (an opaque identifier used by the host).139PulleyCallIndirectHost,140}141142impl fmt::Display for Reloc {143/// Display trait implementation drops the arch, since its used in contexts where the arch is144/// already unambiguous, e.g. clif syntax with isa specified. In other contexts, use Debug.145fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {146match *self {147Self::Abs4 => write!(f, "Abs4"),148Self::Abs8 => write!(f, "Abs8"),149Self::S390xPCRel32Dbl => write!(f, "PCRel32Dbl"),150Self::S390xPLTRel32Dbl => write!(f, "PLTRel32Dbl"),151Self::X86PCRel4 => write!(f, "PCRel4"),152Self::X86CallPCRel4 => write!(f, "CallPCRel4"),153Self::X86CallPLTRel4 => write!(f, "CallPLTRel4"),154Self::X86GOTPCRel4 => write!(f, "GOTPCRel4"),155Self::X86SecRel => write!(f, "SecRel"),156Self::Arm32Call | Self::Arm64Call => write!(f, "Call"),157Self::RiscvCallPlt => write!(f, "RiscvCallPlt"),158Self::RiscvTlsGdHi20 => write!(f, "RiscvTlsGdHi20"),159Self::RiscvGotHi20 => write!(f, "RiscvGotHi20"),160Self::RiscvPCRelHi20 => write!(f, "RiscvPCRelHi20"),161Self::RiscvPCRelLo12I => write!(f, "RiscvPCRelLo12I"),162Self::ElfX86_64TlsGd => write!(f, "ElfX86_64TlsGd"),163Self::MachOX86_64Tlv => write!(f, "MachOX86_64Tlv"),164Self::MachOAarch64TlsAdrPage21 => write!(f, "MachOAarch64TlsAdrPage21"),165Self::MachOAarch64TlsAdrPageOff12 => write!(f, "MachOAarch64TlsAdrPageOff12"),166Self::Aarch64TlsDescAdrPage21 => write!(f, "Aarch64TlsDescAdrPage21"),167Self::Aarch64TlsDescLd64Lo12 => write!(f, "Aarch64TlsDescLd64Lo12"),168Self::Aarch64TlsDescAddLo12 => write!(f, "Aarch64TlsDescAddLo12"),169Self::Aarch64TlsDescCall => write!(f, "Aarch64TlsDescCall"),170Self::Aarch64AdrGotPage21 => write!(f, "Aarch64AdrGotPage21"),171Self::Aarch64Ld64GotLo12Nc => write!(f, "Aarch64AdrGotLo12Nc"),172Self::Aarch64AdrPrelPgHi21 => write!(f, "Aarch64AdrPrelPgHi21"),173Self::Aarch64AddAbsLo12Nc => write!(f, "Aarch64AddAbsLo12Nc"),174Self::S390xTlsGd64 => write!(f, "TlsGd64"),175Self::S390xTlsGdCall => write!(f, "TlsGdCall"),176Self::PulleyPcRel => write!(f, "PulleyPcRel"),177Self::PulleyCallIndirectHost => write!(f, "PulleyCallIndirectHost"),178}179}180}181182/// Container for information about a vector of compiled code and its supporting read-only data.183///184/// The code starts at offset 0 and is followed optionally by relocatable jump tables and copyable185/// (raw binary) read-only data. Any padding between sections is always part of the section that186/// precedes the boundary between the sections.187#[derive(Debug, PartialEq)]188pub struct CodeInfo {189/// Number of bytes in total.190pub total_size: CodeOffset,191}192193194