[PATCH] D137044: [ClangFE] Add support for option -mno-pic-data-is-text-relative

Jonas Paulsson via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Oct 30 12:11:49 PDT 2022


jonpa created this revision.
jonpa added reviewers: mnadeem, MaskRay, tejohnson, awarzynski, uweigand.
Herald added a subscriber: StephenFan.
Herald added a project: All.
jonpa requested review of this revision.

(Patch in progress)

Add support for this GCC option which has the purpose of disallowing text relative accesses of module local symbols with PIC.

I am fumbling a bit with this and would like to ask for some help as I have no good idea of how this is best done. My idea so far has been to

- patch adjustGVALinkageForAttributes() to change the Linkage to External for these GlobalValues.

- Add Driver option but I could not make this work all the way into CompilerInvocation.cpp. The round-trip check of the arguments is failing... :-/




https://reviews.llvm.org/D137044

Files:
  clang/include/clang/Basic/LangOptions.def
  clang/include/clang/Driver/Options.td
  clang/lib/AST/ASTContext.cpp
  clang/lib/Driver/ToolChains/Clang.cpp
  clang/lib/Frontend/CompilerInvocation.cpp


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;
 }
 
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/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/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<f_Group>;
 def fpie : Flag<["-"], "fpie">, Group<f_Group>;
 def fno_pie : Flag<["-"], "fno-pie">, Group<f_Group>;
+def mno_pic_data_is_text_relative : Flag<["-"], "mno-pic-data-is-text-relative">, Group<m_Group>;
 def fdirect_access_external_data : Flag<["-"], "fdirect-access-external-data">, Group<f_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<f_Group>, Flags<[CC1Option]>,
@@ -6045,6 +6046,8 @@
 def pic_is_pie : Flag<["-"], "pic-is-pie">,
   HelpText<"File is for a position independent executable">,
   MarshallingInfoFlag<LangOpts<"PIE">>;
+def no_pic_data_is_text_relative : Flag<["-"], "no-pic-data-is-text-relative">,
+  MarshallingInfoFlag<LangOpts<"PicDataIsTextRelative">>;
 
 } // let Flags = [CC1Option, FC1Option, NoDriverOption]
 
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")


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D137044.471854.patch
Type: text/x-patch
Size: 3457 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221030/28013fdf/attachment.bin>


More information about the llvm-commits mailing list