How can I get the path to clang when clang runs the linker? - llvm

I am working with LLVM 8.0 and clang and am performing some work during the optimization and I need to get the path to clang. I am running clang using the -flto flag, so that clang actually calls the linker and ultimately my code. I am doing this on Ubuntu 16.04.
I'm trying to get the image path using readlink("/proc/self/exe");. The clang image was build in my home folder, so the path I specify to run clang is /home/users/me/llvm/build/llvm/bin/clang. I also have a version of clang installed in /usr/bin/clang.
What I get from the call to readlink is: /usr/bin/ld.
What I want to get is: /home/users/me/llvm/bin/clang.
I realize that -flto calls system() with /usr/bin/ld ... as the command to run. But how can I get the clang path? Since I have multiple versions of clang, I cannot use whereis or the like, as it will return either the wrong one or multiple.
Is there a global variable in LLVM, or some way to query LLVM, or some way I have not yet discovered to get the image path for the parent process?
The suggestions for using getppid and using the in the "?/proc//exe" worked perfectly.

The suggestion by #Ismail was perfect. Did exactly what I wanted it to.

Related

How do I run go test with the -msan option on Ubuntu 18.04?

When I try to run the go unit test system with the memory sanity checking I get an error message that I am sure I use to solve by setting my compiler to CLANG.
The Go Command Documenation is a little brief on the matter.
-msan
enable interoperation with memory sanitizer.
Supported only on linux/amd64, linux/arm64
and only with Clang/LLVM as the host C compiler.
On linux/arm64, pie build mode will be used.
In the past I was use I got this to work by calling:
CC=clang go test -msan ./..
However when I do that now I get errors such as:
g++: error: unrecognized argument to -fsanitize= option: ‘memory’
What do I need to do in order to run my golang tests with the memory sanitizer under Ubuntu 18:04 ?
I am using the following versions of tools at the moment:
$ go version
go version go1.14 linux/amd64
$ clang --version
clang version 6.0.0-1ubuntu2 (tags/RELEASE_600/final)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
It requires a version of LLVM that supports -fsanitize=memory on your version of Ubuntu 18.04 that the program runs on. Please try updating and then try it again.
Also do note, on Linux you need at least LLVM 3.8 to get the -fsanitize flag.
The Go tool automatically adds the -fsanitize=memory option to CGO_CPPFLAGS flag as required by clang for linking and that's where it's resulting in an error for you.
Also, make sure to add both CC and CXX (for clang++) flags so to enable compilation using Clang when you're interop your program with C/C++ i.e.,
CC=clang CXX=clang++ go build -msan
Also do refer this link:
https://go.googlesource.com/go/+/go1.7/misc/cgo/testsanitizers/test.bash (bash script)
(or)
https://github.com/golang/go/tree/master/misc/cgo/testsanitizers (*.go files)
It will help you test the sanitizers if they would work on your setup or not.

ld for Clang installation

I installed CLang on CentOS using sudo yum install clang. Then tried using it by firing this command clang++ main.cpp -o main. This complained of missing ld.
Prior to this I had deleted the usr/bin/ld
I read that llvm has it's own linker; I am unable to find one. I also read that llvm lld was removed and the documentation says that one can make do with Clang only. Given that I have removed /usr/bin/ld, how do I get it back? Do I need to get it back? How do I make CLang use the linker that is supposed to be included with Clang?
I am debugging an issue and to one of the steps in trying to fix the issue was to reinstall the compiler, hence I resorted to deleting all the previously installed packaged and files, including ld.

emscripten issue with ubuntu version "aborting from js compiler due to exception: unknown vector type <4 x i8> | undefined"

