97

Can someone describe what a symbol table is within the context of C and C++?

Pooven
  • 1,744
  • 1
  • 25
  • 44
jdt141
  • 4,993
  • 6
  • 35
  • 36

7 Answers7

94

There are two common and related meaning of symbol tables here.

First, there's the symbol table in your object files. Usually, a C or C++ compiler compiles a single source file into an object file with a .obj or .o extension. This contains a collection of executable code and data that the linker can process into a working application or shared library. The object file has a data structure called a symbol table in it that maps the different items in the object file to names that the linker can understand. If you call a function from your code, the compiler doesn't put the final address of the routine in the object file. Instead, it puts a placeholder value into the code and adds a note that tells the linker to look up the reference in the various symbol tables from all the object files it's processing and stick the final location there.

Second, there's also the symbol table in a shared library or DLL. This is produced by the linker and serves to name all the functions and data items that are visible to users of the library. This allows the system to do run-time linking, resolving open references to those names to the location where the library is loaded in memory.

If you want to learn more, I suggest John Levine's excellent book "Linkers and Loaders".link text

Ben Combee
  • 16,831
  • 6
  • 41
  • 42
  • Hi Ben, in the second case, are you referring to the export table? That is, is the export table a special case of the symbol table? Or are these unrelated concepts? – Pooven Oct 15 '14 at 19:36
  • Hi. I believe it's not in any object file but referred from them. Secondly, could you please explain a bit more what you meant by "final location"? Physical address or finalized relative address in the source code. – stdout Aug 24 '16 at 13:21
  • Don't executables or shared libraries also have symbol tables? Can you explain why you didn't include them? – mfaani Apr 16 '23 at 18:49
28

Briefly, it is the mapping of the name you assign a variable to its address in memory, including metadata like type, scope, and size. It is used by the compiler.

That's in general, not just C[++]*. Technically, it doesn't always include direct memory address. It depends on what language, platform, etc. the compiler is targeting.

Steve Landey
  • 2,609
  • 25
  • 25
  • I think, it was very important you to mention the content of the address by saying "it doesn't always include direct memory address". – stdout Aug 24 '16 at 13:25
  • I think it is important to note what it is mapping of the name you assign a variable and **function** to its address (not only variable) – andreyk2 Hohlov Feb 19 '22 at 14:58
  • How does the compiler know where the variable is located iif the symbol table doesn't include the direct memory address? – Mehdi Charife Sep 01 '23 at 11:11
14

In Linux, you can use command:

nm [object file]

to list the symbol table of that object file. From this printout, you may then decipher the in-use linker symbols from their mangled names.

11

The symbol table is the list of "symbols" in a program/unit. Symbols are most often the names of variables or functions. The symbol table can be used to determine where in memory variables or functions will be located.

Joe Schneider
  • 9,179
  • 7
  • 42
  • 59
3

Check out the Symbol Table wikipedia entry:

data structure used by a language translator such as a compiler or interpreter, where each identifier (or symbol), constant, procedure and function in a program's source code is associated with information relating to its declaration or appearance in the source.

Allan Wind
  • 23,068
  • 5
  • 28
  • 38
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/34932117) – Adrian Mole Sep 01 '23 at 14:35
  • @AdrianMole Good point. Updated answer as requested. – Allan Wind Sep 02 '23 at 00:22
0

Symbol table is an important data structure created and maintained by compilers in order to store information about the occurrence of various entities such as variable names, function names, objects, classes, interfaces, etc.

rashedcs
  • 3,588
  • 2
  • 39
  • 40
0

From the "Computer Systems A Programmer’s Perspective" book, Ch 7 Linking. "Symbols and Symbol Tables":

Symbol table is information about functions and global variables that are defined and referenced in the program

And important note (form the same chapter):

It is important to realize that local linker symbols are not the same as local program variables. The symbol table does not contain any symbols that correspond to local nonstatic program variables. These are managed at run time on the stack and are not of interest to the linker

andreyk2 Hohlov
  • 619
  • 2
  • 9
  • 10