Obj files symbols
Inspecting Obj files symbols
Symbol is one of the basic terms when talking about object files, linking, and so on. In fact, in C/C++ language, symbol is the corresponding entity of most user-defined variables, function names, mangled with namespace, class/struct/name, and so on. For example, a C/C++ compiler may generate symbols in an object file when people define non-static global variables or non-static functions, which are helpful for the linker to decide if different modules (object files, dynamic shared libraries, executables) would share the same data or code.
nm
We can inspect the symbols in the object file using the nm
command line tool.
1
nm object_file.o | c++filt
using nm
with c++filt
allows us to demangle C++ names.
let’s take the following example c++ code
1
2
3
4
5
6
7
8
9
10
// test.cpp
bool testFunc(){
return true;
} // defined function
int declaredFunc(bool par); // declared but not defined
static double staticVar = 0.5; // static global variable
int main(){
int var = 15;
return declaredFunc(true);
}
We can compile this file as an object file without linking it (using the -c flag).
1
g++ -c test.cpp -o test.o
Then we can inspect the output object file using nm
.
1
nm test.o | c++filt
we should get something similar.
1
2
3
4
5
000000000000000f T main
U declaredFunc(bool)
0000000000000000 T testFunc()
0000000000000000 d staticVar
each line represents
1
symbol value symbol type symbol name
Symobl types
The following diagram shows the memory layout of the object file.
Object files are divided into sections, each serving a specific purpose. Common sections include:
- Text Section: Contains the machine code (executable instructions) of the program
- Data Section: Stores initialized global and static variables.
- BSS Section: Reserved for uninitialized global and static variables. It doesn’t occupy space in the object file but is allocated when the program is loaded into memory.
We will use this info to understand the symbol types below.
T t
This symbol is in the object file’s .text code section.
This is most of the time the case for any defined function in our code.
example: main
and testFunc
in the code above.
U
This symbol is undefined and no definition is found in the object file. This symbol could come from another library during linking. If it still couldn’t be found, the linker should throw an error.
example: declaredFunc(bool)
in the code above.
D d
The symbol is in the initialized data section.
example: staticVar
in the code above
Comments powered by Disqus.