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