]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - ld/ld.texi
gdb: fix post-hook execution for remote targets
[thirdparty/binutils-gdb.git] / ld / ld.texi
CommitLineData
252b5132
RH
1\input texinfo
2@setfilename ld.info
d87bef3a 3@c Copyright (C) 1991-2023 Free Software Foundation, Inc.
252b5132 4@syncodeindex ky cp
dff70155 5@c man begin INCLUDE
252b5132
RH
6@include configdoc.texi
7@c (configdoc.texi is generated by the Makefile)
c428fa83 8@include bfdver.texi
dff70155 9@c man end
252b5132
RH
10
11@c @smallbook
12
ff5dcc92
SC
13@macro gcctabopt{body}
14@code{\body\}
15@end macro
16
0285c67d
NC
17@c man begin NAME
18@ifset man
19@c Configure for the generation of man pages
20@set UsesEnvVars
21@set GENERIC
0285c67d 22@set ARM
ac145307 23@set C6X
b8891f8d 24@set CSKY
49fa1e15 25@set H8300
0285c67d 26@set HPPA
0285c67d 27@set M68HC11
7fb9f789 28@set M68K
833794fc 29@set MIPS
3c3bdf30 30@set MMIX
2469cfa2 31@set MSP430
35c08157 32@set NDS32
78058a5e 33@set NIOSII
fa1477dc 34@set PDP11
2a60a7a8
AM
35@set POWERPC
36@set POWERPC64
49fa1e15 37@set Renesas
b4cbbe8f 38@set S/390
49fa1e15
AM
39@set SPU
40@set TICOFF
2ca22b03 41@set WIN32
e0001a05 42@set XTENSA
0285c67d
NC
43@end ifset
44@c man end
45
9160ea82
AM
46@ifnottex
47@dircategory Software development
48@direntry
252b5132 49* Ld: (ld). The GNU linker.
9160ea82
AM
50@end direntry
51@end ifnottex
252b5132 52
0e9517a9 53@copying
e49e529d
JM
54This file documents the @sc{gnu} linker LD
55@ifset VERSION_PACKAGE
56@value{VERSION_PACKAGE}
57@end ifset
58version @value{VERSION}.
252b5132 59
d87bef3a 60Copyright @copyright{} 1991-2023 Free Software Foundation, Inc.
252b5132 61
cf055d54 62Permission is granted to copy, distribute and/or modify this document
793c5807 63under the terms of the GNU Free Documentation License, Version 1.3
cf055d54
NC
64or any later version published by the Free Software Foundation;
65with no Invariant Sections, with no Front-Cover Texts, and with no
66Back-Cover Texts. A copy of the license is included in the
36f63dca 67section entitled ``GNU Free Documentation License''.
0e9517a9 68@end copying
252b5132
RH
69@iftex
70@finalout
71@setchapternewpage odd
71ba23f6 72@settitle The GNU linker
252b5132 73@titlepage
71ba23f6 74@title The GNU linker
252b5132 75@sp 1
e49e529d
JM
76@subtitle @code{ld}
77@ifset VERSION_PACKAGE
78@subtitle @value{VERSION_PACKAGE}
79@end ifset
252b5132
RH
80@subtitle Version @value{VERSION}
81@author Steve Chamberlain
82@author Ian Lance Taylor
252b5132
RH
83@page
84
85@tex
86{\parskip=0pt
704c465c 87\hfill Red Hat Inc\par
3ac23310 88\hfill nickc\@redhat.com, doc\@redhat.com\par
71ba23f6 89\hfill {\it The GNU linker}\par
252b5132
RH
90\hfill Edited by Jeffrey Osier (jeffrey\@cygnus.com)\par
91}
92\global\parindent=0pt % Steve likes it this way.
93@end tex
94
95@vskip 0pt plus 1filll
0285c67d 96@c man begin COPYRIGHT
d87bef3a 97Copyright @copyright{} 1991-2023 Free Software Foundation, Inc.
252b5132 98
0285c67d 99Permission is granted to copy, distribute and/or modify this document
793c5807 100under the terms of the GNU Free Documentation License, Version 1.3
0285c67d
NC
101or any later version published by the Free Software Foundation;
102with no Invariant Sections, with no Front-Cover Texts, and with no
103Back-Cover Texts. A copy of the license is included in the
36f63dca 104section entitled ``GNU Free Documentation License''.
0285c67d 105@c man end
252b5132 106
252b5132
RH
107@end titlepage
108@end iftex
4ecceb71 109@contents
252b5132
RH
110@c FIXME: Talk about importance of *order* of args, cmds to linker!
111
84ec0e6d 112@ifnottex
252b5132 113@node Top
71ba23f6 114@top LD
e49e529d
JM
115This file documents the @sc{gnu} linker ld
116@ifset VERSION_PACKAGE
117@value{VERSION_PACKAGE}
118@end ifset
119version @value{VERSION}.
252b5132 120
cf055d54 121This document is distributed under the terms of the GNU Free
793c5807
NC
122Documentation License version 1.3. A copy of the license is included
123in the section entitled ``GNU Free Documentation License''.
cf055d54 124
252b5132
RH
125@menu
126* Overview:: Overview
127* Invocation:: Invocation
128* Scripts:: Linker Scripts
bf6d8037 129* Plugins:: Linker Plugins
252b5132
RH
130@ifset GENERIC
131* Machine Dependent:: Machine Dependent Features
132@end ifset
133@ifclear GENERIC
134@ifset H8300
135* H8/300:: ld and the H8/300
136@end ifset
c2dcd04e
NC
137@ifset Renesas
138* Renesas:: ld and other Renesas micros
252b5132 139@end ifset
36f63dca
NC
140@ifset ARM
141* ARM:: ld and the ARM family
142@end ifset
93fd0973
SC
143@ifset M68HC11
144* M68HC11/68HC12:: ld and the Motorola 68HC11 and 68HC12 families
145@end ifset
7ca01ed9
NC
146@ifset HPPA
147* HPPA ELF32:: ld and HPPA 32-bit ELF
148@end ifset
7fb9f789
NC
149@ifset M68K
150* M68K:: ld and Motorola 68K family
151@end ifset
833794fc
MR
152@ifset MIPS
153* MIPS:: ld and MIPS family
154@end ifset
2a60a7a8
AM
155@ifset POWERPC
156* PowerPC ELF32:: ld and PowerPC 32-bit ELF Support
157@end ifset
158@ifset POWERPC64
159* PowerPC64 ELF64:: ld and PowerPC64 64-bit ELF Support
160@end ifset
b4cbbe8f
AK
161@ifset S/390
162* S/390 ELF:: ld and S/390 ELF Support
163@end ifset
49fa1e15
AM
164@ifset SPU
165* SPU ELF:: ld and SPU ELF Support
166@end ifset
74459f0e
TW
167@ifset TICOFF
168* TI COFF:: ld and the TI COFF
169@end ifset
2ca22b03
NC
170@ifset WIN32
171* Win32:: ld and WIN32 (cygwin/mingw)
172@end ifset
e0001a05
NC
173@ifset XTENSA
174* Xtensa:: ld and Xtensa Processors
175@end ifset
252b5132
RH
176@end ifclear
177@ifclear SingleFormat
178* BFD:: BFD
179@end ifclear
180@c Following blank line required for remaining bug in makeinfo conds/menus
181
182* Reporting Bugs:: Reporting Bugs
183* MRI:: MRI Compatible Script Files
704c465c 184* GNU Free Documentation License:: GNU Free Documentation License
370b66a1 185* LD Index:: LD Index
252b5132 186@end menu
84ec0e6d 187@end ifnottex
252b5132
RH
188
189@node Overview
190@chapter Overview
191
192@cindex @sc{gnu} linker
193@cindex what is this?
0285c67d 194
0879a67a 195@ifset man
0285c67d 196@c man begin SYNOPSIS
ff5dcc92 197ld [@b{options}] @var{objfile} @dots{}
0285c67d
NC
198@c man end
199
200@c man begin SEEALSO
201ar(1), nm(1), objcopy(1), objdump(1), readelf(1) and
202the Info entries for @file{binutils} and
203@file{ld}.
204@c man end
205@end ifset
206
207@c man begin DESCRIPTION
208
ff5dcc92 209@command{ld} combines a number of object and archive files, relocates
252b5132 210their data and ties up symbol references. Usually the last step in
ff5dcc92 211compiling a program is to run @command{ld}.
252b5132 212
ff5dcc92 213@command{ld} accepts Linker Command Language files written in
252b5132
RH
214a superset of AT&T's Link Editor Command Language syntax,
215to provide explicit and total control over the linking process.
216
0285c67d
NC
217@ifset man
218@c For the man only
ece2d90e 219This man page does not describe the command language; see the
71ba23f6
NC
220@command{ld} entry in @code{info} for full details on the command
221language and on other aspects of the GNU linker.
0285c67d
NC
222@end ifset
223
252b5132 224@ifclear SingleFormat
ff5dcc92
SC
225This version of @command{ld} uses the general purpose BFD libraries
226to operate on object files. This allows @command{ld} to read, combine, and
252b5132
RH
227write object files in many different formats---for example, COFF or
228@code{a.out}. Different formats may be linked together to produce any
229available kind of object file. @xref{BFD}, for more information.
230@end ifclear
231
232Aside from its flexibility, the @sc{gnu} linker is more helpful than other
233linkers in providing diagnostic information. Many linkers abandon
234execution immediately upon encountering an error; whenever possible,
ff5dcc92 235@command{ld} continues executing, allowing you to identify other errors
252b5132
RH
236(or, in some cases, to get an output file in spite of the error).
237
0285c67d
NC
238@c man end
239
252b5132
RH
240@node Invocation
241@chapter Invocation
242
0285c67d
NC
243@c man begin DESCRIPTION
244
ff5dcc92 245The @sc{gnu} linker @command{ld} is meant to cover a broad range of situations,
252b5132
RH
246and to be as compatible as possible with other linkers. As a result,
247you have many choices to control its behavior.
248
0285c67d
NC
249@c man end
250
252b5132
RH
251@ifset UsesEnvVars
252@menu
a05a5b64 253* Options:: Command-line Options
252b5132
RH
254* Environment:: Environment Variables
255@end menu
256
257@node Options
a05a5b64 258@section Command-line Options
252b5132
RH
259@end ifset
260
261@cindex command line
262@cindex options
0285c67d
NC
263
264@c man begin OPTIONS
265
252b5132
RH
266The linker supports a plethora of command-line options, but in actual
267practice few of them are used in any particular context.
268@cindex standard Unix system
ff5dcc92 269For instance, a frequent use of @command{ld} is to link standard Unix
252b5132
RH
270object files on a standard, supported Unix system. On such a system, to
271link a file @code{hello.o}:
272
273@smallexample
274ld -o @var{output} /lib/crt0.o hello.o -lc
275@end smallexample
276
ff5dcc92 277This tells @command{ld} to produce a file called @var{output} as the
252b5132
RH
278result of linking the file @code{/lib/crt0.o} with @code{hello.o} and
279the library @code{libc.a}, which will come from the standard search
280directories. (See the discussion of the @samp{-l} option below.)
281
ff5dcc92 282Some of the command-line options to @command{ld} may be specified at any
511ab9e9
ILT
283point in the command line. However, options which refer to files, such
284as @samp{-l} or @samp{-T}, cause the file to be read at the point at
285which the option appears in the command line, relative to the object
286files and other file options. Repeating non-file options with a
287different argument will either have no further effect, or override prior
252b5132
RH
288occurrences (those further to the left on the command line) of that
289option. Options which may be meaningfully specified more than once are
290noted in the descriptions below.
291
292@cindex object files
511ab9e9
ILT
293Non-option arguments are object files or archives which are to be linked
294together. They may follow, precede, or be mixed in with command-line
295options, except that an object file argument may not be placed between
296an option and its argument.
252b5132
RH
297
298Usually the linker is invoked with at least one object file, but you can
299specify other forms of binary input files using @samp{-l}, @samp{-R},
300and the script command language. If @emph{no} binary input files at all
301are specified, the linker does not produce any output, and issues the
302message @samp{No input files}.
303
36f63dca 304If the linker cannot recognize the format of an object file, it will
252b5132
RH
305assume that it is a linker script. A script specified in this way
306augments the main linker script used for the link (either the default
307linker script or the one specified by using @samp{-T}). This feature
308permits the linker to link against a file which appears to be an object
309or an archive, but actually merely defines some symbol values, or uses
53d25da6
AM
310@code{INPUT} or @code{GROUP} to load other objects. Specifying a
311script in this way merely augments the main linker script, with the
312extra commands placed after the main script; use the @samp{-T} option
313to replace the default linker script entirely, but note the effect of
314the @code{INSERT} command. @xref{Scripts}.
252b5132
RH
315
316For options whose names are a single letter,
317option arguments must either follow the option letter without intervening
318whitespace, or be given as separate arguments immediately following the
319option that requires them.
320
321For options whose names are multiple letters, either one dash or two can
e4897a32 322precede the option name; for example, @samp{-trace-symbol} and
36f63dca 323@samp{--trace-symbol} are equivalent. Note---there is one exception to
e4897a32 324this rule. Multiple letter options that start with a lower case 'o' can
ba1be17e 325only be preceded by two dashes. This is to reduce confusion with the
e4897a32
NC
326@samp{-o} option. So for example @samp{-omagic} sets the output file
327name to @samp{magic} whereas @samp{--omagic} sets the NMAGIC flag on the
328output.
329
330Arguments to multiple-letter options must either be separated from the
331option name by an equals sign, or be given as separate arguments
332immediately following the option that requires them. For example,
333@samp{--trace-symbol foo} and @samp{--trace-symbol=foo} are equivalent.
334Unique abbreviations of the names of multiple-letter options are
335accepted.
252b5132 336
36f63dca 337Note---if the linker is being invoked indirectly, via a compiler driver
a05a5b64 338(e.g. @samp{gcc}) then all the linker command-line options should be
fa19fce0
NC
339prefixed by @samp{-Wl,} (or whatever is appropriate for the particular
340compiler driver) like this:
4e53152f
NC
341
342@smallexample
2509a395 343 gcc -Wl,--start-group foo.o bar.o -Wl,--end-group
4e53152f
NC
344@end smallexample
345
346This is important, because otherwise the compiler driver program may
2509a395
SL
347silently drop the linker options, resulting in a bad link. Confusion
348may also arise when passing options that require values through a
349driver, as the use of a space between option and argument acts as
350a separator, and causes the driver to pass only the option to the linker
351and the argument to the compiler. In this case, it is simplest to use
352the joined forms of both single- and multiple-letter options, such as:
353
354@smallexample
355 gcc foo.o bar.o -Wl,-eENTRY -Wl,-Map=a.map
356@end smallexample
4e53152f 357
a05a5b64 358Here is a table of the generic command-line switches accepted by the GNU
4e53152f
NC
359linker:
360
ff5dcc92 361@table @gcctabopt
38fc1cb1 362@include at-file.texi
dff70155 363
2509a395
SL
364@kindex -a @var{keyword}
365@item -a @var{keyword}
252b5132
RH
366This option is supported for HP/UX compatibility. The @var{keyword}
367argument must be one of the strings @samp{archive}, @samp{shared}, or
368@samp{default}. @samp{-aarchive} is functionally equivalent to
369@samp{-Bstatic}, and the other two keywords are functionally equivalent
370to @samp{-Bdynamic}. This option may be used any number of times.
371
7ee314fa
AM
372@kindex --audit @var{AUDITLIB}
373@item --audit @var{AUDITLIB}
374Adds @var{AUDITLIB} to the @code{DT_AUDIT} entry of the dynamic section.
375@var{AUDITLIB} is not checked for existence, nor will it use the DT_SONAME
376specified in the library. If specified multiple times @code{DT_AUDIT}
377will contain a colon separated list of audit interfaces to use. If the linker
378finds an object with an audit entry while searching for shared libraries,
9d5777a3 379it will add a corresponding @code{DT_DEPAUDIT} entry in the output file.
7ee314fa 380This option is only meaningful on ELF platforms supporting the rtld-audit
9d5777a3 381interface.
7ee314fa 382
252b5132
RH
383@ifclear SingleFormat
384@cindex binary input format
385@kindex -b @var{format}
386@kindex --format=@var{format}
387@cindex input format
388@cindex input format
389@item -b @var{input-format}
390@itemx --format=@var{input-format}
ff5dcc92
SC
391@command{ld} may be configured to support more than one kind of object
392file. If your @command{ld} is configured this way, you can use the
252b5132 393@samp{-b} option to specify the binary format for input object files
ff5dcc92 394that follow this option on the command line. Even when @command{ld} is
252b5132 395configured to support alternative object formats, you don't usually need
ff5dcc92 396to specify this, as @command{ld} should be configured to expect as a
252b5132
RH
397default input format the most usual format on each machine.
398@var{input-format} is a text string, the name of a particular format
399supported by the BFD libraries. (You can list the available binary
400formats with @samp{objdump -i}.)
401@xref{BFD}.
402
403You may want to use this option if you are linking files with an unusual
404binary format. You can also use @samp{-b} to switch formats explicitly (when
405linking object files of different formats), by including
406@samp{-b @var{input-format}} before each group of object files in a
a1ab1d2a 407particular format.
252b5132
RH
408
409The default format is taken from the environment variable
410@code{GNUTARGET}.
411@ifset UsesEnvVars
412@xref{Environment}.
413@end ifset
414You can also define the input format from a script, using the command
0285c67d
NC
415@code{TARGET};
416@ifclear man
417see @ref{Format Commands}.
418@end ifclear
252b5132
RH
419@end ifclear
420
421@kindex -c @var{MRI-cmdfile}
422@kindex --mri-script=@var{MRI-cmdfile}
423@cindex compatibility, MRI
424@item -c @var{MRI-commandfile}
425@itemx --mri-script=@var{MRI-commandfile}
ff5dcc92 426For compatibility with linkers produced by MRI, @command{ld} accepts script
252b5132 427files written in an alternate, restricted command language, described in
0285c67d
NC
428@ifclear man
429@ref{MRI,,MRI Compatible Script Files}.
430@end ifclear
431@ifset man
432the MRI Compatible Script Files section of GNU ld documentation.
433@end ifset
434Introduce MRI script files with
252b5132 435the option @samp{-c}; use the @samp{-T} option to run linker
ff5dcc92
SC
436scripts written in the general-purpose @command{ld} scripting language.
437If @var{MRI-cmdfile} does not exist, @command{ld} looks for it in the directories
252b5132
RH
438specified by any @samp{-L} options.
439
440@cindex common allocation
441@kindex -d
442@kindex -dc
443@kindex -dp
a1ab1d2a 444@item -d
252b5132
RH
445@itemx -dc
446@itemx -dp
447These three options are equivalent; multiple forms are supported for
448compatibility with other linkers. They assign space to common symbols
449even if a relocatable output file is specified (with @samp{-r}). The
450script command @code{FORCE_COMMON_ALLOCATION} has the same effect.
451@xref{Miscellaneous Commands}.
452
7ee314fa
AM
453@kindex --depaudit @var{AUDITLIB}
454@kindex -P @var{AUDITLIB}
455@item --depaudit @var{AUDITLIB}
456@itemx -P @var{AUDITLIB}
457Adds @var{AUDITLIB} to the @code{DT_DEPAUDIT} entry of the dynamic section.
458@var{AUDITLIB} is not checked for existence, nor will it use the DT_SONAME
459specified in the library. If specified multiple times @code{DT_DEPAUDIT}
460will contain a colon separated list of audit interfaces to use. This
461option is only meaningful on ELF platforms supporting the rtld-audit interface.
9d5777a3 462The -P option is provided for Solaris compatibility.
7ee314fa 463
2d5783fa
NC
464@kindex --enable-linker-version
465@item --enable-linker-version
466Enables the @code{LINKER_VERSION} linker script directive, described
467in @ref{Output Section Data}. If this directive is used in a linker
468script and this option has been enabled then a string containing the
469linker version will be inserted at the current point.
470
471Note - this location of this option on the linker command line is
472significant. It will only affect linker scripts that come after it on
473the command line, or which are built into the linker.
474
475@kindex --disable-linker-version
476@item --disable-linker-version
477Disables the @code{LINKER_VERSION} linker script directive, so that it
478does not insert a version string. This is the default.
479
abf874aa
CL
480@kindex --enable-non-contiguous-regions
481@item --enable-non-contiguous-regions
482This option avoids generating an error if an input section does not
483fit a matching output section. The linker tries to allocate the input
484section to subseque nt matching output sections, and generates an
485error only if no output section is large enough. This is useful when
486several non-contiguous memory regions are available and the input
487section does not require a particular one. The order in which input
488sections are evaluated does not change, for instance:
489
490@smallexample
491 MEMORY @{
93cda40b
NC
492 MEM1 (rwx) : ORIGIN = 0x1000, LENGTH = 0x14
493 MEM2 (rwx) : ORIGIN = 0x1000, LENGTH = 0x40
494 MEM3 (rwx) : ORIGIN = 0x2000, LENGTH = 0x40
abf874aa
CL
495 @}
496 SECTIONS @{
497 mem1 : @{ *(.data.*); @} > MEM1
498 mem2 : @{ *(.data.*); @} > MEM2
94ffdb59 499 mem3 : @{ *(.data.*); @} > MEM3
abf874aa
CL
500 @}
501
502 with input sections:
503 .data.1: size 8
504 .data.2: size 0x10
505 .data.3: size 4
506
507 results in .data.1 affected to mem1, and .data.2 and .data.3
508 affected to mem2, even though .data.3 would fit in mem3.
509@end smallexample
510
511This option is incompatible with INSERT statements because it changes
512the way input sections are mapped to output sections.
513
514@kindex --enable-non-contiguous-regions-warnings
515@item --enable-non-contiguous-regions-warnings
516This option enables warnings when
517@code{--enable-non-contiguous-regions} allows possibly unexpected
518matches in sections mapping, potentially leading to silently
519discarding a section instead of failing because it does not fit any
520output region.
521
252b5132
RH
522@cindex entry point, from command line
523@kindex -e @var{entry}
524@kindex --entry=@var{entry}
a1ab1d2a 525@item -e @var{entry}
252b5132
RH
526@itemx --entry=@var{entry}
527Use @var{entry} as the explicit symbol for beginning execution of your
528program, rather than the default entry point. If there is no symbol
529named @var{entry}, the linker will try to parse @var{entry} as a number,
530and use that as the entry address (the number will be interpreted in
531base 10; you may use a leading @samp{0x} for base 16, or a leading
532@samp{0} for base 8). @xref{Entry Point}, for a discussion of defaults
533and other ways of specifying the entry point.
534
b58f81ae
DJ
535@kindex --exclude-libs
536@item --exclude-libs @var{lib},@var{lib},...
537Specifies a list of archive libraries from which symbols should not be automatically
e1c37eb5 538exported. The library names may be delimited by commas or colons. Specifying
b58f81ae
DJ
539@code{--exclude-libs ALL} excludes symbols in all archive libraries from
540automatic export. This option is available only for the i386 PE targeted
541port of the linker and for ELF targeted ports. For i386 PE, symbols
542explicitly listed in a .def file are still exported, regardless of this
543option. For ELF targeted ports, symbols affected by this option will
544be treated as hidden.
545
e1c37eb5
DK
546@kindex --exclude-modules-for-implib
547@item --exclude-modules-for-implib @var{module},@var{module},...
548Specifies a list of object files or archive members, from which symbols
549should not be automatically exported, but which should be copied wholesale
550into the import library being generated during the link. The module names
551may be delimited by commas or colons, and must match exactly the filenames
552used by @command{ld} to open the files; for archive members, this is simply
553the member name, but for object files the name listed must include and
554match precisely any path used to specify the input file on the linker's
555command-line. This option is available only for the i386 PE targeted port
556of the linker. Symbols explicitly listed in a .def file are still exported,
557regardless of this option.
558
252b5132
RH
559@cindex dynamic symbol table
560@kindex -E
561@kindex --export-dynamic
267e2722 562@kindex --no-export-dynamic
252b5132
RH
563@item -E
564@itemx --export-dynamic
267e2722
CD
565@itemx --no-export-dynamic
566When creating a dynamically linked executable, using the @option{-E}
567option or the @option{--export-dynamic} option causes the linker to add
568all symbols to the dynamic symbol table. The dynamic symbol table is the
569set of symbols which are visible from dynamic objects at run time.
570
571If you do not use either of these options (or use the
572@option{--no-export-dynamic} option to restore the default behavior), the
573dynamic symbol table will normally contain only those symbols which are
574referenced by some dynamic object mentioned in the link.
252b5132
RH
575
576If you use @code{dlopen} to load a dynamic object which needs to refer
577back to the symbols defined by the program, rather than some other
578dynamic object, then you will probably need to use this option when
579linking the program itself.
580
55255dae 581You can also use the dynamic list to control what symbols should
cb840a31 582be added to the dynamic symbol table if the output format supports it.
55255dae 583See the description of @samp{--dynamic-list}.
cb840a31 584
8b747e1a
DK
585Note that this option is specific to ELF targeted ports. PE targets
586support a similar function to export all symbols from a DLL or EXE; see
587the description of @samp{--export-all-symbols} below.
588
37a141bf
FS
589@kindex --export-dynamic-symbol=@var{glob}
590@cindex export dynamic symbol
591@item --export-dynamic-symbol=@var{glob}
592When creating a dynamically linked executable, symbols matching
593@var{glob} will be added to the dynamic symbol table. When creating a
594shared library, references to symbols matching @var{glob} will not be
595bound to the definitions within the shared library. This option is a
596no-op when creating a shared library and @samp{-Bsymbolic} or
597@samp{--dynamic-list} are not specified. This option is only meaningful
598on ELF platforms which support shared libraries.
599
600@kindex --export-dynamic-symbol-list=@var{file}
601@cindex export dynamic symbol list
602@item --export-dynamic-symbol-list=@var{file}
603Specify a @samp{--export-dynamic-symbol} for each pattern in the file.
604The format of the file is the same as the version node without
605scope and node name. See @ref{VERSION} for more information.
606
36f63dca 607@ifclear SingleFormat
252b5132
RH
608@cindex big-endian objects
609@cindex endianness
610@kindex -EB
611@item -EB
612Link big-endian objects. This affects the default output format.
613
614@cindex little-endian objects
615@kindex -EL
616@item -EL
617Link little-endian objects. This affects the default output format.
36f63dca 618@end ifclear
252b5132 619
2509a395
SL
620@kindex -f @var{name}
621@kindex --auxiliary=@var{name}
622@item -f @var{name}
623@itemx --auxiliary=@var{name}
252b5132
RH
624When creating an ELF shared object, set the internal DT_AUXILIARY field
625to the specified name. This tells the dynamic linker that the symbol
626table of the shared object should be used as an auxiliary filter on the
627symbol table of the shared object @var{name}.
628
629If you later link a program against this filter object, then, when you
630run the program, the dynamic linker will see the DT_AUXILIARY field. If
631the dynamic linker resolves any symbols from the filter object, it will
632first check whether there is a definition in the shared object
633@var{name}. If there is one, it will be used instead of the definition
634in the filter object. The shared object @var{name} need not exist.
635Thus the shared object @var{name} may be used to provide an alternative
636implementation of certain functions, perhaps for debugging or for
a094d01f 637machine-specific performance.
252b5132
RH
638
639This option may be specified more than once. The DT_AUXILIARY entries
640will be created in the order in which they appear on the command line.
641
2509a395
SL
642@kindex -F @var{name}
643@kindex --filter=@var{name}
252b5132 644@item -F @var{name}
2509a395 645@itemx --filter=@var{name}
252b5132
RH
646When creating an ELF shared object, set the internal DT_FILTER field to
647the specified name. This tells the dynamic linker that the symbol table
648of the shared object which is being created should be used as a filter
649on the symbol table of the shared object @var{name}.
650
651If you later link a program against this filter object, then, when you
652run the program, the dynamic linker will see the DT_FILTER field. The
653dynamic linker will resolve symbols according to the symbol table of the
654filter object as usual, but it will actually link to the definitions
655found in the shared object @var{name}. Thus the filter object can be
656used to select a subset of the symbols provided by the object
657@var{name}.
658
ff5dcc92 659Some older linkers used the @option{-F} option throughout a compilation
252b5132 660toolchain for specifying object-file format for both input and output
36f63dca
NC
661object files.
662@ifclear SingleFormat
663The @sc{gnu} linker uses other mechanisms for this purpose: the
ece2d90e 664@option{-b}, @option{--format}, @option{--oformat} options, the
252b5132 665@code{TARGET} command in linker scripts, and the @code{GNUTARGET}
36f63dca
NC
666environment variable.
667@end ifclear
668The @sc{gnu} linker will ignore the @option{-F} option when not
669creating an ELF shared object.
252b5132 670
3dbf70a2 671@cindex finalization function
2509a395
SL
672@kindex -fini=@var{name}
673@item -fini=@var{name}
3dbf70a2
MM
674When creating an ELF executable or shared object, call NAME when the
675executable or shared object is unloaded, by setting DT_FINI to the
676address of the function. By default, the linker uses @code{_fini} as
677the function to call.
678
252b5132
RH
679@kindex -g
680@item -g
681Ignored. Provided for compatibility with other tools.
682
2509a395
SL
683@kindex -G @var{value}
684@kindex --gpsize=@var{value}
252b5132 685@cindex object size
2509a395 686@item -G @var{value}
252b5132
RH
687@itemx --gpsize=@var{value}
688Set the maximum size of objects to be optimized using the GP register to
689@var{size}. This is only meaningful for object file formats such as
e8044f35 690MIPS ELF that support putting large and small objects into different
252b5132
RH
691sections. This is ignored for other object file formats.
692
693@cindex runtime library name
2509a395 694@kindex -h @var{name}
252b5132 695@kindex -soname=@var{name}
2509a395 696@item -h @var{name}
252b5132
RH
697@itemx -soname=@var{name}
698When creating an ELF shared object, set the internal DT_SONAME field to
699the specified name. When an executable is linked with a shared object
700which has a DT_SONAME field, then when the executable is run the dynamic
701linker will attempt to load the shared object specified by the DT_SONAME
010e444b 702field rather than using the file name given to the linker.
252b5132
RH
703
704@kindex -i
705@cindex incremental link
706@item -i
707Perform an incremental link (same as option @samp{-r}).
708
3dbf70a2 709@cindex initialization function
2509a395
SL
710@kindex -init=@var{name}
711@item -init=@var{name}
3dbf70a2
MM
712When creating an ELF executable or shared object, call NAME when the
713executable or shared object is loaded, by setting DT_INIT to the address
714of the function. By default, the linker uses @code{_init} as the
715function to call.
716
252b5132 717@cindex archive files, from cmd line
2509a395 718@kindex -l @var{namespec}
bcb674cf 719@kindex --library=@var{namespec}
2509a395 720@item -l @var{namespec}
bcb674cf
RS
721@itemx --library=@var{namespec}
722Add the archive or object file specified by @var{namespec} to the
723list of files to link. This option may be used any number of times.
724If @var{namespec} is of the form @file{:@var{filename}}, @command{ld}
07d8eb55 725will search the library path for a file called @var{filename}, otherwise it
bcb674cf 726will search the library path for a file called @file{lib@var{namespec}.a}.
252b5132 727
ff5dcc92 728On systems which support shared libraries, @command{ld} may also search for
bcb674cf
RS
729files other than @file{lib@var{namespec}.a}. Specifically, on ELF
730and SunOS systems, @command{ld} will search a directory for a library
731called @file{lib@var{namespec}.so} before searching for one called
732@file{lib@var{namespec}.a}. (By convention, a @code{.so} extension
733indicates a shared library.) Note that this behavior does not apply
734to @file{:@var{filename}}, which always specifies a file called
735@var{filename}.
252b5132
RH
736
737The linker will search an archive only once, at the location where it is
738specified on the command line. If the archive defines a symbol which
739was undefined in some object which appeared before the archive on the
740command line, the linker will include the appropriate file(s) from the
741archive. However, an undefined symbol in an object appearing later on
742the command line will not cause the linker to search the archive again.
743
ff5dcc92 744See the @option{-(} option for a way to force the linker to search
252b5132
RH
745archives multiple times.
746
747You may list the same archive multiple times on the command line.
748
749@ifset GENERIC
750This type of archive searching is standard for Unix linkers. However,
ff5dcc92 751if you are using @command{ld} on AIX, note that it is different from the
252b5132
RH
752behaviour of the AIX linker.
753@end ifset
754
755@cindex search directory, from cmd line
2509a395 756@kindex -L @var{dir}
252b5132 757@kindex --library-path=@var{dir}
2509a395 758@item -L @var{searchdir}
252b5132 759@itemx --library-path=@var{searchdir}
ff5dcc92
SC
760Add path @var{searchdir} to the list of paths that @command{ld} will search
761for archive libraries and @command{ld} control scripts. You may use this
252b5132
RH
762option any number of times. The directories are searched in the order
763in which they are specified on the command line. Directories specified
764on the command line are searched before the default directories. All
ff5dcc92 765@option{-L} options apply to all @option{-l} options, regardless of the
7d24f02c
KH
766order in which the options appear. @option{-L} options do not affect
767how @command{ld} searches for a linker script unless @option{-T}
768option is specified.
252b5132 769
3aa2d05a
NC
770If @var{searchdir} begins with @code{=} or @code{$SYSROOT}, then this
771prefix will be replaced by the @dfn{sysroot prefix}, controlled by the
772@samp{--sysroot} option, or specified when the linker is configured.
9c8ebd6a 773
252b5132
RH
774@ifset UsesEnvVars
775The default set of paths searched (without being specified with
ff5dcc92 776@samp{-L}) depends on which emulation mode @command{ld} is using, and in
252b5132
RH
777some cases also on how it was configured. @xref{Environment}.
778@end ifset
779
780The paths can also be specified in a link script with the
781@code{SEARCH_DIR} command. Directories specified this way are searched
782at the point in which the linker script appears in the command line.
783
784@cindex emulation
785@kindex -m @var{emulation}
2509a395 786@item -m @var{emulation}
252b5132
RH
787Emulate the @var{emulation} linker. You can list the available
788emulations with the @samp{--verbose} or @samp{-V} options.
789
790If the @samp{-m} option is not used, the emulation is taken from the
791@code{LDEMULATION} environment variable, if that is defined.
792
793Otherwise, the default emulation depends upon how the linker was
794configured.
795
796@cindex link map
797@kindex -M
798@kindex --print-map
799@item -M
800@itemx --print-map
801Print a link map to the standard output. A link map provides
802information about the link, including the following:
803
804@itemize @bullet
805@item
3b83e13a 806Where object files are mapped into memory.
252b5132
RH
807@item
808How common symbols are allocated.
809@item
810All archive members included in the link, with a mention of the symbol
811which caused the archive member to be brought in.
3b83e13a
NC
812@item
813The values assigned to symbols.
814
815Note - symbols whose values are computed by an expression which
816involves a reference to a previous value of the same symbol may not
817have correct result displayed in the link map. This is because the
818linker discards intermediate results and only retains the final value
819of an expression. Under such circumstances the linker will display
820the final value enclosed by square brackets. Thus for example a
821linker script containing:
822
823@smallexample
824 foo = 1
825 foo = foo * 4
826 foo = foo + 8
827@end smallexample
828
829will produce the following output in the link map if the @option{-M}
830option is used:
831
832@smallexample
833 0x00000001 foo = 0x1
834 [0x0000000c] foo = (foo * 0x4)
835 [0x0000000c] foo = (foo + 0x8)
836@end smallexample
837
838See @ref{Expressions} for more information about expressions in linker
839scripts.
d2ef37eb 840
06ddcada
AM
841@item
842How GNU properties are merged.
d2ef37eb 843
06ddcada
AM
844When the linker merges input .note.gnu.property sections into one output
845.note.gnu.property section, some properties are removed or updated.
846These actions are reported in the link map. For example:
d2ef37eb
L
847
848@smallexample
849Removed property 0xc0000002 to merge foo.o (0x1) and bar.o (not found)
850@end smallexample
851
06ddcada 852This indicates that property 0xc0000002 is removed from output when
d2ef37eb
L
853merging properties in @file{foo.o}, whose property 0xc0000002 value
854is 0x1, and @file{bar.o}, which doesn't have property 0xc0000002.
855
856@smallexample
06ddcada 857Updated property 0xc0010001 (0x1) to merge foo.o (0x1) and bar.o (0x1)
d2ef37eb
L
858@end smallexample
859
06ddcada 860This indicates that property 0xc0010001 value is updated to 0x1 in output
d2ef37eb
L
861when merging properties in @file{foo.o}, whose 0xc0010001 property value
862is 0x1, and @file{bar.o}, whose 0xc0010001 property value is 0x1.
252b5132
RH
863@end itemize
864
035801ce
FS
865@cindex link map discarded
866@kindex --print-map-discarded
867@kindex --no-print-map-discarded
868@item --print-map-discarded
869@itemx --no-print-map-discarded
870Print (or do not print) the list of discarded and garbage collected sections
871in the link map. Enabled by default.
872
496917ce
NC
873@kindex --print-map-locals
874@kindex --no-print-map-locals
875@item --print-map-locals
876@itemx --no-print-map-locals
877Print (or do not print) local symbols in the link map. Local symbols
878will have the text @samp{(local)} printed before their name, and will
879be listed after all of the global symbols in a given section.
880Temporary local symbols (typically those that start with @samp{.L})
881will not be included in the output. Disabled by default.
882
252b5132
RH
883@kindex -n
884@cindex read-only text
885@cindex NMAGIC
886@kindex --nmagic
887@item -n
888@itemx --nmagic
90f5d9d9
JZ
889Turn off page alignment of sections, and disable linking against shared
890libraries. If the output format supports Unix style magic numbers,
891mark the output as @code{NMAGIC}.
252b5132
RH
892
893@kindex -N
894@kindex --omagic
895@cindex read/write from cmd line
896@cindex OMAGIC
a1ab1d2a 897@item -N
252b5132
RH
898@itemx --omagic
899Set the text and data sections to be readable and writable. Also, do
63fd3b82
NC
900not page-align the data segment, and disable linking against shared
901libraries. If the output format supports Unix style magic numbers,
4d8907ac
DS
902mark the output as @code{OMAGIC}. Note: Although a writable text section
903is allowed for PE-COFF targets, it does not conform to the format
904specification published by Microsoft.
63fd3b82
NC
905
906@kindex --no-omagic
907@cindex OMAGIC
908@item --no-omagic
909This option negates most of the effects of the @option{-N} option. It
910sets the text section to be read-only, and forces the data segment to
911be page-aligned. Note - this option does not enable linking against
912shared libraries. Use @option{-Bdynamic} for this.
252b5132
RH
913
914@kindex -o @var{output}
915@kindex --output=@var{output}
916@cindex naming the output file
917@item -o @var{output}
918@itemx --output=@var{output}
ff5dcc92 919Use @var{output} as the name for the program produced by @command{ld}; if this
252b5132
RH
920option is not specified, the name @file{a.out} is used by default. The
921script command @code{OUTPUT} can also specify the output file name.
922
f37b21b4
RM
923@kindex --dependency-file=@var{depfile}
924@cindex dependency file
925@item --dependency-file=@var{depfile}
926Write a @dfn{dependency file} to @var{depfile}. This file contains a rule
927suitable for @code{make} describing the output file and all the input files
928that were read to produce it. The output is similar to the compiler's
929output with @samp{-M -MP} (@pxref{Preprocessor Options,, Options
930Controlling the Preprocessor, gcc.info, Using the GNU Compiler
931Collection}). Note that there is no option like the compiler's @samp{-MM},
932to exclude ``system files'' (which is not a well-specified concept in the
933linker, unlike ``system headers'' in the compiler). So the output from
934@samp{--dependency-file} is always specific to the exact state of the
935installation where it was produced, and should not be copied into
936distributed makefiles without careful editing.
937
252b5132
RH
938@kindex -O @var{level}
939@cindex generating optimized output
940@item -O @var{level}
ff5dcc92 941If @var{level} is a numeric values greater than zero @command{ld} optimizes
252b5132 942the output. This might take significantly longer and therefore probably
98c503ac
NC
943should only be enabled for the final binary. At the moment this
944option only affects ELF shared library generation. Future releases of
945the linker may make more use of this option. Also currently there is
946no difference in the linker's behaviour for different non-zero values
947of this option. Again this may change with future releases.
252b5132 948
387dd777
DP
949@kindex -plugin @var{name}
950@item -plugin @var{name}
951Involve a plugin in the linking process. The @var{name} parameter is
952the absolute filename of the plugin. Usually this parameter is
953automatically added by the complier, when using link time
954optimization, but users can also add their own plugins if they so
955wish.
956
957Note that the location of the compiler originated plugins is different
958from the place where the @command{ar}, @command{nm} and
959@command{ranlib} programs search for their plugins. In order for
960those commands to make use of a compiler based plugin it must first be
41f37a6f 961copied into the @file{$@{libdir@}/bfd-plugins} directory. All gcc
387dd777 962based linker plugins are backward compatible, so it is sufficient to
6c19b93b 963just copy in the newest one.
387dd777 964
26278bb8
UD
965@kindex --push-state
966@cindex push state governing input file handling
967@item --push-state
f9a6a8f0 968The @option{--push-state} allows one to preserve the current state of the
26278bb8
UD
969flags which govern the input file handling so that they can all be
970restored with one corresponding @option{--pop-state} option.
971
972The option which are covered are: @option{-Bdynamic}, @option{-Bstatic},
973@option{-dn}, @option{-dy}, @option{-call_shared}, @option{-non_shared},
974@option{-static}, @option{-N}, @option{-n}, @option{--whole-archive},
975@option{--no-whole-archive}, @option{-r}, @option{-Ur},
976@option{--copy-dt-needed-entries}, @option{--no-copy-dt-needed-entries},
977@option{--as-needed}, @option{--no-as-needed}, and @option{-a}.
978
979One target for this option are specifications for @file{pkg-config}. When
980used with the @option{--libs} option all possibly needed libraries are
981listed and then possibly linked with all the time. It is better to return
982something as follows:
983
984@smallexample
985-Wl,--push-state,--as-needed -libone -libtwo -Wl,--pop-state
986@end smallexample
987
988@kindex --pop-state
989@cindex pop state governing input file handling
67cecaba 990@item --pop-state
26278bb8
UD
991Undoes the effect of --push-state, restores the previous values of the
992flags governing input file handling.
993
a712da20
NC
994@kindex -q
995@kindex --emit-relocs
996@cindex retain relocations in final executable
997@item -q
998@itemx --emit-relocs
ba1be17e 999Leave relocation sections and contents in fully linked executables.
a712da20
NC
1000Post link analysis and optimization tools may need this information in
1001order to perform correct modifications of executables. This results
1002in larger executables.
1003
dbab7a7b
NC
1004This option is currently only supported on ELF platforms.
1005
4f471f39
RS
1006@kindex --force-dynamic
1007@cindex forcing the creation of dynamic sections
1008@item --force-dynamic
1009Force the output file to have dynamic sections. This option is specific
1010to VxWorks targets.
1011
252b5132
RH
1012@cindex partial link
1013@cindex relocatable output
1014@kindex -r
1049f94e 1015@kindex --relocatable
252b5132 1016@item -r
1049f94e 1017@itemx --relocatable
252b5132 1018Generate relocatable output---i.e., generate an output file that can in
ff5dcc92 1019turn serve as input to @command{ld}. This is often called @dfn{partial
252b5132
RH
1020linking}. As a side effect, in environments that support standard Unix
1021magic numbers, this option also sets the output file's magic number to
1022@code{OMAGIC}.
ff5dcc92 1023@c ; see @option{-N}.
252b5132
RH
1024If this option is not specified, an absolute file is produced. When
1025linking C++ programs, this option @emph{will not} resolve references to
1026constructors; to do that, use @samp{-Ur}.
1027
62bf86b4
HPN
1028When an input file does not have the same format as the output file,
1029partial linking is only supported if that input file does not contain any
1030relocations. Different output formats can have further restrictions; for
1031example some @code{a.out}-based formats do not support partial linking
1032with input files in other formats at all.
1033
252b5132
RH
1034This option does the same thing as @samp{-i}.
1035
1036@kindex -R @var{file}
1037@kindex --just-symbols=@var{file}
1038@cindex symbol-only input
1039@item -R @var{filename}
1040@itemx --just-symbols=@var{filename}
1041Read symbol names and their addresses from @var{filename}, but do not
1042relocate it or include it in the output. This allows your output file
1043to refer symbolically to absolute locations of memory defined in other
1044programs. You may use this option more than once.
1045
ff5dcc92 1046For compatibility with other ELF linkers, if the @option{-R} option is
252b5132 1047followed by a directory name, rather than a file name, it is treated as
ff5dcc92 1048the @option{-rpath} option.
252b5132
RH
1049
1050@kindex -s
1051@kindex --strip-all
1052@cindex strip all symbols
a1ab1d2a 1053@item -s
252b5132
RH
1054@itemx --strip-all
1055Omit all symbol information from the output file.
1056
1057@kindex -S
1058@kindex --strip-debug
1059@cindex strip debugger symbols
a1ab1d2a 1060@item -S
252b5132
RH
1061@itemx --strip-debug
1062Omit debugger symbol information (but not all symbols) from the output file.
1063
a70f34c0
NC
1064@kindex --strip-discarded
1065@kindex --no-strip-discarded
1066@item --strip-discarded
1067@itemx --no-strip-discarded
1068Omit (or do not omit) global symbols defined in discarded sections.
1069Enabled by default.
1070
252b5132
RH
1071@kindex -t
1072@kindex --trace
1073@cindex input files, displaying
a1ab1d2a 1074@item -t
252b5132 1075@itemx --trace
4f4690cd
AM
1076Print the names of the input files as @command{ld} processes them. If
1077@samp{-t} is given twice then members within archives are also printed.
1078@samp{-t} output is useful to generate a list of all the object files
1079and scripts involved in linking, for example, when packaging files for
1080a linker bug report.
252b5132
RH
1081
1082@kindex -T @var{script}
1083@kindex --script=@var{script}
1084@cindex script files
1085@item -T @var{scriptfile}
1086@itemx --script=@var{scriptfile}
1087Use @var{scriptfile} as the linker script. This script replaces
31f14901
AM
1088@command{ld}'s default linker script (rather than adding to it),
1089unless the script contains @code{INSERT}, so
252b5132 1090@var{commandfile} must specify everything necessary to describe the
114283d8
NC
1091output file. @xref{Scripts}. If @var{scriptfile} does not exist in
1092the current directory, @code{ld} looks for it in the directories
1093specified by any preceding @samp{-L} options. Multiple @samp{-T}
1094options accumulate.
252b5132 1095
14be8564
L
1096@kindex -dT @var{script}
1097@kindex --default-script=@var{script}
1098@cindex script files
1099@item -dT @var{scriptfile}
1100@itemx --default-script=@var{scriptfile}
1101Use @var{scriptfile} as the default linker script. @xref{Scripts}.
1102
1103This option is similar to the @option{--script} option except that
1104processing of the script is delayed until after the rest of the
1105command line has been processed. This allows options placed after the
1106@option{--default-script} option on the command line to affect the
1107behaviour of the linker script, which can be important when the linker
1108command line cannot be directly controlled by the user. (eg because
1109the command line is being constructed by another tool, such as
1110@samp{gcc}).
1111
252b5132
RH
1112@kindex -u @var{symbol}
1113@kindex --undefined=@var{symbol}
1114@cindex undefined symbol
1115@item -u @var{symbol}
1116@itemx --undefined=@var{symbol}
1117Force @var{symbol} to be entered in the output file as an undefined
1118symbol. Doing this may, for example, trigger linking of additional
1119modules from standard libraries. @samp{-u} may be repeated with
1120different option arguments to enter additional undefined symbols. This
1121option is equivalent to the @code{EXTERN} linker script command.
1122
0a618243
AB
1123If this option is being used to force additional modules to be pulled
1124into the link, and if it is an error for the symbol to remain
1125undefined, then the option @option{--require-defined} should be used
1126instead.
1127
1128@kindex --require-defined=@var{symbol}
1129@cindex symbols, require defined
1130@cindex defined symbol
1131@item --require-defined=@var{symbol}
1132Require that @var{symbol} is defined in the output file. This option
1133is the same as option @option{--undefined} except that if @var{symbol}
1134is not defined in the output file then the linker will issue an error
1135and exit. The same effect can be achieved in a linker script by using
1136@code{EXTERN}, @code{ASSERT} and @code{DEFINED} together. This option
1137can be used multiple times to require additional symbols.
1138
252b5132
RH
1139@kindex -Ur
1140@cindex constructors
a1ab1d2a 1141@item -Ur
0a7dda4f
NC
1142
1143For programs that do not use constructors or destructors, or for ELF
1144based systems this option is equivalent to @option{-r}: it generates
1145relocatable output---i.e., an output file that can in turn serve as
1146input to @command{ld}. For other binaries however the @option{-Ur}
1147option is similar to @option{-r} but it also resolves references to
1148constructors and destructors.
1149
1150It does not work to use @option{-Ur} on files that were themselves
1151linked with @option{-Ur}; once the constructor table has been built,
1152it cannot be added to. Use @option{-Ur} only for the last partial
1153link, and @option{-r} for the others.
252b5132 1154
c005eb9e
AB
1155@kindex --orphan-handling=@var{MODE}
1156@cindex orphan sections
1157@cindex sections, orphan
1158@item --orphan-handling=@var{MODE}
1159Control how orphan sections are handled. An orphan section is one not
1160specifically mentioned in a linker script. @xref{Orphan Sections}.
1161
1162@var{MODE} can have any of the following values:
1163
1164@table @code
1165@item place
1166Orphan sections are placed into a suitable output section following
1167the strategy described in @ref{Orphan Sections}. The option
d2732b69 1168@samp{--unique} also affects how sections are placed.
c005eb9e
AB
1169
1170@item discard
1171All orphan sections are discarded, by placing them in the
1172@samp{/DISCARD/} section (@pxref{Output Section Discarding}).
1173
1174@item warn
1175The linker will place the orphan section as for @code{place} and also
1176issue a warning.
1177
1178@item error
1179The linker will exit with an error if any orphan section is found.
1180@end table
1181
1182The default if @samp{--orphan-handling} is not given is @code{place}.
1183
577a0623
AM
1184@kindex --unique[=@var{SECTION}]
1185@item --unique[=@var{SECTION}]
1186Creates a separate output section for every input section matching
1187@var{SECTION}, or if the optional wildcard @var{SECTION} argument is
1188missing, for every orphan input section. An orphan section is one not
1189specifically mentioned in a linker script. You may use this option
1190multiple times on the command line; It prevents the normal merging of
1191input sections with the same name, overriding output section assignments
1192in a linker script.
a854a4a7 1193
252b5132
RH
1194@kindex -v
1195@kindex -V
1196@kindex --version
1197@cindex version
1198@item -v
1199@itemx --version
1200@itemx -V
ff5dcc92 1201Display the version number for @command{ld}. The @option{-V} option also
2d5783fa
NC
1202lists the supported emulations. See also the description of the
1203@option{--enable-linker-version} in @ref{Options,,Command-line Options}
1204which can be used to insert the linker version string into a binary.
252b5132
RH
1205
1206@kindex -x
1207@kindex --discard-all
1208@cindex deleting local symbols
1209@item -x
1210@itemx --discard-all
1211Delete all local symbols.
1212
1213@kindex -X
1214@kindex --discard-locals
1215@cindex local symbols, deleting
a1ab1d2a 1216@item -X
252b5132 1217@itemx --discard-locals
3c68c38f
BW
1218Delete all temporary local symbols. (These symbols start with
1219system-specific local label prefixes, typically @samp{.L} for ELF systems
1220or @samp{L} for traditional a.out systems.)
252b5132
RH
1221
1222@kindex -y @var{symbol}
1223@kindex --trace-symbol=@var{symbol}
1224@cindex symbol tracing
1225@item -y @var{symbol}
1226@itemx --trace-symbol=@var{symbol}
1227Print the name of each linked file in which @var{symbol} appears. This
1228option may be given any number of times. On many systems it is necessary
1229to prepend an underscore.
1230
1231This option is useful when you have an undefined symbol in your link but
1232don't know where the reference is coming from.
1233
1234@kindex -Y @var{path}
1235@item -Y @var{path}
1236Add @var{path} to the default library search path. This option exists
1237for Solaris compatibility.
1238
1239@kindex -z @var{keyword}
1240@item -z @var{keyword}
cd6d6c15
NC
1241The recognized keywords are:
1242@table @samp
1243
e6e2dfbd
AM
1244@item call-nop=prefix-addr
1245@itemx call-nop=suffix-nop
1246@itemx call-nop=prefix-@var{byte}
1247@itemx call-nop=suffix-@var{byte}
1248Specify the 1-byte @code{NOP} padding when transforming indirect call
1249to a locally defined function, foo, via its GOT slot.
1250@option{call-nop=prefix-addr} generates @code{0x67 call foo}.
1251@option{call-nop=suffix-nop} generates @code{call foo 0x90}.
1252@option{call-nop=prefix-@var{byte}} generates @code{@var{byte} call foo}.
1253@option{call-nop=suffix-@var{byte}} generates @code{call foo @var{byte}}.
1254Supported for i386 and x86_64.
1255
233a0083
L
1256@item cet-report=none
1257@itemx cet-report=warning
1258@itemx cet-report=error
1259Specify how to report the missing GNU_PROPERTY_X86_FEATURE_1_IBT and
1260GNU_PROPERTY_X86_FEATURE_1_SHSTK properties in input .note.gnu.property
1261section. @option{cet-report=none}, which is the default, will make the
1262linker not report missing properties in input files.
1263@option{cet-report=warning} will make the linker issue a warning for
1264missing properties in input files. @option{cet-report=error} will make
1265the linker issue an error for missing properties in input files.
1266Note that @option{ibt} will turn off the missing
1267GNU_PROPERTY_X86_FEATURE_1_IBT property report and @option{shstk} will
1268turn off the missing GNU_PROPERTY_X86_FEATURE_1_SHSTK property report.
1269Supported for Linux/i386 and Linux/x86_64.
1270
cd6d6c15 1271@item combreloc
e6e2dfbd
AM
1272@itemx nocombreloc
1273Combine multiple dynamic relocation sections and sort to improve
1274dynamic symbol lookup caching. Do not do this if @samp{nocombreloc}.
cd6d6c15 1275
b8871f35 1276@item common
e6e2dfbd
AM
1277@itemx nocommon
1278Generate common symbols with STT_COMMON type during a relocatable
1279link. Use STT_OBJECT type if @samp{nocommon}.
1280
1281@item common-page-size=@var{value}
1282Set the page size most commonly used to @var{value}. Memory image
1283layout will be optimized to minimize memory pages if the system is
1284using pages of this size.
b8871f35 1285
cd6d6c15 1286@item defs
97a232d7
NC
1287Report unresolved symbol references from regular object files. This
1288is done even if the linker is creating a non-symbolic shared library.
1289This option is the inverse of @samp{-z undefs}.
cd6d6c15 1290
e6e2dfbd
AM
1291@item dynamic-undefined-weak
1292@itemx nodynamic-undefined-weak
1293Make undefined weak symbols dynamic when building a dynamic object,
1294if they are referenced from a regular object file and not forced local
1295by symbol visibility or versioning. Do not make them dynamic if
1296@samp{nodynamic-undefined-weak}. If neither option is given, a target
1297may default to either option being in force, or make some other
1298selection of undefined weak symbols dynamic. Not all targets support
1299these options.
1300
6aa29e7b
JJ
1301@item execstack
1302Marks the object as requiring executable stack.
1303
b039ef04
L
1304@item global
1305This option is only meaningful when building a shared object. It makes
1306the symbols defined by this shared object available for symbol resolution
1307of subsequently loaded libraries.
1308
93ab9c0d
NC
1309@item globalaudit
1310This option is only meaningful when building a dynamic executable.
1311This option marks the executable as requiring global auditing by
1312setting the @code{DF_1_GLOBAUDIT} bit in the @code{DT_FLAGS_1} dynamic
1313tag. Global auditing requires that any auditing library defined via
a05a5b64 1314the @option{--depaudit} or @option{-P} command-line options be run for
6c19b93b 1315all dynamic objects loaded by the application.
93ab9c0d 1316
e6e2dfbd
AM
1317@item ibtplt
1318Generate Intel Indirect Branch Tracking (IBT) enabled PLT entries.
1319Supported for Linux/i386 and Linux/x86_64.
1320
8df93cb5 1321@item ibt
e6e2dfbd
AM
1322Generate GNU_PROPERTY_X86_FEATURE_1_IBT in .note.gnu.property section
1323to indicate compatibility with IBT. This also implies @option{ibtplt}.
1324Supported for Linux/i386 and Linux/x86_64.
1325
6f365fda
L
1326@item indirect-extern-access
1327@itemx noindirect-extern-access
1328Generate GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS in
1329.note.gnu.property section to indicate that object file requires
1330canonical function pointers and cannot be used with copy relocation.
1331This option also implies @option{noextern-protected-data} and
1332@option{nocopyreloc}. Supported for i386 and x86-64.
1333
1334@option{noindirect-extern-access} removes
1335GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS from .note.gnu.property
1336section.
1337
cd6d6c15
NC
1338@item initfirst
1339This option is only meaningful when building a shared object.
1340It marks the object so that its runtime initialization will occur
1341before the runtime initialization of any other objects brought into
1342the process at the same time. Similarly the runtime finalization of
1343the object will occur after the runtime finalization of any other
1344objects.
1345
1346@item interpose
e6e2dfbd
AM
1347Specify that the dynamic loader should modify its symbol search order
1348so that symbols in this shared library interpose all other shared
1349libraries not so marked.
cd6d6c15 1350
c3805e4c
VDM
1351@item unique
1352@itemx nounique
1353When generating a shared library or other dynamically loadable ELF
1354object mark it as one that should (by default) only ever be loaded once,
1355and only in the main namespace (when using @code{dlmopen}). This is
1356primarily used to mark fundamental libraries such as libc, libpthread et
1357al which do not usually function correctly unless they are the sole instances
1358of themselves. This behaviour can be overridden by the @code{dlmopen} caller
1359and does not apply to certain loading mechanisms (such as audit libraries).
1360
279d901e
L
1361@item lam-u48
1362Generate GNU_PROPERTY_X86_FEATURE_1_LAM_U48 in .note.gnu.property section
1363to indicate compatibility with Intel LAM_U48. Supported for Linux/x86_64.
1364
1365@item lam-u57
1366Generate GNU_PROPERTY_X86_FEATURE_1_LAM_U57 in .note.gnu.property section
1367to indicate compatibility with Intel LAM_U57. Supported for Linux/x86_64.
1368
1369@item lam-u48-report=none
1370@itemx lam-u48-report=warning
1371@itemx lam-u48-report=error
1372Specify how to report the missing GNU_PROPERTY_X86_FEATURE_1_LAM_U48
1373property in input .note.gnu.property section.
1374@option{lam-u48-report=none}, which is the default, will make the
1375linker not report missing properties in input files.
1376@option{lam-u48-report=warning} will make the linker issue a warning for
1377missing properties in input files. @option{lam-u48-report=error} will
1378make the linker issue an error for missing properties in input files.
1379Supported for Linux/x86_64.
1380
1381@item lam-u57-report=none
1382@itemx lam-u57-report=warning
1383@itemx lam-u57-report=error
1384Specify how to report the missing GNU_PROPERTY_X86_FEATURE_1_LAM_U57
1385property in input .note.gnu.property section.
1386@option{lam-u57-report=none}, which is the default, will make the
1387linker not report missing properties in input files.
1388@option{lam-u57-report=warning} will make the linker issue a warning for
1389missing properties in input files. @option{lam-u57-report=error} will
1390make the linker issue an error for missing properties in input files.
1391Supported for Linux/x86_64.
1392
1393@item lam-report=none
1394@itemx lam-report=warning
1395@itemx lam-report=error
1396Specify how to report the missing GNU_PROPERTY_X86_FEATURE_1_LAM_U48 and
1397GNU_PROPERTY_X86_FEATURE_1_LAM_U57 properties in input .note.gnu.property
1398section. @option{lam-report=none}, which is the default, will make the
1399linker not report missing properties in input files.
1400@option{lam-report=warning} will make the linker issue a warning for
1401missing properties in input files. @option{lam-report=error} will make
1402the linker issue an error for missing properties in input files.
1403Supported for Linux/x86_64.
1404
5fa222e4
AM
1405@item lazy
1406When generating an executable or shared library, mark it to tell the
1407dynamic linker to defer function call resolution to the point when
1408the function is called (lazy binding), rather than at load time.
1409Lazy binding is the default.
1410
cd6d6c15 1411@item loadfltr
e6e2dfbd 1412Specify that the object's filters be processed immediately at runtime.
cd6d6c15 1413
e6e2dfbd
AM
1414@item max-page-size=@var{value}
1415Set the maximum memory page size supported to @var{value}.
cd6d6c15 1416
e6e2dfbd
AM
1417@item muldefs
1418Allow multiple definitions.
b8871f35 1419
cd6d6c15 1420@item nocopyreloc
daf220f0
AM
1421Disable linker generated .dynbss variables used in place of variables
1422defined in shared libraries. May result in dynamic text relocations.
cd6d6c15
NC
1423
1424@item nodefaultlib
e6e2dfbd
AM
1425Specify that the dynamic loader search for dependencies of this object
1426should ignore any default library search paths.
cd6d6c15
NC
1427
1428@item nodelete
e6e2dfbd 1429Specify that the object shouldn't be unloaded at runtime.
cd6d6c15
NC
1430
1431@item nodlopen
e6e2dfbd 1432Specify that the object is not available to @code{dlopen}.
cd6d6c15
NC
1433
1434@item nodump
e6e2dfbd 1435Specify that the object can not be dumped by @code{dldump}.
cd6d6c15 1436
6aa29e7b
JJ
1437@item noexecstack
1438Marks the object as not requiring executable stack.
1439
e6e2dfbd
AM
1440@item noextern-protected-data
1441Don't treat protected data symbols as external when building a shared
1442library. This option overrides the linker backend default. It can be
1443used to work around incorrect relocations against protected data symbols
1444generated by compiler. Updates on protected data symbols by another
1445module aren't visible to the resulting shared library. Supported for
1446i386 and x86-64.
8dfef1bd 1447
e6e2dfbd
AM
1448@item noreloc-overflow
1449Disable relocation overflow check. This can be used to disable
1450relocation overflow check if there will be no dynamic relocation
1451overflow at run-time. Supported for x86_64.
6aa29e7b 1452
cd6d6c15
NC
1453@item now
1454When generating an executable or shared library, mark it to tell the
1455dynamic linker to resolve all symbols when the program is started, or
e6e2dfbd
AM
1456when the shared library is loaded by dlopen, instead of deferring
1457function call resolution to the point when the function is first
1458called.
cd6d6c15
NC
1459
1460@item origin
e6e2dfbd 1461Specify that the object requires @samp{$ORIGIN} handling in paths.
cd6d6c15 1462
6a91be86
L
1463@item pack-relative-relocs
1464@itemx nopack-relative-relocs
1465Generate compact relative relocation in position-independent executable
1466and shared library. It adds @code{DT_RELR}, @code{DT_RELRSZ} and
1467@code{DT_RELRENT} entries to the dynamic section. It is ignored when
1468building position-dependent executable and relocatable output.
1469@option{nopack-relative-relocs} is the default, which disables compact
72aa8173
L
1470relative relocation. When linked against the GNU C Library, a
1471GLIBC_ABI_DT_RELR symbol version dependency on the shared C Library is
1472added to the output. Supported for i386 and x86-64.
6a91be86 1473
6aa29e7b 1474@item relro
e6e2dfbd 1475@itemx norelro
def5c83c
AM
1476Create an ELF @code{PT_GNU_RELRO} segment header in the object. This
1477specifies a memory segment that should be made read-only after
1478relocation, if supported. Specifying @samp{common-page-size} smaller
1479than the system page size will render this protection ineffective.
e6e2dfbd 1480Don't create an ELF @code{PT_GNU_RELRO} segment if @samp{norelro}.
6aa29e7b 1481
68b00778
L
1482@item report-relative-reloc
1483Report dynamic relative relocations generated by linker. Supported for
1484Linux/i386 and Linux/x86_64.
1485
47acac12
L
1486@item separate-code
1487@itemx noseparate-code
1488Create separate code @code{PT_LOAD} segment header in the object. This
1489specifies a memory segment that should contain only instructions and must
1490be in wholly disjoint pages from any other data. Don't create separate
1491code @code{PT_LOAD} segment if @samp{noseparate-code} is used.
1492
e6e2dfbd
AM
1493@item shstk
1494Generate GNU_PROPERTY_X86_FEATURE_1_SHSTK in .note.gnu.property section
1495to indicate compatibility with Intel Shadow Stack. Supported for
1496Linux/i386 and Linux/x86_64.
24718e3b 1497
04c3a755 1498@item stack-size=@var{value}
e6e2dfbd 1499Specify a stack size for an ELF @code{PT_GNU_STACK} segment.
04c3a755
NS
1500Specifying zero will override any default non-zero sized
1501@code{PT_GNU_STACK} segment creation.
1502
8ee10e86
AM
1503@item start-stop-gc
1504@itemx nostart-stop-gc
1505@cindex start-stop-gc
1506When @samp{--gc-sections} is in effect, a reference from a retained
1507section to @code{__start_SECNAME} or @code{__stop_SECNAME} causes all
1508input sections named @code{SECNAME} to also be retained, if
1509@code{SECNAME} is representable as a C identifier and either
1510@code{__start_SECNAME} or @code{__stop_SECNAME} is synthesized by the
1511linker. @samp{-z start-stop-gc} disables this effect, allowing
1512sections to be garbage collected as if the special synthesized symbols
1513were not defined. @samp{-z start-stop-gc} has no effect on a
1514definition of @code{__start_SECNAME} or @code{__stop_SECNAME} in an
1515object file or linker script. Such a definition will prevent the
1516linker providing a synthesized @code{__start_SECNAME} or
1517@code{__stop_SECNAME} respectively, and therefore the special
1518treatment by garbage collection for those references.
1519
cae64165
RM
1520@item start-stop-visibility=@var{value}
1521@cindex visibility
1522@cindex ELF symbol visibility
1523Specify the ELF symbol visibility for synthesized
1524@code{__start_SECNAME} and @code{__stop_SECNAME} symbols (@pxref{Input
1525Section Example}). @var{value} must be exactly @samp{default},
1526@samp{internal}, @samp{hidden}, or @samp{protected}. If no @samp{-z
1527start-stop-visibility} option is given, @samp{protected} is used for
1528compatibility with historical practice. However, it's highly
1529recommended to use @samp{-z start-stop-visibility=hidden} in new
1530programs and shared libraries so that these symbols are not exported
1531between shared objects, which is not usually what's intended.
1532
e6e2dfbd
AM
1533@item text
1534@itemx notext
1535@itemx textoff
a6dbf402
L
1536Report an error if DT_TEXTREL is set, i.e., if the position-independent
1537or shared object has dynamic relocations in read-only sections. Don't
1538report an error if @samp{notext} or @samp{textoff}.
48580982 1539
97a232d7
NC
1540@item undefs
1541Do not report unresolved symbol references from regular object files,
1542either when creating an executable, or when creating a shared library.
1543This option is the inverse of @samp{-z defs}.
1544
2d95647b
L
1545@item unique-symbol
1546@itemx nounique-symbol
1547Avoid duplicated local symbol names in the symbol string table. Append
1548".@code{number}" to duplicated local symbol names if @samp{unique-symbol}
1549is used. @option{nounique-symbol} is the default.
1550
b0ab0693 1551@item x86-64-baseline
32930e4e
L
1552@item x86-64-v2
1553@item x86-64-v3
1554@itemx x86-64-v4
1555Specify the x86-64 ISA level needed in .note.gnu.property section.
b0ab0693 1556@option{x86-64-baseline} generates @code{GNU_PROPERTY_X86_ISA_1_BASELINE}.
32930e4e
L
1557@option{x86-64-v2} generates @code{GNU_PROPERTY_X86_ISA_1_V2}.
1558@option{x86-64-v3} generates @code{GNU_PROPERTY_X86_ISA_1_V3}.
1559@option{x86-64-v4} generates @code{GNU_PROPERTY_X86_ISA_1_V4}.
1560Supported for Linux/i386 and Linux/x86_64.
1561
cd6d6c15
NC
1562@end table
1563
ece2d90e 1564Other keywords are ignored for Solaris compatibility.
252b5132
RH
1565
1566@kindex -(
1567@cindex groups of archives
1568@item -( @var{archives} -)
1569@itemx --start-group @var{archives} --end-group
1570The @var{archives} should be a list of archive files. They may be
1571either explicit file names, or @samp{-l} options.
1572
1573The specified archives are searched repeatedly until no new undefined
1574references are created. Normally, an archive is searched only once in
1575the order that it is specified on the command line. If a symbol in that
1576archive is needed to resolve an undefined symbol referred to by an
1577object in an archive that appears later on the command line, the linker
1578would not be able to resolve that reference. By grouping the archives,
5d3db3e2 1579they will all be searched repeatedly until all possible references are
252b5132
RH
1580resolved.
1581
1582Using this option has a significant performance cost. It is best to use
1583it only when there are unavoidable circular references between two or
1584more archives.
1585
69da35b5
NC
1586@kindex --accept-unknown-input-arch
1587@kindex --no-accept-unknown-input-arch
1588@item --accept-unknown-input-arch
1589@itemx --no-accept-unknown-input-arch
1590Tells the linker to accept input files whose architecture cannot be
2ca22b03 1591recognised. The assumption is that the user knows what they are doing
69da35b5
NC
1592and deliberately wants to link in these unknown input files. This was
1593the default behaviour of the linker, before release 2.14. The default
1594behaviour from release 2.14 onwards is to reject such input files, and
1595so the @samp{--accept-unknown-input-arch} option has been added to
1596restore the old behaviour.
2ca22b03 1597
4a43e768
AM
1598@kindex --as-needed
1599@kindex --no-as-needed
1600@item --as-needed
1601@itemx --no-as-needed
1602This option affects ELF DT_NEEDED tags for dynamic libraries mentioned
ddbb8a31 1603on the command line after the @option{--as-needed} option. Normally
4a43e768
AM
1604the linker will add a DT_NEEDED tag for each dynamic library mentioned
1605on the command line, regardless of whether the library is actually
ddbb8a31 1606needed or not. @option{--as-needed} causes a DT_NEEDED tag to only be
ffa9430d
AM
1607emitted for a library that @emph{at that point in the link} satisfies a
1608non-weak undefined symbol reference from a regular object file or, if
1240be6b
AM
1609the library is not found in the DT_NEEDED lists of other needed libraries, a
1610non-weak undefined symbol reference from another needed dynamic library.
ffa9430d
AM
1611Object files or libraries appearing on the command line @emph{after}
1612the library in question do not affect whether the library is seen as
1613needed. This is similar to the rules for extraction of object files
1614from archives. @option{--no-as-needed} restores the default behaviour.
4a43e768 1615
6418520e
NC
1616Note: On Linux based systems the @option{--as-needed} option also has
1617an affect on the behaviour of the @option{--rpath} and
1618@option{--rpath-link} options. See the description of
1619@option{--rpath-link} for more details.
1620
e56f61be
L
1621@kindex --add-needed
1622@kindex --no-add-needed
1623@item --add-needed
1624@itemx --no-add-needed
ddbb8a31
NC
1625These two options have been deprecated because of the similarity of
1626their names to the @option{--as-needed} and @option{--no-as-needed}
1627options. They have been replaced by @option{--copy-dt-needed-entries}
1628and @option{--no-copy-dt-needed-entries}.
e56f61be 1629
252b5132
RH
1630@kindex -assert @var{keyword}
1631@item -assert @var{keyword}
1632This option is ignored for SunOS compatibility.
1633
1634@kindex -Bdynamic
1635@kindex -dy
1636@kindex -call_shared
1637@item -Bdynamic
1638@itemx -dy
1639@itemx -call_shared
1640Link against dynamic libraries. This is only meaningful on platforms
1641for which shared libraries are supported. This option is normally the
1642default on such platforms. The different variants of this option are
1643for compatibility with various systems. You may use this option
1644multiple times on the command line: it affects library searching for
da8bce14 1645@option{-l} options which follow it.
252b5132 1646
a1ab1d2a
UD
1647@kindex -Bgroup
1648@item -Bgroup
1649Set the @code{DF_1_GROUP} flag in the @code{DT_FLAGS_1} entry in the dynamic
1650section. This causes the runtime linker to handle lookups in this
1651object and its dependencies to be performed only inside the group.
560e09e9
NC
1652@option{--unresolved-symbols=report-all} is implied. This option is
1653only meaningful on ELF platforms which support shared libraries.
a1ab1d2a 1654
252b5132
RH
1655@kindex -Bstatic
1656@kindex -dn
1657@kindex -non_shared
1658@kindex -static
a1ab1d2a 1659@item -Bstatic
252b5132
RH
1660@itemx -dn
1661@itemx -non_shared
1662@itemx -static
1663Do not link against shared libraries. This is only meaningful on
1664platforms for which shared libraries are supported. The different
1665variants of this option are for compatibility with various systems. You
1666may use this option multiple times on the command line: it affects
560e09e9 1667library searching for @option{-l} options which follow it. This
e9156f74
NC
1668option also implies @option{--unresolved-symbols=report-all}. This
1669option can be used with @option{-shared}. Doing so means that a
1670shared library is being created but that all of the library's external
1671references must be resolved by pulling in entries from static
ece2d90e 1672libraries.
252b5132
RH
1673
1674@kindex -Bsymbolic
1675@item -Bsymbolic
1676When creating a shared library, bind references to global symbols to the
1677definition within the shared library, if any. Normally, it is possible
1678for a program linked against a shared library to override the definition
1914e264
AM
1679within the shared library. This option is only meaningful on ELF
1680platforms which support shared libraries.
252b5132 1681
40b36307
L
1682@kindex -Bsymbolic-functions
1683@item -Bsymbolic-functions
1684When creating a shared library, bind references to global function
c0065db7 1685symbols to the definition within the shared library, if any.
40b36307 1686This option is only meaningful on ELF platforms which support shared
1914e264 1687libraries.
40b36307 1688
cf893b0e
FS
1689@kindex -Bno-symbolic
1690@item -Bno-symbolic
1691This option can cancel previously specified @samp{-Bsymbolic} and
1692@samp{-Bsymbolic-functions}.
1693
55255dae
L
1694@kindex --dynamic-list=@var{dynamic-list-file}
1695@item --dynamic-list=@var{dynamic-list-file}
1696Specify the name of a dynamic list file to the linker. This is
1697typically used when creating shared libraries to specify a list of
1698global symbols whose references shouldn't be bound to the definition
1699within the shared library, or creating dynamically linked executables
1700to specify a list of symbols which should be added to the symbol table
1701in the executable. This option is only meaningful on ELF platforms
1702which support shared libraries.
1703
1704The format of the dynamic list is the same as the version node without
1705scope and node name. See @ref{VERSION} for more information.
1706
40b36307
L
1707@kindex --dynamic-list-data
1708@item --dynamic-list-data
1709Include all global data symbols to the dynamic list.
1710
1711@kindex --dynamic-list-cpp-new
1712@item --dynamic-list-cpp-new
1713Provide the builtin dynamic list for C++ operator new and delete. It
1714is mainly useful for building shared libstdc++.
1715
0b8a70d9
L
1716@kindex --dynamic-list-cpp-typeinfo
1717@item --dynamic-list-cpp-typeinfo
1718Provide the builtin dynamic list for C++ runtime type identification.
1719
252b5132
RH
1720@kindex --check-sections
1721@kindex --no-check-sections
1722@item --check-sections
308b1ffd 1723@itemx --no-check-sections
252b5132 1724Asks the linker @emph{not} to check section addresses after they have
7d816a17 1725been assigned to see if there are any overlaps. Normally the linker will
252b5132
RH
1726perform this check, and if it finds any overlaps it will produce
1727suitable error messages. The linker does know about, and does make
1728allowances for sections in overlays. The default behaviour can be
a05a5b64 1729restored by using the command-line switch @option{--check-sections}.
02b0b1aa
NS
1730Section overlap is not usually checked for relocatable links. You can
1731force checking in that case by using the @option{--check-sections}
1732option.
252b5132 1733
ddbb8a31
NC
1734@kindex --copy-dt-needed-entries
1735@kindex --no-copy-dt-needed-entries
1736@item --copy-dt-needed-entries
1737@itemx --no-copy-dt-needed-entries
9d5777a3 1738This option affects the treatment of dynamic libraries referred to
ddbb8a31 1739by DT_NEEDED tags @emph{inside} ELF dynamic libraries mentioned on the
08efffb8 1740command line. Normally the linker won't add a DT_NEEDED tag to the
ddbb8a31 1741output binary for each library mentioned in a DT_NEEDED tag in an
08efffb8 1742input dynamic library. With @option{--copy-dt-needed-entries}
ddbb8a31 1743specified on the command line however any dynamic libraries that
08efffb8
MM
1744follow it will have their DT_NEEDED entries added. The default
1745behaviour can be restored with @option{--no-copy-dt-needed-entries}.
ddbb8a31
NC
1746
1747This option also has an effect on the resolution of symbols in dynamic
08efffb8
MM
1748libraries. With @option{--copy-dt-needed-entries} dynamic libraries
1749mentioned on the command line will be recursively searched, following
1750their DT_NEEDED tags to other libraries, in order to resolve symbols
1751required by the output binary. With the default setting however
1752the searching of dynamic libraries that follow it will stop with the
1753dynamic library itself. No DT_NEEDED links will be traversed to resolve
ddbb8a31
NC
1754symbols.
1755
252b5132
RH
1756@cindex cross reference table
1757@kindex --cref
1758@item --cref
1759Output a cross reference table. If a linker map file is being
1760generated, the cross reference table is printed to the map file.
1761Otherwise, it is printed on the standard output.
1762
1763The format of the table is intentionally simple, so that it may be
1764easily processed by a script if necessary. The symbols are printed out,
1765sorted by name. For each symbol, a list of file names is given. If the
1766symbol is defined, the first file listed is the location of the
049c1c8e
NC
1767definition. If the symbol is defined as a common value then any files
1768where this happens appear next. Finally any files that reference the
1769symbol are listed.
252b5132 1770
5dba6f05
NA
1771@cindex ctf variables
1772@kindex --ctf-variables
1773@kindex --no-ctf-variables
1774@item --ctf-variables
1775@item --no-ctf-variables
1776The CTF debuginfo format supports a section which encodes the names and
1777types of variables found in the program which do not appear in any symbol
1778table. These variables clearly cannot be looked up by address by
1779conventional debuggers, so the space used for their types and names is
1780usually wasted: the types are usually small but the names are often not.
1781@option{--ctf-variables} causes the generation of such a section.
1782The default behaviour can be restored with @option{--no-ctf-variables}.
1783
1784@cindex ctf type sharing
1785@kindex --ctf-share-types
1786@item --ctf-share-types=@var{method}
1787Adjust the method used to share types between translation units in CTF.
1788
1789@table @samp
1790@item share-unconflicted
1791Put all types that do not have ambiguous definitions into the shared dictionary,
1792where debuggers can easily access them, even if they only occur in one
1793translation unit. This is the default.
1794
1795@item share-duplicated
1796Put only types that occur in multiple translation units into the shared
1797dictionary: types with only one definition go into per-translation-unit
1798dictionaries. Types with ambiguous definitions in multiple translation units
1799always go into per-translation-unit dictionaries. This tends to make the CTF
1800larger, but may reduce the amount of CTF in the shared dictionary. For very
1801large projects this may speed up opening the CTF and save memory in the CTF
1802consumer at runtime.
1803@end table
1804
4818e05f
AM
1805@cindex common allocation
1806@kindex --no-define-common
1807@item --no-define-common
1808This option inhibits the assignment of addresses to common symbols.
1809The script command @code{INHIBIT_COMMON_ALLOCATION} has the same effect.
1810@xref{Miscellaneous Commands}.
1811
1812The @samp{--no-define-common} option allows decoupling
1813the decision to assign addresses to Common symbols from the choice
1814of the output file type; otherwise a non-Relocatable output type
1815forces assigning addresses to Common symbols.
1816Using @samp{--no-define-common} allows Common symbols that are referenced
1817from a shared library to be assigned addresses only in the main program.
1818This eliminates the unused duplicate space in the shared library,
1819and also prevents any possible confusion over resolving to the wrong
1820duplicate when there are many dynamic modules with specialized search
1821paths for runtime symbol resolution.
1822
7bdf4127
AB
1823@cindex group allocation in linker script
1824@cindex section groups
1825@cindex COMDAT
1826@kindex --force-group-allocation
1827@item --force-group-allocation
1828This option causes the linker to place section group members like
1829normal input sections, and to delete the section groups. This is the
1830default behaviour for a final link but this option can be used to
1831change the behaviour of a relocatable link (@samp{-r}). The script
1832command @code{FORCE_GROUP_ALLOCATION} has the same
1833effect. @xref{Miscellaneous Commands}.
1834
252b5132 1835@cindex symbols, from command line
2509a395
SL
1836@kindex --defsym=@var{symbol}=@var{exp}
1837@item --defsym=@var{symbol}=@var{expression}
252b5132
RH
1838Create a global symbol in the output file, containing the absolute
1839address given by @var{expression}. You may use this option as many
1840times as necessary to define multiple symbols in the command line. A
1841limited form of arithmetic is supported for the @var{expression} in this
1842context: you may give a hexadecimal constant or the name of an existing
1843symbol, or use @code{+} and @code{-} to add or subtract hexadecimal
1844constants or symbols. If you need more elaborate expressions, consider
66bc8739
AM
1845using the linker command language from a script (@pxref{Assignments}).
1846@emph{Note:} there should be no white space between @var{symbol}, the
1847equals sign (``@key{=}''), and @var{expression}.
252b5132 1848
54874444
AB
1849The linker processes @samp{--defsym} arguments and @samp{-T} arguments
1850in order, placing @samp{--defsym} before @samp{-T} will define the
1851symbol before the linker script from @samp{-T} is processed, while
1852placing @samp{--defsym} after @samp{-T} will define the symbol after
1853the linker script has been processed. This difference has
1854consequences for expressions within the linker script that use the
1855@samp{--defsym} symbols, which order is correct will depend on what
1856you are trying to achieve.
1857
252b5132 1858@cindex demangling, from command line
28c309a2 1859@kindex --demangle[=@var{style}]
252b5132 1860@kindex --no-demangle
28c309a2 1861@item --demangle[=@var{style}]
252b5132
RH
1862@itemx --no-demangle
1863These options control whether to demangle symbol names in error messages
1864and other output. When the linker is told to demangle, it tries to
1865present symbol names in a readable fashion: it strips leading
1866underscores if they are used by the object file format, and converts C++
a1ab1d2a
UD
1867mangled symbol names into user readable names. Different compilers have
1868different mangling styles. The optional demangling style argument can be used
1869to choose an appropriate demangling style for your compiler. The linker will
28c309a2
NC
1870demangle by default unless the environment variable @samp{COLLECT_NO_DEMANGLE}
1871is set. These options may be used to override the default.
252b5132
RH
1872
1873@cindex dynamic linker, from command line
506eee22 1874@kindex -I@var{file}
2509a395
SL
1875@kindex --dynamic-linker=@var{file}
1876@item -I@var{file}
1877@itemx --dynamic-linker=@var{file}
252b5132
RH
1878Set the name of the dynamic linker. This is only meaningful when
1879generating dynamically linked ELF executables. The default dynamic
1880linker is normally correct; don't use this unless you know what you are
1881doing.
1882
9b8b325a
RF
1883@kindex --no-dynamic-linker
1884@item --no-dynamic-linker
1885When producing an executable file, omit the request for a dynamic
1886linker to be used at load-time. This is only meaningful for ELF
1887executables that contain dynamic relocations, and usually requires
1888entry point code that is capable of processing these relocations.
1889
a70f34c0
NC
1890@kindex --embedded-relocs
1891@item --embedded-relocs
1892This option is similar to the @option{--emit-relocs} option except
a094d01f 1893that the relocs are stored in a target-specific section. This option
a70f34c0
NC
1894is only supported by the @samp{BFIN}, @samp{CR16} and @emph{M68K}
1895targets.
1896
3f0a5f17
ME
1897@kindex --disable-multiple-abs-defs
1898@item --disable-multiple-abs-defs
1899Do not allow multiple definitions with symbols included
1900in filename invoked by -R or --just-symbols
1901
7ce691ae 1902@kindex --fatal-warnings
0fe58ccd 1903@kindex --no-fatal-warnings
7ce691ae 1904@item --fatal-warnings
0fe58ccd
NC
1905@itemx --no-fatal-warnings
1906Treat all warnings as errors. The default behaviour can be restored
1907with the option @option{--no-fatal-warnings}.
7ce691ae 1908
4b2e7a57
NC
1909@kindex -w
1910@kindex --no-warnings
1911@item -w
1912@itemx --no-warnings
1913Do not display any warning or error messages. This overrides
1914@option{--fatal-warnings} if it has been enabled. This option can be
1915used when it is known that the output binary will not work, but there
1916is still a need to create it.
1917
252b5132
RH
1918@kindex --force-exe-suffix
1919@item --force-exe-suffix
1920Make sure that an output file has a .exe suffix.
1921
1922If a successfully built fully linked output file does not have a
1923@code{.exe} or @code{.dll} suffix, this option forces the linker to copy
1924the output file to one of the same name with a @code{.exe} suffix. This
1925option is useful when using unmodified Unix makefiles on a Microsoft
1926Windows host, since some versions of Windows won't run an image unless
1927it ends in a @code{.exe} suffix.
1928
1929@kindex --gc-sections
1930@kindex --no-gc-sections
1931@cindex garbage collection
c17d87de
NC
1932@item --gc-sections
1933@itemx --no-gc-sections
252b5132 1934Enable garbage collection of unused input sections. It is ignored on
ac69cbc6 1935targets that do not support this option. The default behaviour (of not
b3549761 1936performing this garbage collection) can be restored by specifying
0f088b2a
KT
1937@samp{--no-gc-sections} on the command line. Note that garbage
1938collection for COFF and PE format targets is supported, but the
1939implementation is currently considered to be experimental.
252b5132 1940
d5465ba2
AM
1941@samp{--gc-sections} decides which input sections are used by
1942examining symbols and relocations. The section containing the entry
1943symbol and all sections containing symbols undefined on the
1944command-line will be kept, as will sections containing symbols
1945referenced by dynamic objects. Note that when building shared
1946libraries, the linker must assume that any visible symbol is
1947referenced. Once this initial set of sections has been determined,
1948the linker recursively marks as used any section referenced by their
b69e1ff3
CC
1949relocations. See @samp{--entry}, @samp{--undefined}, and
1950@samp{--gc-keep-exported}.
d5465ba2 1951
ac69cbc6 1952This option can be set when doing a partial link (enabled with option
9d5777a3 1953@samp{-r}). In this case the root of symbols kept must be explicitly
b69e1ff3
CC
1954specified either by one of the options @samp{--entry},
1955@samp{--undefined}, or @samp{--gc-keep-exported} or by a @code{ENTRY}
1956command in the linker script.
ac69cbc6 1957
99fabbc9
JL
1958As a GNU extension, ELF input sections marked with the
1959@code{SHF_GNU_RETAIN} flag will not be garbage collected.
1960
c17d87de
NC
1961@kindex --print-gc-sections
1962@kindex --no-print-gc-sections
1963@cindex garbage collection
1964@item --print-gc-sections
1965@itemx --no-print-gc-sections
1966List all sections removed by garbage collection. The listing is
1967printed on stderr. This option is only effective if garbage
1968collection has been enabled via the @samp{--gc-sections}) option. The
1969default behaviour (of not listing the sections that are removed) can
1970be restored by specifying @samp{--no-print-gc-sections} on the command
1971line.
1972
22185505 1973@kindex --gc-keep-exported
1974@cindex garbage collection
1975@item --gc-keep-exported
1976When @samp{--gc-sections} is enabled, this option prevents garbage
1977collection of unused input sections that contain global symbols having
1978default or protected visibility. This option is intended to be used for
1979executables where unreferenced sections would otherwise be garbage
1980collected regardless of the external visibility of contained symbols.
1981Note that this option has no effect when linking shared objects since
1982it is already the default behaviour. This option is only supported for
1983ELF format targets.
1984
30824704
RM
1985@kindex --print-output-format
1986@cindex output format
1987@item --print-output-format
1988Print the name of the default output format (perhaps influenced by
1989other command-line options). This is the string that would appear
1990in an @code{OUTPUT_FORMAT} linker script command (@pxref{File Commands}).
1991
3604cb1f
TG
1992@kindex --print-memory-usage
1993@cindex memory usage
1994@item --print-memory-usage
1995Print used size, total size and used size of memory regions created with
1996the @ref{MEMORY} command. This is useful on embedded targets to have a
1997quick view of amount of free memory. The format of the output has one
1998headline and one line per region. It is both human readable and easily
1999parsable by tools. Here is an example of an output:
2000
2001@smallexample
2002Memory region Used Size Region Size %age Used
2003 ROM: 256 KB 1 MB 25.00%
2004 RAM: 32 B 2 GB 0.00%
2005@end smallexample
2006
252b5132
RH
2007@cindex help
2008@cindex usage
2009@kindex --help
2010@item --help
2011Print a summary of the command-line options on the standard output and exit.
2012
ea20a7da
CC
2013@kindex --target-help
2014@item --target-help
a094d01f 2015Print a summary of all target-specific options on the standard output and exit.
ea20a7da 2016
2509a395
SL
2017@kindex -Map=@var{mapfile}
2018@item -Map=@var{mapfile}
252b5132 2019Print a link map to the file @var{mapfile}. See the description of the
2c72361c
NC
2020@option{-M} option, above. If @var{mapfile} is just the character
2021@code{-} then the map will be written to stdout.
2022
2023Specifying a directory as @var{mapfile} causes the linker map to be
2024written as a file inside the directory. Normally name of the file
2025inside the directory is computed as the basename of the @var{output}
2026file with @code{.map} appended. If however the special character
2027@code{%} is used then this will be replaced by the full path of the
2028output file. Additionally if there are any characters after the
2029@var{%} symbol then @code{.map} will no longer be appended.
2030
2031@smallexample
2032 -o foo.exe -Map=bar [Creates ./bar]
2033 -o ../dir/foo.exe -Map=bar [Creates ./bar]
2034 -o foo.exe -Map=../dir [Creates ../dir/foo.exe.map]
2035 -o ../dir2/foo.exe -Map=../dir [Creates ../dir/foo.exe.map]
2036 -o foo.exe -Map=% [Creates ./foo.exe.map]
2037 -o ../dir/foo.exe -Map=% [Creates ../dir/foo.exe.map]
2038 -o foo.exe -Map=%.bar [Creates ./foo.exe.bar]
2039 -o ../dir/foo.exe -Map=%.bar [Creates ../dir/foo.exe.bar]
2040 -o ../dir2/foo.exe -Map=../dir/% [Creates ../dir/../dir2/foo.exe.map]
2041 -o ../dir2/foo.exe -Map=../dir/%.bar [Creates ../dir/../dir2/foo.exe.bar]
2042@end smallexample
2043
2044It is an error to specify more than one @code{%} character.
2045
2046If the map file already exists then it will be overwritten by this
2047operation.
252b5132
RH
2048
2049@cindex memory usage
2050@kindex --no-keep-memory
2051@item --no-keep-memory
ff5dcc92
SC
2052@command{ld} normally optimizes for speed over memory usage by caching the
2053symbol tables of input files in memory. This option tells @command{ld} to
252b5132 2054instead optimize for memory usage, by rereading the symbol tables as
ff5dcc92 2055necessary. This may be required if @command{ld} runs out of memory space
252b5132
RH
2056while linking a large executable.
2057
2058@kindex --no-undefined
a1ab1d2a 2059@kindex -z defs
97a232d7 2060@kindex -z undefs
252b5132 2061@item --no-undefined
a1ab1d2a 2062@itemx -z defs
560e09e9
NC
2063Report unresolved symbol references from regular object files. This
2064is done even if the linker is creating a non-symbolic shared library.
2065The switch @option{--[no-]allow-shlib-undefined} controls the
2066behaviour for reporting unresolved references found in shared
ece2d90e 2067libraries being linked in.
252b5132 2068
97a232d7
NC
2069The effects of this option can be reverted by using @code{-z undefs}.
2070
aa713662
L
2071@kindex --allow-multiple-definition
2072@kindex -z muldefs
2073@item --allow-multiple-definition
2074@itemx -z muldefs
2075Normally when a symbol is defined multiple times, the linker will
2076report a fatal error. These options allow multiple definitions and the
2077first definition will be used.
2078
b79e8c78 2079@kindex --allow-shlib-undefined
ae9a127f 2080@kindex --no-allow-shlib-undefined
b79e8c78 2081@item --allow-shlib-undefined
ae9a127f 2082@itemx --no-allow-shlib-undefined
903249d7 2083Allows or disallows undefined symbols in shared libraries.
560e09e9
NC
2084This switch is similar to @option{--no-undefined} except that it
2085determines the behaviour when the undefined symbols are in a
2086shared library rather than a regular object file. It does not affect
2087how undefined symbols in regular object files are handled.
2088
903249d7
NC
2089The default behaviour is to report errors for any undefined symbols
2090referenced in shared libraries if the linker is being used to create
2091an executable, but to allow them if the linker is being used to create
2092a shared library.
2093
2094The reasons for allowing undefined symbol references in shared
2095libraries specified at link time are that:
2096
2097@itemize @bullet
2098@item
2099A shared library specified at link time may not be the same as the one
2100that is available at load time, so the symbol might actually be
2101resolvable at load time.
2102@item
2103There are some operating systems, eg BeOS and HPPA, where undefined
2104symbols in shared libraries are normal.
2105
2106The BeOS kernel for example patches shared libraries at load time to
2107select whichever function is most appropriate for the current
2108architecture. This is used, for example, to dynamically select an
2109appropriate memset function.
2110@end itemize
b79e8c78 2111
23ae20f5
NC
2112@kindex --error-handling-script=@var{scriptname}
2113@item --error-handling-script=@var{scriptname}
2114If this option is provided then the linker will invoke
2115@var{scriptname} whenever an error is encountered. Currently however
2116only two kinds of error are supported: missing symbols and missing
2117libraries. Two arguments will be passed to script: the keyword
5c14cc55 2118``undefined-symbol'' or `missing-lib'' and the @var{name} of the
93cf38c0
NC
2119undefined symbol or missing library. The intention is that the script
2120will provide suggestions to the user as to where the symbol or library
23ae20f5
NC
2121might be found. After the script has finished then the normal linker
2122error message will be displayed.
2123
2124The availability of this option is controlled by a configure time
2125switch, so it may not be present in specific implementations.
2126
31941635
L
2127@kindex --no-undefined-version
2128@item --no-undefined-version
2129Normally when a symbol has an undefined version, the linker will ignore
2130it. This option disallows symbols with undefined version and a fatal error
2131will be issued instead.
2132
3e3b46e5
PB
2133@kindex --default-symver
2134@item --default-symver
2135Create and use a default symbol version (the soname) for unversioned
fc0e6df6
PB
2136exported symbols.
2137
2138@kindex --default-imported-symver
2139@item --default-imported-symver
2140Create and use a default symbol version (the soname) for unversioned
2141imported symbols.
3e3b46e5 2142
252b5132
RH
2143@kindex --no-warn-mismatch
2144@item --no-warn-mismatch
ff5dcc92 2145Normally @command{ld} will give an error if you try to link together input
252b5132
RH
2146files that are mismatched for some reason, perhaps because they have
2147been compiled for different processors or for different endiannesses.
ff5dcc92 2148This option tells @command{ld} that it should silently permit such possible
252b5132
RH
2149errors. This option should only be used with care, in cases when you
2150have taken some special action that ensures that the linker errors are
2151inappropriate.
2152
fe7929ce
AM
2153@kindex --no-warn-search-mismatch
2154@item --no-warn-search-mismatch
2155Normally @command{ld} will give a warning if it finds an incompatible
2156library during a library search. This option silences the warning.
2157
252b5132
RH
2158@kindex --no-whole-archive
2159@item --no-whole-archive
ff5dcc92 2160Turn off the effect of the @option{--whole-archive} option for subsequent
252b5132
RH
2161archive files.
2162
2163@cindex output file after errors
2164@kindex --noinhibit-exec
2165@item --noinhibit-exec
2166Retain the executable output file whenever it is still usable.
2167Normally, the linker will not produce an output file if it encounters
2168errors during the link process; it exits without writing an output file
2169when it issues any error whatsoever.
2170
0a9c1c8e
CD
2171@kindex -nostdlib
2172@item -nostdlib
2173Only search library directories explicitly specified on the
2174command line. Library directories specified in linker scripts
2175(including linker scripts specified on the command line) are ignored.
2176
252b5132 2177@ifclear SingleFormat
2509a395
SL
2178@kindex --oformat=@var{output-format}
2179@item --oformat=@var{output-format}
ff5dcc92
SC
2180@command{ld} may be configured to support more than one kind of object
2181file. If your @command{ld} is configured this way, you can use the
252b5132 2182@samp{--oformat} option to specify the binary format for the output
ff5dcc92
SC
2183object file. Even when @command{ld} is configured to support alternative
2184object formats, you don't usually need to specify this, as @command{ld}
252b5132
RH
2185should be configured to produce as a default output format the most
2186usual format on each machine. @var{output-format} is a text string, the
2187name of a particular format supported by the BFD libraries. (You can
2188list the available binary formats with @samp{objdump -i}.) The script
2189command @code{OUTPUT_FORMAT} can also specify the output format, but
2190this option overrides it. @xref{BFD}.
2191@end ifclear
2192
76359541
TP
2193@kindex --out-implib
2194@item --out-implib @var{file}
2195Create an import library in @var{file} corresponding to the executable
2196the linker is generating (eg. a DLL or ELF program). This import
2197library (which should be called @code{*.dll.a} or @code{*.a} for DLLs)
2198may be used to link clients against the generated executable; this
2199behaviour makes it possible to skip a separate import library creation
2200step (eg. @code{dlltool} for DLLs). This option is only available for
2201the i386 PE and ELF targetted ports of the linker.
2202
36af4a4e
JJ
2203@kindex -pie
2204@kindex --pic-executable
2205@item -pie
2206@itemx --pic-executable
2207@cindex position independent executables
2208Create a position independent executable. This is currently only supported on
2209ELF platforms. Position independent executables are similar to shared
2210libraries in that they are relocated by the dynamic linker to the virtual
7e7d5768 2211address the OS chooses for them (which can vary between invocations). Like
36af4a4e
JJ
2212normal dynamically linked executables they can be executed and symbols
2213defined in the executable cannot be overridden by shared libraries.
2214
e8f6c2a5
FS
2215@kindex -no-pie
2216@item -no-pie
2217@cindex position dependent executables
2218Create a position dependent executable. This is the default.
2219
252b5132
RH
2220@kindex -qmagic
2221@item -qmagic
2222This option is ignored for Linux compatibility.
2223
2224@kindex -Qy
2225@item -Qy
2226This option is ignored for SVR4 compatibility.
2227
2228@kindex --relax
2229@cindex synthesizing linker
2230@cindex relaxing addressing modes
28d5f677 2231@cindex --no-relax
252b5132 2232@item --relax
28d5f677 2233@itemx --no-relax
a1ab1d2a 2234An option with machine dependent effects.
252b5132
RH
2235@ifset GENERIC
2236This option is only supported on a few targets.
2237@end ifset
2238@ifset H8300
ff5dcc92 2239@xref{H8/300,,@command{ld} and the H8/300}.
252b5132 2240@end ifset
e0001a05
NC
2241@ifset XTENSA
2242@xref{Xtensa,, @command{ld} and Xtensa Processors}.
2243@end ifset
93fd0973
SC
2244@ifset M68HC11
2245@xref{M68HC11/68HC12,,@command{ld} and the 68HC11 and 68HC12}.
2246@end ifset
78058a5e
SL
2247@ifset NIOSII
2248@xref{Nios II,,@command{ld} and the Altera Nios II}.
2249@end ifset
2a60a7a8
AM
2250@ifset POWERPC
2251@xref{PowerPC ELF32,,@command{ld} and PowerPC 32-bit ELF Support}.
2252@end ifset
252b5132 2253
ccd9fae5 2254On some platforms the @option{--relax} option performs target specific,
28d5f677
NC
2255global optimizations that become possible when the linker resolves
2256addressing in the program, such as relaxing address modes,
2257synthesizing new instructions, selecting shorter version of current
11e7fd74 2258instructions, and combining constant values.
252b5132
RH
2259
2260On some platforms these link time global optimizations may make symbolic
2261debugging of the resulting executable impossible.
2262@ifset GENERIC
28d5f677
NC
2263This is known to be the case for the Matsushita MN10200 and MN10300
2264family of processors.
252b5132
RH
2265@end ifset
2266
ccd9fae5
NC
2267On platforms where the feature is supported, the option
2268@option{--no-relax} will disable it.
28d5f677 2269
ccd9fae5
NC
2270On platforms where the feature is not supported, both @option{--relax}
2271and @option{--no-relax} are accepted, but ignored.
5c14cc55 2272
252b5132
RH
2273@cindex retaining specified symbols
2274@cindex stripping all but some symbols
2275@cindex symbols, retaining selectively
2509a395
SL
2276@kindex --retain-symbols-file=@var{filename}
2277@item --retain-symbols-file=@var{filename}
252b5132
RH
2278Retain @emph{only} the symbols listed in the file @var{filename},
2279discarding all others. @var{filename} is simply a flat file, with one
2280symbol name per line. This option is especially useful in environments
2281@ifset GENERIC
2282(such as VxWorks)
2283@end ifset
2284where a large global symbol table is accumulated gradually, to conserve
2285run-time memory.
2286
2287@samp{--retain-symbols-file} does @emph{not} discard undefined symbols,
2288or symbols needed for relocations.
2289
2290You may only specify @samp{--retain-symbols-file} once in the command
2291line. It overrides @samp{-s} and @samp{-S}.
2292
2293@ifset GENERIC
2509a395 2294@item -rpath=@var{dir}
252b5132 2295@cindex runtime library search path
2509a395 2296@kindex -rpath=@var{dir}
252b5132 2297Add a directory to the runtime library search path. This is used when
ff5dcc92 2298linking an ELF executable with shared objects. All @option{-rpath}
252b5132 2299arguments are concatenated and passed to the runtime linker, which uses
c1b00498
MR
2300them to locate shared objects at runtime.
2301
2302The @option{-rpath} option is also used when locating shared objects which
2303are needed by shared objects explicitly included in the link; see the
2304description of the @option{-rpath-link} option. Searching @option{-rpath}
2305in this way is only supported by native linkers and cross linkers which
2306have been configured with the @option{--with-sysroot} option.
2307
2308If @option{-rpath} is not used when linking an ELF executable, the
2309contents of the environment variable @code{LD_RUN_PATH} will be used if it
2310is defined.
252b5132 2311
ff5dcc92 2312The @option{-rpath} option may also be used on SunOS. By default, on
d8506323 2313SunOS, the linker will form a runtime search path out of all the
ff5dcc92
SC
2314@option{-L} options it is given. If a @option{-rpath} option is used, the
2315runtime search path will be formed exclusively using the @option{-rpath}
2316options, ignoring the @option{-L} options. This can be useful when using
2317gcc, which adds many @option{-L} options which may be on NFS mounted
b45619c0 2318file systems.
252b5132 2319
ff5dcc92 2320For compatibility with other ELF linkers, if the @option{-R} option is
252b5132 2321followed by a directory name, rather than a file name, it is treated as
ff5dcc92 2322the @option{-rpath} option.
252b5132
RH
2323@end ifset
2324
2325@ifset GENERIC
2326@cindex link-time runtime library search path
2509a395
SL
2327@kindex -rpath-link=@var{dir}
2328@item -rpath-link=@var{dir}
252b5132
RH
2329When using ELF or SunOS, one shared library may require another. This
2330happens when an @code{ld -shared} link includes a shared library as one
2331of the input files.
2332
2333When the linker encounters such a dependency when doing a non-shared,
2334non-relocatable link, it will automatically try to locate the required
2335shared library and include it in the link, if it is not included
ff5dcc92 2336explicitly. In such a case, the @option{-rpath-link} option
252b5132 2337specifies the first set of directories to search. The
ff5dcc92 2338@option{-rpath-link} option may specify a sequence of directory names
252b5132
RH
2339either by specifying a list of names separated by colons, or by
2340appearing multiple times.
2341
e680a6b6
NC
2342The tokens @var{$ORIGIN} and @var{$LIB} can appear in these search
2343directories. They will be replaced by the full path to the directory
2344containing the program or shared object in the case of @var{$ORIGIN}
2345and either @samp{lib} - for 32-bit binaries - or @samp{lib64} - for
234664-bit binaries - in the case of @var{$LIB}.
2347
2348The alternative form of these tokens - @var{$@{ORIGIN@}} and
2349@var{$@{LIB@}} can also be used. The token @var{$PLATFORM} is not
2350supported.
2351
28c309a2
NC
2352This option should be used with caution as it overrides the search path
2353that may have been hard compiled into a shared library. In such a case it
2354is possible to use unintentionally a different search path than the
2355runtime linker would do.
2356
252b5132 2357The linker uses the following search paths to locate required shared
ece2d90e 2358libraries:
d8e4137b 2359
252b5132
RH
2360@enumerate
2361@item
ff5dcc92 2362Any directories specified by @option{-rpath-link} options.
252b5132 2363@item
ff5dcc92
SC
2364Any directories specified by @option{-rpath} options. The difference
2365between @option{-rpath} and @option{-rpath-link} is that directories
2366specified by @option{-rpath} options are included in the executable and
2367used at runtime, whereas the @option{-rpath-link} option is only effective
ece2d90e
NC
2368at link time. Searching @option{-rpath} in this way is only supported
2369by native linkers and cross linkers which have been configured with
2370the @option{--with-sysroot} option.
252b5132 2371@item
e2a83dd0
NC
2372On an ELF system, for native linkers, if the @option{-rpath} and
2373@option{-rpath-link} options were not used, search the contents of the
2374environment variable @code{LD_RUN_PATH}.
252b5132 2375@item
ff5dcc92
SC
2376On SunOS, if the @option{-rpath} option was not used, search any
2377directories specified using @option{-L} options.
252b5132 2378@item
a1b8d843 2379For a native linker, search the contents of the environment
e2a83dd0 2380variable @code{LD_LIBRARY_PATH}.
252b5132 2381@item
ec4eb78a
L
2382For a native ELF linker, the directories in @code{DT_RUNPATH} or
2383@code{DT_RPATH} of a shared library are searched for shared
2384libraries needed by it. The @code{DT_RPATH} entries are ignored if
2385@code{DT_RUNPATH} entries exist.
2386@item
d8e4137b
NC
2387For a linker for a Linux system, if the file @file{/etc/ld.so.conf}
2388exists, the list of directories found in that file. Note: the path
2389to this file is prefixed with the @code{sysroot} value, if that is
2390defined, and then any @code{prefix} string if the linker was
2391configured with the @command{--prefix=<path>} option.
2392@item
2393For a native linker on a FreeBSD system, any directories specified by
2394the @code{_PATH_ELF_HINTS} macro defined in the @file{elf-hints.h}
2395header file.
2396@item
364d7729
AM
2397Any directories specified by a @code{SEARCH_DIR} command in a
2398linker script given on the command line, including scripts specified
2399by @option{-T} (but not @option{-dT}).
2400@item
2401The default directories, normally @file{/lib} and @file{/usr/lib}.
2402@item
2403Any directories specified by a plugin LDPT_SET_EXTRA_LIBRARY_PATH.
2404@item
2405Any directories specified by a @code{SEARCH_DIR} command in a default
2406linker script.
252b5132
RH
2407@end enumerate
2408
6418520e
NC
2409Note however on Linux based systems there is an additional caveat: If
2410the @option{--as-needed} option is active @emph{and} a shared library
2411is located which would normally satisfy the search @emph{and} this
2412library does not have DT_NEEDED tag for @file{libc.so}
2413@emph{and} there is a shared library later on in the set of search
2414directories which also satisfies the search @emph{and}
2415this second shared library does have a DT_NEEDED tag for
2416@file{libc.so} @emph{then} the second library will be selected instead
2417of the first.
2418
252b5132
RH
2419If the required shared library is not found, the linker will issue a
2420warning and continue with the link.
6418520e 2421
252b5132
RH
2422@end ifset
2423
2424@kindex -shared
2425@kindex -Bshareable
2426@item -shared
2427@itemx -Bshareable
2428@cindex shared libraries
2429Create a shared library. This is currently only supported on ELF, XCOFF
2430and SunOS platforms. On SunOS, the linker will automatically create a
ff5dcc92 2431shared library if the @option{-e} option is not used and there are
252b5132
RH
2432undefined symbols in the link.
2433
252b5132 2434@kindex --sort-common
2509a395
SL
2435@item --sort-common
2436@itemx --sort-common=ascending
2437@itemx --sort-common=descending
de7dd2bd
NC
2438This option tells @command{ld} to sort the common symbols by alignment in
2439ascending or descending order when it places them in the appropriate output
2440sections. The symbol alignments considered are sixteen-byte or larger,
2441eight-byte, four-byte, two-byte, and one-byte. This is to prevent gaps
2442between symbols due to alignment constraints. If no sorting order is
2443specified, then descending order is assumed.
252b5132 2444
2509a395
SL
2445@kindex --sort-section=name
2446@item --sort-section=name
bcaa7b3e
L
2447This option will apply @code{SORT_BY_NAME} to all wildcard section
2448patterns in the linker script.
2449
2509a395
SL
2450@kindex --sort-section=alignment
2451@item --sort-section=alignment
bcaa7b3e
L
2452This option will apply @code{SORT_BY_ALIGNMENT} to all wildcard section
2453patterns in the linker script.
2454
a70f34c0
NC
2455@kindex --spare-dynamic-tags
2456@item --spare-dynamic-tags=@var{count}
2457This option specifies the number of empty slots to leave in the
2458.dynamic section of ELF shared objects. Empty slots may be needed by
2459post processing tools, such as the prelinker. The default is 5.
2460
252b5132 2461@kindex --split-by-file
2509a395 2462@item --split-by-file[=@var{size}]
ff5dcc92 2463Similar to @option{--split-by-reloc} but creates a new output section for
a854a4a7
AM
2464each input file when @var{size} is reached. @var{size} defaults to a
2465size of 1 if not given.
252b5132
RH
2466
2467@kindex --split-by-reloc
2509a395 2468@item --split-by-reloc[=@var{count}]
a854a4a7 2469Tries to creates extra sections in the output file so that no single
252b5132 2470output section in the file contains more than @var{count} relocations.
a854a4a7 2471This is useful when generating huge relocatable files for downloading into
252b5132
RH
2472certain real time kernels with the COFF object file format; since COFF
2473cannot represent more than 65535 relocations in a single section. Note
2474that this will fail to work with object file formats which do not
2475support arbitrary sections. The linker will not split up individual
2476input sections for redistribution, so if a single input section contains
2477more than @var{count} relocations one output section will contain that
a854a4a7 2478many relocations. @var{count} defaults to a value of 32768.
252b5132
RH
2479
2480@kindex --stats
2481@item --stats
2482Compute and display statistics about the operation of the linker, such
2483as execution time and memory usage.
2484
2509a395 2485@kindex --sysroot=@var{directory}
e2243057
RS
2486@item --sysroot=@var{directory}
2487Use @var{directory} as the location of the sysroot, overriding the
2488configure-time default. This option is only supported by linkers
2489that were configured using @option{--with-sysroot}.
2490
a70f34c0
NC
2491@kindex --task-link
2492@item --task-link
2493This is used by COFF/PE based targets to create a task-linked object
2494file where all of the global symbols have been converted to statics.
2495
252b5132
RH
2496@kindex --traditional-format
2497@cindex traditional format
2498@item --traditional-format
ff5dcc92
SC
2499For some targets, the output of @command{ld} is different in some ways from
2500the output of some existing linker. This switch requests @command{ld} to
252b5132
RH
2501use the traditional format instead.
2502
2503@cindex dbx
ff5dcc92 2504For example, on SunOS, @command{ld} combines duplicate entries in the
252b5132
RH
2505symbol string table. This can reduce the size of an output file with
2506full debugging information by over 30 percent. Unfortunately, the SunOS
2507@code{dbx} program can not read the resulting program (@code{gdb} has no
ff5dcc92 2508trouble). The @samp{--traditional-format} switch tells @command{ld} to not
252b5132
RH
2509combine duplicate entries.
2510
2509a395
SL
2511@kindex --section-start=@var{sectionname}=@var{org}
2512@item --section-start=@var{sectionname}=@var{org}
176355da
NC
2513Locate a section in the output file at the absolute
2514address given by @var{org}. You may use this option as many
2515times as necessary to locate multiple sections in the command
2516line.
2517@var{org} must be a single hexadecimal integer;
2518for compatibility with other linkers, you may omit the leading
2519@samp{0x} usually associated with hexadecimal values. @emph{Note:} there
2520should be no white space between @var{sectionname}, the equals
2521sign (``@key{=}''), and @var{org}.
2522
2509a395
SL
2523@kindex -Tbss=@var{org}
2524@kindex -Tdata=@var{org}
2525@kindex -Ttext=@var{org}
252b5132 2526@cindex segment origins, cmd line
2509a395
SL
2527@item -Tbss=@var{org}
2528@itemx -Tdata=@var{org}
2529@itemx -Ttext=@var{org}
2530Same as @option{--section-start}, with @code{.bss}, @code{.data} or
a6e02871 2531@code{.text} as the @var{sectionname}.
252b5132 2532
2509a395
SL
2533@kindex -Ttext-segment=@var{org}
2534@item -Ttext-segment=@var{org}
258795f5 2535@cindex text segment origin, cmd line
2b8c06a3
L
2536When creating an ELF executable, it will set the address of the first
2537byte of the text segment.
258795f5 2538
9d5777a3
RM
2539@kindex -Trodata-segment=@var{org}
2540@item -Trodata-segment=@var{org}
2541@cindex rodata segment origin, cmd line
2542When creating an ELF executable or shared object for a target where
2543the read-only data is in its own segment separate from the executable
2544text, it will set the address of the first byte of the read-only data segment.
2545
0d705e9f
AM
2546@kindex -Tldata-segment=@var{org}
2547@item -Tldata-segment=@var{org}
2548@cindex ldata segment origin, cmd line
2549When creating an ELF executable or shared object for x86-64 medium memory
2550model, it will set the address of the first byte of the ldata segment.
2551
560e09e9
NC
2552@kindex --unresolved-symbols
2553@item --unresolved-symbols=@var{method}
2554Determine how to handle unresolved symbols. There are four possible
2555values for @samp{method}:
2556
2557@table @samp
2558@item ignore-all
da8bce14 2559Do not report any unresolved symbols.
560e09e9
NC
2560
2561@item report-all
da8bce14 2562Report all unresolved symbols. This is the default.
560e09e9
NC
2563
2564@item ignore-in-object-files
2565Report unresolved symbols that are contained in shared libraries, but
2566ignore them if they come from regular object files.
2567
2568@item ignore-in-shared-libs
2569Report unresolved symbols that come from regular object files, but
2570ignore them if they come from shared libraries. This can be useful
2571when creating a dynamic binary and it is known that all the shared
2572libraries that it should be referencing are included on the linker's
2573command line.
2574@end table
2575
2576The behaviour for shared libraries on their own can also be controlled
2577by the @option{--[no-]allow-shlib-undefined} option.
2578
2579Normally the linker will generate an error message for each reported
2580unresolved symbol but the option @option{--warn-unresolved-symbols}
2581can change this to a warning.
2582
1715a13c
L
2583@kindex --verbose[=@var{NUMBER}]
2584@cindex verbose[=@var{NUMBER}]
252b5132 2585@item --dll-verbose
1715a13c 2586@itemx --verbose[=@var{NUMBER}]
ff5dcc92 2587Display the version number for @command{ld} and list the linker emulations
252b5132 2588supported. Display which input files can and cannot be opened. Display
1715a13c
L
2589the linker script being used by the linker. If the optional @var{NUMBER}
2590argument > 1, plugin symbol status will also be displayed.
252b5132
RH
2591
2592@kindex --version-script=@var{version-scriptfile}
2593@cindex version script, symbol versions
2509a395 2594@item --version-script=@var{version-scriptfile}
252b5132
RH
2595Specify the name of a version script to the linker. This is typically
2596used when creating shared libraries to specify additional information
36f63dca 2597about the version hierarchy for the library being created. This option
09e2aba4
DK
2598is only fully supported on ELF platforms which support shared libraries;
2599see @ref{VERSION}. It is partially supported on PE platforms, which can
2600use version scripts to filter symbol visibility in auto-export mode: any
2601symbols marked @samp{local} in the version script will not be exported.
2602@xref{WIN32}.
252b5132 2603
7ce691ae 2604@kindex --warn-common
252b5132
RH
2605@cindex warnings, on combining symbols
2606@cindex combining symbols, warnings on
2607@item --warn-common
2608Warn when a common symbol is combined with another common symbol or with
11e7fd74 2609a symbol definition. Unix linkers allow this somewhat sloppy practice,
252b5132
RH
2610but linkers on some other operating systems do not. This option allows
2611you to find potential problems from combining global symbols.
11e7fd74 2612Unfortunately, some C libraries use this practice, so you may get some
252b5132
RH
2613warnings about symbols in the libraries as well as in your programs.
2614
2615There are three kinds of global symbols, illustrated here by C examples:
2616
2617@table @samp
2618@item int i = 1;
2619A definition, which goes in the initialized data section of the output
2620file.
2621
2622@item extern int i;
2623An undefined reference, which does not allocate space.
2624There must be either a definition or a common symbol for the
2625variable somewhere.
2626
2627@item int i;
2628A common symbol. If there are only (one or more) common symbols for a
2629variable, it goes in the uninitialized data area of the output file.
2630The linker merges multiple common symbols for the same variable into a
2631single symbol. If they are of different sizes, it picks the largest
2632size. The linker turns a common symbol into a declaration, if there is
2633a definition of the same variable.
2634@end table
2635
2636The @samp{--warn-common} option can produce five kinds of warnings.
2637Each warning consists of a pair of lines: the first describes the symbol
2638just encountered, and the second describes the previous symbol
2639encountered with the same name. One or both of the two symbols will be
2640a common symbol.
2641
2642@enumerate
2643@item
2644Turning a common symbol into a reference, because there is already a
2645definition for the symbol.
2646@smallexample
2647@var{file}(@var{section}): warning: common of `@var{symbol}'
2648 overridden by definition
2649@var{file}(@var{section}): warning: defined here
2650@end smallexample
2651
2652@item
2653Turning a common symbol into a reference, because a later definition for
2654the symbol is encountered. This is the same as the previous case,
2655except that the symbols are encountered in a different order.
2656@smallexample
2657@var{file}(@var{section}): warning: definition of `@var{symbol}'
2658 overriding common
2659@var{file}(@var{section}): warning: common is here
2660@end smallexample
2661
2662@item
2663Merging a common symbol with a previous same-sized common symbol.
2664@smallexample
2665@var{file}(@var{section}): warning: multiple common
2666 of `@var{symbol}'
2667@var{file}(@var{section}): warning: previous common is here
2668@end smallexample
2669
2670@item
2671Merging a common symbol with a previous larger common symbol.
2672@smallexample
2673@var{file}(@var{section}): warning: common of `@var{symbol}'
2674 overridden by larger common
2675@var{file}(@var{section}): warning: larger common is here
2676@end smallexample
2677
2678@item
2679Merging a common symbol with a previous smaller common symbol. This is
2680the same as the previous case, except that the symbols are
2681encountered in a different order.
2682@smallexample
2683@var{file}(@var{section}): warning: common of `@var{symbol}'
2684 overriding smaller common
2685@var{file}(@var{section}): warning: smaller common is here
2686@end smallexample
2687@end enumerate
2688
2689@kindex --warn-constructors
2690@item --warn-constructors
2691Warn if any global constructors are used. This is only useful for a few
2692object file formats. For formats like COFF or ELF, the linker can not
2693detect the use of global constructors.
2694
65daf5be 2695@kindex --warn-execstack
bd7d326d 2696@cindex warnings, on executable stack
65daf5be
NC
2697@cindex executable stack, warnings on
2698@item --warn-execstack
2699@itemx --no-warn-execstack
2700On ELF platforms this option controls how the linker generates warning
2701messages when it creates an output file with an executable stack. By
2702default the linker will not warn if the @command{-z execstack} command
2703line option has been used, but this behaviour can be overridden by the
2704@option{--warn-execstack} option.
2705
2706On the other hand the linker will normally warn if the stack is made
2707executable because one or more of the input files need an execuable
2708stack and neither of the @command{-z execstack} or @command{-z
bd7d326d 2709noexecstack} command line options have been specified. This warning
65daf5be
NC
2710can be disabled via the @command{--no-warn-execstack} option.
2711
2712Note: ELF format input files specify that they need an executable
2713stack by having a @var{.note.GNU-stack} section with the executable
2714bit set in its section flags. They can specify that they do not need
2715an executable stack by having that section, but without the executable
2716flag bit set. If an input file does not have a @var{.note.GNU-stack}
2717section present then the default behaviour is target specific. For
2718some targets, then absence of such a section implies that an
2719executable stack @emph{is} required. This is often a problem for hand
2720crafted assembler files.
2721
252b5132
RH
2722@kindex --warn-multiple-gp
2723@item --warn-multiple-gp
2724Warn if multiple global pointer values are required in the output file.
2725This is only meaningful for certain processors, such as the Alpha.
2726Specifically, some processors put large-valued constants in a special
2727section. A special register (the global pointer) points into the middle
2728of this section, so that constants can be loaded efficiently via a
2729base-register relative addressing mode. Since the offset in
2730base-register relative mode is fixed and relatively small (e.g., 16
2731bits), this limits the maximum size of the constant pool. Thus, in
2732large programs, it is often necessary to use multiple global pointer
2733values in order to be able to address all possible constants. This
2734option causes a warning to be issued whenever this case occurs.
2735
2736@kindex --warn-once
2737@cindex warnings, on undefined symbols
2738@cindex undefined symbols, warnings on
2739@item --warn-once
2740Only warn once for each undefined symbol, rather than once per module
2741which refers to it.
2742
ba951afb
NC
2743@kindex --warn-rwx-segments
2744@cindex warnings, on writeable and exectuable segments
2745@cindex executable segments, warnings on
2746@item --warn-rwx-segments
2747@itemx --no-warn-rwx-segments
2748Warn if the linker creates a loadable, non-zero sized segment that has
2749all three of the read, write and execute permission flags set. Such a
2750segment represents a potential security vulnerability. In addition
2751warnings will be generated if a thread local storage segment is
2752created with the execute permission flag set, regardless of whether or
2753not it has the read and/or write flags set.
2754
2755These warnings are enabled by default. They can be disabled via the
2756@option{--no-warn-rwx-segments} option and re-enabled via the
2757@option{--warn-rwx-segments} option.
2758
252b5132
RH
2759@kindex --warn-section-align
2760@cindex warnings, on section alignment
2761@cindex section alignment, warnings on
2762@item --warn-section-align
2763Warn if the address of an output section is changed because of
2764alignment. Typically, the alignment will be set by an input section.
2765The address will only be changed if it not explicitly specified; that
2766is, if the @code{SECTIONS} command does not specify a start address for
2767the section (@pxref{SECTIONS}).
2768
a6dbf402
L
2769@kindex --warn-textrel
2770@item --warn-textrel
2771Warn if the linker adds DT_TEXTREL to a position-independent executable
2772or shared object.
8fdd7217 2773
a0c402a5
L
2774@kindex --warn-alternate-em
2775@item --warn-alternate-em
2776Warn if an object has alternate ELF machine code.
2777
560e09e9
NC
2778@kindex --warn-unresolved-symbols
2779@item --warn-unresolved-symbols
2780If the linker is going to report an unresolved symbol (see the option
2781@option{--unresolved-symbols}) it will normally generate an error.
2782This option makes it generate a warning instead.
2783
2784@kindex --error-unresolved-symbols
2785@item --error-unresolved-symbols
2786This restores the linker's default behaviour of generating errors when
2787it is reporting unresolved symbols.
2788
252b5132
RH
2789@kindex --whole-archive
2790@cindex including an entire archive
2791@item --whole-archive
2792For each archive mentioned on the command line after the
ff5dcc92 2793@option{--whole-archive} option, include every object file in the archive
252b5132
RH
2794in the link, rather than searching the archive for the required object
2795files. This is normally used to turn an archive file into a shared
2796library, forcing every object to be included in the resulting shared
2797library. This option may be used more than once.
2798
7ec229ce 2799Two notes when using this option from gcc: First, gcc doesn't know
ff5dcc92
SC
2800about this option, so you have to use @option{-Wl,-whole-archive}.
2801Second, don't forget to use @option{-Wl,-no-whole-archive} after your
7ec229ce
DD
2802list of archives, because gcc will add its own list of archives to
2803your link and you may not want this flag to affect those as well.
2804
2509a395
SL
2805@kindex --wrap=@var{symbol}
2806@item --wrap=@var{symbol}
252b5132
RH
2807Use a wrapper function for @var{symbol}. Any undefined reference to
2808@var{symbol} will be resolved to @code{__wrap_@var{symbol}}. Any
2809undefined reference to @code{__real_@var{symbol}} will be resolved to
2810@var{symbol}.
2811
2812This can be used to provide a wrapper for a system function. The
2813wrapper function should be called @code{__wrap_@var{symbol}}. If it
2814wishes to call the system function, it should call
2815@code{__real_@var{symbol}}.
2816
2817Here is a trivial example:
2818
2819@smallexample
2820void *
cc2f008e 2821__wrap_malloc (size_t c)
252b5132 2822@{
cc2f008e 2823 printf ("malloc called with %zu\n", c);
252b5132
RH
2824 return __real_malloc (c);
2825@}
2826@end smallexample
2827
ff5dcc92 2828If you link other code with this file using @option{--wrap malloc}, then
252b5132
RH
2829all calls to @code{malloc} will call the function @code{__wrap_malloc}
2830instead. The call to @code{__real_malloc} in @code{__wrap_malloc} will
2831call the real @code{malloc} function.
2832
2833You may wish to provide a @code{__real_malloc} function as well, so that
ff5dcc92 2834links without the @option{--wrap} option will succeed. If you do this,
252b5132
RH
2835you should not put the definition of @code{__real_malloc} in the same
2836file as @code{__wrap_malloc}; if you do, the assembler may resolve the
2837call before the linker has a chance to wrap it to @code{malloc}.
2838
4ea904ed
SH
2839Only undefined references are replaced by the linker. So, translation unit
2840internal references to @var{symbol} are not resolved to
2841@code{__wrap_@var{symbol}}. In the next example, the call to @code{f} in
2842@code{g} is not resolved to @code{__wrap_f}.
2843
2844@smallexample
2845int
2846f (void)
2847@{
2848 return 123;
2849@}
2850
2851int
2852g (void)
2853@{
2854 return f();
2855@}
2856@end smallexample
2857
6aa29e7b 2858@kindex --eh-frame-hdr
29063f8b 2859@kindex --no-eh-frame-hdr
6aa29e7b 2860@item --eh-frame-hdr
29063f8b
NC
2861@itemx --no-eh-frame-hdr
2862Request (@option{--eh-frame-hdr}) or suppress
2863(@option{--no-eh-frame-hdr}) the creation of @code{.eh_frame_hdr}
2864section and ELF @code{PT_GNU_EH_FRAME} segment header.
6aa29e7b 2865
e41b3a13
JJ
2866@kindex --ld-generated-unwind-info
2867@item --no-ld-generated-unwind-info
2868Request creation of @code{.eh_frame} unwind info for linker
2869generated code sections like PLT. This option is on by default
cf0e0a0b 2870if linker generated unwind info is supported. This option also
1bd123bd 2871controls the generation of @code{.sframe} stack trace info for linker
cf0e0a0b 2872generated code sections like PLT.
e41b3a13 2873
6c1439be
L
2874@kindex --enable-new-dtags
2875@kindex --disable-new-dtags
2876@item --enable-new-dtags
2877@itemx --disable-new-dtags
2878This linker can create the new dynamic tags in ELF. But the older ELF
2879systems may not understand them. If you specify
b1b00fcc
MF
2880@option{--enable-new-dtags}, the new dynamic tags will be created as needed
2881and older dynamic tags will be omitted.
ff5dcc92 2882If you specify @option{--disable-new-dtags}, no new dynamic tags will be
6c1439be
L
2883created. By default, the new dynamic tags are not created. Note that
2884those options are only available for ELF systems.
2885
2d643429 2886@kindex --hash-size=@var{number}
e185dd51 2887@item --hash-size=@var{number}
2d643429
NC
2888Set the default size of the linker's hash tables to a prime number
2889close to @var{number}. Increasing this value can reduce the length of
2890time it takes the linker to perform its tasks, at the expense of
2891increasing the linker's memory requirements. Similarly reducing this
2892value can reduce the memory requirements at the expense of speed.
2893
fdc90cb4
JJ
2894@kindex --hash-style=@var{style}
2895@item --hash-style=@var{style}
2896Set the type of linker's hash table(s). @var{style} can be either
2897@code{sysv} for classic ELF @code{.hash} section, @code{gnu} for
2898new style GNU @code{.gnu.hash} section or @code{both} for both
2899the classic ELF @code{.hash} and new style GNU @code{.gnu.hash}
c8455dc9
NC
2900hash tables. The default depends upon how the linker was configured,
2901but for most Linux based systems it will be @code{both}.
fdc90cb4 2902
0ce398f1
L
2903@kindex --compress-debug-sections=none
2904@kindex --compress-debug-sections=zlib
2905@kindex --compress-debug-sections=zlib-gnu
2906@kindex --compress-debug-sections=zlib-gabi
2cac01e3 2907@kindex --compress-debug-sections=zstd
0ce398f1
L
2908@item --compress-debug-sections=none
2909@itemx --compress-debug-sections=zlib
2910@itemx --compress-debug-sections=zlib-gnu
2911@itemx --compress-debug-sections=zlib-gabi
2cac01e3 2912@itemx --compress-debug-sections=zstd
9af89fba
NC
2913On ELF platforms, these options control how DWARF debug sections are
2914compressed using zlib.
2915
2916@option{--compress-debug-sections=none} doesn't compress DWARF debug
2917sections. @option{--compress-debug-sections=zlib-gnu} compresses
2918DWARF debug sections and renames them to begin with @samp{.zdebug}
2919instead of @samp{.debug}. @option{--compress-debug-sections=zlib-gabi}
2920also compresses DWARF debug sections, but rather than renaming them it
2921sets the SHF_COMPRESSED flag in the sections' headers.
2922
2923The @option{--compress-debug-sections=zlib} option is an alias for
2924@option{--compress-debug-sections=zlib-gabi}.
2925
2cac01e3
FS
2926@option{--compress-debug-sections=zstd} compresses DWARF debug sections using
2927zstd.
2928
9af89fba
NC
2929Note that this option overrides any compression in input debug
2930sections, so if a binary is linked with @option{--compress-debug-sections=none}
2931for example, then any compressed debug sections in input files will be
2932uncompressed before they are copied into the output binary.
2933
2934The default compression behaviour varies depending upon the target
2935involved and the configure options used to build the toolchain. The
2936default can be determined by examining the output from the linker's
2937@option{--help} option.
0ce398f1 2938
35835446
JR
2939@kindex --reduce-memory-overheads
2940@item --reduce-memory-overheads
2941This option reduces memory requirements at ld runtime, at the expense of
f2a8f148 2942linking speed. This was introduced to select the old O(n^2) algorithm
35835446 2943for link map file generation, rather than the new O(n) algorithm which uses
2d643429
NC
2944about 40% more memory for symbol storage.
2945
4f9c04f7 2946Another effect of the switch is to set the default hash table size to
2d643429 29471021, which again saves memory at the cost of lengthening the linker's
a85785bc 2948run time. This is not done however if the @option{--hash-size} switch
2d643429
NC
2949has been used.
2950
2951The @option{--reduce-memory-overheads} switch may be also be used to
2952enable other tradeoffs in future versions of the linker.
35835446 2953
a8dde0a2
L
2954@kindex --max-cache-size=@var{size}
2955@item --max-cache-size=@var{size}
2956@command{ld} normally caches the relocation information and symbol tables
2957of input files in memory with the unlimited size. This option sets the
2958maximum cache size to @var{size}.
2959
c0065db7
RM
2960@kindex --build-id
2961@kindex --build-id=@var{style}
2962@item --build-id
2963@itemx --build-id=@var{style}
61e2488c 2964Request the creation of a @code{.note.gnu.build-id} ELF note section
6033bf41 2965or a @code{.buildid} COFF section. The contents of the note are
61e2488c
JT
2966unique bits identifying this linked file. @var{style} can be
2967@code{uuid} to use 128 random bits, @code{sha1} to use a 160-bit
2968@sc{SHA1} hash on the normative parts of the output contents,
2969@code{md5} to use a 128-bit @sc{MD5} hash on the normative parts of
2970the output contents, or @code{0x@var{hexstring}} to use a chosen bit
2971string specified as an even number of hexadecimal digits (@code{-} and
2972@code{:} characters between digit pairs are ignored). If @var{style}
2973is omitted, @code{sha1} is used.
24382dca
RM
2974
2975The @code{md5} and @code{sha1} styles produces an identifier
2976that is always the same in an identical output file, but will be
2977unique among all nonidentical output files. It is not intended
2978to be compared as a checksum for the file's contents. A linked
2979file may be changed later by other tools, but the build ID bit
2980string identifying the original linked file does not change.
c0065db7
RM
2981
2982Passing @code{none} for @var{style} disables the setting from any
2983@code{--build-id} options earlier on the command line.
9e2bb0cb
LB
2984
2985@kindex --package-metadata=@var{JSON}
2986@item --package-metadata=@var{JSON}
2987Request the creation of a @code{.note.package} ELF note section. The
2988contents of the note are in JSON format, as per the package metadata
2989specification. For more information see:
2990https://systemd.io/ELF_PACKAGE_METADATA/
2991If the JSON argument is missing/empty then this will disable the
2992creation of the metadata note, if one had been enabled by an earlier
2993occurrence of the --package-metdata option.
2994If the linker has been built with libjansson, then the JSON string
2995will be validated.
252b5132
RH
2996@end table
2997
0285c67d
NC
2998@c man end
2999
36f63dca 3000@subsection Options Specific to i386 PE Targets
252b5132 3001
0285c67d
NC
3002@c man begin OPTIONS
3003
ff5dcc92 3004The i386 PE linker supports the @option{-shared} option, which causes
252b5132
RH
3005the output to be a dynamically linked library (DLL) instead of a
3006normal executable. You should name the output @code{*.dll} when you
3007use this option. In addition, the linker fully supports the standard
3008@code{*.def} files, which may be specified on the linker command line
3009like an object file (in fact, it should precede archives it exports
3010symbols from, to ensure that they get linked in, just like a normal
3011object file).
3012
3013In addition to the options common to all targets, the i386 PE linker
a05a5b64 3014support additional command-line options that are specific to the i386
252b5132
RH
3015PE target. Options that take values may be separated from their
3016values by either a space or an equals sign.
3017
ff5dcc92 3018@table @gcctabopt
252b5132
RH
3019
3020@kindex --add-stdcall-alias
3021@item --add-stdcall-alias
3022If given, symbols with a stdcall suffix (@@@var{nn}) will be exported
3023as-is and also with the suffix stripped.
bb10df36 3024[This option is specific to the i386 PE targeted port of the linker]
252b5132
RH
3025
3026@kindex --base-file
3027@item --base-file @var{file}
3028Use @var{file} as the name of a file in which to save the base
3029addresses of all the relocations needed for generating DLLs with
3030@file{dlltool}.
bb10df36 3031[This is an i386 PE specific option]
252b5132
RH
3032
3033@kindex --dll
3034@item --dll
3035Create a DLL instead of a regular executable. You may also use
ff5dcc92 3036@option{-shared} or specify a @code{LIBRARY} in a given @code{.def}
252b5132 3037file.
bb10df36 3038[This option is specific to the i386 PE targeted port of the linker]
252b5132 3039
88183869
DK
3040@kindex --enable-long-section-names
3041@kindex --disable-long-section-names
3042@item --enable-long-section-names
3043@itemx --disable-long-section-names
56e6cf80 3044The PE variants of the COFF object format add an extension that permits
88183869 3045the use of section names longer than eight characters, the normal limit
56e6cf80
NC
3046for COFF. By default, these names are only allowed in object files, as
3047fully-linked executable images do not carry the COFF string table required
88183869
DK
3048to support the longer names. As a GNU extension, it is possible to
3049allow their use in executable images as well, or to (probably pointlessly!)
3050disallow it in object files, by using these two options. Executable images
3051generated with these long section names are slightly non-standard, carrying
3052as they do a string table, and may generate confusing output when examined
9d5777a3
RM
3053with non-GNU PE-aware tools, such as file viewers and dumpers. However,
3054GDB relies on the use of PE long section names to find Dwarf-2 debug
3efd345c
DK
3055information sections in an executable image at runtime, and so if neither
3056option is specified on the command-line, @command{ld} will enable long
3057section names, overriding the default and technically correct behaviour,
3058when it finds the presence of debug information while linking an executable
3059image and not stripping symbols.
88183869
DK
3060[This option is valid for all PE targeted ports of the linker]
3061
252b5132
RH
3062@kindex --enable-stdcall-fixup
3063@kindex --disable-stdcall-fixup
3064@item --enable-stdcall-fixup
3065@itemx --disable-stdcall-fixup
3066If the link finds a symbol that it cannot resolve, it will attempt to
36f63dca 3067do ``fuzzy linking'' by looking for another defined symbol that differs
252b5132
RH
3068only in the format of the symbol name (cdecl vs stdcall) and will
3069resolve that symbol by linking to the match. For example, the
3070undefined symbol @code{_foo} might be linked to the function
3071@code{_foo@@12}, or the undefined symbol @code{_bar@@16} might be linked
3072to the function @code{_bar}. When the linker does this, it prints a
3073warning, since it normally should have failed to link, but sometimes
3074import libraries generated from third-party dlls may need this feature
ff5dcc92 3075to be usable. If you specify @option{--enable-stdcall-fixup}, this
252b5132 3076feature is fully enabled and warnings are not printed. If you specify
ff5dcc92 3077@option{--disable-stdcall-fixup}, this feature is disabled and such
252b5132 3078mismatches are considered to be errors.
bb10df36 3079[This option is specific to the i386 PE targeted port of the linker]
252b5132 3080
522f09cd
KT
3081@kindex --leading-underscore
3082@kindex --no-leading-underscore
3083@item --leading-underscore
3084@itemx --no-leading-underscore
3085For most targets default symbol-prefix is an underscore and is defined
3086in target's description. By this option it is possible to
3087disable/enable the default underscore symbol-prefix.
3088
252b5132
RH
3089@cindex DLLs, creating
3090@kindex --export-all-symbols
3091@item --export-all-symbols
3092If given, all global symbols in the objects used to build a DLL will
3093be exported by the DLL. Note that this is the default if there
3094otherwise wouldn't be any exported symbols. When symbols are
3095explicitly exported via DEF files or implicitly exported via function
3096attributes, the default is to not export anything else unless this
3097option is given. Note that the symbols @code{DllMain@@12},
ece2d90e 3098@code{DllEntryPoint@@0}, @code{DllMainCRTStartup@@12}, and
b044cda1 3099@code{impure_ptr} will not be automatically
ece2d90e
NC
3100exported. Also, symbols imported from other DLLs will not be
3101re-exported, nor will symbols specifying the DLL's internal layout
3102such as those beginning with @code{_head_} or ending with
3103@code{_iname}. In addition, no symbols from @code{libgcc},
b044cda1
CW
3104@code{libstd++}, @code{libmingw32}, or @code{crtX.o} will be exported.
3105Symbols whose names begin with @code{__rtti_} or @code{__builtin_} will
3106not be exported, to help with C++ DLLs. Finally, there is an
ece2d90e 3107extensive list of cygwin-private symbols that are not exported
b044cda1 3108(obviously, this applies on when building DLLs for cygwin targets).
ece2d90e 3109These cygwin-excludes are: @code{_cygwin_dll_entry@@12},
b044cda1 3110@code{_cygwin_crt0_common@@8}, @code{_cygwin_noncygwin_dll_entry@@12},
ece2d90e 3111@code{_fmode}, @code{_impure_ptr}, @code{cygwin_attach_dll},
b044cda1 3112@code{cygwin_premain0}, @code{cygwin_premain1}, @code{cygwin_premain2},
ece2d90e 3113@code{cygwin_premain3}, and @code{environ}.
bb10df36 3114[This option is specific to the i386 PE targeted port of the linker]
252b5132
RH
3115
3116@kindex --exclude-symbols
1d0a3c9c 3117@item --exclude-symbols @var{symbol},@var{symbol},...
252b5132
RH
3118Specifies a list of symbols which should not be automatically
3119exported. The symbol names may be delimited by commas or colons.
bb10df36 3120[This option is specific to the i386 PE targeted port of the linker]
252b5132 3121
2927aaca
NC
3122@kindex --exclude-all-symbols
3123@item --exclude-all-symbols
3124Specifies no symbols should be automatically exported.
3125[This option is specific to the i386 PE targeted port of the linker]
3126
252b5132
RH
3127@kindex --file-alignment
3128@item --file-alignment
3129Specify the file alignment. Sections in the file will always begin at
3130file offsets which are multiples of this number. This defaults to
3131512.
bb10df36 3132[This option is specific to the i386 PE targeted port of the linker]
252b5132
RH
3133
3134@cindex heap size
3135@kindex --heap
3136@item --heap @var{reserve}
3137@itemx --heap @var{reserve},@var{commit}
a00b50c5 3138Specify the number of bytes of memory to reserve (and optionally commit)
fe6d7d6a 3139to be used as heap for this program. The default is 1MB reserved, 4K
252b5132 3140committed.
bb10df36 3141[This option is specific to the i386 PE targeted port of the linker]
252b5132
RH
3142
3143@cindex image base
3144@kindex --image-base
3145@item --image-base @var{value}
3146Use @var{value} as the base address of your program or dll. This is
3147the lowest memory location that will be used when your program or dll
3148is loaded. To reduce the need to relocate and improve performance of
3149your dlls, each should have a unique base address and not overlap any
3150other dlls. The default is 0x400000 for executables, and 0x10000000
3151for dlls.
bb10df36 3152[This option is specific to the i386 PE targeted port of the linker]
252b5132
RH
3153
3154@kindex --kill-at
3155@item --kill-at
3156If given, the stdcall suffixes (@@@var{nn}) will be stripped from
3157symbols before they are exported.
bb10df36 3158[This option is specific to the i386 PE targeted port of the linker]
252b5132 3159
26d2d8a2
BF
3160@kindex --large-address-aware
3161@item --large-address-aware
b45619c0 3162If given, the appropriate bit in the ``Characteristics'' field of the COFF
26d2d8a2 3163header is set to indicate that this executable supports virtual addresses
b45619c0 3164greater than 2 gigabytes. This should be used in conjunction with the /3GB
26d2d8a2
BF
3165or /USERVA=@var{value} megabytes switch in the ``[operating systems]''
3166section of the BOOT.INI. Otherwise, this bit has no effect.
3167[This option is specific to PE targeted ports of the linker]
3168
f69a2f97
NC
3169@kindex --disable-large-address-aware
3170@item --disable-large-address-aware
3171Reverts the effect of a previous @samp{--large-address-aware} option.
3172This is useful if @samp{--large-address-aware} is always set by the compiler
3173driver (e.g. Cygwin gcc) and the executable does not support virtual
3174addresses greater than 2 gigabytes.
3175[This option is specific to PE targeted ports of the linker]
3176
252b5132
RH
3177@kindex --major-image-version
3178@item --major-image-version @var{value}
36f63dca 3179Sets the major number of the ``image version''. Defaults to 1.
bb10df36 3180[This option is specific to the i386 PE targeted port of the linker]
252b5132
RH
3181
3182@kindex --major-os-version
3183@item --major-os-version @var{value}
36f63dca 3184Sets the major number of the ``os version''. Defaults to 4.
bb10df36 3185[This option is specific to the i386 PE targeted port of the linker]
252b5132
RH
3186
3187@kindex --major-subsystem-version
3188@item --major-subsystem-version @var{value}
36f63dca 3189Sets the major number of the ``subsystem version''. Defaults to 4.
bb10df36 3190[This option is specific to the i386 PE targeted port of the linker]
252b5132
RH
3191
3192@kindex --minor-image-version
3193@item --minor-image-version @var{value}
36f63dca 3194Sets the minor number of the ``image version''. Defaults to 0.
bb10df36 3195[This option is specific to the i386 PE targeted port of the linker]
252b5132
RH
3196
3197@kindex --minor-os-version
3198@item --minor-os-version @var{value}
36f63dca 3199Sets the minor number of the ``os version''. Defaults to 0.
bb10df36 3200[This option is specific to the i386 PE targeted port of the linker]
252b5132
RH
3201
3202@kindex --minor-subsystem-version
3203@item --minor-subsystem-version @var{value}
36f63dca 3204Sets the minor number of the ``subsystem version''. Defaults to 0.
bb10df36 3205[This option is specific to the i386 PE targeted port of the linker]
252b5132
RH
3206
3207@cindex DEF files, creating
3208@cindex DLLs, creating
3209@kindex --output-def
3210@item --output-def @var{file}
3211The linker will create the file @var{file} which will contain a DEF
3212file corresponding to the DLL the linker is generating. This DEF file
3213(which should be called @code{*.def}) may be used to create an import
3214library with @code{dlltool} or may be used as a reference to
3215automatically or implicitly exported symbols.
bb10df36 3216[This option is specific to the i386 PE targeted port of the linker]
252b5132 3217
b044cda1 3218@cindex DLLs, creating
b044cda1
CW
3219@kindex --enable-auto-image-base
3220@item --enable-auto-image-base
d0e6d77b
CF
3221@itemx --enable-auto-image-base=@var{value}
3222Automatically choose the image base for DLLs, optionally starting with base
3223@var{value}, unless one is specified using the @code{--image-base} argument.
3224By using a hash generated from the dllname to create unique image bases
3225for each DLL, in-memory collisions and relocations which can delay program
3226execution are avoided.
bb10df36 3227[This option is specific to the i386 PE targeted port of the linker]
b044cda1
CW
3228
3229@kindex --disable-auto-image-base
3230@item --disable-auto-image-base
3231Do not automatically generate a unique image base. If there is no
3232user-specified image base (@code{--image-base}) then use the platform
3233default.
bb10df36 3234[This option is specific to the i386 PE targeted port of the linker]
b044cda1
CW
3235
3236@cindex DLLs, linking to
3237@kindex --dll-search-prefix
3238@item --dll-search-prefix @var{string}
489d0400 3239When linking dynamically to a dll without an import library,
ece2d90e 3240search for @code{<string><basename>.dll} in preference to
560e09e9 3241@code{lib<basename>.dll}. This behaviour allows easy distinction
b044cda1
CW
3242between DLLs built for the various "subplatforms": native, cygwin,
3243uwin, pw, etc. For instance, cygwin DLLs typically use
ece2d90e 3244@code{--dll-search-prefix=cyg}.
bb10df36 3245[This option is specific to the i386 PE targeted port of the linker]
b044cda1
CW
3246
3247@kindex --enable-auto-import
3248@item --enable-auto-import
ece2d90e 3249Do sophisticated linking of @code{_symbol} to @code{__imp__symbol} for
317ff008
EB
3250DATA imports from DLLs, thus making it possible to bypass the dllimport
3251mechanism on the user side and to reference unmangled symbol names.
3252[This option is specific to the i386 PE targeted port of the linker]
3253
3254The following remarks pertain to the original implementation of the
3255feature and are obsolete nowadays for Cygwin and MinGW targets.
3256
3257Note: Use of the 'auto-import' extension will cause the text section
3258of the image file to be made writable. This does not conform to the
3259PE-COFF format specification published by Microsoft.
4d8907ac 3260
e2a83dd0
NC
3261Note - use of the 'auto-import' extension will also cause read only
3262data which would normally be placed into the .rdata section to be
3263placed into the .data section instead. This is in order to work
3264around a problem with consts that is described here:
3265http://www.cygwin.com/ml/cygwin/2004-09/msg01101.html
3266
4d8907ac
DS
3267Using 'auto-import' generally will 'just work' -- but sometimes you may
3268see this message:
0d888aac 3269
ece2d90e 3270"variable '<var>' can't be auto-imported. Please read the
0d888aac
CW
3271documentation for ld's @code{--enable-auto-import} for details."
3272
ece2d90e
NC
3273This message occurs when some (sub)expression accesses an address
3274ultimately given by the sum of two constants (Win32 import tables only
c0065db7
RM
3275allow one). Instances where this may occur include accesses to member
3276fields of struct variables imported from a DLL, as well as using a
3277constant index into an array variable imported from a DLL. Any
2f8d8971
NC
3278multiword variable (arrays, structs, long long, etc) may trigger
3279this error condition. However, regardless of the exact data type
3280of the offending exported variable, ld will always detect it, issue
3281the warning, and exit.
3282
3283There are several ways to address this difficulty, regardless of the
3284data type of the exported variable:
0d888aac 3285
2fa9fc65
NC
3286One way is to use --enable-runtime-pseudo-reloc switch. This leaves the task
3287of adjusting references in your client code for runtime environment, so
560e09e9 3288this method works only when runtime environment supports this feature.
2fa9fc65 3289
c0065db7
RM
3290A second solution is to force one of the 'constants' to be a variable --
3291that is, unknown and un-optimizable at compile time. For arrays,
3292there are two possibilities: a) make the indexee (the array's address)
0d888aac
CW
3293a variable, or b) make the 'constant' index a variable. Thus:
3294
3295@example
3296extern type extern_array[];
c0065db7 3297extern_array[1] -->
0d888aac
CW
3298 @{ volatile type *t=extern_array; t[1] @}
3299@end example
3300
3301or
3302
3303@example
3304extern type extern_array[];
c0065db7 3305extern_array[1] -->
0d888aac
CW
3306 @{ volatile int t=1; extern_array[t] @}
3307@end example
3308
c0065db7 3309For structs (and most other multiword data types) the only option
2f8d8971 3310is to make the struct itself (or the long long, or the ...) variable:
0d888aac
CW
3311
3312@example
3313extern struct s extern_struct;
c0065db7 3314extern_struct.field -->
0d888aac
CW
3315 @{ volatile struct s *t=&extern_struct; t->field @}
3316@end example
3317
c406afaf
NC
3318or
3319
3320@example
3321extern long long extern_ll;
3322extern_ll -->
3323 @{ volatile long long * local_ll=&extern_ll; *local_ll @}
3324@end example
3325
2fa9fc65 3326A third method of dealing with this difficulty is to abandon
c0065db7 3327'auto-import' for the offending symbol and mark it with
11e7fd74 3328@code{__declspec(dllimport)}. However, in practice that
0d888aac 3329requires using compile-time #defines to indicate whether you are
c0065db7
RM
3330building a DLL, building client code that will link to the DLL, or
3331merely building/linking to a static library. In making the choice
3332between the various methods of resolving the 'direct address with
0d888aac
CW
3333constant offset' problem, you should consider typical real-world usage:
3334
3335Original:
3336@example
3337--foo.h
3338extern int arr[];
3339--foo.c
3340#include "foo.h"
3341void main(int argc, char **argv)@{
3342 printf("%d\n",arr[1]);
3343@}
3344@end example
3345
3346Solution 1:
3347@example
3348--foo.h
3349extern int arr[];
3350--foo.c
3351#include "foo.h"
3352void main(int argc, char **argv)@{
3353 /* This workaround is for win32 and cygwin; do not "optimize" */
3354 volatile int *parr = arr;
3355 printf("%d\n",parr[1]);
3356@}
3357@end example
3358
3359Solution 2:
3360@example
3361--foo.h
3362/* Note: auto-export is assumed (no __declspec(dllexport)) */
3363#if (defined(_WIN32) || defined(__CYGWIN__)) && \
3364 !(defined(FOO_BUILD_DLL) || defined(FOO_STATIC))
3365#define FOO_IMPORT __declspec(dllimport)
3366#else
3367#define FOO_IMPORT
3368#endif
3369extern FOO_IMPORT int arr[];
3370--foo.c
3371#include "foo.h"
3372void main(int argc, char **argv)@{
3373 printf("%d\n",arr[1]);
3374@}
3375@end example
3376
c0065db7 3377A fourth way to avoid this problem is to re-code your
0d888aac
CW
3378library to use a functional interface rather than a data interface
3379for the offending variables (e.g. set_foo() and get_foo() accessor
3380functions).
b044cda1
CW
3381
3382@kindex --disable-auto-import
3383@item --disable-auto-import
c0065db7 3384Do not attempt to do sophisticated linking of @code{_symbol} to
b044cda1 3385@code{__imp__symbol} for DATA imports from DLLs.
bb10df36 3386[This option is specific to the i386 PE targeted port of the linker]
b044cda1 3387
2fa9fc65
NC
3388@kindex --enable-runtime-pseudo-reloc
3389@item --enable-runtime-pseudo-reloc
3390If your code contains expressions described in --enable-auto-import section,
3391that is, DATA imports from DLL with non-zero offset, this switch will create
3392a vector of 'runtime pseudo relocations' which can be used by runtime
c0065db7 3393environment to adjust references to such data in your client code.
bb10df36 3394[This option is specific to the i386 PE targeted port of the linker]
2fa9fc65
NC
3395
3396@kindex --disable-runtime-pseudo-reloc
3397@item --disable-runtime-pseudo-reloc
317ff008 3398Do not create pseudo relocations for non-zero offset DATA imports from DLLs.
bb10df36 3399[This option is specific to the i386 PE targeted port of the linker]
2fa9fc65 3400
b044cda1
CW
3401@kindex --enable-extra-pe-debug
3402@item --enable-extra-pe-debug
3403Show additional debug info related to auto-import symbol thunking.
bb10df36 3404[This option is specific to the i386 PE targeted port of the linker]
b044cda1 3405
252b5132
RH
3406@kindex --section-alignment
3407@item --section-alignment
3408Sets the section alignment. Sections in memory will always begin at
3409addresses which are a multiple of this number. Defaults to 0x1000.
bb10df36 3410[This option is specific to the i386 PE targeted port of the linker]
252b5132
RH
3411
3412@cindex stack size
3413@kindex --stack
3414@item --stack @var{reserve}
3415@itemx --stack @var{reserve},@var{commit}
a00b50c5 3416Specify the number of bytes of memory to reserve (and optionally commit)
fe6d7d6a 3417to be used as stack for this program. The default is 2MB reserved, 4K
252b5132 3418committed.
bb10df36 3419[This option is specific to the i386 PE targeted port of the linker]
252b5132
RH
3420
3421@kindex --subsystem
3422@item --subsystem @var{which}
3423@itemx --subsystem @var{which}:@var{major}
3424@itemx --subsystem @var{which}:@var{major}.@var{minor}
3425Specifies the subsystem under which your program will execute. The
3426legal values for @var{which} are @code{native}, @code{windows},
33f362e1
NC
3427@code{console}, @code{posix}, and @code{xbox}. You may optionally set
3428the subsystem version also. Numeric values are also accepted for
3429@var{which}.
bb10df36 3430[This option is specific to the i386 PE targeted port of the linker]
252b5132 3431
2f563b51
DK
3432The following options set flags in the @code{DllCharacteristics} field
3433of the PE file header:
3434[These options are specific to PE targeted ports of the linker]
3435
2d5c3743
NC
3436@kindex --high-entropy-va
3437@item --high-entropy-va
514b4e19 3438@itemx --disable-high-entropy-va
2d5c3743 3439Image is compatible with 64-bit address space layout randomization
514b4e19
JD
3440(ASLR). This option is enabled by default for 64-bit PE images.
3441
dc9bd8c9
HD
3442This option also implies @option{--dynamicbase} and
3443@option{--enable-reloc-section}.
2d5c3743 3444
2f563b51
DK
3445@kindex --dynamicbase
3446@item --dynamicbase
514b4e19 3447@itemx --disable-dynamicbase
2f563b51
DK
3448The image base address may be relocated using address space layout
3449randomization (ASLR). This feature was introduced with MS Windows
514b4e19
JD
3450Vista for i386 PE targets. This option is enabled by default but
3451can be disabled via the @option{--disable-dynamicbase} option.
dc9bd8c9 3452This option also implies @option{--enable-reloc-section}.
2f563b51
DK
3453
3454@kindex --forceinteg
3455@item --forceinteg
514b4e19
JD
3456@itemx --disable-forceinteg
3457Code integrity checks are enforced. This option is disabled by
3458default.
2f563b51
DK
3459
3460@kindex --nxcompat
3461@item --nxcompat
514b4e19 3462@item --disable-nxcompat
2f563b51 3463The image is compatible with the Data Execution Prevention.
514b4e19
JD
3464This feature was introduced with MS Windows XP SP2 for i386 PE
3465targets. The option is enabled by default.
2f563b51
DK
3466
3467@kindex --no-isolation
3468@item --no-isolation
514b4e19 3469@itemx --disable-no-isolation
2f563b51 3470Although the image understands isolation, do not isolate the image.
514b4e19 3471This option is disabled by default.
2f563b51
DK
3472
3473@kindex --no-seh
3474@item --no-seh
514b4e19 3475@itemx --disable-no-seh
2f563b51 3476The image does not use SEH. No SE handler may be called from
514b4e19 3477this image. This option is disabled by default.
2f563b51
DK
3478
3479@kindex --no-bind
3480@item --no-bind
514b4e19
JD
3481@itemx --disable-no-bind
3482Do not bind this image. This option is disabled by default.
2f563b51
DK
3483
3484@kindex --wdmdriver
3485@item --wdmdriver
514b4e19
JD
3486@itemx --disable-wdmdriver
3487The driver uses the MS Windows Driver Model. This option is disabled
3488by default.
9d5777a3 3489
2f563b51
DK
3490@kindex --tsaware
3491@item --tsaware
514b4e19
JD
3492@itemx --disable-tsaware
3493The image is Terminal Server aware. This option is disabled by
3494default.
2f563b51 3495
0cb112f7
CF
3496@kindex --insert-timestamp
3497@item --insert-timestamp
eeb14e5a
LZ
3498@itemx --no-insert-timestamp
3499Insert a real timestamp into the image. This is the default behaviour
3500as it matches legacy code and it means that the image will work with
3501other, proprietary tools. The problem with this default is that it
56e6cf80 3502will result in slightly different images being produced each time the
eeb14e5a
LZ
3503same sources are linked. The option @option{--no-insert-timestamp}
3504can be used to insert a zero value for the timestamp, this ensuring
56e6cf80 3505that binaries produced from identical sources will compare
eeb14e5a 3506identically.
dc9bd8c9
HD
3507
3508@kindex --enable-reloc-section
3509@item --enable-reloc-section
514b4e19 3510@itemx --disable-reloc-section
dc9bd8c9
HD
3511Create the base relocation table, which is necessary if the image
3512is loaded at a different image base than specified in the PE header.
514b4e19 3513This option is enabled by default.
252b5132
RH
3514@end table
3515
0285c67d
NC
3516@c man end
3517
ac145307
BS
3518@ifset C6X
3519@subsection Options specific to C6X uClinux targets
3520
3521@c man begin OPTIONS
3522
3523The C6X uClinux target uses a binary format called DSBT to support shared
3524libraries. Each shared library in the system needs to have a unique index;
3525all executables use an index of 0.
3526
3527@table @gcctabopt
3528
3529@kindex --dsbt-size
3530@item --dsbt-size @var{size}
56e6cf80 3531This option sets the number of entries in the DSBT of the current executable
ac145307
BS
3532or shared library to @var{size}. The default is to create a table with 64
3533entries.
3534
3535@kindex --dsbt-index
3536@item --dsbt-index @var{index}
3537This option sets the DSBT index of the current executable or shared library
3538to @var{index}. The default is 0, which is appropriate for generating
3539executables. If a shared library is generated with a DSBT index of 0, the
3540@code{R_C6000_DSBT_INDEX} relocs are copied into the output file.
3541
fbd9ad90
PB
3542@kindex --no-merge-exidx-entries
3543The @samp{--no-merge-exidx-entries} switch disables the merging of adjacent
3544exidx entries in frame unwind info.
3545
ac145307
BS
3546@end table
3547
3548@c man end
3549@end ifset
3550
b8891f8d
AJ
3551@ifset CSKY
3552@subsection Options specific to C-SKY targets
3553
3554@c man begin OPTIONS
3555
3556@table @gcctabopt
3557
3558@kindex --branch-stub on C-SKY
3559@item --branch-stub
3560This option enables linker branch relaxation by inserting branch stub
3561sections when needed to extend the range of branches. This option is
3562usually not required since C-SKY supports branch and call instructions that
3563can access the full memory range and branch relaxation is normally handled by
3564the compiler or assembler.
3565
3566@kindex --stub-group-size on C-SKY
3567@item --stub-group-size=@var{N}
3568This option allows finer control of linker branch stub creation.
3569It sets the maximum size of a group of input sections that can
3570be handled by one stub section. A negative value of @var{N} locates
3571stub sections after their branches, while a positive value allows stub
3572sections to appear either before or after the branches. Values of
3573@samp{1} or @samp{-1} indicate that the
3574linker should choose suitable defaults.
3575
3576@end table
3577
3578@c man end
3579@end ifset
3580
93fd0973
SC
3581@ifset M68HC11
3582@subsection Options specific to Motorola 68HC11 and 68HC12 targets
3583
3584@c man begin OPTIONS
3585
3586The 68HC11 and 68HC12 linkers support specific options to control the
3587memory bank switching mapping and trampoline code generation.
3588
3589@table @gcctabopt
3590
3591@kindex --no-trampoline
3592@item --no-trampoline
3593This option disables the generation of trampoline. By default a trampoline
3594is generated for each far function which is called using a @code{jsr}
3595instruction (this happens when a pointer to a far function is taken).
3596
3597@kindex --bank-window
3598@item --bank-window @var{name}
3599This option indicates to the linker the name of the memory region in
3600the @samp{MEMORY} specification that describes the memory bank window.
3601The definition of such region is then used by the linker to compute
3602paging and addresses within the memory window.
3603
3604@end table
3605
3606@c man end
3607@end ifset
3608
7fb9f789
NC
3609@ifset M68K
3610@subsection Options specific to Motorola 68K target
3611
3612@c man begin OPTIONS
3613
3614The following options are supported to control handling of GOT generation
3615when linking for 68K targets.
3616
3617@table @gcctabopt
3618
3619@kindex --got
3620@item --got=@var{type}
3621This option tells the linker which GOT generation scheme to use.
3622@var{type} should be one of @samp{single}, @samp{negative},
3623@samp{multigot} or @samp{target}. For more information refer to the
3624Info entry for @file{ld}.
3625
3626@end table
3627
3628@c man end
3629@end ifset
3630
833794fc
MR
3631@ifset MIPS
3632@subsection Options specific to MIPS targets
3633
3634@c man begin OPTIONS
3635
3636The following options are supported to control microMIPS instruction
8b10b0b3
MR
3637generation and branch relocation checks for ISA mode transitions when
3638linking for MIPS targets.
833794fc
MR
3639
3640@table @gcctabopt
3641
3642@kindex --insn32
3643@item --insn32
3644@kindex --no-insn32
3645@itemx --no-insn32
3646These options control the choice of microMIPS instructions used in code
3647generated by the linker, such as that in the PLT or lazy binding stubs,
3648or in relaxation. If @samp{--insn32} is used, then the linker only uses
364932-bit instruction encodings. By default or if @samp{--no-insn32} is
3650used, all instruction encodings are used, including 16-bit ones where
3651possible.
3652
8b10b0b3
MR
3653@kindex --ignore-branch-isa
3654@item --ignore-branch-isa
3655@kindex --no-ignore-branch-isa
3656@itemx --no-ignore-branch-isa
3657These options control branch relocation checks for invalid ISA mode
3658transitions. If @samp{--ignore-branch-isa} is used, then the linker
3659accepts any branch relocations and any ISA mode transition required
3660is lost in relocation calculation, except for some cases of @code{BAL}
3661instructions which meet relaxation conditions and are converted to
3662equivalent @code{JALX} instructions as the associated relocation is
3663calculated. By default or if @samp{--no-ignore-branch-isa} is used
3664a check is made causing the loss of an ISA mode transition to produce
3665an error.
3666
3734320d
MF
3667@kindex --compact-branches
3668@item --compact-branches
3669@kindex --no-compact-branches
fa1477dc 3670@itemx --no-compact-branches
3734320d
MF
3671These options control the generation of compact instructions by the linker
3672in the PLT entries for MIPS R6.
3673
833794fc
MR
3674@end table
3675
3676@c man end
3677@end ifset
3678
fa1477dc
SC
3679
3680@ifset PDP11
3681@subsection Options specific to PDP11 targets
3682
3683@c man begin OPTIONS
3684
3685For the pdp11-aout target, three variants of the output format can be
3686produced as selected by the following options. The default variant
3687for pdp11-aout is the @samp{--omagic} option, whereas for other
3688targets @samp{--nmagic} is the default. The @samp{--imagic} option is
3689defined only for the pdp11-aout target, while the others are described
3690here as they apply to the pdp11-aout target.
3691
3692@table @gcctabopt
3693
3694@kindex -N
3695@item -N
3696@kindex --omagic
3697@itemx --omagic
3698
3699Mark the output as @code{OMAGIC} (0407) in the @file{a.out} header to
3700indicate that the text segment is not to be write-protected and
3701shared. Since the text and data sections are both readable and
3702writable, the data section is allocated immediately contiguous after
3703the text segment. This is the oldest format for PDP11 executable
3704programs and is the default for @command{ld} on PDP11 Unix systems
3705from the beginning through 2.11BSD.
3706
3707@kindex -n
3708@item -n
3709@kindex --nmagic
3710@itemx --nmagic
3711
3712Mark the output as @code{NMAGIC} (0410) in the @file{a.out} header to
3713indicate that when the output file is executed, the text portion will
3714be read-only and shareable among all processes executing the same
3715file. This involves moving the data areas up to the first possible 8K
3716byte page boundary following the end of the text. This option creates
3717a @emph{pure executable} format.
3718
3719@kindex -z
3720@item -z
3721@kindex --imagic
3722@itemx --imagic
3723
3724Mark the output as @code{IMAGIC} (0411) in the @file{a.out} header to
3725indicate that when the output file is executed, the program text and
3726data areas will be loaded into separate address spaces using the split
3727instruction and data space feature of the memory management unit in
3728larger models of the PDP11. This doubles the address space available
3729to the program. The text segment is again pure, write-protected, and
3730shareable. The only difference in the output format between this
3731option and the others, besides the magic number, is that both the text
3732and data sections start at location 0. The @samp{-z} option selected
3733this format in 2.11BSD. This option creates a @emph{separate
3734executable} format.
3735
3736@kindex --no-omagic
3737@item --no-omagic
3738
3739Equivalent to @samp{--nmagic} for pdp11-aout.
3740
3741@end table
3742
3743@c man end
3744@end ifset
3745
252b5132
RH
3746@ifset UsesEnvVars
3747@node Environment
3748@section Environment Variables
3749
0285c67d
NC
3750@c man begin ENVIRONMENT
3751
560e09e9 3752You can change the behaviour of @command{ld} with the environment variables
36f63dca
NC
3753@ifclear SingleFormat
3754@code{GNUTARGET},
3755@end ifclear
3756@code{LDEMULATION} and @code{COLLECT_NO_DEMANGLE}.
252b5132 3757
36f63dca 3758@ifclear SingleFormat
252b5132
RH
3759@kindex GNUTARGET
3760@cindex default input format
3761@code{GNUTARGET} determines the input-file object format if you don't
3762use @samp{-b} (or its synonym @samp{--format}). Its value should be one
3763of the BFD names for an input format (@pxref{BFD}). If there is no
ff5dcc92 3764@code{GNUTARGET} in the environment, @command{ld} uses the natural format
252b5132
RH
3765of the target. If @code{GNUTARGET} is set to @code{default} then BFD
3766attempts to discover the input format by examining binary input files;
3767this method often succeeds, but there are potential ambiguities, since
3768there is no method of ensuring that the magic number used to specify
3769object-file formats is unique. However, the configuration procedure for
3770BFD on each system places the conventional format for that system first
3771in the search-list, so ambiguities are resolved in favor of convention.
36f63dca 3772@end ifclear
252b5132
RH
3773
3774@kindex LDEMULATION
3775@cindex default emulation
3776@cindex emulation, default
3777@code{LDEMULATION} determines the default emulation if you don't use the
3778@samp{-m} option. The emulation can affect various aspects of linker
3779behaviour, particularly the default linker script. You can list the
3780available emulations with the @samp{--verbose} or @samp{-V} options. If
3781the @samp{-m} option is not used, and the @code{LDEMULATION} environment
3782variable is not defined, the default emulation depends upon how the
3783linker was configured.
252b5132
RH
3784
3785@kindex COLLECT_NO_DEMANGLE
3786@cindex demangling, default
3787Normally, the linker will default to demangling symbols. However, if
3788@code{COLLECT_NO_DEMANGLE} is set in the environment, then it will
3789default to not demangling symbols. This environment variable is used in
3790a similar fashion by the @code{gcc} linker wrapper program. The default
3791may be overridden by the @samp{--demangle} and @samp{--no-demangle}
3792options.
3793
0285c67d
NC
3794@c man end
3795@end ifset
3796
252b5132
RH
3797@node Scripts
3798@chapter Linker Scripts
3799
3800@cindex scripts
3801@cindex linker scripts
3802@cindex command files
3803Every link is controlled by a @dfn{linker script}. This script is
3804written in the linker command language.
3805
3806The main purpose of the linker script is to describe how the sections in
3807the input files should be mapped into the output file, and to control
3808the memory layout of the output file. Most linker scripts do nothing
3809more than this. However, when necessary, the linker script can also
3810direct the linker to perform many other operations, using the commands
3811described below.
3812
3813The linker always uses a linker script. If you do not supply one
3814yourself, the linker will use a default script that is compiled into the
a05a5b64
TP
3815linker executable. You can use the @samp{--verbose} command-line option
3816to display the default linker script. Certain command-line options,
252b5132
RH
3817such as @samp{-r} or @samp{-N}, will affect the default linker script.
3818
3819You may supply your own linker script by using the @samp{-T} command
3820line option. When you do this, your linker script will replace the
3821default linker script.
3822
3823You may also use linker scripts implicitly by naming them as input files
3824to the linker, as though they were files to be linked. @xref{Implicit
3825Linker Scripts}.
3826
3827@menu
3828* Basic Script Concepts:: Basic Linker Script Concepts
3829* Script Format:: Linker Script Format
3830* Simple Example:: Simple Linker Script Example
3831* Simple Commands:: Simple Linker Script Commands
3832* Assignments:: Assigning Values to Symbols
3833* SECTIONS:: SECTIONS Command
3834* MEMORY:: MEMORY Command
3835* PHDRS:: PHDRS Command
3836* VERSION:: VERSION Command
3837* Expressions:: Expressions in Linker Scripts
3838* Implicit Linker Scripts:: Implicit Linker Scripts
3839@end menu
3840
3841@node Basic Script Concepts
3842@section Basic Linker Script Concepts
3843@cindex linker script concepts
3844We need to define some basic concepts and vocabulary in order to
3845describe the linker script language.
3846
3847The linker combines input files into a single output file. The output
3848file and each input file are in a special data format known as an
3849@dfn{object file format}. Each file is called an @dfn{object file}.
3850The output file is often called an @dfn{executable}, but for our
3851purposes we will also call it an object file. Each object file has,
3852among other things, a list of @dfn{sections}. We sometimes refer to a
3853section in an input file as an @dfn{input section}; similarly, a section
3854in the output file is an @dfn{output section}.
3855
3856Each section in an object file has a name and a size. Most sections
3857also have an associated block of data, known as the @dfn{section
56dd11f0 3858contents}. A section may be marked as @dfn{loadable}, which means that
252b5132
RH
3859the contents should be loaded into memory when the output file is run.
3860A section with no contents may be @dfn{allocatable}, which means that an
3861area in memory should be set aside, but nothing in particular should be
3862loaded there (in some cases this memory must be zeroed out). A section
3863which is neither loadable nor allocatable typically contains some sort
3864of debugging information.
3865
3866Every loadable or allocatable output section has two addresses. The
3867first is the @dfn{VMA}, or virtual memory address. This is the address
3868the section will have when the output file is run. The second is the
3869@dfn{LMA}, or load memory address. This is the address at which the
3870section will be loaded. In most cases the two addresses will be the
3871same. An example of when they might be different is when a data section
3872is loaded into ROM, and then copied into RAM when the program starts up
3873(this technique is often used to initialize global variables in a ROM
3874based system). In this case the ROM address would be the LMA, and the
3875RAM address would be the VMA.
3876
3877You can see the sections in an object file by using the @code{objdump}
3878program with the @samp{-h} option.
3879
3880Every object file also has a list of @dfn{symbols}, known as the
3881@dfn{symbol table}. A symbol may be defined or undefined. Each symbol
3882has a name, and each defined symbol has an address, among other
3883information. If you compile a C or C++ program into an object file, you
3884will get a defined symbol for every defined function and global or
3885static variable. Every undefined function or global variable which is
3886referenced in the input file will become an undefined symbol.
3887
3888You can see the symbols in an object file by using the @code{nm}
3889program, or by using the @code{objdump} program with the @samp{-t}
3890option.
3891
3892@node Script Format
3893@section Linker Script Format
3894@cindex linker script format
3895Linker scripts are text files.
3896
3897You write a linker script as a series of commands. Each command is
3898either a keyword, possibly followed by arguments, or an assignment to a
3899symbol. You may separate commands using semicolons. Whitespace is
3900generally ignored.
3901
3902Strings such as file or format names can normally be entered directly.
3903If the file name contains a character such as a comma which would
3904otherwise serve to separate file names, you may put the file name in
3905double quotes. There is no way to use a double quote character in a
3906file name.
3907
3908You may include comments in linker scripts just as in C, delimited by
3909@samp{/*} and @samp{*/}. As in C, comments are syntactically equivalent
3910to whitespace.
3911
3912@node Simple Example
3913@section Simple Linker Script Example
3914@cindex linker script example
3915@cindex example of linker script
3916Many linker scripts are fairly simple.
3917
3918The simplest possible linker script has just one command:
3919@samp{SECTIONS}. You use the @samp{SECTIONS} command to describe the
3920memory layout of the output file.
3921
3922The @samp{SECTIONS} command is a powerful command. Here we will
3923describe a simple use of it. Let's assume your program consists only of
3924code, initialized data, and uninitialized data. These will be in the
3925@samp{.text}, @samp{.data}, and @samp{.bss} sections, respectively.
3926Let's assume further that these are the only sections which appear in
3927your input files.
3928
3929For this example, let's say that the code should be loaded at address
39300x10000, and that the data should start at address 0x8000000. Here is a
3931linker script which will do that:
3932@smallexample
3933SECTIONS
3934@{
3935 . = 0x10000;
3936 .text : @{ *(.text) @}
3937 . = 0x8000000;
3938 .data : @{ *(.data) @}
3939 .bss : @{ *(.bss) @}
3940@}
3941@end smallexample
3942
3943You write the @samp{SECTIONS} command as the keyword @samp{SECTIONS},
3944followed by a series of symbol assignments and output section
3945descriptions enclosed in curly braces.
3946
252b5132
RH
3947The first line inside the @samp{SECTIONS} command of the above example
3948sets the value of the special symbol @samp{.}, which is the location
3949counter. If you do not specify the address of an output section in some
3950other way (other ways are described later), the address is set from the
3951current value of the location counter. The location counter is then
3952incremented by the size of the output section. At the start of the
3953@samp{SECTIONS} command, the location counter has the value @samp{0}.
3954
3955The second line defines an output section, @samp{.text}. The colon is
3956required syntax which may be ignored for now. Within the curly braces
3957after the output section name, you list the names of the input sections
3958which should be placed into this output section. The @samp{*} is a
3959wildcard which matches any file name. The expression @samp{*(.text)}
3960means all @samp{.text} input sections in all input files.
3961
3962Since the location counter is @samp{0x10000} when the output section
3963@samp{.text} is defined, the linker will set the address of the
3964@samp{.text} section in the output file to be @samp{0x10000}.
3965
3966The remaining lines define the @samp{.data} and @samp{.bss} sections in
3967the output file. The linker will place the @samp{.data} output section
3968at address @samp{0x8000000}. After the linker places the @samp{.data}
3969output section, the value of the location counter will be
3970@samp{0x8000000} plus the size of the @samp{.data} output section. The
3971effect is that the linker will place the @samp{.bss} output section
58434bc1 3972immediately after the @samp{.data} output section in memory.
252b5132
RH
3973
3974The linker will ensure that each output section has the required
3975alignment, by increasing the location counter if necessary. In this
3976example, the specified addresses for the @samp{.text} and @samp{.data}
3977sections will probably satisfy any alignment constraints, but the linker
3978may have to create a small gap between the @samp{.data} and @samp{.bss}
3979sections.
3980
3981That's it! That's a simple and complete linker script.
3982
3983@node Simple Commands
3984@section Simple Linker Script Commands
3985@cindex linker script simple commands
3986In this section we describe the simple linker script commands.
3987
3988@menu
3989* Entry Point:: Setting the entry point
3990* File Commands:: Commands dealing with files
3991@ifclear SingleFormat
3992* Format Commands:: Commands dealing with object file formats
3993@end ifclear
3994
4a93e180 3995* REGION_ALIAS:: Assign alias names to memory regions
252b5132
RH
3996* Miscellaneous Commands:: Other linker script commands
3997@end menu
3998
3999@node Entry Point
36f63dca 4000@subsection Setting the Entry Point
252b5132
RH
4001@kindex ENTRY(@var{symbol})
4002@cindex start of execution
4003@cindex first instruction
4004@cindex entry point
4005The first instruction to execute in a program is called the @dfn{entry
4006point}. You can use the @code{ENTRY} linker script command to set the
4007entry point. The argument is a symbol name:
4008@smallexample
4009ENTRY(@var{symbol})
4010@end smallexample
4011
4012There are several ways to set the entry point. The linker will set the
4013entry point by trying each of the following methods in order, and
4014stopping when one of them succeeds:
4015@itemize @bullet
a1ab1d2a 4016@item
252b5132 4017the @samp{-e} @var{entry} command-line option;
a1ab1d2a 4018@item
252b5132 4019the @code{ENTRY(@var{symbol})} command in a linker script;
a1ab1d2a 4020@item
a094d01f 4021the value of a target-specific symbol, if it is defined; For many
8a758655 4022targets this is @code{start}, but PE- and BeOS-based systems for example
3ab904c4 4023check a list of possible entry symbols, matching the first one found.
a1ab1d2a 4024@item
5226a6a8
NC
4025the address of the first byte of the code section, if present and an
4026executable is being created - the code section is usually
5c14cc55 4027@samp{.text}, but can be something else;
a1ab1d2a 4028@item
252b5132
RH
4029The address @code{0}.
4030@end itemize
4031
4032@node File Commands
36f63dca 4033@subsection Commands Dealing with Files
252b5132
RH
4034@cindex linker script file commands
4035Several linker script commands deal with files.
4036
4037@table @code
4038@item INCLUDE @var{filename}
4039@kindex INCLUDE @var{filename}
4040@cindex including a linker script
4041Include the linker script @var{filename} at this point. The file will
4042be searched for in the current directory, and in any directory specified
ff5dcc92 4043with the @option{-L} option. You can nest calls to @code{INCLUDE} up to
252b5132
RH
404410 levels deep.
4045
4006703d
NS
4046You can place @code{INCLUDE} directives at the top level, in @code{MEMORY} or
4047@code{SECTIONS} commands, or in output section descriptions.
4048
252b5132
RH
4049@item INPUT(@var{file}, @var{file}, @dots{})
4050@itemx INPUT(@var{file} @var{file} @dots{})
4051@kindex INPUT(@var{files})
4052@cindex input files in linker scripts
4053@cindex input object files in linker scripts
4054@cindex linker script input object files
4055The @code{INPUT} command directs the linker to include the named files
4056in the link, as though they were named on the command line.
4057
4058For example, if you always want to include @file{subr.o} any time you do
4059a link, but you can't be bothered to put it on every link command line,
4060then you can put @samp{INPUT (subr.o)} in your linker script.
4061
4062In fact, if you like, you can list all of your input files in the linker
4063script, and then invoke the linker with nothing but a @samp{-T} option.
4064
e3f2db7f
AO
4065In case a @dfn{sysroot prefix} is configured, and the filename starts
4066with the @samp{/} character, and the script being processed was
4067located inside the @dfn{sysroot prefix}, the filename will be looked
16171946
FS
4068for in the @dfn{sysroot prefix}. The @dfn{sysroot prefix} can also be forced by specifying
4069@code{=} as the first character in the filename path, or prefixing the
4070filename path with @code{$SYSROOT}. See also the description of
4071@samp{-L} in @ref{Options,,Command-line Options}.
4072
4073If a @dfn{sysroot prefix} is not used then the linker will try to open
4074the file in the directory containing the linker script. If it is not
4075found the linker will then search the current directory. If it is still
4076not found the linker will search through the archive library search
4077path.
252b5132 4078
ff5dcc92 4079If you use @samp{INPUT (-l@var{file})}, @command{ld} will transform the
a05a5b64 4080name to @code{lib@var{file}.a}, as with the command-line argument
252b5132
RH
4081@samp{-l}.
4082
4083When you use the @code{INPUT} command in an implicit linker script, the
4084files will be included in the link at the point at which the linker
4085script file is included. This can affect archive searching.
4086
4087@item GROUP(@var{file}, @var{file}, @dots{})
4088@itemx GROUP(@var{file} @var{file} @dots{})
4089@kindex GROUP(@var{files})
4090@cindex grouping input files
4091The @code{GROUP} command is like @code{INPUT}, except that the named
4092files should all be archives, and they are searched repeatedly until no
4093new undefined references are created. See the description of @samp{-(}
a05a5b64 4094in @ref{Options,,Command-line Options}.
252b5132 4095
b717d30e
JJ
4096@item AS_NEEDED(@var{file}, @var{file}, @dots{})
4097@itemx AS_NEEDED(@var{file} @var{file} @dots{})
4098@kindex AS_NEEDED(@var{files})
4099This construct can appear only inside of the @code{INPUT} or @code{GROUP}
4100commands, among other filenames. The files listed will be handled
4101as if they appear directly in the @code{INPUT} or @code{GROUP} commands,
4102with the exception of ELF shared libraries, that will be added only
4103when they are actually needed. This construct essentially enables
4104@option{--as-needed} option for all the files listed inside of it
4105and restores previous @option{--as-needed} resp. @option{--no-as-needed}
4106setting afterwards.
4107
252b5132
RH
4108@item OUTPUT(@var{filename})
4109@kindex OUTPUT(@var{filename})
b45619c0 4110@cindex output file name in linker script
252b5132
RH
4111The @code{OUTPUT} command names the output file. Using
4112@code{OUTPUT(@var{filename})} in the linker script is exactly like using
4113@samp{-o @var{filename}} on the command line (@pxref{Options,,Command
a05a5b64 4114Line Options}). If both are used, the command-line option takes
252b5132
RH
4115precedence.
4116
4117You can use the @code{OUTPUT} command to define a default name for the
4118output file other than the usual default of @file{a.out}.
4119
4120@item SEARCH_DIR(@var{path})
4121@kindex SEARCH_DIR(@var{path})
4122@cindex library search path in linker script
4123@cindex archive search path in linker script
4124@cindex search path in linker script
4125The @code{SEARCH_DIR} command adds @var{path} to the list of paths where
ff5dcc92 4126@command{ld} looks for archive libraries. Using
252b5132 4127@code{SEARCH_DIR(@var{path})} is exactly like using @samp{-L @var{path}}
a05a5b64 4128on the command line (@pxref{Options,,Command-line Options}). If both
252b5132 4129are used, then the linker will search both paths. Paths specified using
a05a5b64 4130the command-line option are searched first.
252b5132
RH
4131
4132@item STARTUP(@var{filename})
4133@kindex STARTUP(@var{filename})
4134@cindex first input file
4135The @code{STARTUP} command is just like the @code{INPUT} command, except
4136that @var{filename} will become the first input file to be linked, as
4137though it were specified first on the command line. This may be useful
4138when using a system in which the entry point is always the start of the
4139first file.
4140@end table
4141
4142@ifclear SingleFormat
4143@node Format Commands
36f63dca 4144@subsection Commands Dealing with Object File Formats
252b5132
RH
4145A couple of linker script commands deal with object file formats.
4146
4147@table @code
4148@item OUTPUT_FORMAT(@var{bfdname})
4149@itemx OUTPUT_FORMAT(@var{default}, @var{big}, @var{little})
4150@kindex OUTPUT_FORMAT(@var{bfdname})
4151@cindex output file format in linker script
4152The @code{OUTPUT_FORMAT} command names the BFD format to use for the
4153output file (@pxref{BFD}). Using @code{OUTPUT_FORMAT(@var{bfdname})} is
024531e2 4154exactly like using @samp{--oformat @var{bfdname}} on the command line
a05a5b64 4155(@pxref{Options,,Command-line Options}). If both are used, the command
252b5132
RH
4156line option takes precedence.
4157
4158You can use @code{OUTPUT_FORMAT} with three arguments to use different
a05a5b64 4159formats based on the @samp{-EB} and @samp{-EL} command-line options.
252b5132
RH
4160This permits the linker script to set the output format based on the
4161desired endianness.
4162
4163If neither @samp{-EB} nor @samp{-EL} are used, then the output format
4164will be the first argument, @var{default}. If @samp{-EB} is used, the
4165output format will be the second argument, @var{big}. If @samp{-EL} is
4166used, the output format will be the third argument, @var{little}.
4167
4168For example, the default linker script for the MIPS ELF target uses this
4169command:
4170@smallexample
4171OUTPUT_FORMAT(elf32-bigmips, elf32-bigmips, elf32-littlemips)
4172@end smallexample
4173This says that the default format for the output file is
a05a5b64 4174@samp{elf32-bigmips}, but if the user uses the @samp{-EL} command-line
252b5132
RH
4175option, the output file will be created in the @samp{elf32-littlemips}
4176format.
4177
4178@item TARGET(@var{bfdname})
4179@kindex TARGET(@var{bfdname})
4180@cindex input file format in linker script
4181The @code{TARGET} command names the BFD format to use when reading input
4182files. It affects subsequent @code{INPUT} and @code{GROUP} commands.
4183This command is like using @samp{-b @var{bfdname}} on the command line
a05a5b64 4184(@pxref{Options,,Command-line Options}). If the @code{TARGET} command
252b5132
RH
4185is used but @code{OUTPUT_FORMAT} is not, then the last @code{TARGET}
4186command is also used to set the format for the output file. @xref{BFD}.
4187@end table
4188@end ifclear
4189
4a93e180
NC
4190@node REGION_ALIAS
4191@subsection Assign alias names to memory regions
4192@kindex REGION_ALIAS(@var{alias}, @var{region})
4193@cindex region alias
4194@cindex region names
4195
4196Alias names can be added to existing memory regions created with the
4197@ref{MEMORY} command. Each name corresponds to at most one memory region.
4198
4199@smallexample
4200REGION_ALIAS(@var{alias}, @var{region})
4201@end smallexample
4202
4203The @code{REGION_ALIAS} function creates an alias name @var{alias} for the
4204memory region @var{region}. This allows a flexible mapping of output sections
4205to memory regions. An example follows.
4206
4207Suppose we have an application for embedded systems which come with various
4208memory storage devices. All have a general purpose, volatile memory @code{RAM}
4209that allows code execution or data storage. Some may have a read-only,
4210non-volatile memory @code{ROM} that allows code execution and read-only data
4211access. The last variant is a read-only, non-volatile memory @code{ROM2} with
4212read-only data access and no code execution capability. We have four output
4213sections:
4214
4215@itemize @bullet
4216@item
4217@code{.text} program code;
4218@item
4219@code{.rodata} read-only data;
4220@item
4221@code{.data} read-write initialized data;
4222@item
4223@code{.bss} read-write zero initialized data.
4224@end itemize
4225
4226The goal is to provide a linker command file that contains a system independent
4227part defining the output sections and a system dependent part mapping the
4228output sections to the memory regions available on the system. Our embedded
4229systems come with three different memory setups @code{A}, @code{B} and
4230@code{C}:
4231@multitable @columnfractions .25 .25 .25 .25
4232@item Section @tab Variant A @tab Variant B @tab Variant C
9d5777a3 4233@item .text @tab RAM @tab ROM @tab ROM
4a93e180
NC
4234@item .rodata @tab RAM @tab ROM @tab ROM2
4235@item .data @tab RAM @tab RAM/ROM @tab RAM/ROM2
4236@item .bss @tab RAM @tab RAM @tab RAM
4237@end multitable
4238The notation @code{RAM/ROM} or @code{RAM/ROM2} means that this section is
4239loaded into region @code{ROM} or @code{ROM2} respectively. Please note that
4240the load address of the @code{.data} section starts in all three variants at
4241the end of the @code{.rodata} section.
4242
4243The base linker script that deals with the output sections follows. It
4244includes the system dependent @code{linkcmds.memory} file that describes the
4245memory layout:
4246@smallexample
4247INCLUDE linkcmds.memory
4248
4249SECTIONS
4250 @{
4251 .text :
4252 @{
4253 *(.text)
4254 @} > REGION_TEXT
4255 .rodata :
4256 @{
4257 *(.rodata)
4258 rodata_end = .;
4259 @} > REGION_RODATA
4260 .data : AT (rodata_end)
4261 @{
4262 data_start = .;
4263 *(.data)
4264 @} > REGION_DATA
4265 data_size = SIZEOF(.data);
4266 data_load_start = LOADADDR(.data);
4267 .bss :
4268 @{
4269 *(.bss)
4270 @} > REGION_BSS
4271 @}
4272@end smallexample
4273
4274Now we need three different @code{linkcmds.memory} files to define memory
4275regions and alias names. The content of @code{linkcmds.memory} for the three
4276variants @code{A}, @code{B} and @code{C}:
4277@table @code
4278@item A
4279Here everything goes into the @code{RAM}.
4280@smallexample
4281MEMORY
4282 @{
4283 RAM : ORIGIN = 0, LENGTH = 4M
4284 @}
4285
4286REGION_ALIAS("REGION_TEXT", RAM);
4287REGION_ALIAS("REGION_RODATA", RAM);
4288REGION_ALIAS("REGION_DATA", RAM);
4289REGION_ALIAS("REGION_BSS", RAM);
4290@end smallexample
4291@item B
4292Program code and read-only data go into the @code{ROM}. Read-write data goes
4293into the @code{RAM}. An image of the initialized data is loaded into the
4294@code{ROM} and will be copied during system start into the @code{RAM}.
4295@smallexample
4296MEMORY
4297 @{
4298 ROM : ORIGIN = 0, LENGTH = 3M
4299 RAM : ORIGIN = 0x10000000, LENGTH = 1M
4300 @}
4301
4302REGION_ALIAS("REGION_TEXT", ROM);
4303REGION_ALIAS("REGION_RODATA", ROM);
4304REGION_ALIAS("REGION_DATA", RAM);
4305REGION_ALIAS("REGION_BSS", RAM);
4306@end smallexample
4307@item C
4308Program code goes into the @code{ROM}. Read-only data goes into the
4309@code{ROM2}. Read-write data goes into the @code{RAM}. An image of the
4310initialized data is loaded into the @code{ROM2} and will be copied during
4311system start into the @code{RAM}.
4312@smallexample
4313MEMORY
4314 @{
4315 ROM : ORIGIN = 0, LENGTH = 2M
4316 ROM2 : ORIGIN = 0x10000000, LENGTH = 1M
4317 RAM : ORIGIN = 0x20000000, LENGTH = 1M
4318 @}
4319
4320REGION_ALIAS("REGION_TEXT", ROM);
4321REGION_ALIAS("REGION_RODATA", ROM2);
4322REGION_ALIAS("REGION_DATA", RAM);
4323REGION_ALIAS("REGION_BSS", RAM);
4324@end smallexample
4325@end table
4326
4327It is possible to write a common system initialization routine to copy the
4328@code{.data} section from @code{ROM} or @code{ROM2} into the @code{RAM} if
4329necessary:
4330@smallexample
4331#include <string.h>
4332
4333extern char data_start [];
4334extern char data_size [];
4335extern char data_load_start [];
4336
4337void copy_data(void)
4338@{
4339 if (data_start != data_load_start)
4340 @{
4341 memcpy(data_start, data_load_start, (size_t) data_size);
4342 @}
4343@}
4344@end smallexample
4345
252b5132 4346@node Miscellaneous Commands
36f63dca 4347@subsection Other Linker Script Commands
252b5132
RH
4348There are a few other linker scripts commands.
4349
4350@table @code
4351@item ASSERT(@var{exp}, @var{message})
4352@kindex ASSERT
4353@cindex assertion in linker script
4354Ensure that @var{exp} is non-zero. If it is zero, then exit the linker
4355with an error code, and print @var{message}.
4356
fd1c4238
NC
4357Note that assertions are checked before the final stages of linking
4358take place. This means that expressions involving symbols PROVIDEd
4359inside section definitions will fail if the user has not set values
4360for those symbols. The only exception to this rule is PROVIDEd
4361symbols that just reference dot. Thus an assertion like this:
4362
4363@smallexample
4364 .stack :
4365 @{
4366 PROVIDE (__stack = .);
4367 PROVIDE (__stack_size = 0x100);
4368 ASSERT ((__stack > (_end + __stack_size)), "Error: No room left for the stack");
4369 @}
4370@end smallexample
4371
4372will fail if @code{__stack_size} is not defined elsewhere. Symbols
4373PROVIDEd outside of section definitions are evaluated earlier, so they
4374can be used inside ASSERTions. Thus:
4375
4376@smallexample
4377 PROVIDE (__stack_size = 0x100);
4378 .stack :
4379 @{
4380 PROVIDE (__stack = .);
4381 ASSERT ((__stack > (_end + __stack_size)), "Error: No room left for the stack");
4382 @}
4383@end smallexample
4384
4385will work.
4386
252b5132
RH
4387@item EXTERN(@var{symbol} @var{symbol} @dots{})
4388@kindex EXTERN
4389@cindex undefined symbol in linker script
4390Force @var{symbol} to be entered in the output file as an undefined
4391symbol. Doing this may, for example, trigger linking of additional
4392modules from standard libraries. You may list several @var{symbol}s for
4393each @code{EXTERN}, and you may use @code{EXTERN} multiple times. This
4394command has the same effect as the @samp{-u} command-line option.
4395
4396@item FORCE_COMMON_ALLOCATION
4397@kindex FORCE_COMMON_ALLOCATION
4398@cindex common allocation in linker script
4399This command has the same effect as the @samp{-d} command-line option:
ff5dcc92 4400to make @command{ld} assign space to common symbols even if a relocatable
252b5132
RH
4401output file is specified (@samp{-r}).
4402
4818e05f
AM
4403@item INHIBIT_COMMON_ALLOCATION
4404@kindex INHIBIT_COMMON_ALLOCATION
4405@cindex common allocation in linker script
4406This command has the same effect as the @samp{--no-define-common}
4407command-line option: to make @code{ld} omit the assignment of addresses
4408to common symbols even for a non-relocatable output file.
4409
7bdf4127
AB
4410@item FORCE_GROUP_ALLOCATION
4411@kindex FORCE_GROUP_ALLOCATION
4412@cindex group allocation in linker script
4413@cindex section groups
4414@cindex COMDAT
4415This command has the same effect as the
4416@samp{--force-group-allocation} command-line option: to make
4417@command{ld} place section group members like normal input sections,
4418and to delete the section groups even if a relocatable output file is
4419specified (@samp{-r}).
4420
53d25da6
AM
4421@item INSERT [ AFTER | BEFORE ] @var{output_section}
4422@kindex INSERT
4423@cindex insert user script into default script
4424This command is typically used in a script specified by @samp{-T} to
4425augment the default @code{SECTIONS} with, for example, overlays. It
4426inserts all prior linker script statements after (or before)
4427@var{output_section}, and also causes @samp{-T} to not override the
4428default linker script. The exact insertion point is as for orphan
4429sections. @xref{Location Counter}. The insertion happens after the
4430linker has mapped input sections to output sections. Prior to the
4431insertion, since @samp{-T} scripts are parsed before the default
4432linker script, statements in the @samp{-T} script occur before the
4433default linker script statements in the internal linker representation
4434of the script. In particular, input section assignments will be made
4435to @samp{-T} output sections before those in the default script. Here
4436is an example of how a @samp{-T} script using @code{INSERT} might look:
4437
4438@smallexample
4439SECTIONS
4440@{
4441 OVERLAY :
4442 @{
4443 .ov1 @{ ov1*(.text) @}
4444 .ov2 @{ ov2*(.text) @}
4445 @}
4446@}
4447INSERT AFTER .text;
4448@end smallexample
4449
31f14901
AM
4450Note that when @samp{-T} is used twice, once to override the default
4451script and once to augment that script using @code{INSERT} the order
4452of parsing and section assignments apply as for the default script.
4453The script with @code{INSERT} should be specified @emph{first} on the
4454command line.
4455
252b5132
RH
4456@item NOCROSSREFS(@var{section} @var{section} @dots{})
4457@kindex NOCROSSREFS(@var{sections})
4458@cindex cross references
ff5dcc92 4459This command may be used to tell @command{ld} to issue an error about any
252b5132
RH
4460references among certain output sections.
4461
4462In certain types of programs, particularly on embedded systems when
4463using overlays, when one section is loaded into memory, another section
4464will not be. Any direct references between the two sections would be
4465errors. For example, it would be an error if code in one section called
4466a function defined in the other section.
4467
4468The @code{NOCROSSREFS} command takes a list of output section names. If
ff5dcc92 4469@command{ld} detects any cross references between the sections, it reports
252b5132
RH
4470an error and returns a non-zero exit status. Note that the
4471@code{NOCROSSREFS} command uses output section names, not input section
4472names.
4473
cdf96953
MF
4474@item NOCROSSREFS_TO(@var{tosection} @var{fromsection} @dots{})
4475@kindex NOCROSSREFS_TO(@var{tosection} @var{fromsections})
4476@cindex cross references
4477This command may be used to tell @command{ld} to issue an error about any
4478references to one section from a list of other sections.
4479
4480The @code{NOCROSSREFS} command is useful when ensuring that two or more
4481output sections are entirely independent but there are situations where
4482a one-way dependency is needed. For example, in a multi-core application
4483there may be shared code that can be called from each core but for safety
4484must never call back.
4485
4486The @code{NOCROSSREFS_TO} command takes a list of output section names.
4487The first section can not be referenced from any of the other sections.
4488If @command{ld} detects any references to the first section from any of
4489the other sections, it reports an error and returns a non-zero exit
4490status. Note that the @code{NOCROSSREFS_TO} command uses output section
4491names, not input section names.
4492
252b5132
RH
4493@ifclear SingleFormat
4494@item OUTPUT_ARCH(@var{bfdarch})
4495@kindex OUTPUT_ARCH(@var{bfdarch})
4496@cindex machine architecture
4497@cindex architecture
4498Specify a particular output machine architecture. The argument is one
4499of the names used by the BFD library (@pxref{BFD}). You can see the
4500architecture of an object file by using the @code{objdump} program with
4501the @samp{-f} option.
4502@end ifclear
01554a74
AM
4503
4504@item LD_FEATURE(@var{string})
4505@kindex LD_FEATURE(@var{string})
4506This command may be used to modify @command{ld} behavior. If
4507@var{string} is @code{"SANE_EXPR"} then absolute symbols and numbers
4508in a script are simply treated as numbers everywhere.
4509@xref{Expression Section}.
252b5132
RH
4510@end table
4511
4512@node Assignments
4513@section Assigning Values to Symbols
4514@cindex assignment in scripts
4515@cindex symbol definition, scripts
4516@cindex variables, defining
4517You may assign a value to a symbol in a linker script. This will define
73ae6183 4518the symbol and place it into the symbol table with a global scope.
252b5132
RH
4519
4520@menu
4521* Simple Assignments:: Simple Assignments
eb8476a6 4522* HIDDEN:: HIDDEN
252b5132 4523* PROVIDE:: PROVIDE
7af8e998 4524* PROVIDE_HIDDEN:: PROVIDE_HIDDEN
73ae6183 4525* Source Code Reference:: How to use a linker script defined symbol in source code
252b5132
RH
4526@end menu
4527
4528@node Simple Assignments
4529@subsection Simple Assignments
4530
4531You may assign to a symbol using any of the C assignment operators:
4532
4533@table @code
4534@item @var{symbol} = @var{expression} ;
4535@itemx @var{symbol} += @var{expression} ;
4536@itemx @var{symbol} -= @var{expression} ;
4537@itemx @var{symbol} *= @var{expression} ;
4538@itemx @var{symbol} /= @var{expression} ;
4539@itemx @var{symbol} <<= @var{expression} ;
4540@itemx @var{symbol} >>= @var{expression} ;
4541@itemx @var{symbol} &= @var{expression} ;
4542@itemx @var{symbol} |= @var{expression} ;
4543@end table
4544
4545The first case will define @var{symbol} to the value of
4546@var{expression}. In the other cases, @var{symbol} must already be
4547defined, and the value will be adjusted accordingly.
4548
4549The special symbol name @samp{.} indicates the location counter. You
b5666f2f 4550may only use this within a @code{SECTIONS} command. @xref{Location Counter}.
252b5132
RH
4551
4552The semicolon after @var{expression} is required.
4553
4554Expressions are defined below; see @ref{Expressions}.
4555
4556You may write symbol assignments as commands in their own right, or as
4557statements within a @code{SECTIONS} command, or as part of an output
4558section description in a @code{SECTIONS} command.
4559
4560The section of the symbol will be set from the section of the
4561expression; for more information, see @ref{Expression Section}.
4562
4563Here is an example showing the three different places that symbol
4564assignments may be used:
4565
4566@smallexample
4567floating_point = 0;
4568SECTIONS
4569@{
4570 .text :
4571 @{
4572 *(.text)
4573 _etext = .;
4574 @}
156e34dd 4575 _bdata = (. + 3) & ~ 3;
252b5132
RH
4576 .data : @{ *(.data) @}
4577@}
4578@end smallexample
4579@noindent
4580In this example, the symbol @samp{floating_point} will be defined as
4581zero. The symbol @samp{_etext} will be defined as the address following
4582the last @samp{.text} input section. The symbol @samp{_bdata} will be
4583defined as the address following the @samp{.text} output section aligned
4584upward to a 4 byte boundary.
4585
eb8476a6
MR
4586@node HIDDEN
4587@subsection HIDDEN
4588@cindex HIDDEN
4589For ELF targeted ports, define a symbol that will be hidden and won't be
4590exported. The syntax is @code{HIDDEN(@var{symbol} = @var{expression})}.
4591
4592Here is the example from @ref{Simple Assignments}, rewritten to use
4593@code{HIDDEN}:
4594
4595@smallexample
4596HIDDEN(floating_point = 0);
4597SECTIONS
4598@{
4599 .text :
4600 @{
4601 *(.text)
4602 HIDDEN(_etext = .);
4603 @}
4604 HIDDEN(_bdata = (. + 3) & ~ 3);
4605 .data : @{ *(.data) @}
4606@}
4607@end smallexample
4608@noindent
4609In this case none of the three symbols will be visible outside this module.
4610
252b5132
RH
4611@node PROVIDE
4612@subsection PROVIDE
4613@cindex PROVIDE
4614In some cases, it is desirable for a linker script to define a symbol
4615only if it is referenced and is not defined by any object included in
4616the link. For example, traditional linkers defined the symbol
4617@samp{etext}. However, ANSI C requires that the user be able to use
4618@samp{etext} as a function name without encountering an error. The
4619@code{PROVIDE} keyword may be used to define a symbol, such as
4620@samp{etext}, only if it is referenced but not defined. The syntax is
4621@code{PROVIDE(@var{symbol} = @var{expression})}.
4622
4623Here is an example of using @code{PROVIDE} to define @samp{etext}:
4624@smallexample
4625SECTIONS
4626@{
4627 .text :
4628 @{
4629 *(.text)
4630 _etext = .;
4631 PROVIDE(etext = .);
4632 @}
4633@}
4634@end smallexample
4635
4636In this example, if the program defines @samp{_etext} (with a leading
3c27360b
JB
4637underscore), the linker will give a multiple definition diagnostic. If,
4638on the other hand, the program defines @samp{etext} (with no leading
252b5132
RH
4639underscore), the linker will silently use the definition in the program.
4640If the program references @samp{etext} but does not define it, the
4641linker will use the definition in the linker script.
4642
b0daac83
NC
4643Note - the @code{PROVIDE} directive considers a common symbol to be
4644defined, even though such a symbol could be combined with the symbol
4645that the @code{PROVIDE} would create. This is particularly important
4646when considering constructor and destructor list symbols such as
4647@samp{__CTOR_LIST__} as these are often defined as common symbols.
4648
7af8e998
L
4649@node PROVIDE_HIDDEN
4650@subsection PROVIDE_HIDDEN
4651@cindex PROVIDE_HIDDEN
4652Similar to @code{PROVIDE}. For ELF targeted ports, the symbol will be
4653hidden and won't be exported.
4654
73ae6183
NC
4655@node Source Code Reference
4656@subsection Source Code Reference
4657
4658Accessing a linker script defined variable from source code is not
4659intuitive. In particular a linker script symbol is not equivalent to
4660a variable declaration in a high level language, it is instead a
4661symbol that does not have a value.
4662
4663Before going further, it is important to note that compilers often
4664transform names in the source code into different names when they are
4665stored in the symbol table. For example, Fortran compilers commonly
4666prepend or append an underscore, and C++ performs extensive @samp{name
4667mangling}. Therefore there might be a discrepancy between the name
4668of a variable as it is used in source code and the name of the same
4669variable as it is defined in a linker script. For example in C a
4670linker script variable might be referred to as:
4671
4672@smallexample
4673 extern int foo;
4674@end smallexample
4675
4676But in the linker script it might be defined as:
4677
4678@smallexample
4679 _foo = 1000;
4680@end smallexample
4681
4682In the remaining examples however it is assumed that no name
4683transformation has taken place.
4684
4685When a symbol is declared in a high level language such as C, two
4686things happen. The first is that the compiler reserves enough space
4687in the program's memory to hold the @emph{value} of the symbol. The
4688second is that the compiler creates an entry in the program's symbol
4689table which holds the symbol's @emph{address}. ie the symbol table
4690contains the address of the block of memory holding the symbol's
4691value. So for example the following C declaration, at file scope:
4692
4693@smallexample
4694 int foo = 1000;
4695@end smallexample
4696
10bf6894 4697creates an entry called @samp{foo} in the symbol table. This entry
73ae6183
NC
4698holds the address of an @samp{int} sized block of memory where the
4699number 1000 is initially stored.
4700
4701When a program references a symbol the compiler generates code that
4702first accesses the symbol table to find the address of the symbol's
4703memory block and then code to read the value from that memory block.
4704So:
4705
4706@smallexample
4707 foo = 1;
4708@end smallexample
4709
4710looks up the symbol @samp{foo} in the symbol table, gets the address
4711associated with this symbol and then writes the value 1 into that
4712address. Whereas:
4713
4714@smallexample
4715 int * a = & foo;
4716@end smallexample
4717
10bf6894 4718looks up the symbol @samp{foo} in the symbol table, gets its address
73ae6183
NC
4719and then copies this address into the block of memory associated with
4720the variable @samp{a}.
4721
4722Linker scripts symbol declarations, by contrast, create an entry in
4723the symbol table but do not assign any memory to them. Thus they are
4724an address without a value. So for example the linker script definition:
4725
4726@smallexample
4727 foo = 1000;
4728@end smallexample
4729
4730creates an entry in the symbol table called @samp{foo} which holds
4731the address of memory location 1000, but nothing special is stored at
4732address 1000. This means that you cannot access the @emph{value} of a
4733linker script defined symbol - it has no value - all you can do is
4734access the @emph{address} of a linker script defined symbol.
4735
4736Hence when you are using a linker script defined symbol in source code
4737you should always take the address of the symbol, and never attempt to
4738use its value. For example suppose you want to copy the contents of a
4739section of memory called .ROM into a section called .FLASH and the
4740linker script contains these declarations:
4741
4742@smallexample
4743@group
4744 start_of_ROM = .ROM;
a5e406b5 4745 end_of_ROM = .ROM + sizeof (.ROM);
73ae6183
NC
4746 start_of_FLASH = .FLASH;
4747@end group
4748@end smallexample
4749
4750Then the C source code to perform the copy would be:
4751
4752@smallexample
4753@group
4754 extern char start_of_ROM, end_of_ROM, start_of_FLASH;
c0065db7 4755
73ae6183
NC
4756 memcpy (& start_of_FLASH, & start_of_ROM, & end_of_ROM - & start_of_ROM);
4757@end group
4758@end smallexample
4759
4760Note the use of the @samp{&} operators. These are correct.
5707d2ad
NC
4761Alternatively the symbols can be treated as the names of vectors or
4762arrays and then the code will again work as expected:
4763
4764@smallexample
4765@group
4766 extern char start_of_ROM[], end_of_ROM[], start_of_FLASH[];
4767
4768 memcpy (start_of_FLASH, start_of_ROM, end_of_ROM - start_of_ROM);
4769@end group
4770@end smallexample
4771
4772Note how using this method does not require the use of @samp{&}
4773operators.
73ae6183 4774
252b5132 4775@node SECTIONS
36f63dca 4776@section SECTIONS Command
252b5132
RH
4777@kindex SECTIONS
4778The @code{SECTIONS} command tells the linker how to map input sections
4779into output sections, and how to place the output sections in memory.
4780
4781The format of the @code{SECTIONS} command is:
4782@smallexample
4783SECTIONS
4784@{
4785 @var{sections-command}
4786 @var{sections-command}
4787 @dots{}
4788@}
4789@end smallexample
4790
4791Each @var{sections-command} may of be one of the following:
4792
4793@itemize @bullet
4794@item
4795an @code{ENTRY} command (@pxref{Entry Point,,Entry command})
4796@item
4797a symbol assignment (@pxref{Assignments})
4798@item
4799an output section description
4800@item
4801an overlay description
4802@end itemize
4803
4804The @code{ENTRY} command and symbol assignments are permitted inside the
4805@code{SECTIONS} command for convenience in using the location counter in
4806those commands. This can also make the linker script easier to
4807understand because you can use those commands at meaningful points in
4808the layout of the output file.
4809
4810Output section descriptions and overlay descriptions are described
4811below.
4812
4813If you do not use a @code{SECTIONS} command in your linker script, the
4814linker will place each input section into an identically named output
4815section in the order that the sections are first encountered in the
4816input files. If all input sections are present in the first file, for
4817example, the order of sections in the output file will match the order
4818in the first input file. The first section will be at address zero.
4819
4820@menu
4821* Output Section Description:: Output section description
4822* Output Section Name:: Output section name
4823* Output Section Address:: Output section address
4824* Input Section:: Input section description
4825* Output Section Data:: Output section data
4826* Output Section Keywords:: Output section keywords
4827* Output Section Discarding:: Output section discarding
4828* Output Section Attributes:: Output section attributes
4829* Overlay Description:: Overlay description
4830@end menu
4831
4832@node Output Section Description
36f63dca 4833@subsection Output Section Description
252b5132
RH
4834The full description of an output section looks like this:
4835@smallexample
a1ab1d2a 4836@group
7e7d5768 4837@var{section} [@var{address}] [(@var{type})] :
0c71d759 4838 [AT(@var{lma})]
1eec346e 4839 [ALIGN(@var{section_align}) | ALIGN_WITH_INPUT]
0c71d759
NC
4840 [SUBALIGN(@var{subsection_align})]
4841 [@var{constraint}]
252b5132
RH
4842 @{
4843 @var{output-section-command}
4844 @var{output-section-command}
4845 @dots{}
abc9061b 4846 @} [>@var{region}] [AT>@var{lma_region}] [:@var{phdr} :@var{phdr} @dots{}] [=@var{fillexp}] [,]
252b5132
RH
4847@end group
4848@end smallexample
4849
4850Most output sections do not use most of the optional section attributes.
4851
4852The whitespace around @var{section} is required, so that the section
4853name is unambiguous. The colon and the curly braces are also required.
abc9061b
CC
4854The comma at the end may be required if a @var{fillexp} is used and
4855the next @var{sections-command} looks like a continuation of the expression.
252b5132
RH
4856The line breaks and other white space are optional.
4857
4858Each @var{output-section-command} may be one of the following:
4859
4860@itemize @bullet
4861@item
4862a symbol assignment (@pxref{Assignments})
4863@item
4864an input section description (@pxref{Input Section})
4865@item
4866data values to include directly (@pxref{Output Section Data})
4867@item
4868a special output section keyword (@pxref{Output Section Keywords})
4869@end itemize
4870
4871@node Output Section Name
36f63dca 4872@subsection Output Section Name
252b5132
RH
4873@cindex name, section
4874@cindex section name
4875The name of the output section is @var{section}. @var{section} must
4876meet the constraints of your output format. In formats which only
4877support a limited number of sections, such as @code{a.out}, the name
4878must be one of the names supported by the format (@code{a.out}, for
4879example, allows only @samp{.text}, @samp{.data} or @samp{.bss}). If the
4880output format supports any number of sections, but with numbers and not
4881names (as is the case for Oasys), the name should be supplied as a
4882quoted numeric string. A section name may consist of any sequence of
4883characters, but a name which contains any unusual characters such as
4884commas must be quoted.
4885
4886The output section name @samp{/DISCARD/} is special; @ref{Output Section
4887Discarding}.
4888
4889@node Output Section Address
2a16d82a 4890@subsection Output Section Address
252b5132
RH
4891@cindex address, section
4892@cindex section address
4893The @var{address} is an expression for the VMA (the virtual memory
ea5cae92
NC
4894address) of the output section. This address is optional, but if it
4895is provided then the output address will be set exactly as specified.
4896
4897If the output address is not specified then one will be chosen for the
4898section, based on the heuristic below. This address will be adjusted
4899to fit the alignment requirement of the output section. The
4900alignment requirement is the strictest alignment of any input section
4901contained within the output section.
4902
4903The output section address heuristic is as follows:
4904
4905@itemize @bullet
4906@item
4907If an output memory @var{region} is set for the section then it
4908is added to this region and its address will be the next free address
4909in that region.
4910
4911@item
4912If the MEMORY command has been used to create a list of memory
4913regions then the first region which has attributes compatible with the
4914section is selected to contain it. The section's output address will
4915be the next free address in that region; @ref{MEMORY}.
4916
4917@item
4918If no memory regions were specified, or none match the section then
4919the output address will be based on the current value of the location
4920counter.
4921@end itemize
4922
4923@noindent
4924For example:
4925
252b5132
RH
4926@smallexample
4927.text . : @{ *(.text) @}
4928@end smallexample
ea5cae92 4929
252b5132
RH
4930@noindent
4931and
ea5cae92 4932
252b5132
RH
4933@smallexample
4934.text : @{ *(.text) @}
4935@end smallexample
ea5cae92 4936
252b5132
RH
4937@noindent
4938are subtly different. The first will set the address of the
4939@samp{.text} output section to the current value of the location
4940counter. The second will set it to the current value of the location
ea5cae92
NC
4941counter aligned to the strictest alignment of any of the @samp{.text}
4942input sections.
252b5132
RH
4943
4944The @var{address} may be an arbitrary expression; @ref{Expressions}.
4945For example, if you want to align the section on a 0x10 byte boundary,
4946so that the lowest four bits of the section address are zero, you could
4947do something like this:
4948@smallexample
4949.text ALIGN(0x10) : @{ *(.text) @}
4950@end smallexample
4951@noindent
4952This works because @code{ALIGN} returns the current location counter
4953aligned upward to the specified value.
4954
4955Specifying @var{address} for a section will change the value of the
6ce340f1
NC
4956location counter, provided that the section is non-empty. (Empty
4957sections are ignored).
252b5132
RH
4958
4959@node Input Section
36f63dca 4960@subsection Input Section Description
252b5132
RH
4961@cindex input sections
4962@cindex mapping input sections to output sections
4963The most common output section command is an input section description.
4964
4965The input section description is the most basic linker script operation.
4966You use output sections to tell the linker how to lay out your program
4967in memory. You use input section descriptions to tell the linker how to
4968map the input files into your memory layout.
4969
4970@menu
4971* Input Section Basics:: Input section basics
4972* Input Section Wildcards:: Input section wildcard patterns
4973* Input Section Common:: Input section for common symbols
4974* Input Section Keep:: Input section and garbage collection
4975* Input Section Example:: Input section example
4976@end menu
4977
4978@node Input Section Basics
36f63dca 4979@subsubsection Input Section Basics
252b5132
RH
4980@cindex input section basics
4981An input section description consists of a file name optionally followed
4982by a list of section names in parentheses.
4983
4984The file name and the section name may be wildcard patterns, which we
4985describe further below (@pxref{Input Section Wildcards}).
4986
4987The most common input section description is to include all input
4988sections with a particular name in the output section. For example, to
4989include all input @samp{.text} sections, you would write:
4990@smallexample
4991*(.text)
4992@end smallexample
4993@noindent
18625d54 4994Here the @samp{*} is a wildcard which matches any file name. To exclude a list
8f1732fc 4995@cindex EXCLUDE_FILE
18625d54
CM
4996of files from matching the file name wildcard, EXCLUDE_FILE may be used to
4997match all files except the ones specified in the EXCLUDE_FILE list. For
4998example:
252b5132 4999@smallexample
8f1732fc
AB
5000EXCLUDE_FILE (*crtend.o *otherfile.o) *(.ctors)
5001@end smallexample
5002@noindent
5003will cause all .ctors sections from all files except @file{crtend.o}
5004and @file{otherfile.o} to be included. The EXCLUDE_FILE can also be
5005placed inside the section list, for example:
5006@smallexample
b4346c09 5007*(EXCLUDE_FILE (*crtend.o *otherfile.o) .ctors)
252b5132 5008@end smallexample
8f1732fc
AB
5009@noindent
5010The result of this is identically to the previous example. Supporting
5011two syntaxes for EXCLUDE_FILE is useful if the section list contains
5012more than one section, as described below.
252b5132
RH
5013
5014There are two ways to include more than one section:
5015@smallexample
5016*(.text .rdata)
5017*(.text) *(.rdata)
5018@end smallexample
5019@noindent
5020The difference between these is the order in which the @samp{.text} and
5021@samp{.rdata} input sections will appear in the output section. In the
b6bf44ba
AM
5022first example, they will be intermingled, appearing in the same order as
5023they are found in the linker input. In the second example, all
252b5132
RH
5024@samp{.text} input sections will appear first, followed by all
5025@samp{.rdata} input sections.
5026
8f1732fc
AB
5027When using EXCLUDE_FILE with more than one section, if the exclusion
5028is within the section list then the exclusion only applies to the
5029immediately following section, for example:
a5bf7d4f
AB
5030@smallexample
5031*(EXCLUDE_FILE (*somefile.o) .text .rdata)
5032@end smallexample
5033@noindent
5034will cause all @samp{.text} sections from all files except
5035@file{somefile.o} to be included, while all @samp{.rdata} sections
5036from all files, including @file{somefile.o}, will be included. To
5037exclude the @samp{.rdata} sections from @file{somefile.o} the example
8f1732fc 5038could be modified to:
a5bf7d4f
AB
5039@smallexample
5040*(EXCLUDE_FILE (*somefile.o) .text EXCLUDE_FILE (*somefile.o) .rdata)
5041@end smallexample
8f1732fc
AB
5042@noindent
5043Alternatively, placing the EXCLUDE_FILE outside of the section list,
5044before the input file selection, will cause the exclusion to apply for
5045all sections. Thus the previous example can be rewritten as:
5046@smallexample
5047EXCLUDE_FILE (*somefile.o) *(.text .rdata)
5048@end smallexample
a5bf7d4f 5049
252b5132
RH
5050You can specify a file name to include sections from a particular file.
5051You would do this if one or more of your files contain special data that
5052needs to be at a particular location in memory. For example:
5053@smallexample
5054data.o(.data)
5055@end smallexample
5056
ae17ab41
CM
5057To refine the sections that are included based on the section flags
5058of an input section, INPUT_SECTION_FLAGS may be used.
5059
5060Here is a simple example for using Section header flags for ELF sections:
5061
5062@smallexample
5063@group
5064SECTIONS @{
5065 .text : @{ INPUT_SECTION_FLAGS (SHF_MERGE & SHF_STRINGS) *(.text) @}
5066 .text2 : @{ INPUT_SECTION_FLAGS (!SHF_WRITE) *(.text) @}
5067@}
5068@end group
5069@end smallexample
5070
5071In this example, the output section @samp{.text} will be comprised of any
5072input section matching the name *(.text) whose section header flags
5073@code{SHF_MERGE} and @code{SHF_STRINGS} are set. The output section
5074@samp{.text2} will be comprised of any input section matching the name *(.text)
5075whose section header flag @code{SHF_WRITE} is clear.
5076
967928e9
AM
5077You can also specify files within archives by writing a pattern
5078matching the archive, a colon, then the pattern matching the file,
5079with no whitespace around the colon.
5080
5081@table @samp
5082@item archive:file
5083matches file within archive
5084@item archive:
5085matches the whole archive
5086@item :file
5087matches file but not one in an archive
5088@end table
5089
5090Either one or both of @samp{archive} and @samp{file} can contain shell
5091wildcards. On DOS based file systems, the linker will assume that a
5092single letter followed by a colon is a drive specifier, so
5093@samp{c:myfile.o} is a simple file specification, not @samp{myfile.o}
5094within an archive called @samp{c}. @samp{archive:file} filespecs may
5095also be used within an @code{EXCLUDE_FILE} list, but may not appear in
5096other linker script contexts. For instance, you cannot extract a file
5097from an archive by using @samp{archive:file} in an @code{INPUT}
5098command.
5099
252b5132
RH
5100If you use a file name without a list of sections, then all sections in
5101the input file will be included in the output section. This is not
5102commonly done, but it may by useful on occasion. For example:
5103@smallexample
5104data.o
5105@end smallexample
5106
967928e9
AM
5107When you use a file name which is not an @samp{archive:file} specifier
5108and does not contain any wild card
252b5132
RH
5109characters, the linker will first see if you also specified the file
5110name on the linker command line or in an @code{INPUT} command. If you
5111did not, the linker will attempt to open the file as an input file, as
5112though it appeared on the command line. Note that this differs from an
5113@code{INPUT} command, because the linker will not search for the file in
5114the archive search path.
5115
5116@node Input Section Wildcards
36f63dca 5117@subsubsection Input Section Wildcard Patterns
252b5132
RH
5118@cindex input section wildcards
5119@cindex wildcard file name patterns
5120@cindex file name wildcard patterns
5121@cindex section name wildcard patterns
5122In an input section description, either the file name or the section
5123name or both may be wildcard patterns.
5124
5125The file name of @samp{*} seen in many examples is a simple wildcard
5126pattern for the file name.
5127
5128The wildcard patterns are like those used by the Unix shell.
5129
5130@table @samp
5131@item *
5132matches any number of characters
5133@item ?
5134matches any single character
5135@item [@var{chars}]
5136matches a single instance of any of the @var{chars}; the @samp{-}
5137character may be used to specify a range of characters, as in
5138@samp{[a-z]} to match any lower case letter
5139@item \
5140quotes the following character
5141@end table
5142
252b5132
RH
5143File name wildcard patterns only match files which are explicitly
5144specified on the command line or in an @code{INPUT} command. The linker
5145does not search directories to expand wildcards.
5146
5147If a file name matches more than one wildcard pattern, or if a file name
5148appears explicitly and is also matched by a wildcard pattern, the linker
5149will use the first match in the linker script. For example, this
5150sequence of input section descriptions is probably in error, because the
5151@file{data.o} rule will not be used:
5152@smallexample
5153.data : @{ *(.data) @}
5154.data1 : @{ data.o(.data) @}
5155@end smallexample
5156
bcaa7b3e 5157@cindex SORT_BY_NAME
252b5132
RH
5158Normally, the linker will place files and sections matched by wildcards
5159in the order in which they are seen during the link. You can change
bcaa7b3e
L
5160this by using the @code{SORT_BY_NAME} keyword, which appears before a wildcard
5161pattern in parentheses (e.g., @code{SORT_BY_NAME(.text*)}). When the
5162@code{SORT_BY_NAME} keyword is used, the linker will sort the files or sections
252b5132
RH
5163into ascending order by name before placing them in the output file.
5164
bcaa7b3e 5165@cindex SORT_BY_ALIGNMENT
9a24a276
AM
5166@code{SORT_BY_ALIGNMENT} is similar to @code{SORT_BY_NAME}.
5167@code{SORT_BY_ALIGNMENT} will sort sections into descending order of
5168alignment before placing them in the output file. Placing larger
5169alignments before smaller alignments can reduce the amount of padding
5170needed.
bcaa7b3e 5171
02ecc8e9 5172@cindex SORT_BY_INIT_PRIORITY
9a24a276
AM
5173@code{SORT_BY_INIT_PRIORITY} is also similar to @code{SORT_BY_NAME}.
5174@code{SORT_BY_INIT_PRIORITY} will sort sections into ascending
5175numerical order of the GCC init_priority attribute encoded in the
5176section name before placing them in the output file. In
5177@code{.init_array.NNNNN} and @code{.fini_array.NNNNN}, @code{NNNNN} is
5178the init_priority. In @code{.ctors.NNNNN} and @code{.dtors.NNNNN},
5179@code{NNNNN} is 65535 minus the init_priority.
02ecc8e9 5180
bcaa7b3e
L
5181@cindex SORT
5182@code{SORT} is an alias for @code{SORT_BY_NAME}.
5183
5184When there are nested section sorting commands in linker script, there
5185can be at most 1 level of nesting for section sorting commands.
5186
5187@enumerate
5188@item
5189@code{SORT_BY_NAME} (@code{SORT_BY_ALIGNMENT} (wildcard section pattern)).
1ae5c3ae 5190It will sort the input sections by name first, then by alignment if two
bcaa7b3e
L
5191sections have the same name.
5192@item
5193@code{SORT_BY_ALIGNMENT} (@code{SORT_BY_NAME} (wildcard section pattern)).
1ae5c3ae 5194It will sort the input sections by alignment first, then by name if two
bcaa7b3e
L
5195sections have the same alignment.
5196@item
c0065db7 5197@code{SORT_BY_NAME} (@code{SORT_BY_NAME} (wildcard section pattern)) is
bcaa7b3e
L
5198treated the same as @code{SORT_BY_NAME} (wildcard section pattern).
5199@item
5200@code{SORT_BY_ALIGNMENT} (@code{SORT_BY_ALIGNMENT} (wildcard section pattern))
5201is treated the same as @code{SORT_BY_ALIGNMENT} (wildcard section pattern).
5202@item
5203All other nested section sorting commands are invalid.
5204@end enumerate
5205
a05a5b64 5206When both command-line section sorting option and linker script
bcaa7b3e 5207section sorting command are used, section sorting command always
a05a5b64 5208takes precedence over the command-line option.
bcaa7b3e
L
5209
5210If the section sorting command in linker script isn't nested, the
a05a5b64 5211command-line option will make the section sorting command to be
bcaa7b3e
L
5212treated as nested sorting command.
5213
5214@enumerate
5215@item
5216@code{SORT_BY_NAME} (wildcard section pattern ) with
5217@option{--sort-sections alignment} is equivalent to
5218@code{SORT_BY_NAME} (@code{SORT_BY_ALIGNMENT} (wildcard section pattern)).
5219@item
5220@code{SORT_BY_ALIGNMENT} (wildcard section pattern) with
5221@option{--sort-section name} is equivalent to
5222@code{SORT_BY_ALIGNMENT} (@code{SORT_BY_NAME} (wildcard section pattern)).
5223@end enumerate
5224
5225If the section sorting command in linker script is nested, the
a05a5b64 5226command-line option will be ignored.
bcaa7b3e 5227
eda680f8 5228@cindex SORT_NONE
a05a5b64 5229@code{SORT_NONE} disables section sorting by ignoring the command-line
eda680f8
L
5230section sorting option.
5231
252b5132
RH
5232If you ever get confused about where input sections are going, use the
5233@samp{-M} linker option to generate a map file. The map file shows
5234precisely how input sections are mapped to output sections.
5235
5236This example shows how wildcard patterns might be used to partition
5237files. This linker script directs the linker to place all @samp{.text}
5238sections in @samp{.text} and all @samp{.bss} sections in @samp{.bss}.
5239The linker will place the @samp{.data} section from all files beginning
5240with an upper case character in @samp{.DATA}; for all other files, the
5241linker will place the @samp{.data} section in @samp{.data}.
5242@smallexample
5243@group
5244SECTIONS @{
5245 .text : @{ *(.text) @}
5246 .DATA : @{ [A-Z]*(.data) @}
5247 .data : @{ *(.data) @}
5248 .bss : @{ *(.bss) @}
5249@}
5250@end group
5251@end smallexample
5252
5253@node Input Section Common
36f63dca 5254@subsubsection Input Section for Common Symbols
252b5132
RH
5255@cindex common symbol placement
5256@cindex uninitialized data placement
5257A special notation is needed for common symbols, because in many object
5258file formats common symbols do not have a particular input section. The
5259linker treats common symbols as though they are in an input section
5260named @samp{COMMON}.
5261
5262You may use file names with the @samp{COMMON} section just as with any
5263other input sections. You can use this to place common symbols from a
5264particular input file in one section while common symbols from other
5265input files are placed in another section.
5266
5267In most cases, common symbols in input files will be placed in the
5268@samp{.bss} section in the output file. For example:
5269@smallexample
5270.bss @{ *(.bss) *(COMMON) @}
5271@end smallexample
5272
5273@cindex scommon section
5274@cindex small common symbols
5275Some object file formats have more than one type of common symbol. For
5276example, the MIPS ELF object file format distinguishes standard common
5277symbols and small common symbols. In this case, the linker will use a
5278different special section name for other types of common symbols. In
5279the case of MIPS ELF, the linker uses @samp{COMMON} for standard common
5280symbols and @samp{.scommon} for small common symbols. This permits you
5281to map the different types of common symbols into memory at different
5282locations.
5283
5284@cindex [COMMON]
5285You will sometimes see @samp{[COMMON]} in old linker scripts. This
5286notation is now considered obsolete. It is equivalent to
5287@samp{*(COMMON)}.
5288
5289@node Input Section Keep
36f63dca 5290@subsubsection Input Section and Garbage Collection
252b5132
RH
5291@cindex KEEP
5292@cindex garbage collection
5293When link-time garbage collection is in use (@samp{--gc-sections}),
a1ab1d2a 5294it is often useful to mark sections that should not be eliminated.
252b5132
RH
5295This is accomplished by surrounding an input section's wildcard entry
5296with @code{KEEP()}, as in @code{KEEP(*(.init))} or
bcaa7b3e 5297@code{KEEP(SORT_BY_NAME(*)(.ctors))}.
252b5132
RH
5298
5299@node Input Section Example
36f63dca 5300@subsubsection Input Section Example
252b5132
RH
5301The following example is a complete linker script. It tells the linker
5302to read all of the sections from file @file{all.o} and place them at the
5303start of output section @samp{outputa} which starts at location
5304@samp{0x10000}. All of section @samp{.input1} from file @file{foo.o}
5305follows immediately, in the same output section. All of section
5306@samp{.input2} from @file{foo.o} goes into output section
5307@samp{outputb}, followed by section @samp{.input1} from @file{foo1.o}.
5308All of the remaining @samp{.input1} and @samp{.input2} sections from any
5309files are written to output section @samp{outputc}.
5310
5311@smallexample
5312@group
5313SECTIONS @{
5314 outputa 0x10000 :
5315 @{
5316 all.o
5317 foo.o (.input1)
5318 @}
36f63dca
NC
5319@end group
5320@group
252b5132
RH
5321 outputb :
5322 @{
5323 foo.o (.input2)
5324 foo1.o (.input1)
5325 @}
36f63dca
NC
5326@end group
5327@group
252b5132
RH
5328 outputc :
5329 @{
5330 *(.input1)
5331 *(.input2)
5332 @}
5333@}
5334@end group
a1ab1d2a 5335@end smallexample
252b5132 5336
cbd0eecf
L
5337If an output section's name is the same as the input section's name
5338and is representable as a C identifier, then the linker will
5339automatically @pxref{PROVIDE} two symbols: __start_SECNAME and
5340__stop_SECNAME, where SECNAME is the name of the section. These
5341indicate the start address and end address of the output section
5342respectively. Note: most section names are not representable as
5343C identifiers because they contain a @samp{.} character.
75bd292b 5344
252b5132 5345@node Output Section Data
36f63dca 5346@subsection Output Section Data
252b5132
RH
5347@cindex data
5348@cindex section data
5349@cindex output section data
a4d5aec7 5350@kindex ASCIZ ``@var{string}''
252b5132
RH
5351@kindex BYTE(@var{expression})
5352@kindex SHORT(@var{expression})
5353@kindex LONG(@var{expression})
5354@kindex QUAD(@var{expression})
5355@kindex SQUAD(@var{expression})
5356You can include explicit bytes of data in an output section by using
5357@code{BYTE}, @code{SHORT}, @code{LONG}, @code{QUAD}, or @code{SQUAD} as
5358an output section command. Each keyword is followed by an expression in
5359parentheses providing the value to store (@pxref{Expressions}). The
5360value of the expression is stored at the current value of the location
5361counter.
5362
5363The @code{BYTE}, @code{SHORT}, @code{LONG}, and @code{QUAD} commands
5364store one, two, four, and eight bytes (respectively). After storing the
5365bytes, the location counter is incremented by the number of bytes
5366stored.
5367
5368For example, this will store the byte 1 followed by the four byte value
5369of the symbol @samp{addr}:
5370@smallexample
5371BYTE(1)
5372LONG(addr)
5373@end smallexample
5374
5375When using a 64 bit host or target, @code{QUAD} and @code{SQUAD} are the
5376same; they both store an 8 byte, or 64 bit, value. When both host and
5377target are 32 bits, an expression is computed as 32 bits. In this case
5378@code{QUAD} stores a 32 bit value zero extended to 64 bits, and
5379@code{SQUAD} stores a 32 bit value sign extended to 64 bits.
5380
5381If the object file format of the output file has an explicit endianness,
5382which is the normal case, the value will be stored in that endianness.
5383When the object file format does not have an explicit endianness, as is
5384true of, for example, S-records, the value will be stored in the
5385endianness of the first input object file.
5386
a4d5aec7
AM
5387You can include a zero-terminated string in an output section by using
5388@code{ASCIZ}. The keyword is followed by a string which is stored at
5389the current value of the location counter adding a zero byte at the
5390end. If the string includes spaces it must be enclosed in double
5391quotes. The string may contain '\n', '\r', '\t' and octal numbers.
5392Hex numbers are not supported.
5393
5394For example, this string of 16 characters will create a 17 byte area
5395@smallexample
5396 ASCIZ "This is 16 bytes"
5397@end smallexample
5398
36f63dca 5399Note---these commands only work inside a section description and not
2b5fc1f5
NC
5400between them, so the following will produce an error from the linker:
5401@smallexample
5402SECTIONS @{@ .text : @{@ *(.text) @}@ LONG(1) .data : @{@ *(.data) @}@ @}@
5403@end smallexample
5404whereas this will work:
5405@smallexample
5406SECTIONS @{@ .text : @{@ *(.text) ; LONG(1) @}@ .data : @{@ *(.data) @}@ @}@
5407@end smallexample
75bd292b 5408
252b5132
RH
5409@kindex FILL(@var{expression})
5410@cindex holes, filling
5411@cindex unspecified memory
5412You may use the @code{FILL} command to set the fill pattern for the
5413current section. It is followed by an expression in parentheses. Any
5414otherwise unspecified regions of memory within the section (for example,
5415gaps left due to the required alignment of input sections) are filled
a139d329 5416with the value of the expression, repeated as
252b5132
RH
5417necessary. A @code{FILL} statement covers memory locations after the
5418point at which it occurs in the section definition; by including more
5419than one @code{FILL} statement, you can have different fill patterns in
5420different parts of an output section.
5421
5422This example shows how to fill unspecified regions of memory with the
563e308f 5423value @samp{0x90}:
252b5132 5424@smallexample
563e308f 5425FILL(0x90909090)
252b5132
RH
5426@end smallexample
5427
5428The @code{FILL} command is similar to the @samp{=@var{fillexp}} output
9673c93c 5429section attribute, but it only affects the
252b5132
RH
5430part of the section following the @code{FILL} command, rather than the
5431entire section. If both are used, the @code{FILL} command takes
9673c93c 5432precedence. @xref{Output Section Fill}, for details on the fill
a139d329 5433expression.
252b5132 5434
2d5783fa
NC
5435@kindex LINKER_VERSION
5436@cindex LINKER_VERSION
5437Inserts a string containing the version of the linker at the current
5438point. Note - by default this directive is disabled and will do
5439nothing. It only becomes active if the
5440@option{--enable-linker-version} command line option is used.
5441
5442Built-in linker scripts for ELF based targets already include this
5443directive in their @samp{.comment} section.
5444
252b5132 5445@node Output Section Keywords
36f63dca 5446@subsection Output Section Keywords
252b5132
RH
5447There are a couple of keywords which can appear as output section
5448commands.
5449
5450@table @code
5451@kindex CREATE_OBJECT_SYMBOLS
5452@cindex input filename symbols
5453@cindex filename symbols
5454@item CREATE_OBJECT_SYMBOLS
5455The command tells the linker to create a symbol for each input file.
5456The name of each symbol will be the name of the corresponding input
5457file. The section of each symbol will be the output section in which
5458the @code{CREATE_OBJECT_SYMBOLS} command appears.
5459
5460This is conventional for the a.out object file format. It is not
5461normally used for any other object file format.
5462
5463@kindex CONSTRUCTORS
5464@cindex C++ constructors, arranging in link
5465@cindex constructors, arranging in link
5466@item CONSTRUCTORS
5467When linking using the a.out object file format, the linker uses an
5468unusual set construct to support C++ global constructors and
5469destructors. When linking object file formats which do not support
5470arbitrary sections, such as ECOFF and XCOFF, the linker will
5471automatically recognize C++ global constructors and destructors by name.
5472For these object file formats, the @code{CONSTRUCTORS} command tells the
5473linker to place constructor information in the output section where the
5474@code{CONSTRUCTORS} command appears. The @code{CONSTRUCTORS} command is
5475ignored for other object file formats.
5476
5477The symbol @w{@code{__CTOR_LIST__}} marks the start of the global
7e69709c
AM
5478constructors, and the symbol @w{@code{__CTOR_END__}} marks the end.
5479Similarly, @w{@code{__DTOR_LIST__}} and @w{@code{__DTOR_END__}} mark
5480the start and end of the global destructors. The
252b5132
RH
5481first word in the list is the number of entries, followed by the address
5482of each constructor or destructor, followed by a zero word. The
5483compiler must arrange to actually run the code. For these object file
5484formats @sc{gnu} C++ normally calls constructors from a subroutine
5485@code{__main}; a call to @code{__main} is automatically inserted into
5486the startup code for @code{main}. @sc{gnu} C++ normally runs
5487destructors either by using @code{atexit}, or directly from the function
5488@code{exit}.
5489
5490For object file formats such as @code{COFF} or @code{ELF} which support
5491arbitrary section names, @sc{gnu} C++ will normally arrange to put the
5492addresses of global constructors and destructors into the @code{.ctors}
5493and @code{.dtors} sections. Placing the following sequence into your
5494linker script will build the sort of table which the @sc{gnu} C++
5495runtime code expects to see.
5496
5497@smallexample
5498 __CTOR_LIST__ = .;
5499 LONG((__CTOR_END__ - __CTOR_LIST__) / 4 - 2)
5500 *(.ctors)
5501 LONG(0)
5502 __CTOR_END__ = .;
5503 __DTOR_LIST__ = .;
5504 LONG((__DTOR_END__ - __DTOR_LIST__) / 4 - 2)
5505 *(.dtors)
5506 LONG(0)
5507 __DTOR_END__ = .;
5508@end smallexample
5509
5510If you are using the @sc{gnu} C++ support for initialization priority,
5511which provides some control over the order in which global constructors
5512are run, you must sort the constructors at link time to ensure that they
5513are executed in the correct order. When using the @code{CONSTRUCTORS}
bcaa7b3e
L
5514command, use @samp{SORT_BY_NAME(CONSTRUCTORS)} instead. When using the
5515@code{.ctors} and @code{.dtors} sections, use @samp{*(SORT_BY_NAME(.ctors))} and
5516@samp{*(SORT_BY_NAME(.dtors))} instead of just @samp{*(.ctors)} and
252b5132
RH
5517@samp{*(.dtors)}.
5518
5519Normally the compiler and linker will handle these issues automatically,
5520and you will not need to concern yourself with them. However, you may
5521need to consider this if you are using C++ and writing your own linker
5522scripts.
5523
5524@end table
5525
5526@node Output Section Discarding
36f63dca 5527@subsection Output Section Discarding
252b5132
RH
5528@cindex discarding sections
5529@cindex sections, discarding
5530@cindex removing sections
2edab91c
AM
5531The linker will not normally create output sections with no contents.
5532This is for convenience when referring to input sections that may or
5533may not be present in any of the input files. For example:
252b5132 5534@smallexample
49c13adb 5535.foo : @{ *(.foo) @}
252b5132
RH
5536@end smallexample
5537@noindent
5538will only create a @samp{.foo} section in the output file if there is a
74541ad4
AM
5539@samp{.foo} section in at least one input file, and if the input
5540sections are not all empty. Other link script directives that allocate
2edab91c
AM
5541space in an output section will also create the output section. So
5542too will assignments to dot even if the assignment does not create
5543space, except for @samp{. = 0}, @samp{. = . + 0}, @samp{. = sym},
5544@samp{. = . + sym} and @samp{. = ALIGN (. != 0, expr, 1)} when
5545@samp{sym} is an absolute symbol of value 0 defined in the script.
5546This allows you to force output of an empty section with @samp{. = .}.
74541ad4 5547
a0976ea4 5548The linker will ignore address assignments (@pxref{Output Section Address})
74541ad4
AM
5549on discarded output sections, except when the linker script defines
5550symbols in the output section. In that case the linker will obey
a0976ea4
AM
5551the address assignments, possibly advancing dot even though the
5552section is discarded.
252b5132
RH
5553
5554@cindex /DISCARD/
5555The special output section name @samp{/DISCARD/} may be used to discard
5556input sections. Any input sections which are assigned to an output
5557section named @samp{/DISCARD/} are not included in the output file.
5558
99fabbc9
JL
5559This can be used to discard input sections marked with the ELF flag
5560@code{SHF_GNU_RETAIN}, which would otherwise have been saved from linker
5561garbage collection.
5562
a2e098c3
NC
5563Note, sections that match the @samp{/DISCARD/} output section will be
5564discarded even if they are in an ELF section group which has other
5565members which are not being discarded. This is deliberate.
5566Discarding takes precedence over grouping.
5567
252b5132 5568@node Output Section Attributes
36f63dca 5569@subsection Output Section Attributes
252b5132
RH
5570@cindex output section attributes
5571We showed above that the full description of an output section looked
5572like this:
0c71d759 5573
252b5132 5574@smallexample
a1ab1d2a 5575@group
7e7d5768 5576@var{section} [@var{address}] [(@var{type})] :
0c71d759 5577 [AT(@var{lma})]
3bbec4bd 5578 [ALIGN(@var{section_align}) | ALIGN_WITH_INPUT]
0c71d759
NC
5579 [SUBALIGN(@var{subsection_align})]
5580 [@var{constraint}]
252b5132
RH
5581 @{
5582 @var{output-section-command}
5583 @var{output-section-command}
5584 @dots{}
562d3460 5585 @} [>@var{region}] [AT>@var{lma_region}] [:@var{phdr} :@var{phdr} @dots{}] [=@var{fillexp}]
252b5132
RH
5586@end group
5587@end smallexample
0c71d759 5588
252b5132
RH
5589We've already described @var{section}, @var{address}, and
5590@var{output-section-command}. In this section we will describe the
5591remaining section attributes.
5592
a1ab1d2a 5593@menu
252b5132
RH
5594* Output Section Type:: Output section type
5595* Output Section LMA:: Output section LMA
bbf115d3 5596* Forced Output Alignment:: Forced Output Alignment
7e7d5768 5597* Forced Input Alignment:: Forced Input Alignment
0c71d759 5598* Output Section Constraint:: Output section constraint
252b5132
RH
5599* Output Section Region:: Output section region
5600* Output Section Phdr:: Output section phdr
5601* Output Section Fill:: Output section fill
5602@end menu
5603
5604@node Output Section Type
36f63dca 5605@subsubsection Output Section Type
252b5132
RH
5606Each output section may have a type. The type is a keyword in
5607parentheses. The following types are defined:
5608
5609@table @code
dcb53d93 5610
252b5132
RH
5611@item NOLOAD
5612The section should be marked as not loadable, so that it will not be
5613loaded into memory when the program is run.
dcb53d93 5614
6b86da53
LB
5615@item READONLY
5616The section should be marked as read-only.
dcb53d93 5617
252b5132 5618@item DSECT
c212f39d
FS
5619@item COPY
5620@item INFO
5621@item OVERLAY
252b5132
RH
5622These type names are supported for backward compatibility, and are
5623rarely used. They all have the same effect: the section should be
5624marked as not allocatable, so that no memory is allocated for the
5625section when the program is run.
dcb53d93 5626
c212f39d
FS
5627@item TYPE = @var{type}
5628Set the section type to the integer @var{type}. When generating an ELF
5629output file, type names @code{SHT_PROGBITS}, @code{SHT_STRTAB},
5c14cc55 5630@code{SHT_NOTE}, @code{SHT_NOBITS}, @code{SHT_INIT_ARRAY},
c212f39d
FS
5631@code{SHT_FINI_ARRAY}, and @code{SHT_PREINIT_ARRAY} are also allowed
5632for @var{type}. It is the user's responsibility to ensure that any
5633special requirements of the section type are met.
dcb53d93
NC
5634
5635Note - the TYPE only is used if some or all of the contents of the
5636section do not have an implicit type of their own. So for example:
5637@smallexample
5638 .foo . TYPE = SHT_PROGBITS @{ *(.bar) @}
5639@end smallexample
5640will set the type of section @samp{.foo} to the type of the section
5641@samp{.bar} in the input files, which may not be the SHT_PROGBITS
5642type. Whereas:
5643@smallexample
5644 .foo . TYPE = SHT_PROGBITS @{ BYTE(1) @}
5645@end smallexample
5646will set the type of @samp{.foo} to SHT_PROGBBITS. If it is necessary
5647to override the type of incoming sections and force the output section
5648type then an extra piece of untyped data will be needed:
5649@smallexample
5650 .foo . TYPE = SHT_PROGBITS @{ BYTE(1); *(.bar) @}
5651@end smallexample
5652
c212f39d
FS
5653@item READONLY ( TYPE = @var{type} )
5654This form of the syntax combines the @var{READONLY} type with the
5655type specified by @var{type}.
dcb53d93 5656
252b5132
RH
5657@end table
5658
5659@kindex NOLOAD
5660@cindex prevent unnecessary loading
5661@cindex loading, preventing
5662The linker normally sets the attributes of an output section based on
5663the input sections which map into it. You can override this by using
5664the section type. For example, in the script sample below, the
5665@samp{ROM} section is addressed at memory location @samp{0} and does not
2e76e85a 5666need to be loaded when the program is run.
252b5132
RH
5667@smallexample
5668@group
5669SECTIONS @{
5670 ROM 0 (NOLOAD) : @{ @dots{} @}
5671 @dots{}
5672@}
5673@end group
5674@end smallexample
5675
5676@node Output Section LMA
36f63dca 5677@subsubsection Output Section LMA
562d3460 5678@kindex AT>@var{lma_region}
252b5132
RH
5679@kindex AT(@var{lma})
5680@cindex load address
5681@cindex section load address
5682Every section has a virtual address (VMA) and a load address (LMA); see
ea5cae92
NC
5683@ref{Basic Script Concepts}. The virtual address is specified by the
5684@pxref{Output Section Address} described earlier. The load address is
5685specified by the @code{AT} or @code{AT>} keywords. Specifying a load
5686address is optional.
6bdafbeb 5687
ea5cae92
NC
5688The @code{AT} keyword takes an expression as an argument. This
5689specifies the exact load address of the section. The @code{AT>} keyword
5690takes the name of a memory region as an argument. @xref{MEMORY}. The
5691load address of the section is set to the next free address in the
5692region, aligned to the section's alignment requirements.
dc0b6aa0
AM
5693
5694If neither @code{AT} nor @code{AT>} is specified for an allocatable
ea5cae92
NC
5695section, the linker will use the following heuristic to determine the
5696load address:
5697
5698@itemize @bullet
5699@item
5700If the section has a specific VMA address, then this is used as
5701the LMA address as well.
5702
5703@item
5704If the section is not allocatable then its LMA is set to its VMA.
5705
5706@item
5707Otherwise if a memory region can be found that is compatible
5708with the current section, and this region contains at least one
5709section, then the LMA is set so the difference between the
5710VMA and LMA is the same as the difference between the VMA and LMA of
5711the last section in the located region.
5712
5713@item
5714If no memory regions have been declared then a default region
5715that covers the entire address space is used in the previous step.
5716
5717@item
5718If no suitable region could be found, or there was no previous
5719section then the LMA is set equal to the VMA.
5720@end itemize
252b5132
RH
5721
5722@cindex ROM initialized data
5723@cindex initialized data in ROM
5724This feature is designed to make it easy to build a ROM image. For
5725example, the following linker script creates three output sections: one
5726called @samp{.text}, which starts at @code{0x1000}, one called
5727@samp{.mdata}, which is loaded at the end of the @samp{.text} section
5728even though its VMA is @code{0x2000}, and one called @samp{.bss} to hold
5729uninitialized data at address @code{0x3000}. The symbol @code{_data} is
5730defined with the value @code{0x2000}, which shows that the location
5731counter holds the VMA value, not the LMA value.
5732
5733@smallexample
5734@group
5735SECTIONS
5736 @{
5737 .text 0x1000 : @{ *(.text) _etext = . ; @}
a1ab1d2a 5738 .mdata 0x2000 :
252b5132
RH
5739 AT ( ADDR (.text) + SIZEOF (.text) )
5740 @{ _data = . ; *(.data); _edata = . ; @}
5741 .bss 0x3000 :
5742 @{ _bstart = . ; *(.bss) *(COMMON) ; _bend = . ;@}
5743@}
5744@end group
5745@end smallexample
5746
5747The run-time initialization code for use with a program generated with
5748this linker script would include something like the following, to copy
5749the initialized data from the ROM image to its runtime address. Notice
5750how this code takes advantage of the symbols defined by the linker
5751script.
5752
5753@smallexample
5754@group
5755extern char _etext, _data, _edata, _bstart, _bend;
5756char *src = &_etext;
5757char *dst = &_data;
5758
ea5cae92
NC
5759/* ROM has data at end of text; copy it. */
5760while (dst < &_edata)
252b5132 5761 *dst++ = *src++;
252b5132 5762
ea5cae92 5763/* Zero bss. */
252b5132
RH
5764for (dst = &_bstart; dst< &_bend; dst++)
5765 *dst = 0;
5766@end group
5767@end smallexample
5768
bbf115d3
L
5769@node Forced Output Alignment
5770@subsubsection Forced Output Alignment
5771@kindex ALIGN(@var{section_align})
5772@cindex forcing output section alignment
5773@cindex output section alignment
1eec346e 5774You can increase an output section's alignment by using ALIGN. As an
13075d04
SH
5775alternative you can enforce that the difference between the VMA and LMA remains
5776intact throughout this output section with the ALIGN_WITH_INPUT attribute.
bbf115d3 5777
7e7d5768
AM
5778@node Forced Input Alignment
5779@subsubsection Forced Input Alignment
5780@kindex SUBALIGN(@var{subsection_align})
5781@cindex forcing input section alignment
5782@cindex input section alignment
5783You can force input section alignment within an output section by using
5784SUBALIGN. The value specified overrides any alignment given by input
5785sections, whether larger or smaller.
5786
0c71d759
NC
5787@node Output Section Constraint
5788@subsubsection Output Section Constraint
5789@kindex ONLY_IF_RO
5790@kindex ONLY_IF_RW
5791@cindex constraints on output sections
5792You can specify that an output section should only be created if all
5793of its input sections are read-only or all of its input sections are
5794read-write by using the keyword @code{ONLY_IF_RO} and
5795@code{ONLY_IF_RW} respectively.
5796
252b5132 5797@node Output Section Region
36f63dca 5798@subsubsection Output Section Region
252b5132
RH
5799@kindex >@var{region}
5800@cindex section, assigning to memory region
5801@cindex memory regions and sections
5802You can assign a section to a previously defined region of memory by
5803using @samp{>@var{region}}. @xref{MEMORY}.
5804
5805Here is a simple example:
5806@smallexample
5807@group
5808MEMORY @{ rom : ORIGIN = 0x1000, LENGTH = 0x1000 @}
5809SECTIONS @{ ROM : @{ *(.text) @} >rom @}
5810@end group
5811@end smallexample
5812
5813@node Output Section Phdr
36f63dca 5814@subsubsection Output Section Phdr
252b5132
RH
5815@kindex :@var{phdr}
5816@cindex section, assigning to program header
5817@cindex program headers and sections
5818You can assign a section to a previously defined program segment by
5819using @samp{:@var{phdr}}. @xref{PHDRS}. If a section is assigned to
5820one or more segments, then all subsequent allocated sections will be
5821assigned to those segments as well, unless they use an explicitly
5822@code{:@var{phdr}} modifier. You can use @code{:NONE} to tell the
5823linker to not put the section in any segment at all.
5824
5825Here is a simple example:
5826@smallexample
5827@group
5828PHDRS @{ text PT_LOAD ; @}
5829SECTIONS @{ .text : @{ *(.text) @} :text @}
5830@end group
5831@end smallexample
5832
5833@node Output Section Fill
36f63dca 5834@subsubsection Output Section Fill
252b5132
RH
5835@kindex =@var{fillexp}
5836@cindex section fill pattern
5837@cindex fill pattern, entire section
5838You can set the fill pattern for an entire section by using
5839@samp{=@var{fillexp}}. @var{fillexp} is an expression
5840(@pxref{Expressions}). Any otherwise unspecified regions of memory
5841within the output section (for example, gaps left due to the required
a139d329
AM
5842alignment of input sections) will be filled with the value, repeated as
5843necessary. If the fill expression is a simple hex number, ie. a string
9673c93c 5844of hex digit starting with @samp{0x} and without a trailing @samp{k} or @samp{M}, then
a139d329
AM
5845an arbitrarily long sequence of hex digits can be used to specify the
5846fill pattern; Leading zeros become part of the pattern too. For all
9673c93c 5847other cases, including extra parentheses or a unary @code{+}, the fill
a139d329
AM
5848pattern is the four least significant bytes of the value of the
5849expression. In all cases, the number is big-endian.
252b5132
RH
5850
5851You can also change the fill value with a @code{FILL} command in the
9673c93c 5852output section commands; (@pxref{Output Section Data}).
252b5132
RH
5853
5854Here is a simple example:
5855@smallexample
5856@group
563e308f 5857SECTIONS @{ .text : @{ *(.text) @} =0x90909090 @}
252b5132
RH
5858@end group
5859@end smallexample
5860
5861@node Overlay Description
36f63dca 5862@subsection Overlay Description
252b5132
RH
5863@kindex OVERLAY
5864@cindex overlays
5865An overlay description provides an easy way to describe sections which
5866are to be loaded as part of a single memory image but are to be run at
5867the same memory address. At run time, some sort of overlay manager will
5868copy the overlaid sections in and out of the runtime memory address as
5869required, perhaps by simply manipulating addressing bits. This approach
5870can be useful, for example, when a certain region of memory is faster
5871than another.
5872
5873Overlays are described using the @code{OVERLAY} command. The
5874@code{OVERLAY} command is used within a @code{SECTIONS} command, like an
5875output section description. The full syntax of the @code{OVERLAY}
5876command is as follows:
5877@smallexample
5878@group
5879OVERLAY [@var{start}] : [NOCROSSREFS] [AT ( @var{ldaddr} )]
5880 @{
5881 @var{secname1}
5882 @{
5883 @var{output-section-command}
5884 @var{output-section-command}
5885 @dots{}
5886 @} [:@var{phdr}@dots{}] [=@var{fill}]
5887 @var{secname2}
5888 @{
5889 @var{output-section-command}
5890 @var{output-section-command}
5891 @dots{}
5892 @} [:@var{phdr}@dots{}] [=@var{fill}]
5893 @dots{}
abc9061b 5894 @} [>@var{region}] [:@var{phdr}@dots{}] [=@var{fill}] [,]
252b5132
RH
5895@end group
5896@end smallexample
5897
5898Everything is optional except @code{OVERLAY} (a keyword), and each
5899section must have a name (@var{secname1} and @var{secname2} above). The
5900section definitions within the @code{OVERLAY} construct are identical to
11e7fd74 5901those within the general @code{SECTIONS} construct (@pxref{SECTIONS}),
252b5132
RH
5902except that no addresses and no memory regions may be defined for
5903sections within an @code{OVERLAY}.
5904
abc9061b
CC
5905The comma at the end may be required if a @var{fill} is used and
5906the next @var{sections-command} looks like a continuation of the expression.
5907
252b5132
RH
5908The sections are all defined with the same starting address. The load
5909addresses of the sections are arranged such that they are consecutive in
5910memory starting at the load address used for the @code{OVERLAY} as a
5911whole (as with normal section definitions, the load address is optional,
5912and defaults to the start address; the start address is also optional,
5913and defaults to the current value of the location counter).
5914
56dd11f0
NC
5915If the @code{NOCROSSREFS} keyword is used, and there are any
5916references among the sections, the linker will report an error. Since
5917the sections all run at the same address, it normally does not make
5918sense for one section to refer directly to another.
5919@xref{Miscellaneous Commands, NOCROSSREFS}.
252b5132
RH
5920
5921For each section within the @code{OVERLAY}, the linker automatically
34711ca3 5922provides two symbols. The symbol @code{__load_start_@var{secname}} is
252b5132
RH
5923defined as the starting load address of the section. The symbol
5924@code{__load_stop_@var{secname}} is defined as the final load address of
5925the section. Any characters within @var{secname} which are not legal
5926within C identifiers are removed. C (or assembler) code may use these
5927symbols to move the overlaid sections around as necessary.
5928
5929At the end of the overlay, the value of the location counter is set to
5930the start address of the overlay plus the size of the largest section.
5931
5932Here is an example. Remember that this would appear inside a
5933@code{SECTIONS} construct.
5934@smallexample
5935@group
5936 OVERLAY 0x1000 : AT (0x4000)
5937 @{
5938 .text0 @{ o1/*.o(.text) @}
5939 .text1 @{ o2/*.o(.text) @}
5940 @}
5941@end group
5942@end smallexample
5943@noindent
5944This will define both @samp{.text0} and @samp{.text1} to start at
5945address 0x1000. @samp{.text0} will be loaded at address 0x4000, and
5946@samp{.text1} will be loaded immediately after @samp{.text0}. The
34711ca3 5947following symbols will be defined if referenced: @code{__load_start_text0},
252b5132
RH
5948@code{__load_stop_text0}, @code{__load_start_text1},
5949@code{__load_stop_text1}.
5950
5951C code to copy overlay @code{.text1} into the overlay area might look
5952like the following.
5953
5954@smallexample
5955@group
5956 extern char __load_start_text1, __load_stop_text1;
5957 memcpy ((char *) 0x1000, &__load_start_text1,
5958 &__load_stop_text1 - &__load_start_text1);
5959@end group
5960@end smallexample
5961
5962Note that the @code{OVERLAY} command is just syntactic sugar, since
5963everything it does can be done using the more basic commands. The above
5964example could have been written identically as follows.
5965
5966@smallexample
5967@group
5968 .text0 0x1000 : AT (0x4000) @{ o1/*.o(.text) @}
34711ca3
AM
5969 PROVIDE (__load_start_text0 = LOADADDR (.text0));
5970 PROVIDE (__load_stop_text0 = LOADADDR (.text0) + SIZEOF (.text0));
252b5132 5971 .text1 0x1000 : AT (0x4000 + SIZEOF (.text0)) @{ o2/*.o(.text) @}
34711ca3
AM
5972 PROVIDE (__load_start_text1 = LOADADDR (.text1));
5973 PROVIDE (__load_stop_text1 = LOADADDR (.text1) + SIZEOF (.text1));
252b5132
RH
5974 . = 0x1000 + MAX (SIZEOF (.text0), SIZEOF (.text1));
5975@end group
5976@end smallexample
5977
5978@node MEMORY
36f63dca 5979@section MEMORY Command
252b5132
RH
5980@kindex MEMORY
5981@cindex memory regions
5982@cindex regions of memory
5983@cindex allocating memory
5984@cindex discontinuous memory
5985The linker's default configuration permits allocation of all available
5986memory. You can override this by using the @code{MEMORY} command.
5987
5988The @code{MEMORY} command describes the location and size of blocks of
5989memory in the target. You can use it to describe which memory regions
5990may be used by the linker, and which memory regions it must avoid. You
5991can then assign sections to particular memory regions. The linker will
5992set section addresses based on the memory regions, and will warn about
5993regions that become too full. The linker will not shuffle sections
5994around to fit into the available regions.
5995
127fcdff
AB
5996A linker script may contain many uses of the @code{MEMORY} command,
5997however, all memory blocks defined are treated as if they were
5998specified inside a single @code{MEMORY} command. The syntax for
5999@code{MEMORY} is:
252b5132
RH
6000@smallexample
6001@group
a1ab1d2a 6002MEMORY
252b5132
RH
6003 @{
6004 @var{name} [(@var{attr})] : ORIGIN = @var{origin}, LENGTH = @var{len}
6005 @dots{}
6006 @}
6007@end group
6008@end smallexample
6009
6010The @var{name} is a name used in the linker script to refer to the
6011region. The region name has no meaning outside of the linker script.
6012Region names are stored in a separate name space, and will not conflict
6013with symbol names, file names, or section names. Each memory region
4a93e180
NC
6014must have a distinct name within the @code{MEMORY} command. However you can
6015add later alias names to existing memory regions with the @ref{REGION_ALIAS}
9d5777a3 6016command.
252b5132
RH
6017
6018@cindex memory region attributes
6019The @var{attr} string is an optional list of attributes that specify
6020whether to use a particular memory region for an input section which is
6021not explicitly mapped in the linker script. As described in
6022@ref{SECTIONS}, if you do not specify an output section for some input
6023section, the linker will create an output section with the same name as
6024the input section. If you define region attributes, the linker will use
6025them to select the memory region for the output section that it creates.
6026
6027The @var{attr} string must consist only of the following characters:
6028@table @samp
6029@item R
6030Read-only section
6031@item W
6032Read/write section
6033@item X
6034Executable section
6035@item A
6036Allocatable section
6037@item I
6038Initialized section
6039@item L
6040Same as @samp{I}
6041@item !
c09e9a8c 6042Invert the sense of any of the attributes that follow
252b5132
RH
6043@end table
6044
81c688d5 6045If an unmapped section matches any of the listed attributes other than
252b5132 6046@samp{!}, it will be placed in the memory region. The @samp{!}
81c688d5
NC
6047attribute reverses the test for the characters that follow, so that an
6048unmapped section will be placed in the memory region only if it does
6049not match any of the attributes listed afterwards. Thus an attribute
6050string of @samp{RW!X} will match any unmapped section that has either
6051or both of the @samp{R} and @samp{W} attributes, but only as long as
6052the section does not also have the @samp{X} attribute.
252b5132
RH
6053
6054@kindex ORIGIN =
6055@kindex o =
6056@kindex org =
9cd6d51a
NC
6057The @var{origin} is an numerical expression for the start address of
6058the memory region. The expression must evaluate to a constant and it
6059cannot involve any symbols. The keyword @code{ORIGIN} may be
6060abbreviated to @code{org} or @code{o} (but not, for example,
6061@code{ORG}).
252b5132
RH
6062
6063@kindex LENGTH =
6064@kindex len =
6065@kindex l =
6066The @var{len} is an expression for the size in bytes of the memory
6067region. As with the @var{origin} expression, the expression must
9cd6d51a
NC
6068be numerical only and must evaluate to a constant. The keyword
6069@code{LENGTH} may be abbreviated to @code{len} or @code{l}.
252b5132
RH
6070
6071In the following example, we specify that there are two memory regions
6072available for allocation: one starting at @samp{0} for 256 kilobytes,
6073and the other starting at @samp{0x40000000} for four megabytes. The
6074linker will place into the @samp{rom} memory region every section which
6075is not explicitly mapped into a memory region, and is either read-only
6076or executable. The linker will place other sections which are not
6077explicitly mapped into a memory region into the @samp{ram} memory
6078region.
6079
6080@smallexample
6081@group
a1ab1d2a 6082MEMORY
252b5132
RH
6083 @{
6084 rom (rx) : ORIGIN = 0, LENGTH = 256K
6085 ram (!rx) : org = 0x40000000, l = 4M
6086 @}
6087@end group
6088@end smallexample
6089
6090Once you define a memory region, you can direct the linker to place
6091specific output sections into that memory region by using the
6092@samp{>@var{region}} output section attribute. For example, if you have
6093a memory region named @samp{mem}, you would use @samp{>mem} in the
6094output section definition. @xref{Output Section Region}. If no address
6095was specified for the output section, the linker will set the address to
6096the next available address within the memory region. If the combined
6097output sections directed to a memory region are too large for the
6098region, the linker will issue an error message.
6099
3ec57632 6100It is possible to access the origin and length of a memory in an
c0065db7 6101expression via the @code{ORIGIN(@var{memory})} and
3ec57632
NC
6102@code{LENGTH(@var{memory})} functions:
6103
6104@smallexample
6105@group
c0065db7 6106 _fstack = ORIGIN(ram) + LENGTH(ram) - 4;
3ec57632
NC
6107@end group
6108@end smallexample
6109
252b5132
RH
6110@node PHDRS
6111@section PHDRS Command
6112@kindex PHDRS
6113@cindex program headers
6114@cindex ELF program headers
6115@cindex program segments
6116@cindex segments, ELF
6117The ELF object file format uses @dfn{program headers}, also knows as
6118@dfn{segments}. The program headers describe how the program should be
6119loaded into memory. You can print them out by using the @code{objdump}
6120program with the @samp{-p} option.
6121
6122When you run an ELF program on a native ELF system, the system loader
6123reads the program headers in order to figure out how to load the
6124program. This will only work if the program headers are set correctly.
6125This manual does not describe the details of how the system loader
6126interprets program headers; for more information, see the ELF ABI.
6127
6128The linker will create reasonable program headers by default. However,
6129in some cases, you may need to specify the program headers more
6130precisely. You may use the @code{PHDRS} command for this purpose. When
6131the linker sees the @code{PHDRS} command in the linker script, it will
6132not create any program headers other than the ones specified.
6133
6134The linker only pays attention to the @code{PHDRS} command when
6135generating an ELF output file. In other cases, the linker will simply
6136ignore @code{PHDRS}.
6137
6138This is the syntax of the @code{PHDRS} command. The words @code{PHDRS},
6139@code{FILEHDR}, @code{AT}, and @code{FLAGS} are keywords.
6140
6141@smallexample
6142@group
6143PHDRS
6144@{
6145 @var{name} @var{type} [ FILEHDR ] [ PHDRS ] [ AT ( @var{address} ) ]
6146 [ FLAGS ( @var{flags} ) ] ;
6147@}
6148@end group
6149@end smallexample
6150
6151The @var{name} is used only for reference in the @code{SECTIONS} command
6152of the linker script. It is not put into the output file. Program
6153header names are stored in a separate name space, and will not conflict
6154with symbol names, file names, or section names. Each program header
5c1a3f0f
NS
6155must have a distinct name. The headers are processed in order and it
6156is usual for them to map to sections in ascending load address order.
252b5132
RH
6157
6158Certain program header types describe segments of memory which the
6159system loader will load from the file. In the linker script, you
6160specify the contents of these segments by placing allocatable output
6161sections in the segments. You use the @samp{:@var{phdr}} output section
6162attribute to place a section in a particular segment. @xref{Output
6163Section Phdr}.
6164
6165It is normal to put certain sections in more than one segment. This
6166merely implies that one segment of memory contains another. You may
6167repeat @samp{:@var{phdr}}, using it once for each segment which should
6168contain the section.
6169
6170If you place a section in one or more segments using @samp{:@var{phdr}},
6171then the linker will place all subsequent allocatable sections which do
6172not specify @samp{:@var{phdr}} in the same segments. This is for
6173convenience, since generally a whole set of contiguous sections will be
6174placed in a single segment. You can use @code{:NONE} to override the
6175default segment and tell the linker to not put the section in any
6176segment at all.
6177
6178@kindex FILEHDR
6179@kindex PHDRS
5c1a3f0f 6180You may use the @code{FILEHDR} and @code{PHDRS} keywords after
252b5132
RH
6181the program header type to further describe the contents of the segment.
6182The @code{FILEHDR} keyword means that the segment should include the ELF
6183file header. The @code{PHDRS} keyword means that the segment should
5c1a3f0f 6184include the ELF program headers themselves. If applied to a loadable
4100cea3
AM
6185segment (@code{PT_LOAD}), all prior loadable segments must have one of
6186these keywords.
252b5132
RH
6187
6188The @var{type} may be one of the following. The numbers indicate the
6189value of the keyword.
6190
6191@table @asis
6192@item @code{PT_NULL} (0)
6193Indicates an unused program header.
6194
6195@item @code{PT_LOAD} (1)
6196Indicates that this program header describes a segment to be loaded from
6197the file.
6198
6199@item @code{PT_DYNAMIC} (2)
6200Indicates a segment where dynamic linking information can be found.
6201
6202@item @code{PT_INTERP} (3)
6203Indicates a segment where the name of the program interpreter may be
6204found.
6205
6206@item @code{PT_NOTE} (4)
6207Indicates a segment holding note information.
6208
6209@item @code{PT_SHLIB} (5)
6210A reserved program header type, defined but not specified by the ELF
6211ABI.
6212
6213@item @code{PT_PHDR} (6)
6214Indicates a segment where the program headers may be found.
6215
1a9ccd70
NC
6216@item @code{PT_TLS} (7)
6217Indicates a segment containing thread local storage.
6218
252b5132
RH
6219@item @var{expression}
6220An expression giving the numeric type of the program header. This may
6221be used for types not defined above.
6222@end table
6223
6224You can specify that a segment should be loaded at a particular address
6225in memory by using an @code{AT} expression. This is identical to the
6226@code{AT} command used as an output section attribute (@pxref{Output
6227Section LMA}). The @code{AT} command for a program header overrides the
6228output section attribute.
6229
6230The linker will normally set the segment flags based on the sections
6231which comprise the segment. You may use the @code{FLAGS} keyword to
6232explicitly specify the segment flags. The value of @var{flags} must be
6233an integer. It is used to set the @code{p_flags} field of the program
6234header.
6235
6236Here is an example of @code{PHDRS}. This shows a typical set of program
6237headers used on a native ELF system.
6238
6239@example
6240@group
6241PHDRS
6242@{
6243 headers PT_PHDR PHDRS ;
6244 interp PT_INTERP ;
6245 text PT_LOAD FILEHDR PHDRS ;
6246 data PT_LOAD ;
6247 dynamic PT_DYNAMIC ;
6248@}
6249
6250SECTIONS
6251@{
6252 . = SIZEOF_HEADERS;
6253 .interp : @{ *(.interp) @} :text :interp
6254 .text : @{ *(.text) @} :text
6255 .rodata : @{ *(.rodata) @} /* defaults to :text */
6256 @dots{}
6257 . = . + 0x1000; /* move to a new page in memory */
6258 .data : @{ *(.data) @} :data
6259 .dynamic : @{ *(.dynamic) @} :data :dynamic
6260 @dots{}
6261@}
6262@end group
6263@end example
6264
6265@node VERSION
6266@section VERSION Command
6267@kindex VERSION @{script text@}
6268@cindex symbol versions
6269@cindex version script
6270@cindex versions of symbols
6271The linker supports symbol versions when using ELF. Symbol versions are
6272only useful when using shared libraries. The dynamic linker can use
6273symbol versions to select a specific version of a function when it runs
6274a program that may have been linked against an earlier version of the
6275shared library.
6276
6277You can include a version script directly in the main linker script, or
6278you can supply the version script as an implicit linker script. You can
6279also use the @samp{--version-script} linker option.
6280
6281The syntax of the @code{VERSION} command is simply
6282@smallexample
6283VERSION @{ version-script-commands @}
6284@end smallexample
6285
6286The format of the version script commands is identical to that used by
6287Sun's linker in Solaris 2.5. The version script defines a tree of
6288version nodes. You specify the node names and interdependencies in the
6289version script. You can specify which symbols are bound to which
6290version nodes, and you can reduce a specified set of symbols to local
6291scope so that they are not globally visible outside of the shared
6292library.
6293
6294The easiest way to demonstrate the version script language is with a few
6295examples.
6296
6297@smallexample
6298VERS_1.1 @{
6299 global:
6300 foo1;
6301 local:
a1ab1d2a
UD
6302 old*;
6303 original*;
6304 new*;
252b5132
RH
6305@};
6306
6307VERS_1.2 @{
6308 foo2;
6309@} VERS_1.1;
6310
6311VERS_2.0 @{
6312 bar1; bar2;
c0065db7 6313 extern "C++" @{
86043bbb 6314 ns::*;
bb1515f2
MF
6315 "f(int, double)";
6316 @};
252b5132
RH
6317@} VERS_1.2;
6318@end smallexample
6319
6320This example version script defines three version nodes. The first
6321version node defined is @samp{VERS_1.1}; it has no other dependencies.
6322The script binds the symbol @samp{foo1} to @samp{VERS_1.1}. It reduces
6323a number of symbols to local scope so that they are not visible outside
313e35ee
AM
6324of the shared library; this is done using wildcard patterns, so that any
6325symbol whose name begins with @samp{old}, @samp{original}, or @samp{new}
6326is matched. The wildcard patterns available are the same as those used
6327in the shell when matching filenames (also known as ``globbing'').
86043bbb
MM
6328However, if you specify the symbol name inside double quotes, then the
6329name is treated as literal, rather than as a glob pattern.
252b5132
RH
6330
6331Next, the version script defines node @samp{VERS_1.2}. This node
6332depends upon @samp{VERS_1.1}. The script binds the symbol @samp{foo2}
6333to the version node @samp{VERS_1.2}.
6334
6335Finally, the version script defines node @samp{VERS_2.0}. This node
6336depends upon @samp{VERS_1.2}. The scripts binds the symbols @samp{bar1}
6337and @samp{bar2} are bound to the version node @samp{VERS_2.0}.
6338
6339When the linker finds a symbol defined in a library which is not
6340specifically bound to a version node, it will effectively bind it to an
6341unspecified base version of the library. You can bind all otherwise
a981ed6f 6342unspecified symbols to a given version node by using @samp{global: *;}
ae5a3597
AM
6343somewhere in the version script. Note that it's slightly crazy to use
6344wildcards in a global spec except on the last version node. Global
6345wildcards elsewhere run the risk of accidentally adding symbols to the
6346set exported for an old version. That's wrong since older versions
6347ought to have a fixed set of symbols.
252b5132
RH
6348
6349The names of the version nodes have no specific meaning other than what
6350they might suggest to the person reading them. The @samp{2.0} version
6351could just as well have appeared in between @samp{1.1} and @samp{1.2}.
6352However, this would be a confusing way to write a version script.
6353
0f6bf451 6354Node name can be omitted, provided it is the only version node
6b9b879a
JJ
6355in the version script. Such version script doesn't assign any versions to
6356symbols, only selects which symbols will be globally visible out and which
6357won't.
6358
6359@smallexample
7c9c73be 6360@{ global: foo; bar; local: *; @};
9d201f2f 6361@end smallexample
6b9b879a 6362
252b5132
RH
6363When you link an application against a shared library that has versioned
6364symbols, the application itself knows which version of each symbol it
6365requires, and it also knows which version nodes it needs from each
6366shared library it is linked against. Thus at runtime, the dynamic
6367loader can make a quick check to make sure that the libraries you have
6368linked against do in fact supply all of the version nodes that the
6369application will need to resolve all of the dynamic symbols. In this
6370way it is possible for the dynamic linker to know with certainty that
6371all external symbols that it needs will be resolvable without having to
6372search for each symbol reference.
6373
6374The symbol versioning is in effect a much more sophisticated way of
6375doing minor version checking that SunOS does. The fundamental problem
6376that is being addressed here is that typically references to external
6377functions are bound on an as-needed basis, and are not all bound when
6378the application starts up. If a shared library is out of date, a
6379required interface may be missing; when the application tries to use
6380that interface, it may suddenly and unexpectedly fail. With symbol
6381versioning, the user will get a warning when they start their program if
6382the libraries being used with the application are too old.
6383
6384There are several GNU extensions to Sun's versioning approach. The
6385first of these is the ability to bind a symbol to a version node in the
6386source file where the symbol is defined instead of in the versioning
6387script. This was done mainly to reduce the burden on the library
6388maintainer. You can do this by putting something like:
6389@smallexample
6390__asm__(".symver original_foo,foo@@VERS_1.1");
6391@end smallexample
6392@noindent
6393in the C source file. This renames the function @samp{original_foo} to
6394be an alias for @samp{foo} bound to the version node @samp{VERS_1.1}.
6395The @samp{local:} directive can be used to prevent the symbol
96a94295
L
6396@samp{original_foo} from being exported. A @samp{.symver} directive
6397takes precedence over a version script.
252b5132
RH
6398
6399The second GNU extension is to allow multiple versions of the same
6400function to appear in a given shared library. In this way you can make
6401an incompatible change to an interface without increasing the major
6402version number of the shared library, while still allowing applications
6403linked against the old interface to continue to function.
6404
6405To do this, you must use multiple @samp{.symver} directives in the
6406source file. Here is an example:
6407
6408@smallexample
6409__asm__(".symver original_foo,foo@@");
6410__asm__(".symver old_foo,foo@@VERS_1.1");
6411__asm__(".symver old_foo1,foo@@VERS_1.2");
6412__asm__(".symver new_foo,foo@@@@VERS_2.0");
6413@end smallexample
6414
6415In this example, @samp{foo@@} represents the symbol @samp{foo} bound to the
6416unspecified base version of the symbol. The source file that contains this
6417example would define 4 C functions: @samp{original_foo}, @samp{old_foo},
6418@samp{old_foo1}, and @samp{new_foo}.
6419
6420When you have multiple definitions of a given symbol, there needs to be
6421some way to specify a default version to which external references to
6422this symbol will be bound. You can do this with the
6423@samp{foo@@@@VERS_2.0} type of @samp{.symver} directive. You can only
6424declare one version of a symbol as the default in this manner; otherwise
6425you would effectively have multiple definitions of the same symbol.
6426
6427If you wish to bind a reference to a specific version of the symbol
6428within the shared library, you can use the aliases of convenience
36f63dca 6429(i.e., @samp{old_foo}), or you can use the @samp{.symver} directive to
252b5132
RH
6430specifically bind to an external version of the function in question.
6431
cb840a31
L
6432You can also specify the language in the version script:
6433
6434@smallexample
6435VERSION extern "lang" @{ version-script-commands @}
6436@end smallexample
6437
c0065db7 6438The supported @samp{lang}s are @samp{C}, @samp{C++}, and @samp{Java}.
cb840a31
L
6439The linker will iterate over the list of symbols at the link time and
6440demangle them according to @samp{lang} before matching them to the
bb1515f2
MF
6441patterns specified in @samp{version-script-commands}. The default
6442@samp{lang} is @samp{C}.
cb840a31 6443
86043bbb
MM
6444Demangled names may contains spaces and other special characters. As
6445described above, you can use a glob pattern to match demangled names,
6446or you can use a double-quoted string to match the string exactly. In
6447the latter case, be aware that minor differences (such as differing
6448whitespace) between the version script and the demangler output will
6449cause a mismatch. As the exact string generated by the demangler
6450might change in the future, even if the mangled name does not, you
6451should check that all of your version directives are behaving as you
6452expect when you upgrade.
6453
252b5132
RH
6454@node Expressions
6455@section Expressions in Linker Scripts
6456@cindex expressions
6457@cindex arithmetic
6458The syntax for expressions in the linker script language is identical to
fb0afe4c
AM
6459that of C expressions, except that whitespace is required in some
6460places to resolve syntactic ambiguities. All expressions are
6461evaluated as integers. All expressions are evaluated in the same
6462size, which is 32 bits if both the host and target are 32 bits, and is
6463otherwise 64 bits.
252b5132
RH
6464
6465You can use and set symbol values in expressions.
6466
6467The linker defines several special purpose builtin functions for use in
6468expressions.
6469
6470@menu
6471* Constants:: Constants
0c71d759 6472* Symbolic Constants:: Symbolic constants
252b5132 6473* Symbols:: Symbol Names
ecca9871 6474* Orphan Sections:: Orphan Sections
252b5132
RH
6475* Location Counter:: The Location Counter
6476* Operators:: Operators
6477* Evaluation:: Evaluation
6478* Expression Section:: The Section of an Expression
6479* Builtin Functions:: Builtin Functions
6480@end menu
6481
6482@node Constants
6483@subsection Constants
6484@cindex integer notation
6485@cindex constants in linker scripts
6486All constants are integers.
6487
6488As in C, the linker considers an integer beginning with @samp{0} to be
6489octal, and an integer beginning with @samp{0x} or @samp{0X} to be
8a308ae8 6490hexadecimal. Alternatively the linker accepts suffixes of @samp{h} or
11e7fd74 6491@samp{H} for hexadecimal, @samp{o} or @samp{O} for octal, @samp{b} or
8a308ae8
NC
6492@samp{B} for binary and @samp{d} or @samp{D} for decimal. Any integer
6493value without a prefix or a suffix is considered to be decimal.
252b5132
RH
6494
6495@cindex scaled integers
6496@cindex K and M integer suffixes
6497@cindex M and K integer suffixes
6498@cindex suffixes for integers
6499@cindex integer suffixes
6500In addition, you can use the suffixes @code{K} and @code{M} to scale a
6501constant by
6502@c TEXI2ROFF-KILL
36f63dca 6503@ifnottex
252b5132
RH
6504@c END TEXI2ROFF-KILL
6505@code{1024} or @code{1024*1024}
6506@c TEXI2ROFF-KILL
36f63dca 6507@end ifnottex
252b5132
RH
6508@tex
6509${\rm 1024}$ or ${\rm 1024}^2$
6510@end tex
6511@c END TEXI2ROFF-KILL
8a308ae8
NC
6512respectively. For example, the following
6513all refer to the same quantity:
6514
252b5132 6515@smallexample
36f63dca
NC
6516_fourk_1 = 4K;
6517_fourk_2 = 4096;
6518_fourk_3 = 0x1000;
8a308ae8 6519_fourk_4 = 10000o;
252b5132
RH
6520@end smallexample
6521
8a308ae8
NC
6522Note - the @code{K} and @code{M} suffixes cannot be used in
6523conjunction with the base suffixes mentioned above.
6524
0c71d759
NC
6525@node Symbolic Constants
6526@subsection Symbolic Constants
6527@cindex symbolic constants
6528@kindex CONSTANT
a094d01f 6529It is possible to refer to target-specific constants via the use of
0c71d759
NC
6530the @code{CONSTANT(@var{name})} operator, where @var{name} is one of:
6531
6532@table @code
6533@item MAXPAGESIZE
6534@kindex MAXPAGESIZE
6535The target's maximum page size.
6536
6537@item COMMONPAGESIZE
6538@kindex COMMONPAGESIZE
6539The target's default page size.
6540@end table
6541
6542So for example:
6543
6544@smallexample
9d5777a3 6545 .text ALIGN (CONSTANT (MAXPAGESIZE)) : @{ *(.text) @}
0c71d759
NC
6546@end smallexample
6547
6548will create a text section aligned to the largest page boundary
6549supported by the target.
6550
252b5132
RH
6551@node Symbols
6552@subsection Symbol Names
6553@cindex symbol names
6554@cindex names
6555@cindex quoted symbol names
6556@kindex "
6557Unless quoted, symbol names start with a letter, underscore, or period
6558and may include letters, digits, underscores, periods, and hyphens.
6559Unquoted symbol names must not conflict with any keywords. You can
6560specify a symbol which contains odd characters or has the same name as a
6561keyword by surrounding the symbol name in double quotes:
6562@smallexample
36f63dca
NC
6563"SECTION" = 9;
6564"with a space" = "also with a space" + 10;
252b5132
RH
6565@end smallexample
6566
6567Since symbols can contain many non-alphabetic characters, it is safest
6568to delimit symbols with spaces. For example, @samp{A-B} is one symbol,
6569whereas @samp{A - B} is an expression involving subtraction.
6570
ecca9871
L
6571@node Orphan Sections
6572@subsection Orphan Sections
6573@cindex orphan
6574Orphan sections are sections present in the input files which
6575are not explicitly placed into the output file by the linker
6576script. The linker will still copy these sections into the
a87ded7b
AB
6577output file by either finding, or creating a suitable output section
6578in which to place the orphaned input section.
6579
6580If the name of an orphaned input section exactly matches the name of
6581an existing output section, then the orphaned input section will be
6582placed at the end of that output section.
6583
6584If there is no output section with a matching name then new output
6585sections will be created. Each new output section will have the same
6586name as the orphan section placed within it. If there are multiple
6587orphan sections with the same name, these will all be combined into
6588one new output section.
6589
6590If new output sections are created to hold orphaned input sections,
6591then the linker must decide where to place these new output sections
e299b355
AM
6592in relation to existing output sections. On most modern targets, the
6593linker attempts to place orphan sections after sections of the same
6594attribute, such as code vs data, loadable vs non-loadable, etc. If no
6595sections with matching attributes are found, or your target lacks this
6596support, the orphan section is placed at the end of the file.
ecca9871 6597
a05a5b64
TP
6598The command-line options @samp{--orphan-handling} and @samp{--unique}
6599(@pxref{Options,,Command-line Options}) can be used to control which
c005eb9e
AB
6600output sections an orphan is placed in.
6601
252b5132
RH
6602@node Location Counter
6603@subsection The Location Counter
6604@kindex .
6605@cindex dot
6606@cindex location counter
6607@cindex current output location
6608The special linker variable @dfn{dot} @samp{.} always contains the
6609current output location counter. Since the @code{.} always refers to a
6610location in an output section, it may only appear in an expression
6611within a @code{SECTIONS} command. The @code{.} symbol may appear
6612anywhere that an ordinary symbol is allowed in an expression.
6613
6614@cindex holes
6615Assigning a value to @code{.} will cause the location counter to be
6616moved. This may be used to create holes in the output section. The
dc0b6aa0
AM
6617location counter may not be moved backwards inside an output section,
6618and may not be moved backwards outside of an output section if so
6619doing creates areas with overlapping LMAs.
252b5132
RH
6620
6621@smallexample
6622SECTIONS
6623@{
6624 output :
6625 @{
6626 file1(.text)
6627 . = . + 1000;
6628 file2(.text)
6629 . += 1000;
6630 file3(.text)
563e308f 6631 @} = 0x12345678;
252b5132
RH
6632@}
6633@end smallexample
6634@noindent
6635In the previous example, the @samp{.text} section from @file{file1} is
6636located at the beginning of the output section @samp{output}. It is
6637followed by a 1000 byte gap. Then the @samp{.text} section from
6638@file{file2} appears, also with a 1000 byte gap following before the
563e308f 6639@samp{.text} section from @file{file3}. The notation @samp{= 0x12345678}
252b5132
RH
6640specifies what data to write in the gaps (@pxref{Output Section Fill}).
6641
5c6bbab8
NC
6642@cindex dot inside sections
6643Note: @code{.} actually refers to the byte offset from the start of the
6644current containing object. Normally this is the @code{SECTIONS}
69da35b5 6645statement, whose start address is 0, hence @code{.} can be used as an
5c6bbab8
NC
6646absolute address. If @code{.} is used inside a section description
6647however, it refers to the byte offset from the start of that section,
6648not an absolute address. Thus in a script like this:
6649
6650@smallexample
6651SECTIONS
6652@{
6653 . = 0x100
6654 .text: @{
6655 *(.text)
6656 . = 0x200
6657 @}
6658 . = 0x500
6659 .data: @{
6660 *(.data)
6661 . += 0x600
6662 @}
6663@}
6664@end smallexample
6665
6666The @samp{.text} section will be assigned a starting address of 0x100
6667and a size of exactly 0x200 bytes, even if there is not enough data in
6668the @samp{.text} input sections to fill this area. (If there is too
6669much data, an error will be produced because this would be an attempt to
6670move @code{.} backwards). The @samp{.data} section will start at 0x500
6671and it will have an extra 0x600 bytes worth of space after the end of
6672the values from the @samp{.data} input sections and before the end of
6673the @samp{.data} output section itself.
6674
b5666f2f
AM
6675@cindex dot outside sections
6676Setting symbols to the value of the location counter outside of an
6677output section statement can result in unexpected values if the linker
6678needs to place orphan sections. For example, given the following:
6679
6680@smallexample
6681SECTIONS
6682@{
6683 start_of_text = . ;
6684 .text: @{ *(.text) @}
6685 end_of_text = . ;
6686
6687 start_of_data = . ;
6688 .data: @{ *(.data) @}
6689 end_of_data = . ;
6690@}
6691@end smallexample
6692
6693If the linker needs to place some input section, e.g. @code{.rodata},
6694not mentioned in the script, it might choose to place that section
6695between @code{.text} and @code{.data}. You might think the linker
6696should place @code{.rodata} on the blank line in the above script, but
6697blank lines are of no particular significance to the linker. As well,
6698the linker doesn't associate the above symbol names with their
6699sections. Instead, it assumes that all assignments or other
6700statements belong to the previous output section, except for the
6701special case of an assignment to @code{.}. I.e., the linker will
6702place the orphan @code{.rodata} section as if the script was written
6703as follows:
6704
6705@smallexample
6706SECTIONS
6707@{
6708 start_of_text = . ;
6709 .text: @{ *(.text) @}
6710 end_of_text = . ;
6711
6712 start_of_data = . ;
6713 .rodata: @{ *(.rodata) @}
6714 .data: @{ *(.data) @}
6715 end_of_data = . ;
6716@}
6717@end smallexample
6718
6719This may or may not be the script author's intention for the value of
6720@code{start_of_data}. One way to influence the orphan section
6721placement is to assign the location counter to itself, as the linker
6722assumes that an assignment to @code{.} is setting the start address of
6723a following output section and thus should be grouped with that
6724section. So you could write:
6725
6726@smallexample
6727SECTIONS
6728@{
6729 start_of_text = . ;
6730 .text: @{ *(.text) @}
6731 end_of_text = . ;
6732
6733 . = . ;
6734 start_of_data = . ;
6735 .data: @{ *(.data) @}
6736 end_of_data = . ;
6737@}
6738@end smallexample
6739
6740Now, the orphan @code{.rodata} section will be placed between
6741@code{end_of_text} and @code{start_of_data}.
6742
252b5132
RH
6743@need 2000
6744@node Operators
6745@subsection Operators
6746@cindex operators for arithmetic
6747@cindex arithmetic operators
6748@cindex precedence in expressions
6749The linker recognizes the standard C set of arithmetic operators, with
6750the standard bindings and precedence levels:
6751@c TEXI2ROFF-KILL
36f63dca 6752@ifnottex
252b5132
RH
6753@c END TEXI2ROFF-KILL
6754@smallexample
6755precedence associativity Operators Notes
6756(highest)
67571 left ! - ~ (1)
67582 left * / %
67593 left + -
67604 left >> <<
67615 left == != > < <= >=
67626 left &
67637 left |
67648 left &&
67659 left ||
676610 right ? :
676711 right &= += -= *= /= (2)
6768(lowest)
6769@end smallexample
6770Notes:
a1ab1d2a 6771(1) Prefix operators
252b5132
RH
6772(2) @xref{Assignments}.
6773@c TEXI2ROFF-KILL
36f63dca 6774@end ifnottex
252b5132
RH
6775@tex
6776\vskip \baselineskip
6777%"lispnarrowing" is the extra indent used generally for smallexample
6778\hskip\lispnarrowing\vbox{\offinterlineskip
6779\hrule
6780\halign
6781{\vrule#&\strut\hfil\ #\ \hfil&\vrule#&\strut\hfil\ #\ \hfil&\vrule#&\strut\hfil\ {\tt #}\ \hfil&\vrule#\cr
6782height2pt&\omit&&\omit&&\omit&\cr
6783&Precedence&& Associativity &&{\rm Operators}&\cr
6784height2pt&\omit&&\omit&&\omit&\cr
6785\noalign{\hrule}
6786height2pt&\omit&&\omit&&\omit&\cr
6787&highest&&&&&\cr
6788% '176 is tilde, '~' in tt font
a1ab1d2a 6789&1&&left&&\qquad- \char'176\ !\qquad\dag&\cr
252b5132
RH
6790&2&&left&&* / \%&\cr
6791&3&&left&&+ -&\cr
6792&4&&left&&>> <<&\cr
6793&5&&left&&== != > < <= >=&\cr
6794&6&&left&&\&&\cr
6795&7&&left&&|&\cr
6796&8&&left&&{\&\&}&\cr
6797&9&&left&&||&\cr
6798&10&&right&&? :&\cr
6799&11&&right&&\qquad\&= += -= *= /=\qquad\ddag&\cr
6800&lowest&&&&&\cr
6801height2pt&\omit&&\omit&&\omit&\cr}
6802\hrule}
6803@end tex
6804@iftex
6805{
6806@obeylines@parskip=0pt@parindent=0pt
6807@dag@quad Prefix operators.
6808@ddag@quad @xref{Assignments}.
6809}
6810@end iftex
6811@c END TEXI2ROFF-KILL
6812
6813@node Evaluation
6814@subsection Evaluation
6815@cindex lazy evaluation
6816@cindex expression evaluation order
6817The linker evaluates expressions lazily. It only computes the value of
6818an expression when absolutely necessary.
6819
6820The linker needs some information, such as the value of the start
6821address of the first section, and the origins and lengths of memory
6822regions, in order to do any linking at all. These values are computed
6823as soon as possible when the linker reads in the linker script.
6824
6825However, other values (such as symbol values) are not known or needed
6826until after storage allocation. Such values are evaluated later, when
6827other information (such as the sizes of output sections) is available
6828for use in the symbol assignment expression.
6829
6830The sizes of sections cannot be known until after allocation, so
6831assignments dependent upon these are not performed until after
6832allocation.
6833
6834Some expressions, such as those depending upon the location counter
6835@samp{.}, must be evaluated during section allocation.
6836
6837If the result of an expression is required, but the value is not
6838available, then an error results. For example, a script like the
6839following
6840@smallexample
6841@group
6842SECTIONS
6843 @{
a1ab1d2a 6844 .text 9+this_isnt_constant :
252b5132
RH
6845 @{ *(.text) @}
6846 @}
6847@end group
6848@end smallexample
6849@noindent
6850will cause the error message @samp{non constant expression for initial
6851address}.
6852
6853@node Expression Section
6854@subsection The Section of an Expression
6855@cindex expression sections
6856@cindex absolute expressions
6857@cindex relative expressions
6858@cindex absolute and relocatable symbols
6859@cindex relocatable and absolute symbols
6860@cindex symbols, relocatable and absolute
7542af2a
AM
6861Addresses and symbols may be section relative, or absolute. A section
6862relative symbol is relocatable. If you request relocatable output
6863using the @samp{-r} option, a further link operation may change the
6864value of a section relative symbol. On the other hand, an absolute
6865symbol will retain the same value throughout any further link
6866operations.
6867
abf4be64
AM
6868Some terms in linker expressions are addresses. This is true of
6869section relative symbols and for builtin functions that return an
6870address, such as @code{ADDR}, @code{LOADADDR}, @code{ORIGIN} and
6871@code{SEGMENT_START}. Other terms are simply numbers, or are builtin
6872functions that return a non-address value, such as @code{LENGTH}.
01554a74
AM
6873One complication is that unless you set @code{LD_FEATURE ("SANE_EXPR")}
6874(@pxref{Miscellaneous Commands}), numbers and absolute symbols are treated
5c3049d2
AM
6875differently depending on their location, for compatibility with older
6876versions of @code{ld}. Expressions appearing outside an output
6877section definition treat all numbers as absolute addresses.
6878Expressions appearing inside an output section definition treat
01554a74
AM
6879absolute symbols as numbers. If @code{LD_FEATURE ("SANE_EXPR")} is
6880given, then absolute symbols and numbers are simply treated as numbers
6881everywhere.
5c3049d2
AM
6882
6883In the following simple example,
252b5132 6884
7542af2a
AM
6885@smallexample
6886@group
6887SECTIONS
6888 @{
6889 . = 0x100;
6890 __executable_start = 0x100;
6891 .data :
6892 @{
6893 . = 0x10;
6894 __data_start = 0x10;
6895 *(.data)
6896 @}
6897 @dots{}
6898 @}
6899@end group
6900@end smallexample
252b5132 6901
7542af2a
AM
6902both @code{.} and @code{__executable_start} are set to the absolute
6903address 0x100 in the first two assignments, then both @code{.} and
6904@code{__data_start} are set to 0x10 relative to the @code{.data}
6905section in the second two assignments.
252b5132 6906
5c3049d2
AM
6907For expressions involving numbers, relative addresses and absolute
6908addresses, ld follows these rules to evaluate terms:
7542af2a
AM
6909
6910@itemize @bullet
6911@item
c05f749e
AM
6912Unary operations on an absolute address or number, and binary
6913operations on two absolute addresses or two numbers, or between one
6914absolute address and a number, apply the operator to the value(s).
6915@item
7542af2a
AM
6916Unary operations on a relative address, and binary operations on two
6917relative addresses in the same section or between one relative address
6918and a number, apply the operator to the offset part of the address(es).
6919@item
c05f749e
AM
6920Other binary operations, that is, between two relative addresses not
6921in the same section, or between a relative address and an absolute
6922address, first convert any non-absolute term to an absolute address
6923before applying the operator.
7542af2a
AM
6924@end itemize
6925
6926The result section of each sub-expression is as follows:
6927
6928@itemize @bullet
6929@item
6930An operation involving only numbers results in a number.
6931@item
6932The result of comparisons, @samp{&&} and @samp{||} is also a number.
6933@item
9bc8bb33 6934The result of other binary arithmetic and logical operations on two
11e7fd74 6935relative addresses in the same section or two absolute addresses
94b41882
AM
6936(after above conversions) is also a number when
6937@code{LD_FEATURE ("SANE_EXPR")} or inside an output section definition
6938but an absolute address otherwise.
9bc8bb33
AM
6939@item
6940The result of other operations on relative addresses or one
6941relative address and a number, is a relative address in the same
6942section as the relative operand(s).
7542af2a
AM
6943@item
6944The result of other operations on absolute addresses (after above
6945conversions) is an absolute address.
6946@end itemize
252b5132
RH
6947
6948You can use the builtin function @code{ABSOLUTE} to force an expression
6949to be absolute when it would otherwise be relative. For example, to
6950create an absolute symbol set to the address of the end of the output
6951section @samp{.data}:
6952@smallexample
6953SECTIONS
6954 @{
6955 .data : @{ *(.data) _edata = ABSOLUTE(.); @}
6956 @}
6957@end smallexample
6958@noindent
6959If @samp{ABSOLUTE} were not used, @samp{_edata} would be relative to the
6960@samp{.data} section.
6961
7542af2a
AM
6962Using @code{LOADADDR} also forces an expression absolute, since this
6963particular builtin function returns an absolute address.
6964
252b5132
RH
6965@node Builtin Functions
6966@subsection Builtin Functions
6967@cindex functions in expressions
6968The linker script language includes a number of builtin functions for
6969use in linker script expressions.
6970
6971@table @code
6972@item ABSOLUTE(@var{exp})
6973@kindex ABSOLUTE(@var{exp})
6974@cindex expression, absolute
6975Return the absolute (non-relocatable, as opposed to non-negative) value
6976of the expression @var{exp}. Primarily useful to assign an absolute
6977value to a symbol within a section definition, where symbol values are
6978normally section relative. @xref{Expression Section}.
6979
6980@item ADDR(@var{section})
6981@kindex ADDR(@var{section})
6982@cindex section address in expression
7542af2a 6983Return the address (VMA) of the named @var{section}. Your
252b5132 6984script must previously have defined the location of that section. In
7542af2a
AM
6985the following example, @code{start_of_output_1}, @code{symbol_1} and
6986@code{symbol_2} are assigned equivalent values, except that
6987@code{symbol_1} will be relative to the @code{.output1} section while
6988the other two will be absolute:
252b5132
RH
6989@smallexample
6990@group
6991SECTIONS @{ @dots{}
6992 .output1 :
a1ab1d2a 6993 @{
252b5132
RH
6994 start_of_output_1 = ABSOLUTE(.);
6995 @dots{}
6996 @}
6997 .output :
6998 @{
6999 symbol_1 = ADDR(.output1);
7000 symbol_2 = start_of_output_1;
7001 @}
7002@dots{} @}
7003@end group
7004@end smallexample
7005
876f4090
NS
7006@item ALIGN(@var{align})
7007@itemx ALIGN(@var{exp},@var{align})
7008@kindex ALIGN(@var{align})
7009@kindex ALIGN(@var{exp},@var{align})
252b5132
RH
7010@cindex round up location counter
7011@cindex align location counter
876f4090
NS
7012@cindex round up expression
7013@cindex align expression
7014Return the location counter (@code{.}) or arbitrary expression aligned
7015to the next @var{align} boundary. The single operand @code{ALIGN}
7016doesn't change the value of the location counter---it just does
7017arithmetic on it. The two operand @code{ALIGN} allows an arbitrary
7018expression to be aligned upwards (@code{ALIGN(@var{align})} is
e0a3af22 7019equivalent to @code{ALIGN(ABSOLUTE(.), @var{align})}).
876f4090
NS
7020
7021Here is an example which aligns the output @code{.data} section to the
7022next @code{0x2000} byte boundary after the preceding section and sets a
7023variable within the section to the next @code{0x8000} boundary after the
7024input sections:
252b5132
RH
7025@smallexample
7026@group
7027SECTIONS @{ @dots{}
7028 .data ALIGN(0x2000): @{
7029 *(.data)
7030 variable = ALIGN(0x8000);
7031 @}
7032@dots{} @}
7033@end group
7034@end smallexample
7035@noindent
7036The first use of @code{ALIGN} in this example specifies the location of
7037a section because it is used as the optional @var{address} attribute of
7038a section definition (@pxref{Output Section Address}). The second use
7039of @code{ALIGN} is used to defines the value of a symbol.
7040
7041The builtin function @code{NEXT} is closely related to @code{ALIGN}.
7042
362c1d1a
NS
7043@item ALIGNOF(@var{section})
7044@kindex ALIGNOF(@var{section})
7045@cindex section alignment
7046Return the alignment in bytes of the named @var{section}, if that section has
7047been allocated. If the section has not been allocated when this is
7048evaluated, the linker will report an error. In the following example,
7049the alignment of the @code{.output} section is stored as the first
7050value in that section.
7051@smallexample
7052@group
7053SECTIONS@{ @dots{}
7054 .output @{
7055 LONG (ALIGNOF (.output))
7056 @dots{}
7057 @}
7058@dots{} @}
7059@end group
7060@end smallexample
7061
252b5132
RH
7062@item BLOCK(@var{exp})
7063@kindex BLOCK(@var{exp})
7064This is a synonym for @code{ALIGN}, for compatibility with older linker
7065scripts. It is most often seen when setting the address of an output
7066section.
7067
2d20f7bf
JJ
7068@item DATA_SEGMENT_ALIGN(@var{maxpagesize}, @var{commonpagesize})
7069@kindex DATA_SEGMENT_ALIGN(@var{maxpagesize}, @var{commonpagesize})
7070This is equivalent to either
7071@smallexample
7072(ALIGN(@var{maxpagesize}) + (. & (@var{maxpagesize} - 1)))
7073@end smallexample
7074or
7075@smallexample
fe6052e1
AM
7076(ALIGN(@var{maxpagesize})
7077 + ((. + @var{commonpagesize} - 1) & (@var{maxpagesize} - @var{commonpagesize})))
2d20f7bf
JJ
7078@end smallexample
7079@noindent
7080depending on whether the latter uses fewer @var{commonpagesize} sized pages
7081for the data segment (area between the result of this expression and
7082@code{DATA_SEGMENT_END}) than the former or not.
7083If the latter form is used, it means @var{commonpagesize} bytes of runtime
7084memory will be saved at the expense of up to @var{commonpagesize} wasted
7085bytes in the on-disk file.
7086
7087This expression can only be used directly in @code{SECTIONS} commands, not in
7088any output section descriptions and only once in the linker script.
7089@var{commonpagesize} should be less or equal to @var{maxpagesize} and should
def5c83c
AM
7090be the system page size the object wants to be optimized for while still
7091running on system page sizes up to @var{maxpagesize}. Note however
7092that @samp{-z relro} protection will not be effective if the system
7093page size is larger than @var{commonpagesize}.
2d20f7bf
JJ
7094
7095@noindent
7096Example:
7097@smallexample
7098 . = DATA_SEGMENT_ALIGN(0x10000, 0x2000);
7099@end smallexample
7100
7101@item DATA_SEGMENT_END(@var{exp})
7102@kindex DATA_SEGMENT_END(@var{exp})
7103This defines the end of data segment for @code{DATA_SEGMENT_ALIGN}
7104evaluation purposes.
7105
7106@smallexample
7107 . = DATA_SEGMENT_END(.);
7108@end smallexample
7109
a4f5ad88
JJ
7110@item DATA_SEGMENT_RELRO_END(@var{offset}, @var{exp})
7111@kindex DATA_SEGMENT_RELRO_END(@var{offset}, @var{exp})
7112This defines the end of the @code{PT_GNU_RELRO} segment when
eec2f3ed 7113@samp{-z relro} option is used.
a4f5ad88
JJ
7114When @samp{-z relro} option is not present, @code{DATA_SEGMENT_RELRO_END}
7115does nothing, otherwise @code{DATA_SEGMENT_ALIGN} is padded so that
def5c83c
AM
7116@var{exp} + @var{offset} is aligned to the @var{commonpagesize}
7117argument given to @code{DATA_SEGMENT_ALIGN}. If present in the linker
7118script, it must be placed between @code{DATA_SEGMENT_ALIGN} and
eec2f3ed
AM
7119@code{DATA_SEGMENT_END}. Evaluates to the second argument plus any
7120padding needed at the end of the @code{PT_GNU_RELRO} segment due to
7121section alignment.
a4f5ad88
JJ
7122
7123@smallexample
7124 . = DATA_SEGMENT_RELRO_END(24, .);
7125@end smallexample
7126
252b5132
RH
7127@item DEFINED(@var{symbol})
7128@kindex DEFINED(@var{symbol})
7129@cindex symbol defaults
7130Return 1 if @var{symbol} is in the linker global symbol table and is
420e579c
HPN
7131defined before the statement using DEFINED in the script, otherwise
7132return 0. You can use this function to provide
252b5132
RH
7133default values for symbols. For example, the following script fragment
7134shows how to set a global symbol @samp{begin} to the first location in
7135the @samp{.text} section---but if a symbol called @samp{begin} already
7136existed, its value is preserved:
7137
7138@smallexample
7139@group
7140SECTIONS @{ @dots{}
7141 .text : @{
7142 begin = DEFINED(begin) ? begin : . ;
7143 @dots{}
7144 @}
7145 @dots{}
7146@}
7147@end group
7148@end smallexample
7149
3ec57632
NC
7150@item LENGTH(@var{memory})
7151@kindex LENGTH(@var{memory})
7152Return the length of the memory region named @var{memory}.
7153
252b5132
RH
7154@item LOADADDR(@var{section})
7155@kindex LOADADDR(@var{section})
7156@cindex section load address in expression
7542af2a 7157Return the absolute LMA of the named @var{section}. (@pxref{Output
252b5132
RH
7158Section LMA}).
7159
2e53f7d6
NC
7160@item LOG2CEIL(@var{exp})
7161@kindex LOG2CEIL(@var{exp})
7162Return the binary logarithm of @var{exp} rounded towards infinity.
7163@code{LOG2CEIL(0)} returns 0.
7164
252b5132
RH
7165@kindex MAX
7166@item MAX(@var{exp1}, @var{exp2})
7167Returns the maximum of @var{exp1} and @var{exp2}.
7168
7169@kindex MIN
7170@item MIN(@var{exp1}, @var{exp2})
7171Returns the minimum of @var{exp1} and @var{exp2}.
7172
7173@item NEXT(@var{exp})
7174@kindex NEXT(@var{exp})
7175@cindex unallocated address, next
7176Return the next unallocated address that is a multiple of @var{exp}.
7177This function is closely related to @code{ALIGN(@var{exp})}; unless you
7178use the @code{MEMORY} command to define discontinuous memory for the
7179output file, the two functions are equivalent.
7180
3ec57632
NC
7181@item ORIGIN(@var{memory})
7182@kindex ORIGIN(@var{memory})
7183Return the origin of the memory region named @var{memory}.
7184
ba916c8a
MM
7185@item SEGMENT_START(@var{segment}, @var{default})
7186@kindex SEGMENT_START(@var{segment}, @var{default})
7187Return the base address of the named @var{segment}. If an explicit
c5da8c7d
NC
7188value has already been given for this segment (with a command-line
7189@samp{-T} option) then that value will be returned otherwise the value
7190will be @var{default}. At present, the @samp{-T} command-line option
7191can only be used to set the base address for the ``text'', ``data'', and
7542af2a 7192``bss'' sections, but you can use @code{SEGMENT_START} with any segment
ba916c8a
MM
7193name.
7194
252b5132
RH
7195@item SIZEOF(@var{section})
7196@kindex SIZEOF(@var{section})
7197@cindex section size
7198Return the size in bytes of the named @var{section}, if that section has
7199been allocated. If the section has not been allocated when this is
7200evaluated, the linker will report an error. In the following example,
7201@code{symbol_1} and @code{symbol_2} are assigned identical values:
7202@smallexample
7203@group
7204SECTIONS@{ @dots{}
7205 .output @{
7206 .start = . ;
7207 @dots{}
7208 .end = . ;
7209 @}
7210 symbol_1 = .end - .start ;
7211 symbol_2 = SIZEOF(.output);
7212@dots{} @}
7213@end group
7214@end smallexample
7215
7216@item SIZEOF_HEADERS
252b5132
RH
7217@kindex SIZEOF_HEADERS
7218@cindex header size
7219Return the size in bytes of the output file's headers. This is
7220information which appears at the start of the output file. You can use
7221this number when setting the start address of the first section, if you
7222choose, to facilitate paging.
7223
7224@cindex not enough room for program headers
7225@cindex program headers, not enough room
7226When producing an ELF output file, if the linker script uses the
7227@code{SIZEOF_HEADERS} builtin function, the linker must compute the
7228number of program headers before it has determined all the section
7229addresses and sizes. If the linker later discovers that it needs
7230additional program headers, it will report an error @samp{not enough
7231room for program headers}. To avoid this error, you must avoid using
7232the @code{SIZEOF_HEADERS} function, or you must rework your linker
7233script to avoid forcing the linker to use additional program headers, or
7234you must define the program headers yourself using the @code{PHDRS}
7235command (@pxref{PHDRS}).
7236@end table
7237
7238@node Implicit Linker Scripts
7239@section Implicit Linker Scripts
7240@cindex implicit linker scripts
7241If you specify a linker input file which the linker can not recognize as
7242an object file or an archive file, it will try to read the file as a
7243linker script. If the file can not be parsed as a linker script, the
7244linker will report an error.
7245
7246An implicit linker script will not replace the default linker script.
7247
7248Typically an implicit linker script would contain only symbol
7249assignments, or the @code{INPUT}, @code{GROUP}, or @code{VERSION}
7250commands.
7251
7252Any input files read because of an implicit linker script will be read
7253at the position in the command line where the implicit linker script was
7254read. This can affect archive searching.
7255
bf6d8037
HC
7256@node Plugins
7257@chapter Linker Plugins
7258
7259@cindex plugins
7260@cindex linker plugins
7261The linker can use dynamically loaded plugins to modify its behavior.
7262For example, the link-time optimization feature that some compilers
7263support is implemented with a linker plugin.
7264
7265Currently there is only one plugin shipped by default, but more may
7266be added here later.
7267
9b38b85e 7268Plugins are enabled via the use of the @option{-plugin @var{name}}
3a117c58 7269command line option. @xref{Options}.
9b38b85e 7270
bf6d8037
HC
7271@menu
7272* libdep Plugin:: Static Library Dependencies Plugin
7273@end menu
7274
7275@node libdep Plugin
7276@section Static Library Dependencies Plugin
7277@cindex static library dependencies
7278Originally, static libraries were contained in an archive file consisting
7279just of a collection of relocatable object files. Later they evolved to
7280optionally include a symbol table, to assist in finding the needed objects
7281within a library. There their evolution ended, and dynamic libraries
7282rose to ascendance.
7283
7284One useful feature of dynamic libraries was that, more than just collecting
7285multiple objects into a single file, they also included a list of their
7286dependencies, such that one could specify just the name of a single dynamic
7287library at link time, and all of its dependencies would be implicitly
7288referenced as well. But static libraries lacked this feature, so if a
7289link invocation was switched from using dynamic libraries to static
7290libraries, the link command would usually fail unless it was rewritten to
7291explicitly list the dependencies of the static library.
7292
7293The GNU @command{ar} utility now supports a @option{--record-libdeps} option
7294to embed dependency lists into static libraries as well, and the @file{libdep}
7295plugin may be used to read this dependency information at link time. The
7296dependency information is stored as a single string, carrying @option{-l}
7297and @option{-L} arguments as they would normally appear in a linker
7298command line. As such, the information can be written with any text
7299utility and stored into any archive, even if GNU @command{ar} is not
7300being used to create the archive. The information is stored in an
7301archive member named @samp{__.LIBDEP}.
7302
7303For example, given a library @file{libssl.a} that depends on another
7304library @file{libcrypto.a} which may be found in @file{/usr/local/lib},
7305the @samp{__.LIBDEP} member of @file{libssl.a} would contain
7306
7307@smallexample
7308-L/usr/local/lib -lcrypto
7309@end smallexample
7310
252b5132
RH
7311@ifset GENERIC
7312@node Machine Dependent
7313@chapter Machine Dependent Features
7314
7315@cindex machine dependencies
ff5dcc92
SC
7316@command{ld} has additional features on some platforms; the following
7317sections describe them. Machines where @command{ld} has no additional
252b5132
RH
7318functionality are not listed.
7319
7320@menu
36f63dca
NC
7321@ifset H8300
7322* H8/300:: @command{ld} and the H8/300
7323@end ifset
7ca01ed9
NC
7324@ifset M68HC11
7325* M68HC11/68HC12:: @code{ld} and the Motorola 68HC11 and 68HC12 families
7326@end ifset
36f63dca
NC
7327@ifset ARM
7328* ARM:: @command{ld} and the ARM family
7329@end ifset
7330@ifset HPPA
7331* HPPA ELF32:: @command{ld} and HPPA 32-bit ELF
7332@end ifset
7fb9f789
NC
7333@ifset M68K
7334* M68K:: @command{ld} and the Motorola 68K family
7335@end ifset
833794fc
MR
7336@ifset MIPS
7337* MIPS:: @command{ld} and the MIPS family
7338@end ifset
3c3bdf30 7339@ifset MMIX
36f63dca 7340* MMIX:: @command{ld} and MMIX
3c3bdf30 7341@end ifset
2469cfa2 7342@ifset MSP430
36f63dca 7343* MSP430:: @command{ld} and MSP430
2469cfa2 7344@end ifset
35c08157
KLC
7345@ifset NDS32
7346* NDS32:: @command{ld} and NDS32
7347@end ifset
78058a5e
SL
7348@ifset NIOSII
7349* Nios II:: @command{ld} and the Altera Nios II
7350@end ifset
2a60a7a8
AM
7351@ifset POWERPC
7352* PowerPC ELF32:: @command{ld} and PowerPC 32-bit ELF Support
7353@end ifset
7354@ifset POWERPC64
7355* PowerPC64 ELF64:: @command{ld} and PowerPC64 64-bit ELF Support
7356@end ifset
b4cbbe8f
AK
7357@ifset S/390
7358* S/390 ELF:: @command{ld} and S/390 ELF Support
7359@end ifset
49fa1e15
AM
7360@ifset SPU
7361* SPU ELF:: @command{ld} and SPU ELF Support
7362@end ifset
74459f0e 7363@ifset TICOFF
ff5dcc92 7364* TI COFF:: @command{ld} and TI COFF
74459f0e 7365@end ifset
2ca22b03
NC
7366@ifset WIN32
7367* WIN32:: @command{ld} and WIN32 (cygwin/mingw)
7368@end ifset
e0001a05
NC
7369@ifset XTENSA
7370* Xtensa:: @command{ld} and Xtensa Processors
7371@end ifset
252b5132
RH
7372@end menu
7373@end ifset
7374
252b5132
RH
7375@ifset H8300
7376@ifclear GENERIC
7377@raisesections
7378@end ifclear
7379
7380@node H8/300
ff5dcc92 7381@section @command{ld} and the H8/300
252b5132
RH
7382
7383@cindex H8/300 support
ff5dcc92 7384For the H8/300, @command{ld} can perform these global optimizations when
252b5132
RH
7385you specify the @samp{--relax} command-line option.
7386
7387@table @emph
7388@cindex relaxing on H8/300
7389@item relaxing address modes
ff5dcc92 7390@command{ld} finds all @code{jsr} and @code{jmp} instructions whose
252b5132
RH
7391targets are within eight bits, and turns them into eight-bit
7392program-counter relative @code{bsr} and @code{bra} instructions,
7393respectively.
7394
7395@cindex synthesizing on H8/300
7396@item synthesizing instructions
81f5558e 7397@c FIXME: specifically mov.b, or any mov instructions really? -> mov.b only, at least on H8, H8H, H8S
ff5dcc92 7398@command{ld} finds all @code{mov.b} instructions which use the
252b5132
RH
7399sixteen-bit absolute address form, but refer to the top
7400page of memory, and changes them to use the eight-bit address form.
7401(That is: the linker turns @samp{mov.b @code{@@}@var{aa}:16} into
7402@samp{mov.b @code{@@}@var{aa}:8} whenever the address @var{aa} is in the
7403top page of memory).
1502569c 7404
81f5558e
NC
7405@command{ld} finds all @code{mov} instructions which use the register
7406indirect with 32-bit displacement addressing mode, but use a small
7407displacement inside 16-bit displacement range, and changes them to use
7408the 16-bit displacement form. (That is: the linker turns @samp{mov.b
7409@code{@@}@var{d}:32,ERx} into @samp{mov.b @code{@@}@var{d}:16,ERx}
7410whenever the displacement @var{d} is in the 16 bit signed integer
7411range. Only implemented in ELF-format ld).
7412
1502569c 7413@item bit manipulation instructions
c0065db7 7414@command{ld} finds all bit manipulation instructions like @code{band, bclr,
1502569c 7415biand, bild, bior, bist, bixor, bld, bnot, bor, bset, bst, btst, bxor}
c0065db7 7416which use 32 bit and 16 bit absolute address form, but refer to the top
1502569c
NC
7417page of memory, and changes them to use the 8 bit address form.
7418(That is: the linker turns @samp{bset #xx:3,@code{@@}@var{aa}:32} into
c0065db7 7419@samp{bset #xx:3,@code{@@}@var{aa}:8} whenever the address @var{aa} is in
1502569c
NC
7420the top page of memory).
7421
7422@item system control instructions
c0065db7
RM
7423@command{ld} finds all @code{ldc.w, stc.w} instructions which use the
742432 bit absolute address form, but refer to the top page of memory, and
1502569c
NC
7425changes them to use 16 bit address form.
7426(That is: the linker turns @samp{ldc.w @code{@@}@var{aa}:32,ccr} into
c0065db7 7427@samp{ldc.w @code{@@}@var{aa}:16,ccr} whenever the address @var{aa} is in
1502569c 7428the top page of memory).
252b5132
RH
7429@end table
7430
7431@ifclear GENERIC
7432@lowersections
7433@end ifclear
7434@end ifset
7435
36f63dca 7436@ifclear GENERIC
c2dcd04e 7437@ifset Renesas
36f63dca 7438@c This stuff is pointless to say unless you're especially concerned
c2dcd04e
NC
7439@c with Renesas chips; don't enable it for generic case, please.
7440@node Renesas
7441@chapter @command{ld} and Other Renesas Chips
36f63dca 7442
c2dcd04e
NC
7443@command{ld} also supports the Renesas (formerly Hitachi) H8/300H,
7444H8/500, and SH chips. No special features, commands, or command-line
7445options are required for these chips.
36f63dca
NC
7446@end ifset
7447@end ifclear
7448
36f63dca
NC
7449@ifset ARM
7450@ifclear GENERIC
7451@raisesections
7452@end ifclear
7453
93fd0973
SC
7454@ifset M68HC11
7455@ifclear GENERIC
7456@raisesections
7457@end ifclear
7458
7459@node M68HC11/68HC12
7460@section @command{ld} and the Motorola 68HC11 and 68HC12 families
7461
7462@cindex M68HC11 and 68HC12 support
7463
7464@subsection Linker Relaxation
7465
7466For the Motorola 68HC11, @command{ld} can perform these global
7467optimizations when you specify the @samp{--relax} command-line option.
7468
7469@table @emph
7470@cindex relaxing on M68HC11
7471@item relaxing address modes
7472@command{ld} finds all @code{jsr} and @code{jmp} instructions whose
7473targets are within eight bits, and turns them into eight-bit
7474program-counter relative @code{bsr} and @code{bra} instructions,
7475respectively.
7476
7477@command{ld} also looks at all 16-bit extended addressing modes and
7478transforms them in a direct addressing mode when the address is in
7479page 0 (between 0 and 0x0ff).
7480
7481@item relaxing gcc instruction group
7482When @command{gcc} is called with @option{-mrelax}, it can emit group
7483of instructions that the linker can optimize to use a 68HC11 direct
7484addressing mode. These instructions consists of @code{bclr} or
7485@code{bset} instructions.
7486
7487@end table
7488
7489@subsection Trampoline Generation
7490
7491@cindex trampoline generation on M68HC11
7492@cindex trampoline generation on M68HC12
7493For 68HC11 and 68HC12, @command{ld} can generate trampoline code to
7494call a far function using a normal @code{jsr} instruction. The linker
c0065db7 7495will also change the relocation to some far function to use the
93fd0973
SC
7496trampoline address instead of the function address. This is typically the
7497case when a pointer to a function is taken. The pointer will in fact
7498point to the function trampoline.
7499
7500@ifclear GENERIC
7501@lowersections
7502@end ifclear
7503@end ifset
7504
36f63dca 7505@node ARM
3674e28a 7506@section @command{ld} and the ARM family
36f63dca
NC
7507
7508@cindex ARM interworking support
7509@kindex --support-old-code
7510For the ARM, @command{ld} will generate code stubs to allow functions calls
b45619c0 7511between ARM and Thumb code. These stubs only work with code that has
36f63dca
NC
7512been compiled and assembled with the @samp{-mthumb-interwork} command
7513line option. If it is necessary to link with old ARM object files or
7514libraries, which have not been compiled with the -mthumb-interwork
a05a5b64 7515option then the @samp{--support-old-code} command-line switch should be
36f63dca
NC
7516given to the linker. This will make it generate larger stub functions
7517which will work with non-interworking aware ARM code. Note, however,
7518the linker does not support generating stubs for function calls to
7519non-interworking aware Thumb code.
7520
7521@cindex thumb entry point
7522@cindex entry point, thumb
7523@kindex --thumb-entry=@var{entry}
7524The @samp{--thumb-entry} switch is a duplicate of the generic
7525@samp{--entry} switch, in that it sets the program's starting address.
7526But it also sets the bottom bit of the address, so that it can be
7527branched to using a BX instruction, and the program will start
7528executing in Thumb mode straight away.
7529
ce11ba6c
KT
7530@cindex PE import table prefixing
7531@kindex --use-nul-prefixed-import-tables
7532The @samp{--use-nul-prefixed-import-tables} switch is specifying, that
7533the import tables idata4 and idata5 have to be generated with a zero
11e7fd74 7534element prefix for import libraries. This is the old style to generate
ce11ba6c
KT
7535import tables. By default this option is turned off.
7536
e489d0ae
PB
7537@cindex BE8
7538@kindex --be8
7539The @samp{--be8} switch instructs @command{ld} to generate BE8 format
080bb7bb
NC
7540executables. This option is only valid when linking big-endian
7541objects - ie ones which have been assembled with the @option{-EB}
7542option. The resulting image will contain big-endian data and
7543little-endian code.
e489d0ae 7544
3674e28a
PB
7545@cindex TARGET1
7546@kindex --target1-rel
7547@kindex --target1-abs
7548The @samp{R_ARM_TARGET1} relocation is typically used for entries in the
7549@samp{.init_array} section. It is interpreted as either @samp{R_ARM_REL32}
7550or @samp{R_ARM_ABS32}, depending on the target. The @samp{--target1-rel}
7551and @samp{--target1-abs} switches override the default.
7552
7553@cindex TARGET2
7554@kindex --target2=@var{type}
7555The @samp{--target2=type} switch overrides the default definition of the
7556@samp{R_ARM_TARGET2} relocation. Valid values for @samp{type}, their
7557meanings, and target defaults are as follows:
7558@table @samp
7559@item rel
eeac373a
PB
7560@samp{R_ARM_REL32} (arm*-*-elf, arm*-*-eabi)
7561@item abs
a57d1773 7562@samp{R_ARM_ABS32}
3674e28a
PB
7563@item got-rel
7564@samp{R_ARM_GOT_PREL} (arm*-*-linux, arm*-*-*bsd)
7565@end table
7566
319850b4
JB
7567@cindex FIX_V4BX
7568@kindex --fix-v4bx
7569The @samp{R_ARM_V4BX} relocation (defined by the ARM AAELF
7570specification) enables objects compiled for the ARMv4 architecture to be
7571interworking-safe when linked with other objects compiled for ARMv4t, but
7572also allows pure ARMv4 binaries to be built from the same ARMv4 objects.
7573
7574In the latter case, the switch @option{--fix-v4bx} must be passed to the
7575linker, which causes v4t @code{BX rM} instructions to be rewritten as
7576@code{MOV PC,rM}, since v4 processors do not have a @code{BX} instruction.
7577
7578In the former case, the switch should not be used, and @samp{R_ARM_V4BX}
7579relocations are ignored.
7580
845b51d6
PB
7581@cindex FIX_V4BX_INTERWORKING
7582@kindex --fix-v4bx-interworking
7583Replace @code{BX rM} instructions identified by @samp{R_ARM_V4BX}
7584relocations with a branch to the following veneer:
7585
7586@smallexample
7587TST rM, #1
7588MOVEQ PC, rM
7589BX Rn
7590@end smallexample
7591
7592This allows generation of libraries/applications that work on ARMv4 cores
7593and are still interworking safe. Note that the above veneer clobbers the
11e7fd74 7594condition flags, so may cause incorrect program behavior in rare cases.
845b51d6 7595
33bfe774
JB
7596@cindex USE_BLX
7597@kindex --use-blx
7598The @samp{--use-blx} switch enables the linker to use ARM/Thumb
7599BLX instructions (available on ARMv5t and above) in various
7600situations. Currently it is used to perform calls via the PLT from Thumb
7601code using BLX rather than using BX and a mode-switching stub before
7602each PLT entry. This should lead to such calls executing slightly faster.
7603
c6dd86c6
JB
7604@cindex VFP11_DENORM_FIX
7605@kindex --vfp11-denorm-fix
7606The @samp{--vfp11-denorm-fix} switch enables a link-time workaround for a
7607bug in certain VFP11 coprocessor hardware, which sometimes allows
7608instructions with denorm operands (which must be handled by support code)
7609to have those operands overwritten by subsequent instructions before
7610the support code can read the intended values.
7611
7612The bug may be avoided in scalar mode if you allow at least one
7613intervening instruction between a VFP11 instruction which uses a register
7614and another instruction which writes to the same register, or at least two
7615intervening instructions if vector mode is in use. The bug only affects
7616full-compliance floating-point mode: you do not need this workaround if
7617you are using "runfast" mode. Please contact ARM for further details.
7618
7619If you know you are using buggy VFP11 hardware, you can
7620enable this workaround by specifying the linker option
7621@samp{--vfp-denorm-fix=scalar} if you are using the VFP11 scalar
7622mode only, or @samp{--vfp-denorm-fix=vector} if you are using
7623vector mode (the latter also works for scalar code). The default is
7624@samp{--vfp-denorm-fix=none}.
7625
7626If the workaround is enabled, instructions are scanned for
7627potentially-troublesome sequences, and a veneer is created for each
7628such sequence which may trigger the erratum. The veneer consists of the
7629first instruction of the sequence and a branch back to the subsequent
7630instruction. The original instruction is then replaced with a branch to
7631the veneer. The extra cycles required to call and return from the veneer
7632are sufficient to avoid the erratum in both the scalar and vector cases.
7633
2de70689
MGD
7634@cindex ARM1176 erratum workaround
7635@kindex --fix-arm1176
7636@kindex --no-fix-arm1176
9d5777a3
RM
7637The @samp{--fix-arm1176} switch enables a link-time workaround for an erratum
7638in certain ARM1176 processors. The workaround is enabled by default if you
7639are targeting ARM v6 (excluding ARM v6T2) or earlier. It can be disabled
2de70689
MGD
7640unconditionally by specifying @samp{--no-fix-arm1176}.
7641
9d5777a3 7642Further information is available in the ``ARM1176JZ-S and ARM1176JZF-S
11e7fd74 7643Programmer Advice Notice'' available on the ARM documentation website at:
2de70689
MGD
7644http://infocenter.arm.com/.
7645
a504d23a
LA
7646@cindex STM32L4xx erratum workaround
7647@kindex --fix-stm32l4xx-629360
7648
7649The @samp{--fix-stm32l4xx-629360} switch enables a link-time
7650workaround for a bug in the bus matrix / memory controller for some of
7651the STM32 Cortex-M4 based products (STM32L4xx). When accessing
7652off-chip memory via the affected bus for bus reads of 9 words or more,
7653the bus can generate corrupt data and/or abort. These are only
7654core-initiated accesses (not DMA), and might affect any access:
7655integer loads such as LDM, POP and floating-point loads such as VLDM,
7656VPOP. Stores are not affected.
7657
7658The bug can be avoided by splitting memory accesses into the
7659necessary chunks to keep bus reads below 8 words.
7660
7661The workaround is not enabled by default, this is equivalent to use
7662@samp{--fix-stm32l4xx-629360=none}. If you know you are using buggy
7663STM32L4xx hardware, you can enable the workaround by specifying the
7664linker option @samp{--fix-stm32l4xx-629360}, or the equivalent
7665@samp{--fix-stm32l4xx-629360=default}.
7666
7667If the workaround is enabled, instructions are scanned for
7668potentially-troublesome sequences, and a veneer is created for each
7669such sequence which may trigger the erratum. The veneer consists in a
7670replacement sequence emulating the behaviour of the original one and a
7671branch back to the subsequent instruction. The original instruction is
7672then replaced with a branch to the veneer.
7673
7674The workaround does not always preserve the memory access order for
7675the LDMDB instruction, when the instruction loads the PC.
7676
7677The workaround is not able to handle problematic instructions when
7678they are in the middle of an IT block, since a branch is not allowed
7679there. In that case, the linker reports a warning and no replacement
7680occurs.
7681
7682The workaround is not able to replace problematic instructions with a
7683PC-relative branch instruction if the @samp{.text} section is too
7684large. In that case, when the branch that replaces the original code
7685cannot be encoded, the linker reports a warning and no replacement
7686occurs.
7687
bf21ed78
MS
7688@cindex NO_ENUM_SIZE_WARNING
7689@kindex --no-enum-size-warning
726150b7 7690The @option{--no-enum-size-warning} switch prevents the linker from
bf21ed78
MS
7691warning when linking object files that specify incompatible EABI
7692enumeration size attributes. For example, with this switch enabled,
7693linking of an object file using 32-bit enumeration values with another
7694using enumeration values fitted into the smallest possible space will
7695not be diagnosed.
a9dc9481
JM
7696
7697@cindex NO_WCHAR_SIZE_WARNING
7698@kindex --no-wchar-size-warning
7699The @option{--no-wchar-size-warning} switch prevents the linker from
7700warning when linking object files that specify incompatible EABI
7701@code{wchar_t} size attributes. For example, with this switch enabled,
7702linking of an object file using 32-bit @code{wchar_t} values with another
7703using 16-bit @code{wchar_t} values will not be diagnosed.
bf21ed78 7704
726150b7
NC
7705@cindex PIC_VENEER
7706@kindex --pic-veneer
7707The @samp{--pic-veneer} switch makes the linker use PIC sequences for
7708ARM/Thumb interworking veneers, even if the rest of the binary
7709is not PIC. This avoids problems on uClinux targets where
7710@samp{--emit-relocs} is used to generate relocatable binaries.
7711
7712@cindex STUB_GROUP_SIZE
7713@kindex --stub-group-size=@var{N}
7714The linker will automatically generate and insert small sequences of
7715code into a linked ARM ELF executable whenever an attempt is made to
7716perform a function call to a symbol that is too far away. The
7717placement of these sequences of instructions - called stubs - is
a05a5b64 7718controlled by the command-line option @option{--stub-group-size=N}.
726150b7 7719The placement is important because a poor choice can create a need for
11e7fd74 7720duplicate stubs, increasing the code size. The linker will try to
726150b7
NC
7721group stubs together in order to reduce interruptions to the flow of
7722code, but it needs guidance as to how big these groups should be and
7723where they should be placed.
7724
7725The value of @samp{N}, the parameter to the
7726@option{--stub-group-size=} option controls where the stub groups are
07d72278 7727placed. If it is negative then all stubs are placed after the first
726150b7
NC
7728branch that needs them. If it is positive then the stubs can be
7729placed either before or after the branches that need them. If the
7730value of @samp{N} is 1 (either +1 or -1) then the linker will choose
7731exactly where to place groups of stubs, using its built in heuristics.
7732A value of @samp{N} greater than 1 (or smaller than -1) tells the
7733linker that a single group of stubs can service at most @samp{N} bytes
7734from the input sections.
7735
7736The default, if @option{--stub-group-size=} is not specified, is
7737@samp{N = +1}.
7738
1a51c1a4
NC
7739Farcalls stubs insertion is fully supported for the ARM-EABI target
7740only, because it relies on object files properties not present
7741otherwise.
7742
1db37fe6
YG
7743@cindex Cortex-A8 erratum workaround
7744@kindex --fix-cortex-a8
7745@kindex --no-fix-cortex-a8
7746The @samp{--fix-cortex-a8} switch enables a link-time workaround for an erratum in certain Cortex-A8 processors. The workaround is enabled by default if you are targeting the ARM v7-A architecture profile. It can be enabled otherwise by specifying @samp{--fix-cortex-a8}, or disabled unconditionally by specifying @samp{--no-fix-cortex-a8}.
7747
7748The erratum only affects Thumb-2 code. Please contact ARM for further details.
7749
68fcca92
JW
7750@cindex Cortex-A53 erratum 835769 workaround
7751@kindex --fix-cortex-a53-835769
7752@kindex --no-fix-cortex-a53-835769
7753The @samp{--fix-cortex-a53-835769} switch enables a link-time workaround for erratum 835769 present on certain early revisions of Cortex-A53 processors. The workaround is disabled by default. It can be enabled by specifying @samp{--fix-cortex-a53-835769}, or disabled unconditionally by specifying @samp{--no-fix-cortex-a53-835769}.
7754
7755Please contact ARM for further details.
7756
1db37fe6
YG
7757@kindex --merge-exidx-entries
7758@kindex --no-merge-exidx-entries
7759@cindex Merging exidx entries
7760The @samp{--no-merge-exidx-entries} switch disables the merging of adjacent exidx entries in debuginfo.
7761
7762@kindex --long-plt
7763@cindex 32-bit PLT entries
7764The @samp{--long-plt} option enables the use of 16 byte PLT entries
7765which support up to 4Gb of code. The default is to use 12 byte PLT
7766entries which only support 512Mb of code.
7767
1f56df9d
JW
7768@kindex --no-apply-dynamic-relocs
7769@cindex AArch64 rela addend
7770The @samp{--no-apply-dynamic-relocs} option makes AArch64 linker do not apply
7771link-time values for dynamic relocations.
7772
4ba2ef8f
TP
7773@cindex Placement of SG veneers
7774All SG veneers are placed in the special output section @code{.gnu.sgstubs}.
a05a5b64 7775Its start address must be set, either with the command-line option
4ba2ef8f
TP
7776@samp{--section-start} or in a linker script, to indicate where to place these
7777veneers in memory.
7778
54ddd295
TP
7779@kindex --cmse-implib
7780@cindex Secure gateway import library
7781The @samp{--cmse-implib} option requests that the import libraries
7782specified by the @samp{--out-implib} and @samp{--in-implib} options are
7783secure gateway import libraries, suitable for linking a non-secure
7784executable against secure code as per ARMv8-M Security Extensions.
7785
0955507f
TP
7786@kindex --in-implib=@var{file}
7787@cindex Input import library
7788The @samp{--in-implib=file} specifies an input import library whose symbols
7789must keep the same address in the executable being produced. A warning is
7790given if no @samp{--out-implib} is given but new symbols have been introduced
7791in the executable that should be listed in its import library. Otherwise, if
7792@samp{--out-implib} is specified, the symbols are added to the output import
7793library. A warning is also given if some symbols present in the input import
7794library have disappeared from the executable. This option is only effective
7795for Secure Gateway import libraries, ie. when @samp{--cmse-implib} is
7796specified.
7797
36f63dca
NC
7798@ifclear GENERIC
7799@lowersections
7800@end ifclear
7801@end ifset
7802
7803@ifset HPPA
7804@ifclear GENERIC
7805@raisesections
7806@end ifclear
7807
7808@node HPPA ELF32
7809@section @command{ld} and HPPA 32-bit ELF Support
7810@cindex HPPA multiple sub-space stubs
7811@kindex --multi-subspace
7812When generating a shared library, @command{ld} will by default generate
7813import stubs suitable for use with a single sub-space application.
7814The @samp{--multi-subspace} switch causes @command{ld} to generate export
7815stubs, and different (larger) import stubs suitable for use with
7816multiple sub-spaces.
7817
7818@cindex HPPA stub grouping
7819@kindex --stub-group-size=@var{N}
7820Long branch stubs and import/export stubs are placed by @command{ld} in
7821stub sections located between groups of input sections.
7822@samp{--stub-group-size} specifies the maximum size of a group of input
7823sections handled by one stub section. Since branch offsets are signed,
7824a stub section may serve two groups of input sections, one group before
7825the stub section, and one group after it. However, when using
7826conditional branches that require stubs, it may be better (for branch
7827prediction) that stub sections only serve one group of input sections.
7828A negative value for @samp{N} chooses this scheme, ensuring that
7829branches to stubs always use a negative offset. Two special values of
7830@samp{N} are recognized, @samp{1} and @samp{-1}. These both instruct
7831@command{ld} to automatically size input section groups for the branch types
7832detected, with the same behaviour regarding stub placement as other
7833positive or negative values of @samp{N} respectively.
7834
7835Note that @samp{--stub-group-size} does not split input sections. A
7836single input section larger than the group size specified will of course
7837create a larger group (of one section). If input sections are too
7838large, it may not be possible for a branch to reach its stub.
7839
7840@ifclear GENERIC
7841@lowersections
7842@end ifclear
7843@end ifset
7844
7fb9f789
NC
7845@ifset M68K
7846@ifclear GENERIC
7847@raisesections
7848@end ifclear
7849
7850@node M68K
7851@section @command{ld} and the Motorola 68K family
7852
7853@cindex Motorola 68K GOT generation
7854@kindex --got=@var{type}
7855The @samp{--got=@var{type}} option lets you choose the GOT generation scheme.
7856The choices are @samp{single}, @samp{negative}, @samp{multigot} and
7857@samp{target}. When @samp{target} is selected the linker chooses
7858the default GOT generation scheme for the current target.
7859@samp{single} tells the linker to generate a single GOT with
7860entries only at non-negative offsets.
7861@samp{negative} instructs the linker to generate a single GOT with
7862entries at both negative and positive offsets. Not all environments
7863support such GOTs.
7864@samp{multigot} allows the linker to generate several GOTs in the
7865output file. All GOT references from a single input object
7866file access the same GOT, but references from different input object
7867files might access different GOTs. Not all environments support such GOTs.
7868
7869@ifclear GENERIC
7870@lowersections
7871@end ifclear
7872@end ifset
7873
833794fc
MR
7874@ifset MIPS
7875@ifclear GENERIC
7876@raisesections
7877@end ifclear
7878
7879@node MIPS
7880@section @command{ld} and the MIPS family
7881
7882@cindex MIPS microMIPS instruction choice selection
7883@kindex --insn32
7884@kindex --no-insn32
7885The @samp{--insn32} and @samp{--no-insn32} options control the choice of
7886microMIPS instructions used in code generated by the linker, such as that
7887in the PLT or lazy binding stubs, or in relaxation. If @samp{--insn32} is
7888used, then the linker only uses 32-bit instruction encodings. By default
7889or if @samp{--no-insn32} is used, all instruction encodings are used,
7890including 16-bit ones where possible.
7891
8b10b0b3
MR
7892@cindex MIPS branch relocation check control
7893@kindex --ignore-branch-isa
7894@kindex --no-ignore-branch-isa
7895The @samp{--ignore-branch-isa} and @samp{--no-ignore-branch-isa} options
7896control branch relocation checks for invalid ISA mode transitions. If
7897@samp{--ignore-branch-isa} is used, then the linker accepts any branch
7898relocations and any ISA mode transition required is lost in relocation
7899calculation, except for some cases of @code{BAL} instructions which meet
7900relaxation conditions and are converted to equivalent @code{JALX}
7901instructions as the associated relocation is calculated. By default
7902or if @samp{--no-ignore-branch-isa} is used a check is made causing
7903the loss of an ISA mode transition to produce an error.
7904
833794fc
MR
7905@ifclear GENERIC
7906@lowersections
7907@end ifclear
7908@end ifset
7909
36f63dca
NC
7910@ifset MMIX
7911@ifclear GENERIC
7912@raisesections
7913@end ifclear
7914
7915@node MMIX
7916@section @code{ld} and MMIX
7917For MMIX, there is a choice of generating @code{ELF} object files or
7918@code{mmo} object files when linking. The simulator @code{mmix}
7919understands the @code{mmo} format. The binutils @code{objcopy} utility
7920can translate between the two formats.
7921
7922There is one special section, the @samp{.MMIX.reg_contents} section.
7923Contents in this section is assumed to correspond to that of global
7924registers, and symbols referring to it are translated to special symbols,
7925equal to registers. In a final link, the start address of the
7926@samp{.MMIX.reg_contents} section corresponds to the first allocated
7927global register multiplied by 8. Register @code{$255} is not included in
7928this section; it is always set to the program entry, which is at the
7929symbol @code{Main} for @code{mmo} files.
7930
7a2de473
HPN
7931Global symbols with the prefix @code{__.MMIX.start.}, for example
7932@code{__.MMIX.start..text} and @code{__.MMIX.start..data} are special.
7933The default linker script uses these to set the default start address
7934of a section.
36f63dca
NC
7935
7936Initial and trailing multiples of zero-valued 32-bit words in a section,
7937are left out from an mmo file.
7938
7939@ifclear GENERIC
7940@lowersections
7941@end ifclear
7942@end ifset
7943
7944@ifset MSP430
7945@ifclear GENERIC
7946@raisesections
7947@end ifclear
7948
7949@node MSP430
7950@section @code{ld} and MSP430
7951For the MSP430 it is possible to select the MPU architecture. The flag @samp{-m [mpu type]}
7952will select an appropriate linker script for selected MPU type. (To get a list of known MPUs
7953just pass @samp{-m help} option to the linker).
7954
7955@cindex MSP430 extra sections
7956The linker will recognize some extra sections which are MSP430 specific:
7957
7958@table @code
7959@item @samp{.vectors}
7960Defines a portion of ROM where interrupt vectors located.
7961
7962@item @samp{.bootloader}
7963Defines the bootloader portion of the ROM (if applicable). Any code
7964in this section will be uploaded to the MPU.
7965
7966@item @samp{.infomem}
7967Defines an information memory section (if applicable). Any code in
7968this section will be uploaded to the MPU.
7969
c0065db7 7970@item @samp{.infomemnobits}
36f63dca
NC
7971This is the same as the @samp{.infomem} section except that any code
7972in this section will not be uploaded to the MPU.
7973
7974@item @samp{.noinit}
7975Denotes a portion of RAM located above @samp{.bss} section.
7976
c0065db7 7977The last two sections are used by gcc.
36f63dca
NC
7978@end table
7979
7ef3addb
JL
7980@table @option
7981@cindex MSP430 Options
7982@kindex --code-region
7983@item --code-region=[either,lower,upper,none]
7984This will transform .text* sections to [either,lower,upper].text* sections. The
7985argument passed to GCC for -mcode-region is propagated to the linker
7986using this option.
7987
7988@kindex --data-region
7989@item --data-region=[either,lower,upper,none]
7990This will transform .data*, .bss* and .rodata* sections to
7991[either,lower,upper].[data,bss,rodata]* sections. The argument passed to GCC
7992for -mdata-region is propagated to the linker using this option.
7993
7994@kindex --disable-sec-transformation
7995@item --disable-sec-transformation
7996Prevent the transformation of sections as specified by the @code{--code-region}
7997and @code{--data-region} options.
7998This is useful if you are compiling and linking using a single call to the GCC
7999wrapper, and want to compile the source files using -m[code,data]-region but
8000not transform the sections for prebuilt libraries and objects.
8001@end table
8002
36f63dca
NC
8003@ifclear GENERIC
8004@lowersections
8005@end ifclear
8006@end ifset
8007
35c08157
KLC
8008@ifset NDS32
8009@ifclear GENERIC
8010@raisesections
8011@end ifclear
8012
8013@node NDS32
8014@section @code{ld} and NDS32
8015@kindex relaxing on NDS32
8016For NDS32, there are some options to select relaxation behavior. The linker
8017relaxes objects according to these options.
8018
8019@table @code
8020@item @samp{--m[no-]fp-as-gp}
8021Disable/enable fp-as-gp relaxation.
8022
8023@item @samp{--mexport-symbols=FILE}
8024Exporting symbols and their address into FILE as linker script.
8025
8026@item @samp{--m[no-]ex9}
8027Disable/enable link-time EX9 relaxation.
8028
8029@item @samp{--mexport-ex9=FILE}
8030Export the EX9 table after linking.
8031
8032@item @samp{--mimport-ex9=FILE}
8033Import the Ex9 table for EX9 relaxation.
8034
8035@item @samp{--mupdate-ex9}
8036Update the existing EX9 table.
8037
8038@item @samp{--mex9-limit=NUM}
8039Maximum number of entries in the ex9 table.
8040
8041@item @samp{--mex9-loop-aware}
8042Avoid generating the EX9 instruction inside the loop.
8043
8044@item @samp{--m[no-]ifc}
8045Disable/enable the link-time IFC optimization.
8046
8047@item @samp{--mifc-loop-aware}
8048Avoid generating the IFC instruction inside the loop.
8049@end table
8050
8051@ifclear GENERIC
8052@lowersections
8053@end ifclear
8054@end ifset
8055
78058a5e
SL
8056@ifset NIOSII
8057@ifclear GENERIC
8058@raisesections
8059@end ifclear
8060
8061@node Nios II
8062@section @command{ld} and the Altera Nios II
8063@cindex Nios II call relaxation
8064@kindex --relax on Nios II
8065
8066Call and immediate jump instructions on Nios II processors are limited to
8067transferring control to addresses in the same 256MB memory segment,
8068which may result in @command{ld} giving
8069@samp{relocation truncated to fit} errors with very large programs.
8070The command-line option @option{--relax} enables the generation of
8071trampolines that can access the entire 32-bit address space for calls
8072outside the normal @code{call} and @code{jmpi} address range. These
8073trampolines are inserted at section boundaries, so may not themselves
8074be reachable if an input section and its associated call trampolines are
8075larger than 256MB.
8076
8077The @option{--relax} option is enabled by default unless @option{-r}
8078is also specified. You can disable trampoline generation by using the
8079@option{--no-relax} linker option. You can also disable this optimization
8080locally by using the @samp{set .noat} directive in assembly-language
8081source files, as the linker-inserted trampolines use the @code{at}
8082register as a temporary.
8083
8084Note that the linker @option{--relax} option is independent of assembler
8085relaxation options, and that using the GNU assembler's @option{-relax-all}
8086option interferes with the linker's more selective call instruction relaxation.
8087
8088@ifclear GENERIC
8089@lowersections
8090@end ifclear
8091@end ifset
8092
2a60a7a8
AM
8093@ifset POWERPC
8094@ifclear GENERIC
8095@raisesections
8096@end ifclear
8097
8098@node PowerPC ELF32
8099@section @command{ld} and PowerPC 32-bit ELF Support
8100@cindex PowerPC long branches
8101@kindex --relax on PowerPC
8102Branches on PowerPC processors are limited to a signed 26-bit
8103displacement, which may result in @command{ld} giving
8104@samp{relocation truncated to fit} errors with very large programs.
8105@samp{--relax} enables the generation of trampolines that can access
8106the entire 32-bit address space. These trampolines are inserted at
8107section boundaries, so may not themselves be reachable if an input
c8a1f254
NS
8108section exceeds 33M in size. You may combine @samp{-r} and
8109@samp{--relax} to add trampolines in a partial link. In that case
8110both branches to undefined symbols and inter-section branches are also
8111considered potentially out of range, and trampolines inserted.
2a60a7a8
AM
8112
8113@cindex PowerPC ELF32 options
8114@table @option
8115@cindex PowerPC PLT
8116@kindex --bss-plt
8117@item --bss-plt
8118Current PowerPC GCC accepts a @samp{-msecure-plt} option that
8119generates code capable of using a newer PLT and GOT layout that has
8120the security advantage of no executable section ever needing to be
8121writable and no writable section ever being executable. PowerPC
8122@command{ld} will generate this layout, including stubs to access the
8123PLT, if all input files (including startup and static libraries) were
8124compiled with @samp{-msecure-plt}. @samp{--bss-plt} forces the old
8125BSS PLT (and GOT layout) which can give slightly better performance.
8126
016687f8
AM
8127@kindex --secure-plt
8128@item --secure-plt
8129@command{ld} will use the new PLT and GOT layout if it is linking new
8130@samp{-fpic} or @samp{-fPIC} code, but does not do so automatically
8131when linking non-PIC code. This option requests the new PLT and GOT
8132layout. A warning will be given if some object file requires the old
8133style BSS PLT.
8134
2a60a7a8
AM
8135@cindex PowerPC GOT
8136@kindex --sdata-got
8137@item --sdata-got
8138The new secure PLT and GOT are placed differently relative to other
8139sections compared to older BSS PLT and GOT placement. The location of
8140@code{.plt} must change because the new secure PLT is an initialized
8141section while the old PLT is uninitialized. The reason for the
8142@code{.got} change is more subtle: The new placement allows
8143@code{.got} to be read-only in applications linked with
8144@samp{-z relro -z now}. However, this placement means that
8145@code{.sdata} cannot always be used in shared libraries, because the
8146PowerPC ABI accesses @code{.sdata} in shared libraries from the GOT
8147pointer. @samp{--sdata-got} forces the old GOT placement. PowerPC
8148GCC doesn't use @code{.sdata} in shared libraries, so this option is
8149really only useful for other compilers that may do so.
8150
8151@cindex PowerPC stub symbols
8152@kindex --emit-stub-syms
8153@item --emit-stub-syms
8154This option causes @command{ld} to label linker stubs with a local
8155symbol that encodes the stub type and destination.
8156
8157@cindex PowerPC TLS optimization
8158@kindex --no-tls-optimize
8159@item --no-tls-optimize
8160PowerPC @command{ld} normally performs some optimization of code
8161sequences used to access Thread-Local Storage. Use this option to
8162disable the optimization.
8163@end table
8164
8165@ifclear GENERIC
8166@lowersections
8167@end ifclear
8168@end ifset
8169
8170@ifset POWERPC64
8171@ifclear GENERIC
8172@raisesections
8173@end ifclear
8174
8175@node PowerPC64 ELF64
8176@section @command{ld} and PowerPC64 64-bit ELF Support
8177
8178@cindex PowerPC64 ELF64 options
8179@table @option
8180@cindex PowerPC64 stub grouping
8181@kindex --stub-group-size
8182@item --stub-group-size
8183Long branch stubs, PLT call stubs and TOC adjusting stubs are placed
8184by @command{ld} in stub sections located between groups of input sections.
8185@samp{--stub-group-size} specifies the maximum size of a group of input
8186sections handled by one stub section. Since branch offsets are signed,
8187a stub section may serve two groups of input sections, one group before
8188the stub section, and one group after it. However, when using
8189conditional branches that require stubs, it may be better (for branch
8190prediction) that stub sections only serve one group of input sections.
8191A negative value for @samp{N} chooses this scheme, ensuring that
8192branches to stubs always use a negative offset. Two special values of
8193@samp{N} are recognized, @samp{1} and @samp{-1}. These both instruct
8194@command{ld} to automatically size input section groups for the branch types
8195detected, with the same behaviour regarding stub placement as other
8196positive or negative values of @samp{N} respectively.
8197
8198Note that @samp{--stub-group-size} does not split input sections. A
8199single input section larger than the group size specified will of course
8200create a larger group (of one section). If input sections are too
8201large, it may not be possible for a branch to reach its stub.
8202
8203@cindex PowerPC64 stub symbols
8204@kindex --emit-stub-syms
8205@item --emit-stub-syms
8206This option causes @command{ld} to label linker stubs with a local
8207symbol that encodes the stub type and destination.
8208
8209@cindex PowerPC64 dot symbols
8210@kindex --dotsyms
8211@kindex --no-dotsyms
95421fb9
AM
8212@item --dotsyms
8213@itemx --no-dotsyms
2a60a7a8
AM
8214These two options control how @command{ld} interprets version patterns
8215in a version script. Older PowerPC64 compilers emitted both a
8216function descriptor symbol with the same name as the function, and a
8217code entry symbol with the name prefixed by a dot (@samp{.}). To
8218properly version a function @samp{foo}, the version script thus needs
8219to control both @samp{foo} and @samp{.foo}. The option
8220@samp{--dotsyms}, on by default, automatically adds the required
8221dot-prefixed patterns. Use @samp{--no-dotsyms} to disable this
8222feature.
8223
7ae4ea7d
AM
8224@cindex PowerPC64 register save/restore functions
8225@kindex --save-restore-funcs
8226@kindex --no-save-restore-funcs
95421fb9
AM
8227@item --save-restore-funcs
8228@itemx --no-save-restore-funcs
7ae4ea7d
AM
8229These two options control whether PowerPC64 @command{ld} automatically
8230provides out-of-line register save and restore functions used by
8231@samp{-Os} code. The default is to provide any such referenced
8232function for a normal final link, and to not do so for a relocatable
8233link.
8234
2a60a7a8
AM
8235@cindex PowerPC64 TLS optimization
8236@kindex --no-tls-optimize
8237@item --no-tls-optimize
8238PowerPC64 @command{ld} normally performs some optimization of code
8239sequences used to access Thread-Local Storage. Use this option to
8240disable the optimization.
8241
7c9cf415
AM
8242@cindex PowerPC64 __tls_get_addr optimization
8243@kindex --tls-get-addr-optimize
8244@kindex --no-tls-get-addr-optimize
9e7028aa
AM
8245@kindex --tls-get-addr-regsave
8246@kindex --no-tls-get-addr-regsave
95421fb9
AM
8247@item --tls-get-addr-optimize
8248@itemx --no-tls-get-addr-optimize
9e7028aa 8249These options control how PowerPC64 @command{ld} uses a special
7c9cf415
AM
8250stub to call __tls_get_addr. PowerPC64 glibc 2.22 and later support
8251an optimization that allows the second and subsequent calls to
8252@code{__tls_get_addr} for a given symbol to be resolved by the special
9e7028aa
AM
8253stub without calling in to glibc. By default the linker enables
8254generation of the stub when glibc advertises the availability of
8255__tls_get_addr_opt.
8256Using @option{--tls-get-addr-optimize} with an older glibc won't do
8257much besides slow down your applications, but may be useful if linking
8258an application against an older glibc with the expectation that it
8259will normally be used on systems having a newer glibc.
8260@option{--tls-get-addr-regsave} forces generation of a stub that saves
8261and restores volatile registers around the call into glibc. Normally,
8262this is done when the linker detects a call to __tls_get_addr_desc.
8263Such calls then go via the register saving stub to __tls_get_addr_opt.
5cebc931 8264@option{--no-tls-get-addr-regsave} disables generation of the
9e7028aa 8265register saves.
7c9cf415 8266
2a60a7a8
AM
8267@cindex PowerPC64 OPD optimization
8268@kindex --no-opd-optimize
8269@item --no-opd-optimize
8270PowerPC64 @command{ld} normally removes @code{.opd} section entries
8271corresponding to deleted link-once functions, or functions removed by
e7fc76dd 8272the action of @samp{--gc-sections} or linker script @code{/DISCARD/}.
2a60a7a8
AM
8273Use this option to disable @code{.opd} optimization.
8274
8275@cindex PowerPC64 OPD spacing
8276@kindex --non-overlapping-opd
8277@item --non-overlapping-opd
8278Some PowerPC64 compilers have an option to generate compressed
8279@code{.opd} entries spaced 16 bytes apart, overlapping the third word,
8280the static chain pointer (unused in C) with the first word of the next
8281entry. This option expands such entries to the full 24 bytes.
8282
8283@cindex PowerPC64 TOC optimization
8284@kindex --no-toc-optimize
8285@item --no-toc-optimize
8286PowerPC64 @command{ld} normally removes unused @code{.toc} section
8287entries. Such entries are detected by examining relocations that
8288reference the TOC in code sections. A reloc in a deleted code section
8289marks a TOC word as unneeded, while a reloc in a kept code section
8290marks a TOC word as needed. Since the TOC may reference itself, TOC
8291relocs are also examined. TOC words marked as both needed and
8292unneeded will of course be kept. TOC words without any referencing
8293reloc are assumed to be part of a multi-word entry, and are kept or
8294discarded as per the nearest marked preceding word. This works
8295reliably for compiler generated code, but may be incorrect if assembly
8296code is used to insert TOC entries. Use this option to disable the
8297optimization.
8298
d882c988
AM
8299@cindex PowerPC64 inline PLT call optimization
8300@kindex --no-inline-optimize
8301@item --no-inline-optimize
8302PowerPC64 @command{ld} normally replaces inline PLT call sequences
8303marked with @code{R_PPC64_PLTSEQ}, @code{R_PPC64_PLTCALL},
8304@code{R_PPC64_PLT16_HA} and @code{R_PPC64_PLT16_LO_DS} relocations by
8305a number of @code{nop}s and a direct call when the function is defined
8306locally and can't be overridden by some other definition. This option
8307disables that optimization.
8308
2a60a7a8
AM
8309@cindex PowerPC64 multi-TOC
8310@kindex --no-multi-toc
8311@item --no-multi-toc
794e51c0
AM
8312If given any toc option besides @code{-mcmodel=medium} or
8313@code{-mcmodel=large}, PowerPC64 GCC generates code for a TOC model
8314where TOC
2a60a7a8
AM
8315entries are accessed with a 16-bit offset from r2. This limits the
8316total TOC size to 64K. PowerPC64 @command{ld} extends this limit by
8317grouping code sections such that each group uses less than 64K for its
8318TOC entries, then inserts r2 adjusting stubs between inter-group
8319calls. @command{ld} does not split apart input sections, so cannot
8320help if a single input file has a @code{.toc} section that exceeds
832164K, most likely from linking multiple files with @command{ld -r}.
8322Use this option to turn off this feature.
794e51c0
AM
8323
8324@cindex PowerPC64 TOC sorting
8325@kindex --no-toc-sort
8326@item --no-toc-sort
8327By default, @command{ld} sorts TOC sections so that those whose file
8328happens to have a section called @code{.init} or @code{.fini} are
8329placed first, followed by TOC sections referenced by code generated
8330with PowerPC64 gcc's @code{-mcmodel=small}, and lastly TOC sections
8331referenced only by code generated with PowerPC64 gcc's
8332@code{-mcmodel=medium} or @code{-mcmodel=large} options. Doing this
8333results in better TOC grouping for multi-TOC. Use this option to turn
8334off this feature.
8335
8336@cindex PowerPC64 PLT stub alignment
8337@kindex --plt-align
8338@kindex --no-plt-align
8339@item --plt-align
8340@itemx --no-plt-align
8341Use these options to control whether individual PLT call stubs are
2420fff6
AM
8342aligned to a 32-byte boundary, or to the specified power of two
8343boundary when using @code{--plt-align=}. A negative value may be
8344specified to pad PLT call stubs so that they do not cross the
8345specified power of two boundary (or the minimum number of boundaries
8346if a PLT stub is so large that it must cross a boundary). By default
8347PLT call stubs are aligned to 32-byte boundaries.
794e51c0
AM
8348
8349@cindex PowerPC64 PLT call stub static chain
8350@kindex --plt-static-chain
8351@kindex --no-plt-static-chain
8352@item --plt-static-chain
8353@itemx --no-plt-static-chain
8354Use these options to control whether PLT call stubs load the static
8355chain pointer (r11). @code{ld} defaults to not loading the static
8356chain since there is never any need to do so on a PLT call.
8357
8358@cindex PowerPC64 PLT call stub thread safety
8359@kindex --plt-thread-safe
8360@kindex --no-plt-thread-safe
8361@item --plt-thread-safe
1be5d8d3 8362@itemx --no-plt-thread-safe
794e51c0
AM
8363With power7's weakly ordered memory model, it is possible when using
8364lazy binding for ld.so to update a plt entry in one thread and have
8365another thread see the individual plt entry words update in the wrong
8366order, despite ld.so carefully writing in the correct order and using
8367memory write barriers. To avoid this we need some sort of read
8368barrier in the call stub, or use LD_BIND_NOW=1. By default, @code{ld}
8369looks for calls to commonly used functions that create threads, and if
8370seen, adds the necessary barriers. Use these options to change the
8371default behaviour.
8b5f1ed8
AM
8372
8373@cindex PowerPC64 ELFv2 PLT localentry optimization
8374@kindex --plt-localentry
8375@kindex --no-plt-localentry
8376@item --plt-localentry
8377@itemx --no-localentry
8378ELFv2 functions with localentry:0 are those with a single entry point,
8379ie. global entry == local entry, and that have no requirement on r2
8380(the TOC/GOT pointer) or r12, and guarantee r2 is unchanged on return.
8381Such an external function can be called via the PLT without saving r2
8382or restoring it on return, avoiding a common load-hit-store for small
8383functions. The optimization is attractive, with up to 40% reduction
8384in execution time for a small function, but can result in symbol
d44c746a
AM
8385interposition failures. Also, minor changes in a shared library,
8386including system libraries, can cause a function that was localentry:0
8387to become localentry:8. This will result in a dynamic loader
8388complaint and failure to run. The option is experimental, use with
8389care. @option{--no-plt-localentry} is the default.
d882c988
AM
8390
8391@cindex PowerPC64 Power10 stubs
8392@kindex --power10-stubs
8393@kindex --no-power10-stubs
8394@item --power10-stubs
8395@itemx --no-power10-stubs
8396When PowerPC64 @command{ld} links input object files containing
8397relocations used on power10 prefixed instructions it normally creates
e10a07b3
AM
8398linkage stubs (PLT call and long branch) using power10 instructions
8399for @code{@@notoc} PLT calls where @code{r2} is not known. The
8400power10 notoc stubs are smaller and faster, so are preferred for
d882c988
AM
8401power10. @option{--power10-stubs} and @option{--no-power10-stubs}
8402allow you to override the linker's selection of stub instructions.
e10a07b3
AM
8403@option{--power10-stubs=auto} allows the user to select the default
8404auto mode.
2a60a7a8
AM
8405@end table
8406
8407@ifclear GENERIC
8408@lowersections
8409@end ifclear
8410@end ifset
8411
b4cbbe8f
AK
8412@ifset S/390
8413@ifclear GENERIC
8414@raisesections
8415@end ifclear
8416
8417@node S/390 ELF
8418@section @command{ld} and S/390 ELF Support
8419
8420@cindex S/390 ELF options
8421@table @option
8422
8423@cindex S/390
8424@kindex --s390-pgste
8425@item --s390-pgste
8426This option marks the result file with a @code{PT_S390_PGSTE}
8427segment. The Linux kernel is supposed to allocate 4k page tables for
8428binaries marked that way.
8429@end table
8430
8431@ifclear GENERIC
8432@lowersections
8433@end ifclear
8434@end ifset
8435
49fa1e15
AM
8436@ifset SPU
8437@ifclear GENERIC
8438@raisesections
8439@end ifclear
8440
8441@node SPU ELF
8442@section @command{ld} and SPU ELF Support
8443
8444@cindex SPU ELF options
8445@table @option
8446
8447@cindex SPU plugins
8448@kindex --plugin
8449@item --plugin
8450This option marks an executable as a PIC plugin module.
8451
8452@cindex SPU overlays
8453@kindex --no-overlays
8454@item --no-overlays
8455Normally, @command{ld} recognizes calls to functions within overlay
8456regions, and redirects such calls to an overlay manager via a stub.
8457@command{ld} also provides a built-in overlay manager. This option
8458turns off all this special overlay handling.
8459
8460@cindex SPU overlay stub symbols
8461@kindex --emit-stub-syms
8462@item --emit-stub-syms
8463This option causes @command{ld} to label overlay stubs with a local
8464symbol that encodes the stub type and destination.
8465
8466@cindex SPU extra overlay stubs
8467@kindex --extra-overlay-stubs
8468@item --extra-overlay-stubs
8469This option causes @command{ld} to add overlay call stubs on all
8470function calls out of overlay regions. Normally stubs are not added
8471on calls to non-overlay regions.
8472
8473@cindex SPU local store size
8474@kindex --local-store=lo:hi
8475@item --local-store=lo:hi
8476@command{ld} usually checks that a final executable for SPU fits in
8477the address range 0 to 256k. This option may be used to change the
8478range. Disable the check entirely with @option{--local-store=0:0}.
8479
c0065db7 8480@cindex SPU
49fa1e15
AM
8481@kindex --stack-analysis
8482@item --stack-analysis
8483SPU local store space is limited. Over-allocation of stack space
8484unnecessarily limits space available for code and data, while
8485under-allocation results in runtime failures. If given this option,
8486@command{ld} will provide an estimate of maximum stack usage.
8487@command{ld} does this by examining symbols in code sections to
8488determine the extents of functions, and looking at function prologues
8489for stack adjusting instructions. A call-graph is created by looking
8490for relocations on branch instructions. The graph is then searched
8491for the maximum stack usage path. Note that this analysis does not
8492find calls made via function pointers, and does not handle recursion
8493and other cycles in the call graph. Stack usage may be
8494under-estimated if your code makes such calls. Also, stack usage for
8495dynamic allocation, e.g. alloca, will not be detected. If a link map
8496is requested, detailed information about each function's stack usage
8497and calls will be given.
8498
c0065db7 8499@cindex SPU
49fa1e15
AM
8500@kindex --emit-stack-syms
8501@item --emit-stack-syms
8502This option, if given along with @option{--stack-analysis} will result
8503in @command{ld} emitting stack sizing symbols for each function.
8504These take the form @code{__stack_<function_name>} for global
8505functions, and @code{__stack_<number>_<function_name>} for static
8506functions. @code{<number>} is the section id in hex. The value of
8507such symbols is the stack requirement for the corresponding function.
8508The symbol size will be zero, type @code{STT_NOTYPE}, binding
c0065db7 8509@code{STB_LOCAL}, and section @code{SHN_ABS}.
49fa1e15
AM
8510@end table
8511
8512@ifclear GENERIC
8513@lowersections
8514@end ifclear
8515@end ifset
8516
36f63dca
NC
8517@ifset TICOFF
8518@ifclear GENERIC
8519@raisesections
8520@end ifclear
8521
8522@node TI COFF
8523@section @command{ld}'s Support for Various TI COFF Versions
8524@cindex TI COFF versions
8525@kindex --format=@var{version}
8526The @samp{--format} switch allows selection of one of the various
8527TI COFF versions. The latest of this writing is 2; versions 0 and 1 are
8528also supported. The TI COFF versions also vary in header byte-order
8529format; @command{ld} will read any version or byte order, but the output
8530header format depends on the default specified by the specific target.
8531
8532@ifclear GENERIC
8533@lowersections
8534@end ifclear
8535@end ifset
8536
2ca22b03
NC
8537@ifset WIN32
8538@ifclear GENERIC
8539@raisesections
8540@end ifclear
8541
8542@node WIN32
8543@section @command{ld} and WIN32 (cygwin/mingw)
8544
c0065db7 8545This section describes some of the win32 specific @command{ld} issues.
a05a5b64
TP
8546See @ref{Options,,Command-line Options} for detailed description of the
8547command-line options mentioned here.
2ca22b03
NC
8548
8549@table @emph
c0065db7
RM
8550@cindex import libraries
8551@item import libraries
69da35b5 8552The standard Windows linker creates and uses so-called import
2ca22b03 8553libraries, which contains information for linking to dll's. They are
69da35b5
NC
8554regular static archives and are handled as any other static
8555archive. The cygwin and mingw ports of @command{ld} have specific
2ca22b03 8556support for creating such libraries provided with the
a05a5b64 8557@samp{--out-implib} command-line option.
2ca22b03 8558
d1792f72
NC
8559@item Resource only DLLs
8560It is possible to create a DLL that only contains resources, ie just a
8561@samp{.rsrc} section, but in order to do so a custom linker script
8562must be used. This is because the built-in default linker scripts
8563will always create @samp{.text} amd @samp {.idata} sections, even if
8564there is no input to go into them.
8565
8566The script should look like this, although the @code{OUTPUT_FORMAT}
8567should be changed to match the desired format.
8568
8569@example
8570OUTPUT_FORMAT(pei-i386)
8571SECTIONS
8572@{
8573 . = SIZEOF_HEADERS;
8574 . = ALIGN(__section_alignment__);
8575 .rsrc __image_base__ + __section_alignment__ : ALIGN(4)
8576 @{
8577 KEEP (*(.rsrc))
8578 KEEP (*(.rsrc$*))
8579 @}
8580 /DISCARD/ : @{ *(*) @}
8581@}
8582@end example
8583
8584With this script saved to a file called, eg @file{rsrc.ld}, a command
8585line like this can be used to create the resource only DLL
8586@file{rsrc.dll} from an input file called @file{rsrc.o}:
8587
8588@smallexample
8589ld -dll --subsystem windows -e 0 -s rsrc.o -o rsrc.dll -T rsrc.ld
8590@end smallexample
8591
c0065db7
RM
8592@item exporting DLL symbols
8593@cindex exporting DLL symbols
dc8465bf
NC
8594The cygwin/mingw @command{ld} has several ways to export symbols for dll's.
8595
8596@table @emph
8597@item using auto-export functionality
8598@cindex using auto-export functionality
8599By default @command{ld} exports symbols with the auto-export functionality,
a05a5b64 8600which is controlled by the following command-line options:
dc8465bf 8601
0a5d968e
NC
8602@itemize
8603@item --export-all-symbols [This is the default]
8604@item --exclude-symbols
8605@item --exclude-libs
e1c37eb5 8606@item --exclude-modules-for-implib
09e2aba4 8607@item --version-script
0a5d968e
NC
8608@end itemize
8609
09e2aba4
DK
8610When auto-export is in operation, @command{ld} will export all the non-local
8611(global and common) symbols it finds in a DLL, with the exception of a few
8612symbols known to belong to the system's runtime and libraries. As it will
8613often not be desirable to export all of a DLL's symbols, which may include
8614private functions that are not part of any public interface, the command-line
9d5777a3 8615options listed above may be used to filter symbols out from the list for
09e2aba4
DK
8616exporting. The @samp{--output-def} option can be used in order to see the
8617final list of exported symbols with all exclusions taken into effect.
8618
8619If @samp{--export-all-symbols} is not given explicitly on the
0a5d968e
NC
8620command line, then the default auto-export behavior will be @emph{disabled}
8621if either of the following are true:
8622
8623@itemize
8624@item A DEF file is used.
8625@item Any symbol in any object file was marked with the __declspec(dllexport) attribute.
8626@end itemize
dc8465bf 8627
c0065db7
RM
8628@item using a DEF file
8629@cindex using a DEF file
dc8465bf
NC
8630Another way of exporting symbols is using a DEF file. A DEF file is
8631an ASCII file containing definitions of symbols which should be
8632exported when a dll is created. Usually it is named @samp{<dll
8633name>.def} and is added as any other object file to the linker's
0a5d968e 8634command line. The file's name must end in @samp{.def} or @samp{.DEF}.
dc8465bf
NC
8635
8636@example
8637gcc -o <output> <objectfiles> <dll name>.def
8638@end example
8639
0a5d968e
NC
8640Using a DEF file turns off the normal auto-export behavior, unless the
8641@samp{--export-all-symbols} option is also used.
8642
dc8465bf
NC
8643Here is an example of a DEF file for a shared library called @samp{xyz.dll}:
8644
8645@example
4b5bd4e7 8646LIBRARY "xyz.dll" BASE=0x20000000
dc8465bf
NC
8647
8648EXPORTS
8649foo
8650bar
8651_bar = bar
4b5bd4e7
DS
8652another_foo = abc.dll.afoo
8653var1 DATA
7fcab871
KT
8654doo = foo == foo2
8655eoo DATA == var1
c0065db7 8656@end example
dc8465bf 8657
7fcab871 8658This example defines a DLL with a non-default base address and seven
4b5bd4e7
DS
8659symbols in the export table. The third exported symbol @code{_bar} is an
8660alias for the second. The fourth symbol, @code{another_foo} is resolved
8661by "forwarding" to another module and treating it as an alias for
8662@code{afoo} exported from the DLL @samp{abc.dll}. The final symbol
7fcab871
KT
8663@code{var1} is declared to be a data object. The @samp{doo} symbol in
8664export library is an alias of @samp{foo}, which gets the string name
8665in export table @samp{foo2}. The @samp{eoo} symbol is an data export
8666symbol, which gets in export table the name @samp{var1}.
4b5bd4e7 8667
6b31ad16
DS
8668The optional @code{LIBRARY <name>} command indicates the @emph{internal}
8669name of the output DLL. If @samp{<name>} does not include a suffix,
8670the default library suffix, @samp{.DLL} is appended.
8671
b45619c0
NC
8672When the .DEF file is used to build an application, rather than a
8673library, the @code{NAME <name>} command should be used instead of
6b31ad16 8674@code{LIBRARY}. If @samp{<name>} does not include a suffix, the default
c0065db7 8675executable suffix, @samp{.EXE} is appended.
6b31ad16
DS
8676
8677With either @code{LIBRARY <name>} or @code{NAME <name>} the optional
8678specification @code{BASE = <number>} may be used to specify a
c0065db7 8679non-default base address for the image.
6b31ad16
DS
8680
8681If neither @code{LIBRARY <name>} nor @code{NAME <name>} is specified,
a2877985
DS
8682or they specify an empty string, the internal name is the same as the
8683filename specified on the command line.
6b31ad16 8684
4b5bd4e7
DS
8685The complete specification of an export symbol is:
8686
8687@example
8688EXPORTS
8689 ( ( ( <name1> [ = <name2> ] )
8690 | ( <name1> = <module-name> . <external-name>))
7fcab871 8691 [ @@ <integer> ] [NONAME] [DATA] [CONSTANT] [PRIVATE] [== <name3>] ) *
c0065db7 8692@end example
4b5bd4e7
DS
8693
8694Declares @samp{<name1>} as an exported symbol from the DLL, or declares
8695@samp{<name1>} as an exported alias for @samp{<name2>}; or declares
8696@samp{<name1>} as a "forward" alias for the symbol
8697@samp{<external-name>} in the DLL @samp{<module-name>}.
8698Optionally, the symbol may be exported by the specified ordinal
7fcab871
KT
8699@samp{<integer>} alias. The optional @samp{<name3>} is the to be used
8700string in import/export table for the symbol.
4b5bd4e7
DS
8701
8702The optional keywords that follow the declaration indicate:
8703
8704@code{NONAME}: Do not put the symbol name in the DLL's export table. It
8705will still be exported by its ordinal alias (either the value specified
8706by the .def specification or, otherwise, the value assigned by the
8707linker). The symbol name, however, does remain visible in the import
8708library (if any), unless @code{PRIVATE} is also specified.
8709
8710@code{DATA}: The symbol is a variable or object, rather than a function.
8711The import lib will export only an indirect reference to @code{foo} as
8712the symbol @code{_imp__foo} (ie, @code{foo} must be resolved as
8713@code{*_imp__foo}).
8714
8715@code{CONSTANT}: Like @code{DATA}, but put the undecorated @code{foo} as
8716well as @code{_imp__foo} into the import library. Both refer to the
8717read-only import address table's pointer to the variable, not to the
8718variable itself. This can be dangerous. If the user code fails to add
8719the @code{dllimport} attribute and also fails to explicitly add the
8720extra indirection that the use of the attribute enforces, the
8721application will behave unexpectedly.
8722
8723@code{PRIVATE}: Put the symbol in the DLL's export table, but do not put
8724it into the static import library used to resolve imports at link time. The
8725symbol can still be imported using the @code{LoadLibrary/GetProcAddress}
de194d85 8726API at runtime or by using the GNU ld extension of linking directly to
4b5bd4e7 8727the DLL without an import library.
c0065db7 8728
4b5bd4e7
DS
8729See ld/deffilep.y in the binutils sources for the full specification of
8730other DEF file statements
dc8465bf
NC
8731
8732@cindex creating a DEF file
8733While linking a shared dll, @command{ld} is able to create a DEF file
a05a5b64 8734with the @samp{--output-def <file>} command-line option.
0a5d968e
NC
8735
8736@item Using decorations
8737@cindex Using decorations
8738Another way of marking symbols for export is to modify the source code
8739itself, so that when building the DLL each symbol to be exported is
8740declared as:
8741
8742@example
8743__declspec(dllexport) int a_variable
8744__declspec(dllexport) void a_function(int with_args)
8745@end example
8746
8747All such symbols will be exported from the DLL. If, however,
8748any of the object files in the DLL contain symbols decorated in
8749this way, then the normal auto-export behavior is disabled, unless
8750the @samp{--export-all-symbols} option is also used.
8751
8752Note that object files that wish to access these symbols must @emph{not}
c0065db7 8753decorate them with dllexport. Instead, they should use dllimport,
0a5d968e
NC
8754instead:
8755
8756@example
8757__declspec(dllimport) int a_variable
8758__declspec(dllimport) void a_function(int with_args)
8759@end example
8760
c0065db7
RM
8761This complicates the structure of library header files, because
8762when included by the library itself the header must declare the
0a5d968e
NC
8763variables and functions as dllexport, but when included by client
8764code the header must declare them as dllimport. There are a number
c0065db7 8765of idioms that are typically used to do this; often client code can
0a5d968e
NC
8766omit the __declspec() declaration completely. See
8767@samp{--enable-auto-import} and @samp{automatic data imports} for more
b45619c0 8768information.
c0065db7 8769@end table
dc8465bf 8770
2ca22b03
NC
8771@cindex automatic data imports
8772@item automatic data imports
8773The standard Windows dll format supports data imports from dlls only
69da35b5 8774by adding special decorations (dllimport/dllexport), which let the
2ca22b03 8775compiler produce specific assembler instructions to deal with this
c0065db7 8776issue. This increases the effort necessary to port existing Un*x
69da35b5 8777code to these platforms, especially for large
2ca22b03 8778c++ libraries and applications. The auto-import feature, which was
c0065db7 8779initially provided by Paul Sokolovsky, allows one to omit the
b45619c0 8780decorations to achieve a behavior that conforms to that on POSIX/Un*x
c0065db7 8781platforms. This feature is enabled with the @samp{--enable-auto-import}
69da35b5
NC
8782command-line option, although it is enabled by default on cygwin/mingw.
8783The @samp{--enable-auto-import} option itself now serves mainly to
8784suppress any warnings that are ordinarily emitted when linked objects
8785trigger the feature's use.
8786
c0065db7 8787auto-import of variables does not always work flawlessly without
69da35b5
NC
8788additional assistance. Sometimes, you will see this message
8789
c0065db7 8790"variable '<var>' can't be auto-imported. Please read the
69da35b5
NC
8791documentation for ld's @code{--enable-auto-import} for details."
8792
c0065db7
RM
8793The @samp{--enable-auto-import} documentation explains why this error
8794occurs, and several methods that can be used to overcome this difficulty.
8795One of these methods is the @emph{runtime pseudo-relocs} feature, described
69da35b5
NC
8796below.
8797
8798@cindex runtime pseudo-relocation
c0065db7
RM
8799For complex variables imported from DLLs (such as structs or classes),
8800object files typically contain a base address for the variable and an
8801offset (@emph{addend}) within the variable--to specify a particular
8802field or public member, for instance. Unfortunately, the runtime loader used
8803in win32 environments is incapable of fixing these references at runtime
69da35b5 8804without the additional information supplied by dllimport/dllexport decorations.
c0065db7 8805The standard auto-import feature described above is unable to resolve these
69da35b5
NC
8806references.
8807
c0065db7
RM
8808The @samp{--enable-runtime-pseudo-relocs} switch allows these references to
8809be resolved without error, while leaving the task of adjusting the references
8810themselves (with their non-zero addends) to specialized code provided by the
8811runtime environment. Recent versions of the cygwin and mingw environments and
8812compilers provide this runtime support; older versions do not. However, the
8813support is only necessary on the developer's platform; the compiled result will
69da35b5
NC
8814run without error on an older system.
8815
c0065db7
RM
8816@samp{--enable-runtime-pseudo-relocs} is not the default; it must be explicitly
8817enabled as needed.
2ca22b03
NC
8818
8819@cindex direct linking to a dll
8820@item direct linking to a dll
8821The cygwin/mingw ports of @command{ld} support the direct linking,
8822including data symbols, to a dll without the usage of any import
69da35b5 8823libraries. This is much faster and uses much less memory than does the
b45619c0 8824traditional import library method, especially when linking large
c0065db7
RM
8825libraries or applications. When @command{ld} creates an import lib, each
8826function or variable exported from the dll is stored in its own bfd, even
8827though a single bfd could contain many exports. The overhead involved in
69da35b5 8828storing, loading, and processing so many bfd's is quite large, and explains the
c0065db7 8829tremendous time, memory, and storage needed to link against particularly
69da35b5
NC
8830large or complex libraries when using import libs.
8831
c0065db7 8832Linking directly to a dll uses no extra command-line switches other than
69da35b5 8833@samp{-L} and @samp{-l}, because @command{ld} already searches for a number
c0065db7 8834of names to match each library. All that is needed from the developer's
69da35b5
NC
8835perspective is an understanding of this search, in order to force ld to
8836select the dll instead of an import library.
8837
2ca22b03 8838
69da35b5
NC
8839For instance, when ld is called with the argument @samp{-lxxx} it will attempt
8840to find, in the first directory of its search path,
2ca22b03
NC
8841
8842@example
45e948fe
NC
8843libxxx.dll.a
8844xxx.dll.a
8845libxxx.a
8846xxx.lib
f6c4d4b1 8847libxxx.lib
69da35b5 8848cygxxx.dll (*)
45e948fe
NC
8849libxxx.dll
8850xxx.dll
2ca22b03
NC
8851@end example
8852
69da35b5
NC
8853before moving on to the next directory in the search path.
8854
c0065db7
RM
8855(*) Actually, this is not @samp{cygxxx.dll} but in fact is @samp{<prefix>xxx.dll},
8856where @samp{<prefix>} is set by the @command{ld} option
8857@samp{--dll-search-prefix=<prefix>}. In the case of cygwin, the standard gcc spec
8858file includes @samp{--dll-search-prefix=cyg}, so in effect we actually search for
69da35b5
NC
8859@samp{cygxxx.dll}.
8860
c0065db7
RM
8861Other win32-based unix environments, such as mingw or pw32, may use other
8862@samp{<prefix>}es, although at present only cygwin makes use of this feature. It
69da35b5
NC
8863was originally intended to help avoid name conflicts among dll's built for the
8864various win32/un*x environments, so that (for example) two versions of a zlib dll
8865could coexist on the same machine.
8866
2ca22b03
NC
8867The generic cygwin/mingw path layout uses a @samp{bin} directory for
8868applications and dll's and a @samp{lib} directory for the import
69da35b5 8869libraries (using cygwin nomenclature):
2ca22b03
NC
8870
8871@example
8872bin/
8873 cygxxx.dll
8874lib/
8875 libxxx.dll.a (in case of dll's)
c0065db7 8876 libxxx.a (in case of static archive)
2ca22b03
NC
8877@end example
8878
c0065db7
RM
8879Linking directly to a dll without using the import library can be
8880done two ways:
2ca22b03
NC
8881
88821. Use the dll directly by adding the @samp{bin} path to the link line
8883@example
8884gcc -Wl,-verbose -o a.exe -L../bin/ -lxxx
c0065db7 8885@end example
2ca22b03 8886
69da35b5
NC
8887However, as the dll's often have version numbers appended to their names
8888(@samp{cygncurses-5.dll}) this will often fail, unless one specifies
8889@samp{-L../bin -lncurses-5} to include the version. Import libs are generally
8890not versioned, and do not have this difficulty.
8891
2ca22b03
NC
88922. Create a symbolic link from the dll to a file in the @samp{lib}
8893directory according to the above mentioned search pattern. This
8894should be used to avoid unwanted changes in the tools needed for
8895making the app/dll.
8896
8897@example
8898ln -s bin/cygxxx.dll lib/[cyg|lib|]xxx.dll[.a]
c0065db7 8899@end example
2ca22b03
NC
8900
8901Then you can link without any make environment changes.
8902
8903@example
8904gcc -Wl,-verbose -o a.exe -L../lib/ -lxxx
c0065db7 8905@end example
69da35b5
NC
8906
8907This technique also avoids the version number problems, because the following is
8908perfectly legal
8909
8910@example
8911bin/
8912 cygxxx-5.dll
8913lib/
c0065db7 8914 libxxx.dll.a -> ../bin/cygxxx-5.dll
69da35b5
NC
8915@end example
8916
dc8465bf 8917Linking directly to a dll without using an import lib will work
69da35b5
NC
8918even when auto-import features are exercised, and even when
8919@samp{--enable-runtime-pseudo-relocs} is used.
8920
8921Given the improvements in speed and memory usage, one might justifiably
45e948fe 8922wonder why import libraries are used at all. There are three reasons:
69da35b5
NC
8923
89241. Until recently, the link-directly-to-dll functionality did @emph{not}
8925work with auto-imported data.
8926
dc8465bf
NC
89272. Sometimes it is necessary to include pure static objects within the
8928import library (which otherwise contains only bfd's for indirection
8929symbols that point to the exports of a dll). Again, the import lib
8930for the cygwin kernel makes use of this ability, and it is not
8931possible to do this without an import lib.
69da35b5 8932
45e948fe
NC
89333. Symbol aliases can only be resolved using an import lib. This is
8934critical when linking against OS-supplied dll's (eg, the win32 API)
8935in which symbols are usually exported as undecorated aliases of their
8936stdcall-decorated assembly names.
8937
69da35b5 8938So, import libs are not going away. But the ability to replace
c0065db7
RM
8939true import libs with a simple symbolic link to (or a copy of)
8940a dll, in many cases, is a useful addition to the suite of tools
8941binutils makes available to the win32 developer. Given the
69da35b5
NC
8942massive improvements in memory requirements during linking, storage
8943requirements, and linking speed, we expect that many developers
8944will soon begin to use this feature whenever possible.
dc8465bf 8945
c0065db7 8946@item symbol aliasing
dc8465bf 8947@table @emph
c0065db7
RM
8948@item adding additional names
8949Sometimes, it is useful to export symbols with additional names.
dc8465bf
NC
8950A symbol @samp{foo} will be exported as @samp{foo}, but it can also be
8951exported as @samp{_foo} by using special directives in the DEF file
8952when creating the dll. This will affect also the optional created
c0065db7 8953import library. Consider the following DEF file:
dc8465bf 8954
c0065db7 8955@example
dc8465bf
NC
8956LIBRARY "xyz.dll" BASE=0x61000000
8957
8958EXPORTS
c0065db7 8959foo
dc8465bf 8960_foo = foo
c0065db7 8961@end example
dc8465bf
NC
8962
8963The line @samp{_foo = foo} maps the symbol @samp{foo} to @samp{_foo}.
8964
8965Another method for creating a symbol alias is to create it in the
8966source code using the "weak" attribute:
8967
c0065db7
RM
8968@example
8969void foo () @{ /* Do something. */; @}
dc8465bf 8970void _foo () __attribute__ ((weak, alias ("foo")));
c0065db7 8971@end example
dc8465bf
NC
8972
8973See the gcc manual for more information about attributes and weak
8974symbols.
8975
8976@item renaming symbols
8977Sometimes it is useful to rename exports. For instance, the cygwin
c0065db7 8978kernel does this regularly. A symbol @samp{_foo} can be exported as
dc8465bf
NC
8979@samp{foo} but not as @samp{_foo} by using special directives in the
8980DEF file. (This will also affect the import library, if it is
c0065db7 8981created). In the following example:
dc8465bf 8982
c0065db7 8983@example
dc8465bf
NC
8984LIBRARY "xyz.dll" BASE=0x61000000
8985
8986EXPORTS
8987_foo = foo
c0065db7 8988@end example
dc8465bf
NC
8989
8990The line @samp{_foo = foo} maps the exported symbol @samp{foo} to
8991@samp{_foo}.
c0065db7 8992@end table
dc8465bf 8993
0a5d968e 8994Note: using a DEF file disables the default auto-export behavior,
a05a5b64 8995unless the @samp{--export-all-symbols} command-line option is used.
0a5d968e 8996If, however, you are trying to rename symbols, then you should list
c0065db7
RM
8997@emph{all} desired exports in the DEF file, including the symbols
8998that are not being renamed, and do @emph{not} use the
8999@samp{--export-all-symbols} option. If you list only the
9000renamed symbols in the DEF file, and use @samp{--export-all-symbols}
9001to handle the other symbols, then the both the new names @emph{and}
9002the original names for the renamed symbols will be exported.
9003In effect, you'd be aliasing those symbols, not renaming them,
0a5d968e 9004which is probably not what you wanted.
c87db184
CF
9005
9006@cindex weak externals
9007@item weak externals
9008The Windows object format, PE, specifies a form of weak symbols called
9009weak externals. When a weak symbol is linked and the symbol is not
9010defined, the weak symbol becomes an alias for some other symbol. There
9011are three variants of weak externals:
9012@itemize
9013@item Definition is searched for in objects and libraries, historically
9014called lazy externals.
9015@item Definition is searched for only in other objects, not in libraries.
9016This form is not presently implemented.
9017@item No search; the symbol is an alias. This form is not presently
9018implemented.
9019@end itemize
9020As a GNU extension, weak symbols that do not specify an alternate symbol
9021are supported. If the symbol is undefined when linking, the symbol
9022uses a default value.
c1711530
DK
9023
9024@cindex aligned common symbols
9025@item aligned common symbols
9026As a GNU extension to the PE file format, it is possible to specify the
9027desired alignment for a common symbol. This information is conveyed from
9028the assembler or compiler to the linker by means of GNU-specific commands
9029carried in the object file's @samp{.drectve} section, which are recognized
9030by @command{ld} and respected when laying out the common symbols. Native
9031tools will be able to process object files employing this GNU extension,
9032but will fail to respect the alignment instructions, and may issue noisy
9033warnings about unknown linker directives.
5063daf7 9034
2ca22b03
NC
9035@end table
9036
9037@ifclear GENERIC
9038@lowersections
9039@end ifclear
9040@end ifset
9041
e0001a05
NC
9042@ifset XTENSA
9043@ifclear GENERIC
9044@raisesections
9045@end ifclear
9046
9047@node Xtensa
9048@section @code{ld} and Xtensa Processors
9049
9050@cindex Xtensa processors
9051The default @command{ld} behavior for Xtensa processors is to interpret
9052@code{SECTIONS} commands so that lists of explicitly named sections in a
9053specification with a wildcard file will be interleaved when necessary to
9054keep literal pools within the range of PC-relative load offsets. For
9055example, with the command:
9056
9057@smallexample
9058SECTIONS
9059@{
9060 .text : @{
9061 *(.literal .text)
9062 @}
9063@}
9064@end smallexample
9065
9066@noindent
9067@command{ld} may interleave some of the @code{.literal}
9068and @code{.text} sections from different object files to ensure that the
9069literal pools are within the range of PC-relative load offsets. A valid
9070interleaving might place the @code{.literal} sections from an initial
9071group of files followed by the @code{.text} sections of that group of
9072files. Then, the @code{.literal} sections from the rest of the files
9073and the @code{.text} sections from the rest of the files would follow.
e0001a05 9074
43cd72b9 9075@cindex @option{--relax} on Xtensa
e0001a05 9076@cindex relaxing on Xtensa
43cd72b9
BW
9077Relaxation is enabled by default for the Xtensa version of @command{ld} and
9078provides two important link-time optimizations. The first optimization
9079is to combine identical literal values to reduce code size. A redundant
9080literal will be removed and all the @code{L32R} instructions that use it
9081will be changed to reference an identical literal, as long as the
9082location of the replacement literal is within the offset range of all
9083the @code{L32R} instructions. The second optimization is to remove
9084unnecessary overhead from assembler-generated ``longcall'' sequences of
9085@code{L32R}/@code{CALLX@var{n}} when the target functions are within
9086range of direct @code{CALL@var{n}} instructions.
9087
9088For each of these cases where an indirect call sequence can be optimized
9089to a direct call, the linker will change the @code{CALLX@var{n}}
9090instruction to a @code{CALL@var{n}} instruction, remove the @code{L32R}
9091instruction, and remove the literal referenced by the @code{L32R}
9092instruction if it is not used for anything else. Removing the
9093@code{L32R} instruction always reduces code size but can potentially
9094hurt performance by changing the alignment of subsequent branch targets.
9095By default, the linker will always preserve alignments, either by
9096switching some instructions between 24-bit encodings and the equivalent
9097density instructions or by inserting a no-op in place of the @code{L32R}
9098instruction that was removed. If code size is more important than
9099performance, the @option{--size-opt} option can be used to prevent the
9100linker from widening density instructions or inserting no-ops, except in
9101a few cases where no-ops are required for correctness.
9102
9103The following Xtensa-specific command-line options can be used to
9104control the linker:
9105
9106@cindex Xtensa options
9107@table @option
43cd72b9
BW
9108@item --size-opt
9109When optimizing indirect calls to direct calls, optimize for code size
9110more than performance. With this option, the linker will not insert
9111no-ops or widen density instructions to preserve branch target
9112alignment. There may still be some cases where no-ops are required to
9113preserve the correctness of the code.
7a77f1ac
MF
9114
9115@item --abi-windowed
9116@itemx --abi-call0
9117Choose ABI for the output object and for the generated PLT code.
9118PLT code inserted by the linker must match ABI of the output object
9119because windowed and call0 ABI use incompatible function call
9120conventions.
9121Default ABI is chosen by the ABI tag in the @code{.xtensa.info} section
9122of the first input object.
9123A warning is issued if ABI tags of input objects do not match each other
9124or the chosen output object ABI.
43cd72b9 9125@end table
e0001a05
NC
9126
9127@ifclear GENERIC
9128@lowersections
9129@end ifclear
9130@end ifset
9131
252b5132
RH
9132@ifclear SingleFormat
9133@node BFD
9134@chapter BFD
9135
9136@cindex back end
9137@cindex object file management
9138@cindex object formats available
9139@kindex objdump -i
9140The linker accesses object and archive files using the BFD libraries.
9141These libraries allow the linker to use the same routines to operate on
9142object files whatever the object file format. A different object file
9143format can be supported simply by creating a new BFD back end and adding
9144it to the library. To conserve runtime memory, however, the linker and
9145associated tools are usually configured to support only a subset of the
9146object file formats available. You can use @code{objdump -i}
9147(@pxref{objdump,,objdump,binutils.info,The GNU Binary Utilities}) to
9148list all the formats available for your configuration.
9149
9150@cindex BFD requirements
9151@cindex requirements for BFD
9152As with most implementations, BFD is a compromise between
9153several conflicting requirements. The major factor influencing
9154BFD design was efficiency: any time used converting between
9155formats is time which would not have been spent had BFD not
9156been involved. This is partly offset by abstraction payback; since
9157BFD simplifies applications and back ends, more time and care
9158may be spent optimizing algorithms for a greater speed.
9159
9160One minor artifact of the BFD solution which you should bear in
9161mind is the potential for information loss. There are two places where
9162useful information can be lost using the BFD mechanism: during
9163conversion and during output. @xref{BFD information loss}.
9164
9165@menu
9166* BFD outline:: How it works: an outline of BFD
9167@end menu
9168
9169@node BFD outline
36f63dca 9170@section How It Works: An Outline of BFD
252b5132
RH
9171@cindex opening object files
9172@include bfdsumm.texi
9173@end ifclear
9174
9175@node Reporting Bugs
9176@chapter Reporting Bugs
ff5dcc92
SC
9177@cindex bugs in @command{ld}
9178@cindex reporting bugs in @command{ld}
252b5132 9179
ff5dcc92 9180Your bug reports play an essential role in making @command{ld} reliable.
252b5132
RH
9181
9182Reporting a bug may help you by bringing a solution to your problem, or
9183it may not. But in any case the principal function of a bug report is
ff5dcc92 9184to help the entire community by making the next version of @command{ld}
252b5132 9185work better. Bug reports are your contribution to the maintenance of
ff5dcc92 9186@command{ld}.
252b5132
RH
9187
9188In order for a bug report to serve its purpose, you must include the
9189information that enables us to fix the bug.
9190
9191@menu
9192* Bug Criteria:: Have you found a bug?
9193* Bug Reporting:: How to report bugs
9194@end menu
9195
9196@node Bug Criteria
36f63dca 9197@section Have You Found a Bug?
252b5132
RH
9198@cindex bug criteria
9199
9200If you are not sure whether you have found a bug, here are some guidelines:
9201
9202@itemize @bullet
9203@cindex fatal signal
9204@cindex linker crash
9205@cindex crash of linker
9206@item
9207If the linker gets a fatal signal, for any input whatever, that is a
ff5dcc92 9208@command{ld} bug. Reliable linkers never crash.
252b5132
RH
9209
9210@cindex error on valid input
9211@item
ff5dcc92 9212If @command{ld} produces an error message for valid input, that is a bug.
252b5132
RH
9213
9214@cindex invalid input
9215@item
ff5dcc92 9216If @command{ld} does not produce an error message for invalid input, that
252b5132
RH
9217may be a bug. In the general case, the linker can not verify that
9218object files are correct.
9219
9220@item
9221If you are an experienced user of linkers, your suggestions for
ff5dcc92 9222improvement of @command{ld} are welcome in any case.
252b5132
RH
9223@end itemize
9224
9225@node Bug Reporting
36f63dca 9226@section How to Report Bugs
252b5132 9227@cindex bug reports
ff5dcc92 9228@cindex @command{ld} bugs, reporting
252b5132
RH
9229
9230A number of companies and individuals offer support for @sc{gnu}
ff5dcc92 9231products. If you obtained @command{ld} from a support organization, we
252b5132
RH
9232recommend you contact that organization first.
9233
9234You can find contact information for many support companies and
9235individuals in the file @file{etc/SERVICE} in the @sc{gnu} Emacs
9236distribution.
9237
ad22bfe8 9238@ifset BUGURL
ff5dcc92 9239Otherwise, send bug reports for @command{ld} to
ad22bfe8
JM
9240@value{BUGURL}.
9241@end ifset
252b5132
RH
9242
9243The fundamental principle of reporting bugs usefully is this:
9244@strong{report all the facts}. If you are not sure whether to state a
9245fact or leave it out, state it!
9246
9247Often people omit facts because they think they know what causes the
9248problem and assume that some details do not matter. Thus, you might
b553b183
NC
9249assume that the name of a symbol you use in an example does not
9250matter. Well, probably it does not, but one cannot be sure. Perhaps
9251the bug is a stray memory reference which happens to fetch from the
9252location where that name is stored in memory; perhaps, if the name
9253were different, the contents of that location would fool the linker
9254into doing the right thing despite the bug. Play it safe and give a
9255specific, complete example. That is the easiest thing for you to do,
c0065db7 9256and the most helpful.
b553b183
NC
9257
9258Keep in mind that the purpose of a bug report is to enable us to fix
9259the bug if it is new to us. Therefore, always write your bug reports
9260on the assumption that the bug has not been reported previously.
252b5132
RH
9261
9262Sometimes people give a few sketchy facts and ask, ``Does this ring a
36f63dca
NC
9263bell?'' This cannot help us fix a bug, so it is basically useless. We
9264respond by asking for enough details to enable us to investigate.
9265You might as well expedite matters by sending them to begin with.
252b5132
RH
9266
9267To enable us to fix the bug, you should include all these things:
9268
9269@itemize @bullet
9270@item
ff5dcc92 9271The version of @command{ld}. @command{ld} announces it if you start it with
252b5132
RH
9272the @samp{--version} argument.
9273
9274Without this, we will not know whether there is any point in looking for
ff5dcc92 9275the bug in the current version of @command{ld}.
252b5132
RH
9276
9277@item
ff5dcc92 9278Any patches you may have applied to the @command{ld} source, including any
252b5132
RH
9279patches made to the @code{BFD} library.
9280
9281@item
9282The type of machine you are using, and the operating system name and
9283version number.
9284
9285@item
ff5dcc92 9286What compiler (and its version) was used to compile @command{ld}---e.g.
252b5132
RH
9287``@code{gcc-2.7}''.
9288
9289@item
9290The command arguments you gave the linker to link your example and
9291observe the bug. To guarantee you will not omit something important,
9292list them all. A copy of the Makefile (or the output from make) is
9293sufficient.
9294
9295If we were to try to guess the arguments, we would probably guess wrong
9296and then we might not encounter the bug.
9297
9298@item
9299A complete input file, or set of input files, that will reproduce the
b553b183
NC
9300bug. It is generally most helpful to send the actual object files
9301provided that they are reasonably small. Say no more than 10K. For
9302bigger files you can either make them available by FTP or HTTP or else
9303state that you are willing to send the object file(s) to whomever
9304requests them. (Note - your email will be going to a mailing list, so
9305we do not want to clog it up with large attachments). But small
9306attachments are best.
252b5132
RH
9307
9308If the source files were assembled using @code{gas} or compiled using
9309@code{gcc}, then it may be OK to send the source files rather than the
9310object files. In this case, be sure to say exactly what version of
9311@code{gas} or @code{gcc} was used to produce the object files. Also say
9312how @code{gas} or @code{gcc} were configured.
9313
9314@item
9315A description of what behavior you observe that you believe is
9316incorrect. For example, ``It gets a fatal signal.''
9317
ff5dcc92 9318Of course, if the bug is that @command{ld} gets a fatal signal, then we
252b5132
RH
9319will certainly notice it. But if the bug is incorrect output, we might
9320not notice unless it is glaringly wrong. You might as well not give us
9321a chance to make a mistake.
9322
9323Even if the problem you experience is a fatal signal, you should still
9324say so explicitly. Suppose something strange is going on, such as, your
b45619c0 9325copy of @command{ld} is out of sync, or you have encountered a bug in the
252b5132
RH
9326C library on your system. (This has happened!) Your copy might crash
9327and ours would not. If you told us to expect a crash, then when ours
9328fails to crash, we would know that the bug was not happening for us. If
9329you had not told us to expect a crash, then we would not be able to draw
9330any conclusion from our observations.
9331
9332@item
ff5dcc92 9333If you wish to suggest changes to the @command{ld} source, send us context
252b5132
RH
9334diffs, as generated by @code{diff} with the @samp{-u}, @samp{-c}, or
9335@samp{-p} option. Always send diffs from the old file to the new file.
ff5dcc92 9336If you even discuss something in the @command{ld} source, refer to it by
252b5132
RH
9337context, not by line number.
9338
9339The line numbers in our development sources will not match those in your
9340sources. Your line numbers would convey no useful information to us.
9341@end itemize
9342
9343Here are some things that are not necessary:
9344
9345@itemize @bullet
9346@item
9347A description of the envelope of the bug.
9348
9349Often people who encounter a bug spend a lot of time investigating
9350which changes to the input file will make the bug go away and which
9351changes will not affect it.
9352
9353This is often time consuming and not very useful, because the way we
9354will find the bug is by running a single example under the debugger
9355with breakpoints, not by pure deduction from a series of examples.
9356We recommend that you save your time for something else.
9357
9358Of course, if you can find a simpler example to report @emph{instead}
9359of the original one, that is a convenience for us. Errors in the
9360output will be easier to spot, running under the debugger will take
9361less time, and so on.
9362
9363However, simplification is not vital; if you do not want to do this,
9364report the bug anyway and send us the entire test case you used.
9365
9366@item
9367A patch for the bug.
9368
9369A patch for the bug does help us if it is a good one. But do not omit
9370the necessary information, such as the test case, on the assumption that
9371a patch is all we need. We might see problems with your patch and decide
9372to fix the problem another way, or we might not understand it at all.
9373
ff5dcc92 9374Sometimes with a program as complicated as @command{ld} it is very hard to
252b5132
RH
9375construct an example that will make the program follow a certain path
9376through the code. If you do not send us the example, we will not be
9377able to construct one, so we will not be able to verify that the bug is
9378fixed.
9379
9380And if we cannot understand what bug you are trying to fix, or why your
9381patch should be an improvement, we will not install it. A test case will
9382help us to understand.
9383
9384@item
9385A guess about what the bug is or what it depends on.
9386
9387Such guesses are usually wrong. Even we cannot guess right about such
9388things without first using the debugger to find the facts.
9389@end itemize
9390
9391@node MRI
9392@appendix MRI Compatible Script Files
9393@cindex MRI compatibility
ff5dcc92
SC
9394To aid users making the transition to @sc{gnu} @command{ld} from the MRI
9395linker, @command{ld} can use MRI compatible linker scripts as an
252b5132
RH
9396alternative to the more general-purpose linker scripting language
9397described in @ref{Scripts}. MRI compatible linker scripts have a much
9398simpler command set than the scripting language otherwise used with
ff5dcc92 9399@command{ld}. @sc{gnu} @command{ld} supports the most commonly used MRI
252b5132
RH
9400linker commands; these commands are described here.
9401
9402In general, MRI scripts aren't of much use with the @code{a.out} object
9403file format, since it only has three sections and MRI scripts lack some
9404features to make use of them.
9405
9406You can specify a file containing an MRI-compatible script using the
9407@samp{-c} command-line option.
9408
9409Each command in an MRI-compatible script occupies its own line; each
9410command line starts with the keyword that identifies the command (though
9411blank lines are also allowed for punctuation). If a line of an
ff5dcc92 9412MRI-compatible script begins with an unrecognized keyword, @command{ld}
252b5132
RH
9413issues a warning message, but continues processing the script.
9414
9415Lines beginning with @samp{*} are comments.
9416
9417You can write these commands using all upper-case letters, or all
9418lower case; for example, @samp{chip} is the same as @samp{CHIP}.
9419The following list shows only the upper-case form of each command.
9420
9421@table @code
9422@cindex @code{ABSOLUTE} (MRI)
9423@item ABSOLUTE @var{secname}
9424@itemx ABSOLUTE @var{secname}, @var{secname}, @dots{} @var{secname}
ff5dcc92 9425Normally, @command{ld} includes in the output file all sections from all
252b5132
RH
9426the input files. However, in an MRI-compatible script, you can use the
9427@code{ABSOLUTE} command to restrict the sections that will be present in
9428your output program. If the @code{ABSOLUTE} command is used at all in a
9429script, then only the sections named explicitly in @code{ABSOLUTE}
9430commands will appear in the linker output. You can still use other
9431input sections (whatever you select on the command line, or using
9432@code{LOAD}) to resolve addresses in the output file.
9433
9434@cindex @code{ALIAS} (MRI)
9435@item ALIAS @var{out-secname}, @var{in-secname}
9436Use this command to place the data from input section @var{in-secname}
9437in a section called @var{out-secname} in the linker output file.
9438
9439@var{in-secname} may be an integer.
9440
9441@cindex @code{ALIGN} (MRI)
9442@item ALIGN @var{secname} = @var{expression}
9443Align the section called @var{secname} to @var{expression}. The
9444@var{expression} should be a power of two.
9445
9446@cindex @code{BASE} (MRI)
9447@item BASE @var{expression}
9448Use the value of @var{expression} as the lowest address (other than
9449absolute addresses) in the output file.
9450
9451@cindex @code{CHIP} (MRI)
9452@item CHIP @var{expression}
9453@itemx CHIP @var{expression}, @var{expression}
9454This command does nothing; it is accepted only for compatibility.
9455
9456@cindex @code{END} (MRI)
9457@item END
9458This command does nothing whatever; it's only accepted for compatibility.
9459
9460@cindex @code{FORMAT} (MRI)
9461@item FORMAT @var{output-format}
9462Similar to the @code{OUTPUT_FORMAT} command in the more general linker
dc12032b 9463language, but restricted to S-records, if @var{output-format} is @samp{S}
252b5132
RH
9464
9465@cindex @code{LIST} (MRI)
9466@item LIST @var{anything}@dots{}
9467Print (to the standard output file) a link map, as produced by the
ff5dcc92 9468@command{ld} command-line option @samp{-M}.
252b5132
RH
9469
9470The keyword @code{LIST} may be followed by anything on the
9471same line, with no change in its effect.
9472
9473@cindex @code{LOAD} (MRI)
9474@item LOAD @var{filename}
9475@itemx LOAD @var{filename}, @var{filename}, @dots{} @var{filename}
9476Include one or more object file @var{filename} in the link; this has the
ff5dcc92 9477same effect as specifying @var{filename} directly on the @command{ld}
252b5132
RH
9478command line.
9479
9480@cindex @code{NAME} (MRI)
9481@item NAME @var{output-name}
ff5dcc92 9482@var{output-name} is the name for the program produced by @command{ld}; the
252b5132
RH
9483MRI-compatible command @code{NAME} is equivalent to the command-line
9484option @samp{-o} or the general script language command @code{OUTPUT}.
9485
9486@cindex @code{ORDER} (MRI)
9487@item ORDER @var{secname}, @var{secname}, @dots{} @var{secname}
9488@itemx ORDER @var{secname} @var{secname} @var{secname}
ff5dcc92 9489Normally, @command{ld} orders the sections in its output file in the
252b5132
RH
9490order in which they first appear in the input files. In an MRI-compatible
9491script, you can override this ordering with the @code{ORDER} command. The
9492sections you list with @code{ORDER} will appear first in your output
9493file, in the order specified.
9494
9495@cindex @code{PUBLIC} (MRI)
9496@item PUBLIC @var{name}=@var{expression}
9497@itemx PUBLIC @var{name},@var{expression}
9498@itemx PUBLIC @var{name} @var{expression}
9499Supply a value (@var{expression}) for external symbol
9500@var{name} used in the linker input files.
9501
9502@cindex @code{SECT} (MRI)
9503@item SECT @var{secname}, @var{expression}
9504@itemx SECT @var{secname}=@var{expression}
9505@itemx SECT @var{secname} @var{expression}
9506You can use any of these three forms of the @code{SECT} command to
9507specify the start address (@var{expression}) for section @var{secname}.
9508If you have more than one @code{SECT} statement for the same
9509@var{secname}, only the @emph{first} sets the start address.
9510@end table
9511
793c5807
NC
9512@node GNU Free Documentation License
9513@appendix GNU Free Documentation License
36f63dca 9514@include fdl.texi
704c465c 9515
370b66a1
CD
9516@node LD Index
9517@unnumbered LD Index
252b5132
RH
9518
9519@printindex cp
9520
9521@tex
7ca01ed9 9522% I think something like @@colophon should be in texinfo. In the
252b5132
RH
9523% meantime:
9524\long\def\colophon{\hbox to0pt{}\vfill
9525\centerline{The body of this manual is set in}
9526\centerline{\fontname\tenrm,}
9527\centerline{with headings in {\bf\fontname\tenbf}}
9528\centerline{and examples in {\tt\fontname\tentt}.}
9529\centerline{{\it\fontname\tenit\/} and}
9530\centerline{{\sl\fontname\tensl\/}}
9531\centerline{are used for emphasis.}\vfill}
9532\page\colophon
7ca01ed9 9533% Blame: doc@@cygnus.com, 28mar91.
252b5132
RH
9534@end tex
9535
252b5132 9536@bye