Emulate the behavior of ld with ORC LLJIT


I am trying to compile my code with llvm profiling static library. My goal is to use the Clang Compiler instance and LLJIT to emulate the same behavior as we do this through the command clang -fprofile-generate=./test.profraw test.cpp. I use gdb to get the corresponding commands invoked by the driver.cpp, and try to configure my own Clang frontend compiler to generate the LLVM module.

However, when I try to invoke the ORC LLJIT to link and generate the final executable, I fail to get the profiling result writing to test.profraw. I figure out that we should compile libclang_rt.profile-x86_64.a together with our JIT code in order to get the correct binary. The original command used by driver.cpp is like

/usr/bin/ld -z relro --hash-style=gnu --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o a.out /usr/lib/gcc/x86_64-linux-gnu/11/../../../x86_64-linux-gnu/crt1.o /usr/lib/gcc/x86_64-linux-gnu/11/../../../x86_64-linux-gnu/crti.o /usr/lib/gcc/x86_64-linux-gnu/11/crtbegin.o ... -L/usr/lib ./output.o -u__llvm_profile_runtime ./llvm-project/build-compiler-rt/lib/linux/libclang_rt.profile-x86_64.a -lgcc -as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/x86_64-linux-gnu/11/crtend.o /usr/lib/gcc/x86_64-linux-gnu/11/../../../x86_64-linux-gnu/crtn.o 

where the most important part is -u__llvm_profile_runtime ./llvm-project/build-compiler-rt/lib/linux/libclang_rt.profile-x86_64.a. Apparently I should force LLJIT to resolve __llvm_profile_runtime and introduce the archive to LLJIT. I add the archive to JIT by


but it doesn’t work. I also have no idea how to emulate the -u in LD in LLJIT. How can I do -u and introduce the archive to the JIT session as in ld?

Thank you so much in advance.