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