Index: clang/include/clang/Basic/LangOptions.def =================================================================== --- clang/include/clang/Basic/LangOptions.def +++ clang/include/clang/Basic/LangOptions.def @@ -201,6 +201,7 @@ LANGOPT(EnableAIXQuadwordAtomicsABI , 1, 0, "Use 16-byte atomic lock free semantics") COMPATIBLE_VALUE_LANGOPT(PICLevel , 2, 0, "__PIC__ level") COMPATIBLE_VALUE_LANGOPT(PIE , 1, 0, "is pie") +LANGOPT(PicDataIsTextRelative , 1, 1, "Module-local data is text relative with PIC.") LANGOPT(ROPI , 1, 0, "Read-only position independence") LANGOPT(RWPI , 1, 0, "Read-write position independence") COMPATIBLE_LANGOPT(GNUInline , 1, 0, "GNU inline semantics") Index: clang/include/clang/Driver/Options.td =================================================================== --- clang/include/clang/Driver/Options.td +++ clang/include/clang/Driver/Options.td @@ -2692,6 +2692,7 @@ def fno_pic : Flag<["-"], "fno-pic">, Group; def fpie : Flag<["-"], "fpie">, Group; def fno_pie : Flag<["-"], "fno-pie">, Group; +def mno_pic_data_is_text_relative : Flag<["-"], "mno-pic-data-is-text-relative">, Group; 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]>, @@ -6045,6 +6046,8 @@ def pic_is_pie : Flag<["-"], "pic-is-pie">, HelpText<"File is for a position independent executable">, MarshallingInfoFlag>; +def no_pic_data_is_text_relative : Flag<["-"], "no-pic-data-is-text-relative">, + MarshallingInfoFlag>; } // let Flags = [CC1Option, FC1Option, NoDriverOption] Index: clang/lib/AST/ASTContext.cpp =================================================================== --- clang/lib/AST/ASTContext.cpp +++ clang/lib/AST/ASTContext.cpp @@ -11571,7 +11571,12 @@ // units. if (Context.shouldExternalize(D)) return GVA_StrongExternal; - } + } else if (Context.getLangOpts().PIE && + !Context.getLangOpts().PicDataIsTextRelative && + D->getKind() == Decl::Var && L == GVA_Internal) + // Treat GVs local to module as external with PIE if requested. + return GVA_StrongExternal; + return L; } Index: clang/lib/Driver/ToolChains/Clang.cpp =================================================================== --- clang/lib/Driver/ToolChains/Clang.cpp +++ clang/lib/Driver/ToolChains/Clang.cpp @@ -5118,6 +5118,8 @@ CmdArgs.push_back(PICLevel == 1 ? "1" : "2"); if (IsPIE) CmdArgs.push_back("-pic-is-pie"); + if (Args.hasArg(options::OPT_mno_pic_data_is_text_relative)) + CmdArgs.push_back("-no-pic-data-is-text-relative"); } if (RelocationModel == llvm::Reloc::ROPI || Index: clang/lib/Frontend/CompilerInvocation.cpp =================================================================== --- clang/lib/Frontend/CompilerInvocation.cpp +++ clang/lib/Frontend/CompilerInvocation.cpp @@ -4110,6 +4110,8 @@ Diags.Report(diag::err_drv_hlsl_unsupported_target) << T.str(); } + Opts.PicDataIsTextRelative = !Args.hasArg(OPT_no_pic_data_is_text_relative); + return Diags.getNumErrors() == NumErrorsBefore; }