9.4. ELF Extensions to the GLOBAL Directive

ELF object files can contain more information about a global symbol than just its address: they can contain the size of the symbol and its type as well. These are not merely debugger conveniences, but are actually necessary when the program being written is a ((shared library)). Yasm therefore supports some extensions to the NASM syntax GLOBAL directive (see Section 5.6), allowing you to specify these features. Yasm also provides the ELF-specific directives in Section 9.3 to allow specifying this information for non-global symbols.

You can specify whether a global variable is a function or a data object by suffixing the name with a colon and the word function or data. (((object)) is a synonym for data.) For example:

global   hashlookup:function, hashtable:data

exports the global symbol hashlookup as a function and hashtable as a data object.

Optionally, you can control the ELF visibility of the symbol. Just add one of the visibility keywords: default, internal, hidden, or protected. The default is default, of course. For example, to make hashlookup hidden:

global   hashlookup:function hidden

You can also specify the size of the data associated with the symbol, as a numeric expression (which may involve labels, and even forward references) after the type specifier. Like this:

global  hashtable:data (hashtable.end - hashtable)

hashtable:
        db this,that,theother  ; some data here
.end:

This makes Yasm automatically calculate the length of the table and place that information into the ELF symbol table. The same information can be given more verbosely using the TYPE (see Section 9.3.3) and SIZE (see Section 9.3.2) directives as follows:

global  hashtable
type hashtable object
size hashtable hashtable.end - hashtable

hashtable:
        db this,that,theother  ; some data here
.end:

Declaring the type and size of global symbols is necessary when writing shared library code.