Get an LLVM IR with debuginfo from linux kernel

I get the following linker error when trying to build linux kernel, help me please to fix the issue. Thanks in advance


.

cc @nickdesaulniers

Sounds like a bad interaction between wllvm and Linux’s linker scripts. Probably you need to teach the linker scripts that .llvm_bc exists (thinking of it like, say, DWARF sections) given wllvm wants to use it even in the linked binary (normal LTO only has bitcode for intermediate object files, not the final output).

1 Like

I have never seen the section .llvm_bc. Do you have local modifications to your build of LLVM?

This warning is from --orphan-handling=warn (the diagnostic from LLD is improved in newer versions).

Exactly; we use --orphan-handling=warn to warn when new sections appear in objects and are not placed explicitly in linker scripts.

include/asm-generic/vmlinux.lds.h
arch/x86/kernel/vmlinux.lds.S

What is CC=wllvm?

You should only need make LLVM=1 to build the linux kernel with clang+lld.
https://docs.kernel.org/kbuild/llvm.html

I was assuming it was this GitHub - travitch/whole-program-llvm: A wrapper script to build whole-program LLVM bitcode files

1 Like

Ah thanks!

The wrappers first invoke the compiler as normal. Then, for each object file, they call a bitcode compiler to produce LLVM bitcode. The wrappers also store the location of the generated bitcode file in a dedicated section of the object file.

Yeah so LLD is complaining because we asked it to; in KBuild we set --orphan-handling=warn. So .llvm_bc is not placed anywhere specific relative to other sections. .llvm_bc needs to be added explicitly to a linker script (either of the two I listed above) but not in DISCARDS as that will likely break the tool the expects to consume .llvm_bc if you plan to run it on the final vmlinux ELF executable; or .llvm_bc could be placed in DISCARDS if the tool is to be run on per object file.

Honestly, if the goal is to get llvm bc or ll files for the kernel I suggest two approaches instead of wllvm:

  1. make LLVM=1 lib/string.ll works for most sources in the kernel, except for a few drivers that have questionable make rules.
  2. make LLVM=1 all compile_commands.json will build a json file with all of the build commands summarized. It should be trivial to consume that from a trivial python script, and rerun each command with -emit-llvm -S.

We get this type of question once a month; I assume it’s a homework assignment for a college class or something.

Yes I know, but I use wllvm wrapper to get an IR after build

Thanks, I’ll try.
No actually it’s not a hw, we need to get an llvm ir to analyze the code with our static analyzer.

Which static analyzer?

we get an LLVM IR from C source projects and analyze the code, and report memory leak issues, something like INFER or scan-build, that’s why we need the whole project linked in one .bc

JIC: this method didn’t work:((
but now I’m trying to modify the Makefile to get it

Dear, did you solve this problem?
I am trying to compile the Linux kernel to .bc files with wllvm and facing the same problem.
I’ll appreciate if you have solved this problem and share your knowledge with me.