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