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