emacs.d/clones/www.sbcl.org/sbcl-internals/Linkage_002dtable.html
2023-01-18 20:30:47 +01:00

128 lines
5.8 KiB
HTML

<html lang="en">
<head>
<title>Linkage-table - SBCL Internals</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="SBCL Internals">
<meta name="generator" content="makeinfo 4.11">
<link title="Top" rel="start" href="index.html#Top">
<link rel="up" href="Foreign-Linkage.html#Foreign-Linkage" title="Foreign Linkage">
<link rel="next" href="Lazy-Alien-Resolution.html#Lazy-Alien-Resolution" title="Lazy Alien Resolution">
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
<!--
This manual is part of the SBCL software system. See the `README'
file for more information.
This manual is in the public domain and is provided with
absolutely no warranty. See the `COPYING' and `CREDITS' files for
more information.
-->
<meta http-equiv="Content-Style-Type" content="text/css">
<style type="text/css"><!--
pre.display { font-family:inherit }
pre.format { font-family:inherit }
pre.smalldisplay { font-family:inherit; font-size:smaller }
pre.smallformat { font-family:inherit; font-size:smaller }
pre.smallexample { font-size:smaller }
pre.smalllisp { font-size:smaller }
span.sc { font-variant:small-caps }
span.roman { font-family:serif; font-weight:normal; }
span.sansserif { font-family:sans-serif; font-weight:normal; }
--></style>
</head>
<body>
<div class="node">
<p>
<a name="Linkage-table"></a>
<a name="Linkage_002dtable"></a>
Next:&nbsp;<a rel="next" accesskey="n" href="Lazy-Alien-Resolution.html#Lazy-Alien-Resolution">Lazy Alien Resolution</a>,
Up:&nbsp;<a rel="up" accesskey="u" href="Foreign-Linkage.html#Foreign-Linkage">Foreign Linkage</a>
<hr>
</div>
<!-- node-name, next, previous, up -->
<h3 class="section">4.1 Linkage-table</h3>
<p>Linkage-table allows saving cores with foreign code loaded, and is
also utilized to allow references to as-of-yet unknown aliens.
See <a href="Lazy-Alien-Resolution.html#Lazy-Alien-Resolution">Lazy Alien Resolution</a>.
<p>The SBCL implementation is somewhat simplified from the CMUCL one by
Timothy Moore, but the basic idea and mechanism remain identical:
instead of having addresses from <code>dlsym(3)</code> in the core, we have
addresses to an mmapped memory area (<code>LINKAGE_TABLE_SPACE</code>) that
is initialized at startup to contain jumps &amp; references to the correct
addresses, based on information stored on the lisp side in
<code>*LINKAGE-INFO*</code>.
<h4 class="subsection">4.1.1 Differences to CMUCL</h4>
<p>CMUCL does lazy linkage for code, keeps all foreign addresses in the
linkage-table, and handles the initialization from C. We do eager
linkage for everything, maintain a separate
<code>*STATIC-FOREIGN-SYMBOLS*</code> just like on non-linkage-table ports
(this allows more code sharing between ports, makes thread-safety
easier to achieve, and cuts one jump's worth of overhead from stuff
like closure_tramp), and do the initialization from lisp.
<h4 class="subsection">4.1.2 Nitty Gritty Details</h4>
<p>Symbols in <code>*STATIC-FOREIGN-SYMBOLS*</code> are handled the old
fashioned way: linkage-table is only used for symbols resolved with
<code>dlsym(3)</code>.
<p>On system startup <code>FOREIGN-REINIT</code> iterates through the
<code>*LINKAGE-INFO*</code>, which is a hash-table mapping dynamic foreign
names to <code>LINKAGE-INFO</code> structures, and calls
<code>arch_write_linkage_table_jmp</code><code>/ref</code> to write the
appropriate entries to the linkage-table.
<p>When a foreign symbol is referred to, it is first looked for in the
<code>*STATIC-FOREIGN-SYMBOLS*</code>. If not found,
<code>ENSURE-FOREIGN-LINKAGE</code> is called, which looks for the
corresponding entry in <code>*LINKAGE-INFO*</code>, creating one and writing
the appropriate entry in the linkage table if necessary.
<p><code>FOREIGN-SYMBOL-ADDRESS</code> and <code>FOREIGN-SYMBOL-SAP</code> take an
optional datap argument, used to indicate that the symbol refers to a
variable. In similar fashion there is a new kind of fixup and a new
VOP: <code>:FOREIGN-DATAREF</code> and <code>FOREIGN-SYMBOL-DATAREF-SAP</code>.
<p>The <code>DATAP</code> argument is automagically provided by the alien
interface for normal definitions, but is really needed only for
dynamic foreign variables. For those it indicates the need for the
indirection either within a conditional branch in
<code>FOREIGN-SYMBOL-SAP</code>, or via <code>:FOREIGN-DATAREF</code> fixup and
<code>FOREIGN-SYMBOL-DATAREF-SAP</code> VOP: "this address holds the
address of the foreign variable, not the variable itself". Within SBCL
itself (in the fixups manifest in various VOPs) this fixup type is
never used, as all foreign symbols used internally are static.
<p>One thing worth noting is that <code>FOREIGN-SYMBOL-SAP</code> and friends
now have the potential side-effect of entering information in
<code>*LINKAGE-INFO*</code> and the linkage-table proper. If the usage case
is about checking if the symbol is available use
<code>FIND-FOREIGN-SYMBOL-ADDRESS</code>, which is side-effect free. (This
is used by SB-POSIX.)
<h4 class="subsection">4.1.3 Porting</h4>
<h5 class="subsubsection">4.1.3.1 Porting to new operating systems</h5>
<p>Find a memory area for the linkage-table, and add it for the OS in
<samp><span class="file">src/compiler/target/parms.lisp</span></samp> by defining
<code>SB!VM:LINKAGE-TABLE-SPACE-START</code> and
<code>SB!VM:LINKAGE-TABLE-SPACE-END</code>. See existing ports and CMUCL for
examples.
<h5 class="subsubsection">4.1.3.2 Porting to new architectures</h5>
<p>Write <code>arch_write_linkage_table_jmp</code> and <code>arch_write_linkage_table_ref</code>.
<p>Write <code>FOREIGN-SYMBOL-DATAREF</code> VOP.
<p>Define correct <code>SB!VM:LINKAGE-TABLE-ENTRY-SIZE</code> in
<samp><span class="file">src/compiler/target/parms.lisp</span></samp>.
</body></html>