Skip to content

Commit 4542d01

Browse files
committed
Use more accurate ELF flags on MIPS
1 parent 3559e0a commit 4542d01

File tree

2 files changed

+25
-20
lines changed

2 files changed

+25
-20
lines changed

compiler/rustc_codegen_ssa/src/back/metadata.rs

+17-19
Original file line numberDiff line numberDiff line change
@@ -270,45 +270,43 @@ pub(super) fn elf_os_abi(sess: &Session) -> u8 {
270270

271271
pub(super) fn elf_e_flags(architecture: Architecture, sess: &Session) -> u32 {
272272
match architecture {
273-
Architecture::Mips => {
274-
let arch = match sess.target.options.cpu.as_ref() {
273+
Architecture::Mips | Architecture::Mips64 | Architecture::Mips64_N32 => {
274+
let mut e_flags = match sess.target.options.cpu.as_ref() {
275275
"mips1" => elf::EF_MIPS_ARCH_1,
276276
"mips2" => elf::EF_MIPS_ARCH_2,
277277
"mips3" => elf::EF_MIPS_ARCH_3,
278278
"mips4" => elf::EF_MIPS_ARCH_4,
279279
"mips5" => elf::EF_MIPS_ARCH_5,
280-
s if s.contains("r6") => elf::EF_MIPS_ARCH_32R6,
280+
"mips32r2" => elf::EF_MIPS_ARCH_32R2,
281+
"mips32r6" => elf::EF_MIPS_ARCH_32R6,
282+
"mips64r2" => elf::EF_MIPS_ARCH_64R2,
283+
"mips64r6" => elf::EF_MIPS_ARCH_64R6,
281284
_ => elf::EF_MIPS_ARCH_32R2,
282285
};
283286

284-
let mut e_flags = elf::EF_MIPS_CPIC | arch;
285-
286-
// If the ABI is explicitly given, use it or default to O32.
287+
// If the ABI is explicitly given, use it or default to O32 on 32-bit MIPS
288+
// and N64 (which needs no extra flag) on 64-bit MIPS.
287289
match sess.target.options.llvm_abiname.to_lowercase().as_str() {
288290
"n32" => e_flags |= elf::EF_MIPS_ABI2,
289291
"o32" => e_flags |= elf::EF_MIPS_ABI_O32,
290-
_ => e_flags |= elf::EF_MIPS_ABI_O32,
292+
_ => {
293+
if architecture == Architecture::Mips {
294+
e_flags |= elf::EF_MIPS_ABI_O32
295+
}
296+
}
291297
};
292298

299+
if !sess.target.options.features.contains("noabicalls") {
300+
e_flags |= elf::EF_MIPS_CPIC;
301+
}
293302
if sess.target.options.relocation_model != RelocModel::Static {
294-
e_flags |= elf::EF_MIPS_PIC;
303+
e_flags |= elf::EF_MIPS_PIC | elf::EF_MIPS_CPIC;
295304
}
296305
if sess.target.options.cpu.contains("r6") {
297306
e_flags |= elf::EF_MIPS_NAN2008;
298307
}
299308
e_flags
300309
}
301-
Architecture::Mips64 => {
302-
// copied from `mips64el-linux-gnuabi64-gcc foo.c -c`
303-
let e_flags = elf::EF_MIPS_CPIC
304-
| elf::EF_MIPS_PIC
305-
| if sess.target.options.cpu.contains("r6") {
306-
elf::EF_MIPS_ARCH_64R6 | elf::EF_MIPS_NAN2008
307-
} else {
308-
elf::EF_MIPS_ARCH_64R2
309-
};
310-
e_flags
311-
}
312310
Architecture::Riscv32 | Architecture::Riscv64 => {
313311
// Source: https://github.com/riscv-non-isa/riscv-elf-psabi-doc/blob/079772828bd10933d34121117a222b4cc0ee2200/riscv-elf.adoc
314312
let mut e_flags: u32 = 0x0;

compiler/rustc_target/src/spec/mod.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -3567,7 +3567,14 @@ impl Target {
35673567
"x86" => (Architecture::I386, None),
35683568
"s390x" => (Architecture::S390x, None),
35693569
"mips" | "mips32r6" => (Architecture::Mips, None),
3570-
"mips64" | "mips64r6" => (Architecture::Mips64, None),
3570+
"mips64" | "mips64r6" => (
3571+
if self.options.llvm_abiname.to_lowercase().as_str() == "n32" {
3572+
Architecture::Mips64_N32
3573+
} else {
3574+
Architecture::Mips64
3575+
},
3576+
None,
3577+
),
35713578
"x86_64" => (
35723579
if self.pointer_width == 32 {
35733580
Architecture::X86_64_X32

0 commit comments

Comments
 (0)