<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
    <title>nu1lptr - LLVM</title>
    <subtitle>Learner &#x2F;&#x2F; Cybersecurity Ph.D.@UMich CSE &#x2F;&#x2F; Photographer</subtitle>
    <link rel="self" type="application/atom+xml" href="https://shengtuo.me/tags/llvm/atom.xml"/>
    <link rel="alternate" type="text/html" href="https://shengtuo.me"/>
    <generator uri="https://www.getzola.org/">Zola</generator>
    <updated>2026-09-07T18:00:00-07:00</updated>
    <id>https://shengtuo.me/tags/llvm/atom.xml</id>
    <entry xml:lang="en">
        <title>Two Ways to Extract Whole-Program Bitcode</title>
        <published>2026-09-07T18:00:00-07:00</published>
        <updated>2026-09-07T18:00:00-07:00</updated>
        
        <author>
          <name>
            Shengtuo Hu
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://shengtuo.me/blog/whole-program-bitcode/"/>
        <id>https://shengtuo.me/blog/whole-program-bitcode/</id>
        
        <content type="html" xml:base="https://shengtuo.me/blog/whole-program-bitcode/">&lt;p&gt;A compiler sees one source file at a time. That is the whole design: hand it a file,
get back an object file, repeat, and let the linker staple the results together. It
works so well that we forget it is a constraint.&lt;&#x2F;p&gt;
&lt;p&gt;It becomes a constraint the moment you want to analyze a program instead of build it.
Checking a property that spans function boundaries, running a custom compiler pass
over everything at once, auditing every call site of a risky API — each of these needs
the whole program in one piece, in a form built for analysis rather than for
execution. LLVM bitcode is that form: what the compiler works in after it has
understood your code but before it has turned it into machine instructions.&lt;&#x2F;p&gt;
&lt;p&gt;Getting bitcode for one file is easy. Getting it for a whole program, out of a real
build, is not. The difficulty is not that the information is missing but that the
build throws it away. After &lt;code&gt;make&lt;&#x2F;code&gt; finishes, nothing on disk records which source
files went into which binary, or how each one was compiled. The linker knew, briefly,
and did not write it down.&lt;&#x2F;p&gt;
&lt;p&gt;I have built two tools that recover it. They share a name, a goal, and almost no code,
because they come at the problem from opposite ends.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;rllvm-pose-as-the-compiler&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#rllvm-pose-as-the-compiler&quot; aria-label=&quot;Anchor link for: rllvm-pose-as-the-compiler&quot;&gt;&lt;i class=&quot;icon&quot;&gt;&lt;&#x2F;i&gt;&lt;&#x2F;a&gt;
rllvm: pose as the compiler&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;a rel=&quot;noopener&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;shengtuo.me&#x2F;rllvm&#x2F;&quot;&gt;rllvm&lt;&#x2F;a&gt; is a set of compiler wrappers written in Rust. You
point your build at them and build normally:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#31333d;color:#ffffffc4;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#dbbb3d;&quot;&gt;export &lt;&#x2F;span&gt;&lt;span style=&quot;font-weight:bold;color:#a3cbe3;&quot;&gt;CC&lt;&#x2F;span&gt;&lt;span style=&quot;color:#db7c6d;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt;rllvm-cc &lt;&#x2F;span&gt;&lt;span style=&quot;font-weight:bold;color:#a3cbe3;&quot;&gt;CXX&lt;&#x2F;span&gt;&lt;span style=&quot;color:#db7c6d;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt;rllvm-cxx
&lt;&#x2F;span&gt;&lt;span style=&quot;font-weight:bold;color:#a3cbe3;&quot;&gt;.&#x2F;configure &lt;&#x2F;span&gt;&lt;span style=&quot;color:#db7c6d;&quot;&gt;&amp;amp;&amp;amp; &lt;&#x2F;span&gt;&lt;span style=&quot;font-weight:bold;color:#a3cbe3;&quot;&gt;make
&lt;&#x2F;span&gt;&lt;span style=&quot;font-weight:bold;color:#a3cbe3;&quot;&gt;rllvm-get-bc&lt;&#x2F;span&gt;&lt;span&gt; .&#x2F;myprogram      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#707180;&quot;&gt;# produces myprogram.bc
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;how-it-works&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#how-it-works&quot; aria-label=&quot;Anchor link for: how-it-works&quot;&gt;&lt;i class=&quot;icon&quot;&gt;&lt;&#x2F;i&gt;&lt;&#x2F;a&gt;
How it works&lt;&#x2F;h3&gt;
&lt;p&gt;Underneath, each source file compiles twice: once to the object file your build
expects, and once to bitcode written off to the side. The second copy then has to be
findable again, and that is the interesting part. Every object file carries a small
extra section — a labelled region of bytes that object files can hold alongside code
and data — recording the path to that file&#x27;s bitcode. The linker has no idea what that
section means, so it does the one thing linkers do with sections they do not
recognize: it concatenates them. The finished binary ends up carrying a complete list
of the bitcode files that make it up, assembled by a tool that does not know it is
helping.&lt;&#x2F;p&gt;
&lt;figure class=&quot;bc-fig bc-ledger&quot; aria-label=&quot;Five source files each record one bitcode path, and the linker concatenates them into a single section naming the whole program&quot;&gt;
&lt;ol&gt;
&lt;li style=&quot;--i:0&quot;&gt;&lt;span class=&quot;src&quot;&gt;parse.c&lt;&#x2F;span&gt;&lt;span class=&quot;tie&quot; aria-hidden=&quot;true&quot;&gt;&lt;&#x2F;span&gt;&lt;span class=&quot;bc&quot;&gt;parse.bc&lt;&#x2F;span&gt;&lt;&#x2F;li&gt;
&lt;li style=&quot;--i:1&quot;&gt;&lt;span class=&quot;src&quot;&gt;lexer.c&lt;&#x2F;span&gt;&lt;span class=&quot;tie&quot; aria-hidden=&quot;true&quot;&gt;&lt;&#x2F;span&gt;&lt;span class=&quot;bc&quot;&gt;lexer.bc&lt;&#x2F;span&gt;&lt;&#x2F;li&gt;
&lt;li style=&quot;--i:2&quot;&gt;&lt;span class=&quot;src&quot;&gt;codegen.c&lt;&#x2F;span&gt;&lt;span class=&quot;tie&quot; aria-hidden=&quot;true&quot;&gt;&lt;&#x2F;span&gt;&lt;span class=&quot;bc&quot;&gt;codegen.bc&lt;&#x2F;span&gt;&lt;&#x2F;li&gt;
&lt;li style=&quot;--i:3&quot;&gt;&lt;span class=&quot;src&quot;&gt;util.c&lt;&#x2F;span&gt;&lt;span class=&quot;tie&quot; aria-hidden=&quot;true&quot;&gt;&lt;&#x2F;span&gt;&lt;span class=&quot;bc&quot;&gt;util.bc&lt;&#x2F;span&gt;&lt;&#x2F;li&gt;
&lt;li style=&quot;--i:4&quot;&gt;&lt;span class=&quot;src&quot;&gt;main.c&lt;&#x2F;span&gt;&lt;span class=&quot;tie&quot; aria-hidden=&quot;true&quot;&gt;&lt;&#x2F;span&gt;&lt;span class=&quot;bc&quot;&gt;main.bc&lt;&#x2F;span&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;figcaption&gt;Each object file carries one line. The linker concatenates them into &lt;b&gt;__RLLVM,__rllvm_bc&lt;&#x2F;b&gt;, so the finished binary holds the whole list, and &lt;b&gt;rllvm-get-bc&lt;&#x2F;b&gt; reads it back out as &lt;b&gt;prog.bc&lt;&#x2F;b&gt;.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;I still think that is a good trick. It borrows the linker as an accumulator, and it
asks nothing of your project except permission to be its compiler — a knob every build
system already has.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;where-the-idea-came-from&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#where-the-idea-came-from&quot; aria-label=&quot;Anchor link for: where-the-idea-came-from&quot;&gt;&lt;i class=&quot;icon&quot;&gt;&lt;&#x2F;i&gt;&lt;&#x2F;a&gt;
Where the idea came from&lt;&#x2F;h3&gt;
&lt;p&gt;The trick is not mine. &lt;a rel=&quot;noopener&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;travitch&#x2F;whole-program-llvm&quot;&gt;wllvm&lt;&#x2F;a&gt;
established it years ago with Python wrappers that build each file normally, build it
again as bitcode, and record where that bitcode landed in a section called &lt;code&gt;.llvm_bc&lt;&#x2F;code&gt;;
a companion tool, &lt;code&gt;extract-bc&lt;&#x2F;code&gt;, reads the section back out of the finished binary and
links the pieces together. &lt;a rel=&quot;noopener&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;SRI-CSL&#x2F;gllvm&quot;&gt;gllvm&lt;&#x2F;a&gt; is the same
design rewritten in Go, and it runs faster for an unglamorous reason: it generates
bitcode in parallel where the Python wrappers fork and wait. The two differ mostly in
reach, as wllvm can drive gcc through the dragonegg plugin while gllvm is clang-only
and its README rules out combining it with link-time optimization.&lt;&#x2F;p&gt;
&lt;p&gt;rllvm is the third entry in that lineage, and the naming says so: &lt;code&gt;w&lt;&#x2F;code&gt; for
whole-program, &lt;code&gt;g&lt;&#x2F;code&gt; for Go, &lt;code&gt;r&lt;&#x2F;code&gt; for Rust. If gllvm or wllvm already work for you, there
is no urgency to switch.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;what-rllvm-adds&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#what-rllvm-adds&quot; aria-label=&quot;Anchor link for: what-rllvm-adds&quot;&gt;&lt;i class=&quot;icon&quot;&gt;&lt;&#x2F;i&gt;&lt;&#x2F;a&gt;
What rllvm adds&lt;&#x2F;h3&gt;
&lt;p&gt;What rllvm adds, above all, is records that survive being moved. The original design
writes down where each bitcode file landed as an absolute path, which holds up exactly
as long as nothing moves. Rename the directory, run the build inside a container and
read the results outside it, or reuse a cached object file that another machine
compiled, and every recorded path names a directory that does not exist. What makes
this particular failure unpleasant is that extraction does not stop; it simply
resolves fewer files than it should and hands you a smaller program than you asked
for. rllvm can record those paths relative to a root you name at extraction time
instead. Because the section is plain text, both forms can appear in the same binary
and objects built before the change keep working.&lt;&#x2F;p&gt;
&lt;p&gt;The rest of what rllvm adds is reach. Turning on link-time optimization removes the
object file that the original mechanism needs to write into, so rllvm records the path
another way and hides the difference behind the same interface. Its rustc wrapper
copes with cargo naming outputs its own way, so a cargo build gives back bitcode with
dependency crates included. It produces whole-program bitcode for a linked WebAssembly
module rather than only for the pieces that went into it. For projects too large to
link in one pass it can stage the merge directory by directory, or produce an archive
instead. And &lt;code&gt;rllvm-info&lt;&#x2F;code&gt; reports what a module actually contains, which is worth more
than it sounds when the characteristic failure is an empty result.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;what-it-costs&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#what-it-costs&quot; aria-label=&quot;Anchor link for: what-it-costs&quot;&gt;&lt;i class=&quot;icon&quot;&gt;&lt;&#x2F;i&gt;&lt;&#x2F;a&gt;
What it costs&lt;&#x2F;h3&gt;
&lt;p&gt;The weaknesses follow from the same trick as the strengths. The section holds a path,
not the bitcode, so the binary is not self-contained: &lt;code&gt;make clean&lt;&#x2F;code&gt; breaks it, moving
the directory breaks it, and copying the binary to another machine breaks it. Storing
the bitcode itself would fix this, and I have a working prototype, but bitcode is
comparable to or larger than the object code it would ride along with, and I have not
been willing to pay that in binary size. Compiling every file twice is a real tax on a
large C++ project. And because a wrapper has to understand the compiler&#x27;s command line
well enough to know what is being built, one misread flag is enough to make bitcode
stop appearing.&lt;&#x2F;p&gt;
&lt;p&gt;Linkers supply the rest of the difficulty, and each lesson arrived the hard way.
Nothing in the program references that extra section, so a linker that discards
unreferenced data throws it away unless it is explicitly marked to keep. The
WebAssembly linker deletes sections with certain LLVM-standard names while faithfully
keeping every other custom section, which is exactly why rllvm&#x27;s sections are not
named after LLVM&#x27;s.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;rules-rllvm-ask-the-build-system&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#rules-rllvm-ask-the-build-system&quot; aria-label=&quot;Anchor link for: rules-rllvm-ask-the-build-system&quot;&gt;&lt;i class=&quot;icon&quot;&gt;&lt;&#x2F;i&gt;&lt;&#x2F;a&gt;
rules_rllvm: ask the build system&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;a rel=&quot;noopener&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;shengtuo.me&#x2F;rules_rllvm&#x2F;&quot;&gt;rules_rllvm&lt;&#x2F;a&gt; answers the same question for Bazel
builds, and it does not use rllvm to do it.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;why-i-stopped-wrapping-rllvm&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#why-i-stopped-wrapping-rllvm&quot; aria-label=&quot;Anchor link for: why-i-stopped-wrapping-rllvm&quot;&gt;&lt;i class=&quot;icon&quot;&gt;&lt;&#x2F;i&gt;&lt;&#x2F;a&gt;
Why I stopped wrapping rllvm&lt;&#x2F;h3&gt;
&lt;p&gt;I did not start from scratch. I started by wrapping rllvm inside Bazel rules — keep
the wrappers, keep the sections, let Bazel drive them — and spent a while on it before
concluding it could not be made to work properly.&lt;&#x2F;p&gt;
&lt;p&gt;The obstacles were not details. Bazel keeps only the files an action declares in
advance as its outputs, so bitcode written off to the side gets discarded when the
action finishes and never comes back at all when the build runs on another machine.
The absolute paths recorded in those sections are wrong on any machine but the one
that compiled them, which quietly breaks a shared cache. Extraction afterwards is a
separate pass Bazel knows nothing about, so none of it is incremental or cached. And
depending on the rllvm binary tied the whole thing to one processor architecture,
which meant it would not run on my own laptop. Working around all of that meant asking
Bazel to stop doing the things people use Bazel for.&lt;&#x2F;p&gt;
&lt;p&gt;So I stopped wrapping and started again on a better premise. Bazel already knows the
dependency graph, and it already knows the exact command it used to compile every
file, because it built that command itself. A wrapper under Bazel spends all its
ingenuity recovering information that was never lost.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;how-it-works-1&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#how-it-works-1&quot; aria-label=&quot;Anchor link for: how-it-works-1&quot;&gt;&lt;i class=&quot;icon&quot;&gt;&lt;&#x2F;i&gt;&lt;&#x2F;a&gt;
How it works&lt;&#x2F;h3&gt;
&lt;p&gt;These rules ask instead. They use a Bazel &lt;em&gt;aspect&lt;&#x2F;em&gt; — a piece of code that walks over
the targets you already have and attaches extra work to them, without you editing a
single build file. For each source file it finds, the aspect asks the toolchain for
the compile command Bazel was going to run anyway, changes where the output goes, and
declares the result as a proper build output. There is no compiler wrapper, nothing
hidden inside object files, and no absolute path anywhere in the results.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#31333d;color:#ffffffc4;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;font-weight:bold;color:#a3cbe3;&quot;&gt;bazel&lt;&#x2F;span&gt;&lt;span&gt; build &#x2F;&#x2F;:app_bc
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;what-living-in-the-graph-buys&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#what-living-in-the-graph-buys&quot; aria-label=&quot;Anchor link for: what-living-in-the-graph-buys&quot;&gt;&lt;i class=&quot;icon&quot;&gt;&lt;&#x2F;i&gt;&lt;&#x2F;a&gt;
What living in the graph buys&lt;&#x2F;h3&gt;
&lt;p&gt;The first thing this buys is that extraction costs nothing until somebody asks for it.
The bitcode files are not part of what a target normally produces; they sit in a
separate group you request by name, and Bazel runs work only when something needs its
output. An ordinary build runs none of it. That is what makes it reasonable to leave
these rules switched on in a repository other people share, because the people who
never wanted bitcode never pay for it.&lt;&#x2F;p&gt;
&lt;p&gt;The second is that every point in the dependency graph becomes an extraction point.
The bitcode travels up the graph node by node, which makes a library just as
addressable as a finished program. &quot;Give me the bitcode for this one library, not
linked into anything&quot; is a request the wrapper approach cannot express at all, because
reading those sections starts at the final binary.&lt;&#x2F;p&gt;
&lt;p&gt;Handling shared dependencies is where this earns its keep: each point in the graph
collects only its own source files, never its dependencies&#x27;. Folding a dependency&#x27;s
bitcode into everything that uses it would include a shared library twice, and merging
then fails on the duplicate definitions. Instead the bitcode travels in a set that
treats the same file as the same file, so when two libraries both depend on a third,
the shared one contributes exactly one copy.&lt;&#x2F;p&gt;
&lt;figure class=&quot;bc-fig bc-graph&quot; aria-label=&quot;A diamond dependency graph in which two libraries both depend on a third, which contributes one copy of its bitcode&quot;&gt;
&lt;svg viewBox=&quot;0 0 420 300&quot; role=&quot;img&quot;&gt;
&lt;g class=&quot;edges&quot;&gt;
&lt;line x1=&quot;172&quot; y1=&quot;66&quot; x2=&quot;112&quot; y2=&quot;128&quot;&#x2F;&gt;
&lt;line x1=&quot;248&quot; y1=&quot;66&quot; x2=&quot;308&quot; y2=&quot;128&quot;&#x2F;&gt;
&lt;line x1=&quot;112&quot; y1=&quot;172&quot; x2=&quot;172&quot; y2=&quot;234&quot;&#x2F;&gt;
&lt;line x1=&quot;308&quot; y1=&quot;172&quot; x2=&quot;248&quot; y2=&quot;234&quot;&#x2F;&gt;
&lt;&#x2F;g&gt;
&lt;g class=&quot;node&quot; style=&quot;--i:0&quot;&gt;
&lt;rect x=&quot;155&quot; y=&quot;22&quot; width=&quot;110&quot; height=&quot;44&quot; rx=&quot;8&quot;&#x2F;&gt;
&lt;text class=&quot;target&quot; x=&quot;210&quot; y=&quot;41&quot;&gt;&#x2F;&#x2F;:diamond&lt;&#x2F;text&gt;
&lt;text class=&quot;bc&quot; x=&quot;210&quot; y=&quot;57&quot;&gt;diamond.bc&lt;&#x2F;text&gt;
&lt;&#x2F;g&gt;
&lt;g class=&quot;node&quot; style=&quot;--i:1&quot;&gt;
&lt;rect x=&quot;37&quot; y=&quot;128&quot; width=&quot;110&quot; height=&quot;44&quot; rx=&quot;8&quot;&#x2F;&gt;
&lt;text class=&quot;target&quot; x=&quot;92&quot; y=&quot;147&quot;&gt;&#x2F;&#x2F;:lib_a&lt;&#x2F;text&gt;
&lt;text class=&quot;bc&quot; x=&quot;92&quot; y=&quot;163&quot;&gt;lib_a.bc&lt;&#x2F;text&gt;
&lt;&#x2F;g&gt;
&lt;g class=&quot;node&quot; style=&quot;--i:1&quot;&gt;
&lt;rect x=&quot;273&quot; y=&quot;128&quot; width=&quot;110&quot; height=&quot;44&quot; rx=&quot;8&quot;&#x2F;&gt;
&lt;text class=&quot;target&quot; x=&quot;328&quot; y=&quot;147&quot;&gt;&#x2F;&#x2F;:lib_b&lt;&#x2F;text&gt;
&lt;text class=&quot;bc&quot; x=&quot;328&quot; y=&quot;163&quot;&gt;lib_b.bc&lt;&#x2F;text&gt;
&lt;&#x2F;g&gt;
&lt;g class=&quot;node shared&quot; style=&quot;--i:2&quot;&gt;
&lt;rect x=&quot;155&quot; y=&quot;234&quot; width=&quot;110&quot; height=&quot;44&quot; rx=&quot;8&quot;&#x2F;&gt;
&lt;text class=&quot;target&quot; x=&quot;210&quot; y=&quot;253&quot;&gt;&#x2F;&#x2F;:lib_c&lt;&#x2F;text&gt;
&lt;text class=&quot;bc&quot; x=&quot;210&quot; y=&quot;269&quot;&gt;lib_c.bc&lt;&#x2F;text&gt;
&lt;&#x2F;g&gt;
&lt;&#x2F;svg&gt;
&lt;figcaption&gt;Both paths down from &lt;b&gt;&#x2F;&#x2F;:diamond&lt;&#x2F;b&gt; arrive at &lt;b&gt;&#x2F;&#x2F;:lib_c&lt;&#x2F;b&gt;. Because the bitcode travels in a set keyed on file identity, &lt;b&gt;lib_c.bc&lt;&#x2F;b&gt; is built once and merged once. The edges are dashed because they are declared during analysis and run nothing until an output group asks for them.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;The third is that the bitcode always matches the real compile. The aspect never
interprets a command line; it asks the toolchain for the command Bazel had already
built and changes only where the output goes. A wrapper has to understand flags it did
not write, and anything it misreads becomes bitcode that quietly differs from the
object that actually shipped. Here there is nothing to misread. For the same reason,
the work behaves like every other step in the build: scheduled, parallelized, cached,
and distributed across machines, with no special case for a shared cache because there
are no absolute paths to go stale.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;what-it-costs-1&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#what-it-costs-1&quot; aria-label=&quot;Anchor link for: what-it-costs-1&quot;&gt;&lt;i class=&quot;icon&quot;&gt;&lt;&#x2F;i&gt;&lt;&#x2F;a&gt;
What it costs&lt;&#x2F;h3&gt;
&lt;p&gt;The cost of all this is that it works with Bazel and nothing else, and with a recent
Bazel at that, since older-style workspace configuration is no longer supported. What
makes the approach clean is exactly what makes it narrow.&lt;&#x2F;p&gt;
&lt;p&gt;The smaller costs are worth naming too. A rule cannot mark itself as &quot;do not build me
by default,&quot; so bitcode targets get swept up by a build-everything command unless
whoever writes them opts out. Running a one-off extraction without editing build files
works, but the incantation is clumsy and easy to get subtly wrong. Because the aspect
reuses the toolchain&#x27;s own machinery, it is coupled to the internals of the LLVM and
Rust rule sets, and an upstream release can require changes here; I took that deal
deliberately, since deriving those commands independently would drift out of sync
silently, which is worse. It is also early software: not yet in Bazel&#x27;s central
package registry, so depending on it takes an extra line. Objective-C works only on
macOS and needs an extra flag, and Swift does not work yet because the bitcode it
emits is newer than what the toolchain&#x27;s LLVM tools will read.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;which-one-to-use&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#which-one-to-use&quot; aria-label=&quot;Anchor link for: which-one-to-use&quot;&gt;&lt;i class=&quot;icon&quot;&gt;&lt;&#x2F;i&gt;&lt;&#x2F;a&gt;
Which one to use&lt;&#x2F;h2&gt;
&lt;p&gt;If you build with Bazel, use rules_rllvm. If you build with anything else, use rllvm.
That is the honest short answer, and the second half covers most existing C and C++
software.&lt;&#x2F;p&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;&lt;&#x2F;th&gt;&lt;th&gt;rllvm&lt;&#x2F;th&gt;&lt;th&gt;rules_rllvm&lt;&#x2F;th&gt;&lt;&#x2F;tr&gt;&lt;&#x2F;thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;Build system&lt;&#x2F;td&gt;&lt;td&gt;Any&lt;&#x2F;td&gt;&lt;td&gt;Bazel only&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;How you adopt it&lt;&#x2F;td&gt;&lt;td&gt;Set &lt;code&gt;CC&lt;&#x2F;code&gt; and build&lt;&#x2F;td&gt;&lt;td&gt;Point an aspect at existing targets&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;Extract from&lt;&#x2F;td&gt;&lt;td&gt;The finished binary&lt;&#x2F;td&gt;&lt;td&gt;Any library or binary in the graph&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;Cost when unused&lt;&#x2F;td&gt;&lt;td&gt;Every file compiles twice&lt;&#x2F;td&gt;&lt;td&gt;Nothing runs&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;Result&lt;&#x2F;td&gt;&lt;td&gt;Points at bitcode by path&lt;&#x2F;td&gt;&lt;td&gt;Ordinary build outputs&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;Portability&lt;&#x2F;td&gt;&lt;td&gt;The entire point&lt;&#x2F;td&gt;&lt;td&gt;Not a goal&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;&#x2F;tbody&gt;&lt;&#x2F;table&gt;
&lt;p&gt;The more useful question is what each tool assumes about your situation. Reach for
rllvm when you do not control the build — third-party source, a vendored tree,
reproducing somebody else&#x27;s build — or when you need one method that behaves the same
across many projects that share no build system. Reach for rules_rllvm when you are
already using Bazel, and especially when you want bitcode for individual libraries,
when your builds are cached or spread across machines, or when you cannot afford to
double the cost of a build people run all day.&lt;&#x2F;p&gt;
&lt;p&gt;Neither is a strict upgrade on the other. rllvm buys portability with a second compile
and a result that points at files instead of containing them. rules_rllvm buys
precision and costs nothing when idle, by giving up every build system but one.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;what-building-both-taught-me&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#what-building-both-taught-me&quot; aria-label=&quot;Anchor link for: what-building-both-taught-me&quot;&gt;&lt;i class=&quot;icon&quot;&gt;&lt;&#x2F;i&gt;&lt;&#x2F;a&gt;
What building both taught me&lt;&#x2F;h2&gt;
&lt;p&gt;Where you get into the build decides everything else. Both tools answer the same
question, and nearly every difference between them follows from one choice about where
to intervene. rllvm intervenes at the compiler, which every build system can be told
to swap out, so it works everywhere and understands nothing about the project it sits
inside. rules_rllvm intervenes at the build system, which understands everything and
exists in exactly one place. Portability, cost, robustness, and what you can even
extract were not four decisions. They were one, and I did not see that clearly until I
had built the second tool.&lt;&#x2F;p&gt;
&lt;p&gt;Recovering what somebody already knows is a losing game when you have the option of
asking. rllvm&#x27;s central trick reconstructs, after the fact, something the build never
bothered to record. That is satisfying to make work, and it is also labor that exists
only because nobody wrote the answer down. Under Bazel the answer is written down, and
reconstructing it anyway means maintaining a careful parser for command lines that the
build system generated itself. This does not make reconstruction bad. With make or
autotools there is nobody to ask, and reconstruction is the only approach that works
at all. It makes noticing which situation you are in the actual skill, because the
answer is a property of the environment rather than of the problem.&lt;&#x2F;p&gt;
&lt;p&gt;Trying to get both sets of benefits got me neither, which is what the pivot taught me.
Wrapping rllvm inside Bazel rules looked like reuse: one mechanism, one codebase, a
thin adapter. What it produced was the fragility of reconstruction plus a constant
fight with the guarantees that make Bazel worth using. The two designs are not layers.
They are alternatives, and the honest move was to stop making one wear the other as a
costume.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;what-comes-next&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#what-comes-next&quot; aria-label=&quot;Anchor link for: what-comes-next&quot;&gt;&lt;i class=&quot;icon&quot;&gt;&lt;&#x2F;i&gt;&lt;&#x2F;a&gt;
What comes next&lt;&#x2F;h2&gt;
&lt;p&gt;Extraction is a means, not an end. A &lt;code&gt;.bc&lt;&#x2F;code&gt; file matters only because something else
reads it, and plenty of things already do: symbolic execution engines like KLEE, and
static analysis frameworks like SVF and Phasar, all take whole-program bitcode as
their input. Getting that file out of a real build has always been the awkward step,
which is why tools like these exist at all.&lt;&#x2F;p&gt;
&lt;p&gt;The direction I find most interesting now is handing those facts to a coding
assistant. An assistant reading your source approximates the call graph by matching
names and patterns. It cannot know what actually reaches what once everything is
linked, and indirect calls make the guess worse. Bitcode knows. A small query
interface over a whole-program module — who calls this function, whether anything can
reach that one, what an indirect call might actually target — turns a guess into a
lookup. The answers have to come back as file and line numbers rather than as compiler
IR, because that is where the work happens.&lt;&#x2F;p&gt;
&lt;p&gt;For security review the useful output is evidence, not a verdict. Asking whether
attacker-controlled input can reach a risky function is a reachability question, and
the honest answer is the path itself, so a person can read those particular functions
and decide. Static analysis over-approximates. A tool that reports &quot;vulnerable&quot;
without showing its work has only moved the guessing somewhere else.&lt;&#x2F;p&gt;
&lt;p&gt;None of this is built yet, and two things would decide whether it works. The bitcode
has to be compiled with debug information and little optimization, or there is nothing
to map back to source. Indirect calls remain the hard problem they have always been.
But extraction that works per library and costs nothing when idle is a good substrate
for a loop that asks a question, changes a file, and asks again.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;Both projects are Apache-2.0 and take issues and patches:
&lt;a rel=&quot;noopener&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;h1994st&#x2F;rllvm&quot;&gt;rllvm&lt;&#x2F;a&gt; ·
&lt;a rel=&quot;noopener&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;h1994st&#x2F;rules_rllvm&quot;&gt;rules_rllvm&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
</content>
        
    </entry>
</feed>
