I'm trying to understand the swift runtime and it's security implications, since even parts of the standard library is implemented in C++!!! I'm trying this on Linux at the moment, because that's the platform I'm most familiar with.
I wanted to ask what's the entry point into the swift Runtime from the dynamic-linker/libc, because I could not find the main(int, char**) function either in the swift runtime library or the compiler source code? (I did find SILGenFunction::emitArtificialTopLevel which the compiler seems to be using to artificially inject NSApplicationMain into the application, but I could not find the main(int,char**) function anywhere.)
Since the executable is not statically linked, it has to follow System-V ABI so that ld.so can launch the application correctly and will really appreciate if someone can point out how ld.so/libc hands over control to swift runtime.
The main entry point comes from the main executable, as in C. The Swift runtime code is almost entirely executed on-demand by calls emitted by the compiler as part of code generation (aside from a small .init that registers each image containing Swift code with the runtime for reflection purposes, which is defined in SwiftRT-ELF.cpp for Linux). It does not proactively take control of execution.
So, will it be fair to say that if my main.swift file is the following
$ cat main.swift
print("Hello Swift")
Then the compiler will automatically create a main entry point whose contents will be print("Hello Swift") and libc will basically directly call this synthesized main function without calling any special entry point in the swift runtime (apart from those that are in .init section).
I guess I should look at compiler driver source code to see what it does with main.swift. (Please confirm that's the right place to look at. Thanks for your help.)
Yeah, that should be a good starting point. You can look at the places in SILGen where it calls emitTopLevelFunction to create and populate the main entry point for a sense of what happens there. Mostly, top-level code is just directly emitted into it.