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