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