Executing code changed with LLVM

Hi everyone,

1. I had a file called server.cpp that i can run using:
g++ -o server server.cpp -lrpc -ldl -lpthread

2. Converted the file into LLVM IR, modified it and saved to file using the WriteBitcodeToFile function. The name of resulting file is “output”.

3. Used llc command for generation of “output.o” file as:

llc -filetype=obj output

4. Used gcc command to create executable as:
gcc -lrpc -ldl -lpthread output.o

Running gcc command gives me errors:
/usr/bin/ld: output.o: relocation R_X86_64_32 against `.rodata.str1.1’ can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: final link failed: nonrepresentable section on output
collect2: error: ld returned 1 exit status

5. Tried it by giving “–relocation-model=pic” flag to llc command. But if i run gcc command after that I get so many “undefined reference to” errors like:
/usr/bin/ld: output.o: in function main': llvm-link:(.text+0x41): undefined reference to rpc::server::server(unsigned short)’
/usr/bin/ld: llvm-link:(.text+0x4d): undefined reference to std::allocator<char>::allocator()' /usr/bin/ld: llvm-link:(.text+0x60): undefined reference to std::__cxx11::basic_string<char, std::char_traits, std::allocator >::basic_string(char const*, std::allocator const&)’
/usr/bin/ld: llvm-link:(.text+0x81): undefined reference to std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()' /usr/bin/ld: llvm-link:(.text+0x8a): undefined reference to std::allocator::~allocator()’
/usr/bin/ld: llvm-link:(.text+0x93): undefined reference to rpc::server::run()' /usr/bin/ld: llvm-link:(.text+0xa5): undefined reference to rpc::server::~server()’
/usr/bin/ld: llvm-link:(.text+0xc8): undefined reference to `std::__cxx11::basic_string<char, std::char_traits, std::allocator >::~basic_string()’

Someone please help me with this. Been stuck on it for so long.
If i don’t have any external libraries, I can run a simple program after modifications using the above steps easily.

The std::* errors are because you need to link against libstdc++. If
you invoked GCC as "g++" I think it would automatically do that, or
you could manually add -lstdc++.

I'm not sure about the rpc::* errors though. The only librpc.so I can
find on Debian (in the sinfo package) does not contain those symbols,
so maybe you have an alternative version that you need to build to go
with your source and make sure it's the one found by ld.

Cheers.

Tim.

Thanks a lot for the response. Upon using g++ or gcc with -lstdc++ flag I get the original error I was getting without the “–relocation-model=pic” flag for llc.i.e

/usr/bin/ld: output.o: relocation R_X86_64_32 against `.rodata.str1.1’ can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: final link failed: nonrepresentable section on output
collect2: error: ld returned 1 exit status

Is there another way I can try to save the modified program to a file and then execute it? Without using “llc” maybe?

Thanks a lot for the response. Upon using g++ or gcc with -lstdc++ flag I get the original error I was getting without the "--relocation-model=pic" flag for llc.i.e

I'm not sure why that would be. PIC should eliminate those kinds of
relocations entirely.

Is there another way I can try to save the modified program to a file and then execute it? Without using "llc" maybe?

You might have more luck using Clang itself to compile the modified IR
(it knows how to, if they have the correct .ll or .bc extension);
you'd have to give it the -fPIC option. But it's a bit of a long shot,
llc should be able to do the job.

Cheers.

Tim.