#![cfg_attr(not(unix), allow(dead_code))]
use std::ffi::OsString;
use std::fmt;
use std::path::PathBuf;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum OutputKind {
Relocatable,
#[default]
Executable,
SharedObject,
Pie,
}
impl OutputKind {
pub(crate) const fn raw(self) -> i32 {
match self {
Self::Relocatable => 0,
Self::Executable => 1,
Self::SharedObject => 2,
Self::Pie => 3,
}
}
}
impl From<crate::args::OutputKind> for OutputKind {
fn from(kind: crate::args::OutputKind) -> Self {
use crate::args::OutputKind as Link;
match kind {
Link::Executable | Link::StaticExecutable => Self::Executable,
Link::Pie | Link::StaticPie => Self::Pie,
Link::Shared => Self::SharedObject,
Link::Relocatable => Self::Relocatable,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum SymbolKind {
Definition,
WeakDefinition,
Undefined,
WeakUndefined,
Common,
}
impl SymbolKind {
pub(crate) const fn from_raw(raw: u8) -> Option<Self> {
Some(match raw {
0 => Self::Definition,
1 => Self::WeakDefinition,
2 => Self::Undefined,
3 => Self::WeakUndefined,
4 => Self::Common,
_ => return None,
})
}
#[must_use]
pub const fn is_undefined(self) -> bool {
matches!(self, Self::Undefined | Self::WeakUndefined)
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Visibility {
#[default]
Default,
Protected,
Internal,
Hidden,
}
impl Visibility {
pub(crate) fn from_raw(raw: i32) -> Option<Self> {
Some(match raw {
0 => Self::Default,
1 => Self::Protected,
2 => Self::Internal,
3 => Self::Hidden,
_ => return None,
})
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum SymbolType {
#[default]
Unknown,
Function,
Variable,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum SectionKind {
#[default]
Default,
Bss,
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct ClaimedSymbol {
pub name: Vec<u8>,
pub version: Option<Vec<u8>>,
pub kind: SymbolKind,
pub visibility: Visibility,
pub size: u64,
pub comdat_key: Option<Vec<u8>>,
pub symbol_type: SymbolType,
pub section_kind: SectionKind,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum SymbolResolution {
Unknown,
Undefined,
PrevailingDef,
PrevailingDefIronly,
PreemptedRegular,
PreemptedIr,
ResolvedIr,
ResolvedExec,
ResolvedDyn,
PrevailingDefIronlyExp,
}
impl SymbolResolution {
pub(crate) const fn raw(self) -> i32 {
match self {
Self::Unknown => 0,
Self::Undefined => 1,
Self::PrevailingDef => 2,
Self::PrevailingDefIronly => 3,
Self::PreemptedRegular => 4,
Self::PreemptedIr => 5,
Self::ResolvedIr => 6,
Self::ResolvedExec => 7,
Self::ResolvedDyn => 8,
Self::PrevailingDefIronlyExp => 9,
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum FileResolution {
Included(Vec<SymbolResolution>),
NotIncluded,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ClaimedFile {
pub handle: u64,
pub path: PathBuf,
pub offset: u64,
pub size: u64,
pub plugin: usize,
pub symbols: Vec<ClaimedSymbol>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct InputFile {
pub path: PathBuf,
pub offset: u64,
pub size: u64,
pub handle: u64,
pub known_used: bool,
}
impl InputFile {
pub fn new(path: impl Into<PathBuf>, offset: u64, size: u64, handle: u64) -> Self {
Self {
path: path.into(),
offset,
size,
handle,
known_used: true,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum MessageLevel {
Info,
Warning,
Error,
Fatal,
}
impl MessageLevel {
pub(crate) fn from_raw(raw: i32) -> Self {
match raw {
0 => Self::Info,
1 => Self::Warning,
3 => Self::Fatal,
_ => Self::Error,
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PluginMessage {
pub level: MessageLevel,
pub text: String,
}
impl fmt::Display for PluginMessage {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.text)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SectionRef {
pub file: u64,
pub index: u32,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct UniqueSegment {
pub name: Vec<u8>,
pub flags: u64,
pub alignment: u64,
pub sections: Vec<SectionRef>,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct LtoOutput {
pub files: Vec<PathBuf>,
pub libraries: Vec<OsString>,
pub library_paths: Vec<PathBuf>,
pub section_order: Option<Vec<SectionRef>>,
pub unique_segments: Vec<UniqueSegment>,
pub errors: usize,
}