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