Accessing data through the toc entry with tc storage-mapping class

An external data item is accessed by first getting that item’s address out of the TOC, and then using that address to get the data.

An external data item is accessed by first getting that item's address out of the TOC, and then using that address to get the data. In order to do this, proper relocation information must be provided to access the correct TOC entry. The .toc and .tc pseudo-ops generate the correct information to access a TOC entry. The following code shows how to access item a using its TOC entry:

       .set     RTOC,2
       .csect prog1[pr]          #prog1 is a csect 
                                 #containing instrs.
       ...
       l  5,TCA(RTOC)            #Now GPR5 contains the
                                 #address of a[rw].
...
       .toc
TCA:   .tc  a[tc],a[rw]          #1st parameter is TOC entry
                                 #name, 2nd is contents of
                                 #TOC entry.
       .extern a[rw]             #a[rw] is an external symbol.
This same method is used to access a program's static internal data, which is data that retains its value over a call, but which can only be accessed by the procedures in the file where the data items are declared. Following is the C language data having the static attribute:

static int xyz;
This data is given a name determined by convention. In XCOFF, the name is preceded by an underscore:

       .csect prog1[pr]
       ...
       l 1,STprog1(RTOC)         #Load r1 with the address
                                 #prog1's static data.
       ...
       .csect _prog1[rw]         #prog1's static data.
       .long    0
       ...
       .toc
STprog1: .tc.prog1[tc],_prog1[rw]       #TOC entry with address of
                                        #prog1's static data.