Skip to content

Commit 57941af

Browse files
Apply suggestions from code review
Co-authored-by: Jubilee <[email protected]>
1 parent fa6d0d1 commit 57941af

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

compiler/rustc_codegen_ssa/src/back/metadata.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,8 @@ pub(super) fn elf_os_abi(sess: &Session) -> u8 {
271271
pub(super) fn elf_e_flags(architecture: Architecture, sess: &Session) -> u32 {
272272
match architecture {
273273
Architecture::Mips | Architecture::Mips64 | Architecture::Mips64_N32 => {
274+
// "N32" indicates an "ILP32" data model on a 64-bit MIPS CPU
275+
// like SPARC's "v8+", x86_64's "x32", or the watchOS "arm64_32".
274276
let is_32bit = architecture == Architecture::Mips;
275277
let mut e_flags = match sess.target.options.cpu.as_ref() {
276278
"mips1" if is_32bit => elf::EF_MIPS_ARCH_1,
@@ -293,7 +295,7 @@ pub(super) fn elf_e_flags(architecture: Architecture, sess: &Session) -> u32 {
293295
};
294296

295297
// If the ABI is explicitly given, use it, or default to O32 on 32-bit MIPS,
296-
// which is the only option.
298+
// which is the only "true" 32-bit option that LLVM supports.
297299
match sess.target.options.llvm_abiname.as_ref() {
298300
"o32" if is_32bit => e_flags |= elf::EF_MIPS_ABI_O32,
299301
"n32" if !is_32bit => e_flags |= elf::EF_MIPS_ABI2,
@@ -307,6 +309,15 @@ pub(super) fn elf_e_flags(architecture: Architecture, sess: &Session) -> u32 {
307309
};
308310

309311
if sess.target.options.relocation_model != RelocModel::Static {
312+
// PIC means position-independent code. CPIC means "calls PIC".
313+
// CPIC was mutually exclusive with PIC according to
314+
// the SVR4 MIPS ABI https://refspecs.linuxfoundation.org/elf/mipsabi.pdf
315+
// and should have only appeared on static objects with dynamically calls.
316+
// At some point someone (GCC?) decided to set CPIC even for PIC.
317+
// Nowadays various things expect both set on the same object file
318+
// and may even error if you mix CPIC and non-CPIC object files,
319+
// despite that being the entire point of the CPIC ABI extension!
320+
// As we are in Rome, we do as the Romans do.
310321
e_flags |= elf::EF_MIPS_PIC | elf::EF_MIPS_CPIC;
311322
}
312323
if sess.target.options.cpu.contains("r6") {

compiler/rustc_target/src/spec/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -3568,6 +3568,11 @@ impl Target {
35683568
"s390x" => (Architecture::S390x, None),
35693569
"mips" | "mips32r6" => (Architecture::Mips, None),
35703570
"mips64" | "mips64r6" => (
3571+
// While there are currently no builtin targets
3572+
// using the N32 ABI, it is possible to specify
3573+
// it using a custom target specification. N32
3574+
// is an ILP32 ABI like the Aarch64_Ilp32
3575+
// and X86_64_X32 cases above and below this one.
35713576
if self.options.llvm_abiname.as_ref() == "n32" {
35723577
Architecture::Mips64_N32
35733578
} else {

0 commit comments

Comments
 (0)