Dynamically choosing the main function

Ben Gamari ben at smart-cactus.org
Sun Nov 12 17:48:45 UTC 2017


Harendra Kumar <harendra.kumar at gmail.com> writes:

> Hi,
>
> GHC allows choosing a main function at link time using the "-main-is"
> option. I was wondering if there is a possibility to choose the main
> function at runtime. Or even better, if something equivalent to "ghc -e"
> is somehow possible in a linked binary executable. If not, are there any
> plans to achieve something like that in future? Are there any theoretical,
> practical obstacles to that?
>
In general it's not easy to get a reference to a an arbitrary function
in an object file by it's source name. Unless the symbol is exported the
compiler is free to optimize the function or even drop it altogether.

If you can guarantee that the functions you are about are exported then
in principle you could probably do what you ask from a library. Just
`dlopen` the executable (which would need to be compiled as a position
independent exectuable), and dlsym the appropriately mangled name.

Either way, I don't think think this request is in scope for the
compiler.

Cheers,

- Ben
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 487 bytes
Desc: not available
URL: <http://mail.haskell.org/pipermail/ghc-devs/attachments/20171112/55a727b4/attachment.sig>


More information about the ghc-devs mailing list