[Main]
Title=__ld_insert_kernel_libs
See Also=insert_fargo020_libs: __ld_insert_fargo020_libs, insert_fargo021_libs: __ld_insert_fargo021_libs

[Top]
__ld_insert_kernel_libs can be used to handle
<A HREF="$$LINK(symbols_lib_call)">library calls</A>. It inserts references
to libraries in the format used by kernels:
<UL>
<LI>For each referenced library...
    <UL>
    <LI><B>8 bytes:</B> library variable name</LI>
    <LI><B>1 byte:</B> 0</LI>
    <LI><B>1 byte:</B> minimum version number (see
        <A HREF="$$LINK(control_lib_min_version)">Minimum Library
        Versions</A>)</LI>
    </UL></LI>
<LI>For each referenced library (again)...
    <UL>
    <LI><B>2 bytes:</B> number of imported functions/variables minus 1</LI>
    <LI>For each imported function/variable...
        <UL>
        <LI><B>2 bytes:</B> <I>index</I> of the function/variable</LI>
        <LI>Relocation table for this function/variable:
            <UL>
            <LI>For each reference...
                <UL>
                <LI><B>2 bytes:</B> <I>location</I> (may not be 0)</LI>
                </UL></LI>
            <LI><B>2 bytes:</B> 0</LI>
            </UL></LI>
        </UL></LI>
    </UL></LI>
</UL>
If a program uses this insertion, it must process it as follows:
<UL>
<LI>For each library...
    <UL>
    <LI>Do whatever is necessary to check the version and load it into the
        program's address space;</LI>
    <LI>For each relocation entry...
        <UL>
        <LI>Get the starting address of the program (via
            <A HREF="$$LINK(symbols_ld_entry_point)">__ld_entry_point</A>);</LI>
        <LI>Add the <I>location</I> value;</LI>
        <LI>Modify the 4-byte value (<I>offset</I>) at the resulting address:
            <UL>
            <LI>Get the address of the library function/variable indexed by
                <I>index</I>;</LI>
            <LI>Add the value of <I>offset</I>;</LI>
            <LI>Write the resulting address to the 4-byte space covered by
                <I>offset</I>.</LI>
            </UL></LI>
        </UL></LI>
    </UL></LI>
</UL>
Before program termination, this process has to be reverted, so that it can
be repeated the next time the program starts. Since programs and libraries
may be moved in memory while they are not executed, they may <I>not</I>
simply deactivate the relocation code. This would also prevent programs from
being transferred between devices.
