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