Update Trac ticket URLs to point to GitLab
[ghc.git] / compiler / main / SysTools / Info.hs
1 {-# LANGUAGE ScopedTypeVariables #-}
2 -----------------------------------------------------------------------------
3 --
4 -- Compiler information functions
5 --
6 -- (c) The GHC Team 2017
7 --
8 -----------------------------------------------------------------------------
9 module SysTools.Info where
10
11 import Exception
12 import ErrUtils
13 import DynFlags
14 import Outputable
15 import Util
16
17 import Data.List
18 import Data.IORef
19
20 import System.IO
21
22 import Platform
23 import GhcPrelude
24
25 import SysTools.Process
26
27 {- Note [Run-time linker info]
28
29 See also: #5240, #6063, #10110
30
31 Before 'runLink', we need to be sure to get the relevant information
32 about the linker we're using at runtime to see if we need any extra
33 options. For example, GNU ld requires '--reduce-memory-overheads' and
34 '--hash-size=31' in order to use reasonable amounts of memory (see
35 trac #5240.) But this isn't supported in GNU gold.
36
37 Generally, the linker changing from what was detected at ./configure
38 time has always been possible using -pgml, but on Linux it can happen
39 'transparently' by installing packages like binutils-gold, which
40 change what /usr/bin/ld actually points to.
41
42 Clang vs GCC notes:
43
44 For gcc, 'gcc -Wl,--version' gives a bunch of output about how to
45 invoke the linker before the version information string. For 'clang',
46 the version information for 'ld' is all that's output. For this
47 reason, we typically need to slurp up all of the standard error output
48 and look through it.
49
50 Other notes:
51
52 We cache the LinkerInfo inside DynFlags, since clients may link
53 multiple times. The definition of LinkerInfo is there to avoid a
54 circular dependency.
55
56 -}
57
58 {- Note [ELF needed shared libs]
59
60 Some distributions change the link editor's default handling of
61 ELF DT_NEEDED tags to include only those shared objects that are
62 needed to resolve undefined symbols. For Template Haskell we need
63 the last temporary shared library also if it is not needed for the
64 currently linked temporary shared library. We specify --no-as-needed
65 to override the default. This flag exists in GNU ld and GNU gold.
66
67 The flag is only needed on ELF systems. On Windows (PE) and Mac OS X
68 (Mach-O) the flag is not needed.
69
70 -}
71
72 {- Note [Windows static libGCC]
73
74 The GCC versions being upgraded to in #10726 are configured with
75 dynamic linking of libgcc supported. This results in libgcc being
76 linked dynamically when a shared library is created.
77
78 This introduces thus an extra dependency on GCC dll that was not
79 needed before by shared libraries created with GHC. This is a particular
80 issue on Windows because you get a non-obvious error due to this missing
81 dependency. This dependent dll is also not commonly on your path.
82
83 For this reason using the static libgcc is preferred as it preserves
84 the same behaviour that existed before. There are however some very good
85 reasons to have the shared version as well as described on page 181 of
86 https://gcc.gnu.org/onlinedocs/gcc-5.2.0/gcc.pdf :
87
88 "There are several situations in which an application should use the
89 shared ‘libgcc’ instead of the static version. The most common of these
90 is when the application wishes to throw and catch exceptions across different
91 shared libraries. In that case, each of the libraries as well as the application
92 itself should use the shared ‘libgcc’. "
93
94 -}
95
96 neededLinkArgs :: LinkerInfo -> [Option]
97 neededLinkArgs (GnuLD o) = o
98 neededLinkArgs (GnuGold o) = o
99 neededLinkArgs (LlvmLLD o) = o
100 neededLinkArgs (DarwinLD o) = o
101 neededLinkArgs (SolarisLD o) = o
102 neededLinkArgs (AixLD o) = o
103 neededLinkArgs UnknownLD = []
104
105 -- Grab linker info and cache it in DynFlags.
106 getLinkerInfo :: DynFlags -> IO LinkerInfo
107 getLinkerInfo dflags = do
108 info <- readIORef (rtldInfo dflags)
109 case info of
110 Just v -> return v
111 Nothing -> do
112 v <- getLinkerInfo' dflags
113 writeIORef (rtldInfo dflags) (Just v)
114 return v
115
116 -- See Note [Run-time linker info].
117 getLinkerInfo' :: DynFlags -> IO LinkerInfo
118 getLinkerInfo' dflags = do
119 let platform = targetPlatform dflags
120 os = platformOS platform
121 (pgm,args0) = pgm_l dflags
122 args1 = map Option (getOpts dflags opt_l)
123 args2 = args0 ++ args1
124 args3 = filter notNull (map showOpt args2)
125
126 -- Try to grab the info from the process output.
127 parseLinkerInfo stdo _stde _exitc
128 | any ("GNU ld" `isPrefixOf`) stdo =
129 -- GNU ld specifically needs to use less memory. This especially
130 -- hurts on small object files. #5240.
131 -- Set DT_NEEDED for all shared libraries. #10110.
132 -- TODO: Investigate if these help or hurt when using split sections.
133 return (GnuLD $ map Option ["-Wl,--hash-size=31",
134 "-Wl,--reduce-memory-overheads",
135 -- ELF specific flag
136 -- see Note [ELF needed shared libs]
137 "-Wl,--no-as-needed"])
138
139 | any ("GNU gold" `isPrefixOf`) stdo =
140 -- GNU gold only needs --no-as-needed. #10110.
141 -- ELF specific flag, see Note [ELF needed shared libs]
142 return (GnuGold [Option "-Wl,--no-as-needed"])
143
144 | any ("LLD" `isPrefixOf`) stdo =
145 return (LlvmLLD $ map Option [
146 -- see Note [ELF needed shared libs]
147 "-Wl,--no-as-needed"])
148
149 -- Unknown linker.
150 | otherwise = fail "invalid --version output, or linker is unsupported"
151
152 -- Process the executable call
153 info <- catchIO (do
154 case os of
155 OSSolaris2 ->
156 -- Solaris uses its own Solaris linker. Even all
157 -- GNU C are recommended to configure with Solaris
158 -- linker instead of using GNU binutils linker. Also
159 -- all GCC distributed with Solaris follows this rule
160 -- precisely so we assume here, the Solaris linker is
161 -- used.
162 return $ SolarisLD []
163 OSAIX ->
164 -- IBM AIX uses its own non-binutils linker as well
165 return $ AixLD []
166 OSDarwin ->
167 -- Darwin has neither GNU Gold or GNU LD, but a strange linker
168 -- that doesn't support --version. We can just assume that's
169 -- what we're using.
170 return $ DarwinLD []
171 OSMinGW32 ->
172 -- GHC doesn't support anything but GNU ld on Windows anyway.
173 -- Process creation is also fairly expensive on win32, so
174 -- we short-circuit here.
175 return $ GnuLD $ map Option
176 [ -- Reduce ld memory usage
177 "-Wl,--hash-size=31"
178 , "-Wl,--reduce-memory-overheads"
179 -- Emit gcc stack checks
180 -- Note [Windows stack usage]
181 , "-fstack-check"
182 -- Force static linking of libGCC
183 -- Note [Windows static libGCC]
184 , "-static-libgcc" ]
185 _ -> do
186 -- In practice, we use the compiler as the linker here. Pass
187 -- -Wl,--version to get linker version info.
188 (exitc, stdo, stde) <- readProcessEnvWithExitCode pgm
189 (["-Wl,--version"] ++ args3)
190 c_locale_env
191 -- Split the output by lines to make certain kinds
192 -- of processing easier. In particular, 'clang' and 'gcc'
193 -- have slightly different outputs for '-Wl,--version', but
194 -- it's still easy to figure out.
195 parseLinkerInfo (lines stdo) (lines stde) exitc
196 )
197 (\err -> do
198 debugTraceMsg dflags 2
199 (text "Error (figuring out linker information):" <+>
200 text (show err))
201 errorMsg dflags $ hang (text "Warning:") 9 $
202 text "Couldn't figure out linker information!" $$
203 text "Make sure you're using GNU ld, GNU gold" <+>
204 text "or the built in OS X linker, etc."
205 return UnknownLD)
206 return info
207
208 -- Grab compiler info and cache it in DynFlags.
209 getCompilerInfo :: DynFlags -> IO CompilerInfo
210 getCompilerInfo dflags = do
211 info <- readIORef (rtccInfo dflags)
212 case info of
213 Just v -> return v
214 Nothing -> do
215 v <- getCompilerInfo' dflags
216 writeIORef (rtccInfo dflags) (Just v)
217 return v
218
219 -- See Note [Run-time linker info].
220 getCompilerInfo' :: DynFlags -> IO CompilerInfo
221 getCompilerInfo' dflags = do
222 let (pgm,_) = pgm_c dflags
223 -- Try to grab the info from the process output.
224 parseCompilerInfo _stdo stde _exitc
225 -- Regular GCC
226 | any ("gcc version" `isInfixOf`) stde =
227 return GCC
228 -- Regular clang
229 | any ("clang version" `isInfixOf`) stde =
230 return Clang
231 -- FreeBSD clang
232 | any ("FreeBSD clang version" `isInfixOf`) stde =
233 return Clang
234 -- XCode 5.1 clang
235 | any ("Apple LLVM version 5.1" `isPrefixOf`) stde =
236 return AppleClang51
237 -- XCode 5 clang
238 | any ("Apple LLVM version" `isPrefixOf`) stde =
239 return AppleClang
240 -- XCode 4.1 clang
241 | any ("Apple clang version" `isPrefixOf`) stde =
242 return AppleClang
243 -- Unknown linker.
244 | otherwise = fail "invalid -v output, or compiler is unsupported"
245
246 -- Process the executable call
247 info <- catchIO (do
248 (exitc, stdo, stde) <-
249 readProcessEnvWithExitCode pgm ["-v"] c_locale_env
250 -- Split the output by lines to make certain kinds
251 -- of processing easier.
252 parseCompilerInfo (lines stdo) (lines stde) exitc
253 )
254 (\err -> do
255 debugTraceMsg dflags 2
256 (text "Error (figuring out C compiler information):" <+>
257 text (show err))
258 errorMsg dflags $ hang (text "Warning:") 9 $
259 text "Couldn't figure out C compiler information!" $$
260 text "Make sure you're using GNU gcc, or clang"
261 return UnknownCC)
262 return info