Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement sysroot handling #419

Merged
merged 1 commit into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions libwild/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ blake3 = { version = "1.5.5", features = ["rayon"] }
uuid = { version = "1.13.1", features = ["v4"] }
hex = "0.4.3"
atomic-take = "1.1.0"
normalize-path = "0.2.1"

[dev-dependencies]
ar = "0.9.0"
Expand Down
28 changes: 24 additions & 4 deletions libwild/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::alignment::Alignment;
use crate::arch::Architecture;
use crate::error::Result;
use crate::input_data::FileId;
use crate::linker_script::maybe_forced_sysroot;
use crate::save_dir::SaveDir;
use anyhow::bail;
use anyhow::ensure;
Expand Down Expand Up @@ -56,6 +57,7 @@ pub(crate) struct Args {
pub(crate) file_write_mode: FileWriteMode,
pub(crate) no_undefined: bool,
pub(crate) allow_copy_relocations: bool,
pub(crate) sysroot: Option<Box<Path>>,

/// If set, GC stats will be written to the specified filename.
pub(crate) write_gc_stats: Option<PathBuf>,
Expand Down Expand Up @@ -240,6 +242,7 @@ impl Default for Args {
files_per_group: None,
no_undefined: false,
should_print_version: false,
sysroot: None,
}
}
}
Expand Down Expand Up @@ -302,13 +305,19 @@ pub(crate) fn parse<S: AsRef<str>, I: Iterator<Item = S>>(mut input: I) -> Resul
};

