[clang] [clang] fix uefi target for aarch64 & x86_64 (PR #120632)

Tristan Ross via cfe-commits cfe-commits at lists.llvm.org
Thu Dec 19 12:17:00 PST 2024


https://github.com/RossComputerGuy created https://github.com/llvm/llvm-project/pull/120632

This PR introduces some fixes where the data layout wasn't propagated correctly. To test this, feed clang any C file (I used an empty main for testing) and pass `-target aarch64-uefi` or `-target x86_64-uefi`.

Before this PR, you would get an error similar to this:
```
error: backend data layout 'e-m:w-p:64:64-i32:32-i64:64-i128:128-n32:64-S128' does not match expected target description 'e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128'
1 error generated.
```

This PR also adds a new `__uefi__` macro which will hopefully let us work towards getting compiler-rt and libc working with UEFI.

>From 965537e08a14416629f3891f4af0a59925ada38d Mon Sep 17 00:00:00 2001
From: Tristan Ross <tristan.ross at midstall.com>
Date: Thu, 19 Dec 2024 10:54:58 -0800
Subject: [PATCH 1/2] [clang] fix uefi data layout on x86 & aarch64

---
 clang/lib/Basic/Targets/AArch64.cpp | 4 ++++
 clang/lib/Basic/Targets/X86.h       | 2 +-
 2 files changed, 5 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Basic/Targets/AArch64.cpp b/clang/lib/Basic/Targets/AArch64.cpp
index 53e102bbe44687..d8aef4837f9267 100644
--- a/clang/lib/Basic/Targets/AArch64.cpp
+++ b/clang/lib/Basic/Targets/AArch64.cpp
@@ -1537,6 +1537,10 @@ void AArch64leTargetInfo::setDataLayout() {
       resetDataLayout("e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-"
                       "n32:64-S128-Fn32",
                       "_");
+  } else if (getTriple().isUEFI() && getTriple().isOSBinFormatCOFF()) {
+    resetDataLayout(
+        "e-m:w-p270:32:32-p271:32:32-p272:64:64-p:64:64-i32:32-"
+        "i64:64-i128:128-n32:64-S128-Fn32");
   } else
     resetDataLayout("e-m:e-p270:32:32-p271:32:32-p272:64:64-i8:8:32-i16:16:32-"
                     "i64:64-i128:128-n32:64-S128-Fn32");
diff --git a/clang/lib/Basic/Targets/X86.h b/clang/lib/Basic/Targets/X86.h
index 3ed36c8fa724b5..bb72a92b1cf5b7 100644
--- a/clang/lib/Basic/Targets/X86.h
+++ b/clang/lib/Basic/Targets/X86.h
@@ -714,7 +714,7 @@ class LLVM_LIBRARY_VISIBILITY X86_64TargetInfo : public X86TargetInfo {
       : X86TargetInfo(Triple, Opts) {
     const bool IsX32 = getTriple().isX32();
     bool IsWinCOFF =
-        getTriple().isOSWindows() && getTriple().isOSBinFormatCOFF();
+        (getTriple().isOSWindows() || getTriple().isUEFI()) && getTriple().isOSBinFormatCOFF();
     LongWidth = LongAlign = PointerWidth = PointerAlign = IsX32 ? 32 : 64;
     LongDoubleWidth = 128;
     LongDoubleAlign = 128;

>From 8f5ef58d51aac7b91faf9e9b6e56eda137bad9ce Mon Sep 17 00:00:00 2001
From: Tristan Ross <tristan.ross at midstall.com>
Date: Thu, 19 Dec 2024 08:37:46 -0800
Subject: [PATCH 2/2] [clang] add __uefi__ define

---
 clang/lib/Basic/Targets/OSTargets.h | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Basic/Targets/OSTargets.h b/clang/lib/Basic/Targets/OSTargets.h
index cd9b3760ca5874..53dd23c3129636 100644
--- a/clang/lib/Basic/Targets/OSTargets.h
+++ b/clang/lib/Basic/Targets/OSTargets.h
@@ -790,7 +790,9 @@ template <typename Target>
 class LLVM_LIBRARY_VISIBILITY UEFITargetInfo : public OSTargetInfo<Target> {
 protected:
   void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
-                    MacroBuilder &Builder) const override {}
+                    MacroBuilder &Builder) const override {
+    Builder.defineMacro("__UEFI__");
+  }
 
 public:
   UEFITargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)



More information about the cfe-commits mailing list