Compiling Apache httpd into LLVM bytecode using clang - llvm

I want to compile httpd into LLVM bytecode using clang. First I tried compiling it using gcc, for which I did the following:
./configure --prefix=/home/varun/apache/httpd/gcc --with-included-apr
make
sudo make install
And it successfully installs!
Now, I try compiling it with clang, for which I do the following:
CC="clang" CFLAGS="-O4" ./configure --prefix=/home/varun/apache/httpd/clang --with-included-apr
make # didn't come to this step
sudo make install # didn't come to this step
And, the configure itself fails. I chose -O4 as I read that LLVM outputs bytecode if you use -O4 or -emit-llvm as CFLAGS(neither of them work).
This is the error I get:
checking whether the C compiler works... no
configure: error: in `/home/varun/apache/httpd/httpd-2.4.3/srclib/apr':
configure: error: C compiler cannot create executables
Is this related to the the linker not being able to link the LLVM bytecode files?

[I knew it was something related to the linking step, but was not able to get things working. Finally compilation successful, so I'm writing my own answer]
Approach 1 (Failed)
Installed clang on my system using the Synaptic Package Manager.
Installed binutils-gold, because that is required to give the LLVMgold.so as a plugin to the linker. But for this the clang which was installed, should have the gold plugin.
Trying to configure httpd with this command:
CC="clang" CFLAGS="-O4" ./configure --prefix=/home/varun/work/httpd/build --with-included-apr
Here, --with-included-apr is required by httpd. -O4 is required so that the compilation happens through bytecode.
This configuration step itself fails, because the clang which is installed doesn't have the proper plugin for enabling the linker to link bytecode object files.
Approach 2 (Success)
Installed binutils-gold. Also obtained the source of binutils.
Compiling LLVM and using the binutils source code to compile, so that LLVM has the gold plugin.
Compile LLVM,
../configure --with-binutils-include=/usr/src/binutils/binutils-2.22/include --enable-gold --prefix=/usr/local
make
sudo make install
ln -s /usr/local/lib/LLVMgold.so /usr/lib/bdf-plugins/LLVMgold.so
Now, compile httpd
CC="clang" CFLAGS="-O4" ./configure --prefix=/home/varun/work/httpd/build --with-included-apr
make
sudo make install

LLVM bytecode is an intermediate representation used within LLVM. It cannot be executed by any machine. That's why the configure script is complaining that
C compiler cannot create executables.
Do not output LLVM bytecode. Try to use other optimization level instead. (and don't use -emit-llvm in CFLAGS as well).

I answered a very similar question here:
Generate LLVM IR for httpd
It is actually easy to build IR for most well written projects.
Shameless plug for our tool:
https://github.com/SRI-CSL/whole-program-llvm
Good luck.

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.

LLVM compiler-rt i386 target on x86_64 platform

I have been building LLVM and clang 3.8 using svn for some time now. I started using git (this is not the cause of the problem) today and an error interrupted the build process that I have seen before. When make is trying to build the i386 sanitizer library it fails. I was able to disable building the sanitizers in ccmake by setting COMPILER_RT_BUILD_SANITIZERS to OFF. I would prefer to disable building the i386 target altogether. Does anyone know how to do this?
compiler-rt needs to be built out of tree. This is done so that it can be compiled with the newly built clang.
This process will only build the supported architecture, x86_64 in my case.
The following example uses the default install prefix (/usr/local)
to specify the location of llvm-config.
Once LLVM is built, change the directory to where you want compiler-rt
then:
svn co http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mkdir build
cd build
cmake ../compiler-rt -DLLVM_CONFIG_PATH=/usr/local/bin/llvm-config
make
make install

Questions about upgrading scipy via homebrew and how to go about getting a fortran compiler

I try to run
pip install --upgrade scipy
But I get a bunch of errors:
running build_clib
customize UnixCCompiler
customize UnixCCompiler using build_clib
customize Gnu95FCompiler
Could not locate executable gfortran
Could not locate executable f95
customize NAGFCompiler
customize AbsoftFCompiler
Could not locate executable f90
Could not locate executable f77
customize IBMFCompiler
Could not locate executable xlf90
Could not locate executable xlf
customize IntelFCompiler
Could not locate executable ifort
Could not locate executable ifc
customize GnuFCompiler
Could not locate executable g77
customize G95FCompiler
Could not locate executable g95
customize PGroupFCompiler
Could not locate executable pgfortran
don't know how to compile Fortran code on platform 'posix'
building 'dfftpack' library
error: library dfftpack has Fortran sources but no Fortran compiler found
So it looks like I need to install a fortran compiler (don't know if this will solve all my problems). So then I try
brew install gcc
I then get a warning saying:
gcc-4.9.2_1 already installed, it's just not linked
I then try to link it.
brew link gcc
But then I get
Error: Could not symlink bin/c++-4.9
Target /usr/local/bin/c++-4.9
is a symlink belonging to gcc49. You can unlink it:
brew unlink gcc49
Should I do this? I want gcc49 because I'm on a mac and I use gcc49 to get openmp support (and I if I remember correctly, it gets used when I do any NVIDIA stuff). What are the consequences of unlinking gcc49 and linking gcc?
You should only need gcc49 if you really need version 4.9 forever.
To get both an OpenMP-compatible gcc and a fortran compiler, you can:
brew reinstall gcc49 --with-fortran --without-multilib, or
brew unlink gcc49 && brew install gcc --without-multilib
You'll have to rebuild the whole gcc collection either way; sorry!

No Working C Compiler Found

I just downloaded it from here to try it:
http://www.videolan.org/developers/x264.html
I ran ./configure. It complained that I didn't have an assembler and suggested I ran ./configure --disable-asm. Then all seemed to compile OK.
How did you install gcc? From the repositories?
After i check the GCC version by using (gcc --version) command in terminal. The log is
i686-apple-darwin11-llvm-gcc-4.2
(GCC) 4.2.1 (Based on Apple Inc. build 5658)
(LLVM build 2336.11.00)
how can i solve this issue ..? I want to make compile and install.
Check config.log and search for "working C compiler". You will see the error that caused the test to fail.
You really need to supply more information (for example, the output of running configure). "Then all seemed to compile OK." is especially confusing. If "all seemed to compile OK", what is your problem?
it seems that you don't have required yasm,
go through below link & download.
http://www.linuxfromscratch.org/blfs/view/svn/general/yasm.html
Steps to follow :
download from above link
extract files
./configure
make
make install #
yasm --version
if it is 1.3.0 then its OK, otherwise reply me if you are not getting right version details
again try to install your software, you will definitely find right assembler

What's the relationship between binutils and gcc?

As titled, is binutils contained in gcc for Centos Linux?
If I install gcc rpm package, is there need to install binutils also?
What's more, are gcc and g++ both installed by default in Centos?
The gcc package probably contains the compiler proper, e.g. files /usr/bin/gcc and directory /usr/lib/gcc/x86_64-linux-gnu/4.8/ (which contains the cc1 executable).
The /usr/bin/gcc program starts cc1 (or cc1plus etc...) to compile your source code *.c, and also as to translate cc1-generated assembly code (produced by cc1) into object file *.o, and at last ld to link.
Compile once with gcc -v to understand what is happening, it would show the really executed binaries. Notice that gcc is only a driving program (starting other executables like cc1, as, ld ...)
The as and ld programs are provided by binutils -which is needed to compile.
So the binutils package is a required dependency for the gcc package (with many other dependencies, probably including libc and libc-devel, but if you really want you could use some other libc like MUSL libc; the libc is generally providing the dynamic linker like /lib/ld-linux.so*).
Learn how to use rpm (on Centos, or dpkg on Ubuntu & Debian) to query the dependencies between packages.
For development you probably want some other packages. Debian has the build-essential virtual package. Probably CentOS has an equivalent. And you'll surely want to use some libraries (and you want the development packages for them, e.g. on Debian libcurl4-gnutls-dev to develop with the libcurl HTTP client library). See also this answer (for Ubuntu and Debian, but you can adapt it for CentOS).
In 2021 you want to use GCC 10 at least, as g++ -Wall -Wextra -g and you could decide to code your own GCC plugin (checking some coding rules in your C++ code; you also want to document your coding conventions by writing). Be aware of the rule of five.