Ensure the entrypoint is taken into account when computing the export table #146
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Presently on trunk and - as far as I can tell - since OCaml 3.11, in Cygwin64:
This is strange, since ocaml/ocaml@27780d9#diff-3c8eb806a6be5e7baa5af9546b7e12cdfa1220daae562235a450f87b93dd06e7 in OCaml 3.12 ensures that
debugger.o
is linked into OCaml programs precisely to prevent this problem. The symbol is indeed present:the issue is that it is not included in the flexlink export table:
$ PATH=opt/bin:$PATH ./boot/ocamlrun.exe ./ocamlopt.exe -nostdlib -I ./stdlib -I otherlibs/dynlink -I utils -I parsing -I typing -I bytecomp -I file_formats -I lambda -I middle_end -I middle_end/closure -I middle_end/flambda -I middle_end/flambda/base_types -I asmcomp -I driver -I toplevel -I tools -I runtime -I otherlibs/dynlink -I otherlibs/str -I otherlibs/systhreads -I otherlibs/unix -I otherlibs/runtime_events -g -linkall -I toplevel/native -o ocamlnat.exe compilerlibs/ocamlcommon.cmxa compilerlibs/ocamloptcomp.cmxa compilerlibs/ocamlbytecomp.cmxa otherlibs/dynlink/dynlink.cmxa compilerlibs/ocamltoplevel.cmxa toplevel/topstart.cmx -cclib -show-exports | fgrep caml_debugger_in_use
The reason is that while
-lasmrun
is passed to flexlink, the objects passed on the command line give flexlink no reason to linkstartup_nat.n.o
, which would then causedebugger.n.o
to be considered and consequently these symbols to be added to the export table.The fundamental fix here is that when linking an executable, flexlink now considers the entry point when closing over the required list of objects. For Cygwin, this fix is quite simple as it suffices to consider linking
main
. Cygwin's gcc does linkcrt0.o
, but the full mechanism is technically a bit more involved. This doesn't matter, though, because being a Posix system, the entrypoint is alwaysmain
(notwmain
orWinMain
/wWinMain
, etc.).For both MSVC and mingw-w64, the fix is a little more complex as, being non-Posix, there's not a strict requirement for the entrypoint to be
main
. For mingw-w64, the entrypoint ismainCRTStartup
, but whether that callsmain
orwmain
is determined by-municode
which selects between usingcrt2.o
orcrt2u.o
as the startup object. MSVC is affected both by the/ENTRY
and/SUBSYSTEM
linker options, which select between one ofmainCRTStartup
,wmainCRTStartup
,WinMainCRTStartup
orwWinMainCRTStartup
.MSVCRT.lib
contains each of those symbols in a separate object each of which then has an import reference to eithermain
,wmain
,WinMain
orwWinMain
as required. Since OCaml 4.06, OCaml therefore uses-municode
for mingw-w64 and/ENTRY:wMainCRTStartup
for MSVC.This leads to the main part of this PR - the entrypoint for mingw-w64 and MSVC is therefore defined not in one of the objects or libraries passed on the command line (i.e. in
libasmrun.a
), but in one of the default libraries (eithercrt2u.o
orMSVCRT.lib
). The problem is that while these libraries are scanned, they are not analysed when closing over the objects which need to be linked. This is correct (the default libraries are assumed to be being linked in both executable and DLL) - but the objects should be considered in case they import symbols which do come from the objects and libraries passed on the command line. The changes here therefore mean that for mingw-w64,mainCRTStartup
gets resolved tocrt2u.o
. That module importswmain
which then causesmain.n.o
fromlibasmrun.a
to be linked and, consequentlystartup_nat.n.o
and hencedebugger.n.o
. This PR happens to be an alternate fix (or, rather, workaround) for ocaml/ocaml#13520.