Lua’s authors do not document the precompiled format; it is defined by the source of
each release. This page follows undump.c, undump.h,
opcode.h, opcode.c and the luac sources of Lua
2.4, 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 | 0x23. |
5 | 2 | test word | 0x1234 in the writer’s byte order: 34 12 little-endian, 12 34 big-endian. |
7 | 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. |
The header records no sizes. A word (Word) is 2 bytes, a float 4 bytes
and the pointer slot in the code 4 bytes. 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 4-byte operand 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 | 4 | 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 |
| 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 |
| 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 | |
| 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) |
nil is the false value; Lua 2.4 has 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.
What the loader rejects
- Next chunk: where a chunk should start, anything but
1Bor the end of the file (“not a lua binary file”). - Signature: anything other than
Luaafter it (“bad signature”). - Test float: it must equal the loader’s own, either as read or with its bytes reversed (“different float representation”).
- Code size: more than 65,535 bytes (“code too long”).
- Opcode: only while swapping bytes, a byte that is not in the table (“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:
luaconly warns when a function’s code does not fit in 16 bits, although the loader refuses it.
Program behaviour
- Version byte: the loader skips it without comparing it. Given a Lua 2.5 chunk, which has three size bytes after the version, it reads those bytes as part of the test word.
- Byte order: any 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. If either is on, the loader rewrites the word and float operands of the code. - Machines with 8-byte pointers: the pointer slot stays 4 bytes, so only 4 bytes of a function’s address are stored in the code, and the other 4 are not set when a pointer is rebuilt from the slot.
luacrebuilds it when it writes a chunk (dump.c) and the interpreter when it runsPUSHFUNCTION. Unless the address fits in 4 bytes and the unset bytes happen to be zero,luacfails on a source file that has a function and the interpreter on a chunk that has one. - 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). It loads and runs one chunk after
another until the file ends. luac writes luac.out unless
another name is given with -o.
Example
The chunk that luac 2.4 writes on a little-endian machine for a file
ex.lua holding the function function f(a) return a end on
three lines. The four bytes of the PUSHFUNCTION slot are whatever
address was in memory.
1B 4C 75 61 signature
23 version 2.4
34 12 test word
46 0A BF 17 test float
46 function record
00 00 09 00 code: 9 bytes
00 00 lineDefined 0: the main function
07 00 fileName: 7 bytes
65 78 2E 6C 75 61 00 "ex.lua"
08 80 2A 01 01 PUSHFUNCTION (slot at offset 1)
22 00 00 STOREGLOBAL (end of the chain)
40 RETCODE0
56 variable record
06 00 head: offset 6
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.4 sources from lua.org:
undump.c, undump.h, opcode.h,
opcode.c and, of the compiler, luac.c, dump.c
and print.c.