⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 linuxemu-advanced.html

📁 这是很好的学习嵌入式LINUX的文章
💻 HTML
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta name="generator" content="HTML Tidy, see www.w3.org" /><title>Advanced Topics</title><meta name="GENERATOR" content="Modular DocBook HTML Stylesheet Version 1.7" /><link rel="HOME" title="FreeBSD Handbook" href="index.html" /><link rel="UP" title="Linux Binary Compatibility" href="linuxemu.html" /><link rel="PREVIOUS" title="Installing SAP燫/3" href="sapr3.html" /><link rel="NEXT" title="System Administration" href="system-administration.html" /><link rel="STYLESHEET" type="text/css" href="docbook.css" /></head><body class="SECT1" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#840084"alink="#0000FF"><div class="NAVHEADER"><table summary="Header navigation table" width="100%" border="0" cellpadding="0"cellspacing="0"><tr><th colspan="3" align="center">FreeBSD Handbook</th></tr><tr><td width="10%" align="left" valign="bottom"><a href="sapr3.html"accesskey="P">Prev</a></td><td width="80%" align="center" valign="bottom">Chapter 10 Linux Binary Compatibility</td><td width="10%" align="right" valign="bottom"><a href="system-administration.html"accesskey="N">Next</a></td></tr></table><hr align="LEFT" width="100%" /></div><div class="SECT1"><h1 class="SECT1"><a id="LINUXEMU-ADVANCED" name="LINUXEMU-ADVANCED">10.8 AdvancedTopics</a></h1><p>If you are curious as to how the Linux binary compatibility works, this is the sectionyou want to read. Most of what follows is based heavily on an email written to <ahref="http://lists.FreeBSD.org/mailman/listinfo/freebsd-chat" target="_top">FreeBSD chatmailing list</a> by Terry Lambert <code class="EMAIL">&#60;<ahref="mailto:tlambert@primenet.com">tlambert@primenet.com</a>&#62;</code> (Message ID:<var class="LITERAL">&lt;199906020108.SAA07001@usr09.primenet.com&gt;</var>).</p><div class="SECT2"><h2 class="SECT2"><a id="AEN15490" name="AEN15490">10.8.1 How Does It Work?</a></h2><p>FreeBSD has an abstraction called an ``execution class loader''. This is a wedge intothe <a href="http://www.FreeBSD.org/cgi/man.cgi?query=execve&sektion=2"><spanclass="CITEREFENTRY"><span class="REFENTRYTITLE">execve</span>(2)</span></a> systemcall.</p><p>What happens is that FreeBSD has a list of loaders, instead of a single loader with afallback to the <var class="LITERAL">#!</var> loader for running any shell interpretersor shell scripts.</p><p>Historically, the only loader on the <span class="TRADEMARK">UNIX</span>&reg; platformexamined the magic number (generally the first 4 or 8 bytes of the file) to see if it wasa binary known to the system, and if so, invoked the binary loader.</p><p>If it was not the binary type for the system, the <ahref="http://www.FreeBSD.org/cgi/man.cgi?query=execve&sektion=2"><spanclass="CITEREFENTRY"><span class="REFENTRYTITLE">execve</span>(2)</span></a> callreturned a failure, and the shell attempted to start executing it as shell commands.</p><p>The assumption was a default of ``whatever the current shell is''.</p><p>Later, a hack was made for <ahref="http://www.FreeBSD.org/cgi/man.cgi?query=sh&sektion=1"><spanclass="CITEREFENTRY"><span class="REFENTRYTITLE">sh</span>(1)</span></a> to examine thefirst two characters, and if they were <var class="LITERAL">:\n</var>, then it invokedthe <a href="http://www.FreeBSD.org/cgi/man.cgi?query=csh&sektion=1"><spanclass="CITEREFENTRY"><span class="REFENTRYTITLE">csh</span>(1)</span></a> shell instead(we believe SCO first made this hack).</p><p>What FreeBSD does now is go through a list of loaders, with a generic <varclass="LITERAL">#!</var> loader that knows about interpreters as the characters whichfollow to the next whitespace next to last, followed by a fallback to <ttclass="FILENAME">/bin/sh</tt>.</p><p>For the Linux ABI support, FreeBSD sees the magic number as an ELF binary (it makes nodistinction between FreeBSD, <span class="TRADEMARK">Solaris</span>&#8482;, Linux, or anyother OS which has an ELF image type, at this point).</p><p>The ELF loader looks for a specialized <span class="emphasis"><iclass="EMPHASIS">brand</i></span>, which is a comment section in the ELF image, and whichis not present on SVR4/<span class="TRADEMARK">Solaris</span> ELF binaries.</p><p>For Linux binaries to function, they must be <span class="emphasis"><iclass="EMPHASIS">branded</i></span> as type <var class="LITERAL">Linux</var> from <ahref="http://www.FreeBSD.org/cgi/man.cgi?query=brandelf&sektion=1"><spanclass="CITEREFENTRY"><span class="REFENTRYTITLE">brandelf</span>(1)</span></a>:</p><pre class="SCREEN"><samp class="PROMPT">#</samp> <kbd class="USERINPUT">brandelf -t Linux file</kbd></pre><p>When this is done, the ELF loader will see the <var class="LITERAL">Linux</var> brandon the file.</p><p>When the ELF loader sees the <var class="LITERAL">Linux</var> brand, the loaderreplaces a pointer in the <var class="LITERAL">proc</var> structure. All system calls areindexed through this pointer (in a traditional <span class="TRADEMARK">UNIX</span>system, this would be the <var class="LITERAL">sysent[]</var> structure array, containingthe system calls). In addition, the process is flagged for special handling of the trapvector for the signal trampoline code, and several other (minor) fix-ups that are handledby the Linux kernel module.</p><p>The Linux system call vector contains, among other things, a list of <varclass="LITERAL">sysent[]</var> entries whose addresses reside in the kernel module.</p><p>When a system call is called by the Linux binary, the trap code dereferences thesystem call function pointer off the <var class="LITERAL">proc</var> structure, and getsthe Linux, not the FreeBSD, system call entry points.</p><p>In addition, the Linux mode dynamically <span class="emphasis"><iclass="EMPHASIS">reroots</i></span> lookups; this is, in effect, what the <varclass="OPTION">union</var> option to file system mounts (<span class="emphasis"><iclass="EMPHASIS">not</i></span> the <var class="LITERAL">unionfs</var> file system type!)does. First, an attempt is made to lookup the file in the <ttclass="FILENAME">/compat/linux/<var class="REPLACEABLE">original-path</var></tt>directory, <span class="emphasis"><i class="EMPHASIS">then</i></span> only if that fails,the lookup is done in the <tt class="FILENAME">/<varclass="REPLACEABLE">original-path</var></tt> directory. This makes sure that binariesthat require other binaries can run (e.g., the Linux toolchain can all run under LinuxABI support). It also means that the Linux binaries can load and execute FreeBSDbinaries, if there are no corresponding Linux binaries present, and that you could placea <a href="http://www.FreeBSD.org/cgi/man.cgi?query=uname&sektion=1"><spanclass="CITEREFENTRY"><span class="REFENTRYTITLE">uname</span>(1)</span></a> command inthe <tt class="FILENAME">/compat/linux</tt> directory tree to ensure that the Linuxbinaries could not tell they were not running on Linux.</p><p>In effect, there is a Linux kernel in the FreeBSD kernel; the various underlyingfunctions that implement all of the services provided by the kernel are identical to boththe FreeBSD system call table entries, and the Linux system call table entries: filesystem operations, virtual memory operations, signal delivery, System V IPC, etc... Theonly difference is that FreeBSD binaries get the FreeBSD <span class="emphasis"><iclass="EMPHASIS">glue</i></span> functions, and Linux binaries get the Linux <spanclass="emphasis"><i class="EMPHASIS">glue</i></span> functions (most older OS's only hadtheir own <span class="emphasis"><i class="EMPHASIS">glue</i></span> functions: addressesof functions in a static global <var class="LITERAL">sysent[]</var> structure array,instead of addresses of functions dereferenced off a dynamically initialized pointer inthe <var class="LITERAL">proc</var> structure of the process making the call).</p><p>Which one is the native FreeBSD ABI? It does not matter. Basically the only differenceis that (currently; this could easily be changed in a future release, and probably willbe after this) the FreeBSD <span class="emphasis"><i class="EMPHASIS">glue</i></span>functions are statically linked into the kernel, and the Linux <span class="emphasis"><iclass="EMPHASIS">glue</i></span> functions can be statically linked, or they can beaccessed via a kernel module.</p><p>Yeah, but is this really emulation? No. It is an ABI implementation, not an emulation.There is no emulator (or simulator, to cut off the next question) involved.</p><p>So why is it sometimes called ``Linux emulation''? To make it hard to sell FreeBSD!Really, it is because the historical implementation was done at a time when there wasreally no word other than that to describe what was going on; saying that FreeBSD ranLinux binaries was not true, if you did not compile the code in or load a module, andthere needed to be a word to describe what was being loaded--hence ``the Linuxemulator''.</p></div></div><div class="NAVFOOTER"><hr align="LEFT" width="100%" /><table summary="Footer navigation table" width="100%" border="0" cellpadding="0"cellspacing="0"><tr><td width="33%" align="left" valign="top"><a href="sapr3.html"accesskey="P">Prev</a></td><td width="34%" align="center" valign="top"><a href="index.html"accesskey="H">Home</a></td><td width="33%" align="right" valign="top"><a href="system-administration.html"accesskey="N">Next</a></td></tr><tr><td width="33%" align="left" valign="top">Installing <spanclass="TRADEMARK">SAP</span>&reg;&nbsp;<span class="TRADEMARK">R/3</span>&reg;</td><td width="34%" align="center" valign="top"><a href="linuxemu.html"accesskey="U">Up</a></td><td width="33%" align="right" valign="top">System Administration</td></tr></table></div><p align="center"><small>This, and other documents, can be downloaded from <ahref="ftp://ftp.FreeBSD.org/pub/FreeBSD/doc/">ftp://ftp.FreeBSD.org/pub/FreeBSD/doc/</a>.</small></p><p align="center"><small>For questions about FreeBSD, read the <ahref="http://www.FreeBSD.org/docs.html">documentation</a> before contacting &#60;<ahref="mailto:questions@FreeBSD.org">questions@FreeBSD.org</a>&#62;.<br />For questions about this documentation, e-mail &#60;<ahref="mailto:doc@FreeBSD.org">doc@FreeBSD.org</a>&#62;.</small></p></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -