--- /dev/null
+<?xml version="1.0" encoding="utf-8" ?>
+<!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" xml:lang="en" lang="en">
+<head>
+<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+<meta name="generator" content="Docutils 0.5: http://docutils.sourceforge.net/" />
+<title>Bold - The Byte Optimized Linker</title>
+<meta name="author" content="Amand Tihon" />
+<meta name="date" content="Aug 8, 2009" />
+<meta name="copyright" content="GNU GPL version 3 + Exception, see copyright file." />
+<style type="text/css">
+
+/*
+:Author: David Goodger (goodger@python.org)
+:Id: $Id: html4css1.css 5196 2007-06-03 20:25:28Z wiemann $
+:Copyright: This stylesheet has been placed in the public domain.
+
+Default cascading style sheet for the HTML output of Docutils.
+
+See http://docutils.sf.net/docs/howto/html-stylesheets.html for how to
+customize this style sheet.
+*/
+
+/* used to remove borders from tables and images */
+.borderless, table.borderless td, table.borderless th {
+ border: 0 }
+
+table.borderless td, table.borderless th {
+ /* Override padding for "table.docutils td" with "! important".
+ The right padding separates the table cells. */
+ padding: 0 0.5em 0 0 ! important }
+
+.first {
+ /* Override more specific margin styles with "! important". */
+ margin-top: 0 ! important }
+
+.last, .with-subtitle {
+ margin-bottom: 0 ! important }
+
+.hidden {
+ display: none }
+
+a.toc-backref {
+ text-decoration: none ;
+ color: black }
+
+blockquote.epigraph {
+ margin: 2em 5em ; }
+
+dl.docutils dd {
+ margin-bottom: 0.5em }
+
+/* Uncomment (and remove this text!) to get bold-faced definition list terms
+dl.docutils dt {
+ font-weight: bold }
+*/
+
+div.abstract {
+ margin: 2em 5em }
+
+div.abstract p.topic-title {
+ font-weight: bold ;
+ text-align: center }
+
+div.admonition, div.attention, div.caution, div.danger, div.error,
+div.hint, div.important, div.note, div.tip, div.warning {
+ margin: 2em ;
+ border: medium outset ;
+ padding: 1em }
+
+div.admonition p.admonition-title, div.hint p.admonition-title,
+div.important p.admonition-title, div.note p.admonition-title,
+div.tip p.admonition-title {
+ font-weight: bold ;
+ font-family: sans-serif }
+
+div.attention p.admonition-title, div.caution p.admonition-title,
+div.danger p.admonition-title, div.error p.admonition-title,
+div.warning p.admonition-title {
+ color: red ;
+ font-weight: bold ;
+ font-family: sans-serif }
+
+/* Uncomment (and remove this text!) to get reduced vertical space in
+ compound paragraphs.
+div.compound .compound-first, div.compound .compound-middle {
+ margin-bottom: 0.5em }
+
+div.compound .compound-last, div.compound .compound-middle {
+ margin-top: 0.5em }
+*/
+
+div.dedication {
+ margin: 2em 5em ;
+ text-align: center ;
+ font-style: italic }
+
+div.dedication p.topic-title {
+ font-weight: bold ;
+ font-style: normal }
+
+div.figure {
+ margin-left: 2em ;
+ margin-right: 2em }
+
+div.footer, div.header {
+ clear: both;
+ font-size: smaller }
+
+div.line-block {
+ display: block ;
+ margin-top: 1em ;
+ margin-bottom: 1em }
+
+div.line-block div.line-block {
+ margin-top: 0 ;
+ margin-bottom: 0 ;
+ margin-left: 1.5em }
+
+div.sidebar {
+ margin: 0 0 0.5em 1em ;
+ border: medium outset ;
+ padding: 1em ;
+ background-color: #ffffee ;
+ width: 40% ;
+ float: right ;
+ clear: right }
+
+div.sidebar p.rubric {
+ font-family: sans-serif ;
+ font-size: medium }
+
+div.system-messages {
+ margin: 5em }
+
+div.system-messages h1 {
+ color: red }
+
+div.system-message {
+ border: medium outset ;
+ padding: 1em }
+
+div.system-message p.system-message-title {
+ color: red ;
+ font-weight: bold }
+
+div.topic {
+ margin: 2em }
+
+h1.section-subtitle, h2.section-subtitle, h3.section-subtitle,
+h4.section-subtitle, h5.section-subtitle, h6.section-subtitle {
+ margin-top: 0.4em }
+
+h1.title {
+ text-align: center }
+
+h2.subtitle {
+ text-align: center }
+
+hr.docutils {
+ width: 75% }
+
+img.align-left {
+ clear: left }
+
+img.align-right {
+ clear: right }
+
+ol.simple, ul.simple {
+ margin-bottom: 1em }
+
+ol.arabic {
+ list-style: decimal }
+
+ol.loweralpha {
+ list-style: lower-alpha }
+
+ol.upperalpha {
+ list-style: upper-alpha }
+
+ol.lowerroman {
+ list-style: lower-roman }
+
+ol.upperroman {
+ list-style: upper-roman }
+
+p.attribution {
+ text-align: right ;
+ margin-left: 50% }
+
+p.caption {
+ font-style: italic }
+
+p.credits {
+ font-style: italic ;
+ font-size: smaller }
+
+p.label {
+ white-space: nowrap }
+
+p.rubric {
+ font-weight: bold ;
+ font-size: larger ;
+ color: maroon ;
+ text-align: center }
+
+p.sidebar-title {
+ font-family: sans-serif ;
+ font-weight: bold ;
+ font-size: larger }
+
+p.sidebar-subtitle {
+ font-family: sans-serif ;
+ font-weight: bold }
+
+p.topic-title {
+ font-weight: bold }
+
+pre.address {
+ margin-bottom: 0 ;
+ margin-top: 0 ;
+ font-family: serif ;
+ font-size: 100% }
+
+pre.literal-block, pre.doctest-block {
+ margin-left: 2em ;
+ margin-right: 2em }
+
+span.classifier {
+ font-family: sans-serif ;
+ font-style: oblique }
+
+span.classifier-delimiter {
+ font-family: sans-serif ;
+ font-weight: bold }
+
+span.interpreted {
+ font-family: sans-serif }
+
+span.option {
+ white-space: nowrap }
+
+span.pre {
+ white-space: pre }
+
+span.problematic {
+ color: red }
+
+span.section-subtitle {
+ /* font-size relative to parent (h1..h6 element) */
+ font-size: 80% }
+
+table.citation {
+ border-left: solid 1px gray;
+ margin-left: 1px }
+
+table.docinfo {
+ margin: 2em 4em }
+
+table.docutils {
+ margin-top: 0.5em ;
+ margin-bottom: 0.5em }
+
+table.footnote {
+ border-left: solid 1px black;
+ margin-left: 1px }
+
+table.docutils td, table.docutils th,
+table.docinfo td, table.docinfo th {
+ padding-left: 0.5em ;
+ padding-right: 0.5em ;
+ vertical-align: top }
+
+table.docutils th.field-name, table.docinfo th.docinfo-name {
+ font-weight: bold ;
+ text-align: left ;
+ white-space: nowrap ;
+ padding-left: 0 }
+
+h1 tt.docutils, h2 tt.docutils, h3 tt.docutils,
+h4 tt.docutils, h5 tt.docutils, h6 tt.docutils {
+ font-size: 100% }
+
+ul.auto-toc {
+ list-style-type: none }
+
+</style>
+</head>
+<body>
+<div class="document" id="bold-the-byte-optimized-linker">
+<h1 class="title">Bold - The Byte Optimized Linker</h1>
+<table class="docinfo" frame="void" rules="none">
+<col class="docinfo-name" />
+<col class="docinfo-content" />
+<tbody valign="top">
+<tr><th class="docinfo-name">Author:</th>
+<td>Amand Tihon</td></tr>
+<tr><th class="docinfo-name">Contact:</th>
+<td><<a class="reference external" href="mailto:amand.tihon@alrj.org">amand.tihon@alrj.org</a>></td></tr>
+<tr><th class="docinfo-name">Version:</th>
+<td>0.1.0</td></tr>
+<tr><th class="docinfo-name">Date:</th>
+<td>Aug 8, 2009</td></tr>
+<tr><th class="docinfo-name">Copyright:</th>
+<td>GNU GPL version 3 + Exception, see copyright file.</td></tr>
+</tbody>
+</table>
+<!-- HTML version generated with LC_ALL=C rst2html -t README > README.html -->
+<div class="contents topic" id="table-of-contents">
+<p class="topic-title first">Table of contents</p>
+<ul class="auto-toc simple">
+<li><a class="reference internal" href="#abstract" id="id1">1 Abstract</a></li>
+<li><a class="reference internal" href="#rationale" id="id2">2 Rationale</a></li>
+<li><a class="reference internal" href="#getting-bold" id="id3">3 Getting Bold</a></li>
+<li><a class="reference internal" href="#requirements" id="id4">4 Requirements</a></li>
+<li><a class="reference internal" href="#installation" id="id5">5 Installation</a></li>
+<li><a class="reference internal" href="#using-bold" id="id6">6 Using Bold</a><ul class="auto-toc">
+<li><a class="reference internal" href="#synopsys" id="id7">6.1 Synopsys</a></li>
+<li><a class="reference internal" href="#description" id="id8">6.2 Description</a></li>
+<li><a class="reference internal" href="#options" id="id9">6.3 Options</a></li>
+<li><a class="reference internal" href="#notes" id="id10">6.4 Notes</a></li>
+</ul>
+</li>
+<li><a class="reference internal" href="#internals" id="id11">7 Internals</a><ul class="auto-toc">
+<li><a class="reference internal" href="#external-symbols-resolution" id="id12">7.1 External symbols resolution</a></li>
+<li><a class="reference internal" href="#calling-from-c" id="id13">7.2 Calling from C</a></li>
+<li><a class="reference internal" href="#aligning" id="id14">7.3 Aligning</a></li>
+<li><a class="reference internal" href="#additional-trick-1-dt-debug" id="id15">7.4 Additional Trick 1: DT_DEBUG</a></li>
+<li><a class="reference internal" href="#additional-trick-2-short-dynamic-table" id="id16">7.5 Additional Trick 2: Short DYNAMIC table</a></li>
+</ul>
+</li>
+<li><a class="reference internal" href="#examples" id="id17">8 Examples</a></li>
+</ul>
+</div>
+<div class="section" id="abstract">
+<h1>1 Abstract</h1>
+<p>Bold is an ELF linker, currently only targetting x86_64 under Linux. Being
+limited in capabilities, it should not be considered as an all-purpose linker.</p>
+</div>
+<div class="section" id="rationale">
+<h1>2 Rationale</h1>
+<p>Bold's main purpose is to generate very small executable programs.</p>
+<p>While <tt class="docutils literal"><span class="pre">ld</span></tt> from the GNU binutils can do almost anything anyone would ever
+need, some specific goals need an awful lot of tweaking, or can simply not be
+achieved. Bold uses several tricks to reduce the size of the final executable
+binary.</p>
+</div>
+<div class="section" id="getting-bold">
+<h1>3 Getting Bold</h1>
+<p>You can download the tarball from <a class="reference external" href="http://www.alrj.org/projects/bold">http://www.alrj.org/projects/bold</a>
+or get the latest development version with the following git command:</p>
+<pre class="literal-block">
+git clone http://git.alrj.org/git/bold.git
+</pre>
+<p>A gitweb interface is also available at <a class="reference external" href="http://git.alrj.org/">http://git.alrj.org/</a></p>
+</div>
+<div class="section" id="requirements">
+<h1>4 Requirements</h1>
+<p>Bold itself is entirely written in Python. There are no additionnal
+dependencies.</p>
+<p>The runtime library that contains the external symbols resolver is written
+in assembler (Intel syntax). An assembler like Nasm or Yasm is needed to
+recompile the source code into an object file.</p>
+</div>
+<div class="section" id="installation">
+<h1>5 Installation</h1>
+<p>Go into Bold's directory, and run</p>
+<pre class="literal-block">
+python setup.py build
+</pre>
+<p>Then, as root or using sudo, run</p>
+<pre class="literal-block">
+python setup.py install
+</pre>
+</div>
+<div class="section" id="using-bold">
+<h1>6 Using Bold</h1>
+<div class="section" id="synopsys">
+<h2>6.1 Synopsys</h2>
+<blockquote>
+bold [options] objfile...</blockquote>
+</div>
+<div class="section" id="description">
+<h2>6.2 Description</h2>
+<p>Bold combines a number of object files, relocate their data and resolves their
+symbols references, in order to generate executable binaries.</p>
+<p>Bold has only one, very specific purpose: making small executables.</p>
+</div>
+<div class="section" id="options">
+<h2>6.3 Options</h2>
+<table class="docutils option-list" frame="void" rules="none">
+<col class="option" />
+<col class="description" />
+<tbody valign="top">
+<tr><td class="option-group">
+<kbd><span class="option">--version</span></kbd></td>
+<td>Show program's version and exit.</td></tr>
+<tr><td class="option-group">
+<kbd><span class="option">-h</span>, <span class="option">--help</span></kbd></td>
+<td>Show help message and exit.</td></tr>
+<tr><td class="option-group" colspan="2">
+<kbd><span class="option">-e <var>SYMBOL</var></span>, <span class="option">--entry=<var>SYMBOL</var></span></kbd></td>
+</tr>
+<tr><td> </td><td>Use SYMBOL as the explicit symbol for beginning execution of your program.
+If <tt class="docutils literal"><span class="pre">--raw</span></tt> is specified, it defaults to <tt class="docutils literal"><span class="pre">_start</span></tt>.</td></tr>
+<tr><td class="option-group" colspan="2">
+<kbd><span class="option">-l <var>LIBNAME</var></span>, <span class="option">--library=<var>LIBNAME</var></span></kbd></td>
+</tr>
+<tr><td> </td><td>Link against the shared library specified by LIBNAME. Bold relies on python's
+ctypes module to find the libraries. This option may be used any number of
+times.</td></tr>
+<tr><td class="option-group" colspan="2">
+<kbd><span class="option">-L <var>DIRECTORY</var></span>, <span class="option">--library-path=<var>DIRECTORY</var></span></kbd></td>
+</tr>
+<tr><td> </td><td>This option does nothing, and is present ony for compatibility reasons. It
+MAY get implemented in the future, though. This option may be used any number
+of times.</td></tr>
+<tr><td class="option-group" colspan="2">
+<kbd><span class="option">-o <var>FILE</var></span>, <span class="option">--output=<var>FILE</var></span></kbd></td>
+</tr>
+<tr><td> </td><td>Set the output file name (default value is a.out).</td></tr>
+<tr><td class="option-group">
+<kbd><span class="option">--raw</span></kbd></td>
+<td>Don't include the builtin external symbols resolution code. This is
+described in details further in this document.</td></tr>
+<tr><td class="option-group">
+<kbd><span class="option">-c</span>, <span class="option">--ccall</span></kbd></td>
+<td>Make external symbols directly callable by C, without having to declare the
+pointers on functions. This option adds 6 bytes for each externally defined
+function. This is described in details further in this document.</td></tr>
+<tr><td class="option-group">
+<kbd><span class="option">-a</span>, <span class="option">--align</span></kbd></td>
+<td>Align the wrappers for external symbols on an 8 byte boundary, to take
+advantage of the RIP-relative addressing. This is described in details
+further in this document.</td></tr>
+</tbody>
+</table>
+</div>
+<div class="section" id="notes">
+<h2>6.4 Notes</h2>
+<p>The <tt class="docutils literal"><span class="pre">LD_PRELOAD</span></tt> environment variable may not always work (as expected or
+at all).</p>
+<p>The <tt class="docutils literal"><span class="pre">main()</span></tt> function is called without any argument. Its return code is used
+as exit code, though.</p>
+</div>
+</div>
+<div class="section" id="internals">
+<h1>7 Internals</h1>
+<div class="section" id="external-symbols-resolution">
+<h2>7.1 External symbols resolution</h2>
+<p>The "import by hash" method is from parapete, leblane, las, as described on
+<a class="reference external" href="http://www.pouet.net/topic.php?which=5392">http://www.pouet.net/topic.php?which=5392</a></p>
+</div>
+<div class="section" id="calling-from-c">
+<h2>7.2 Calling from C</h2>
+<p>If you write your code in C and need to call the external symbols, you
+basically have two options. The first one is to redefine them (or define new
+ones) to call by pointers. For instance,</p>
+<pre class="literal-block">
+int SDL_Init(int);
+</pre>
+<p>would become:</p>
+<pre class="literal-block">
+int (*SDL_Init)(int);
+</pre>
+<p>Repeat it for all functions, or write a tool to automate it (hint: look at
+<a class="reference external" href="http://research.mercury-labs.org/ibh-i386-0.2.2.tar.gz">http://research.mercury-labs.org/ibh-i386-0.2.2.tar.gz</a> for help).</p>
+<p>There's a second possibility however, and it's the one used by Bold when you
+specify the <tt class="docutils literal"><span class="pre">--ccall</span></tt> option: make the resolved symbol point, not to the
+address of the function, but to a JMP instruction to the actual address:</p>
+<pre class="literal-block">
+global SDL_Init
+
+.text
+
+SDL_Init: jmp [rel _bold__SDL_Init]
+SDL_SetVideoMode: jmp [rel _bold__SDL_SetVideoMode]
+
+.bss
+
+_bold__SDL_Init resq ; Filled by the import by hash code
+_bold__SDL_SetVideoMode resq
+</pre>
+<p>This approach takes 6 bytes (the JMP instruction) for each external function
+used.</p>
+</div>
+<div class="section" id="aligning">
+<h2>7.3 Aligning</h2>
+<p>The x86_64 architecture has this nice thing called "RIP-relative addressing".
+If all the JMP instructions are in the same order than the pointers to the
+functions they refer to, having them aligned with the pointers would result
+in identical instructions. This is done with the <tt class="docutils literal"><span class="pre">--align</span></tt> option.</p>
+<p>Adding two null bytes between each JMP enlarges the final executable by
+2 x (number of function - 1) bytes, and may seem to go against our goal.
+However, the result is a repetition of the <em>same eight bytes</em>, something that
+can improve compression a lot!</p>
+</div>
+<div class="section" id="additional-trick-1-dt-debug">
+<h2>7.4 Additional Trick 1: DT_DEBUG</h2>
+<p>Bold declares a global symbol named <tt class="docutils literal"><span class="pre">_dt_debug</span></tt>, that points to the value of
+the <tt class="docutils literal"><span class="pre">DT_DEBUG</span></tt> entry of the <tt class="docutils literal"><span class="pre">DYNAMIC</span></tt> table, for easy access. Just in case,
+the <tt class="docutils literal"><span class="pre">DYNAMIC</span></tt> table can also be reached using the global <tt class="docutils literal"><span class="pre">_DYNAMIC</span></tt> symbol.</p>
+</div>
+<div class="section" id="additional-trick-2-short-dynamic-table">
+<h2>7.5 Additional Trick 2: Short DYNAMIC table</h2>
+<p>Executables generated by <tt class="docutils literal"><span class="pre">ld</span></tt> usually have a lot of entries in their
+<tt class="docutils literal"><span class="pre">DYNAMIC</span></tt> table. Bold puts only the strict necessary:</p>
+<ul class="simple">
+<li>One <tt class="docutils literal"><span class="pre">DT_NEEDED</span></tt> entry for each shared library to load (obviously).</li>
+<li>A <tt class="docutils literal"><span class="pre">DT_SYMTAB</span></tt> entry, with null-pointer. Without this one, the interpreter
+wouldn't do its job.</li>
+<li>a <tt class="docutils literal"><span class="pre">DT_DEBUG</span></tt> entry, that will be used for symbol resolution.</li>
+</ul>
+<p>And that's it!</p>
+</div>
+</div>
+<div class="section" id="examples">
+<h1>8 Examples</h1>
+<p>The <tt class="docutils literal"><span class="pre">examples/</span></tt> directory contains a port of the <em>flow2</em> intro
+(<a class="reference external" href="http://www.pouet.net/prod.php?which=30589">http://www.pouet.net/prod.php?which=30589</a>). Adding the dropper is left as an
+exercise for the reader.</p>
+</div>
+</div>
+<div class="footer">
+<hr class="footer" />
+Generated on: 2009-08-08 18:30 UTC.
+
+</div>
+</body>
+</html>