Skip to content

Commit 67fcef8

Browse files
author
Serban Iorga
committed
fix libfdt static linking
Signed-off-by: Serban Iorga <[email protected]>
1 parent 1e6bdf2 commit 67fcef8

File tree

4 files changed

+49
-9
lines changed

4 files changed

+49
-9
lines changed

.cargo/config

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,5 @@
22
target = "x86_64-unknown-linux-musl"
33
target-dir = "build/cargo_target"
44

5-
[target.'cfg(any(target_arch="arm", target_arch="aarch64"))']
6-
# On aarch64 musl depends on some libgcc functions (i.e `__addtf3` and other `*tf3` functions) for logic that uses
7-
# long double. Such functions are not builtin in the rust compiler, so we need to get them from libgcc.
8-
# No need for the `crt_static` flag as rustc appends it by default.
9-
rustflags = [
10-
"-C", "link-arg=-lgcc",
11-
"-C", "link-arg=-lfdt",
12-
]
13-
145
[net]
156
git-fetch-with-cli = true

src/libfdt-bindings/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ name = "libfdt-bindings"
33
version = "0.1.0"
44
authors = ["Amazon Firecracker team <[email protected]>"]
55
edition = "2018"
6+
build = "build.rs"
67

78
[target.'cfg(target_arch="aarch64")'.dependencies]
89
libc = ">=0.2.39"

src/libfdt-bindings/build.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
use std::process::Command;
5+
6+
/// Get the ld linker search paths
7+
///
8+
/// Cargo overwrites LD_LIBRARY_PATH with rust specific paths. But we need the default system
9+
/// paths in order to find libfdt. So we query `ld` in order to get them.
10+
fn get_ld_search_dirs() -> Vec<String> {
11+
// We need to extract from `ld --verbose` all the search paths.
12+
// For example `ld --verbose | grep SEARCH_DIR | tr -s ' ;' '\n'` returns the following:
13+
// ```
14+
// SEARCH_DIR("=/usr/local/lib/aarch64-linux-gnu")
15+
// SEARCH_DIR("=/lib/aarch64-linux-gnu")
16+
// SEARCH_DIR("=/usr/lib/aarch64-linux-gnu")
17+
// SEARCH_DIR("=/usr/local/lib")
18+
// SEARCH_DIR("=/lib")
19+
// SEARCH_DIR("=/usr/lib")
20+
// SEARCH_DIR("=/usr/aarch64-linux-gnu/lib")
21+
// ```
22+
let cmd = r#"
23+
ld --verbose | grep -oP '(?<=SEARCH_DIR\(\"=)[^"]+(?=\"\);)'
24+
"#;
25+
26+
Command::new("sh")
27+
.arg("-c")
28+
.arg(cmd)
29+
.output()
30+
.ok()
31+
.and_then(|output| {
32+
if output.status.success() {
33+
return Some(output.stdout);
34+
}
35+
None
36+
})
37+
.and_then(|stdout_bytes| String::from_utf8(stdout_bytes).ok())
38+
.map_or(vec![], |stdout| {
39+
stdout.lines().map(|item| item.to_string()).collect()
40+
})
41+
}
42+
43+
fn main() {
44+
for ld_search_dir in get_ld_search_dirs() {
45+
println!("cargo:rustc-link-search=native={}", ld_search_dir);
46+
}
47+
}

src/libfdt-bindings/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use libc::{c_char, c_int, c_void};
88
// flattened device tree (fdt) that is passed to the kernel and indicates
99
// the hardware configuration of the machine.
1010
#[cfg(target_arch = "aarch64")]
11+
#[link(name = "fdt", kind = "static")]
1112
extern "C" {
1213
pub fn fdt_create(buf: *mut c_void, bufsize: c_int) -> c_int;
1314
pub fn fdt_finish_reservemap(fdt: *mut c_void) -> c_int;

0 commit comments

Comments
 (0)