cx-language / cx

C* is a hybrid low-level/high-level systems programming language focused on performance and productivity.
https://cx-language.github.io/
MIT License
129 stars 9 forks source link

LLVM ERROR: inconsistency in registered CommandLine options / undefined reference to `backtrace' #71

Closed ghost closed 3 years ago

ghost commented 3 years ago

hello I love the idea of this programming language and I would like to try it out, right now I am running on llvm 12 and I managed to set CMakeLists.txt to search for newer version of llvm and compiled it successfully. However it gives me error when running the cx executable. Something to do with invalid arguments:

: CommandLine Error: Option 'debug-pass' registered more than once! LLVM ERROR: inconsistency in registered CommandLine options zsh: abort ./cx

emlai commented 3 years ago

Hello! Which operating system are you using and how did you install LLVM?

emlai commented 3 years ago

I just pushed an LLVM 12 upgrade commit to main. I doubt it will fix your error but you could give it a try just in case.

ghost commented 3 years ago

I,ve tried master again and I still get the same error. I am running my own custom musl based Linux distribution similar to Void Linux and Alpine Linux.

     cmake -G Ninja -Wno-dev \
      -DCMAKE_INSTALL_PREFIX=/an/duyum \
      -DCMAKE_BUILD_TYPE=Release \
      -DLLVM_BUILD_LLVM_DYLIB=ON \
      -DLLVM_LINK_LLVM_DYLIB=ON \
      -DCLANG_LINK_CLANG_DYLIB=ON \
      -DLIBCXX_HAS_MUSL_LIBC=YES \
      -DLIBCXX_CXX_ABI=libcxxabi \
      -DLIBCXX_ENABLE_STATIC_ABI_LIBRARY=ON \
      -DLIBCXXABI_USE_LLVM_UNWINDER=1 \
      -DBUILD_SHARED_LIBS=OFF \
      -DLLVM_INSTALL_UTILS=ON \
      -DLLVM_ENABLE_RTTI=ON \
      -DLLVM_ENABLE_FFI=ON \
      -DLLVM_ENABLE_SPHINX=OFF \
      -DLLVM_ENABLE_DOXYGEN=OFF \
      -DFFI_INCLUDE_DIR=$önek \
      -DLLVM_BINUTILS_INCDIR=$önek \
      -DLLVM_DEFAULT_TARGET_TRIPLE=$CTARGET \
      $ÖZGECİK/$İZGE-$SÜRÜ.src
     ninja all
     DESTDIR=$İZGECİK ninja install

backtrace:

(gdb) run Starting program: /ev/cx/build/cx warning: File "/an/duyum/veri/libstdc++.so.6.0.29-gdb.py" auto-loading has been declined by your `auto-load safe-path' set to "$debugdir:$datadir/auto-load". To enable execution of this file add add-auto-load-safe-path /an/duyum/veri/libstdc++.so.6.0.29-gdb.py line to your configuration file "/ev/.gdbinit". To completely disable this security protection add set auto-load safe-path / line to your configuration file "/ev/.gdbinit". For more information about this security protection see the "Auto-loading safe path" section in the GDB manual. E.g., run from the shell: info "(gdb)Auto-loading safe path" : CommandLine Error: Option 'debug-pass' registered more than once! LLVM ERROR: inconsistency in registered CommandLine options

Program received signal SIGABRT, Aborted. 0x00007ffff7f9713c in setjmp () from /lib/ld-musl-x86_64.so.1 (gdb) bt

0 0x00007ffff7f9713c in setjmp () from /lib/ld-musl-x86_64.so.1

1 0x00007ffff7f972d0 in raise () from /lib/ld-musl-x86_64.so.1

2 0x00007ffff7f6afb4 in abort () from /lib/ld-musl-x86_64.so.1

3 0x00005555561c9e39 in llvm::report_fatal_error(llvm::Twine const&, bool) ()

4 0x00005555561c9f5e in llvm::report_fatal_error(char const*, bool) ()

5 0x00005555561bed50 in (anonymous namespace)::CommandLineParser::addOption(llvm::cl::Option, llvm::cl::SubCommand) ()

