1
votes

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?

3
Thanks @Lei Mou, for editing the question. I should framed the question properly.varuaa

3 Answers

3
votes

[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)

  1. Installed clang on my system using the Synaptic Package Manager.
  2. 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)

  1. Installed binutils-gold. Also obtained the source of binutils.
  2. 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
1
votes

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).

1
votes

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.