[lld] [llvm] [lld][ELF] Check ARM float ABI compatibility during linking (PR #205667)

Prabhu Rajasekaran via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 25 11:49:09 PDT 2026


https://github.com/Prabhuk updated https://github.com/llvm/llvm-project/pull/205667

>From 7b9f65352e681c41371d159454d7d8154da1743f Mon Sep 17 00:00:00 2001
From: prabhukr <prabhukr at google.com>
Date: Wed, 24 Jun 2026 13:32:15 -0700
Subject: [PATCH 1/5] [lld][ELF] Check ARM float ABI compatibility during
 linking

Relocatable object files for ARM do not store the float ABI in their ELF
header flags. Instead, they use build attributes in the `.ARM.attributes`
section. Previously, if an object file omitted the `Tag_ABI_VFP_args` attribute
(which defaults to `BaseAAPCS` / soft-float), LLD skipped the compatibility check
for that file. This allowed `softfp` and `hard-float` object files to be linked
together without error, leading to silent runtime crashes.

This patch:
- Parses and tracks both the `Tag_ABI_VFP_args` (float ABI calling convention)
  and `Tag_FP_arch` (floating-point hardware usage) attributes per object file.
- Rewrites `ARM::calcEFlags()` to verify that all code-containing object files
  have compatible float ABIs.
- Treats files with no FP hardware usage and no explicit float ABI tag as
  neutral/compatible, avoiding false positives on pure integer assembly or
  library helpers.
- Falls back to the detected target float ABI when generating output ELF header
  flags.
---
 lld/ELF/Arch/ARM.cpp               | 67 +++++++++++++++++++++++++++---
 lld/ELF/InputFiles.cpp             | 14 ++++++-
 lld/ELF/InputFiles.h               |  2 +
 lld/test/ELF/arm-float-abi-check.s | 35 ++++++++++++++++
 4 files changed, 112 insertions(+), 6 deletions(-)
 create mode 100644 lld/test/ELF/arm-float-abi-check.s

diff --git a/lld/ELF/Arch/ARM.cpp b/lld/ELF/Arch/ARM.cpp
index e0b2f321264b5..6a662a6d4eaa2 100644
--- a/lld/ELF/Arch/ARM.cpp
+++ b/lld/ELF/Arch/ARM.cpp
@@ -15,6 +15,7 @@
 #include "Target.h"
 #include "lld/Common/Filesystem.h"
 #include "llvm/BinaryFormat/ELF.h"
+#include "llvm/Support/ARMBuildAttributes.h"
 #include "llvm/Support/Endian.h"
 
 using namespace llvm;
@@ -117,7 +118,17 @@ ARM::ARM(Ctx &ctx) : TargetInfo(ctx) {
   defaultMaxPageSize = 65536;
 }
 
+static bool inputFileHasCode(const InputFile *f) {
+  for (const auto *sec : f->getSections())
+    if (sec && sec->flags & SHF_EXECINSTR)
+      return true;
+  return false;
+}
+
 uint32_t ARM::calcEFlags() const {
+  if (ctx.objectFiles.empty())
+    return 0;
+
   // The ABIFloatType is used by loaders to detect the floating point calling
   // convention.
   uint32_t abiFloatType = 0;
@@ -126,14 +137,60 @@ uint32_t ARM::calcEFlags() const {
   // with BE-8 code.
   uint32_t armBE8 = 0;
 
-  if (ctx.arg.armVFPArgs == ARMVFPArgKind::Base ||
-      ctx.arg.armVFPArgs == ARMVFPArgKind::Default)
+  if (!ctx.arg.isLE && ctx.arg.armBe8)
+    armBE8 = EF_ARM_BE8;
+
+  // Check the float ABI flags/attributes of all input object files.
+  // We use the Tag_ABI_VFP_args build attributes because relocatable object
+  // files do not have float ABI flags set in their ELF headers.
+  uint32_t target = 0; // 0: unset, 1: soft-float/softfp, 2: hard-float
+  const InputFile *targetFile = nullptr;
+  for (const InputFile *f : ctx.objectFiles) {
+    if (!inputFileHasCode(f))
+      continue;
+
+    uint32_t val = 0;
+    uint32_t fpArch = 0;
+    if (const auto *elfFile = dyn_cast<ELFFileBase>(f)) {
+      if (elfFile->armVFPArgs)
+        val = *elfFile->armVFPArgs;
+      if (elfFile->armFPArch)
+        fpArch = *elfFile->armFPArch;
+    }
+
+    if (val == ARMBuildAttrs::CompatibleFPAAPCS) // 3: compatible with all
+      continue;
+
+    // If the file has no VFP usage and no explicit VFP calling convention,
+    // it is neutral/compatible with everything. We skip it.
+    if (fpArch == 0 && val == 0)
+      continue;
+
+    uint32_t fFloat = 1;                   // Default to soft-float/softfp (1)
+    if (val == ARMBuildAttrs::HardFPAAPCS) // 1: AAPCS VFP (hard float)
+      fFloat = 2;                          // Hard float (2)
+
+    if (target == 0) {
+      target = fFloat;
+      targetFile = f;
+    } else if (fFloat != target) {
+      Err(ctx) << f
+               << ": cannot link object files with different floating-point "
+                  "ABI from "
+               << targetFile;
+    }
+  }
+
+  if (ctx.arg.armVFPArgs == ARMVFPArgKind::Base)
     abiFloatType = EF_ARM_ABI_FLOAT_SOFT;
   else if (ctx.arg.armVFPArgs == ARMVFPArgKind::VFP)
     abiFloatType = EF_ARM_ABI_FLOAT_HARD;
-
-  if (!ctx.arg.isLE && ctx.arg.armBe8)
-    armBE8 = EF_ARM_BE8;
+  else if (ctx.arg.armVFPArgs == ARMVFPArgKind::Default) {
+    if (target == 2)
+      abiFloatType = EF_ARM_ABI_FLOAT_HARD;
+    else
+      abiFloatType = EF_ARM_ABI_FLOAT_SOFT;
+  }
 
   // We don't currently use any features incompatible with EF_ARM_EABI_VER5,
   // but we don't have any firm guarantees of conformance. Linux AArch64
diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp
index 2f544f0fe0958..63b17b3d7788d 100644
--- a/lld/ELF/InputFiles.cpp
+++ b/lld/ELF/InputFiles.cpp
@@ -104,9 +104,12 @@ static ELFKind getELFKind(Ctx &ctx, MemoryBufferRef mb, StringRef archiveName) {
 // flag in the ELF Header we need to look at Tag_ABI_VFP_args to find out how
 // the input objects have been compiled.
 static void updateARMVFPArgs(Ctx &ctx, const ARMAttributeParser &attributes,
-                             const InputFile *f) {
+                             InputFile *f) {
   std::optional<unsigned> attr =
       attributes.getAttributeValue(ARMBuildAttrs::ABI_VFP_args);
+  if (attr)
+    cast<ELFFileBase>(f)->armVFPArgs = *attr;
+
   if (!attr)
     // If an ABI tag isn't present then it is implicitly given the value of 0
     // which maps to ARMBuildAttrs::BaseAAPCS. However many assembler files,
@@ -142,6 +145,14 @@ static void updateARMVFPArgs(Ctx &ctx, const ARMAttributeParser &attributes,
     ctx.arg.armVFPArgs = arg;
 }
 
+static void updateARMFPArch(const ARMAttributeParser &attributes,
+                            InputFile *f) {
+  std::optional<unsigned> attr =
+      attributes.getAttributeValue(ARMBuildAttrs::FP_arch);
+  if (attr)
+    cast<ELFFileBase>(f)->armFPArch = *attr;
+}
+
 // The ARM support in lld makes some use of instructions that are not available
 // on all ARM architectures. Namely:
 // - Use of BLX instruction for interworking between ARM and Thumb state.
@@ -655,6 +666,7 @@ template <class ELFT> void ObjFile<ELFT>::parse(bool ignoreComdats) {
         } else {
           updateSupportedARMFeatures(ctx, attributes);
           updateARMVFPArgs(ctx, attributes, this);
+          updateARMFPArch(attributes, this);
 
           // FIXME: Retain the first attribute section we see. The eglibc ARM
           // dynamic loaders require the presence of an attribute section for
diff --git a/lld/ELF/InputFiles.h b/lld/ELF/InputFiles.h
index aef599102ecfc..bf60376e1d768 100644
--- a/lld/ELF/InputFiles.h
+++ b/lld/ELF/InputFiles.h
@@ -238,6 +238,8 @@ class ELFFileBase : public InputFile {
   uint32_t andFeatures = 0;
   bool hasCommonSyms = false;
   std::optional<AArch64PauthAbiCoreInfo> aarch64PauthAbiCoreInfo;
+  std::optional<uint32_t> armVFPArgs;
+  std::optional<uint32_t> armFPArch;
 };
 
 // .o file.
diff --git a/lld/test/ELF/arm-float-abi-check.s b/lld/test/ELF/arm-float-abi-check.s
new file mode 100644
index 0000000000000..d145952072077
--- /dev/null
+++ b/lld/test/ELF/arm-float-abi-check.s
@@ -0,0 +1,35 @@
+// REQUIRES: arm
+// RUN: rm -rf %t && split-file %s %t && cd %t
+// RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi hf.s -o hf.o
+// RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi softfp.s -o softfp.o
+// RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi compat.s -o compat.o
+// RUN: not ld.lld hf.o softfp.o -o /dev/null 2>&1 | FileCheck %s --check-prefix=ERR -DF1=softfp.o -DF2=hf.o
+// RUN: ld.lld hf.o compat.o -o /dev/null
+
+// ERR: error: [[F1]]: cannot link object files with different floating-point ABI from [[F2]]
+
+//--- hf.s
+.syntax unified
+.global _start
+.type _start, %function
+_start:
+    bx lr
+.eabi_attribute 10, 2 // Tag_FP_arch = VFPv2
+.eabi_attribute 28, 1 // Tag_ABI_VFP_args = AAPCS VFP (hard float)
+
+//--- softfp.s
+.syntax unified
+.global f
+.type f, %function
+f:
+    bx lr
+.eabi_attribute 10, 2 // Tag_FP_arch = VFPv2
+// Tag_ABI_VFP_args is omitted (implicitly BaseAAPCS, i.e., softfp since Tag_FP_arch is set)
+
+//--- compat.s
+.syntax unified
+.global g
+.type g, %function
+g:
+    bx lr
+.eabi_attribute 28, 3 // Tag_ABI_VFP_args = CompatibleFPAAPCS (compatible with all)

>From b17e787990d051e4b71690be652e74851a383cc2 Mon Sep 17 00:00:00 2001
From: prabhukr <prabhukr at google.com>
Date: Wed, 24 Jun 2026 14:13:06 -0700
Subject: [PATCH 2/5] Add another test.

---
 lld/test/ELF/arm-float-abi-check.s | 1 +
 1 file changed, 1 insertion(+)

diff --git a/lld/test/ELF/arm-float-abi-check.s b/lld/test/ELF/arm-float-abi-check.s
index d145952072077..6c4f5b1c00f36 100644
--- a/lld/test/ELF/arm-float-abi-check.s
+++ b/lld/test/ELF/arm-float-abi-check.s
@@ -5,6 +5,7 @@
 // RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi compat.s -o compat.o
 // RUN: not ld.lld hf.o softfp.o -o /dev/null 2>&1 | FileCheck %s --check-prefix=ERR -DF1=softfp.o -DF2=hf.o
 // RUN: ld.lld hf.o compat.o -o /dev/null
+// RUN: ld.lld softfp.o compat.o -o /dev/null
 
 // ERR: error: [[F1]]: cannot link object files with different floating-point ABI from [[F2]]
 

>From 404fe01b5e70904e464a6cf49a65d494e494c565 Mon Sep 17 00:00:00 2001
From: prabhukr <prabhukr at google.com>
Date: Thu, 25 Jun 2026 10:05:21 -0700
Subject: [PATCH 3/5] Set VFP args attribute based on triple from assembler

---
 llvm/lib/Target/ARM/MCTargetDesc/ARMELFStreamer.cpp | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/llvm/lib/Target/ARM/MCTargetDesc/ARMELFStreamer.cpp b/llvm/lib/Target/ARM/MCTargetDesc/ARMELFStreamer.cpp
index a9072e38f1181..07fac56e17455 100644
--- a/llvm/lib/Target/ARM/MCTargetDesc/ARMELFStreamer.cpp
+++ b/llvm/lib/Target/ARM/MCTargetDesc/ARMELFStreamer.cpp
@@ -50,6 +50,7 @@
 #include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/FormattedStream.h"
 #include "llvm/Support/raw_ostream.h"
+#include "llvm/TargetParser/Triple.h"
 #include <cassert>
 #include <climits>
 #include <cstdint>
@@ -1068,6 +1069,15 @@ void ARMTargetELFStreamer::finishAttributeSection() {
   if (Arch != ARM::ArchKind::INVALID)
     emitArchDefaultAttributes();
 
+  const Triple &TT = S.getContext().getTargetTriple();
+  if (TT.getEnvironment() == Triple::GNUEABIHF ||
+      TT.getEnvironment() == Triple::GNUEABIHFT64 ||
+      TT.getEnvironment() == Triple::MuslEABIHF ||
+      TT.getEnvironment() == Triple::EABIHF || TT.isOSWindows()) {
+    S.setAttributeItem(ARMBuildAttrs::ABI_VFP_args, ARMBuildAttrs::HardFPAAPCS,
+                       /* OverwriteExisting= */ false);
+  }
+
   if (S.Contents.empty())
     return;
 

>From 3cc8ed97c0045d79bebb08cd2c7a695830525117 Mon Sep 17 00:00:00 2001
From: prabhukr <prabhukr at google.com>
Date: Thu, 25 Jun 2026 10:42:00 -0700
Subject: [PATCH 4/5] Add useful comment. Add tests.

---
 .../ARM/MCTargetDesc/ARMELFStreamer.cpp       |  6 ++++++
 llvm/test/MC/ARM/elf-float-abi-explicit.s     | 14 +++++++++++++
 llvm/test/MC/ARM/elf-float-abi.s              | 21 +++++++++++++++++++
 3 files changed, 41 insertions(+)
 create mode 100644 llvm/test/MC/ARM/elf-float-abi-explicit.s
 create mode 100644 llvm/test/MC/ARM/elf-float-abi.s

diff --git a/llvm/lib/Target/ARM/MCTargetDesc/ARMELFStreamer.cpp b/llvm/lib/Target/ARM/MCTargetDesc/ARMELFStreamer.cpp
index 07fac56e17455..921fbe71a1119 100644
--- a/llvm/lib/Target/ARM/MCTargetDesc/ARMELFStreamer.cpp
+++ b/llvm/lib/Target/ARM/MCTargetDesc/ARMELFStreamer.cpp
@@ -1069,6 +1069,12 @@ void ARMTargetELFStreamer::finishAttributeSection() {
   if (Arch != ARM::ArchKind::INVALID)
     emitArchDefaultAttributes();
 
+  // Set Tag_ABI_VFP_args based on the target triple environment.
+  // For hard-float environments (ending in 'hf') and Windows on ARM (which
+  // strictly mandates the AAPCS-VFP hard-float calling convention), we default
+  // Tag_ABI_VFP_args to HardFPAAPCS. We set OverwriteExisting to false to
+  // ensure that any explicit .eabi_attribute directives in the assembly source
+  // are respected and not overwritten.
   const Triple &TT = S.getContext().getTargetTriple();
   if (TT.getEnvironment() == Triple::GNUEABIHF ||
       TT.getEnvironment() == Triple::GNUEABIHFT64 ||
diff --git a/llvm/test/MC/ARM/elf-float-abi-explicit.s b/llvm/test/MC/ARM/elf-float-abi-explicit.s
new file mode 100644
index 0000000000000..3727cf0f23282
--- /dev/null
+++ b/llvm/test/MC/ARM/elf-float-abi-explicit.s
@@ -0,0 +1,14 @@
+@ RUN: llvm-mc -triple armv7a-unknown-linux-gnueabihf -filetype=obj -o - %s \
+@ RUN:   | llvm-readobj --arch-specific - | FileCheck %s --check-prefix=EXPLICIT-SOFT
+
+@ EXPLICIT-SOFT:        Tag: 28
+@ EXPLICIT-SOFT-NEXT:   Value: 0
+@ EXPLICIT-SOFT-NEXT:   TagName: ABI_VFP_args
+@ EXPLICIT-SOFT-NEXT:   Description: AAPCS
+
+.syntax unified
+.eabi_attribute 28, 0 @ Explicitly declare Base AAPCS (soft-float/softfp)
+.text
+.global _start
+_start:
+    bx lr
diff --git a/llvm/test/MC/ARM/elf-float-abi.s b/llvm/test/MC/ARM/elf-float-abi.s
new file mode 100644
index 0000000000000..df01965e40e0d
--- /dev/null
+++ b/llvm/test/MC/ARM/elf-float-abi.s
@@ -0,0 +1,21 @@
+@ RUN: llvm-mc -triple armv7a-unknown-linux-gnueabihf -filetype=obj -o - %s \
+@ RUN:   | llvm-readobj --arch-specific - | FileCheck %s --check-prefix=HARD
+
+@ RUN: llvm-mc -triple armv7a-unknown-linux-gnueabi -filetype=obj -o - %s \
+@ RUN:   | llvm-readobj --arch-specific - | FileCheck %s --check-prefix=SOFT
+
+@ RUN: llvm-mc -triple thumbv7-pc-windows-elf -filetype=obj -o - %s \
+@ RUN:   | llvm-readobj --arch-specific - | FileCheck %s --check-prefix=HARD
+
+@ HARD:        Tag: 28
+@ HARD-NEXT:   Value: 1
+@ HARD-NEXT:   TagName: ABI_VFP_args
+@ HARD-NEXT:   Description: AAPCS VFP
+
+@ SOFT-NOT: TagName: ABI_VFP_args
+
+.syntax unified
+.text
+.global _start
+_start:
+    bx lr

>From 99a9df8941fb95cd91e9bde32018c24452da9ae6 Mon Sep 17 00:00:00 2001
From: prabhukr <prabhukr at google.com>
Date: Thu, 25 Jun 2026 11:48:45 -0700
Subject: [PATCH 5/5] break out llvm-mc changes.

---
 .../ARM/MCTargetDesc/ARMELFStreamer.cpp       | 16 --------------
 llvm/test/MC/ARM/elf-float-abi-explicit.s     | 14 -------------
 llvm/test/MC/ARM/elf-float-abi.s              | 21 -------------------
 3 files changed, 51 deletions(-)
 delete mode 100644 llvm/test/MC/ARM/elf-float-abi-explicit.s
 delete mode 100644 llvm/test/MC/ARM/elf-float-abi.s

diff --git a/llvm/lib/Target/ARM/MCTargetDesc/ARMELFStreamer.cpp b/llvm/lib/Target/ARM/MCTargetDesc/ARMELFStreamer.cpp
index 921fbe71a1119..a9072e38f1181 100644
--- a/llvm/lib/Target/ARM/MCTargetDesc/ARMELFStreamer.cpp
+++ b/llvm/lib/Target/ARM/MCTargetDesc/ARMELFStreamer.cpp
@@ -50,7 +50,6 @@
 #include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/FormattedStream.h"
 #include "llvm/Support/raw_ostream.h"
-#include "llvm/TargetParser/Triple.h"
 #include <cassert>
 #include <climits>
 #include <cstdint>
@@ -1069,21 +1068,6 @@ void ARMTargetELFStreamer::finishAttributeSection() {
   if (Arch != ARM::ArchKind::INVALID)
     emitArchDefaultAttributes();
 
-  // Set Tag_ABI_VFP_args based on the target triple environment.
-  // For hard-float environments (ending in 'hf') and Windows on ARM (which
-  // strictly mandates the AAPCS-VFP hard-float calling convention), we default
-  // Tag_ABI_VFP_args to HardFPAAPCS. We set OverwriteExisting to false to
-  // ensure that any explicit .eabi_attribute directives in the assembly source
-  // are respected and not overwritten.
-  const Triple &TT = S.getContext().getTargetTriple();
-  if (TT.getEnvironment() == Triple::GNUEABIHF ||
-      TT.getEnvironment() == Triple::GNUEABIHFT64 ||
-      TT.getEnvironment() == Triple::MuslEABIHF ||
-      TT.getEnvironment() == Triple::EABIHF || TT.isOSWindows()) {
-    S.setAttributeItem(ARMBuildAttrs::ABI_VFP_args, ARMBuildAttrs::HardFPAAPCS,
-                       /* OverwriteExisting= */ false);
-  }
-
   if (S.Contents.empty())
     return;
 
diff --git a/llvm/test/MC/ARM/elf-float-abi-explicit.s b/llvm/test/MC/ARM/elf-float-abi-explicit.s
deleted file mode 100644
index 3727cf0f23282..0000000000000
--- a/llvm/test/MC/ARM/elf-float-abi-explicit.s
+++ /dev/null
@@ -1,14 +0,0 @@
-@ RUN: llvm-mc -triple armv7a-unknown-linux-gnueabihf -filetype=obj -o - %s \
-@ RUN:   | llvm-readobj --arch-specific - | FileCheck %s --check-prefix=EXPLICIT-SOFT
-
-@ EXPLICIT-SOFT:        Tag: 28
-@ EXPLICIT-SOFT-NEXT:   Value: 0
-@ EXPLICIT-SOFT-NEXT:   TagName: ABI_VFP_args
-@ EXPLICIT-SOFT-NEXT:   Description: AAPCS
-
-.syntax unified
-.eabi_attribute 28, 0 @ Explicitly declare Base AAPCS (soft-float/softfp)
-.text
-.global _start
-_start:
-    bx lr
diff --git a/llvm/test/MC/ARM/elf-float-abi.s b/llvm/test/MC/ARM/elf-float-abi.s
deleted file mode 100644
index df01965e40e0d..0000000000000
--- a/llvm/test/MC/ARM/elf-float-abi.s
+++ /dev/null
@@ -1,21 +0,0 @@
-@ RUN: llvm-mc -triple armv7a-unknown-linux-gnueabihf -filetype=obj -o - %s \
-@ RUN:   | llvm-readobj --arch-specific - | FileCheck %s --check-prefix=HARD
-
-@ RUN: llvm-mc -triple armv7a-unknown-linux-gnueabi -filetype=obj -o - %s \
-@ RUN:   | llvm-readobj --arch-specific - | FileCheck %s --check-prefix=SOFT
-
-@ RUN: llvm-mc -triple thumbv7-pc-windows-elf -filetype=obj -o - %s \
-@ RUN:   | llvm-readobj --arch-specific - | FileCheck %s --check-prefix=HARD
-
-@ HARD:        Tag: 28
-@ HARD-NEXT:   Value: 1
-@ HARD-NEXT:   TagName: ABI_VFP_args
-@ HARD-NEXT:   Description: AAPCS VFP
-
-@ SOFT-NOT: TagName: ABI_VFP_args
-
-.syntax unified
-.text
-.global _start
-_start:
-    bx lr



More information about the llvm-commits mailing list