SysTools: Add detection support for LLD linker
[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: Trac #5240, Trac #6063, Trac #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. Trac #5240.
131 -- Set DT_NEEDED for all shared libraries. Trac #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. Trac #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 [])
146
147 -- Unknown linker.
148 | otherwise = fail "invalid --version output, or linker is unsupported"
149
150 -- Process the executable call
151 info <- catchIO (do
152 case os of
153 OSSolaris2 ->
154 -- Solaris uses its own Solaris linker. Even all
155 -- GNU C are recommended to configure with Solaris
156 -- linker instead of using GNU binutils linker. Also
157 -- all GCC distributed with Solaris follows this rule
158 -- precisely so we assume here, the Solaris linker is
159 -- used.
160 return $ SolarisLD []
161 OSAIX ->
162 -- IBM AIX uses its own non-binutils linker as well
163 return $ AixLD []
164 OSDarwin ->
165 -- Darwin has neither GNU Gold or GNU LD, but a strange linker
166 -- that doesn't support --version. We can just assume that's
167 -- what we're using.
168 return $ DarwinLD []
169 OSMinGW32 ->
170 -- GHC doesn't support anything but GNU ld on Windows anyway.
171 -- Process creation is also fairly expensive on win32, so
172 -- we short-circuit here.
173 return $ GnuLD $ map Option
174 [ -- Reduce ld memory usage
175 "-Wl,--hash-size=31"
176 , "-Wl,--reduce-memory-overheads"
177 -- Emit gcc stack checks
178 -- Note [Windows stack usage]
179 , "-fstack-check"
180 -- Force static linking of libGCC
181 -- Note [Windows static libGCC]
182 , "-static-libgcc" ]
183 _ -> do
184 -- In practice, we use the compiler as the linker here. Pass
185 -- -Wl,--version to get linker version info.
186 (exitc, stdo, stde) <- readProcessEnvWithExitCode pgm
187 (["-Wl,--version"] ++ args3)
188 c_locale_env
189 -- Split the output by lines to make certain kinds
190 -- of processing easier. In particular, 'clang' and 'gcc'
191 -- have slightly different outputs for '-Wl,--version', but
192 -- it's still easy to figure out.
193 parseLinkerInfo (lines stdo) (lines stde) exitc
194 )
195 (\err -> do
196 debugTraceMsg dflags 2
197 (text "Error (figuring out linker information):" <+>
198 text (show err))
199 errorMsg dflags $ hang (text "Warning:") 9 $
200 text "Couldn't figure out linker information!" $$
201 text "Make sure you're using GNU ld, GNU gold" <+>
202 text "or the built in OS X linker, etc."
203 return UnknownLD)
204 return info
205
206 -- Grab compiler info and cache it in DynFlags.
207 getCompilerInfo :: DynFlags -> IO CompilerInfo
208 getCompilerInfo dflags = do
209 info <- readIORef (rtccInfo dflags)
210 case info of
211 Just v -> return v
212 Nothing -> do
213 v <- getCompilerInfo' dflags
214 writeIORef (rtccInfo dflags) (Just v)
215 return v
216
217 -- See Note [Run-time linker info].
218 getCompilerInfo' :: DynFlags -> IO CompilerInfo
219 getCompilerInfo' dflags = do
220 let (pgm,_) = pgm_c dflags
221 -- Try to grab the info from the process output.
222 parseCompilerInfo _stdo stde _exitc
223 -- Regular GCC
224 | any ("gcc version" `isInfixOf`) stde =
225 return GCC
226 -- Regular clang
227 | any ("clang version" `isInfixOf`) stde =
228 return Clang
229 -- FreeBSD clang
230 | any ("FreeBSD clang version" `isInfixOf`) stde =
231 return Clang
232 -- XCode 5.1 clang
233 | any ("Apple LLVM version 5.1" `isPrefixOf`) stde =
234 return AppleClang51
235 -- XCode 5 clang
236 | any ("Apple LLVM version" `isPrefixOf`) stde =
237 return AppleClang
238 -- XCode 4.1 clang
239 | any ("Apple clang version" `isPrefixOf`) stde =
240 return AppleClang
241 -- Unknown linker.
242 | otherwise = fail "invalid -v output, or compiler is unsupported"
243
244 -- Process the executable call
245 info <- catchIO (do
246 (exitc, stdo, stde) <-
247 readProcessEnvWithExitCode pgm ["-v"] c_locale_env
248 -- Split the output by lines to make certain kinds
249 -- of processing easier.
250 parseCompilerInfo (lines stdo) (lines stde) exitc
251 )
252 (\err -> do
253 debugTraceMsg dflags 2
254 (text "Error (figuring out C compiler information):" <+>
255 text (show err))
256 errorMsg dflags $ hang (text "Warning:") 9 $
257 text "Couldn't figure out C compiler information!" $$
258 text "Make sure you're using GNU gcc, or clang"
259 return UnknownCC)
260 return info