[PATCH][RFC] Bug #4406: stubs for external functions should be registered even if DlsymStubs are disabled

See http://llvm.org/bugs/show_bug.cgi?id=4406

The testcase at http://llvm.org/bugs/attachment.cgi?id=3141 needs to be
updated to free the machine code with:


Once that's done, though, the AssertingVH is still triggered on
destruction of the Module.

It turns out that the stub for the external "write" is not registered by
JITEmitter::AddStubToCurrentFunction() because DlsymStubs are not

  void JITEmitter::AddStubToCurrentFunction(void *StubAddr) {
    if (!TheJIT->areDlsymStubsEnabled())

It seems wrong. From include/llvm/ExecutionEngine/JITMemoryManager.h, my
understanding is that DlsymStubs are only necessary in certain
situations (like supporting relocations in a different process that the
one doing the JIT), and do not have to be enabled for the testcase to be
correct. (Enabling DlsymStubs, however, does solve the problem, so maybe
that's just what I need to do).

Because the stub is not registered by JITEmitter::AddStubToCurrentFunction(),
JITEmitter::deallocateMemForFunction() cannot dereference it, and it
never gets freed. When the GlobalValue for "write" gets destroyed, the
stub still holds a reference to it, triggering the AssertingVH.

I assume that it is correct for the JIT to create a stub for "write"
even when DlsymStub are disabled. If that's right, then the stub should
be registered by AddStubToCurrentFunction().

If, on the other hand, we don't want stubs in such a case, then there is
another problem elsewhere that turns the call to "write" into an
incorrect MachineRelocation.


Index: lib/ExecutionEngine/JIT/JITEmitter.cpp