Mach-O Reference
LC_SYMTABLC_SYMTAB — the symbol and string tables
LC_SYMTAB is the load command that locates a Mach-O file's classic symbol table: an array of nlist entries plus the string table their names point into. It is what nm and a linker read.
What it is
LC_SYMTAB is a short, fixed-size load command
that tells a tool where the file's symbol table and string table live.
It does not contain the symbols itself — only four offsets into the file.
Fields
| Field | Meaning |
|---|---|
cmd | LC_SYMTAB (0x2). |
cmdsize | 24 bytes — the command is fixed size. |
symoff | File offset of the first nlist entry. |
nsyms | Number of symbol-table entries. |
stroff | File offset of the string table. |
strsize | Size of the string table in bytes. |
The symbol and string tables normally sit inside the
__LINKEDIT segment at the end of the file.
The nlist entry
Each of the nsyms entries is an nlist_64 (or nlist on 32-bit files):
| Field | Meaning |
|---|---|
n_strx | Offset into the string table for the symbol's name (0 = no name). |
n_type | Type byte: external bit N_EXT, the N_TYPE field (N_UNDF, N_SECT, N_ABS, N_INDR), and debug (N_STAB) bits. |
n_sect | 1-based section number the symbol is defined in, or NO_SECT. |
n_desc | Extra flags (weak references, library ordinals, and so on). |
n_value | The symbol's address — or 0 for an undefined symbol. |
A symbol is defined when its N_TYPE is N_SECT (it lives in a section)
or N_ABS; it is undefined (imported from a
dynamic library) when N_TYPE is N_UNDF.
Debugging stabs (N_STAB set) are a separate, older channel and are usually
filtered out when counting real symbols.
How a parser reads it
Walk the load commands until LC_SYMTAB, then
read nsyms entries starting at symoff. For each entry, the name is the
NUL-terminated string at stroff + n_strx. Dynamic-linking metadata — which
symbols are local, which are external definitions, and which are undefined
imports — is further organised by LC_DYSYMTAB, which indexes into this same
table.