Dynamic linking (dlopen/dlsym) against LLVM from a C program

[Resending as my previous message was sent to moderation. The list
info page doesn't mention this is a subscriber only list]

I'm trying to write a .so which will be loaded using dlopen and called
using dlopen. The application is written in C and I want to use the
LLVM C API jfrom it. Obviously it will end up calling C++ functions
internally but as long as I expose a C API to the application I should
be ok.

I have a code that compiles fine but when I try to call it from the
application dlopen complains about missing C++ symbols. It seems for
some reason the LLVM shared libraries aren't being added to the ELF
NEEDED lines even though other shared libraries are. I switched
--as-needed to --no-as-needed but it hasn't helped.

The link line I'm using is:

clang -Wall -Wmissing-prototypes -Wpointer-arith
-Wdeclaration-after-statement -Wendif-labels
-Wmissing-format-attribute -Wformat-security -fno-strict-aliasing
-fwrapv -g -g -O0 -fpic -I/usr/lib/llvm-3.5/include -DNDEBUG
-D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS
-D__STDC_LIMIT_MACROS -g -O2 -fomit-frame-pointer -fPIC
-Wno-ignored-attributes -shared -o pgjit.so pgjit.o
-L/usr/local/pgsql/lib -Wl,--no-as-needed
-Wl,-rpath,'/usr/local/pgsql/lib',-rpath,'/usr/lib/llvm-3.5/lib',--enable-new-dtags
-I/usr/lib/llvm-3.5/include -DNDEBUG -D_GNU_SOURCE
-D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS
-g -O2 -fomit-frame-pointer -std=c++11 -fvisibility-inlines-hidden
-fno-exceptions -fPIC -Woverloaded-virtual -ffunction-sections
-fdata-sections -Wcast-qual -L/usr/lib/llvm-3.5/lib -lLLVMBitWriter
-lLLVMX86Disassembler -lLLVMX86AsmParser -lLLVMX86CodeGen
-lLLVMSelectionDAG -lLLVMAsmPrinter -lLLVMX86Desc -lLLVMObject
-lLLVMMCParser -lLLVMBitReader -lLLVMX86Info -lLLVMX86AsmPrinter
-lLLVMX86Utils -lLLVMInterpreter -lLLVMJIT -lLLVMCodeGen
-lLLVMScalarOpts -lLLVMInstCombine -lLLVMTransformUtils -lLLVMipa
-lLLVMAnalysis -lLLVMTarget -lLLVMExecutionEngine -lLLVMMC -lLLVMCore
-lLLVMSupport -lz -lpthread -lffi -ledit -ltinfo -ldl -lm -lm

And the error I'm getting from dlopen is ERROR: could not load
library "/home/stark/src/pgjit/pgjit.so":
/home/stark/src/pgjit/pgjit.so: undefined symbol:
_ZTVN10__cxxabiv120__si_class_type_infoE

The NEEDED lines list all the non-llvm libraries but none of the llvm libraries:

Dynamic Section:
  NEEDED libz.so.1
  NEEDED libpthread.so.0
  NEEDED libffi.so.6
  NEEDED libedit.so.2
  NEEDED libtinfo.so.5
  NEEDED libdl.so.2
  NEEDED libm.so.6
  NEEDED libgcc_s.so.1
  NEEDED libc.so.6
  RUNPATH /usr/local/pgsql/lib:/usr/lib/llvm-3.5/lib

What am I doing wrong here?

[Resending as my previous message was sent to moderation. The list
info page doesn't mention this is a subscriber only list]

I'm trying to write a .so which will be loaded using dlopen and called
using dlopen. The application is written in C and I want to use the
LLVM C API jfrom it. Obviously it will end up calling C++ functions
internally but as long as I expose a C API to the application I should
be ok.

I have a code that compiles fine but when I try to call it from the
application dlopen complains about missing C++ symbols. It seems for
some reason the LLVM shared libraries aren't being added to the ELF
NEEDED lines even though other shared libraries are. I switched
--as-needed to --no-as-needed but it hasn't helped.

The link line I'm using is:

clang -Wall -Wmissing-prototypes -Wpointer-arith
-Wdeclaration-after-statement -Wendif-labels
-Wmissing-format-attribute -Wformat-security -fno-strict-aliasing
-fwrapv -g -g -O0 -fpic -I/usr/lib/llvm-3.5/include -DNDEBUG
-D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS
-D__STDC_LIMIT_MACROS -g -O2 -fomit-frame-pointer -fPIC
-Wno-ignored-attributes -shared -o pgjit.so pgjit.o
-L/usr/local/pgsql/lib -Wl,--no-as-needed
-Wl,-rpath,'/usr/local/pgsql/lib',-rpath,'/usr/lib/llvm-3.5/lib',--enable-new-dtags
-I/usr/lib/llvm-3.5/include -DNDEBUG -D_GNU_SOURCE
-D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS
-g -O2 -fomit-frame-pointer -std=c++11 -fvisibility-inlines-hidden
-fno-exceptions -fPIC -Woverloaded-virtual -ffunction-sections
-fdata-sections -Wcast-qual -L/usr/lib/llvm-3.5/lib -lLLVMBitWriter
-lLLVMX86Disassembler -lLLVMX86AsmParser -lLLVMX86CodeGen
-lLLVMSelectionDAG -lLLVMAsmPrinter -lLLVMX86Desc -lLLVMObject
-lLLVMMCParser -lLLVMBitReader -lLLVMX86Info -lLLVMX86AsmPrinter
-lLLVMX86Utils -lLLVMInterpreter -lLLVMJIT -lLLVMCodeGen
-lLLVMScalarOpts -lLLVMInstCombine -lLLVMTransformUtils -lLLVMipa
-lLLVMAnalysis -lLLVMTarget -lLLVMExecutionEngine -lLLVMMC -lLLVMCore
-lLLVMSupport -lz -lpthread -lffi -ledit -ltinfo -ldl -lm -lm

And the error I'm getting from dlopen is ERROR: could not load
library "/home/stark/src/pgjit/pgjit.so":
/home/stark/src/pgjit/pgjit.so: undefined symbol:
_ZTVN10__cxxabiv120__si_class_type_infoE

That particular symbol is in libcxxabi. I think you need `-lcxxabi -lc++`

Thanks. That helped me track it down. What I actually needed was:

  -lc++abi -lstdc++

Dear All,

I have a function which is some thing like this:

define float @ir.test.f32(float %arg1, float %arg2, i1 zeroext %flag) #6 {
br i1 % flag, label %1, label %3

; :1 ; preds = %0
%2 = tail call float @__my_test_1_f32(float %arg1, float %arg2) #8
return float %2

; :3 ; preds = %0
%3 = tail call float @__my_test_2_f32(float %arg1, float %arg2, i1 zeroext %flag) #8
return float %3

}

and my input will always something like
"float @ir.test.f32(float %arg1, float %arg2, true)”
or "float @ir.test.f32(float %arg1, float %arg2, true)”, that is said that the flag is decided before compiling.
So ideally I want something like :
"define float @ir.test.f32(float %arg1, float %arg2, true) #6{
return tail call float @__my_test_1_f32(float %arg1, float %arg2) #8
}

and

"define float @ir.test.f32(float %arg1, float %arg2, false) #6{
return tail call float @__my_test_2_f32(float %arg1, float %arg2) #8
}

therefore, the dynamic branch can be avoided.
I feel that the optimization part can help me to achieve this, is it possible and how?

best
kevin