How to get bitcode llvm after linking? - clang

I am trying to get LLVM IR for a file which is linked with some static libararies.
I tried to link using llvm-link . It just copy the .bc files in one file ( not like native linking).
clang -L$(T_LIB_PATH) -lpthread -emit-llvm gives an error: emit-llvm can not be used with linking. When passing -c option, it gives warning that the linking options were not used.
My main goal is to get .bc file with all resolved symbols and references. How can I achieve that with clang version 3.4.?

You may have a look at wllvm. It is a wrapper on the compiler, which enable to build a project and extract the LLVM bitcode of the whole program.
You need to use wllvm and wllvm++ for C and C++, respectively (after setting some environment variables).

Some symbols come from source code via LLVM IR. IR is short for intermediate representation. Those symbols are easy to handle, just stop in the middle of the build process.
Some others come from a library and probably were generated by some other compiler, one that never makes any IR, and in any case the compiler was run by some other people at some other location. You can't go back in time and make those people build IR for you, even if their compiler has the right options. All you can do is obtain the source code for the libraries and build your entire application from source.

Related

warnings when trying to statically link cross compiled Fortran 90 code to run on Aarch64-linux - one being "relocation truncated to fit"

I am able to cross compile some Fortran 90 code (large block written by someone else so do not want to convert it) using x86_64 GNU/Linux as the build system and aarch64-linux as the host system and using dynamic linking. However, I want to generate a statically linked binary so added -static to the mpif90 call. When I do this, I get this warning:
/home/me/CROSS-REPOS/glibc-2.35/math/../sysdeps/ieee754/dbl-64/e_log.c:106: warning: too many GOT entries for -fpic, please recompile with -fPIC
When I add this flag as in "mpif90 -static -fPIC" the same error appears. Also tried -mcmodel=large option as in "mpif90 -static -mcmodel=large" to no avail.
Then checked the options for "/home/me/CROSS-JUL2022/lib/gcc/aarch64-linux/12.1.0/../../../../aarch64-linux/bin/ld", I see this one, --long-plt (to generate long PLT entries and to handle large .plt/.got displacements). But trying "mpif90 -static -Wl,--long-plt" says --long-plt is not an option. How to invoke this --long-plt option then?
One other thing, I know static linking will make the binaries a fair amount bigger but do not want to carry libs over to the Android device. Furthermore, some reading is indicating that dynamic linking on the Android device could lead to some security issues. Thanks for any suggestions.

LLVM IR for a large C project

I'm new to LLVM, clang etc.
My primary need is to convert C code to LLVM IR (.ll) then parse this to a assembly-like language. For this I was till now working with small length c codes (single file) and to get these converted to LLVM IR I used -
$ clang -emit-llvm -S multiply.c -o multiply.ll
To do this conversion with a large C project, I tried gllvm - https://github.com/SRI-CSL/gllvm
and it was really easy & convenient but I am facing this issue with it - https://github.com/PLSysSec/sys/issues/16
which basically a bug in the llvm-hs repo essentially asking me to pull the commit which has the fix and build it. I'm trying to avoid doing this (is this a better way ?)
So I moved onto another solution of using the gold linker - http://gbalats.github.io/2015/12/10/compiling-autotooled-projects-to-LLVM-bitcode.html
I am now stuck on using this gold plugin to get .bc or .ll files - I want to use this gold plugin to convert a large C project into .ll or .bc files - but the steps only show how to use the linker to get optimised executable. Are there any command line options to only convert it till .bc files ?
Any help would be appreciated.
The llvm-link tool combines multiple .bc files into a single .bc. It does not support native object files at all, nor does it work well with normal linker flags (it does not pretend to be ld).
If you do want changes to the llvm IR due to your native libraries on the system, the gold plugin to llvm also supports a emit-llvm flag. Run a clang or gcc link with -Wl,-plugin-opt=emit-llvm when linking with gold.

LLVM compilation process and LLD

