Index: clang/include/clang/Driver/Options.td =================================================================== --- clang/include/clang/Driver/Options.td +++ clang/include/clang/Driver/Options.td @@ -2697,6 +2697,8 @@ def fno_pic : Flag<["-"], "fno-pic">, Group; def fpie : Flag<["-"], "fpie">, Group; def fno_pie : Flag<["-"], "fno-pie">, Group; +defm pic_data_is_text_relative : SimpleMFlag<"pic-data-is-text-relative", + "Assume", "Don't assume", " data segments are relative to text segment">; def fdirect_access_external_data : Flag<["-"], "fdirect-access-external-data">, Group, Flags<[CC1Option]>, HelpText<"Don't use GOT indirection to reference external data symbols">; def fno_direct_access_external_data : Flag<["-"], "fno-direct-access-external-data">, Group, Flags<[CC1Option]>, Index: clang/lib/Driver/ToolChains/Clang.cpp =================================================================== --- clang/lib/Driver/ToolChains/Clang.cpp +++ clang/lib/Driver/ToolChains/Clang.cpp @@ -5104,6 +5104,25 @@ unsigned PICLevel; bool IsPIE; std::tie(RelocationModel, PICLevel, IsPIE) = ParsePICArgs(TC, Args); + Arg *LastPICDataRelArg = + Args.getLastArg(options::OPT_mno_pic_data_is_text_relative, + options::OPT_mpic_data_is_text_relative); + bool NoPICDataIsTextRelative = false; + if (LastPICDataRelArg) { + if (LastPICDataRelArg->getOption().matches( + options::OPT_mno_pic_data_is_text_relative)) { + NoPICDataIsTextRelative = true; + if (!PICLevel) + D.Diag(diag::err_drv_argument_only_allowed_with) + << "-mno-pic-data-is-text-relative" + << "-fpic/-fpie"; + } + if (!Triple.isSystemZ()) + D.Diag(diag::err_drv_unsupported_opt_for_target) + << (NoPICDataIsTextRelative ? "-mno-pic-data-is-text-relative" + : "-mpic-data-is-text-relative") + << RawTriple.str(); + } bool IsROPI = RelocationModel == llvm::Reloc::ROPI || RelocationModel == llvm::Reloc::ROPI_RWPI; @@ -5132,6 +5151,8 @@ CmdArgs.push_back(PICLevel == 1 ? "1" : "2"); if (IsPIE) CmdArgs.push_back("-pic-is-pie"); + if (NoPICDataIsTextRelative) + CmdArgs.push_back("-mcmodel=medium"); } if (RelocationModel == llvm::Reloc::ROPI || Index: clang/test/Driver/pic.c =================================================================== --- clang/test/Driver/pic.c +++ clang/test/Driver/pic.c @@ -1,5 +1,5 @@ -// Test the driver's control over the PIC behavior. These consist of tests of -// the relocation model flags and the pic level flags passed to CC1. +// Test the driver's control over the PIC behavior. These mainly consist of +// tests of the relocation model flags and the pic level flags passed to CC1. // // CHECK-NO-PIC: "-mrelocation-model" "static" // CHECK-NO-PIC-NOT: "-pic-level" @@ -45,6 +45,11 @@ // // CHECK-NO-UNUSED-ARG-NOT: argument unused during compilation // +// CHECK-NO-PIC-DATA-TEXT-REL: "-mcmodel=medium" +// CHECK-PIC-DATA-TEXT-REL-NOT: "-mcmodel=medium" +// CHECK-NO-PIC-DATA-TEXT-REL-NON-SYSTEMZ: error: unsupported option '-mno-pic-data-is-text-relative' for target 'arm-arm-none-eabi' +// CHECK-PIC-DATA-TEXT-REL-NON-SYSTEMZ: error: unsupported option '-mpic-data-is-text-relative' for target 'arm-arm-none-eabi' +// // RUN: %clang -c %s -target i386-unknown-unknown -### 2>&1 \ // RUN: | FileCheck %s --check-prefix=CHECK-NO-PIC // RUN: %clang -c %s -target i386-unknown-unknown -fpic -### 2>&1 \ @@ -313,3 +318,12 @@ // RUN: | FileCheck %s --check-prefix=CHECK-PIC2 // RUN: %clang -fPIC -c %s -target armv7-pc-windows-gnu -### 2>&1 \ // RUN: | FileCheck %s --check-prefix=CHECK-NO-PIC + +// RUN: %clang -c --target=s390x-linux-gnu -mno-pic-data-is-text-relative %s \ +// RUN: -### 2>&1 | FileCheck %s --check-prefix=CHECK-NO-PIC-DATA-TEXT-REL +// RUN: %clang -c --target=s390x-linux-gnu -mpic-data-is-text-relative %s -### \ +// RUN: 2>&1 | FileCheck %s --check-prefix=CHECK-PIC-DATA-TEXT-REL +// RUN: %clang -c --target=arm-arm-none-eabi -mno-pic-data-is-text-relative %s \ +// RUN: -### 2>&1 | FileCheck %s --check-prefix=CHECK-NO-PIC-DATA-TEXT-REL-NON-SYSTEMZ +// RUN: %clang -c --target=arm-arm-none-eabi -mpic-data-is-text-relative %s \ +// RUN: -### 2>&1 | FileCheck %s --check-prefix=CHECK-PIC-DATA-TEXT-REL-NON-SYSTEMZ