@@ -163,6 +163,63 @@ forward compiler options to the frontend driver, `flang-new -fc1`.
163
163
You can read more on the design of ` clangDriver ` in Clang's [ Driver Design &
164
164
Internals] ( https://clang.llvm.org/docs/DriverInternals.html ) .
165
165
166
+ ## Linker Driver
167
+ When used as a linker, Flang's frontend driver assembles the command line for an
168
+ external linker command (e.g., LLVM's ` lld ` ) and invokes it to create the final
169
+ executable by linking static and shared libraries together with all the
170
+ translation units supplied as object files.
171
+
172
+ By default, the Flang linker driver adds several libraries to the linker
173
+ invocation to make sure that all entrypoints for program start
174
+ (Fortran's program unit) and runtime routines can be resolved by the linker.
175
+
176
+ An abridged example (only showing the Fortran specific linker flags, omission
177
+ indicated by ` [...] ` ) for such a linker invocation on a Linux system would look
178
+ like this:
179
+
180
+ ```
181
+ $ flang -v -o example example.o
182
+ "/usr/bin/ld" [...] example.o [...] "--whole-archive" "-lFortran_main"
183
+ "--no-whole-archive" "-lFortranRuntime" "-lFortranDecimal" [...]
184
+ ```
185
+
186
+ The automatically added libraries are:
187
+
188
+ * ` Fortran_main ` : Provides the main entry point ` main ` that then invokes
189
+ ` _QQmain ` with the Fortran program unit. This library has a dependency to
190
+ the ` FortranRuntime ` library.
191
+ * ` FortranRuntime ` : Provides most of the Flang runtime library.
192
+ * ` FortranDecimal ` : Provides operations for decimal numbers.
193
+
194
+ The default is that, when using Flang as the linker, one of the Fortran
195
+ translation units provides the program unit and therefore it is assumed that
196
+ Fortran is the main code part (calling into C/C++ routines via ` BIND (C) `
197
+ interfaces). When composing the linker commandline, Flang uses
198
+ ` --whole-archive ` and ` --no-whole-archive ` (Windows: ` /WHOLEARCHIVE: ` ,
199
+ Darwin & AIX: * not implemented yet* ) to make sure that all for ` Fortran_main `
200
+ is processed by the linker. This is done to issue a proper error message when
201
+ multiple definitions of ` main ` occur. This happens, for instance, when linking
202
+ a code that has a Fortran program unit with a C/C++ code that also defines a
203
+ ` main ` function. A user may be required to explicitly provide the C++ runtime
204
+ libraries at link time (e.g., via ` -lstdc++ ` for STL)
205
+
206
+ If the code is C/C++ based and invokes Fortran routines, one can either use Clang
207
+ or Flang as the linker driver. If Clang is used, it will automatically all
208
+ required runtime libraries needed by C++ (e.g., for STL) to the linker invocation.
209
+ In this case, one has to explicitly provide the Fortran runtime libraries
210
+ ` FortranRuntime ` and/or ` FortranDecimal ` . An alternative is to use Flang to link
211
+ and use the ` -fno-fortran-main ` flag. This flag removes
212
+ ` Fortran_main ` from the linker stage and hence requires one of the C/C++
213
+ translation units to provide a definition of the ` main ` function. In this case,
214
+ it may be required to explicitly supply C++ runtime libraries as mentioned above.
215
+
216
+ When creating shared or static libraries using Flang with ` -shared ` or ` -static `
217
+ flag, Fortran_main is automatically removed from the linker stage (i.e.,
218
+ ` -fno-fortran-main ` is on by default). It is assumed that when creating a
219
+ static or shared library, the generated library does not need a ` main `
220
+ function, as a final link stage will occur that will provide the ` Fortran_main `
221
+ library when creating the final executable.
222
+
166
223
## Frontend Driver
167
224
Flang's frontend driver is the main interface between compiler developers and
168
225
the Flang frontend. The high-level design is similar to Clang's frontend
0 commit comments