blob: 88a87c96ea64e70fe835b3ebbabeaa64bf7c9bb3 [file] [log] [blame]
<html lang="en">
<head>
<title>Output Section Keywords - Untitled</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="Untitled">
<meta name="generator" content="makeinfo 4.13">
<link title="Top" rel="start" href="index.html#Top">
<link rel="up" href="SECTIONS.html#SECTIONS" title="SECTIONS">
<link rel="prev" href="Output-Section-Data.html#Output-Section-Data" title="Output Section Data">
<link rel="next" href="Output-Section-Discarding.html#Output-Section-Discarding" title="Output Section Discarding">
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
<!--
This file documents the GNU linker LD
(GNU Toolchain for the A-profile Architecture 9.2-2019.12 (arm-9.10))
version 2.33.1.
Copyright (C) 1991-2019 Free Software Foundation, Inc.
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3
or any later version published by the Free Software Foundation;
with no Invariant Sections, with no Front-Cover Texts, and with no
Back-Cover Texts. A copy of the license is included in the
section entitled ``GNU Free Documentation License''.-->
<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">
<a name="Output-Section-Keywords"></a>
<p>
Next:&nbsp;<a rel="next" accesskey="n" href="Output-Section-Discarding.html#Output-Section-Discarding">Output Section Discarding</a>,
Previous:&nbsp;<a rel="previous" accesskey="p" href="Output-Section-Data.html#Output-Section-Data">Output Section Data</a>,
Up:&nbsp;<a rel="up" accesskey="u" href="SECTIONS.html#SECTIONS">SECTIONS</a>
<hr>
</div>
<h4 class="subsection">3.6.6 Output Section Keywords</h4>
<p>There are a couple of keywords which can appear as output section
commands.
<a name="index-CREATE_005fOBJECT_005fSYMBOLS-486"></a>
<a name="index-input-filename-symbols-487"></a>
<a name="index-filename-symbols-488"></a>
<dl><dt><code>CREATE_OBJECT_SYMBOLS</code><dd>The command tells the linker to create a symbol for each input file.
The name of each symbol will be the name of the corresponding input
file. The section of each symbol will be the output section in which
the <code>CREATE_OBJECT_SYMBOLS</code> command appears.
<p>This is conventional for the a.out object file format. It is not
normally used for any other object file format.
<p><a name="index-CONSTRUCTORS-489"></a><a name="index-C_002b_002b-constructors_002c-arranging-in-link-490"></a><a name="index-constructors_002c-arranging-in-link-491"></a><br><dt><code>CONSTRUCTORS</code><dd>When linking using the a.out object file format, the linker uses an
unusual set construct to support C++ global constructors and
destructors. When linking object file formats which do not support
arbitrary sections, such as ECOFF and XCOFF, the linker will
automatically recognize C++ global constructors and destructors by name.
For these object file formats, the <code>CONSTRUCTORS</code> command tells the
linker to place constructor information in the output section where the
<code>CONSTRUCTORS</code> command appears. The <code>CONSTRUCTORS</code> command is
ignored for other object file formats.
<p>The symbol <code>__CTOR_LIST__</code><!-- /@w --> marks the start of the global
constructors, and the symbol <code>__CTOR_END__</code><!-- /@w --> marks the end.
Similarly, <code>__DTOR_LIST__</code><!-- /@w --> and <code>__DTOR_END__</code><!-- /@w --> mark
the start and end of the global destructors. The
first word in the list is the number of entries, followed by the address
of each constructor or destructor, followed by a zero word. The
compiler must arrange to actually run the code. For these object file
formats <span class="sc">gnu</span> C++ normally calls constructors from a subroutine
<code>__main</code>; a call to <code>__main</code> is automatically inserted into
the startup code for <code>main</code>. <span class="sc">gnu</span> C++ normally runs
destructors either by using <code>atexit</code>, or directly from the function
<code>exit</code>.
<p>For object file formats such as <code>COFF</code> or <code>ELF</code> which support
arbitrary section names, <span class="sc">gnu</span> C++ will normally arrange to put the
addresses of global constructors and destructors into the <code>.ctors</code>
and <code>.dtors</code> sections. Placing the following sequence into your
linker script will build the sort of table which the <span class="sc">gnu</span> C++
runtime code expects to see.
<pre class="smallexample"> __CTOR_LIST__ = .;
LONG((__CTOR_END__ - __CTOR_LIST__) / 4 - 2)
*(.ctors)
LONG(0)
__CTOR_END__ = .;
__DTOR_LIST__ = .;
LONG((__DTOR_END__ - __DTOR_LIST__) / 4 - 2)
*(.dtors)
LONG(0)
__DTOR_END__ = .;
</pre>
<p>If you are using the <span class="sc">gnu</span> C++ support for initialization priority,
which provides some control over the order in which global constructors
are run, you must sort the constructors at link time to ensure that they
are executed in the correct order. When using the <code>CONSTRUCTORS</code>
command, use &lsquo;<samp><span class="samp">SORT_BY_NAME(CONSTRUCTORS)</span></samp>&rsquo; instead. When using the
<code>.ctors</code> and <code>.dtors</code> sections, use &lsquo;<samp><span class="samp">*(SORT_BY_NAME(.ctors))</span></samp>&rsquo; and
&lsquo;<samp><span class="samp">*(SORT_BY_NAME(.dtors))</span></samp>&rsquo; instead of just &lsquo;<samp><span class="samp">*(.ctors)</span></samp>&rsquo; and
&lsquo;<samp><span class="samp">*(.dtors)</span></samp>&rsquo;.
<p>Normally the compiler and linker will handle these issues automatically,
and you will not need to concern yourself with them. However, you may
need to consider this if you are using C++ and writing your own linker
scripts.
</dl>
</body></html>