I am trying to use llvm-lld to link an object that has been compiled with -flto.
I have the ld in the path call lld with all the arguments provided.
I created a simple C++ program, which I compile with clang++:
#include <stdio.h>
int main() {
printf("Goodbye cruel world\n");
return 0;
}
Without the -flto flag I get an executable, with the -flto I get this error:
warning: ignoring unknown argument: -plugin
warning: ignoring unknown argument: -plugin-opt=mcpu=x86-64
Cannot open /tmp/a-f1a2a7.o: Exec format error
clang-3.8: error: linker command failed with exit code 1 (use -v to see invocation)
Here are the arguments being passed to the linker, I see that with -flto the plugin arguments are provided include this plugin option, yet it seems to be unsupported.
--eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o a.out /usr/lib64/gcc/x86_64-pc-linux-gnu/5.2.0/../../../../lib64/crt1.o /usr/lib64/gcc/x86_64-pc-linux-gnu/5.2.0/../../../../lib64/crti.o /usr/lib64/gcc/x86_64-pc-linux-gnu/5.2.0/crtbegin.o -L/usr/lib64/gcc/x86_64-pc-linux-gnu/5.2.0 -L/usr/lib64/gcc/x86_64-pc-linux-gnu/5.2.0/../../../../lib64 -L/lib/../lib64 -L/usr/lib/../lib64 -L/usr/lib64/gcc/x86_64-pc-linux-gnu/5.2.0/../../../../x86_64-pc-linux-gnu/lib -L/usr/lib64/gcc/x86_64-pc-linux-gnu/5.2.0/../../.. -L/mnt/data/tmp/dev/llvm/prev/bin/../lib -L/lib -L/usr/lib -plugin /mnt/data/tmp/dev/llvm/prev/bin/../lib/LLVMgold.so -plugin-opt=mcpu=x86-64 /tmp/a-f1a2a7.o -lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc /usr/lib64/gcc/x86_64-pc-linux-gnu/5.2.0/crtend.o /usr/lib64/gcc/x86_64-pc-linux-gnu/5.2.0/../../../../lib64/crtn.o
Is there a way for lld to link this code?
Yes, you can do it by using gold-linker which supports LTO via plugins:
Here is the instructions how to build and run it
Related
I prepare two very simple c files, main.c and test.c, and use the below command to compile them:
clang --target=aarch64 -c main.c
clang --target=aarch64 -c test.c
But when I use the command clang --target=aarch64 -fuse-ld=lld -v main.o test.o, it actually invoke "/usr/bin/gcc" -fuse-ld=lld
root#6ab3fca322b6:/home/example# clang --target=aarch64 -fuse-ld=lld -v main.o test.o
clang version 13.0.0
Target: aarch64
Thread model: posix
InstalledDir: /usr/local/bin
"/usr/bin/gcc" -fuse-ld=lld -o a.out main.o test.o
ld.lld: error: main.o is incompatible with elf64-x86-64
ld.lld: error: test.o is incompatible with elf64-x86-64
collect2: error: ld returned 1 exit status
clang-13: error: linker (via gcc) command failed with exit code 1 (use -v to see invocation)
But if I manually invoke ld.lld, everything there is no problem just a warning about "cannot find entry symbol _start;".
ld.lld main.o test.o
Also I try to remove the flag --target=aarch64, it still failed:
root#6ab3fca322b6:/home/example# clang -v main.o test.o
clang version 13.0.0
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/local/bin
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/9
Selected GCC installation: /usr/lib/gcc/x86_64-linux-gnu/9
Candidate multilib: .;#m64
Candidate multilib: 32;#m32
Candidate multilib: x32;#mx32
Selected multilib: .;#m64
"/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 /lib/x86_64-linux-gnu/crt1.o /lib/x86_64-linux-gnu/crti.o /usr/lib/gcc/x86_64-linux-gnu/9/crtbegin.o -L/usr/lib/gcc/x86_64-linux-gnu/9 -L/usr/lib/gcc/x86_64-linux-gnu/9/../../../../lib64 -L/lib/x86_64-linux-gnu -L/lib/../lib64 -L/usr/lib/x86_64-linux-gnu -L/usr/lib/../lib64 -L/usr/local/bin/../lib -L/lib -L/usr/lib main.o test.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/x86_64-linux-gnu/9/crtend.o /lib/x86_64-linux-gnu/crtn.o
ld: error: main.o is incompatible with elf64-x86-64
ld: error: test.o is incompatible with elf64-x86-64
clang-13: error: linker command failed with exit code 1 (use -v to see invocation)
I have soft linked the /usr/bin/ld to /usr/bin/ld.lld, but I don't know why it adds some many flags which I don't specify, e.g., -z relro --hash-style=gnu --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2
So my questions are:
why clang --target=aarch64 -fuse-ld=lld can't invoke ld.lld?
why clang can invoke /usr/bin/ld but add some many unspecified flags?
For aarch64, which is the correct way to make clang correctly invoke ld.lld?
I am using clang and lld 13.0.0.
Best Regards,
Simon
I want to use opemmp with clang compile in Windows and here is my compile command:
clang++ 1.cpp -o 1.exe -fopenmp=libomp -I=\"C:\\Program Files\\LLVM\\lib\\clang\\9.0.0\\include\" -L=\"C:\\Program Files\\LLVM\\bin\" -Wall -g -O2 -static-libgcc --target=x86_64-w64-mingw -std=c++17
and there is an error:
C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin\ld.exe: cannot find -lomp
clang: error: linker command failed with exit code 1 (use -v to see invocation)
but I have the "libomp.lib" file and write the link path in command argument. How to solve it?
Thanks!
In macOS Sierra, installation for xgboost with openmp enabled always fails.
From https://xgboost.readthedocs.io/en/latest/build.html,
I've tried:
cp make/config.mk ./config.mk; make -j4
With,
export CC=/usr/local/Cellar/llvm/4.0.0_1/bin/clang
export CXX=/usr/local/Cellar/llvm/4.0.0_1/bin/clang++
export CXX1X=/usr/local/Cellar/llvm/4.0.0_1/bin/clang++
It fails with,
clang-4.0clang-4.0: : warning: argument unused during compilation: '-pthread' [-Wunused-command-line-argument]warning:
argument unused during compilation: '-pthread' [-Wunused-command-line-argument]
ld: library not found for -lomp
ld: library not found for -lomp
clang-4.0: error: linker command failed with exit code 1 (use -v to see invocation)
llvm support openmp, but it fails with ld: library not found for -lomp
related question
Install xgboost on Mac - ld: library not found
But, clang-omp goes to boneyard, and discontinue with llvm's OpenMP support.
I've drilled down to, (with -v option)
/usr/local/Cellar/llvm/4.0.0_1/bin/clang++ -std=c++11 -Wall -Wno-unknown-pragmas -Iinclude -Idmlc-core/include -Irabit/include -I/include -O3 -funroll-loops -msse2 -fPIC -fopenmp -shared -o lib/libxgboost.so build/learner.o build/logging.o build/c_api/c_api.o build/c_api/c_api_error.o build/common/common.o build/common/hist_util.o build/data/data.o build/data/simple_csr_source.o build/data/simple_dmatrix.o build/data/sparse_page_dmatrix.o build/data/sparse_page_raw_format.o build/data/sparse_page_source.o build/data/sparse_page_writer.o build/gbm/gblinear.o build/gbm/gbm.o build/gbm/gbtree.o build/metric/elementwise_metric.o build/metric/metric.o build/metric/multiclass_metric.o build/metric/rank_metric.o build/objective/multiclass_obj.o build/objective/objective.o build/objective/rank_obj.o build/objective/regression_obj.o build/tree/tree_model.o build/tree/tree_updater.o build/tree/updater_colmaker.o build/tree/updater_fast_hist.o build/tree/updater_histmaker.o build/tree/updater_prune.o build/tree/updater_refresh.o build/tree/updater_skmaker.o build/tree/updater_sync.o dmlc-core/libdmlc.a rabit/lib/librabit.a -pthread -lm -fopenmp
SOLVED
cd /usr/local/lib
ln -s /usr/local/Cellar/llvm/4.0.0_1/lib/libomp.dylib libomp.dylib
llvm installation miss its symbolic link.
In my case I solved adding the following linker flag in addition to -lomp:
-Lpath_to_libomp.dylib_folder
or something like:
-Llib/
In my case I used a version of clang that came with Homebrew's llvm instead of the one that came with the Laptop from the factory, and it worked.
Running setup.py bdist_wheel for gnureadline
Error:
DHAVE_RL_CALLBACK -DHAVE_RL_CATCH_SIGNAL -DHAVE_RL_COMPLETION_APPEND_CHARACTER -DHAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK -DHAVE_RL_COMPLETION_MATCHES -DHAVE_RL_COMPLETION_SUPPRESS_APPEND -DHAVE_RL_PRE_INPUT_HOOK -I. -I/usr/include/python2.7 -c Modules/2.x/readline.c -o build/temp.linux-x86_64-2.7/Modules/2.x/readline.o
In file included from Modules/2.x/readline.c:31:0:
./readline/readline.h:385:1: warning: function declaration isn’t a prototype [-Wstrict-prototypes]
gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-z,relro build/temp.linux-x86_64-2.7/Modules/2.x/readline.o readline/libreadline.a readline/libhistory.a -lncurses -o build/lib.linux-x86_64-2.7/gnureadline.so
/usr/bin/ld: cannot find -lncurses
collect2: error: ld returned 1 exit status
error: command 'gcc' failed with exit status 1
Mange Jenkins settings:
Python : CPython-2.7
Path: /usr/bin/python
The problem was gnureadline was in my requirements.txt and when building the virtual environments and installing requirements it could not install the package.
gnureadline isn't required on most OSes. As Ludwig points out in this post:
gnureadline [is only needed] on platforms that don't provide readline
at all, or that substitute readline for an incompatible product like
libedit (i.e. Mac OS X)
So the solution was simply to remove gnureadline from requirements.txt
I cannot seem to get this to work. I configured with the --with-gcc-toolchain= where after the equals I put the directory where gcc was (/usr/lib/gcc-snapshot/bin).
I also looked into the answers to " clang linker problem" but I do not see how I can get the accepted answer to find the correct location, and the symlink answer would have worked, except that all of the directories that it is searching exist (copying the lib folder from gcc-snapshot to one of the include locations does not seem to help).
As suggested in the other question, the output of clang++ test.cpp -v:
bob#bob:~/programming$ clang++ test.cpp -v
clang version 3.3 (trunk 171350)
Target: x86_64-unknown-linux-gnu
Thread model: posix
"/home/bob/programming/build/Release+Asserts/bin/clang" -cc1 -triple x86_64-unknown-linux-gnu -emit-obj -mrelax-all -disable-free -main-file-name test.cpp -mrelocation-model static -mdisable-fp-elim -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -target-cpu x86-64 -target-linker-version 2.22 -momit-leaf-frame-pointer -v -resource-dir /home/bob/programming/build/Release+Asserts/bin/../lib/clang/3.3 -fmodule-cache-path /var/tmp/clang-module-cache -internal-isystem /usr/local/include -internal-isystem /home/bob/programming/build/Release+Asserts/bin/../lib/clang/3.3/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fdeprecated-macro -fdebug-compilation-dir /home/bob/programming -ferror-limit 19 -fmessage-length 80 -mstackrealign -fobjc-runtime=gcc -fcxx-exceptions -fexceptions -fdiagnostics-show-option -fcolor-diagnostics -backend-option -vectorize-loops -o /tmp/test-PWiB4M.o -x c++ test.cpp
clang -cc1 version 3.3 based upon LLVM 3.3svn default target x86_64-unknown-linux-gnu
ignoring nonexistent directory "/include"
#include "..." search starts here:
#include <...> search starts here:
/usr/local/include
/home/bob/programming/build/Release+Asserts/bin/../lib/clang/3.3/include
/usr/include/x86_64-linux-gnu
/usr/include
End of search list.
test.cpp:1:10: fatal error: 'iostream' file not found
#include
^
1 error generated.
clang version 3.3 (trunk revision 171350)
EDIT: There are no .o files in the Release+Asserts folder for the build. Is this normal? If not, why is this the case, and how can I fix it (if I can point it as clang's stdlib, then that works as well).
Edit 2: It also fails to compile int main(){return 0;} as it can't find crtbegin.o. Full output is as follows:
/usr/bin/ld: cannot find crtbegin.o: No such file or directory
/usr/bin/ld: cannot find -lgcc
/usr/bin/ld: cannot find -lgcc_s
clang: error: linker command failed with exit code 1 (use -v to see invocation)
The results after -v are the same as the above, but with the ending error being different.
Config.log:
The full file can be found here, but the first (non-comment) line is: $ ../llvm/configure --with-gcc-toolchain=/usr/lib/gcc-snapshot/gcc
The issue that we've identified is that gcc was installed with --prefix=/usr/lib/gcc-snapshot configure argument. and llvm was configured with --with-gcc-toolchain=/usr/lib/gcc-snapshot/gcc argument. It should be the same as the gcc installation prefix, i.e. --with-gcc-toolchain=/usr/lib/gcc-snapshot.
The question's current first line says " I configured with the --with-gcc-toolchain= where after the equals I put the directory where gcc was (/usr/lib/gcc-snapshot/bin).", and the current last line says "first (non-comment) line is: $ ../llvm/configure --with-gcc-toolchain=/usr/lib/gcc-snapshot/gcc"
That's puzzling, you might have thought that you had put the flag --with-gcc-toolchain=/usr/lib/gcc-snapshot/bin but you had actually put --with-gcc-toolchain=/usr/lib/gcc-snapshot/gcc
Regardless, I think you should try the following.
remove or rename the build directory (the directory where you ran the configure command, i.e. the directory where you found the config.log file) I assume it's the /home/bob/programming/build directory.
now create /home/bob/programming/build directory again. it should be empty now.
cd /home/bob/programming/build
run the following configure command
../llvm/configure --with-gcc-toolchain=/usr/lib/gcc-snapshot
follow through with the rest of your build process.
The issue seems to be that you didn't specify the gcc-toolchain prefix correctly. It should be the same as the --prefix parameter you used while configuring the gcc.
Report back the results here.
Also see Clang 3.2 build broken after building gcc 4.7