8.6.4 Referencing from a position-independent program to a position-dependent program

Position-dependent programs, such as existing resources, can be referenced from position-independent programs by following a certain procedure. The position-dependent programs in this case are called the common part.

(1)

The common part must already be created before creating position-independent programs as a precondition.

When creating the executable form of the common part, the addresses of the functions or variables to be referenced from position-independent programs are output to an .fsy file, using the -fsymbol option of the optimizing linker.

-

To reference library functions not from within the common part but from position-independent programs as the common part, write a pseudo reference code such as that shown below to link the library functions to the common part.

#include <string.h>
void* const dummy_libcall[] = {&memcpy, &memcmp, &strcpy};

 

The common.fsy file is output with the following manipulation.

>ccrh dummy_libcall.c -ocommon.abs -Xlk_option=-fsymbol=.text

 

To reference from position-independent programs, the executable form of the common part that has already been created, confirm the addresses of the functions or variables to be referenced in the link map file, etc. of the common part and write them to an .fsy file.

Example

When addresses are displayed in the link map file as shown below:

FILE=memcmp
                                  00002000  00002023        24
  _memcmp
                                  00002000         0   none ,g         *
FILE=memcpy
                                  00002024  0000203b        18
  _memcpy
                                  00002024         0   none ,g         *
FILE=strcpy
                                  0000203c  0000204f        14
  _strcpy
                                  0000203c         0   none ,g         *

 

Code to be written to the common.fsy file:

        .public _memcmp
_memcmp .equ 0x2000
        .public _memcpy
_memcpy .equ 0x2024
        .public _strcpy
_strcpy .equ 0x203c

 

(2)

When creating a position-independent program, write a declaration of the functions or variables in the common part to be referenced and a proccessing to reference them. In this case, the section in which the declaration belongs should comply with the section containing the definition of functions or variables on the common part side. The PIC functions on the side to perform reference should be defined in the section for PIC.

Note

Even when the -pid, -pirod, or -pid option is specified, a section relocation attribute that is position-dependent (e.g. text, const, and r0_disp16) can be specified in a #pragma section directive. Only the declaration of functions or variables can be written in this case. When the definition of functions or variables is written, an error will occur.

 

#pragma section text
extern  void *memcpy(void *, const void *, unsigned long);
extern  int memcmp(const void *, const void *, size_t);
extern  char *strcpy(char *, const char *);
 
#pragma section pctext  /* When defining a PIC function, the section relocation attribute must be returned to that for PIC. */
 
void pic_func(char* a, char* b, unsigned long c) {
  memcpy(a, b, c);
}

 

Write code to reference functions or variables in the common part and build a position-independent program. By building this program together with the .fsy file that was generated in the common part, reference to functions or variables on the common part side can be achieved by absolute addressing.

 

>ccrh pic.c common.fsy

 

(3)

There is a restriction on the method for referencing the functions or variables in the common part which can be referenced from position-independent programs.

The table below shows whether reference is possible and the reference method for a case between position-independent programs or a case between a position-independent program and the common part.

 

Reference destination

PIC function

Non-PIC function

PIROD variable

Non-PIROD variable

PID variableNote 2

Non-PID variable

Reference source

PIC function

PC-relative

R0-relative

PC-relative

R0-relative

GP- or EP-relative

GP- or EP-relative

R0-relative

Non-PIC function

Not possibleNote 1

PC-relative

R0-relative

Not possibleNote 1

R0-relative

GP- or EP-relative

GP- or EP-relative

R0-relative

Note 1.

When a non-PIC function is linked, no kind of direct reference is possible because the linker cannot identify the addresses of PIC functions or PIROD functions at execution. A possible method of reference is to receive the pointer at execution and perform reference via the pointer.

Note 2.

PID variables indicate the variables that were compiled with the -pid option specified instead of the variables in general which are allocated to the GP-relative or EP-relative sections.