[LLVMdev] New kind of metadata to capture LLVM IR linking structure

Khilan Gudka Khilan.Gudka at cl.cam.ac.uk
Fri Mar 20 11:30:08 PDT 2015

Hello all

llvm-link merges together the metadata from the IR files being linked
together. This means that when linking different libraries together (i.e.
multiple source files that have been compiled into a single LLVM IR file)
it can be hard or impossible to identify the library boundaries.

We're using LLVM to do static analysis of applications (together with their
dependent libraries) and have found it useful to be able to determine which
library a particular Instruction* or GlobalVariable* came from (e.g. so
that we can ignore some of them, or focus analysis on particular ones).

To preserve this information across linking, I've implemented a new kind of
metadata node MDLLVMModule that records:

1) Which LLVM modules (i.e. LLVM IR file) have been linked into this LLVM
2) Which compilation units directly contribute to this LLVM module (i.e.
that are not part of an LLVM submodule)

The format of the metadata looks like this:

!llvm.module = !{!0}

!0 = !MDLLVMModule(name: "test123.bc", modules: !1, cus: !24)
!1 = !{!2}
!2 = !MDLLVMModule(name: "test12.bc", cus: !3)
!3 = !{!4, !18}
!4 = !MDCompileUnit(... filename: "test1.c" ...)
!18 = !MDCompileUnit(... filename: "test2.c" ...)
!24 = !{!25}
!25 = !MDCompileUnit(... filename: "test3.c" ...)

Each linked LLVM module has the named metadata node "llvm.module" that
to its own MDLLVMModule node. In this example, we see that this is the
for llvm module "test123.bc" that is built up from linking module
and the compilation unit "test3.c." Module "test12.bc" itself is built up
by linking the compilation units "test1.c" and "test2.c"

The name of a module defaults to the base filename of the output file, but
this can be overridden with the (also new) command-line flag -module-name
to llvm-link, as in:

llvm-link -module-name=mytest -o test.bc <files>

I thought this might be useful to the wider LLVM community and would like
to see this added to LLVM.

I have attached a patch that I produced against r232466. I've also added a
corresponding DILLVMModule class.

I would be very grateful if someone could review this.


Khilan Gudka
Research Associate
Security Group
Computer Laboratory
University of Cambridge
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20150320/29a8a5d6/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: llvm.module.metadata.patch
Type: application/octet-stream
Size: 18503 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20150320/29a8a5d6/attachment.obj>

More information about the llvm-dev mailing list