summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Gamari <bgamari.foss@gmail.com>2014-08-15 11:11:17 -0400
committerReid Barton <rwbarton@gmail.com>2014-08-15 11:11:18 -0400
commit5895f2b8ffba72a8393e9f712461e6e5ed7ceced (patch)
tree40daf2661079c8efa502bc19aa7ae6a1dca638e6
parent03a8003e5d3aec97b3a14b2d3c774aad43e0456e (diff)
downloadhaskell-5895f2b8ffba72a8393e9f712461e6e5ed7ceced.tar.gz
LlvmMangler: Be more selective when mangling object types
Summary: We previously did a wholesale replace of `%function` to `%object` to mangle object `.type` annotations. This is bad as it can end up replacing appearances of `"%function"` in the user's code. We now look for a proper `.type` keyword before performing the replacement. Thanks to @rwbarton for pointing out the bug. Test Plan: Previously, $ echo 'main = putStrLn "@function"' > test.hs $ ghc -fllvm test.hs $ ./test @object Now, $ echo 'main = putStrLn "@function"' > test.hs $ ghc -fllvm test.hs $ ./test @function Reviewers: rwbarton, austin Reviewed By: rwbarton, austin Subscribers: phaskell, simonmar, relrod, ezyang, carter Differential Revision: https://phabricator.haskell.org/D150 GHC Trac Issues: #9439
-rw-r--r--compiler/llvmGen/LlvmMangler.hs18
1 files changed, 14 insertions, 4 deletions
diff --git a/compiler/llvmGen/LlvmMangler.hs b/compiler/llvmGen/LlvmMangler.hs
index 7084a2e727..8652a890cf 100644
--- a/compiler/llvmGen/LlvmMangler.hs
+++ b/compiler/llvmGen/LlvmMangler.hs
@@ -58,13 +58,23 @@ llvmFixupAsm dflags f1 f2 = {-# SCC "llvm_mangler" #-} do
hClose w
return ()
+-- | This rewrites @.type@ annotations of function symbols to @%object@.
+-- This is done as the linker can relocate @%functions@ through the
+-- Procedure Linking Table (PLT). This is bad since we expect that the
+-- info table will appear directly before the symbol's location. In the
+-- case that the PLT is used, this will be not an info table but instead
+-- some random PLT garbage.
rewriteSymType :: B.ByteString -> B.ByteString
rewriteSymType s =
- foldl (\s' (typeFunc,typeObj)->replace typeFunc typeObj s') s types
+ B.unlines $ map (rewrite '@' . rewrite '%') $ B.lines s
where
- types = [ (B.pack "@function", B.pack "@object")
- , (B.pack "%function", B.pack "%object")
- ]
+ rewrite :: Char -> B.ByteString -> B.ByteString
+ rewrite prefix x
+ | isType x = replace funcType objType x
+ | otherwise = x
+ where
+ funcType = prefix `B.cons` B.pack "function"
+ objType = prefix `B.cons` B.pack "object"
-- | Splits the file contents into its sections
readSections :: Handle -> Handle -> IO [Section]