2026.11.24
LLVM IR without arithmetic
The M/o/Vfuscator compiles C into mov instructions, and only mov instructions. Arithmetic, comparisons, jumps, function calls — everything happens through table lookups in memory, there is no self-modifying code, no other form of cheating, and the only concession to reality is a single jmp to dispatch external library calls. Chris Domas built it on the strength of Stephen Dolan’s proof that mov alone is Turing-complete — and when asked why he made it, his answer in the FAQ is simply «I thought it would be funny.»
I like that kind of funny. It got me wondering what the idea looks like one abstraction level up: not x86, but LLVM IR. The goal I settled on: a pass that turns most arithmetic — add, sub, mul, rem, even the comparisons — into and, or, xor and shifts. So I wrote llvm-ir-no-math, and in the process finally learned the IRBuilder API properly, which was half the point of the exercise.
Why IR and not x86?
LLVM IR has no mov, and its loads are pure — you cannot smuggle side effects or control flow through them the way the movfuscator’s memory writes do, so the one-opcode stunt itself does not port. What does port is the underlying idea: compute with a deliberately tiny instruction vocabulary. On x86 that vocabulary is mov; in IR the natural choice is the bitwise ops — and, or, xor and the shifts — plus whatever memory traffic they need.
So the pass, obfuscate-math, rewrites a function’s integer arithmetic and comparisons into exactly that:
| opcode | becomes |
|---|---|
add | bit-serial ripple-carry: width rounds of xor (sum) and and+shl (carry) |
sub | x + ~y + 1, wrap flags carried over |
and / or | nibble-indexed 4×4 lookup tables |
xor | a full 256×256 byte table |
mul | shift-and-add over the set bits of the multiplier |
shl / lshr | repeated single-bit shifts off a weight table |
ashr | lshr with a sign refill, built from lshr/or/and/sub/add only |
icmp | a per-bit verdict walk over a 2-bit code table |
urem / srem | x - (x / y) * y, poison semantics included |
udiv and sdiv are the one arithmetic opcode left standing — they have no strategy, mostly to keep the rem lowering expressible.
Half of the strategies are just hardware idioms: the ripple-carry add is a hardware adder transcribed into IR, sub is the two’s-complement identity above, mul is shift-and-add — the way one multiplies when the CPU refuses to. The other half leans on precomputed lookup tables, and that is where the movfuscator’s fingerprint shows. A table is also the one thing a reverse engineer can pattern-match on, so they land in the module as globals with randomized names, their contents scrambled with keys drawn from getentropy(3) — two runs over the same input produce different IR. OBF_SEED pins keys and names when one wants reproducible output.
What survives in a function afterwards: the bitwise ops themselves, the getelementptr/load traffic feeding the tables, the br/phi/icmp scaffolding around the shift walks — and div.
What IRBuilder actually teaches you
A few things that no amount of reading LangRef had made stick:
The builder is a cursor. Construct it at an instruction, SetInsertPoint elsewhere, and every Create* call inserts exactly there and hands you back the Value* it made. The pass’s split-and-build idiom for the ashr loop:
auto Cont = Orig->splitBasicBlock(&Instr, "ashr.cont");
auto Check = BasicBlock::Create(context, "ashr.check", Func, Cont);
Orig->getTerminator()->eraseFromParent(); // split left a br behind
B.SetInsertPoint(Check);
auto j = B.CreatePHI(ITy, 2, "ashr.j");
j->addIncoming(ConstantInt::get(ITy, 0), Orig);
// ... build the loop body ...
j->addIncoming(jN, Step); // wired once Step exists
PHIs are built backwards. The counterintuitive part for a C programmer: one creates the phi at the loop head before the back-edge block exists, and wires the second incoming edge after the fact. addIncoming is a two-argument confession that IR is a graph, not a listing.
Tables are just globals. The movfuscator mechanics in IR amount to a constant GlobalVariable plus a two-index GEP and a load:
auto *Table = new GlobalVariable(*module, TabTy, /*isConstant=*/true,
GlobalValue::PrivateLinkage, Init, Name);
// ...
auto Ptr = B.CreateInBoundsGEP(RowTy, LUT, {KA, KB}, "lut.p");
auto Byte = B.CreateLoad(I8Ty, Ptr, "lut.v");The two-line heart of any transformation pass:
Instr.replaceAllUsesWith(res);
Instr.eraseFromParent();Swap the uses, delete the corpse. Everything else is bookkeeping around those two lines.
It eats its own output
The replacement code is built from ops that have strategies of their own — the and/xor/shl an add becomes are, in turn, table material — so the pass would happily re-obfuscate its own output, forever. Every emitted instruction therefore carries an origin tag,
%sum0 = xor <4 x i32> %x, %y, !obf.origin !0and gets re-obfuscated only while its generation is below its origin opcode’s depth (OBF_DEPTH='sub=3,mul,default=1' retunes that at run time). This composes, which is the polite way of saying that a three-instruction function ends up at half a million instructions under the default depths. Vocabulary reduction is a game best played with a budget.
Caveats
- This is exploration, not armor. A single
and i32becomes a ~90-line LUT walk, and it survivesopt -O2— but anyone who recognizes table-driven constructions reads it right back out. The movfuscator has the same property, and it never claimed otherwise. - Semantics are checked by the verifier and seeded golden diffs; nothing executes original against transformed code yet. Alive2 is the obvious next step.
- Vectors work,
<2 x i128>included; the shift walks cap ati64.
Closing remarks
cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Release
cmake --build build
opt -load-pass-plugin build/src/obfuscate-math.so -passes=obfuscate-math -S in.ll -o out.ll
ctest --test-dir build --output-on-failure
If the mood strikes, x86.mov has the movfuscator running in the browser, an out-of-tree LLVM backend that targets mov-only x86 from clang, and a compiler-explorer-style UI over both. The idea, it turns out, is funny at every level of the stack.