Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to link bitcode with llvm-ar archieve into a single bitcode file?

Tags:

c

linker

llvm

clang

I have read this thread on llvm-dev and is faced with the same problem: I cannot link the llvm-ar archieve library with other bitcode files into another single bitcode file with the help of llvm-link.

clang -emit-llvm -g -c -o main.bc main.c
clang -emit-llvm -g -c -o calc.bc calc.c
llvm-ar rcs libcalc.la calc.bc
llvm-link libcalc.la main.bc -o test

the problem is the same: llvm-link complains

llvm-link: libcalc.la:1:2: error: expected integer
!<arch>
 ^

And after reading How to link object to libraries with LLVM >= 3.1 ? ( no GNU ld ), I also tried a llvm2.9 version of llvm-ld.

llvm-ld --disable-opt libcalc.la main.bc -o test

however libcalc.la is not linked into the module correctly and lli reports:

LLVM ERROR: Program used external function 'Square' which could not be resolved!

So what should i do?

UPDATE

I then read Can't link against static library when compiling objects from LLVM bitcode. and find that llvm-ld WORKS when changing the order:

llvm-ld --disable-opt  main.bc libcalc.la -o test

But llvm-link still fails.

like image 372
Hongxu Chen Avatar asked Sep 24 '13 08:09

Hongxu Chen


People also ask

What is LLVM IR Bitcode?

What is commonly known as the LLVM bitcode file format (also, sometimes anachronistically known as bytecode) is actually two things: a bitstream container format and an encoding of LLVM IR into the container format. The bitstream format is an abstract encoding of structured data, very similar to XML in some ways.

What does LLVM link do?

llvm-link takes several LLVM bitcode files and links them together into a single LLVM bitcode file. It writes the output file to standard output, unless the -o option is used to specify a filename.

What linker does clang use?

Clang can be configured to use one of several different linkers: GNU ld. GNU gold. LLVM's lld.

How do I run a BC file?

It is possible to execute a bc-file with lli command. However that doesn't create a stand alone executable product. There's always the option of using llc to compile to assembly from which you can generate an executable.


1 Answers

  • llvm-link does not support bitcode archives, AFAIK. It simply goes over the input files it was provided, and tries to parse each one as a bitcode file (either binary or textual LLVM IR).
  • llvm-ld doesn't exist in the newer LLVMs, so I would suggest to stay away from it completely.

Just link the separate .bc files together with llvm-link. The archiving of bitcode files doesn't have the same benefits for the linker as in native linking anyway.

like image 152
Eli Bendersky Avatar answered Oct 20 '22 05:10

Eli Bendersky