Symbols may be shared among sections through use of the XDEF and XREF directives. The XDEF directive instructs the Assembler that certain symbol definitions that occur within the current section are to be accessible by other sections:
XDEF symbol,symbol,...,symbol
The XREF directive instructs the Assembler that all references to symbol within the current section are references to a symbol that was declared public within another section with the XDEF directive:
XREF symbol,symbol,...,symbol
XDEFed symbols by default are recognized only in other sections which XREF them. They can be made fully global (recognizable by sections which do not XREF them) by use of the XR option. Alternatively, the GLOBAL directive may be used within a section to make the named symbols visible outside of the section. Both the XDEF and XREF directives must be used before the symbols to which they refer are defined or used in the section. See the listing below for another example.
SYM1 EQU 1 SECTION SECT1 XDEF SYM2 SYM1 EQU 2 SYM2 EQU 3 ENDSEC SECTION SECT2 XREF SYM2 MOVE #SYM1,R0 MOVE #SYM2,R1 ENDSEC MOVE #SYM2,R2
SYM1 is first defined outside of any section. Then in section SECT1SYM2 is declared public with an XDEF directive. SYM1 is also defined locally to section SECT1. In section SECT2, SYM2 is declared external via the XREF directive, followed by a move of SYM1 to R0. Since SYM1 was defined locally to section SECT1, the Assembler uses the global value and moves a 1 to R0. Because SYM2 was declared external in section SECT1 the value moved to R1 is 3. If SYM2 had not been XREFed in section SECT2 the value moved to R1 would have been unknown at this point. In the last instruction, it is not known what value will be moved to R2, since SYM2 was not defined outside of any section or was not declared GLOBAL within a section.
If the GLOBAL qualifier follows the sectionname in the SECTION directive, then all symbols defined in the section until the next ENDSEC directive are considered global. The effect is as if every symbol in the section were declared with the GLOBAL directive. This is useful when a section needs to be independently relocatable, but data hiding is not required.
If the LOCAL qualifier follows the sectionname in the SECTION directive, then all symbols defined in the section until the next ENDSEC directive are visible to the immediately enclosing section. The effect is as if every symbol in the section were defined within the parent section. This is useful when a section needs to be independently relocatable, but data hiding within an enclosing section is not required.
Symbols that are defined with the SET directive can be made visible with XDEF only in absolute mode, and the section name associated with the symbol is the section name of the section where the symbol was first defined. This is true even if the symbol value is changed in another section.