Section 12.8

Symbol table

Definition

A symbol table is a table, inside a file of machine code from a compiler, that connects names to places in the program. Each entry, called a symbol, records the name of a function or global variable with a value (normally an address), a size, a type and a binding, which says whether other files may use the name. Linkers §12.4, which join such files into one program, use it to connect them. Debuggers, which inspect running programs, use it to turn addresses back into names. The processor never reads it.

The idea

A processor works with addresses only. People and tools work with names. A linker, the tool that joins compiled files into one program, must connect the name checksum used in one file to the function defined in another. A debugger should report that the program stopped in read_low, not at address 0x00000024 (the prefix 0x marks a number written in hexadecimal, base 16). So the compiler writes a directory of names next to the code.

The picture is the directory board in the lobby of an office building. Taking the board down removes no rooms and no people: the building works as before, and only visitors are lost. That is what stripping, the deletion of the symbol table from a finished program, does. The board also lists only rooms that exist. An office that was merged into two larger ones has no line on the board, although its work is still done, now in two places. That is what inlining does, in which the compiler copies the body of a function into the functions that call it. The picture stops being accurate in one respect: a symbol table also lists names that the file needs and does not contain.

How it works

The description uses ELF §12.6 (Executable and Linking Format), the file format for compiled programs on Linux and on many embedded systems. An ELF file keeps its symbol table in a part of the file named .symtab, and the text of the names in a second part named .strtab, the string table. Each symbol has six fields.

One entry takes 16 bytes in a 32-bit file and 24 bytes in a 64-bit file. The tools nm and readelf -s print the table in readable form.

The table changes while the program is built.

  1. The compiler and the assembler create a symbol for each function, and for each variable with a fixed place in memory, that still exists as a separate item after optimization.

  2. The linker merges the tables of its input files, connects the names that files need to their definitions, and replaces positions inside sections by final addresses.

  3. A tool such as strip can then delete the table. Unless it is told otherwise, strip removes all symbols, and the information for a debugger with them.

A program that uses shared libraries, which are libraries kept in separate files and connected when the program starts, has a second, smaller table named .dynsym. It lists the names needed for that connection, and it stays in the file when .symtab is removed.

Worked example

One entry of a symbol table, taken from a 32-bit program for an Arm microcontroller §6.1 (a small computer on one chip), consists of these 16 bytes, shown in hexadecimal:

15 00 00 00 00 00 00 20 04 00 00 00 11 00 02 00

Numbers of several bytes are stored in this file with the least significant byte first.

The entry says: a global variable named limit, 4 bytes long, is at address 0x20000000 in the data section.

The same program shows what optimization and stripping do. Its source defines the variable limit, a small function clamp that is marked static, meaning usable only inside its own source file, and two functions that each call clamp:

uint32_t limit = 100;
static uint32_t clamp(uint32_t value)
{ return value > limit ? limit : value; }
uint32_t read_low(uint32_t raw) { return clamp(raw) + 1; }
uint32_t read_high(uint32_t raw) { return clamp(raw * 2) + 2; }

It is built with version 13.2 of the compiler arm-none-eabi-gcc and the processor options -mcpu=cortex-m4 -mthumb.

build symbols for the three functions, with sizes in bytes
not optimized (-O0) clamp 36, read_low 26, read_high 30
optimized (-O2) read_low 20, read_high 20
optimized, then strip none

In the optimized build the compiler copied the body of clamp into both callers, which is called inlining. The instructions that compare and limit the value now appear twice, once inside each caller. No block of code is clamp on its own, so there is no symbol for it. After strip the file has no symbol table and no information for a debugger. Its code and data are unchanged, byte for byte, and so is the start address in the header of the file.

Stated precisely

A symbol table entry makes one statement: the name NN refers to the value VV, with size SS, type TT and binding BB, in section XX. For a function symbol in an executable file this means that the code labeled NN starts at address VV and is SS bytes long. On Arm processors one bit of VV is a marker and not part of the address, as the limits below describe.

The table carries no promise of being complete. The format has no rule that every function in the source code has a symbol, that every byte of code lies inside the range of some symbol, or that a call to a function goes through the address of its symbol.

The table is not needed to run a program that is linked completely before it starts. Loading uses a different table of the file, the program header table, and .symtab is normally not among the parts that are loaded. Removing it therefore leaves the loaded bytes unchanged.

Limits and common mistakes

A symbol that is present does not show that calls go through it. If clamp in the worked example is not marked static, the optimized build keeps a separate copy with a symbol, because other files might call it, and still copies the body into both callers. A hook §14.26, extra code attached to a function so that it runs on every call, placed at the address of the symbol then sees none of those calls.

A symbol can lack a size or a type: a label in assembly language, the text form of machine instructions, gets size 0 and no type unless its author states them. Several symbols can also share one value, so a tool that turns an address into a name may have to choose among them.

On Arm processors that run the Thumb instruction set §4.40, one of the two instruction sets of 32-bit Arm processors, the value of a function symbol is the address of the function plus 1. The lowest bit is a marker, not part of the address, and must be cleared before the value is used as an address.

Names are labels written by whoever produced the file. Nothing in the format ties a name to what the code at that address does.

Questions answered

What remains knowable after symbols are stripped or functions are inlined?

After stripping, every instruction and every data byte of the program remains, and its directory of names is gone. A symbol table is the list, inside a file of machine code that a compiler produced, that connects names such as function names to addresses and sizes. Stripping deletes it, normally together with the information for a debugger, a tool for inspecting a running program. The code can still be run and can still be disassembled, meaning turned back into readable instruction text. The start address and the memory layout stay in the headers of the file, and the names needed to connect shared libraries when the program starts stay in a separate table. What can no longer be read off is where each function begins and ends and what it was called. The boundaries have to be reconstructed from the code, for example from the destinations of call instructions, and the original names cannot be recovered from the file.

After inlining, the computation of the function remains, and its existence as a separate block of code does not. Inlining means that the compiler copied the body of the function into each caller. The work is still done, once in each caller and possibly simplified differently in each. There is no single address where the function starts. A symbol with its name exists only if the compiler also kept a separate copy of the function. If the file still has debug information §12.9, the data that lets a debugger relate the program to its source, that information records each inlined copy with the name of the function and the address range of the copy. Without it, nothing in the file marks which instructions came from the inlined function.

In both cases names, instructions and behavior are three separate things. Stripping removes names and keeps instructions. Inlining rearranges instructions and keeps behavior.

Related entries: Debug information (DWARF) (§12.9), Link-time optimization (LTO) (§12.10), Capstone (§12.34), Static versus dynamic instrumentation (§14.25), ELF (§12.6), Linker (§12.4), Probe and hook (§14.26), Relocation: turning a symbolic reference into exact encoded bits (§12.50). Further reading: [110].