I've been trying to make the switch to LLVM, since I'd like to get more into the whole 'software-dev' scene, and it seems like right now, LLVM is the future. I built LLVM/Clang/LLD/compiler-rt/libcxx from source several times now, both with GNU/GCC and LLVM/Clang.
The problem appears when I try to use the newly compiled compilers. From what I can see, clang is using GNU ld rather than LLVM's lld. Is this true?
LLD seems to be a very limited program from the lld -help output, but from what I have read, it is as full featured as ld. I cannot find documentation on how to use it anywhere -- does anyone know where I can find some kind of comprehensive manual on it?
Thank you.
Pass -fuse-ld=lld to clang to make it use lld for linking. By now, it's in very good shape.
You can pass -v or -### to clang to make it print which linker command it runs or would run.
There's no manual for the moment and depending on platform may work well enough for you. That said, if lld were "production ready" we'd have switched clang to using it by default for the various platforms. It's not there yet so I wouldn't suggest you use it for your day to day development.
The LLVM team say that is production ready because FreeBSD can compile and link a lot of things with LLD.
The documentation on the LLD project can be found on http://lld.llvm.org/.
It's written :
LLD is a drop-in replacement for the GNU linkers.
That accepts the same command line arguments and linker scripts as GNU.
So you can use same arguments than GNU LD.
I know this question is old, but there is a newer solution to it:
To use the ld.lld linker when building any llvm target, just pass -DLLVM_ENABLE_LLD=ON in the commandline to cmake.
//Use lld as C and C++ linker.
LLVM_ENABLE_LLD:BOOL=TRUE
For other cmake projects, pass: -DCMAKE_LINKER=/etc/bin/ld.lld

Make an LLVM ModulePass available on clang command line

I have a ModulePass that's working with the opt tool, but I'm having trouble figuring out how to make it available to clang at the command line. My current workflow for using my pass is:
clang -c -emit-llvm [c-source code files]
llvm-link [llvm bitcode files]
opt -load [PassName].so -[pass-name] [linked llvm file]
llc [resulting bitcode file]
gcc [resulting assembler file] -o [target]
I would like to get my pass integrated with the clang command line so that it could be invoked as part of the build of existing software (e.g. c++ standard library) without having to remake the whole build system for each thing I compile. I've seen hints about how to do this, but I haven't been able to put the pieces together into a working setup.
Run an LLVM Pass Automatically with Clang describes exactly what I want, but the method appears to be deprecated in LLVM 3.4 (PassManagerBuilder has been moved to the legacy namespace).
LLVM - Run Own Pass automatically with clang seems to address the basic issue, but I was hoping I could do this without having to modify clang (which seems to be what's suggested there).
What is the best way to make a new pass available from clang using LLVM 3.4?
Clang still uses PassManagerBuilder as of 3.5 (see the PassManagerBuilderWrapper class in BackendUtil.cpp). So I believe extending it with RegisterStandardPasses, as in my blog post, is still the only way to add a pass to Clang's pass manager.
It's frustratingly difficult to find any information about how deprecated the "old" pass manager infrastructure is. But since Clang is still using it, it can't be that deprecated. :)

Trouble disabling LLVM optimizations via pragma

I have a chunk of code that crashes unless I build with optimizations off. I'm building with LLVM compiler 2.0
I would like to turn off optimizations by wrapping the offending code with a #pragma compiler directive; or turn off optimizations for an entire file.
I've been digging in the clang manual and code; but nothing jumps out at me.
Does anyone know how to change the optimizations for a single CU (as opposed to for the entire app)?
You can set per-file compiler flags in Xcode. In Xcode 4 (which I assume you're using because of the LLVM 2.0 reference), first select the project in the left-hand project browser. Go to the Build Phases tab and expand the Compile Sources build phase.
In there, you can set per-file compiler flags, so you could try going to the offending file and entering in -O0 as a flag to try and disable optimizations for just that file.
GCC has some attributes you can set for this, as pointed out by Johannes in his answer here, but these might not be in LLVM. Also, from the comments there, it appears that these are not even in Apple's customized GCC used for building iOS applications.

Resources