From 822d849ad922edc6306f96efe3e936359bb4541f Mon Sep 17 00:00:00 2001
From: xiajingze <xiajingze1@huawei.com>
Date: Thu, 31 Oct 2024 16:30:51 +0800
Subject: [PATCH] [Driver] Pass -z\<arg\> and -Wl,-z \<arg\> option to the
 linker

Signed-off-by: xiajingze <xiajingze1@huawei.com>
---
 clang/include/clang/Driver/Options.td |  5 +++++
 clang/lib/Driver/Driver.cpp           | 18 ++++++++++++++++++
 clang/test/Driver/z-args.c            | 15 +++++++++++++++
 3 files changed, 38 insertions(+)
 create mode 100644 clang/test/Driver/z-args.c

diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index 344c8bd49da7..b40046a740a8 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -886,8 +886,13 @@ def Xopenmp_target : Separate<["-"], "Xopenmp-target">, Group<CompileOnly_Group>
 def Xopenmp_target_EQ : JoinedAndSeparate<["-"], "Xopenmp-target=">, Group<CompileOnly_Group>,
   HelpText<"Pass <arg> to the target offloading toolchain identified by <triple>.">,
   MetaVarName<"<triple> <arg>">;
+#ifdef BUILD_FOR_OPENEULER
+def z : JoinedOrSeparate<["-"], "z">, Flags<[LinkerInput]>,
+  HelpText<"Pass -z<arg> or -z <arg> to the linker">, MetaVarName<"<arg>">,
+#else
 def z : Separate<["-"], "z">, Flags<[LinkerInput]>,
   HelpText<"Pass -z <arg> to the linker">, MetaVarName<"<arg>">,
+#endif
   Group<Link_Group>;
 def offload_link : Flag<["--"], "offload-link">, Group<Link_Group>,
   HelpText<"Use the new offloading linker to perform the link job.">;
diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index bd9db7714f95..28b33c8862e4 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -2597,6 +2597,16 @@ void Driver::BuildUniversalActions(Compilation &C, const ToolChain &TC,
   }
 }
 
+#ifdef BUILD_FOR_OPENEULER
+llvm::DenseSet<StringRef> ZArgsList{
+  "defs", "muldefs", "execstack", "noexecstack", "globalaudit", "combreloc",
+  "nocombreloc", "global", "initfirst", "interpose", "lazy", "loadfltr",
+  "nocopyreloc", "nodefaultlib", "nodelete", "nodlopen", "nodump", "now",
+  "origin", "relro", "norelro", "separate-code", "noseparate-code", "common",
+  "nocommon", "text", "notext", "textoff"
+};
+#endif
+
 bool Driver::DiagnoseInputExistence(const DerivedArgList &Args, StringRef Value,
                                     types::ID Ty, bool TypoCorrect) const {
   if (!getCheckInputsExist())
@@ -2673,6 +2683,14 @@ bool Driver::DiagnoseInputExistence(const DerivedArgList &Args, StringRef Value,
   if (IsCLMode() && Ty == types::TY_Object && !Value.startswith("/"))
     return true;
 
+#ifdef BUILD_FOR_OPENEULER
+  if (ZArgsList.find(Value) != ZArgsList.end() ||
+      Value.starts_with("common-page-size=") ||
+      Value.starts_with("max-page-size=") ||
+      Value.starts_with("stack-size="))
+    return true;
+#endif
+
   Diag(clang::diag::err_drv_no_such_file) << Value;
   return false;
 }
diff --git a/clang/test/Driver/z-args.c b/clang/test/Driver/z-args.c
new file mode 100644
index 000000000000..83bb2b570e69
--- /dev/null
+++ b/clang/test/Driver/z-args.c
@@ -0,0 +1,15 @@
+// REQUIRES: build_for_openeuler
+// RUN: %clang -### -znow 2>&1 | FileCheck -check-prefix=CHECK-LINKER %s
+// CHECK-LINKER: "-z" "now"
+
+// RUN: %clang -### -Wl,-z now 2>&1 | FileCheck -check-prefix=CHECK-WLCOMMAZ %s
+// CHECK-WLCOMMAZ: "-z" "now"
+// RUN: %clang -### -Wl,-z -Wl,now 2>&1 | FileCheck \
+// RUN: -check-prefix=CHECK-WLCOMMAZ1 %s
+// CHECK-WLCOMMAZ1: "-z" "now"
+// RUN: %clang -### -Wl,-z -O3 now 2>&1 | FileCheck \
+// RUN: -check-prefix=CHECK-WLCOMMAZ2 %s
+// CHECK-WLCOMMAZ2: "-z" "now"
+// RUN: %clang -### -Wl,-z stack-size=1 2>&1 | FileCheck \
+// RUN: -check-prefix=CHECK-WLCOMMAZ3 %s
+// CHECK-WLCOMMAZ3: "-z" "stack-size=1"
\ No newline at end of file
-- 
2.43.0

