[clang] [llvm] Hurd: Add x86_64 support (PR #78065)

Samuel Thibault via cfe-commits cfe-commits at lists.llvm.org
Sat Jan 13 12:43:36 PST 2024


https://github.com/sthibaul created https://github.com/llvm/llvm-project/pull/78065

None

>From 17619023092c35fb851866172094540f6d18718f Mon Sep 17 00:00:00 2001
From: Samuel Thibault <samuel.thibault at ens-lyon.org>
Date: Sat, 13 Jan 2024 20:16:03 +0100
Subject: [PATCH] Hurd: Add x86_64 support

---
 clang/lib/Basic/Targets.cpp                   |  2 +
 clang/lib/Driver/ToolChains/Gnu.cpp           |  2 +-
 clang/lib/Driver/ToolChains/Hurd.cpp          | 16 +++-
 .../usr/lib/gcc/x86_64-gnu/10/crtbegin.o      |  0
 .../usr/x86_64-gnu/bin/as                     |  0
 .../usr/x86_64-gnu/bin/ld                     |  0
 .../usr/x86_64-gnu/lib/.keep                  |  0
 .../basic_hurd_tree/lib/x86_64-gnu/.keep      |  0
 .../Driver/Inputs/basic_hurd_tree/lib64/.keep |  0
 .../usr/include/x86_64-gnu/.keep              |  0
 .../usr/include/x86_64-gnu/c++/10/.keep       |  0
 .../usr/lib/gcc/x86_64-gnu/10/crtbegin.o      |  0
 .../usr/lib/gcc/x86_64-gnu/10/crtbeginS.o     |  0
 .../usr/lib/gcc/x86_64-gnu/10/crtbeginT.o     |  0
 .../basic_hurd_tree/usr/lib/x86_64-gnu/.keep  |  0
 .../Inputs/basic_hurd_tree/usr/lib64/.keep    |  0
 clang/test/Driver/hurd.cpp                    | 79 +++++++++++++++++++
 llvm/unittests/TargetParser/TripleTest.cpp    |  6 ++
 18 files changed, 102 insertions(+), 3 deletions(-)
 create mode 100644 clang/test/Driver/Inputs/basic_cross_hurd_tree/usr/lib/gcc/x86_64-gnu/10/crtbegin.o
 create mode 100755 clang/test/Driver/Inputs/basic_cross_hurd_tree/usr/x86_64-gnu/bin/as
 create mode 100755 clang/test/Driver/Inputs/basic_cross_hurd_tree/usr/x86_64-gnu/bin/ld
 create mode 100644 clang/test/Driver/Inputs/basic_cross_hurd_tree/usr/x86_64-gnu/lib/.keep
 create mode 100644 clang/test/Driver/Inputs/basic_hurd_tree/lib/x86_64-gnu/.keep
 create mode 100644 clang/test/Driver/Inputs/basic_hurd_tree/lib64/.keep
 create mode 100644 clang/test/Driver/Inputs/basic_hurd_tree/usr/include/x86_64-gnu/.keep
 create mode 100644 clang/test/Driver/Inputs/basic_hurd_tree/usr/include/x86_64-gnu/c++/10/.keep
 create mode 100644 clang/test/Driver/Inputs/basic_hurd_tree/usr/lib/gcc/x86_64-gnu/10/crtbegin.o
 create mode 100644 clang/test/Driver/Inputs/basic_hurd_tree/usr/lib/gcc/x86_64-gnu/10/crtbeginS.o
 create mode 100644 clang/test/Driver/Inputs/basic_hurd_tree/usr/lib/gcc/x86_64-gnu/10/crtbeginT.o
 create mode 100644 clang/test/Driver/Inputs/basic_hurd_tree/usr/lib/x86_64-gnu/.keep
 create mode 100644 clang/test/Driver/Inputs/basic_hurd_tree/usr/lib64/.keep

diff --git a/clang/lib/Basic/Targets.cpp b/clang/lib/Basic/Targets.cpp
index ea002bb464fcc5..e3283510c6aac7 100644
--- a/clang/lib/Basic/Targets.cpp
+++ b/clang/lib/Basic/Targets.cpp
@@ -644,6 +644,8 @@ std::unique_ptr<TargetInfo> AllocateTarget(const llvm::Triple &Triple,
       return std::make_unique<PS4OSTargetInfo<X86_64TargetInfo>>(Triple, Opts);
     case llvm::Triple::PS5:
       return std::make_unique<PS5OSTargetInfo<X86_64TargetInfo>>(Triple, Opts);
+    case llvm::Triple::Hurd:
+      return std::make_unique<HurdTargetInfo<X86_64TargetInfo>>(Triple, Opts);
     default:
       return std::make_unique<X86_64TargetInfo>(Triple, Opts);
     }
diff --git a/clang/lib/Driver/ToolChains/Gnu.cpp b/clang/lib/Driver/ToolChains/Gnu.cpp
index 771240dac7a83e..46ecef421e1ad5 100644
--- a/clang/lib/Driver/ToolChains/Gnu.cpp
+++ b/clang/lib/Driver/ToolChains/Gnu.cpp
@@ -2477,7 +2477,7 @@ void Generic_GCC::GCCInstallationDetector::AddDefaultGCCPrefixes(
       "x86_64-redhat-linux",    "x86_64-suse-linux",
       "x86_64-manbo-linux-gnu", "x86_64-linux-gnu",
       "x86_64-slackware-linux", "x86_64-unknown-linux",
-      "x86_64-amazon-linux"};
+      "x86_64-amazon-linux",    "x86_64-gnu"};
   static const char *const X32Triples[] = {"x86_64-linux-gnux32",
                                            "x86_64-pc-linux-gnux32"};
   static const char *const X32LibDirs[] = {"/libx32", "/lib"};
