An object file's symbol table holds information needed to locate and relocate a program's symbolic definitions and references. A symbol table index is a subscript into this array. Index 0 both designates the first entry in the table and serves as the undefined symbol index. The contents of the initial entry are specified later in this section.
Table 7-19 Symbol Table Initial Entry
Name |
Value |
---|---|
STN_UNDEF |
0 |
A symbol table entry has the following format (defined in sys/elf.h):
typedef struct { Elf32_Word st_name; Elf32_Addr st_value; Elf32_Word st_size; unsigned char st_info; unsigned char st_other; Elf32_Half st_shndx; } Elf32_Sym; typedef struct { Elf64_Word st_name; unsigned char st_info; unsigned char st_other; Elf64_Half st_shndx; Elf64_Addr st_value; Elf64_Xword st_size; } Elf64_Sym; |
This member holds an index into the object file's symbol string table, which holds the character representations of the symbol names. If the value is nonzero, it represents a string table index that gives the symbol name. Otherwise, the symbol table entry has no name.
External C symbols have the same names in C and in object files' symbol tables.
This member gives the value of the associated symbol. Depending on the context, this can be an absolute value, an address, and so forth. See "Symbol Values".
Many symbols have associated sizes. For example, a data object's size is the number of bytes contained in the object. This member holds 0 if the symbol has no size or an unknown size.
This member specifies the symbol's type and binding attributes. A list of the values and meanings appears below. The following code shows how to manipulate the values (defined in sys/elf.h):
#define ELF32_ST_BIND(i) ((i) >> 4) #define ELF32_ST_TYPE(i) ((i) & 0xf) #define ELF32_ST_INFO(b, t) (((b)<<4)+((t)&0xf)) #define ELF64_ST_BIND(info) ((info) >> 4) #define ELF64_ST_TYPE(info) ((info) & 0xf) #define ELF64_ST_INFO(bind, type) (((bind)<<4)+((type)&0xf)) |
This member currently specifies a symbol's visibility. A list of the values and meanings appears below. The following code shows how to manipulate the values for both 32 and 64-bit objects. Other bits contain 0 and have no defined meaning.
#define ELF32_ST_VISIBILITY(o) ((o)&0x3) #define ELF64_ST_VISIBILITY(o) ((o)&0x3) |
Every symbol table entry is defined in relation to some section; this member holds the relevant section header table index. Some section indexes indicate special meanings. See Table 7-12.
A symbol's binding determines the linkage visibility and behavior.
Table 7-20 Symbol Binding, ELF32_ST_BIND & ELF64_ST_BIND
Name |
Value |
---|---|
STB_LOCAL |
0 |
STB_GLOBAL |
1 |
STB_WEAK |
2 |
STB_LOOS |
10 |
STB_HIOS |
12 |
STB_LOPROC |
13 |
STB_HIPROC |
15 |
Local symbols are not visible outside the object file containing their definition. Local symbols of the same name can exist in multiple files without interfering with each other.
Global symbols are visible to all object files being combined. One file's definition of a global symbol will satisfy another file's undefined reference to the same global symbol.
Weak symbols resemble global symbols, but their definitions have lower precedence.
Values in this inclusive range are reserved for operating system-specific semantics.
Values in this inclusive range are reserved for processor-specific semantics.
Global and weak symbols differ in two major ways:
When the link-editor combines several relocatable object files, it does not allow multiple definitions of STB_GLOBAL symbols with the same name. On the other hand, if a defined global symbol exists, the appearance of a weak symbol with the same name will not cause an error. The link-editor honors the global definition and ignores the weak ones.
Similarly, if a common symbol exists (that is, a symbol with the st_shndx field holding SHN_COMMON), the appearance of a weak symbol with the same name does not cause an error. The link-editor uses the common definition and ignores the weak one. See "Symbol Resolution" for more information.
When the link-editor searches archive libraries (see "Archive Processing") , it extracts archive members that contain definitions of undefined or tentative global symbols. The member's definition can be either a global or a weak symbol.
The link-editor, by default, does not extract archive members to resolve undefined weak symbols. Unresolved weak symbols have a zero value. The use of -zweakextract overrides this default behavior, and allows weak references to cause the extraction of archive members.
Weak symbols are intended primarily for use in system software. Their use in application programs is discouraged.
In each symbol table, all symbols with STB_LOCAL binding precede the weak and global symbols. As "Sections" describes, a symbol table section's sh_info section header member holds the symbol table index for the first non-local symbol.
A symbol's type provides a general classification for the associated entity.
Table 7-21 Symbol Types, ELF32_ST_TYPE & ELF64_ST_TYPE
Name |
Value |
---|---|
STT_NOTYPE |
0 |
STT_OBJECT |
1 |
STT_FUNC |
2 |
STT_SECTION |
3 |
STT_FILE |
4 |
STT_COMMON |
5 |
STT_LOOS |
10 |
STT_HIOS |
12 |
STT_LOPROC |
13 |
STT_SPARC_REGISTER |
13 |
STT_HIPROC |
15 |
The symbol type is not specified.
This symbol is associated with a data object, such as a variable, an array, and so forth.
This symbol is associated with a function or other executable code.
This symbol is associated with a section. Symbol table entries of this type exist primarily for relocation and normally have STB_LOCAL binding.
Conventionally, the symbol's name gives the name of the source file associated with the object file. A file symbol has STB_LOCAL binding, its section index is SHN_ABS, and it precedes the other STB_LOCAL symbols for the file, if it is present. Symbol index 1 of the SHT_SYMTAB is an STT_FILE symbol representing the file itself. Conventionally, this is followed by the files STT_SECTION symbols, and any global symbols that have been reduced to locals (see "Reducing Symbol Scope", and Chapter 5, Versioning for more details).
This symbol labels an uninitialized common block, however it is treated exactly the same as STT_OBJECT.
values in this inclusive range are reserved for operating system-specific semantics.
Values in this inclusive range are reserved for processor-specific semantics.
Function symbols (those with type STT_FUNC) in shared object files have special significance. When another object file references a function from a shared object, the link-editor automatically creates a procedure linkage table entry for the referenced symbol. Shared object symbols with types other than STT_FUNC will not be referenced automatically through the procedure linkage table.
A symbol's visibility, although it may be specified in a relocatable object, defines how that symbol may be accessed once it has become part of an executable or shared object.
Table 7-22 Symbol Visibility
Name |
Value |
---|---|
STV_DEFAULT |
1 |
STV_INTERNAL |
2 |
STV_HIDDEN |
3 |
STV_PROTECTED |
4 |
The visibility of symbols with the STV_DEFAULT attribute is as specified by the symbol's binding type. That is, global and weak symbols are visible outside of their defining component (executable file or shared object). Local symbols are hidden, as described below. Global and weak symbols are also preemptable, that is, they may by interposed by definitions of the same name in another component.
A symbol defined in the current component is protected if it is visible in other components but not preemptable, meaning that any reference to such a symbol from within the defining component must be resolved to the definition in that component, even if there is a definition in another component that would interpose by the default rules. A symbol with STB_LOCAL binding will not have STV_PROTECTED visibility.
A symbol defined in the current component is hidden if its name is not visible to other components. Such a symbol is necessarily protected. This attribute is used to control the external interface of a component. Note that an object named by such a symbol may still be referenced from another component if its address is passed outside.
A hidden symbol contained in a relocatable object is either removed or converted to STB_LOCAL binding by the link-editor when the relocatable object is included in an executable file or shared object.
This visibility attribute is currently reserved. If used in future its semantics will be at least as restrictive as STV_HIDDEN.
None of the visibility attributes affects resolution of symbols within an executable or shared object during link-editing; such resolution is controlled by the binding type. Once the link-editor has chosen its resolution, these attributes impose two requirements, both based on the fact that references in the code being linked may have been optimized to take advantage of the attributes.
First, all of the non-default visibility attributes, when applied to a symbol reference, imply that a definition to satisfy that reference must be provided within the current executable or shared object. If such a symbol reference has no definition within the component being linked, then the reference must have STB_WEAK binding and is resolved to zero.
Second, if any reference to or definition of a name is a symbol with a non-default visibility attribute, the visibility attribute must be propagated to the resolving symbol in the linked object. If different visibility attributes are specified for distinct references to or definitions of a symbol, the most constraining visibility attribute must be propagated to the resolving symbol in the linked object. The attributes, ordered from least to most constraining, are: STV_PROTECTED, STV_HIDDEN and STV_INTERNAL.
If a symbol's value refers to a specific location within a section, its section index member, st_shndx, holds an index into the section header table. As the section moves during relocation, the symbol's value changes as well, and references to the symbol continue to point to the same location in the program. Some special section index values give other semantics:
This symbol has an absolute value that will not change because of relocation.
This symbol labels a common block that has not yet been allocated. The symbol's value gives alignment constraints, similar to a section's sh_addralign member. That is, the link-editor will allocate the storage for the symbol at an address that is a multiple of st_value. The symbol's size tells how many bytes are required.
This section table index means the symbol is undefined. When the link-editor combines this object file with another that defines the indicated symbol, this file's references to the symbol will be bound to the actual definition.
As mentioned above, the symbol table entry for index 0 (STN_UNDEF) is reserved; it holds the following values.
Table 7-23 Symbol Table Entry: Index 0
Name |
Value |
Note |
---|---|---|
st_name |
0 |
No name |
st_value |
0 |
Zero value |
st_size |
0 |
No size |
st_info |
0 |
No type, local binding |
st_other |
0 |
|
st_shndx |
SHN_UNDEF |
No section |
Symbol table entries for different object file types have slightly different interpretations for the st_value member.
In relocatable files, st_value holds alignment constraints for a symbol whose section index is SHN_COMMON.
In relocatable files, st_value holds a section offset for a defined symbol. That is, st_value is an offset from the beginning of the section that st_shndx identifies.
In executable and shared object files, st_value holds a virtual address. To make these files' symbols more useful for the runtime linker, the section offset (file interpretation) gives way to a virtual address (memory interpretation) for which the section number is irrelevant.
Although the symbol table values have similar meanings for different object files, the data allow efficient access by the appropriate programs.
The SPARC architecture supports register symbols, that is, a symbol that initializes a global register. A symbol table entry for a register symbol contains the following entries.
Table 7-24 SPARC: Symbol Table Entry: Register Symbol
Field |
Meaning |
---|---|
st_name |
Index into string table of the name of the symbol, or 0 for a scratch register. |
st_value | Register number. See the ABI manual for integer register assignments. |
st_size |
Unused (0). |
st_info | Bind is typically STB_GLOBAL, type must be STT_SPARC_REGISTER. |
st_other |
Unused (0). |
st_shndx |
SHN_ABS if this object initializes this register symbol; SHN_UNDEF otherwise. |
Table 7-25 SPARC: Register Numbers
Name |
Value |
Meaning |
---|---|---|
STO_SPARC_REGISTER_G2 |
0x2 |
%g2 |
STO_SPARC_REGISTER_G3 |
0x3 |
%g3 |
Absence of an entry for a particular global register means that the particular global register is not used at all by the object.