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