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