mod cli;
use std::{
env,
ffi::OsString,
path::{Path, PathBuf},
process::ExitCode,
};
use cli::{Options, Request, emulation, expand_response_files, parse};
#[cfg(feature = "lto")]
use xold::lto;
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 {
xold::pool::allow_huge_page_tuning();
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)?;
if inputs.bitcode {
return link_bitcode(opts, &list);
}
let files = list.views();
if inputs.macho {
return link_macho_image(opts, &list, &files);
}
if inputs.coff {
refuse_macho_output_options(opts, "COFF")?;
refuse_elf_only(opts, "COFF")?;
let entry = default_entry(opts, b"main");
return link_coff(&files, &opts.output, entry, opts.shared);
}
link_elf(opts, &files, inputs.shared)
}
fn link_elf(opts: &Options, files: &[Input<'_>], shared: bool) -> Result<()> {
refuse_macho_output_options(opts, "ELF")?;
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_macho_exported_symbols(opts: &Options) -> Result<Option<Vec<String>>> {
let Some(path) = opts.macho_exported_symbols.as_deref() else {
return Ok(None);
};
let text = std::fs::read_to_string(path)?;
let mut names = Vec::new();
for line in text.lines() {
let name = line.trim();
if name.is_empty() {
continue;
}
if name.bytes().any(|b| matches!(b, b'*' | b'?' | b'[')) {
return Err(Error::CommandLine(format!(
"wildcards in -exported_symbols_list are not implemented: \
`{name}`"
)));
}
names.push(name.to_string());
}
names.sort_unstable();
names.dedup();
Ok(Some(names))
}
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 link_macho_image(
opts: &Options,
list: &InputList,
files: &[Input<'_>],
) -> Result<()> {
refuse_elf_only(opts, "Mach-O")?;
if opts.shared {
return Err(unsupported("-shared".into(), "Mach-O"));
}
if opts.macho_install_name.is_some() && !opts.macho_dylib {
return Err(unsupported("-install_name".into(), "MH_EXECUTE"));
}
if opts.macho_exported_symbols.is_some() && !opts.macho_dynamic {
return Err(unsupported(
"-exported_symbols_list".into(),
"static Mach-O",
));
}
let entry = if opts.macho_dylib {
b"".as_slice()
} else {
default_entry(opts, b"_main")
};
let install_name = if opts.macho_dylib {
Some(opts.macho_install_name.as_deref().map_or_else(
|| {
opts.output.to_str().ok_or_else(|| {
Error::CommandLine(
"Mach-O dylib output path is not valid UTF-8; use \
-install_name to give LC_ID_DYLIB a name"
.into(),
)
})
},
Ok,
)?)
} else {
None
};
let exported_symbols = read_macho_exported_symbols(opts)?;
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,
dylib: opts.macho_dylib,
install_name,
exported_symbols: exported_symbols.as_deref(),
platform: opts.macho_platform,
dead_strip: opts.macho_dead_strip,
dylibs: &dylibs,
},
)?;
ad_hoc_sign(&opts.output)
}
#[cfg(feature = "lto")]
const DEFAULT_ENTRIES: [&[u8]; 3] = [b"_start", b"main", b"_main"];
#[cfg(not(feature = "lto"))]
const fn link_bitcode(_opts: &Options, _list: &InputList) -> Result<()> {
Err(Error::Format(
"input holds LLVM bitcode, and this build of xold was compiled \
without the `lto` feature that loads a plugin to compile it: \
rebuild xold with the feature, or build the input with -fno-lto",
))
}
#[cfg(feature = "lto")]
fn link_bitcode(opts: &Options, list: &InputList) -> Result<()> {
let inputs: Vec<PathBuf> = list.paths().map(Path::to_path_buf).collect();
let kind = if opts.shared {
lto::Output::Shared
} else if opts.pie == Some(true) {
lto::Output::Pie
} else {
lto::Output::Executable
};
let mut pinned: Vec<&[u8]> =
opts.undefined.iter().map(String::as_bytes).collect();
if opts.entry_given {
pinned.push(opts.entry.as_bytes());
} else if !opts.shared {
pinned.extend_from_slice(&DEFAULT_ENTRIES);
}
let groups: Vec<(&Path, &[u8])> = list.groups().collect();
let compiled = lto::compile(<o::Job {
named_plugin: opts.lto_plugin.as_deref(),
options: &opts.lto_plugin_opts,
output: &opts.output,
kind,
inputs: &inputs,
pinned: &pinned,
groups: &groups,
export_all: opts.shared || opts.export_dynamic,
})?;
let files = list.views_with_lto(&compiled.objects);
let result = link_compiled(opts, &files, list, &compiled.objects);
let swept = compiled.finish();
result.and(swept)
}
#[cfg(feature = "lto")]
fn link_compiled(
opts: &Options,
files: &[Input<'_>],
list: &InputList,
objects: &[PathBuf],
) -> Result<()> {
let mut head = [0u8; HEAD_LEN];
let format = objects
.first()
.and_then(|path| read_head(path, &mut head))
.and_then(|n| head.get(..n))
.and_then(Format::detect);
match format {
Some(Format::Coff) => {
refuse_macho_output_options(opts, "COFF")?;
refuse_elf_only(opts, "COFF")?;
let entry = default_entry(opts, b"main");
link_coff(files, &opts.output, entry, opts.shared)
}
Some(Format::MachO) => link_macho_image(opts, list, files),
_ => link_elf(opts, files, list.scan.shared),
}
}
fn refuse_macho_output_options(
opts: &Options,
format: &'static str,
) -> Result<()> {
let option = if opts.macho_dylib {
Some("-dylib")
} else if opts.macho_install_name.is_some() {
Some("-install_name")
} else if opts.macho_exported_symbols.is_some() {
Some("-exported_symbols_list")
} else {
None
};
option.map_or(Ok(()), |name| Err(unsupported(name.into(), format)))
}
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()
}
}
#[allow(clippy::struct_excessive_bools)]
#[derive(Clone, Copy, Default)]
struct InputScan {
macho: bool,
coff: bool,
shared: bool,
machine: Option<u16>,
bitcode: bool,
}
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,
bitcode: 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),
bitcode: false,
};
self.add(file, &search, SCRIPT_DEPTH)?;
i += 1;
}
Ok(())
}
#[cfg(feature = "lto")]
fn paths(&self) -> impl Iterator<Item = &Path> {
self.files.iter().filter_map(|entry| match entry {
Entry::File(file) => Some(&*file.path),
Entry::Group { .. } => None,
})
}
#[cfg(feature = "lto")]
fn groups(&self) -> impl Iterator<Item = (&Path, &[u8])> {
self.files.iter().filter_map(|entry| match entry {
Entry::Group { name, bytes } => {
Some((name.as_path(), bytes.as_slice()))
}
Entry::File(_) => None,
})
}
#[cfg(feature = "lto")]
fn views_with_lto<'a>(&'a self, objects: &'a [PathBuf]) -> Vec<Input<'a>> {
let mut out = Vec::with_capacity(self.files.len() + objects.len());
let mut spliced = false;
for entry in &self.files {
match entry {
Entry::File(file) if file.bitcode => {
if !spliced {
spliced = true;
out.extend(
objects.iter().map(|o| Input::Path(o.as_path())),
);
}
}
Entry::File(file) => out.push(Self::view_of(file)),
Entry::Group { name, bytes } => {
out.push(Input::Memory { name, bytes });
}
}
}
out
}
#[cfg(feature = "lto")]
fn view_of(file: &Resolved) -> Input<'_> {
match (file.as_needed, file.from_l) {
(true, from_l) => Input::AsNeeded {
path: &file.path,
from_l,
},
(false, true) => Input::Library(&file.path),
(false, false) => Input::Path(&file.path),
}
}
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,
mut 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::Bitcode) => {
self.scan.bitcode = true;
file.bitcode = true;
}
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,
bitcode: false,
},
search,
depth.saturating_sub(1),
)?;
}
Ok(())
}
}
#[cfg_attr(
not(target_os = "macos"),
allow(clippy::unnecessary_wraps, clippy::missing_const_for_fn)
)]
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)
}