diff --git a/clang/lib/Driver/ToolChains/Hurd.cpp b/clang/lib/Driver/ToolChains/Hurd.cpp
index 7a4c2bb7ede1d5..5074eda5f41559 100644
--- a/clang/lib/Driver/ToolChains/Hurd.cpp
+++ b/clang/lib/Driver/ToolChains/Hurd.cpp
@@ -30,13 +30,21 @@ using tools::addPathIfExists;
 std::string Hurd::getMultiarchTriple(const Driver &D,
                                      const llvm::Triple &TargetTriple,
                                      StringRef SysRoot) const {
-  if (TargetTriple.getArch() == llvm::Triple::x86) {
+  switch (TargetTriple.getArch()) {
+  default:
+    break;
+
+  case llvm::Triple::x86:
     // We use the existence of '/lib/<triple>' as a directory to detect some
     // common hurd triples that don't quite match the Clang triple for both
     // 32-bit and 64-bit targets. Multiarch fixes its install triples to these
     // regardless of what the actual target triple is.
     if (D.getVFS().exists(SysRoot + "/lib/i386-gnu"))
       return "i386-gnu";
+    break;
+
+  case llvm::Triple::x86_64:
+    return "x86_64-gnu";
   }
 
   // For most architectures, just use whatever we have rather than trying to be
@@ -126,8 +134,12 @@ Tool *Hurd::buildAssembler() const {
 }
 
 std::string Hurd::getDynamicLinker(const ArgList &Args) const {
-  if (getArch() == llvm::Triple::x86)
+  switch (getArch()) {
+  case llvm::Triple::x86:
     return "/lib/ld.so";
+  case llvm::Triple::x86_64:
+    return "/lib/ld-x86-64.so.1";
+  }
 
   llvm_unreachable("unsupported architecture");
 }
diff --git a/clang/test/Driver/Inputs/basic_cross_hurd_tree/usr/lib/gcc/x86_64-gnu/10/crtbegin.o b/clang/test/Driver/Inputs/basic_cross_hurd_tree/usr/lib/gcc/x86_64-gnu/10/crtbegin.o
new file mode 100644
index 00000000000000..e69de29bb2d1d6
diff --git a/clang/test/Driver/Inputs/basic_cross_hurd_tree/usr/x86_64-gnu/bin/as b/clang/test/Driver/Inputs/basic_cross_hurd_tree/usr/x86_64-gnu/bin/as
new file mode 100755
index 00000000000000..e69de29bb2d1d6
diff --git a/clang/test/Driver/Inputs/basic_cross_hurd_tree/usr/x86_64-gnu/bin/ld b/clang/test/Driver/Inputs/basic_cross_hurd_tree/usr/x86_64-gnu/bin/ld
new file mode 100755
index 00000000000000..e69de29bb2d1d6
diff --git a/clang/test/Driver/Inputs/basic_cross_hurd_tree/usr/x86_64-gnu/lib/.keep b/clang/test/Driver/Inputs/basic_cross_hurd_tree/usr/x86_64-gnu/lib/.keep
new file mode 100644
index 00000000000000..e69de29bb2d1d6
diff --git a/clang/test/Driver/Inputs/basic_hurd_tree/lib/x86_64-gnu/.keep b/clang/test/Driver/Inputs/basic_hurd_tree/lib/x86_64-gnu/.keep
new file mode 100644
index 00000000000000..e69de29bb2d1d6
diff --git a/clang/test/Driver/Inputs/basic_hurd_tree/lib64/.keep b/clang/test/Driver/Inputs/basic_hurd_tree/lib64/.keep
new file mode 100644
index 00000000000000..e69de29bb2d1d6
diff --git a/clang/test/Driver/Inputs/basic_hurd_tree/usr/include/x86_64-gnu/.keep b/clang/test/Driver/Inputs/basic_hurd_tree/usr/include/x86_64-gnu/.keep
new file mode 100644
index 00000000000000..e69de29bb2d1d6
diff --git a/clang/test/Driver/Inputs/basic_hurd_tree/usr/include/x86_64-gnu/c++/10/.keep b/clang/test/Driver/Inputs/basic_hurd_tree/usr/include/x86_64-gnu/c++/10/.keep
new file mode 100644
index 00000000000000..e69de29bb2d1d6
diff --git a/clang/test/Driver/Inputs/basic_hurd_tree/usr/lib/gcc/x86_64-gnu/10/crtbegin.o b/clang/test/Driver/Inputs/basic_hurd_tree/usr/lib/gcc/x86_64-gnu/10/crtbegin.o
new file mode 100644
index 00000000000000..e69de29bb2d1d6
diff --git a/clang/test/Driver/Inputs/basic_hurd_tree/usr/lib/gcc/x86_64-gnu/10/crtbeginS.o b/clang/test/Driver/Inputs/basic_hurd_tree/usr/lib/gcc/x86_64-gnu/10/crtbeginS.o
new file mode 100644
index 00000000000000..e69de29bb2d1d6
diff --git a/clang/test/Driver/Inputs/basic_hurd_tree/usr/lib/gcc/x86_64-gnu/10/crtbeginT.o b/clang/test/Driver/Inputs/basic_hurd_tree/usr/lib/gcc/x86_64-gnu/10/crtbeginT.o
new file mode 100644
index 00000000000000..e69de29bb2d1d6
diff --git a/clang/test/Driver/Inputs/basic_hurd_tree/usr/lib/x86_64-gnu/.keep b/clang/test/Driver/Inputs/basic_hurd_tree/usr/lib/x86_64-gnu/.keep
new file mode 100644
index 00000000000000..e69de29bb2d1d6
diff --git a/clang/test/Driver/Inputs/basic_hurd_tree/usr/lib64/.keep b/clang/test/Driver/Inputs/basic_hurd_tree/usr/lib64/.keep
new file mode 100644
index 00000000000000..e69de29bb2d1d6
diff --git a/clang/test/Driver/hurd.cpp b/clang/test/Driver/hurd.cpp
index 1c4ba16af063d7..c7d1cf317ffd99 100644
--- a/clang/test/Driver/hurd.cpp
+++ b/clang/test/Driver/hurd.cpp
@@ -78,3 +78,82 @@
 // CHECK-CROSS: "{{.*}}/Inputs/basic_cross_hurd_tree/usr/lib/gcc/i686-gnu/10/../../../../i686-gnu/bin/ld" {{.*}} "-m" "elf_i386"
 // CHECK-CROSS: "{{.*}}/Inputs/basic_cross_hurd_tree/usr/lib/gcc/i686-gnu/10/crtbegin.o"
 // CHECK-CROSS: "-L{{.*}}/Inputs/basic_cross_hurd_tree/usr/lib/gcc/i686-gnu/10/../../../../i686-gnu/lib"
+
+// RUN: %clang -### %s --target=x86_64-pc-hurd-gnu --sysroot=%S/Inputs/basic_hurd_tree \
+// RUN:   --stdlib=platform 2>&1 | FileCheck --check-prefix=CHECK-64 %s
+// CHECK-64: "-cc1"
+// CHECK-64: "-isysroot" "[[SYSROOT:[^"]+]]"
+// CHECK-64-SAME: {{^}} "-internal-isystem" "[[SYSROOT]]/usr/lib/gcc/x86_64-gnu/10/../../../../include/c++/10"
+/// Debian specific - the path component after 'include' is x86_64-gnu even
+/// though the installation is x86_64-gnu.
+// CHECK-64-SAME: {{^}} "-internal-isystem" "[[SYSROOT]]/usr/lib/gcc/x86_64-gnu/10/../../../../include/x86_64-gnu/c++/10"
+// CHECK-64-SAME: {{^}} "-internal-isystem" "[[SYSROOT]]/usr/lib/gcc/x86_64-gnu/10/../../../../include/c++/10/backward"
+// CHECK-64-SAME: {{^}} "-internal-isystem" "[[SYSROOT]]/usr/local/include"
+// CHECK-64:      "-internal-externc-isystem"
+// CHECK-64-SAME: {{^}} "[[SYSROOT]]/usr/include/x86_64-gnu"
+// CHECK-64-SAME: {{^}} "-internal-externc-isystem" "[[SYSROOT]]/include"
+// CHECK-64-SAME: {{^}} "-internal-externc-isystem" "[[SYSROOT]]/usr/include"
+// CHECK-64:      "{{.*}}ld" "--sysroot=[[SYSROOT:[^"]+]]"
+// CHECK-64:      "-dynamic-linker" "/lib/ld-x86-64.so.1"
+// CHECK-64:      "{{.*}}/usr/lib/gcc/x86_64-gnu/10/crtbegin.o"
+// CHECK-64:      "-L
+// CHECK-64-SAME: {{^}}[[SYSROOT]]/usr/lib/gcc/x86_64-gnu/10"
+// CHECK-64-SAME: {{^}} "-L[[SYSROOT]]/usr/lib/gcc/x86_64-gnu/10/../../../../lib64"
+// CHECK-64-SAME: {{^}} "-L[[SYSROOT]]/lib/x86_64-gnu"
+// CHECK-64-SAME: {{^}} "-L[[SYSROOT]]/lib/../lib64"
+// CHECK-64-SAME: {{^}} "-L[[SYSROOT]]/usr/lib/x86_64-gnu"
+// CHECK-64-SAME: {{^}} "-L[[SYSROOT]]/usr/lib/../lib64"
+// CHECK-64-SAME: {{^}} "-L[[SYSROOT]]/lib"
+// CHECK-64-SAME: {{^}} "-L[[SYSROOT]]/usr/lib"
+
+// RUN: %clang -### %s --target=x86_64-pc-hurd-gnu --sysroot=%S/Inputs/basic_hurd_tree \
+// RUN:   --stdlib=platform -static 2>&1 | FileCheck --check-prefix=CHECK-64-STATIC %s
+// CHECK-64-STATIC: "-cc1"
+// CHECK-64-STATIC: "-static-define"
+// CHECK-64-STATIC: "-isysroot" "[[SYSROOT:[^"]+]]"
+// CHECK-64-STATIC-SAME: {{^}} "-internal-isystem" "[[SYSROOT]]/usr/lib/gcc/x86_64-gnu/10/../../../../include/c++/10"
+/// Debian specific - the path component after 'include' is x86_64-gnu even
+/// though the installation is x86_64-gnu.
+// CHECK-64-STATIC-SAME: {{^}} "-internal-isystem" "[[SYSROOT]]/usr/lib/gcc/x86_64-gnu/10/../../../../include/x86_64-gnu/c++/10"
+// CHECK-64-STATIC-SAME: {{^}} "-internal-isystem" "[[SYSROOT]]/usr/lib/gcc/x86_64-gnu/10/../../../../include/c++/10/backward"
+// CHECK-64-STATIC-SAME: {{^}} "-internal-isystem" "[[SYSROOT]]/usr/local/include"
+// CHECK-64-STATIC:      "-internal-externc-isystem"
+// CHECK-64-STATIC-SAME: {{^}} "[[SYSROOT]]/usr/include/x86_64-gnu"
+// CHECK-64-STATIC-SAME: {{^}} "-internal-externc-isystem" "[[SYSROOT]]/include"
+// CHECK-64-STATIC-SAME: {{^}} "-internal-externc-isystem" "[[SYSROOT]]/usr/include"
+// CHECK-64-STATIC:      "{{.*}}ld" "--sysroot=[[SYSROOT:[^"]+]]"
+// CHECK-64-STATIC:      "-static"
+// CHECK-64-STATIC:      "{{.*}}/usr/lib/gcc/x86_64-gnu/10/crtbeginT.o"
+// CHECK-64-STATIC:      "-L
+// CHECK-64-STATIC-SAME: {{^}}[[SYSROOT]]/usr/lib/gcc/x86_64-gnu/10"
+// CHECK-64-STATIC-SAME: {{^}} "-L[[SYSROOT]]/usr/lib/gcc/x86_64-gnu/10/../../../../lib64"
+// CHECK-64-STATIC-SAME: {{^}} "-L[[SYSROOT]]/lib/x86_64-gnu"
+// CHECK-64-STATIC-SAME: {{^}} "-L[[SYSROOT]]/lib/../lib64"
+// CHECK-64-STATIC-SAME: {{^}} "-L[[SYSROOT]]/usr/lib/x86_64-gnu"
+// CHECK-64-STATIC-SAME: {{^}} "-L[[SYSROOT]]/usr/lib/../lib64"
+// CHECK-64-STATIC-SAME: {{^}} "-L[[SYSROOT]]/lib"
+// CHECK-64-STATIC-SAME: {{^}} "-L[[SYSROOT]]/usr/lib"
+
+// RUN: %clang -### %s --target=x86_64-pc-hurd-gnu --sysroot=%S/Inputs/basic_hurd_tree \
+// RUN:   -shared 2>&1 | FileCheck --check-prefix=CHECK-64-SHARED %s
+// CHECK-64-SHARED: "{{.*}}ld" "--sysroot=[[SYSROOT:[^"]+]]"
+// CHECK-64-SHARED: "{{.*}}/usr/lib/gcc/x86_64-gnu/10/crtbeginS.o"
+// CHECK-64-SHARED: "-L
+// CHECK-64-SHARED-SAME: {{^}}[[SYSROOT]]/usr/lib/gcc/x86_64-gnu/10"
+// CHECK-64-SHARED-SAME: {{^}} "-L[[SYSROOT]]/usr/lib/gcc/x86_64-gnu/10/../../../../lib64"
+// CHECK-64-SHARED-SAME: {{^}} "-L[[SYSROOT]]/lib/x86_64-gnu"
+// CHECK-64-SHARED-SAME: {{^}} "-L[[SYSROOT]]/lib/../lib64"
+// CHECK-64-SHARED-SAME: {{^}} "-L[[SYSROOT]]/usr/lib/x86_64-gnu"
+// CHECK-64-SHARED-SAME: {{^}} "-L[[SYSROOT]]/usr/lib/../lib64"
+// CHECK-64-SHARED-SAME: {{^}} "-L[[SYSROOT]]/lib"
+// CHECK-64-SHARED-SAME: {{^}} "-L[[SYSROOT]]/usr/lib"
+
+// RUN: %clang -### -o %t %s 2>&1 -no-integrated-as -fuse-ld=ld \
+// RUN:     --gcc-toolchain=%S/Inputs/basic_cross_hurd_tree/usr \
+// RUN:     --target=x86_64-pc-gnu \
+// RUN:   | FileCheck --check-prefix=CHECK-64-CROSS %s
+// CHECK-64-CROSS: "-cc1" "-triple" "x86_64-pc-hurd-gnu"
+// CHECK-64-CROSS: "{{.*}}/Inputs/basic_cross_hurd_tree/usr/lib/gcc/x86_64-gnu/10/../../../../x86_64-gnu/bin/as" "--64"
+// CHECK-64-CROSS: "{{.*}}/Inputs/basic_cross_hurd_tree/usr/lib/gcc/x86_64-gnu/10/../../../../x86_64-gnu/bin/ld" {{.*}} "-m" "elf_x86_64"
+// CHECK-64-CROSS: "{{.*}}/Inputs/basic_cross_hurd_tree/usr/lib/gcc/x86_64-gnu/10/crtbegin.o"
+// CHECK-64-CROSS: "-L{{.*}}/Inputs/basic_cross_hurd_tree/usr/lib/gcc/x86_64-gnu/10/../../../../x86_64-gnu/lib"
diff --git a/llvm/unittests/TargetParser/TripleTest.cpp b/llvm/unittests/TargetParser/TripleTest.cpp
index d3bde2986ea2cd..e6018a1e0aaa83 100644
--- a/llvm/unittests/TargetParser/TripleTest.cpp
+++ b/llvm/unittests/TargetParser/TripleTest.cpp
@@ -111,6 +111,12 @@ TEST(TripleTest, ParsedIDs) {
   EXPECT_EQ(Triple::Linux, T.getOS());
   EXPECT_EQ(Triple::MuslX32, T.getEnvironment());
 
+  T = Triple("x86_64-pc-hurd-gnu");
+  EXPECT_EQ(Triple::x86_64, T.getArch());
+  EXPECT_EQ(Triple::PC, T.getVendor());
+  EXPECT_EQ(Triple::Hurd, T.getOS());
+  EXPECT_EQ(Triple::GNU, T.getEnvironment());
+
   T = Triple("arm-unknown-linux-android16");
   EXPECT_EQ(Triple::arm, T.getArch());
   EXPECT_EQ(Triple::UnknownVendor, T.getVendor());



More information about the cfe-commits mailing list