[lld] [lld] Fix ILP32 ABI checks for bitcode files. (PR #116537)
Alex Rønne Petersen via llvm-commits
llvm-commits at lists.llvm.org
Sun Nov 17 01:51:56 PST 2024
https://github.com/alexrp created https://github.com/llvm/llvm-project/pull/116537
Previously, using LTO with the MIPS N32 ABI and (e.g.) `-m elf32btsmipn32` would fail because `isN32Abi()` made no effort to check whether a bitcode file is using the N32 ABI. Additionally, `getBitcodeELFKind()` would incorrectly pick 64-bit ELF for all ILP32 ABIs (not just N32).
There's probably still more work to be done for LTO to fully work for these ABIs, but this should hopefully be an uncontroversial step in the right direction.
Note: I don't have commit access.
>From 36c92b8e735ab77abce9c3fed7b652604ee30d48 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= <alex at alexrp.com>
Date: Sun, 17 Nov 2024 08:54:47 +0100
Subject: [PATCH] [lld] Fix ILP32 ABI checks for bitcode files.
Previously, using LTO with the MIPS N32 ABI and (e.g.) -m elf32btsmipn32 would
fail because isN32Abi() made no effort to check whether a bitcode file is using
the N32 ABI. Additionally, getBitcodeELFKind() would incorrectly pick 64-bit ELF
for all ILP32 ABIs (not just N32).
---
lld/ELF/Arch/MipsArchTree.cpp | 2 ++
lld/ELF/InputFiles.cpp | 15 ++++++++-------
lld/ELF/InputFiles.h | 3 +++
3 files changed, 13 insertions(+), 7 deletions(-)
diff --git a/lld/ELF/Arch/MipsArchTree.cpp b/lld/ELF/Arch/MipsArchTree.cpp
index 0c64a46fe85d08..e7d3d591e3b27b 100644
--- a/lld/ELF/Arch/MipsArchTree.cpp
+++ b/lld/ELF/Arch/MipsArchTree.cpp
@@ -365,6 +365,8 @@ uint8_t elf::getMipsFpAbiFlag(Ctx &ctx, uint8_t oldFlag, uint8_t newFlag,
template <class ELFT> static bool isN32Abi(const InputFile &f) {
if (auto *ef = dyn_cast<ELFFileBase>(&f))
return ef->template getObj<ELFT>().getHeader().e_flags & EF_MIPS_ABI2;
+ if (auto *bc = dyn_cast<BitcodeFile>(&f))
+ return bc->triple.isABIN32();
return false;
}
diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp
index 769081fa71a449..76d4c33ecb4f5e 100644
--- a/lld/ELF/InputFiles.cpp
+++ b/lld/ELF/InputFiles.cpp
@@ -1636,9 +1636,9 @@ template <class ELFT> void SharedFile::parse() {
}
static ELFKind getBitcodeELFKind(const Triple &t) {
- if (t.isLittleEndian())
- return t.isArch64Bit() ? ELF64LEKind : ELF32LEKind;
- return t.isArch64Bit() ? ELF64BEKind : ELF32BEKind;
+ if (t.isArch64Bit() && !t.isABIN32() && !t.isX32() && t.getEnvironment() != Triple::GNUILP32)
+ return t.isLittleEndian() ? ELF64LEKind : ELF64BEKind;
+ return t.isLittleEndian() ? ELF32LEKind : ELF32BEKind;
}
static uint16_t getBitcodeMachineKind(Ctx &ctx, StringRef path,
@@ -1731,10 +1731,11 @@ BitcodeFile::BitcodeFile(Ctx &ctx, MemoryBufferRef mb, StringRef archiveName,
obj = CHECK(lto::InputFile::create(mbref), this);
- Triple t(obj->getTargetTriple());
- ekind = getBitcodeELFKind(t);
- emachine = getBitcodeMachineKind(ctx, mb.getBufferIdentifier(), t);
- osabi = getOsAbi(t);
+ this->triple = Triple(obj->getTargetTriple());
+
+ ekind = getBitcodeELFKind(this->triple);
+ emachine = getBitcodeMachineKind(ctx, mb.getBufferIdentifier(), this->triple);
+ osabi = getOsAbi(this->triple);
}
static uint8_t mapVisibility(GlobalValue::VisibilityTypes gvVisibility) {
diff --git a/lld/ELF/InputFiles.h b/lld/ELF/InputFiles.h
index e00e2c83d017c2..33bc26cdf9e3a5 100644
--- a/lld/ELF/InputFiles.h
+++ b/lld/ELF/InputFiles.h
@@ -19,6 +19,7 @@
#include "llvm/Object/ELF.h"
#include "llvm/Support/MemoryBufferRef.h"
#include "llvm/Support/Threading.h"
+#include "llvm/TargetParser/Triple.h"
namespace llvm {
struct DILineInfo;
@@ -335,6 +336,8 @@ class BitcodeFile : public InputFile {
void postParse();
std::unique_ptr<llvm::lto::InputFile> obj;
std::vector<bool> keptComdats;
+
+ llvm::Triple triple;
};
// .so file.
More information about the llvm-commits
mailing list