📄 binary-formats.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>Binary Formats</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="UNIX Basics" href="basics.html" /><link rel="PREVIOUS" title="Devices and Device Nodes" href="basics-devices.html" /><link rel="NEXT" title="For More Information" href="basics-more-information.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="basics-devices.html"accesskey="P">Prev</a></td><td width="80%" align="center" valign="bottom">Chapter 3 UNIX Basics</td><td width="10%" align="right" valign="bottom"><a href="basics-more-information.html"accesskey="N">Next</a></td></tr></table><hr align="LEFT" width="100%" /></div><div class="SECT1"><h1 class="SECT1"><a id="BINARY-FORMATS" name="BINARY-FORMATS">3.12 BinaryFormats</a></h1><p>To understand why FreeBSD uses the <ahref="http://www.FreeBSD.org/cgi/man.cgi?query=elf&sektion=5"><spanclass="CITEREFENTRY"><span class="REFENTRYTITLE">elf</span>(5)</span></a> format, youmust first know a little about the three currently ``dominant'' executable formats for<span class="TRADEMARK">UNIX</span>®:</p><ul><li><p><a href="http://www.FreeBSD.org/cgi/man.cgi?query=a.out&sektion=5"><spanclass="CITEREFENTRY"><span class="REFENTRYTITLE">a.out</span>(5)</span></a></p><p>The oldest and ``classic'' <span class="TRADEMARK">UNIX</span> object format. It usesa short and compact header with a magic number at the beginning that is often used tocharacterize the format (see <ahref="http://www.FreeBSD.org/cgi/man.cgi?query=a.out&sektion=5"><spanclass="CITEREFENTRY"><span class="REFENTRYTITLE">a.out</span>(5)</span></a> for moredetails). It contains three loaded segments: .text, .data, and .bss plus a symbol tableand a string table.</p></li><li><p><acronym class="ACRONYM">COFF</acronym></p><p>The SVR3 object format. The header now comprises a section table, so you can have morethan just .text, .data, and .bss sections.</p></li><li><p><a href="http://www.FreeBSD.org/cgi/man.cgi?query=elf&sektion=5"><spanclass="CITEREFENTRY"><span class="REFENTRYTITLE">elf</span>(5)</span></a></p><p>The successor to <acronym class="ACRONYM">COFF</acronym>, featuring multiple sectionsand 32-bit or 64-bit possible values. One major drawback: <acronymclass="ACRONYM">ELF</acronym> was also designed with the assumption that there would beonly one ABI per system architecture. That assumption is actually quite incorrect, andnot even in the commercial SYSV world (which has at least three ABIs: SVR4, Solaris, SCO)does it hold true.</p><p>FreeBSD tries to work around this problem somewhat by providing a utility for <spanclass="emphasis"><i class="EMPHASIS">branding</i></span> a known <acronymclass="ACRONYM">ELF</acronym> executable with information about the ABI it is compliantwith. See the manual page for <ahref="http://www.FreeBSD.org/cgi/man.cgi?query=brandelf&sektion=1"><spanclass="CITEREFENTRY"><span class="REFENTRYTITLE">brandelf</span>(1)</span></a> for moreinformation.</p></li></ul><p>FreeBSD comes from the ``classic'' camp and used the <ahref="http://www.FreeBSD.org/cgi/man.cgi?query=a.out&sektion=5"><spanclass="CITEREFENTRY"><span class="REFENTRYTITLE">a.out</span>(5)</span></a> format, atechnology tried and proven through many generations of BSD releases, until the beginningof the 3.X branch. Though it was possible to build and run native <acronymclass="ACRONYM">ELF</acronym> binaries (and kernels) on a FreeBSD system for some timebefore that, FreeBSD initially resisted the ``push'' to switch to <acronymclass="ACRONYM">ELF</acronym> as the default format. Why? Well, when the Linux camp madetheir painful transition to <acronym class="ACRONYM">ELF</acronym>, it was not so much toflee the <tt class="FILENAME">a.out</tt> executable format as it was their inflexiblejump-table based shared library mechanism, which made the construction of sharedlibraries very difficult for vendors and developers alike. Since the <acronymclass="ACRONYM">ELF</acronym> tools available offered a solution to the shared libraryproblem and were generally seen as ``the way forward'' anyway, the migration cost wasaccepted as necessary and the transition made. FreeBSD's shared library mechanism isbased more closely on Sun's <span class="TRADEMARK">SunOS</span>™ style sharedlibrary mechanism and, as such, is very easy to use.</p><p>So, why are there so many different formats?</p><p>Back in the dim, dark past, there was simple hardware. This simple hardware supporteda simple, small system. <tt class="FILENAME">a.out</tt> was completely adequate for thejob of representing binaries on this simple system (a PDP-11). As people ported <spanclass="TRADEMARK">UNIX</span> from this simple system, they retained the <ttclass="FILENAME">a.out</tt> format because it was sufficient for the early ports of <spanclass="TRADEMARK">UNIX</span> to architectures like the Motorola 68k, VAXen, etc.</p><p>Then some bright hardware engineer decided that if he could force software to do somesleazy tricks, then he would be able to shave a few gates off the design and allow hisCPU core to run faster. While it was made to work with this new kind of hardware (knownthese days as <acronym class="ACRONYM">RISC</acronym>), <tt class="FILENAME">a.out</tt>was ill-suited for this hardware, so many formats were developed to get to a betterperformance from this hardware than the limited, simple <tt class="FILENAME">a.out</tt>format could offer. Things like <acronym class="ACRONYM">COFF</acronym>, <acronymclass="ACRONYM">ECOFF</acronym>, and a few obscure others were invented and theirlimitations explored before things seemed to settle on <acronymclass="ACRONYM">ELF</acronym>.</p><p>In addition, program sizes were getting huge and disks (and physical memory) werestill relatively small so the concept of a shared library was born. The VM system alsobecame more sophisticated. While each one of these advancements was done using the <ttclass="FILENAME">a.out</tt> format, its usefulness was stretched more and more with eachnew feature. In addition, people wanted to dynamically load things at run time, or tojunk parts of their program after the init code had run to save in core memory and swapspace. Languages became more sophisticated and people wanted code called before mainautomatically. Lots of hacks were done to the <tt class="FILENAME">a.out</tt> format toallow all of these things to happen, and they basically worked for a time. In time, <ttclass="FILENAME">a.out</tt> was not up to handling all these problems without an everincreasing overhead in code and complexity. While <acronym class="ACRONYM">ELF</acronym>solved many of these problems, it would be painful to switch from the system thatbasically worked. So <acronym class="ACRONYM">ELF</acronym> had to wait until it was morepainful to remain with <tt class="FILENAME">a.out</tt> than it was to migrate to <acronymclass="ACRONYM">ELF</acronym>.</p><p>However, as time passed, the build tools that FreeBSD derived their build tools from(the assembler and loader especially) evolved in two parallel trees. The FreeBSD treeadded shared libraries and fixed some bugs. The GNU folks that originally wrote theseprograms rewrote them and added simpler support for building cross compilers, plugging indifferent formats at will, and so on. Since many people wanted to build cross compilerstargeting FreeBSD, they were out of luck since the older sources that FreeBSD had for <bclass="APPLICATION">as</b> and <b class="APPLICATION">ld</b> were not up to the task. Thenew GNU tools chain (<b class="APPLICATION">binutils</b>) does support cross compiling,<acronym class="ACRONYM">ELF</acronym>, shared libraries, C++ extensions, etc. Inaddition, many vendors are releasing <acronym class="ACRONYM">ELF</acronym> binaries, andit is a good thing for FreeBSD to run them.</p><p><acronym class="ACRONYM">ELF</acronym> is more expressive than <ttclass="FILENAME">a.out</tt> and allows more extensibility in the base system. The<acronym class="ACRONYM">ELF</acronym> tools are better maintained, and offer crosscompilation support, which is important to many people. <acronymclass="ACRONYM">ELF</acronym> may be a little slower than <ttclass="FILENAME">a.out</tt>, but trying to measure it can be difficult. There are alsonumerous details that are different between the two in how they map pages, handle initcode, etc. None of these are very important, but they are differences. In time supportfor <tt class="FILENAME">a.out</tt> will be moved out of the <ttclass="FILENAME">GENERIC</tt> kernel, and eventually removed from the kernel once theneed to run legacy <tt class="FILENAME">a.out</tt> programs is past.</p></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="basics-devices.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="basics-more-information.html"accesskey="N">Next</a></td></tr><tr><td width="33%" align="left" valign="top">Devices and Device Nodes</td><td width="34%" align="center" valign="top"><a href="basics.html"accesskey="U">Up</a></td><td width="33%" align="right" valign="top">For More Information</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 <<ahref="mailto:questions@FreeBSD.org">questions@FreeBSD.org</a>>.<br />For questions about this documentation, e-mail <<ahref="mailto:doc@FreeBSD.org">doc@FreeBSD.org</a>>.</small></p></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -