8.4.1 Global pointer (gp)

Data that is globally declared in an application is allocated to memory. When referencing (loading or storing) this data that has been allocated to memory, the global pointer (gp) is provided to enable access independent of the allocation position (PID: Position Independent Data).

(1)

Setting the global pointer (gp)

Set the value of the global pointer symbol (gp symbol) as the address set in the global pointer (gp).

-

The gp symbol is handled as the constant symbol name "__gp_data".

-

Declare the gp symbol as a reference symbol (.extern) in the startup routine.

-

rlink creates an externally defined symbol (.public) and determines the address of the gp symbol.

-

The operations for defining and referencing the gp symbol are as follows.

(a)

If there is only an ".extern" declaration of the gp symbol

rlink creates the definition information and sets the address automatically.

(b)

If the gp symbol is defined

The defined address is used.

(c)

If there is no gp symbol (it is not used)

The following error message is output for code that makes references relative to the gp.

Undefined external symbol "GP-symbol (__gp_data)" referenced in "FILE"

(2)

Rules for determining the gp symbol value

The value of the gp symbol is determined from sections with relocation attributes of sdata, sbss, sdata23, and sbss23, in the following order of precedence.

Figure 8.1

Rules for Determining the Global Pointer Value

 

Below is an example of how the value of the gp symbol is determined.

(a)

If there is a section with a relocation attribute of sdata or sbss

Sets the halfway point between the start address of the sdata or sbss section with the smallest address, and the end address of the sdata or sbss section with the greatest address (multiple of 2; if the midway point is an odd number, takes the first multiple of two) as the address value of the gp symbol.

<1>

If an sdata and sbss section are located in this order, from smallest to highest address

<2>

If an sbss and sdata section are located in this order, from smallest to highest address

<3>

If sdata and sbss sections are placed in this order, from lowest to highest address, and there is a non-eligible section in between the sdata and sbss sections

(b)

If there is a section with a relocation attribute of sdata23 or sbss23

Sets the halfway point between the start address of the sdata23 or sbss23 section with the smallest address, and the end address of the sdata or sbss section with the greatest address (multiple of 2; if the midway point is an odd number, takes the first multiple of two) as the address value of the gp symbol.

<1>

If an sdata23 and sbss23 section are located in this order, from smallest to highest address

<2>

If an sbss23 and sdata23 section are located in this order, from smallest to highest address

<3>

If sdata23 and sbss23 sections are placed in this order, from lowest to highest address, and there is a non-eligible section in between the sdata23 and sbss23 sections

(c)

If there are no sections with sdata, sbss, sdata23, or sbss23 relocation attributes

If there are no sdata, sbss, sdata23, or sbss23 sections, then the address value of the gp symbol is set to zero (0).