mod cli;
use std::{
env,
ffi::OsString,
path::{Path, PathBuf},
process::ExitCode,
};
use cli::{Options, Request, emulation, expand_response_files, parse};
use xold::{
buildid::BuildId,
coff::link_coff,
dynamic::LinkMode,
elf::constants::ET_DYN,
error::{Error, Result},
icf::IcfMode,
input::{Format, Input},
linker::{Link, link_image},
macho::{Dylib, LinkOptions as MachOLinkOptions, link_macho_with_options},
script::{self, Search},
startlib,
versionscript::{self, VersionScript},
};
#[cfg(not(miri))]
#[global_allocator]
static ALLOC: mimalloc::MiMalloc = mimalloc::MiMalloc;
fn main() -> ExitCode {
let raw: Vec<OsString> = env::args_os().skip(1).collect();
let args = match expand_response_files(&raw) {
Ok(args) => args,
Err(msg) => {
eprintln!("{msg}");
return ExitCode::FAILURE;
}
};
match parse(&args) {
Ok(Request::Print(text)) => {
print!("{text}");
ExitCode::SUCCESS
}
Ok(Request::Link(opts)) => link_detached(&opts),
Err(msg) => {
eprintln!("{msg}");
ExitCode::FAILURE
}
}
}
fn link_detached(opts: &Options) -> ExitCode {
if !opts.no_fork && !opts.macho_dynamic {
xold::detach::fork_child();
}
match with_threads(opts, || run(opts)) {
Ok(()) => ExitCode::SUCCESS,
Err(err) => {
eprintln!("xold: {err}");
ExitCode::FAILURE
}
}
}
fn with_threads<T: Send>(opts: &Options, job: impl FnOnce() -> T + Send) -> T {
let Some(threads) = opts.threads else {
return job();
};
match rayon::ThreadPoolBuilder::new().num_threads(threads).build() {
Ok(pool) => pool.install(job),
Err(_) => job(),
}
}
fn run(opts: &Options) -> Result<()> {
let mut list = InputList::default();
list.collect(opts)?;
let inputs = list.scan;
check_command_line(opts, inputs)?;
let files = list.views();
if inputs.macho {
refuse_elf_only(opts, "Mach-O")?;
if opts.shared {
return Err(unsupported("-shared".into(), "Mach-O"));
}
let entry = default_entry(opts, b"_main");
let dylibs: Vec<Dylib<'_>> = list
.dylibs
.iter()
.map(|dylib| Dylib {
install_name: &dylib.install_name,
exports: &dylib.exports,
})
.collect();
link_macho_with_options(
&files,
&opts.output,
entry,
&MachOLinkOptions {
arch: opts.macho_arch,
dynamic: opts.macho_dynamic,
platform: opts.macho_platform,
dead_strip: opts.macho_dead_strip,
dylibs: &dylibs,
},
)?;
return ad_hoc_sign(&opts.output);
}
if inputs.coff {
refuse_elf_only(opts, "COFF")?;
let entry = default_entry(opts, b"main");
return link_coff(&files, &opts.output, entry, opts.shared);
}
let shared = inputs.shared;
let script = read_version_script(opts)?;
let undefined: Vec<&[u8]> =
opts.undefined.iter().map(String::as_bytes).collect();
link_image(&Link {
inputs: &files,
output: &opts.output,
mode: mode_of(opts, shared),
entry: if opts.shared && !opts.entry_given {
b""
} else {
opts.entry.as_bytes()
},
soname: opts.soname.as_deref().map(str::as_bytes),
interpreter: (opts.dynamic_exec || shared)
.then_some(opts.interpreter.as_bytes()),
gc: opts.gc_sections,
export_dynamic: opts.export_dynamic,
icf: opts.icf,
relax: opts.relax,
no_undefined: opts.no_undefined,
z: opts.z,
pie: opts.pie,
strip: opts.strip,
hash_style: opts.hash_style,
rpath: opts.rpath.as_bytes(),
build_id: (opts.build_id != BuildId::None).then_some(&opts.build_id),
undefined: &undefined,
version_script: script.as_ref(),
undefined_version: opts.undefined_version,
})
}
fn read_version_script(opts: &Options) -> Result<Option<VersionScript>> {
let Some(path) = opts.version_script.as_deref() else {
return Ok(None);
};
let text = std::fs::read_to_string(path)?;
versionscript::parse(path, &text).map(Some)
}
fn check_command_line(opts: &Options, scan: InputScan) -> Result<()> {
if opts.static_link && scan.shared {
return Err(Error::CommandLine(
"-static was given, but an input is a shared object: a static \
image cannot depend on one"
.into(),
));
}
if opts.pie == Some(true) && opts.static_link {
return Err(Error::CommandLine(
"-pie and -static ask for different images: a static image has \
no loader to place it at a random base"
.into(),
));
}
if opts.static_link && opts.shared {
return Err(Error::CommandLine(
"-static and -shared ask for different images".into(),
));
}
let Some(name) = opts.emulation.as_deref() else {
return Ok(());
};
emulation::check(name, machine_of(scan)).map_err(Error::CommandLine)
}
const fn machine_of(scan: InputScan) -> emulation::Machine {
if scan.macho {
emulation::Machine::MachO
} else if scan.coff {
emulation::Machine::Coff
} else if let Some(machine) = scan.machine {
emulation::Machine::Elf(machine)
} else {
emulation::Machine::Unknown
}
}
fn refuse_elf_only(opts: &Options, format: &'static str) -> Result<()> {
if opts.gc_sections {
return Err(unsupported("--gc-sections".into(), format));
}
if opts.icf != IcfMode::None {
return Err(unsupported("--icf".into(), format));
}
if opts.relax && opts.relax_named {
return Err(unsupported("--relax".into(), format));
}
if opts.soname.is_some() {
return Err(unsupported("-soname".into(), format));
}
if opts.pie.is_some() {
return Err(unsupported("-pie/-no-pie".into(), format));
}
Ok(())
}
fn unsupported(option: String, format: &'static str) -> Error {
Error::unsupported_option(option, format)
}
const fn mode_of(opts: &Options, has_shared_input: bool) -> LinkMode {
if opts.static_link {
LinkMode::Static
} else if opts.shared {
LinkMode::Shared
} else if opts.dynamic_exec || has_shared_input {
LinkMode::DynExec
} else {
LinkMode::Static
}
}
fn read_group(paths: &[PathBuf]) -> Result<Vec<(PathBuf, Vec<u8>)>> {
paths
.iter()
.map(|path| Ok((path.clone(), std::fs::read(path)?)))
.collect()
}
fn default_entry<'a>(opts: &'a Options, platform: &'a [u8]) -> &'a [u8] {
if opts.entry.is_empty() || opts.entry == "_start" {
platform
} else {
opts.entry.as_bytes()
}
}
#[derive(Clone, Copy, Default)]
struct InputScan {
macho: bool,
coff: bool,
shared: bool,
machine: Option<u16>,
}
const HEAD_LEN: usize = 64;
const SCRIPT_DEPTH: usize = 8;
enum Entry {
File(Resolved),
Group { name: PathBuf, bytes: Vec<u8> },
}
struct Resolved {
path: PathBuf,
from_l: bool,
as_needed: bool,
}
#[derive(Default)]
struct InputList {
files: Vec<Entry>,
scan: InputScan,
dylibs: Vec<DylibInput>,
}
struct DylibInput {
install_name: String,
exports: Vec<String>,
}
impl InputList {
fn collect(&mut self, opts: &Options) -> Result<()> {
let search = Search {
paths: &opts.search,
sysroot: opts.sysroot.as_deref(),
};
let mut i = 0;
while let Some(path) = opts.inputs.get(i) {
if let Some(&(_, end)) =
opts.lib_groups.iter().find(|(start, _)| *start == i)
{
let members = read_group(&opts.inputs[i..end])?;
let bytes = startlib::archive(&members)?;
self.add_group(&members, &bytes);
i = end;
continue;
}
let file = Resolved {
path: path.clone(),
from_l: opts.from_l.get(i).copied().unwrap_or(false),
as_needed: opts.as_needed.get(i).copied().unwrap_or(false),
};
self.add(file, &search, SCRIPT_DEPTH)?;
i += 1;
}
Ok(())
}
fn views(&self) -> Vec<Input<'_>> {
self.files
.iter()
.map(|f| match f {
Entry::File(f) => match (f.as_needed, f.from_l) {
(true, from_l) => Input::AsNeeded {
path: &f.path,
from_l,
},
(false, true) => Input::Library(&f.path),
(false, false) => Input::Path(&f.path),
},
Entry::Group { name, bytes } => Input::Memory { name, bytes },
})
.collect()
}
fn add(
&mut self,
file: Resolved,
search: &Search<'_>,
depth: usize,
) -> Result<()> {
if file.path.extension().and_then(|ext| ext.to_str()) == Some("dylib") {
let install_name = search
.sysroot
.and_then(|root| file.path.strip_prefix(root).ok())
.map_or_else(
|| file.path.to_string_lossy().into_owned(),
|path| format!("/{}", path.display()),
);
if !self
.dylibs
.iter()
.any(|dylib| dylib.install_name == install_name)
{
self.dylibs.push(DylibInput {
install_name,
exports: Vec::new(),
});
}
return Ok(());
}
if file.path.extension().and_then(|ext| ext.to_str()) == Some("tbd") {
let stub = xold::macho::tapi::parse(&std::fs::read_to_string(
&file.path,
)?)?;
if let Some(existing) = self
.dylibs
.iter_mut()
.find(|dylib| dylib.install_name == stub.install_name)
{
existing.exports.extend(stub.exports);
existing.exports.sort_unstable();
existing.exports.dedup();
} else {
self.dylibs.push(DylibInput {
install_name: stub.install_name,
exports: stub.exports,
});
}
return Ok(());
}
let mut buf = [0u8; HEAD_LEN];
let head = read_head(&file.path, &mut buf)
.and_then(|n| buf.get(..n))
.unwrap_or(&[]);
match Format::detect(head) {
Some(Format::MachO) => self.scan.macho = true,
Some(Format::Coff) => self.scan.coff = true,
Some(Format::Elf) => {
self.scan.shared |= is_elf_shared(head);
if self.scan.machine.is_none() {
self.scan.machine = elf_machine(head);
}
}
Some(Format::Pe) => {}
None => {
if depth > 0 && script::looks_like(head) {
return self.expand(&file, search, depth);
}
}
}
self.files.push(Entry::File(file));
Ok(())
}
fn add_group(&mut self, members: &[(PathBuf, Vec<u8>)], bytes: &[u8]) {
let name = members.first().map_or_else(
|| PathBuf::from("--start-lib"),
|(p, _)| {
let mut dir = p.clone();
dir.set_file_name("--start-lib group");
dir
},
);
self.files.push(Entry::Group {
name,
bytes: bytes.to_vec(),
});
}
fn expand(
&mut self,
file: &Resolved,
search: &Search<'_>,
depth: usize,
) -> Result<()> {
let text = std::fs::read_to_string(&file.path)?;
let mut names = Vec::new();
script::parse(&file.path, &text, &mut names)?;
for name in &names {
let path = search
.resolve(name, &file.path)
.ok_or_else(|| script::not_found(&file.path, name))?;
self.add(
Resolved {
path,
from_l: name.library,
as_needed: name.as_needed || file.as_needed,
},
search,
depth.saturating_sub(1),
)?;
}
Ok(())
}
}
fn ad_hoc_sign(path: &Path) -> Result<()> {
#[cfg(target_os = "macos")]
{
let status = std::process::Command::new("/usr/bin/codesign")
.args(["--force", "--sign", "-"])
.arg(path)
.status()?;
if !status.success() {
return Err(Error::CommandLine(format!(
"ad-hoc codesigning failed for {}",
path.display()
)));
}
}
let _ = path;
Ok(())
}
fn read_head(path: &Path, head: &mut [u8; HEAD_LEN]) -> Option<usize> {
use std::io::Read;
let mut file = std::fs::File::open(path).ok()?;
let mut filled = 0;
while filled < head.len() {
match file.read(&mut head[filled..]) {
Ok(0) => break,
Ok(n) => filled = filled.saturating_add(n),
Err(_) => return None,
}
}
Some(filled)
}
fn elf_machine(head: &[u8]) -> Option<u16> {
head.get(18..20)
.and_then(|b| <[u8; 2]>::try_from(b).ok())
.map(u16::from_le_bytes)
}
fn is_elf_shared(head: &[u8]) -> bool {
head.get(16..18)
.and_then(|b| <[u8; 2]>::try_from(b).ok())
.is_some_and(|b| u16::from_le_bytes(b) == ET_DYN)
}