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