[Git][ghc/ghc][master] llvmGen: Consider Relocatable read-only data as not constantReferences: #18137

Marge Bot gitlab at gitlab.haskell.org
Thu May 21 16:16:57 UTC 2020



 Marge Bot pushed to branch master at Glasgow Haskell Compiler / GHC


Commits:
0004ccb8 by Tuan Le at 2020-05-21T12:16:46-04:00
llvmGen: Consider Relocatable read-only data as not constantReferences: #18137

- - - - -


6 changed files:

- compiler/GHC/Cmm.hs
- compiler/GHC/CmmToC.hs
- compiler/GHC/CmmToLlvm/Data.hs
- + testsuite/tests/codeGen/should_gen_asm/T18137.asm
- + testsuite/tests/codeGen/should_gen_asm/T18137.hs
- testsuite/tests/codeGen/should_gen_asm/all.T


Changes:

=====================================
compiler/GHC/Cmm.hs
=====================================
@@ -12,7 +12,7 @@ module GHC.Cmm (
      CmmBlock, RawCmmDecl,
      Section(..), SectionType(..),
      GenCmmStatics(..), type CmmStatics, type RawCmmStatics, CmmStatic(..),
-     isSecConstant,
+     SectionProtection(..), sectionProtection,
 
      -- ** Blocks containing lists
      GenBasicBlock(..), blockId,
@@ -185,17 +185,33 @@ data SectionType
   | OtherSection String
   deriving (Show)
 
--- | Should a data in this section be considered constant
-isSecConstant :: Section -> Bool
-isSecConstant (Section t _) = case t of
-    Text                    -> True
-    ReadOnlyData            -> True
-    RelocatableReadOnlyData -> True
-    ReadOnlyData16          -> True
-    CString                 -> True
-    Data                    -> False
-    UninitialisedData       -> False
-    (OtherSection _)        -> False
+data SectionProtection
+  = ReadWriteSection
+  | ReadOnlySection
+  | WriteProtectedSection -- See Note [Relocatable Read-Only Data]
+  deriving (Eq)
+
+-- | Should a data in this section be considered constant at runtime
+sectionProtection :: Section -> SectionProtection
+sectionProtection (Section t _) = case t of
+    Text                    -> ReadOnlySection
+    ReadOnlyData            -> ReadOnlySection
+    RelocatableReadOnlyData -> WriteProtectedSection
+    ReadOnlyData16          -> ReadOnlySection
+    CString                 -> ReadOnlySection
+    Data                    -> ReadWriteSection
+    UninitialisedData       -> ReadWriteSection
+    (OtherSection _)        -> ReadWriteSection
+
+{-
+Note [Relocatable Read-Only Data]
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+Relocatable data are only read-only after relocation at the start of the
+program. They should be writable from the source code until then. Failure to
+do so would end up in segfaults at execution when using linkers that do not
+enforce writability of those sections, such as the gold linker.
+-}
 
 data Section = Section SectionType CLabel
 


=====================================
compiler/GHC/CmmToC.hs
=====================================
@@ -129,6 +129,10 @@ pprTop dflags = \case
     pprDataExterns platform lits $$
     pprWordArray dflags (isSecConstant section) lbl lits
   where
+    isSecConstant section = case sectionProtection section of
+      ReadOnlySection -> True
+      WriteProtectedSection -> True
+      _ -> False
     platform = targetPlatform dflags
 
 -- --------------------------------------------------------------------------


=====================================
compiler/GHC/CmmToLlvm/Data.hs
=====================================
@@ -83,7 +83,8 @@ genLlvmData (sec, CmmStaticsRaw lbl xs) = do
                             Section CString _ -> if (platformArch platform == ArchS390X)
                                                     then Just 2 else Just 1
                             _                 -> Nothing
-        const          = if isSecConstant sec then Constant else Global
+        const          = if sectionProtection sec == ReadOnlySection
+                            then Constant else Global
         varDef         = LMGlobalVar label tyAlias link lmsec align const
         globDef        = LMGlobal varDef struct
 


=====================================
testsuite/tests/codeGen/should_gen_asm/T18137.asm
=====================================
@@ -0,0 +1 @@
+\.section	\.data\.rel\.ro\.RelocRoData_SomeData_closure_tbl,"aw",(?:%|@)progbits
\ No newline at end of file


=====================================
testsuite/tests/codeGen/should_gen_asm/T18137.hs
=====================================
@@ -0,0 +1,6 @@
+module RelocRoData
+    ( SomeData(..)
+    )
+where
+
+data SomeData = SomeConstr


=====================================
testsuite/tests/codeGen/should_gen_asm/all.T
=====================================
@@ -9,3 +9,4 @@ test('memcpy-unroll-conprop', is_amd64_codegen, compile_cmp_asm, ['cmm', ''])
 test('memset-unroll', is_amd64_codegen, compile_cmp_asm, ['cmm', ''])
 test('bytearray-memset-unroll', is_amd64_codegen, compile_grep_asm, ['hs', True, ''])
 test('bytearray-memcpy-unroll', is_amd64_codegen, compile_grep_asm, ['hs', True, ''])
+test('T18137', [when(opsys('darwin'), skip), only_ways(llvm_ways)], compile_grep_asm, ['hs', False, '-fllvm -split-sections'])



View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/0004ccb885e534c386ceae21580fc59ec7ad0ede

-- 
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/0004ccb885e534c386ceae21580fc59ec7ad0ede
You're receiving this email because of your account on gitlab.haskell.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-commits/attachments/20200521/84b2b27a/attachment-0001.html>


More information about the ghc-commits mailing list