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