Use macros for TUI window names
[deliverable/binutils-gdb.git] / ld / ld.texi
CommitLineData
252b5132
RH
1\input texinfo
2@setfilename ld.info
b3adc24a 3@c Copyright (C) 1991-2020 Free Software Foundation, Inc.
252b5132 4@syncodeindex ky cp
dff70155 5@c man begin INCLUDE
252b5132
RH
6@include configdoc.texi
7@c (configdoc.texi is generated by the Makefile)
c428fa83 8@include bfdver.texi
dff70155 9@c man end
252b5132
RH
10
11@c @smallbook
12
ff5dcc92
SC
13@macro gcctabopt{body}
14@code{\body\}
15@end macro
16
0285c67d
NC
17@c man begin NAME
18@ifset man
19@c Configure for the generation of man pages
20@set UsesEnvVars
21@set GENERIC
0285c67d 22@set ARM
ac145307 23@set C6X
b8891f8d 24@set CSKY
49fa1e15 25@set H8300
0285c67d 26@set HPPA
0285c67d 27@set M68HC11
7fb9f789 28@set M68K
833794fc 29@set MIPS
3c3bdf30 30@set MMIX
2469cfa2 31@set MSP430
35c08157 32@set NDS32
78058a5e 33@set NIOSII
fa1477dc 34@set PDP11
2a60a7a8
AM
35@set POWERPC
36@set POWERPC64
49fa1e15 37@set Renesas
b4cbbe8f 38@set S/390
49fa1e15
AM
39@set SPU
40@set TICOFF
2ca22b03 41@set WIN32
e0001a05 42@set XTENSA
0285c67d
NC
43@end ifset
44@c man end
45
9160ea82
AM
46@ifnottex
47@dircategory Software development
48@direntry
252b5132 49* Ld: (ld). The GNU linker.
9160ea82
AM
50@end direntry
51@end ifnottex
252b5132 52
0e9517a9 53@copying
e49e529d
JM
54This file documents the @sc{gnu} linker LD
55@ifset VERSION_PACKAGE
56@value{VERSION_PACKAGE}
57@end ifset
58version @value{VERSION}.
252b5132 59
b3adc24a 60Copyright @copyright{} 1991-2020 Free Software Foundation, Inc.
252b5132 61
cf055d54 62Permission is granted to copy, distribute and/or modify this document
793c5807 63under the terms of the GNU Free Documentation License, Version 1.3
cf055d54
NC
64or any later version published by the Free Software Foundation;
65with no Invariant Sections, with no Front-Cover Texts, and with no
66Back-Cover Texts. A copy of the license is included in the
36f63dca 67section entitled ``GNU Free Documentation License''.
0e9517a9 68@end copying
252b5132
RH
69@iftex
70@finalout
71@setchapternewpage odd
71ba23f6 72@settitle The GNU linker
252b5132 73@titlepage
71ba23f6 74@title The GNU linker
252b5132 75@sp 1
e49e529d
JM
76@subtitle @code{ld}
77@ifset VERSION_PACKAGE
78@subtitle @value{VERSION_PACKAGE}
79@end ifset
252b5132
RH
80@subtitle Version @value{VERSION}
81@author Steve Chamberlain
82@author Ian Lance Taylor
252b5132
RH
83@page
84
85@tex
86{\parskip=0pt
704c465c
NC
87\hfill Red Hat Inc\par
88\hfill nickc\@credhat.com, doc\@redhat.com\par
71ba23f6 89\hfill {\it The GNU linker}\par
252b5132
RH
90\hfill Edited by Jeffrey Osier (jeffrey\@cygnus.com)\par
91}
92\global\parindent=0pt % Steve likes it this way.
93@end tex
94
95@vskip 0pt plus 1filll
0285c67d 96@c man begin COPYRIGHT
b3adc24a 97Copyright @copyright{} 1991-2020 Free Software Foundation, Inc.
252b5132 98
0285c67d 99Permission is granted to copy, distribute and/or modify this document
793c5807 100under the terms of the GNU Free Documentation License, Version 1.3
0285c67d
NC
101or any later version published by the Free Software Foundation;
102with no Invariant Sections, with no Front-Cover Texts, and with no
103Back-Cover Texts. A copy of the license is included in the
36f63dca 104section entitled ``GNU Free Documentation License''.
0285c67d 105@c man end
252b5132 106
252b5132
RH
107@end titlepage
108@end iftex
4ecceb71 109@contents
252b5132
RH
110@c FIXME: Talk about importance of *order* of args, cmds to linker!
111
84ec0e6d 112@ifnottex
252b5132 113@node Top
71ba23f6 114@top LD
e49e529d
JM
115This file documents the @sc{gnu} linker ld
116@ifset VERSION_PACKAGE
117@value{VERSION_PACKAGE}
118@end ifset
119version @value{VERSION}.
252b5132 120
cf055d54 121This document is distributed under the terms of the GNU Free
793c5807
NC
122Documentation License version 1.3. A copy of the license is included
123in the section entitled ``GNU Free Documentation License''.
cf055d54 124
252b5132
RH
125@menu
126* Overview:: Overview
127* Invocation:: Invocation
128* Scripts:: Linker Scripts
129@ifset GENERIC
130* Machine Dependent:: Machine Dependent Features
131@end ifset
132@ifclear GENERIC
133@ifset H8300
134* H8/300:: ld and the H8/300
135@end ifset
c2dcd04e
NC
136@ifset Renesas
137* Renesas:: ld and other Renesas micros
252b5132 138@end ifset
36f63dca
NC
139@ifset ARM
140* ARM:: ld and the ARM family
141@end ifset
93fd0973
SC
142@ifset M68HC11
143* M68HC11/68HC12:: ld and the Motorola 68HC11 and 68HC12 families
144@end ifset
7ca01ed9
NC
145@ifset HPPA
146* HPPA ELF32:: ld and HPPA 32-bit ELF
147@end ifset
7fb9f789
NC
148@ifset M68K
149* M68K:: ld and Motorola 68K family
150@end ifset
833794fc
MR
151@ifset MIPS
152* MIPS:: ld and MIPS family
153@end ifset
2a60a7a8
AM
154@ifset POWERPC
155* PowerPC ELF32:: ld and PowerPC 32-bit ELF Support
156@end ifset
157@ifset POWERPC64
158* PowerPC64 ELF64:: ld and PowerPC64 64-bit ELF Support
159@end ifset
b4cbbe8f
AK
160@ifset S/390
161* S/390 ELF:: ld and S/390 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
a05a5b64 252* Options:: Command-line Options
252b5132
RH
253* Environment:: Environment Variables
254@end menu
255
256@node Options
a05a5b64 257@section Command-line Options
252b5132
RH
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
53d25da6
AM
309@code{INPUT} or @code{GROUP} to load other objects. Specifying a
310script in this way merely augments the main linker script, with the
311extra commands placed after the main script; use the @samp{-T} option
312to replace the default linker script entirely, but note the effect of
313the @code{INSERT} command. @xref{Scripts}.
252b5132
RH
314
315For options whose names are a single letter,
316option arguments must either follow the option letter without intervening
317whitespace, or be given as separate arguments immediately following the
318option that requires them.
319
320For options whose names are multiple letters, either one dash or two can
e4897a32 321precede the option name; for example, @samp{-trace-symbol} and
36f63dca 322@samp{--trace-symbol} are equivalent. Note---there is one exception to
e4897a32 323this rule. Multiple letter options that start with a lower case 'o' can
ba1be17e 324only be preceded by two dashes. This is to reduce confusion with the
e4897a32
NC
325@samp{-o} option. So for example @samp{-omagic} sets the output file
326name to @samp{magic} whereas @samp{--omagic} sets the NMAGIC flag on the
327output.
328
329Arguments to multiple-letter options must either be separated from the
330option name by an equals sign, or be given as separate arguments
331immediately following the option that requires them. For example,
332@samp{--trace-symbol foo} and @samp{--trace-symbol=foo} are equivalent.
333Unique abbreviations of the names of multiple-letter options are
334accepted.
252b5132 335
36f63dca 336Note---if the linker is being invoked indirectly, via a compiler driver
a05a5b64 337(e.g. @samp{gcc}) then all the linker command-line options should be
fa19fce0
NC
338prefixed by @samp{-Wl,} (or whatever is appropriate for the particular
339compiler driver) like this:
4e53152f
NC
340
341@smallexample
2509a395 342 gcc -Wl,--start-group foo.o bar.o -Wl,--end-group
4e53152f
NC
343@end smallexample
344
345This is important, because otherwise the compiler driver program may
2509a395
SL
346silently drop the linker options, resulting in a bad link. Confusion
347may also arise when passing options that require values through a
348driver, as the use of a space between option and argument acts as
349a separator, and causes the driver to pass only the option to the linker
350and the argument to the compiler. In this case, it is simplest to use
351the joined forms of both single- and multiple-letter options, such as:
352
353@smallexample
354 gcc foo.o bar.o -Wl,-eENTRY -Wl,-Map=a.map
355@end smallexample
4e53152f 356
a05a5b64 357Here is a table of the generic command-line switches accepted by the GNU
4e53152f
NC
358linker:
359
ff5dcc92 360@table @gcctabopt
38fc1cb1 361@include at-file.texi
dff70155 362
2509a395
SL
363@kindex -a @var{keyword}
364@item -a @var{keyword}
252b5132
RH
365This option is supported for HP/UX compatibility. The @var{keyword}
366argument must be one of the strings @samp{archive}, @samp{shared}, or
367@samp{default}. @samp{-aarchive} is functionally equivalent to
368@samp{-Bstatic}, and the other two keywords are functionally equivalent
369to @samp{-Bdynamic}. This option may be used any number of times.
370
7ee314fa
AM
371@kindex --audit @var{AUDITLIB}
372@item --audit @var{AUDITLIB}
373Adds @var{AUDITLIB} to the @code{DT_AUDIT} entry of the dynamic section.
374@var{AUDITLIB} is not checked for existence, nor will it use the DT_SONAME
375specified in the library. If specified multiple times @code{DT_AUDIT}
376will contain a colon separated list of audit interfaces to use. If the linker
377finds an object with an audit entry while searching for shared libraries,
9d5777a3 378it will add a corresponding @code{DT_DEPAUDIT} entry in the output file.
7ee314fa 379This option is only meaningful on ELF platforms supporting the rtld-audit
9d5777a3 380interface.
7ee314fa 381
252b5132
RH
382@ifclear SingleFormat
383@cindex binary input format
384@kindex -b @var{format}
385@kindex --format=@var{format}
386@cindex input format
387@cindex input format
388@item -b @var{input-format}
389@itemx --format=@var{input-format}
ff5dcc92
SC
390@command{ld} may be configured to support more than one kind of object
391file. If your @command{ld} is configured this way, you can use the
252b5132 392@samp{-b} option to specify the binary format for input object files
ff5dcc92 393that follow this option on the command line. Even when @command{ld} is
252b5132 394configured to support alternative object formats, you don't usually need
ff5dcc92 395to specify this, as @command{ld} should be configured to expect as a
252b5132
RH
396default input format the most usual format on each machine.
397@var{input-format} is a text string, the name of a particular format
398supported by the BFD libraries. (You can list the available binary
399formats with @samp{objdump -i}.)
400@xref{BFD}.
401
402You may want to use this option if you are linking files with an unusual
403binary format. You can also use @samp{-b} to switch formats explicitly (when
404linking object files of different formats), by including
405@samp{-b @var{input-format}} before each group of object files in a
a1ab1d2a 406particular format.
252b5132
RH
407
408The default format is taken from the environment variable
409@code{GNUTARGET}.
410@ifset UsesEnvVars
411@xref{Environment}.
412@end ifset
413You can also define the input format from a script, using the command
0285c67d
NC
414@code{TARGET};
415@ifclear man
416see @ref{Format Commands}.
417@end ifclear
252b5132
RH
418@end ifclear
419
420@kindex -c @var{MRI-cmdfile}
421@kindex --mri-script=@var{MRI-cmdfile}
422@cindex compatibility, MRI
423@item -c @var{MRI-commandfile}
424@itemx --mri-script=@var{MRI-commandfile}
ff5dcc92 425For compatibility with linkers produced by MRI, @command{ld} accepts script
252b5132 426files written in an alternate, restricted command language, described in
0285c67d
NC
427@ifclear man
428@ref{MRI,,MRI Compatible Script Files}.
429@end ifclear
430@ifset man
431the MRI Compatible Script Files section of GNU ld documentation.
432@end ifset
433Introduce MRI script files with
252b5132 434the option @samp{-c}; use the @samp{-T} option to run linker
ff5dcc92
SC
435scripts written in the general-purpose @command{ld} scripting language.
436If @var{MRI-cmdfile} does not exist, @command{ld} looks for it in the directories
252b5132
RH
437specified by any @samp{-L} options.
438
439@cindex common allocation
440@kindex -d
441@kindex -dc
442@kindex -dp
a1ab1d2a 443@item -d
252b5132
RH
444@itemx -dc
445@itemx -dp
446These three options are equivalent; multiple forms are supported for
447compatibility with other linkers. They assign space to common symbols
448even if a relocatable output file is specified (with @samp{-r}). The
449script command @code{FORCE_COMMON_ALLOCATION} has the same effect.
450@xref{Miscellaneous Commands}.
451
7ee314fa
AM
452@kindex --depaudit @var{AUDITLIB}
453@kindex -P @var{AUDITLIB}
454@item --depaudit @var{AUDITLIB}
455@itemx -P @var{AUDITLIB}
456Adds @var{AUDITLIB} to the @code{DT_DEPAUDIT} entry of the dynamic section.
457@var{AUDITLIB} is not checked for existence, nor will it use the DT_SONAME
458specified in the library. If specified multiple times @code{DT_DEPAUDIT}
459will contain a colon separated list of audit interfaces to use. This
460option is only meaningful on ELF platforms supporting the rtld-audit interface.
9d5777a3 461The -P option is provided for Solaris compatibility.
7ee314fa 462
abf874aa
CL
463@kindex --enable-non-contiguous-regions
464@item --enable-non-contiguous-regions
465This option avoids generating an error if an input section does not
466fit a matching output section. The linker tries to allocate the input
467section to subseque nt matching output sections, and generates an
468error only if no output section is large enough. This is useful when
469several non-contiguous memory regions are available and the input
470section does not require a particular one. The order in which input
471sections are evaluated does not change, for instance:
472
473@smallexample
474 MEMORY @{
475 MEM1 (rwx) : ORIGIN : 0x1000, LENGTH = 0x14
476 MEM2 (rwx) : ORIGIN : 0x1000, LENGTH = 0x40
477 MEM3 (rwx) : ORIGIN : 0x2000, LENGTH = 0x40
478 @}
479 SECTIONS @{
480 mem1 : @{ *(.data.*); @} > MEM1
481 mem2 : @{ *(.data.*); @} > MEM2
482 mem3 : @{ *(.data.*); @} > MEM2
483 @}
484
485 with input sections:
486 .data.1: size 8
487 .data.2: size 0x10
488 .data.3: size 4
489
490 results in .data.1 affected to mem1, and .data.2 and .data.3
491 affected to mem2, even though .data.3 would fit in mem3.
492@end smallexample
493
494This option is incompatible with INSERT statements because it changes
495the way input sections are mapped to output sections.
496
497@kindex --enable-non-contiguous-regions-warnings
498@item --enable-non-contiguous-regions-warnings
499This option enables warnings when
500@code{--enable-non-contiguous-regions} allows possibly unexpected
501matches in sections mapping, potentially leading to silently
502discarding a section instead of failing because it does not fit any
503output region.
504
252b5132
RH
505@cindex entry point, from command line
506@kindex -e @var{entry}
507@kindex --entry=@var{entry}
a1ab1d2a 508@item -e @var{entry}
252b5132
RH
509@itemx --entry=@var{entry}
510Use @var{entry} as the explicit symbol for beginning execution of your
511program, rather than the default entry point. If there is no symbol
512named @var{entry}, the linker will try to parse @var{entry} as a number,
513and use that as the entry address (the number will be interpreted in
514base 10; you may use a leading @samp{0x} for base 16, or a leading
515@samp{0} for base 8). @xref{Entry Point}, for a discussion of defaults
516and other ways of specifying the entry point.
517
b58f81ae
DJ
518@kindex --exclude-libs
519@item --exclude-libs @var{lib},@var{lib},...
520Specifies a list of archive libraries from which symbols should not be automatically
e1c37eb5 521exported. The library names may be delimited by commas or colons. Specifying
b58f81ae
DJ
522@code{--exclude-libs ALL} excludes symbols in all archive libraries from
523automatic export. This option is available only for the i386 PE targeted
524port of the linker and for ELF targeted ports. For i386 PE, symbols
525explicitly listed in a .def file are still exported, regardless of this
526option. For ELF targeted ports, symbols affected by this option will
527be treated as hidden.
528
e1c37eb5
DK
529@kindex --exclude-modules-for-implib
530@item --exclude-modules-for-implib @var{module},@var{module},...
531Specifies a list of object files or archive members, from which symbols
532should not be automatically exported, but which should be copied wholesale
533into the import library being generated during the link. The module names
534may be delimited by commas or colons, and must match exactly the filenames
535used by @command{ld} to open the files; for archive members, this is simply
536the member name, but for object files the name listed must include and
537match precisely any path used to specify the input file on the linker's
538command-line. This option is available only for the i386 PE targeted port
539of the linker. Symbols explicitly listed in a .def file are still exported,
540regardless of this option.
541
252b5132
RH
542@cindex dynamic symbol table
543@kindex -E
544@kindex --export-dynamic
267e2722 545@kindex --no-export-dynamic
252b5132
RH
546@item -E
547@itemx --export-dynamic
267e2722
CD
548@itemx --no-export-dynamic
549When creating a dynamically linked executable, using the @option{-E}
550option or the @option{--export-dynamic} option causes the linker to add
551all symbols to the dynamic symbol table. The dynamic symbol table is the
552set of symbols which are visible from dynamic objects at run time.
553
554If you do not use either of these options (or use the
555@option{--no-export-dynamic} option to restore the default behavior), the
556dynamic symbol table will normally contain only those symbols which are
557referenced by some dynamic object mentioned in the link.
252b5132
RH
558
559If you use @code{dlopen} to load a dynamic object which needs to refer
560back to the symbols defined by the program, rather than some other
561dynamic object, then you will probably need to use this option when
562linking the program itself.
563
55255dae 564You can also use the dynamic list to control what symbols should
cb840a31 565be added to the dynamic symbol table if the output format supports it.
55255dae 566See the description of @samp{--dynamic-list}.
cb840a31 567
8b747e1a
DK
568Note that this option is specific to ELF targeted ports. PE targets
569support a similar function to export all symbols from a DLL or EXE; see
570the description of @samp{--export-all-symbols} below.
571
37a141bf
FS
572@kindex --export-dynamic-symbol=@var{glob}
573@cindex export dynamic symbol
574@item --export-dynamic-symbol=@var{glob}
575When creating a dynamically linked executable, symbols matching
576@var{glob} will be added to the dynamic symbol table. When creating a
577shared library, references to symbols matching @var{glob} will not be
578bound to the definitions within the shared library. This option is a
579no-op when creating a shared library and @samp{-Bsymbolic} or
580@samp{--dynamic-list} are not specified. This option is only meaningful
581on ELF platforms which support shared libraries.
582
583@kindex --export-dynamic-symbol-list=@var{file}
584@cindex export dynamic symbol list
585@item --export-dynamic-symbol-list=@var{file}
586Specify a @samp{--export-dynamic-symbol} for each pattern in the file.
587The format of the file is the same as the version node without
588scope and node name. See @ref{VERSION} for more information.
589
36f63dca 590@ifclear SingleFormat
252b5132
RH
591@cindex big-endian objects
592@cindex endianness
593@kindex -EB
594@item -EB
595Link big-endian objects. This affects the default output format.
596
597@cindex little-endian objects
598@kindex -EL
599@item -EL
600Link little-endian objects. This affects the default output format.
36f63dca 601@end ifclear
252b5132 602
2509a395
SL
603@kindex -f @var{name}
604@kindex --auxiliary=@var{name}
605@item -f @var{name}
606@itemx --auxiliary=@var{name}
252b5132
RH
607When creating an ELF shared object, set the internal DT_AUXILIARY field
608to the specified name. This tells the dynamic linker that the symbol
609table of the shared object should be used as an auxiliary filter on the
610symbol table of the shared object @var{name}.
611
612If you later link a program against this filter object, then, when you
613run the program, the dynamic linker will see the DT_AUXILIARY field. If
614the dynamic linker resolves any symbols from the filter object, it will
615first check whether there is a definition in the shared object
616@var{name}. If there is one, it will be used instead of the definition
617in the filter object. The shared object @var{name} need not exist.
618Thus the shared object @var{name} may be used to provide an alternative
619implementation of certain functions, perhaps for debugging or for
a094d01f 620machine-specific performance.
252b5132
RH
621
622This option may be specified more than once. The DT_AUXILIARY entries
623will be created in the order in which they appear on the command line.
624
2509a395
SL
625@kindex -F @var{name}
626@kindex --filter=@var{name}
252b5132 627@item -F @var{name}
2509a395 628@itemx --filter=@var{name}
252b5132
RH
629When creating an ELF shared object, set the internal DT_FILTER field to
630the specified name. This tells the dynamic linker that the symbol table
631of the shared object which is being created should be used as a filter
632on the symbol table of the shared object @var{name}.
633
634If you later link a program against this filter object, then, when you
635run the program, the dynamic linker will see the DT_FILTER field. The
636dynamic linker will resolve symbols according to the symbol table of the
637filter object as usual, but it will actually link to the definitions
638found in the shared object @var{name}. Thus the filter object can be
639used to select a subset of the symbols provided by the object
640@var{name}.
641
ff5dcc92 642Some older linkers used the @option{-F} option throughout a compilation
252b5132 643toolchain for specifying object-file format for both input and output
36f63dca
NC
644object files.
645@ifclear SingleFormat
646The @sc{gnu} linker uses other mechanisms for this purpose: the
ece2d90e 647@option{-b}, @option{--format}, @option{--oformat} options, the
252b5132 648@code{TARGET} command in linker scripts, and the @code{GNUTARGET}
36f63dca
NC
649environment variable.
650@end ifclear
651The @sc{gnu} linker will ignore the @option{-F} option when not
652creating an ELF shared object.
252b5132 653
3dbf70a2 654@cindex finalization function
2509a395
SL
655@kindex -fini=@var{name}
656@item -fini=@var{name}
3dbf70a2
MM
657When creating an ELF executable or shared object, call NAME when the
658executable or shared object is unloaded, by setting DT_FINI to the
659address of the function. By default, the linker uses @code{_fini} as
660the function to call.
661
252b5132
RH
662@kindex -g
663@item -g
664Ignored. Provided for compatibility with other tools.
665
2509a395
SL
666@kindex -G @var{value}
667@kindex --gpsize=@var{value}
252b5132 668@cindex object size
2509a395 669@item -G @var{value}
252b5132
RH
670@itemx --gpsize=@var{value}
671Set the maximum size of objects to be optimized using the GP register to
672@var{size}. This is only meaningful for object file formats such as
e8044f35 673MIPS ELF that support putting large and small objects into different
252b5132
RH
674sections. This is ignored for other object file formats.
675
676@cindex runtime library name
2509a395 677@kindex -h @var{name}
252b5132 678@kindex -soname=@var{name}
2509a395 679@item -h @var{name}
252b5132
RH
680@itemx -soname=@var{name}
681When creating an ELF shared object, set the internal DT_SONAME field to
682the specified name. When an executable is linked with a shared object
683which has a DT_SONAME field, then when the executable is run the dynamic
684linker will attempt to load the shared object specified by the DT_SONAME
685field rather than the using the file name given to the linker.
686
687@kindex -i
688@cindex incremental link
689@item -i
690Perform an incremental link (same as option @samp{-r}).
691
3dbf70a2 692@cindex initialization function
2509a395
SL
693@kindex -init=@var{name}
694@item -init=@var{name}
3dbf70a2
MM
695When creating an ELF executable or shared object, call NAME when the
696executable or shared object is loaded, by setting DT_INIT to the address
697of the function. By default, the linker uses @code{_init} as the
698function to call.
699
252b5132 700@cindex archive files, from cmd line
2509a395 701@kindex -l @var{namespec}
bcb674cf 702@kindex --library=@var{namespec}
2509a395 703@item -l @var{namespec}
bcb674cf
RS
704@itemx --library=@var{namespec}
705Add the archive or object file specified by @var{namespec} to the
706list of files to link. This option may be used any number of times.
707If @var{namespec} is of the form @file{:@var{filename}}, @command{ld}
07d8eb55 708will search the library path for a file called @var{filename}, otherwise it
bcb674cf 709will search the library path for a file called @file{lib@var{namespec}.a}.
252b5132 710
ff5dcc92 711On systems which support shared libraries, @command{ld} may also search for
bcb674cf
RS
712files other than @file{lib@var{namespec}.a}. Specifically, on ELF
713and SunOS systems, @command{ld} will search a directory for a library
714called @file{lib@var{namespec}.so} before searching for one called
715@file{lib@var{namespec}.a}. (By convention, a @code{.so} extension
716indicates a shared library.) Note that this behavior does not apply
717to @file{:@var{filename}}, which always specifies a file called
718@var{filename}.
252b5132
RH
719
720The linker will search an archive only once, at the location where it is
721specified on the command line. If the archive defines a symbol which
722was undefined in some object which appeared before the archive on the
723command line, the linker will include the appropriate file(s) from the
724archive. However, an undefined symbol in an object appearing later on
725the command line will not cause the linker to search the archive again.
726
ff5dcc92 727See the @option{-(} option for a way to force the linker to search
252b5132
RH
728archives multiple times.
729
730You may list the same archive multiple times on the command line.
731
732@ifset GENERIC
733This type of archive searching is standard for Unix linkers. However,
ff5dcc92 734if you are using @command{ld} on AIX, note that it is different from the
252b5132
RH
735behaviour of the AIX linker.
736@end ifset
737
738@cindex search directory, from cmd line
2509a395 739@kindex -L @var{dir}
252b5132 740@kindex --library-path=@var{dir}
2509a395 741@item -L @var{searchdir}
252b5132 742@itemx --library-path=@var{searchdir}
ff5dcc92
SC
743Add path @var{searchdir} to the list of paths that @command{ld} will search
744for archive libraries and @command{ld} control scripts. You may use this
252b5132
RH
745option any number of times. The directories are searched in the order
746in which they are specified on the command line. Directories specified
747on the command line are searched before the default directories. All
ff5dcc92 748@option{-L} options apply to all @option{-l} options, regardless of the
7d24f02c
KH
749order in which the options appear. @option{-L} options do not affect
750how @command{ld} searches for a linker script unless @option{-T}
751option is specified.
252b5132 752
3aa2d05a
NC
753If @var{searchdir} begins with @code{=} or @code{$SYSROOT}, then this
754prefix will be replaced by the @dfn{sysroot prefix}, controlled by the
755@samp{--sysroot} option, or specified when the linker is configured.
9c8ebd6a 756
252b5132
RH
757@ifset UsesEnvVars
758The default set of paths searched (without being specified with
ff5dcc92 759@samp{-L}) depends on which emulation mode @command{ld} is using, and in
252b5132
RH
760some cases also on how it was configured. @xref{Environment}.
761@end ifset
762
763The paths can also be specified in a link script with the
764@code{SEARCH_DIR} command. Directories specified this way are searched
765at the point in which the linker script appears in the command line.
766
767@cindex emulation
768@kindex -m @var{emulation}
2509a395 769@item -m @var{emulation}
252b5132
RH
770Emulate the @var{emulation} linker. You can list the available
771emulations with the @samp{--verbose} or @samp{-V} options.
772
773If the @samp{-m} option is not used, the emulation is taken from the
774@code{LDEMULATION} environment variable, if that is defined.
775
776Otherwise, the default emulation depends upon how the linker was
777configured.
778
779@cindex link map
780@kindex -M
781@kindex --print-map
782@item -M
783@itemx --print-map
784Print a link map to the standard output. A link map provides
785information about the link, including the following:
786
787@itemize @bullet
788@item
3b83e13a 789Where object files are mapped into memory.
252b5132
RH
790@item
791How common symbols are allocated.
792@item
793All archive members included in the link, with a mention of the symbol
794which caused the archive member to be brought in.
3b83e13a
NC
795@item
796The values assigned to symbols.
797
798Note - symbols whose values are computed by an expression which
799involves a reference to a previous value of the same symbol may not
800have correct result displayed in the link map. This is because the
801linker discards intermediate results and only retains the final value
802of an expression. Under such circumstances the linker will display
803the final value enclosed by square brackets. Thus for example a
804linker script containing:
805
806@smallexample
807 foo = 1
808 foo = foo * 4
809 foo = foo + 8
810@end smallexample
811
812will produce the following output in the link map if the @option{-M}
813option is used:
814
815@smallexample
816 0x00000001 foo = 0x1
817 [0x0000000c] foo = (foo * 0x4)
818 [0x0000000c] foo = (foo + 0x8)
819@end smallexample
820
821See @ref{Expressions} for more information about expressions in linker
822scripts.
d2ef37eb 823
06ddcada
AM
824@item
825How GNU properties are merged.
d2ef37eb 826
06ddcada
AM
827When the linker merges input .note.gnu.property sections into one output
828.note.gnu.property section, some properties are removed or updated.
829These actions are reported in the link map. For example:
d2ef37eb
L
830
831@smallexample
832Removed property 0xc0000002 to merge foo.o (0x1) and bar.o (not found)
833@end smallexample
834
06ddcada 835This indicates that property 0xc0000002 is removed from output when
d2ef37eb
L
836merging properties in @file{foo.o}, whose property 0xc0000002 value
837is 0x1, and @file{bar.o}, which doesn't have property 0xc0000002.
838
839@smallexample
06ddcada 840Updated property 0xc0010001 (0x1) to merge foo.o (0x1) and bar.o (0x1)
d2ef37eb
L
841@end smallexample
842
06ddcada 843This indicates that property 0xc0010001 value is updated to 0x1 in output
d2ef37eb
L
844when merging properties in @file{foo.o}, whose 0xc0010001 property value
845is 0x1, and @file{bar.o}, whose 0xc0010001 property value is 0x1.
252b5132
RH
846@end itemize
847
035801ce
FS
848@cindex link map discarded
849@kindex --print-map-discarded
850@kindex --no-print-map-discarded
851@item --print-map-discarded
852@itemx --no-print-map-discarded
853Print (or do not print) the list of discarded and garbage collected sections
854in the link map. Enabled by default.
855
252b5132
RH
856@kindex -n
857@cindex read-only text
858@cindex NMAGIC
859@kindex --nmagic
860@item -n
861@itemx --nmagic
90f5d9d9
JZ
862Turn off page alignment of sections, and disable linking against shared
863libraries. If the output format supports Unix style magic numbers,
864mark the output as @code{NMAGIC}.
252b5132
RH
865
866@kindex -N
867@kindex --omagic
868@cindex read/write from cmd line
869@cindex OMAGIC
a1ab1d2a 870@item -N
252b5132
RH
871@itemx --omagic
872Set the text and data sections to be readable and writable. Also, do
63fd3b82
NC
873not page-align the data segment, and disable linking against shared
874libraries. If the output format supports Unix style magic numbers,
4d8907ac
DS
875mark the output as @code{OMAGIC}. Note: Although a writable text section
876is allowed for PE-COFF targets, it does not conform to the format
877specification published by Microsoft.
63fd3b82
NC
878
879@kindex --no-omagic
880@cindex OMAGIC
881@item --no-omagic
882This option negates most of the effects of the @option{-N} option. It
883sets the text section to be read-only, and forces the data segment to
884be page-aligned. Note - this option does not enable linking against
885shared libraries. Use @option{-Bdynamic} for this.
252b5132
RH
886
887@kindex -o @var{output}
888@kindex --output=@var{output}
889@cindex naming the output file
890@item -o @var{output}
891@itemx --output=@var{output}
ff5dcc92 892Use @var{output} as the name for the program produced by @command{ld}; if this
252b5132
RH
893option is not specified, the name @file{a.out} is used by default. The
894script command @code{OUTPUT} can also specify the output file name.
895
896@kindex -O @var{level}
897@cindex generating optimized output
898@item -O @var{level}
ff5dcc92 899If @var{level} is a numeric values greater than zero @command{ld} optimizes
252b5132 900the output. This might take significantly longer and therefore probably
98c503ac
NC
901should only be enabled for the final binary. At the moment this
902option only affects ELF shared library generation. Future releases of
903the linker may make more use of this option. Also currently there is
904no difference in the linker's behaviour for different non-zero values
905of this option. Again this may change with future releases.
252b5132 906
387dd777
DP
907@kindex -plugin @var{name}
908@item -plugin @var{name}
909Involve a plugin in the linking process. The @var{name} parameter is
910the absolute filename of the plugin. Usually this parameter is
911automatically added by the complier, when using link time
912optimization, but users can also add their own plugins if they so
913wish.
914
915Note that the location of the compiler originated plugins is different
916from the place where the @command{ar}, @command{nm} and
917@command{ranlib} programs search for their plugins. In order for
918those commands to make use of a compiler based plugin it must first be
41f37a6f 919copied into the @file{$@{libdir@}/bfd-plugins} directory. All gcc
387dd777 920based linker plugins are backward compatible, so it is sufficient to
6c19b93b 921just copy in the newest one.
387dd777 922
26278bb8
UD
923@kindex --push-state
924@cindex push state governing input file handling
925@item --push-state
926The @option{--push-state} allows to preserve the current state of the
927flags which govern the input file handling so that they can all be
928restored with one corresponding @option{--pop-state} option.
929
930The option which are covered are: @option{-Bdynamic}, @option{-Bstatic},
931@option{-dn}, @option{-dy}, @option{-call_shared}, @option{-non_shared},
932@option{-static}, @option{-N}, @option{-n}, @option{--whole-archive},
933@option{--no-whole-archive}, @option{-r}, @option{-Ur},
934@option{--copy-dt-needed-entries}, @option{--no-copy-dt-needed-entries},
935@option{--as-needed}, @option{--no-as-needed}, and @option{-a}.
936
937One target for this option are specifications for @file{pkg-config}. When
938used with the @option{--libs} option all possibly needed libraries are
939listed and then possibly linked with all the time. It is better to return
940something as follows:
941
942@smallexample
943-Wl,--push-state,--as-needed -libone -libtwo -Wl,--pop-state
944@end smallexample
945
946@kindex --pop-state
947@cindex pop state governing input file handling
67cecaba 948@item --pop-state
26278bb8
UD
949Undoes the effect of --push-state, restores the previous values of the
950flags governing input file handling.
951
a712da20
NC
952@kindex -q
953@kindex --emit-relocs
954@cindex retain relocations in final executable
955@item -q
956@itemx --emit-relocs
ba1be17e 957Leave relocation sections and contents in fully linked executables.
a712da20
NC
958Post link analysis and optimization tools may need this information in
959order to perform correct modifications of executables. This results
960in larger executables.
961
dbab7a7b
NC
962This option is currently only supported on ELF platforms.
963
4f471f39
RS
964@kindex --force-dynamic
965@cindex forcing the creation of dynamic sections
966@item --force-dynamic
967Force the output file to have dynamic sections. This option is specific
968to VxWorks targets.
969
252b5132
RH
970@cindex partial link
971@cindex relocatable output
972@kindex -r
1049f94e 973@kindex --relocatable
252b5132 974@item -r
1049f94e 975@itemx --relocatable
252b5132 976Generate relocatable output---i.e., generate an output file that can in
ff5dcc92 977turn serve as input to @command{ld}. This is often called @dfn{partial
252b5132
RH
978linking}. As a side effect, in environments that support standard Unix
979magic numbers, this option also sets the output file's magic number to
980@code{OMAGIC}.
ff5dcc92 981@c ; see @option{-N}.
252b5132
RH
982If this option is not specified, an absolute file is produced. When
983linking C++ programs, this option @emph{will not} resolve references to
984constructors; to do that, use @samp{-Ur}.
985
62bf86b4
HPN
986When an input file does not have the same format as the output file,
987partial linking is only supported if that input file does not contain any
988relocations. Different output formats can have further restrictions; for
989example some @code{a.out}-based formats do not support partial linking
990with input files in other formats at all.
991
252b5132
RH
992This option does the same thing as @samp{-i}.
993
994@kindex -R @var{file}
995@kindex --just-symbols=@var{file}
996@cindex symbol-only input
997@item -R @var{filename}
998@itemx --just-symbols=@var{filename}
999Read symbol names and their addresses from @var{filename}, but do not
1000relocate it or include it in the output. This allows your output file
1001to refer symbolically to absolute locations of memory defined in other
1002programs. You may use this option more than once.
1003
ff5dcc92 1004For compatibility with other ELF linkers, if the @option{-R} option is
252b5132 1005followed by a directory name, rather than a file name, it is treated as
ff5dcc92 1006the @option{-rpath} option.
252b5132
RH
1007
1008@kindex -s
1009@kindex --strip-all
1010@cindex strip all symbols
a1ab1d2a 1011@item -s
252b5132
RH
1012@itemx --strip-all
1013Omit all symbol information from the output file.
1014
1015@kindex -S
1016@kindex --strip-debug
1017@cindex strip debugger symbols
a1ab1d2a 1018@item -S
252b5132
RH
1019@itemx --strip-debug
1020Omit debugger symbol information (but not all symbols) from the output file.
1021
a70f34c0
NC
1022@kindex --strip-discarded
1023@kindex --no-strip-discarded
1024@item --strip-discarded
1025@itemx --no-strip-discarded
1026Omit (or do not omit) global symbols defined in discarded sections.
1027Enabled by default.
1028
252b5132
RH
1029@kindex -t
1030@kindex --trace
1031@cindex input files, displaying
a1ab1d2a 1032@item -t
252b5132 1033@itemx --trace
4f4690cd
AM
1034Print the names of the input files as @command{ld} processes them. If
1035@samp{-t} is given twice then members within archives are also printed.
1036@samp{-t} output is useful to generate a list of all the object files
1037and scripts involved in linking, for example, when packaging files for
1038a linker bug report.
252b5132
RH
1039
1040@kindex -T @var{script}
1041@kindex --script=@var{script}
1042@cindex script files
1043@item -T @var{scriptfile}
1044@itemx --script=@var{scriptfile}
1045Use @var{scriptfile} as the linker script. This script replaces
ff5dcc92 1046@command{ld}'s default linker script (rather than adding to it), so
252b5132 1047@var{commandfile} must specify everything necessary to describe the
114283d8
NC
1048output file. @xref{Scripts}. If @var{scriptfile} does not exist in
1049the current directory, @code{ld} looks for it in the directories
1050specified by any preceding @samp{-L} options. Multiple @samp{-T}
1051options accumulate.
252b5132 1052
14be8564
L
1053@kindex -dT @var{script}
1054@kindex --default-script=@var{script}
1055@cindex script files
1056@item -dT @var{scriptfile}
1057@itemx --default-script=@var{scriptfile}
1058Use @var{scriptfile} as the default linker script. @xref{Scripts}.
1059
1060This option is similar to the @option{--script} option except that
1061processing of the script is delayed until after the rest of the
1062command line has been processed. This allows options placed after the
1063@option{--default-script} option on the command line to affect the
1064behaviour of the linker script, which can be important when the linker
1065command line cannot be directly controlled by the user. (eg because
1066the command line is being constructed by another tool, such as
1067@samp{gcc}).
1068
252b5132
RH
1069@kindex -u @var{symbol}
1070@kindex --undefined=@var{symbol}
1071@cindex undefined symbol
1072@item -u @var{symbol}
1073@itemx --undefined=@var{symbol}
1074Force @var{symbol} to be entered in the output file as an undefined
1075symbol. Doing this may, for example, trigger linking of additional
1076modules from standard libraries. @samp{-u} may be repeated with
1077different option arguments to enter additional undefined symbols. This
1078option is equivalent to the @code{EXTERN} linker script command.
1079
0a618243
AB
1080If this option is being used to force additional modules to be pulled
1081into the link, and if it is an error for the symbol to remain
1082undefined, then the option @option{--require-defined} should be used
1083instead.
1084
1085@kindex --require-defined=@var{symbol}
1086@cindex symbols, require defined
1087@cindex defined symbol
1088@item --require-defined=@var{symbol}
1089Require that @var{symbol} is defined in the output file. This option
1090is the same as option @option{--undefined} except that if @var{symbol}
1091is not defined in the output file then the linker will issue an error
1092and exit. The same effect can be achieved in a linker script by using
1093@code{EXTERN}, @code{ASSERT} and @code{DEFINED} together. This option
1094can be used multiple times to require additional symbols.
1095
252b5132
RH
1096@kindex -Ur
1097@cindex constructors
a1ab1d2a 1098@item -Ur
252b5132
RH
1099For anything other than C++ programs, this option is equivalent to
1100@samp{-r}: it generates relocatable output---i.e., an output file that can in
ff5dcc92 1101turn serve as input to @command{ld}. When linking C++ programs, @samp{-Ur}
252b5132
RH
1102@emph{does} resolve references to constructors, unlike @samp{-r}.
1103It does not work to use @samp{-Ur} on files that were themselves linked
1104with @samp{-Ur}; once the constructor table has been built, it cannot
1105be added to. Use @samp{-Ur} only for the last partial link, and
1106@samp{-r} for the others.
1107
c005eb9e
AB
1108@kindex --orphan-handling=@var{MODE}
1109@cindex orphan sections
1110@cindex sections, orphan
1111@item --orphan-handling=@var{MODE}
1112Control how orphan sections are handled. An orphan section is one not
1113specifically mentioned in a linker script. @xref{Orphan Sections}.
1114
1115@var{MODE} can have any of the following values:
1116
1117@table @code
1118@item place
1119Orphan sections are placed into a suitable output section following
1120the strategy described in @ref{Orphan Sections}. The option
d2732b69 1121@samp{--unique} also affects how sections are placed.
c005eb9e
AB
1122
1123@item discard
1124All orphan sections are discarded, by placing them in the
1125@samp{/DISCARD/} section (@pxref{Output Section Discarding}).
1126
1127@item warn
1128The linker will place the orphan section as for @code{place} and also
1129issue a warning.
1130
1131@item error
1132The linker will exit with an error if any orphan section is found.
1133@end table
1134
1135The default if @samp{--orphan-handling} is not given is @code{place}.
1136
577a0623
AM
1137@kindex --unique[=@var{SECTION}]
1138@item --unique[=@var{SECTION}]
1139Creates a separate output section for every input section matching
1140@var{SECTION}, or if the optional wildcard @var{SECTION} argument is
1141missing, for every orphan input section. An orphan section is one not
1142specifically mentioned in a linker script. You may use this option
1143multiple times on the command line; It prevents the normal merging of
1144input sections with the same name, overriding output section assignments
1145in a linker script.
a854a4a7 1146
252b5132
RH
1147@kindex -v
1148@kindex -V
1149@kindex --version
1150@cindex version
1151@item -v
1152@itemx --version
1153@itemx -V
ff5dcc92 1154Display the version number for @command{ld}. The @option{-V} option also
252b5132
RH
1155lists the supported emulations.
1156
1157@kindex -x
1158@kindex --discard-all
1159@cindex deleting local symbols
1160@item -x
1161@itemx --discard-all
1162Delete all local symbols.
1163
1164@kindex -X
1165@kindex --discard-locals
1166@cindex local symbols, deleting
a1ab1d2a 1167@item -X
252b5132 1168@itemx --discard-locals
3c68c38f
BW
1169Delete all temporary local symbols. (These symbols start with
1170system-specific local label prefixes, typically @samp{.L} for ELF systems
1171or @samp{L} for traditional a.out systems.)
252b5132
RH
1172
1173@kindex -y @var{symbol}
1174@kindex --trace-symbol=@var{symbol}
1175@cindex symbol tracing
1176@item -y @var{symbol}
1177@itemx --trace-symbol=@var{symbol}
1178Print the name of each linked file in which @var{symbol} appears. This
1179option may be given any number of times. On many systems it is necessary
1180to prepend an underscore.
1181
1182This option is useful when you have an undefined symbol in your link but
1183don't know where the reference is coming from.
1184
1185@kindex -Y @var{path}
1186@item -Y @var{path}
1187Add @var{path} to the default library search path. This option exists
1188for Solaris compatibility.
1189
1190@kindex -z @var{keyword}
1191@item -z @var{keyword}
cd6d6c15
NC
1192The recognized keywords are:
1193@table @samp
1194
e6e2dfbd
AM
1195@item bndplt
1196Always generate BND prefix in PLT entries. Supported for Linux/x86_64.
1197
1198@item call-nop=prefix-addr
1199@itemx call-nop=suffix-nop
1200@itemx call-nop=prefix-@var{byte}
1201@itemx call-nop=suffix-@var{byte}
1202Specify the 1-byte @code{NOP} padding when transforming indirect call
1203to a locally defined function, foo, via its GOT slot.
1204@option{call-nop=prefix-addr} generates @code{0x67 call foo}.
1205@option{call-nop=suffix-nop} generates @code{call foo 0x90}.
1206@option{call-nop=prefix-@var{byte}} generates @code{@var{byte} call foo}.
1207@option{call-nop=suffix-@var{byte}} generates @code{call foo @var{byte}}.
1208Supported for i386 and x86_64.
1209
233a0083
L
1210@item cet-report=none
1211@itemx cet-report=warning
1212@itemx cet-report=error
1213Specify how to report the missing GNU_PROPERTY_X86_FEATURE_1_IBT and
1214GNU_PROPERTY_X86_FEATURE_1_SHSTK properties in input .note.gnu.property
1215section. @option{cet-report=none}, which is the default, will make the
1216linker not report missing properties in input files.
1217@option{cet-report=warning} will make the linker issue a warning for
1218missing properties in input files. @option{cet-report=error} will make
1219the linker issue an error for missing properties in input files.
1220Note that @option{ibt} will turn off the missing
1221GNU_PROPERTY_X86_FEATURE_1_IBT property report and @option{shstk} will
1222turn off the missing GNU_PROPERTY_X86_FEATURE_1_SHSTK property report.
1223Supported for Linux/i386 and Linux/x86_64.
1224
cd6d6c15 1225@item combreloc
e6e2dfbd
AM
1226@itemx nocombreloc
1227Combine multiple dynamic relocation sections and sort to improve
1228dynamic symbol lookup caching. Do not do this if @samp{nocombreloc}.
cd6d6c15 1229
b8871f35 1230@item common
e6e2dfbd
AM
1231@itemx nocommon
1232Generate common symbols with STT_COMMON type during a relocatable
1233link. Use STT_OBJECT type if @samp{nocommon}.
1234
1235@item common-page-size=@var{value}
1236Set the page size most commonly used to @var{value}. Memory image
1237layout will be optimized to minimize memory pages if the system is
1238using pages of this size.
b8871f35 1239
cd6d6c15 1240@item defs
97a232d7
NC
1241Report unresolved symbol references from regular object files. This
1242is done even if the linker is creating a non-symbolic shared library.
1243This option is the inverse of @samp{-z undefs}.
cd6d6c15 1244
e6e2dfbd
AM
1245@item dynamic-undefined-weak
1246@itemx nodynamic-undefined-weak
1247Make undefined weak symbols dynamic when building a dynamic object,
1248if they are referenced from a regular object file and not forced local
1249by symbol visibility or versioning. Do not make them dynamic if
1250@samp{nodynamic-undefined-weak}. If neither option is given, a target
1251may default to either option being in force, or make some other
1252selection of undefined weak symbols dynamic. Not all targets support
1253these options.
1254
6aa29e7b
JJ
1255@item execstack
1256Marks the object as requiring executable stack.
1257
b039ef04
L
1258@item global
1259This option is only meaningful when building a shared object. It makes
1260the symbols defined by this shared object available for symbol resolution
1261of subsequently loaded libraries.
1262
93ab9c0d
NC
1263@item globalaudit
1264This option is only meaningful when building a dynamic executable.
1265This option marks the executable as requiring global auditing by
1266setting the @code{DF_1_GLOBAUDIT} bit in the @code{DT_FLAGS_1} dynamic
1267tag. Global auditing requires that any auditing library defined via
a05a5b64 1268the @option{--depaudit} or @option{-P} command-line options be run for
6c19b93b 1269all dynamic objects loaded by the application.
93ab9c0d 1270
e6e2dfbd
AM
1271@item ibtplt
1272Generate Intel Indirect Branch Tracking (IBT) enabled PLT entries.
1273Supported for Linux/i386 and Linux/x86_64.
1274
8df93cb5 1275@item ibt
e6e2dfbd
AM
1276Generate GNU_PROPERTY_X86_FEATURE_1_IBT in .note.gnu.property section
1277to indicate compatibility with IBT. This also implies @option{ibtplt}.
1278Supported for Linux/i386 and Linux/x86_64.
1279
cd6d6c15
NC
1280@item initfirst
1281This option is only meaningful when building a shared object.
1282It marks the object so that its runtime initialization will occur
1283before the runtime initialization of any other objects brought into
1284the process at the same time. Similarly the runtime finalization of
1285the object will occur after the runtime finalization of any other
1286objects.
1287
1288@item interpose
e6e2dfbd
AM
1289Specify that the dynamic loader should modify its symbol search order
1290so that symbols in this shared library interpose all other shared
1291libraries not so marked.
cd6d6c15 1292
5fa222e4
AM
1293@item lazy
1294When generating an executable or shared library, mark it to tell the
1295dynamic linker to defer function call resolution to the point when
1296the function is called (lazy binding), rather than at load time.
1297Lazy binding is the default.
1298
cd6d6c15 1299@item loadfltr
e6e2dfbd 1300Specify that the object's filters be processed immediately at runtime.
cd6d6c15 1301
e6e2dfbd
AM
1302@item max-page-size=@var{value}
1303Set the maximum memory page size supported to @var{value}.
cd6d6c15 1304
e6e2dfbd
AM
1305@item muldefs
1306Allow multiple definitions.
b8871f35 1307
cd6d6c15 1308@item nocopyreloc
daf220f0
AM
1309Disable linker generated .dynbss variables used in place of variables
1310defined in shared libraries. May result in dynamic text relocations.
cd6d6c15
NC
1311
1312@item nodefaultlib
e6e2dfbd
AM
1313Specify that the dynamic loader search for dependencies of this object
1314should ignore any default library search paths.
cd6d6c15
NC
1315
1316@item nodelete
e6e2dfbd 1317Specify that the object shouldn't be unloaded at runtime.
cd6d6c15
NC
1318
1319@item nodlopen
e6e2dfbd 1320Specify that the object is not available to @code{dlopen}.
cd6d6c15
NC
1321
1322@item nodump
e6e2dfbd 1323Specify that the object can not be dumped by @code{dldump}.
cd6d6c15 1324
6aa29e7b
JJ
1325@item noexecstack
1326Marks the object as not requiring executable stack.
1327
e6e2dfbd
AM
1328@item noextern-protected-data
1329Don't treat protected data symbols as external when building a shared
1330library. This option overrides the linker backend default. It can be
1331used to work around incorrect relocations against protected data symbols
1332generated by compiler. Updates on protected data symbols by another
1333module aren't visible to the resulting shared library. Supported for
1334i386 and x86-64.
8dfef1bd 1335
e6e2dfbd
AM
1336@item noreloc-overflow
1337Disable relocation overflow check. This can be used to disable
1338relocation overflow check if there will be no dynamic relocation
1339overflow at run-time. Supported for x86_64.
6aa29e7b 1340
cd6d6c15
NC
1341@item now
1342When generating an executable or shared library, mark it to tell the
1343dynamic linker to resolve all symbols when the program is started, or
e6e2dfbd
AM
1344when the shared library is loaded by dlopen, instead of deferring
1345function call resolution to the point when the function is first
1346called.
cd6d6c15
NC
1347
1348@item origin
e6e2dfbd 1349Specify that the object requires @samp{$ORIGIN} handling in paths.
cd6d6c15 1350
6aa29e7b 1351@item relro
e6e2dfbd 1352@itemx norelro
def5c83c
AM
1353Create an ELF @code{PT_GNU_RELRO} segment header in the object. This
1354specifies a memory segment that should be made read-only after
1355relocation, if supported. Specifying @samp{common-page-size} smaller
1356than the system page size will render this protection ineffective.
e6e2dfbd 1357Don't create an ELF @code{PT_GNU_RELRO} segment if @samp{norelro}.
6aa29e7b 1358
47acac12
L
1359@item separate-code
1360@itemx noseparate-code
1361Create separate code @code{PT_LOAD} segment header in the object. This
1362specifies a memory segment that should contain only instructions and must
1363be in wholly disjoint pages from any other data. Don't create separate
1364code @code{PT_LOAD} segment if @samp{noseparate-code} is used.
1365
e6e2dfbd
AM
1366@item shstk
1367Generate GNU_PROPERTY_X86_FEATURE_1_SHSTK in .note.gnu.property section
1368to indicate compatibility with Intel Shadow Stack. Supported for
1369Linux/i386 and Linux/x86_64.
24718e3b 1370
04c3a755 1371@item stack-size=@var{value}
e6e2dfbd 1372Specify a stack size for an ELF @code{PT_GNU_STACK} segment.
04c3a755
NS
1373Specifying zero will override any default non-zero sized
1374@code{PT_GNU_STACK} segment creation.
1375
cae64165
RM
1376@item start-stop-visibility=@var{value}
1377@cindex visibility
1378@cindex ELF symbol visibility
1379Specify the ELF symbol visibility for synthesized
1380@code{__start_SECNAME} and @code{__stop_SECNAME} symbols (@pxref{Input
1381Section Example}). @var{value} must be exactly @samp{default},
1382@samp{internal}, @samp{hidden}, or @samp{protected}. If no @samp{-z
1383start-stop-visibility} option is given, @samp{protected} is used for
1384compatibility with historical practice. However, it's highly
1385recommended to use @samp{-z start-stop-visibility=hidden} in new
1386programs and shared libraries so that these symbols are not exported
1387between shared objects, which is not usually what's intended.
1388
e6e2dfbd
AM
1389@item text
1390@itemx notext
1391@itemx textoff
a6dbf402
L
1392Report an error if DT_TEXTREL is set, i.e., if the position-independent
1393or shared object has dynamic relocations in read-only sections. Don't
1394report an error if @samp{notext} or @samp{textoff}.
48580982 1395
97a232d7
NC
1396@item undefs
1397Do not report unresolved symbol references from regular object files,
1398either when creating an executable, or when creating a shared library.
1399This option is the inverse of @samp{-z defs}.
1400
cd6d6c15
NC
1401@end table
1402
ece2d90e 1403Other keywords are ignored for Solaris compatibility.
252b5132
RH
1404
1405@kindex -(
1406@cindex groups of archives
1407@item -( @var{archives} -)
1408@itemx --start-group @var{archives} --end-group
1409The @var{archives} should be a list of archive files. They may be
1410either explicit file names, or @samp{-l} options.
1411
1412The specified archives are searched repeatedly until no new undefined
1413references are created. Normally, an archive is searched only once in
1414the order that it is specified on the command line. If a symbol in that
1415archive is needed to resolve an undefined symbol referred to by an
1416object in an archive that appears later on the command line, the linker
1417would not be able to resolve that reference. By grouping the archives,
5d3db3e2 1418they will all be searched repeatedly until all possible references are
252b5132
RH
1419resolved.
1420
1421Using this option has a significant performance cost. It is best to use
1422it only when there are unavoidable circular references between two or
1423more archives.
1424
69da35b5
NC
1425@kindex --accept-unknown-input-arch
1426@kindex --no-accept-unknown-input-arch
1427@item --accept-unknown-input-arch
1428@itemx --no-accept-unknown-input-arch
1429Tells the linker to accept input files whose architecture cannot be
2ca22b03 1430recognised. The assumption is that the user knows what they are doing
69da35b5
NC
1431and deliberately wants to link in these unknown input files. This was
1432the default behaviour of the linker, before release 2.14. The default
1433behaviour from release 2.14 onwards is to reject such input files, and
1434so the @samp{--accept-unknown-input-arch} option has been added to
1435restore the old behaviour.
2ca22b03 1436
4a43e768
AM
1437@kindex --as-needed
1438@kindex --no-as-needed
1439@item --as-needed
1440@itemx --no-as-needed
1441This option affects ELF DT_NEEDED tags for dynamic libraries mentioned
ddbb8a31 1442on the command line after the @option{--as-needed} option. Normally
4a43e768
AM
1443the linker will add a DT_NEEDED tag for each dynamic library mentioned
1444on the command line, regardless of whether the library is actually
ddbb8a31 1445needed or not. @option{--as-needed} causes a DT_NEEDED tag to only be
ffa9430d
AM
1446emitted for a library that @emph{at that point in the link} satisfies a
1447non-weak undefined symbol reference from a regular object file or, if
1240be6b
AM
1448the library is not found in the DT_NEEDED lists of other needed libraries, a
1449non-weak undefined symbol reference from another needed dynamic library.
ffa9430d
AM
1450Object files or libraries appearing on the command line @emph{after}
1451the library in question do not affect whether the library is seen as
1452needed. This is similar to the rules for extraction of object files
1453from archives. @option{--no-as-needed} restores the default behaviour.
4a43e768 1454
e56f61be
L
1455@kindex --add-needed
1456@kindex --no-add-needed
1457@item --add-needed
1458@itemx --no-add-needed
ddbb8a31
NC
1459These two options have been deprecated because of the similarity of
1460their names to the @option{--as-needed} and @option{--no-as-needed}
1461options. They have been replaced by @option{--copy-dt-needed-entries}
1462and @option{--no-copy-dt-needed-entries}.
e56f61be 1463
252b5132
RH
1464@kindex -assert @var{keyword}
1465@item -assert @var{keyword}
1466This option is ignored for SunOS compatibility.
1467
1468@kindex -Bdynamic
1469@kindex -dy
1470@kindex -call_shared
1471@item -Bdynamic
1472@itemx -dy
1473@itemx -call_shared
1474Link against dynamic libraries. This is only meaningful on platforms
1475for which shared libraries are supported. This option is normally the
1476default on such platforms. The different variants of this option are
1477for compatibility with various systems. You may use this option
1478multiple times on the command line: it affects library searching for
da8bce14 1479@option{-l} options which follow it.
252b5132 1480
a1ab1d2a
UD
1481@kindex -Bgroup
1482@item -Bgroup
1483Set the @code{DF_1_GROUP} flag in the @code{DT_FLAGS_1} entry in the dynamic
1484section. This causes the runtime linker to handle lookups in this
1485object and its dependencies to be performed only inside the group.
560e09e9
NC
1486@option{--unresolved-symbols=report-all} is implied. This option is
1487only meaningful on ELF platforms which support shared libraries.
a1ab1d2a 1488
252b5132
RH
1489@kindex -Bstatic
1490@kindex -dn
1491@kindex -non_shared
1492@kindex -static
a1ab1d2a 1493@item -Bstatic
252b5132
RH
1494@itemx -dn
1495@itemx -non_shared
1496@itemx -static
1497Do not link against shared libraries. This is only meaningful on
1498platforms for which shared libraries are supported. The different
1499variants of this option are for compatibility with various systems. You
1500may use this option multiple times on the command line: it affects
560e09e9 1501library searching for @option{-l} options which follow it. This
e9156f74
NC
1502option also implies @option{--unresolved-symbols=report-all}. This
1503option can be used with @option{-shared}. Doing so means that a
1504shared library is being created but that all of the library's external
1505references must be resolved by pulling in entries from static
ece2d90e 1506libraries.
252b5132
RH
1507
1508@kindex -Bsymbolic
1509@item -Bsymbolic
1510When creating a shared library, bind references to global symbols to the
1511definition within the shared library, if any. Normally, it is possible
1512for a program linked against a shared library to override the definition
1914e264
AM
1513within the shared library. This option is only meaningful on ELF
1514platforms which support shared libraries.
252b5132 1515
40b36307
L
1516@kindex -Bsymbolic-functions
1517@item -Bsymbolic-functions
1518When creating a shared library, bind references to global function
c0065db7 1519symbols to the definition within the shared library, if any.
40b36307 1520This option is only meaningful on ELF platforms which support shared
1914e264 1521libraries.
40b36307 1522
55255dae
L
1523@kindex --dynamic-list=@var{dynamic-list-file}
1524@item --dynamic-list=@var{dynamic-list-file}
1525Specify the name of a dynamic list file to the linker. This is
1526typically used when creating shared libraries to specify a list of
1527global symbols whose references shouldn't be bound to the definition
1528within the shared library, or creating dynamically linked executables
1529to specify a list of symbols which should be added to the symbol table
1530in the executable. This option is only meaningful on ELF platforms
1531which support shared libraries.
1532
1533The format of the dynamic list is the same as the version node without
1534scope and node name. See @ref{VERSION} for more information.
1535
40b36307
L
1536@kindex --dynamic-list-data
1537@item --dynamic-list-data
1538Include all global data symbols to the dynamic list.
1539
1540@kindex --dynamic-list-cpp-new
1541@item --dynamic-list-cpp-new
1542Provide the builtin dynamic list for C++ operator new and delete. It
1543is mainly useful for building shared libstdc++.
1544
0b8a70d9
L
1545@kindex --dynamic-list-cpp-typeinfo
1546@item --dynamic-list-cpp-typeinfo
1547Provide the builtin dynamic list for C++ runtime type identification.
1548
252b5132
RH
1549@kindex --check-sections
1550@kindex --no-check-sections
1551@item --check-sections
308b1ffd 1552@itemx --no-check-sections
252b5132 1553Asks the linker @emph{not} to check section addresses after they have
7d816a17 1554been assigned to see if there are any overlaps. Normally the linker will
252b5132
RH
1555perform this check, and if it finds any overlaps it will produce
1556suitable error messages. The linker does know about, and does make
1557allowances for sections in overlays. The default behaviour can be
a05a5b64 1558restored by using the command-line switch @option{--check-sections}.
02b0b1aa
NS
1559Section overlap is not usually checked for relocatable links. You can
1560force checking in that case by using the @option{--check-sections}
1561option.
252b5132 1562
ddbb8a31
NC
1563@kindex --copy-dt-needed-entries
1564@kindex --no-copy-dt-needed-entries
1565@item --copy-dt-needed-entries
1566@itemx --no-copy-dt-needed-entries
9d5777a3 1567This option affects the treatment of dynamic libraries referred to
ddbb8a31 1568by DT_NEEDED tags @emph{inside} ELF dynamic libraries mentioned on the
08efffb8 1569command line. Normally the linker won't add a DT_NEEDED tag to the
ddbb8a31 1570output binary for each library mentioned in a DT_NEEDED tag in an
08efffb8 1571input dynamic library. With @option{--copy-dt-needed-entries}
ddbb8a31 1572specified on the command line however any dynamic libraries that
08efffb8
MM
1573follow it will have their DT_NEEDED entries added. The default
1574behaviour can be restored with @option{--no-copy-dt-needed-entries}.
ddbb8a31
NC
1575
1576This option also has an effect on the resolution of symbols in dynamic
08efffb8
MM
1577libraries. With @option{--copy-dt-needed-entries} dynamic libraries
1578mentioned on the command line will be recursively searched, following
1579their DT_NEEDED tags to other libraries, in order to resolve symbols
1580required by the output binary. With the default setting however
1581the searching of dynamic libraries that follow it will stop with the
1582dynamic library itself. No DT_NEEDED links will be traversed to resolve
ddbb8a31
NC
1583symbols.
1584
252b5132
RH
1585@cindex cross reference table
1586@kindex --cref
1587@item --cref
1588Output a cross reference table. If a linker map file is being
1589generated, the cross reference table is printed to the map file.
1590Otherwise, it is printed on the standard output.
1591
1592The format of the table is intentionally simple, so that it may be
1593easily processed by a script if necessary. The symbols are printed out,
1594sorted by name. For each symbol, a list of file names is given. If the
1595symbol is defined, the first file listed is the location of the
049c1c8e
NC
1596definition. If the symbol is defined as a common value then any files
1597where this happens appear next. Finally any files that reference the
1598symbol are listed.
252b5132 1599
4818e05f
AM
1600@cindex common allocation
1601@kindex --no-define-common
1602@item --no-define-common
1603This option inhibits the assignment of addresses to common symbols.
1604The script command @code{INHIBIT_COMMON_ALLOCATION} has the same effect.
1605@xref{Miscellaneous Commands}.
1606
1607The @samp{--no-define-common} option allows decoupling
1608the decision to assign addresses to Common symbols from the choice
1609of the output file type; otherwise a non-Relocatable output type
1610forces assigning addresses to Common symbols.
1611Using @samp{--no-define-common} allows Common symbols that are referenced
1612from a shared library to be assigned addresses only in the main program.
1613This eliminates the unused duplicate space in the shared library,
1614and also prevents any possible confusion over resolving to the wrong
1615duplicate when there are many dynamic modules with specialized search
1616paths for runtime symbol resolution.
1617
7bdf4127
AB
1618@cindex group allocation in linker script
1619@cindex section groups
1620@cindex COMDAT
1621@kindex --force-group-allocation
1622@item --force-group-allocation
1623This option causes the linker to place section group members like
1624normal input sections, and to delete the section groups. This is the
1625default behaviour for a final link but this option can be used to
1626change the behaviour of a relocatable link (@samp{-r}). The script
1627command @code{FORCE_GROUP_ALLOCATION} has the same
1628effect. @xref{Miscellaneous Commands}.
1629
252b5132 1630@cindex symbols, from command line
2509a395
SL
1631@kindex --defsym=@var{symbol}=@var{exp}
1632@item --defsym=@var{symbol}=@var{expression}
252b5132
RH
1633Create a global symbol in the output file, containing the absolute
1634address given by @var{expression}. You may use this option as many
1635times as necessary to define multiple symbols in the command line. A
1636limited form of arithmetic is supported for the @var{expression} in this
1637context: you may give a hexadecimal constant or the name of an existing
1638symbol, or use @code{+} and @code{-} to add or subtract hexadecimal
1639constants or symbols. If you need more elaborate expressions, consider
66bc8739
AM
1640using the linker command language from a script (@pxref{Assignments}).
1641@emph{Note:} there should be no white space between @var{symbol}, the
1642equals sign (``@key{=}''), and @var{expression}.
252b5132
RH
1643
1644@cindex demangling, from command line
28c309a2 1645@kindex --demangle[=@var{style}]
252b5132 1646@kindex --no-demangle
28c309a2 1647@item --demangle[=@var{style}]
252b5132
RH
1648@itemx --no-demangle
1649These options control whether to demangle symbol names in error messages
1650and other output. When the linker is told to demangle, it tries to
1651present symbol names in a readable fashion: it strips leading
1652underscores if they are used by the object file format, and converts C++
a1ab1d2a
UD
1653mangled symbol names into user readable names. Different compilers have
1654different mangling styles. The optional demangling style argument can be used
1655to choose an appropriate demangling style for your compiler. The linker will
28c309a2
NC
1656demangle by default unless the environment variable @samp{COLLECT_NO_DEMANGLE}
1657is set. These options may be used to override the default.
252b5132
RH
1658
1659@cindex dynamic linker, from command line
506eee22 1660@kindex -I@var{file}
2509a395
SL
1661@kindex --dynamic-linker=@var{file}
1662@item -I@var{file}
1663@itemx --dynamic-linker=@var{file}
252b5132
RH
1664Set the name of the dynamic linker. This is only meaningful when
1665generating dynamically linked ELF executables. The default dynamic
1666linker is normally correct; don't use this unless you know what you are
1667doing.
1668
9b8b325a
RF
1669@kindex --no-dynamic-linker
1670@item --no-dynamic-linker
1671When producing an executable file, omit the request for a dynamic
1672linker to be used at load-time. This is only meaningful for ELF
1673executables that contain dynamic relocations, and usually requires
1674entry point code that is capable of processing these relocations.
1675
a70f34c0
NC
1676@kindex --embedded-relocs
1677@item --embedded-relocs
1678This option is similar to the @option{--emit-relocs} option except
a094d01f 1679that the relocs are stored in a target-specific section. This option
a70f34c0
NC
1680is only supported by the @samp{BFIN}, @samp{CR16} and @emph{M68K}
1681targets.
1682
3f0a5f17
ME
1683@kindex --disable-multiple-abs-defs
1684@item --disable-multiple-abs-defs
1685Do not allow multiple definitions with symbols included
1686in filename invoked by -R or --just-symbols
1687
7ce691ae 1688@kindex --fatal-warnings
0fe58ccd 1689@kindex --no-fatal-warnings
7ce691ae 1690@item --fatal-warnings
0fe58ccd
NC
1691@itemx --no-fatal-warnings
1692Treat all warnings as errors. The default behaviour can be restored
1693with the option @option{--no-fatal-warnings}.
7ce691ae 1694
252b5132
RH
1695@kindex --force-exe-suffix
1696@item --force-exe-suffix
1697Make sure that an output file has a .exe suffix.
1698
1699If a successfully built fully linked output file does not have a
1700@code{.exe} or @code{.dll} suffix, this option forces the linker to copy
1701the output file to one of the same name with a @code{.exe} suffix. This
1702option is useful when using unmodified Unix makefiles on a Microsoft
1703Windows host, since some versions of Windows won't run an image unless
1704it ends in a @code{.exe} suffix.
1705
1706@kindex --gc-sections
1707@kindex --no-gc-sections
1708@cindex garbage collection
c17d87de
NC
1709@item --gc-sections
1710@itemx --no-gc-sections
252b5132 1711Enable garbage collection of unused input sections. It is ignored on
ac69cbc6 1712targets that do not support this option. The default behaviour (of not
b3549761 1713performing this garbage collection) can be restored by specifying
0f088b2a
KT
1714@samp{--no-gc-sections} on the command line. Note that garbage
1715collection for COFF and PE format targets is supported, but the
1716implementation is currently considered to be experimental.
252b5132 1717
d5465ba2
AM
1718@samp{--gc-sections} decides which input sections are used by
1719examining symbols and relocations. The section containing the entry
1720symbol and all sections containing symbols undefined on the
1721command-line will be kept, as will sections containing symbols
1722referenced by dynamic objects. Note that when building shared
1723libraries, the linker must assume that any visible symbol is
1724referenced. Once this initial set of sections has been determined,
1725the linker recursively marks as used any section referenced by their
b69e1ff3
CC
1726relocations. See @samp{--entry}, @samp{--undefined}, and
1727@samp{--gc-keep-exported}.
d5465ba2 1728
ac69cbc6 1729This option can be set when doing a partial link (enabled with option
9d5777a3 1730@samp{-r}). In this case the root of symbols kept must be explicitly
b69e1ff3
CC
1731specified either by one of the options @samp{--entry},
1732@samp{--undefined}, or @samp{--gc-keep-exported} or by a @code{ENTRY}
1733command in the linker script.
ac69cbc6 1734
c17d87de
NC
1735@kindex --print-gc-sections
1736@kindex --no-print-gc-sections
1737@cindex garbage collection
1738@item --print-gc-sections
1739@itemx --no-print-gc-sections
1740List all sections removed by garbage collection. The listing is
1741printed on stderr. This option is only effective if garbage
1742collection has been enabled via the @samp{--gc-sections}) option. The
1743default behaviour (of not listing the sections that are removed) can
1744be restored by specifying @samp{--no-print-gc-sections} on the command
1745line.
1746
22185505 1747@kindex --gc-keep-exported
1748@cindex garbage collection
1749@item --gc-keep-exported
1750When @samp{--gc-sections} is enabled, this option prevents garbage
1751collection of unused input sections that contain global symbols having
1752default or protected visibility. This option is intended to be used for
1753executables where unreferenced sections would otherwise be garbage
1754collected regardless of the external visibility of contained symbols.
1755Note that this option has no effect when linking shared objects since
1756it is already the default behaviour. This option is only supported for
1757ELF format targets.
1758
30824704
RM
1759@kindex --print-output-format
1760@cindex output format
1761@item --print-output-format
1762Print the name of the default output format (perhaps influenced by
1763other command-line options). This is the string that would appear
1764in an @code{OUTPUT_FORMAT} linker script command (@pxref{File Commands}).
1765
3604cb1f
TG
1766@kindex --print-memory-usage
1767@cindex memory usage
1768@item --print-memory-usage
1769Print used size, total size and used size of memory regions created with
1770the @ref{MEMORY} command. This is useful on embedded targets to have a
1771quick view of amount of free memory. The format of the output has one
1772headline and one line per region. It is both human readable and easily
1773parsable by tools. Here is an example of an output:
1774
1775@smallexample
1776Memory region Used Size Region Size %age Used
1777 ROM: 256 KB 1 MB 25.00%
1778 RAM: 32 B 2 GB 0.00%
1779@end smallexample
1780
252b5132
RH
1781@cindex help
1782@cindex usage
1783@kindex --help
1784@item --help
1785Print a summary of the command-line options on the standard output and exit.
1786
ea20a7da
CC
1787@kindex --target-help
1788@item --target-help
a094d01f 1789Print a summary of all target-specific options on the standard output and exit.
ea20a7da 1790
2509a395
SL
1791@kindex -Map=@var{mapfile}
1792@item -Map=@var{mapfile}
252b5132 1793Print a link map to the file @var{mapfile}. See the description of the
72a3b182
NC
1794@option{-M} option, above. Specifying a directory as @var{mapfile}
1795causes the linker map to be written into a file inside the directory.
1796The name of the file is based upon the @var{output} filename with
1797@code{.map} appended.
252b5132
RH
1798
1799@cindex memory usage
1800@kindex --no-keep-memory
1801@item --no-keep-memory
ff5dcc92
SC
1802@command{ld} normally optimizes for speed over memory usage by caching the
1803symbol tables of input files in memory. This option tells @command{ld} to
252b5132 1804instead optimize for memory usage, by rereading the symbol tables as
ff5dcc92 1805necessary. This may be required if @command{ld} runs out of memory space
252b5132
RH
1806while linking a large executable.
1807
1808@kindex --no-undefined
a1ab1d2a 1809@kindex -z defs
97a232d7 1810@kindex -z undefs
252b5132 1811@item --no-undefined
a1ab1d2a 1812@itemx -z defs
560e09e9
NC
1813Report unresolved symbol references from regular object files. This
1814is done even if the linker is creating a non-symbolic shared library.
1815The switch @option{--[no-]allow-shlib-undefined} controls the
1816behaviour for reporting unresolved references found in shared
ece2d90e 1817libraries being linked in.
252b5132 1818
97a232d7
NC
1819The effects of this option can be reverted by using @code{-z undefs}.
1820
aa713662
L
1821@kindex --allow-multiple-definition
1822@kindex -z muldefs
1823@item --allow-multiple-definition
1824@itemx -z muldefs
1825Normally when a symbol is defined multiple times, the linker will
1826report a fatal error. These options allow multiple definitions and the
1827first definition will be used.
1828
b79e8c78 1829@kindex --allow-shlib-undefined
ae9a127f 1830@kindex --no-allow-shlib-undefined
b79e8c78 1831@item --allow-shlib-undefined
ae9a127f 1832@itemx --no-allow-shlib-undefined
903249d7 1833Allows or disallows undefined symbols in shared libraries.
560e09e9
NC
1834This switch is similar to @option{--no-undefined} except that it
1835determines the behaviour when the undefined symbols are in a
1836shared library rather than a regular object file. It does not affect
1837how undefined symbols in regular object files are handled.
1838
903249d7
NC
1839The default behaviour is to report errors for any undefined symbols
1840referenced in shared libraries if the linker is being used to create
1841an executable, but to allow them if the linker is being used to create
1842a shared library.
1843
1844The reasons for allowing undefined symbol references in shared
1845libraries specified at link time are that:
1846
1847@itemize @bullet
1848@item
1849A shared library specified at link time may not be the same as the one
1850that is available at load time, so the symbol might actually be
1851resolvable at load time.
1852@item
1853There are some operating systems, eg BeOS and HPPA, where undefined
1854symbols in shared libraries are normal.
1855
1856The BeOS kernel for example patches shared libraries at load time to
1857select whichever function is most appropriate for the current
1858architecture. This is used, for example, to dynamically select an
1859appropriate memset function.
1860@end itemize
b79e8c78 1861
31941635
L
1862@kindex --no-undefined-version
1863@item --no-undefined-version
1864Normally when a symbol has an undefined version, the linker will ignore
1865it. This option disallows symbols with undefined version and a fatal error
1866will be issued instead.
1867
3e3b46e5
PB
1868@kindex --default-symver
1869@item --default-symver
1870Create and use a default symbol version (the soname) for unversioned
fc0e6df6
PB
1871exported symbols.
1872
1873@kindex --default-imported-symver
1874@item --default-imported-symver
1875Create and use a default symbol version (the soname) for unversioned
1876imported symbols.
3e3b46e5 1877
252b5132
RH
1878@kindex --no-warn-mismatch
1879@item --no-warn-mismatch
ff5dcc92 1880Normally @command{ld} will give an error if you try to link together input
252b5132
RH
1881files that are mismatched for some reason, perhaps because they have
1882been compiled for different processors or for different endiannesses.
ff5dcc92 1883This option tells @command{ld} that it should silently permit such possible
252b5132
RH
1884errors. This option should only be used with care, in cases when you
1885have taken some special action that ensures that the linker errors are
1886inappropriate.
1887
fe7929ce
AM
1888@kindex --no-warn-search-mismatch
1889@item --no-warn-search-mismatch
1890Normally @command{ld} will give a warning if it finds an incompatible
1891library during a library search. This option silences the warning.
1892
252b5132
RH
1893@kindex --no-whole-archive
1894@item --no-whole-archive
ff5dcc92 1895Turn off the effect of the @option{--whole-archive} option for subsequent
252b5132
RH
1896archive files.
1897
1898@cindex output file after errors
1899@kindex --noinhibit-exec
1900@item --noinhibit-exec
1901Retain the executable output file whenever it is still usable.
1902Normally, the linker will not produce an output file if it encounters
1903errors during the link process; it exits without writing an output file
1904when it issues any error whatsoever.
1905
0a9c1c8e
CD
1906@kindex -nostdlib
1907@item -nostdlib
1908Only search library directories explicitly specified on the
1909command line. Library directories specified in linker scripts
1910(including linker scripts specified on the command line) are ignored.
1911
252b5132 1912@ifclear SingleFormat
2509a395
SL
1913@kindex --oformat=@var{output-format}
1914@item --oformat=@var{output-format}
ff5dcc92
SC
1915@command{ld} may be configured to support more than one kind of object
1916file. If your @command{ld} is configured this way, you can use the
252b5132 1917@samp{--oformat} option to specify the binary format for the output
ff5dcc92
SC
1918object file. Even when @command{ld} is configured to support alternative
1919object formats, you don't usually need to specify this, as @command{ld}
252b5132
RH
1920should be configured to produce as a default output format the most
1921usual format on each machine. @var{output-format} is a text string, the
1922name of a particular format supported by the BFD libraries. (You can
1923list the available binary formats with @samp{objdump -i}.) The script
1924command @code{OUTPUT_FORMAT} can also specify the output format, but
1925this option overrides it. @xref{BFD}.
1926@end ifclear
1927
76359541
TP
1928@kindex --out-implib
1929@item --out-implib @var{file}
1930Create an import library in @var{file} corresponding to the executable
1931the linker is generating (eg. a DLL or ELF program). This import
1932library (which should be called @code{*.dll.a} or @code{*.a} for DLLs)
1933may be used to link clients against the generated executable; this
1934behaviour makes it possible to skip a separate import library creation
1935step (eg. @code{dlltool} for DLLs). This option is only available for
1936the i386 PE and ELF targetted ports of the linker.
1937
36af4a4e
JJ
1938@kindex -pie
1939@kindex --pic-executable
1940@item -pie
1941@itemx --pic-executable
1942@cindex position independent executables
1943Create a position independent executable. This is currently only supported on
1944ELF platforms. Position independent executables are similar to shared
1945libraries in that they are relocated by the dynamic linker to the virtual
7e7d5768 1946address the OS chooses for them (which can vary between invocations). Like
36af4a4e
JJ
1947normal dynamically linked executables they can be executed and symbols
1948defined in the executable cannot be overridden by shared libraries.
1949
252b5132
RH
1950@kindex -qmagic
1951@item -qmagic
1952This option is ignored for Linux compatibility.
1953
1954@kindex -Qy
1955@item -Qy
1956This option is ignored for SVR4 compatibility.
1957
1958@kindex --relax
1959@cindex synthesizing linker
1960@cindex relaxing addressing modes
28d5f677 1961@cindex --no-relax
252b5132 1962@item --relax
28d5f677 1963@itemx --no-relax
a1ab1d2a 1964An option with machine dependent effects.
252b5132
RH
1965@ifset GENERIC
1966This option is only supported on a few targets.
1967@end ifset
1968@ifset H8300
ff5dcc92 1969@xref{H8/300,,@command{ld} and the H8/300}.
252b5132 1970@end ifset
e0001a05
NC
1971@ifset XTENSA
1972@xref{Xtensa,, @command{ld} and Xtensa Processors}.
1973@end ifset
93fd0973
SC
1974@ifset M68HC11
1975@xref{M68HC11/68HC12,,@command{ld} and the 68HC11 and 68HC12}.
1976@end ifset
78058a5e
SL
1977@ifset NIOSII
1978@xref{Nios II,,@command{ld} and the Altera Nios II}.
1979@end ifset
2a60a7a8
AM
1980@ifset POWERPC
1981@xref{PowerPC ELF32,,@command{ld} and PowerPC 32-bit ELF Support}.
1982@end ifset
252b5132 1983
a094d01f 1984On some platforms the @samp{--relax} option performs target-specific,
28d5f677
NC
1985global optimizations that become possible when the linker resolves
1986addressing in the program, such as relaxing address modes,
1987synthesizing new instructions, selecting shorter version of current
11e7fd74 1988instructions, and combining constant values.
252b5132
RH
1989
1990On some platforms these link time global optimizations may make symbolic
1991debugging of the resulting executable impossible.
1992@ifset GENERIC
28d5f677
NC
1993This is known to be the case for the Matsushita MN10200 and MN10300
1994family of processors.
252b5132
RH
1995@end ifset
1996
1997@ifset GENERIC
1998On platforms where this is not supported, @samp{--relax} is accepted,
1999but ignored.
2000@end ifset
2001
28d5f677
NC
2002On platforms where @samp{--relax} is accepted the option
2003@samp{--no-relax} can be used to disable the feature.
2004
252b5132
RH
2005@cindex retaining specified symbols
2006@cindex stripping all but some symbols
2007@cindex symbols, retaining selectively
2509a395
SL
2008@kindex --retain-symbols-file=@var{filename}
2009@item --retain-symbols-file=@var{filename}
252b5132
RH
2010Retain @emph{only} the symbols listed in the file @var{filename},
2011discarding all others. @var{filename} is simply a flat file, with one
2012symbol name per line. This option is especially useful in environments
2013@ifset GENERIC
2014(such as VxWorks)
2015@end ifset
2016where a large global symbol table is accumulated gradually, to conserve
2017run-time memory.
2018
2019@samp{--retain-symbols-file} does @emph{not} discard undefined symbols,
2020or symbols needed for relocations.
2021
2022You may only specify @samp{--retain-symbols-file} once in the command
2023line. It overrides @samp{-s} and @samp{-S}.
2024
2025@ifset GENERIC
2509a395 2026@item -rpath=@var{dir}
252b5132 2027@cindex runtime library search path
2509a395 2028@kindex -rpath=@var{dir}
252b5132 2029Add a directory to the runtime library search path. This is used when
ff5dcc92 2030linking an ELF executable with shared objects. All @option{-rpath}
252b5132 2031arguments are concatenated and passed to the runtime linker, which uses
c1b00498
MR
2032them to locate shared objects at runtime.
2033
2034The @option{-rpath} option is also used when locating shared objects which
2035are needed by shared objects explicitly included in the link; see the
2036description of the @option{-rpath-link} option. Searching @option{-rpath}
2037in this way is only supported by native linkers and cross linkers which
2038have been configured with the @option{--with-sysroot} option.
2039
2040If @option{-rpath} is not used when linking an ELF executable, the
2041contents of the environment variable @code{LD_RUN_PATH} will be used if it
2042is defined.
252b5132 2043
ff5dcc92 2044The @option{-rpath} option may also be used on SunOS. By default, on
d8506323 2045SunOS, the linker will form a runtime search path out of all the
ff5dcc92
SC
2046@option{-L} options it is given. If a @option{-rpath} option is used, the
2047runtime search path will be formed exclusively using the @option{-rpath}
2048options, ignoring the @option{-L} options. This can be useful when using
2049gcc, which adds many @option{-L} options which may be on NFS mounted
b45619c0 2050file systems.
252b5132 2051
ff5dcc92 2052For compatibility with other ELF linkers, if the @option{-R} option is
252b5132 2053followed by a directory name, rather than a file name, it is treated as
ff5dcc92 2054the @option{-rpath} option.
252b5132
RH
2055@end ifset
2056
2057@ifset GENERIC
2058@cindex link-time runtime library search path
2509a395
SL
2059@kindex -rpath-link=@var{dir}
2060@item -rpath-link=@var{dir}
252b5132
RH
2061When using ELF or SunOS, one shared library may require another. This
2062happens when an @code{ld -shared} link includes a shared library as one
2063of the input files.
2064
2065When the linker encounters such a dependency when doing a non-shared,
2066non-relocatable link, it will automatically try to locate the required
2067shared library and include it in the link, if it is not included
ff5dcc92 2068explicitly. In such a case, the @option{-rpath-link} option
252b5132 2069specifies the first set of directories to search. The
ff5dcc92 2070@option{-rpath-link} option may specify a sequence of directory names
252b5132
RH
2071either by specifying a list of names separated by colons, or by
2072appearing multiple times.
2073
e680a6b6
NC
2074The tokens @var{$ORIGIN} and @var{$LIB} can appear in these search
2075directories. They will be replaced by the full path to the directory
2076containing the program or shared object in the case of @var{$ORIGIN}
2077and either @samp{lib} - for 32-bit binaries - or @samp{lib64} - for
207864-bit binaries - in the case of @var{$LIB}.
2079
2080The alternative form of these tokens - @var{$@{ORIGIN@}} and
2081@var{$@{LIB@}} can also be used. The token @var{$PLATFORM} is not
2082supported.
2083
28c309a2
NC
2084This option should be used with caution as it overrides the search path
2085that may have been hard compiled into a shared library. In such a case it
2086is possible to use unintentionally a different search path than the
2087runtime linker would do.
2088
252b5132 2089The linker uses the following search paths to locate required shared
ece2d90e 2090libraries:
d8e4137b 2091
252b5132
RH
2092@enumerate
2093@item
ff5dcc92 2094Any directories specified by @option{-rpath-link} options.
252b5132 2095@item
ff5dcc92
SC
2096Any directories specified by @option{-rpath} options. The difference
2097between @option{-rpath} and @option{-rpath-link} is that directories
2098specified by @option{-rpath} options are included in the executable and
2099used at runtime, whereas the @option{-rpath-link} option is only effective
ece2d90e
NC
2100at link time. Searching @option{-rpath} in this way is only supported
2101by native linkers and cross linkers which have been configured with
2102the @option{--with-sysroot} option.
252b5132 2103@item
e2a83dd0
NC
2104On an ELF system, for native linkers, if the @option{-rpath} and
2105@option{-rpath-link} options were not used, search the contents of the
2106environment variable @code{LD_RUN_PATH}.
252b5132 2107@item
ff5dcc92
SC
2108On SunOS, if the @option{-rpath} option was not used, search any
2109directories specified using @option{-L} options.
252b5132 2110@item
a1b8d843 2111For a native linker, search the contents of the environment
e2a83dd0 2112variable @code{LD_LIBRARY_PATH}.
252b5132 2113@item
ec4eb78a
L
2114For a native ELF linker, the directories in @code{DT_RUNPATH} or
2115@code{DT_RPATH} of a shared library are searched for shared
2116libraries needed by it. The @code{DT_RPATH} entries are ignored if
2117@code{DT_RUNPATH} entries exist.
2118@item
252b5132
RH
2119The default directories, normally @file{/lib} and @file{/usr/lib}.
2120@item
d8e4137b
NC
2121For a linker for a Linux system, if the file @file{/etc/ld.so.conf}
2122exists, the list of directories found in that file. Note: the path
2123to this file is prefixed with the @code{sysroot} value, if that is
2124defined, and then any @code{prefix} string if the linker was
2125configured with the @command{--prefix=<path>} option.
2126@item
2127For a native linker on a FreeBSD system, any directories specified by
2128the @code{_PATH_ELF_HINTS} macro defined in the @file{elf-hints.h}
2129header file.
2130@item
2131Any directories specifed by a @code{SEARCH_DIR} command in the
2132linker script being used.
252b5132
RH
2133@end enumerate
2134
2135If the required shared library is not found, the linker will issue a
2136warning and continue with the link.
2137@end ifset
2138
2139@kindex -shared
2140@kindex -Bshareable
2141@item -shared
2142@itemx -Bshareable
2143@cindex shared libraries
2144Create a shared library. This is currently only supported on ELF, XCOFF
2145and SunOS platforms. On SunOS, the linker will automatically create a
ff5dcc92 2146shared library if the @option{-e} option is not used and there are
252b5132
RH
2147undefined symbols in the link.
2148
252b5132 2149@kindex --sort-common
2509a395
SL
2150@item --sort-common
2151@itemx --sort-common=ascending
2152@itemx --sort-common=descending
de7dd2bd
NC
2153This option tells @command{ld} to sort the common symbols by alignment in
2154ascending or descending order when it places them in the appropriate output
2155sections. The symbol alignments considered are sixteen-byte or larger,
2156eight-byte, four-byte, two-byte, and one-byte. This is to prevent gaps
2157between symbols due to alignment constraints. If no sorting order is
2158specified, then descending order is assumed.
252b5132 2159
2509a395
SL
2160@kindex --sort-section=name
2161@item --sort-section=name
bcaa7b3e
L
2162This option will apply @code{SORT_BY_NAME} to all wildcard section
2163patterns in the linker script.
2164
2509a395
SL
2165@kindex --sort-section=alignment
2166@item --sort-section=alignment
bcaa7b3e
L
2167This option will apply @code{SORT_BY_ALIGNMENT} to all wildcard section
2168patterns in the linker script.
2169
a70f34c0
NC
2170@kindex --spare-dynamic-tags
2171@item --spare-dynamic-tags=@var{count}
2172This option specifies the number of empty slots to leave in the
2173.dynamic section of ELF shared objects. Empty slots may be needed by
2174post processing tools, such as the prelinker. The default is 5.
2175
252b5132 2176@kindex --split-by-file
2509a395 2177@item --split-by-file[=@var{size}]
ff5dcc92 2178Similar to @option{--split-by-reloc} but creates a new output section for
a854a4a7
AM
2179each input file when @var{size} is reached. @var{size} defaults to a
2180size of 1 if not given.
252b5132
RH
2181
2182@kindex --split-by-reloc
2509a395 2183@item --split-by-reloc[=@var{count}]
a854a4a7 2184Tries to creates extra sections in the output file so that no single
252b5132 2185output section in the file contains more than @var{count} relocations.
a854a4a7 2186This is useful when generating huge relocatable files for downloading into
252b5132
RH
2187certain real time kernels with the COFF object file format; since COFF
2188cannot represent more than 65535 relocations in a single section. Note
2189that this will fail to work with object file formats which do not
2190support arbitrary sections. The linker will not split up individual
2191input sections for redistribution, so if a single input section contains
2192more than @var{count} relocations one output section will contain that
a854a4a7 2193many relocations. @var{count} defaults to a value of 32768.
252b5132
RH
2194
2195@kindex --stats
2196@item --stats
2197Compute and display statistics about the operation of the linker, such
2198as execution time and memory usage.
2199
2509a395 2200@kindex --sysroot=@var{directory}
e2243057
RS
2201@item --sysroot=@var{directory}
2202Use @var{directory} as the location of the sysroot, overriding the
2203configure-time default. This option is only supported by linkers
2204that were configured using @option{--with-sysroot}.
2205
a70f34c0
NC
2206@kindex --task-link
2207@item --task-link
2208This is used by COFF/PE based targets to create a task-linked object
2209file where all of the global symbols have been converted to statics.
2210
252b5132
RH
2211@kindex --traditional-format
2212@cindex traditional format
2213@item --traditional-format
ff5dcc92
SC
2214For some targets, the output of @command{ld} is different in some ways from
2215the output of some existing linker. This switch requests @command{ld} to
252b5132
RH
2216use the traditional format instead.
2217
2218@cindex dbx
ff5dcc92 2219For example, on SunOS, @command{ld} combines duplicate entries in the
252b5132
RH
2220symbol string table. This can reduce the size of an output file with
2221full debugging information by over 30 percent. Unfortunately, the SunOS
2222@code{dbx} program can not read the resulting program (@code{gdb} has no
ff5dcc92 2223trouble). The @samp{--traditional-format} switch tells @command{ld} to not
252b5132
RH
2224combine duplicate entries.
2225
2509a395
SL
2226@kindex --section-start=@var{sectionname}=@var{org}
2227@item --section-start=@var{sectionname}=@var{org}
176355da
NC
2228Locate a section in the output file at the absolute
2229address given by @var{org}. You may use this option as many
2230times as necessary to locate multiple sections in the command
2231line.
2232@var{org} must be a single hexadecimal integer;
2233for compatibility with other linkers, you may omit the leading
2234@samp{0x} usually associated with hexadecimal values. @emph{Note:} there
2235should be no white space between @var{sectionname}, the equals
2236sign (``@key{=}''), and @var{org}.
2237
2509a395
SL
2238@kindex -Tbss=@var{org}
2239@kindex -Tdata=@var{org}
2240@kindex -Ttext=@var{org}
252b5132 2241@cindex segment origins, cmd line
2509a395
SL
2242@item -Tbss=@var{org}
2243@itemx -Tdata=@var{org}
2244@itemx -Ttext=@var{org}
2245Same as @option{--section-start}, with @code{.bss}, @code{.data} or
a6e02871 2246@code{.text} as the @var{sectionname}.
252b5132 2247
2509a395
SL
2248@kindex -Ttext-segment=@var{org}
2249@item -Ttext-segment=@var{org}
258795f5 2250@cindex text segment origin, cmd line
2b8c06a3
L
2251When creating an ELF executable, it will set the address of the first
2252byte of the text segment.
258795f5 2253
9d5777a3
RM
2254@kindex -Trodata-segment=@var{org}
2255@item -Trodata-segment=@var{org}
2256@cindex rodata segment origin, cmd line
2257When creating an ELF executable or shared object for a target where
2258the read-only data is in its own segment separate from the executable
2259text, it will set the address of the first byte of the read-only data segment.
2260
0d705e9f
AM
2261@kindex -Tldata-segment=@var{org}
2262@item -Tldata-segment=@var{org}
2263@cindex ldata segment origin, cmd line
2264When creating an ELF executable or shared object for x86-64 medium memory
2265model, it will set the address of the first byte of the ldata segment.
2266
560e09e9
NC
2267@kindex --unresolved-symbols
2268@item --unresolved-symbols=@var{method}
2269Determine how to handle unresolved symbols. There are four possible
2270values for @samp{method}:
2271
2272@table @samp
2273@item ignore-all
da8bce14 2274Do not report any unresolved symbols.
560e09e9
NC
2275
2276@item report-all
da8bce14 2277Report all unresolved symbols. This is the default.
560e09e9
NC
2278
2279@item ignore-in-object-files
2280Report unresolved symbols that are contained in shared libraries, but
2281ignore them if they come from regular object files.
2282
2283@item ignore-in-shared-libs
2284Report unresolved symbols that come from regular object files, but
2285ignore them if they come from shared libraries. This can be useful
2286when creating a dynamic binary and it is known that all the shared
2287libraries that it should be referencing are included on the linker's
2288command line.
2289@end table
2290
2291The behaviour for shared libraries on their own can also be controlled
2292by the @option{--[no-]allow-shlib-undefined} option.
2293
2294Normally the linker will generate an error message for each reported
2295unresolved symbol but the option @option{--warn-unresolved-symbols}
2296can change this to a warning.
2297
1715a13c
L
2298@kindex --verbose[=@var{NUMBER}]
2299@cindex verbose[=@var{NUMBER}]
252b5132 2300@item --dll-verbose
1715a13c 2301@itemx --verbose[=@var{NUMBER}]
ff5dcc92 2302Display the version number for @command{ld} and list the linker emulations
252b5132 2303supported. Display which input files can and cannot be opened. Display
1715a13c
L
2304the linker script being used by the linker. If the optional @var{NUMBER}
2305argument > 1, plugin symbol status will also be displayed.
252b5132
RH
2306
2307@kindex --version-script=@var{version-scriptfile}
2308@cindex version script, symbol versions
2509a395 2309@item --version-script=@var{version-scriptfile}
252b5132
RH
2310Specify the name of a version script to the linker. This is typically
2311used when creating shared libraries to specify additional information
36f63dca 2312about the version hierarchy for the library being created. This option
09e2aba4
DK
2313is only fully supported on ELF platforms which support shared libraries;
2314see @ref{VERSION}. It is partially supported on PE platforms, which can
2315use version scripts to filter symbol visibility in auto-export mode: any
2316symbols marked @samp{local} in the version script will not be exported.
2317@xref{WIN32}.
252b5132 2318
7ce691ae 2319@kindex --warn-common
252b5132
RH
2320@cindex warnings, on combining symbols
2321@cindex combining symbols, warnings on
2322@item --warn-common
2323Warn when a common symbol is combined with another common symbol or with
11e7fd74 2324a symbol definition. Unix linkers allow this somewhat sloppy practice,
252b5132
RH
2325but linkers on some other operating systems do not. This option allows
2326you to find potential problems from combining global symbols.
11e7fd74 2327Unfortunately, some C libraries use this practice, so you may get some
252b5132
RH
2328warnings about symbols in the libraries as well as in your programs.
2329
2330There are three kinds of global symbols, illustrated here by C examples:
2331
2332@table @samp
2333@item int i = 1;
2334A definition, which goes in the initialized data section of the output
2335file.
2336
2337@item extern int i;
2338An undefined reference, which does not allocate space.
2339There must be either a definition or a common symbol for the
2340variable somewhere.
2341
2342@item int i;
2343A common symbol. If there are only (one or more) common symbols for a
2344variable, it goes in the uninitialized data area of the output file.
2345The linker merges multiple common symbols for the same variable into a
2346single symbol. If they are of different sizes, it picks the largest
2347size. The linker turns a common symbol into a declaration, if there is
2348a definition of the same variable.
2349@end table
2350
2351The @samp{--warn-common} option can produce five kinds of warnings.
2352Each warning consists of a pair of lines: the first describes the symbol
2353just encountered, and the second describes the previous symbol
2354encountered with the same name. One or both of the two symbols will be
2355a common symbol.
2356
2357@enumerate
2358@item
2359Turning a common symbol into a reference, because there is already a
2360definition for the symbol.
2361@smallexample
2362@var{file}(@var{section}): warning: common of `@var{symbol}'
2363 overridden by definition
2364@var{file}(@var{section}): warning: defined here
2365@end smallexample
2366
2367@item
2368Turning a common symbol into a reference, because a later definition for
2369the symbol is encountered. This is the same as the previous case,
2370except that the symbols are encountered in a different order.
2371@smallexample
2372@var{file}(@var{section}): warning: definition of `@var{symbol}'
2373 overriding common
2374@var{file}(@var{section}): warning: common is here
2375@end smallexample
2376
2377@item
2378Merging a common symbol with a previous same-sized common symbol.
2379@smallexample
2380@var{file}(@var{section}): warning: multiple common
2381 of `@var{symbol}'
2382@var{file}(@var{section}): warning: previous common is here
2383@end smallexample
2384
2385@item
2386Merging a common symbol with a previous larger common symbol.
2387@smallexample
2388@var{file}(@var{section}): warning: common of `@var{symbol}'
2389 overridden by larger common
2390@var{file}(@var{section}): warning: larger common is here
2391@end smallexample
2392
2393@item
2394Merging a common symbol with a previous smaller common symbol. This is
2395the same as the previous case, except that the symbols are
2396encountered in a different order.
2397@smallexample
2398@var{file}(@var{section}): warning: common of `@var{symbol}'
2399 overriding smaller common
2400@var{file}(@var{section}): warning: smaller common is here
2401@end smallexample
2402@end enumerate
2403
2404@kindex --warn-constructors
2405@item --warn-constructors
2406Warn if any global constructors are used. This is only useful for a few
2407object file formats. For formats like COFF or ELF, the linker can not
2408detect the use of global constructors.
2409
2410@kindex --warn-multiple-gp
2411@item --warn-multiple-gp
2412Warn if multiple global pointer values are required in the output file.
2413This is only meaningful for certain processors, such as the Alpha.
2414Specifically, some processors put large-valued constants in a special
2415section. A special register (the global pointer) points into the middle
2416of this section, so that constants can be loaded efficiently via a
2417base-register relative addressing mode. Since the offset in
2418base-register relative mode is fixed and relatively small (e.g., 16
2419bits), this limits the maximum size of the constant pool. Thus, in
2420large programs, it is often necessary to use multiple global pointer
2421values in order to be able to address all possible constants. This
2422option causes a warning to be issued whenever this case occurs.
2423
2424@kindex --warn-once
2425@cindex warnings, on undefined symbols
2426@cindex undefined symbols, warnings on
2427@item --warn-once
2428Only warn once for each undefined symbol, rather than once per module
2429which refers to it.
2430
2431@kindex --warn-section-align
2432@cindex warnings, on section alignment
2433@cindex section alignment, warnings on
2434@item --warn-section-align
2435Warn if the address of an output section is changed because of
2436alignment. Typically, the alignment will be set by an input section.
2437The address will only be changed if it not explicitly specified; that
2438is, if the @code{SECTIONS} command does not specify a start address for
2439the section (@pxref{SECTIONS}).
2440
a6dbf402
L
2441@kindex --warn-textrel
2442@item --warn-textrel
2443Warn if the linker adds DT_TEXTREL to a position-independent executable
2444or shared object.
8fdd7217 2445
a0c402a5
L
2446@kindex --warn-alternate-em
2447@item --warn-alternate-em
2448Warn if an object has alternate ELF machine code.
2449
560e09e9
NC
2450@kindex --warn-unresolved-symbols
2451@item --warn-unresolved-symbols
2452If the linker is going to report an unresolved symbol (see the option
2453@option{--unresolved-symbols}) it will normally generate an error.
2454This option makes it generate a warning instead.
2455
2456@kindex --error-unresolved-symbols
2457@item --error-unresolved-symbols
2458This restores the linker's default behaviour of generating errors when
2459it is reporting unresolved symbols.
2460
252b5132
RH
2461@kindex --whole-archive
2462@cindex including an entire archive
2463@item --whole-archive
2464For each archive mentioned on the command line after the
ff5dcc92 2465@option{--whole-archive} option, include every object file in the archive
252b5132
RH
2466in the link, rather than searching the archive for the required object
2467files. This is normally used to turn an archive file into a shared
2468library, forcing every object to be included in the resulting shared
2469library. This option may be used more than once.
2470
7ec229ce 2471Two notes when using this option from gcc: First, gcc doesn't know
ff5dcc92
SC
2472about this option, so you have to use @option{-Wl,-whole-archive}.
2473Second, don't forget to use @option{-Wl,-no-whole-archive} after your
7ec229ce
DD
2474list of archives, because gcc will add its own list of archives to
2475your link and you may not want this flag to affect those as well.
2476
2509a395
SL
2477@kindex --wrap=@var{symbol}
2478@item --wrap=@var{symbol}
252b5132
RH
2479Use a wrapper function for @var{symbol}. Any undefined reference to
2480@var{symbol} will be resolved to @code{__wrap_@var{symbol}}. Any
2481undefined reference to @code{__real_@var{symbol}} will be resolved to
2482@var{symbol}.
2483
2484This can be used to provide a wrapper for a system function. The
2485wrapper function should be called @code{__wrap_@var{symbol}}. If it
2486wishes to call the system function, it should call
2487@code{__real_@var{symbol}}.
2488
2489Here is a trivial example:
2490
2491@smallexample
2492void *
cc2f008e 2493__wrap_malloc (size_t c)
252b5132 2494@{
cc2f008e 2495 printf ("malloc called with %zu\n", c);
252b5132
RH
2496 return __real_malloc (c);
2497@}
2498@end smallexample
2499
ff5dcc92 2500If you link other code with this file using @option{--wrap malloc}, then
252b5132
RH
2501all calls to @code{malloc} will call the function @code{__wrap_malloc}
2502instead. The call to @code{__real_malloc} in @code{__wrap_malloc} will
2503call the real @code{malloc} function.
2504
2505You may wish to provide a @code{__real_malloc} function as well, so that
ff5dcc92 2506links without the @option{--wrap} option will succeed. If you do this,
252b5132
RH
2507you should not put the definition of @code{__real_malloc} in the same
2508file as @code{__wrap_malloc}; if you do, the assembler may resolve the
2509call before the linker has a chance to wrap it to @code{malloc}.
2510
4ea904ed
SH
2511Only undefined references are replaced by the linker. So, translation unit
2512internal references to @var{symbol} are not resolved to
2513@code{__wrap_@var{symbol}}. In the next example, the call to @code{f} in
2514@code{g} is not resolved to @code{__wrap_f}.
2515
2516@smallexample
2517int
2518f (void)
2519@{
2520 return 123;
2521@}
2522
2523int
2524g (void)
2525@{
2526 return f();
2527@}
2528@end smallexample
2529
6aa29e7b 2530@kindex --eh-frame-hdr
29063f8b 2531@kindex --no-eh-frame-hdr
6aa29e7b 2532@item --eh-frame-hdr
29063f8b
NC
2533@itemx --no-eh-frame-hdr
2534Request (@option{--eh-frame-hdr}) or suppress
2535(@option{--no-eh-frame-hdr}) the creation of @code{.eh_frame_hdr}
2536section and ELF @code{PT_GNU_EH_FRAME} segment header.
6aa29e7b 2537
e41b3a13
JJ
2538@kindex --ld-generated-unwind-info
2539@item --no-ld-generated-unwind-info
2540Request creation of @code{.eh_frame} unwind info for linker
2541generated code sections like PLT. This option is on by default
2542if linker generated unwind info is supported.
2543
6c1439be
L
2544@kindex --enable-new-dtags
2545@kindex --disable-new-dtags
2546@item --enable-new-dtags
2547@itemx --disable-new-dtags
2548This linker can create the new dynamic tags in ELF. But the older ELF
2549systems may not understand them. If you specify
b1b00fcc
MF
2550@option{--enable-new-dtags}, the new dynamic tags will be created as needed
2551and older dynamic tags will be omitted.
ff5dcc92 2552If you specify @option{--disable-new-dtags}, no new dynamic tags will be
6c1439be
L
2553created. By default, the new dynamic tags are not created. Note that
2554those options are only available for ELF systems.
2555
2d643429 2556@kindex --hash-size=@var{number}
e185dd51 2557@item --hash-size=@var{number}
2d643429
NC
2558Set the default size of the linker's hash tables to a prime number
2559close to @var{number}. Increasing this value can reduce the length of
2560time it takes the linker to perform its tasks, at the expense of
2561increasing the linker's memory requirements. Similarly reducing this
2562value can reduce the memory requirements at the expense of speed.
2563
fdc90cb4
JJ
2564@kindex --hash-style=@var{style}
2565@item --hash-style=@var{style}
2566Set the type of linker's hash table(s). @var{style} can be either
2567@code{sysv} for classic ELF @code{.hash} section, @code{gnu} for
2568new style GNU @code{.gnu.hash} section or @code{both} for both
2569the classic ELF @code{.hash} and new style GNU @code{.gnu.hash}
c8455dc9
NC
2570hash tables. The default depends upon how the linker was configured,
2571but for most Linux based systems it will be @code{both}.
fdc90cb4 2572
0ce398f1
L
2573@kindex --compress-debug-sections=none
2574@kindex --compress-debug-sections=zlib
2575@kindex --compress-debug-sections=zlib-gnu
2576@kindex --compress-debug-sections=zlib-gabi
2577@item --compress-debug-sections=none
2578@itemx --compress-debug-sections=zlib
2579@itemx --compress-debug-sections=zlib-gnu
2580@itemx --compress-debug-sections=zlib-gabi
9af89fba
NC
2581On ELF platforms, these options control how DWARF debug sections are
2582compressed using zlib.
2583
2584@option{--compress-debug-sections=none} doesn't compress DWARF debug
2585sections. @option{--compress-debug-sections=zlib-gnu} compresses
2586DWARF debug sections and renames them to begin with @samp{.zdebug}
2587instead of @samp{.debug}. @option{--compress-debug-sections=zlib-gabi}
2588also compresses DWARF debug sections, but rather than renaming them it
2589sets the SHF_COMPRESSED flag in the sections' headers.
2590
2591The @option{--compress-debug-sections=zlib} option is an alias for
2592@option{--compress-debug-sections=zlib-gabi}.
2593
2594Note that this option overrides any compression in input debug
2595sections, so if a binary is linked with @option{--compress-debug-sections=none}
2596for example, then any compressed debug sections in input files will be
2597uncompressed before they are copied into the output binary.
2598
2599The default compression behaviour varies depending upon the target
2600involved and the configure options used to build the toolchain. The
2601default can be determined by examining the output from the linker's
2602@option{--help} option.
0ce398f1 2603
35835446
JR
2604@kindex --reduce-memory-overheads
2605@item --reduce-memory-overheads
2606This option reduces memory requirements at ld runtime, at the expense of
f2a8f148 2607linking speed. This was introduced to select the old O(n^2) algorithm
35835446 2608for link map file generation, rather than the new O(n) algorithm which uses
2d643429
NC
2609about 40% more memory for symbol storage.
2610
4f9c04f7 2611Another effect of the switch is to set the default hash table size to
2d643429 26121021, which again saves memory at the cost of lengthening the linker's
a85785bc 2613run time. This is not done however if the @option{--hash-size} switch
2d643429
NC
2614has been used.
2615
2616The @option{--reduce-memory-overheads} switch may be also be used to
2617enable other tradeoffs in future versions of the linker.
35835446 2618
c0065db7
RM
2619@kindex --build-id
2620@kindex --build-id=@var{style}
2621@item --build-id
2622@itemx --build-id=@var{style}
61e2488c 2623Request the creation of a @code{.note.gnu.build-id} ELF note section
6033bf41 2624or a @code{.buildid} COFF section. The contents of the note are
61e2488c
JT
2625unique bits identifying this linked file. @var{style} can be
2626@code{uuid} to use 128 random bits, @code{sha1} to use a 160-bit
2627@sc{SHA1} hash on the normative parts of the output contents,
2628@code{md5} to use a 128-bit @sc{MD5} hash on the normative parts of
2629the output contents, or @code{0x@var{hexstring}} to use a chosen bit
2630string specified as an even number of hexadecimal digits (@code{-} and
2631@code{:} characters between digit pairs are ignored). If @var{style}
2632is omitted, @code{sha1} is used.
24382dca
RM
2633
2634The @code{md5} and @code{sha1} styles produces an identifier
2635that is always the same in an identical output file, but will be
2636unique among all nonidentical output files. It is not intended
2637to be compared as a checksum for the file's contents. A linked
2638file may be changed later by other tools, but the build ID bit
2639string identifying the original linked file does not change.
c0065db7
RM
2640
2641Passing @code{none} for @var{style} disables the setting from any
2642@code{--build-id} options earlier on the command line.
252b5132
RH
2643@end table
2644
0285c67d
NC
2645@c man end
2646
36f63dca 2647@subsection Options Specific to i386 PE Targets
252b5132 2648
0285c67d
NC
2649@c man begin OPTIONS
2650
ff5dcc92 2651The i386 PE linker supports the @option{-shared} option, which causes
252b5132
RH
2652the output to be a dynamically linked library (DLL) instead of a
2653normal executable. You should name the output @code{*.dll} when you
2654use this option. In addition, the linker fully supports the standard
2655@code{*.def} files, which may be specified on the linker command line
2656like an object file (in fact, it should precede archives it exports
2657symbols from, to ensure that they get linked in, just like a normal
2658object file).
2659
2660In addition to the options common to all targets, the i386 PE linker
a05a5b64 2661support additional command-line options that are specific to the i386
252b5132
RH
2662PE target. Options that take values may be separated from their
2663values by either a space or an equals sign.
2664
ff5dcc92 2665@table @gcctabopt
252b5132
RH
2666
2667@kindex --add-stdcall-alias
2668@item --add-stdcall-alias
2669If given, symbols with a stdcall suffix (@@@var{nn}) will be exported
2670as-is and also with the suffix stripped.
bb10df36 2671[This option is specific to the i386 PE targeted port of the linker]
252b5132
RH
2672
2673@kindex --base-file
2674@item --base-file @var{file}
2675Use @var{file} as the name of a file in which to save the base
2676addresses of all the relocations needed for generating DLLs with
2677@file{dlltool}.
bb10df36 2678[This is an i386 PE specific option]
252b5132
RH
2679
2680@kindex --dll
2681@item --dll
2682Create a DLL instead of a regular executable. You may also use
ff5dcc92 2683@option{-shared} or specify a @code{LIBRARY} in a given @code{.def}
252b5132 2684file.
bb10df36 2685[This option is specific to the i386 PE targeted port of the linker]
252b5132 2686
88183869
DK
2687@kindex --enable-long-section-names
2688@kindex --disable-long-section-names
2689@item --enable-long-section-names
2690@itemx --disable-long-section-names
56e6cf80 2691The PE variants of the COFF object format add an extension that permits
88183869 2692the use of section names longer than eight characters, the normal limit
56e6cf80
NC
2693for COFF. By default, these names are only allowed in object files, as
2694fully-linked executable images do not carry the COFF string table required
88183869
DK
2695to support the longer names. As a GNU extension, it is possible to
2696allow their use in executable images as well, or to (probably pointlessly!)
2697disallow it in object files, by using these two options. Executable images
2698generated with these long section names are slightly non-standard, carrying
2699as they do a string table, and may generate confusing output when examined
9d5777a3
RM
2700with non-GNU PE-aware tools, such as file viewers and dumpers. However,
2701GDB relies on the use of PE long section names to find Dwarf-2 debug
3efd345c
DK
2702information sections in an executable image at runtime, and so if neither
2703option is specified on the command-line, @command{ld} will enable long
2704section names, overriding the default and technically correct behaviour,
2705when it finds the presence of debug information while linking an executable
2706image and not stripping symbols.
88183869
DK
2707[This option is valid for all PE targeted ports of the linker]
2708
252b5132
RH
2709@kindex --enable-stdcall-fixup
2710@kindex --disable-stdcall-fixup
2711@item --enable-stdcall-fixup
2712@itemx --disable-stdcall-fixup
2713If the link finds a symbol that it cannot resolve, it will attempt to
36f63dca 2714do ``fuzzy linking'' by looking for another defined symbol that differs
252b5132
RH
2715only in the format of the symbol name (cdecl vs stdcall) and will
2716resolve that symbol by linking to the match. For example, the
2717undefined symbol @code{_foo} might be linked to the function
2718@code{_foo@@12}, or the undefined symbol @code{_bar@@16} might be linked
2719to the function @code{_bar}. When the linker does this, it prints a
2720warning, since it normally should have failed to link, but sometimes
2721import libraries generated from third-party dlls may need this feature
ff5dcc92 2722to be usable. If you specify @option{--enable-stdcall-fixup}, this
252b5132 2723feature is fully enabled and warnings are not printed. If you specify
ff5dcc92 2724@option{--disable-stdcall-fixup}, this feature is disabled and such
252b5132 2725mismatches are considered to be errors.
bb10df36 2726[This option is specific to the i386 PE targeted port of the linker]
252b5132 2727
522f09cd
KT
2728@kindex --leading-underscore
2729@kindex --no-leading-underscore
2730@item --leading-underscore
2731@itemx --no-leading-underscore
2732For most targets default symbol-prefix is an underscore and is defined
2733in target's description. By this option it is possible to
2734disable/enable the default underscore symbol-prefix.
2735
252b5132
RH
2736@cindex DLLs, creating
2737@kindex --export-all-symbols
2738@item --export-all-symbols
2739If given, all global symbols in the objects used to build a DLL will
2740be exported by the DLL. Note that this is the default if there
2741otherwise wouldn't be any exported symbols. When symbols are
2742explicitly exported via DEF files or implicitly exported via function
2743attributes, the default is to not export anything else unless this
2744option is given. Note that the symbols @code{DllMain@@12},
ece2d90e 2745@code{DllEntryPoint@@0}, @code{DllMainCRTStartup@@12}, and
b044cda1 2746@code{impure_ptr} will not be automatically
ece2d90e
NC
2747exported. Also, symbols imported from other DLLs will not be
2748re-exported, nor will symbols specifying the DLL's internal layout
2749such as those beginning with @code{_head_} or ending with
2750@code{_iname}. In addition, no symbols from @code{libgcc},
b044cda1
CW
2751@code{libstd++}, @code{libmingw32}, or @code{crtX.o} will be exported.
2752Symbols whose names begin with @code{__rtti_} or @code{__builtin_} will
2753not be exported, to help with C++ DLLs. Finally, there is an
ece2d90e 2754extensive list of cygwin-private symbols that are not exported
b044cda1 2755(obviously, this applies on when building DLLs for cygwin targets).
ece2d90e 2756These cygwin-excludes are: @code{_cygwin_dll_entry@@12},
b044cda1 2757@code{_cygwin_crt0_common@@8}, @code{_cygwin_noncygwin_dll_entry@@12},
ece2d90e 2758@code{_fmode}, @code{_impure_ptr}, @code{cygwin_attach_dll},
b044cda1 2759@code{cygwin_premain0}, @code{cygwin_premain1}, @code{cygwin_premain2},
ece2d90e 2760@code{cygwin_premain3}, and @code{environ}.
bb10df36 2761[This option is specific to the i386 PE targeted port of the linker]
252b5132
RH
2762
2763@kindex --exclude-symbols
1d0a3c9c 2764@item --exclude-symbols @var{symbol},@var{symbol},...
252b5132
RH
2765Specifies a list of symbols which should not be automatically
2766exported. The symbol names may be delimited by commas or colons.
bb10df36 2767[This option is specific to the i386 PE targeted port of the linker]
252b5132 2768
2927aaca
NC
2769@kindex --exclude-all-symbols
2770@item --exclude-all-symbols
2771Specifies no symbols should be automatically exported.
2772[This option is specific to the i386 PE targeted port of the linker]
2773
252b5132
RH
2774@kindex --file-alignment
2775@item --file-alignment
2776Specify the file alignment. Sections in the file will always begin at
2777file offsets which are multiples of this number. This defaults to
2778512.
bb10df36 2779[This option is specific to the i386 PE targeted port of the linker]
252b5132
RH
2780
2781@cindex heap size
2782@kindex --heap
2783@item --heap @var{reserve}
2784@itemx --heap @var{reserve},@var{commit}
a00b50c5 2785Specify the number of bytes of memory to reserve (and optionally commit)
fe6d7d6a 2786to be used as heap for this program. The default is 1MB reserved, 4K
252b5132 2787committed.
bb10df36 2788[This option is specific to the i386 PE targeted port of the linker]
252b5132
RH
2789
2790@cindex image base
2791@kindex --image-base
2792@item --image-base @var{value}
2793Use @var{value} as the base address of your program or dll. This is
2794the lowest memory location that will be used when your program or dll
2795is loaded. To reduce the need to relocate and improve performance of
2796your dlls, each should have a unique base address and not overlap any
2797other dlls. The default is 0x400000 for executables, and 0x10000000
2798for dlls.
bb10df36 2799[This option is specific to the i386 PE targeted port of the linker]
252b5132
RH
2800
2801@kindex --kill-at
2802@item --kill-at
2803If given, the stdcall suffixes (@@@var{nn}) will be stripped from
2804symbols before they are exported.
bb10df36 2805[This option is specific to the i386 PE targeted port of the linker]
252b5132 2806
26d2d8a2
BF
2807@kindex --large-address-aware
2808@item --large-address-aware
b45619c0 2809If given, the appropriate bit in the ``Characteristics'' field of the COFF
26d2d8a2 2810header is set to indicate that this executable supports virtual addresses
b45619c0 2811greater than 2 gigabytes. This should be used in conjunction with the /3GB
26d2d8a2
BF
2812or /USERVA=@var{value} megabytes switch in the ``[operating systems]''
2813section of the BOOT.INI. Otherwise, this bit has no effect.
2814[This option is specific to PE targeted ports of the linker]
2815
f69a2f97
NC
2816@kindex --disable-large-address-aware
2817@item --disable-large-address-aware
2818Reverts the effect of a previous @samp{--large-address-aware} option.
2819This is useful if @samp{--large-address-aware} is always set by the compiler
2820driver (e.g. Cygwin gcc) and the executable does not support virtual
2821addresses greater than 2 gigabytes.
2822[This option is specific to PE targeted ports of the linker]
2823
252b5132
RH
2824@kindex --major-image-version
2825@item --major-image-version @var{value}
36f63dca 2826Sets the major number of the ``image version''. Defaults to 1.
bb10df36 2827[This option is specific to the i386 PE targeted port of the linker]
252b5132
RH
2828
2829@kindex --major-os-version
2830@item --major-os-version @var{value}
36f63dca 2831Sets the major number of the ``os version''. Defaults to 4.
bb10df36 2832[This option is specific to the i386 PE targeted port of the linker]
252b5132
RH
2833
2834@kindex --major-subsystem-version
2835@item --major-subsystem-version @var{value}
36f63dca 2836Sets the major number of the ``subsystem version''. Defaults to 4.
bb10df36 2837[This option is specific to the i386 PE targeted port of the linker]
252b5132
RH
2838
2839@kindex --minor-image-version
2840@item --minor-image-version @var{value}
36f63dca 2841Sets the minor number of the ``image version''. Defaults to 0.
bb10df36 2842[This option is specific to the i386 PE targeted port of the linker]
252b5132
RH
2843
2844@kindex --minor-os-version
2845@item --minor-os-version @var{value}
36f63dca 2846Sets the minor number of the ``os version''. Defaults to 0.
bb10df36 2847[This option is specific to the i386 PE targeted port of the linker]
252b5132
RH
2848
2849@kindex --minor-subsystem-version
2850@item --minor-subsystem-version @var{value}
36f63dca 2851Sets the minor number of the ``subsystem version''. Defaults to 0.
bb10df36 2852[This option is specific to the i386 PE targeted port of the linker]
252b5132
RH
2853
2854@cindex DEF files, creating
2855@cindex DLLs, creating
2856@kindex --output-def
2857@item --output-def @var{file}
2858The linker will create the file @var{file} which will contain a DEF
2859file corresponding to the DLL the linker is generating. This DEF file
2860(which should be called @code{*.def}) may be used to create an import
2861library with @code{dlltool} or may be used as a reference to
2862automatically or implicitly exported symbols.
bb10df36 2863[This option is specific to the i386 PE targeted port of the linker]
252b5132 2864
b044cda1 2865@cindex DLLs, creating
b044cda1
CW
2866@kindex --enable-auto-image-base
2867@item --enable-auto-image-base
d0e6d77b
CF
2868@itemx --enable-auto-image-base=@var{value}
2869Automatically choose the image base for DLLs, optionally starting with base
2870@var{value}, unless one is specified using the @code{--image-base} argument.
2871By using a hash generated from the dllname to create unique image bases
2872for each DLL, in-memory collisions and relocations which can delay program
2873execution are avoided.
bb10df36 2874[This option is specific to the i386 PE targeted port of the linker]
b044cda1
CW
2875
2876@kindex --disable-auto-image-base
2877@item --disable-auto-image-base
2878Do not automatically generate a unique image base. If there is no
2879user-specified image base (@code{--image-base}) then use the platform
2880default.
bb10df36 2881[This option is specific to the i386 PE targeted port of the linker]
b044cda1
CW
2882
2883@cindex DLLs, linking to
2884@kindex --dll-search-prefix
2885@item --dll-search-prefix @var{string}
489d0400 2886When linking dynamically to a dll without an import library,
ece2d90e 2887search for @code{<string><basename>.dll} in preference to
560e09e9 2888@code{lib<basename>.dll}. This behaviour allows easy distinction
b044cda1
CW
2889between DLLs built for the various "subplatforms": native, cygwin,
2890uwin, pw, etc. For instance, cygwin DLLs typically use
ece2d90e 2891@code{--dll-search-prefix=cyg}.
bb10df36 2892[This option is specific to the i386 PE targeted port of the linker]
b044cda1
CW
2893
2894@kindex --enable-auto-import
2895@item --enable-auto-import
ece2d90e 2896Do sophisticated linking of @code{_symbol} to @code{__imp__symbol} for
317ff008
EB
2897DATA imports from DLLs, thus making it possible to bypass the dllimport
2898mechanism on the user side and to reference unmangled symbol names.
2899[This option is specific to the i386 PE targeted port of the linker]
2900
2901The following remarks pertain to the original implementation of the
2902feature and are obsolete nowadays for Cygwin and MinGW targets.
2903
2904Note: Use of the 'auto-import' extension will cause the text section
2905of the image file to be made writable. This does not conform to the
2906PE-COFF format specification published by Microsoft.
4d8907ac 2907
e2a83dd0
NC
2908Note - use of the 'auto-import' extension will also cause read only
2909data which would normally be placed into the .rdata section to be
2910placed into the .data section instead. This is in order to work
2911around a problem with consts that is described here:
2912http://www.cygwin.com/ml/cygwin/2004-09/msg01101.html
2913
4d8907ac
DS
2914Using 'auto-import' generally will 'just work' -- but sometimes you may
2915see this message:
0d888aac 2916
ece2d90e 2917"variable '<var>' can't be auto-imported. Please read the
0d888aac
CW
2918documentation for ld's @code{--enable-auto-import} for details."
2919
ece2d90e
NC
2920This message occurs when some (sub)expression accesses an address
2921ultimately given by the sum of two constants (Win32 import tables only
c0065db7
RM
2922allow one). Instances where this may occur include accesses to member
2923fields of struct variables imported from a DLL, as well as using a
2924constant index into an array variable imported from a DLL. Any
2f8d8971
NC
2925multiword variable (arrays, structs, long long, etc) may trigger
2926this error condition. However, regardless of the exact data type
2927of the offending exported variable, ld will always detect it, issue
2928the warning, and exit.
2929
2930There are several ways to address this difficulty, regardless of the
2931data type of the exported variable:
0d888aac 2932
2fa9fc65
NC
2933One way is to use --enable-runtime-pseudo-reloc switch. This leaves the task
2934of adjusting references in your client code for runtime environment, so
560e09e9 2935this method works only when runtime environment supports this feature.
2fa9fc65 2936
c0065db7
RM
2937A second solution is to force one of the 'constants' to be a variable --
2938that is, unknown and un-optimizable at compile time. For arrays,
2939there are two possibilities: a) make the indexee (the array's address)
0d888aac
CW
2940a variable, or b) make the 'constant' index a variable. Thus:
2941
2942@example
2943extern type extern_array[];
c0065db7 2944extern_array[1] -->
0d888aac
CW
2945 @{ volatile type *t=extern_array; t[1] @}
2946@end example
2947
2948or
2949
2950@example
2951extern type extern_array[];
c0065db7 2952extern_array[1] -->
0d888aac
CW
2953 @{ volatile int t=1; extern_array[t] @}
2954@end example
2955
c0065db7 2956For structs (and most other multiword data types) the only option
2f8d8971 2957is to make the struct itself (or the long long, or the ...) variable:
0d888aac
CW
2958
2959@example
2960extern struct s extern_struct;
c0065db7 2961extern_struct.field -->
0d888aac
CW
2962 @{ volatile struct s *t=&extern_struct; t->field @}
2963@end example
2964
c406afaf
NC
2965or
2966
2967@example
2968extern long long extern_ll;
2969extern_ll -->
2970 @{ volatile long long * local_ll=&extern_ll; *local_ll @}
2971@end example
2972
2fa9fc65 2973A third method of dealing with this difficulty is to abandon
c0065db7 2974'auto-import' for the offending symbol and mark it with
11e7fd74 2975@code{__declspec(dllimport)}. However, in practice that
0d888aac 2976requires using compile-time #defines to indicate whether you are
c0065db7
RM
2977building a DLL, building client code that will link to the DLL, or
2978merely building/linking to a static library. In making the choice
2979between the various methods of resolving the 'direct address with
0d888aac
CW
2980constant offset' problem, you should consider typical real-world usage:
2981
2982Original:
2983@example
2984--foo.h
2985extern int arr[];
2986--foo.c
2987#include "foo.h"
2988void main(int argc, char **argv)@{
2989 printf("%d\n",arr[1]);
2990@}
2991@end example
2992
2993Solution 1:
2994@example
2995--foo.h
2996extern int arr[];
2997--foo.c
2998#include "foo.h"
2999void main(int argc, char **argv)@{
3000 /* This workaround is for win32 and cygwin; do not "optimize" */
3001 volatile int *parr = arr;
3002 printf("%d\n",parr[1]);
3003@}
3004@end example
3005
3006Solution 2:
3007@example
3008--foo.h
3009/* Note: auto-export is assumed (no __declspec(dllexport)) */
3010#if (defined(_WIN32) || defined(__CYGWIN__)) && \
3011 !(defined(FOO_BUILD_DLL) || defined(FOO_STATIC))
3012#define FOO_IMPORT __declspec(dllimport)
3013#else
3014#define FOO_IMPORT
3015#endif
3016extern FOO_IMPORT int arr[];
3017--foo.c
3018#include "foo.h"
3019void main(int argc, char **argv)@{
3020 printf("%d\n",arr[1]);
3021@}
3022@end example
3023
c0065db7 3024A fourth way to avoid this problem is to re-code your
0d888aac
CW
3025library to use a functional interface rather than a data interface
3026for the offending variables (e.g. set_foo() and get_foo() accessor
3027functions).
b044cda1
CW
3028
3029@kindex --disable-auto-import
3030@item --disable-auto-import
c0065db7 3031Do not attempt to do sophisticated linking of @code{_symbol} to
b044cda1 3032@code{__imp__symbol} for DATA imports from DLLs.
bb10df36 3033[This option is specific to the i386 PE targeted port of the linker]
b044cda1 3034
2fa9fc65
NC
3035@kindex --enable-runtime-pseudo-reloc
3036@item --enable-runtime-pseudo-reloc
3037If your code contains expressions described in --enable-auto-import section,
3038that is, DATA imports from DLL with non-zero offset, this switch will create
3039a vector of 'runtime pseudo relocations' which can be used by runtime
c0065db7 3040environment to adjust references to such data in your client code.
bb10df36 3041[This option is specific to the i386 PE targeted port of the linker]
2fa9fc65
NC
3042
3043@kindex --disable-runtime-pseudo-reloc
3044@item --disable-runtime-pseudo-reloc
317ff008 3045Do not create pseudo relocations for non-zero offset DATA imports from DLLs.
bb10df36 3046[This option is specific to the i386 PE targeted port of the linker]
2fa9fc65 3047
b044cda1
CW
3048@kindex --enable-extra-pe-debug
3049@item --enable-extra-pe-debug
3050Show additional debug info related to auto-import symbol thunking.
bb10df36 3051[This option is specific to the i386 PE targeted port of the linker]
b044cda1 3052
252b5132
RH
3053@kindex --section-alignment
3054@item --section-alignment
3055Sets the section alignment. Sections in memory will always begin at
3056addresses which are a multiple of this number. Defaults to 0x1000.
bb10df36 3057[This option is specific to the i386 PE targeted port of the linker]
252b5132
RH
3058
3059@cindex stack size
3060@kindex --stack
3061@item --stack @var{reserve}
3062@itemx --stack @var{reserve},@var{commit}
a00b50c5 3063Specify the number of bytes of memory to reserve (and optionally commit)
fe6d7d6a 3064to be used as stack for this program. The default is 2MB reserved, 4K
252b5132 3065committed.
bb10df36 3066[This option is specific to the i386 PE targeted port of the linker]
252b5132
RH
3067
3068@kindex --subsystem
3069@item --subsystem @var{which}
3070@itemx --subsystem @var{which}:@var{major}
3071@itemx --subsystem @var{which}:@var{major}.@var{minor}
3072Specifies the subsystem under which your program will execute. The
3073legal values for @var{which} are @code{native}, @code{windows},
33f362e1
NC
3074@code{console}, @code{posix}, and @code{xbox}. You may optionally set
3075the subsystem version also. Numeric values are also accepted for
3076@var{which}.
bb10df36 3077[This option is specific to the i386 PE targeted port of the linker]
252b5132 3078
2f563b51
DK
3079The following options set flags in the @code{DllCharacteristics} field
3080of the PE file header:
3081[These options are specific to PE targeted ports of the linker]
3082
2d5c3743
NC
3083@kindex --high-entropy-va
3084@item --high-entropy-va
3085Image is compatible with 64-bit address space layout randomization
3086(ASLR).
dc9bd8c9
HD
3087This option also implies @option{--dynamicbase} and
3088@option{--enable-reloc-section}.
2d5c3743 3089
2f563b51
DK
3090@kindex --dynamicbase
3091@item --dynamicbase
3092The image base address may be relocated using address space layout
3093randomization (ASLR). This feature was introduced with MS Windows
3094Vista for i386 PE targets.
dc9bd8c9 3095This option also implies @option{--enable-reloc-section}.
2f563b51
DK
3096
3097@kindex --forceinteg
3098@item --forceinteg
3099Code integrity checks are enforced.
3100
3101@kindex --nxcompat
3102@item --nxcompat
3103The image is compatible with the Data Execution Prevention.
3104This feature was introduced with MS Windows XP SP2 for i386 PE targets.
3105
3106@kindex --no-isolation
3107@item --no-isolation
3108Although the image understands isolation, do not isolate the image.
3109
3110@kindex --no-seh
3111@item --no-seh
3112The image does not use SEH. No SE handler may be called from
3113this image.
3114
3115@kindex --no-bind
3116@item --no-bind
3117Do not bind this image.
3118
3119@kindex --wdmdriver
3120@item --wdmdriver
3121The driver uses the MS Windows Driver Model.
9d5777a3 3122
2f563b51
DK
3123@kindex --tsaware
3124@item --tsaware
3125The image is Terminal Server aware.
3126
0cb112f7
CF
3127@kindex --insert-timestamp
3128@item --insert-timestamp
eeb14e5a
LZ
3129@itemx --no-insert-timestamp
3130Insert a real timestamp into the image. This is the default behaviour
3131as it matches legacy code and it means that the image will work with
3132other, proprietary tools. The problem with this default is that it
56e6cf80 3133will result in slightly different images being produced each time the
eeb14e5a
LZ
3134same sources are linked. The option @option{--no-insert-timestamp}
3135can be used to insert a zero value for the timestamp, this ensuring
56e6cf80 3136that binaries produced from identical sources will compare
eeb14e5a 3137identically.
dc9bd8c9
HD
3138
3139@kindex --enable-reloc-section
3140@item --enable-reloc-section
3141Create the base relocation table, which is necessary if the image
3142is loaded at a different image base than specified in the PE header.
252b5132
RH
3143@end table
3144
0285c67d
NC
3145@c man end
3146
ac145307
BS
3147@ifset C6X
3148@subsection Options specific to C6X uClinux targets
3149
3150@c man begin OPTIONS
3151
3152The C6X uClinux target uses a binary format called DSBT to support shared
3153libraries. Each shared library in the system needs to have a unique index;
3154all executables use an index of 0.
3155
3156@table @gcctabopt
3157
3158@kindex --dsbt-size
3159@item --dsbt-size @var{size}
56e6cf80 3160This option sets the number of entries in the DSBT of the current executable
ac145307
BS
3161or shared library to @var{size}. The default is to create a table with 64
3162entries.
3163
3164@kindex --dsbt-index
3165@item --dsbt-index @var{index}
3166This option sets the DSBT index of the current executable or shared library
3167to @var{index}. The default is 0, which is appropriate for generating
3168executables. If a shared library is generated with a DSBT index of 0, the
3169@code{R_C6000_DSBT_INDEX} relocs are copied into the output file.
3170
fbd9ad90
PB
3171@kindex --no-merge-exidx-entries
3172The @samp{--no-merge-exidx-entries} switch disables the merging of adjacent
3173exidx entries in frame unwind info.
3174
ac145307
BS
3175@end table
3176
3177@c man end
3178@end ifset
3179
b8891f8d
AJ
3180@ifset CSKY
3181@subsection Options specific to C-SKY targets
3182
3183@c man begin OPTIONS
3184
3185@table @gcctabopt
3186
3187@kindex --branch-stub on C-SKY
3188@item --branch-stub
3189This option enables linker branch relaxation by inserting branch stub
3190sections when needed to extend the range of branches. This option is
3191usually not required since C-SKY supports branch and call instructions that
3192can access the full memory range and branch relaxation is normally handled by
3193the compiler or assembler.
3194
3195@kindex --stub-group-size on C-SKY
3196@item --stub-group-size=@var{N}
3197This option allows finer control of linker branch stub creation.
3198It sets the maximum size of a group of input sections that can
3199be handled by one stub section. A negative value of @var{N} locates
3200stub sections after their branches, while a positive value allows stub
3201sections to appear either before or after the branches. Values of
3202@samp{1} or @samp{-1} indicate that the
3203linker should choose suitable defaults.
3204
3205@end table
3206
3207@c man end
3208@end ifset
3209
93fd0973
SC
3210@ifset M68HC11
3211@subsection Options specific to Motorola 68HC11 and 68HC12 targets
3212
3213@c man begin OPTIONS
3214
3215The 68HC11 and 68HC12 linkers support specific options to control the
3216memory bank switching mapping and trampoline code generation.
3217
3218@table @gcctabopt
3219
3220@kindex --no-trampoline
3221@item --no-trampoline
3222This option disables the generation of trampoline. By default a trampoline
3223is generated for each far function which is called using a @code{jsr}
3224instruction (this happens when a pointer to a far function is taken).
3225
3226@kindex --bank-window
3227@item --bank-window @var{name}
3228This option indicates to the linker the name of the memory region in
3229the @samp{MEMORY} specification that describes the memory bank window.
3230The definition of such region is then used by the linker to compute
3231paging and addresses within the memory window.
3232
3233@end table
3234
3235@c man end
3236@end ifset
3237
7fb9f789
NC
3238@ifset M68K
3239@subsection Options specific to Motorola 68K target
3240
3241@c man begin OPTIONS
3242
3243The following options are supported to control handling of GOT generation
3244when linking for 68K targets.
3245
3246@table @gcctabopt
3247
3248@kindex --got
3249@item --got=@var{type}
3250This option tells the linker which GOT generation scheme to use.
3251@var{type} should be one of @samp{single}, @samp{negative},
3252@samp{multigot} or @samp{target}. For more information refer to the
3253Info entry for @file{ld}.
3254
3255@end table
3256
3257@c man end
3258@end ifset
3259
833794fc
MR
3260@ifset MIPS
3261@subsection Options specific to MIPS targets
3262
3263@c man begin OPTIONS
3264
3265The following options are supported to control microMIPS instruction
8b10b0b3
MR
3266generation and branch relocation checks for ISA mode transitions when
3267linking for MIPS targets.
833794fc
MR
3268
3269@table @gcctabopt
3270
3271@kindex --insn32
3272@item --insn32
3273@kindex --no-insn32
3274@itemx --no-insn32
3275These options control the choice of microMIPS instructions used in code
3276generated by the linker, such as that in the PLT or lazy binding stubs,
3277or in relaxation. If @samp{--insn32} is used, then the linker only uses
327832-bit instruction encodings. By default or if @samp{--no-insn32} is
3279used, all instruction encodings are used, including 16-bit ones where
3280possible.
3281
8b10b0b3
MR
3282@kindex --ignore-branch-isa
3283@item --ignore-branch-isa
3284@kindex --no-ignore-branch-isa
3285@itemx --no-ignore-branch-isa
3286These options control branch relocation checks for invalid ISA mode
3287transitions. If @samp{--ignore-branch-isa} is used, then the linker
3288accepts any branch relocations and any ISA mode transition required
3289is lost in relocation calculation, except for some cases of @code{BAL}
3290instructions which meet relaxation conditions and are converted to
3291equivalent @code{JALX} instructions as the associated relocation is
3292calculated. By default or if @samp{--no-ignore-branch-isa} is used
3293a check is made causing the loss of an ISA mode transition to produce
3294an error.
3295
3734320d
MF
3296@kindex --compact-branches
3297@item --compact-branches
3298@kindex --no-compact-branches
fa1477dc 3299@itemx --no-compact-branches
3734320d
MF
3300These options control the generation of compact instructions by the linker
3301in the PLT entries for MIPS R6.
3302
833794fc
MR
3303@end table
3304
3305@c man end
3306@end ifset
3307
fa1477dc
SC
3308
3309@ifset PDP11
3310@subsection Options specific to PDP11 targets
3311
3312@c man begin OPTIONS
3313
3314For the pdp11-aout target, three variants of the output format can be
3315produced as selected by the following options. The default variant
3316for pdp11-aout is the @samp{--omagic} option, whereas for other
3317targets @samp{--nmagic} is the default. The @samp{--imagic} option is
3318defined only for the pdp11-aout target, while the others are described
3319here as they apply to the pdp11-aout target.
3320
3321@table @gcctabopt
3322
3323@kindex -N
3324@item -N
3325@kindex --omagic
3326@itemx --omagic
3327
3328Mark the output as @code{OMAGIC} (0407) in the @file{a.out} header to
3329indicate that the text segment is not to be write-protected and
3330shared. Since the text and data sections are both readable and
3331writable, the data section is allocated immediately contiguous after
3332the text segment. This is the oldest format for PDP11 executable
3333programs and is the default for @command{ld} on PDP11 Unix systems
3334from the beginning through 2.11BSD.
3335
3336@kindex -n
3337@item -n
3338@kindex --nmagic
3339@itemx --nmagic
3340
3341Mark the output as @code{NMAGIC} (0410) in the @file{a.out} header to
3342indicate that when the output file is executed, the text portion will
3343be read-only and shareable among all processes executing the same
3344file. This involves moving the data areas up to the first possible 8K
3345byte page boundary following the end of the text. This option creates
3346a @emph{pure executable} format.
3347
3348@kindex -z
3349@item -z
3350@kindex --imagic
3351@itemx --imagic
3352
3353Mark the output as @code{IMAGIC} (0411) in the @file{a.out} header to
3354indicate that when the output file is executed, the program text and
3355data areas will be loaded into separate address spaces using the split
3356instruction and data space feature of the memory management unit in
3357larger models of the PDP11. This doubles the address space available
3358to the program. The text segment is again pure, write-protected, and
3359shareable. The only difference in the output format between this
3360option and the others, besides the magic number, is that both the text
3361and data sections start at location 0. The @samp{-z} option selected
3362this format in 2.11BSD. This option creates a @emph{separate
3363executable} format.
3364
3365@kindex --no-omagic
3366@item --no-omagic
3367
3368Equivalent to @samp{--nmagic} for pdp11-aout.
3369
3370@end table
3371
3372@c man end
3373@end ifset
3374
252b5132
RH
3375@ifset UsesEnvVars
3376@node Environment
3377@section Environment Variables
3378
0285c67d
NC
3379@c man begin ENVIRONMENT
3380
560e09e9 3381You can change the behaviour of @command{ld} with the environment variables
36f63dca
NC
3382@ifclear SingleFormat
3383@code{GNUTARGET},
3384@end ifclear
3385@code{LDEMULATION} and @code{COLLECT_NO_DEMANGLE}.
252b5132 3386
36f63dca 3387@ifclear SingleFormat
252b5132
RH
3388@kindex GNUTARGET
3389@cindex default input format
3390@code{GNUTARGET} determines the input-file object format if you don't
3391use @samp{-b} (or its synonym @samp{--format}). Its value should be one
3392of the BFD names for an input format (@pxref{BFD}). If there is no
ff5dcc92 3393@code{GNUTARGET} in the environment, @command{ld} uses the natural format
252b5132
RH
3394of the target. If @code{GNUTARGET} is set to @code{default} then BFD
3395attempts to discover the input format by examining binary input files;
3396this method often succeeds, but there are potential ambiguities, since
3397there is no method of ensuring that the magic number used to specify
3398object-file formats is unique. However, the configuration procedure for
3399BFD on each system places the conventional format for that system first
3400in the search-list, so ambiguities are resolved in favor of convention.
36f63dca 3401@end ifclear
252b5132
RH
3402
3403@kindex LDEMULATION
3404@cindex default emulation
3405@cindex emulation, default
3406@code{LDEMULATION} determines the default emulation if you don't use the
3407@samp{-m} option. The emulation can affect various aspects of linker
3408behaviour, particularly the default linker script. You can list the
3409available emulations with the @samp{--verbose} or @samp{-V} options. If
3410the @samp{-m} option is not used, and the @code{LDEMULATION} environment
3411variable is not defined, the default emulation depends upon how the
3412linker was configured.
252b5132
RH
3413
3414@kindex COLLECT_NO_DEMANGLE
3415@cindex demangling, default
3416Normally, the linker will default to demangling symbols. However, if
3417@code{COLLECT_NO_DEMANGLE} is set in the environment, then it will
3418default to not demangling symbols. This environment variable is used in
3419a similar fashion by the @code{gcc} linker wrapper program. The default
3420may be overridden by the @samp{--demangle} and @samp{--no-demangle}
3421options.
3422
0285c67d
NC
3423@c man end
3424@end ifset
3425
252b5132
RH
3426@node Scripts
3427@chapter Linker Scripts
3428
3429@cindex scripts
3430@cindex linker scripts
3431@cindex command files
3432Every link is controlled by a @dfn{linker script}. This script is
3433written in the linker command language.
3434
3435The main purpose of the linker script is to describe how the sections in
3436the input files should be mapped into the output file, and to control
3437the memory layout of the output file. Most linker scripts do nothing
3438more than this. However, when necessary, the linker script can also
3439direct the linker to perform many other operations, using the commands
3440described below.
3441
3442The linker always uses a linker script. If you do not supply one
3443yourself, the linker will use a default script that is compiled into the
a05a5b64
TP
3444linker executable. You can use the @samp{--verbose} command-line option
3445to display the default linker script. Certain command-line options,
252b5132
RH
3446such as @samp{-r} or @samp{-N}, will affect the default linker script.
3447
3448You may supply your own linker script by using the @samp{-T} command
3449line option. When you do this, your linker script will replace the
3450default linker script.
3451
3452You may also use linker scripts implicitly by naming them as input files
3453to the linker, as though they were files to be linked. @xref{Implicit
3454Linker Scripts}.
3455
3456@menu
3457* Basic Script Concepts:: Basic Linker Script Concepts
3458* Script Format:: Linker Script Format
3459* Simple Example:: Simple Linker Script Example
3460* Simple Commands:: Simple Linker Script Commands
3461* Assignments:: Assigning Values to Symbols
3462* SECTIONS:: SECTIONS Command
3463* MEMORY:: MEMORY Command
3464* PHDRS:: PHDRS Command
3465* VERSION:: VERSION Command
3466* Expressions:: Expressions in Linker Scripts
3467* Implicit Linker Scripts:: Implicit Linker Scripts
3468@end menu
3469
3470@node Basic Script Concepts
3471@section Basic Linker Script Concepts
3472@cindex linker script concepts
3473We need to define some basic concepts and vocabulary in order to
3474describe the linker script language.
3475
3476The linker combines input files into a single output file. The output
3477file and each input file are in a special data format known as an
3478@dfn{object file format}. Each file is called an @dfn{object file}.
3479The output file is often called an @dfn{executable}, but for our
3480purposes we will also call it an object file. Each object file has,
3481among other things, a list of @dfn{sections}. We sometimes refer to a
3482section in an input file as an @dfn{input section}; similarly, a section
3483in the output file is an @dfn{output section}.
3484
3485Each section in an object file has a name and a size. Most sections
3486also have an associated block of data, known as the @dfn{section
56dd11f0 3487contents}. A section may be marked as @dfn{loadable}, which means that
252b5132
RH
3488the contents should be loaded into memory when the output file is run.
3489A section with no contents may be @dfn{allocatable}, which means that an
3490area in memory should be set aside, but nothing in particular should be
3491loaded there (in some cases this memory must be zeroed out). A section
3492which is neither loadable nor allocatable typically contains some sort
3493of debugging information.
3494
3495Every loadable or allocatable output section has two addresses. The
3496first is the @dfn{VMA}, or virtual memory address. This is the address
3497the section will have when the output file is run. The second is the
3498@dfn{LMA}, or load memory address. This is the address at which the
3499section will be loaded. In most cases the two addresses will be the
3500same. An example of when they might be different is when a data section
3501is loaded into ROM, and then copied into RAM when the program starts up
3502(this technique is often used to initialize global variables in a ROM
3503based system). In this case the ROM address would be the LMA, and the
3504RAM address would be the VMA.
3505
3506You can see the sections in an object file by using the @code{objdump}
3507program with the @samp{-h} option.
3508
3509Every object file also has a list of @dfn{symbols}, known as the
3510@dfn{symbol table}. A symbol may be defined or undefined. Each symbol
3511has a name, and each defined symbol has an address, among other
3512information. If you compile a C or C++ program into an object file, you
3513will get a defined symbol for every defined function and global or
3514static variable. Every undefined function or global variable which is
3515referenced in the input file will become an undefined symbol.
3516
3517You can see the symbols in an object file by using the @code{nm}
3518program, or by using the @code{objdump} program with the @samp{-t}
3519option.
3520
3521@node Script Format
3522@section Linker Script Format
3523@cindex linker script format
3524Linker scripts are text files.
3525
3526You write a linker script as a series of commands. Each command is
3527either a keyword, possibly followed by arguments, or an assignment to a
3528symbol. You may separate commands using semicolons. Whitespace is
3529generally ignored.
3530
3531Strings such as file or format names can normally be entered directly.
3532If the file name contains a character such as a comma which would
3533otherwise serve to separate file names, you may put the file name in
3534double quotes. There is no way to use a double quote character in a
3535file name.
3536
3537You may include comments in linker scripts just as in C, delimited by
3538@samp{/*} and @samp{*/}. As in C, comments are syntactically equivalent
3539to whitespace.
3540
3541@node Simple Example
3542@section Simple Linker Script Example
3543@cindex linker script example
3544@cindex example of linker script
3545Many linker scripts are fairly simple.
3546
3547The simplest possible linker script has just one command:
3548@samp{SECTIONS}. You use the @samp{SECTIONS} command to describe the
3549memory layout of the output file.
3550
3551The @samp{SECTIONS} command is a powerful command. Here we will
3552describe a simple use of it. Let's assume your program consists only of
3553code, initialized data, and uninitialized data. These will be in the
3554@samp{.text}, @samp{.data}, and @samp{.bss} sections, respectively.
3555Let's assume further that these are the only sections which appear in
3556your input files.
3557
3558For this example, let's say that the code should be loaded at address
35590x10000, and that the data should start at address 0x8000000. Here is a
3560linker script which will do that:
3561@smallexample
3562SECTIONS
3563@{
3564 . = 0x10000;
3565 .text : @{ *(.text) @}
3566 . = 0x8000000;
3567 .data : @{ *(.data) @}
3568 .bss : @{ *(.bss) @}
3569@}
3570@end smallexample
3571
3572You write the @samp{SECTIONS} command as the keyword @samp{SECTIONS},
3573followed by a series of symbol assignments and output section
3574descriptions enclosed in curly braces.
3575
252b5132
RH
3576The first line inside the @samp{SECTIONS} command of the above example
3577sets the value of the special symbol @samp{.}, which is the location
3578counter. If you do not specify the address of an output section in some
3579other way (other ways are described later), the address is set from the
3580current value of the location counter. The location counter is then
3581incremented by the size of the output section. At the start of the
3582@samp{SECTIONS} command, the location counter has the value @samp{0}.
3583
3584The second line defines an output section, @samp{.text}. The colon is
3585required syntax which may be ignored for now. Within the curly braces
3586after the output section name, you list the names of the input sections
3587which should be placed into this output section. The @samp{*} is a
3588wildcard which matches any file name. The expression @samp{*(.text)}
3589means all @samp{.text} input sections in all input files.
3590
3591Since the location counter is @samp{0x10000} when the output section
3592@samp{.text} is defined, the linker will set the address of the
3593@samp{.text} section in the output file to be @samp{0x10000}.
3594
3595The remaining lines define the @samp{.data} and @samp{.bss} sections in
3596the output file. The linker will place the @samp{.data} output section
3597at address @samp{0x8000000}. After the linker places the @samp{.data}
3598output section, the value of the location counter will be
3599@samp{0x8000000} plus the size of the @samp{.data} output section. The
3600effect is that the linker will place the @samp{.bss} output section
58434bc1 3601immediately after the @samp{.data} output section in memory.
252b5132
RH
3602
3603The linker will ensure that each output section has the required
3604alignment, by increasing the location counter if necessary. In this
3605example, the specified addresses for the @samp{.text} and @samp{.data}
3606sections will probably satisfy any alignment constraints, but the linker
3607may have to create a small gap between the @samp{.data} and @samp{.bss}
3608sections.
3609
3610That's it! That's a simple and complete linker script.
3611
3612@node Simple Commands
3613@section Simple Linker Script Commands
3614@cindex linker script simple commands
3615In this section we describe the simple linker script commands.
3616
3617@menu
3618* Entry Point:: Setting the entry point
3619* File Commands:: Commands dealing with files
3620@ifclear SingleFormat
3621* Format Commands:: Commands dealing with object file formats
3622@end ifclear
3623
4a93e180 3624* REGION_ALIAS:: Assign alias names to memory regions
252b5132
RH
3625* Miscellaneous Commands:: Other linker script commands
3626@end menu
3627
3628@node Entry Point
36f63dca 3629@subsection Setting the Entry Point
252b5132
RH
3630@kindex ENTRY(@var{symbol})
3631@cindex start of execution
3632@cindex first instruction
3633@cindex entry point
3634The first instruction to execute in a program is called the @dfn{entry
3635point}. You can use the @code{ENTRY} linker script command to set the
3636entry point. The argument is a symbol name:
3637@smallexample
3638ENTRY(@var{symbol})
3639@end smallexample
3640
3641There are several ways to set the entry point. The linker will set the
3642entry point by trying each of the following methods in order, and
3643stopping when one of them succeeds:
3644@itemize @bullet
a1ab1d2a 3645@item
252b5132 3646the @samp{-e} @var{entry} command-line option;
a1ab1d2a 3647@item
252b5132 3648the @code{ENTRY(@var{symbol})} command in a linker script;
a1ab1d2a 3649@item
a094d01f 3650the value of a target-specific symbol, if it is defined; For many
8a758655 3651targets this is @code{start}, but PE- and BeOS-based systems for example
3ab904c4 3652check a list of possible entry symbols, matching the first one found.
a1ab1d2a 3653@item
252b5132 3654the address of the first byte of the @samp{.text} section, if present;
a1ab1d2a 3655@item
252b5132
RH
3656The address @code{0}.
3657@end itemize
3658
3659@node File Commands
36f63dca 3660@subsection Commands Dealing with Files
252b5132
RH
3661@cindex linker script file commands
3662Several linker script commands deal with files.
3663
3664@table @code
3665@item INCLUDE @var{filename}
3666@kindex INCLUDE @var{filename}
3667@cindex including a linker script
3668Include the linker script @var{filename} at this point. The file will
3669be searched for in the current directory, and in any directory specified
ff5dcc92 3670with the @option{-L} option. You can nest calls to @code{INCLUDE} up to
252b5132
RH
367110 levels deep.
3672
4006703d
NS
3673You can place @code{INCLUDE} directives at the top level, in @code{MEMORY} or
3674@code{SECTIONS} commands, or in output section descriptions.
3675
252b5132
RH
3676@item INPUT(@var{file}, @var{file}, @dots{})
3677@itemx INPUT(@var{file} @var{file} @dots{})
3678@kindex INPUT(@var{files})
3679@cindex input files in linker scripts
3680@cindex input object files in linker scripts
3681@cindex linker script input object files
3682The @code{INPUT} command directs the linker to include the named files
3683in the link, as though they were named on the command line.
3684
3685For example, if you always want to include @file{subr.o} any time you do
3686a link, but you can't be bothered to put it on every link command line,
3687then you can put @samp{INPUT (subr.o)} in your linker script.
3688
3689In fact, if you like, you can list all of your input files in the linker
3690script, and then invoke the linker with nothing but a @samp{-T} option.
3691
e3f2db7f
AO
3692In case a @dfn{sysroot prefix} is configured, and the filename starts
3693with the @samp{/} character, and the script being processed was
3694located inside the @dfn{sysroot prefix}, the filename will be looked
16171946
FS
3695for in the @dfn{sysroot prefix}. The @dfn{sysroot prefix} can also be forced by specifying
3696@code{=} as the first character in the filename path, or prefixing the
3697filename path with @code{$SYSROOT}. See also the description of
3698@samp{-L} in @ref{Options,,Command-line Options}.
3699
3700If a @dfn{sysroot prefix} is not used then the linker will try to open
3701the file in the directory containing the linker script. If it is not
3702found the linker will then search the current directory. If it is still
3703not found the linker will search through the archive library search
3704path.
252b5132 3705
ff5dcc92 3706If you use @samp{INPUT (-l@var{file})}, @command{ld} will transform the
a05a5b64 3707name to @code{lib@var{file}.a}, as with the command-line argument
252b5132
RH
3708@samp{-l}.
3709
3710When you use the @code{INPUT} command in an implicit linker script, the
3711files will be included in the link at the point at which the linker
3712script file is included. This can affect archive searching.
3713
3714@item GROUP(@var{file}, @var{file}, @dots{})
3715@itemx GROUP(@var{file} @var{file} @dots{})
3716@kindex GROUP(@var{files})
3717@cindex grouping input files
3718The @code{GROUP} command is like @code{INPUT}, except that the named
3719files should all be archives, and they are searched repeatedly until no
3720new undefined references are created. See the description of @samp{-(}
a05a5b64 3721in @ref{Options,,Command-line Options}.
252b5132 3722
b717d30e
JJ
3723@item AS_NEEDED(@var{file}, @var{file}, @dots{})
3724@itemx AS_NEEDED(@var{file} @var{file} @dots{})
3725@kindex AS_NEEDED(@var{files})
3726This construct can appear only inside of the @code{INPUT} or @code{GROUP}
3727commands, among other filenames. The files listed will be handled
3728as if they appear directly in the @code{INPUT} or @code{GROUP} commands,
3729with the exception of ELF shared libraries, that will be added only
3730when they are actually needed. This construct essentially enables
3731@option{--as-needed} option for all the files listed inside of it
3732and restores previous @option{--as-needed} resp. @option{--no-as-needed}
3733setting afterwards.
3734
252b5132
RH
3735@item OUTPUT(@var{filename})
3736@kindex OUTPUT(@var{filename})
b45619c0 3737@cindex output file name in linker script
252b5132
RH
3738The @code{OUTPUT} command names the output file. Using
3739@code{OUTPUT(@var{filename})} in the linker script is exactly like using
3740@samp{-o @var{filename}} on the command line (@pxref{Options,,Command
a05a5b64 3741Line Options}). If both are used, the command-line option takes
252b5132
RH
3742precedence.
3743
3744You can use the @code{OUTPUT} command to define a default name for the
3745output file other than the usual default of @file{a.out}.
3746
3747@item SEARCH_DIR(@var{path})
3748@kindex SEARCH_DIR(@var{path})
3749@cindex library search path in linker script
3750@cindex archive search path in linker script
3751@cindex search path in linker script
3752The @code{SEARCH_DIR} command adds @var{path} to the list of paths where
ff5dcc92 3753@command{ld} looks for archive libraries. Using
252b5132 3754@code{SEARCH_DIR(@var{path})} is exactly like using @samp{-L @var{path}}
a05a5b64 3755on the command line (@pxref{Options,,Command-line Options}). If both
252b5132 3756are used, then the linker will search both paths. Paths specified using
a05a5b64 3757the command-line option are searched first.
252b5132
RH
3758
3759@item STARTUP(@var{filename})
3760@kindex STARTUP(@var{filename})
3761@cindex first input file
3762The @code{STARTUP} command is just like the @code{INPUT} command, except
3763that @var{filename} will become the first input file to be linked, as
3764though it were specified first on the command line. This may be useful
3765when using a system in which the entry point is always the start of the
3766first file.
3767@end table
3768
3769@ifclear SingleFormat
3770@node Format Commands
36f63dca 3771@subsection Commands Dealing with Object File Formats
252b5132
RH
3772A couple of linker script commands deal with object file formats.
3773
3774@table @code
3775@item OUTPUT_FORMAT(@var{bfdname})
3776@itemx OUTPUT_FORMAT(@var{default}, @var{big}, @var{little})
3777@kindex OUTPUT_FORMAT(@var{bfdname})
3778@cindex output file format in linker script
3779The @code{OUTPUT_FORMAT} command names the BFD format to use for the
3780output file (@pxref{BFD}). Using @code{OUTPUT_FORMAT(@var{bfdname})} is
024531e2 3781exactly like using @samp{--oformat @var{bfdname}} on the command line
a05a5b64 3782(@pxref{Options,,Command-line Options}). If both are used, the command
252b5132
RH
3783line option takes precedence.
3784
3785You can use @code{OUTPUT_FORMAT} with three arguments to use different
a05a5b64 3786formats based on the @samp{-EB} and @samp{-EL} command-line options.
252b5132
RH
3787This permits the linker script to set the output format based on the
3788desired endianness.
3789
3790If neither @samp{-EB} nor @samp{-EL} are used, then the output format
3791will be the first argument, @var{default}. If @samp{-EB} is used, the
3792output format will be the second argument, @var{big}. If @samp{-EL} is
3793used, the output format will be the third argument, @var{little}.
3794
3795For example, the default linker script for the MIPS ELF target uses this
3796command:
3797@smallexample
3798OUTPUT_FORMAT(elf32-bigmips, elf32-bigmips, elf32-littlemips)
3799@end smallexample
3800This says that the default format for the output file is
a05a5b64 3801@samp{elf32-bigmips}, but if the user uses the @samp{-EL} command-line
252b5132
RH
3802option, the output file will be created in the @samp{elf32-littlemips}
3803format.
3804
3805@item TARGET(@var{bfdname})
3806@kindex TARGET(@var{bfdname})
3807@cindex input file format in linker script
3808The @code{TARGET} command names the BFD format to use when reading input
3809files. It affects subsequent @code{INPUT} and @code{GROUP} commands.
3810This command is like using @samp{-b @var{bfdname}} on the command line
a05a5b64 3811(@pxref{Options,,Command-line Options}). If the @code{TARGET} command
252b5132
RH
3812is used but @code{OUTPUT_FORMAT} is not, then the last @code{TARGET}
3813command is also used to set the format for the output file. @xref{BFD}.
3814@end table
3815@end ifclear
3816
4a93e180
NC
3817@node REGION_ALIAS
3818@subsection Assign alias names to memory regions
3819@kindex REGION_ALIAS(@var{alias}, @var{region})
3820@cindex region alias
3821@cindex region names
3822
3823Alias names can be added to existing memory regions created with the
3824@ref{MEMORY} command. Each name corresponds to at most one memory region.
3825
3826@smallexample
3827REGION_ALIAS(@var{alias}, @var{region})
3828@end smallexample
3829
3830The @code{REGION_ALIAS} function creates an alias name @var{alias} for the
3831memory region @var{region}. This allows a flexible mapping of output sections
3832to memory regions. An example follows.
3833
3834Suppose we have an application for embedded systems which come with various
3835memory storage devices. All have a general purpose, volatile memory @code{RAM}
3836that allows code execution or data storage. Some may have a read-only,
3837non-volatile memory @code{ROM} that allows code execution and read-only data
3838access. The last variant is a read-only, non-volatile memory @code{ROM2} with
3839read-only data access and no code execution capability. We have four output
3840sections:
3841
3842@itemize @bullet
3843@item
3844@code{.text} program code;
3845@item
3846@code{.rodata} read-only data;
3847@item
3848@code{.data} read-write initialized data;
3849@item
3850@code{.bss} read-write zero initialized data.
3851@end itemize
3852
3853The goal is to provide a linker command file that contains a system independent
3854part defining the output sections and a system dependent part mapping the
3855output sections to the memory regions available on the system. Our embedded
3856systems come with three different memory setups @code{A}, @code{B} and
3857@code{C}:
3858@multitable @columnfractions .25 .25 .25 .25
3859@item Section @tab Variant A @tab Variant B @tab Variant C
9d5777a3 3860@item .text @tab RAM @tab ROM @tab ROM
4a93e180
NC
3861@item .rodata @tab RAM @tab ROM @tab ROM2
3862@item .data @tab RAM @tab RAM/ROM @tab RAM/ROM2
3863@item .bss @tab RAM @tab RAM @tab RAM
3864@end multitable
3865The notation @code{RAM/ROM} or @code{RAM/ROM2} means that this section is
3866loaded into region @code{ROM} or @code{ROM2} respectively. Please note that
3867the load address of the @code{.data} section starts in all three variants at
3868the end of the @code{.rodata} section.
3869
3870The base linker script that deals with the output sections follows. It
3871includes the system dependent @code{linkcmds.memory} file that describes the
3872memory layout:
3873@smallexample
3874INCLUDE linkcmds.memory
3875
3876SECTIONS
3877 @{
3878 .text :
3879 @{
3880 *(.text)
3881 @} > REGION_TEXT
3882 .rodata :
3883 @{
3884 *(.rodata)
3885 rodata_end = .;
3886 @} > REGION_RODATA
3887 .data : AT (rodata_end)
3888 @{
3889 data_start = .;
3890 *(.data)
3891 @} > REGION_DATA
3892 data_size = SIZEOF(.data);
3893 data_load_start = LOADADDR(.data);
3894 .bss :
3895 @{
3896 *(.bss)
3897 @} > REGION_BSS
3898 @}
3899@end smallexample
3900
3901Now we need three different @code{linkcmds.memory} files to define memory
3902regions and alias names. The content of @code{linkcmds.memory} for the three
3903variants @code{A}, @code{B} and @code{C}:
3904@table @code
3905@item A
3906Here everything goes into the @code{RAM}.
3907@smallexample
3908MEMORY
3909 @{
3910 RAM : ORIGIN = 0, LENGTH = 4M
3911 @}
3912
3913REGION_ALIAS("REGION_TEXT", RAM);
3914REGION_ALIAS("REGION_RODATA", RAM);
3915REGION_ALIAS("REGION_DATA", RAM);
3916REGION_ALIAS("REGION_BSS", RAM);
3917@end smallexample
3918@item B
3919Program code and read-only data go into the @code{ROM}. Read-write data goes
3920into the @code{RAM}. An image of the initialized data is loaded into the
3921@code{ROM} and will be copied during system start into the @code{RAM}.
3922@smallexample
3923MEMORY
3924 @{
3925 ROM : ORIGIN = 0, LENGTH = 3M
3926 RAM : ORIGIN = 0x10000000, LENGTH = 1M
3927 @}
3928
3929REGION_ALIAS("REGION_TEXT", ROM);
3930REGION_ALIAS("REGION_RODATA", ROM);
3931REGION_ALIAS("REGION_DATA", RAM);
3932REGION_ALIAS("REGION_BSS", RAM);
3933@end smallexample
3934@item C
3935Program code goes into the @code{ROM}. Read-only data goes into the
3936@code{ROM2}. Read-write data goes into the @code{RAM}. An image of the
3937initialized data is loaded into the @code{ROM2} and will be copied during
3938system start into the @code{RAM}.
3939@smallexample
3940MEMORY
3941 @{
3942 ROM : ORIGIN = 0, LENGTH = 2M
3943 ROM2 : ORIGIN = 0x10000000, LENGTH = 1M
3944 RAM : ORIGIN = 0x20000000, LENGTH = 1M
3945 @}
3946
3947REGION_ALIAS("REGION_TEXT", ROM);
3948REGION_ALIAS("REGION_RODATA", ROM2);
3949REGION_ALIAS("REGION_DATA", RAM);
3950REGION_ALIAS("REGION_BSS", RAM);
3951@end smallexample
3952@end table
3953
3954It is possible to write a common system initialization routine to copy the
3955@code{.data} section from @code{ROM} or @code{ROM2} into the @code{RAM} if
3956necessary:
3957@smallexample
3958#include <string.h>
3959
3960extern char data_start [];
3961extern char data_size [];
3962extern char data_load_start [];
3963
3964void copy_data(void)
3965@{
3966 if (data_start != data_load_start)
3967 @{
3968 memcpy(data_start, data_load_start, (size_t) data_size);
3969 @}
3970@}
3971@end smallexample
3972
252b5132 3973@node Miscellaneous Commands
36f63dca 3974@subsection Other Linker Script Commands
252b5132
RH
3975There are a few other linker scripts commands.
3976
3977@table @code
3978@item ASSERT(@var{exp}, @var{message})
3979@kindex ASSERT
3980@cindex assertion in linker script
3981Ensure that @var{exp} is non-zero. If it is zero, then exit the linker
3982with an error code, and print @var{message}.
3983
fd1c4238
NC
3984Note that assertions are checked before the final stages of linking
3985take place. This means that expressions involving symbols PROVIDEd
3986inside section definitions will fail if the user has not set values
3987for those symbols. The only exception to this rule is PROVIDEd
3988symbols that just reference dot. Thus an assertion like this:
3989
3990@smallexample
3991 .stack :
3992 @{
3993 PROVIDE (__stack = .);
3994 PROVIDE (__stack_size = 0x100);
3995 ASSERT ((__stack > (_end + __stack_size)), "Error: No room left for the stack");
3996 @}
3997@end smallexample
3998
3999will fail if @code{__stack_size} is not defined elsewhere. Symbols
4000PROVIDEd outside of section definitions are evaluated earlier, so they
4001can be used inside ASSERTions. Thus:
4002
4003@smallexample
4004 PROVIDE (__stack_size = 0x100);
4005 .stack :
4006 @{
4007 PROVIDE (__stack = .);
4008 ASSERT ((__stack > (_end + __stack_size)), "Error: No room left for the stack");
4009 @}
4010@end smallexample
4011
4012will work.
4013
252b5132
RH
4014@item EXTERN(@var{symbol} @var{symbol} @dots{})
4015@kindex EXTERN
4016@cindex undefined symbol in linker script
4017Force @var{symbol} to be entered in the output file as an undefined
4018symbol. Doing this may, for example, trigger linking of additional
4019modules from standard libraries. You may list several @var{symbol}s for
4020each @code{EXTERN}, and you may use @code{EXTERN} multiple times. This
4021command has the same effect as the @samp{-u} command-line option.
4022
4023@item FORCE_COMMON_ALLOCATION
4024@kindex FORCE_COMMON_ALLOCATION
4025@cindex common allocation in linker script
4026This command has the same effect as the @samp{-d} command-line option:
ff5dcc92 4027to make @command{ld} assign space to common symbols even if a relocatable
252b5132
RH
4028output file is specified (@samp{-r}).
4029
4818e05f
AM
4030@item INHIBIT_COMMON_ALLOCATION
4031@kindex INHIBIT_COMMON_ALLOCATION
4032@cindex common allocation in linker script
4033This command has the same effect as the @samp{--no-define-common}
4034command-line option: to make @code{ld} omit the assignment of addresses
4035to common symbols even for a non-relocatable output file.
4036
7bdf4127
AB
4037@item FORCE_GROUP_ALLOCATION
4038@kindex FORCE_GROUP_ALLOCATION
4039@cindex group allocation in linker script
4040@cindex section groups
4041@cindex COMDAT
4042This command has the same effect as the
4043@samp{--force-group-allocation} command-line option: to make
4044@command{ld} place section group members like normal input sections,
4045and to delete the section groups even if a relocatable output file is
4046specified (@samp{-r}).
4047
53d25da6
AM
4048@item INSERT [ AFTER | BEFORE ] @var{output_section}
4049@kindex INSERT
4050@cindex insert user script into default script
4051This command is typically used in a script specified by @samp{-T} to
4052augment the default @code{SECTIONS} with, for example, overlays. It
4053inserts all prior linker script statements after (or before)
4054@var{output_section}, and also causes @samp{-T} to not override the
4055default linker script. The exact insertion point is as for orphan
4056sections. @xref{Location Counter}. The insertion happens after the
4057linker has mapped input sections to output sections. Prior to the
4058insertion, since @samp{-T} scripts are parsed before the default
4059linker script, statements in the @samp{-T} script occur before the
4060default linker script statements in the internal linker representation
4061of the script. In particular, input section assignments will be made
4062to @samp{-T} output sections before those in the default script. Here
4063is an example of how a @samp{-T} script using @code{INSERT} might look:
4064
4065@smallexample
4066SECTIONS
4067@{
4068 OVERLAY :
4069 @{
4070 .ov1 @{ ov1*(.text) @}
4071 .ov2 @{ ov2*(.text) @}
4072 @}
4073@}
4074INSERT AFTER .text;
4075@end smallexample
4076
252b5132
RH
4077@item NOCROSSREFS(@var{section} @var{section} @dots{})
4078@kindex NOCROSSREFS(@var{sections})
4079@cindex cross references
ff5dcc92 4080This command may be used to tell @command{ld} to issue an error about any
252b5132
RH
4081references among certain output sections.
4082
4083In certain types of programs, particularly on embedded systems when
4084using overlays, when one section is loaded into memory, another section
4085will not be. Any direct references between the two sections would be
4086errors. For example, it would be an error if code in one section called
4087a function defined in the other section.
4088
4089The @code{NOCROSSREFS} command takes a list of output section names. If
ff5dcc92 4090@command{ld} detects any cross references between the sections, it reports
252b5132
RH
4091an error and returns a non-zero exit status. Note that the
4092@code{NOCROSSREFS} command uses output section names, not input section
4093names.
4094
cdf96953
MF
4095@item NOCROSSREFS_TO(@var{tosection} @var{fromsection} @dots{})
4096@kindex NOCROSSREFS_TO(@var{tosection} @var{fromsections})
4097@cindex cross references
4098This command may be used to tell @command{ld} to issue an error about any
4099references to one section from a list of other sections.
4100
4101The @code{NOCROSSREFS} command is useful when ensuring that two or more
4102output sections are entirely independent but there are situations where
4103a one-way dependency is needed. For example, in a multi-core application
4104there may be shared code that can be called from each core but for safety
4105must never call back.
4106
4107The @code{NOCROSSREFS_TO} command takes a list of output section names.
4108The first section can not be referenced from any of the other sections.
4109If @command{ld} detects any references to the first section from any of
4110the other sections, it reports an error and returns a non-zero exit
4111status. Note that the @code{NOCROSSREFS_TO} command uses output section
4112names, not input section names.
4113
252b5132
RH
4114@ifclear SingleFormat
4115@item OUTPUT_ARCH(@var{bfdarch})
4116@kindex OUTPUT_ARCH(@var{bfdarch})
4117@cindex machine architecture
4118@cindex architecture
4119Specify a particular output machine architecture. The argument is one
4120of the names used by the BFD library (@pxref{BFD}). You can see the
4121architecture of an object file by using the @code{objdump} program with
4122the @samp{-f} option.
4123@end ifclear
01554a74
AM
4124
4125@item LD_FEATURE(@var{string})
4126@kindex LD_FEATURE(@var{string})
4127This command may be used to modify @command{ld} behavior. If
4128@var{string} is @code{"SANE_EXPR"} then absolute symbols and numbers
4129in a script are simply treated as numbers everywhere.
4130@xref{Expression Section}.
252b5132
RH
4131@end table
4132
4133@node Assignments
4134@section Assigning Values to Symbols
4135@cindex assignment in scripts
4136@cindex symbol definition, scripts
4137@cindex variables, defining
4138You may assign a value to a symbol in a linker script. This will define
73ae6183 4139the symbol and place it into the symbol table with a global scope.
252b5132
RH
4140
4141@menu
4142* Simple Assignments:: Simple Assignments
eb8476a6 4143* HIDDEN:: HIDDEN
252b5132 4144* PROVIDE:: PROVIDE
7af8e998 4145* PROVIDE_HIDDEN:: PROVIDE_HIDDEN
73ae6183 4146* Source Code Reference:: How to use a linker script defined symbol in source code
252b5132
RH
4147@end menu
4148
4149@node Simple Assignments
4150@subsection Simple Assignments
4151
4152You may assign to a symbol using any of the C assignment operators:
4153
4154@table @code
4155@item @var{symbol} = @var{expression} ;
4156@itemx @var{symbol} += @var{expression} ;
4157@itemx @var{symbol} -= @var{expression} ;
4158@itemx @var{symbol} *= @var{expression} ;
4159@itemx @var{symbol} /= @var{expression} ;
4160@itemx @var{symbol} <<= @var{expression} ;
4161@itemx @var{symbol} >>= @var{expression} ;
4162@itemx @var{symbol} &= @var{expression} ;
4163@itemx @var{symbol} |= @var{expression} ;
4164@end table
4165
4166The first case will define @var{symbol} to the value of
4167@var{expression}. In the other cases, @var{symbol} must already be
4168defined, and the value will be adjusted accordingly.
4169
4170The special symbol name @samp{.} indicates the location counter. You
b5666f2f 4171may only use this within a @code{SECTIONS} command. @xref{Location Counter}.
252b5132
RH
4172
4173The semicolon after @var{expression} is required.
4174
4175Expressions are defined below; see @ref{Expressions}.
4176
4177You may write symbol assignments as commands in their own right, or as
4178statements within a @code{SECTIONS} command, or as part of an output
4179section description in a @code{SECTIONS} command.
4180
4181The section of the symbol will be set from the section of the
4182expression; for more information, see @ref{Expression Section}.
4183
4184Here is an example showing the three different places that symbol
4185assignments may be used:
4186
4187@smallexample
4188floating_point = 0;
4189SECTIONS
4190@{
4191 .text :
4192 @{
4193 *(.text)
4194 _etext = .;
4195 @}
156e34dd 4196 _bdata = (. + 3) & ~ 3;
252b5132
RH
4197 .data : @{ *(.data) @}
4198@}
4199@end smallexample
4200@noindent
4201In this example, the symbol @samp{floating_point} will be defined as
4202zero. The symbol @samp{_etext} will be defined as the address following
4203the last @samp{.text} input section. The symbol @samp{_bdata} will be
4204defined as the address following the @samp{.text} output section aligned
4205upward to a 4 byte boundary.
4206
eb8476a6
MR
4207@node HIDDEN
4208@subsection HIDDEN
4209@cindex HIDDEN
4210For ELF targeted ports, define a symbol that will be hidden and won't be
4211exported. The syntax is @code{HIDDEN(@var{symbol} = @var{expression})}.
4212
4213Here is the example from @ref{Simple Assignments}, rewritten to use
4214@code{HIDDEN}:
4215
4216@smallexample
4217HIDDEN(floating_point = 0);
4218SECTIONS
4219@{
4220 .text :
4221 @{
4222 *(.text)
4223 HIDDEN(_etext = .);
4224 @}
4225 HIDDEN(_bdata = (. + 3) & ~ 3);
4226 .data : @{ *(.data) @}
4227@}
4228@end smallexample
4229@noindent
4230In this case none of the three symbols will be visible outside this module.
4231
252b5132
RH
4232@node PROVIDE
4233@subsection PROVIDE
4234@cindex PROVIDE
4235In some cases, it is desirable for a linker script to define a symbol
4236only if it is referenced and is not defined by any object included in
4237the link. For example, traditional linkers defined the symbol
4238@samp{etext}. However, ANSI C requires that the user be able to use
4239@samp{etext} as a function name without encountering an error. The
4240@code{PROVIDE} keyword may be used to define a symbol, such as
4241@samp{etext}, only if it is referenced but not defined. The syntax is
4242@code{PROVIDE(@var{symbol} = @var{expression})}.
4243
4244Here is an example of using @code{PROVIDE} to define @samp{etext}:
4245@smallexample
4246SECTIONS
4247@{
4248 .text :
4249 @{
4250 *(.text)
4251 _etext = .;
4252 PROVIDE(etext = .);
4253 @}
4254@}
4255@end smallexample
4256
4257In this example, if the program defines @samp{_etext} (with a leading
4258underscore), the linker will give a multiple definition error. If, on
4259the other hand, the program defines @samp{etext} (with no leading
4260underscore), the linker will silently use the definition in the program.
4261If the program references @samp{etext} but does not define it, the
4262linker will use the definition in the linker script.
4263
b0daac83
NC
4264Note - the @code{PROVIDE} directive considers a common symbol to be
4265defined, even though such a symbol could be combined with the symbol
4266that the @code{PROVIDE} would create. This is particularly important
4267when considering constructor and destructor list symbols such as
4268@samp{__CTOR_LIST__} as these are often defined as common symbols.
4269
7af8e998
L
4270@node PROVIDE_HIDDEN
4271@subsection PROVIDE_HIDDEN
4272@cindex PROVIDE_HIDDEN
4273Similar to @code{PROVIDE}. For ELF targeted ports, the symbol will be
4274hidden and won't be exported.
4275
73ae6183
NC
4276@node Source Code Reference
4277@subsection Source Code Reference
4278
4279Accessing a linker script defined variable from source code is not
4280intuitive. In particular a linker script symbol is not equivalent to
4281a variable declaration in a high level language, it is instead a
4282symbol that does not have a value.
4283
4284Before going further, it is important to note that compilers often
4285transform names in the source code into different names when they are
4286stored in the symbol table. For example, Fortran compilers commonly
4287prepend or append an underscore, and C++ performs extensive @samp{name
4288mangling}. Therefore there might be a discrepancy between the name
4289of a variable as it is used in source code and the name of the same
4290variable as it is defined in a linker script. For example in C a
4291linker script variable might be referred to as:
4292
4293@smallexample
4294 extern int foo;
4295@end smallexample
4296
4297But in the linker script it might be defined as:
4298
4299@smallexample
4300 _foo = 1000;
4301@end smallexample
4302
4303In the remaining examples however it is assumed that no name
4304transformation has taken place.
4305
4306When a symbol is declared in a high level language such as C, two
4307things happen. The first is that the compiler reserves enough space
4308in the program's memory to hold the @emph{value} of the symbol. The
4309second is that the compiler creates an entry in the program's symbol
4310table which holds the symbol's @emph{address}. ie the symbol table
4311contains the address of the block of memory holding the symbol's
4312value. So for example the following C declaration, at file scope:
4313
4314@smallexample
4315 int foo = 1000;
4316@end smallexample
4317
10bf6894 4318creates an entry called @samp{foo} in the symbol table. This entry
73ae6183
NC
4319holds the address of an @samp{int} sized block of memory where the
4320number 1000 is initially stored.
4321
4322When a program references a symbol the compiler generates code that
4323first accesses the symbol table to find the address of the symbol's
4324memory block and then code to read the value from that memory block.
4325So:
4326
4327@smallexample
4328 foo = 1;
4329@end smallexample
4330
4331looks up the symbol @samp{foo} in the symbol table, gets the address
4332associated with this symbol and then writes the value 1 into that
4333address. Whereas:
4334
4335@smallexample
4336 int * a = & foo;
4337@end smallexample
4338
10bf6894 4339looks up the symbol @samp{foo} in the symbol table, gets its address
73ae6183
NC
4340and then copies this address into the block of memory associated with
4341the variable @samp{a}.
4342
4343Linker scripts symbol declarations, by contrast, create an entry in
4344the symbol table but do not assign any memory to them. Thus they are
4345an address without a value. So for example the linker script definition:
4346
4347@smallexample
4348 foo = 1000;
4349@end smallexample
4350
4351creates an entry in the symbol table called @samp{foo} which holds
4352the address of memory location 1000, but nothing special is stored at
4353address 1000. This means that you cannot access the @emph{value} of a
4354linker script defined symbol - it has no value - all you can do is
4355access the @emph{address} of a linker script defined symbol.
4356
4357Hence when you are using a linker script defined symbol in source code
4358you should always take the address of the symbol, and never attempt to
4359use its value. For example suppose you want to copy the contents of a
4360section of memory called .ROM into a section called .FLASH and the
4361linker script contains these declarations:
4362
4363@smallexample
4364@group
4365 start_of_ROM = .ROM;
a5e406b5 4366 end_of_ROM = .ROM + sizeof (.ROM);
73ae6183
NC
4367 start_of_FLASH = .FLASH;
4368@end group
4369@end smallexample
4370
4371Then the C source code to perform the copy would be:
4372
4373@smallexample
4374@group
4375 extern char start_of_ROM, end_of_ROM, start_of_FLASH;
c0065db7 4376
73ae6183
NC
4377 memcpy (& start_of_FLASH, & start_of_ROM, & end_of_ROM - & start_of_ROM);
4378@end group
4379@end smallexample
4380
4381Note the use of the @samp{&} operators. These are correct.
5707d2ad
NC
4382Alternatively the symbols can be treated as the names of vectors or
4383arrays and then the code will again work as expected:
4384
4385@smallexample
4386@group
4387 extern char start_of_ROM[], end_of_ROM[], start_of_FLASH[];
4388
4389 memcpy (start_of_FLASH, start_of_ROM, end_of_ROM - start_of_ROM);
4390@end group
4391@end smallexample
4392
4393Note how using this method does not require the use of @samp{&}
4394operators.
73ae6183 4395
252b5132 4396@node SECTIONS
36f63dca 4397@section SECTIONS Command
252b5132
RH
4398@kindex SECTIONS
4399The @code{SECTIONS} command tells the linker how to map input sections
4400into output sections, and how to place the output sections in memory.
4401
4402The format of the @code{SECTIONS} command is:
4403@smallexample
4404SECTIONS
4405@{
4406 @var{sections-command}
4407 @var{sections-command}
4408 @dots{}
4409@}
4410@end smallexample
4411
4412Each @var{sections-command} may of be one of the following:
4413
4414@itemize @bullet
4415@item
4416an @code{ENTRY} command (@pxref{Entry Point,,Entry command})
4417@item
4418a symbol assignment (@pxref{Assignments})
4419@item
4420an output section description
4421@item
4422an overlay description
4423@end itemize
4424
4425The @code{ENTRY} command and symbol assignments are permitted inside the
4426@code{SECTIONS} command for convenience in using the location counter in
4427those commands. This can also make the linker script easier to
4428understand because you can use those commands at meaningful points in
4429the layout of the output file.
4430
4431Output section descriptions and overlay descriptions are described
4432below.
4433
4434If you do not use a @code{SECTIONS} command in your linker script, the
4435linker will place each input section into an identically named output
4436section in the order that the sections are first encountered in the
4437input files. If all input sections are present in the first file, for
4438example, the order of sections in the output file will match the order
4439in the first input file. The first section will be at address zero.
4440
4441@menu
4442* Output Section Description:: Output section description
4443* Output Section Name:: Output section name
4444* Output Section Address:: Output section address
4445* Input Section:: Input section description
4446* Output Section Data:: Output section data
4447* Output Section Keywords:: Output section keywords
4448* Output Section Discarding:: Output section discarding
4449* Output Section Attributes:: Output section attributes
4450* Overlay Description:: Overlay description
4451@end menu
4452
4453@node Output Section Description
36f63dca 4454@subsection Output Section Description
252b5132
RH
4455The full description of an output section looks like this:
4456@smallexample
a1ab1d2a 4457@group
7e7d5768 4458@var{section} [@var{address}] [(@var{type})] :
0c71d759 4459 [AT(@var{lma})]
1eec346e 4460 [ALIGN(@var{section_align}) | ALIGN_WITH_INPUT]
0c71d759
NC
4461 [SUBALIGN(@var{subsection_align})]
4462 [@var{constraint}]
252b5132
RH
4463 @{
4464 @var{output-section-command}
4465 @var{output-section-command}
4466 @dots{}
abc9061b 4467 @} [>@var{region}] [AT>@var{lma_region}] [:@var{phdr} :@var{phdr} @dots{}] [=@var{fillexp}] [,]
252b5132
RH
4468@end group
4469@end smallexample
4470
4471Most output sections do not use most of the optional section attributes.
4472
4473The whitespace around @var{section} is required, so that the section
4474name is unambiguous. The colon and the curly braces are also required.
abc9061b
CC
4475The comma at the end may be required if a @var{fillexp} is used and
4476the next @var{sections-command} looks like a continuation of the expression.
252b5132
RH
4477The line breaks and other white space are optional.
4478
4479Each @var{output-section-command} may be one of the following:
4480
4481@itemize @bullet
4482@item
4483a symbol assignment (@pxref{Assignments})
4484@item
4485an input section description (@pxref{Input Section})
4486@item
4487data values to include directly (@pxref{Output Section Data})
4488@item
4489a special output section keyword (@pxref{Output Section Keywords})
4490@end itemize
4491
4492@node Output Section Name
36f63dca 4493@subsection Output Section Name
252b5132
RH
4494@cindex name, section
4495@cindex section name
4496The name of the output section is @var{section}. @var{section} must
4497meet the constraints of your output format. In formats which only
4498support a limited number of sections, such as @code{a.out}, the name
4499must be one of the names supported by the format (@code{a.out}, for
4500example, allows only @samp{.text}, @samp{.data} or @samp{.bss}). If the
4501output format supports any number of sections, but with numbers and not
4502names (as is the case for Oasys), the name should be supplied as a
4503quoted numeric string. A section name may consist of any sequence of
4504characters, but a name which contains any unusual characters such as
4505commas must be quoted.
4506
4507The output section name @samp{/DISCARD/} is special; @ref{Output Section
4508Discarding}.
4509
4510@node Output Section Address
2a16d82a 4511@subsection Output Section Address
252b5132
RH
4512@cindex address, section
4513@cindex section address
4514The @var{address} is an expression for the VMA (the virtual memory
ea5cae92
NC
4515address) of the output section. This address is optional, but if it
4516is provided then the output address will be set exactly as specified.
4517
4518If the output address is not specified then one will be chosen for the
4519section, based on the heuristic below. This address will be adjusted
4520to fit the alignment requirement of the output section. The
4521alignment requirement is the strictest alignment of any input section
4522contained within the output section.
4523
4524The output section address heuristic is as follows:
4525
4526@itemize @bullet
4527@item
4528If an output memory @var{region} is set for the section then it
4529is added to this region and its address will be the next free address
4530in that region.
4531
4532@item
4533If the MEMORY command has been used to create a list of memory
4534regions then the first region which has attributes compatible with the
4535section is selected to contain it. The section's output address will
4536be the next free address in that region; @ref{MEMORY}.
4537
4538@item
4539If no memory regions were specified, or none match the section then
4540the output address will be based on the current value of the location
4541counter.
4542@end itemize
4543
4544@noindent
4545For example:
4546
252b5132
RH
4547@smallexample
4548.text . : @{ *(.text) @}
4549@end smallexample
ea5cae92 4550
252b5132
RH
4551@noindent
4552and
ea5cae92 4553
252b5132
RH
4554@smallexample
4555.text : @{ *(.text) @}
4556@end smallexample
ea5cae92 4557
252b5132
RH
4558@noindent
4559are subtly different. The first will set the address of the
4560@samp{.text} output section to the current value of the location
4561counter. The second will set it to the current value of the location
ea5cae92
NC
4562counter aligned to the strictest alignment of any of the @samp{.text}
4563input sections.
252b5132
RH
4564
4565The @var{address} may be an arbitrary expression; @ref{Expressions}.
4566For example, if you want to align the section on a 0x10 byte boundary,
4567so that the lowest four bits of the section address are zero, you could
4568do something like this:
4569@smallexample
4570.text ALIGN(0x10) : @{ *(.text) @}
4571@end smallexample
4572@noindent
4573This works because @code{ALIGN} returns the current location counter
4574aligned upward to the specified value.
4575
4576Specifying @var{address} for a section will change the value of the
6ce340f1
NC
4577location counter, provided that the section is non-empty. (Empty
4578sections are ignored).
252b5132
RH
4579
4580@node Input Section
36f63dca 4581@subsection Input Section Description
252b5132
RH
4582@cindex input sections
4583@cindex mapping input sections to output sections
4584The most common output section command is an input section description.
4585
4586The input section description is the most basic linker script operation.
4587You use output sections to tell the linker how to lay out your program
4588in memory. You use input section descriptions to tell the linker how to
4589map the input files into your memory layout.
4590
4591@menu
4592* Input Section Basics:: Input section basics
4593* Input Section Wildcards:: Input section wildcard patterns
4594* Input Section Common:: Input section for common symbols
4595* Input Section Keep:: Input section and garbage collection
4596* Input Section Example:: Input section example
4597@end menu
4598
4599@node Input Section Basics
36f63dca 4600@subsubsection Input Section Basics
252b5132
RH
4601@cindex input section basics
4602An input section description consists of a file name optionally followed
4603by a list of section names in parentheses.
4604
4605The file name and the section name may be wildcard patterns, which we
4606describe further below (@pxref{Input Section Wildcards}).
4607
4608The most common input section description is to include all input
4609sections with a particular name in the output section. For example, to
4610include all input @samp{.text} sections, you would write:
4611@smallexample
4612*(.text)
4613@end smallexample
4614@noindent
18625d54 4615Here the @samp{*} is a wildcard which matches any file name. To exclude a list
8f1732fc 4616@cindex EXCLUDE_FILE
18625d54
CM
4617of files from matching the file name wildcard, EXCLUDE_FILE may be used to
4618match all files except the ones specified in the EXCLUDE_FILE list. For
4619example:
252b5132 4620@smallexample
8f1732fc
AB
4621EXCLUDE_FILE (*crtend.o *otherfile.o) *(.ctors)
4622@end smallexample
4623@noindent
4624will cause all .ctors sections from all files except @file{crtend.o}
4625and @file{otherfile.o} to be included. The EXCLUDE_FILE can also be
4626placed inside the section list, for example:
4627@smallexample
b4346c09 4628*(EXCLUDE_FILE (*crtend.o *otherfile.o) .ctors)
252b5132 4629@end smallexample
8f1732fc
AB
4630@noindent
4631The result of this is identically to the previous example. Supporting
4632two syntaxes for EXCLUDE_FILE is useful if the section list contains
4633more than one section, as described below.
252b5132
RH
4634
4635There are two ways to include more than one section:
4636@smallexample
4637*(.text .rdata)
4638*(.text) *(.rdata)
4639@end smallexample
4640@noindent
4641The difference between these is the order in which the @samp{.text} and
4642@samp{.rdata} input sections will appear in the output section. In the
b6bf44ba
AM
4643first example, they will be intermingled, appearing in the same order as
4644they are found in the linker input. In the second example, all
252b5132
RH
4645@samp{.text} input sections will appear first, followed by all
4646@samp{.rdata} input sections.
4647
8f1732fc
AB
4648When using EXCLUDE_FILE with more than one section, if the exclusion
4649is within the section list then the exclusion only applies to the
4650immediately following section, for example:
a5bf7d4f
AB
4651@smallexample
4652*(EXCLUDE_FILE (*somefile.o) .text .rdata)
4653@end smallexample
4654@noindent
4655will cause all @samp{.text} sections from all files except
4656@file{somefile.o} to be included, while all @samp{.rdata} sections
4657from all files, including @file{somefile.o}, will be included. To
4658exclude the @samp{.rdata} sections from @file{somefile.o} the example
8f1732fc 4659could be modified to:
a5bf7d4f
AB
4660@smallexample
4661*(EXCLUDE_FILE (*somefile.o) .text EXCLUDE_FILE (*somefile.o) .rdata)
4662@end smallexample
8f1732fc
AB
4663@noindent
4664Alternatively, placing the EXCLUDE_FILE outside of the section list,
4665before the input file selection, will cause the exclusion to apply for
4666all sections. Thus the previous example can be rewritten as:
4667@smallexample
4668EXCLUDE_FILE (*somefile.o) *(.text .rdata)
4669@end smallexample
a5bf7d4f 4670
252b5132
RH
4671You can specify a file name to include sections from a particular file.
4672You would do this if one or more of your files contain special data that
4673needs to be at a particular location in memory. For example:
4674@smallexample
4675data.o(.data)
4676@end smallexample
4677
ae17ab41
CM
4678To refine the sections that are included based on the section flags
4679of an input section, INPUT_SECTION_FLAGS may be used.
4680
4681Here is a simple example for using Section header flags for ELF sections:
4682
4683@smallexample
4684@group
4685SECTIONS @{
4686 .text : @{ INPUT_SECTION_FLAGS (SHF_MERGE & SHF_STRINGS) *(.text) @}
4687 .text2 : @{ INPUT_SECTION_FLAGS (!SHF_WRITE) *(.text) @}
4688@}
4689@end group
4690@end smallexample
4691
4692In this example, the output section @samp{.text} will be comprised of any
4693input section matching the name *(.text) whose section header flags
4694@code{SHF_MERGE} and @code{SHF_STRINGS} are set. The output section
4695@samp{.text2} will be comprised of any input section matching the name *(.text)
4696whose section header flag @code{SHF_WRITE} is clear.
4697
967928e9
AM
4698You can also specify files within archives by writing a pattern
4699matching the archive, a colon, then the pattern matching the file,
4700with no whitespace around the colon.
4701
4702@table @samp
4703@item archive:file
4704matches file within archive
4705@item archive:
4706matches the whole archive
4707@item :file
4708matches file but not one in an archive
4709@end table
4710
4711Either one or both of @samp{archive} and @samp{file} can contain shell
4712wildcards. On DOS based file systems, the linker will assume that a
4713single letter followed by a colon is a drive specifier, so
4714@samp{c:myfile.o} is a simple file specification, not @samp{myfile.o}
4715within an archive called @samp{c}. @samp{archive:file} filespecs may
4716also be used within an @code{EXCLUDE_FILE} list, but may not appear in
4717other linker script contexts. For instance, you cannot extract a file
4718from an archive by using @samp{archive:file} in an @code{INPUT}
4719command.
4720
252b5132
RH
4721If you use a file name without a list of sections, then all sections in
4722the input file will be included in the output section. This is not
4723commonly done, but it may by useful on occasion. For example:
4724@smallexample
4725data.o
4726@end smallexample
4727
967928e9
AM
4728When you use a file name which is not an @samp{archive:file} specifier
4729and does not contain any wild card
252b5132
RH
4730characters, the linker will first see if you also specified the file
4731name on the linker command line or in an @code{INPUT} command. If you
4732did not, the linker will attempt to open the file as an input file, as
4733though it appeared on the command line. Note that this differs from an
4734@code{INPUT} command, because the linker will not search for the file in
4735the archive search path.
4736
4737@node Input Section Wildcards
36f63dca 4738@subsubsection Input Section Wildcard Patterns
252b5132
RH
4739@cindex input section wildcards
4740@cindex wildcard file name patterns
4741@cindex file name wildcard patterns
4742@cindex section name wildcard patterns
4743In an input section description, either the file name or the section
4744name or both may be wildcard patterns.
4745
4746The file name of @samp{*} seen in many examples is a simple wildcard
4747pattern for the file name.
4748
4749The wildcard patterns are like those used by the Unix shell.
4750
4751@table @samp
4752@item *
4753matches any number of characters
4754@item ?
4755matches any single character
4756@item [@var{chars}]
4757matches a single instance of any of the @var{chars}; the @samp{-}
4758character may be used to specify a range of characters, as in
4759@samp{[a-z]} to match any lower case letter
4760@item \
4761quotes the following character
4762@end table
4763
4764When a file name is matched with a wildcard, the wildcard characters
4765will not match a @samp{/} character (used to separate directory names on
4766Unix). A pattern consisting of a single @samp{*} character is an
4767exception; it will always match any file name, whether it contains a
4768@samp{/} or not. In a section name, the wildcard characters will match
4769a @samp{/} character.
4770
4771File name wildcard patterns only match files which are explicitly
4772specified on the command line or in an @code{INPUT} command. The linker
4773does not search directories to expand wildcards.
4774
4775If a file name matches more than one wildcard pattern, or if a file name
4776appears explicitly and is also matched by a wildcard pattern, the linker
4777will use the first match in the linker script. For example, this
4778sequence of input section descriptions is probably in error, because the
4779@file{data.o} rule will not be used:
4780@smallexample
4781.data : @{ *(.data) @}
4782.data1 : @{ data.o(.data) @}
4783@end smallexample
4784
bcaa7b3e 4785@cindex SORT_BY_NAME
252b5132
RH
4786Normally, the linker will place files and sections matched by wildcards
4787in the order in which they are seen during the link. You can change
bcaa7b3e
L
4788this by using the @code{SORT_BY_NAME} keyword, which appears before a wildcard
4789pattern in parentheses (e.g., @code{SORT_BY_NAME(.text*)}). When the
4790@code{SORT_BY_NAME} keyword is used, the linker will sort the files or sections
252b5132
RH
4791into ascending order by name before placing them in the output file.
4792
bcaa7b3e 4793@cindex SORT_BY_ALIGNMENT
9a24a276
AM
4794@code{SORT_BY_ALIGNMENT} is similar to @code{SORT_BY_NAME}.
4795@code{SORT_BY_ALIGNMENT} will sort sections into descending order of
4796alignment before placing them in the output file. Placing larger
4797alignments before smaller alignments can reduce the amount of padding
4798needed.
bcaa7b3e 4799
02ecc8e9 4800@cindex SORT_BY_INIT_PRIORITY
9a24a276
AM
4801@code{SORT_BY_INIT_PRIORITY} is also similar to @code{SORT_BY_NAME}.
4802@code{SORT_BY_INIT_PRIORITY} will sort sections into ascending
4803numerical order of the GCC init_priority attribute encoded in the
4804section name before placing them in the output file. In
4805@code{.init_array.NNNNN} and @code{.fini_array.NNNNN}, @code{NNNNN} is
4806the init_priority. In @code{.ctors.NNNNN} and @code{.dtors.NNNNN},
4807@code{NNNNN} is 65535 minus the init_priority.
02ecc8e9 4808
bcaa7b3e
L
4809@cindex SORT
4810@code{SORT} is an alias for @code{SORT_BY_NAME}.
4811
4812When there are nested section sorting commands in linker script, there
4813can be at most 1 level of nesting for section sorting commands.
4814
4815@enumerate
4816@item
4817@code{SORT_BY_NAME} (@code{SORT_BY_ALIGNMENT} (wildcard section pattern)).
1ae5c3ae 4818It will sort the input sections by name first, then by alignment if two
bcaa7b3e
L
4819sections have the same name.
4820@item
4821@code{SORT_BY_ALIGNMENT} (@code{SORT_BY_NAME} (wildcard section pattern)).
1ae5c3ae 4822It will sort the input sections by alignment first, then by name if two
bcaa7b3e
L
4823sections have the same alignment.
4824@item
c0065db7 4825@code{SORT_BY_NAME} (@code{SORT_BY_NAME} (wildcard section pattern)) is
bcaa7b3e
L
4826treated the same as @code{SORT_BY_NAME} (wildcard section pattern).
4827@item
4828@code{SORT_BY_ALIGNMENT} (@code{SORT_BY_ALIGNMENT} (wildcard section pattern))
4829is treated the same as @code{SORT_BY_ALIGNMENT} (wildcard section pattern).
4830@item
4831All other nested section sorting commands are invalid.
4832@end enumerate
4833
a05a5b64 4834When both command-line section sorting option and linker script
bcaa7b3e 4835section sorting command are used, section sorting command always
a05a5b64 4836takes precedence over the command-line option.
bcaa7b3e
L
4837
4838If the section sorting command in linker script isn't nested, the
a05a5b64 4839command-line option will make the section sorting command to be
bcaa7b3e
L
4840treated as nested sorting command.
4841
4842@enumerate
4843@item
4844@code{SORT_BY_NAME} (wildcard section pattern ) with
4845@option{--sort-sections alignment} is equivalent to
4846@code{SORT_BY_NAME} (@code{SORT_BY_ALIGNMENT} (wildcard section pattern)).
4847@item
4848@code{SORT_BY_ALIGNMENT} (wildcard section pattern) with
4849@option{--sort-section name} is equivalent to
4850@code{SORT_BY_ALIGNMENT} (@code{SORT_BY_NAME} (wildcard section pattern)).
4851@end enumerate
4852
4853If the section sorting command in linker script is nested, the
a05a5b64 4854command-line option will be ignored.
bcaa7b3e 4855
eda680f8 4856@cindex SORT_NONE
a05a5b64 4857@code{SORT_NONE} disables section sorting by ignoring the command-line
eda680f8
L
4858section sorting option.
4859
252b5132
RH
4860If you ever get confused about where input sections are going, use the
4861@samp{-M} linker option to generate a map file. The map file shows
4862precisely how input sections are mapped to output sections.
4863
4864This example shows how wildcard patterns might be used to partition
4865files. This linker script directs the linker to place all @samp{.text}
4866sections in @samp{.text} and all @samp{.bss} sections in @samp{.bss}.
4867The linker will place the @samp{.data} section from all files beginning
4868with an upper case character in @samp{.DATA}; for all other files, the
4869linker will place the @samp{.data} section in @samp{.data}.
4870@smallexample
4871@group
4872SECTIONS @{
4873 .text : @{ *(.text) @}
4874 .DATA : @{ [A-Z]*(.data) @}
4875 .data : @{ *(.data) @}
4876 .bss : @{ *(.bss) @}
4877@}
4878@end group
4879@end smallexample
4880
4881@node Input Section Common
36f63dca 4882@subsubsection Input Section for Common Symbols
252b5132
RH
4883@cindex common symbol placement
4884@cindex uninitialized data placement
4885A special notation is needed for common symbols, because in many object
4886file formats common symbols do not have a particular input section. The
4887linker treats common symbols as though they are in an input section
4888named @samp{COMMON}.
4889
4890You may use file names with the @samp{COMMON} section just as with any
4891other input sections. You can use this to place common symbols from a
4892particular input file in one section while common symbols from other
4893input files are placed in another section.
4894
4895In most cases, common symbols in input files will be placed in the
4896@samp{.bss} section in the output file. For example:
4897@smallexample
4898.bss @{ *(.bss) *(COMMON) @}
4899@end smallexample
4900
4901@cindex scommon section
4902@cindex small common symbols
4903Some object file formats have more than one type of common symbol. For
4904example, the MIPS ELF object file format distinguishes standard common
4905symbols and small common symbols. In this case, the linker will use a
4906different special section name for other types of common symbols. In
4907the case of MIPS ELF, the linker uses @samp{COMMON} for standard common
4908symbols and @samp{.scommon} for small common symbols. This permits you
4909to map the different types of common symbols into memory at different
4910locations.
4911
4912@cindex [COMMON]
4913You will sometimes see @samp{[COMMON]} in old linker scripts. This
4914notation is now considered obsolete. It is equivalent to
4915@samp{*(COMMON)}.
4916
4917@node Input Section Keep
36f63dca 4918@subsubsection Input Section and Garbage Collection
252b5132
RH
4919@cindex KEEP
4920@cindex garbage collection
4921When link-time garbage collection is in use (@samp{--gc-sections}),
a1ab1d2a 4922it is often useful to mark sections that should not be eliminated.
252b5132
RH
4923This is accomplished by surrounding an input section's wildcard entry
4924with @code{KEEP()}, as in @code{KEEP(*(.init))} or
bcaa7b3e 4925@code{KEEP(SORT_BY_NAME(*)(.ctors))}.
252b5132
RH
4926
4927@node Input Section Example
36f63dca 4928@subsubsection Input Section Example
252b5132
RH
4929The following example is a complete linker script. It tells the linker
4930to read all of the sections from file @file{all.o} and place them at the
4931start of output section @samp{outputa} which starts at location
4932@samp{0x10000}. All of section @samp{.input1} from file @file{foo.o}
4933follows immediately, in the same output section. All of section
4934@samp{.input2} from @file{foo.o} goes into output section
4935@samp{outputb}, followed by section @samp{.input1} from @file{foo1.o}.
4936All of the remaining @samp{.input1} and @samp{.input2} sections from any
4937files are written to output section @samp{outputc}.
4938
4939@smallexample
4940@group
4941SECTIONS @{
4942 outputa 0x10000 :
4943 @{
4944 all.o
4945 foo.o (.input1)
4946 @}
36f63dca
NC
4947@end group
4948@group
252b5132
RH
4949 outputb :
4950 @{
4951 foo.o (.input2)
4952 foo1.o (.input1)
4953 @}
36f63dca
NC
4954@end group
4955@group
252b5132
RH
4956 outputc :
4957 @{
4958 *(.input1)
4959 *(.input2)
4960 @}
4961@}
4962@end group
a1ab1d2a 4963@end smallexample
252b5132 4964
cbd0eecf
L
4965If an output section's name is the same as the input section's name
4966and is representable as a C identifier, then the linker will
4967automatically @pxref{PROVIDE} two symbols: __start_SECNAME and
4968__stop_SECNAME, where SECNAME is the name of the section. These
4969indicate the start address and end address of the output section
4970respectively. Note: most section names are not representable as
4971C identifiers because they contain a @samp{.} character.
4972
252b5132 4973@node Output Section Data
36f63dca 4974@subsection Output Section Data
252b5132
RH
4975@cindex data
4976@cindex section data
4977@cindex output section data
4978@kindex BYTE(@var{expression})
4979@kindex SHORT(@var{expression})
4980@kindex LONG(@var{expression})
4981@kindex QUAD(@var{expression})
4982@kindex SQUAD(@var{expression})
4983You can include explicit bytes of data in an output section by using
4984@code{BYTE}, @code{SHORT}, @code{LONG}, @code{QUAD}, or @code{SQUAD} as
4985an output section command. Each keyword is followed by an expression in
4986parentheses providing the value to store (@pxref{Expressions}). The
4987value of the expression is stored at the current value of the location
4988counter.
4989
4990The @code{BYTE}, @code{SHORT}, @code{LONG}, and @code{QUAD} commands
4991store one, two, four, and eight bytes (respectively). After storing the
4992bytes, the location counter is incremented by the number of bytes
4993stored.
4994
4995For example, this will store the byte 1 followed by the four byte value
4996of the symbol @samp{addr}:
4997@smallexample
4998BYTE(1)
4999LONG(addr)
5000@end smallexample
5001
5002When using a 64 bit host or target, @code{QUAD} and @code{SQUAD} are the
5003same; they both store an 8 byte, or 64 bit, value. When both host and
5004target are 32 bits, an expression is computed as 32 bits. In this case
5005@code{QUAD} stores a 32 bit value zero extended to 64 bits, and
5006@code{SQUAD} stores a 32 bit value sign extended to 64 bits.
5007
5008If the object file format of the output file has an explicit endianness,
5009which is the normal case, the value will be stored in that endianness.
5010When the object file format does not have an explicit endianness, as is
5011true of, for example, S-records, the value will be stored in the
5012endianness of the first input object file.
5013
36f63dca 5014Note---these commands only work inside a section description and not
2b5fc1f5
NC
5015between them, so the following will produce an error from the linker:
5016@smallexample
5017SECTIONS @{@ .text : @{@ *(.text) @}@ LONG(1) .data : @{@ *(.data) @}@ @}@
5018@end smallexample
5019whereas this will work:
5020@smallexample
5021SECTIONS @{@ .text : @{@ *(.text) ; LONG(1) @}@ .data : @{@ *(.data) @}@ @}@
5022@end smallexample
5023
252b5132
RH
5024@kindex FILL(@var{expression})
5025@cindex holes, filling
5026@cindex unspecified memory
5027You may use the @code{FILL} command to set the fill pattern for the
5028current section. It is followed by an expression in parentheses. Any
5029otherwise unspecified regions of memory within the section (for example,
5030gaps left due to the required alignment of input sections) are filled
a139d329 5031with the value of the expression, repeated as
252b5132
RH
5032necessary. A @code{FILL} statement covers memory locations after the
5033point at which it occurs in the section definition; by including more
5034than one @code{FILL} statement, you can have different fill patterns in
5035different parts of an output section.
5036
5037This example shows how to fill unspecified regions of memory with the
563e308f 5038value @samp{0x90}:
252b5132 5039@smallexample
563e308f 5040FILL(0x90909090)
252b5132
RH
5041@end smallexample
5042
5043The @code{FILL} command is similar to the @samp{=@var{fillexp}} output
9673c93c 5044section attribute, but it only affects the
252b5132
RH
5045part of the section following the @code{FILL} command, rather than the
5046entire section. If both are used, the @code{FILL} command takes
9673c93c 5047precedence. @xref{Output Section Fill}, for details on the fill
a139d329 5048expression.
252b5132
RH
5049
5050@node Output Section Keywords
36f63dca 5051@subsection Output Section Keywords
252b5132
RH
5052There are a couple of keywords which can appear as output section
5053commands.
5054
5055@table @code
5056@kindex CREATE_OBJECT_SYMBOLS
5057@cindex input filename symbols
5058@cindex filename symbols
5059@item CREATE_OBJECT_SYMBOLS
5060The command tells the linker to create a symbol for each input file.
5061The name of each symbol will be the name of the corresponding input
5062file. The section of each symbol will be the output section in which
5063the @code{CREATE_OBJECT_SYMBOLS} command appears.
5064
5065This is conventional for the a.out object file format. It is not
5066normally used for any other object file format.
5067
5068@kindex CONSTRUCTORS
5069@cindex C++ constructors, arranging in link
5070@cindex constructors, arranging in link
5071@item CONSTRUCTORS
5072When linking using the a.out object file format, the linker uses an
5073unusual set construct to support C++ global constructors and
5074destructors. When linking object file formats which do not support
5075arbitrary sections, such as ECOFF and XCOFF, the linker will
5076automatically recognize C++ global constructors and destructors by name.
5077For these object file formats, the @code{CONSTRUCTORS} command tells the
5078linker to place constructor information in the output section where the
5079@code{CONSTRUCTORS} command appears. The @code{CONSTRUCTORS} command is
5080ignored for other object file formats.
5081
5082The symbol @w{@code{__CTOR_LIST__}} marks the start of the global
7e69709c
AM
5083constructors, and the symbol @w{@code{__CTOR_END__}} marks the end.
5084Similarly, @w{@code{__DTOR_LIST__}} and @w{@code{__DTOR_END__}} mark
5085the start and end of the global destructors. The
252b5132
RH
5086first word in the list is the number of entries, followed by the address
5087of each constructor or destructor, followed by a zero word. The
5088compiler must arrange to actually run the code. For these object file
5089formats @sc{gnu} C++ normally calls constructors from a subroutine
5090@code{__main}; a call to @code{__main} is automatically inserted into
5091the startup code for @code{main}. @sc{gnu} C++ normally runs
5092destructors either by using @code{atexit}, or directly from the function
5093@code{exit}.
5094
5095For object file formats such as @code{COFF} or @code{ELF} which support
5096arbitrary section names, @sc{gnu} C++ will normally arrange to put the
5097addresses of global constructors and destructors into the @code{.ctors}
5098and @code{.dtors} sections. Placing the following sequence into your
5099linker script will build the sort of table which the @sc{gnu} C++
5100runtime code expects to see.
5101
5102@smallexample
5103 __CTOR_LIST__ = .;
5104 LONG((__CTOR_END__ - __CTOR_LIST__) / 4 - 2)
5105 *(.ctors)
5106 LONG(0)
5107 __CTOR_END__ = .;
5108 __DTOR_LIST__ = .;
5109 LONG((__DTOR_END__ - __DTOR_LIST__) / 4 - 2)
5110 *(.dtors)
5111 LONG(0)
5112 __DTOR_END__ = .;
5113@end smallexample
5114
5115If you are using the @sc{gnu} C++ support for initialization priority,
5116which provides some control over the order in which global constructors
5117are run, you must sort the constructors at link time to ensure that they
5118are executed in the correct order. When using the @code{CONSTRUCTORS}
bcaa7b3e
L
5119command, use @samp{SORT_BY_NAME(CONSTRUCTORS)} instead. When using the
5120@code{.ctors} and @code{.dtors} sections, use @samp{*(SORT_BY_NAME(.ctors))} and
5121@samp{*(SORT_BY_NAME(.dtors))} instead of just @samp{*(.ctors)} and
252b5132
RH
5122@samp{*(.dtors)}.
5123
5124Normally the compiler and linker will handle these issues automatically,
5125and you will not need to concern yourself with them. However, you may
5126need to consider this if you are using C++ and writing your own linker
5127scripts.
5128
5129@end table
5130
5131@node Output Section Discarding
36f63dca 5132@subsection Output Section Discarding
252b5132
RH
5133@cindex discarding sections
5134@cindex sections, discarding
5135@cindex removing sections
2edab91c
AM
5136The linker will not normally create output sections with no contents.
5137This is for convenience when referring to input sections that may or
5138may not be present in any of the input files. For example:
252b5132 5139@smallexample
49c13adb 5140.foo : @{ *(.foo) @}
252b5132
RH
5141@end smallexample
5142@noindent
5143will only create a @samp{.foo} section in the output file if there is a
74541ad4
AM
5144@samp{.foo} section in at least one input file, and if the input
5145sections are not all empty. Other link script directives that allocate
2edab91c
AM
5146space in an output section will also create the output section. So
5147too will assignments to dot even if the assignment does not create
5148space, except for @samp{. = 0}, @samp{. = . + 0}, @samp{. = sym},
5149@samp{. = . + sym} and @samp{. = ALIGN (. != 0, expr, 1)} when
5150@samp{sym} is an absolute symbol of value 0 defined in the script.
5151This allows you to force output of an empty section with @samp{. = .}.
74541ad4 5152
a0976ea4 5153The linker will ignore address assignments (@pxref{Output Section Address})
74541ad4
AM
5154on discarded output sections, except when the linker script defines
5155symbols in the output section. In that case the linker will obey
a0976ea4
AM
5156the address assignments, possibly advancing dot even though the
5157section is discarded.
252b5132
RH
5158
5159@cindex /DISCARD/
5160The special output section name @samp{/DISCARD/} may be used to discard
5161input sections. Any input sections which are assigned to an output
5162section named @samp{/DISCARD/} are not included in the output file.
5163
a2e098c3
NC
5164Note, sections that match the @samp{/DISCARD/} output section will be
5165discarded even if they are in an ELF section group which has other
5166members which are not being discarded. This is deliberate.
5167Discarding takes precedence over grouping.
5168
252b5132 5169@node Output Section Attributes
36f63dca 5170@subsection Output Section Attributes
252b5132
RH
5171@cindex output section attributes
5172We showed above that the full description of an output section looked
5173like this:
0c71d759 5174
252b5132 5175@smallexample
a1ab1d2a 5176@group
7e7d5768 5177@var{section} [@var{address}] [(@var{type})] :
0c71d759 5178 [AT(@var{lma})]
3bbec4bd 5179 [ALIGN(@var{section_align}) | ALIGN_WITH_INPUT]
0c71d759
NC
5180 [SUBALIGN(@var{subsection_align})]
5181 [@var{constraint}]
252b5132
RH
5182 @{
5183 @var{output-section-command}
5184 @var{output-section-command}
5185 @dots{}
562d3460 5186 @} [>@var{region}] [AT>@var{lma_region}] [:@var{phdr} :@var{phdr} @dots{}] [=@var{fillexp}]
252b5132
RH
5187@end group
5188@end smallexample
0c71d759 5189
252b5132
RH
5190We've already described @var{section}, @var{address}, and
5191@var{output-section-command}. In this section we will describe the
5192remaining section attributes.
5193
a1ab1d2a 5194@menu
252b5132
RH
5195* Output Section Type:: Output section type
5196* Output Section LMA:: Output section LMA
bbf115d3 5197* Forced Output Alignment:: Forced Output Alignment
7e7d5768 5198* Forced Input Alignment:: Forced Input Alignment
0c71d759 5199* Output Section Constraint:: Output section constraint
252b5132
RH
5200* Output Section Region:: Output section region
5201* Output Section Phdr:: Output section phdr
5202* Output Section Fill:: Output section fill
5203@end menu
5204
5205@node Output Section Type
36f63dca 5206@subsubsection Output Section Type
252b5132
RH
5207Each output section may have a type. The type is a keyword in
5208parentheses. The following types are defined:
5209
5210@table @code
5211@item NOLOAD
5212The section should be marked as not loadable, so that it will not be
5213loaded into memory when the program is run.
5214@item DSECT
5215@itemx COPY
5216@itemx INFO
5217@itemx OVERLAY
5218These type names are supported for backward compatibility, and are
5219rarely used. They all have the same effect: the section should be
5220marked as not allocatable, so that no memory is allocated for the
5221section when the program is run.
5222@end table
5223
5224@kindex NOLOAD
5225@cindex prevent unnecessary loading
5226@cindex loading, preventing
5227The linker normally sets the attributes of an output section based on
5228the input sections which map into it. You can override this by using
5229the section type. For example, in the script sample below, the
5230@samp{ROM} section is addressed at memory location @samp{0} and does not
2e76e85a 5231need to be loaded when the program is run.
252b5132
RH
5232@smallexample
5233@group
5234SECTIONS @{
5235 ROM 0 (NOLOAD) : @{ @dots{} @}
5236 @dots{}
5237@}
5238@end group
5239@end smallexample
5240
5241@node Output Section LMA
36f63dca 5242@subsubsection Output Section LMA
562d3460 5243@kindex AT>@var{lma_region}
252b5132
RH
5244@kindex AT(@var{lma})
5245@cindex load address
5246@cindex section load address
5247Every section has a virtual address (VMA) and a load address (LMA); see
ea5cae92
NC
5248@ref{Basic Script Concepts}. The virtual address is specified by the
5249@pxref{Output Section Address} described earlier. The load address is
5250specified by the @code{AT} or @code{AT>} keywords. Specifying a load
5251address is optional.
6bdafbeb 5252
ea5cae92
NC
5253The @code{AT} keyword takes an expression as an argument. This
5254specifies the exact load address of the section. The @code{AT>} keyword
5255takes the name of a memory region as an argument. @xref{MEMORY}. The
5256load address of the section is set to the next free address in the
5257region, aligned to the section's alignment requirements.
dc0b6aa0
AM
5258
5259If neither @code{AT} nor @code{AT>} is specified for an allocatable
ea5cae92
NC
5260section, the linker will use the following heuristic to determine the
5261load address:
5262
5263@itemize @bullet
5264@item
5265If the section has a specific VMA address, then this is used as
5266the LMA address as well.
5267
5268@item
5269If the section is not allocatable then its LMA is set to its VMA.
5270
5271@item
5272Otherwise if a memory region can be found that is compatible
5273with the current section, and this region contains at least one
5274section, then the LMA is set so the difference between the
5275VMA and LMA is the same as the difference between the VMA and LMA of
5276the last section in the located region.
5277
5278@item
5279If no memory regions have been declared then a default region
5280that covers the entire address space is used in the previous step.
5281
5282@item
5283If no suitable region could be found, or there was no previous
5284section then the LMA is set equal to the VMA.
5285@end itemize
252b5132
RH
5286
5287@cindex ROM initialized data
5288@cindex initialized data in ROM
5289This feature is designed to make it easy to build a ROM image. For
5290example, the following linker script creates three output sections: one
5291called @samp{.text}, which starts at @code{0x1000}, one called
5292@samp{.mdata}, which is loaded at the end of the @samp{.text} section
5293even though its VMA is @code{0x2000}, and one called @samp{.bss} to hold
5294uninitialized data at address @code{0x3000}. The symbol @code{_data} is
5295defined with the value @code{0x2000}, which shows that the location
5296counter holds the VMA value, not the LMA value.
5297
5298@smallexample
5299@group
5300SECTIONS
5301 @{
5302 .text 0x1000 : @{ *(.text) _etext = . ; @}
a1ab1d2a 5303 .mdata 0x2000 :
252b5132
RH
5304 AT ( ADDR (.text) + SIZEOF (.text) )
5305 @{ _data = . ; *(.data); _edata = . ; @}
5306 .bss 0x3000 :
5307 @{ _bstart = . ; *(.bss) *(COMMON) ; _bend = . ;@}
5308@}
5309@end group
5310@end smallexample
5311
5312The run-time initialization code for use with a program generated with
5313this linker script would include something like the following, to copy
5314the initialized data from the ROM image to its runtime address. Notice
5315how this code takes advantage of the symbols defined by the linker
5316script.
5317
5318@smallexample
5319@group
5320extern char _etext, _data, _edata, _bstart, _bend;
5321char *src = &_etext;
5322char *dst = &_data;
5323
ea5cae92
NC
5324/* ROM has data at end of text; copy it. */
5325while (dst < &_edata)
252b5132 5326 *dst++ = *src++;
252b5132 5327
ea5cae92 5328/* Zero bss. */
252b5132
RH
5329for (dst = &_bstart; dst< &_bend; dst++)
5330 *dst = 0;
5331@end group
5332@end smallexample
5333
bbf115d3
L
5334@node Forced Output Alignment
5335@subsubsection Forced Output Alignment
5336@kindex ALIGN(@var{section_align})
5337@cindex forcing output section alignment
5338@cindex output section alignment
1eec346e 5339You can increase an output section's alignment by using ALIGN. As an
13075d04
SH
5340alternative you can enforce that the difference between the VMA and LMA remains
5341intact throughout this output section with the ALIGN_WITH_INPUT attribute.
bbf115d3 5342
7e7d5768
AM
5343@node Forced Input Alignment
5344@subsubsection Forced Input Alignment
5345@kindex SUBALIGN(@var{subsection_align})
5346@cindex forcing input section alignment
5347@cindex input section alignment
5348You can force input section alignment within an output section by using
5349SUBALIGN. The value specified overrides any alignment given by input
5350sections, whether larger or smaller.
5351
0c71d759
NC
5352@node Output Section Constraint
5353@subsubsection Output Section Constraint
5354@kindex ONLY_IF_RO
5355@kindex ONLY_IF_RW
5356@cindex constraints on output sections
5357You can specify that an output section should only be created if all
5358of its input sections are read-only or all of its input sections are
5359read-write by using the keyword @code{ONLY_IF_RO} and
5360@code{ONLY_IF_RW} respectively.
5361
252b5132 5362@node Output Section Region
36f63dca 5363@subsubsection Output Section Region
252b5132
RH
5364@kindex >@var{region}
5365@cindex section, assigning to memory region
5366@cindex memory regions and sections
5367You can assign a section to a previously defined region of memory by
5368using @samp{>@var{region}}. @xref{MEMORY}.
5369
5370Here is a simple example:
5371@smallexample
5372@group
5373MEMORY @{ rom : ORIGIN = 0x1000, LENGTH = 0x1000 @}
5374SECTIONS @{ ROM : @{ *(.text) @} >rom @}
5375@end group
5376@end smallexample
5377
5378@node Output Section Phdr
36f63dca 5379@subsubsection Output Section Phdr
252b5132
RH
5380@kindex :@var{phdr}
5381@cindex section, assigning to program header
5382@cindex program headers and sections
5383You can assign a section to a previously defined program segment by
5384using @samp{:@var{phdr}}. @xref{PHDRS}. If a section is assigned to
5385one or more segments, then all subsequent allocated sections will be
5386assigned to those segments as well, unless they use an explicitly
5387@code{:@var{phdr}} modifier. You can use @code{:NONE} to tell the
5388linker to not put the section in any segment at all.
5389
5390Here is a simple example:
5391@smallexample
5392@group
5393PHDRS @{ text PT_LOAD ; @}
5394SECTIONS @{ .text : @{ *(.text) @} :text @}
5395@end group
5396@end smallexample
5397
5398@node Output Section Fill
36f63dca 5399@subsubsection Output Section Fill
252b5132
RH
5400@kindex =@var{fillexp}
5401@cindex section fill pattern
5402@cindex fill pattern, entire section
5403You can set the fill pattern for an entire section by using
5404@samp{=@var{fillexp}}. @var{fillexp} is an expression
5405(@pxref{Expressions}). Any otherwise unspecified regions of memory
5406within the output section (for example, gaps left due to the required
a139d329
AM
5407alignment of input sections) will be filled with the value, repeated as
5408necessary. If the fill expression is a simple hex number, ie. a string
9673c93c 5409of hex digit starting with @samp{0x} and without a trailing @samp{k} or @samp{M}, then
a139d329
AM
5410an arbitrarily long sequence of hex digits can be used to specify the
5411fill pattern; Leading zeros become part of the pattern too. For all
9673c93c 5412other cases, including extra parentheses or a unary @code{+}, the fill
a139d329
AM
5413pattern is the four least significant bytes of the value of the
5414expression. In all cases, the number is big-endian.
252b5132
RH
5415
5416You can also change the fill value with a @code{FILL} command in the
9673c93c 5417output section commands; (@pxref{Output Section Data}).
252b5132
RH
5418
5419Here is a simple example:
5420@smallexample
5421@group
563e308f 5422SECTIONS @{ .text : @{ *(.text) @} =0x90909090 @}
252b5132
RH
5423@end group
5424@end smallexample
5425
5426@node Overlay Description
36f63dca 5427@subsection Overlay Description
252b5132
RH
5428@kindex OVERLAY
5429@cindex overlays
5430An overlay description provides an easy way to describe sections which
5431are to be loaded as part of a single memory image but are to be run at
5432the same memory address. At run time, some sort of overlay manager will
5433copy the overlaid sections in and out of the runtime memory address as
5434required, perhaps by simply manipulating addressing bits. This approach
5435can be useful, for example, when a certain region of memory is faster
5436than another.
5437
5438Overlays are described using the @code{OVERLAY} command. The
5439@code{OVERLAY} command is used within a @code{SECTIONS} command, like an
5440output section description. The full syntax of the @code{OVERLAY}
5441command is as follows:
5442@smallexample
5443@group
5444OVERLAY [@var{start}] : [NOCROSSREFS] [AT ( @var{ldaddr} )]
5445 @{
5446 @var{secname1}
5447 @{
5448 @var{output-section-command}
5449 @var{output-section-command}
5450 @dots{}
5451 @} [:@var{phdr}@dots{}] [=@var{fill}]
5452 @var{secname2}
5453 @{
5454 @var{output-section-command}
5455 @var{output-section-command}
5456 @dots{}
5457 @} [:@var{phdr}@dots{}] [=@var{fill}]
5458 @dots{}
abc9061b 5459 @} [>@var{region}] [:@var{phdr}@dots{}] [=@var{fill}] [,]
252b5132
RH
5460@end group
5461@end smallexample
5462
5463Everything is optional except @code{OVERLAY} (a keyword), and each
5464section must have a name (@var{secname1} and @var{secname2} above). The
5465section definitions within the @code{OVERLAY} construct are identical to
11e7fd74 5466those within the general @code{SECTIONS} construct (@pxref{SECTIONS}),
252b5132
RH
5467except that no addresses and no memory regions may be defined for
5468sections within an @code{OVERLAY}.
5469
abc9061b
CC
5470The comma at the end may be required if a @var{fill} is used and
5471the next @var{sections-command} looks like a continuation of the expression.
5472
252b5132
RH
5473The sections are all defined with the same starting address. The load
5474addresses of the sections are arranged such that they are consecutive in
5475memory starting at the load address used for the @code{OVERLAY} as a
5476whole (as with normal section definitions, the load address is optional,
5477and defaults to the start address; the start address is also optional,
5478and defaults to the current value of the location counter).
5479
56dd11f0
NC
5480If the @code{NOCROSSREFS} keyword is used, and there are any
5481references among the sections, the linker will report an error. Since
5482the sections all run at the same address, it normally does not make
5483sense for one section to refer directly to another.
5484@xref{Miscellaneous Commands, NOCROSSREFS}.
252b5132
RH
5485
5486For each section within the @code{OVERLAY}, the linker automatically
34711ca3 5487provides two symbols. The symbol @code{__load_start_@var{secname}} is
252b5132
RH
5488defined as the starting load address of the section. The symbol
5489@code{__load_stop_@var{secname}} is defined as the final load address of
5490the section. Any characters within @var{secname} which are not legal
5491within C identifiers are removed. C (or assembler) code may use these
5492symbols to move the overlaid sections around as necessary.
5493
5494At the end of the overlay, the value of the location counter is set to
5495the start address of the overlay plus the size of the largest section.
5496
5497Here is an example. Remember that this would appear inside a
5498@code{SECTIONS} construct.
5499@smallexample
5500@group
5501 OVERLAY 0x1000 : AT (0x4000)
5502 @{
5503 .text0 @{ o1/*.o(.text) @}
5504 .text1 @{ o2/*.o(.text) @}
5505 @}
5506@end group
5507@end smallexample
5508@noindent
5509This will define both @samp{.text0} and @samp{.text1} to start at
5510address 0x1000. @samp{.text0} will be loaded at address 0x4000, and
5511@samp{.text1} will be loaded immediately after @samp{.text0}. The
34711ca3 5512following symbols will be defined if referenced: @code{__load_start_text0},
252b5132
RH
5513@code{__load_stop_text0}, @code{__load_start_text1},
5514@code{__load_stop_text1}.
5515
5516C code to copy overlay @code{.text1} into the overlay area might look
5517like the following.
5518
5519@smallexample
5520@group
5521 extern char __load_start_text1, __load_stop_text1;
5522 memcpy ((char *) 0x1000, &__load_start_text1,
5523 &__load_stop_text1 - &__load_start_text1);
5524@end group
5525@end smallexample
5526
5527Note that the @code{OVERLAY} command is just syntactic sugar, since
5528everything it does can be done using the more basic commands. The above
5529example could have been written identically as follows.
5530
5531@smallexample
5532@group
5533 .text0 0x1000 : AT (0x4000) @{ o1/*.o(.text) @}
34711ca3
AM
5534 PROVIDE (__load_start_text0 = LOADADDR (.text0));
5535 PROVIDE (__load_stop_text0 = LOADADDR (.text0) + SIZEOF (.text0));
252b5132 5536 .text1 0x1000 : AT (0x4000 + SIZEOF (.text0)) @{ o2/*.o(.text) @}
34711ca3
AM
5537 PROVIDE (__load_start_text1 = LOADADDR (.text1));
5538 PROVIDE (__load_stop_text1 = LOADADDR (.text1) + SIZEOF (.text1));
252b5132
RH
5539 . = 0x1000 + MAX (SIZEOF (.text0), SIZEOF (.text1));
5540@end group
5541@end smallexample
5542
5543@node MEMORY
36f63dca 5544@section MEMORY Command
252b5132
RH
5545@kindex MEMORY
5546@cindex memory regions
5547@cindex regions of memory
5548@cindex allocating memory
5549@cindex discontinuous memory
5550The linker's default configuration permits allocation of all available
5551memory. You can override this by using the @code{MEMORY} command.
5552
5553The @code{MEMORY} command describes the location and size of blocks of
5554memory in the target. You can use it to describe which memory regions
5555may be used by the linker, and which memory regions it must avoid. You
5556can then assign sections to particular memory regions. The linker will
5557set section addresses based on the memory regions, and will warn about
5558regions that become too full. The linker will not shuffle sections
5559around to fit into the available regions.
5560
127fcdff
AB
5561A linker script may contain many uses of the @code{MEMORY} command,
5562however, all memory blocks defined are treated as if they were
5563specified inside a single @code{MEMORY} command. The syntax for
5564@code{MEMORY} is:
252b5132
RH
5565@smallexample
5566@group
a1ab1d2a 5567MEMORY
252b5132
RH
5568 @{
5569 @var{name} [(@var{attr})] : ORIGIN = @var{origin}, LENGTH = @var{len}
5570 @dots{}
5571 @}
5572@end group
5573@end smallexample
5574
5575The @var{name} is a name used in the linker script to refer to the
5576region. The region name has no meaning outside of the linker script.
5577Region names are stored in a separate name space, and will not conflict
5578with symbol names, file names, or section names. Each memory region
4a93e180
NC
5579must have a distinct name within the @code{MEMORY} command. However you can
5580add later alias names to existing memory regions with the @ref{REGION_ALIAS}
9d5777a3 5581command.
252b5132
RH
5582
5583@cindex memory region attributes
5584The @var{attr} string is an optional list of attributes that specify
5585whether to use a particular memory region for an input section which is
5586not explicitly mapped in the linker script. As described in
5587@ref{SECTIONS}, if you do not specify an output section for some input
5588section, the linker will create an output section with the same name as
5589the input section. If you define region attributes, the linker will use
5590them to select the memory region for the output section that it creates.
5591
5592The @var{attr} string must consist only of the following characters:
5593@table @samp
5594@item R
5595Read-only section
5596@item W
5597Read/write section
5598@item X
5599Executable section
5600@item A
5601Allocatable section
5602@item I
5603Initialized section
5604@item L
5605Same as @samp{I}
5606@item !
c09e9a8c 5607Invert the sense of any of the attributes that follow
252b5132
RH
5608@end table
5609
81c688d5 5610If an unmapped section matches any of the listed attributes other than
252b5132 5611@samp{!}, it will be placed in the memory region. The @samp{!}
81c688d5
NC
5612attribute reverses the test for the characters that follow, so that an
5613unmapped section will be placed in the memory region only if it does
5614not match any of the attributes listed afterwards. Thus an attribute
5615string of @samp{RW!X} will match any unmapped section that has either
5616or both of the @samp{R} and @samp{W} attributes, but only as long as
5617the section does not also have the @samp{X} attribute.
252b5132
RH
5618
5619@kindex ORIGIN =
5620@kindex o =
5621@kindex org =
9cd6d51a
NC
5622The @var{origin} is an numerical expression for the start address of
5623the memory region. The expression must evaluate to a constant and it
5624cannot involve any symbols. The keyword @code{ORIGIN} may be
5625abbreviated to @code{org} or @code{o} (but not, for example,
5626@code{ORG}).
252b5132
RH
5627
5628@kindex LENGTH =
5629@kindex len =
5630@kindex l =
5631The @var{len} is an expression for the size in bytes of the memory
5632region. As with the @var{origin} expression, the expression must
9cd6d51a
NC
5633be numerical only and must evaluate to a constant. The keyword
5634@code{LENGTH} may be abbreviated to @code{len} or @code{l}.
252b5132
RH
5635
5636In the following example, we specify that there are two memory regions
5637available for allocation: one starting at @samp{0} for 256 kilobytes,
5638and the other starting at @samp{0x40000000} for four megabytes. The
5639linker will place into the @samp{rom} memory region every section which
5640is not explicitly mapped into a memory region, and is either read-only
5641or executable. The linker will place other sections which are not
5642explicitly mapped into a memory region into the @samp{ram} memory
5643region.
5644
5645@smallexample
5646@group
a1ab1d2a 5647MEMORY
252b5132
RH
5648 @{
5649 rom (rx) : ORIGIN = 0, LENGTH = 256K
5650 ram (!rx) : org = 0x40000000, l = 4M
5651 @}
5652@end group
5653@end smallexample
5654
5655Once you define a memory region, you can direct the linker to place
5656specific output sections into that memory region by using the
5657@samp{>@var{region}} output section attribute. For example, if you have
5658a memory region named @samp{mem}, you would use @samp{>mem} in the
5659output section definition. @xref{Output Section Region}. If no address
5660was specified for the output section, the linker will set the address to
5661the next available address within the memory region. If the combined
5662output sections directed to a memory region are too large for the
5663region, the linker will issue an error message.
5664
3ec57632 5665It is possible to access the origin and length of a memory in an
c0065db7 5666expression via the @code{ORIGIN(@var{memory})} and
3ec57632
NC
5667@code{LENGTH(@var{memory})} functions:
5668
5669@smallexample
5670@group
c0065db7 5671 _fstack = ORIGIN(ram) + LENGTH(ram) - 4;
3ec57632
NC
5672@end group
5673@end smallexample
5674
252b5132
RH
5675@node PHDRS
5676@section PHDRS Command
5677@kindex PHDRS
5678@cindex program headers
5679@cindex ELF program headers
5680@cindex program segments
5681@cindex segments, ELF
5682The ELF object file format uses @dfn{program headers}, also knows as
5683@dfn{segments}. The program headers describe how the program should be
5684loaded into memory. You can print them out by using the @code{objdump}
5685program with the @samp{-p} option.
5686
5687When you run an ELF program on a native ELF system, the system loader
5688reads the program headers in order to figure out how to load the
5689program. This will only work if the program headers are set correctly.
5690This manual does not describe the details of how the system loader
5691interprets program headers; for more information, see the ELF ABI.
5692
5693The linker will create reasonable program headers by default. However,
5694in some cases, you may need to specify the program headers more
5695precisely. You may use the @code{PHDRS} command for this purpose. When
5696the linker sees the @code{PHDRS} command in the linker script, it will
5697not create any program headers other than the ones specified.
5698
5699The linker only pays attention to the @code{PHDRS} command when
5700generating an ELF output file. In other cases, the linker will simply
5701ignore @code{PHDRS}.
5702
5703This is the syntax of the @code{PHDRS} command. The words @code{PHDRS},
5704@code{FILEHDR}, @code{AT}, and @code{FLAGS} are keywords.
5705
5706@smallexample
5707@group
5708PHDRS
5709@{
5710 @var{name} @var{type} [ FILEHDR ] [ PHDRS ] [ AT ( @var{address} ) ]
5711 [ FLAGS ( @var{flags} ) ] ;
5712@}
5713@end group
5714@end smallexample
5715
5716The @var{name} is used only for reference in the @code{SECTIONS} command
5717of the linker script. It is not put into the output file. Program
5718header names are stored in a separate name space, and will not conflict
5719with symbol names, file names, or section names. Each program header
5c1a3f0f
NS
5720must have a distinct name. The headers are processed in order and it
5721is usual for them to map to sections in ascending load address order.
252b5132
RH
5722
5723Certain program header types describe segments of memory which the
5724system loader will load from the file. In the linker script, you
5725specify the contents of these segments by placing allocatable output
5726sections in the segments. You use the @samp{:@var{phdr}} output section
5727attribute to place a section in a particular segment. @xref{Output
5728Section Phdr}.
5729
5730It is normal to put certain sections in more than one segment. This
5731merely implies that one segment of memory contains another. You may
5732repeat @samp{:@var{phdr}}, using it once for each segment which should
5733contain the section.
5734
5735If you place a section in one or more segments using @samp{:@var{phdr}},
5736then the linker will place all subsequent allocatable sections which do
5737not specify @samp{:@var{phdr}} in the same segments. This is for
5738convenience, since generally a whole set of contiguous sections will be
5739placed in a single segment. You can use @code{:NONE} to override the
5740default segment and tell the linker to not put the section in any
5741segment at all.
5742
5743@kindex FILEHDR
5744@kindex PHDRS
5c1a3f0f 5745You may use the @code{FILEHDR} and @code{PHDRS} keywords after
252b5132
RH
5746the program header type to further describe the contents of the segment.
5747The @code{FILEHDR} keyword means that the segment should include the ELF
5748file header. The @code{PHDRS} keyword means that the segment should
5c1a3f0f 5749include the ELF program headers themselves. If applied to a loadable
4100cea3
AM
5750segment (@code{PT_LOAD}), all prior loadable segments must have one of
5751these keywords.
252b5132
RH
5752
5753The @var{type} may be one of the following. The numbers indicate the
5754value of the keyword.
5755
5756@table @asis
5757@item @code{PT_NULL} (0)
5758Indicates an unused program header.
5759
5760@item @code{PT_LOAD} (1)
5761Indicates that this program header describes a segment to be loaded from
5762the file.
5763
5764@item @code{PT_DYNAMIC} (2)
5765Indicates a segment where dynamic linking information can be found.
5766
5767@item @code{PT_INTERP} (3)
5768Indicates a segment where the name of the program interpreter may be
5769found.
5770
5771@item @code{PT_NOTE} (4)
5772Indicates a segment holding note information.
5773
5774@item @code{PT_SHLIB} (5)
5775A reserved program header type, defined but not specified by the ELF
5776ABI.
5777
5778@item @code{PT_PHDR} (6)
5779Indicates a segment where the program headers may be found.
5780
1a9ccd70
NC
5781@item @code{PT_TLS} (7)
5782Indicates a segment containing thread local storage.
5783
252b5132
RH
5784@item @var{expression}
5785An expression giving the numeric type of the program header. This may
5786be used for types not defined above.
5787@end table
5788
5789You can specify that a segment should be loaded at a particular address
5790in memory by using an @code{AT} expression. This is identical to the
5791@code{AT} command used as an output section attribute (@pxref{Output
5792Section LMA}). The @code{AT} command for a program header overrides the
5793output section attribute.
5794
5795The linker will normally set the segment flags based on the sections
5796which comprise the segment. You may use the @code{FLAGS} keyword to
5797explicitly specify the segment flags. The value of @var{flags} must be
5798an integer. It is used to set the @code{p_flags} field of the program
5799header.
5800
5801Here is an example of @code{PHDRS}. This shows a typical set of program
5802headers used on a native ELF system.
5803
5804@example
5805@group
5806PHDRS
5807@{
5808 headers PT_PHDR PHDRS ;
5809 interp PT_INTERP ;
5810 text PT_LOAD FILEHDR PHDRS ;
5811 data PT_LOAD ;
5812 dynamic PT_DYNAMIC ;
5813@}
5814
5815SECTIONS
5816@{
5817 . = SIZEOF_HEADERS;
5818 .interp : @{ *(.interp) @} :text :interp
5819 .text : @{ *(.text) @} :text
5820 .rodata : @{ *(.rodata) @} /* defaults to :text */
5821 @dots{}
5822 . = . + 0x1000; /* move to a new page in memory */
5823 .data : @{ *(.data) @} :data
5824 .dynamic : @{ *(.dynamic) @} :data :dynamic
5825 @dots{}
5826@}
5827@end group
5828@end example
5829
5830@node VERSION
5831@section VERSION Command
5832@kindex VERSION @{script text@}
5833@cindex symbol versions
5834@cindex version script
5835@cindex versions of symbols
5836The linker supports symbol versions when using ELF. Symbol versions are
5837only useful when using shared libraries. The dynamic linker can use
5838symbol versions to select a specific version of a function when it runs
5839a program that may have been linked against an earlier version of the
5840shared library.
5841
5842You can include a version script directly in the main linker script, or
5843you can supply the version script as an implicit linker script. You can
5844also use the @samp{--version-script} linker option.
5845
5846The syntax of the @code{VERSION} command is simply
5847@smallexample
5848VERSION @{ version-script-commands @}
5849@end smallexample
5850
5851The format of the version script commands is identical to that used by
5852Sun's linker in Solaris 2.5. The version script defines a tree of
5853version nodes. You specify the node names and interdependencies in the
5854version script. You can specify which symbols are bound to which
5855version nodes, and you can reduce a specified set of symbols to local
5856scope so that they are not globally visible outside of the shared
5857library.
5858
5859The easiest way to demonstrate the version script language is with a few
5860examples.
5861
5862@smallexample
5863VERS_1.1 @{
5864 global:
5865 foo1;
5866 local:
a1ab1d2a
UD
5867 old*;
5868 original*;
5869 new*;
252b5132
RH
5870@};
5871
5872VERS_1.2 @{
5873 foo2;
5874@} VERS_1.1;
5875
5876VERS_2.0 @{
5877 bar1; bar2;
c0065db7 5878 extern "C++" @{
86043bbb 5879 ns::*;
bb1515f2
MF
5880 "f(int, double)";
5881 @};
252b5132
RH
5882@} VERS_1.2;
5883@end smallexample
5884
5885This example version script defines three version nodes. The first
5886version node defined is @samp{VERS_1.1}; it has no other dependencies.
5887The script binds the symbol @samp{foo1} to @samp{VERS_1.1}. It reduces
5888a number of symbols to local scope so that they are not visible outside
313e35ee
AM
5889of the shared library; this is done using wildcard patterns, so that any
5890symbol whose name begins with @samp{old}, @samp{original}, or @samp{new}
5891is matched. The wildcard patterns available are the same as those used
5892in the shell when matching filenames (also known as ``globbing'').
86043bbb
MM
5893However, if you specify the symbol name inside double quotes, then the
5894name is treated as literal, rather than as a glob pattern.
252b5132
RH
5895
5896Next, the version script defines node @samp{VERS_1.2}. This node
5897depends upon @samp{VERS_1.1}. The script binds the symbol @samp{foo2}
5898to the version node @samp{VERS_1.2}.
5899
5900Finally, the version script defines node @samp{VERS_2.0}. This node
5901depends upon @samp{VERS_1.2}. The scripts binds the symbols @samp{bar1}
5902and @samp{bar2} are bound to the version node @samp{VERS_2.0}.
5903
5904When the linker finds a symbol defined in a library which is not
5905specifically bound to a version node, it will effectively bind it to an
5906unspecified base version of the library. You can bind all otherwise
a981ed6f 5907unspecified symbols to a given version node by using @samp{global: *;}
ae5a3597
AM
5908somewhere in the version script. Note that it's slightly crazy to use
5909wildcards in a global spec except on the last version node. Global
5910wildcards elsewhere run the risk of accidentally adding symbols to the
5911set exported for an old version. That's wrong since older versions
5912ought to have a fixed set of symbols.
252b5132
RH
5913
5914The names of the version nodes have no specific meaning other than what
5915they might suggest to the person reading them. The @samp{2.0} version
5916could just as well have appeared in between @samp{1.1} and @samp{1.2}.
5917However, this would be a confusing way to write a version script.
5918
0f6bf451 5919Node name can be omitted, provided it is the only version node
6b9b879a
JJ
5920in the version script. Such version script doesn't assign any versions to
5921symbols, only selects which symbols will be globally visible out and which
5922won't.
5923
5924@smallexample
7c9c73be 5925@{ global: foo; bar; local: *; @};
9d201f2f 5926@end smallexample
6b9b879a 5927
252b5132
RH
5928When you link an application against a shared library that has versioned
5929symbols, the application itself knows which version of each symbol it
5930requires, and it also knows which version nodes it needs from each
5931shared library it is linked against. Thus at runtime, the dynamic
5932loader can make a quick check to make sure that the libraries you have
5933linked against do in fact supply all of the version nodes that the
5934application will need to resolve all of the dynamic symbols. In this
5935way it is possible for the dynamic linker to know with certainty that
5936all external symbols that it needs will be resolvable without having to
5937search for each symbol reference.
5938
5939The symbol versioning is in effect a much more sophisticated way of
5940doing minor version checking that SunOS does. The fundamental problem
5941that is being addressed here is that typically references to external
5942functions are bound on an as-needed basis, and are not all bound when
5943the application starts up. If a shared library is out of date, a
5944required interface may be missing; when the application tries to use
5945that interface, it may suddenly and unexpectedly fail. With symbol
5946versioning, the user will get a warning when they start their program if
5947the libraries being used with the application are too old.
5948
5949There are several GNU extensions to Sun's versioning approach. The
5950first of these is the ability to bind a symbol to a version node in the
5951source file where the symbol is defined instead of in the versioning
5952script. This was done mainly to reduce the burden on the library
5953maintainer. You can do this by putting something like:
5954@smallexample
5955__asm__(".symver original_foo,foo@@VERS_1.1");
5956@end smallexample
5957@noindent
5958in the C source file. This renames the function @samp{original_foo} to
5959be an alias for @samp{foo} bound to the version node @samp{VERS_1.1}.
5960The @samp{local:} directive can be used to prevent the symbol
96a94295
L
5961@samp{original_foo} from being exported. A @samp{.symver} directive
5962takes precedence over a version script.
252b5132
RH
5963
5964The second GNU extension is to allow multiple versions of the same
5965function to appear in a given shared library. In this way you can make
5966an incompatible change to an interface without increasing the major
5967version number of the shared library, while still allowing applications
5968linked against the old interface to continue to function.
5969
5970To do this, you must use multiple @samp{.symver} directives in the
5971source file. Here is an example:
5972
5973@smallexample
5974__asm__(".symver original_foo,foo@@");
5975__asm__(".symver old_foo,foo@@VERS_1.1");
5976__asm__(".symver old_foo1,foo@@VERS_1.2");
5977__asm__(".symver new_foo,foo@@@@VERS_2.0");
5978@end smallexample
5979
5980In this example, @samp{foo@@} represents the symbol @samp{foo} bound to the
5981unspecified base version of the symbol. The source file that contains this
5982example would define 4 C functions: @samp{original_foo}, @samp{old_foo},
5983@samp{old_foo1}, and @samp{new_foo}.
5984
5985When you have multiple definitions of a given symbol, there needs to be
5986some way to specify a default version to which external references to
5987this symbol will be bound. You can do this with the
5988@samp{foo@@@@VERS_2.0} type of @samp{.symver} directive. You can only
5989declare one version of a symbol as the default in this manner; otherwise
5990you would effectively have multiple definitions of the same symbol.
5991
5992If you wish to bind a reference to a specific version of the symbol
5993within the shared library, you can use the aliases of convenience
36f63dca 5994(i.e., @samp{old_foo}), or you can use the @samp{.symver} directive to
252b5132
RH
5995specifically bind to an external version of the function in question.
5996
cb840a31
L
5997You can also specify the language in the version script:
5998
5999@smallexample
6000VERSION extern "lang" @{ version-script-commands @}
6001@end smallexample
6002
c0065db7 6003The supported @samp{lang}s are @samp{C}, @samp{C++}, and @samp{Java}.
cb840a31
L
6004The linker will iterate over the list of symbols at the link time and
6005demangle them according to @samp{lang} before matching them to the
bb1515f2
MF
6006patterns specified in @samp{version-script-commands}. The default
6007@samp{lang} is @samp{C}.
cb840a31 6008
86043bbb
MM
6009Demangled names may contains spaces and other special characters. As
6010described above, you can use a glob pattern to match demangled names,
6011or you can use a double-quoted string to match the string exactly. In
6012the latter case, be aware that minor differences (such as differing
6013whitespace) between the version script and the demangler output will
6014cause a mismatch. As the exact string generated by the demangler
6015might change in the future, even if the mangled name does not, you
6016should check that all of your version directives are behaving as you
6017expect when you upgrade.
6018
252b5132
RH
6019@node Expressions
6020@section Expressions in Linker Scripts
6021@cindex expressions
6022@cindex arithmetic
6023The syntax for expressions in the linker script language is identical to
6024that of C expressions. All expressions are evaluated as integers. All
6025expressions are evaluated in the same size, which is 32 bits if both the
6026host and target are 32 bits, and is otherwise 64 bits.
6027
6028You can use and set symbol values in expressions.
6029
6030The linker defines several special purpose builtin functions for use in
6031expressions.
6032
6033@menu
6034* Constants:: Constants
0c71d759 6035* Symbolic Constants:: Symbolic constants
252b5132 6036* Symbols:: Symbol Names
ecca9871 6037* Orphan Sections:: Orphan Sections
252b5132
RH
6038* Location Counter:: The Location Counter
6039* Operators:: Operators
6040* Evaluation:: Evaluation
6041* Expression Section:: The Section of an Expression
6042* Builtin Functions:: Builtin Functions
6043@end menu
6044
6045@node Constants
6046@subsection Constants
6047@cindex integer notation
6048@cindex constants in linker scripts
6049All constants are integers.
6050
6051As in C, the linker considers an integer beginning with @samp{0} to be
6052octal, and an integer beginning with @samp{0x} or @samp{0X} to be
8a308ae8 6053hexadecimal. Alternatively the linker accepts suffixes of @samp{h} or
11e7fd74 6054@samp{H} for hexadecimal, @samp{o} or @samp{O} for octal, @samp{b} or
8a308ae8
NC
6055@samp{B} for binary and @samp{d} or @samp{D} for decimal. Any integer
6056value without a prefix or a suffix is considered to be decimal.
252b5132
RH
6057
6058@cindex scaled integers
6059@cindex K and M integer suffixes
6060@cindex M and K integer suffixes
6061@cindex suffixes for integers
6062@cindex integer suffixes
6063In addition, you can use the suffixes @code{K} and @code{M} to scale a
6064constant by
6065@c TEXI2ROFF-KILL
36f63dca 6066@ifnottex
252b5132
RH
6067@c END TEXI2ROFF-KILL
6068@code{1024} or @code{1024*1024}
6069@c TEXI2ROFF-KILL
36f63dca 6070@end ifnottex
252b5132
RH
6071@tex
6072${\rm 1024}$ or ${\rm 1024}^2$
6073@end tex
6074@c END TEXI2ROFF-KILL
8a308ae8
NC
6075respectively. For example, the following
6076all refer to the same quantity:
6077
252b5132 6078@smallexample
36f63dca
NC
6079_fourk_1 = 4K;
6080_fourk_2 = 4096;
6081_fourk_3 = 0x1000;
8a308ae8 6082_fourk_4 = 10000o;
252b5132
RH
6083@end smallexample
6084
8a308ae8
NC
6085Note - the @code{K} and @code{M} suffixes cannot be used in
6086conjunction with the base suffixes mentioned above.
6087
0c71d759
NC
6088@node Symbolic Constants
6089@subsection Symbolic Constants
6090@cindex symbolic constants
6091@kindex CONSTANT
a094d01f 6092It is possible to refer to target-specific constants via the use of
0c71d759
NC
6093the @code{CONSTANT(@var{name})} operator, where @var{name} is one of:
6094
6095@table @code
6096@item MAXPAGESIZE
6097@kindex MAXPAGESIZE
6098The target's maximum page size.
6099
6100@item COMMONPAGESIZE
6101@kindex COMMONPAGESIZE
6102The target's default page size.
6103@end table
6104
6105So for example:
6106
6107@smallexample
9d5777a3 6108 .text ALIGN (CONSTANT (MAXPAGESIZE)) : @{ *(.text) @}
0c71d759
NC
6109@end smallexample
6110
6111will create a text section aligned to the largest page boundary
6112supported by the target.
6113
252b5132
RH
6114@node Symbols
6115@subsection Symbol Names
6116@cindex symbol names
6117@cindex names
6118@cindex quoted symbol names
6119@kindex "
6120Unless quoted, symbol names start with a letter, underscore, or period
6121and may include letters, digits, underscores, periods, and hyphens.
6122Unquoted symbol names must not conflict with any keywords. You can
6123specify a symbol which contains odd characters or has the same name as a
6124keyword by surrounding the symbol name in double quotes:
6125@smallexample
36f63dca
NC
6126"SECTION" = 9;
6127"with a space" = "also with a space" + 10;
252b5132
RH
6128@end smallexample
6129
6130Since symbols can contain many non-alphabetic characters, it is safest
6131to delimit symbols with spaces. For example, @samp{A-B} is one symbol,
6132whereas @samp{A - B} is an expression involving subtraction.
6133
ecca9871
L
6134@node Orphan Sections
6135@subsection Orphan Sections
6136@cindex orphan
6137Orphan sections are sections present in the input files which
6138are not explicitly placed into the output file by the linker
6139script. The linker will still copy these sections into the
a87ded7b
AB
6140output file by either finding, or creating a suitable output section
6141in which to place the orphaned input section.
6142
6143If the name of an orphaned input section exactly matches the name of
6144an existing output section, then the orphaned input section will be
6145placed at the end of that output section.
6146
6147If there is no output section with a matching name then new output
6148sections will be created. Each new output section will have the same
6149name as the orphan section placed within it. If there are multiple
6150orphan sections with the same name, these will all be combined into
6151one new output section.
6152
6153If new output sections are created to hold orphaned input sections,
6154then the linker must decide where to place these new output sections
e299b355
AM
6155in relation to existing output sections. On most modern targets, the
6156linker attempts to place orphan sections after sections of the same
6157attribute, such as code vs data, loadable vs non-loadable, etc. If no
6158sections with matching attributes are found, or your target lacks this
6159support, the orphan section is placed at the end of the file.
ecca9871 6160
a05a5b64
TP
6161The command-line options @samp{--orphan-handling} and @samp{--unique}
6162(@pxref{Options,,Command-line Options}) can be used to control which
c005eb9e
AB
6163output sections an orphan is placed in.
6164
252b5132
RH
6165@node Location Counter
6166@subsection The Location Counter
6167@kindex .
6168@cindex dot
6169@cindex location counter
6170@cindex current output location
6171The special linker variable @dfn{dot} @samp{.} always contains the
6172current output location counter. Since the @code{.} always refers to a
6173location in an output section, it may only appear in an expression
6174within a @code{SECTIONS} command. The @code{.} symbol may appear
6175anywhere that an ordinary symbol is allowed in an expression.
6176
6177@cindex holes
6178Assigning a value to @code{.} will cause the location counter to be
6179moved. This may be used to create holes in the output section. The
dc0b6aa0
AM
6180location counter may not be moved backwards inside an output section,
6181and may not be moved backwards outside of an output section if so
6182doing creates areas with overlapping LMAs.
252b5132
RH
6183
6184@smallexample
6185SECTIONS
6186@{
6187 output :
6188 @{
6189 file1(.text)
6190 . = . + 1000;
6191 file2(.text)
6192 . += 1000;
6193 file3(.text)
563e308f 6194 @} = 0x12345678;
252b5132
RH
6195@}
6196@end smallexample
6197@noindent
6198In the previous example, the @samp{.text} section from @file{file1} is
6199located at the beginning of the output section @samp{output}. It is
6200followed by a 1000 byte gap. Then the @samp{.text} section from
6201@file{file2} appears, also with a 1000 byte gap following before the
563e308f 6202@samp{.text} section from @file{file3}. The notation @samp{= 0x12345678}
252b5132
RH
6203specifies what data to write in the gaps (@pxref{Output Section Fill}).
6204
5c6bbab8
NC
6205@cindex dot inside sections
6206Note: @code{.} actually refers to the byte offset from the start of the
6207current containing object. Normally this is the @code{SECTIONS}
69da35b5 6208statement, whose start address is 0, hence @code{.} can be used as an
5c6bbab8
NC
6209absolute address. If @code{.} is used inside a section description
6210however, it refers to the byte offset from the start of that section,
6211not an absolute address. Thus in a script like this:
6212
6213@smallexample
6214SECTIONS
6215@{
6216 . = 0x100
6217 .text: @{
6218 *(.text)
6219 . = 0x200
6220 @}
6221 . = 0x500
6222 .data: @{
6223 *(.data)
6224 . += 0x600
6225 @}
6226@}
6227@end smallexample
6228
6229The @samp{.text} section will be assigned a starting address of 0x100
6230and a size of exactly 0x200 bytes, even if there is not enough data in
6231the @samp{.text} input sections to fill this area. (If there is too
6232much data, an error will be produced because this would be an attempt to
6233move @code{.} backwards). The @samp{.data} section will start at 0x500
6234and it will have an extra 0x600 bytes worth of space after the end of
6235the values from the @samp{.data} input sections and before the end of
6236the @samp{.data} output section itself.
6237
b5666f2f
AM
6238@cindex dot outside sections
6239Setting symbols to the value of the location counter outside of an
6240output section statement can result in unexpected values if the linker
6241needs to place orphan sections. For example, given the following:
6242
6243@smallexample
6244SECTIONS
6245@{
6246 start_of_text = . ;
6247 .text: @{ *(.text) @}
6248 end_of_text = . ;
6249
6250 start_of_data = . ;
6251 .data: @{ *(.data) @}
6252 end_of_data = . ;
6253@}
6254@end smallexample
6255
6256If the linker needs to place some input section, e.g. @code{.rodata},
6257not mentioned in the script, it might choose to place that section
6258between @code{.text} and @code{.data}. You might think the linker
6259should place @code{.rodata} on the blank line in the above script, but
6260blank lines are of no particular significance to the linker. As well,
6261the linker doesn't associate the above symbol names with their
6262sections. Instead, it assumes that all assignments or other
6263statements belong to the previous output section, except for the
6264special case of an assignment to @code{.}. I.e., the linker will
6265place the orphan @code{.rodata} section as if the script was written
6266as follows:
6267
6268@smallexample
6269SECTIONS
6270@{
6271 start_of_text = . ;
6272 .text: @{ *(.text) @}
6273 end_of_text = . ;
6274
6275 start_of_data = . ;
6276 .rodata: @{ *(.rodata) @}
6277 .data: @{ *(.data) @}
6278 end_of_data = . ;
6279@}
6280@end smallexample
6281
6282This may or may not be the script author's intention for the value of
6283@code{start_of_data}. One way to influence the orphan section
6284placement is to assign the location counter to itself, as the linker
6285assumes that an assignment to @code{.} is setting the start address of
6286a following output section and thus should be grouped with that
6287section. So you could write:
6288
6289@smallexample
6290SECTIONS
6291@{
6292 start_of_text = . ;
6293 .text: @{ *(.text) @}
6294 end_of_text = . ;
6295
6296 . = . ;
6297 start_of_data = . ;
6298 .data: @{ *(.data) @}
6299 end_of_data = . ;
6300@}
6301@end smallexample
6302
6303Now, the orphan @code{.rodata} section will be placed between
6304@code{end_of_text} and @code{start_of_data}.
6305
252b5132
RH
6306@need 2000
6307@node Operators
6308@subsection Operators
6309@cindex operators for arithmetic
6310@cindex arithmetic operators
6311@cindex precedence in expressions
6312The linker recognizes the standard C set of arithmetic operators, with
6313the standard bindings and precedence levels:
6314@c TEXI2ROFF-KILL
36f63dca 6315@ifnottex
252b5132
RH
6316@c END TEXI2ROFF-KILL
6317@smallexample
6318precedence associativity Operators Notes
6319(highest)
63201 left ! - ~ (1)
63212 left * / %
63223 left + -
63234 left >> <<
63245 left == != > < <= >=
63256 left &
63267 left |
63278 left &&
63289 left ||
632910 right ? :
633011 right &= += -= *= /= (2)
6331(lowest)
6332@end smallexample
6333Notes:
a1ab1d2a 6334(1) Prefix operators
252b5132
RH
6335(2) @xref{Assignments}.
6336@c TEXI2ROFF-KILL
36f63dca 6337@end ifnottex
252b5132
RH
6338@tex
6339\vskip \baselineskip
6340%"lispnarrowing" is the extra indent used generally for smallexample
6341\hskip\lispnarrowing\vbox{\offinterlineskip
6342\hrule
6343\halign
6344{\vrule#&\strut\hfil\ #\ \hfil&\vrule#&\strut\hfil\ #\ \hfil&\vrule#&\strut\hfil\ {\tt #}\ \hfil&\vrule#\cr
6345height2pt&\omit&&\omit&&\omit&\cr
6346&Precedence&& Associativity &&{\rm Operators}&\cr
6347height2pt&\omit&&\omit&&\omit&\cr
6348\noalign{\hrule}
6349height2pt&\omit&&\omit&&\omit&\cr
6350&highest&&&&&\cr
6351% '176 is tilde, '~' in tt font
a1ab1d2a 6352&1&&left&&\qquad- \char'176\ !\qquad\dag&\cr
252b5132
RH
6353&2&&left&&* / \%&\cr
6354&3&&left&&+ -&\cr
6355&4&&left&&>> <<&\cr
6356&5&&left&&== != > < <= >=&\cr
6357&6&&left&&\&&\cr
6358&7&&left&&|&\cr
6359&8&&left&&{\&\&}&\cr
6360&9&&left&&||&\cr
6361&10&&right&&? :&\cr
6362&11&&right&&\qquad\&= += -= *= /=\qquad\ddag&\cr
6363&lowest&&&&&\cr
6364height2pt&\omit&&\omit&&\omit&\cr}
6365\hrule}
6366@end tex
6367@iftex
6368{
6369@obeylines@parskip=0pt@parindent=0pt
6370@dag@quad Prefix operators.
6371@ddag@quad @xref{Assignments}.
6372}
6373@end iftex
6374@c END TEXI2ROFF-KILL
6375
6376@node Evaluation
6377@subsection Evaluation
6378@cindex lazy evaluation
6379@cindex expression evaluation order
6380The linker evaluates expressions lazily. It only computes the value of
6381an expression when absolutely necessary.
6382
6383The linker needs some information, such as the value of the start
6384address of the first section, and the origins and lengths of memory
6385regions, in order to do any linking at all. These values are computed
6386as soon as possible when the linker reads in the linker script.
6387
6388However, other values (such as symbol values) are not known or needed
6389until after storage allocation. Such values are evaluated later, when
6390other information (such as the sizes of output sections) is available
6391for use in the symbol assignment expression.
6392
6393The sizes of sections cannot be known until after allocation, so
6394assignments dependent upon these are not performed until after
6395allocation.
6396
6397Some expressions, such as those depending upon the location counter
6398@samp{.}, must be evaluated during section allocation.
6399
6400If the result of an expression is required, but the value is not
6401available, then an error results. For example, a script like the
6402following
6403@smallexample
6404@group
6405SECTIONS
6406 @{
a1ab1d2a 6407 .text 9+this_isnt_constant :
252b5132
RH
6408 @{ *(.text) @}
6409 @}
6410@end group
6411@end smallexample
6412@noindent
6413will cause the error message @samp{non constant expression for initial
6414address}.
6415
6416@node Expression Section
6417@subsection The Section of an Expression
6418@cindex expression sections
6419@cindex absolute expressions
6420@cindex relative expressions
6421@cindex absolute and relocatable symbols
6422@cindex relocatable and absolute symbols
6423@cindex symbols, relocatable and absolute
7542af2a
AM
6424Addresses and symbols may be section relative, or absolute. A section
6425relative symbol is relocatable. If you request relocatable output
6426using the @samp{-r} option, a further link operation may change the
6427value of a section relative symbol. On the other hand, an absolute
6428symbol will retain the same value throughout any further link
6429operations.
6430
abf4be64
AM
6431Some terms in linker expressions are addresses. This is true of
6432section relative symbols and for builtin functions that return an
6433address, such as @code{ADDR}, @code{LOADADDR}, @code{ORIGIN} and
6434@code{SEGMENT_START}. Other terms are simply numbers, or are builtin
6435functions that return a non-address value, such as @code{LENGTH}.
01554a74
AM
6436One complication is that unless you set @code{LD_FEATURE ("SANE_EXPR")}
6437(@pxref{Miscellaneous Commands}), numbers and absolute symbols are treated
5c3049d2
AM
6438differently depending on their location, for compatibility with older
6439versions of @code{ld}. Expressions appearing outside an output
6440section definition treat all numbers as absolute addresses.
6441Expressions appearing inside an output section definition treat
01554a74
AM
6442absolute symbols as numbers. If @code{LD_FEATURE ("SANE_EXPR")} is
6443given, then absolute symbols and numbers are simply treated as numbers
6444everywhere.
5c3049d2
AM
6445
6446In the following simple example,
252b5132 6447
7542af2a
AM
6448@smallexample
6449@group
6450SECTIONS
6451 @{
6452 . = 0x100;
6453 __executable_start = 0x100;
6454 .data :
6455 @{
6456 . = 0x10;
6457 __data_start = 0x10;
6458 *(.data)
6459 @}
6460 @dots{}
6461 @}
6462@end group
6463@end smallexample
252b5132 6464
7542af2a
AM
6465both @code{.} and @code{__executable_start} are set to the absolute
6466address 0x100 in the first two assignments, then both @code{.} and
6467@code{__data_start} are set to 0x10 relative to the @code{.data}
6468section in the second two assignments.
252b5132 6469
5c3049d2
AM
6470For expressions involving numbers, relative addresses and absolute
6471addresses, ld follows these rules to evaluate terms:
7542af2a
AM
6472
6473@itemize @bullet
6474@item
c05f749e
AM
6475Unary operations on an absolute address or number, and binary
6476operations on two absolute addresses or two numbers, or between one
6477absolute address and a number, apply the operator to the value(s).
6478@item
7542af2a
AM
6479Unary operations on a relative address, and binary operations on two
6480relative addresses in the same section or between one relative address
6481and a number, apply the operator to the offset part of the address(es).
6482@item
c05f749e
AM
6483Other binary operations, that is, between two relative addresses not
6484in the same section, or between a relative address and an absolute
6485address, first convert any non-absolute term to an absolute address
6486before applying the operator.
7542af2a
AM
6487@end itemize
6488
6489The result section of each sub-expression is as follows:
6490
6491@itemize @bullet
6492@item
6493An operation involving only numbers results in a number.
6494@item
6495The result of comparisons, @samp{&&} and @samp{||} is also a number.
6496@item
9bc8bb33 6497The result of other binary arithmetic and logical operations on two
11e7fd74 6498relative addresses in the same section or two absolute addresses
94b41882
AM
6499(after above conversions) is also a number when
6500@code{LD_FEATURE ("SANE_EXPR")} or inside an output section definition
6501but an absolute address otherwise.
9bc8bb33
AM
6502@item
6503The result of other operations on relative addresses or one
6504relative address and a number, is a relative address in the same
6505section as the relative operand(s).
7542af2a
AM
6506@item
6507The result of other operations on absolute addresses (after above
6508conversions) is an absolute address.
6509@end itemize
252b5132
RH
6510
6511You can use the builtin function @code{ABSOLUTE} to force an expression
6512to be absolute when it would otherwise be relative. For example, to
6513create an absolute symbol set to the address of the end of the output
6514section @samp{.data}:
6515@smallexample
6516SECTIONS
6517 @{
6518 .data : @{ *(.data) _edata = ABSOLUTE(.); @}
6519 @}
6520@end smallexample
6521@noindent
6522If @samp{ABSOLUTE} were not used, @samp{_edata} would be relative to the
6523@samp{.data} section.
6524
7542af2a
AM
6525Using @code{LOADADDR} also forces an expression absolute, since this
6526particular builtin function returns an absolute address.
6527
252b5132
RH
6528@node Builtin Functions
6529@subsection Builtin Functions
6530@cindex functions in expressions
6531The linker script language includes a number of builtin functions for
6532use in linker script expressions.
6533
6534@table @code
6535@item ABSOLUTE(@var{exp})
6536@kindex ABSOLUTE(@var{exp})
6537@cindex expression, absolute
6538Return the absolute (non-relocatable, as opposed to non-negative) value
6539of the expression @var{exp}. Primarily useful to assign an absolute
6540value to a symbol within a section definition, where symbol values are
6541normally section relative. @xref{Expression Section}.
6542
6543@item ADDR(@var{section})
6544@kindex ADDR(@var{section})
6545@cindex section address in expression
7542af2a 6546Return the address (VMA) of the named @var{section}. Your
252b5132 6547script must previously have defined the location of that section. In
7542af2a
AM
6548the following example, @code{start_of_output_1}, @code{symbol_1} and
6549@code{symbol_2} are assigned equivalent values, except that
6550@code{symbol_1} will be relative to the @code{.output1} section while
6551the other two will be absolute:
252b5132
RH
6552@smallexample
6553@group
6554SECTIONS @{ @dots{}
6555 .output1 :
a1ab1d2a 6556 @{
252b5132
RH
6557 start_of_output_1 = ABSOLUTE(.);
6558 @dots{}
6559 @}
6560 .output :
6561 @{
6562 symbol_1 = ADDR(.output1);
6563 symbol_2 = start_of_output_1;
6564 @}
6565@dots{} @}
6566@end group
6567@end smallexample
6568
876f4090
NS
6569@item ALIGN(@var{align})
6570@itemx ALIGN(@var{exp},@var{align})
6571@kindex ALIGN(@var{align})
6572@kindex ALIGN(@var{exp},@var{align})
252b5132
RH
6573@cindex round up location counter
6574@cindex align location counter
876f4090
NS
6575@cindex round up expression
6576@cindex align expression
6577Return the location counter (@code{.}) or arbitrary expression aligned
6578to the next @var{align} boundary. The single operand @code{ALIGN}
6579doesn't change the value of the location counter---it just does
6580arithmetic on it. The two operand @code{ALIGN} allows an arbitrary
6581expression to be aligned upwards (@code{ALIGN(@var{align})} is
e0a3af22 6582equivalent to @code{ALIGN(ABSOLUTE(.), @var{align})}).
876f4090
NS
6583
6584Here is an example which aligns the output @code{.data} section to the
6585next @code{0x2000} byte boundary after the preceding section and sets a
6586variable within the section to the next @code{0x8000} boundary after the
6587input sections:
252b5132
RH
6588@smallexample
6589@group
6590SECTIONS @{ @dots{}
6591 .data ALIGN(0x2000): @{
6592 *(.data)
6593 variable = ALIGN(0x8000);
6594 @}
6595@dots{} @}
6596@end group
6597@end smallexample
6598@noindent
6599The first use of @code{ALIGN} in this example specifies the location of
6600a section because it is used as the optional @var{address} attribute of
6601a section definition (@pxref{Output Section Address}). The second use
6602of @code{ALIGN} is used to defines the value of a symbol.
6603
6604The builtin function @code{NEXT} is closely related to @code{ALIGN}.
6605
362c1d1a
NS
6606@item ALIGNOF(@var{section})
6607@kindex ALIGNOF(@var{section})
6608@cindex section alignment
6609Return the alignment in bytes of the named @var{section}, if that section has
6610been allocated. If the section has not been allocated when this is
6611evaluated, the linker will report an error. In the following example,
6612the alignment of the @code{.output} section is stored as the first
6613value in that section.
6614@smallexample
6615@group
6616SECTIONS@{ @dots{}
6617 .output @{
6618 LONG (ALIGNOF (.output))
6619 @dots{}
6620 @}
6621@dots{} @}
6622@end group
6623@end smallexample
6624
252b5132
RH
6625@item BLOCK(@var{exp})
6626@kindex BLOCK(@var{exp})
6627This is a synonym for @code{ALIGN}, for compatibility with older linker
6628scripts. It is most often seen when setting the address of an output
6629section.
6630
2d20f7bf
JJ
6631@item DATA_SEGMENT_ALIGN(@var{maxpagesize}, @var{commonpagesize})
6632@kindex DATA_SEGMENT_ALIGN(@var{maxpagesize}, @var{commonpagesize})
6633This is equivalent to either
6634@smallexample
6635(ALIGN(@var{maxpagesize}) + (. & (@var{maxpagesize} - 1)))
6636@end smallexample
6637or
6638@smallexample
fe6052e1
AM
6639(ALIGN(@var{maxpagesize})
6640 + ((. + @var{commonpagesize} - 1) & (@var{maxpagesize} - @var{commonpagesize})))
2d20f7bf
JJ
6641@end smallexample
6642@noindent
6643depending on whether the latter uses fewer @var{commonpagesize} sized pages
6644for the data segment (area between the result of this expression and
6645@code{DATA_SEGMENT_END}) than the former or not.
6646If the latter form is used, it means @var{commonpagesize} bytes of runtime
6647memory will be saved at the expense of up to @var{commonpagesize} wasted
6648bytes in the on-disk file.
6649
6650This expression can only be used directly in @code{SECTIONS} commands, not in
6651any output section descriptions and only once in the linker script.
6652@var{commonpagesize} should be less or equal to @var{maxpagesize} and should
def5c83c
AM
6653be the system page size the object wants to be optimized for while still
6654running on system page sizes up to @var{maxpagesize}. Note however
6655that @samp{-z relro} protection will not be effective if the system
6656page size is larger than @var{commonpagesize}.
2d20f7bf
JJ
6657
6658@noindent
6659Example:
6660@smallexample
6661 . = DATA_SEGMENT_ALIGN(0x10000, 0x2000);
6662@end smallexample
6663
6664@item DATA_SEGMENT_END(@var{exp})
6665@kindex DATA_SEGMENT_END(@var{exp})
6666This defines the end of data segment for @code{DATA_SEGMENT_ALIGN}
6667evaluation purposes.
6668
6669@smallexample
6670 . = DATA_SEGMENT_END(.);
6671@end smallexample
6672
a4f5ad88
JJ
6673@item DATA_SEGMENT_RELRO_END(@var{offset}, @var{exp})
6674@kindex DATA_SEGMENT_RELRO_END(@var{offset}, @var{exp})
6675This defines the end of the @code{PT_GNU_RELRO} segment when
eec2f3ed 6676@samp{-z relro} option is used.
a4f5ad88
JJ
6677When @samp{-z relro} option is not present, @code{DATA_SEGMENT_RELRO_END}
6678does nothing, otherwise @code{DATA_SEGMENT_ALIGN} is padded so that
def5c83c
AM
6679@var{exp} + @var{offset} is aligned to the @var{commonpagesize}
6680argument given to @code{DATA_SEGMENT_ALIGN}. If present in the linker
6681script, it must be placed between @code{DATA_SEGMENT_ALIGN} and
eec2f3ed
AM
6682@code{DATA_SEGMENT_END}. Evaluates to the second argument plus any
6683padding needed at the end of the @code{PT_GNU_RELRO} segment due to
6684section alignment.
a4f5ad88
JJ
6685
6686@smallexample
6687 . = DATA_SEGMENT_RELRO_END(24, .);
6688@end smallexample
6689
252b5132
RH
6690@item DEFINED(@var{symbol})
6691@kindex DEFINED(@var{symbol})
6692@cindex symbol defaults
6693Return 1 if @var{symbol} is in the linker global symbol table and is
420e579c
HPN
6694defined before the statement using DEFINED in the script, otherwise
6695return 0. You can use this function to provide
252b5132
RH
6696default values for symbols. For example, the following script fragment
6697shows how to set a global symbol @samp{begin} to the first location in
6698the @samp{.text} section---but if a symbol called @samp{begin} already
6699existed, its value is preserved:
6700
6701@smallexample
6702@group
6703SECTIONS @{ @dots{}
6704 .text : @{
6705 begin = DEFINED(begin) ? begin : . ;
6706 @dots{}
6707 @}
6708 @dots{}
6709@}
6710@end group
6711@end smallexample
6712
3ec57632
NC
6713@item LENGTH(@var{memory})
6714@kindex LENGTH(@var{memory})
6715Return the length of the memory region named @var{memory}.
6716
252b5132
RH
6717@item LOADADDR(@var{section})
6718@kindex LOADADDR(@var{section})
6719@cindex section load address in expression
7542af2a 6720Return the absolute LMA of the named @var{section}. (@pxref{Output
252b5132
RH
6721Section LMA}).
6722
2e53f7d6
NC
6723@item LOG2CEIL(@var{exp})
6724@kindex LOG2CEIL(@var{exp})
6725Return the binary logarithm of @var{exp} rounded towards infinity.
6726@code{LOG2CEIL(0)} returns 0.
6727
252b5132
RH
6728@kindex MAX
6729@item MAX(@var{exp1}, @var{exp2})
6730Returns the maximum of @var{exp1} and @var{exp2}.
6731
6732@kindex MIN
6733@item MIN(@var{exp1}, @var{exp2})
6734Returns the minimum of @var{exp1} and @var{exp2}.
6735
6736@item NEXT(@var{exp})
6737@kindex NEXT(@var{exp})
6738@cindex unallocated address, next
6739Return the next unallocated address that is a multiple of @var{exp}.
6740This function is closely related to @code{ALIGN(@var{exp})}; unless you
6741use the @code{MEMORY} command to define discontinuous memory for the
6742output file, the two functions are equivalent.
6743
3ec57632
NC
6744@item ORIGIN(@var{memory})
6745@kindex ORIGIN(@var{memory})
6746Return the origin of the memory region named @var{memory}.
6747
ba916c8a
MM
6748@item SEGMENT_START(@var{segment}, @var{default})
6749@kindex SEGMENT_START(@var{segment}, @var{default})
6750Return the base address of the named @var{segment}. If an explicit
c5da8c7d
NC
6751value has already been given for this segment (with a command-line
6752@samp{-T} option) then that value will be returned otherwise the value
6753will be @var{default}. At present, the @samp{-T} command-line option
6754can only be used to set the base address for the ``text'', ``data'', and
7542af2a 6755``bss'' sections, but you can use @code{SEGMENT_START} with any segment
ba916c8a
MM
6756name.
6757
252b5132
RH
6758@item SIZEOF(@var{section})
6759@kindex SIZEOF(@var{section})
6760@cindex section size
6761Return the size in bytes of the named @var{section}, if that section has
6762been allocated. If the section has not been allocated when this is
6763evaluated, the linker will report an error. In the following example,
6764@code{symbol_1} and @code{symbol_2} are assigned identical values:
6765@smallexample
6766@group
6767SECTIONS@{ @dots{}
6768 .output @{
6769 .start = . ;
6770 @dots{}
6771 .end = . ;
6772 @}
6773 symbol_1 = .end - .start ;
6774 symbol_2 = SIZEOF(.output);
6775@dots{} @}
6776@end group
6777@end smallexample
6778
6779@item SIZEOF_HEADERS
6780@itemx sizeof_headers
6781@kindex SIZEOF_HEADERS
6782@cindex header size
6783Return the size in bytes of the output file's headers. This is
6784information which appears at the start of the output file. You can use
6785this number when setting the start address of the first section, if you
6786choose, to facilitate paging.
6787
6788@cindex not enough room for program headers
6789@cindex program headers, not enough room
6790When producing an ELF output file, if the linker script uses the
6791@code{SIZEOF_HEADERS} builtin function, the linker must compute the
6792number of program headers before it has determined all the section
6793addresses and sizes. If the linker later discovers that it needs
6794additional program headers, it will report an error @samp{not enough
6795room for program headers}. To avoid this error, you must avoid using
6796the @code{SIZEOF_HEADERS} function, or you must rework your linker
6797script to avoid forcing the linker to use additional program headers, or
6798you must define the program headers yourself using the @code{PHDRS}
6799command (@pxref{PHDRS}).
6800@end table
6801
6802@node Implicit Linker Scripts
6803@section Implicit Linker Scripts
6804@cindex implicit linker scripts
6805If you specify a linker input file which the linker can not recognize as
6806an object file or an archive file, it will try to read the file as a
6807linker script. If the file can not be parsed as a linker script, the
6808linker will report an error.
6809
6810An implicit linker script will not replace the default linker script.
6811
6812Typically an implicit linker script would contain only symbol
6813assignments, or the @code{INPUT}, @code{GROUP}, or @code{VERSION}
6814commands.
6815
6816Any input files read because of an implicit linker script will be read
6817at the position in the command line where the implicit linker script was
6818read. This can affect archive searching.
6819
6820@ifset GENERIC
6821@node Machine Dependent
6822@chapter Machine Dependent Features
6823
6824@cindex machine dependencies
ff5dcc92
SC
6825@command{ld} has additional features on some platforms; the following
6826sections describe them. Machines where @command{ld} has no additional
252b5132
RH
6827functionality are not listed.
6828
6829@menu
36f63dca
NC
6830@ifset H8300
6831* H8/300:: @command{ld} and the H8/300
6832@end ifset
7ca01ed9
NC
6833@ifset M68HC11
6834* M68HC11/68HC12:: @code{ld} and the Motorola 68HC11 and 68HC12 families
6835@end ifset
36f63dca
NC
6836@ifset ARM
6837* ARM:: @command{ld} and the ARM family
6838@end ifset
6839@ifset HPPA
6840* HPPA ELF32:: @command{ld} and HPPA 32-bit ELF
6841@end ifset
7fb9f789
NC
6842@ifset M68K
6843* M68K:: @command{ld} and the Motorola 68K family
6844@end ifset
833794fc
MR
6845@ifset MIPS
6846* MIPS:: @command{ld} and the MIPS family
6847@end ifset
3c3bdf30 6848@ifset MMIX
36f63dca 6849* MMIX:: @command{ld} and MMIX
3c3bdf30 6850@end ifset
2469cfa2 6851@ifset MSP430
36f63dca 6852* MSP430:: @command{ld} and MSP430
2469cfa2 6853@end ifset
35c08157
KLC
6854@ifset NDS32
6855* NDS32:: @command{ld} and NDS32
6856@end ifset
78058a5e
SL
6857@ifset NIOSII
6858* Nios II:: @command{ld} and the Altera Nios II
6859@end ifset
2a60a7a8
AM
6860@ifset POWERPC
6861* PowerPC ELF32:: @command{ld} and PowerPC 32-bit ELF Support
6862@end ifset
6863@ifset POWERPC64
6864* PowerPC64 ELF64:: @command{ld} and PowerPC64 64-bit ELF Support
6865@end ifset
b4cbbe8f
AK
6866@ifset S/390
6867* S/390 ELF:: @command{ld} and S/390 ELF Support
6868@end ifset
49fa1e15
AM
6869@ifset SPU
6870* SPU ELF:: @command{ld} and SPU ELF Support
6871@end ifset
74459f0e 6872@ifset TICOFF
ff5dcc92 6873* TI COFF:: @command{ld} and TI COFF
74459f0e 6874@end ifset
2ca22b03
NC
6875@ifset WIN32
6876* WIN32:: @command{ld} and WIN32 (cygwin/mingw)
6877@end ifset
e0001a05
NC
6878@ifset XTENSA
6879* Xtensa:: @command{ld} and Xtensa Processors
6880@end ifset
252b5132
RH
6881@end menu
6882@end ifset
6883
252b5132
RH
6884@ifset H8300
6885@ifclear GENERIC
6886@raisesections
6887@end ifclear
6888
6889@node H8/300
ff5dcc92 6890@section @command{ld} and the H8/300
252b5132
RH
6891
6892@cindex H8/300 support
ff5dcc92 6893For the H8/300, @command{ld} can perform these global optimizations when
252b5132
RH
6894you specify the @samp{--relax} command-line option.
6895
6896@table @emph
6897@cindex relaxing on H8/300
6898@item relaxing address modes
ff5dcc92 6899@command{ld} finds all @code{jsr} and @code{jmp} instructions whose
252b5132
RH
6900targets are within eight bits, and turns them into eight-bit
6901program-counter relative @code{bsr} and @code{bra} instructions,
6902respectively.
6903
6904@cindex synthesizing on H8/300
6905@item synthesizing instructions
81f5558e 6906@c FIXME: specifically mov.b, or any mov instructions really? -> mov.b only, at least on H8, H8H, H8S
ff5dcc92 6907@command{ld} finds all @code{mov.b} instructions which use the
252b5132
RH
6908sixteen-bit absolute address form, but refer to the top
6909page of memory, and changes them to use the eight-bit address form.
6910(That is: the linker turns @samp{mov.b @code{@@}@var{aa}:16} into
6911@samp{mov.b @code{@@}@var{aa}:8} whenever the address @var{aa} is in the
6912top page of memory).
1502569c 6913
81f5558e
NC
6914@command{ld} finds all @code{mov} instructions which use the register
6915indirect with 32-bit displacement addressing mode, but use a small
6916displacement inside 16-bit displacement range, and changes them to use
6917the 16-bit displacement form. (That is: the linker turns @samp{mov.b
6918@code{@@}@var{d}:32,ERx} into @samp{mov.b @code{@@}@var{d}:16,ERx}
6919whenever the displacement @var{d} is in the 16 bit signed integer
6920range. Only implemented in ELF-format ld).
6921
1502569c 6922@item bit manipulation instructions
c0065db7 6923@command{ld} finds all bit manipulation instructions like @code{band, bclr,
1502569c 6924biand, bild, bior, bist, bixor, bld, bnot, bor, bset, bst, btst, bxor}
c0065db7 6925which use 32 bit and 16 bit absolute address form, but refer to the top
1502569c
NC
6926page of memory, and changes them to use the 8 bit address form.
6927(That is: the linker turns @samp{bset #xx:3,@code{@@}@var{aa}:32} into
c0065db7 6928@samp{bset #xx:3,@code{@@}@var{aa}:8} whenever the address @var{aa} is in
1502569c
NC
6929the top page of memory).
6930
6931@item system control instructions
c0065db7
RM
6932@command{ld} finds all @code{ldc.w, stc.w} instructions which use the
693332 bit absolute address form, but refer to the top page of memory, and
1502569c
NC
6934changes them to use 16 bit address form.
6935(That is: the linker turns @samp{ldc.w @code{@@}@var{aa}:32,ccr} into
c0065db7 6936@samp{ldc.w @code{@@}@var{aa}:16,ccr} whenever the address @var{aa} is in
1502569c 6937the top page of memory).
252b5132
RH
6938@end table
6939
6940@ifclear GENERIC
6941@lowersections
6942@end ifclear
6943@end ifset
6944
36f63dca 6945@ifclear GENERIC
c2dcd04e 6946@ifset Renesas
36f63dca 6947@c This stuff is pointless to say unless you're especially concerned
c2dcd04e
NC
6948@c with Renesas chips; don't enable it for generic case, please.
6949@node Renesas
6950@chapter @command{ld} and Other Renesas Chips
36f63dca 6951
c2dcd04e
NC
6952@command{ld} also supports the Renesas (formerly Hitachi) H8/300H,
6953H8/500, and SH chips. No special features, commands, or command-line
6954options are required for these chips.
36f63dca
NC
6955@end ifset
6956@end ifclear
6957
36f63dca
NC
6958@ifset ARM
6959@ifclear GENERIC
6960@raisesections
6961@end ifclear
6962
93fd0973
SC
6963@ifset M68HC11
6964@ifclear GENERIC
6965@raisesections
6966@end ifclear
6967
6968@node M68HC11/68HC12
6969@section @command{ld} and the Motorola 68HC11 and 68HC12 families
6970
6971@cindex M68HC11 and 68HC12 support
6972
6973@subsection Linker Relaxation
6974
6975For the Motorola 68HC11, @command{ld} can perform these global
6976optimizations when you specify the @samp{--relax} command-line option.
6977
6978@table @emph
6979@cindex relaxing on M68HC11
6980@item relaxing address modes
6981@command{ld} finds all @code{jsr} and @code{jmp} instructions whose
6982targets are within eight bits, and turns them into eight-bit
6983program-counter relative @code{bsr} and @code{bra} instructions,
6984respectively.
6985
6986@command{ld} also looks at all 16-bit extended addressing modes and
6987transforms them in a direct addressing mode when the address is in
6988page 0 (between 0 and 0x0ff).
6989
6990@item relaxing gcc instruction group
6991When @command{gcc} is called with @option{-mrelax}, it can emit group
6992of instructions that the linker can optimize to use a 68HC11 direct
6993addressing mode. These instructions consists of @code{bclr} or
6994@code{bset} instructions.
6995
6996@end table
6997
6998@subsection Trampoline Generation
6999
7000@cindex trampoline generation on M68HC11
7001@cindex trampoline generation on M68HC12
7002For 68HC11 and 68HC12, @command{ld} can generate trampoline code to
7003call a far function using a normal @code{jsr} instruction. The linker
c0065db7 7004will also change the relocation to some far function to use the
93fd0973
SC
7005trampoline address instead of the function address. This is typically the
7006case when a pointer to a function is taken. The pointer will in fact
7007point to the function trampoline.
7008
7009@ifclear GENERIC
7010@lowersections
7011@end ifclear
7012@end ifset
7013
36f63dca 7014@node ARM
3674e28a 7015@section @command{ld} and the ARM family
36f63dca
NC
7016
7017@cindex ARM interworking support
7018@kindex --support-old-code
7019For the ARM, @command{ld} will generate code stubs to allow functions calls
b45619c0 7020between ARM and Thumb code. These stubs only work with code that has
36f63dca
NC
7021been compiled and assembled with the @samp{-mthumb-interwork} command
7022line option. If it is necessary to link with old ARM object files or
7023libraries, which have not been compiled with the -mthumb-interwork
a05a5b64 7024option then the @samp{--support-old-code} command-line switch should be
36f63dca
NC
7025given to the linker. This will make it generate larger stub functions
7026which will work with non-interworking aware ARM code. Note, however,
7027the linker does not support generating stubs for function calls to
7028non-interworking aware Thumb code.
7029
7030@cindex thumb entry point
7031@cindex entry point, thumb
7032@kindex --thumb-entry=@var{entry}
7033The @samp{--thumb-entry} switch is a duplicate of the generic
7034@samp{--entry} switch, in that it sets the program's starting address.
7035But it also sets the bottom bit of the address, so that it can be
7036branched to using a BX instruction, and the program will start
7037executing in Thumb mode straight away.
7038
ce11ba6c
KT
7039@cindex PE import table prefixing
7040@kindex --use-nul-prefixed-import-tables
7041The @samp{--use-nul-prefixed-import-tables} switch is specifying, that
7042the import tables idata4 and idata5 have to be generated with a zero
11e7fd74 7043element prefix for import libraries. This is the old style to generate
ce11ba6c
KT
7044import tables. By default this option is turned off.
7045
e489d0ae
PB
7046@cindex BE8
7047@kindex --be8
7048The @samp{--be8} switch instructs @command{ld} to generate BE8 format
080bb7bb
NC
7049executables. This option is only valid when linking big-endian
7050objects - ie ones which have been assembled with the @option{-EB}
7051option. The resulting image will contain big-endian data and
7052little-endian code.
e489d0ae 7053
3674e28a
PB
7054@cindex TARGET1
7055@kindex --target1-rel
7056@kindex --target1-abs
7057The @samp{R_ARM_TARGET1} relocation is typically used for entries in the
7058@samp{.init_array} section. It is interpreted as either @samp{R_ARM_REL32}
7059or @samp{R_ARM_ABS32}, depending on the target. The @samp{--target1-rel}
7060and @samp{--target1-abs} switches override the default.
7061
7062@cindex TARGET2
7063@kindex --target2=@var{type}
7064The @samp{--target2=type} switch overrides the default definition of the
7065@samp{R_ARM_TARGET2} relocation. Valid values for @samp{type}, their
7066meanings, and target defaults are as follows:
7067@table @samp
7068@item rel
eeac373a
PB
7069@samp{R_ARM_REL32} (arm*-*-elf, arm*-*-eabi)
7070@item abs
7071@samp{R_ARM_ABS32} (arm*-*-symbianelf)
3674e28a
PB
7072@item got-rel
7073@samp{R_ARM_GOT_PREL} (arm*-*-linux, arm*-*-*bsd)
7074@end table
7075
319850b4
JB
7076@cindex FIX_V4BX
7077@kindex --fix-v4bx
7078The @samp{R_ARM_V4BX} relocation (defined by the ARM AAELF
7079specification) enables objects compiled for the ARMv4 architecture to be
7080interworking-safe when linked with other objects compiled for ARMv4t, but
7081also allows pure ARMv4 binaries to be built from the same ARMv4 objects.
7082
7083In the latter case, the switch @option{--fix-v4bx} must be passed to the
7084linker, which causes v4t @code{BX rM} instructions to be rewritten as
7085@code{MOV PC,rM}, since v4 processors do not have a @code{BX} instruction.
7086
7087In the former case, the switch should not be used, and @samp{R_ARM_V4BX}
7088relocations are ignored.
7089
845b51d6
PB
7090@cindex FIX_V4BX_INTERWORKING
7091@kindex --fix-v4bx-interworking
7092Replace @code{BX rM} instructions identified by @samp{R_ARM_V4BX}
7093relocations with a branch to the following veneer:
7094
7095@smallexample
7096TST rM, #1
7097MOVEQ PC, rM
7098BX Rn
7099@end smallexample
7100
7101This allows generation of libraries/applications that work on ARMv4 cores
7102and are still interworking safe. Note that the above veneer clobbers the
11e7fd74 7103condition flags, so may cause incorrect program behavior in rare cases.
845b51d6 7104
33bfe774
JB
7105@cindex USE_BLX
7106@kindex --use-blx
7107The @samp{--use-blx} switch enables the linker to use ARM/Thumb
7108BLX instructions (available on ARMv5t and above) in various
7109situations. Currently it is used to perform calls via the PLT from Thumb
7110code using BLX rather than using BX and a mode-switching stub before
7111each PLT entry. This should lead to such calls executing slightly faster.
7112
7113This option is enabled implicitly for SymbianOS, so there is no need to
7114specify it if you are using that target.
7115
c6dd86c6
JB
7116@cindex VFP11_DENORM_FIX
7117@kindex --vfp11-denorm-fix
7118The @samp{--vfp11-denorm-fix} switch enables a link-time workaround for a
7119bug in certain VFP11 coprocessor hardware, which sometimes allows
7120instructions with denorm operands (which must be handled by support code)
7121to have those operands overwritten by subsequent instructions before
7122the support code can read the intended values.
7123
7124The bug may be avoided in scalar mode if you allow at least one
7125intervening instruction between a VFP11 instruction which uses a register
7126and another instruction which writes to the same register, or at least two
7127intervening instructions if vector mode is in use. The bug only affects
7128full-compliance floating-point mode: you do not need this workaround if
7129you are using "runfast" mode. Please contact ARM for further details.
7130
7131If you know you are using buggy VFP11 hardware, you can
7132enable this workaround by specifying the linker option
7133@samp{--vfp-denorm-fix=scalar} if you are using the VFP11 scalar
7134mode only, or @samp{--vfp-denorm-fix=vector} if you are using
7135vector mode (the latter also works for scalar code). The default is
7136@samp{--vfp-denorm-fix=none}.
7137
7138If the workaround is enabled, instructions are scanned for
7139potentially-troublesome sequences, and a veneer is created for each
7140such sequence which may trigger the erratum. The veneer consists of the
7141first instruction of the sequence and a branch back to the subsequent
7142instruction. The original instruction is then replaced with a branch to
7143the veneer. The extra cycles required to call and return from the veneer
7144are sufficient to avoid the erratum in both the scalar and vector cases.
7145
2de70689
MGD
7146@cindex ARM1176 erratum workaround
7147@kindex --fix-arm1176
7148@kindex --no-fix-arm1176
9d5777a3
RM
7149The @samp{--fix-arm1176} switch enables a link-time workaround for an erratum
7150in certain ARM1176 processors. The workaround is enabled by default if you
7151are targeting ARM v6 (excluding ARM v6T2) or earlier. It can be disabled
2de70689
MGD
7152unconditionally by specifying @samp{--no-fix-arm1176}.
7153
9d5777a3 7154Further information is available in the ``ARM1176JZ-S and ARM1176JZF-S
11e7fd74 7155Programmer Advice Notice'' available on the ARM documentation website at:
2de70689
MGD
7156http://infocenter.arm.com/.
7157
a504d23a
LA
7158@cindex STM32L4xx erratum workaround
7159@kindex --fix-stm32l4xx-629360
7160
7161The @samp{--fix-stm32l4xx-629360} switch enables a link-time
7162workaround for a bug in the bus matrix / memory controller for some of
7163the STM32 Cortex-M4 based products (STM32L4xx). When accessing
7164off-chip memory via the affected bus for bus reads of 9 words or more,
7165the bus can generate corrupt data and/or abort. These are only
7166core-initiated accesses (not DMA), and might affect any access:
7167integer loads such as LDM, POP and floating-point loads such as VLDM,
7168VPOP. Stores are not affected.
7169
7170The bug can be avoided by splitting memory accesses into the
7171necessary chunks to keep bus reads below 8 words.
7172
7173The workaround is not enabled by default, this is equivalent to use
7174@samp{--fix-stm32l4xx-629360=none}. If you know you are using buggy
7175STM32L4xx hardware, you can enable the workaround by specifying the
7176linker option @samp{--fix-stm32l4xx-629360}, or the equivalent
7177@samp{--fix-stm32l4xx-629360=default}.
7178
7179If the workaround is enabled, instructions are scanned for
7180potentially-troublesome sequences, and a veneer is created for each
7181such sequence which may trigger the erratum. The veneer consists in a
7182replacement sequence emulating the behaviour of the original one and a
7183branch back to the subsequent instruction. The original instruction is
7184then replaced with a branch to the veneer.
7185
7186The workaround does not always preserve the memory access order for
7187the LDMDB instruction, when the instruction loads the PC.
7188
7189The workaround is not able to handle problematic instructions when
7190they are in the middle of an IT block, since a branch is not allowed
7191there. In that case, the linker reports a warning and no replacement
7192occurs.
7193
7194The workaround is not able to replace problematic instructions with a
7195PC-relative branch instruction if the @samp{.text} section is too
7196large. In that case, when the branch that replaces the original code
7197cannot be encoded, the linker reports a warning and no replacement
7198occurs.
7199
bf21ed78
MS
7200@cindex NO_ENUM_SIZE_WARNING
7201@kindex --no-enum-size-warning
726150b7 7202The @option{--no-enum-size-warning} switch prevents the linker from
bf21ed78
MS
7203warning when linking object files that specify incompatible EABI
7204enumeration size attributes. For example, with this switch enabled,
7205linking of an object file using 32-bit enumeration values with another
7206using enumeration values fitted into the smallest possible space will
7207not be diagnosed.
a9dc9481
JM
7208
7209@cindex NO_WCHAR_SIZE_WARNING
7210@kindex --no-wchar-size-warning
7211The @option{--no-wchar-size-warning} switch prevents the linker from
7212warning when linking object files that specify incompatible EABI
7213@code{wchar_t} size attributes. For example, with this switch enabled,
7214linking of an object file using 32-bit @code{wchar_t} values with another
7215using 16-bit @code{wchar_t} values will not be diagnosed.
bf21ed78 7216
726150b7
NC
7217@cindex PIC_VENEER
7218@kindex --pic-veneer
7219The @samp{--pic-veneer} switch makes the linker use PIC sequences for
7220ARM/Thumb interworking veneers, even if the rest of the binary
7221is not PIC. This avoids problems on uClinux targets where
7222@samp{--emit-relocs} is used to generate relocatable binaries.
7223
7224@cindex STUB_GROUP_SIZE
7225@kindex --stub-group-size=@var{N}
7226The linker will automatically generate and insert small sequences of
7227code into a linked ARM ELF executable whenever an attempt is made to
7228perform a function call to a symbol that is too far away. The
7229placement of these sequences of instructions - called stubs - is
a05a5b64 7230controlled by the command-line option @option{--stub-group-size=N}.
726150b7 7231The placement is important because a poor choice can create a need for
11e7fd74 7232duplicate stubs, increasing the code size. The linker will try to
726150b7
NC
7233group stubs together in order to reduce interruptions to the flow of
7234code, but it needs guidance as to how big these groups should be and
7235where they should be placed.
7236
7237The value of @samp{N}, the parameter to the
7238@option{--stub-group-size=} option controls where the stub groups are
07d72278 7239placed. If it is negative then all stubs are placed after the first
726150b7
NC
7240branch that needs them. If it is positive then the stubs can be
7241placed either before or after the branches that need them. If the
7242value of @samp{N} is 1 (either +1 or -1) then the linker will choose
7243exactly where to place groups of stubs, using its built in heuristics.
7244A value of @samp{N} greater than 1 (or smaller than -1) tells the
7245linker that a single group of stubs can service at most @samp{N} bytes
7246from the input sections.
7247
7248The default, if @option{--stub-group-size=} is not specified, is
7249@samp{N = +1}.
7250
1a51c1a4
NC
7251Farcalls stubs insertion is fully supported for the ARM-EABI target
7252only, because it relies on object files properties not present
7253otherwise.
7254
1db37fe6
YG
7255@cindex Cortex-A8 erratum workaround
7256@kindex --fix-cortex-a8
7257@kindex --no-fix-cortex-a8
7258The @samp{--fix-cortex-a8} switch enables a link-time workaround for an erratum in certain Cortex-A8 processors. The workaround is enabled by default if you are targeting the ARM v7-A architecture profile. It can be enabled otherwise by specifying @samp{--fix-cortex-a8}, or disabled unconditionally by specifying @samp{--no-fix-cortex-a8}.
7259
7260The erratum only affects Thumb-2 code. Please contact ARM for further details.
7261
68fcca92
JW
7262@cindex Cortex-A53 erratum 835769 workaround
7263@kindex --fix-cortex-a53-835769
7264@kindex --no-fix-cortex-a53-835769
7265The @samp{--fix-cortex-a53-835769} switch enables a link-time workaround for erratum 835769 present on certain early revisions of Cortex-A53 processors. The workaround is disabled by default. It can be enabled by specifying @samp{--fix-cortex-a53-835769}, or disabled unconditionally by specifying @samp{--no-fix-cortex-a53-835769}.
7266
7267Please contact ARM for further details.
7268
1db37fe6
YG
7269@kindex --merge-exidx-entries
7270@kindex --no-merge-exidx-entries
7271@cindex Merging exidx entries
7272The @samp{--no-merge-exidx-entries} switch disables the merging of adjacent exidx entries in debuginfo.
7273
7274@kindex --long-plt
7275@cindex 32-bit PLT entries
7276The @samp{--long-plt} option enables the use of 16 byte PLT entries
7277which support up to 4Gb of code. The default is to use 12 byte PLT
7278entries which only support 512Mb of code.
7279
1f56df9d
JW
7280@kindex --no-apply-dynamic-relocs
7281@cindex AArch64 rela addend
7282The @samp{--no-apply-dynamic-relocs} option makes AArch64 linker do not apply
7283link-time values for dynamic relocations.
7284
4ba2ef8f
TP
7285@cindex Placement of SG veneers
7286All SG veneers are placed in the special output section @code{.gnu.sgstubs}.
a05a5b64 7287Its start address must be set, either with the command-line option
4ba2ef8f
TP
7288@samp{--section-start} or in a linker script, to indicate where to place these
7289veneers in memory.
7290
54ddd295
TP
7291@kindex --cmse-implib
7292@cindex Secure gateway import library
7293The @samp{--cmse-implib} option requests that the import libraries
7294specified by the @samp{--out-implib} and @samp{--in-implib} options are
7295secure gateway import libraries, suitable for linking a non-secure
7296executable against secure code as per ARMv8-M Security Extensions.
7297
0955507f
TP
7298@kindex --in-implib=@var{file}
7299@cindex Input import library
7300The @samp{--in-implib=file} specifies an input import library whose symbols
7301must keep the same address in the executable being produced. A warning is
7302given if no @samp{--out-implib} is given but new symbols have been introduced
7303in the executable that should be listed in its import library. Otherwise, if
7304@samp{--out-implib} is specified, the symbols are added to the output import
7305library. A warning is also given if some symbols present in the input import
7306library have disappeared from the executable. This option is only effective
7307for Secure Gateway import libraries, ie. when @samp{--cmse-implib} is
7308specified.
7309
36f63dca
NC
7310@ifclear GENERIC
7311@lowersections
7312@end ifclear
7313@end ifset
7314
7315@ifset HPPA
7316@ifclear GENERIC
7317@raisesections
7318@end ifclear
7319
7320@node HPPA ELF32
7321@section @command{ld} and HPPA 32-bit ELF Support
7322@cindex HPPA multiple sub-space stubs
7323@kindex --multi-subspace
7324When generating a shared library, @command{ld} will by default generate
7325import stubs suitable for use with a single sub-space application.
7326The @samp{--multi-subspace} switch causes @command{ld} to generate export
7327stubs, and different (larger) import stubs suitable for use with
7328multiple sub-spaces.
7329
7330@cindex HPPA stub grouping
7331@kindex --stub-group-size=@var{N}
7332Long branch stubs and import/export stubs are placed by @command{ld} in
7333stub sections located between groups of input sections.
7334@samp{--stub-group-size} specifies the maximum size of a group of input
7335sections handled by one stub section. Since branch offsets are signed,
7336a stub section may serve two groups of input sections, one group before
7337the stub section, and one group after it. However, when using
7338conditional branches that require stubs, it may be better (for branch
7339prediction) that stub sections only serve one group of input sections.
7340A negative value for @samp{N} chooses this scheme, ensuring that
7341branches to stubs always use a negative offset. Two special values of
7342@samp{N} are recognized, @samp{1} and @samp{-1}. These both instruct
7343@command{ld} to automatically size input section groups for the branch types
7344detected, with the same behaviour regarding stub placement as other
7345positive or negative values of @samp{N} respectively.
7346
7347Note that @samp{--stub-group-size} does not split input sections. A
7348single input section larger than the group size specified will of course
7349create a larger group (of one section). If input sections are too
7350large, it may not be possible for a branch to reach its stub.
7351
7352@ifclear GENERIC
7353@lowersections
7354@end ifclear
7355@end ifset
7356
7fb9f789
NC
7357@ifset M68K
7358@ifclear GENERIC
7359@raisesections
7360@end ifclear
7361
7362@node M68K
7363@section @command{ld} and the Motorola 68K family
7364
7365@cindex Motorola 68K GOT generation
7366@kindex --got=@var{type}
7367The @samp{--got=@var{type}} option lets you choose the GOT generation scheme.
7368The choices are @samp{single}, @samp{negative}, @samp{multigot} and
7369@samp{target}. When @samp{target} is selected the linker chooses
7370the default GOT generation scheme for the current target.
7371@samp{single} tells the linker to generate a single GOT with
7372entries only at non-negative offsets.
7373@samp{negative} instructs the linker to generate a single GOT with
7374entries at both negative and positive offsets. Not all environments
7375support such GOTs.
7376@samp{multigot} allows the linker to generate several GOTs in the
7377output file. All GOT references from a single input object
7378file access the same GOT, but references from different input object
7379files might access different GOTs. Not all environments support such GOTs.
7380
7381@ifclear GENERIC
7382@lowersections
7383@end ifclear
7384@end ifset
7385
833794fc
MR
7386@ifset MIPS
7387@ifclear GENERIC
7388@raisesections
7389@end ifclear
7390
7391@node MIPS
7392@section @command{ld} and the MIPS family
7393
7394@cindex MIPS microMIPS instruction choice selection
7395@kindex --insn32
7396@kindex --no-insn32
7397The @samp{--insn32} and @samp{--no-insn32} options control the choice of
7398microMIPS instructions used in code generated by the linker, such as that
7399in the PLT or lazy binding stubs, or in relaxation. If @samp{--insn32} is
7400used, then the linker only uses 32-bit instruction encodings. By default
7401or if @samp{--no-insn32} is used, all instruction encodings are used,
7402including 16-bit ones where possible.
7403
8b10b0b3
MR
7404@cindex MIPS branch relocation check control
7405@kindex --ignore-branch-isa
7406@kindex --no-ignore-branch-isa
7407The @samp{--ignore-branch-isa} and @samp{--no-ignore-branch-isa} options
7408control branch relocation checks for invalid ISA mode transitions. If
7409@samp{--ignore-branch-isa} is used, then the linker accepts any branch
7410relocations and any ISA mode transition required is lost in relocation
7411calculation, except for some cases of @code{BAL} instructions which meet
7412relaxation conditions and are converted to equivalent @code{JALX}
7413instructions as the associated relocation is calculated. By default
7414or if @samp{--no-ignore-branch-isa} is used a check is made causing
7415the loss of an ISA mode transition to produce an error.
7416
833794fc
MR
7417@ifclear GENERIC
7418@lowersections
7419@end ifclear
7420@end ifset
7421
36f63dca
NC
7422@ifset MMIX
7423@ifclear GENERIC
7424@raisesections
7425@end ifclear
7426
7427@node MMIX
7428@section @code{ld} and MMIX
7429For MMIX, there is a choice of generating @code{ELF} object files or
7430@code{mmo} object files when linking. The simulator @code{mmix}
7431understands the @code{mmo} format. The binutils @code{objcopy} utility
7432can translate between the two formats.
7433
7434There is one special section, the @samp{.MMIX.reg_contents} section.
7435Contents in this section is assumed to correspond to that of global
7436registers, and symbols referring to it are translated to special symbols,
7437equal to registers. In a final link, the start address of the
7438@samp{.MMIX.reg_contents} section corresponds to the first allocated
7439global register multiplied by 8. Register @code{$255} is not included in
7440this section; it is always set to the program entry, which is at the
7441symbol @code{Main} for @code{mmo} files.
7442
7a2de473
HPN
7443Global symbols with the prefix @code{__.MMIX.start.}, for example
7444@code{__.MMIX.start..text} and @code{__.MMIX.start..data} are special.
7445The default linker script uses these to set the default start address
7446of a section.
36f63dca
NC
7447
7448Initial and trailing multiples of zero-valued 32-bit words in a section,
7449are left out from an mmo file.
7450
7451@ifclear GENERIC
7452@lowersections
7453@end ifclear
7454@end ifset
7455
7456@ifset MSP430
7457@ifclear GENERIC
7458@raisesections
7459@end ifclear
7460
7461@node MSP430
7462@section @code{ld} and MSP430
7463For the MSP430 it is possible to select the MPU architecture. The flag @samp{-m [mpu type]}
7464will select an appropriate linker script for selected MPU type. (To get a list of known MPUs
7465just pass @samp{-m help} option to the linker).
7466
7467@cindex MSP430 extra sections
7468The linker will recognize some extra sections which are MSP430 specific:
7469
7470@table @code
7471@item @samp{.vectors}
7472Defines a portion of ROM where interrupt vectors located.
7473
7474@item @samp{.bootloader}
7475Defines the bootloader portion of the ROM (if applicable). Any code
7476in this section will be uploaded to the MPU.
7477
7478@item @samp{.infomem}
7479Defines an information memory section (if applicable). Any code in
7480this section will be uploaded to the MPU.
7481
c0065db7 7482@item @samp{.infomemnobits}
36f63dca
NC
7483This is the same as the @samp{.infomem} section except that any code
7484in this section will not be uploaded to the MPU.
7485
7486@item @samp{.noinit}
7487Denotes a portion of RAM located above @samp{.bss} section.
7488
c0065db7 7489The last two sections are used by gcc.
36f63dca
NC
7490@end table
7491
7ef3addb
JL
7492@table @option
7493@cindex MSP430 Options
7494@kindex --code-region
7495@item --code-region=[either,lower,upper,none]
7496This will transform .text* sections to [either,lower,upper].text* sections. The
7497argument passed to GCC for -mcode-region is propagated to the linker
7498using this option.
7499
7500@kindex --data-region
7501@item --data-region=[either,lower,upper,none]
7502This will transform .data*, .bss* and .rodata* sections to
7503[either,lower,upper].[data,bss,rodata]* sections. The argument passed to GCC
7504for -mdata-region is propagated to the linker using this option.
7505
7506@kindex --disable-sec-transformation
7507@item --disable-sec-transformation
7508Prevent the transformation of sections as specified by the @code{--code-region}
7509and @code{--data-region} options.
7510This is useful if you are compiling and linking using a single call to the GCC
7511wrapper, and want to compile the source files using -m[code,data]-region but
7512not transform the sections for prebuilt libraries and objects.
7513@end table
7514
36f63dca
NC
7515@ifclear GENERIC
7516@lowersections
7517@end ifclear
7518@end ifset
7519
35c08157
KLC
7520@ifset NDS32
7521@ifclear GENERIC
7522@raisesections
7523@end ifclear
7524
7525@node NDS32
7526@section @code{ld} and NDS32
7527@kindex relaxing on NDS32
7528For NDS32, there are some options to select relaxation behavior. The linker
7529relaxes objects according to these options.
7530
7531@table @code
7532@item @samp{--m[no-]fp-as-gp}
7533Disable/enable fp-as-gp relaxation.
7534
7535@item @samp{--mexport-symbols=FILE}
7536Exporting symbols and their address into FILE as linker script.
7537
7538@item @samp{--m[no-]ex9}
7539Disable/enable link-time EX9 relaxation.
7540
7541@item @samp{--mexport-ex9=FILE}
7542Export the EX9 table after linking.
7543
7544@item @samp{--mimport-ex9=FILE}
7545Import the Ex9 table for EX9 relaxation.
7546
7547@item @samp{--mupdate-ex9}
7548Update the existing EX9 table.
7549
7550@item @samp{--mex9-limit=NUM}
7551Maximum number of entries in the ex9 table.
7552
7553@item @samp{--mex9-loop-aware}
7554Avoid generating the EX9 instruction inside the loop.
7555
7556@item @samp{--m[no-]ifc}
7557Disable/enable the link-time IFC optimization.
7558
7559@item @samp{--mifc-loop-aware}
7560Avoid generating the IFC instruction inside the loop.
7561@end table
7562
7563@ifclear GENERIC
7564@lowersections
7565@end ifclear
7566@end ifset
7567
78058a5e
SL
7568@ifset NIOSII
7569@ifclear GENERIC
7570@raisesections
7571@end ifclear
7572
7573@node Nios II
7574@section @command{ld} and the Altera Nios II
7575@cindex Nios II call relaxation
7576@kindex --relax on Nios II
7577
7578Call and immediate jump instructions on Nios II processors are limited to
7579transferring control to addresses in the same 256MB memory segment,
7580which may result in @command{ld} giving
7581@samp{relocation truncated to fit} errors with very large programs.
7582The command-line option @option{--relax} enables the generation of
7583trampolines that can access the entire 32-bit address space for calls
7584outside the normal @code{call} and @code{jmpi} address range. These
7585trampolines are inserted at section boundaries, so may not themselves
7586be reachable if an input section and its associated call trampolines are
7587larger than 256MB.
7588
7589The @option{--relax} option is enabled by default unless @option{-r}
7590is also specified. You can disable trampoline generation by using the
7591@option{--no-relax} linker option. You can also disable this optimization
7592locally by using the @samp{set .noat} directive in assembly-language
7593source files, as the linker-inserted trampolines use the @code{at}
7594register as a temporary.
7595
7596Note that the linker @option{--relax} option is independent of assembler
7597relaxation options, and that using the GNU assembler's @option{-relax-all}
7598option interferes with the linker's more selective call instruction relaxation.
7599
7600@ifclear GENERIC
7601@lowersections
7602@end ifclear
7603@end ifset
7604
2a60a7a8
AM
7605@ifset POWERPC
7606@ifclear GENERIC
7607@raisesections
7608@end ifclear
7609
7610@node PowerPC ELF32
7611@section @command{ld} and PowerPC 32-bit ELF Support
7612@cindex PowerPC long branches
7613@kindex --relax on PowerPC
7614Branches on PowerPC processors are limited to a signed 26-bit
7615displacement, which may result in @command{ld} giving
7616@samp{relocation truncated to fit} errors with very large programs.
7617@samp{--relax} enables the generation of trampolines that can access
7618the entire 32-bit address space. These trampolines are inserted at
7619section boundaries, so may not themselves be reachable if an input
c8a1f254
NS
7620section exceeds 33M in size. You may combine @samp{-r} and
7621@samp{--relax} to add trampolines in a partial link. In that case
7622both branches to undefined symbols and inter-section branches are also
7623considered potentially out of range, and trampolines inserted.
2a60a7a8
AM
7624
7625@cindex PowerPC ELF32 options
7626@table @option
7627@cindex PowerPC PLT
7628@kindex --bss-plt
7629@item --bss-plt
7630Current PowerPC GCC accepts a @samp{-msecure-plt} option that
7631generates code capable of using a newer PLT and GOT layout that has
7632the security advantage of no executable section ever needing to be
7633writable and no writable section ever being executable. PowerPC
7634@command{ld} will generate this layout, including stubs to access the
7635PLT, if all input files (including startup and static libraries) were
7636compiled with @samp{-msecure-plt}. @samp{--bss-plt} forces the old
7637BSS PLT (and GOT layout) which can give slightly better performance.
7638
016687f8
AM
7639@kindex --secure-plt
7640@item --secure-plt
7641@command{ld} will use the new PLT and GOT layout if it is linking new
7642@samp{-fpic} or @samp{-fPIC} code, but does not do so automatically
7643when linking non-PIC code. This option requests the new PLT and GOT
7644layout. A warning will be given if some object file requires the old
7645style BSS PLT.
7646
2a60a7a8
AM
7647@cindex PowerPC GOT
7648@kindex --sdata-got
7649@item --sdata-got
7650The new secure PLT and GOT are placed differently relative to other
7651sections compared to older BSS PLT and GOT placement. The location of
7652@code{.plt} must change because the new secure PLT is an initialized
7653section while the old PLT is uninitialized. The reason for the
7654@code{.got} change is more subtle: The new placement allows
7655@code{.got} to be read-only in applications linked with
7656@samp{-z relro -z now}. However, this placement means that
7657@code{.sdata} cannot always be used in shared libraries, because the
7658PowerPC ABI accesses @code{.sdata} in shared libraries from the GOT
7659pointer. @samp{--sdata-got} forces the old GOT placement. PowerPC
7660GCC doesn't use @code{.sdata} in shared libraries, so this option is
7661really only useful for other compilers that may do so.
7662
7663@cindex PowerPC stub symbols
7664@kindex --emit-stub-syms
7665@item --emit-stub-syms
7666This option causes @command{ld} to label linker stubs with a local
7667symbol that encodes the stub type and destination.
7668
7669@cindex PowerPC TLS optimization
7670@kindex --no-tls-optimize
7671@item --no-tls-optimize
7672PowerPC @command{ld} normally performs some optimization of code
7673sequences used to access Thread-Local Storage. Use this option to
7674disable the optimization.
7675@end table
7676
7677@ifclear GENERIC
7678@lowersections
7679@end ifclear
7680@end ifset
7681
7682@ifset POWERPC64
7683@ifclear GENERIC
7684@raisesections
7685@end ifclear
7686
7687@node PowerPC64 ELF64
7688@section @command{ld} and PowerPC64 64-bit ELF Support
7689
7690@cindex PowerPC64 ELF64 options
7691@table @option
7692@cindex PowerPC64 stub grouping
7693@kindex --stub-group-size
7694@item --stub-group-size
7695Long branch stubs, PLT call stubs and TOC adjusting stubs are placed
7696by @command{ld} in stub sections located between groups of input sections.
7697@samp{--stub-group-size} specifies the maximum size of a group of input
7698sections handled by one stub section. Since branch offsets are signed,
7699a stub section may serve two groups of input sections, one group before
7700the stub section, and one group after it. However, when using
7701conditional branches that require stubs, it may be better (for branch
7702prediction) that stub sections only serve one group of input sections.
7703A negative value for @samp{N} chooses this scheme, ensuring that
7704branches to stubs always use a negative offset. Two special values of
7705@samp{N} are recognized, @samp{1} and @samp{-1}. These both instruct
7706@command{ld} to automatically size input section groups for the branch types
7707detected, with the same behaviour regarding stub placement as other
7708positive or negative values of @samp{N} respectively.
7709
7710Note that @samp{--stub-group-size} does not split input sections. A
7711single input section larger than the group size specified will of course
7712create a larger group (of one section). If input sections are too
7713large, it may not be possible for a branch to reach its stub.
7714
7715@cindex PowerPC64 stub symbols
7716@kindex --emit-stub-syms
7717@item --emit-stub-syms
7718This option causes @command{ld} to label linker stubs with a local
7719symbol that encodes the stub type and destination.
7720
7721@cindex PowerPC64 dot symbols
7722@kindex --dotsyms
7723@kindex --no-dotsyms
95421fb9
AM
7724@item --dotsyms
7725@itemx --no-dotsyms
2a60a7a8
AM
7726These two options control how @command{ld} interprets version patterns
7727in a version script. Older PowerPC64 compilers emitted both a
7728function descriptor symbol with the same name as the function, and a
7729code entry symbol with the name prefixed by a dot (@samp{.}). To
7730properly version a function @samp{foo}, the version script thus needs
7731to control both @samp{foo} and @samp{.foo}. The option
7732@samp{--dotsyms}, on by default, automatically adds the required
7733dot-prefixed patterns. Use @samp{--no-dotsyms} to disable this
7734feature.
7735
7ae4ea7d
AM
7736@cindex PowerPC64 register save/restore functions
7737@kindex --save-restore-funcs
7738@kindex --no-save-restore-funcs
95421fb9
AM
7739@item --save-restore-funcs
7740@itemx --no-save-restore-funcs
7ae4ea7d
AM
7741These two options control whether PowerPC64 @command{ld} automatically
7742provides out-of-line register save and restore functions used by
7743@samp{-Os} code. The default is to provide any such referenced
7744function for a normal final link, and to not do so for a relocatable
7745link.
7746
2a60a7a8
AM
7747@cindex PowerPC64 TLS optimization
7748@kindex --no-tls-optimize
7749@item --no-tls-optimize
7750PowerPC64 @command{ld} normally performs some optimization of code
7751sequences used to access Thread-Local Storage. Use this option to
7752disable the optimization.
7753
7c9cf415
AM
7754@cindex PowerPC64 __tls_get_addr optimization
7755@kindex --tls-get-addr-optimize
7756@kindex --no-tls-get-addr-optimize
9e7028aa
AM
7757@kindex --tls-get-addr-regsave
7758@kindex --no-tls-get-addr-regsave
95421fb9
AM
7759@item --tls-get-addr-optimize
7760@itemx --no-tls-get-addr-optimize
9e7028aa 7761These options control how PowerPC64 @command{ld} uses a special
7c9cf415
AM
7762stub to call __tls_get_addr. PowerPC64 glibc 2.22 and later support
7763an optimization that allows the second and subsequent calls to
7764@code{__tls_get_addr} for a given symbol to be resolved by the special
9e7028aa
AM
7765stub without calling in to glibc. By default the linker enables
7766generation of the stub when glibc advertises the availability of
7767__tls_get_addr_opt.
7768Using @option{--tls-get-addr-optimize} with an older glibc won't do
7769much besides slow down your applications, but may be useful if linking
7770an application against an older glibc with the expectation that it
7771will normally be used on systems having a newer glibc.
7772@option{--tls-get-addr-regsave} forces generation of a stub that saves
7773and restores volatile registers around the call into glibc. Normally,
7774this is done when the linker detects a call to __tls_get_addr_desc.
7775Such calls then go via the register saving stub to __tls_get_addr_opt.
5cebc931 7776@option{--no-tls-get-addr-regsave} disables generation of the
9e7028aa 7777register saves.
7c9cf415 7778
2a60a7a8
AM
7779@cindex PowerPC64 OPD optimization
7780@kindex --no-opd-optimize
7781@item --no-opd-optimize
7782PowerPC64 @command{ld} normally removes @code{.opd} section entries
7783corresponding to deleted link-once functions, or functions removed by
e7fc76dd 7784the action of @samp{--gc-sections} or linker script @code{/DISCARD/}.
2a60a7a8
AM
7785Use this option to disable @code{.opd} optimization.
7786
7787@cindex PowerPC64 OPD spacing
7788@kindex --non-overlapping-opd
7789@item --non-overlapping-opd
7790Some PowerPC64 compilers have an option to generate compressed
7791@code{.opd} entries spaced 16 bytes apart, overlapping the third word,
7792the static chain pointer (unused in C) with the first word of the next
7793entry. This option expands such entries to the full 24 bytes.
7794
7795@cindex PowerPC64 TOC optimization
7796@kindex --no-toc-optimize
7797@item --no-toc-optimize
7798PowerPC64 @command{ld} normally removes unused @code{.toc} section
7799entries. Such entries are detected by examining relocations that
7800reference the TOC in code sections. A reloc in a deleted code section
7801marks a TOC word as unneeded, while a reloc in a kept code section
7802marks a TOC word as needed. Since the TOC may reference itself, TOC
7803relocs are also examined. TOC words marked as both needed and
7804unneeded will of course be kept. TOC words without any referencing
7805reloc are assumed to be part of a multi-word entry, and are kept or
7806discarded as per the nearest marked preceding word. This works
7807reliably for compiler generated code, but may be incorrect if assembly
7808code is used to insert TOC entries. Use this option to disable the
7809optimization.
7810
7811@cindex PowerPC64 multi-TOC
7812@kindex --no-multi-toc
7813@item --no-multi-toc
794e51c0
AM
7814If given any toc option besides @code{-mcmodel=medium} or
7815@code{-mcmodel=large}, PowerPC64 GCC generates code for a TOC model
7816where TOC
2a60a7a8
AM
7817entries are accessed with a 16-bit offset from r2. This limits the
7818total TOC size to 64K. PowerPC64 @command{ld} extends this limit by
7819grouping code sections such that each group uses less than 64K for its
7820TOC entries, then inserts r2 adjusting stubs between inter-group
7821calls. @command{ld} does not split apart input sections, so cannot
7822help if a single input file has a @code{.toc} section that exceeds
782364K, most likely from linking multiple files with @command{ld -r}.
7824Use this option to turn off this feature.
794e51c0
AM
7825
7826@cindex PowerPC64 TOC sorting
7827@kindex --no-toc-sort
7828@item --no-toc-sort
7829By default, @command{ld} sorts TOC sections so that those whose file
7830happens to have a section called @code{.init} or @code{.fini} are
7831placed first, followed by TOC sections referenced by code generated
7832with PowerPC64 gcc's @code{-mcmodel=small}, and lastly TOC sections
7833referenced only by code generated with PowerPC64 gcc's
7834@code{-mcmodel=medium} or @code{-mcmodel=large} options. Doing this
7835results in better TOC grouping for multi-TOC. Use this option to turn
7836off this feature.
7837
7838@cindex PowerPC64 PLT stub alignment
7839@kindex --plt-align
7840@kindex --no-plt-align
7841@item --plt-align
7842@itemx --no-plt-align
7843Use these options to control whether individual PLT call stubs are
2420fff6
AM
7844aligned to a 32-byte boundary, or to the specified power of two
7845boundary when using @code{--plt-align=}. A negative value may be
7846specified to pad PLT call stubs so that they do not cross the
7847specified power of two boundary (or the minimum number of boundaries
7848if a PLT stub is so large that it must cross a boundary). By default
7849PLT call stubs are aligned to 32-byte boundaries.
794e51c0
AM
7850
7851@cindex PowerPC64 PLT call stub static chain
7852@kindex --plt-static-chain
7853@kindex --no-plt-static-chain
7854@item --plt-static-chain
7855@itemx --no-plt-static-chain
7856Use these options to control whether PLT call stubs load the static
7857chain pointer (r11). @code{ld} defaults to not loading the static
7858chain since there is never any need to do so on a PLT call.
7859
7860@cindex PowerPC64 PLT call stub thread safety
7861@kindex --plt-thread-safe
7862@kindex --no-plt-thread-safe
7863@item --plt-thread-safe
1be5d8d3 7864@itemx --no-plt-thread-safe
794e51c0
AM
7865With power7's weakly ordered memory model, it is possible when using
7866lazy binding for ld.so to update a plt entry in one thread and have
7867another thread see the individual plt entry words update in the wrong
7868order, despite ld.so carefully writing in the correct order and using
7869memory write barriers. To avoid this we need some sort of read
7870barrier in the call stub, or use LD_BIND_NOW=1. By default, @code{ld}
7871looks for calls to commonly used functions that create threads, and if
7872seen, adds the necessary barriers. Use these options to change the
7873default behaviour.
8b5f1ed8
AM
7874
7875@cindex PowerPC64 ELFv2 PLT localentry optimization
7876@kindex --plt-localentry
7877@kindex --no-plt-localentry
7878@item --plt-localentry
7879@itemx --no-localentry
7880ELFv2 functions with localentry:0 are those with a single entry point,
7881ie. global entry == local entry, and that have no requirement on r2
7882(the TOC/GOT pointer) or r12, and guarantee r2 is unchanged on return.
7883Such an external function can be called via the PLT without saving r2
7884or restoring it on return, avoiding a common load-hit-store for small
7885functions. The optimization is attractive, with up to 40% reduction
7886in execution time for a small function, but can result in symbol
d44c746a
AM
7887interposition failures. Also, minor changes in a shared library,
7888including system libraries, can cause a function that was localentry:0
7889to become localentry:8. This will result in a dynamic loader
7890complaint and failure to run. The option is experimental, use with
7891care. @option{--no-plt-localentry} is the default.
2a60a7a8
AM
7892@end table
7893
7894@ifclear GENERIC
7895@lowersections
7896@end ifclear
7897@end ifset
7898
b4cbbe8f
AK
7899@ifset S/390
7900@ifclear GENERIC
7901@raisesections
7902@end ifclear
7903
7904@node S/390 ELF
7905@section @command{ld} and S/390 ELF Support
7906
7907@cindex S/390 ELF options
7908@table @option
7909
7910@cindex S/390
7911@kindex --s390-pgste
7912@item --s390-pgste
7913This option marks the result file with a @code{PT_S390_PGSTE}
7914segment. The Linux kernel is supposed to allocate 4k page tables for
7915binaries marked that way.
7916@end table
7917
7918@ifclear GENERIC
7919@lowersections
7920@end ifclear
7921@end ifset
7922
49fa1e15
AM
7923@ifset SPU
7924@ifclear GENERIC
7925@raisesections
7926@end ifclear
7927
7928@node SPU ELF
7929@section @command{ld} and SPU ELF Support
7930
7931@cindex SPU ELF options
7932@table @option
7933
7934@cindex SPU plugins
7935@kindex --plugin
7936@item --plugin
7937This option marks an executable as a PIC plugin module.
7938
7939@cindex SPU overlays
7940@kindex --no-overlays
7941@item --no-overlays
7942Normally, @command{ld} recognizes calls to functions within overlay
7943regions, and redirects such calls to an overlay manager via a stub.
7944@command{ld} also provides a built-in overlay manager. This option
7945turns off all this special overlay handling.
7946
7947@cindex SPU overlay stub symbols
7948@kindex --emit-stub-syms
7949@item --emit-stub-syms
7950This option causes @command{ld} to label overlay stubs with a local
7951symbol that encodes the stub type and destination.
7952
7953@cindex SPU extra overlay stubs
7954@kindex --extra-overlay-stubs
7955@item --extra-overlay-stubs
7956This option causes @command{ld} to add overlay call stubs on all
7957function calls out of overlay regions. Normally stubs are not added
7958on calls to non-overlay regions.
7959
7960@cindex SPU local store size
7961@kindex --local-store=lo:hi
7962@item --local-store=lo:hi
7963@command{ld} usually checks that a final executable for SPU fits in
7964the address range 0 to 256k. This option may be used to change the
7965range. Disable the check entirely with @option{--local-store=0:0}.
7966
c0065db7 7967@cindex SPU
49fa1e15
AM
7968@kindex --stack-analysis
7969@item --stack-analysis
7970SPU local store space is limited. Over-allocation of stack space
7971unnecessarily limits space available for code and data, while
7972under-allocation results in runtime failures. If given this option,
7973@command{ld} will provide an estimate of maximum stack usage.
7974@command{ld} does this by examining symbols in code sections to
7975determine the extents of functions, and looking at function prologues
7976for stack adjusting instructions. A call-graph is created by looking
7977for relocations on branch instructions. The graph is then searched
7978for the maximum stack usage path. Note that this analysis does not
7979find calls made via function pointers, and does not handle recursion
7980and other cycles in the call graph. Stack usage may be
7981under-estimated if your code makes such calls. Also, stack usage for
7982dynamic allocation, e.g. alloca, will not be detected. If a link map
7983is requested, detailed information about each function's stack usage
7984and calls will be given.
7985
c0065db7 7986@cindex SPU
49fa1e15
AM
7987@kindex --emit-stack-syms
7988@item --emit-stack-syms
7989This option, if given along with @option{--stack-analysis} will result
7990in @command{ld} emitting stack sizing symbols for each function.
7991These take the form @code{__stack_<function_name>} for global
7992functions, and @code{__stack_<number>_<function_name>} for static
7993functions. @code{<number>} is the section id in hex. The value of
7994such symbols is the stack requirement for the corresponding function.
7995The symbol size will be zero, type @code{STT_NOTYPE}, binding
c0065db7 7996@code{STB_LOCAL}, and section @code{SHN_ABS}.
49fa1e15
AM
7997@end table
7998
7999@ifclear GENERIC
8000@lowersections
8001@end ifclear
8002@end ifset
8003
36f63dca
NC
8004@ifset TICOFF
8005@ifclear GENERIC
8006@raisesections
8007@end ifclear
8008
8009@node TI COFF
8010@section @command{ld}'s Support for Various TI COFF Versions
8011@cindex TI COFF versions
8012@kindex --format=@var{version}
8013The @samp{--format} switch allows selection of one of the various
8014TI COFF versions. The latest of this writing is 2; versions 0 and 1 are
8015also supported. The TI COFF versions also vary in header byte-order
8016format; @command{ld} will read any version or byte order, but the output
8017header format depends on the default specified by the specific target.
8018
8019@ifclear GENERIC
8020@lowersections
8021@end ifclear
8022@end ifset
8023
2ca22b03
NC
8024@ifset WIN32
8025@ifclear GENERIC
8026@raisesections
8027@end ifclear
8028
8029@node WIN32
8030@section @command{ld} and WIN32 (cygwin/mingw)
8031
c0065db7 8032This section describes some of the win32 specific @command{ld} issues.
a05a5b64
TP
8033See @ref{Options,,Command-line Options} for detailed description of the
8034command-line options mentioned here.
2ca22b03
NC
8035
8036@table @emph
c0065db7
RM
8037@cindex import libraries
8038@item import libraries
69da35b5 8039The standard Windows linker creates and uses so-called import
2ca22b03 8040libraries, which contains information for linking to dll's. They are
69da35b5
NC
8041regular static archives and are handled as any other static
8042archive. The cygwin and mingw ports of @command{ld} have specific
2ca22b03 8043support for creating such libraries provided with the
a05a5b64 8044@samp{--out-implib} command-line option.
2ca22b03 8045
c0065db7
RM
8046@item exporting DLL symbols
8047@cindex exporting DLL symbols
dc8465bf
NC
8048The cygwin/mingw @command{ld} has several ways to export symbols for dll's.
8049
8050@table @emph
8051@item using auto-export functionality
8052@cindex using auto-export functionality
8053By default @command{ld} exports symbols with the auto-export functionality,
a05a5b64 8054which is controlled by the following command-line options:
dc8465bf 8055
0a5d968e
NC
8056@itemize
8057@item --export-all-symbols [This is the default]
8058@item --exclude-symbols
8059@item --exclude-libs
e1c37eb5 8060@item --exclude-modules-for-implib
09e2aba4 8061@item --version-script
0a5d968e
NC
8062@end itemize
8063
09e2aba4
DK
8064When auto-export is in operation, @command{ld} will export all the non-local
8065(global and common) symbols it finds in a DLL, with the exception of a few
8066symbols known to belong to the system's runtime and libraries. As it will
8067often not be desirable to export all of a DLL's symbols, which may include
8068private functions that are not part of any public interface, the command-line
9d5777a3 8069options listed above may be used to filter symbols out from the list for
09e2aba4
DK
8070exporting. The @samp{--output-def} option can be used in order to see the
8071final list of exported symbols with all exclusions taken into effect.
8072
8073If @samp{--export-all-symbols} is not given explicitly on the
0a5d968e
NC
8074command line, then the default auto-export behavior will be @emph{disabled}
8075if either of the following are true:
8076
8077@itemize
8078@item A DEF file is used.
8079@item Any symbol in any object file was marked with the __declspec(dllexport) attribute.
8080@end itemize
dc8465bf 8081
c0065db7
RM
8082@item using a DEF file
8083@cindex using a DEF file
dc8465bf
NC
8084Another way of exporting symbols is using a DEF file. A DEF file is
8085an ASCII file containing definitions of symbols which should be
8086exported when a dll is created. Usually it is named @samp{<dll
8087name>.def} and is added as any other object file to the linker's
0a5d968e 8088command line. The file's name must end in @samp{.def} or @samp{.DEF}.
dc8465bf
NC
8089
8090@example
8091gcc -o <output> <objectfiles> <dll name>.def
8092@end example
8093
0a5d968e
NC
8094Using a DEF file turns off the normal auto-export behavior, unless the
8095@samp{--export-all-symbols} option is also used.
8096
dc8465bf
NC
8097Here is an example of a DEF file for a shared library called @samp{xyz.dll}:
8098
8099@example
4b5bd4e7 8100LIBRARY "xyz.dll" BASE=0x20000000
dc8465bf
NC
8101
8102EXPORTS
8103foo
8104bar
8105_bar = bar
4b5bd4e7
DS
8106another_foo = abc.dll.afoo
8107var1 DATA
7fcab871
KT
8108doo = foo == foo2
8109eoo DATA == var1
c0065db7 8110@end example
dc8465bf 8111
7fcab871 8112This example defines a DLL with a non-default base address and seven
4b5bd4e7
DS
8113symbols in the export table. The third exported symbol @code{_bar} is an
8114alias for the second. The fourth symbol, @code{another_foo} is resolved
8115by "forwarding" to another module and treating it as an alias for
8116@code{afoo} exported from the DLL @samp{abc.dll}. The final symbol
7fcab871
KT
8117@code{var1} is declared to be a data object. The @samp{doo} symbol in
8118export library is an alias of @samp{foo}, which gets the string name
8119in export table @samp{foo2}. The @samp{eoo} symbol is an data export
8120symbol, which gets in export table the name @samp{var1}.
4b5bd4e7 8121
6b31ad16
DS
8122The optional @code{LIBRARY <name>} command indicates the @emph{internal}
8123name of the output DLL. If @samp{<name>} does not include a suffix,
8124the default library suffix, @samp{.DLL} is appended.
8125
b45619c0
NC
8126When the .DEF file is used to build an application, rather than a
8127library, the @code{NAME <name>} command should be used instead of
6b31ad16 8128@code{LIBRARY}. If @samp{<name>} does not include a suffix, the default
c0065db7 8129executable suffix, @samp{.EXE} is appended.
6b31ad16
DS
8130
8131With either @code{LIBRARY <name>} or @code{NAME <name>} the optional
8132specification @code{BASE = <number>} may be used to specify a
c0065db7 8133non-default base address for the image.
6b31ad16
DS
8134
8135If neither @code{LIBRARY <name>} nor @code{NAME <name>} is specified,
a2877985
DS
8136or they specify an empty string, the internal name is the same as the
8137filename specified on the command line.
6b31ad16 8138
4b5bd4e7
DS
8139The complete specification of an export symbol is:
8140
8141@example
8142EXPORTS
8143 ( ( ( <name1> [ = <name2> ] )
8144 | ( <name1> = <module-name> . <external-name>))
7fcab871 8145 [ @@ <integer> ] [NONAME] [DATA] [CONSTANT] [PRIVATE] [== <name3>] ) *
c0065db7 8146@end example
4b5bd4e7
DS
8147
8148Declares @samp{<name1>} as an exported symbol from the DLL, or declares
8149@samp{<name1>} as an exported alias for @samp{<name2>}; or declares
8150@samp{<name1>} as a "forward" alias for the symbol
8151@samp{<external-name>} in the DLL @samp{<module-name>}.
8152Optionally, the symbol may be exported by the specified ordinal
7fcab871
KT
8153@samp{<integer>} alias. The optional @samp{<name3>} is the to be used
8154string in import/export table for the symbol.
4b5bd4e7
DS
8155
8156The optional keywords that follow the declaration indicate:
8157
8158@code{NONAME}: Do not put the symbol name in the DLL's export table. It
8159will still be exported by its ordinal alias (either the value specified
8160by the .def specification or, otherwise, the value assigned by the
8161linker). The symbol name, however, does remain visible in the import
8162library (if any), unless @code{PRIVATE} is also specified.
8163
8164@code{DATA}: The symbol is a variable or object, rather than a function.
8165The import lib will export only an indirect reference to @code{foo} as
8166the symbol @code{_imp__foo} (ie, @code{foo} must be resolved as
8167@code{*_imp__foo}).
8168
8169@code{CONSTANT}: Like @code{DATA}, but put the undecorated @code{foo} as
8170well as @code{_imp__foo} into the import library. Both refer to the
8171read-only import address table's pointer to the variable, not to the
8172variable itself. This can be dangerous. If the user code fails to add
8173the @code{dllimport} attribute and also fails to explicitly add the
8174extra indirection that the use of the attribute enforces, the
8175application will behave unexpectedly.
8176
8177@code{PRIVATE}: Put the symbol in the DLL's export table, but do not put
8178it into the static import library used to resolve imports at link time. The
8179symbol can still be imported using the @code{LoadLibrary/GetProcAddress}
de194d85 8180API at runtime or by using the GNU ld extension of linking directly to
4b5bd4e7 8181the DLL without an import library.
c0065db7 8182
4b5bd4e7
DS
8183See ld/deffilep.y in the binutils sources for the full specification of
8184other DEF file statements
dc8465bf
NC
8185
8186@cindex creating a DEF file
8187While linking a shared dll, @command{ld} is able to create a DEF file
a05a5b64 8188with the @samp{--output-def <file>} command-line option.
0a5d968e
NC
8189
8190@item Using decorations
8191@cindex Using decorations
8192Another way of marking symbols for export is to modify the source code
8193itself, so that when building the DLL each symbol to be exported is
8194declared as:
8195
8196@example
8197__declspec(dllexport) int a_variable
8198__declspec(dllexport) void a_function(int with_args)
8199@end example
8200
8201All such symbols will be exported from the DLL. If, however,
8202any of the object files in the DLL contain symbols decorated in
8203this way, then the normal auto-export behavior is disabled, unless
8204the @samp{--export-all-symbols} option is also used.
8205
8206Note that object files that wish to access these symbols must @emph{not}
c0065db7 8207decorate them with dllexport. Instead, they should use dllimport,
0a5d968e
NC
8208instead:
8209
8210@example
8211__declspec(dllimport) int a_variable
8212__declspec(dllimport) void a_function(int with_args)
8213@end example
8214
c0065db7
RM
8215This complicates the structure of library header files, because
8216when included by the library itself the header must declare the
0a5d968e
NC
8217variables and functions as dllexport, but when included by client
8218code the header must declare them as dllimport. There are a number
c0065db7 8219of idioms that are typically used to do this; often client code can
0a5d968e
NC
8220omit the __declspec() declaration completely. See
8221@samp{--enable-auto-import} and @samp{automatic data imports} for more
b45619c0 8222information.
c0065db7 8223@end table
dc8465bf 8224
2ca22b03
NC
8225@cindex automatic data imports
8226@item automatic data imports
8227The standard Windows dll format supports data imports from dlls only
69da35b5 8228by adding special decorations (dllimport/dllexport), which let the
2ca22b03 8229compiler produce specific assembler instructions to deal with this
c0065db7 8230issue. This increases the effort necessary to port existing Un*x
69da35b5 8231code to these platforms, especially for large
2ca22b03 8232c++ libraries and applications. The auto-import feature, which was
c0065db7 8233initially provided by Paul Sokolovsky, allows one to omit the
b45619c0 8234decorations to achieve a behavior that conforms to that on POSIX/Un*x
c0065db7 8235platforms. This feature is enabled with the @samp{--enable-auto-import}
69da35b5
NC
8236command-line option, although it is enabled by default on cygwin/mingw.
8237The @samp{--enable-auto-import} option itself now serves mainly to
8238suppress any warnings that are ordinarily emitted when linked objects
8239trigger the feature's use.
8240
c0065db7 8241auto-import of variables does not always work flawlessly without
69da35b5
NC
8242additional assistance. Sometimes, you will see this message
8243
c0065db7 8244"variable '<var>' can't be auto-imported. Please read the
69da35b5
NC
8245documentation for ld's @code{--enable-auto-import} for details."
8246
c0065db7
RM
8247The @samp{--enable-auto-import} documentation explains why this error
8248occurs, and several methods that can be used to overcome this difficulty.
8249One of these methods is the @emph{runtime pseudo-relocs} feature, described
69da35b5
NC
8250below.
8251
8252@cindex runtime pseudo-relocation
c0065db7
RM
8253For complex variables imported from DLLs (such as structs or classes),
8254object files typically contain a base address for the variable and an
8255offset (@emph{addend}) within the variable--to specify a particular
8256field or public member, for instance. Unfortunately, the runtime loader used
8257in win32 environments is incapable of fixing these references at runtime
69da35b5 8258without the additional information supplied by dllimport/dllexport decorations.
c0065db7 8259The standard auto-import feature described above is unable to resolve these
69da35b5
NC
8260references.
8261
c0065db7
RM
8262The @samp{--enable-runtime-pseudo-relocs} switch allows these references to
8263be resolved without error, while leaving the task of adjusting the references
8264themselves (with their non-zero addends) to specialized code provided by the
8265runtime environment. Recent versions of the cygwin and mingw environments and
8266compilers provide this runtime support; older versions do not. However, the
8267support is only necessary on the developer's platform; the compiled result will
69da35b5
NC
8268run without error on an older system.
8269
c0065db7
RM
8270@samp{--enable-runtime-pseudo-relocs} is not the default; it must be explicitly
8271enabled as needed.
2ca22b03
NC
8272
8273@cindex direct linking to a dll
8274@item direct linking to a dll
8275The cygwin/mingw ports of @command{ld} support the direct linking,
8276including data symbols, to a dll without the usage of any import
69da35b5 8277libraries. This is much faster and uses much less memory than does the
b45619c0 8278traditional import library method, especially when linking large
c0065db7
RM
8279libraries or applications. When @command{ld} creates an import lib, each
8280function or variable exported from the dll is stored in its own bfd, even
8281though a single bfd could contain many exports. The overhead involved in
69da35b5 8282storing, loading, and processing so many bfd's is quite large, and explains the
c0065db7 8283tremendous time, memory, and storage needed to link against particularly
69da35b5
NC
8284large or complex libraries when using import libs.
8285
c0065db7 8286Linking directly to a dll uses no extra command-line switches other than
69da35b5 8287@samp{-L} and @samp{-l}, because @command{ld} already searches for a number
c0065db7 8288of names to match each library. All that is needed from the developer's
69da35b5
NC
8289perspective is an understanding of this search, in order to force ld to
8290select the dll instead of an import library.
8291
2ca22b03 8292
69da35b5
NC
8293For instance, when ld is called with the argument @samp{-lxxx} it will attempt
8294to find, in the first directory of its search path,
2ca22b03
NC
8295
8296@example
45e948fe
NC
8297libxxx.dll.a
8298xxx.dll.a
8299libxxx.a
8300xxx.lib
f6c4d4b1 8301libxxx.lib
69da35b5 8302cygxxx.dll (*)
45e948fe
NC
8303libxxx.dll
8304xxx.dll
2ca22b03
NC
8305@end example
8306
69da35b5
NC
8307before moving on to the next directory in the search path.
8308
c0065db7
RM
8309(*) Actually, this is not @samp{cygxxx.dll} but in fact is @samp{<prefix>xxx.dll},
8310where @samp{<prefix>} is set by the @command{ld} option
8311@samp{--dll-search-prefix=<prefix>}. In the case of cygwin, the standard gcc spec
8312file includes @samp{--dll-search-prefix=cyg}, so in effect we actually search for
69da35b5
NC
8313@samp{cygxxx.dll}.
8314
c0065db7
RM
8315Other win32-based unix environments, such as mingw or pw32, may use other
8316@samp{<prefix>}es, although at present only cygwin makes use of this feature. It
69da35b5
NC
8317was originally intended to help avoid name conflicts among dll's built for the
8318various win32/un*x environments, so that (for example) two versions of a zlib dll
8319could coexist on the same machine.
8320
2ca22b03
NC
8321The generic cygwin/mingw path layout uses a @samp{bin} directory for
8322applications and dll's and a @samp{lib} directory for the import
69da35b5 8323libraries (using cygwin nomenclature):
2ca22b03
NC
8324
8325@example
8326bin/
8327 cygxxx.dll
8328lib/
8329 libxxx.dll.a (in case of dll's)
c0065db7 8330 libxxx.a (in case of static archive)
2ca22b03
NC
8331@end example
8332
c0065db7
RM
8333Linking directly to a dll without using the import library can be
8334done two ways:
2ca22b03
NC
8335
83361. Use the dll directly by adding the @samp{bin} path to the link line
8337@example
8338gcc -Wl,-verbose -o a.exe -L../bin/ -lxxx
c0065db7 8339@end example
2ca22b03 8340
69da35b5
NC
8341However, as the dll's often have version numbers appended to their names
8342(@samp{cygncurses-5.dll}) this will often fail, unless one specifies
8343@samp{-L../bin -lncurses-5} to include the version. Import libs are generally
8344not versioned, and do not have this difficulty.
8345
2ca22b03
NC
83462. Create a symbolic link from the dll to a file in the @samp{lib}
8347directory according to the above mentioned search pattern. This
8348should be used to avoid unwanted changes in the tools needed for
8349making the app/dll.
8350
8351@example
8352ln -s bin/cygxxx.dll lib/[cyg|lib|]xxx.dll[.a]
c0065db7 8353@end example
2ca22b03
NC
8354
8355Then you can link without any make environment changes.
8356
8357@example
8358gcc -Wl,-verbose -o a.exe -L../lib/ -lxxx
c0065db7 8359@end example
69da35b5
NC
8360
8361This technique also avoids the version number problems, because the following is
8362perfectly legal
8363
8364@example
8365bin/
8366 cygxxx-5.dll
8367lib/
c0065db7 8368 libxxx.dll.a -> ../bin/cygxxx-5.dll
69da35b5
NC
8369@end example
8370
dc8465bf 8371Linking directly to a dll without using an import lib will work
69da35b5
NC
8372even when auto-import features are exercised, and even when
8373@samp{--enable-runtime-pseudo-relocs} is used.
8374
8375Given the improvements in speed and memory usage, one might justifiably
45e948fe 8376wonder why import libraries are used at all. There are three reasons:
69da35b5
NC
8377
83781. Until recently, the link-directly-to-dll functionality did @emph{not}
8379work with auto-imported data.
8380
dc8465bf
NC
83812. Sometimes it is necessary to include pure static objects within the
8382import library (which otherwise contains only bfd's for indirection
8383symbols that point to the exports of a dll). Again, the import lib
8384for the cygwin kernel makes use of this ability, and it is not
8385possible to do this without an import lib.
69da35b5 8386
45e948fe
NC
83873. Symbol aliases can only be resolved using an import lib. This is
8388critical when linking against OS-supplied dll's (eg, the win32 API)
8389in which symbols are usually exported as undecorated aliases of their
8390stdcall-decorated assembly names.
8391
69da35b5 8392So, import libs are not going away. But the ability to replace
c0065db7
RM
8393true import libs with a simple symbolic link to (or a copy of)
8394a dll, in many cases, is a useful addition to the suite of tools
8395binutils makes available to the win32 developer. Given the
69da35b5
NC
8396massive improvements in memory requirements during linking, storage
8397requirements, and linking speed, we expect that many developers
8398will soon begin to use this feature whenever possible.
dc8465bf 8399
c0065db7 8400@item symbol aliasing
dc8465bf 8401@table @emph
c0065db7
RM
8402@item adding additional names
8403Sometimes, it is useful to export symbols with additional names.
dc8465bf
NC
8404A symbol @samp{foo} will be exported as @samp{foo}, but it can also be
8405exported as @samp{_foo} by using special directives in the DEF file
8406when creating the dll. This will affect also the optional created
c0065db7 8407import library. Consider the following DEF file:
dc8465bf 8408
c0065db7 8409@example
dc8465bf
NC
8410LIBRARY "xyz.dll" BASE=0x61000000
8411
8412EXPORTS
c0065db7 8413foo
dc8465bf 8414_foo = foo
c0065db7 8415@end example
dc8465bf
NC
8416
8417The line @samp{_foo = foo} maps the symbol @samp{foo} to @samp{_foo}.
8418
8419Another method for creating a symbol alias is to create it in the
8420source code using the "weak" attribute:
8421
c0065db7
RM
8422@example
8423void foo () @{ /* Do something. */; @}
dc8465bf 8424void _foo () __attribute__ ((weak, alias ("foo")));
c0065db7 8425@end example
dc8465bf
NC
8426
8427See the gcc manual for more information about attributes and weak
8428symbols.
8429
8430@item renaming symbols
8431Sometimes it is useful to rename exports. For instance, the cygwin
c0065db7 8432kernel does this regularly. A symbol @samp{_foo} can be exported as
dc8465bf
NC
8433@samp{foo} but not as @samp{_foo} by using special directives in the
8434DEF file. (This will also affect the import library, if it is
c0065db7 8435created). In the following example:
dc8465bf 8436
c0065db7 8437@example
dc8465bf
NC
8438LIBRARY "xyz.dll" BASE=0x61000000
8439
8440EXPORTS
8441_foo = foo
c0065db7 8442@end example
dc8465bf
NC
8443
8444The line @samp{_foo = foo} maps the exported symbol @samp{foo} to
8445@samp{_foo}.
c0065db7 8446@end table
dc8465bf 8447
0a5d968e 8448Note: using a DEF file disables the default auto-export behavior,
a05a5b64 8449unless the @samp{--export-all-symbols} command-line option is used.
0a5d968e 8450If, however, you are trying to rename symbols, then you should list
c0065db7
RM
8451@emph{all} desired exports in the DEF file, including the symbols
8452that are not being renamed, and do @emph{not} use the
8453@samp{--export-all-symbols} option. If you list only the
8454renamed symbols in the DEF file, and use @samp{--export-all-symbols}
8455to handle the other symbols, then the both the new names @emph{and}
8456the original names for the renamed symbols will be exported.
8457In effect, you'd be aliasing those symbols, not renaming them,
0a5d968e 8458which is probably not what you wanted.
c87db184
CF
8459
8460@cindex weak externals
8461@item weak externals
8462The Windows object format, PE, specifies a form of weak symbols called
8463weak externals. When a weak symbol is linked and the symbol is not
8464defined, the weak symbol becomes an alias for some other symbol. There
8465are three variants of weak externals:
8466@itemize
8467@item Definition is searched for in objects and libraries, historically
8468called lazy externals.
8469@item Definition is searched for only in other objects, not in libraries.
8470This form is not presently implemented.
8471@item No search; the symbol is an alias. This form is not presently
8472implemented.
8473@end itemize
8474As a GNU extension, weak symbols that do not specify an alternate symbol
8475are supported. If the symbol is undefined when linking, the symbol
8476uses a default value.
c1711530
DK
8477
8478@cindex aligned common symbols
8479@item aligned common symbols
8480As a GNU extension to the PE file format, it is possible to specify the
8481desired alignment for a common symbol. This information is conveyed from
8482the assembler or compiler to the linker by means of GNU-specific commands
8483carried in the object file's @samp{.drectve} section, which are recognized
8484by @command{ld} and respected when laying out the common symbols. Native
8485tools will be able to process object files employing this GNU extension,
8486but will fail to respect the alignment instructions, and may issue noisy
8487warnings about unknown linker directives.
5063daf7 8488
2ca22b03
NC
8489@end table
8490
8491@ifclear GENERIC
8492@lowersections
8493@end ifclear
8494@end ifset
8495
e0001a05
NC
8496@ifset XTENSA
8497@ifclear GENERIC
8498@raisesections
8499@end ifclear
8500
8501@node Xtensa
8502@section @code{ld} and Xtensa Processors
8503
8504@cindex Xtensa processors
8505The default @command{ld} behavior for Xtensa processors is to interpret
8506@code{SECTIONS} commands so that lists of explicitly named sections in a
8507specification with a wildcard file will be interleaved when necessary to
8508keep literal pools within the range of PC-relative load offsets. For
8509example, with the command:
8510
8511@smallexample
8512SECTIONS
8513@{
8514 .text : @{
8515 *(.literal .text)
8516 @}
8517@}
8518@end smallexample
8519
8520@noindent
8521@command{ld} may interleave some of the @code{.literal}
8522and @code{.text} sections from different object files to ensure that the
8523literal pools are within the range of PC-relative load offsets. A valid
8524interleaving might place the @code{.literal} sections from an initial
8525group of files followed by the @code{.text} sections of that group of
8526files. Then, the @code{.literal} sections from the rest of the files
8527and the @code{.text} sections from the rest of the files would follow.
e0001a05 8528
43cd72b9 8529@cindex @option{--relax} on Xtensa
e0001a05 8530@cindex relaxing on Xtensa
43cd72b9
BW
8531Relaxation is enabled by default for the Xtensa version of @command{ld} and
8532provides two important link-time optimizations. The first optimization
8533is to combine identical literal values to reduce code size. A redundant
8534literal will be removed and all the @code{L32R} instructions that use it
8535will be changed to reference an identical literal, as long as the
8536location of the replacement literal is within the offset range of all
8537the @code{L32R} instructions. The second optimization is to remove
8538unnecessary overhead from assembler-generated ``longcall'' sequences of
8539@code{L32R}/@code{CALLX@var{n}} when the target functions are within
8540range of direct @code{CALL@var{n}} instructions.
8541
8542For each of these cases where an indirect call sequence can be optimized
8543to a direct call, the linker will change the @code{CALLX@var{n}}
8544instruction to a @code{CALL@var{n}} instruction, remove the @code{L32R}
8545instruction, and remove the literal referenced by the @code{L32R}
8546instruction if it is not used for anything else. Removing the
8547@code{L32R} instruction always reduces code size but can potentially
8548hurt performance by changing the alignment of subsequent branch targets.
8549By default, the linker will always preserve alignments, either by
8550switching some instructions between 24-bit encodings and the equivalent
8551density instructions or by inserting a no-op in place of the @code{L32R}
8552instruction that was removed. If code size is more important than
8553performance, the @option{--size-opt} option can be used to prevent the
8554linker from widening density instructions or inserting no-ops, except in
8555a few cases where no-ops are required for correctness.
8556
8557The following Xtensa-specific command-line options can be used to
8558control the linker:
8559
8560@cindex Xtensa options
8561@table @option
43cd72b9
BW
8562@item --size-opt
8563When optimizing indirect calls to direct calls, optimize for code size
8564more than performance. With this option, the linker will not insert
8565no-ops or widen density instructions to preserve branch target
8566alignment. There may still be some cases where no-ops are required to
8567preserve the correctness of the code.
7a77f1ac
MF
8568
8569@item --abi-windowed
8570@itemx --abi-call0
8571Choose ABI for the output object and for the generated PLT code.
8572PLT code inserted by the linker must match ABI of the output object
8573because windowed and call0 ABI use incompatible function call
8574conventions.
8575Default ABI is chosen by the ABI tag in the @code{.xtensa.info} section
8576of the first input object.
8577A warning is issued if ABI tags of input objects do not match each other
8578or the chosen output object ABI.
43cd72b9 8579@end table
e0001a05
NC
8580
8581@ifclear GENERIC
8582@lowersections
8583@end ifclear
8584@end ifset
8585
252b5132
RH
8586@ifclear SingleFormat
8587@node BFD
8588@chapter BFD
8589
8590@cindex back end
8591@cindex object file management
8592@cindex object formats available
8593@kindex objdump -i
8594The linker accesses object and archive files using the BFD libraries.
8595These libraries allow the linker to use the same routines to operate on
8596object files whatever the object file format. A different object file
8597format can be supported simply by creating a new BFD back end and adding
8598it to the library. To conserve runtime memory, however, the linker and
8599associated tools are usually configured to support only a subset of the
8600object file formats available. You can use @code{objdump -i}
8601(@pxref{objdump,,objdump,binutils.info,The GNU Binary Utilities}) to
8602list all the formats available for your configuration.
8603
8604@cindex BFD requirements
8605@cindex requirements for BFD
8606As with most implementations, BFD is a compromise between
8607several conflicting requirements. The major factor influencing
8608BFD design was efficiency: any time used converting between
8609formats is time which would not have been spent had BFD not
8610been involved. This is partly offset by abstraction payback; since
8611BFD simplifies applications and back ends, more time and care
8612may be spent optimizing algorithms for a greater speed.
8613
8614One minor artifact of the BFD solution which you should bear in
8615mind is the potential for information loss. There are two places where
8616useful information can be lost using the BFD mechanism: during
8617conversion and during output. @xref{BFD information loss}.
8618
8619@menu
8620* BFD outline:: How it works: an outline of BFD
8621@end menu
8622
8623@node BFD outline
36f63dca 8624@section How It Works: An Outline of BFD
252b5132
RH
8625@cindex opening object files
8626@include bfdsumm.texi
8627@end ifclear
8628
8629@node Reporting Bugs
8630@chapter Reporting Bugs
ff5dcc92
SC
8631@cindex bugs in @command{ld}
8632@cindex reporting bugs in @command{ld}
252b5132 8633
ff5dcc92 8634Your bug reports play an essential role in making @command{ld} reliable.
252b5132
RH
8635
8636Reporting a bug may help you by bringing a solution to your problem, or
8637it may not. But in any case the principal function of a bug report is
ff5dcc92 8638to help the entire community by making the next version of @command{ld}
252b5132 8639work better. Bug reports are your contribution to the maintenance of
ff5dcc92 8640@command{ld}.
252b5132
RH
8641
8642In order for a bug report to serve its purpose, you must include the
8643information that enables us to fix the bug.
8644
8645@menu
8646* Bug Criteria:: Have you found a bug?
8647* Bug Reporting:: How to report bugs
8648@end menu
8649
8650@node Bug Criteria
36f63dca 8651@section Have You Found a Bug?
252b5132
RH
8652@cindex bug criteria
8653
8654If you are not sure whether you have found a bug, here are some guidelines:
8655
8656@itemize @bullet
8657@cindex fatal signal
8658@cindex linker crash
8659@cindex crash of linker
8660@item
8661If the linker gets a fatal signal, for any input whatever, that is a
ff5dcc92 8662@command{ld} bug. Reliable linkers never crash.
252b5132
RH
8663
8664@cindex error on valid input
8665@item
ff5dcc92 8666If @command{ld} produces an error message for valid input, that is a bug.
252b5132
RH
8667
8668@cindex invalid input
8669@item
ff5dcc92 8670If @command{ld} does not produce an error message for invalid input, that
252b5132
RH
8671may be a bug. In the general case, the linker can not verify that
8672object files are correct.
8673
8674@item
8675If you are an experienced user of linkers, your suggestions for
ff5dcc92 8676improvement of @command{ld} are welcome in any case.
252b5132
RH
8677@end itemize
8678
8679@node Bug Reporting
36f63dca 8680@section How to Report Bugs
252b5132 8681@cindex bug reports
ff5dcc92 8682@cindex @command{ld} bugs, reporting
252b5132
RH
8683
8684A number of companies and individuals offer support for @sc{gnu}
ff5dcc92 8685products. If you obtained @command{ld} from a support organization, we
252b5132
RH
8686recommend you contact that organization first.
8687
8688You can find contact information for many support companies and
8689individuals in the file @file{etc/SERVICE} in the @sc{gnu} Emacs
8690distribution.
8691
ad22bfe8 8692@ifset BUGURL
ff5dcc92 8693Otherwise, send bug reports for @command{ld} to
ad22bfe8
JM
8694@value{BUGURL}.
8695@end ifset
252b5132
RH
8696
8697The fundamental principle of reporting bugs usefully is this:
8698@strong{report all the facts}. If you are not sure whether to state a
8699fact or leave it out, state it!
8700
8701Often people omit facts because they think they know what causes the
8702problem and assume that some details do not matter. Thus, you might
b553b183
NC
8703assume that the name of a symbol you use in an example does not
8704matter. Well, probably it does not, but one cannot be sure. Perhaps
8705the bug is a stray memory reference which happens to fetch from the
8706location where that name is stored in memory; perhaps, if the name
8707were different, the contents of that location would fool the linker
8708into doing the right thing despite the bug. Play it safe and give a
8709specific, complete example. That is the easiest thing for you to do,
c0065db7 8710and the most helpful.
b553b183
NC
8711
8712Keep in mind that the purpose of a bug report is to enable us to fix
8713the bug if it is new to us. Therefore, always write your bug reports
8714on the assumption that the bug has not been reported previously.
252b5132
RH
8715
8716Sometimes people give a few sketchy facts and ask, ``Does this ring a
36f63dca
NC
8717bell?'' This cannot help us fix a bug, so it is basically useless. We
8718respond by asking for enough details to enable us to investigate.
8719You might as well expedite matters by sending them to begin with.
252b5132
RH
8720
8721To enable us to fix the bug, you should include all these things:
8722
8723@itemize @bullet
8724@item
ff5dcc92 8725The version of @command{ld}. @command{ld} announces it if you start it with
252b5132
RH
8726the @samp{--version} argument.
8727
8728Without this, we will not know whether there is any point in looking for
ff5dcc92 8729the bug in the current version of @command{ld}.
252b5132
RH
8730
8731@item
ff5dcc92 8732Any patches you may have applied to the @command{ld} source, including any
252b5132
RH
8733patches made to the @code{BFD} library.
8734
8735@item
8736The type of machine you are using, and the operating system name and
8737version number.
8738
8739@item
ff5dcc92 8740What compiler (and its version) was used to compile @command{ld}---e.g.
252b5132
RH
8741``@code{gcc-2.7}''.
8742
8743@item
8744The command arguments you gave the linker to link your example and
8745observe the bug. To guarantee you will not omit something important,
8746list them all. A copy of the Makefile (or the output from make) is
8747sufficient.
8748
8749If we were to try to guess the arguments, we would probably guess wrong
8750and then we might not encounter the bug.
8751
8752@item
8753A complete input file, or set of input files, that will reproduce the
b553b183
NC
8754bug. It is generally most helpful to send the actual object files
8755provided that they are reasonably small. Say no more than 10K. For
8756bigger files you can either make them available by FTP or HTTP or else
8757state that you are willing to send the object file(s) to whomever
8758requests them. (Note - your email will be going to a mailing list, so
8759we do not want to clog it up with large attachments). But small
8760attachments are best.
252b5132
RH
8761
8762If the source files were assembled using @code{gas} or compiled using
8763@code{gcc}, then it may be OK to send the source files rather than the
8764object files. In this case, be sure to say exactly what version of
8765@code{gas} or @code{gcc} was used to produce the object files. Also say
8766how @code{gas} or @code{gcc} were configured.
8767
8768@item
8769A description of what behavior you observe that you believe is
8770incorrect. For example, ``It gets a fatal signal.''
8771
ff5dcc92 8772Of course, if the bug is that @command{ld} gets a fatal signal, then we
252b5132
RH
8773will certainly notice it. But if the bug is incorrect output, we might
8774not notice unless it is glaringly wrong. You might as well not give us
8775a chance to make a mistake.
8776
8777Even if the problem you experience is a fatal signal, you should still
8778say so explicitly. Suppose something strange is going on, such as, your
b45619c0 8779copy of @command{ld} is out of sync, or you have encountered a bug in the
252b5132
RH
8780C library on your system. (This has happened!) Your copy might crash
8781and ours would not. If you told us to expect a crash, then when ours
8782fails to crash, we would know that the bug was not happening for us. If
8783you had not told us to expect a crash, then we would not be able to draw
8784any conclusion from our observations.
8785
8786@item
ff5dcc92 8787If you wish to suggest changes to the @command{ld} source, send us context
252b5132
RH
8788diffs, as generated by @code{diff} with the @samp{-u}, @samp{-c}, or
8789@samp{-p} option. Always send diffs from the old file to the new file.
ff5dcc92 8790If you even discuss something in the @command{ld} source, refer to it by
252b5132
RH
8791context, not by line number.
8792
8793The line numbers in our development sources will not match those in your
8794sources. Your line numbers would convey no useful information to us.
8795@end itemize
8796
8797Here are some things that are not necessary:
8798
8799@itemize @bullet
8800@item
8801A description of the envelope of the bug.
8802
8803Often people who encounter a bug spend a lot of time investigating
8804which changes to the input file will make the bug go away and which
8805changes will not affect it.
8806
8807This is often time consuming and not very useful, because the way we
8808will find the bug is by running a single example under the debugger
8809with breakpoints, not by pure deduction from a series of examples.
8810We recommend that you save your time for something else.
8811
8812Of course, if you can find a simpler example to report @emph{instead}
8813of the original one, that is a convenience for us. Errors in the
8814output will be easier to spot, running under the debugger will take
8815less time, and so on.
8816
8817However, simplification is not vital; if you do not want to do this,
8818report the bug anyway and send us the entire test case you used.
8819
8820@item
8821A patch for the bug.
8822
8823A patch for the bug does help us if it is a good one. But do not omit
8824the necessary information, such as the test case, on the assumption that
8825a patch is all we need. We might see problems with your patch and decide
8826to fix the problem another way, or we might not understand it at all.
8827
ff5dcc92 8828Sometimes with a program as complicated as @command{ld} it is very hard to
252b5132
RH
8829construct an example that will make the program follow a certain path
8830through the code. If you do not send us the example, we will not be
8831able to construct one, so we will not be able to verify that the bug is
8832fixed.
8833
8834And if we cannot understand what bug you are trying to fix, or why your
8835patch should be an improvement, we will not install it. A test case will
8836help us to understand.
8837
8838@item
8839A guess about what the bug is or what it depends on.
8840
8841Such guesses are usually wrong. Even we cannot guess right about such
8842things without first using the debugger to find the facts.
8843@end itemize
8844
8845@node MRI
8846@appendix MRI Compatible Script Files
8847@cindex MRI compatibility
ff5dcc92
SC
8848To aid users making the transition to @sc{gnu} @command{ld} from the MRI
8849linker, @command{ld} can use MRI compatible linker scripts as an
252b5132
RH
8850alternative to the more general-purpose linker scripting language
8851described in @ref{Scripts}. MRI compatible linker scripts have a much
8852simpler command set than the scripting language otherwise used with
ff5dcc92 8853@command{ld}. @sc{gnu} @command{ld} supports the most commonly used MRI
252b5132
RH
8854linker commands; these commands are described here.
8855
8856In general, MRI scripts aren't of much use with the @code{a.out} object
8857file format, since it only has three sections and MRI scripts lack some
8858features to make use of them.
8859
8860You can specify a file containing an MRI-compatible script using the
8861@samp{-c} command-line option.
8862
8863Each command in an MRI-compatible script occupies its own line; each
8864command line starts with the keyword that identifies the command (though
8865blank lines are also allowed for punctuation). If a line of an
ff5dcc92 8866MRI-compatible script begins with an unrecognized keyword, @command{ld}
252b5132
RH
8867issues a warning message, but continues processing the script.
8868
8869Lines beginning with @samp{*} are comments.
8870
8871You can write these commands using all upper-case letters, or all
8872lower case; for example, @samp{chip} is the same as @samp{CHIP}.
8873The following list shows only the upper-case form of each command.
8874
8875@table @code
8876@cindex @code{ABSOLUTE} (MRI)
8877@item ABSOLUTE @var{secname}
8878@itemx ABSOLUTE @var{secname}, @var{secname}, @dots{} @var{secname}
ff5dcc92 8879Normally, @command{ld} includes in the output file all sections from all
252b5132
RH
8880the input files. However, in an MRI-compatible script, you can use the
8881@code{ABSOLUTE} command to restrict the sections that will be present in
8882your output program. If the @code{ABSOLUTE} command is used at all in a
8883script, then only the sections named explicitly in @code{ABSOLUTE}
8884commands will appear in the linker output. You can still use other
8885input sections (whatever you select on the command line, or using
8886@code{LOAD}) to resolve addresses in the output file.
8887
8888@cindex @code{ALIAS} (MRI)
8889@item ALIAS @var{out-secname}, @var{in-secname}
8890Use this command to place the data from input section @var{in-secname}
8891in a section called @var{out-secname} in the linker output file.
8892
8893@var{in-secname} may be an integer.
8894
8895@cindex @code{ALIGN} (MRI)
8896@item ALIGN @var{secname} = @var{expression}
8897Align the section called @var{secname} to @var{expression}. The
8898@var{expression} should be a power of two.
8899
8900@cindex @code{BASE} (MRI)
8901@item BASE @var{expression}
8902Use the value of @var{expression} as the lowest address (other than
8903absolute addresses) in the output file.
8904
8905@cindex @code{CHIP} (MRI)
8906@item CHIP @var{expression}
8907@itemx CHIP @var{expression}, @var{expression}
8908This command does nothing; it is accepted only for compatibility.
8909
8910@cindex @code{END} (MRI)
8911@item END
8912This command does nothing whatever; it's only accepted for compatibility.
8913
8914@cindex @code{FORMAT} (MRI)
8915@item FORMAT @var{output-format}
8916Similar to the @code{OUTPUT_FORMAT} command in the more general linker
dc12032b 8917language, but restricted to S-records, if @var{output-format} is @samp{S}
252b5132
RH
8918
8919@cindex @code{LIST} (MRI)
8920@item LIST @var{anything}@dots{}
8921Print (to the standard output file) a link map, as produced by the
ff5dcc92 8922@command{ld} command-line option @samp{-M}.
252b5132
RH
8923
8924The keyword @code{LIST} may be followed by anything on the
8925same line, with no change in its effect.
8926
8927@cindex @code{LOAD} (MRI)
8928@item LOAD @var{filename}
8929@itemx LOAD @var{filename}, @var{filename}, @dots{} @var{filename}
8930Include one or more object file @var{filename} in the link; this has the
ff5dcc92 8931same effect as specifying @var{filename} directly on the @command{ld}
252b5132
RH
8932command line.
8933
8934@cindex @code{NAME} (MRI)
8935@item NAME @var{output-name}
ff5dcc92 8936@var{output-name} is the name for the program produced by @command{ld}; the
252b5132
RH
8937MRI-compatible command @code{NAME} is equivalent to the command-line
8938option @samp{-o} or the general script language command @code{OUTPUT}.
8939
8940@cindex @code{ORDER} (MRI)
8941@item ORDER @var{secname}, @var{secname}, @dots{} @var{secname}
8942@itemx ORDER @var{secname} @var{secname} @var{secname}
ff5dcc92 8943Normally, @command{ld} orders the sections in its output file in the
252b5132
RH
8944order in which they first appear in the input files. In an MRI-compatible
8945script, you can override this ordering with the @code{ORDER} command. The
8946sections you list with @code{ORDER} will appear first in your output
8947file, in the order specified.
8948
8949@cindex @code{PUBLIC} (MRI)
8950@item PUBLIC @var{name}=@var{expression}
8951@itemx PUBLIC @var{name},@var{expression}
8952@itemx PUBLIC @var{name} @var{expression}
8953Supply a value (@var{expression}) for external symbol
8954@var{name} used in the linker input files.
8955
8956@cindex @code{SECT} (MRI)
8957@item SECT @var{secname}, @var{expression}
8958@itemx SECT @var{secname}=@var{expression}
8959@itemx SECT @var{secname} @var{expression}
8960You can use any of these three forms of the @code{SECT} command to
8961specify the start address (@var{expression}) for section @var{secname}.
8962If you have more than one @code{SECT} statement for the same
8963@var{secname}, only the @emph{first} sets the start address.
8964@end table
8965
793c5807
NC
8966@node GNU Free Documentation License
8967@appendix GNU Free Documentation License
36f63dca 8968@include fdl.texi
704c465c 8969
370b66a1
CD
8970@node LD Index
8971@unnumbered LD Index
252b5132
RH
8972
8973@printindex cp
8974
8975@tex
7ca01ed9 8976% I think something like @@colophon should be in texinfo. In the
252b5132
RH
8977% meantime:
8978\long\def\colophon{\hbox to0pt{}\vfill
8979\centerline{The body of this manual is set in}
8980\centerline{\fontname\tenrm,}
8981\centerline{with headings in {\bf\fontname\tenbf}}
8982\centerline{and examples in {\tt\fontname\tentt}.}
8983\centerline{{\it\fontname\tenit\/} and}
8984\centerline{{\sl\fontname\tensl\/}}
8985\centerline{are used for emphasis.}\vfill}
8986\page\colophon
7ca01ed9 8987% Blame: doc@@cygnus.com, 28mar91.
252b5132
RH
8988@end tex
8989
252b5132 8990@bye
This page took 1.932852 seconds and 4 git commands to generate.