diff --git a/0003-Driver-Default-LoongArch-to-fno-direct-access-extern.patch b/0003-Driver-Default-LoongArch-to-fno-direct-access-extern.patch new file mode 100644 index 0000000000000000000000000000000000000000..ad66c009cb6cdc60ebd2a91074521e6df2a56675 --- /dev/null +++ b/0003-Driver-Default-LoongArch-to-fno-direct-access-extern.patch @@ -0,0 +1,66 @@ +From 894681c0e07ea65bfd3b5306e09fb2b6a9f3aad8 Mon Sep 17 00:00:00 2001 +From: Fangrui Song +Date: Tue, 14 Nov 2023 00:43:40 -0800 +Subject: [PATCH] [Driver] Default LoongArch to + -fno-direct-access-external-data for non-PIC (#72221) + +For -fno-pic, if an extern variable is defined in a DSO, a copy +relocation will be needed. However, loongarch*-linux does not and will +not support copy relocations. + +Change Driver to default to -fno-direct-access-external-data for +LoongArch && non-PIC. +Keep Frontend conditions unchanged (-fdirect-access-external-data || +-fno-direct-access-external-data && PIC>0 => direct access). + +Fix #71645 + +(cherry picked from commit 47eeee297775347cbdb7624d6a766c2a3eec4a59) + +Change-Id: I55ea6b71de30e22d159c5ab04f0dfc1436473792 +--- + clang/lib/Driver/ToolChains/Clang.cpp | 7 ++++++- + clang/test/Driver/fdirect-access-external-data.c | 6 ++++++ + 2 files changed, 12 insertions(+), 1 deletion(-) + +diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp +index 8e6f880bec96..a6b1560b1ec6 100644 +--- a/clang/lib/Driver/ToolChains/Clang.cpp ++++ b/clang/lib/Driver/ToolChains/Clang.cpp +@@ -5418,10 +5418,15 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, + // defaults to -fno-direct-access-external-data. Pass the option if different + // from the default. + if (Arg *A = Args.getLastArg(options::OPT_fdirect_access_external_data, +- options::OPT_fno_direct_access_external_data)) ++ options::OPT_fno_direct_access_external_data)) { + if (A->getOption().matches(options::OPT_fdirect_access_external_data) != + (PICLevel == 0)) + A->render(Args, CmdArgs); ++ } else if (PICLevel == 0 && Triple.isLoongArch()) { ++ // Some targets default to -fno-direct-access-external-data even for ++ // -fno-pic. ++ CmdArgs.push_back("-fno-direct-access-external-data"); ++ } + + if (Args.hasFlag(options::OPT_fno_plt, options::OPT_fplt, false)) { + CmdArgs.push_back("-fno-plt"); +diff --git a/clang/test/Driver/fdirect-access-external-data.c b/clang/test/Driver/fdirect-access-external-data.c +index f132b1b088af..a6da776e6977 100644 +--- a/clang/test/Driver/fdirect-access-external-data.c ++++ b/clang/test/Driver/fdirect-access-external-data.c +@@ -9,6 +9,12 @@ + // RUN: %clang -### -c -target aarch64 %s -fpic 2>&1 | FileCheck %s --check-prefix=DEFAULT + // RUN: %clang -### -c -target aarch64 %s -fpic -fdirect-access-external-data 2>&1 | FileCheck %s --check-prefix=DIRECT + ++/// loongarch* targets default to -fno-direct-access-external-data even for -fno-pic. ++// RUN: %clang -### -c --target=loongarch64 -fno-pic %s 2>&1 | FileCheck %s --check-prefix=INDIRECT ++// RUN: %clang -### -c --target=loongarch64 -fpie %s 2>&1 | FileCheck %s --check-prefix=DEFAULT ++// RUN: %clang -### -c --target=loongarch32 -fno-pic -fdirect-access-external-data %s 2>&1 | FileCheck %s --check-prefix=DEFAULT ++// RUN: %clang -### -c --target=loongarch32 -fpie -fdirect-access-external-data %s 2>&1 | FileCheck %s --check-prefix=DIRECT ++ + // DEFAULT-NOT: direct-access-external-data" + // DIRECT: "-fdirect-access-external-data" + // INDIRECT: "-fno-direct-access-external-data" +-- +2.20.1 + diff --git a/clang.spec b/clang.spec index 55d1b37b06a367b8c39d59171a9992420c982582..c8a18ee673721bc31814a3c742ab815c1a4920a5 100644 --- a/clang.spec +++ b/clang.spec @@ -1,4 +1,4 @@ -%define anolis_release 2 +%define anolis_release 3 %global toolchain clang @@ -47,6 +47,7 @@ Patch10: fix-ieee128-cross.diff Patch11: 1001-add-anolis-os-support-in-driver.patch Patch12: 0001-backport-LoongArch-patches.patch Patch13: 0002-LoongArch-fix-symbol-lookup-error.patch +Patch14: 0003-Driver-Default-LoongArch-to-fno-direct-access-extern.patch # Patches for clang-tools-extra # See https://reviews.llvm.org/D120301 @@ -469,6 +470,9 @@ LD_LIBRARY_PATH=%{buildroot}/%{_libdir} %{__ninja} check-all -C %{__cmake_buildd %{python3_sitelib}/clang/ %changelog +* Mon Apr 1 2024 Chen Li - 16.0.6-3 +- LoongArch: fix Segmentation fault when pie is disabled + * Fri Aug 11 2023 Chen Li - 16.0.6-2 - Backport LoongArch patches from llvmorg-17.0.0-rc2 - Add the triple of loongarch64 for anolis os