@@ -163,6 +163,40 @@ 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
+ The libraries are:
176
+
177
+ * ` Fortran_main ` : Provides the main entry point ` main ` that then invokes
178
+ ` _QQmain ` with the Fortran program unit. This library has a dependency to
179
+ the ` FortranRuntime ` library.
180
+ * ` FortranRuntime ` : Provides most of the Flang runtime library.
181
+ * ` FortranDecimal ` : Provides operations for decimal numbers.
182
+
183
+ The default is that, when using Flang as the linker, one of the Fortran
184
+ translation units provides the program unit and therefore it is assumed that
185
+ Fortran is the main code part (calling into C/C++ routines via `BIND
186
+ (C)` interfaces). When composing the linker commandline, Flang uses
187
+ ` --whole-archive ` and ` --no-whole-archive ` (Windows: ` /WHOLEARCHIVE: ` ,
188
+ Darwin: * not implemented yet* ) to make sure that all for ` Fortran_main ` is
189
+ processed by the linker. This is done to issue a proper error message when
190
+ multiple definitions of ` main ` occur. This happens, for instance, when linking
191
+ a code that has a Fortran program unit with a C/C++ code that also defines a
192
+ ` main ` function.
193
+
194
+ If the code is C/C++ based and invokes Fortran routines, either use Clang as the
195
+ linker driver (supplying ` FortranRuntime ` and/or ` FortranDecimal ` to the linker
196
+ driver) or use Flang with the ` -fno-fortran-main ` flag. This flag removes
197
+ ` Fortran_main ` from the linker stage and hence requires one of the C/C++
198
+ translation units to provide a definition of the ` main ` function.
199
+
166
200
## Frontend Driver
167
201
Flang's frontend driver is the main interface between compiler developers and
168
202
the Flang frontend. The high-level design is similar to Clang's frontend
0 commit comments