This page follows Lua 5.2’s ldump.c, lundump.c,
lundump.h and lopcodes.h (5.2.4). The Lua authors do not
document the format. Numbers are written in the byte order and sizes of the machine
that wrote the chunk; the header records them and the loader refuses a mismatch.
Header (18 bytes)
| Offset | Size | Field | Meaning |
|---|---|---|---|
0 | 4 | signature | 1B 4C 75 61: escape and Lua. |
4 | 1 | version | 0x52. |
5 | 1 | format | 0, the official format. |
6 | 1 | endianness | 1 little-endian, 0 big-endian. |
7 | 1 | sizeof(int) | Bytes in an int. |
8 | 1 | sizeof(size_t) | Bytes in a size_t. |
9 | 1 | sizeof(Instruction) | Bytes in an instruction. |
10 | 1 | sizeof(lua_Number) | Bytes in a number. |
11 | 1 | integral flag | 0 floating point, 1 integer. |
12 | 6 | tail | 19 93 0D 0A 1A 0A (LUAC_TAIL, “data to catch conversion errors”). |
Basic types
| Type | Encoding |
|---|---|
| byte | One byte. |
| int | sizeof(int) bytes, in the header’s byte order. |
| number | sizeof(lua_Number) bytes. |
| string | A size_t length counting the terminating zero, then the bytes and the zero. 0 means no string. |
| list of T | An int count, then that many T. |
Function prototype
The main function follows the header. The loader makes it a closure; if the function does not have exactly one upvalue, the loader creates a closure with the right number.
| Field | Type | Meaning |
|---|---|---|
| linedefined | int | First line. |
| lastlinedefined | int | Last line. |
| numparams | byte | Number of fixed parameters. |
| is_vararg | byte | Whether the function takes .... |
| maxstacksize | byte | Registers needed. |
| code | list of Instruction | 4 bytes each. |
| constants | list of constant | Tag byte and value, below. |
| protos | list of function | Nested prototypes in this layout. |
| upvalues | list of (byte, byte) | instack and idx, one byte each. |
| source | string | Source name. Debug information starts here. |
| lineinfo | list of int | Line of each instruction. |
| locvars | list of (string, int, int) | Local name, start pc, end pc. |
| upvalue names | list of string | Names assigned to the upvalues in order. |
Constants
| Tag byte | Type | Then |
|---|---|---|
0 | nil | nothing |
1 | boolean | one byte |
3 | number | a number |
4 | string | a string |
Instructions
| Mode | Bits 31–23 | Bits 22–14 | Bits 13–6 | Bits 5–0 |
|---|---|---|---|---|
iABC | B (9) | C (9) | A (8) | opcode (6) |
iABx | Bx (18) | A (8) | opcode (6) | |
iAsBx | sBx (18), stored + 131,071 | A (8) | opcode (6) | |
iAx | Ax (26) | opcode (6) | ||
The opcode numbers, field sizes (SIZE_OP 6, SIZE_A 8,
SIZE_B and SIZE_C 9, SIZE_Bx 18,
SIZE_Ax 26), the actions, RK(x) (constant x − 256
when bit 8 is set, otherwise register x) and the SETLIST batch
(LFIELDS_PER_FLUSH 50) are from 5.2’s own
lopcodes.h
on lua.org, read 2026-11-12.
| Op | Name | Mode | Action |
|---|---|---|---|
| 0 | MOVE | A B | R(A) := R(B) |
| 1 | LOADK | A Bx | R(A) := Kst(Bx) |
| 2 | LOADKX | A | R(A) := Kst(Ax of the next instruction, an EXTRAARG) |
| 3 | LOADBOOL | A B C | R(A) := (Bool)B; if C, skip next |
| 4 | LOADNIL | A B | R(A), …, R(A+B) := nil |
| 5 | GETUPVAL | A B | R(A) := UpValue[B] |
| 6 | GETTABUP | A B C | R(A) := UpValue[B][RK(C)] (globals, through _ENV) |
| 7 | GETTABLE | A B C | R(A) := R(B)[RK(C)] |
| 8 | SETTABUP | A B C | UpValue[A][RK(B)] := RK(C) |
| 9 | SETUPVAL | A B | UpValue[B] := R(A) |
| 10 | SETTABLE | A B C | R(A)[RK(B)] := RK(C) |
| 11 | NEWTABLE | A B C | R(A) := {}; B is the initial array size and C the initial hash size, each a “floating point byte” (below); when both are 0 the table is left empty |
| 12 | SELF | A B C | R(A+1) := R(B); R(A) := R(B)[RK(C)] |
| 13 | ADD | A B C | R(A) := RK(B) + RK(C) |
| 14 | SUB | A B C | R(A) := RK(B) − RK(C) |
| 15 | MUL | A B C | R(A) := RK(B) × RK(C) |
| 16 | DIV | A B C | R(A) := RK(B) / RK(C) |
| 17 | MOD | A B C | R(A) := RK(B) % RK(C) |
| 18 | POW | A B C | R(A) := RK(B) ^ RK(C) |
| 19 | UNM | A B | R(A) := −R(B) |
| 20 | NOT | A B | R(A) := not R(B) |
| 21 | LEN | A B | R(A) := length of R(B) |
| 22 | CONCAT | A B C | R(A) := R(B) .. … .. R(C) |
| 23 | JMP | A sBx | pc += sBx; if A, close upvalues at or above R(A−1) |
| 24 | EQ | A B C | if (RK(B) == RK(C)) ~= A then skip next |
| 25 | LT | A B C | if (RK(B) < RK(C)) ~= A then skip next |
| 26 | LE | A B C | if (RK(B) <= RK(C)) ~= A then skip next |
| 27 | TEST | A C | if not (R(A) <=> C) then skip next |
| 28 | TESTSET | A B C | if R(B) <=> C then R(A) := R(B) else skip next |
| 29 | CALL | A B C | R(A), …, R(A+C−2) := R(A)(R(A+1), …, R(A+B−1)); B = 0 means B = top (arguments up to the top); C = 0 means top is set to last_result + 1, so the next open instruction (CALL, RETURN, SETLIST) may use it |
| 30 | TAILCALL | A B C | return R(A)(R(A+1), …, R(A+B−1)) |
| 31 | RETURN | A B | return R(A), …, R(A+B−2); B = 0 means return up to the top |
| 32 | FORLOOP | A sBx | R(A) += R(A+2); if within R(A+1), pc += sBx and R(A+3) := R(A) |
| 33 | FORPREP | A sBx | R(A) −= R(A+2); pc += sBx |
| 34 | TFORCALL | A C | R(A+3), …, R(A+2+C) := R(A)(R(A+1), R(A+2)) |
| 35 | TFORLOOP | A sBx | if R(A+1) ~= nil then R(A) := R(A+1); pc += sBx |
| 36 | SETLIST | A B C | R(A)[(C−1) × 50 + i] := R(A+i), 1 ≤ i ≤ B; B = 0 means the real B is the top; C = 0 means the next instruction is an EXTRAARG holding the real C |
| 37 | CLOSURE | A Bx | R(A) := closure(KPROTO[Bx]); upvalues come from the prototype’s descriptors |
| 38 | VARARG | A B | R(A), …, R(A+B−2) := vararg; B = 0 means use the actual number of varargs and set top (like CALL with C = 0) |
| 39 | EXTRAARG | Ax | A larger argument for the previous instruction. After LOADKX the next instruction is always an EXTRAARG |
Floating point byte (NEWTABLE B and C)
The parser stores the constructor’s array count and hash count with
luaO_int2fb and the VM reads them back with luaO_fb2int
(lobject.c). A “floating point byte” is eeeeexxx: when
eeeee is 0 the value is xxx; otherwise it is
(1xxx)2 × 2eeeee−1, that is
((x & 7) + 8) << (e − 1). luaO_int2fb returns the
number itself below 8; otherwise, while the value is at least 16 (0x10) it
computes x = (x + 1) >> 1 and adds 1 to the exponent, and returns
((e + 1) << 3) | (x − 8). The encoded size is therefore never smaller
than the real one. Example: 100 → 50 → 25 → 13 (e = 3), so the byte is
(4 << 3) | 5 = 0x25, which decodes to 13 << 3 = 104.
Compared with 5.1, globals are reached through the _ENV upvalue
(GETTABUP, SETTABUP replace GETGLOBAL and
SETGLOBAL), CLOSE is folded into JMP, and the
generic for is split into TFORCALL and
TFORLOOP.
Operand modes (luaP_opmodes)
lopcodes.c holds one byte per opcode, built by
opmode(t, a, b, c, m) as
(t << 7) | (a << 6) | (b << 4) | (c << 2) | m: bits 0–1
the format, bits 2–3 the C argument mode, bits 4–5 the B argument mode, bit 6 “sets
register A” and bit 7 “is a test”. The argument modes (enum OpArgMask) are
N, argument not used; U, used (a number, count or flag);
R, a register or a jump offset; K, a constant or
register/constant (RK).
| Ops | Names | Format | B | C | Sets A | Test |
|---|---|---|---|---|---|---|
| 0 | MOVE | iABC | R | N | yes | |
| 1 | LOADK | iABx | K | N | yes | |
| 2 | LOADKX | iABx | N | N | yes | |
| 3 | LOADBOOL | iABC | U | U | yes | |
| 4–5 | LOADNIL, GETUPVAL | iABC | U | N | yes | |
| 6 | GETTABUP | iABC | U | K | yes | |
| 7 | GETTABLE | iABC | R | K | yes | |
| 8 | SETTABUP | iABC | K | K | ||
| 9 | SETUPVAL | iABC | U | N | ||
| 10 | SETTABLE | iABC | K | K | ||
| 11 | NEWTABLE | iABC | U | U | yes | |
| 12 | SELF | iABC | R | K | yes | |
| 13–18 | ADD, SUB, MUL, DIV, MOD, POW | iABC | K | K | yes | |
| 19–21 | UNM, NOT, LEN | iABC | R | N | yes | |
| 22 | CONCAT | iABC | R | R | yes | |
| 23 | JMP | iAsBx | R | N | ||
| 24–26 | EQ, LT, LE | iABC | K | K | yes | |
| 27 | TEST | iABC | N | U | yes | |
| 28 | TESTSET | iABC | R | U | yes | yes |
| 29–30 | CALL, TAILCALL | iABC | U | U | yes | |
| 31 | RETURN | iABC | U | N | ||
| 32–33 | FORLOOP, FORPREP | iAsBx | R | N | yes | |
| 34 | TFORCALL | iABC | N | U | ||
| 35 | TFORLOOP | iAsBx | R | N | yes | |
| 36 | SETLIST | iABC | U | U | ||
| 37 | CLOSURE | iABx | U | N | yes | |
| 38 | VARARG | iABC | U | N | yes | |
| 39 | EXTRAARG | iAx | U | U |
Source: luaP_opmodes in lopcodes.c, Lua 5.2 on lua.org.
What the loader rejects
Every rule below makes luaU_undump fail with a syntax error; message wording is
not part of the format. Source: Lua 5.2.4 lundump.c and llimits.h on
lua.org, read as raw HTML text.
- Header: the whole 18-byte header (12 bytes of fields plus
LUAC_TAIL) is compared at once with the header this build writes. A difference is refused, and the loader reports which part differs: the signature, the version and format bytes, the byte-order, size and integral-flag bytes (int,size_t,Instruction,lua_Numbersizes), or the tail. A chunk is readable only by a build with the same sizes and byte order. - Truncation: any read that does not get all its bytes is refused.
- Integers: every
intread throughLoadInt(the counts,linedefined,lastlinedefined,startpc,endpc) must be 0 or more. The entries oflineinfoare read as a block and not checked. - Constants: an unknown tag byte is not refused in a normal build: the loader’s only test is
lua_assert( 0 ), which does nothing unless the library is built with assertions, so the constant stays nil and no payload is read. A string constant of size 0 is accepted (a null string pointer). There is no rule on string constants. - Upvalue names: the number of names in the debug section is read and used as the loop count without comparing it with the upvalue count, so a larger count than the number of upvalue descriptors is not rejected by the loader (it would write past the array).
- Nesting: none.
LoadFunctionhas no depth counter (LUAI_MAXCCALLS, 200 inllimits.h, is not used by the loader). - Code: not verified.
luai_verifycodeis an empty hook unless a build defines it, so no opcode or operand range is checked at load time. - Chunk name: never a cause of refusal; it is used only in messages: a leading
@or=is dropped, a name that starts with the signature byte shows as “binary string”, any other is shown as it is.
Stripped chunks
luac -s and string.dump(f, true) pass strip to
luaU_dump. In 5.2’s ldump.c, DumpDebug writes the
source as no string when strip is set, and writes lineinfo,
locvars and the upvalue-name list each with a count of 0. Unlike 5.3 and
5.4, 5.2 has no “same source as the parent” rule: each function’s debug section carries
its own source string when not stripped. Header, code, constants, upvalue descriptors and
nested prototypes are unchanged.
On loading, LoadDebug sets source to whatever string it reads;
a size of 0 gives no string (NULL) and the loader substitutes nothing, so a
stripped 5.2 chunk’s functions, main function included, have no source (lundump.c).
Example
1B 4C 75 61 52 00 01 04 08 04 08 00 header fields (64-bit little-endian)
19 93 0D 0A 1A 0A LUAC_TAIL
00 00 00 00 linedefined 0 (main chunk)
00 00 00 00 lastlinedefined 0
00 01 02 numparams 0, is_vararg 1, maxstacksize 2
The values after the header are illustrative.
Sources
Lua 5.2 source on lua.org:
lundump.c (header check and the order of every field),
lundump.h (LUAC_TAIL), lua.h (type tags) and
lopcodes.h (opcode numbers, Ax size and position). Opcode actions: see
the note under Instructions. The notes on B or C = 0, luaP_opmodes
(lopcodes.c), luaO_int2fb and luaO_fb2int
(lobject.c), lparser.c and lvm.c
(OP_NEWTABLE) and ldump.c (strip) were read from
the same lua.org source viewer.