6 0x00005555561bf0c4 in llvm::cl::Option::addArgument() ()

7 0x0000555555e9a279 in __static_initialization_and_destruction_0(int, int) [clone .constprop.0] ()

8 0x00007ffff7facac0 in ?? () from /lib/ld-musl-x86_64.so.1

9 0x00007ffff7fadde1 in ?? () from /lib/ld-musl-x86_64.so.1

10 0x00007ffff7f6aa31 in ?? () from /lib/ld-musl-x86_64.so.1

11 0x0000555555ee871e in _start ()

emlai commented 3 years ago

According to https://github.com/ziglang/zig/issues/67, when using a dylib build of LLVM, you need to link to the single LLVM dylib instead of the component libs, like so:

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 65cea2a3..c50b6b4d 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -66,7 +66,11 @@ file(GLOB_RECURSE CX_SOURCES src/*.h src/*.cpp)
 add_executable(cx ${CX_SOURCES})
 target_precompile_headers(cx PRIVATE src/pch.h)

-llvm_map_components_to_libnames(LLVM_LIBS core native linker support)
+if (LLVM_LINK_LLVM_DYLIB)
+    set(LLVM_LIBS LLVM)
+else()
+    llvm_map_components_to_libnames(LLVM_LIBS core native linker support)
+endif()
 list(APPEND LLVM_LIBS clangAST clangBasic clangFrontend clangLex clangParse clangSema)
 target_link_libraries(cx ${LLVM_LIBS})

Does the above patch (or something along those lines) help?

This matches what is being done e.g. in flang: https://github.com/llvm/llvm-project/blob/062644bb399bd05fd0aeb3cb9d52fa0014db0bdd/flang/unittests/Runtime/CMakeLists.txt#L11-L15

ghost commented 3 years ago

Thanks for the patch! It worked great :) Now I have an usable cx binary!

ghost commented 3 years ago

I try to compile a simple example:

void main() { println("Hello world"); }

and I get this error:

/an/duyum/etki/ld: /tmp/cx-062be3.o: relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a PIE object; recompile with -fPIE cx: error: linker command failed with exit code 1 (use -v to see invocation)

if I try to add --fpic option I get this error:

/an/duyum/etki/ld: /tmp/cx-319a30.o: in function _EN3std15printStackTraceE': :(.text+0x858): undefined reference tobacktrace' /an/duyum/etki/ld: :(.text+0x866): undefined reference to `backtrace_symbols' cx: error: linker command failed with exit code 1 (use -v to see invocation)

update: I've added libexecinfo package to cover the backtrace error but didn't help that much.

update2: I've managed to run the example by removing some content of std/os/gnu.cx file for now. I'm very excited to explare the language further. Thanks for all the help I got!

update3: I cloned the master again and I am able to run everything without any patch or changes to the code. Good work!

emlai commented 3 years ago

I added a commit to change the compiler to emit position-independent code by default, that should fix the first error without having to use --fpic.

For the second error: there was a similar issue a while back: #48. Musl libc doesn't have execinfo.h, but in your case the compiler still finds execinfo.h on your system (source). Maybe the compiler finds some other execinfo.h from another libc?

Can you check if this is the case? The relevant code is in parse.cpp:

https://github.com/cx-language/cx/blob/ee51593bc671aa1281a6049a333a8f2225e2e5ae/src/parser/parse.cpp#L1426-L1432

ghost commented 3 years ago

As I mentioned before I am fully able to build and run cx wıth the hello example. I'll only have musl libc so I'll check if that is the case.

update: You were right about it. I seem to have .../zig/libc/include/generic-glibc/execinfo.h as well.

update2: I removed the execinfo.h from the system by uninstalling zig and libexecinfo package. And I have fully working cx that compiles fine. Great!

emlai commented 3 years ago

Oh great!

I'll close this issue then and rename it to more accurately reflect the underlying problem.

Also would be nice if the compiler supported having multiple libc's side by side without finding the wrong header. I created an issue for this: #72. But it isn't critical yet since you got the issue figured out by other means, so this can be implemented later.

Thanks for reporting the issues!