Dynamically choosing the main function

Harendra Kumar harendra.kumar at gmail.com
Sun Nov 12 18:54:36 UTC 2017


On 12 November 2017 at 23:18, Ben Gamari <ben at smart-cactus.org> wrote:

> 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.
>

I understand that. I was also wondering if there is way (some sort of
annotation, pragma or any other workaround) to keep the symbols around
without actually exporting them.  Exporting either clobbers the module
interface from user point of view or the modules need a wrapper to avoid
exporting such symbols in user exposed modules. I would also expect an
option to remove the effect of any such annotation so that the production
build is not under optimized because of this. This is in fact one of the
problems that I have been facing in general. I do not know of a good way to
hide symbols from users but use them in tests and benchmarks during dev.
What is the GHC recommended way to achieve this?


> 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.
>

Yeah that is a good option for dynamically linked executables. In fact I
have been using dlopen to implement Haskell plugins but did not think of
using it for this use case. Thanks for reminding me. In a static
executable, does it make sense to make the RTS choose an exported symbol as
entry point using an RTS option?

-harendra
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-devs/attachments/20171113/9f9374b1/attachment-0001.html>


More information about the ghc-devs mailing list