1\input texinfo
2@setfilename ld.info
3@afourpaper
4@comment $MirOS: src/gnu/usr.bin/binutils/ld/ld.texinfo,v 1.2 2009/10/04 03:06:13 tg Exp $
5@c Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
6@c 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
7@syncodeindex ky cp
8@include configdoc.texi
9@c (configdoc.texi is generated by the Makefile)
10@include ldver.texi
11
12@c @smallbook
13
14@macro gcctabopt{body}
15@code{\body\}
16@end macro
17
18@c man begin NAME
19@ifset man
20@c Configure for the generation of man pages
21@set UsesEnvVars
22@set GENERIC
23@set A29K
24@set ARC
25@set ARM
26@set D10V
27@set D30V
28@set H8/300
29@set H8/500
30@set HPPA
31@set I370
32@set I80386
33@set I860
34@set I960
35@set M32R
36@set M68HC11
37@set M680X0
38@set MCORE
39@set MIPS
40@set MMIX
41@set MSP430
42@set PDP11
43@set PJ
44@set SH
45@set SPARC
46@set TIC54X
47@set V850
48@set VAX
49@set WIN32
50@set XTENSA
51@end ifset
52@c man end
53
54@ifinfo
55@format
56START-INFO-DIR-ENTRY
57* Ld: (ld).                       The GNU linker.
58END-INFO-DIR-ENTRY
59@end format
60@end ifinfo
61
62@ifinfo
63This file documents the @sc{gnu} linker LD version @value{VERSION}.
64
65Copyright (C) 1991, 92, 93, 94, 95, 96, 97, 98, 99, 2000,
662001, 2002, 2003, 2004 Free Software Foundation, Inc.
67
68@ignore
69
70Permission is granted to copy, distribute and/or modify this document
71under the terms of the GNU Free Documentation License, Version 1.1
72or any later version published by the Free Software Foundation;
73with no Invariant Sections, with no Front-Cover Texts, and with no
74Back-Cover Texts.  A copy of the license is included in the
75section entitled ``GNU Free Documentation License''.
76
77Permission is granted to process this file through Tex and print the
78results, provided the printed document carries copying permission
79notice identical to this one except for the removal of this paragraph
80(this paragraph not being relevant to the printed manual).
81
82@end ignore
83@end ifinfo
84@iftex
85@finalout
86@setchapternewpage odd
87@settitle Using LD, the GNU linker
88@titlepage
89@title Using ld
90@subtitle The GNU linker
91@sp 1
92@subtitle @code{ld} version 2
93@subtitle Version @value{VERSION}
94@author Steve Chamberlain
95@author Ian Lance Taylor
96@page
97
98@tex
99{\parskip=0pt
100\hfill Red Hat Inc\par
101\hfill nickc\@credhat.com, doc\@redhat.com\par
102\hfill {\it Using LD, the GNU linker}\par
103\hfill Edited by Jeffrey Osier (jeffrey\@cygnus.com)\par
104}
105\global\parindent=0pt % Steve likes it this way.
106@end tex
107
108@vskip 0pt plus 1filll
109@c man begin COPYRIGHT
110Copyright @copyright{} 1991, 92, 93, 94, 95, 96, 97, 98, 99, 2000, 2001,
1112002, 2003, 2004 Free Software Foundation, Inc.
112
113Permission is granted to copy, distribute and/or modify this document
114under the terms of the GNU Free Documentation License, Version 1.1
115or any later version published by the Free Software Foundation;
116with no Invariant Sections, with no Front-Cover Texts, and with no
117Back-Cover Texts.  A copy of the license is included in the
118section entitled ``GNU Free Documentation License''.
119@c man end
120
121@end titlepage
122@end iftex
123@c FIXME: Talk about importance of *order* of args, cmds to linker!
124
125@ifnottex
126@node Top
127@top Using ld
128This file documents the @sc{gnu} linker ld version @value{VERSION}.
129
130This document is distributed under the terms of the GNU Free
131Documentation License.  A copy of the license is included in the
132section entitled ``GNU Free Documentation License''.
133
134@menu
135* Overview::                    Overview
136* Invocation::                  Invocation
137* Scripts::                     Linker Scripts
138@ifset GENERIC
139* Machine Dependent::           Machine Dependent Features
140@end ifset
141@ifclear GENERIC
142@ifset H8300
143* H8/300::                      ld and the H8/300
144@end ifset
145@ifset Renesas
146* Renesas::                     ld and other Renesas micros
147@end ifset
148@ifset I960
149* i960::                        ld and the Intel 960 family
150@end ifset
151@ifset ARM
152* ARM::				ld and the ARM family
153@end ifset
154@ifset HPPA
155* HPPA ELF32::                  ld and HPPA 32-bit ELF
156@end ifset
157@ifset M68HC11
158* M68HC11/68HC12::              ld and the Motorola 68HC11 and 68HC12 families
159@end ifset
160@ifset TICOFF
161* TI COFF::                     ld and the TI COFF
162@end ifset
163@ifset WIN32
164* Win32::                       ld and WIN32 (cygwin/mingw)
165@end ifset
166@ifset XTENSA
167* Xtensa::                      ld and Xtensa Processors
168@end ifset
169@end ifclear
170@ifclear SingleFormat
171* BFD::                         BFD
172@end ifclear
173@c Following blank line required for remaining bug in makeinfo conds/menus
174
175* Reporting Bugs::              Reporting Bugs
176* MRI::                         MRI Compatible Script Files
177* GNU Free Documentation License::  GNU Free Documentation License
178* Index::                       Index
179@end menu
180@end ifnottex
181
182@node Overview
183@chapter Overview
184
185@cindex @sc{gnu} linker
186@cindex what is this?
187
188@ifset man
189@c man begin SYNOPSIS
190ld [@b{options}] @var{objfile} @dots{}
191@c man end
192
193@c man begin SEEALSO
194ar(1), nm(1), objcopy(1), objdump(1), readelf(1) and
195the Info entries for @file{binutils} and
196@file{ld}.
197@c man end
198@end ifset
199
200@c man begin DESCRIPTION
201
202@command{ld} combines a number of object and archive files, relocates
203their data and ties up symbol references. Usually the last step in
204compiling a program is to run @command{ld}.
205
206@command{ld} accepts Linker Command Language files written in
207a superset of AT&T's Link Editor Command Language syntax,
208to provide explicit and total control over the linking process.
209
210@ifset man
211@c For the man only
212This man page does not describe the command language; see the
213@command{ld} entry in @code{info}, or the manual
214ld: the GNU linker, for full details on the command language and
215on other aspects of the GNU linker.
216@end ifset
217
218@ifclear SingleFormat
219This version of @command{ld} uses the general purpose BFD libraries
220to operate on object files. This allows @command{ld} to read, combine, and
221write object files in many different formats---for example, COFF or
222@code{a.out}.  Different formats may be linked together to produce any
223available kind of object file.  @xref{BFD}, for more information.
224@end ifclear
225
226Aside from its flexibility, the @sc{gnu} linker is more helpful than other
227linkers in providing diagnostic information.  Many linkers abandon
228execution immediately upon encountering an error; whenever possible,
229@command{ld} continues executing, allowing you to identify other errors
230(or, in some cases, to get an output file in spite of the error).
231
232@c man end
233
234@node Invocation
235@chapter Invocation
236
237@c man begin DESCRIPTION
238
239The @sc{gnu} linker @command{ld} is meant to cover a broad range of situations,
240and to be as compatible as possible with other linkers.  As a result,
241you have many choices to control its behavior.
242
243@c man end
244
245@ifset UsesEnvVars
246@menu
247* Options::                     Command Line Options
248* Environment::                 Environment Variables
249@end menu
250
251@node Options
252@section Command Line Options
253@end ifset
254
255@cindex command line
256@cindex options
257
258@c man begin OPTIONS
259
260The linker supports a plethora of command-line options, but in actual
261practice few of them are used in any particular context.
262@cindex standard Unix system
263For instance, a frequent use of @command{ld} is to link standard Unix
264object files on a standard, supported Unix system.  On such a system, to
265link a file @code{hello.o}:
266
267@smallexample
268ld -o @var{output} /lib/crt0.o hello.o -lc
269@end smallexample
270
271This tells @command{ld} to produce a file called @var{output} as the
272result of linking the file @code{/lib/crt0.o} with @code{hello.o} and
273the library @code{libc.a}, which will come from the standard search
274directories.  (See the discussion of the @samp{-l} option below.)
275
276Some of the command-line options to @command{ld} may be specified at any
277point in the command line.  However, options which refer to files, such
278as @samp{-l} or @samp{-T}, cause the file to be read at the point at
279which the option appears in the command line, relative to the object
280files and other file options.  Repeating non-file options with a
281different argument will either have no further effect, or override prior
282occurrences (those further to the left on the command line) of that
283option.  Options which may be meaningfully specified more than once are
284noted in the descriptions below.
285
286@cindex object files
287Non-option arguments are object files or archives which are to be linked
288together.  They may follow, precede, or be mixed in with command-line
289options, except that an object file argument may not be placed between
290an option and its argument.
291
292Usually the linker is invoked with at least one object file, but you can
293specify other forms of binary input files using @samp{-l}, @samp{-R},
294and the script command language.  If @emph{no} binary input files at all
295are specified, the linker does not produce any output, and issues the
296message @samp{No input files}.
297
298If the linker cannot recognize the format of an object file, it will
299assume that it is a linker script.  A script specified in this way
300augments the main linker script used for the link (either the default
301linker script or the one specified by using @samp{-T}).  This feature
302permits the linker to link against a file which appears to be an object
303or an archive, but actually merely defines some symbol values, or uses
304@code{INPUT} or @code{GROUP} to load other objects.  Note that
305specifying a script in this way merely augments the main linker script;
306use the @samp{-T} option to replace the default linker script entirely.
307@xref{Scripts}.
308
309For options whose names are a single letter,
310option arguments must either follow the option letter without intervening
311whitespace, or be given as separate arguments immediately following the
312option that requires them.
313
314For options whose names are multiple letters, either one dash or two can
315precede the option name; for example, @samp{-trace-symbol} and
316@samp{--trace-symbol} are equivalent.  Note---there is one exception to
317this rule.  Multiple letter options that start with a lower case 'o' can
318only be preceeded by two dashes.  This is to reduce confusion with the
319@samp{-o} option.  So for example @samp{-omagic} sets the output file
320name to @samp{magic} whereas @samp{--omagic} sets the NMAGIC flag on the
321output.
322
323Arguments to multiple-letter options must either be separated from the
324option name by an equals sign, or be given as separate arguments
325immediately following the option that requires them.  For example,
326@samp{--trace-symbol foo} and @samp{--trace-symbol=foo} are equivalent.
327Unique abbreviations of the names of multiple-letter options are
328accepted.
329
330Note---if the linker is being invoked indirectly, via a compiler driver
331(e.g. @samp{gcc}) then all the linker command line options should be
332prefixed by @samp{-Wl,} (or whatever is appropriate for the particular
333compiler driver) like this:
334
335@smallexample
336  gcc -Wl,--startgroup foo.o bar.o -Wl,--endgroup
337@end smallexample
338
339This is important, because otherwise the compiler driver program may
340silently drop the linker options, resulting in a bad link.
341
342Here is a table of the generic command line switches accepted by the GNU
343linker:
344
345@table @gcctabopt
346@kindex -a@var{keyword}
347@item -a@var{keyword}
348This option is supported for HP/UX compatibility.  The @var{keyword}
349argument must be one of the strings @samp{archive}, @samp{shared}, or
350@samp{default}.  @samp{-aarchive} is functionally equivalent to
351@samp{-Bstatic}, and the other two keywords are functionally equivalent
352to @samp{-Bdynamic}.  This option may be used any number of times.
353
354@ifset I960
355@cindex architectures
356@kindex -A@var{arch}
357@item -A@var{architecture}
358@kindex --architecture=@var{arch}
359@itemx --architecture=@var{architecture}
360In the current release of @command{ld}, this option is useful only for the
361Intel 960 family of architectures.  In that @command{ld} configuration, the
362@var{architecture} argument identifies the particular architecture in
363the 960 family, enabling some safeguards and modifying the
364archive-library search path.  @xref{i960,,@command{ld} and the Intel 960
365family}, for details.
366
367Future releases of @command{ld} may support similar functionality for
368other architecture families.
369@end ifset
370
371@ifclear SingleFormat
372@cindex binary input format
373@kindex -b @var{format}
374@kindex --format=@var{format}
375@cindex input format
376@cindex input format
377@item -b @var{input-format}
378@itemx --format=@var{input-format}
379@command{ld} may be configured to support more than one kind of object
380file.  If your @command{ld} is configured this way, you can use the
381@samp{-b} option to specify the binary format for input object files
382that follow this option on the command line.  Even when @command{ld} is
383configured to support alternative object formats, you don't usually need
384to specify this, as @command{ld} should be configured to expect as a
385default input format the most usual format on each machine.
386@var{input-format} is a text string, the name of a particular format
387supported by the BFD libraries.  (You can list the available binary
388formats with @samp{objdump -i}.)
389@xref{BFD}.
390
391You may want to use this option if you are linking files with an unusual
392binary format.  You can also use @samp{-b} to switch formats explicitly (when
393linking object files of different formats), by including
394@samp{-b @var{input-format}} before each group of object files in a
395particular format.
396
397The default format is taken from the environment variable
398@code{GNUTARGET}.
399@ifset UsesEnvVars
400@xref{Environment}.
401@end ifset
402You can also define the input format from a script, using the command
403@code{TARGET};
404@ifclear man
405see @ref{Format Commands}.
406@end ifclear
407@end ifclear
408
409@kindex -c @var{MRI-cmdfile}
410@kindex --mri-script=@var{MRI-cmdfile}
411@cindex compatibility, MRI
412@item -c @var{MRI-commandfile}
413@itemx --mri-script=@var{MRI-commandfile}
414For compatibility with linkers produced by MRI, @command{ld} accepts script
415files written in an alternate, restricted command language, described in
416@ifclear man
417@ref{MRI,,MRI Compatible Script Files}.
418@end ifclear
419@ifset man
420the MRI Compatible Script Files section of GNU ld documentation.
421@end ifset
422Introduce MRI script files with
423the option @samp{-c}; use the @samp{-T} option to run linker
424scripts written in the general-purpose @command{ld} scripting language.
425If @var{MRI-cmdfile} does not exist, @command{ld} looks for it in the directories
426specified by any @samp{-L} options.
427
428@cindex common allocation
429@kindex -d
430@kindex -dc
431@kindex -dp
432@item -d
433@itemx -dc
434@itemx -dp
435These three options are equivalent; multiple forms are supported for
436compatibility with other linkers.  They assign space to common symbols
437even if a relocatable output file is specified (with @samp{-r}).  The
438script command @code{FORCE_COMMON_ALLOCATION} has the same effect.
439@xref{Miscellaneous Commands}.
440
441@cindex entry point, from command line
442@kindex -e @var{entry}
443@kindex --entry=@var{entry}
444@item -e @var{entry}
445@itemx --entry=@var{entry}
446Use @var{entry} as the explicit symbol for beginning execution of your
447program, rather than the default entry point.  If there is no symbol
448named @var{entry}, the linker will try to parse @var{entry} as a number,
449and use that as the entry address (the number will be interpreted in
450base 10; you may use a leading @samp{0x} for base 16, or a leading
451@samp{0} for base 8).  @xref{Entry Point}, for a discussion of defaults
452and other ways of specifying the entry point.
453
454@kindex --exclude-libs
455@item --exclude-libs @var{lib},@var{lib},...
456Specifies a list of archive libraries from which symbols should not be automatically
457exported. The library names may be delimited by commas or colons.  Specifying
458@code{--exclude-libs ALL} excludes symbols in all archive libraries from
459automatic export.  This option is available only for the i386 PE targeted
460port of the linker and for ELF targeted ports.  For i386 PE, symbols
461explicitly listed in a .def file are still exported, regardless of this
462option.  For ELF targeted ports, symbols affected by this option will
463be treated as hidden.
464
465@cindex dynamic symbol table
466@kindex -E
467@kindex --export-dynamic
468@item -E
469@itemx --export-dynamic
470When creating a dynamically linked executable, add all symbols to the
471dynamic symbol table.  The dynamic symbol table is the set of symbols
472which are visible from dynamic objects at run time.
473
474If you do not use this option, the dynamic symbol table will normally
475contain only those symbols which are referenced by some dynamic object
476mentioned in the link.
477
478If you use @code{dlopen} to load a dynamic object which needs to refer
479back to the symbols defined by the program, rather than some other
480dynamic object, then you will probably need to use this option when
481linking the program itself.
482
483You can also use the version script to control what symbols should
484be added to the dynamic symbol table if the output format supports it.
485See the description of @samp{--version-script} in @ref{VERSION}.
486
487@ifclear SingleFormat
488@cindex big-endian objects
489@cindex endianness
490@kindex -EB
491@item -EB
492Link big-endian objects.  This affects the default output format.
493
494@cindex little-endian objects
495@kindex -EL
496@item -EL
497Link little-endian objects.  This affects the default output format.
498@end ifclear
499
500@kindex -f
501@kindex --auxiliary
502@item -f
503@itemx --auxiliary @var{name}
504When creating an ELF shared object, set the internal DT_AUXILIARY field
505to the specified name.  This tells the dynamic linker that the symbol
506table of the shared object should be used as an auxiliary filter on the
507symbol table of the shared object @var{name}.
508
509If you later link a program against this filter object, then, when you
510run the program, the dynamic linker will see the DT_AUXILIARY field.  If
511the dynamic linker resolves any symbols from the filter object, it will
512first check whether there is a definition in the shared object
513@var{name}.  If there is one, it will be used instead of the definition
514in the filter object.  The shared object @var{name} need not exist.
515Thus the shared object @var{name} may be used to provide an alternative
516implementation of certain functions, perhaps for debugging or for
517machine specific performance.
518
519This option may be specified more than once.  The DT_AUXILIARY entries
520will be created in the order in which they appear on the command line.
521
522@kindex -F
523@kindex --filter
524@item -F @var{name}
525@itemx --filter @var{name}
526When creating an ELF shared object, set the internal DT_FILTER field to
527the specified name.  This tells the dynamic linker that the symbol table
528of the shared object which is being created should be used as a filter
529on the symbol table of the shared object @var{name}.
530
531If you later link a program against this filter object, then, when you
532run the program, the dynamic linker will see the DT_FILTER field.  The
533dynamic linker will resolve symbols according to the symbol table of the
534filter object as usual, but it will actually link to the definitions
535found in the shared object @var{name}.  Thus the filter object can be
536used to select a subset of the symbols provided by the object
537@var{name}.
538
539Some older linkers used the @option{-F} option throughout a compilation
540toolchain for specifying object-file format for both input and output
541object files.
542@ifclear SingleFormat
543The @sc{gnu} linker uses other mechanisms for this purpose: the
544@option{-b}, @option{--format}, @option{--oformat} options, the
545@code{TARGET} command in linker scripts, and the @code{GNUTARGET}
546environment variable.
547@end ifclear
548The @sc{gnu} linker will ignore the @option{-F} option when not
549creating an ELF shared object.
550
551@cindex finalization function
552@kindex -fini
553@item -fini @var{name}
554When creating an ELF executable or shared object, call NAME when the
555executable or shared object is unloaded, by setting DT_FINI to the
556address of the function.  By default, the linker uses @code{_fini} as
557the function to call.
558
559@kindex -g
560@item -g
561Ignored.  Provided for compatibility with other tools.
562
563@kindex -G
564@kindex --gpsize
565@cindex object size
566@item -G@var{value}
567@itemx --gpsize=@var{value}
568Set the maximum size of objects to be optimized using the GP register to
569@var{size}.  This is only meaningful for object file formats such as
570MIPS ECOFF which supports putting large and small objects into different
571sections.  This is ignored for other object file formats.
572
573@cindex runtime library name
574@kindex -h@var{name}
575@kindex -soname=@var{name}
576@item -h@var{name}
577@itemx -soname=@var{name}
578When creating an ELF shared object, set the internal DT_SONAME field to
579the specified name.  When an executable is linked with a shared object
580which has a DT_SONAME field, then when the executable is run the dynamic
581linker will attempt to load the shared object specified by the DT_SONAME
582field rather than the using the file name given to the linker.
583
584@kindex -i
585@cindex incremental link
586@item -i
587Perform an incremental link (same as option @samp{-r}).
588
589@cindex initialization function
590@kindex -init
591@item -init @var{name}
592When creating an ELF executable or shared object, call NAME when the
593executable or shared object is loaded, by setting DT_INIT to the address
594of the function.  By default, the linker uses @code{_init} as the
595function to call.
596
597@cindex archive files, from cmd line
598@kindex -l@var{archive}
599@kindex --library=@var{archive}
600@item -l@var{archive}
601@itemx --library=@var{archive}
602Add archive file @var{archive} to the list of files to link.  This
603option may be used any number of times.  @command{ld} will search its
604path-list for occurrences of @code{lib@var{archive}.a} for every
605@var{archive} specified.
606
607On systems which support shared libraries, @command{ld} may also search for
608libraries with extensions other than @code{.a}.  Specifically, on ELF
609and SunOS systems, @command{ld} will search a directory for a library with
610an extension of @code{.so} before searching for one with an extension of
611@code{.a}.  By convention, a @code{.so} extension indicates a shared
612library.
613
614The linker will search an archive only once, at the location where it is
615specified on the command line.  If the archive defines a symbol which
616was undefined in some object which appeared before the archive on the
617command line, the linker will include the appropriate file(s) from the
618archive.  However, an undefined symbol in an object appearing later on
619the command line will not cause the linker to search the archive again.
620
621See the @option{-(} option for a way to force the linker to search
622archives multiple times.
623
624You may list the same archive multiple times on the command line.
625
626@ifset GENERIC
627This type of archive searching is standard for Unix linkers.  However,
628if you are using @command{ld} on AIX, note that it is different from the
629behaviour of the AIX linker.
630@end ifset
631
632@cindex search directory, from cmd line
633@kindex -L@var{dir}
634@kindex --library-path=@var{dir}
635@item -L@var{searchdir}
636@itemx --library-path=@var{searchdir}
637Add path @var{searchdir} to the list of paths that @command{ld} will search
638for archive libraries and @command{ld} control scripts.  You may use this
639option any number of times.  The directories are searched in the order
640in which they are specified on the command line.  Directories specified
641on the command line are searched before the default directories.  All
642@option{-L} options apply to all @option{-l} options, regardless of the
643order in which the options appear.
644
645If @var{searchdir} begins with @code{=}, then the @code{=} will be replaced
646by the @dfn{sysroot prefix}, a path specified when the linker is configured.
647
648@ifset UsesEnvVars
649The default set of paths searched (without being specified with
650@samp{-L}) depends on which emulation mode @command{ld} is using, and in
651some cases also on how it was configured.  @xref{Environment}.
652@end ifset
653
654The paths can also be specified in a link script with the
655@code{SEARCH_DIR} command.  Directories specified this way are searched
656at the point in which the linker script appears in the command line.
657
658@cindex emulation
659@kindex -m @var{emulation}
660@item -m@var{emulation}
661Emulate the @var{emulation} linker.  You can list the available
662emulations with the @samp{--verbose} or @samp{-V} options.
663
664If the @samp{-m} option is not used, the emulation is taken from the
665@code{LDEMULATION} environment variable, if that is defined.
666
667Otherwise, the default emulation depends upon how the linker was
668configured.
669
670@cindex link map
671@kindex -M
672@kindex --print-map
673@item -M
674@itemx --print-map
675Print a link map to the standard output.  A link map provides
676information about the link, including the following:
677
678@itemize @bullet
679@item
680Where object files are mapped into memory.
681@item
682How common symbols are allocated.
683@item
684All archive members included in the link, with a mention of the symbol
685which caused the archive member to be brought in.
686@item
687The values assigned to symbols.
688
689Note - symbols whose values are computed by an expression which
690involves a reference to a previous value of the same symbol may not
691have correct result displayed in the link map.  This is because the
692linker discards intermediate results and only retains the final value
693of an expression.  Under such circumstances the linker will display
694the final value enclosed by square brackets.  Thus for example a
695linker script containing:
696
697@smallexample
698   foo = 1
699   foo = foo * 4
700   foo = foo + 8
701@end smallexample
702
703will produce the following output in the link map if the @option{-M}
704option is used:
705
706@smallexample
707   0x00000001                foo = 0x1
708   [0x0000000c]                foo = (foo * 0x4)
709   [0x0000000c]                foo = (foo + 0x8)
710@end smallexample
711
712See @ref{Expressions} for more information about expressions in linker
713scripts.
714@end itemize
715
716@kindex -n
717@cindex read-only text
718@cindex NMAGIC
719@kindex --nmagic
720@item -n
721@itemx --nmagic
722Turn off page alignment of sections, and mark the output as
723@code{NMAGIC} if possible.
724
725@kindex -N
726@kindex --omagic
727@cindex read/write from cmd line
728@cindex OMAGIC
729@item -N
730@itemx --omagic
731Set the text and data sections to be readable and writable.  Also, do
732not page-align the data segment, and disable linking against shared
733libraries.  If the output format supports Unix style magic numbers,
734mark the output as @code{OMAGIC}. Note: Although a writable text section
735is allowed for PE-COFF targets, it does not conform to the format
736specification published by Microsoft.
737
738@kindex --no-omagic
739@cindex OMAGIC
740@item --no-omagic
741This option negates most of the effects of the @option{-N} option.  It
742sets the text section to be read-only, and forces the data segment to
743be page-aligned.  Note - this option does not enable linking against
744shared libraries.  Use @option{-Bdynamic} for this.
745
746@kindex -o @var{output}
747@kindex --output=@var{output}
748@cindex naming the output file
749@item -o @var{output}
750@itemx --output=@var{output}
751Use @var{output} as the name for the program produced by @command{ld}; if this
752option is not specified, the name @file{a.out} is used by default.  The
753script command @code{OUTPUT} can also specify the output file name.
754
755@kindex -O @var{level}
756@cindex generating optimized output
757@item -O @var{level}
758If @var{level} is a numeric values greater than zero @command{ld} optimizes
759the output.  This might take significantly longer and therefore probably
760should only be enabled for the final binary.
761
762@kindex -q
763@kindex --emit-relocs
764@cindex retain relocations in final executable
765@item -q
766@itemx --emit-relocs
767Leave relocation sections and contents in fully linked exececutables.
768Post link analysis and optimization tools may need this information in
769order to perform correct modifications of executables.  This results
770in larger executables.
771
772This option is currently only supported on ELF platforms.
773
774@cindex partial link
775@cindex relocatable output
776@kindex -r
777@kindex --relocatable
778@item -r
779@itemx --relocatable
780Generate relocatable output---i.e., generate an output file that can in
781turn serve as input to @command{ld}.  This is often called @dfn{partial
782linking}.  As a side effect, in environments that support standard Unix
783magic numbers, this option also sets the output file's magic number to
784@code{OMAGIC}.
785@c ; see @option{-N}.
786If this option is not specified, an absolute file is produced.  When
787linking C++ programs, this option @emph{will not} resolve references to
788constructors; to do that, use @samp{-Ur}.
789
790When an input file does not have the same format as the output file,
791partial linking is only supported if that input file does not contain any
792relocations.  Different output formats can have further restrictions; for
793example some @code{a.out}-based formats do not support partial linking
794with input files in other formats at all.
795
796This option does the same thing as @samp{-i}.
797
798@kindex -R @var{file}
799@kindex --just-symbols=@var{file}
800@cindex symbol-only input
801@item -R @var{filename}
802@itemx --just-symbols=@var{filename}
803Read symbol names and their addresses from @var{filename}, but do not
804relocate it or include it in the output.  This allows your output file
805to refer symbolically to absolute locations of memory defined in other
806programs.  You may use this option more than once.
807
808For compatibility with other ELF linkers, if the @option{-R} option is
809followed by a directory name, rather than a file name, it is treated as
810the @option{-rpath} option.
811
812@kindex -s
813@kindex --strip-all
814@cindex strip all symbols
815@item -s
816@itemx --strip-all
817Omit all symbol information from the output file.
818
819@kindex -S
820@kindex --strip-debug
821@cindex strip debugger symbols
822@item -S
823@itemx --strip-debug
824Omit debugger symbol information (but not all symbols) from the output file.
825
826@kindex -t
827@kindex --trace
828@cindex input files, displaying
829@item -t
830@itemx --trace
831Print the names of the input files as @command{ld} processes them.
832
833@kindex -T @var{script}
834@kindex --script=@var{script}
835@cindex script files
836@item -T @var{scriptfile}
837@itemx --script=@var{scriptfile}
838Use @var{scriptfile} as the linker script.  This script replaces
839@command{ld}'s default linker script (rather than adding to it), so
840@var{commandfile} must specify everything necessary to describe the
841output file.  @xref{Scripts}.  If @var{scriptfile} does not exist in
842the current directory, @code{ld} looks for it in the directories
843specified by any preceding @samp{-L} options.  Multiple @samp{-T}
844options accumulate.
845
846@kindex -u @var{symbol}
847@kindex --undefined=@var{symbol}
848@cindex undefined symbol
849@item -u @var{symbol}
850@itemx --undefined=@var{symbol}
851Force @var{symbol} to be entered in the output file as an undefined
852symbol.  Doing this may, for example, trigger linking of additional
853modules from standard libraries.  @samp{-u} may be repeated with
854different option arguments to enter additional undefined symbols.  This
855option is equivalent to the @code{EXTERN} linker script command.
856
857@kindex -Ur
858@cindex constructors
859@item -Ur
860For anything other than C++ programs, this option is equivalent to
861@samp{-r}: it generates relocatable output---i.e., an output file that can in
862turn serve as input to @command{ld}.  When linking C++ programs, @samp{-Ur}
863@emph{does} resolve references to constructors, unlike @samp{-r}.
864It does not work to use @samp{-Ur} on files that were themselves linked
865with @samp{-Ur}; once the constructor table has been built, it cannot
866be added to.  Use @samp{-Ur} only for the last partial link, and
867@samp{-r} for the others.
868
869@kindex --unique[=@var{SECTION}]
870@item --unique[=@var{SECTION}]
871Creates a separate output section for every input section matching
872@var{SECTION}, or if the optional wildcard @var{SECTION} argument is
873missing, for every orphan input section.  An orphan section is one not
874specifically mentioned in a linker script.  You may use this option
875multiple times on the command line;  It prevents the normal merging of
876input sections with the same name, overriding output section assignments
877in a linker script.
878
879@kindex -v
880@kindex -V
881@kindex --version
882@cindex version
883@item -v
884@itemx --version
885@itemx -V
886Display the version number for @command{ld}.  The @option{-V} option also
887lists the supported emulations.
888
889@kindex -x
890@kindex --discard-all
891@cindex deleting local symbols
892@item -x
893@itemx --discard-all
894Delete all local symbols.
895
896@kindex -X
897@kindex --discard-locals
898@cindex local symbols, deleting
899@cindex L, deleting symbols beginning
900@item -X
901@itemx --discard-locals
902Delete all temporary local symbols.  For most targets, this is all local
903symbols whose names begin with @samp{L}.
904
905@kindex -y @var{symbol}
906@kindex --trace-symbol=@var{symbol}
907@cindex symbol tracing
908@item -y @var{symbol}
909@itemx --trace-symbol=@var{symbol}
910Print the name of each linked file in which @var{symbol} appears.  This
911option may be given any number of times.  On many systems it is necessary
912to prepend an underscore.
913
914This option is useful when you have an undefined symbol in your link but
915don't know where the reference is coming from.
916
917@kindex -Y @var{path}
918@item -Y @var{path}
919Add @var{path} to the default library search path.  This option exists
920for Solaris compatibility.
921
922@kindex -z @var{keyword}
923@item -z @var{keyword}
924The recognized keywords are:
925@table @samp
926
927@item combreloc
928Combines multiple reloc sections and sorts them to make dynamic symbol
929lookup caching possible.
930
931@item defs
932Disallows undefined symbols in object files.  Undefined symbols in
933shared libraries are still allowed.
934
935@item initfirst
936This option is only meaningful when building a shared object.
937It marks the object so that its runtime initialization will occur
938before the runtime initialization of any other objects brought into
939the process at the same time.  Similarly the runtime finalization of
940the object will occur after the runtime finalization of any other
941objects.
942
943@item interpose
944Marks the object that its symbol table interposes before all symbols
945but the primary executable.
946
947@item loadfltr
948Marks  the object that its filters be processed immediately at
949runtime.
950
951@item muldefs
952Allows multiple definitions.
953
954@item nocombreloc
955Disables multiple reloc sections combining.
956
957@item nocopyreloc
958Disables production of copy relocs.
959
960@item nodefaultlib
961Marks the object that the search for dependencies of this object will
962ignore any default library search paths.
963
964@item nodelete
965Marks the object shouldn't be unloaded at runtime.
966
967@item nodlopen
968Marks the object not available to @code{dlopen}.
969
970@item nodump
971Marks the object can not be dumped by @code{dldump}.
972
973@item now
974When generating an executable or shared library, mark it to tell the
975dynamic linker to resolve all symbols when the program is started, or
976when the shared library is linked to using dlopen, instead of
977deferring function call resolution to the point when the function is
978first called.
979
980@item origin
981Marks the object may contain $ORIGIN.
982
983@end table
984
985Other keywords are ignored for Solaris compatibility.
986
987@kindex -(
988@cindex groups of archives
989@item -( @var{archives} -)
990@itemx --start-group @var{archives} --end-group
991The @var{archives} should be a list of archive files.  They may be
992either explicit file names, or @samp{-l} options.
993
994The specified archives are searched repeatedly until no new undefined
995references are created.  Normally, an archive is searched only once in
996the order that it is specified on the command line.  If a symbol in that
997archive is needed to resolve an undefined symbol referred to by an
998object in an archive that appears later on the command line, the linker
999would not be able to resolve that reference.  By grouping the archives,
1000they all be searched repeatedly until all possible references are
1001resolved.
1002
1003Using this option has a significant performance cost.  It is best to use
1004it only when there are unavoidable circular references between two or
1005more archives.
1006
1007@kindex --accept-unknown-input-arch
1008@kindex --no-accept-unknown-input-arch
1009@item --accept-unknown-input-arch
1010@itemx --no-accept-unknown-input-arch
1011Tells the linker to accept input files whose architecture cannot be
1012recognised.  The assumption is that the user knows what they are doing
1013and deliberately wants to link in these unknown input files.  This was
1014the default behaviour of the linker, before release 2.14.  The default
1015behaviour from release 2.14 onwards is to reject such input files, and
1016so the @samp{--accept-unknown-input-arch} option has been added to
1017restore the old behaviour.
1018
1019@kindex --as-needed
1020@kindex --no-as-needed
1021@item --as-needed
1022@itemx --no-as-needed
1023This option affects ELF DT_NEEDED tags for dynamic libraries mentioned
1024on the command line after the @option{--as-needed} option.  Normally,
1025the linker will add a DT_NEEDED tag for each dynamic library mentioned
1026on the command line, regardless of whether the library is actually
1027needed.  @option{--as-needed} causes DT_NEEDED tags to only be emitted
1028for libraries that satisfy some symbol reference from regular objects
1029which is undefined at the point that the library was linked.
1030@option{--no-as-needed} restores the default behaviour.
1031
1032@kindex --add-needed
1033@kindex --no-add-needed
1034@item --add-needed
1035@itemx --no-add-needed
1036This option affects the treatment of dynamic libraries from ELF
1037DT_NEEDED tags in dynamic libraries mentioned on the command line after
1038the @option{--no-add-needed} option.  Normally, the linker will add
1039a DT_NEEDED tag for each dynamic library from DT_NEEDED tags.
1040@option{--no-add-needed} causes DT_NEEDED tags will never be emitted
1041for those libraries from DT_NEEDED tags. @option{--add-needed} restores
1042the default behaviour.
1043
1044@kindex -assert @var{keyword}
1045@item -assert @var{keyword}
1046This option is ignored for SunOS compatibility.
1047
1048@kindex -Bdynamic
1049@kindex -dy
1050@kindex -call_shared
1051@item -Bdynamic
1052@itemx -dy
1053@itemx -call_shared
1054Link against dynamic libraries.  This is only meaningful on platforms
1055for which shared libraries are supported.  This option is normally the
1056default on such platforms.  The different variants of this option are
1057for compatibility with various systems.  You may use this option
1058multiple times on the command line: it affects library searching for
1059@option{-l} options which follow it.
1060
1061@kindex -Bgroup
1062@item -Bgroup
1063Set the @code{DF_1_GROUP} flag in the @code{DT_FLAGS_1} entry in the dynamic
1064section.  This causes the runtime linker to handle lookups in this
1065object and its dependencies to be performed only inside the group.
1066@option{--unresolved-symbols=report-all} is implied.  This option is
1067only meaningful on ELF platforms which support shared libraries.
1068
1069@kindex -Bstatic
1070@kindex -dn
1071@kindex -non_shared
1072@kindex -static
1073@item -Bstatic
1074@itemx -dn
1075@itemx -non_shared
1076@itemx -static
1077Do not link against shared libraries.  This is only meaningful on
1078platforms for which shared libraries are supported.  The different
1079variants of this option are for compatibility with various systems.  You
1080may use this option multiple times on the command line: it affects
1081library searching for @option{-l} options which follow it.  This
1082option also implies @option{--unresolved-symbols=report-all}.
1083
1084@kindex -Bsymbolic
1085@item -Bsymbolic
1086When creating a shared library, bind references to global symbols to the
1087definition within the shared library, if any.  Normally, it is possible
1088for a program linked against a shared library to override the definition
1089within the shared library.  This option is only meaningful on ELF
1090platforms which support shared libraries.
1091
1092@kindex --check-sections
1093@kindex --no-check-sections
1094@item --check-sections
1095@itemx --no-check-sections
1096Asks the linker @emph{not} to check section addresses after they have
1097been assigned to see if there any overlaps.  Normally the linker will
1098perform this check, and if it finds any overlaps it will produce
1099suitable error messages.  The linker does know about, and does make
1100allowances for sections in overlays.  The default behaviour can be
1101restored by using the command line switch @option{--check-sections}.
1102
1103@cindex cross reference table
1104@kindex --cref
1105@item --cref
1106Output a cross reference table.  If a linker map file is being
1107generated, the cross reference table is printed to the map file.
1108Otherwise, it is printed on the standard output.
1109
1110The format of the table is intentionally simple, so that it may be
1111easily processed by a script if necessary.  The symbols are printed out,
1112sorted by name.  For each symbol, a list of file names is given.  If the
1113symbol is defined, the first file listed is the location of the
1114definition.  The remaining files contain references to the symbol.
1115
1116@cindex common allocation
1117@kindex --no-define-common
1118@item --no-define-common
1119This option inhibits the assignment of addresses to common symbols.
1120The script command @code{INHIBIT_COMMON_ALLOCATION} has the same effect.
1121@xref{Miscellaneous Commands}.
1122
1123The @samp{--no-define-common} option allows decoupling
1124the decision to assign addresses to Common symbols from the choice
1125of the output file type; otherwise a non-Relocatable output type
1126forces assigning addresses to Common symbols.
1127Using @samp{--no-define-common} allows Common symbols that are referenced
1128from a shared library to be assigned addresses only in the main program.
1129This eliminates the unused duplicate space in the shared library,
1130and also prevents any possible confusion over resolving to the wrong
1131duplicate when there are many dynamic modules with specialized search
1132paths for runtime symbol resolution.
1133
1134@cindex symbols, from command line
1135@kindex --defsym @var{symbol}=@var{exp}
1136@item --defsym @var{symbol}=@var{expression}
1137Create a global symbol in the output file, containing the absolute
1138address given by @var{expression}.  You may use this option as many
1139times as necessary to define multiple symbols in the command line.  A
1140limited form of arithmetic is supported for the @var{expression} in this
1141context: you may give a hexadecimal constant or the name of an existing
1142symbol, or use @code{+} and @code{-} to add or subtract hexadecimal
1143constants or symbols.  If you need more elaborate expressions, consider
1144using the linker command language from a script (@pxref{Assignments,,
1145Assignment: Symbol Definitions}).  @emph{Note:} there should be no white
1146space between @var{symbol}, the equals sign (``@key{=}''), and
1147@var{expression}.
1148
1149@cindex demangling, from command line
1150@kindex --demangle[=@var{style}]
1151@kindex --no-demangle
1152@item --demangle[=@var{style}]
1153@itemx --no-demangle
1154These options control whether to demangle symbol names in error messages
1155and other output.  When the linker is told to demangle, it tries to
1156present symbol names in a readable fashion: it strips leading
1157underscores if they are used by the object file format, and converts C++
1158mangled symbol names into user readable names.  Different compilers have
1159different mangling styles.  The optional demangling style argument can be used
1160to choose an appropriate demangling style for your compiler.  The linker will
1161demangle by default unless the environment variable @samp{COLLECT_NO_DEMANGLE}
1162is set.  These options may be used to override the default.
1163
1164@cindex dynamic linker, from command line
1165@kindex -I@var{file}
1166@kindex --dynamic-linker @var{file}
1167@item --dynamic-linker @var{file}
1168Set the name of the dynamic linker.  This is only meaningful when
1169generating dynamically linked ELF executables.  The default dynamic
1170linker is normally correct; don't use this unless you know what you are
1171doing.
1172
1173
1174@kindex --fatal-warnings
1175@item --fatal-warnings
1176Treat all warnings as errors.
1177
1178@kindex --force-exe-suffix
1179@item  --force-exe-suffix
1180Make sure that an output file has a .exe suffix.
1181
1182If a successfully built fully linked output file does not have a
1183@code{.exe} or @code{.dll} suffix, this option forces the linker to copy
1184the output file to one of the same name with a @code{.exe} suffix. This
1185option is useful when using unmodified Unix makefiles on a Microsoft
1186Windows host, since some versions of Windows won't run an image unless
1187it ends in a @code{.exe} suffix.
1188
1189@kindex --gc-sections
1190@kindex --no-gc-sections
1191@cindex garbage collection
1192@item --no-gc-sections
1193@itemx --gc-sections
1194Enable garbage collection of unused input sections.  It is ignored on
1195targets that do not support this option.  This option is not compatible
1196with @samp{-r}. The default behaviour (of not performing this garbage
1197collection) can be restored by specifying @samp{--no-gc-sections} on
1198the command line.
1199
1200@cindex help
1201@cindex usage
1202@kindex --help
1203@item --help
1204Print a summary of the command-line options on the standard output and exit.
1205
1206@kindex --target-help
1207@item --target-help
1208Print a summary of all target specific options on the standard output and exit.
1209
1210@kindex -Map
1211@item -Map @var{mapfile}
1212Print a link map to the file @var{mapfile}.  See the description of the
1213@option{-M} option, above.
1214
1215@cindex memory usage
1216@kindex --no-keep-memory
1217@item --no-keep-memory
1218@command{ld} normally optimizes for speed over memory usage by caching the
1219symbol tables of input files in memory.  This option tells @command{ld} to
1220instead optimize for memory usage, by rereading the symbol tables as
1221necessary.  This may be required if @command{ld} runs out of memory space
1222while linking a large executable.
1223
1224@kindex --no-undefined
1225@kindex -z defs
1226@item --no-undefined
1227@itemx -z defs
1228Report unresolved symbol references from regular object files.  This
1229is done even if the linker is creating a non-symbolic shared library.
1230The switch @option{--[no-]allow-shlib-undefined} controls the
1231behaviour for reporting unresolved references found in shared
1232libraries being linked in.
1233
1234@kindex --allow-multiple-definition
1235@kindex -z muldefs
1236@item --allow-multiple-definition
1237@itemx -z muldefs
1238Normally when a symbol is defined multiple times, the linker will
1239report a fatal error. These options allow multiple definitions and the
1240first definition will be used.
1241
1242@kindex --allow-shlib-undefined
1243@kindex --no-allow-shlib-undefined
1244@item --allow-shlib-undefined
1245@itemx --no-allow-shlib-undefined
1246Allows (the default) or disallows undefined symbols in shared libraries.
1247This switch is similar to @option{--no-undefined} except that it
1248determines the behaviour when the undefined symbols are in a
1249shared library rather than a regular object file.  It does not affect
1250how undefined symbols in regular object files are handled.
1251
1252The reason that @option{--allow-shlib-undefined} is the default is that
1253the shared library being specified at link time may not be the same as
1254the one that is available at load time, so the symbols might actually be
1255resolvable at load time.  Plus there are some systems, (eg BeOS) where
1256undefined symbols in shared libraries is normal.  (The kernel patches
1257them at load time to select which function is most appropriate
1258for the current architecture.  This is used for example to dynamically
1259select an appropriate memset function).  Apparently it is also normal
1260for HPPA shared libraries to have undefined symbols.
1261
1262@kindex --no-undefined-version
1263@item --no-undefined-version
1264Normally when a symbol has an undefined version, the linker will ignore
1265it. This option disallows symbols with undefined version and a fatal error
1266will be issued instead.
1267
1268@kindex --default-symver
1269@item --default-symver
1270Create and use a default symbol version (the soname) for unversioned
1271exported symbols.
1272
1273@kindex --default-imported-symver
1274@item --default-imported-symver
1275Create and use a default symbol version (the soname) for unversioned
1276imported symbols.
1277
1278@kindex --no-warn-mismatch
1279@item --no-warn-mismatch
1280Normally @command{ld} will give an error if you try to link together input
1281files that are mismatched for some reason, perhaps because they have
1282been compiled for different processors or for different endiannesses.
1283This option tells @command{ld} that it should silently permit such possible
1284errors.  This option should only be used with care, in cases when you
1285have taken some special action that ensures that the linker errors are
1286inappropriate.
1287
1288@kindex --no-whole-archive
1289@item --no-whole-archive
1290Turn off the effect of the @option{--whole-archive} option for subsequent
1291archive files.
1292
1293@cindex output file after errors
1294@kindex --noinhibit-exec
1295@item --noinhibit-exec
1296Retain the executable output file whenever it is still usable.
1297Normally, the linker will not produce an output file if it encounters
1298errors during the link process; it exits without writing an output file
1299when it issues any error whatsoever.
1300
1301@kindex -nostdlib
1302@item -nostdlib
1303Only search library directories explicitly specified on the
1304command line.  Library directories specified in linker scripts
1305(including linker scripts specified on the command line) are ignored.
1306
1307@ifclear SingleFormat
1308@kindex --oformat
1309@item --oformat @var{output-format}
1310@command{ld} may be configured to support more than one kind of object
1311file.  If your @command{ld} is configured this way, you can use the
1312@samp{--oformat} option to specify the binary format for the output
1313object file.  Even when @command{ld} is configured to support alternative
1314object formats, you don't usually need to specify this, as @command{ld}
1315should be configured to produce as a default output format the most
1316usual format on each machine.  @var{output-format} is a text string, the
1317name of a particular format supported by the BFD libraries.  (You can
1318list the available binary formats with @samp{objdump -i}.)  The script
1319command @code{OUTPUT_FORMAT} can also specify the output format, but
1320this option overrides it.  @xref{BFD}.
1321@end ifclear
1322
1323@kindex -pie
1324@kindex --pic-executable
1325@item -pie
1326@itemx --pic-executable
1327@cindex position independent executables
1328Create a position independent executable.  This is currently only supported on
1329ELF platforms.  Position independent executables are similar to shared
1330libraries in that they are relocated by the dynamic linker to the virtual
1331address the OS chooses for them (which can vary between invocations).  Like
1332normal dynamically linked executables they can be executed and symbols
1333defined in the executable cannot be overridden by shared libraries.
1334
1335@kindex -qmagic
1336@item -qmagic
1337This option is ignored for Linux compatibility.
1338
1339@kindex -Qy
1340@item -Qy
1341This option is ignored for SVR4 compatibility.
1342
1343@kindex --relax
1344@cindex synthesizing linker
1345@cindex relaxing addressing modes
1346@item --relax
1347An option with machine dependent effects.
1348@ifset GENERIC
1349This option is only supported on a few targets.
1350@end ifset
1351@ifset H8300
1352@xref{H8/300,,@command{ld} and the H8/300}.
1353@end ifset
1354@ifset I960
1355@xref{i960,, @command{ld} and the Intel 960 family}.
1356@end ifset
1357@ifset XTENSA
1358@xref{Xtensa,, @command{ld} and Xtensa Processors}.
1359@end ifset
1360@ifset M68HC11
1361@xref{M68HC11/68HC12,,@command{ld} and the 68HC11 and 68HC12}.
1362@end ifset
1363
1364On some platforms, the @samp{--relax} option performs global
1365optimizations that become possible when the linker resolves addressing
1366in the program, such as relaxing address modes and synthesizing new
1367instructions in the output object file.
1368
1369On some platforms these link time global optimizations may make symbolic
1370debugging of the resulting executable impossible.
1371@ifset GENERIC
1372This is known to be
1373the case for the Matsushita MN10200 and MN10300 family of processors.
1374@end ifset
1375
1376@ifset GENERIC
1377On platforms where this is not supported, @samp{--relax} is accepted,
1378but ignored.
1379@end ifset
1380
1381@cindex retaining specified symbols
1382@cindex stripping all but some symbols
1383@cindex symbols, retaining selectively
1384@item --retain-symbols-file @var{filename}
1385Retain @emph{only} the symbols listed in the file @var{filename},
1386discarding all others.  @var{filename} is simply a flat file, with one
1387symbol name per line.  This option is especially useful in environments
1388@ifset GENERIC
1389(such as VxWorks)
1390@end ifset
1391where a large global symbol table is accumulated gradually, to conserve
1392run-time memory.
1393
1394@samp{--retain-symbols-file} does @emph{not} discard undefined symbols,
1395or symbols needed for relocations.
1396
1397You may only specify @samp{--retain-symbols-file} once in the command
1398line.  It overrides @samp{-s} and @samp{-S}.
1399
1400@ifset GENERIC
1401@item -rpath @var{dir}
1402@cindex runtime library search path
1403@kindex -rpath
1404Add a directory to the runtime library search path.  This is used when
1405linking an ELF executable with shared objects.  All @option{-rpath}
1406arguments are concatenated and passed to the runtime linker, which uses
1407them to locate shared objects at runtime.  The @option{-rpath} option is
1408also used when locating shared objects which are needed by shared
1409objects explicitly included in the link; see the description of the
1410@option{-rpath-link} option.  If @option{-rpath} is not used when linking an
1411ELF executable, the contents of the environment variable
1412@code{LD_RUN_PATH} will be used if it is defined.
1413
1414The @option{-rpath} option may also be used on SunOS.  By default, on
1415SunOS, the linker will form a runtime search patch out of all the
1416@option{-L} options it is given.  If a @option{-rpath} option is used, the
1417runtime search path will be formed exclusively using the @option{-rpath}
1418options, ignoring the @option{-L} options.  This can be useful when using
1419gcc, which adds many @option{-L} options which may be on NFS mounted
1420filesystems.
1421
1422For compatibility with other ELF linkers, if the @option{-R} option is
1423followed by a directory name, rather than a file name, it is treated as
1424the @option{-rpath} option.
1425@end ifset
1426
1427@ifset GENERIC
1428@cindex link-time runtime library search path
1429@kindex -rpath-link
1430@item -rpath-link @var{DIR}
1431When using ELF or SunOS, one shared library may require another.  This
1432happens when an @code{ld -shared} link includes a shared library as one
1433of the input files.
1434
1435When the linker encounters such a dependency when doing a non-shared,
1436non-relocatable link, it will automatically try to locate the required
1437shared library and include it in the link, if it is not included
1438explicitly.  In such a case, the @option{-rpath-link} option
1439specifies the first set of directories to search.  The
1440@option{-rpath-link} option may specify a sequence of directory names
1441either by specifying a list of names separated by colons, or by
1442appearing multiple times.
1443
1444This option should be used with caution as it overrides the search path
1445that may have been hard compiled into a shared library. In such a case it
1446is possible to use unintentionally a different search path than the
1447runtime linker would do.
1448
1449The linker uses the following search paths to locate required shared
1450libraries.
1451@enumerate
1452@item
1453Any directories specified by @option{-rpath-link} options.
1454@item
1455Any directories specified by @option{-rpath} options.  The difference
1456between @option{-rpath} and @option{-rpath-link} is that directories
1457specified by @option{-rpath} options are included in the executable and
1458used at runtime, whereas the @option{-rpath-link} option is only effective
1459at link time. It is for the native linker only.
1460@item
1461On an ELF system, if the @option{-rpath} and @code{rpath-link} options
1462were not used, search the contents of the environment variable
1463@code{LD_RUN_PATH}. It is for the native linker only.
1464@item
1465On SunOS, if the @option{-rpath} option was not used, search any
1466directories specified using @option{-L} options.
1467@item
1468For a native linker, the contents of the environment variable
1469@code{LD_LIBRARY_PATH}.
1470@item
1471For a native ELF linker, the directories in @code{DT_RUNPATH} or
1472@code{DT_RPATH} of a shared library are searched for shared
1473libraries needed by it. The @code{DT_RPATH} entries are ignored if
1474@code{DT_RUNPATH} entries exist.
1475@item
1476The default directories, normally @file{/lib} and @file{/usr/lib}.
1477@item
1478For a native linker on an ELF system, if the file @file{/etc/ld.so.conf}
1479exists, the list of directories found in that file.
1480@end enumerate
1481
1482If the required shared library is not found, the linker will issue a
1483warning and continue with the link.
1484@end ifset
1485
1486@kindex -shared
1487@kindex -Bshareable
1488@item -shared
1489@itemx -Bshareable
1490@cindex shared libraries
1491Create a shared library.  This is currently only supported on ELF, XCOFF
1492and SunOS platforms.  On SunOS, the linker will automatically create a
1493shared library if the @option{-e} option is not used and there are
1494undefined symbols in the link.
1495
1496@item --sort-common
1497@kindex --sort-common
1498This option tells @command{ld} to sort the common symbols by size when it
1499places them in the appropriate output sections.  First come all the one
1500byte symbols, then all the two byte, then all the four byte, and then
1501everything else.  This is to prevent gaps between symbols due to
1502alignment constraints.
1503
1504@kindex --sort-section name
1505@item --sort-section name
1506This option will apply @code{SORT_BY_NAME} to all wildcard section
1507patterns in the linker script.
1508
1509@kindex --sort-section alignment
1510@item --sort-section alignment
1511This option will apply @code{SORT_BY_ALIGNMENT} to all wildcard section
1512patterns in the linker script.
1513
1514@kindex --split-by-file
1515@item --split-by-file [@var{size}]
1516Similar to @option{--split-by-reloc} but creates a new output section for
1517each input file when @var{size} is reached.  @var{size} defaults to a
1518size of 1 if not given.
1519
1520@kindex --split-by-reloc
1521@item --split-by-reloc [@var{count}]
1522Tries to creates extra sections in the output file so that no single
1523output section in the file contains more than @var{count} relocations.
1524This is useful when generating huge relocatable files for downloading into
1525certain real time kernels with the COFF object file format; since COFF
1526cannot represent more than 65535 relocations in a single section.  Note
1527that this will fail to work with object file formats which do not
1528support arbitrary sections.  The linker will not split up individual
1529input sections for redistribution, so if a single input section contains
1530more than @var{count} relocations one output section will contain that
1531many relocations.  @var{count} defaults to a value of 32768.
1532
1533@kindex --stats
1534@item --stats
1535Compute and display statistics about the operation of the linker, such
1536as execution time and memory usage.
1537
1538@kindex --sysroot
1539@item --sysroot=@var{directory}
1540Use @var{directory} as the location of the sysroot, overriding the
1541configure-time default.  This option is only supported by linkers
1542that were configured using @option{--with-sysroot}.
1543
1544@kindex --traditional-format
1545@cindex traditional format
1546@item --traditional-format
1547For some targets, the output of @command{ld} is different in some ways from
1548the output of some existing linker.  This switch requests @command{ld} to
1549use the traditional format instead.
1550
1551@cindex dbx
1552For example, on SunOS, @command{ld} combines duplicate entries in the
1553symbol string table.  This can reduce the size of an output file with
1554full debugging information by over 30 percent.  Unfortunately, the SunOS
1555@code{dbx} program can not read the resulting program (@code{gdb} has no
1556trouble).  The @samp{--traditional-format} switch tells @command{ld} to not
1557combine duplicate entries.
1558
1559@kindex --section-start @var{sectionname}=@var{org}
1560@item --section-start @var{sectionname}=@var{org}
1561Locate a section in the output file at the absolute
1562address given by @var{org}.  You may use this option as many
1563times as necessary to locate multiple sections in the command
1564line.
1565@var{org} must be a single hexadecimal integer;
1566for compatibility with other linkers, you may omit the leading
1567@samp{0x} usually associated with hexadecimal values.  @emph{Note:} there
1568should be no white space between @var{sectionname}, the equals
1569sign (``@key{=}''), and @var{org}.
1570
1571@kindex -Tbss @var{org}
1572@kindex -Tdata @var{org}
1573@kindex -Ttext @var{org}
1574@cindex segment origins, cmd line
1575@item -Tbss @var{org}
1576@itemx -Tdata @var{org}
1577@itemx -Ttext @var{org}
1578Same as --section-start, with @code{.bss}, @code{.data} or
1579@code{.text} as the @var{sectionname}.
1580
1581@kindex --unresolved-symbols
1582@item --unresolved-symbols=@var{method}
1583Determine how to handle unresolved symbols.  There are four possible
1584values for @samp{method}:
1585
1586@table @samp
1587@item ignore-all
1588Do not report any unresolved symbols.
1589
1590@item report-all
1591Report all unresolved symbols.  This is the default.
1592
1593@item ignore-in-object-files
1594Report unresolved symbols that are contained in shared libraries, but
1595ignore them if they come from regular object files.
1596
1597@item ignore-in-shared-libs
1598Report unresolved symbols that come from regular object files, but
1599ignore them if they come from shared libraries.  This can be useful
1600when creating a dynamic binary and it is known that all the shared
1601libraries that it should be referencing are included on the linker's
1602command line.
1603@end table
1604
1605The behaviour for shared libraries on their own can also be controlled
1606by the @option{--[no-]allow-shlib-undefined} option.
1607
1608Normally the linker will generate an error message for each reported
1609unresolved symbol but the option @option{--warn-unresolved-symbols}
1610can change this to a warning.
1611
1612@kindex --verbose
1613@cindex verbose
1614@item --dll-verbose
1615@itemx --verbose
1616Display the version number for @command{ld} and list the linker emulations
1617supported.  Display which input files can and cannot be opened.  Display
1618the linker script being used by the linker.
1619
1620@kindex --version-script=@var{version-scriptfile}
1621@cindex version script, symbol versions
1622@itemx --version-script=@var{version-scriptfile}
1623Specify the name of a version script to the linker.  This is typically
1624used when creating shared libraries to specify additional information
1625about the version hierarchy for the library being created.  This option
1626is only meaningful on ELF platforms which support shared libraries.
1627@xref{VERSION}.
1628
1629@kindex --warn-common
1630@cindex warnings, on combining symbols
1631@cindex combining symbols, warnings on
1632@item --warn-common
1633Warn when a common symbol is combined with another common symbol or with
1634a symbol definition.  Unix linkers allow this somewhat sloppy practise,
1635but linkers on some other operating systems do not.  This option allows
1636you to find potential problems from combining global symbols.
1637Unfortunately, some C libraries use this practise, so you may get some
1638warnings about symbols in the libraries as well as in your programs.
1639
1640There are three kinds of global symbols, illustrated here by C examples:
1641
1642@table @samp
1643@item int i = 1;
1644A definition, which goes in the initialized data section of the output
1645file.
1646
1647@item extern int i;
1648An undefined reference, which does not allocate space.
1649There must be either a definition or a common symbol for the
1650variable somewhere.
1651
1652@item int i;
1653A common symbol.  If there are only (one or more) common symbols for a
1654variable, it goes in the uninitialized data area of the output file.
1655The linker merges multiple common symbols for the same variable into a
1656single symbol.  If they are of different sizes, it picks the largest
1657size.  The linker turns a common symbol into a declaration, if there is
1658a definition of the same variable.
1659@end table
1660
1661The @samp{--warn-common} option can produce five kinds of warnings.
1662Each warning consists of a pair of lines: the first describes the symbol
1663just encountered, and the second describes the previous symbol
1664encountered with the same name.  One or both of the two symbols will be
1665a common symbol.
1666
1667@enumerate
1668@item
1669Turning a common symbol into a reference, because there is already a
1670definition for the symbol.
1671@smallexample
1672@var{file}(@var{section}): warning: common of `@var{symbol}'
1673   overridden by definition
1674@var{file}(@var{section}): warning: defined here
1675@end smallexample
1676
1677@item
1678Turning a common symbol into a reference, because a later definition for
1679the symbol is encountered.  This is the same as the previous case,
1680except that the symbols are encountered in a different order.
1681@smallexample
1682@var{file}(@var{section}): warning: definition of `@var{symbol}'
1683   overriding common
1684@var{file}(@var{section}): warning: common is here
1685@end smallexample
1686
1687@item
1688Merging a common symbol with a previous same-sized common symbol.
1689@smallexample
1690@var{file}(@var{section}): warning: multiple common
1691   of `@var{symbol}'
1692@var{file}(@var{section}): warning: previous common is here
1693@end smallexample
1694
1695@item
1696Merging a common symbol with a previous larger common symbol.
1697@smallexample
1698@var{file}(@var{section}): warning: common of `@var{symbol}'
1699   overridden by larger common
1700@var{file}(@var{section}): warning: larger common is here
1701@end smallexample
1702
1703@item
1704Merging a common symbol with a previous smaller common symbol.  This is
1705the same as the previous case, except that the symbols are
1706encountered in a different order.
1707@smallexample
1708@var{file}(@var{section}): warning: common of `@var{symbol}'
1709   overriding smaller common
1710@var{file}(@var{section}): warning: smaller common is here
1711@end smallexample
1712@end enumerate
1713
1714@kindex --warn-constructors
1715@item --warn-constructors
1716Warn if any global constructors are used.  This is only useful for a few
1717object file formats.  For formats like COFF or ELF, the linker can not
1718detect the use of global constructors.
1719
1720@kindex --warn-multiple-gp
1721@item --warn-multiple-gp
1722Warn if multiple global pointer values are required in the output file.
1723This is only meaningful for certain processors, such as the Alpha.
1724Specifically, some processors put large-valued constants in a special
1725section.  A special register (the global pointer) points into the middle
1726of this section, so that constants can be loaded efficiently via a
1727base-register relative addressing mode.  Since the offset in
1728base-register relative mode is fixed and relatively small (e.g., 16
1729bits), this limits the maximum size of the constant pool.  Thus, in
1730large programs, it is often necessary to use multiple global pointer
1731values in order to be able to address all possible constants.  This
1732option causes a warning to be issued whenever this case occurs.
1733
1734@kindex --warn-once
1735@cindex warnings, on undefined symbols
1736@cindex undefined symbols, warnings on
1737@item --warn-once
1738Only warn once for each undefined symbol, rather than once per module
1739which refers to it.
1740
1741@kindex --warn-section-align
1742@cindex warnings, on section alignment
1743@cindex section alignment, warnings on
1744@item --warn-section-align
1745Warn if the address of an output section is changed because of
1746alignment.  Typically, the alignment will be set by an input section.
1747The address will only be changed if it not explicitly specified; that
1748is, if the @code{SECTIONS} command does not specify a start address for
1749the section (@pxref{SECTIONS}).
1750
1751@kindex --warn-shared-textrel
1752@item --warn-shared-textrel
1753Warn if the linker adds a DT_TEXTREL to a shared object.
1754
1755@kindex --warn-unresolved-symbols
1756@item --warn-unresolved-symbols
1757If the linker is going to report an unresolved symbol (see the option
1758@option{--unresolved-symbols}) it will normally generate an error.
1759This option makes it generate a warning instead.
1760
1761@kindex --error-unresolved-symbols
1762@item --error-unresolved-symbols
1763This restores the linker's default behaviour of generating errors when
1764it is reporting unresolved symbols.
1765
1766@kindex --whole-archive
1767@cindex including an entire archive
1768@item --whole-archive
1769For each archive mentioned on the command line after the
1770@option{--whole-archive} option, include every object file in the archive
1771in the link, rather than searching the archive for the required object
1772files.  This is normally used to turn an archive file into a shared
1773library, forcing every object to be included in the resulting shared
1774library.  This option may be used more than once.
1775
1776Two notes when using this option from gcc: First, gcc doesn't know
1777about this option, so you have to use @option{-Wl,-whole-archive}.
1778Second, don't forget to use @option{-Wl,-no-whole-archive} after your
1779list of archives, because gcc will add its own list of archives to
1780your link and you may not want this flag to affect those as well.
1781
1782@kindex --wrap
1783@item --wrap @var{symbol}
1784Use a wrapper function for @var{symbol}.  Any undefined reference to
1785@var{symbol} will be resolved to @code{__wrap_@var{symbol}}.  Any
1786undefined reference to @code{__real_@var{symbol}} will be resolved to
1787@var{symbol}.
1788
1789This can be used to provide a wrapper for a system function.  The
1790wrapper function should be called @code{__wrap_@var{symbol}}.  If it
1791wishes to call the system function, it should call
1792@code{__real_@var{symbol}}.
1793
1794Here is a trivial example:
1795
1796@smallexample
1797void *
1798__wrap_malloc (size_t c)
1799@{
1800  printf ("malloc called with %zu\n", c);
1801  return __real_malloc (c);
1802@}
1803@end smallexample
1804
1805If you link other code with this file using @option{--wrap malloc}, then
1806all calls to @code{malloc} will call the function @code{__wrap_malloc}
1807instead.  The call to @code{__real_malloc} in @code{__wrap_malloc} will
1808call the real @code{malloc} function.
1809
1810You may wish to provide a @code{__real_malloc} function as well, so that
1811links without the @option{--wrap} option will succeed.  If you do this,
1812you should not put the definition of @code{__real_malloc} in the same
1813file as @code{__wrap_malloc}; if you do, the assembler may resolve the
1814call before the linker has a chance to wrap it to @code{malloc}.
1815
1816@kindex --enable-new-dtags
1817@kindex --disable-new-dtags
1818@item --enable-new-dtags
1819@itemx --disable-new-dtags
1820This linker can create the new dynamic tags in ELF. But the older ELF
1821systems may not understand them. If you specify
1822@option{--enable-new-dtags}, the dynamic tags will be created as needed.
1823If you specify @option{--disable-new-dtags}, no new dynamic tags will be
1824created. By default, the new dynamic tags are not created. Note that
1825those options are only available for ELF systems.
1826
1827@kindex --hash-size=@var{number}
1828@item --hash-size=@var{number}
1829Set the default size of the linker's hash tables to a prime number
1830close to @var{number}.  Increasing this value can reduce the length of
1831time it takes the linker to perform its tasks, at the expense of
1832increasing the linker's memory requirements.  Similarly reducing this
1833value can reduce the memory requirements at the expense of speed.
1834
1835@kindex --reduce-memory-overheads
1836@item --reduce-memory-overheads
1837This option reduces memory requirements at ld runtime, at the expense of
1838linking speed.  This was introduced to to select the old O(n^2) algorithm
1839for link map file generation, rather than the new O(n) algorithm which uses
1840about 40% more memory for symbol storage.
1841
1842Another affect of the switch is to set the default hash table size to
18431021, which again saves memory at the cost of lengthening the linker's
1844run time.  This is not done however if the @option{--hash-size} switch
1845has been used.
1846
1847The @option{--reduce-memory-overheads} switch may be also be used to
1848enable other tradeoffs in future versions of the linker.
1849
1850@end table
1851
1852@c man end
1853
1854@subsection Options Specific to i386 PE Targets
1855
1856@c man begin OPTIONS
1857
1858The i386 PE linker supports the @option{-shared} option, which causes
1859the output to be a dynamically linked library (DLL) instead of a
1860normal executable.  You should name the output @code{*.dll} when you
1861use this option.  In addition, the linker fully supports the standard
1862@code{*.def} files, which may be specified on the linker command line
1863like an object file (in fact, it should precede archives it exports
1864symbols from, to ensure that they get linked in, just like a normal
1865object file).
1866
1867In addition to the options common to all targets, the i386 PE linker
1868support additional command line options that are specific to the i386
1869PE target.  Options that take values may be separated from their
1870values by either a space or an equals sign.
1871
1872@table @gcctabopt
1873
1874@kindex --add-stdcall-alias
1875@item --add-stdcall-alias
1876If given, symbols with a stdcall suffix (@@@var{nn}) will be exported
1877as-is and also with the suffix stripped.
1878[This option is specific to the i386 PE targeted port of the linker]
1879
1880@kindex --base-file
1881@item --base-file @var{file}
1882Use @var{file} as the name of a file in which to save the base
1883addresses of all the relocations needed for generating DLLs with
1884@file{dlltool}.
1885[This is an i386 PE specific option]
1886
1887@kindex --dll
1888@item --dll
1889Create a DLL instead of a regular executable.  You may also use
1890@option{-shared} or specify a @code{LIBRARY} in a given @code{.def}
1891file.
1892[This option is specific to the i386 PE targeted port of the linker]
1893
1894@kindex --enable-stdcall-fixup
1895@kindex --disable-stdcall-fixup
1896@item --enable-stdcall-fixup
1897@itemx --disable-stdcall-fixup
1898If the link finds a symbol that it cannot resolve, it will attempt to
1899do ``fuzzy linking'' by looking for another defined symbol that differs
1900only in the format of the symbol name (cdecl vs stdcall) and will
1901resolve that symbol by linking to the match.  For example, the
1902undefined symbol @code{_foo} might be linked to the function
1903@code{_foo@@12}, or the undefined symbol @code{_bar@@16} might be linked
1904to the function @code{_bar}.  When the linker does this, it prints a
1905warning, since it normally should have failed to link, but sometimes
1906import libraries generated from third-party dlls may need this feature
1907to be usable.  If you specify @option{--enable-stdcall-fixup}, this
1908feature is fully enabled and warnings are not printed.  If you specify
1909@option{--disable-stdcall-fixup}, this feature is disabled and such
1910mismatches are considered to be errors.
1911[This option is specific to the i386 PE targeted port of the linker]
1912
1913@cindex DLLs, creating
1914@kindex --export-all-symbols
1915@item --export-all-symbols
1916If given, all global symbols in the objects used to build a DLL will
1917be exported by the DLL.  Note that this is the default if there
1918otherwise wouldn't be any exported symbols.  When symbols are
1919explicitly exported via DEF files or implicitly exported via function
1920attributes, the default is to not export anything else unless this
1921option is given.  Note that the symbols @code{DllMain@@12},
1922@code{DllEntryPoint@@0}, @code{DllMainCRTStartup@@12}, and
1923@code{impure_ptr} will not be automatically
1924exported.  Also, symbols imported from other DLLs will not be
1925re-exported, nor will symbols specifying the DLL's internal layout
1926such as those beginning with @code{_head_} or ending with
1927@code{_iname}.  In addition, no symbols from @code{libgcc},
1928@code{libstd++}, @code{libmingw32}, or @code{crtX.o} will be exported.
1929Symbols whose names begin with @code{__rtti_} or @code{__builtin_} will
1930not be exported, to help with C++ DLLs.  Finally, there is an
1931extensive list of cygwin-private symbols that are not exported
1932(obviously, this applies on when building DLLs for cygwin targets).
1933These cygwin-excludes are: @code{_cygwin_dll_entry@@12},
1934@code{_cygwin_crt0_common@@8}, @code{_cygwin_noncygwin_dll_entry@@12},
1935@code{_fmode}, @code{_impure_ptr}, @code{cygwin_attach_dll},
1936@code{cygwin_premain0}, @code{cygwin_premain1}, @code{cygwin_premain2},
1937@code{cygwin_premain3}, and @code{environ}.
1938[This option is specific to the i386 PE targeted port of the linker]
1939
1940@kindex --exclude-symbols
1941@item --exclude-symbols @var{symbol},@var{symbol},...
1942Specifies a list of symbols which should not be automatically
1943exported.  The symbol names may be delimited by commas or colons.
1944[This option is specific to the i386 PE targeted port of the linker]
1945
1946@kindex --file-alignment
1947@item --file-alignment
1948Specify the file alignment.  Sections in the file will always begin at
1949file offsets which are multiples of this number.  This defaults to
1950512.
1951[This option is specific to the i386 PE targeted port of the linker]
1952
1953@cindex heap size
1954@kindex --heap
1955@item --heap @var{reserve}
1956@itemx --heap @var{reserve},@var{commit}
1957Specify the amount of memory to reserve (and optionally commit) to be
1958used as heap for this program.  The default is 1Mb reserved, 4K
1959committed.
1960[This option is specific to the i386 PE targeted port of the linker]
1961
1962@cindex image base
1963@kindex --image-base
1964@item --image-base @var{value}
1965Use @var{value} as the base address of your program or dll.  This is
1966the lowest memory location that will be used when your program or dll
1967is loaded.  To reduce the need to relocate and improve performance of
1968your dlls, each should have a unique base address and not overlap any
1969other dlls.  The default is 0x400000 for executables, and 0x10000000
1970for dlls.
1971[This option is specific to the i386 PE targeted port of the linker]
1972
1973@kindex --kill-at
1974@item --kill-at
1975If given, the stdcall suffixes (@@@var{nn}) will be stripped from
1976symbols before they are exported.
1977[This option is specific to the i386 PE targeted port of the linker]
1978
1979@kindex --large-address-aware
1980@item --large-address-aware
1981If given, the appropriate bit in the ``Charateristics'' field of the COFF
1982header is set to indicate that this executable supports virtual addresses
1983greater than 2 gigabytes.  This should be used in conjuction with the /3GB
1984or /USERVA=@var{value} megabytes switch in the ``[operating systems]''
1985section of the BOOT.INI.  Otherwise, this bit has no effect.
1986[This option is specific to PE targeted ports of the linker]
1987
1988@kindex --major-image-version
1989@item --major-image-version @var{value}
1990Sets the major number of the ``image version''.  Defaults to 1.
1991[This option is specific to the i386 PE targeted port of the linker]
1992
1993@kindex --major-os-version
1994@item --major-os-version @var{value}
1995Sets the major number of the ``os version''.  Defaults to 4.
1996[This option is specific to the i386 PE targeted port of the linker]
1997
1998@kindex --major-subsystem-version
1999@item --major-subsystem-version @var{value}
2000Sets the major number of the ``subsystem version''.  Defaults to 4.
2001[This option is specific to the i386 PE targeted port of the linker]
2002
2003@kindex --minor-image-version
2004@item --minor-image-version @var{value}
2005Sets the minor number of the ``image version''.  Defaults to 0.
2006[This option is specific to the i386 PE targeted port of the linker]
2007
2008@kindex --minor-os-version
2009@item --minor-os-version @var{value}
2010Sets the minor number of the ``os version''.  Defaults to 0.
2011[This option is specific to the i386 PE targeted port of the linker]
2012
2013@kindex --minor-subsystem-version
2014@item --minor-subsystem-version @var{value}
2015Sets the minor number of the ``subsystem version''.  Defaults to 0.
2016[This option is specific to the i386 PE targeted port of the linker]
2017
2018@cindex DEF files, creating
2019@cindex DLLs, creating
2020@kindex --output-def
2021@item --output-def @var{file}
2022The linker will create the file @var{file} which will contain a DEF
2023file corresponding to the DLL the linker is generating.  This DEF file
2024(which should be called @code{*.def}) may be used to create an import
2025library with @code{dlltool} or may be used as a reference to
2026automatically or implicitly exported symbols.
2027[This option is specific to the i386 PE targeted port of the linker]
2028
2029@cindex DLLs, creating
2030@kindex --out-implib
2031@item --out-implib @var{file}
2032The linker will create the file @var{file} which will contain an
2033import lib corresponding to the DLL the linker is generating. This
2034import lib (which should be called @code{*.dll.a} or @code{*.a}
2035may be used to link clients against the generated DLL; this behaviour
2036makes it possible to skip a separate @code{dlltool} import library
2037creation step.
2038[This option is specific to the i386 PE targeted port of the linker]
2039
2040@kindex --enable-auto-image-base
2041@item --enable-auto-image-base
2042Automatically choose the image base for DLLs, unless one is specified
2043using the @code{--image-base} argument.  By using a hash generated
2044from the dllname to create unique image bases for each DLL, in-memory
2045collisions and relocations which can delay program execution are
2046avoided.
2047[This option is specific to the i386 PE targeted port of the linker]
2048
2049@kindex --disable-auto-image-base
2050@item --disable-auto-image-base
2051Do not automatically generate a unique image base.  If there is no
2052user-specified image base (@code{--image-base}) then use the platform
2053default.
2054[This option is specific to the i386 PE targeted port of the linker]
2055
2056@cindex DLLs, linking to
2057@kindex --dll-search-prefix
2058@item --dll-search-prefix @var{string}
2059When linking dynamically to a dll without an import library,
2060search for @code{<string><basename>.dll} in preference to
2061@code{lib<basename>.dll}. This behaviour allows easy distinction
2062between DLLs built for the various "subplatforms": native, cygwin,
2063uwin, pw, etc.  For instance, cygwin DLLs typically use
2064@code{--dll-search-prefix=cyg}.
2065[This option is specific to the i386 PE targeted port of the linker]
2066
2067@kindex --enable-auto-import
2068@item --enable-auto-import
2069Do sophisticated linking of @code{_symbol} to @code{__imp__symbol} for
2070DATA imports from DLLs, and create the necessary thunking symbols when
2071building the import libraries with those DATA exports. Note: Use of the
2072'auto-import' extension will cause the text section of the image file
2073to be made writable. This does not conform to the PE-COFF format
2074specification published by Microsoft.
2075
2076Using 'auto-import' generally will 'just work' -- but sometimes you may
2077see this message:
2078
2079"variable '<var>' can't be auto-imported. Please read the
2080documentation for ld's @code{--enable-auto-import} for details."
2081
2082This message occurs when some (sub)expression accesses an address
2083ultimately given by the sum of two constants (Win32 import tables only
2084allow one).  Instances where this may occur include accesses to member
2085fields of struct variables imported from a DLL, as well as using a
2086constant index into an array variable imported from a DLL.  Any
2087multiword variable (arrays, structs, long long, etc) may trigger
2088this error condition.  However, regardless of the exact data type
2089of the offending exported variable, ld will always detect it, issue
2090the warning, and exit.
2091
2092There are several ways to address this difficulty, regardless of the
2093data type of the exported variable:
2094
2095One way is to use --enable-runtime-pseudo-reloc switch. This leaves the task
2096of adjusting references in your client code for runtime environment, so
2097this method works only when runtime environment supports this feature.
2098
2099A second solution is to force one of the 'constants' to be a variable --
2100that is, unknown and un-optimizable at compile time.  For arrays,
2101there are two possibilities: a) make the indexee (the array's address)
2102a variable, or b) make the 'constant' index a variable.  Thus:
2103
2104@example
2105extern type extern_array[];
2106extern_array[1] -->
2107   @{ volatile type *t=extern_array; t[1] @}
2108@end example
2109
2110or
2111
2112@example
2113extern type extern_array[];
2114extern_array[1] -->
2115   @{ volatile int t=1; extern_array[t] @}
2116@end example
2117
2118For structs (and most other multiword data types) the only option
2119is to make the struct itself (or the long long, or the ...) variable:
2120
2121@example
2122extern struct s extern_struct;
2123extern_struct.field -->
2124   @{ volatile struct s *t=&extern_struct; t->field @}
2125@end example
2126
2127or
2128
2129@example
2130extern long long extern_ll;
2131extern_ll -->
2132  @{ volatile long long * local_ll=&extern_ll; *local_ll @}
2133@end example
2134
2135A third method of dealing with this difficulty is to abandon
2136'auto-import' for the offending symbol and mark it with
2137@code{__declspec(dllimport)}.  However, in practise that
2138requires using compile-time #defines to indicate whether you are
2139building a DLL, building client code that will link to the DLL, or
2140merely building/linking to a static library.   In making the choice
2141between the various methods of resolving the 'direct address with
2142constant offset' problem, you should consider typical real-world usage:
2143
2144Original:
2145@example
2146--foo.h
2147extern int arr[];
2148--foo.c
2149#include "foo.h"
2150void main(int argc, char **argv)@{
2151  printf("%d\n",arr[1]);
2152@}
2153@end example
2154
2155Solution 1:
2156@example
2157--foo.h
2158extern int arr[];
2159--foo.c
2160#include "foo.h"
2161void main(int argc, char **argv)@{
2162  /* This workaround is for win32 and cygwin; do not "optimize" */
2163  volatile int *parr = arr;
2164  printf("%d\n",parr[1]);
2165@}
2166@end example
2167
2168Solution 2:
2169@example
2170--foo.h
2171/* Note: auto-export is assumed (no __declspec(dllexport)) */
2172#if (defined(_WIN32) || defined(__CYGWIN__)) && \
2173  !(defined(FOO_BUILD_DLL) || defined(FOO_STATIC))
2174#define FOO_IMPORT __declspec(dllimport)
2175#else
2176#define FOO_IMPORT
2177#endif
2178extern FOO_IMPORT int arr[];
2179--foo.c
2180#include "foo.h"
2181void main(int argc, char **argv)@{
2182  printf("%d\n",arr[1]);
2183@}
2184@end example
2185
2186A fourth way to avoid this problem is to re-code your
2187library to use a functional interface rather than a data interface
2188for the offending variables (e.g. set_foo() and get_foo() accessor
2189functions).
2190[This option is specific to the i386 PE targeted port of the linker]
2191
2192@kindex --disable-auto-import
2193@item --disable-auto-import
2194Do not attempt to do sophisticated linking of @code{_symbol} to
2195@code{__imp__symbol} for DATA imports from DLLs.
2196[This option is specific to the i386 PE targeted port of the linker]
2197
2198@kindex --enable-runtime-pseudo-reloc
2199@item --enable-runtime-pseudo-reloc
2200If your code contains expressions described in --enable-auto-import section,
2201that is, DATA imports from DLL with non-zero offset, this switch will create
2202a vector of 'runtime pseudo relocations' which can be used by runtime
2203environment to adjust references to such data in your client code.
2204[This option is specific to the i386 PE targeted port of the linker]
2205
2206@kindex --disable-runtime-pseudo-reloc
2207@item --disable-runtime-pseudo-reloc
2208Do not create pseudo relocations for non-zero offset DATA imports from
2209DLLs.  This is the default.
2210[This option is specific to the i386 PE targeted port of the linker]
2211
2212@kindex --enable-extra-pe-debug
2213@item --enable-extra-pe-debug
2214Show additional debug info related to auto-import symbol thunking.
2215[This option is specific to the i386 PE targeted port of the linker]
2216
2217@kindex --section-alignment
2218@item --section-alignment
2219Sets the section alignment.  Sections in memory will always begin at
2220addresses which are a multiple of this number.  Defaults to 0x1000.
2221[This option is specific to the i386 PE targeted port of the linker]
2222
2223@cindex stack size
2224@kindex --stack
2225@item --stack @var{reserve}
2226@itemx --stack @var{reserve},@var{commit}
2227Specify the amount of memory to reserve (and optionally commit) to be
2228used as stack for this program.  The default is 2Mb reserved, 4K
2229committed.
2230[This option is specific to the i386 PE targeted port of the linker]
2231
2232@kindex --subsystem
2233@item --subsystem @var{which}
2234@itemx --subsystem @var{which}:@var{major}
2235@itemx --subsystem @var{which}:@var{major}.@var{minor}
2236Specifies the subsystem under which your program will execute.  The
2237legal values for @var{which} are @code{native}, @code{windows},
2238@code{console}, @code{posix}, and @code{xbox}.  You may optionally set
2239the subsystem version also.  Numeric values are also accepted for
2240@var{which}.
2241[This option is specific to the i386 PE targeted port of the linker]
2242
2243@end table
2244
2245@c man end
2246
2247@ifset M68HC11
2248@subsection Options specific to Motorola 68HC11 and 68HC12 targets
2249
2250@c man begin OPTIONS
2251
2252The 68HC11 and 68HC12 linkers support specific options to control the
2253memory bank switching mapping and trampoline code generation.
2254
2255@table @gcctabopt
2256
2257@kindex --no-trampoline
2258@item --no-trampoline
2259This option disables the generation of trampoline. By default a trampoline
2260is generated for each far function which is called using a @code{jsr}
2261instruction (this happens when a pointer to a far function is taken).
2262
2263@kindex --bank-window
2264@item --bank-window @var{name}
2265This option indicates to the linker the name of the memory region in
2266the @samp{MEMORY} specification that describes the memory bank window.
2267The definition of such region is then used by the linker to compute
2268paging and addresses within the memory window.
2269
2270@end table
2271
2272@c man end
2273@end ifset
2274
2275@ifset UsesEnvVars
2276@node Environment
2277@section Environment Variables
2278
2279@c man begin ENVIRONMENT
2280
2281You can change the behaviour of @command{ld} with the environment variables
2282@ifclear SingleFormat
2283@code{GNUTARGET},
2284@end ifclear
2285@code{LDEMULATION} and @code{COLLECT_NO_DEMANGLE}.
2286
2287@ifclear SingleFormat
2288@kindex GNUTARGET
2289@cindex default input format
2290@code{GNUTARGET} determines the input-file object format if you don't
2291use @samp{-b} (or its synonym @samp{--format}).  Its value should be one
2292of the BFD names for an input format (@pxref{BFD}).  If there is no
2293@code{GNUTARGET} in the environment, @command{ld} uses the natural format
2294of the target. If @code{GNUTARGET} is set to @code{default} then BFD
2295attempts to discover the input format by examining binary input files;
2296this method often succeeds, but there are potential ambiguities, since
2297there is no method of ensuring that the magic number used to specify
2298object-file formats is unique.  However, the configuration procedure for
2299BFD on each system places the conventional format for that system first
2300in the search-list, so ambiguities are resolved in favor of convention.
2301@end ifclear
2302
2303@kindex LDEMULATION
2304@cindex default emulation
2305@cindex emulation, default
2306@code{LDEMULATION} determines the default emulation if you don't use the
2307@samp{-m} option.  The emulation can affect various aspects of linker
2308behaviour, particularly the default linker script.  You can list the
2309available emulations with the @samp{--verbose} or @samp{-V} options.  If
2310the @samp{-m} option is not used, and the @code{LDEMULATION} environment
2311variable is not defined, the default emulation depends upon how the
2312linker was configured.
2313
2314@kindex COLLECT_NO_DEMANGLE
2315@cindex demangling, default
2316Normally, the linker will default to demangling symbols.  However, if
2317@code{COLLECT_NO_DEMANGLE} is set in the environment, then it will
2318default to not demangling symbols.  This environment variable is used in
2319a similar fashion by the @code{gcc} linker wrapper program.  The default
2320may be overridden by the @samp{--demangle} and @samp{--no-demangle}
2321options.
2322
2323@c man end
2324@end ifset
2325
2326@node Scripts
2327@chapter Linker Scripts
2328
2329@cindex scripts
2330@cindex linker scripts
2331@cindex command files
2332Every link is controlled by a @dfn{linker script}.  This script is
2333written in the linker command language.
2334
2335The main purpose of the linker script is to describe how the sections in
2336the input files should be mapped into the output file, and to control
2337the memory layout of the output file.  Most linker scripts do nothing
2338more than this.  However, when necessary, the linker script can also
2339direct the linker to perform many other operations, using the commands
2340described below.
2341
2342The linker always uses a linker script.  If you do not supply one
2343yourself, the linker will use a default script that is compiled into the
2344linker executable.  You can use the @samp{--verbose} command line option
2345to display the default linker script.  Certain command line options,
2346such as @samp{-r} or @samp{-N}, will affect the default linker script.
2347
2348You may supply your own linker script by using the @samp{-T} command
2349line option.  When you do this, your linker script will replace the
2350default linker script.
2351
2352You may also use linker scripts implicitly by naming them as input files
2353to the linker, as though they were files to be linked.  @xref{Implicit
2354Linker Scripts}.
2355
2356@menu
2357* Basic Script Concepts::	Basic Linker Script Concepts
2358* Script Format::		Linker Script Format
2359* Simple Example::		Simple Linker Script Example
2360* Simple Commands::		Simple Linker Script Commands
2361* Assignments::			Assigning Values to Symbols
2362* SECTIONS::			SECTIONS Command
2363* MEMORY::			MEMORY Command
2364* PHDRS::			PHDRS Command
2365* VERSION::			VERSION Command
2366* Expressions::			Expressions in Linker Scripts
2367* Implicit Linker Scripts::	Implicit Linker Scripts
2368@end menu
2369
2370@node Basic Script Concepts
2371@section Basic Linker Script Concepts
2372@cindex linker script concepts
2373We need to define some basic concepts and vocabulary in order to
2374describe the linker script language.
2375
2376The linker combines input files into a single output file.  The output
2377file and each input file are in a special data format known as an
2378@dfn{object file format}.  Each file is called an @dfn{object file}.
2379The output file is often called an @dfn{executable}, but for our
2380purposes we will also call it an object file.  Each object file has,
2381among other things, a list of @dfn{sections}.  We sometimes refer to a
2382section in an input file as an @dfn{input section}; similarly, a section
2383in the output file is an @dfn{output section}.
2384
2385Each section in an object file has a name and a size.  Most sections
2386also have an associated block of data, known as the @dfn{section
2387contents}.  A section may be marked as @dfn{loadable}, which mean that
2388the contents should be loaded into memory when the output file is run.
2389A section with no contents may be @dfn{allocatable}, which means that an
2390area in memory should be set aside, but nothing in particular should be
2391loaded there (in some cases this memory must be zeroed out).  A section
2392which is neither loadable nor allocatable typically contains some sort
2393of debugging information.
2394
2395Every loadable or allocatable output section has two addresses.  The
2396first is the @dfn{VMA}, or virtual memory address.  This is the address
2397the section will have when the output file is run.  The second is the
2398@dfn{LMA}, or load memory address.  This is the address at which the
2399section will be loaded.  In most cases the two addresses will be the
2400same.  An example of when they might be different is when a data section
2401is loaded into ROM, and then copied into RAM when the program starts up
2402(this technique is often used to initialize global variables in a ROM
2403based system).  In this case the ROM address would be the LMA, and the
2404RAM address would be the VMA.
2405
2406You can see the sections in an object file by using the @code{objdump}
2407program with the @samp{-h} option.
2408
2409Every object file also has a list of @dfn{symbols}, known as the
2410@dfn{symbol table}.  A symbol may be defined or undefined.  Each symbol
2411has a name, and each defined symbol has an address, among other
2412information.  If you compile a C or C++ program into an object file, you
2413will get a defined symbol for every defined function and global or
2414static variable.  Every undefined function or global variable which is
2415referenced in the input file will become an undefined symbol.
2416
2417You can see the symbols in an object file by using the @code{nm}
2418program, or by using the @code{objdump} program with the @samp{-t}
2419option.
2420
2421@node Script Format
2422@section Linker Script Format
2423@cindex linker script format
2424Linker scripts are text files.
2425
2426You write a linker script as a series of commands.  Each command is
2427either a keyword, possibly followed by arguments, or an assignment to a
2428symbol.  You may separate commands using semicolons.  Whitespace is
2429generally ignored.
2430
2431Strings such as file or format names can normally be entered directly.
2432If the file name contains a character such as a comma which would
2433otherwise serve to separate file names, you may put the file name in
2434double quotes.  There is no way to use a double quote character in a
2435file name.
2436
2437You may include comments in linker scripts just as in C, delimited by
2438@samp{/*} and @samp{*/}.  As in C, comments are syntactically equivalent
2439to whitespace.
2440
2441@node Simple Example
2442@section Simple Linker Script Example
2443@cindex linker script example
2444@cindex example of linker script
2445Many linker scripts are fairly simple.
2446
2447The simplest possible linker script has just one command:
2448@samp{SECTIONS}.  You use the @samp{SECTIONS} command to describe the
2449memory layout of the output file.
2450
2451The @samp{SECTIONS} command is a powerful command.  Here we will
2452describe a simple use of it.  Let's assume your program consists only of
2453code, initialized data, and uninitialized data.  These will be in the
2454@samp{.text}, @samp{.data}, and @samp{.bss} sections, respectively.
2455Let's assume further that these are the only sections which appear in
2456your input files.
2457
2458For this example, let's say that the code should be loaded at address
24590x10000, and that the data should start at address 0x8000000.  Here is a
2460linker script which will do that:
2461@smallexample
2462SECTIONS
2463@{
2464  . = 0x10000;
2465  .text : @{ *(.text) @}
2466  . = 0x8000000;
2467  .data : @{ *(.data) @}
2468  .bss : @{ *(.bss) @}
2469@}
2470@end smallexample
2471
2472You write the @samp{SECTIONS} command as the keyword @samp{SECTIONS},
2473followed by a series of symbol assignments and output section
2474descriptions enclosed in curly braces.
2475
2476The first line inside the @samp{SECTIONS} command of the above example
2477sets the value of the special symbol @samp{.}, which is the location
2478counter.  If you do not specify the address of an output section in some
2479other way (other ways are described later), the address is set from the
2480current value of the location counter.  The location counter is then
2481incremented by the size of the output section.  At the start of the
2482@samp{SECTIONS} command, the location counter has the value @samp{0}.
2483
2484The second line defines an output section, @samp{.text}.  The colon is
2485required syntax which may be ignored for now.  Within the curly braces
2486after the output section name, you list the names of the input sections
2487which should be placed into this output section.  The @samp{*} is a
2488wildcard which matches any file name.  The expression @samp{*(.text)}
2489means all @samp{.text} input sections in all input files.
2490
2491Since the location counter is @samp{0x10000} when the output section
2492@samp{.text} is defined, the linker will set the address of the
2493@samp{.text} section in the output file to be @samp{0x10000}.
2494
2495The remaining lines define the @samp{.data} and @samp{.bss} sections in
2496the output file.  The linker will place the @samp{.data} output section
2497at address @samp{0x8000000}.  After the linker places the @samp{.data}
2498output section, the value of the location counter will be
2499@samp{0x8000000} plus the size of the @samp{.data} output section.  The
2500effect is that the linker will place the @samp{.bss} output section
2501immediately after the @samp{.data} output section in memory.
2502
2503The linker will ensure that each output section has the required
2504alignment, by increasing the location counter if necessary.  In this
2505example, the specified addresses for the @samp{.text} and @samp{.data}
2506sections will probably satisfy any alignment constraints, but the linker
2507may have to create a small gap between the @samp{.data} and @samp{.bss}
2508sections.
2509
2510That's it!  That's a simple and complete linker script.
2511
2512@node Simple Commands
2513@section Simple Linker Script Commands
2514@cindex linker script simple commands
2515In this section we describe the simple linker script commands.
2516
2517@menu
2518* Entry Point::			Setting the entry point
2519* File Commands::		Commands dealing with files
2520@ifclear SingleFormat
2521* Format Commands::		Commands dealing with object file formats
2522@end ifclear
2523
2524* Miscellaneous Commands::	Other linker script commands
2525@end menu
2526
2527@node Entry Point
2528@subsection Setting the Entry Point
2529@kindex ENTRY(@var{symbol})
2530@cindex start of execution
2531@cindex first instruction
2532@cindex entry point
2533The first instruction to execute in a program is called the @dfn{entry
2534point}.  You can use the @code{ENTRY} linker script command to set the
2535entry point.  The argument is a symbol name:
2536@smallexample
2537ENTRY(@var{symbol})
2538@end smallexample
2539
2540There are several ways to set the entry point.  The linker will set the
2541entry point by trying each of the following methods in order, and
2542stopping when one of them succeeds:
2543@itemize @bullet
2544@item
2545the @samp{-e} @var{entry} command-line option;
2546@item
2547the @code{ENTRY(@var{symbol})} command in a linker script;
2548@item
2549the value of the symbol @code{start}, if defined;
2550@item
2551the address of the first byte of the @samp{.text} section, if present;
2552@item
2553The address @code{0}.
2554@end itemize
2555
2556@node File Commands
2557@subsection Commands Dealing with Files
2558@cindex linker script file commands
2559Several linker script commands deal with files.
2560
2561@table @code
2562@item INCLUDE @var{filename}
2563@kindex INCLUDE @var{filename}
2564@cindex including a linker script
2565Include the linker script @var{filename} at this point.  The file will
2566be searched for in the current directory, and in any directory specified
2567with the @option{-L} option.  You can nest calls to @code{INCLUDE} up to
256810 levels deep.
2569
2570@item INPUT(@var{file}, @var{file}, @dots{})
2571@itemx INPUT(@var{file} @var{file} @dots{})
2572@kindex INPUT(@var{files})
2573@cindex input files in linker scripts
2574@cindex input object files in linker scripts
2575@cindex linker script input object files
2576The @code{INPUT} command directs the linker to include the named files
2577in the link, as though they were named on the command line.
2578
2579For example, if you always want to include @file{subr.o} any time you do
2580a link, but you can't be bothered to put it on every link command line,
2581then you can put @samp{INPUT (subr.o)} in your linker script.
2582
2583In fact, if you like, you can list all of your input files in the linker
2584script, and then invoke the linker with nothing but a @samp{-T} option.
2585
2586In case a @dfn{sysroot prefix} is configured, and the filename starts
2587with the @samp{/} character, and the script being processed was
2588located inside the @dfn{sysroot prefix}, the filename will be looked
2589for in the @dfn{sysroot prefix}.  Otherwise, the linker will try to
2590open the file in the current directory.  If it is not found, the
2591linker will search through the archive library search path.  See the
2592description of @samp{-L} in @ref{Options,,Command Line Options}.
2593
2594If you use @samp{INPUT (-l@var{file})}, @command{ld} will transform the
2595name to @code{lib@var{file}.a}, as with the command line argument
2596@samp{-l}.
2597
2598When you use the @code{INPUT} command in an implicit linker script, the
2599files will be included in the link at the point at which the linker
2600script file is included.  This can affect archive searching.
2601
2602@item GROUP(@var{file}, @var{file}, @dots{})
2603@itemx GROUP(@var{file} @var{file} @dots{})
2604@kindex GROUP(@var{files})
2605@cindex grouping input files
2606The @code{GROUP} command is like @code{INPUT}, except that the named
2607files should all be archives, and they are searched repeatedly until no
2608new undefined references are created.  See the description of @samp{-(}
2609in @ref{Options,,Command Line Options}.
2610
2611@item AS_NEEDED(@var{file}, @var{file}, @dots{})
2612@itemx AS_NEEDED(@var{file} @var{file} @dots{})
2613@kindex AS_NEEDED(@var{files})
2614This construct can appear only inside of the @code{INPUT} or @code{GROUP}
2615commands, among other filenames.  The files listed will be handled
2616as if they appear directly in the @code{INPUT} or @code{GROUP} commands,
2617with the exception of ELF shared libraries, that will be added only
2618when they are actually needed.  This construct essentially enables
2619@option{--as-needed} option for all the files listed inside of it
2620and restores previous @option{--as-needed} resp. @option{--no-as-needed}
2621setting afterwards.
2622
2623@item OUTPUT(@var{filename})
2624@kindex OUTPUT(@var{filename})
2625@cindex output file name in linker scripot
2626The @code{OUTPUT} command names the output file.  Using
2627@code{OUTPUT(@var{filename})} in the linker script is exactly like using
2628@samp{-o @var{filename}} on the command line (@pxref{Options,,Command
2629Line Options}).  If both are used, the command line option takes
2630precedence.
2631
2632You can use the @code{OUTPUT} command to define a default name for the
2633output file other than the usual default of @file{a.out}.
2634
2635@item SEARCH_DIR(@var{path})
2636@kindex SEARCH_DIR(@var{path})
2637@cindex library search path in linker script
2638@cindex archive search path in linker script
2639@cindex search path in linker script
2640The @code{SEARCH_DIR} command adds @var{path} to the list of paths where
2641@command{ld} looks for archive libraries.  Using
2642@code{SEARCH_DIR(@var{path})} is exactly like using @samp{-L @var{path}}
2643on the command line (@pxref{Options,,Command Line Options}).  If both
2644are used, then the linker will search both paths.  Paths specified using
2645the command line option are searched first.
2646
2647@item STARTUP(@var{filename})
2648@kindex STARTUP(@var{filename})
2649@cindex first input file
2650The @code{STARTUP} command is just like the @code{INPUT} command, except
2651that @var{filename} will become the first input file to be linked, as
2652though it were specified first on the command line.  This may be useful
2653when using a system in which the entry point is always the start of the
2654first file.
2655@end table
2656
2657@ifclear SingleFormat
2658@node Format Commands
2659@subsection Commands Dealing with Object File Formats
2660A couple of linker script commands deal with object file formats.
2661
2662@table @code
2663@item OUTPUT_FORMAT(@var{bfdname})
2664@itemx OUTPUT_FORMAT(@var{default}, @var{big}, @var{little})
2665@kindex OUTPUT_FORMAT(@var{bfdname})
2666@cindex output file format in linker script
2667The @code{OUTPUT_FORMAT} command names the BFD format to use for the
2668output file (@pxref{BFD}).  Using @code{OUTPUT_FORMAT(@var{bfdname})} is
2669exactly like using @samp{--oformat @var{bfdname}} on the command line
2670(@pxref{Options,,Command Line Options}).  If both are used, the command
2671line option takes precedence.
2672
2673You can use @code{OUTPUT_FORMAT} with three arguments to use different
2674formats based on the @samp{-EB} and @samp{-EL} command line options.
2675This permits the linker script to set the output format based on the
2676desired endianness.
2677
2678If neither @samp{-EB} nor @samp{-EL} are used, then the output format
2679will be the first argument, @var{default}.  If @samp{-EB} is used, the
2680output format will be the second argument, @var{big}.  If @samp{-EL} is
2681used, the output format will be the third argument, @var{little}.
2682
2683For example, the default linker script for the MIPS ELF target uses this
2684command:
2685@smallexample
2686OUTPUT_FORMAT(elf32-bigmips, elf32-bigmips, elf32-littlemips)
2687@end smallexample
2688This says that the default format for the output file is
2689@samp{elf32-bigmips}, but if the user uses the @samp{-EL} command line
2690option, the output file will be created in the @samp{elf32-littlemips}
2691format.
2692
2693@item TARGET(@var{bfdname})
2694@kindex TARGET(@var{bfdname})
2695@cindex input file format in linker script
2696The @code{TARGET} command names the BFD format to use when reading input
2697files.  It affects subsequent @code{INPUT} and @code{GROUP} commands.
2698This command is like using @samp{-b @var{bfdname}} on the command line
2699(@pxref{Options,,Command Line Options}).  If the @code{TARGET} command
2700is used but @code{OUTPUT_FORMAT} is not, then the last @code{TARGET}
2701command is also used to set the format for the output file.  @xref{BFD}.
2702@end table
2703@end ifclear
2704
2705@node Miscellaneous Commands
2706@subsection Other Linker Script Commands
2707There are a few other linker scripts commands.
2708
2709@table @code
2710@item ASSERT(@var{exp}, @var{message})
2711@kindex ASSERT
2712@cindex assertion in linker script
2713Ensure that @var{exp} is non-zero.  If it is zero, then exit the linker
2714with an error code, and print @var{message}.
2715
2716@item EXTERN(@var{symbol} @var{symbol} @dots{})
2717@kindex EXTERN
2718@cindex undefined symbol in linker script
2719Force @var{symbol} to be entered in the output file as an undefined
2720symbol.  Doing this may, for example, trigger linking of additional
2721modules from standard libraries.  You may list several @var{symbol}s for
2722each @code{EXTERN}, and you may use @code{EXTERN} multiple times.  This
2723command has the same effect as the @samp{-u} command-line option.
2724
2725@item FORCE_COMMON_ALLOCATION
2726@kindex FORCE_COMMON_ALLOCATION
2727@cindex common allocation in linker script
2728This command has the same effect as the @samp{-d} command-line option:
2729to make @command{ld} assign space to common symbols even if a relocatable
2730output file is specified (@samp{-r}).
2731
2732@item INHIBIT_COMMON_ALLOCATION
2733@kindex INHIBIT_COMMON_ALLOCATION
2734@cindex common allocation in linker script
2735This command has the same effect as the @samp{--no-define-common}
2736command-line option: to make @code{ld} omit the assignment of addresses
2737to common symbols even for a non-relocatable output file.
2738
2739@item NOCROSSREFS(@var{section} @var{section} @dots{})
2740@kindex NOCROSSREFS(@var{sections})
2741@cindex cross references
2742This command may be used to tell @command{ld} to issue an error about any
2743references among certain output sections.
2744
2745In certain types of programs, particularly on embedded systems when
2746using overlays, when one section is loaded into memory, another section
2747will not be.  Any direct references between the two sections would be
2748errors.  For example, it would be an error if code in one section called
2749a function defined in the other section.
2750
2751The @code{NOCROSSREFS} command takes a list of output section names.  If
2752@command{ld} detects any cross references between the sections, it reports
2753an error and returns a non-zero exit status.  Note that the
2754@code{NOCROSSREFS} command uses output section names, not input section
2755names.
2756
2757@ifclear SingleFormat
2758@item OUTPUT_ARCH(@var{bfdarch})
2759@kindex OUTPUT_ARCH(@var{bfdarch})
2760@cindex machine architecture
2761@cindex architecture
2762Specify a particular output machine architecture.  The argument is one
2763of the names used by the BFD library (@pxref{BFD}).  You can see the
2764architecture of an object file by using the @code{objdump} program with
2765the @samp{-f} option.
2766@end ifclear
2767@end table
2768
2769@node Assignments
2770@section Assigning Values to Symbols
2771@cindex assignment in scripts
2772@cindex symbol definition, scripts
2773@cindex variables, defining
2774You may assign a value to a symbol in a linker script.  This will define
2775the symbol and place it into the symbol table with a global scope.
2776
2777@menu
2778* Simple Assignments::		Simple Assignments
2779* PROVIDE::			PROVIDE
2780* Source Code Reference::	How to use a linker script defined symbol in source code
2781@end menu
2782
2783@node Simple Assignments
2784@subsection Simple Assignments
2785
2786You may assign to a symbol using any of the C assignment operators:
2787
2788@table @code
2789@item @var{symbol} = @var{expression} ;
2790@itemx @var{symbol} += @var{expression} ;
2791@itemx @var{symbol} -= @var{expression} ;
2792@itemx @var{symbol} *= @var{expression} ;
2793@itemx @var{symbol} /= @var{expression} ;
2794@itemx @var{symbol} <<= @var{expression} ;
2795@itemx @var{symbol} >>= @var{expression} ;
2796@itemx @var{symbol} &= @var{expression} ;
2797@itemx @var{symbol} |= @var{expression} ;
2798@end table
2799
2800The first case will define @var{symbol} to the value of
2801@var{expression}.  In the other cases, @var{symbol} must already be
2802defined, and the value will be adjusted accordingly.
2803
2804The special symbol name @samp{.} indicates the location counter.  You
2805may only use this within a @code{SECTIONS} command.  @xref{Location Counter}.
2806
2807The semicolon after @var{expression} is required.
2808
2809Expressions are defined below; see @ref{Expressions}.
2810
2811You may write symbol assignments as commands in their own right, or as
2812statements within a @code{SECTIONS} command, or as part of an output
2813section description in a @code{SECTIONS} command.
2814
2815The section of the symbol will be set from the section of the
2816expression; for more information, see @ref{Expression Section}.
2817
2818Here is an example showing the three different places that symbol
2819assignments may be used:
2820
2821@smallexample
2822floating_point = 0;
2823SECTIONS
2824@{
2825  .text :
2826    @{
2827      *(.text)
2828      _etext = .;
2829    @}
2830  _bdata = (. + 3) & ~ 3;
2831  .data : @{ *(.data) @}
2832@}
2833@end smallexample
2834@noindent
2835In this example, the symbol @samp{floating_point} will be defined as
2836zero.  The symbol @samp{_etext} will be defined as the address following
2837the last @samp{.text} input section.  The symbol @samp{_bdata} will be
2838defined as the address following the @samp{.text} output section aligned
2839upward to a 4 byte boundary.
2840
2841@node PROVIDE
2842@subsection PROVIDE
2843@cindex PROVIDE
2844In some cases, it is desirable for a linker script to define a symbol
2845only if it is referenced and is not defined by any object included in
2846the link.  For example, traditional linkers defined the symbol
2847@samp{etext}.  However, ANSI C requires that the user be able to use
2848@samp{etext} as a function name without encountering an error.  The
2849@code{PROVIDE} keyword may be used to define a symbol, such as
2850@samp{etext}, only if it is referenced but not defined.  The syntax is
2851@code{PROVIDE(@var{symbol} = @var{expression})}.
2852
2853Here is an example of using @code{PROVIDE} to define @samp{etext}:
2854@smallexample
2855SECTIONS
2856@{
2857  .text :
2858    @{
2859      *(.text)
2860      _etext = .;
2861      PROVIDE(etext = .);
2862    @}
2863@}
2864@end smallexample
2865
2866In this example, if the program defines @samp{_etext} (with a leading
2867underscore), the linker will give a multiple definition error.  If, on
2868the other hand, the program defines @samp{etext} (with no leading
2869underscore), the linker will silently use the definition in the program.
2870If the program references @samp{etext} but does not define it, the
2871linker will use the definition in the linker script.
2872
2873@node Source Code Reference
2874@subsection Source Code Reference
2875
2876Accessing a linker script defined variable from source code is not
2877intuitive.  In particular a linker script symbol is not equivalent to
2878a variable declaration in a high level language, it is instead a
2879symbol that does not have a value.
2880
2881Before going further, it is important to note that compilers often
2882transform names in the source code into different names when they are
2883stored in the symbol table.  For example, Fortran compilers commonly
2884prepend or append an underscore, and C++ performs extensive @samp{name
2885mangling}.  Therefore there might be a discrepancy between the name
2886of a variable as it is used in source code and the name of the same
2887variable as it is defined in a linker script.  For example in C a
2888linker script variable might be referred to as:
2889
2890@smallexample
2891  extern int foo;
2892@end smallexample
2893
2894But in the linker script it might be defined as:
2895
2896@smallexample
2897  _foo = 1000;
2898@end smallexample
2899
2900In the remaining examples however it is assumed that no name
2901transformation has taken place.
2902
2903When a symbol is declared in a high level language such as C, two
2904things happen.  The first is that the compiler reserves enough space
2905in the program's memory to hold the @emph{value} of the symbol.  The
2906second is that the compiler creates an entry in the program's symbol
2907table which holds the symbol's @emph{address}.  ie the symbol table
2908contains the address of the block of memory holding the symbol's
2909value.  So for example the following C declaration, at file scope:
2910
2911@smallexample
2912  int foo = 1000;
2913@end smallexample
2914
2915creates a entry called @samp{foo} in the symbol table.  This entry
2916holds the address of an @samp{int} sized block of memory where the
2917number 1000 is initially stored.
2918
2919When a program references a symbol the compiler generates code that
2920first accesses the symbol table to find the address of the symbol's
2921memory block and then code to read the value from that memory block.
2922So:
2923
2924@smallexample
2925  foo = 1;
2926@end smallexample
2927
2928looks up the symbol @samp{foo} in the symbol table, gets the address
2929associated with this symbol and then writes the value 1 into that
2930address.  Whereas:
2931
2932@smallexample
2933  int * a = & foo;
2934@end smallexample
2935
2936looks up the symbol @samp{foo} in the symbol table, gets it address
2937and then copies this address into the block of memory associated with
2938the variable @samp{a}.
2939
2940Linker scripts symbol declarations, by contrast, create an entry in
2941the symbol table but do not assign any memory to them.  Thus they are
2942an address without a value.  So for example the linker script definition:
2943
2944@smallexample
2945  foo = 1000;
2946@end smallexample
2947
2948creates an entry in the symbol table called @samp{foo} which holds
2949the address of memory location 1000, but nothing special is stored at
2950address 1000.  This means that you cannot access the @emph{value} of a
2951linker script defined symbol - it has no value - all you can do is
2952access the @emph{address} of a linker script defined symbol.
2953
2954Hence when you are using a linker script defined symbol in source code
2955you should always take the address of the symbol, and never attempt to
2956use its value.  For example suppose you want to copy the contents of a
2957section of memory called .ROM into a section called .FLASH and the
2958linker script contains these declarations:
2959
2960@smallexample
2961@group
2962  start_of_ROM   = .ROM;
2963  end_of_ROM     = .ROM + sizeof (.ROM) - 1;
2964  start_of_FLASH = .FLASH;
2965@end group
2966@end smallexample
2967
2968Then the C source code to perform the copy would be:
2969
2970@smallexample
2971@group
2972  extern char start_of_ROM, end_of_ROM, start_of_FLASH;
2973
2974  memcpy (& start_of_FLASH, & start_of_ROM, & end_of_ROM - & start_of_ROM);
2975@end group
2976@end smallexample
2977
2978Note the use of the @samp{&} operators.  These are correct.
2979
2980@node SECTIONS
2981@section SECTIONS Command
2982@kindex SECTIONS
2983The @code{SECTIONS} command tells the linker how to map input sections
2984into output sections, and how to place the output sections in memory.
2985
2986The format of the @code{SECTIONS} command is:
2987@smallexample
2988SECTIONS
2989@{
2990  @var{sections-command}
2991  @var{sections-command}
2992  @dots{}
2993@}
2994@end smallexample
2995
2996Each @var{sections-command} may of be one of the following:
2997
2998@itemize @bullet
2999@item
3000an @code{ENTRY} command (@pxref{Entry Point,,Entry command})
3001@item
3002a symbol assignment (@pxref{Assignments})
3003@item
3004an output section description
3005@item
3006an overlay description
3007@end itemize
3008
3009The @code{ENTRY} command and symbol assignments are permitted inside the
3010@code{SECTIONS} command for convenience in using the location counter in
3011those commands.  This can also make the linker script easier to
3012understand because you can use those commands at meaningful points in
3013the layout of the output file.
3014
3015Output section descriptions and overlay descriptions are described
3016below.
3017
3018If you do not use a @code{SECTIONS} command in your linker script, the
3019linker will place each input section into an identically named output
3020section in the order that the sections are first encountered in the
3021input files.  If all input sections are present in the first file, for
3022example, the order of sections in the output file will match the order
3023in the first input file.  The first section will be at address zero.
3024
3025@menu
3026* Output Section Description::	Output section description
3027* Output Section Name::		Output section name
3028* Output Section Address::	Output section address
3029* Input Section::		Input section description
3030* Output Section Data::		Output section data
3031* Output Section Keywords::	Output section keywords
3032* Output Section Discarding::	Output section discarding
3033* Output Section Attributes::	Output section attributes
3034* Overlay Description::		Overlay description
3035@end menu
3036
3037@node Output Section Description
3038@subsection Output Section Description
3039The full description of an output section looks like this:
3040@smallexample
3041@group
3042@var{section} [@var{address}] [(@var{type})] :
3043  [AT(@var{lma})] [SUBALIGN(@var{subsection_align})]
3044  @{
3045    @var{output-section-command}
3046    @var{output-section-command}
3047    @dots{}
3048  @} [>@var{region}] [AT>@var{lma_region}] [:@var{phdr} :@var{phdr} @dots{}] [=@var{fillexp}]
3049@end group
3050@end smallexample
3051
3052Most output sections do not use most of the optional section attributes.
3053
3054The whitespace around @var{section} is required, so that the section
3055name is unambiguous.  The colon and the curly braces are also required.
3056The line breaks and other white space are optional.
3057
3058Each @var{output-section-command} may be one of the following:
3059
3060@itemize @bullet
3061@item
3062a symbol assignment (@pxref{Assignments})
3063@item
3064an input section description (@pxref{Input Section})
3065@item
3066data values to include directly (@pxref{Output Section Data})
3067@item
3068a special output section keyword (@pxref{Output Section Keywords})
3069@end itemize
3070
3071@node Output Section Name
3072@subsection Output Section Name
3073@cindex name, section
3074@cindex section name
3075The name of the output section is @var{section}.  @var{section} must
3076meet the constraints of your output format.  In formats which only
3077support a limited number of sections, such as @code{a.out}, the name
3078must be one of the names supported by the format (@code{a.out}, for
3079example, allows only @samp{.text}, @samp{.data} or @samp{.bss}). If the
3080output format supports any number of sections, but with numbers and not
3081names (as is the case for Oasys), the name should be supplied as a
3082quoted numeric string.  A section name may consist of any sequence of
3083characters, but a name which contains any unusual characters such as
3084commas must be quoted.
3085
3086The output section name @samp{/DISCARD/} is special; @ref{Output Section
3087Discarding}.
3088
3089@node Output Section Address
3090@subsection Output Section Address
3091@cindex address, section
3092@cindex section address
3093The @var{address} is an expression for the VMA (the virtual memory
3094address) of the output section.  If you do not provide @var{address},
3095the linker will set it based on @var{region} if present, or otherwise
3096based on the current value of the location counter.
3097
3098If you provide @var{address}, the address of the output section will be
3099set to precisely that.  If you provide neither @var{address} nor
3100@var{region}, then the address of the output section will be set to the
3101current value of the location counter aligned to the alignment
3102requirements of the output section.  The alignment requirement of the
3103output section is the strictest alignment of any input section contained
3104within the output section.
3105
3106For example,
3107@smallexample
3108.text . : @{ *(.text) @}
3109@end smallexample
3110@noindent
3111and
3112@smallexample
3113.text : @{ *(.text) @}
3114@end smallexample
3115@noindent
3116are subtly different.  The first will set the address of the
3117@samp{.text} output section to the current value of the location
3118counter.  The second will set it to the current value of the location
3119counter aligned to the strictest alignment of a @samp{.text} input
3120section.
3121
3122The @var{address} may be an arbitrary expression; @ref{Expressions}.
3123For example, if you want to align the section on a 0x10 byte boundary,
3124so that the lowest four bits of the section address are zero, you could
3125do something like this:
3126@smallexample
3127.text ALIGN(0x10) : @{ *(.text) @}
3128@end smallexample
3129@noindent
3130This works because @code{ALIGN} returns the current location counter
3131aligned upward to the specified value.
3132
3133Specifying @var{address} for a section will change the value of the
3134location counter.
3135
3136@node Input Section
3137@subsection Input Section Description
3138@cindex input sections
3139@cindex mapping input sections to output sections
3140The most common output section command is an input section description.
3141
3142The input section description is the most basic linker script operation.
3143You use output sections to tell the linker how to lay out your program
3144in memory.  You use input section descriptions to tell the linker how to
3145map the input files into your memory layout.
3146
3147@menu
3148* Input Section Basics::	Input section basics
3149* Input Section Wildcards::	Input section wildcard patterns
3150* Input Section Common::	Input section for common symbols
3151* Input Section Keep::		Input section and garbage collection
3152* Input Section Example::	Input section example
3153@end menu
3154
3155@node Input Section Basics
3156@subsubsection Input Section Basics
3157@cindex input section basics
3158An input section description consists of a file name optionally followed
3159by a list of section names in parentheses.
3160
3161The file name and the section name may be wildcard patterns, which we
3162describe further below (@pxref{Input Section Wildcards}).
3163
3164The most common input section description is to include all input
3165sections with a particular name in the output section.  For example, to
3166include all input @samp{.text} sections, you would write:
3167@smallexample
3168*(.text)
3169@end smallexample
3170@noindent
3171Here the @samp{*} is a wildcard which matches any file name.  To exclude a list
3172of files from matching the file name wildcard, EXCLUDE_FILE may be used to
3173match all files except the ones specified in the EXCLUDE_FILE list.  For
3174example:
3175@smallexample
3176(*(EXCLUDE_FILE (*crtend.o *otherfile.o) .ctors))
3177@end smallexample
3178will cause all .ctors sections from all files except @file{crtend.o} and
3179@file{otherfile.o} to be included.
3180
3181There are two ways to include more than one section:
3182@smallexample
3183*(.text .rdata)
3184*(.text) *(.rdata)
3185@end smallexample
3186@noindent
3187The difference between these is the order in which the @samp{.text} and
3188@samp{.rdata} input sections will appear in the output section.  In the
3189first example, they will be intermingled, appearing in the same order as
3190they are found in the linker input.  In the second example, all
3191@samp{.text} input sections will appear first, followed by all
3192@samp{.rdata} input sections.
3193
3194You can specify a file name to include sections from a particular file.
3195You would do this if one or more of your files contain special data that
3196needs to be at a particular location in memory.  For example:
3197@smallexample
3198data.o(.data)
3199@end smallexample
3200
3201If you use a file name without a list of sections, then all sections in
3202the input file will be included in the output section.  This is not
3203commonly done, but it may by useful on occasion.  For example:
3204@smallexample
3205data.o
3206@end smallexample
3207
3208When you use a file name which does not contain any wild card
3209characters, the linker will first see if you also specified the file
3210name on the linker command line or in an @code{INPUT} command.  If you
3211did not, the linker will attempt to open the file as an input file, as
3212though it appeared on the command line.  Note that this differs from an
3213@code{INPUT} command, because the linker will not search for the file in
3214the archive search path.
3215
3216@node Input Section Wildcards
3217@subsubsection Input Section Wildcard Patterns
3218@cindex input section wildcards
3219@cindex wildcard file name patterns
3220@cindex file name wildcard patterns
3221@cindex section name wildcard patterns
3222In an input section description, either the file name or the section
3223name or both may be wildcard patterns.
3224
3225The file name of @samp{*} seen in many examples is a simple wildcard
3226pattern for the file name.
3227
3228The wildcard patterns are like those used by the Unix shell.
3229
3230@table @samp
3231@item *
3232matches any number of characters
3233@item ?
3234matches any single character
3235@item [@var{chars}]
3236matches a single instance of any of the @var{chars}; the @samp{-}
3237character may be used to specify a range of characters, as in
3238@samp{[a-z]} to match any lower case letter
3239@item \
3240quotes the following character
3241@end table
3242
3243When a file name is matched with a wildcard, the wildcard characters
3244will not match a @samp{/} character (used to separate directory names on
3245Unix).  A pattern consisting of a single @samp{*} character is an
3246exception; it will always match any file name, whether it contains a
3247@samp{/} or not.  In a section name, the wildcard characters will match
3248a @samp{/} character.
3249
3250File name wildcard patterns only match files which are explicitly
3251specified on the command line or in an @code{INPUT} command.  The linker
3252does not search directories to expand wildcards.
3253
3254If a file name matches more than one wildcard pattern, or if a file name
3255appears explicitly and is also matched by a wildcard pattern, the linker
3256will use the first match in the linker script.  For example, this
3257sequence of input section descriptions is probably in error, because the
3258@file{data.o} rule will not be used:
3259@smallexample
3260.data : @{ *(.data) @}
3261.data1 : @{ data.o(.data) @}
3262@end smallexample
3263
3264@cindex SORT_BY_NAME
3265Normally, the linker will place files and sections matched by wildcards
3266in the order in which they are seen during the link.  You can change
3267this by using the @code{SORT_BY_NAME} keyword, which appears before a wildcard
3268pattern in parentheses (e.g., @code{SORT_BY_NAME(.text*)}).  When the
3269@code{SORT_BY_NAME} keyword is used, the linker will sort the files or sections
3270into ascending order by name before placing them in the output file.
3271
3272@cindex SORT_BY_ALIGNMENT
3273@code{SORT_BY_ALIGNMENT} is very similar to @code{SORT_BY_NAME}. The
3274difference is @code{SORT_BY_ALIGNMENT} will sort sections into
3275ascending order by alignment before placing them in the output file.
3276
3277@cindex SORT
3278@code{SORT} is an alias for @code{SORT_BY_NAME}.
3279
3280When there are nested section sorting commands in linker script, there
3281can be at most 1 level of nesting for section sorting commands.
3282
3283@enumerate
3284@item
3285@code{SORT_BY_NAME} (@code{SORT_BY_ALIGNMENT} (wildcard section pattern)).
3286It will sort the input sections by name first, then by alignment if 2
3287sections have the same name.
3288@item
3289@code{SORT_BY_ALIGNMENT} (@code{SORT_BY_NAME} (wildcard section pattern)).
3290It will sort the input sections by alignment first, then by name if 2
3291sections have the same alignment.
3292@item
3293@code{SORT_BY_NAME} (@code{SORT_BY_NAME} (wildcard section pattern)) is
3294treated the same as @code{SORT_BY_NAME} (wildcard section pattern).
3295@item
3296@code{SORT_BY_ALIGNMENT} (@code{SORT_BY_ALIGNMENT} (wildcard section pattern))
3297is treated the same as @code{SORT_BY_ALIGNMENT} (wildcard section pattern).
3298@item
3299All other nested section sorting commands are invalid.
3300@end enumerate
3301
3302When both command line section sorting option and linker script
3303section sorting command are used, section sorting command always
3304takes precedence over the command line option.
3305
3306If the section sorting command in linker script isn't nested, the
3307command line option will make the section sorting command to be
3308treated as nested sorting command.
3309
3310@enumerate
3311@item
3312@code{SORT_BY_NAME} (wildcard section pattern ) with
3313@option{--sort-sections alignment} is equivalent to
3314@code{SORT_BY_NAME} (@code{SORT_BY_ALIGNMENT} (wildcard section pattern)).
3315@item
3316@code{SORT_BY_ALIGNMENT} (wildcard section pattern) with
3317@option{--sort-section name} is equivalent to
3318@code{SORT_BY_ALIGNMENT} (@code{SORT_BY_NAME} (wildcard section pattern)).
3319@end enumerate
3320
3321If the section sorting command in linker script is nested, the
3322command line option will be ignored.
3323
3324If you ever get confused about where input sections are going, use the
3325@samp{-M} linker option to generate a map file.  The map file shows
3326precisely how input sections are mapped to output sections.
3327
3328This example shows how wildcard patterns might be used to partition
3329files.  This linker script directs the linker to place all @samp{.text}
3330sections in @samp{.text} and all @samp{.bss} sections in @samp{.bss}.
3331The linker will place the @samp{.data} section from all files beginning
3332with an upper case character in @samp{.DATA}; for all other files, the
3333linker will place the @samp{.data} section in @samp{.data}.
3334@smallexample
3335@group
3336SECTIONS @{
3337  .text : @{ *(.text) @}
3338  .DATA : @{ [A-Z]*(.data) @}
3339  .data : @{ *(.data) @}
3340  .bss : @{ *(.bss) @}
3341@}
3342@end group
3343@end smallexample
3344
3345@node Input Section Common
3346@subsubsection Input Section for Common Symbols
3347@cindex common symbol placement
3348@cindex uninitialized data placement
3349A special notation is needed for common symbols, because in many object
3350file formats common symbols do not have a particular input section.  The
3351linker treats common symbols as though they are in an input section
3352named @samp{COMMON}.
3353
3354You may use file names with the @samp{COMMON} section just as with any
3355other input sections.  You can use this to place common symbols from a
3356particular input file in one section while common symbols from other
3357input files are placed in another section.
3358
3359In most cases, common symbols in input files will be placed in the
3360@samp{.bss} section in the output file.  For example:
3361@smallexample
3362.bss @{ *(.bss) *(COMMON) @}
3363@end smallexample
3364
3365@cindex scommon section
3366@cindex small common symbols
3367Some object file formats have more than one type of common symbol.  For
3368example, the MIPS ELF object file format distinguishes standard common
3369symbols and small common symbols.  In this case, the linker will use a
3370different special section name for other types of common symbols.  In
3371the case of MIPS ELF, the linker uses @samp{COMMON} for standard common
3372symbols and @samp{.scommon} for small common symbols.  This permits you
3373to map the different types of common symbols into memory at different
3374locations.
3375
3376@cindex [COMMON]
3377You will sometimes see @samp{[COMMON]} in old linker scripts.  This
3378notation is now considered obsolete.  It is equivalent to
3379@samp{*(COMMON)}.
3380
3381@node Input Section Keep
3382@subsubsection Input Section and Garbage Collection
3383@cindex KEEP
3384@cindex garbage collection
3385When link-time garbage collection is in use (@samp{--gc-sections}),
3386it is often useful to mark sections that should not be eliminated.
3387This is accomplished by surrounding an input section's wildcard entry
3388with @code{KEEP()}, as in @code{KEEP(*(.init))} or
3389@code{KEEP(SORT_BY_NAME(*)(.ctors))}.
3390
3391@node Input Section Example
3392@subsubsection Input Section Example
3393The following example is a complete linker script.  It tells the linker
3394to read all of the sections from file @file{all.o} and place them at the
3395start of output section @samp{outputa} which starts at location
3396@samp{0x10000}.  All of section @samp{.input1} from file @file{foo.o}
3397follows immediately, in the same output section.  All of section
3398@samp{.input2} from @file{foo.o} goes into output section
3399@samp{outputb}, followed by section @samp{.input1} from @file{foo1.o}.
3400All of the remaining @samp{.input1} and @samp{.input2} sections from any
3401files are written to output section @samp{outputc}.
3402
3403@smallexample
3404@group
3405SECTIONS @{
3406  outputa 0x10000 :
3407    @{
3408    all.o
3409    foo.o (.input1)
3410    @}
3411@end group
3412@group
3413  outputb :
3414    @{
3415    foo.o (.input2)
3416    foo1.o (.input1)
3417    @}
3418@end group
3419@group
3420  outputc :
3421    @{
3422    *(.input1)
3423    *(.input2)
3424    @}
3425@}
3426@end group
3427@end smallexample
3428
3429@node Output Section Data
3430@subsection Output Section Data
3431@cindex data
3432@cindex section data
3433@cindex output section data
3434@kindex BYTE(@var{expression})
3435@kindex SHORT(@var{expression})
3436@kindex LONG(@var{expression})
3437@kindex QUAD(@var{expression})
3438@kindex SQUAD(@var{expression})
3439You can include explicit bytes of data in an output section by using
3440@code{BYTE}, @code{SHORT}, @code{LONG}, @code{QUAD}, or @code{SQUAD} as
3441an output section command.  Each keyword is followed by an expression in
3442parentheses providing the value to store (@pxref{Expressions}).  The
3443value of the expression is stored at the current value of the location
3444counter.
3445
3446The @code{BYTE}, @code{SHORT}, @code{LONG}, and @code{QUAD} commands
3447store one, two, four, and eight bytes (respectively).  After storing the
3448bytes, the location counter is incremented by the number of bytes
3449stored.
3450
3451For example, this will store the byte 1 followed by the four byte value
3452of the symbol @samp{addr}:
3453@smallexample
3454BYTE(1)
3455LONG(addr)
3456@end smallexample
3457
3458When using a 64 bit host or target, @code{QUAD} and @code{SQUAD} are the
3459same; they both store an 8 byte, or 64 bit, value.  When both host and
3460target are 32 bits, an expression is computed as 32 bits.  In this case
3461@code{QUAD} stores a 32 bit value zero extended to 64 bits, and
3462@code{SQUAD} stores a 32 bit value sign extended to 64 bits.
3463
3464If the object file format of the output file has an explicit endianness,
3465which is the normal case, the value will be stored in that endianness.
3466When the object file format does not have an explicit endianness, as is
3467true of, for example, S-records, the value will be stored in the
3468endianness of the first input object file.
3469
3470Note---these commands only work inside a section description and not
3471between them, so the following will produce an error from the linker:
3472@smallexample
3473SECTIONS @{@ .text : @{@ *(.text) @}@ LONG(1) .data : @{@ *(.data) @}@ @}@
3474@end smallexample
3475whereas this will work:
3476@smallexample
3477SECTIONS @{@ .text : @{@ *(.text) ; LONG(1) @}@ .data : @{@ *(.data) @}@ @}@
3478@end smallexample
3479
3480@kindex FILL(@var{expression})
3481@cindex holes, filling
3482@cindex unspecified memory
3483You may use the @code{FILL} command to set the fill pattern for the
3484current section.  It is followed by an expression in parentheses.  Any
3485otherwise unspecified regions of memory within the section (for example,
3486gaps left due to the required alignment of input sections) are filled
3487with the value of the expression, repeated as
3488necessary.  A @code{FILL} statement covers memory locations after the
3489point at which it occurs in the section definition; by including more
3490than one @code{FILL} statement, you can have different fill patterns in
3491different parts of an output section.
3492
3493This example shows how to fill unspecified regions of memory with the
3494value @samp{0x90}:
3495@smallexample
3496FILL(0x90909090)
3497@end smallexample
3498
3499The @code{FILL} command is similar to the @samp{=@var{fillexp}} output
3500section attribute, but it only affects the
3501part of the section following the @code{FILL} command, rather than the
3502entire section.  If both are used, the @code{FILL} command takes
3503precedence.  @xref{Output Section Fill}, for details on the fill
3504expression.
3505
3506@node Output Section Keywords
3507@subsection Output Section Keywords
3508There are a couple of keywords which can appear as output section
3509commands.
3510
3511@table @code
3512@kindex CREATE_OBJECT_SYMBOLS
3513@cindex input filename symbols
3514@cindex filename symbols
3515@item CREATE_OBJECT_SYMBOLS
3516The command tells the linker to create a symbol for each input file.
3517The name of each symbol will be the name of the corresponding input
3518file.  The section of each symbol will be the output section in which
3519the @code{CREATE_OBJECT_SYMBOLS} command appears.
3520
3521This is conventional for the a.out object file format.  It is not
3522normally used for any other object file format.
3523
3524@kindex CONSTRUCTORS
3525@cindex C++ constructors, arranging in link
3526@cindex constructors, arranging in link
3527@item CONSTRUCTORS
3528When linking using the a.out object file format, the linker uses an
3529unusual set construct to support C++ global constructors and
3530destructors.  When linking object file formats which do not support
3531arbitrary sections, such as ECOFF and XCOFF, the linker will
3532automatically recognize C++ global constructors and destructors by name.
3533For these object file formats, the @code{CONSTRUCTORS} command tells the
3534linker to place constructor information in the output section where the
3535@code{CONSTRUCTORS} command appears.  The @code{CONSTRUCTORS} command is
3536ignored for other object file formats.
3537
3538The symbol @w{@code{__CTOR_LIST__}} marks the start of the global
3539constructors, and the symbol @w{@code{__CTOR_END__}} marks the end.
3540Similarly, @w{@code{__DTOR_LIST__}} and @w{@code{__DTOR_END__}} mark
3541the start and end of the global destructors.  The
3542first word in the list is the number of entries, followed by the address
3543of each constructor or destructor, followed by a zero word.  The
3544compiler must arrange to actually run the code.  For these object file
3545formats @sc{gnu} C++ normally calls constructors from a subroutine
3546@code{__main}; a call to @code{__main} is automatically inserted into
3547the startup code for @code{main}.  @sc{gnu} C++ normally runs
3548destructors either by using @code{atexit}, or directly from the function
3549@code{exit}.
3550
3551For object file formats such as @code{COFF} or @code{ELF} which support
3552arbitrary section names, @sc{gnu} C++ will normally arrange to put the
3553addresses of global constructors and destructors into the @code{.ctors}
3554and @code{.dtors} sections.  Placing the following sequence into your
3555linker script will build the sort of table which the @sc{gnu} C++
3556runtime code expects to see.
3557
3558@smallexample
3559      __CTOR_LIST__ = .;
3560      LONG((__CTOR_END__ - __CTOR_LIST__) / 4 - 2)
3561      *(.ctors)
3562      LONG(0)
3563      __CTOR_END__ = .;
3564      __DTOR_LIST__ = .;
3565      LONG((__DTOR_END__ - __DTOR_LIST__) / 4 - 2)
3566      *(.dtors)
3567      LONG(0)
3568      __DTOR_END__ = .;
3569@end smallexample
3570
3571If you are using the @sc{gnu} C++ support for initialization priority,
3572which provides some control over the order in which global constructors
3573are run, you must sort the constructors at link time to ensure that they
3574are executed in the correct order.  When using the @code{CONSTRUCTORS}
3575command, use @samp{SORT_BY_NAME(CONSTRUCTORS)} instead.  When using the
3576@code{.ctors} and @code{.dtors} sections, use @samp{*(SORT_BY_NAME(.ctors))} and
3577@samp{*(SORT_BY_NAME(.dtors))} instead of just @samp{*(.ctors)} and
3578@samp{*(.dtors)}.
3579
3580Normally the compiler and linker will handle these issues automatically,
3581and you will not need to concern yourself with them.  However, you may
3582need to consider this if you are using C++ and writing your own linker
3583scripts.
3584
3585@end table
3586
3587@node Output Section Discarding
3588@subsection Output Section Discarding
3589@cindex discarding sections
3590@cindex sections, discarding
3591@cindex removing sections
3592The linker will not create output section which do not have any
3593contents.  This is for convenience when referring to input sections that
3594may or may not be present in any of the input files.  For example:
3595@smallexample
3596.foo @{ *(.foo) @}
3597@end smallexample
3598@noindent
3599will only create a @samp{.foo} section in the output file if there is a
3600@samp{.foo} section in at least one input file.
3601
3602If you use anything other than an input section description as an output
3603section command, such as a symbol assignment, then the output section
3604will always be created, even if there are no matching input sections.
3605
3606@cindex /DISCARD/
3607The special output section name @samp{/DISCARD/} may be used to discard
3608input sections.  Any input sections which are assigned to an output
3609section named @samp{/DISCARD/} are not included in the output file.
3610
3611@node Output Section Attributes
3612@subsection Output Section Attributes
3613@cindex output section attributes
3614We showed above that the full description of an output section looked
3615like this:
3616@smallexample
3617@group
3618@var{section} [@var{address}] [(@var{type})] :
3619  [AT(@var{lma})] [SUBALIGN(@var{subsection_align})]
3620  @{
3621    @var{output-section-command}
3622    @var{output-section-command}
3623    @dots{}
3624  @} [>@var{region}] [AT>@var{lma_region}] [:@var{phdr} :@var{phdr} @dots{}] [=@var{fillexp}]
3625@end group
3626@end smallexample
3627We've already described @var{section}, @var{address}, and
3628@var{output-section-command}.  In this section we will describe the
3629remaining section attributes.
3630
3631@menu
3632* Output Section Type::		Output section type
3633* Output Section LMA::		Output section LMA
3634* Forced Input Alignment::	Forced Input Alignment
3635* Output Section Region::	Output section region
3636* Output Section Phdr::		Output section phdr
3637* Output Section Fill::		Output section fill
3638@end menu
3639
3640@node Output Section Type
3641@subsubsection Output Section Type
3642Each output section may have a type.  The type is a keyword in
3643parentheses.  The following types are defined:
3644
3645@table @code
3646@item NOLOAD
3647The section should be marked as not loadable, so that it will not be
3648loaded into memory when the program is run.
3649@item DSECT
3650@itemx COPY
3651@itemx INFO
3652@itemx OVERLAY
3653These type names are supported for backward compatibility, and are
3654rarely used.  They all have the same effect: the section should be
3655marked as not allocatable, so that no memory is allocated for the
3656section when the program is run.
3657@end table
3658
3659@kindex NOLOAD
3660@cindex prevent unnecessary loading
3661@cindex loading, preventing
3662The linker normally sets the attributes of an output section based on
3663the input sections which map into it.  You can override this by using
3664the section type.  For example, in the script sample below, the
3665@samp{ROM} section is addressed at memory location @samp{0} and does not
3666need to be loaded when the program is run.  The contents of the
3667@samp{ROM} section will appear in the linker output file as usual.
3668@smallexample
3669@group
3670SECTIONS @{
3671  ROM 0 (NOLOAD) : @{ @dots{} @}
3672  @dots{}
3673@}
3674@end group
3675@end smallexample
3676
3677@node Output Section LMA
3678@subsubsection Output Section LMA
3679@kindex AT>@var{lma_region}
3680@kindex AT(@var{lma})
3681@cindex load address
3682@cindex section load address
3683Every section has a virtual address (VMA) and a load address (LMA); see
3684@ref{Basic Script Concepts}.  The address expression which may appear in
3685an output section description sets the VMA (@pxref{Output Section
3686Address}).
3687
3688The linker will normally set the LMA equal to the VMA.  You can change
3689that by using the @code{AT} keyword.  The expression @var{lma} that
3690follows the @code{AT} keyword specifies the load address of the
3691section.
3692
3693Alternatively, with @samp{AT>@var{lma_region}} expression, you may
3694specify a memory region for the section's load address. @xref{MEMORY}.
3695Note that if the section has not had a VMA assigned to it then the
3696linker will use the @var{lma_region} as the VMA region as well.
3697@xref{Output Section Region}.
3698
3699@cindex ROM initialized data
3700@cindex initialized data in ROM
3701This feature is designed to make it easy to build a ROM image.  For
3702example, the following linker script creates three output sections: one
3703called @samp{.text}, which starts at @code{0x1000}, one called
3704@samp{.mdata}, which is loaded at the end of the @samp{.text} section
3705even though its VMA is @code{0x2000}, and one called @samp{.bss} to hold
3706uninitialized data at address @code{0x3000}.  The symbol @code{_data} is
3707defined with the value @code{0x2000}, which shows that the location
3708counter holds the VMA value, not the LMA value.
3709
3710@smallexample
3711@group
3712SECTIONS
3713  @{
3714  .text 0x1000 : @{ *(.text) _etext = . ; @}
3715  .mdata 0x2000 :
3716    AT ( ADDR (.text) + SIZEOF (.text) )
3717    @{ _data = . ; *(.data); _edata = . ;  @}
3718  .bss 0x3000 :
3719    @{ _bstart = . ;  *(.bss) *(COMMON) ; _bend = . ;@}
3720@}
3721@end group
3722@end smallexample
3723
3724The run-time initialization code for use with a program generated with
3725this linker script would include something like the following, to copy
3726the initialized data from the ROM image to its runtime address.  Notice
3727how this code takes advantage of the symbols defined by the linker
3728script.
3729
3730@smallexample
3731@group
3732extern char _etext, _data, _edata, _bstart, _bend;
3733char *src = &_etext;
3734char *dst = &_data;
3735
3736/* ROM has data at end of text; copy it. */
3737while (dst < &_edata) @{
3738  *dst++ = *src++;
3739@}
3740
3741/* Zero bss */
3742for (dst = &_bstart; dst< &_bend; dst++)
3743  *dst = 0;
3744@end group
3745@end smallexample
3746
3747@node Forced Input Alignment
3748@subsubsection Forced Input Alignment
3749@kindex SUBALIGN(@var{subsection_align})
3750@cindex forcing input section alignment
3751@cindex input section alignment
3752You can force input section alignment within an output section by using
3753SUBALIGN.  The value specified overrides any alignment given by input
3754sections, whether larger or smaller.
3755
3756@node Output Section Region
3757@subsubsection Output Section Region
3758@kindex >@var{region}
3759@cindex section, assigning to memory region
3760@cindex memory regions and sections
3761You can assign a section to a previously defined region of memory by
3762using @samp{>@var{region}}.  @xref{MEMORY}.
3763
3764Here is a simple example:
3765@smallexample
3766@group
3767MEMORY @{ rom : ORIGIN = 0x1000, LENGTH = 0x1000 @}
3768SECTIONS @{ ROM : @{ *(.text) @} >rom @}
3769@end group
3770@end smallexample
3771
3772@node Output Section Phdr
3773@subsubsection Output Section Phdr
3774@kindex :@var{phdr}
3775@cindex section, assigning to program header
3776@cindex program headers and sections
3777You can assign a section to a previously defined program segment by
3778using @samp{:@var{phdr}}.  @xref{PHDRS}.  If a section is assigned to
3779one or more segments, then all subsequent allocated sections will be
3780assigned to those segments as well, unless they use an explicitly
3781@code{:@var{phdr}} modifier.  You can use @code{:NONE} to tell the
3782linker to not put the section in any segment at all.
3783
3784Here is a simple example:
3785@smallexample
3786@group
3787PHDRS @{ text PT_LOAD ; @}
3788SECTIONS @{ .text : @{ *(.text) @} :text @}
3789@end group
3790@end smallexample
3791
3792@node Output Section Fill
3793@subsubsection Output Section Fill
3794@kindex =@var{fillexp}
3795@cindex section fill pattern
3796@cindex fill pattern, entire section
3797You can set the fill pattern for an entire section by using
3798@samp{=@var{fillexp}}.  @var{fillexp} is an expression
3799(@pxref{Expressions}).  Any otherwise unspecified regions of memory
3800within the output section (for example, gaps left due to the required
3801alignment of input sections) will be filled with the value, repeated as
3802necessary.  If the fill expression is a simple hex number, ie. a string
3803of hex digit starting with @samp{0x} and without a trailing @samp{k} or @samp{M}, then
3804an arbitrarily long sequence of hex digits can be used to specify the
3805fill pattern;  Leading zeros become part of the pattern too.  For all
3806other cases, including extra parentheses or a unary @code{+}, the fill
3807pattern is the four least significant bytes of the value of the
3808expression.  In all cases, the number is big-endian.
3809
3810You can also change the fill value with a @code{FILL} command in the
3811output section commands; (@pxref{Output Section Data}).
3812
3813Here is a simple example:
3814@smallexample
3815@group
3816SECTIONS @{ .text : @{ *(.text) @} =0x90909090 @}
3817@end group
3818@end smallexample
3819
3820@node Overlay Description
3821@subsection Overlay Description
3822@kindex OVERLAY
3823@cindex overlays
3824An overlay description provides an easy way to describe sections which
3825are to be loaded as part of a single memory image but are to be run at
3826the same memory address.  At run time, some sort of overlay manager will
3827copy the overlaid sections in and out of the runtime memory address as
3828required, perhaps by simply manipulating addressing bits.  This approach
3829can be useful, for example, when a certain region of memory is faster
3830than another.
3831
3832Overlays are described using the @code{OVERLAY} command.  The
3833@code{OVERLAY} command is used within a @code{SECTIONS} command, like an
3834output section description.  The full syntax of the @code{OVERLAY}
3835command is as follows:
3836@smallexample
3837@group
3838OVERLAY [@var{start}] : [NOCROSSREFS] [AT ( @var{ldaddr} )]
3839  @{
3840    @var{secname1}
3841      @{
3842        @var{output-section-command}
3843        @var{output-section-command}
3844        @dots{}
3845      @} [:@var{phdr}@dots{}] [=@var{fill}]
3846    @var{secname2}
3847      @{
3848        @var{output-section-command}
3849        @var{output-section-command}
3850        @dots{}
3851      @} [:@var{phdr}@dots{}] [=@var{fill}]
3852    @dots{}
3853  @} [>@var{region}] [:@var{phdr}@dots{}] [=@var{fill}]
3854@end group
3855@end smallexample
3856
3857Everything is optional except @code{OVERLAY} (a keyword), and each
3858section must have a name (@var{secname1} and @var{secname2} above).  The
3859section definitions within the @code{OVERLAY} construct are identical to
3860those within the general @code{SECTIONS} contruct (@pxref{SECTIONS}),
3861except that no addresses and no memory regions may be defined for
3862sections within an @code{OVERLAY}.
3863
3864The sections are all defined with the same starting address.  The load
3865addresses of the sections are arranged such that they are consecutive in
3866memory starting at the load address used for the @code{OVERLAY} as a
3867whole (as with normal section definitions, the load address is optional,
3868and defaults to the start address; the start address is also optional,
3869and defaults to the current value of the location counter).
3870
3871If the @code{NOCROSSREFS} keyword is used, and there any references
3872among the sections, the linker will report an error.  Since the sections
3873all run at the same address, it normally does not make sense for one
3874section to refer directly to another.  @xref{Miscellaneous Commands,
3875NOCROSSREFS}.
3876
3877For each section within the @code{OVERLAY}, the linker automatically
3878defines two symbols.  The symbol @code{__load_start_@var{secname}} is
3879defined as the starting load address of the section.  The symbol
3880@code{__load_stop_@var{secname}} is defined as the final load address of
3881the section.  Any characters within @var{secname} which are not legal
3882within C identifiers are removed.  C (or assembler) code may use these
3883symbols to move the overlaid sections around as necessary.
3884
3885At the end of the overlay, the value of the location counter is set to
3886the start address of the overlay plus the size of the largest section.
3887
3888Here is an example.  Remember that this would appear inside a
3889@code{SECTIONS} construct.
3890@smallexample
3891@group
3892  OVERLAY 0x1000 : AT (0x4000)
3893   @{
3894     .text0 @{ o1/*.o(.text) @}
3895     .text1 @{ o2/*.o(.text) @}
3896   @}
3897@end group
3898@end smallexample
3899@noindent
3900This will define both @samp{.text0} and @samp{.text1} to start at
3901address 0x1000.  @samp{.text0} will be loaded at address 0x4000, and
3902@samp{.text1} will be loaded immediately after @samp{.text0}.  The
3903following symbols will be defined: @code{__load_start_text0},
3904@code{__load_stop_text0}, @code{__load_start_text1},
3905@code{__load_stop_text1}.
3906
3907C code to copy overlay @code{.text1} into the overlay area might look
3908like the following.
3909
3910@smallexample
3911@group
3912  extern char __load_start_text1, __load_stop_text1;
3913  memcpy ((char *) 0x1000, &__load_start_text1,
3914          &__load_stop_text1 - &__load_start_text1);
3915@end group
3916@end smallexample
3917
3918Note that the @code{OVERLAY} command is just syntactic sugar, since
3919everything it does can be done using the more basic commands.  The above
3920example could have been written identically as follows.
3921
3922@smallexample
3923@group
3924  .text0 0x1000 : AT (0x4000) @{ o1/*.o(.text) @}
3925  __load_start_text0 = LOADADDR (.text0);
3926  __load_stop_text0 = LOADADDR (.text0) + SIZEOF (.text0);
3927  .text1 0x1000 : AT (0x4000 + SIZEOF (.text0)) @{ o2/*.o(.text) @}
3928  __load_start_text1 = LOADADDR (.text1);
3929  __load_stop_text1 = LOADADDR (.text1) + SIZEOF (.text1);
3930  . = 0x1000 + MAX (SIZEOF (.text0), SIZEOF (.text1));
3931@end group
3932@end smallexample
3933
3934@node MEMORY
3935@section MEMORY Command
3936@kindex MEMORY
3937@cindex memory regions
3938@cindex regions of memory
3939@cindex allocating memory
3940@cindex discontinuous memory
3941The linker's default configuration permits allocation of all available
3942memory.  You can override this by using the @code{MEMORY} command.
3943
3944The @code{MEMORY} command describes the location and size of blocks of
3945memory in the target.  You can use it to describe which memory regions
3946may be used by the linker, and which memory regions it must avoid.  You
3947can then assign sections to particular memory regions.  The linker will
3948set section addresses based on the memory regions, and will warn about
3949regions that become too full.  The linker will not shuffle sections
3950around to fit into the available regions.
3951
3952A linker script may contain at most one use of the @code{MEMORY}
3953command.  However, you can define as many blocks of memory within it as
3954you wish.  The syntax is:
3955@smallexample
3956@group
3957MEMORY
3958  @{
3959    @var{name} [(@var{attr})] : ORIGIN = @var{origin}, LENGTH = @var{len}
3960    @dots{}
3961  @}
3962@end group
3963@end smallexample
3964
3965The @var{name} is a name used in the linker script to refer to the
3966region.  The region name has no meaning outside of the linker script.
3967Region names are stored in a separate name space, and will not conflict
3968with symbol names, file names, or section names.  Each memory region
3969must have a distinct name.
3970
3971@cindex memory region attributes
3972The @var{attr} string is an optional list of attributes that specify
3973whether to use a particular memory region for an input section which is
3974not explicitly mapped in the linker script.  As described in
3975@ref{SECTIONS}, if you do not specify an output section for some input
3976section, the linker will create an output section with the same name as
3977the input section.  If you define region attributes, the linker will use
3978them to select the memory region for the output section that it creates.
3979
3980The @var{attr} string must consist only of the following characters:
3981@table @samp
3982@item R
3983Read-only section
3984@item W
3985Read/write section
3986@item X
3987Executable section
3988@item A
3989Allocatable section
3990@item I
3991Initialized section
3992@item L
3993Same as @samp{I}
3994@item !
3995Invert the sense of any of the preceding attributes
3996@end table
3997
3998If a unmapped section matches any of the listed attributes other than
3999@samp{!}, it will be placed in the memory region.  The @samp{!}
4000attribute reverses this test, so that an unmapped section will be placed
4001in the memory region only if it does not match any of the listed
4002attributes.
4003
4004@kindex ORIGIN =
4005@kindex o =
4006@kindex org =
4007The @var{origin} is an numerical expression for the start address of
4008the memory region.  The expression must evaluate to a constant and it
4009cannot involve any symbols.  The keyword @code{ORIGIN} may be
4010abbreviated to @code{org} or @code{o} (but not, for example,
4011@code{ORG}).
4012
4013@kindex LENGTH =
4014@kindex len =
4015@kindex l =
4016The @var{len} is an expression for the size in bytes of the memory
4017region.  As with the @var{origin} expression, the expression must
4018be numerical only and must evaluate to a constant.  The keyword
4019@code{LENGTH} may be abbreviated to @code{len} or @code{l}.
4020
4021In the following example, we specify that there are two memory regions
4022available for allocation: one starting at @samp{0} for 256 kilobytes,
4023and the other starting at @samp{0x40000000} for four megabytes.  The
4024linker will place into the @samp{rom} memory region every section which
4025is not explicitly mapped into a memory region, and is either read-only
4026or executable.  The linker will place other sections which are not
4027explicitly mapped into a memory region into the @samp{ram} memory
4028region.
4029
4030@smallexample
4031@group
4032MEMORY
4033  @{
4034    rom (rx)  : ORIGIN = 0, LENGTH = 256K
4035    ram (!rx) : org = 0x40000000, l = 4M
4036  @}
4037@end group
4038@end smallexample
4039
4040Once you define a memory region, you can direct the linker to place
4041specific output sections into that memory region by using the
4042@samp{>@var{region}} output section attribute.  For example, if you have
4043a memory region named @samp{mem}, you would use @samp{>mem} in the
4044output section definition.  @xref{Output Section Region}.  If no address
4045was specified for the output section, the linker will set the address to
4046the next available address within the memory region.  If the combined
4047output sections directed to a memory region are too large for the
4048region, the linker will issue an error message.
4049
4050It is possible to access the origin and length of a memory in an
4051expression via the @code{ORIGIN(@var{memory})} and
4052@code{LENGTH(@var{memory})} functions:
4053
4054@smallexample
4055@group
4056  _fstack = ORIGIN(ram) + LENGTH(ram) - 4;
4057@end group
4058@end smallexample
4059
4060@node PHDRS
4061@section PHDRS Command
4062@kindex PHDRS
4063@cindex program headers
4064@cindex ELF program headers
4065@cindex program segments
4066@cindex segments, ELF
4067The ELF object file format uses @dfn{program headers}, also knows as
4068@dfn{segments}.  The program headers describe how the program should be
4069loaded into memory.  You can print them out by using the @code{objdump}
4070program with the @samp{-p} option.
4071
4072When you run an ELF program on a native ELF system, the system loader
4073reads the program headers in order to figure out how to load the
4074program.  This will only work if the program headers are set correctly.
4075This manual does not describe the details of how the system loader
4076interprets program headers; for more information, see the ELF ABI.
4077
4078The linker will create reasonable program headers by default.  However,
4079in some cases, you may need to specify the program headers more
4080precisely.  You may use the @code{PHDRS} command for this purpose.  When
4081the linker sees the @code{PHDRS} command in the linker script, it will
4082not create any program headers other than the ones specified.
4083
4084The linker only pays attention to the @code{PHDRS} command when
4085generating an ELF output file.  In other cases, the linker will simply
4086ignore @code{PHDRS}.
4087
4088This is the syntax of the @code{PHDRS} command.  The words @code{PHDRS},
4089@code{FILEHDR}, @code{AT}, and @code{FLAGS} are keywords.
4090
4091@smallexample
4092@group
4093PHDRS
4094@{
4095  @var{name} @var{type} [ FILEHDR ] [ PHDRS ] [ AT ( @var{address} ) ]
4096        [ FLAGS ( @var{flags} ) ] ;
4097@}
4098@end group
4099@end smallexample
4100
4101The @var{name} is used only for reference in the @code{SECTIONS} command
4102of the linker script.  It is not put into the output file.  Program
4103header names are stored in a separate name space, and will not conflict
4104with symbol names, file names, or section names.  Each program header
4105must have a distinct name.
4106
4107Certain program header types describe segments of memory which the
4108system loader will load from the file.  In the linker script, you
4109specify the contents of these segments by placing allocatable output
4110sections in the segments.  You use the @samp{:@var{phdr}} output section
4111attribute to place a section in a particular segment.  @xref{Output
4112Section Phdr}.
4113
4114It is normal to put certain sections in more than one segment.  This
4115merely implies that one segment of memory contains another.  You may
4116repeat @samp{:@var{phdr}}, using it once for each segment which should
4117contain the section.
4118
4119If you place a section in one or more segments using @samp{:@var{phdr}},
4120then the linker will place all subsequent allocatable sections which do
4121not specify @samp{:@var{phdr}} in the same segments.  This is for
4122convenience, since generally a whole set of contiguous sections will be
4123placed in a single segment.  You can use @code{:NONE} to override the
4124default segment and tell the linker to not put the section in any
4125segment at all.
4126
4127@kindex FILEHDR
4128@kindex PHDRS
4129You may use the @code{FILEHDR} and @code{PHDRS} keywords appear after
4130the program header type to further describe the contents of the segment.
4131The @code{FILEHDR} keyword means that the segment should include the ELF
4132file header.  The @code{PHDRS} keyword means that the segment should
4133include the ELF program headers themselves.
4134
4135The @var{type} may be one of the following.  The numbers indicate the
4136value of the keyword.
4137
4138@table @asis
4139@item @code{PT_NULL} (0)
4140Indicates an unused program header.
4141
4142@item @code{PT_LOAD} (1)
4143Indicates that this program header describes a segment to be loaded from
4144the file.
4145
4146@item @code{PT_DYNAMIC} (2)
4147Indicates a segment where dynamic linking information can be found.
4148
4149@item @code{PT_INTERP} (3)
4150Indicates a segment where the name of the program interpreter may be
4151found.
4152
4153@item @code{PT_NOTE} (4)
4154Indicates a segment holding note information.
4155
4156@item @code{PT_SHLIB} (5)
4157A reserved program header type, defined but not specified by the ELF
4158ABI.
4159
4160@item @code{PT_PHDR} (6)
4161Indicates a segment where the program headers may be found.
4162
4163@item @var{expression}
4164An expression giving the numeric type of the program header.  This may
4165be used for types not defined above.
4166@end table
4167
4168You can specify that a segment should be loaded at a particular address
4169in memory by using an @code{AT} expression.  This is identical to the
4170@code{AT} command used as an output section attribute (@pxref{Output
4171Section LMA}).  The @code{AT} command for a program header overrides the
4172output section attribute.
4173
4174The linker will normally set the segment flags based on the sections
4175which comprise the segment.  You may use the @code{FLAGS} keyword to
4176explicitly specify the segment flags.  The value of @var{flags} must be
4177an integer.  It is used to set the @code{p_flags} field of the program
4178header.
4179
4180Here is an example of @code{PHDRS}.  This shows a typical set of program
4181headers used on a native ELF system.
4182
4183@example
4184@group
4185PHDRS
4186@{
4187  headers PT_PHDR PHDRS ;
4188  interp PT_INTERP ;
4189  text PT_LOAD FILEHDR PHDRS ;
4190  data PT_LOAD ;
4191  dynamic PT_DYNAMIC ;
4192@}
4193
4194SECTIONS
4195@{
4196  . = SIZEOF_HEADERS;
4197  .interp : @{ *(.interp) @} :text :interp
4198  .text : @{ *(.text) @} :text
4199  .rodata : @{ *(.rodata) @} /* defaults to :text */
4200  @dots{}
4201  . = . + 0x1000; /* move to a new page in memory */
4202  .data : @{ *(.data) @} :data
4203  .dynamic : @{ *(.dynamic) @} :data :dynamic
4204  @dots{}
4205@}
4206@end group
4207@end example
4208
4209@node VERSION
4210@section VERSION Command
4211@kindex VERSION @{script text@}
4212@cindex symbol versions
4213@cindex version script
4214@cindex versions of symbols
4215The linker supports symbol versions when using ELF.  Symbol versions are
4216only useful when using shared libraries.  The dynamic linker can use
4217symbol versions to select a specific version of a function when it runs
4218a program that may have been linked against an earlier version of the
4219shared library.
4220
4221You can include a version script directly in the main linker script, or
4222you can supply the version script as an implicit linker script.  You can
4223also use the @samp{--version-script} linker option.
4224
4225The syntax of the @code{VERSION} command is simply
4226@smallexample
4227VERSION @{ version-script-commands @}
4228@end smallexample
4229
4230The format of the version script commands is identical to that used by
4231Sun's linker in Solaris 2.5.  The version script defines a tree of
4232version nodes.  You specify the node names and interdependencies in the
4233version script.  You can specify which symbols are bound to which
4234version nodes, and you can reduce a specified set of symbols to local
4235scope so that they are not globally visible outside of the shared
4236library.
4237
4238The easiest way to demonstrate the version script language is with a few
4239examples.
4240
4241@smallexample
4242VERS_1.1 @{
4243	 global:
4244		 foo1;
4245	 local:
4246		 old*;
4247		 original*;
4248		 new*;
4249@};
4250
4251VERS_1.2 @{
4252		 foo2;
4253@} VERS_1.1;
4254
4255VERS_2.0 @{
4256		 bar1; bar2;
4257@} VERS_1.2;
4258@end smallexample
4259
4260This example version script defines three version nodes.  The first
4261version node defined is @samp{VERS_1.1}; it has no other dependencies.
4262The script binds the symbol @samp{foo1} to @samp{VERS_1.1}.  It reduces
4263a number of symbols to local scope so that they are not visible outside
4264of the shared library; this is done using wildcard patterns, so that any
4265symbol whose name begins with @samp{old}, @samp{original}, or @samp{new}
4266is matched.  The wildcard patterns available are the same as those used
4267in the shell when matching filenames (also known as ``globbing'').
4268
4269Next, the version script defines node @samp{VERS_1.2}.  This node
4270depends upon @samp{VERS_1.1}.  The script binds the symbol @samp{foo2}
4271to the version node @samp{VERS_1.2}.
4272
4273Finally, the version script defines node @samp{VERS_2.0}.  This node
4274depends upon @samp{VERS_1.2}.  The scripts binds the symbols @samp{bar1}
4275and @samp{bar2} are bound to the version node @samp{VERS_2.0}.
4276
4277When the linker finds a symbol defined in a library which is not
4278specifically bound to a version node, it will effectively bind it to an
4279unspecified base version of the library.  You can bind all otherwise
4280unspecified symbols to a given version node by using @samp{global: *;}
4281somewhere in the version script.
4282
4283The names of the version nodes have no specific meaning other than what
4284they might suggest to the person reading them.  The @samp{2.0} version
4285could just as well have appeared in between @samp{1.1} and @samp{1.2}.
4286However, this would be a confusing way to write a version script.
4287
4288Node name can be omited, provided it is the only version node
4289in the version script.  Such version script doesn't assign any versions to
4290symbols, only selects which symbols will be globally visible out and which
4291won't.
4292
4293@smallexample
4294@{ global: foo; bar; local: *; @};
4295@end smallexample
4296
4297When you link an application against a shared library that has versioned
4298symbols, the application itself knows which version of each symbol it
4299requires, and it also knows which version nodes it needs from each
4300shared library it is linked against.  Thus at runtime, the dynamic
4301loader can make a quick check to make sure that the libraries you have
4302linked against do in fact supply all of the version nodes that the
4303application will need to resolve all of the dynamic symbols.  In this
4304way it is possible for the dynamic linker to know with certainty that
4305all external symbols that it needs will be resolvable without having to
4306search for each symbol reference.
4307
4308The symbol versioning is in effect a much more sophisticated way of
4309doing minor version checking that SunOS does.  The fundamental problem
4310that is being addressed here is that typically references to external
4311functions are bound on an as-needed basis, and are not all bound when
4312the application starts up.  If a shared library is out of date, a
4313required interface may be missing; when the application tries to use
4314that interface, it may suddenly and unexpectedly fail.  With symbol
4315versioning, the user will get a warning when they start their program if
4316the libraries being used with the application are too old.
4317
4318There are several GNU extensions to Sun's versioning approach.  The
4319first of these is the ability to bind a symbol to a version node in the
4320source file where the symbol is defined instead of in the versioning
4321script.  This was done mainly to reduce the burden on the library
4322maintainer.  You can do this by putting something like:
4323@smallexample
4324__asm__(".symver original_foo,foo@@VERS_1.1");
4325@end smallexample
4326@noindent
4327in the C source file.  This renames the function @samp{original_foo} to
4328be an alias for @samp{foo} bound to the version node @samp{VERS_1.1}.
4329The @samp{local:} directive can be used to prevent the symbol
4330@samp{original_foo} from being exported. A @samp{.symver} directive
4331takes precedence over a version script.
4332
4333The second GNU extension is to allow multiple versions of the same
4334function to appear in a given shared library.  In this way you can make
4335an incompatible change to an interface without increasing the major
4336version number of the shared library, while still allowing applications
4337linked against the old interface to continue to function.
4338
4339To do this, you must use multiple @samp{.symver} directives in the
4340source file.  Here is an example:
4341
4342@smallexample
4343__asm__(".symver original_foo,foo@@");
4344__asm__(".symver old_foo,foo@@VERS_1.1");
4345__asm__(".symver old_foo1,foo@@VERS_1.2");
4346__asm__(".symver new_foo,foo@@@@VERS_2.0");
4347@end smallexample
4348
4349In this example, @samp{foo@@} represents the symbol @samp{foo} bound to the
4350unspecified base version of the symbol.  The source file that contains this
4351example would define 4 C functions: @samp{original_foo}, @samp{old_foo},
4352@samp{old_foo1}, and @samp{new_foo}.
4353
4354When you have multiple definitions of a given symbol, there needs to be
4355some way to specify a default version to which external references to
4356this symbol will be bound.  You can do this with the
4357@samp{foo@@@@VERS_2.0} type of @samp{.symver} directive.  You can only
4358declare one version of a symbol as the default in this manner; otherwise
4359you would effectively have multiple definitions of the same symbol.
4360
4361If you wish to bind a reference to a specific version of the symbol
4362within the shared library, you can use the aliases of convenience
4363(i.e., @samp{old_foo}), or you can use the @samp{.symver} directive to
4364specifically bind to an external version of the function in question.
4365
4366You can also specify the language in the version script:
4367
4368@smallexample
4369VERSION extern "lang" @{ version-script-commands @}
4370@end smallexample
4371
4372The supported @samp{lang}s are @samp{C}, @samp{C++}, and @samp{Java}.
4373The linker will iterate over the list of symbols at the link time and
4374demangle them according to @samp{lang} before matching them to the
4375patterns specified in @samp{version-script-commands}.
4376
4377@node Expressions
4378@section Expressions in Linker Scripts
4379@cindex expressions
4380@cindex arithmetic
4381The syntax for expressions in the linker script language is identical to
4382that of C expressions.  All expressions are evaluated as integers.  All
4383expressions are evaluated in the same size, which is 32 bits if both the
4384host and target are 32 bits, and is otherwise 64 bits.
4385
4386You can use and set symbol values in expressions.
4387
4388The linker defines several special purpose builtin functions for use in
4389expressions.
4390
4391@menu
4392* Constants::			Constants
4393* Symbols::			Symbol Names
4394* Location Counter::		The Location Counter
4395* Operators::			Operators
4396* Evaluation::			Evaluation
4397* Expression Section::		The Section of an Expression
4398* Builtin Functions::		Builtin Functions
4399@end menu
4400
4401@node Constants
4402@subsection Constants
4403@cindex integer notation
4404@cindex constants in linker scripts
4405All constants are integers.
4406
4407As in C, the linker considers an integer beginning with @samp{0} to be
4408octal, and an integer beginning with @samp{0x} or @samp{0X} to be
4409hexadecimal.  The linker considers other integers to be decimal.
4410
4411@cindex scaled integers
4412@cindex K and M integer suffixes
4413@cindex M and K integer suffixes
4414@cindex suffixes for integers
4415@cindex integer suffixes
4416In addition, you can use the suffixes @code{K} and @code{M} to scale a
4417constant by
4418@c TEXI2ROFF-KILL
4419@ifnottex
4420@c END TEXI2ROFF-KILL
4421@code{1024} or @code{1024*1024}
4422@c TEXI2ROFF-KILL
4423@end ifnottex
4424@tex
4425${\rm 1024}$ or ${\rm 1024}^2$
4426@end tex
4427@c END TEXI2ROFF-KILL
4428respectively. For example, the following all refer to the same quantity:
4429@smallexample
4430_fourk_1 = 4K;
4431_fourk_2 = 4096;
4432_fourk_3 = 0x1000;
4433@end smallexample
4434
4435@node Symbols
4436@subsection Symbol Names
4437@cindex symbol names
4438@cindex names
4439@cindex quoted symbol names
4440@kindex "
4441Unless quoted, symbol names start with a letter, underscore, or period
4442and may include letters, digits, underscores, periods, and hyphens.
4443Unquoted symbol names must not conflict with any keywords.  You can
4444specify a symbol which contains odd characters or has the same name as a
4445keyword by surrounding the symbol name in double quotes:
4446@smallexample
4447"SECTION" = 9;
4448"with a space" = "also with a space" + 10;
4449@end smallexample
4450
4451Since symbols can contain many non-alphabetic characters, it is safest
4452to delimit symbols with spaces.  For example, @samp{A-B} is one symbol,
4453whereas @samp{A - B} is an expression involving subtraction.
4454
4455@node Location Counter
4456@subsection The Location Counter
4457@kindex .
4458@cindex dot
4459@cindex location counter
4460@cindex current output location
4461The special linker variable @dfn{dot} @samp{.} always contains the
4462current output location counter.  Since the @code{.} always refers to a
4463location in an output section, it may only appear in an expression
4464within a @code{SECTIONS} command.  The @code{.} symbol may appear
4465anywhere that an ordinary symbol is allowed in an expression.
4466
4467@cindex holes
4468Assigning a value to @code{.} will cause the location counter to be
4469moved.  This may be used to create holes in the output section.  The
4470location counter may never be moved backwards.
4471
4472@smallexample
4473SECTIONS
4474@{
4475  output :
4476    @{
4477      file1(.text)
4478      . = . + 1000;
4479      file2(.text)
4480      . += 1000;
4481      file3(.text)
4482    @} = 0x12345678;
4483@}
4484@end smallexample
4485@noindent
4486In the previous example, the @samp{.text} section from @file{file1} is
4487located at the beginning of the output section @samp{output}.  It is
4488followed by a 1000 byte gap.  Then the @samp{.text} section from
4489@file{file2} appears, also with a 1000 byte gap following before the
4490@samp{.text} section from @file{file3}.  The notation @samp{= 0x12345678}
4491specifies what data to write in the gaps (@pxref{Output Section Fill}).
4492
4493@cindex dot inside sections
4494Note: @code{.} actually refers to the byte offset from the start of the
4495current containing object.  Normally this is the @code{SECTIONS}
4496statement, whose start address is 0, hence @code{.} can be used as an
4497absolute address.  If @code{.} is used inside a section description
4498however, it refers to the byte offset from the start of that section,
4499not an absolute address.  Thus in a script like this:
4500
4501@smallexample
4502SECTIONS
4503@{
4504    . = 0x100
4505    .text: @{
4506      *(.text)
4507      . = 0x200
4508    @}
4509    . = 0x500
4510    .data: @{
4511      *(.data)
4512      . += 0x600
4513    @}
4514@}
4515@end smallexample
4516
4517The @samp{.text} section will be assigned a starting address of 0x100
4518and a size of exactly 0x200 bytes, even if there is not enough data in
4519the @samp{.text} input sections to fill this area.  (If there is too
4520much data, an error will be produced because this would be an attempt to
4521move @code{.} backwards).  The @samp{.data} section will start at 0x500
4522and it will have an extra 0x600 bytes worth of space after the end of
4523the values from the @samp{.data} input sections and before the end of
4524the @samp{.data} output section itself.
4525
4526@cindex dot outside sections
4527Setting symbols to the value of the location counter outside of an
4528output section statement can result in unexpected values if the linker
4529needs to place orphan sections.  For example, given the following:
4530
4531@smallexample
4532SECTIONS
4533@{
4534    start_of_text = . ;
4535    .text: @{ *(.text) @}
4536    end_of_text = . ;
4537
4538    start_of_data = . ;
4539    .data: @{ *(.data) @}
4540    end_of_data = . ;
4541@}
4542@end smallexample
4543
4544If the linker needs to place some input section, e.g. @code{.rodata},
4545not mentioned in the script, it might choose to place that section
4546between @code{.text} and @code{.data}.  You might think the linker
4547should place @code{.rodata} on the blank line in the above script, but
4548blank lines are of no particular significance to the linker.  As well,
4549the linker doesn't associate the above symbol names with their
4550sections.  Instead, it assumes that all assignments or other
4551statements belong to the previous output section, except for the
4552special case of an assignment to @code{.}.  I.e., the linker will
4553place the orphan @code{.rodata} section as if the script was written
4554as follows:
4555
4556@smallexample
4557SECTIONS
4558@{
4559    start_of_text = . ;
4560    .text: @{ *(.text) @}
4561    end_of_text = . ;
4562
4563    start_of_data = . ;
4564    .rodata: @{ *(.rodata) @}
4565    .data: @{ *(.data) @}
4566    end_of_data = . ;
4567@}
4568@end smallexample
4569
4570This may or may not be the script author's intention for the value of
4571@code{start_of_data}.  One way to influence the orphan section
4572placement is to assign the location counter to itself, as the linker
4573assumes that an assignment to @code{.} is setting the start address of
4574a following output section and thus should be grouped with that
4575section.  So you could write:
4576
4577@smallexample
4578SECTIONS
4579@{
4580    start_of_text = . ;
4581    .text: @{ *(.text) @}
4582    end_of_text = . ;
4583
4584    . = . ;
4585    start_of_data = . ;
4586    .data: @{ *(.data) @}
4587    end_of_data = . ;
4588@}
4589@end smallexample
4590
4591Now, the orphan @code{.rodata} section will be placed between
4592@code{end_of_text} and @code{start_of_data}.
4593
4594@need 2000
4595@node Operators
4596@subsection Operators
4597@cindex operators for arithmetic
4598@cindex arithmetic operators
4599@cindex precedence in expressions
4600The linker recognizes the standard C set of arithmetic operators, with
4601the standard bindings and precedence levels:
4602@c TEXI2ROFF-KILL
4603@ifnottex
4604@c END TEXI2ROFF-KILL
4605@smallexample
4606precedence      associativity   Operators                Notes
4607(highest)
46081               left            !  -  ~                  (1)
46092               left            *  /  %
46103               left            +  -
46114               left            >>  <<
46125               left            ==  !=  >  <  <=  >=
46136               left            &
46147               left            |
46158               left            &&
46169               left            ||
461710              right           ? :
461811              right           &=  +=  -=  *=  /=       (2)
4619(lowest)
4620@end smallexample
4621Notes:
4622(1) Prefix operators
4623(2) @xref{Assignments}.
4624@c TEXI2ROFF-KILL
4625@end ifnottex
4626@tex
4627\vskip \baselineskip
4628%"lispnarrowing" is the extra indent used generally for smallexample
4629\hskip\lispnarrowing\vbox{\offinterlineskip
4630\hrule
4631\halign
4632{\vrule#&\strut\hfil\ #\ \hfil&\vrule#&\strut\hfil\ #\ \hfil&\vrule#&\strut\hfil\ {\tt #}\ \hfil&\vrule#\cr
4633height2pt&\omit&&\omit&&\omit&\cr
4634&Precedence&&  Associativity  &&{\rm Operators}&\cr
4635height2pt&\omit&&\omit&&\omit&\cr
4636\noalign{\hrule}
4637height2pt&\omit&&\omit&&\omit&\cr
4638&highest&&&&&\cr
4639% '176 is tilde, '~' in tt font
4640&1&&left&&\qquad-          \char'176\      !\qquad\dag&\cr
4641&2&&left&&*          /        \%&\cr
4642&3&&left&&+          -&\cr
4643&4&&left&&>>         <<&\cr
4644&5&&left&&==         !=       >      <      <=      >=&\cr
4645&6&&left&&\&&\cr
4646&7&&left&&|&\cr
4647&8&&left&&{\&\&}&\cr
4648&9&&left&&||&\cr
4649&10&&right&&?        :&\cr
4650&11&&right&&\qquad\&=      +=       -=     *=     /=\qquad\ddag&\cr
4651&lowest&&&&&\cr
4652height2pt&\omit&&\omit&&\omit&\cr}
4653\hrule}
4654@end tex
4655@iftex
4656{
4657@obeylines@parskip=0pt@parindent=0pt
4658@dag@quad Prefix operators.
4659@ddag@quad @xref{Assignments}.
4660}
4661@end iftex
4662@c END TEXI2ROFF-KILL
4663
4664@node Evaluation
4665@subsection Evaluation
4666@cindex lazy evaluation
4667@cindex expression evaluation order
4668The linker evaluates expressions lazily.  It only computes the value of
4669an expression when absolutely necessary.
4670
4671The linker needs some information, such as the value of the start
4672address of the first section, and the origins and lengths of memory
4673regions, in order to do any linking at all.  These values are computed
4674as soon as possible when the linker reads in the linker script.
4675
4676However, other values (such as symbol values) are not known or needed
4677until after storage allocation.  Such values are evaluated later, when
4678other information (such as the sizes of output sections) is available
4679for use in the symbol assignment expression.
4680
4681The sizes of sections cannot be known until after allocation, so
4682assignments dependent upon these are not performed until after
4683allocation.
4684
4685Some expressions, such as those depending upon the location counter
4686@samp{.}, must be evaluated during section allocation.
4687
4688If the result of an expression is required, but the value is not
4689available, then an error results.  For example, a script like the
4690following
4691@smallexample
4692@group
4693SECTIONS
4694  @{
4695    .text 9+this_isnt_constant :
4696      @{ *(.text) @}
4697  @}
4698@end group
4699@end smallexample
4700@noindent
4701will cause the error message @samp{non constant expression for initial
4702address}.
4703
4704@node Expression Section
4705@subsection The Section of an Expression
4706@cindex expression sections
4707@cindex absolute expressions
4708@cindex relative expressions
4709@cindex absolute and relocatable symbols
4710@cindex relocatable and absolute symbols
4711@cindex symbols, relocatable and absolute
4712When the linker evaluates an expression, the result is either absolute
4713or relative to some section.  A relative expression is expressed as a
4714fixed offset from the base of a section.
4715
4716The position of the expression within the linker script determines
4717whether it is absolute or relative.  An expression which appears within
4718an output section definition is relative to the base of the output
4719section.  An expression which appears elsewhere will be absolute.
4720
4721A symbol set to a relative expression will be relocatable if you request
4722relocatable output using the @samp{-r} option.  That means that a
4723further link operation may change the value of the symbol.  The symbol's
4724section will be the section of the relative expression.
4725
4726A symbol set to an absolute expression will retain the same value
4727through any further link operation.  The symbol will be absolute, and
4728will not have any particular associated section.
4729
4730You can use the builtin function @code{ABSOLUTE} to force an expression
4731to be absolute when it would otherwise be relative.  For example, to
4732create an absolute symbol set to the address of the end of the output
4733section @samp{.data}:
4734@smallexample
4735SECTIONS
4736  @{
4737    .data : @{ *(.data) _edata = ABSOLUTE(.); @}
4738  @}
4739@end smallexample
4740@noindent
4741If @samp{ABSOLUTE} were not used, @samp{_edata} would be relative to the
4742@samp{.data} section.
4743
4744@node Builtin Functions
4745@subsection Builtin Functions
4746@cindex functions in expressions
4747The linker script language includes a number of builtin functions for
4748use in linker script expressions.
4749
4750@table @code
4751@item ABSOLUTE(@var{exp})
4752@kindex ABSOLUTE(@var{exp})
4753@cindex expression, absolute
4754Return the absolute (non-relocatable, as opposed to non-negative) value
4755of the expression @var{exp}.  Primarily useful to assign an absolute
4756value to a symbol within a section definition, where symbol values are
4757normally section relative.  @xref{Expression Section}.
4758
4759@item ADDR(@var{section})
4760@kindex ADDR(@var{section})
4761@cindex section address in expression
4762Return the absolute address (the VMA) of the named @var{section}.  Your
4763script must previously have defined the location of that section.  In
4764the following example, @code{symbol_1} and @code{symbol_2} are assigned
4765identical values:
4766@smallexample
4767@group
4768SECTIONS @{ @dots{}
4769  .output1 :
4770    @{
4771    start_of_output_1 = ABSOLUTE(.);
4772    @dots{}
4773    @}
4774  .output :
4775    @{
4776    symbol_1 = ADDR(.output1);
4777    symbol_2 = start_of_output_1;
4778    @}
4779@dots{} @}
4780@end group
4781@end smallexample
4782
4783@item ALIGN(@var{align})
4784@itemx ALIGN(@var{exp},@var{align})
4785@kindex ALIGN(@var{align})
4786@kindex ALIGN(@var{exp},@var{align})
4787@cindex round up location counter
4788@cindex align location counter
4789@cindex round up expression
4790@cindex align expression
4791Return the location counter (@code{.}) or arbitrary expression aligned
4792to the next @var{align} boundary.  The single operand @code{ALIGN}
4793doesn't change the value of the location counter---it just does
4794arithmetic on it.  The two operand @code{ALIGN} allows an arbitrary
4795expression to be aligned upwards (@code{ALIGN(@var{align})} is
4796equivalent to @code{ALIGN(., @var{align})}).
4797
4798Here is an example which aligns the output @code{.data} section to the
4799next @code{0x2000} byte boundary after the preceding section and sets a
4800variable within the section to the next @code{0x8000} boundary after the
4801input sections:
4802@smallexample
4803@group
4804SECTIONS @{ @dots{}
4805  .data ALIGN(0x2000): @{
4806    *(.data)
4807    variable = ALIGN(0x8000);
4808  @}
4809@dots{} @}
4810@end group
4811@end smallexample
4812@noindent
4813The first use of @code{ALIGN} in this example specifies the location of
4814a section because it is used as the optional @var{address} attribute of
4815a section definition (@pxref{Output Section Address}).  The second use
4816of @code{ALIGN} is used to defines the value of a symbol.
4817
4818The builtin function @code{NEXT} is closely related to @code{ALIGN}.
4819
4820@item BLOCK(@var{exp})
4821@kindex BLOCK(@var{exp})
4822This is a synonym for @code{ALIGN}, for compatibility with older linker
4823scripts.  It is most often seen when setting the address of an output
4824section.
4825
4826@item DATA_SEGMENT_ALIGN(@var{maxpagesize}, @var{commonpagesize})
4827@kindex DATA_SEGMENT_ALIGN(@var{maxpagesize}, @var{commonpagesize})
4828This is equivalent to either
4829@smallexample
4830(ALIGN(@var{maxpagesize}) + (. & (@var{maxpagesize} - 1)))
4831@end smallexample
4832or
4833@smallexample
4834(ALIGN(@var{maxpagesize}) + (. & (@var{maxpagesize} - @var{commonpagesize})))
4835@end smallexample
4836@noindent
4837depending on whether the latter uses fewer @var{commonpagesize} sized pages
4838for the data segment (area between the result of this expression and
4839@code{DATA_SEGMENT_END}) than the former or not.
4840If the latter form is used, it means @var{commonpagesize} bytes of runtime
4841memory will be saved at the expense of up to @var{commonpagesize} wasted
4842bytes in the on-disk file.
4843
4844This expression can only be used directly in @code{SECTIONS} commands, not in
4845any output section descriptions and only once in the linker script.
4846@var{commonpagesize} should be less or equal to @var{maxpagesize} and should
4847be the system page size the object wants to be optimized for (while still
4848working on system page sizes up to @var{maxpagesize}).
4849
4850@noindent
4851Example:
4852@smallexample
4853  . = DATA_SEGMENT_ALIGN(0x10000, 0x2000);
4854@end smallexample
4855
4856@item DATA_SEGMENT_END(@var{exp})
4857@kindex DATA_SEGMENT_END(@var{exp})
4858This defines the end of data segment for @code{DATA_SEGMENT_ALIGN}
4859evaluation purposes.
4860
4861@smallexample
4862  . = DATA_SEGMENT_END(.);
4863@end smallexample
4864
4865@item DATA_SEGMENT_RELRO_END(@var{offset}, @var{exp})
4866@kindex DATA_SEGMENT_RELRO_END(@var{offset}, @var{exp})
4867This defines the end of the @code{PT_GNU_RELRO} segment when
4868@samp{-z relro} option is used.  Second argument is returned.
4869When @samp{-z relro} option is not present, @code{DATA_SEGMENT_RELRO_END}
4870does nothing, otherwise @code{DATA_SEGMENT_ALIGN} is padded so that
4871@var{exp} + @var{offset} is aligned to the most commonly used page
4872boundary for particular target.  If present in the linker script,
4873it must always come in between @code{DATA_SEGMENT_ALIGN} and
4874@code{DATA_SEGMENT_END}.
4875
4876@smallexample
4877  . = DATA_SEGMENT_RELRO_END(24, .);
4878@end smallexample
4879
4880@item DEFINED(@var{symbol})
4881@kindex DEFINED(@var{symbol})
4882@cindex symbol defaults
4883Return 1 if @var{symbol} is in the linker global symbol table and is
4884defined before the statement using DEFINED in the script, otherwise
4885return 0.  You can use this function to provide
4886default values for symbols.  For example, the following script fragment
4887shows how to set a global symbol @samp{begin} to the first location in
4888the @samp{.text} section---but if a symbol called @samp{begin} already
4889existed, its value is preserved:
4890
4891@smallexample
4892@group
4893SECTIONS @{ @dots{}
4894  .text : @{
4895    begin = DEFINED(begin) ? begin : . ;
4896    @dots{}
4897  @}
4898  @dots{}
4899@}
4900@end group
4901@end smallexample
4902
4903@item LENGTH(@var{memory})
4904@kindex LENGTH(@var{memory})
4905Return the length of the memory region named @var{memory}.
4906
4907@item LOADADDR(@var{section})
4908@kindex LOADADDR(@var{section})
4909@cindex section load address in expression
4910Return the absolute LMA of the named @var{section}.  This is normally
4911the same as @code{ADDR}, but it may be different if the @code{AT}
4912attribute is used in the output section definition (@pxref{Output
4913Section LMA}).
4914
4915@kindex MAX
4916@item MAX(@var{exp1}, @var{exp2})
4917Returns the maximum of @var{exp1} and @var{exp2}.
4918
4919@kindex MIN
4920@item MIN(@var{exp1}, @var{exp2})
4921Returns the minimum of @var{exp1} and @var{exp2}.
4922
4923@item NEXT(@var{exp})
4924@kindex NEXT(@var{exp})
4925@cindex unallocated address, next
4926Return the next unallocated address that is a multiple of @var{exp}.
4927This function is closely related to @code{ALIGN(@var{exp})}; unless you
4928use the @code{MEMORY} command to define discontinuous memory for the
4929output file, the two functions are equivalent.
4930
4931@item ORIGIN(@var{memory})
4932@kindex ORIGIN(@var{memory})
4933Return the origin of the memory region named @var{memory}.
4934
4935@item SEGMENT_START(@var{segment}, @var{default})
4936@kindex SEGMENT_START(@var{segment}, @var{default})
4937Return the base address of the named @var{segment}.  If an explicit
4938value has been given for this segment (with a command-line @samp{-T}
4939option) that value will be returned; otherwise the value will be
4940@var{default}.  At present, the @samp{-T} command-line option can only
4941be used to set the base address for the ``text'', ``data'', and
4942``bss'' sections, but you use @code{SEGMENT_START} with any segment
4943name.
4944
4945@item SIZEOF(@var{section})
4946@kindex SIZEOF(@var{section})
4947@cindex section size
4948Return the size in bytes of the named @var{section}, if that section has
4949been allocated.  If the section has not been allocated when this is
4950evaluated, the linker will report an error.  In the following example,
4951@code{symbol_1} and @code{symbol_2} are assigned identical values:
4952@smallexample
4953@group
4954SECTIONS@{ @dots{}
4955  .output @{
4956    .start = . ;
4957    @dots{}
4958    .end = . ;
4959    @}
4960  symbol_1 = .end - .start ;
4961  symbol_2 = SIZEOF(.output);
4962@dots{} @}
4963@end group
4964@end smallexample
4965
4966@item SIZEOF_HEADERS
4967@itemx sizeof_headers
4968@kindex SIZEOF_HEADERS
4969@cindex header size
4970Return the size in bytes of the output file's headers.  This is
4971information which appears at the start of the output file.  You can use
4972this number when setting the start address of the first section, if you
4973choose, to facilitate paging.
4974
4975@cindex not enough room for program headers
4976@cindex program headers, not enough room
4977When producing an ELF output file, if the linker script uses the
4978@code{SIZEOF_HEADERS} builtin function, the linker must compute the
4979number of program headers before it has determined all the section
4980addresses and sizes.  If the linker later discovers that it needs
4981additional program headers, it will report an error @samp{not enough
4982room for program headers}.  To avoid this error, you must avoid using
4983the @code{SIZEOF_HEADERS} function, or you must rework your linker
4984script to avoid forcing the linker to use additional program headers, or
4985you must define the program headers yourself using the @code{PHDRS}
4986command (@pxref{PHDRS}).
4987@end table
4988
4989@node Implicit Linker Scripts
4990@section Implicit Linker Scripts
4991@cindex implicit linker scripts
4992If you specify a linker input file which the linker can not recognize as
4993an object file or an archive file, it will try to read the file as a
4994linker script.  If the file can not be parsed as a linker script, the
4995linker will report an error.
4996
4997An implicit linker script will not replace the default linker script.
4998
4999Typically an implicit linker script would contain only symbol
5000assignments, or the @code{INPUT}, @code{GROUP}, or @code{VERSION}
5001commands.
5002
5003Any input files read because of an implicit linker script will be read
5004at the position in the command line where the implicit linker script was
5005read.  This can affect archive searching.
5006
5007@ifset GENERIC
5008@node Machine Dependent
5009@chapter Machine Dependent Features
5010
5011@cindex machine dependencies
5012@command{ld} has additional features on some platforms; the following
5013sections describe them.  Machines where @command{ld} has no additional
5014functionality are not listed.
5015
5016@menu
5017@ifset H8300
5018* H8/300::                      @command{ld} and the H8/300
5019@end ifset
5020@ifset I960
5021* i960::                        @command{ld} and the Intel 960 family
5022@end ifset
5023@ifset ARM
5024* ARM::				@command{ld} and the ARM family
5025@end ifset
5026@ifset HPPA
5027* HPPA ELF32::                  @command{ld} and HPPA 32-bit ELF
5028@end ifset
5029@ifset MMIX
5030* MMIX::			@command{ld} and MMIX
5031@end ifset
5032@ifset MSP430
5033* MSP430::			@command{ld} and MSP430
5034@end ifset
5035@ifset M68HC11
5036* M68HC11/68HC12::		@code{ld} and the Motorola 68HC11 and 68HC12 families
5037@end ifset
5038@ifset TICOFF
5039* TI COFF::                     @command{ld} and TI COFF
5040@end ifset
5041@ifset WIN32
5042* WIN32::                       @command{ld} and WIN32 (cygwin/mingw)
5043@end ifset
5044@ifset XTENSA
5045* Xtensa::                      @command{ld} and Xtensa Processors
5046@end ifset
5047@end menu
5048@end ifset
5049
5050@ifset H8300
5051@ifclear GENERIC
5052@raisesections
5053@end ifclear
5054
5055@node H8/300
5056@section @command{ld} and the H8/300
5057
5058@cindex H8/300 support
5059For the H8/300, @command{ld} can perform these global optimizations when
5060you specify the @samp{--relax} command-line option.
5061
5062@table @emph
5063@cindex relaxing on H8/300
5064@item relaxing address modes
5065@command{ld} finds all @code{jsr} and @code{jmp} instructions whose
5066targets are within eight bits, and turns them into eight-bit
5067program-counter relative @code{bsr} and @code{bra} instructions,
5068respectively.
5069
5070@cindex synthesizing on H8/300
5071@item synthesizing instructions
5072@c FIXME: specifically mov.b, or any mov instructions really?
5073@command{ld} finds all @code{mov.b} instructions which use the
5074sixteen-bit absolute address form, but refer to the top
5075page of memory, and changes them to use the eight-bit address form.
5076(That is: the linker turns @samp{mov.b @code{@@}@var{aa}:16} into
5077@samp{mov.b @code{@@}@var{aa}:8} whenever the address @var{aa} is in the
5078top page of memory).
5079
5080@item bit manipulation instructions
5081@command{ld} finds all bit manipulation instructions like @code{band, bclr,
5082biand, bild, bior, bist, bixor, bld, bnot, bor, bset, bst, btst, bxor}
5083which use 32 bit and 16 bit absolute address form, but refer to the top
5084page of memory, and changes them to use the 8 bit address form.
5085(That is: the linker turns @samp{bset #xx:3,@code{@@}@var{aa}:32} into
5086@samp{bset #xx:3,@code{@@}@var{aa}:8} whenever the address @var{aa} is in
5087the top page of memory).
5088
5089@item system control instructions
5090@command{ld} finds all @code{ldc.w, stc.w} instrcutions which use the
509132 bit absolute address form, but refer to the top page of memory, and
5092changes them to use 16 bit address form.
5093(That is: the linker turns @samp{ldc.w @code{@@}@var{aa}:32,ccr} into
5094@samp{ldc.w @code{@@}@var{aa}:16,ccr} whenever the address @var{aa} is in
5095the top page of memory).
5096@end table
5097
5098@ifclear GENERIC
5099@lowersections
5100@end ifclear
5101@end ifset
5102
5103@ifclear GENERIC
5104@ifset Renesas
5105@c This stuff is pointless to say unless you're especially concerned
5106@c with Renesas chips; don't enable it for generic case, please.
5107@node Renesas
5108@chapter @command{ld} and Other Renesas Chips
5109
5110@command{ld} also supports the Renesas (formerly Hitachi) H8/300H,
5111H8/500, and SH chips.  No special features, commands, or command-line
5112options are required for these chips.
5113@end ifset
5114@end ifclear
5115
5116@ifset I960
5117@ifclear GENERIC
5118@raisesections
5119@end ifclear
5120
5121@node i960
5122@section @command{ld} and the Intel 960 Family
5123
5124@cindex i960 support
5125
5126You can use the @samp{-A@var{architecture}} command line option to
5127specify one of the two-letter names identifying members of the 960
5128family; the option specifies the desired output target, and warns of any
5129incompatible instructions in the input files.  It also modifies the
5130linker's search strategy for archive libraries, to support the use of
5131libraries specific to each particular architecture, by including in the
5132search loop names suffixed with the string identifying the architecture.
5133
5134For example, if your @command{ld} command line included @w{@samp{-ACA}} as
5135well as @w{@samp{-ltry}}, the linker would look (in its built-in search
5136paths, and in any paths you specify with @samp{-L}) for a library with
5137the names
5138
5139@smallexample
5140@group
5141try
5142libtry.a
5143tryca
5144libtryca.a
5145@end group
5146@end smallexample
5147
5148@noindent
5149The first two possibilities would be considered in any event; the last
5150two are due to the use of @w{@samp{-ACA}}.
5151
5152You can meaningfully use @samp{-A} more than once on a command line, since
5153the 960 architecture family allows combination of target architectures; each
5154use will add another pair of name variants to search for when @w{@samp{-l}}
5155specifies a library.
5156
5157@cindex @option{--relax} on i960
5158@cindex relaxing on i960
5159@command{ld} supports the @samp{--relax} option for the i960 family.  If
5160you specify @samp{--relax}, @command{ld} finds all @code{balx} and
5161@code{calx} instructions whose targets are within 24 bits, and turns
5162them into 24-bit program-counter relative @code{bal} and @code{cal}
5163instructions, respectively.  @command{ld} also turns @code{cal}
5164instructions into @code{bal} instructions when it determines that the
5165target subroutine is a leaf routine (that is, the target subroutine does
5166not itself call any subroutines).
5167
5168@ifclear GENERIC
5169@lowersections
5170@end ifclear
5171@end ifset
5172
5173@ifset ARM
5174@ifclear GENERIC
5175@raisesections
5176@end ifclear
5177
5178@ifset M68HC11
5179@ifclear GENERIC
5180@raisesections
5181@end ifclear
5182
5183@node M68HC11/68HC12
5184@section @command{ld} and the Motorola 68HC11 and 68HC12 families
5185
5186@cindex M68HC11 and 68HC12 support
5187
5188@subsection Linker Relaxation
5189
5190For the Motorola 68HC11, @command{ld} can perform these global
5191optimizations when you specify the @samp{--relax} command-line option.
5192
5193@table @emph
5194@cindex relaxing on M68HC11
5195@item relaxing address modes
5196@command{ld} finds all @code{jsr} and @code{jmp} instructions whose
5197targets are within eight bits, and turns them into eight-bit
5198program-counter relative @code{bsr} and @code{bra} instructions,
5199respectively.
5200
5201@command{ld} also looks at all 16-bit extended addressing modes and
5202transforms them in a direct addressing mode when the address is in
5203page 0 (between 0 and 0x0ff).
5204
5205@item relaxing gcc instruction group
5206When @command{gcc} is called with @option{-mrelax}, it can emit group
5207of instructions that the linker can optimize to use a 68HC11 direct
5208addressing mode. These instructions consists of @code{bclr} or
5209@code{bset} instructions.
5210
5211@end table
5212
5213@subsection Trampoline Generation
5214
5215@cindex trampoline generation on M68HC11
5216@cindex trampoline generation on M68HC12
5217For 68HC11 and 68HC12, @command{ld} can generate trampoline code to
5218call a far function using a normal @code{jsr} instruction. The linker
5219will also change the relocation to some far function to use the
5220trampoline address instead of the function address. This is typically the
5221case when a pointer to a function is taken. The pointer will in fact
5222point to the function trampoline.
5223
5224@ifclear GENERIC
5225@lowersections
5226@end ifclear
5227@end ifset
5228
5229@node ARM
5230@section @command{ld} and the ARM family
5231
5232@cindex ARM interworking support
5233@kindex --support-old-code
5234For the ARM, @command{ld} will generate code stubs to allow functions calls
5235betweem ARM and Thumb code.  These stubs only work with code that has
5236been compiled and assembled with the @samp{-mthumb-interwork} command
5237line option.  If it is necessary to link with old ARM object files or
5238libraries, which have not been compiled with the -mthumb-interwork
5239option then the @samp{--support-old-code} command line switch should be
5240given to the linker.  This will make it generate larger stub functions
5241which will work with non-interworking aware ARM code.  Note, however,
5242the linker does not support generating stubs for function calls to
5243non-interworking aware Thumb code.
5244
5245@cindex thumb entry point
5246@cindex entry point, thumb
5247@kindex --thumb-entry=@var{entry}
5248The @samp{--thumb-entry} switch is a duplicate of the generic
5249@samp{--entry} switch, in that it sets the program's starting address.
5250But it also sets the bottom bit of the address, so that it can be
5251branched to using a BX instruction, and the program will start
5252executing in Thumb mode straight away.
5253
5254@cindex BE8
5255@kindex --be8
5256The @samp{--be8} switch instructs @command{ld} to generate BE8 format
5257executables.  This option is only valid when linking big-endian objects.
5258The resulting image will contain big-endian data and little-endian code.
5259
5260@cindex TARGET1
5261@kindex --target1-rel
5262@kindex --target1-abs
5263The @samp{R_ARM_TARGET1} relocation is typically used for entries in the
5264@samp{.init_array} section.  It is interpreted as either @samp{R_ARM_REL32}
5265or @samp{R_ARM_ABS32}, depending on the target.  The @samp{--target1-rel}
5266and @samp{--target1-abs} switches override the default.
5267
5268@cindex TARGET2
5269@kindex --target2=@var{type}
5270The @samp{--target2=type} switch overrides the default definition of the
5271@samp{R_ARM_TARGET2} relocation.  Valid values for @samp{type}, their
5272meanings, and target defaults are as follows:
5273@table @samp
5274@item rel
5275@samp{R_ARM_REL32} (arm*-*-elf, arm*-*-eabi)
5276@item abs
5277@samp{R_ARM_ABS32} (arm*-*-symbianelf)
5278@item got-rel
5279@samp{R_ARM_GOT_PREL} (arm*-*-linux, arm*-*-*bsd)
5280@end table
5281
5282@cindex FIX_V4BX
5283@kindex --fix-v4bx
5284The @samp{R_ARM_V4BX} relocation (defined by the ARM AAELF
5285specification) enables objects compiled for the ARMv4 architecture to be
5286interworking-safe when linked with other objects compiled for ARMv4t, but
5287also allows pure ARMv4 binaries to be built from the same ARMv4 objects.
5288
5289In the latter case, the switch @option{--fix-v4bx} must be passed to the
5290linker, which causes v4t @code{BX rM} instructions to be rewritten as
5291@code{MOV PC,rM}, since v4 processors do not have a @code{BX} instruction.
5292
5293In the former case, the switch should not be used, and @samp{R_ARM_V4BX}
5294relocations are ignored.
5295
5296@cindex USE_BLX
5297@kindex --use-blx
5298The @samp{--use-blx} switch enables the linker to use ARM/Thumb
5299BLX instructions (available on ARMv5t and above) in various
5300situations. Currently it is used to perform calls via the PLT from Thumb
5301code using BLX rather than using BX and a mode-switching stub before
5302each PLT entry. This should lead to such calls executing slightly faster.
5303
5304This option is enabled implicitly for SymbianOS, so there is no need to
5305specify it if you are using that target.
5306
5307@ifclear GENERIC
5308@lowersections
5309@end ifclear
5310@end ifset
5311
5312@ifset HPPA
5313@ifclear GENERIC
5314@raisesections
5315@end ifclear
5316
5317@node HPPA ELF32
5318@section @command{ld} and HPPA 32-bit ELF Support
5319@cindex HPPA multiple sub-space stubs
5320@kindex --multi-subspace
5321When generating a shared library, @command{ld} will by default generate
5322import stubs suitable for use with a single sub-space application.
5323The @samp{--multi-subspace} switch causes @command{ld} to generate export
5324stubs, and different (larger) import stubs suitable for use with
5325multiple sub-spaces.
5326
5327@cindex HPPA stub grouping
5328@kindex --stub-group-size=@var{N}
5329Long branch stubs and import/export stubs are placed by @command{ld} in
5330stub sections located between groups of input sections.
5331@samp{--stub-group-size} specifies the maximum size of a group of input
5332sections handled by one stub section.  Since branch offsets are signed,
5333a stub section may serve two groups of input sections, one group before
5334the stub section, and one group after it.  However, when using
5335conditional branches that require stubs, it may be better (for branch
5336prediction) that stub sections only serve one group of input sections.
5337A negative value for @samp{N} chooses this scheme, ensuring that
5338branches to stubs always use a negative offset.  Two special values of
5339@samp{N} are recognized, @samp{1} and @samp{-1}.  These both instruct
5340@command{ld} to automatically size input section groups for the branch types
5341detected, with the same behaviour regarding stub placement as other
5342positive or negative values of @samp{N} respectively.
5343
5344Note that @samp{--stub-group-size} does not split input sections.  A
5345single input section larger than the group size specified will of course
5346create a larger group (of one section).  If input sections are too
5347large, it may not be possible for a branch to reach its stub.
5348
5349@ifclear GENERIC
5350@lowersections
5351@end ifclear
5352@end ifset
5353
5354@ifset MMIX
5355@ifclear GENERIC
5356@raisesections
5357@end ifclear
5358
5359@node MMIX
5360@section @code{ld} and MMIX
5361For MMIX, there is a choice of generating @code{ELF} object files or
5362@code{mmo} object files when linking.  The simulator @code{mmix}
5363understands the @code{mmo} format.  The binutils @code{objcopy} utility
5364can translate between the two formats.
5365
5366There is one special section, the @samp{.MMIX.reg_contents} section.
5367Contents in this section is assumed to correspond to that of global
5368registers, and symbols referring to it are translated to special symbols,
5369equal to registers.  In a final link, the start address of the
5370@samp{.MMIX.reg_contents} section corresponds to the first allocated
5371global register multiplied by 8.  Register @code{$255} is not included in
5372this section; it is always set to the program entry, which is at the
5373symbol @code{Main} for @code{mmo} files.
5374
5375Symbols with the prefix @code{__.MMIX.start.}, for example
5376@code{__.MMIX.start..text} and @code{__.MMIX.start..data} are special;
5377there must be only one each, even if they are local.  The default linker
5378script uses these to set the default start address of a section.
5379
5380Initial and trailing multiples of zero-valued 32-bit words in a section,
5381are left out from an mmo file.
5382
5383@ifclear GENERIC
5384@lowersections
5385@end ifclear
5386@end ifset
5387
5388@ifset MSP430
5389@ifclear GENERIC
5390@raisesections
5391@end ifclear
5392
5393@node  MSP430
5394@section @code{ld} and MSP430
5395For the MSP430 it is possible to select the MPU architecture.  The flag @samp{-m [mpu type]}
5396will select an appropriate linker script for selected MPU type.  (To get a list of known MPUs
5397just pass @samp{-m help} option to the linker).
5398
5399@cindex MSP430 extra sections
5400The linker will recognize some extra sections which are MSP430 specific:
5401
5402@table @code
5403@item @samp{.vectors}
5404Defines a portion of ROM where interrupt vectors located.
5405
5406@item @samp{.bootloader}
5407Defines the bootloader portion of the ROM (if applicable).  Any code
5408in this section will be uploaded to the MPU.
5409
5410@item @samp{.infomem}
5411Defines an information memory section (if applicable).  Any code in
5412this section will be uploaded to the MPU.
5413
5414@item @samp{.infomemnobits}
5415This is the same as the @samp{.infomem} section except that any code
5416in this section will not be uploaded to the MPU.
5417
5418@item @samp{.noinit}
5419Denotes a portion of RAM located above @samp{.bss} section.
5420
5421The last two sections are used by gcc.
5422@end table
5423
5424@ifclear GENERIC
5425@lowersections
5426@end ifclear
5427@end ifset
5428
5429@ifset TICOFF
5430@ifclear GENERIC
5431@raisesections
5432@end ifclear
5433
5434@node TI COFF
5435@section @command{ld}'s Support for Various TI COFF Versions
5436@cindex TI COFF versions
5437@kindex --format=@var{version}
5438The @samp{--format} switch allows selection of one of the various
5439TI COFF versions.  The latest of this writing is 2; versions 0 and 1 are
5440also supported.  The TI COFF versions also vary in header byte-order
5441format; @command{ld} will read any version or byte order, but the output
5442header format depends on the default specified by the specific target.
5443
5444@ifclear GENERIC
5445@lowersections
5446@end ifclear
5447@end ifset
5448
5449@ifset WIN32
5450@ifclear GENERIC
5451@raisesections
5452@end ifclear
5453
5454@node WIN32
5455@section @command{ld} and WIN32 (cygwin/mingw)
5456
5457This section describes some of the win32 specific @command{ld} issues.
5458See @ref{Options,,Command Line Options} for detailed decription of the
5459command line options mentioned here.
5460
5461@table @emph
5462@cindex import libraries
5463@item import libraries
5464The standard Windows linker creates and uses so-called import
5465libraries, which contains information for linking to DLLs.  They are
5466regular static archives and are handled as any other static
5467archive.  The cygwin and mingw ports of @command{ld} have specific
5468support for creating such libraries provided with the
5469@samp{--out-implib} command line option.
5470
5471@item   exporting DLL symbols
5472@cindex exporting DLL symbols
5473The cygwin/mingw @command{ld} has several ways to export symbols for DLLs.
5474
5475@table @emph
5476@item   using auto-export functionality
5477@cindex using auto-export functionality
5478By default @command{ld} exports symbols with the auto-export functionality,
5479which is controlled by the following command line options:
5480
5481@itemize
5482@item --export-all-symbols   [This is the default]
5483@item --exclude-symbols
5484@item --exclude-libs
5485@end itemize
5486
5487If, however, @samp{--export-all-symbols} is not given explicitly on the
5488command line, then the default auto-export behavior will be @emph{disabled}
5489if either of the following are true:
5490
5491@itemize
5492@item A DEF file is used.
5493@item Any symbol in any object file was marked with the __declspec(dllexport) attribute.
5494@end itemize
5495
5496@item   using a DEF file
5497@cindex using a DEF file
5498Another way of exporting symbols is using a DEF file.  A DEF file is
5499an ASCII file containing definitions of symbols which should be
5500exported when a dll is created.  Usually it is named @samp{<dll
5501name>.def} and is added as any other object file to the linker's
5502command line.  The file's name must end in @samp{.def} or @samp{.DEF}.
5503
5504@example
5505gcc -o <output> <objectfiles> <dll name>.def
5506@end example
5507
5508Using a DEF file turns off the normal auto-export behavior, unless the
5509@samp{--export-all-symbols} option is also used.
5510
5511Here is an example of a DEF file for a shared library called @samp{xyz.dll}:
5512
5513@example
5514LIBRARY "xyz.dll" BASE=0x10000000
5515
5516EXPORTS
5517foo
5518bar
5519_bar = bar
5520@end example
5521
5522This example defines a base address and three symbols.  The third
5523symbol is an alias for the second.  For the complete format
5524specification see ld/deffilep.y in the binutils sources.
5525
5526@cindex creating a DEF file
5527While linking a shared dll, @command{ld} is able to create a DEF file
5528with the @samp{--output-def <file>} command line option.
5529
5530@item   Using decorations
5531@cindex Using decorations
5532Another way of marking symbols for export is to modify the source code
5533itself, so that when building the DLL each symbol to be exported is
5534declared as:
5535
5536@example
5537__declspec(dllexport) int a_variable
5538__declspec(dllexport) void a_function(int with_args)
5539@end example
5540
5541All such symbols will be exported from the DLL.  If, however,
5542any of the object files in the DLL contain symbols decorated in
5543this way, then the normal auto-export behavior is disabled, unless
5544the @samp{--export-all-symbols} option is also used.
5545
5546Note that object files that wish to access these symbols must @emph{not}
5547decorate them with dllexport.  Instead, they should use dllimport,
5548instead:
5549
5550@example
5551__declspec(dllimport) int a_variable
5552__declspec(dllimport) void a_function(int with_args)
5553@end example
5554
5555This complicates the structure of library header files, because
5556when included by the library itself the header must declare the
5557variables and functions as dllexport, but when included by client
5558code the header must declare them as dllimport.  There are a number
5559of idioms that are typically used to do this; often client code can
5560omit the __declspec() declaration completely.  See
5561@samp{--enable-auto-import} and @samp{automatic data imports} for more
5562imformation.
5563@end table
5564
5565@cindex automatic data imports
5566@item automatic data imports
5567The standard Windows dll format supports data imports from dlls only
5568by adding special decorations (dllimport/dllexport), which let the
5569compiler produce specific assembler instructions to deal with this
5570issue.  This increases the effort necessary to port existing Un*x
5571code to these platforms, especially for large
5572c++ libraries and applications.  The auto-import feature, which was
5573initially provided by Paul Sokolovsky, allows one to omit the
5574decorations to archieve a behavior that conforms to that on POSIX/Un*x
5575platforms. This feature is enabled with the @samp{--enable-auto-import}
5576command-line option, although it is enabled by default on cygwin/mingw.
5577The @samp{--enable-auto-import} option itself now serves mainly to
5578suppress any warnings that are ordinarily emitted when linked objects
5579trigger the feature's use.
5580
5581auto-import of variables does not always work flawlessly without
5582additional assistance.  Sometimes, you will see this message
5583
5584"variable '<var>' can't be auto-imported. Please read the
5585documentation for ld's @code{--enable-auto-import} for details."
5586
5587The @samp{--enable-auto-import} documentation explains why this error
5588occurs, and several methods that can be used to overcome this difficulty.
5589One of these methods is the @emph{runtime pseudo-relocs} feature, described
5590below.
5591
5592@cindex runtime pseudo-relocation
5593For complex variables imported from DLLs (such as structs or classes),
5594object files typically contain a base address for the variable and an
5595offset (@emph{addend}) within the variable--to specify a particular
5596field or public member, for instance.  Unfortunately, the runtime loader used
5597in win32 environments is incapable of fixing these references at runtime
5598without the additional information supplied by dllimport/dllexport decorations.
5599The standard auto-import feature described above is unable to resolve these
5600references.
5601
5602The @samp{--enable-runtime-pseudo-relocs} switch allows these references to
5603be resolved without error, while leaving the task of adjusting the references
5604themselves (with their non-zero addends) to specialized code provided by the
5605runtime environment.  Recent versions of the cygwin and mingw environments and
5606compilers provide this runtime support; older versions do not.  However, the
5607support is only necessary on the developer's platform; the compiled result will
5608run without error on an older system.
5609
5610@samp{--enable-runtime-pseudo-relocs} is not the default; it must be explicitly
5611enabled as needed.
5612
5613@cindex direct linking to a dll
5614@item direct linking to a dll
5615The cygwin/mingw ports of @command{ld} support the direct linking,
5616including data symbols, to a dll without the usage of any import
5617libraries.  This is much faster and uses much less memory than does the
5618traditional import library method, expecially when linking large
5619libraries or applications.  When @command{ld} creates an import lib, each
5620function or variable exported from the dll is stored in its own bfd, even
5621though a single bfd could contain many exports.  The overhead involved in
5622storing, loading, and processing so many bfd's is quite large, and explains the
5623tremendous time, memory, and storage needed to link against particularly
5624large or complex libraries when using import libs.
5625
5626Linking directly to a dll uses no extra command-line switches other than
5627@samp{-L} and @samp{-l}, because @command{ld} already searches for a number
5628of names to match each library.  All that is needed from the developer's
5629perspective is an understanding of this search, in order to force ld to
5630select the dll instead of an import library.
5631
5632
5633For instance, when ld is called with the argument @samp{-lxxx} it will attempt
5634to find, in the first directory of its search path,
5635
5636@example
5637libxxx.dll.a
5638xxx.dll.a
5639libxxx.a
5640cygxxx.dll (*)
5641libxxx.dll
5642xxx.dll
5643@end example
5644
5645before moving on to the next directory in the search path.
5646
5647(*) Actually, this is not @samp{cygxxx.dll} but in fact is @samp{<prefix>xxx.dll},
5648where @samp{<prefix>} is set by the @command{ld} option
5649@samp{--dll-search-prefix=<prefix>}. In the case of cygwin, the standard gcc spec
5650file includes @samp{--dll-search-prefix=cyg}, so in effect we actually search for
5651@samp{cygxxx.dll}.
5652
5653Other win32-based unix environments, such as mingw or pw32, may use other
5654@samp{<prefix>}es, although at present only cygwin makes use of this feature.  It
5655was originally intended to help avoid name conflicts among DLLs built for the
5656various win32/un*x environments, so that (for example) two versions of a zlib dll
5657could coexist on the same machine.
5658
5659The generic cygwin/mingw path layout uses a @samp{bin} directory for
5660applications and DLLs and a @samp{lib} directory for the import
5661libraries (using cygwin nomenclature):
5662
5663@example
5664bin/
5665	cygxxx.dll
5666lib/
5667	libxxx.dll.a   (in case of DLLs)
5668	libxxx.a       (in case of static archive)
5669@end example
5670
5671Linking directly to a dll without using the import library can be
5672done two ways:
5673
56741. Use the dll directly by adding the @samp{bin} path to the link line
5675@example
5676gcc -Wl,-verbose  -o a.exe -L../bin/ -lxxx
5677@end example
5678
5679However, as the DLLs often have version numbers appended to their names
5680(@samp{cygncurses-5.dll}) this will often fail, unless one specifies
5681@samp{-L../bin -lncurses-5} to include the version.  Import libs are generally
5682not versioned, and do not have this difficulty.
5683
56842. Create a symbolic link from the dll to a file in the @samp{lib}
5685directory according to the above mentioned search pattern.  This
5686should be used to avoid unwanted changes in the tools needed for
5687making the app/dll.
5688
5689@example
5690ln -s bin/cygxxx.dll lib/[cyg|lib|]xxx.dll[.a]
5691@end example
5692
5693Then you can link without any make environment changes.
5694
5695@example
5696gcc -Wl,-verbose  -o a.exe -L../lib/ -lxxx
5697@end example
5698
5699This technique also avoids the version number problems, because the following is
5700perfectly legal
5701
5702@example
5703bin/
5704	cygxxx-5.dll
5705lib/
5706	libxxx.dll.a -> ../bin/cygxxx-5.dll
5707@end example
5708
5709Linking directly to a dll without using an import lib will work
5710even when auto-import features are exercised, and even when
5711@samp{--enable-runtime-pseudo-relocs} is used.
5712
5713Given the improvements in speed and memory usage, one might justifiably
5714wonder why import libraries are used at all.  There are two reasons:
5715
57161. Until recently, the link-directly-to-dll functionality did @emph{not}
5717work with auto-imported data.
5718
57192. Sometimes it is necessary to include pure static objects within the
5720import library (which otherwise contains only bfd's for indirection
5721symbols that point to the exports of a dll).  Again, the import lib
5722for the cygwin kernel makes use of this ability, and it is not
5723possible to do this without an import lib.
5724
5725So, import libs are not going away.  But the ability to replace
5726true import libs with a simple symbolic link to (or a copy of)
5727a dll, in most cases, is a useful addition to the suite of tools
5728binutils makes available to the win32 developer.  Given the
5729massive improvements in memory requirements during linking, storage
5730requirements, and linking speed, we expect that many developers
5731will soon begin to use this feature whenever possible.
5732
5733@item symbol aliasing
5734@table @emph
5735@item adding additional names
5736Sometimes, it is useful to export symbols with additional names.
5737A symbol @samp{foo} will be exported as @samp{foo}, but it can also be
5738exported as @samp{_foo} by using special directives in the DEF file
5739when creating the dll.  This will affect also the optional created
5740import library.  Consider the following DEF file:
5741
5742@example
5743LIBRARY "xyz.dll" BASE=0x61000000
5744
5745EXPORTS
5746foo
5747_foo = foo
5748@end example
5749
5750The line @samp{_foo = foo} maps the symbol @samp{foo} to @samp{_foo}.
5751
5752Another method for creating a symbol alias is to create it in the
5753source code using the "weak" attribute:
5754
5755@example
5756void foo () @{ /* Do something.  */; @}
5757void _foo () __attribute__ ((weak, alias ("foo")));
5758@end example
5759
5760See the gcc manual for more information about attributes and weak
5761symbols.
5762
5763@item renaming symbols
5764Sometimes it is useful to rename exports.  For instance, the cygwin
5765kernel does this regularly.  A symbol @samp{_foo} can be exported as
5766@samp{foo} but not as @samp{_foo} by using special directives in the
5767DEF file. (This will also affect the import library, if it is
5768created).  In the following example:
5769
5770@example
5771LIBRARY "xyz.dll" BASE=0x61000000
5772
5773EXPORTS
5774_foo = foo
5775@end example
5776
5777The line @samp{_foo = foo} maps the exported symbol @samp{foo} to
5778@samp{_foo}.
5779@end table
5780
5781Note: using a DEF file disables the default auto-export behavior,
5782unless the @samp{--export-all-symbols} command line option is used.
5783If, however, you are trying to rename symbols, then you should list
5784@emph{all} desired exports in the DEF file, including the symbols
5785that are not being renamed, and do @emph{not} use the
5786@samp{--export-all-symbols} option.  If you list only the
5787renamed symbols in the DEF file, and use @samp{--export-all-symbols}
5788to handle the other symbols, then the both the new names @emph{and}
5789the original names for the renamed symbols will be exported.
5790In effect, you'd be aliasing those symbols, not renaming them,
5791which is probably not what you wanted.
5792
5793@cindex weak externals
5794@item weak externals
5795The Windows object format, PE, specifies a form of weak symbols called
5796weak externals.  When a weak symbol is linked and the symbol is not
5797defined, the weak symbol becomes an alias for some other symbol.  There
5798are three variants of weak externals:
5799@itemize
5800@item Definition is searched for in objects and libraries, historically
5801called lazy externals.
5802@item Definition is searched for only in other objects, not in libraries.
5803This form is not presently implemented.
5804@item No search; the symbol is an alias.  This form is not presently
5805implemented.
5806@end itemize
5807As a GNU extension, weak symbols that do not specify an alternate symbol
5808are supported.  If the symbol is undefined when linking, the symbol
5809uses a default value.
5810@end table
5811
5812@ifclear GENERIC
5813@lowersections
5814@end ifclear
5815@end ifset
5816
5817@ifset XTENSA
5818@ifclear GENERIC
5819@raisesections
5820@end ifclear
5821
5822@node Xtensa
5823@section @code{ld} and Xtensa Processors
5824
5825@cindex Xtensa processors
5826The default @command{ld} behavior for Xtensa processors is to interpret
5827@code{SECTIONS} commands so that lists of explicitly named sections in a
5828specification with a wildcard file will be interleaved when necessary to
5829keep literal pools within the range of PC-relative load offsets.  For
5830example, with the command:
5831
5832@smallexample
5833SECTIONS
5834@{
5835  .text : @{
5836    *(.literal .text)
5837  @}
5838@}
5839@end smallexample
5840
5841@noindent
5842@command{ld} may interleave some of the @code{.literal}
5843and @code{.text} sections from different object files to ensure that the
5844literal pools are within the range of PC-relative load offsets.  A valid
5845interleaving might place the @code{.literal} sections from an initial
5846group of files followed by the @code{.text} sections of that group of
5847files.  Then, the @code{.literal} sections from the rest of the files
5848and the @code{.text} sections from the rest of the files would follow.
5849
5850@cindex @option{--relax} on Xtensa
5851@cindex relaxing on Xtensa
5852Relaxation is enabled by default for the Xtensa version of @command{ld} and
5853provides two important link-time optimizations.  The first optimization
5854is to combine identical literal values to reduce code size.  A redundant
5855literal will be removed and all the @code{L32R} instructions that use it
5856will be changed to reference an identical literal, as long as the
5857location of the replacement literal is within the offset range of all
5858the @code{L32R} instructions.  The second optimization is to remove
5859unnecessary overhead from assembler-generated ``longcall'' sequences of
5860@code{L32R}/@code{CALLX@var{n}} when the target functions are within
5861range of direct @code{CALL@var{n}} instructions.
5862
5863For each of these cases where an indirect call sequence can be optimized
5864to a direct call, the linker will change the @code{CALLX@var{n}}
5865instruction to a @code{CALL@var{n}} instruction, remove the @code{L32R}
5866instruction, and remove the literal referenced by the @code{L32R}
5867instruction if it is not used for anything else.  Removing the
5868@code{L32R} instruction always reduces code size but can potentially
5869hurt performance by changing the alignment of subsequent branch targets.
5870By default, the linker will always preserve alignments, either by
5871switching some instructions between 24-bit encodings and the equivalent
5872density instructions or by inserting a no-op in place of the @code{L32R}
5873instruction that was removed.  If code size is more important than
5874performance, the @option{--size-opt} option can be used to prevent the
5875linker from widening density instructions or inserting no-ops, except in
5876a few cases where no-ops are required for correctness.
5877
5878The following Xtensa-specific command-line options can be used to
5879control the linker:
5880
5881@cindex Xtensa options
5882@table @option
5883@kindex --no-relax
5884@item --no-relax
5885Since the Xtensa version of @code{ld} enables the @option{--relax} option
5886by default, the @option{--no-relax} option is provided to disable
5887relaxation.
5888
5889@item --size-opt
5890When optimizing indirect calls to direct calls, optimize for code size
5891more than performance.  With this option, the linker will not insert
5892no-ops or widen density instructions to preserve branch target
5893alignment.  There may still be some cases where no-ops are required to
5894preserve the correctness of the code.
5895@end table
5896
5897@ifclear GENERIC
5898@lowersections
5899@end ifclear
5900@end ifset
5901
5902@ifclear SingleFormat
5903@node BFD
5904@chapter BFD
5905
5906@cindex back end
5907@cindex object file management
5908@cindex object formats available
5909@kindex objdump -i
5910The linker accesses object and archive files using the BFD libraries.
5911These libraries allow the linker to use the same routines to operate on
5912object files whatever the object file format.  A different object file
5913format can be supported simply by creating a new BFD back end and adding
5914it to the library.  To conserve runtime memory, however, the linker and
5915associated tools are usually configured to support only a subset of the
5916object file formats available.  You can use @code{objdump -i}
5917(@pxref{objdump,,objdump,binutils.info,The GNU Binary Utilities}) to
5918list all the formats available for your configuration.
5919
5920@cindex BFD requirements
5921@cindex requirements for BFD
5922As with most implementations, BFD is a compromise between
5923several conflicting requirements. The major factor influencing
5924BFD design was efficiency: any time used converting between
5925formats is time which would not have been spent had BFD not
5926been involved. This is partly offset by abstraction payback; since
5927BFD simplifies applications and back ends, more time and care
5928may be spent optimizing algorithms for a greater speed.
5929
5930One minor artifact of the BFD solution which you should bear in
5931mind is the potential for information loss.  There are two places where
5932useful information can be lost using the BFD mechanism: during
5933conversion and during output. @xref{BFD information loss}.
5934
5935@menu
5936* BFD outline::                 How it works: an outline of BFD
5937@end menu
5938
5939@node BFD outline
5940@section How It Works: An Outline of BFD
5941@cindex opening object files
5942@include bfdsumm.texi
5943@end ifclear
5944
5945@node Reporting Bugs
5946@chapter Reporting Bugs
5947@cindex bugs in @command{ld}
5948@cindex reporting bugs in @command{ld}
5949
5950Your bug reports play an essential role in making @command{ld} reliable.
5951
5952Reporting a bug may help you by bringing a solution to your problem, or
5953it may not.  But in any case the principal function of a bug report is
5954to help the entire community by making the next version of @command{ld}
5955work better.  Bug reports are your contribution to the maintenance of
5956@command{ld}.
5957
5958In order for a bug report to serve its purpose, you must include the
5959information that enables us to fix the bug.
5960
5961@menu
5962* Bug Criteria::                Have you found a bug?
5963* Bug Reporting::               How to report bugs
5964@end menu
5965
5966@node Bug Criteria
5967@section Have You Found a Bug?
5968@cindex bug criteria
5969
5970If you are not sure whether you have found a bug, here are some guidelines:
5971
5972@itemize @bullet
5973@cindex fatal signal
5974@cindex linker crash
5975@cindex crash of linker
5976@item
5977If the linker gets a fatal signal, for any input whatever, that is a
5978@command{ld} bug.  Reliable linkers never crash.
5979
5980@cindex error on valid input
5981@item
5982If @command{ld} produces an error message for valid input, that is a bug.
5983
5984@cindex invalid input
5985@item
5986If @command{ld} does not produce an error message for invalid input, that
5987may be a bug.  In the general case, the linker can not verify that
5988object files are correct.
5989
5990@item
5991If you are an experienced user of linkers, your suggestions for
5992improvement of @command{ld} are welcome in any case.
5993@end itemize
5994
5995@node Bug Reporting
5996@section How to Report Bugs
5997@cindex bug reports
5998@cindex @command{ld} bugs, reporting
5999
6000A number of companies and individuals offer support for @sc{gnu}
6001products.  If you obtained @command{ld} from a support organization, we
6002recommend you contact that organization first.
6003
6004You can find contact information for many support companies and
6005individuals in the file @file{etc/SERVICE} in the @sc{gnu} Emacs
6006distribution.
6007
6008Otherwise, send bug reports for @command{ld} to
6009@samp{bug-binutils@@gnu.org}.
6010
6011The fundamental principle of reporting bugs usefully is this:
6012@strong{report all the facts}.  If you are not sure whether to state a
6013fact or leave it out, state it!
6014
6015Often people omit facts because they think they know what causes the
6016problem and assume that some details do not matter.  Thus, you might
6017assume that the name of a symbol you use in an example does not
6018matter.  Well, probably it does not, but one cannot be sure.  Perhaps
6019the bug is a stray memory reference which happens to fetch from the
6020location where that name is stored in memory; perhaps, if the name
6021were different, the contents of that location would fool the linker
6022into doing the right thing despite the bug.  Play it safe and give a
6023specific, complete example.  That is the easiest thing for you to do,
6024and the most helpful.
6025
6026Keep in mind that the purpose of a bug report is to enable us to fix
6027the bug if it is new to us.  Therefore, always write your bug reports
6028on the assumption that the bug has not been reported previously.
6029
6030Sometimes people give a few sketchy facts and ask, ``Does this ring a
6031bell?''  This cannot help us fix a bug, so it is basically useless.  We
6032respond by asking for enough details to enable us to investigate.
6033You might as well expedite matters by sending them to begin with.
6034
6035To enable us to fix the bug, you should include all these things:
6036
6037@itemize @bullet
6038@item
6039The version of @command{ld}.  @command{ld} announces it if you start it with
6040the @samp{--version} argument.
6041
6042Without this, we will not know whether there is any point in looking for
6043the bug in the current version of @command{ld}.
6044
6045@item
6046Any patches you may have applied to the @command{ld} source, including any
6047patches made to the @code{BFD} library.
6048
6049@item
6050The type of machine you are using, and the operating system name and
6051version number.
6052
6053@item
6054What compiler (and its version) was used to compile @command{ld}---e.g.
6055``@code{gcc-2.7}''.
6056
6057@item
6058The command arguments you gave the linker to link your example and
6059observe the bug.  To guarantee you will not omit something important,
6060list them all.  A copy of the Makefile (or the output from make) is
6061sufficient.
6062
6063If we were to try to guess the arguments, we would probably guess wrong
6064and then we might not encounter the bug.
6065
6066@item
6067A complete input file, or set of input files, that will reproduce the
6068bug.  It is generally most helpful to send the actual object files
6069provided that they are reasonably small.  Say no more than 10K.  For
6070bigger files you can either make them available by FTP or HTTP or else
6071state that you are willing to send the object file(s) to whomever
6072requests them.  (Note - your email will be going to a mailing list, so
6073we do not want to clog it up with large attachments).  But small
6074attachments are best.
6075
6076If the source files were assembled using @code{gas} or compiled using
6077@code{gcc}, then it may be OK to send the source files rather than the
6078object files.  In this case, be sure to say exactly what version of
6079@code{gas} or @code{gcc} was used to produce the object files.  Also say
6080how @code{gas} or @code{gcc} were configured.
6081
6082@item
6083A description of what behavior you observe that you believe is
6084incorrect.  For example, ``It gets a fatal signal.''
6085
6086Of course, if the bug is that @command{ld} gets a fatal signal, then we
6087will certainly notice it.  But if the bug is incorrect output, we might
6088not notice unless it is glaringly wrong.  You might as well not give us
6089a chance to make a mistake.
6090
6091Even if the problem you experience is a fatal signal, you should still
6092say so explicitly.  Suppose something strange is going on, such as, your
6093copy of @command{ld} is out of synch, or you have encountered a bug in the
6094C library on your system.  (This has happened!)  Your copy might crash
6095and ours would not.  If you told us to expect a crash, then when ours
6096fails to crash, we would know that the bug was not happening for us.  If
6097you had not told us to expect a crash, then we would not be able to draw
6098any conclusion from our observations.
6099
6100@item
6101If you wish to suggest changes to the @command{ld} source, send us context
6102diffs, as generated by @code{diff} with the @samp{-u}, @samp{-c}, or
6103@samp{-p} option.  Always send diffs from the old file to the new file.
6104If you even discuss something in the @command{ld} source, refer to it by
6105context, not by line number.
6106
6107The line numbers in our development sources will not match those in your
6108sources.  Your line numbers would convey no useful information to us.
6109@end itemize
6110
6111Here are some things that are not necessary:
6112
6113@itemize @bullet
6114@item
6115A description of the envelope of the bug.
6116
6117Often people who encounter a bug spend a lot of time investigating
6118which changes to the input file will make the bug go away and which
6119changes will not affect it.
6120
6121This is often time consuming and not very useful, because the way we
6122will find the bug is by running a single example under the debugger
6123with breakpoints, not by pure deduction from a series of examples.
6124We recommend that you save your time for something else.
6125
6126Of course, if you can find a simpler example to report @emph{instead}
6127of the original one, that is a convenience for us.  Errors in the
6128output will be easier to spot, running under the debugger will take
6129less time, and so on.
6130
6131However, simplification is not vital; if you do not want to do this,
6132report the bug anyway and send us the entire test case you used.
6133
6134@item
6135A patch for the bug.
6136
6137A patch for the bug does help us if it is a good one.  But do not omit
6138the necessary information, such as the test case, on the assumption that
6139a patch is all we need.  We might see problems with your patch and decide
6140to fix the problem another way, or we might not understand it at all.
6141
6142Sometimes with a program as complicated as @command{ld} it is very hard to
6143construct an example that will make the program follow a certain path
6144through the code.  If you do not send us the example, we will not be
6145able to construct one, so we will not be able to verify that the bug is
6146fixed.
6147
6148And if we cannot understand what bug you are trying to fix, or why your
6149patch should be an improvement, we will not install it.  A test case will
6150help us to understand.
6151
6152@item
6153A guess about what the bug is or what it depends on.
6154
6155Such guesses are usually wrong.  Even we cannot guess right about such
6156things without first using the debugger to find the facts.
6157@end itemize
6158
6159@node MRI
6160@appendix MRI Compatible Script Files
6161@cindex MRI compatibility
6162To aid users making the transition to @sc{gnu} @command{ld} from the MRI
6163linker, @command{ld} can use MRI compatible linker scripts as an
6164alternative to the more general-purpose linker scripting language
6165described in @ref{Scripts}.  MRI compatible linker scripts have a much
6166simpler command set than the scripting language otherwise used with
6167@command{ld}.  @sc{gnu} @command{ld} supports the most commonly used MRI
6168linker commands; these commands are described here.
6169
6170In general, MRI scripts aren't of much use with the @code{a.out} object
6171file format, since it only has three sections and MRI scripts lack some
6172features to make use of them.
6173
6174You can specify a file containing an MRI-compatible script using the
6175@samp{-c} command-line option.
6176
6177Each command in an MRI-compatible script occupies its own line; each
6178command line starts with the keyword that identifies the command (though
6179blank lines are also allowed for punctuation).  If a line of an
6180MRI-compatible script begins with an unrecognized keyword, @command{ld}
6181issues a warning message, but continues processing the script.
6182
6183Lines beginning with @samp{*} are comments.
6184
6185You can write these commands using all upper-case letters, or all
6186lower case; for example, @samp{chip} is the same as @samp{CHIP}.
6187The following list shows only the upper-case form of each command.
6188
6189@table @code
6190@cindex @code{ABSOLUTE} (MRI)
6191@item ABSOLUTE @var{secname}
6192@itemx ABSOLUTE @var{secname}, @var{secname}, @dots{} @var{secname}
6193Normally, @command{ld} includes in the output file all sections from all
6194the input files.  However, in an MRI-compatible script, you can use the
6195@code{ABSOLUTE} command to restrict the sections that will be present in
6196your output program.  If the @code{ABSOLUTE} command is used at all in a
6197script, then only the sections named explicitly in @code{ABSOLUTE}
6198commands will appear in the linker output.  You can still use other
6199input sections (whatever you select on the command line, or using
6200@code{LOAD}) to resolve addresses in the output file.
6201
6202@cindex @code{ALIAS} (MRI)
6203@item ALIAS @var{out-secname}, @var{in-secname}
6204Use this command to place the data from input section @var{in-secname}
6205in a section called @var{out-secname} in the linker output file.
6206
6207@var{in-secname} may be an integer.
6208
6209@cindex @code{ALIGN} (MRI)
6210@item ALIGN @var{secname} = @var{expression}
6211Align the section called @var{secname} to @var{expression}.  The
6212@var{expression} should be a power of two.
6213
6214@cindex @code{BASE} (MRI)
6215@item BASE @var{expression}
6216Use the value of @var{expression} as the lowest address (other than
6217absolute addresses) in the output file.
6218
6219@cindex @code{CHIP} (MRI)
6220@item CHIP @var{expression}
6221@itemx CHIP @var{expression}, @var{expression}
6222This command does nothing; it is accepted only for compatibility.
6223
6224@cindex @code{END} (MRI)
6225@item END
6226This command does nothing whatever; it's only accepted for compatibility.
6227
6228@cindex @code{FORMAT} (MRI)
6229@item FORMAT @var{output-format}
6230Similar to the @code{OUTPUT_FORMAT} command in the more general linker
6231language, but restricted to one of these output formats:
6232
6233@enumerate
6234@item
6235S-records, if @var{output-format} is @samp{S}
6236
6237@item
6238IEEE, if @var{output-format} is @samp{IEEE}
6239
6240@item
6241COFF (the @samp{coff-m68k} variant in BFD), if @var{output-format} is
6242@samp{COFF}
6243@end enumerate
6244
6245@cindex @code{LIST} (MRI)
6246@item LIST @var{anything}@dots{}
6247Print (to the standard output file) a link map, as produced by the
6248@command{ld} command-line option @samp{-M}.
6249
6250The keyword @code{LIST} may be followed by anything on the
6251same line, with no change in its effect.
6252
6253@cindex @code{LOAD} (MRI)
6254@item LOAD @var{filename}
6255@itemx LOAD @var{filename}, @var{filename}, @dots{} @var{filename}
6256Include one or more object file @var{filename} in the link; this has the
6257same effect as specifying @var{filename} directly on the @command{ld}
6258command line.
6259
6260@cindex @code{NAME} (MRI)
6261@item NAME @var{output-name}
6262@var{output-name} is the name for the program produced by @command{ld}; the
6263MRI-compatible command @code{NAME} is equivalent to the command-line
6264option @samp{-o} or the general script language command @code{OUTPUT}.
6265
6266@cindex @code{ORDER} (MRI)
6267@item ORDER @var{secname}, @var{secname}, @dots{} @var{secname}
6268@itemx ORDER @var{secname} @var{secname} @var{secname}
6269Normally, @command{ld} orders the sections in its output file in the
6270order in which they first appear in the input files.  In an MRI-compatible
6271script, you can override this ordering with the @code{ORDER} command.  The
6272sections you list with @code{ORDER} will appear first in your output
6273file, in the order specified.
6274
6275@cindex @code{PUBLIC} (MRI)
6276@item PUBLIC @var{name}=@var{expression}
6277@itemx PUBLIC @var{name},@var{expression}
6278@itemx PUBLIC @var{name} @var{expression}
6279Supply a value (@var{expression}) for external symbol
6280@var{name} used in the linker input files.
6281
6282@cindex @code{SECT} (MRI)
6283@item SECT @var{secname}, @var{expression}
6284@itemx SECT @var{secname}=@var{expression}
6285@itemx SECT @var{secname} @var{expression}
6286You can use any of these three forms of the @code{SECT} command to
6287specify the start address (@var{expression}) for section @var{secname}.
6288If you have more than one @code{SECT} statement for the same
6289@var{secname}, only the @emph{first} sets the start address.
6290@end table
6291
6292@include fdl.texi
6293
6294@node Index
6295@unnumbered Index
6296
6297@printindex cp
6298
6299@tex
6300% I think something like @colophon should be in texinfo.  In the
6301% meantime:
6302\long\def\colophon{\hbox to0pt{}\vfill
6303\centerline{The body of this manual is set in}
6304\centerline{\fontname\tenrm,}
6305\centerline{with headings in {\bf\fontname\tenbf}}
6306\centerline{and examples in {\tt\fontname\tentt}.}
6307\centerline{{\it\fontname\tenit\/} and}
6308\centerline{{\sl\fontname\tensl\/}}
6309\centerline{are used for emphasis.}\vfill}
6310\page\colophon
6311% Blame: doc@cygnus.com, 28mar91.
6312@end tex
6313
6314
6315@contents
6316@bye
6317