Skip to content

Commit 9043aff

Browse files
committed
When running a 32-bit rustup on an aarch64 CPU, select a 32-bit toolchain
this mirrors a similar check that exists in rustup-init.sh fixes #3307
1 parent d4c6844 commit 9043aff

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

src/dist/dist.rs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use std::collections::HashSet;
22
use std::env;
33
use std::fmt;
4-
use std::io::Write;
4+
use std::fs;
5+
use std::io::{self, Read, Write};
56
use std::ops::Deref;
67
use std::path::Path;
78
use std::str::FromStr;
@@ -219,6 +220,26 @@ impl Deref for TargetTriple {
219220
}
220221
}
221222

223+
fn is_32bit_userspace() -> bool {
224+
// Check if /bin/sh is a 32-bit binary. If it doesn't exist, fall back to
225+
// checking if _we_ are a 32-bit binary.
226+
// rustup-init.sh also relies on checking /bin/sh for bitness.
227+
228+
// inner function is to simplify error handling.
229+
fn inner() -> io::Result<bool> {
230+
let mut f = fs::File::open("/bin/sh")?;
231+
let mut buf = [0; 5];
232+
f.read_exact(&mut buf)?;
233+
234+
// ELF files start out "\x7fELF", and the following byte is
235+
// 0x01 for 32-bit and
236+
// 0x02 for 64-bit.
237+
Ok(&buf == b"\x7fELF\x01")
238+
}
239+
240+
inner().unwrap_or(cfg!(target_pointer_width = "32"))
241+
}
242+
222243
impl TargetTriple {
223244
pub fn new(name: &str) -> Self {
224245
Self(name.to_string())
@@ -346,7 +367,13 @@ impl TargetTriple {
346367
(b"Linux", b"arm") => Some("arm-unknown-linux-gnueabi"),
347368
(b"Linux", b"armv7l") => Some("armv7-unknown-linux-gnueabihf"),
348369
(b"Linux", b"armv8l") => Some("armv7-unknown-linux-gnueabihf"),
349-
(b"Linux", b"aarch64") => Some(TRIPLE_AARCH64_UNKNOWN_LINUX),
370+
(b"Linux", b"aarch64") => {
371+
if is_32bit_userspace() {
372+
Some("armv7-unknown-linux-gnueabihf")
373+
} else {
374+
Some(TRIPLE_AARCH64_UNKNOWN_LINUX)
375+
}
376+
}
350377
(b"Darwin", b"x86_64") => Some("x86_64-apple-darwin"),
351378
(b"Darwin", b"i686") => Some("i686-apple-darwin"),
352379
(b"FreeBSD", b"x86_64") => Some("x86_64-unknown-freebsd"),

0 commit comments

Comments
 (0)