if let Some(rest) = arg.strip_prefix("-L") {
let handle_sysroot = |path| {
args.sysroot
.as_ref()
.and_then(|sysroot| maybe_forced_sysroot(path, sysroot))
.unwrap_or_else(|| Box::from(path))
};
if rest.is_empty() {
if let Some(next) = input.next() {
args.lib_search_path
.push(Box::from(Path::new(next.as_ref())));
.push(handle_sysroot(Path::new(next.as_ref())));
}
} else {
args.lib_search_path.push(Box::from(Path::new(rest)));
args.lib_search_path.push(handle_sysroot(Path::new(rest)));
}
} else if let Some(rest) = arg.strip_prefix("-l") {
args.inputs.push(Input {
Expand Down Expand Up @@ -507,8 +516,14 @@ pub(crate) fn parse<S: AsRef<str>, I: Iterator<Item = S>>(mut input: I) -> Resul
} else if strip_option(arg)
.is_some_and(|stripped_arg| SILENTLY_IGNORED_FLAGS.contains(&stripped_arg))
{
} else if long_arg_split_prefix("sysroot=").is_some() {
warn_unsupported("--sysroot")?;
} else if let Some(sysroot) = long_arg_split_prefix("sysroot=") {
let sysroot = Path::new(sysroot);
args.sysroot = Some(Box::from(sysroot));
for path in &mut args.lib_search_path {
if let Some(new_path) = maybe_forced_sysroot(path, sysroot) {
*path = new_path;
}
}
} else if arg.starts_with('-') {
unrecognised.push(format!("`{arg}`"));
} else {
Expand Down Expand Up @@ -889,6 +904,7 @@ mod tests {
"-X",
"-EL",
"-v",
"--sysroot=/usr/aarch64-linux-gnu",
];

#[track_caller]
Expand Down Expand Up @@ -923,6 +939,10 @@ mod tests {
assert_eq!(args.soname, Some("bar".to_owned()));
assert_eq!(args.num_threads, NonZeroUsize::new(1).unwrap());
assert!(args.should_print_version);
assert_eq!(
args.sysroot,
Some(Box::from(Path::new("/usr/aarch64-linux-gnu")))
);
}

#[test]
Expand Down
7 changes: 4 additions & 3 deletions libwild/src/input_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ impl<'config> InputData<'config> {
};

for input in &config.inputs {
input_data.register_input(input)?;
input_data.register_input(input, config.sysroot.as_deref())?;
}

// Our last "file", similar to the prelude is responsible for internal stuff, but this time
Expand All @@ -114,7 +114,7 @@ impl<'config> InputData<'config> {
Ok(input_data)
}

fn register_input(&mut self, input: &Input) -> Result {
fn register_input(&mut self, input: &Input, sysroot: Option<&Path>) -> Result {
let paths = input.path(self.config)?;
let absolute_path = &paths.absolute;
if !self.filenames.insert(absolute_path.clone()) {
Expand Down Expand Up @@ -154,8 +154,9 @@ impl<'config> InputData<'config> {
&bytes,
absolute_path,
input.modifiers,
sysroot,
)? {
self.register_input(&input)?;
self.register_input(&input, sysroot)?;
}
return Ok(());
}
Expand Down
68 changes: 68 additions & 0 deletions libwild/src/linker_script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::symbol::SymbolName;
use anyhow::anyhow;
use anyhow::bail;
use anyhow::Context;
use normalize_path::NormalizePath;
use std::collections::HashSet;
use std::path::Path;

Expand All @@ -23,6 +24,7 @@ pub(crate) fn linker_script_to_inputs(
bytes: &[u8],
path: &Path,
modifiers: Modifiers,
sysroot: Option<&Path>,
) -> Result<Vec<Input>> {
let text = std::str::from_utf8(bytes)?;
let directory = path
Expand All @@ -33,11 +35,36 @@ pub(crate) fn linker_script_to_inputs(
.into_iter()
.map(|mut input| {
input.search_first = Some(directory.to_owned());
if let (Some(sysroot), InputSpec::File(file)) = (sysroot, &mut input.spec) {
if let Some(new_file) = maybe_apply_sysroot(path, file, sysroot) {
*file = new_file;
}
}

input
})
.collect())
}

fn maybe_apply_sysroot(
linker_script_path: &Path,
input_path: &Path,
sysroot: &Path,
) -> Option<Box<Path>> {
if linker_script_path.normalize().starts_with(sysroot) {
Some(Box::from(sysroot.join(input_path.strip_prefix("/").ok()?)))
} else {
maybe_forced_sysroot(input_path, sysroot)
}
}

pub(crate) fn maybe_forced_sysroot(path: &Path, sysroot: &Path) -> Option<Box<Path>> {
path.strip_prefix("=")
.or_else(|_| path.strip_prefix("$SYSROOT"))
.ok()
.map(|stripped| Box::from(sysroot.join(stripped)))
}

/// A version script. See https://sourceware.org/binutils/docs/ld/VERSION.html
#[derive(Default)]
pub(crate) struct VersionScript<'data> {
Expand Down Expand Up @@ -461,4 +488,45 @@ mod tests {
);
assert!(version.locals.matches_all);
}

#[test]
fn test_sysroot_application() {
let sysroot = Path::new("/usr/aarch64-linux-gnu");
// Linker script is located in the sysroot
assert_equal(
maybe_apply_sysroot(
&sysroot.join("lib/libc.so"),
Path::new("/lib/libc.so.6"),
sysroot,
),
Some(Box::from(sysroot.join("lib/libc.so.6"))),
);
// Linker script is not located in the sysroot
assert_equal(
maybe_apply_sysroot(
Path::new("/lib/libc.so"),
Path::new("/lib/libc.so.6"),
sysroot,
),
None,
);
// Sysroot enforced by `=`
assert_equal(
maybe_apply_sysroot(
Path::new("/lib/libc.so"),
Path::new("=/lib/libc.so.6"),
sysroot,
),
Some(Box::from(sysroot.join("lib/libc.so.6"))),
);
// Sysroot enforced by `$SYSROOT`
assert_equal(
maybe_apply_sysroot(
Path::new("/lib/libc.so"),
Path::new("$SYSROOT/lib/libc.so.6"),
sysroot,
),
Some(Box::from(sysroot.join("lib/libc.so.6"))),
);
}
}
Loading