Weak Scope Declaration Directive

References to symbols with a weak scope are resolved within the object file in which the symbols are declared, and within other object files. Weak symbols with the same name in different object files may not refer to the same entity. When a symbol name is declared with a weak scope as well as a global or local scope, the global or local scope will take precedence over the weak scope in link time.

To declare one or more symbols with a weak scope, use the .weak directive. These symbols are flagged as weak symbols for the linkage editor. The weak scope declaration format for UNIX* (ELF) and Windows NT (COFF32) differ and are described in the sections that follow.

Weak Scope Declaration for UNIX (ELF)

For UNIX (ELF), use the .weak directive in the following format:

.weak    name1,name2, ...

Where:

name

Represents a symbol name.

The following example illustrates how to declare an undefined symbol with a weak scope. The defined symbol x: has a local scope. y has the attributes of x and has a local scope. The symbol y can then be declared with a weak scope using the .weak directive while keeping the other attributes of x.

x:
 y == x
 .weak y

 

Weak Scope Declaration for Windows NT (COFF32)

For Windows NT (COFF32), use the .weak directive in the following format to declare a symbol with a weak scope and search for defined symbols within other object files and libraries:

.weak    identifier1 = identifier2

Where:

identifier1

Represents a symbol name that is assigned a weak symbol scope, which is resolved in link time.

identifier2

Represents a symbol name that holds the symbol definition.

Use the following syntax to declare a symbol with a weak scope and search for defined symbols within other object files and not within libraries:

.weak    identifier1 == identifier2

Where:

identifier1

Represents a symbol name that is assigned a weak symbol scope, which is resolved in link time.

identifier2

Represents a symbol name that holds the symbol definition.

The following example illustrates a weak scope declaration where x: is a local defined symbol. x is the associated symbol for y. The .weak directive assigns y a weak scope.

x:
.weak y = x