Skip to content

Commit

Permalink
[Driver] Default LoongArch to -fno-direct-access-external-data for no…
Browse files Browse the repository at this point in the history
…n-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
  • Loading branch information
MaskRay authored Nov 14, 2023
1 parent 6229cd0 commit 47eeee2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
7 changes: 6 additions & 1 deletion clang/lib/Driver/ToolChains/Clang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5663,10 +5663,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");
Expand Down
6 changes: 6 additions & 0 deletions clang/test/Driver/fdirect-access-external-data.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit 47eeee2

Please sign in to comment.