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.
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. |
The common.fsy file is output with the following manipulation.
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.
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 |
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. |
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. |
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.
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.
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. |