Running a dynamic shared library codeGen pass from clang

Hi,

I was trying to run a custom pass built as a shared library codeGen pass but it keeps giving me this error msg:


fatal error: error in backend: Trying to construct TargetPassConfig without a target machine. Scheduling a CodeGen pass without a target triple set?
PLEASE submit a bug report to https://bugs.llvm.org/ and include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0. Program arguments: clang-12 -target x86_64-unknown-linux-gnu -mcpu=x86_64 -O2 -fstack-protector-all -Xclang -load -Xclang ./libShadowStack.so -S test.c
1. <eof> parser at end of file
Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it):
/usr/lib/x86_64-linux-gnu/libLLVM-12.so.1(_ZN4llvm3sys15PrintStackTraceERNS_11raw_ostreamEi+0x23)[0x7fd8a6747303]
/usr/lib/x86_64-linux-gnu/libLLVM-12.so.1(_ZN4llvm3sys17RunSignalHandlersEv+0x50)[0x7fd8a6745640]
/usr/lib/x86_64-linux-gnu/libLLVM-12.so.1(_ZN4llvm3sys15CleanupOnSignalEm+0xdd)[0x7fd8a6746a5d]
/usr/lib/x86_64-linux-gnu/libLLVM-12.so.1(+0xa4b72a)[0x7fd8a669972a]
/usr/lib/x86_64-linux-gnu/libLLVM-12.so.1(+0xa4b6cb)[0x7fd8a66996cb]
/usr/lib/x86_64-linux-gnu/libLLVM-12.so.1(_ZN4llvm3sys7Process4ExitEib+0x27)[0x7fd8a67424d7]
clang-12[0x412bc0]
/usr/lib/x86_64-linux-gnu/libLLVM-12.so.1(_ZN4llvm18report_fatal_errorERKNS_5TwineEb+0x122)[0x7fd8a66a5102]
/usr/lib/x86_64-linux-gnu/libLLVM-12.so.1(+0xa56fd6)[0x7fd8a66a4fd6]
/usr/lib/x86_64-linux-gnu/libLLVM-12.so.1(+0xfbdd6e)[0x7fd8a6c0bd6e]
/usr/lib/x86_64-linux-gnu/libLLVM-12.so.1(+0xfc0bd3)[0x7fd8a6c0ebd3]
/usr/lib/x86_64-linux-gnu/libLLVM-12.so.1(_ZN4llvm17PMTopLevelManager12schedulePassEPNS_4PassE+0x17b)[0x7fd8a686ebdb]
/usr/lib/x86_64-linux-gnu/libLLVM-12.so.1(_ZN4llvm17PMTopLevelManager12schedulePassEPNS_4PassE+0x13d)[0x7fd8a686eb9d]
./libShadowStack.so(+0x11268)[0x7fd8a3ecf268]
./libShadowStack.so(_ZNSt17_Function_handlerIFvRKN4llvm18PassManagerBuilderERNS0_6legacy15PassManagerBaseEEPS7_E9_M_invokeERKSt9_Any_dataS3_S6_+0x49)[0x7fd8a3ed2b0e]
/usr/lib/x86_64-linux-gnu/libLLVM-12.so.1(_ZNK4llvm18PassManagerBuilder17addExtensionsToPMENS0_16ExtensionPointTyERNS_6legacy15PassManagerBaseE+0xdf)[0x7fd8a758225f]
/usr/lib/x86_64-linux-gnu/libLLVM-12.so.1(_ZN4llvm18PassManagerBuilder25populateModulePassManagerERNS_6legacy15PassManagerBaseE+0xa71)[0x7fd8a7583991]
/usr/lib/x86_64-linux-gnu/libclang-cpp.so.12(_ZN5clang17EmitBackendOutputERNS_17DiagnosticsEngineERKNS_19HeaderSearchOptionsERKNS_14CodeGenOptionsERKNS_13TargetOptionsERKNS_11LangOptionsERKN4llvm10DataLayoutEPNSE_6ModuleENS_13BackendActionESt10unique_ptrINSE_17raw_pwrite_streamESt14default_deleteISM_EE+0x260c)[0x7fd8ac54cc5c]
/usr/lib/x86_64-linux-gnu/libclang-cpp.so.12(+0x180b42f)[0x7fd8ac80e42f]
/usr/lib/x86_64-linux-gnu/libclang-cpp.so.12(_ZN5clang8ParseASTERNS_4SemaEbb+0x244)[0x7fd8ab931f14]
/usr/lib/x86_64-linux-gnu/libclang-cpp.so.12(_ZN5clang14FrontendAction7ExecuteEv+0x48)[0x7fd8acec88c8]
/usr/lib/x86_64-linux-gnu/libclang-cpp.so.12(_ZN5clang16CompilerInstance13ExecuteActionERNS_14FrontendActionE+0x841)[0x7fd8ace7bbc1]
/usr/lib/x86_64-linux-gnu/libclang-cpp.so.12(_ZN5clang25ExecuteCompilerInvocationEPNS_16CompilerInstanceE+0x582)[0x7fd8acf2c012]
clang-12(_Z8cc1_mainN4llvm8ArrayRefIPKcEES2_Pv+0x5fb)[0x41258b]
clang-12[0x410d3e]
/usr/lib/x86_64-linux-gnu/libclang-cpp.so.12(+0x1b96662)[0x7fd8acb99662]
/usr/lib/x86_64-linux-gnu/libLLVM-12.so.1(_ZN4llvm20CrashRecoveryContext9RunSafelyENS_12function_refIFvvEEE+0xdd)[0x7fd8a66996ad]
/usr/lib/x86_64-linux-gnu/libclang-cpp.so.12(_ZNK5clang6driver10CC1Command7ExecuteEN4llvm8ArrayRefINS2_8OptionalINS2_9StringRefEEEEEPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEPb+0x139)[0x7fd8acb98e49]
/usr/lib/x86_64-linux-gnu/libclang-cpp.so.12(_ZNK5clang6driver11Compilation14ExecuteCommandERKNS0_7CommandERPS3_+0x17f)[0x7fd8acb6edff]
/usr/lib/x86_64-linux-gnu/libclang-cpp.so.12(_ZNK5clang6driver11Compilation11ExecuteJobsERKNS0_7JobListERN4llvm15SmallVectorImplISt4pairIiPKNS0_7CommandEEEE+0x77)[0x7fd8acb6f1b7]
/usr/lib/x86_64-linux-gnu/libclang-cpp.so.12(_ZN5clang6driver6Driver18ExecuteCompilationERNS0_11CompilationERN4llvm15SmallVectorImplISt4pairIiPKNS0_7CommandEEEE+0xdc)[0x7fd8acb83bec]
clang-12(main+0x24cd)[0x4105fd]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7fd8a4fa0840]
clang-12(_start+0x29)[0x40de49]
clang: error: clang frontend command failed with exit code 70 (use -v to see invocation)
Ubuntu clang version 12.0.0-++20201209080817+199ec40e7bc-1~exp1~20201209191501.3386
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
clang: note: diagnostic msg:

You might want to use some ways to symbolize the stack trace. The most straight forward way might just be building clang and your Pass with debug info (e.g. -DCMAKE_BUILD_TYPE=Debug when invoking the cmake for building LLVM).

After that you can figure out the problem by either looking at the stack trace or using debuggers like gdb.

-Min