Introduction
Lua is a small scripting language that is often
built into games and other programs. Lua source (.lua) is compiled to
bytecode for Lua’s virtual machine each time it is loaded. The luac tool
saves that bytecode to a file instead, a precompiled chunk, which loads faster and does
not show the source. Its default output name is luac.out; people usually
rename it .luac, and some programs load precompiled chunks under a plain
.lua name.
The bytecode changes between Lua versions, so a chunk only loads in the version that
wrote it, built with the same number sizes. LuaJIT, a separate fast implementation of
Lua 5.1, has its own bytecode format, which starts with 1B 4C 4A
(\x1BLJ).
Structure
A precompiled chunk starts with the byte 1B (escape) and
Lua, then one byte for the version: 0x51 for Lua 5.1,
0x52 for 5.2, 0x53 for 5.3 and 0x54 for 5.4.
Next is a format byte, 0 for the official format. The rest of the header records the
sizes the compiler used for integers, instructions and numbers, and, from Lua 5.2 on,
a few fixed bytes and sample numbers that let the loader detect a damaged file or a
different byte order. After the header comes the main function: its instructions,
constants, upvalues, nested functions and optional debug information.
The Lua developers do not document this layout and change it between versions, so
tools that read it follow the source code of each version. The reference pages here
do the same, one per version: Lua 5.1,
Lua 5.2,
Lua 5.3 and
Lua 5.4, each with the header, the function
layout, the constant tags, the instruction layout, every opcode with its operand
modes (luaP_opmodes), the NEWTABLE size encoding and how
stripped chunks (luac -s) differ.
LuaJIT bytecode is a separate format and is not covered here. It
starts with 1B 4C 4A (\x1BLJ), not the
1B 4C 75 61 (\x1BLua) signature of the official Lua versions,
and is written and read by LuaJIT, not by the official luac; its layout is
not described on these pages.
Not described
Known, not yet covered
- Viewer: the viewer reads chunks of Lua 5.1, 5.2, 5.3 and 5.4: the header fields with their meanings, every function as a tree (source, line range, parameters, vararg flag, stack size, constants with their types, upvalues, nested functions and the debug information), every instruction decoded by that version’s opcode table with its operands, constants, upvalue and local names resolved, the disassembly with each function a region linked to its bytes, stripped chunks labelled as above, and every loader check listed on the reference pages as a problem (a header that differs, a truncation, a bad count or constant tag, the nesting limit, and the 5.1 code verifier). What is not done: its listings have not been compared with
luac -l -l(a Lua build was approved, but the build was blocked in the tool that checks commands, so it was not made); it has been tried on five real chunks (Lua 5.1 and 5.4, in samples/luac/real) and on chunks written from these pages, but there is no real Lua 5.2 or 5.3 chunk; the raw sources of Lua 5.1 and 5.2 could not be fetched from GitHub (the tags answer 404), so theirlundump.candlopcodes.cwere read in the lua.org source viewer; a header that differs from the one a particular Lua build would write is shown but not refused, since the viewer does not know the reader’s build; and functions nested more than 500 deep are not followed (Lua 5.1 allows 200; later versions have no limit). - Viewer: LuaJIT bytecode (
1B 4C 4A) is a separate format with no reference pages here: a.luacor.outfile that starts so is labelled as LuaJIT bytecode and shown as bytes. A.lubfile is routed by its signature and a parse. A.luafile or a file with no extension that is really a chunk is not routed to the viewer by its name (there is no registry row for.lua); Identify finds it.
Links
Software
Reference
- Lua source code – browsable source of each version;
ldump.c,lundump.candlopcodes.hdefine the format