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