llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.55k stars 11.8k forks source link

lli with -jit-kind=orc-lazy can't find symbol 'atexit' for c++ program #48282

Open llvmbot opened 3 years ago

llvmbot commented 3 years ago
Bugzilla Link 48938
Version 10.0
OS Linux
Reporter LLVM Bugzilla Contributor

Extended Description

$ cat main.cpp

#include <iostream>

int main(int argc, char** argv) {
    std::cout << "hi c++ from jit: " << argv[0] << std::endl;
    return 0;
}

Compile with 'no-use-cxa-atexit' $ clang++ -S -emit-llvm main.cpp -stdlib=libstdc++ -fno-use-cxa-atexit

the 'main.ll' load fine with lli 'orc-mcjit' and 'mcjit' $ lli --jit-kind=orc-mcjit ./main.ll hi c++ from jit: ./main.ll

$lli --jit-kind=mcjit ./main.ll hi c++ from jit: ./main.ll

while with orc-lazy, I get undefined symbol

$lli --jit-kind=orc-lazy ./main.ll JIT session error: Symbols not found: [ atexit ] JIT session error: Failed to materialize symbols: { (0x23e0c50, { __orc_lcl.cxx_global_var_init.0, _GLOBALsub_I_main.cpp }) }

The issue can be found on both clang10 and clang11.

llvmbot commented 3 years ago

there is a difference between orc-lazy and the other two kinds(orc-mcjit and mcjit) on handling C++ code:

  1. for orc-mcjit/mcjit, I have to add '-fno-use-cxa-atexit' to make lli happy
  2. while for orc-lazy, 'no-use-cxa-atexit' will trigger this undefined symbol 'atexit'

I tried to write a simple jit to simulate orc-lazy's behavior and fix the unresolved symbol issue but without any luck, addGenerator to impoort symbols from current process didn't work. Lack of throughly understanding of LLVM, I can't fix this on my own.

Hope this info is helpful for your later investigation.