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