I cannot compile anything with the current version of emscripten from the ubuntu repos
Here is the error
http://pastebin.com/j5Z0ztTs
I suspect it might be because emscripten is outdated in the repos, but why is there no bug reports??
Could anyone help? I cannot find any updated information.
Thanks in advance.
The reason is because you are using the /usr/bin/clang++ that comes with your Linux distribution.
This version does not support Javascript backend. In order to use emscripten, you have to compile fastcomp (an LLVM clang compiler with Javascript backend added)
If you have not built fastcomp yet, Emscripten won't work.
Check out this page for installation instructions:
http://kripken.github.io/emscripten-site/docs/building_from_source/LLVM-Backend.html
If you have already built fastcomp, then the problem is in your emscriptenrc file, and moreover your PATH.
This is how I worked around the issue:
I created a file called emscriptenrc.sh that did this:
export PATH=/home/mike/emscripten/fastcomp/build/master/bin:/home/mike/emscripten/fastcomp/build/master/include:/home/mike/emscripten/fastcomp/build/master/lib:$PATH
then after I created that file
I rebuilt the ~/.emscripten configuration file by calling
./emcc -v from the emscripten build directory -- this seemed to get the right version of clang registered...
now you can go ahead and compile with emcc
I would suggest reading http://kripken.github.io/emscripten-site/docs/building_from_source/LLVM-Backend.html
as well as the rest of the documentation prior to trying to use emscripten.

Clang, LLVM, and g++

I installed clang to compare its use with g++ (gcc) for some C++ programs; as in, I compared compilation time, error feedback, etc,. for the same programs. I did this mainly for the official LLVM tutorial (implementing a compiler using LLVM).
At one point, I needed to install the LLVM libraries (v. 2.9.), while before I had only downloaded clang. Subsequent to this, compilation with clang++ doesn't seem to work for the following tutorial code; while g++ appears to work with LLVM directives (e.g., 'llvm-config --libs`, etc.). Concurrent to installation of the libraries, I had to also go from the old tutorial I was reading (llvm 2.2., suggesting clang++ syntax), to one for 3.0. (suggesting g++ syntax), as the library include locations had changed.
My questions to explain the changes after downloading the llvm libs:
(1) When I now type 'g++', am I still actually working with gcc, or has llvm/clang set itself as a new default mimicking the gcc syntax? If yes, how can I make sure that I actually use gcc, not clang, when I want to?
(2) If g++ still is gcc, any opinions/guesses why the official clang/llvm tutorial switches from suggesting the use of its own competing compiler back to gcc? (c., eg, here)
My System:
Ubuntu 12.04.
llvm 2.9.
gcc 4.6.3.
clang 3.0.
In order for typing g++ to execute a clang compiler, you'd probably be looking at either a symbolic link called g++ to clang, earlier in your $PATH than the path to the real g++, or else an alias named g++, along the lines of alias g++='clang++'.
You can check for the alias (although I imagine it's unlikely) by typing alias g++, which will let you know if you have any alias set up (and if so, what to)
In order to change the $PATH issue, if it occurs, you'd have to either remove the symbolic link (seems sensible, given if you wanted clang, you could just type clang++ instead of g++) or change the position of the symbolic link in the PATH variable, but since they're likely to exist in /usr/local/bin or something similar, that would render a symbolic link unlikely too!
Given this, probably g++ still calls the gcc g++ compiler, and someone was just a little careless when typing the tutorial - I see only the one mention of g++ on the page you linked, near the bottom?

What is the sanctioned way to build libc++ for clang on Linux?

Edit/Update/Note: Just let clang use libstdc++. Has been working really well for me so far.
===============================
In the past I have been able to succeed by doing something with cmake, but just now I discovered a buildit script inside the lib directory of the http://llvm.org/svn/llvm-project/libcxx/trunk project tree.
This buildit script appears to not make use of libsupc++ which is what the cmake approach that I took earlier used. For instance, this guide shows one cmake incantation to produce a makefile for libc++, which will be able to take care of compiling and installation.
My question is what is the difference between these different ways to produce the LLVM-libc++ and which one should be used? Will they behave differently?
The buildit script does not appear to provide any help for installation. Are there directions anywhere for how to properly install the library? With my previous libc++ built with cmake, I had to always add -lc++ to the linker flags (and the path with -L), which is not necessary in my OS X makefiles.
The libc++ website has a nice overview of the possible ways to build libc++.
I suggest using CMake + libc++abi.
Also see the Arch Linux User Repository build script, which uses the buildit script. I installed libc++ from that and used it with the Arch Linux Clang package succesfully by using
clang++ -std=c++11 -stdlib=libc++ -lc++abi