Lua’s authors do not document the precompiled format; it is defined by the source of
each release. Lua 2.5 and 3.0 write the same version byte and the same layout; 3.0 adds
two opcodes. This page follows undump.c, undump.h,
opcode.h, opcode.c, inout.c and the
luac sources of Lua 2.5 and 3.0, from lua.org (see
sources). The code is a byte code for a stack machine. Numbers
are stored in the byte order of the machine that wrote the chunk, and the header shows
which that was. Names of globals and string constants are kept outside the code, in
records that the loader links back into it.
Header
| Offset | Size | Field | Meaning |
|---|---|---|---|
0 | 4 | signature | 1B 4C 75 61: the byte ID_CHUNK (escape) and Lua. |
4 | 1 | version | 0x25, in both releases. |
5 | 1 | sizeof(Word) | 2. |
6 | 1 | sizeof(float) | 4. |
7 | 1 | sizeof(TFunc*) | The size of a pointer in the writing build, usually 4 or 8. It is the width of the slot after PUSHFUNCTION. |
8 | 2 | test word | 0x1234 in the writer’s byte order: 34 12 little-endian, 12 34 big-endian. |
10 | 4 | test float | The single-precision value 0.123456789e-23 in the writer’s byte order: 46 0A BF 17 little-endian, 17 BF 0A 46 big-endian. |
Function records follow the header directly. A file may hold several chunks, one after the other, each with its own header.
Function record
| Field | Size | Meaning |
|---|---|---|
| tag | 1 | 46 (F, ID_FUN). |
| size | 2 + 2 | Bytes of code, as two words: the high half first, then the low half. |
| lineDefined | 2 | 0 marks the main function of the chunk. For any other function it is the source line on which the parameter list ends. |
| fileName | 2 + n | Main function only: a string, which is a word length that counts the terminating zero and then that many bytes. |
| marked | 2 | Other functions only: the offset, in the code of the main function, of the pointer slot of the PUSHFUNCTION instruction that creates this function. |
| code | size | The byte code. |
| symbols | Any number of symbol records. |
The main function comes first. The other functions of the source follow it as
records of their own, all at one level, however deeply they are nested in the source.
The chunk ends at the first byte after a record that is not another
F.
Symbol records
| Field | Size | Meaning |
|---|---|---|
| tag | 1 | 56 (V, a global variable) or 53 (S, a string constant). |
| head | 2 | Offset in this function’s code of the last operand that uses the name. |
| name | 2 + n | A string. |
The records of a function form chains through its code. Each word operand that uses
a name holds, in the file, the offset of the previous operand that uses the same
name, and 0 ends the chain. The loader walks the chain from the head and writes into
each operand the number its own symbol table gives the name. V chains
run through the operands of PUSHGLOBAL and STOREGLOBAL;
S chains run through the operands of PUSHSTRING,
PUSHSELF and each word of STORERECORD.
Instructions
| Operand | Bytes | Meaning |
|---|---|---|
| b, c | 1 | an unsigned byte |
| w | 2 | a word |
| f | 4 | a single-precision float |
| p | sizeof(TFunc*) | the slot the loader fills with the address of a function; the value in the file carries no information |
| V, S | 2 | a word that is a link of a variable or string chain |
| J | 2 | a word: a distance in bytes, counted from the following instruction |
Opcodes 0 to 66 are the same as in Lua 2.4.
VARARGS is in 2.5 and 3.0; STOREMAP is new in 3.0.
| Op | Name | Operands | Action |
|---|---|---|---|
| 0 | PUSHNIL | push nil | |
| 1 | PUSH0 | push the number 0 | |
| 2 | PUSH1 | push the number 1 | |
| 3 | PUSH2 | push the number 2 | |
| 4 | PUSHBYTE | b | push the number b |
| 5 | PUSHWORD | w | push the number w |
| 6 | PUSHFLOAT | f | push the number f |
| 7 | PUSHSTRING | S | push the string |
| 8 | PUSHFUNCTION | p | push the function whose record names this slot |
| 9 | PUSHLOCAL0 | push local 0 | |
| 10 | PUSHLOCAL1 | push local 1 | |
| 11 | PUSHLOCAL2 | push local 2 | |
| 12 | PUSHLOCAL3 | push local 3 | |
| 13 | PUSHLOCAL4 | push local 4 | |
| 14 | PUSHLOCAL5 | push local 5 | |
| 15 | PUSHLOCAL6 | push local 6 | |
| 16 | PUSHLOCAL7 | push local 7 | |
| 17 | PUSHLOCAL8 | push local 8 | |
| 18 | PUSHLOCAL9 | push local 9 | |
| 19 | PUSHLOCAL | b | push local b |
| 20 | PUSHGLOBAL | V | push the global |
| 21 | PUSHINDEXED | pop a key; replace the table under it with its value at that key | |
| 22 | PUSHSELF | S | for a method call: replace the object on top with its field of that name, then push the object again |
| 23 | STORELOCAL0 | pop into local 0 | |
| 24 | STORELOCAL1 | pop into local 1 | |
| 25 | STORELOCAL2 | pop into local 2 | |
| 26 | STORELOCAL3 | pop into local 3 | |
| 27 | STORELOCAL4 | pop into local 4 | |
| 28 | STORELOCAL5 | pop into local 5 | |
| 29 | STORELOCAL6 | pop into local 6 | |
| 30 | STORELOCAL7 | pop into local 7 | |
| 31 | STORELOCAL8 | pop into local 8 | |
| 32 | STORELOCAL9 | pop into local 9 | |
| 33 | STORELOCAL | b | pop into local b |
| 34 | STOREGLOBAL | V | pop into the global |
| 35 | STOREINDEXED0 | with table, key and value on top, store the value and pop all three | |
| 36 | STOREINDEXED | b | store the top value in the table and key that lie b places further down; pop only the value |
| 37 | STORELIST0 | b | pop b values into the table below them, as items 1 to b |
| 38 | STORELIST | b c | pop c values into the table below them, as items b × 40 + 1 onwards (FIELDS_PER_FLUSH = 40) |
| 39 | STORERECORD | b, then b × S | pop b values into the named fields of the table below them. The 3.0 compiler no longer emits it |
| 40 | ADJUST0 | cut the stack back to the base of the function | |
| 41 | ADJUST | b | make the stack exactly b slots above the base, filling with nil or cutting |
| 42 | CREATEARRAY | w | push a new table sized for w entries |
| 43 | EQOP | pop y and x, push 1 if they are equal, else nil | |
| 44 | LTOP | pop y and x, push 1 if x < y, else nil | |
| 45 | LEOP | pop y and x, push 1 if x <= y, else nil | |
| 46 | GTOP | pop y and x, push 1 if x > y, else nil | |
| 47 | GEOP | pop y and x, push 1 if x >= y, else nil | |
| 48 | ADDOP | pop y and x, push x + y | |
| 49 | SUBOP | pop y and x, push x − y | |
| 50 | MULTOP | pop y and x, push x × y | |
| 51 | DIVOP | pop y and x, push x / y | |
| 52 | POWOP | pop y and x, push x ^ y through the fallback or tag method | |
| 53 | CONCOP | pop y and x, push x .. y | |
| 54 | MINUSOP | negate the value on top | |
| 55 | NOTOP | replace nil on top with 1, and any other value with nil | |
| 56 | ONTJMP | J | jump forward if the value on top is not nil; the value stays |
| 57 | ONFJMP | J | jump forward if the value on top is nil; the value stays |
| 58 | JMP | J | jump forward |
| 59 | UPJMP | J | jump back |
| 60 | IFFJMP | J | pop a value; jump forward if it is nil |
| 61 | IFFUPJMP | J | pop a value; jump back if it is nil |
| 62 | POP | pop one value | |
| 63 | CALLFUNC | b c | call the function that has b arguments above it; keep c results, all of them when c is 255 |
| 64 | RETCODE0 | return the values from the base of the function to the top | |
| 65 | RETCODE | b | return the values from stack slot b to the top |
| 66 | SETLINE | w | record that line w is running (debug information) |
| 67 | VARARGS | b | for a function with ...: keep b fixed parameters and put the other arguments in a table, the local arg, with their number in its field n |
| 68 | STOREMAP | b | pop b key and value pairs into the table below them |
nil is the false value; these releases have no boolean type.
The main function ends with RETCODE0; any other function ends with
RETCODE0 or with RETCODE followed by its number of locals. A
record stores no parameter count. A function with parameters starts with
ADJUST0 or ADJUST b, after the first SETLINE
if there is one, and b is the number of parameters; a function with
... starts with VARARGS b instead.
What the loader rejects
- Next chunk: where a chunk should start, anything but
1Bor the end of the input (“not a lua binary file”). - Signature: anything other than
Luaafter it (“bad signature”). - Sizes: when the version byte is above
0x23,sizeof(Word)must be 2,sizeof(float)4, andsizeof(TFunc*)that of the loading build. - Test float: it must equal the loader’s own, either as read or with its bytes reversed (“different float representation” in 2.5, “unknown float representation” in 3.0).
- Code size: more than 65,535 bytes (“code too long”).
- Opcode: only while swapping bytes, a byte that the loader’s table of lengths does not list (“corrupt binary file”).
The loader checks nothing else. It does not test the results of its reads, the marked offsets, the chains or the jumps, and it has no code verifier.
What luac writes
- One chunk for each source file, one after the other when several are given.
- Debug information: only with
-d, and it consists ofSETLINEinstructions. Local variable names are not saved. - Long code:
luac2.5 and 3.0 only warn when a function’s code does not fit in 16 bits, although the loaders refuse it.
Program behaviour
- Vararg functions in 2.5: the parser emits
VARARGS, but the part ofluac2.5 that builds the chains has no case for it, so it stops with “cannot happen: opcode=67” and leaves an incomplete file. The byte-swapping part of the 2.5 loader has no case for it either. - Byte order and 8-byte pointers: while swapping bytes, both loaders step over
PUSHFUNCTIONas an instruction of 5 bytes whatever the pointer size, so they lose their place in a swapped chunk written with 8-byte pointers. - Byte order: a test word other than
0x1234turns on the swapping of words. A test float that differs is reversed and compared again. Both settings are kept for the rest of the run and are never turned off for a later chunk. - Lua 2.4 chunks: both loaders accept version byte
0x23, which has no size bytes, but put an address of their own pointer size into its 4-byte slot. Built with 8-byte pointers, they overwrite the 4 bytes that follow. - A chunk with no function record leaves the loader with the main function of the chunk before it, or with none. A first function record that is not a main function makes it write through a pointer that is missing or already freed.
- Chains: neither the head nor a link is checked against the length of the code, and a chain that returns to an offset it has visited may never end.
How a file is recognised
lua_dofile looks at the first byte: 1B means a precompiled
file, anything else source (opcode.c in 2.5, inout.c in
3.0, which then reopens the file in binary mode). It loads and runs one chunk after
another until the file ends. In 3.0, lua_dobuffer takes only
precompiled chunks and lua_dostring only source. luac writes
luac.out unless another name is given with -o.
luac -u in 3.0 lists a precompiled file.
Example
The chunk that luac 2.5 writes on a little-endian machine with 8-byte
pointers for a file ex.lua holding the function
function f(a) return a end on three lines. The bytes of the
PUSHFUNCTION slot are whatever address was in memory.
1B 4C 75 61 signature
25 version 2.5 or 3.0
02 04 08 sizes of Word, float and pointer
34 12 test word
46 0A BF 17 test float
46 function record
00 00 0D 00 code: 13 bytes
00 00 lineDefined 0: the main function
07 00 fileName: 7 bytes
65 78 2E 6C 75 61 00 "ex.lua"
08 50 7E 75 03 01 00 00 00 PUSHFUNCTION (slot at offset 1)
22 00 00 STOREGLOBAL (end of the chain)
40 RETCODE0
56 variable record
0A 00 head: offset 10
02 00 66 00 "f"
46 function record
00 00 07 00 code: 7 bytes
01 00 lineDefined 1
01 00 marked: offset 1 of the main code
29 01 ADJUST 1 (one parameter)
09 PUSHLOCAL0
41 01 RETCODE 1 (return a)
41 01 RETCODE 1 (end of the function)
Sources
The Lua 2.5 and 3.0 sources from lua.org:
undump.c, undump.h, opcode.h,
opcode.c, inout.c (3.0) and, of the compiler,
luac.c, dump.c and print.c.