Skip to content

Commit 47468ec

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 47468ec

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

src/dist/dist.rs

Lines changed: 28 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,25 @@ 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+
fn inner() -> io::Result<bool> {
229+
let mut f = fs::File::open("/bin/sh")?;
230+
let mut buf = [0; 5];
231+
f.read_exact(&mut buf)?;
232+
233+
// ELF files start out "\x7fELF", and the following byte is
234+
// 0x01 for 32-bit and
235+
// 0x02 for 64-bit.
236+
Ok(&buf == b"\x7fELF\x01")
237+
}
238+
239+
inner().unwrap_or(cfg!(target_pointer_width = "32"))
240+
}
241+
222242
impl TargetTriple {
223243
pub fn new(name: &str) -> Self {
224244
Self(name.to_string())
@@ -346,7 +366,13 @@ impl TargetTriple {
346366
(b"Linux", b"arm") => Some("arm-unknown-linux-gnueabi"),
347367
(b"Linux", b"armv7l") => Some("armv7-unknown-linux-gnueabihf"),
348368
(b"Linux", b"armv8l") => Some("armv7-unknown-linux-gnueabihf"),
349-
(b"Linux", b"aarch64") => Some(TRIPLE_AARCH64_UNKNOWN_LINUX),
369+
(b"Linux", b"aarch64") => {
370+
if is_32bit_userspace() {
371+
Some("armv7-unknown-linux-gnueabihf")
372+
} else {
373+
Some(TRIPLE_AARCH64_UNKNOWN_LINUX)
374+
}
375+
}
350376
(b"Darwin", b"x86_64") => Some("x86_64-apple-darwin"),
351377
(b"Darwin", b"i686") => Some("i686-apple-darwin"),
352378
(b"FreeBSD", b"x86_64") => Some("x86_64-unknown-freebsd"),

0 commit comments

Comments
 (0)