ld/
[deliverable/binutils-gdb.git] / ld / ld.texinfo
1 \input texinfo
2 @setfilename ld.info
3 @c Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
4 @c 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
5 @syncodeindex ky cp
6 @c man begin INCLUDE
7 @include configdoc.texi
8 @c (configdoc.texi is generated by the Makefile)
9 @include ldver.texi
10 @c man end
11
12 @c @smallbook
13
14 @macro gcctabopt{body}
15 @code{\body\}
16 @end macro
17
18 @c man begin NAME
19 @ifset man
20 @c Configure for the generation of man pages
21 @set UsesEnvVars
22 @set GENERIC
23 @set ARC
24 @set ARM
25 @set D10V
26 @set D30V
27 @set H8/300
28 @set H8/500
29 @set HPPA
30 @set I370
31 @set I80386
32 @set I860
33 @set I960
34 @set M32R
35 @set M68HC11
36 @set M680X0
37 @set MCORE
38 @set MIPS
39 @set MMIX
40 @set MSP430
41 @set PDP11
42 @set PJ
43 @set POWERPC
44 @set POWERPC64
45 @set SH
46 @set SPARC
47 @set TIC54X
48 @set V850
49 @set VAX
50 @set WIN32
51 @set XTENSA
52 @end ifset
53 @c man end
54
55 @ifinfo
56 @format
57 START-INFO-DIR-ENTRY
58 * Ld: (ld). The GNU linker.
59 END-INFO-DIR-ENTRY
60 @end format
61 @end ifinfo
62
63 @ifinfo
64 This file documents the @sc{gnu} linker LD version @value{VERSION}.
65
66 Copyright (C) 1991, 92, 93, 94, 95, 96, 97, 98, 99, 2000,
67 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
68
69 @ignore
70
71 Permission is granted to copy, distribute and/or modify this document
72 under the terms of the GNU Free Documentation License, Version 1.1
73 or any later version published by the Free Software Foundation;
74 with no Invariant Sections, with no Front-Cover Texts, and with no
75 Back-Cover Texts. A copy of the license is included in the
76 section entitled ``GNU Free Documentation License''.
77
78 Permission is granted to process this file through Tex and print the
79 results, provided the printed document carries copying permission
80 notice identical to this one except for the removal of this paragraph
81 (this paragraph not being relevant to the printed manual).
82
83 @end ignore
84 @end ifinfo
85 @iftex
86 @finalout
87 @setchapternewpage odd
88 @settitle The GNU linker
89 @titlepage
90 @title The GNU linker
91 @sp 1
92 @subtitle @code{ld} version 2
93 @subtitle Version @value{VERSION}
94 @author Steve Chamberlain
95 @author Ian Lance Taylor
96 @page
97
98 @tex
99 {\parskip=0pt
100 \hfill Red Hat Inc\par
101 \hfill nickc\@credhat.com, doc\@redhat.com\par
102 \hfill {\it The GNU linker}\par
103 \hfill Edited by Jeffrey Osier (jeffrey\@cygnus.com)\par
104 }
105 \global\parindent=0pt % Steve likes it this way.
106 @end tex
107
108 @vskip 0pt plus 1filll
109 @c man begin COPYRIGHT
110 Copyright @copyright{} 1991, 92, 93, 94, 95, 96, 97, 98, 99, 2000, 2001,
111 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
112
113 Permission is granted to copy, distribute and/or modify this document
114 under the terms of the GNU Free Documentation License, Version 1.1
115 or any later version published by the Free Software Foundation;
116 with no Invariant Sections, with no Front-Cover Texts, and with no
117 Back-Cover Texts. A copy of the license is included in the
118 section entitled ``GNU Free Documentation License''.
119 @c man end
120
121 @end titlepage
122 @end iftex
123 @c FIXME: Talk about importance of *order* of args, cmds to linker!
124
125 @ifnottex
126 @node Top
127 @top LD
128 This file documents the @sc{gnu} linker ld version @value{VERSION}.
129
130 This document is distributed under the terms of the GNU Free
131 Documentation License. A copy of the license is included in the
132 section entitled ``GNU Free Documentation License''.
133
134 @menu
135 * Overview:: Overview
136 * Invocation:: Invocation
137 * Scripts:: Linker Scripts
138 @ifset GENERIC
139 * Machine Dependent:: Machine Dependent Features
140 @end ifset
141 @ifclear GENERIC
142 @ifset H8300
143 * H8/300:: ld and the H8/300
144 @end ifset
145 @ifset Renesas
146 * Renesas:: ld and other Renesas micros
147 @end ifset
148 @ifset I960
149 * i960:: ld and the Intel 960 family
150 @end ifset
151 @ifset ARM
152 * ARM:: ld and the ARM family
153 @end ifset
154 @ifset HPPA
155 * HPPA ELF32:: ld and HPPA 32-bit ELF
156 @end ifset
157 @ifset M68HC11
158 * M68HC11/68HC12:: ld and the Motorola 68HC11 and 68HC12 families
159 @end ifset
160 @ifset POWERPC
161 * PowerPC ELF32:: ld and PowerPC 32-bit ELF Support
162 @end ifset
163 @ifset POWERPC64
164 * PowerPC64 ELF64:: ld and PowerPC64 64-bit 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. Note that
310 specifying a script in this way merely augments the main linker script;
311 use the @samp{-T} option to replace the default linker script entirely.
312 @xref{Scripts}.
313
314 For options whose names are a single letter,
315 option arguments must either follow the option letter without intervening
316 whitespace, or be given as separate arguments immediately following the
317 option that requires them.
318
319 For options whose names are multiple letters, either one dash or two can
320 precede the option name; for example, @samp{-trace-symbol} and
321 @samp{--trace-symbol} are equivalent. Note---there is one exception to
322 this rule. Multiple letter options that start with a lower case 'o' can
323 only be preceded by two dashes. This is to reduce confusion with the
324 @samp{-o} option. So for example @samp{-omagic} sets the output file
325 name to @samp{magic} whereas @samp{--omagic} sets the NMAGIC flag on the
326 output.
327
328 Arguments to multiple-letter options must either be separated from the
329 option name by an equals sign, or be given as separate arguments
330 immediately following the option that requires them. For example,
331 @samp{--trace-symbol foo} and @samp{--trace-symbol=foo} are equivalent.
332 Unique abbreviations of the names of multiple-letter options are
333 accepted.
334
335 Note---if the linker is being invoked indirectly, via a compiler driver
336 (e.g. @samp{gcc}) then all the linker command line options should be
337 prefixed by @samp{-Wl,} (or whatever is appropriate for the particular
338 compiler driver) like this:
339
340 @smallexample
341 gcc -Wl,--startgroup foo.o bar.o -Wl,--endgroup
342 @end smallexample
343
344 This is important, because otherwise the compiler driver program may
345 silently drop the linker options, resulting in a bad link.
346
347 Here is a table of the generic command line switches accepted by the GNU
348 linker:
349
350 @table @gcctabopt
351 @include at-file.texi
352
353 @kindex -a@var{keyword}
354 @item -a@var{keyword}
355 This option is supported for HP/UX compatibility. The @var{keyword}
356 argument must be one of the strings @samp{archive}, @samp{shared}, or
357 @samp{default}. @samp{-aarchive} is functionally equivalent to
358 @samp{-Bstatic}, and the other two keywords are functionally equivalent
359 to @samp{-Bdynamic}. This option may be used any number of times.
360
361 @ifset I960
362 @cindex architectures
363 @kindex -A@var{arch}
364 @item -A@var{architecture}
365 @kindex --architecture=@var{arch}
366 @itemx --architecture=@var{architecture}
367 In the current release of @command{ld}, this option is useful only for the
368 Intel 960 family of architectures. In that @command{ld} configuration, the
369 @var{architecture} argument identifies the particular architecture in
370 the 960 family, enabling some safeguards and modifying the
371 archive-library search path. @xref{i960,,@command{ld} and the Intel 960
372 family}, for details.
373
374 Future releases of @command{ld} may support similar functionality for
375 other architecture families.
376 @end ifset
377
378 @ifclear SingleFormat
379 @cindex binary input format
380 @kindex -b @var{format}
381 @kindex --format=@var{format}
382 @cindex input format
383 @cindex input format
384 @item -b @var{input-format}
385 @itemx --format=@var{input-format}
386 @command{ld} may be configured to support more than one kind of object
387 file. If your @command{ld} is configured this way, you can use the
388 @samp{-b} option to specify the binary format for input object files
389 that follow this option on the command line. Even when @command{ld} is
390 configured to support alternative object formats, you don't usually need
391 to specify this, as @command{ld} should be configured to expect as a
392 default input format the most usual format on each machine.
393 @var{input-format} is a text string, the name of a particular format
394 supported by the BFD libraries. (You can list the available binary
395 formats with @samp{objdump -i}.)
396 @xref{BFD}.
397
398 You may want to use this option if you are linking files with an unusual
399 binary format. You can also use @samp{-b} to switch formats explicitly (when
400 linking object files of different formats), by including
401 @samp{-b @var{input-format}} before each group of object files in a
402 particular format.
403
404 The default format is taken from the environment variable
405 @code{GNUTARGET}.
406 @ifset UsesEnvVars
407 @xref{Environment}.
408 @end ifset
409 You can also define the input format from a script, using the command
410 @code{TARGET};
411 @ifclear man
412 see @ref{Format Commands}.
413 @end ifclear
414 @end ifclear
415
416 @kindex -c @var{MRI-cmdfile}
417 @kindex --mri-script=@var{MRI-cmdfile}
418 @cindex compatibility, MRI
419 @item -c @var{MRI-commandfile}
420 @itemx --mri-script=@var{MRI-commandfile}
421 For compatibility with linkers produced by MRI, @command{ld} accepts script
422 files written in an alternate, restricted command language, described in
423 @ifclear man
424 @ref{MRI,,MRI Compatible Script Files}.
425 @end ifclear
426 @ifset man
427 the MRI Compatible Script Files section of GNU ld documentation.
428 @end ifset
429 Introduce MRI script files with
430 the option @samp{-c}; use the @samp{-T} option to run linker
431 scripts written in the general-purpose @command{ld} scripting language.
432 If @var{MRI-cmdfile} does not exist, @command{ld} looks for it in the directories
433 specified by any @samp{-L} options.
434
435 @cindex common allocation
436 @kindex -d
437 @kindex -dc
438 @kindex -dp
439 @item -d
440 @itemx -dc
441 @itemx -dp
442 These three options are equivalent; multiple forms are supported for
443 compatibility with other linkers. They assign space to common symbols
444 even if a relocatable output file is specified (with @samp{-r}). The
445 script command @code{FORCE_COMMON_ALLOCATION} has the same effect.
446 @xref{Miscellaneous Commands}.
447
448 @cindex entry point, from command line
449 @kindex -e @var{entry}
450 @kindex --entry=@var{entry}
451 @item -e @var{entry}
452 @itemx --entry=@var{entry}
453 Use @var{entry} as the explicit symbol for beginning execution of your
454 program, rather than the default entry point. If there is no symbol
455 named @var{entry}, the linker will try to parse @var{entry} as a number,
456 and use that as the entry address (the number will be interpreted in
457 base 10; you may use a leading @samp{0x} for base 16, or a leading
458 @samp{0} for base 8). @xref{Entry Point}, for a discussion of defaults
459 and other ways of specifying the entry point.
460
461 @kindex --exclude-libs
462 @item --exclude-libs @var{lib},@var{lib},...
463 Specifies a list of archive libraries from which symbols should not be automatically
464 exported. The library names may be delimited by commas or colons. Specifying
465 @code{--exclude-libs ALL} excludes symbols in all archive libraries from
466 automatic export. This option is available only for the i386 PE targeted
467 port of the linker and for ELF targeted ports. For i386 PE, symbols
468 explicitly listed in a .def file are still exported, regardless of this
469 option. For ELF targeted ports, symbols affected by this option will
470 be treated as hidden.
471
472 @cindex dynamic symbol table
473 @kindex -E
474 @kindex --export-dynamic
475 @item -E
476 @itemx --export-dynamic
477 When creating a dynamically linked executable, add all symbols to the
478 dynamic symbol table. The dynamic symbol table is the set of symbols
479 which are visible from dynamic objects at run time.
480
481 If you do not use this option, the dynamic symbol table will normally
482 contain only those symbols which are referenced by some dynamic object
483 mentioned in the link.
484
485 If you use @code{dlopen} to load a dynamic object which needs to refer
486 back to the symbols defined by the program, rather than some other
487 dynamic object, then you will probably need to use this option when
488 linking the program itself.
489
490 You can also use the dynamic list to control what symbols should
491 be added to the dynamic symbol table if the output format supports it.
492 See the description of @samp{--dynamic-list}.
493
494 @ifclear SingleFormat
495 @cindex big-endian objects
496 @cindex endianness
497 @kindex -EB
498 @item -EB
499 Link big-endian objects. This affects the default output format.
500
501 @cindex little-endian objects
502 @kindex -EL
503 @item -EL
504 Link little-endian objects. This affects the default output format.
505 @end ifclear
506
507 @kindex -f
508 @kindex --auxiliary
509 @item -f
510 @itemx --auxiliary @var{name}
511 When creating an ELF shared object, set the internal DT_AUXILIARY field
512 to the specified name. This tells the dynamic linker that the symbol
513 table of the shared object should be used as an auxiliary filter on the
514 symbol table of the shared object @var{name}.
515
516 If you later link a program against this filter object, then, when you
517 run the program, the dynamic linker will see the DT_AUXILIARY field. If
518 the dynamic linker resolves any symbols from the filter object, it will
519 first check whether there is a definition in the shared object
520 @var{name}. If there is one, it will be used instead of the definition
521 in the filter object. The shared object @var{name} need not exist.
522 Thus the shared object @var{name} may be used to provide an alternative
523 implementation of certain functions, perhaps for debugging or for
524 machine specific performance.
525
526 This option may be specified more than once. The DT_AUXILIARY entries
527 will be created in the order in which they appear on the command line.
528
529 @kindex -F
530 @kindex --filter
531 @item -F @var{name}
532 @itemx --filter @var{name}
533 When creating an ELF shared object, set the internal DT_FILTER field to
534 the specified name. This tells the dynamic linker that the symbol table
535 of the shared object which is being created should be used as a filter
536 on the symbol table of the shared object @var{name}.
537
538 If you later link a program against this filter object, then, when you
539 run the program, the dynamic linker will see the DT_FILTER field. The
540 dynamic linker will resolve symbols according to the symbol table of the
541 filter object as usual, but it will actually link to the definitions
542 found in the shared object @var{name}. Thus the filter object can be
543 used to select a subset of the symbols provided by the object
544 @var{name}.
545
546 Some older linkers used the @option{-F} option throughout a compilation
547 toolchain for specifying object-file format for both input and output
548 object files.
549 @ifclear SingleFormat
550 The @sc{gnu} linker uses other mechanisms for this purpose: the
551 @option{-b}, @option{--format}, @option{--oformat} options, the
552 @code{TARGET} command in linker scripts, and the @code{GNUTARGET}
553 environment variable.
554 @end ifclear
555 The @sc{gnu} linker will ignore the @option{-F} option when not
556 creating an ELF shared object.
557
558 @cindex finalization function
559 @kindex -fini
560 @item -fini @var{name}
561 When creating an ELF executable or shared object, call NAME when the
562 executable or shared object is unloaded, by setting DT_FINI to the
563 address of the function. By default, the linker uses @code{_fini} as
564 the function to call.
565
566 @kindex -g
567 @item -g
568 Ignored. Provided for compatibility with other tools.
569
570 @kindex -G
571 @kindex --gpsize
572 @cindex object size
573 @item -G@var{value}
574 @itemx --gpsize=@var{value}
575 Set the maximum size of objects to be optimized using the GP register to
576 @var{size}. This is only meaningful for object file formats such as
577 MIPS ECOFF which supports putting large and small objects into different
578 sections. This is ignored for other object file formats.
579
580 @cindex runtime library name
581 @kindex -h@var{name}
582 @kindex -soname=@var{name}
583 @item -h@var{name}
584 @itemx -soname=@var{name}
585 When creating an ELF shared object, set the internal DT_SONAME field to
586 the specified name. When an executable is linked with a shared object
587 which has a DT_SONAME field, then when the executable is run the dynamic
588 linker will attempt to load the shared object specified by the DT_SONAME
589 field rather than the using the file name given to the linker.
590
591 @kindex -i
592 @cindex incremental link
593 @item -i
594 Perform an incremental link (same as option @samp{-r}).
595
596 @cindex initialization function
597 @kindex -init
598 @item -init @var{name}
599 When creating an ELF executable or shared object, call NAME when the
600 executable or shared object is loaded, by setting DT_INIT to the address
601 of the function. By default, the linker uses @code{_init} as the
602 function to call.
603
604 @cindex archive files, from cmd line
605 @kindex -l@var{archive}
606 @kindex --library=@var{archive}
607 @item -l@var{archive}
608 @itemx --library=@var{archive}
609 Add archive file @var{archive} to the list of files to link. This
610 option may be used any number of times. @command{ld} will search its
611 path-list for occurrences of @code{lib@var{archive}.a} for every
612 @var{archive} specified.
613
614 On systems which support shared libraries, @command{ld} may also search for
615 libraries with extensions other than @code{.a}. Specifically, on ELF
616 and SunOS systems, @command{ld} will search a directory for a library with
617 an extension of @code{.so} before searching for one with an extension of
618 @code{.a}. By convention, a @code{.so} extension indicates a shared
619 library.
620
621 The linker will search an archive only once, at the location where it is
622 specified on the command line. If the archive defines a symbol which
623 was undefined in some object which appeared before the archive on the
624 command line, the linker will include the appropriate file(s) from the
625 archive. However, an undefined symbol in an object appearing later on
626 the command line will not cause the linker to search the archive again.
627
628 See the @option{-(} option for a way to force the linker to search
629 archives multiple times.
630
631 You may list the same archive multiple times on the command line.
632
633 @ifset GENERIC
634 This type of archive searching is standard for Unix linkers. However,
635 if you are using @command{ld} on AIX, note that it is different from the
636 behaviour of the AIX linker.
637 @end ifset
638
639 @cindex search directory, from cmd line
640 @kindex -L@var{dir}
641 @kindex --library-path=@var{dir}
642 @item -L@var{searchdir}
643 @itemx --library-path=@var{searchdir}
644 Add path @var{searchdir} to the list of paths that @command{ld} will search
645 for archive libraries and @command{ld} control scripts. You may use this
646 option any number of times. The directories are searched in the order
647 in which they are specified on the command line. Directories specified
648 on the command line are searched before the default directories. All
649 @option{-L} options apply to all @option{-l} options, regardless of the
650 order in which the options appear.
651
652 If @var{searchdir} begins with @code{=}, then the @code{=} will be replaced
653 by the @dfn{sysroot prefix}, a path specified when the linker is configured.
654
655 @ifset UsesEnvVars
656 The default set of paths searched (without being specified with
657 @samp{-L}) depends on which emulation mode @command{ld} is using, and in
658 some cases also on how it was configured. @xref{Environment}.
659 @end ifset
660
661 The paths can also be specified in a link script with the
662 @code{SEARCH_DIR} command. Directories specified this way are searched
663 at the point in which the linker script appears in the command line.
664
665 @cindex emulation
666 @kindex -m @var{emulation}
667 @item -m@var{emulation}
668 Emulate the @var{emulation} linker. You can list the available
669 emulations with the @samp{--verbose} or @samp{-V} options.
670
671 If the @samp{-m} option is not used, the emulation is taken from the
672 @code{LDEMULATION} environment variable, if that is defined.
673
674 Otherwise, the default emulation depends upon how the linker was
675 configured.
676
677 @cindex link map
678 @kindex -M
679 @kindex --print-map
680 @item -M
681 @itemx --print-map
682 Print a link map to the standard output. A link map provides
683 information about the link, including the following:
684
685 @itemize @bullet
686 @item
687 Where object files are mapped into memory.
688 @item
689 How common symbols are allocated.
690 @item
691 All archive members included in the link, with a mention of the symbol
692 which caused the archive member to be brought in.
693 @item
694 The values assigned to symbols.
695
696 Note - symbols whose values are computed by an expression which
697 involves a reference to a previous value of the same symbol may not
698 have correct result displayed in the link map. This is because the
699 linker discards intermediate results and only retains the final value
700 of an expression. Under such circumstances the linker will display
701 the final value enclosed by square brackets. Thus for example a
702 linker script containing:
703
704 @smallexample
705 foo = 1
706 foo = foo * 4
707 foo = foo + 8
708 @end smallexample
709
710 will produce the following output in the link map if the @option{-M}
711 option is used:
712
713 @smallexample
714 0x00000001 foo = 0x1
715 [0x0000000c] foo = (foo * 0x4)
716 [0x0000000c] foo = (foo + 0x8)
717 @end smallexample
718
719 See @ref{Expressions} for more information about expressions in linker
720 scripts.
721 @end itemize
722
723 @kindex -n
724 @cindex read-only text
725 @cindex NMAGIC
726 @kindex --nmagic
727 @item -n
728 @itemx --nmagic
729 Turn off page alignment of sections, and mark the output as
730 @code{NMAGIC} if possible.
731
732 @kindex -N
733 @kindex --omagic
734 @cindex read/write from cmd line
735 @cindex OMAGIC
736 @item -N
737 @itemx --omagic
738 Set the text and data sections to be readable and writable. Also, do
739 not page-align the data segment, and disable linking against shared
740 libraries. If the output format supports Unix style magic numbers,
741 mark the output as @code{OMAGIC}. Note: Although a writable text section
742 is allowed for PE-COFF targets, it does not conform to the format
743 specification published by Microsoft.
744
745 @kindex --no-omagic
746 @cindex OMAGIC
747 @item --no-omagic
748 This option negates most of the effects of the @option{-N} option. It
749 sets the text section to be read-only, and forces the data segment to
750 be page-aligned. Note - this option does not enable linking against
751 shared libraries. Use @option{-Bdynamic} for this.
752
753 @kindex -o @var{output}
754 @kindex --output=@var{output}
755 @cindex naming the output file
756 @item -o @var{output}
757 @itemx --output=@var{output}
758 Use @var{output} as the name for the program produced by @command{ld}; if this
759 option is not specified, the name @file{a.out} is used by default. The
760 script command @code{OUTPUT} can also specify the output file name.
761
762 @kindex -O @var{level}
763 @cindex generating optimized output
764 @item -O @var{level}
765 If @var{level} is a numeric values greater than zero @command{ld} optimizes
766 the output. This might take significantly longer and therefore probably
767 should only be enabled for the final binary.
768
769 @kindex -q
770 @kindex --emit-relocs
771 @cindex retain relocations in final executable
772 @item -q
773 @itemx --emit-relocs
774 Leave relocation sections and contents in fully linked executables.
775 Post link analysis and optimization tools may need this information in
776 order to perform correct modifications of executables. This results
777 in larger executables.
778
779 This option is currently only supported on ELF platforms.
780
781 @kindex --force-dynamic
782 @cindex forcing the creation of dynamic sections
783 @item --force-dynamic
784 Force the output file to have dynamic sections. This option is specific
785 to VxWorks targets.
786
787 @cindex partial link
788 @cindex relocatable output
789 @kindex -r
790 @kindex --relocatable
791 @item -r
792 @itemx --relocatable
793 Generate relocatable output---i.e., generate an output file that can in
794 turn serve as input to @command{ld}. This is often called @dfn{partial
795 linking}. As a side effect, in environments that support standard Unix
796 magic numbers, this option also sets the output file's magic number to
797 @code{OMAGIC}.
798 @c ; see @option{-N}.
799 If this option is not specified, an absolute file is produced. When
800 linking C++ programs, this option @emph{will not} resolve references to
801 constructors; to do that, use @samp{-Ur}.
802
803 When an input file does not have the same format as the output file,
804 partial linking is only supported if that input file does not contain any
805 relocations. Different output formats can have further restrictions; for
806 example some @code{a.out}-based formats do not support partial linking
807 with input files in other formats at all.
808
809 This option does the same thing as @samp{-i}.
810
811 @kindex -R @var{file}
812 @kindex --just-symbols=@var{file}
813 @cindex symbol-only input
814 @item -R @var{filename}
815 @itemx --just-symbols=@var{filename}
816 Read symbol names and their addresses from @var{filename}, but do not
817 relocate it or include it in the output. This allows your output file
818 to refer symbolically to absolute locations of memory defined in other
819 programs. You may use this option more than once.
820
821 For compatibility with other ELF linkers, if the @option{-R} option is
822 followed by a directory name, rather than a file name, it is treated as
823 the @option{-rpath} option.
824
825 @kindex -s
826 @kindex --strip-all
827 @cindex strip all symbols
828 @item -s
829 @itemx --strip-all
830 Omit all symbol information from the output file.
831
832 @kindex -S
833 @kindex --strip-debug
834 @cindex strip debugger symbols
835 @item -S
836 @itemx --strip-debug
837 Omit debugger symbol information (but not all symbols) from the output file.
838
839 @kindex -t
840 @kindex --trace
841 @cindex input files, displaying
842 @item -t
843 @itemx --trace
844 Print the names of the input files as @command{ld} processes them.
845
846 @kindex -T @var{script}
847 @kindex --script=@var{script}
848 @cindex script files
849 @item -T @var{scriptfile}
850 @itemx --script=@var{scriptfile}
851 Use @var{scriptfile} as the linker script. This script replaces
852 @command{ld}'s default linker script (rather than adding to it), so
853 @var{commandfile} must specify everything necessary to describe the
854 output file. @xref{Scripts}. If @var{scriptfile} does not exist in
855 the current directory, @code{ld} looks for it in the directories
856 specified by any preceding @samp{-L} options. Multiple @samp{-T}
857 options accumulate.
858
859 @kindex -dT @var{script}
860 @kindex --default-script=@var{script}
861 @cindex script files
862 @item -dT @var{scriptfile}
863 @itemx --default-script=@var{scriptfile}
864 Use @var{scriptfile} as the default linker script. @xref{Scripts}.
865
866 This option is similar to the @option{--script} option except that
867 processing of the script is delayed until after the rest of the
868 command line has been processed. This allows options placed after the
869 @option{--default-script} option on the command line to affect the
870 behaviour of the linker script, which can be important when the linker
871 command line cannot be directly controlled by the user. (eg because
872 the command line is being constructed by another tool, such as
873 @samp{gcc}).
874
875 @kindex -u @var{symbol}
876 @kindex --undefined=@var{symbol}
877 @cindex undefined symbol
878 @item -u @var{symbol}
879 @itemx --undefined=@var{symbol}
880 Force @var{symbol} to be entered in the output file as an undefined
881 symbol. Doing this may, for example, trigger linking of additional
882 modules from standard libraries. @samp{-u} may be repeated with
883 different option arguments to enter additional undefined symbols. This
884 option is equivalent to the @code{EXTERN} linker script command.
885
886 @kindex -Ur
887 @cindex constructors
888 @item -Ur
889 For anything other than C++ programs, this option is equivalent to
890 @samp{-r}: it generates relocatable output---i.e., an output file that can in
891 turn serve as input to @command{ld}. When linking C++ programs, @samp{-Ur}
892 @emph{does} resolve references to constructors, unlike @samp{-r}.
893 It does not work to use @samp{-Ur} on files that were themselves linked
894 with @samp{-Ur}; once the constructor table has been built, it cannot
895 be added to. Use @samp{-Ur} only for the last partial link, and
896 @samp{-r} for the others.
897
898 @kindex --unique[=@var{SECTION}]
899 @item --unique[=@var{SECTION}]
900 Creates a separate output section for every input section matching
901 @var{SECTION}, or if the optional wildcard @var{SECTION} argument is
902 missing, for every orphan input section. An orphan section is one not
903 specifically mentioned in a linker script. You may use this option
904 multiple times on the command line; It prevents the normal merging of
905 input sections with the same name, overriding output section assignments
906 in a linker script.
907
908 @kindex -v
909 @kindex -V
910 @kindex --version
911 @cindex version
912 @item -v
913 @itemx --version
914 @itemx -V
915 Display the version number for @command{ld}. The @option{-V} option also
916 lists the supported emulations.
917
918 @kindex -x
919 @kindex --discard-all
920 @cindex deleting local symbols
921 @item -x
922 @itemx --discard-all
923 Delete all local symbols.
924
925 @kindex -X
926 @kindex --discard-locals
927 @cindex local symbols, deleting
928 @item -X
929 @itemx --discard-locals
930 Delete all temporary local symbols. (These symbols start with
931 system-specific local label prefixes, typically @samp{.L} for ELF systems
932 or @samp{L} for traditional a.out systems.)
933
934 @kindex -y @var{symbol}
935 @kindex --trace-symbol=@var{symbol}
936 @cindex symbol tracing
937 @item -y @var{symbol}
938 @itemx --trace-symbol=@var{symbol}
939 Print the name of each linked file in which @var{symbol} appears. This
940 option may be given any number of times. On many systems it is necessary
941 to prepend an underscore.
942
943 This option is useful when you have an undefined symbol in your link but
944 don't know where the reference is coming from.
945
946 @kindex -Y @var{path}
947 @item -Y @var{path}
948 Add @var{path} to the default library search path. This option exists
949 for Solaris compatibility.
950
951 @kindex -z @var{keyword}
952 @item -z @var{keyword}
953 The recognized keywords are:
954 @table @samp
955
956 @item combreloc
957 Combines multiple reloc sections and sorts them to make dynamic symbol
958 lookup caching possible.
959
960 @item defs
961 Disallows undefined symbols in object files. Undefined symbols in
962 shared libraries are still allowed.
963
964 @item execstack
965 Marks the object as requiring executable stack.
966
967 @item initfirst
968 This option is only meaningful when building a shared object.
969 It marks the object so that its runtime initialization will occur
970 before the runtime initialization of any other objects brought into
971 the process at the same time. Similarly the runtime finalization of
972 the object will occur after the runtime finalization of any other
973 objects.
974
975 @item interpose
976 Marks the object that its symbol table interposes before all symbols
977 but the primary executable.
978
979 @item lazy
980 When generating an executable or shared library, mark it to tell the
981 dynamic linker to defer function call resolution to the point when
982 the function is called (lazy binding), rather than at load time.
983 Lazy binding is the default.
984
985 @item loadfltr
986 Marks the object that its filters be processed immediately at
987 runtime.
988
989 @item muldefs
990 Allows multiple definitions.
991
992 @item nocombreloc
993 Disables multiple reloc sections combining.
994
995 @item nocopyreloc
996 Disables production of copy relocs.
997
998 @item nodefaultlib
999 Marks the object that the search for dependencies of this object will
1000 ignore any default library search paths.
1001
1002 @item nodelete
1003 Marks the object shouldn't be unloaded at runtime.
1004
1005 @item nodlopen
1006 Marks the object not available to @code{dlopen}.
1007
1008 @item nodump
1009 Marks the object can not be dumped by @code{dldump}.
1010
1011 @item noexecstack
1012 Marks the object as not requiring executable stack.
1013
1014 @item norelro
1015 Don't create an ELF @code{PT_GNU_RELRO} segment header in the object.
1016
1017 @item now
1018 When generating an executable or shared library, mark it to tell the
1019 dynamic linker to resolve all symbols when the program is started, or
1020 when the shared library is linked to using dlopen, instead of
1021 deferring function call resolution to the point when the function is
1022 first called.
1023
1024 @item origin
1025 Marks the object may contain $ORIGIN.
1026
1027 @item relro
1028 Create an ELF @code{PT_GNU_RELRO} segment header in the object.
1029
1030 @item max-page-size=@var{value}
1031 Set the emulation maximum page size to @var{value}.
1032
1033 @item common-page-size=@var{value}
1034 Set the emulation common page size to @var{value}.
1035
1036 @end table
1037
1038 Other keywords are ignored for Solaris compatibility.
1039
1040 @kindex -(
1041 @cindex groups of archives
1042 @item -( @var{archives} -)
1043 @itemx --start-group @var{archives} --end-group
1044 The @var{archives} should be a list of archive files. They may be
1045 either explicit file names, or @samp{-l} options.
1046
1047 The specified archives are searched repeatedly until no new undefined
1048 references are created. Normally, an archive is searched only once in
1049 the order that it is specified on the command line. If a symbol in that
1050 archive is needed to resolve an undefined symbol referred to by an
1051 object in an archive that appears later on the command line, the linker
1052 would not be able to resolve that reference. By grouping the archives,
1053 they all be searched repeatedly until all possible references are
1054 resolved.
1055
1056 Using this option has a significant performance cost. It is best to use
1057 it only when there are unavoidable circular references between two or
1058 more archives.
1059
1060 @kindex --accept-unknown-input-arch
1061 @kindex --no-accept-unknown-input-arch
1062 @item --accept-unknown-input-arch
1063 @itemx --no-accept-unknown-input-arch
1064 Tells the linker to accept input files whose architecture cannot be
1065 recognised. The assumption is that the user knows what they are doing
1066 and deliberately wants to link in these unknown input files. This was
1067 the default behaviour of the linker, before release 2.14. The default
1068 behaviour from release 2.14 onwards is to reject such input files, and
1069 so the @samp{--accept-unknown-input-arch} option has been added to
1070 restore the old behaviour.
1071
1072 @kindex --as-needed
1073 @kindex --no-as-needed
1074 @item --as-needed
1075 @itemx --no-as-needed
1076 This option affects ELF DT_NEEDED tags for dynamic libraries mentioned
1077 on the command line after the @option{--as-needed} option. Normally,
1078 the linker will add a DT_NEEDED tag for each dynamic library mentioned
1079 on the command line, regardless of whether the library is actually
1080 needed. @option{--as-needed} causes DT_NEEDED tags to only be emitted
1081 for libraries that satisfy some symbol reference from regular objects
1082 which is undefined at the point that the library was linked.
1083 @option{--no-as-needed} restores the default behaviour.
1084
1085 @kindex --add-needed
1086 @kindex --no-add-needed
1087 @item --add-needed
1088 @itemx --no-add-needed
1089 This option affects the treatment of dynamic libraries from ELF
1090 DT_NEEDED tags in dynamic libraries mentioned on the command line after
1091 the @option{--no-add-needed} option. Normally, the linker will add
1092 a DT_NEEDED tag for each dynamic library from DT_NEEDED tags.
1093 @option{--no-add-needed} causes DT_NEEDED tags will never be emitted
1094 for those libraries from DT_NEEDED tags. @option{--add-needed} restores
1095 the default behaviour.
1096
1097 @kindex -assert @var{keyword}
1098 @item -assert @var{keyword}
1099 This option is ignored for SunOS compatibility.
1100
1101 @kindex -Bdynamic
1102 @kindex -dy
1103 @kindex -call_shared
1104 @item -Bdynamic
1105 @itemx -dy
1106 @itemx -call_shared
1107 Link against dynamic libraries. This is only meaningful on platforms
1108 for which shared libraries are supported. This option is normally the
1109 default on such platforms. The different variants of this option are
1110 for compatibility with various systems. You may use this option
1111 multiple times on the command line: it affects library searching for
1112 @option{-l} options which follow it.
1113
1114 @kindex -Bgroup
1115 @item -Bgroup
1116 Set the @code{DF_1_GROUP} flag in the @code{DT_FLAGS_1} entry in the dynamic
1117 section. This causes the runtime linker to handle lookups in this
1118 object and its dependencies to be performed only inside the group.
1119 @option{--unresolved-symbols=report-all} is implied. This option is
1120 only meaningful on ELF platforms which support shared libraries.
1121
1122 @kindex -Bstatic
1123 @kindex -dn
1124 @kindex -non_shared
1125 @kindex -static
1126 @item -Bstatic
1127 @itemx -dn
1128 @itemx -non_shared
1129 @itemx -static
1130 Do not link against shared libraries. This is only meaningful on
1131 platforms for which shared libraries are supported. The different
1132 variants of this option are for compatibility with various systems. You
1133 may use this option multiple times on the command line: it affects
1134 library searching for @option{-l} options which follow it. This
1135 option also implies @option{--unresolved-symbols=report-all}. This
1136 option can be used with @option{-shared}. Doing so means that a
1137 shared library is being created but that all of the library's external
1138 references must be resolved by pulling in entries from static
1139 libraries.
1140
1141 @kindex -Bsymbolic
1142 @item -Bsymbolic
1143 When creating a shared library, bind references to global symbols to the
1144 definition within the shared library, if any. Normally, it is possible
1145 for a program linked against a shared library to override the definition
1146 within the shared library. This option is only meaningful on ELF
1147 platforms which support shared libraries.
1148
1149 @kindex -Bsymbolic-functions
1150 @item -Bsymbolic-functions
1151 When creating a shared library, bind references to global function
1152 symbols to the definition within the shared library, if any.
1153 @option{-Bsymbolic-functions} is an alias for @option{--dynamic-list-data}.
1154 This option is only meaningful on ELF platforms which support shared
1155 libraries.
1156
1157 @kindex --dynamic-list=@var{dynamic-list-file}
1158 @item --dynamic-list=@var{dynamic-list-file}
1159 Specify the name of a dynamic list file to the linker. This is
1160 typically used when creating shared libraries to specify a list of
1161 global symbols whose references shouldn't be bound to the definition
1162 within the shared library, or creating dynamically linked executables
1163 to specify a list of symbols which should be added to the symbol table
1164 in the executable. This option is only meaningful on ELF platforms
1165 which support shared libraries.
1166
1167 The format of the dynamic list is the same as the version node without
1168 scope and node name. See @ref{VERSION} for more information.
1169
1170 @kindex --dynamic-list-data
1171 @item --dynamic-list-data
1172 Include all global data symbols to the dynamic list.
1173
1174 @kindex --dynamic-list-cpp-new
1175 @item --dynamic-list-cpp-new
1176 Provide the builtin dynamic list for C++ operator new and delete. It
1177 is mainly useful for building shared libstdc++.
1178
1179 @kindex --dynamic-list-cpp-typeinfo
1180 @item --dynamic-list-cpp-typeinfo
1181 Provide the builtin dynamic list for C++ runtime type identification.
1182
1183 @kindex --check-sections
1184 @kindex --no-check-sections
1185 @item --check-sections
1186 @itemx --no-check-sections
1187 Asks the linker @emph{not} to check section addresses after they have
1188 been assigned to see if there are any overlaps. Normally the linker will
1189 perform this check, and if it finds any overlaps it will produce
1190 suitable error messages. The linker does know about, and does make
1191 allowances for sections in overlays. The default behaviour can be
1192 restored by using the command line switch @option{--check-sections}.
1193
1194 @cindex cross reference table
1195 @kindex --cref
1196 @item --cref
1197 Output a cross reference table. If a linker map file is being
1198 generated, the cross reference table is printed to the map file.
1199 Otherwise, it is printed on the standard output.
1200
1201 The format of the table is intentionally simple, so that it may be
1202 easily processed by a script if necessary. The symbols are printed out,
1203 sorted by name. For each symbol, a list of file names is given. If the
1204 symbol is defined, the first file listed is the location of the
1205 definition. The remaining files contain references to the symbol.
1206
1207 @cindex common allocation
1208 @kindex --no-define-common
1209 @item --no-define-common
1210 This option inhibits the assignment of addresses to common symbols.
1211 The script command @code{INHIBIT_COMMON_ALLOCATION} has the same effect.
1212 @xref{Miscellaneous Commands}.
1213
1214 The @samp{--no-define-common} option allows decoupling
1215 the decision to assign addresses to Common symbols from the choice
1216 of the output file type; otherwise a non-Relocatable output type
1217 forces assigning addresses to Common symbols.
1218 Using @samp{--no-define-common} allows Common symbols that are referenced
1219 from a shared library to be assigned addresses only in the main program.
1220 This eliminates the unused duplicate space in the shared library,
1221 and also prevents any possible confusion over resolving to the wrong
1222 duplicate when there are many dynamic modules with specialized search
1223 paths for runtime symbol resolution.
1224
1225 @cindex symbols, from command line
1226 @kindex --defsym @var{symbol}=@var{exp}
1227 @item --defsym @var{symbol}=@var{expression}
1228 Create a global symbol in the output file, containing the absolute
1229 address given by @var{expression}. You may use this option as many
1230 times as necessary to define multiple symbols in the command line. A
1231 limited form of arithmetic is supported for the @var{expression} in this
1232 context: you may give a hexadecimal constant or the name of an existing
1233 symbol, or use @code{+} and @code{-} to add or subtract hexadecimal
1234 constants or symbols. If you need more elaborate expressions, consider
1235 using the linker command language from a script (@pxref{Assignments,,
1236 Assignment: Symbol Definitions}). @emph{Note:} there should be no white
1237 space between @var{symbol}, the equals sign (``@key{=}''), and
1238 @var{expression}.
1239
1240 @cindex demangling, from command line
1241 @kindex --demangle[=@var{style}]
1242 @kindex --no-demangle
1243 @item --demangle[=@var{style}]
1244 @itemx --no-demangle
1245 These options control whether to demangle symbol names in error messages
1246 and other output. When the linker is told to demangle, it tries to
1247 present symbol names in a readable fashion: it strips leading
1248 underscores if they are used by the object file format, and converts C++
1249 mangled symbol names into user readable names. Different compilers have
1250 different mangling styles. The optional demangling style argument can be used
1251 to choose an appropriate demangling style for your compiler. The linker will
1252 demangle by default unless the environment variable @samp{COLLECT_NO_DEMANGLE}
1253 is set. These options may be used to override the default.
1254
1255 @cindex dynamic linker, from command line
1256 @kindex -I@var{file}
1257 @kindex --dynamic-linker @var{file}
1258 @item --dynamic-linker @var{file}
1259 Set the name of the dynamic linker. This is only meaningful when
1260 generating dynamically linked ELF executables. The default dynamic
1261 linker is normally correct; don't use this unless you know what you are
1262 doing.
1263
1264
1265 @kindex --fatal-warnings
1266 @item --fatal-warnings
1267 Treat all warnings as errors.
1268
1269 @kindex --force-exe-suffix
1270 @item --force-exe-suffix
1271 Make sure that an output file has a .exe suffix.
1272
1273 If a successfully built fully linked output file does not have a
1274 @code{.exe} or @code{.dll} suffix, this option forces the linker to copy
1275 the output file to one of the same name with a @code{.exe} suffix. This
1276 option is useful when using unmodified Unix makefiles on a Microsoft
1277 Windows host, since some versions of Windows won't run an image unless
1278 it ends in a @code{.exe} suffix.
1279
1280 @kindex --gc-sections
1281 @kindex --no-gc-sections
1282 @cindex garbage collection
1283 @item --gc-sections
1284 @itemx --no-gc-sections
1285 Enable garbage collection of unused input sections. It is ignored on
1286 targets that do not support this option. This option is not compatible
1287 with @samp{-r} or @samp{--emit-relocs}. The default behaviour (of not
1288 performing this garbage collection) can be restored by specifying
1289 @samp{--no-gc-sections} on the command line.
1290
1291 @kindex --print-gc-sections
1292 @kindex --no-print-gc-sections
1293 @cindex garbage collection
1294 @item --print-gc-sections
1295 @itemx --no-print-gc-sections
1296 List all sections removed by garbage collection. The listing is
1297 printed on stderr. This option is only effective if garbage
1298 collection has been enabled via the @samp{--gc-sections}) option. The
1299 default behaviour (of not listing the sections that are removed) can
1300 be restored by specifying @samp{--no-print-gc-sections} on the command
1301 line.
1302
1303 @cindex help
1304 @cindex usage
1305 @kindex --help
1306 @item --help
1307 Print a summary of the command-line options on the standard output and exit.
1308
1309 @kindex --target-help
1310 @item --target-help
1311 Print a summary of all target specific options on the standard output and exit.
1312
1313 @kindex -Map
1314 @item -Map @var{mapfile}
1315 Print a link map to the file @var{mapfile}. See the description of the
1316 @option{-M} option, above.
1317
1318 @cindex memory usage
1319 @kindex --no-keep-memory
1320 @item --no-keep-memory
1321 @command{ld} normally optimizes for speed over memory usage by caching the
1322 symbol tables of input files in memory. This option tells @command{ld} to
1323 instead optimize for memory usage, by rereading the symbol tables as
1324 necessary. This may be required if @command{ld} runs out of memory space
1325 while linking a large executable.
1326
1327 @kindex --no-undefined
1328 @kindex -z defs
1329 @item --no-undefined
1330 @itemx -z defs
1331 Report unresolved symbol references from regular object files. This
1332 is done even if the linker is creating a non-symbolic shared library.
1333 The switch @option{--[no-]allow-shlib-undefined} controls the
1334 behaviour for reporting unresolved references found in shared
1335 libraries being linked in.
1336
1337 @kindex --allow-multiple-definition
1338 @kindex -z muldefs
1339 @item --allow-multiple-definition
1340 @itemx -z muldefs
1341 Normally when a symbol is defined multiple times, the linker will
1342 report a fatal error. These options allow multiple definitions and the
1343 first definition will be used.
1344
1345 @kindex --allow-shlib-undefined
1346 @kindex --no-allow-shlib-undefined
1347 @item --allow-shlib-undefined
1348 @itemx --no-allow-shlib-undefined
1349 Allows (the default) or disallows undefined symbols in shared libraries.
1350 This switch is similar to @option{--no-undefined} except that it
1351 determines the behaviour when the undefined symbols are in a
1352 shared library rather than a regular object file. It does not affect
1353 how undefined symbols in regular object files are handled.
1354
1355 The reason that @option{--allow-shlib-undefined} is the default is that
1356 the shared library being specified at link time may not be the same as
1357 the one that is available at load time, so the symbols might actually be
1358 resolvable at load time. Plus there are some systems, (eg BeOS) where
1359 undefined symbols in shared libraries is normal. (The kernel patches
1360 them at load time to select which function is most appropriate
1361 for the current architecture. This is used for example to dynamically
1362 select an appropriate memset function). Apparently it is also normal
1363 for HPPA shared libraries to have undefined symbols.
1364
1365 @kindex --no-undefined-version
1366 @item --no-undefined-version
1367 Normally when a symbol has an undefined version, the linker will ignore
1368 it. This option disallows symbols with undefined version and a fatal error
1369 will be issued instead.
1370
1371 @kindex --default-symver
1372 @item --default-symver
1373 Create and use a default symbol version (the soname) for unversioned
1374 exported symbols.
1375
1376 @kindex --default-imported-symver
1377 @item --default-imported-symver
1378 Create and use a default symbol version (the soname) for unversioned
1379 imported symbols.
1380
1381 @kindex --no-warn-mismatch
1382 @item --no-warn-mismatch
1383 Normally @command{ld} will give an error if you try to link together input
1384 files that are mismatched for some reason, perhaps because they have
1385 been compiled for different processors or for different endiannesses.
1386 This option tells @command{ld} that it should silently permit such possible
1387 errors. This option should only be used with care, in cases when you
1388 have taken some special action that ensures that the linker errors are
1389 inappropriate.
1390
1391 @kindex --no-whole-archive
1392 @item --no-whole-archive
1393 Turn off the effect of the @option{--whole-archive} option for subsequent
1394 archive files.
1395
1396 @cindex output file after errors
1397 @kindex --noinhibit-exec
1398 @item --noinhibit-exec
1399 Retain the executable output file whenever it is still usable.
1400 Normally, the linker will not produce an output file if it encounters
1401 errors during the link process; it exits without writing an output file
1402 when it issues any error whatsoever.
1403
1404 @kindex -nostdlib
1405 @item -nostdlib
1406 Only search library directories explicitly specified on the
1407 command line. Library directories specified in linker scripts
1408 (including linker scripts specified on the command line) are ignored.
1409
1410 @ifclear SingleFormat
1411 @kindex --oformat
1412 @item --oformat @var{output-format}
1413 @command{ld} may be configured to support more than one kind of object
1414 file. If your @command{ld} is configured this way, you can use the
1415 @samp{--oformat} option to specify the binary format for the output
1416 object file. Even when @command{ld} is configured to support alternative
1417 object formats, you don't usually need to specify this, as @command{ld}
1418 should be configured to produce as a default output format the most
1419 usual format on each machine. @var{output-format} is a text string, the
1420 name of a particular format supported by the BFD libraries. (You can
1421 list the available binary formats with @samp{objdump -i}.) The script
1422 command @code{OUTPUT_FORMAT} can also specify the output format, but
1423 this option overrides it. @xref{BFD}.
1424 @end ifclear
1425
1426 @kindex -pie
1427 @kindex --pic-executable
1428 @item -pie
1429 @itemx --pic-executable
1430 @cindex position independent executables
1431 Create a position independent executable. This is currently only supported on
1432 ELF platforms. Position independent executables are similar to shared
1433 libraries in that they are relocated by the dynamic linker to the virtual
1434 address the OS chooses for them (which can vary between invocations). Like
1435 normal dynamically linked executables they can be executed and symbols
1436 defined in the executable cannot be overridden by shared libraries.
1437
1438 @kindex -qmagic
1439 @item -qmagic
1440 This option is ignored for Linux compatibility.
1441
1442 @kindex -Qy
1443 @item -Qy
1444 This option is ignored for SVR4 compatibility.
1445
1446 @kindex --relax
1447 @cindex synthesizing linker
1448 @cindex relaxing addressing modes
1449 @item --relax
1450 An option with machine dependent effects.
1451 @ifset GENERIC
1452 This option is only supported on a few targets.
1453 @end ifset
1454 @ifset H8300
1455 @xref{H8/300,,@command{ld} and the H8/300}.
1456 @end ifset
1457 @ifset I960
1458 @xref{i960,, @command{ld} and the Intel 960 family}.
1459 @end ifset
1460 @ifset XTENSA
1461 @xref{Xtensa,, @command{ld} and Xtensa Processors}.
1462 @end ifset
1463 @ifset M68HC11
1464 @xref{M68HC11/68HC12,,@command{ld} and the 68HC11 and 68HC12}.
1465 @end ifset
1466 @ifset POWERPC
1467 @xref{PowerPC ELF32,,@command{ld} and PowerPC 32-bit ELF Support}.
1468 @end ifset
1469
1470 On some platforms, the @samp{--relax} option performs global
1471 optimizations that become possible when the linker resolves addressing
1472 in the program, such as relaxing address modes and synthesizing new
1473 instructions in the output object file.
1474
1475 On some platforms these link time global optimizations may make symbolic
1476 debugging of the resulting executable impossible.
1477 @ifset GENERIC
1478 This is known to be
1479 the case for the Matsushita MN10200 and MN10300 family of processors.
1480 @end ifset
1481
1482 @ifset GENERIC
1483 On platforms where this is not supported, @samp{--relax} is accepted,
1484 but ignored.
1485 @end ifset
1486
1487 @cindex retaining specified symbols
1488 @cindex stripping all but some symbols
1489 @cindex symbols, retaining selectively
1490 @item --retain-symbols-file @var{filename}
1491 Retain @emph{only} the symbols listed in the file @var{filename},
1492 discarding all others. @var{filename} is simply a flat file, with one
1493 symbol name per line. This option is especially useful in environments
1494 @ifset GENERIC
1495 (such as VxWorks)
1496 @end ifset
1497 where a large global symbol table is accumulated gradually, to conserve
1498 run-time memory.
1499
1500 @samp{--retain-symbols-file} does @emph{not} discard undefined symbols,
1501 or symbols needed for relocations.
1502
1503 You may only specify @samp{--retain-symbols-file} once in the command
1504 line. It overrides @samp{-s} and @samp{-S}.
1505
1506 @ifset GENERIC
1507 @item -rpath @var{dir}
1508 @cindex runtime library search path
1509 @kindex -rpath
1510 Add a directory to the runtime library search path. This is used when
1511 linking an ELF executable with shared objects. All @option{-rpath}
1512 arguments are concatenated and passed to the runtime linker, which uses
1513 them to locate shared objects at runtime. The @option{-rpath} option is
1514 also used when locating shared objects which are needed by shared
1515 objects explicitly included in the link; see the description of the
1516 @option{-rpath-link} option. If @option{-rpath} is not used when linking an
1517 ELF executable, the contents of the environment variable
1518 @code{LD_RUN_PATH} will be used if it is defined.
1519
1520 The @option{-rpath} option may also be used on SunOS. By default, on
1521 SunOS, the linker will form a runtime search patch out of all the
1522 @option{-L} options it is given. If a @option{-rpath} option is used, the
1523 runtime search path will be formed exclusively using the @option{-rpath}
1524 options, ignoring the @option{-L} options. This can be useful when using
1525 gcc, which adds many @option{-L} options which may be on NFS mounted
1526 file systems.
1527
1528 For compatibility with other ELF linkers, if the @option{-R} option is
1529 followed by a directory name, rather than a file name, it is treated as
1530 the @option{-rpath} option.
1531 @end ifset
1532
1533 @ifset GENERIC
1534 @cindex link-time runtime library search path
1535 @kindex -rpath-link
1536 @item -rpath-link @var{DIR}
1537 When using ELF or SunOS, one shared library may require another. This
1538 happens when an @code{ld -shared} link includes a shared library as one
1539 of the input files.
1540
1541 When the linker encounters such a dependency when doing a non-shared,
1542 non-relocatable link, it will automatically try to locate the required
1543 shared library and include it in the link, if it is not included
1544 explicitly. In such a case, the @option{-rpath-link} option
1545 specifies the first set of directories to search. The
1546 @option{-rpath-link} option may specify a sequence of directory names
1547 either by specifying a list of names separated by colons, or by
1548 appearing multiple times.
1549
1550 This option should be used with caution as it overrides the search path
1551 that may have been hard compiled into a shared library. In such a case it
1552 is possible to use unintentionally a different search path than the
1553 runtime linker would do.
1554
1555 The linker uses the following search paths to locate required shared
1556 libraries:
1557 @enumerate
1558 @item
1559 Any directories specified by @option{-rpath-link} options.
1560 @item
1561 Any directories specified by @option{-rpath} options. The difference
1562 between @option{-rpath} and @option{-rpath-link} is that directories
1563 specified by @option{-rpath} options are included in the executable and
1564 used at runtime, whereas the @option{-rpath-link} option is only effective
1565 at link time. Searching @option{-rpath} in this way is only supported
1566 by native linkers and cross linkers which have been configured with
1567 the @option{--with-sysroot} option.
1568 @item
1569 On an ELF system, if the @option{-rpath} and @code{rpath-link} options
1570 were not used, search the contents of the environment variable
1571 @code{LD_RUN_PATH}. It is for the native linker only.
1572 @item
1573 On SunOS, if the @option{-rpath} option was not used, search any
1574 directories specified using @option{-L} options.
1575 @item
1576 For a native linker, the contents of the environment variable
1577 @code{LD_LIBRARY_PATH}.
1578 @item
1579 For a native ELF linker, the directories in @code{DT_RUNPATH} or
1580 @code{DT_RPATH} of a shared library are searched for shared
1581 libraries needed by it. The @code{DT_RPATH} entries are ignored if
1582 @code{DT_RUNPATH} entries exist.
1583 @item
1584 The default directories, normally @file{/lib} and @file{/usr/lib}.
1585 @item
1586 For a native linker on an ELF system, if the file @file{/etc/ld.so.conf}
1587 exists, the list of directories found in that file.
1588 @end enumerate
1589
1590 If the required shared library is not found, the linker will issue a
1591 warning and continue with the link.
1592 @end ifset
1593
1594 @kindex -shared
1595 @kindex -Bshareable
1596 @item -shared
1597 @itemx -Bshareable
1598 @cindex shared libraries
1599 Create a shared library. This is currently only supported on ELF, XCOFF
1600 and SunOS platforms. On SunOS, the linker will automatically create a
1601 shared library if the @option{-e} option is not used and there are
1602 undefined symbols in the link.
1603
1604 @item --sort-common
1605 @kindex --sort-common
1606 This option tells @command{ld} to sort the common symbols by size when it
1607 places them in the appropriate output sections. First come all the one
1608 byte symbols, then all the two byte, then all the four byte, and then
1609 everything else. This is to prevent gaps between symbols due to
1610 alignment constraints.
1611
1612 @kindex --sort-section name
1613 @item --sort-section name
1614 This option will apply @code{SORT_BY_NAME} to all wildcard section
1615 patterns in the linker script.
1616
1617 @kindex --sort-section alignment
1618 @item --sort-section alignment
1619 This option will apply @code{SORT_BY_ALIGNMENT} to all wildcard section
1620 patterns in the linker script.
1621
1622 @kindex --split-by-file
1623 @item --split-by-file [@var{size}]
1624 Similar to @option{--split-by-reloc} but creates a new output section for
1625 each input file when @var{size} is reached. @var{size} defaults to a
1626 size of 1 if not given.
1627
1628 @kindex --split-by-reloc
1629 @item --split-by-reloc [@var{count}]
1630 Tries to creates extra sections in the output file so that no single
1631 output section in the file contains more than @var{count} relocations.
1632 This is useful when generating huge relocatable files for downloading into
1633 certain real time kernels with the COFF object file format; since COFF
1634 cannot represent more than 65535 relocations in a single section. Note
1635 that this will fail to work with object file formats which do not
1636 support arbitrary sections. The linker will not split up individual
1637 input sections for redistribution, so if a single input section contains
1638 more than @var{count} relocations one output section will contain that
1639 many relocations. @var{count} defaults to a value of 32768.
1640
1641 @kindex --stats
1642 @item --stats
1643 Compute and display statistics about the operation of the linker, such
1644 as execution time and memory usage.
1645
1646 @kindex --sysroot
1647 @item --sysroot=@var{directory}
1648 Use @var{directory} as the location of the sysroot, overriding the
1649 configure-time default. This option is only supported by linkers
1650 that were configured using @option{--with-sysroot}.
1651
1652 @kindex --traditional-format
1653 @cindex traditional format
1654 @item --traditional-format
1655 For some targets, the output of @command{ld} is different in some ways from
1656 the output of some existing linker. This switch requests @command{ld} to
1657 use the traditional format instead.
1658
1659 @cindex dbx
1660 For example, on SunOS, @command{ld} combines duplicate entries in the
1661 symbol string table. This can reduce the size of an output file with
1662 full debugging information by over 30 percent. Unfortunately, the SunOS
1663 @code{dbx} program can not read the resulting program (@code{gdb} has no
1664 trouble). The @samp{--traditional-format} switch tells @command{ld} to not
1665 combine duplicate entries.
1666
1667 @kindex --section-start @var{sectionname}=@var{org}
1668 @item --section-start @var{sectionname}=@var{org}
1669 Locate a section in the output file at the absolute
1670 address given by @var{org}. You may use this option as many
1671 times as necessary to locate multiple sections in the command
1672 line.
1673 @var{org} must be a single hexadecimal integer;
1674 for compatibility with other linkers, you may omit the leading
1675 @samp{0x} usually associated with hexadecimal values. @emph{Note:} there
1676 should be no white space between @var{sectionname}, the equals
1677 sign (``@key{=}''), and @var{org}.
1678
1679 @kindex -Tbss @var{org}
1680 @kindex -Tdata @var{org}
1681 @kindex -Ttext @var{org}
1682 @cindex segment origins, cmd line
1683 @item -Tbss @var{org}
1684 @itemx -Tdata @var{org}
1685 @itemx -Ttext @var{org}
1686 Same as --section-start, with @code{.bss}, @code{.data} or
1687 @code{.text} as the @var{sectionname}.
1688
1689 @kindex --unresolved-symbols
1690 @item --unresolved-symbols=@var{method}
1691 Determine how to handle unresolved symbols. There are four possible
1692 values for @samp{method}:
1693
1694 @table @samp
1695 @item ignore-all
1696 Do not report any unresolved symbols.
1697
1698 @item report-all
1699 Report all unresolved symbols. This is the default.
1700
1701 @item ignore-in-object-files
1702 Report unresolved symbols that are contained in shared libraries, but
1703 ignore them if they come from regular object files.
1704
1705 @item ignore-in-shared-libs
1706 Report unresolved symbols that come from regular object files, but
1707 ignore them if they come from shared libraries. This can be useful
1708 when creating a dynamic binary and it is known that all the shared
1709 libraries that it should be referencing are included on the linker's
1710 command line.
1711 @end table
1712
1713 The behaviour for shared libraries on their own can also be controlled
1714 by the @option{--[no-]allow-shlib-undefined} option.
1715
1716 Normally the linker will generate an error message for each reported
1717 unresolved symbol but the option @option{--warn-unresolved-symbols}
1718 can change this to a warning.
1719
1720 @kindex --verbose
1721 @cindex verbose
1722 @item --dll-verbose
1723 @itemx --verbose
1724 Display the version number for @command{ld} and list the linker emulations
1725 supported. Display which input files can and cannot be opened. Display
1726 the linker script being used by the linker.
1727
1728 @kindex --version-script=@var{version-scriptfile}
1729 @cindex version script, symbol versions
1730 @itemx --version-script=@var{version-scriptfile}
1731 Specify the name of a version script to the linker. This is typically
1732 used when creating shared libraries to specify additional information
1733 about the version hierarchy for the library being created. This option
1734 is only meaningful on ELF platforms which support shared libraries.
1735 @xref{VERSION}.
1736
1737 @kindex --warn-common
1738 @cindex warnings, on combining symbols
1739 @cindex combining symbols, warnings on
1740 @item --warn-common
1741 Warn when a common symbol is combined with another common symbol or with
1742 a symbol definition. Unix linkers allow this somewhat sloppy practise,
1743 but linkers on some other operating systems do not. This option allows
1744 you to find potential problems from combining global symbols.
1745 Unfortunately, some C libraries use this practise, so you may get some
1746 warnings about symbols in the libraries as well as in your programs.
1747
1748 There are three kinds of global symbols, illustrated here by C examples:
1749
1750 @table @samp
1751 @item int i = 1;
1752 A definition, which goes in the initialized data section of the output
1753 file.
1754
1755 @item extern int i;
1756 An undefined reference, which does not allocate space.
1757 There must be either a definition or a common symbol for the
1758 variable somewhere.
1759
1760 @item int i;
1761 A common symbol. If there are only (one or more) common symbols for a
1762 variable, it goes in the uninitialized data area of the output file.
1763 The linker merges multiple common symbols for the same variable into a
1764 single symbol. If they are of different sizes, it picks the largest
1765 size. The linker turns a common symbol into a declaration, if there is
1766 a definition of the same variable.
1767 @end table
1768
1769 The @samp{--warn-common} option can produce five kinds of warnings.
1770 Each warning consists of a pair of lines: the first describes the symbol
1771 just encountered, and the second describes the previous symbol
1772 encountered with the same name. One or both of the two symbols will be
1773 a common symbol.
1774
1775 @enumerate
1776 @item
1777 Turning a common symbol into a reference, because there is already a
1778 definition for the symbol.
1779 @smallexample
1780 @var{file}(@var{section}): warning: common of `@var{symbol}'
1781 overridden by definition
1782 @var{file}(@var{section}): warning: defined here
1783 @end smallexample
1784
1785 @item
1786 Turning a common symbol into a reference, because a later definition for
1787 the symbol is encountered. This is the same as the previous case,
1788 except that the symbols are encountered in a different order.
1789 @smallexample
1790 @var{file}(@var{section}): warning: definition of `@var{symbol}'
1791 overriding common
1792 @var{file}(@var{section}): warning: common is here
1793 @end smallexample
1794
1795 @item
1796 Merging a common symbol with a previous same-sized common symbol.
1797 @smallexample
1798 @var{file}(@var{section}): warning: multiple common
1799 of `@var{symbol}'
1800 @var{file}(@var{section}): warning: previous common is here
1801 @end smallexample
1802
1803 @item
1804 Merging a common symbol with a previous larger common symbol.
1805 @smallexample
1806 @var{file}(@var{section}): warning: common of `@var{symbol}'
1807 overridden by larger common
1808 @var{file}(@var{section}): warning: larger common is here
1809 @end smallexample
1810
1811 @item
1812 Merging a common symbol with a previous smaller common symbol. This is
1813 the same as the previous case, except that the symbols are
1814 encountered in a different order.
1815 @smallexample
1816 @var{file}(@var{section}): warning: common of `@var{symbol}'
1817 overriding smaller common
1818 @var{file}(@var{section}): warning: smaller common is here
1819 @end smallexample
1820 @end enumerate
1821
1822 @kindex --warn-constructors
1823 @item --warn-constructors
1824 Warn if any global constructors are used. This is only useful for a few
1825 object file formats. For formats like COFF or ELF, the linker can not
1826 detect the use of global constructors.
1827
1828 @kindex --warn-multiple-gp
1829 @item --warn-multiple-gp
1830 Warn if multiple global pointer values are required in the output file.
1831 This is only meaningful for certain processors, such as the Alpha.
1832 Specifically, some processors put large-valued constants in a special
1833 section. A special register (the global pointer) points into the middle
1834 of this section, so that constants can be loaded efficiently via a
1835 base-register relative addressing mode. Since the offset in
1836 base-register relative mode is fixed and relatively small (e.g., 16
1837 bits), this limits the maximum size of the constant pool. Thus, in
1838 large programs, it is often necessary to use multiple global pointer
1839 values in order to be able to address all possible constants. This
1840 option causes a warning to be issued whenever this case occurs.
1841
1842 @kindex --warn-once
1843 @cindex warnings, on undefined symbols
1844 @cindex undefined symbols, warnings on
1845 @item --warn-once
1846 Only warn once for each undefined symbol, rather than once per module
1847 which refers to it.
1848
1849 @kindex --warn-section-align
1850 @cindex warnings, on section alignment
1851 @cindex section alignment, warnings on
1852 @item --warn-section-align
1853 Warn if the address of an output section is changed because of
1854 alignment. Typically, the alignment will be set by an input section.
1855 The address will only be changed if it not explicitly specified; that
1856 is, if the @code{SECTIONS} command does not specify a start address for
1857 the section (@pxref{SECTIONS}).
1858
1859 @kindex --warn-shared-textrel
1860 @item --warn-shared-textrel
1861 Warn if the linker adds a DT_TEXTREL to a shared object.
1862
1863 @kindex --warn-unresolved-symbols
1864 @item --warn-unresolved-symbols
1865 If the linker is going to report an unresolved symbol (see the option
1866 @option{--unresolved-symbols}) it will normally generate an error.
1867 This option makes it generate a warning instead.
1868
1869 @kindex --error-unresolved-symbols
1870 @item --error-unresolved-symbols
1871 This restores the linker's default behaviour of generating errors when
1872 it is reporting unresolved symbols.
1873
1874 @kindex --whole-archive
1875 @cindex including an entire archive
1876 @item --whole-archive
1877 For each archive mentioned on the command line after the
1878 @option{--whole-archive} option, include every object file in the archive
1879 in the link, rather than searching the archive for the required object
1880 files. This is normally used to turn an archive file into a shared
1881 library, forcing every object to be included in the resulting shared
1882 library. This option may be used more than once.
1883
1884 Two notes when using this option from gcc: First, gcc doesn't know
1885 about this option, so you have to use @option{-Wl,-whole-archive}.
1886 Second, don't forget to use @option{-Wl,-no-whole-archive} after your
1887 list of archives, because gcc will add its own list of archives to
1888 your link and you may not want this flag to affect those as well.
1889
1890 @kindex --wrap
1891 @item --wrap @var{symbol}
1892 Use a wrapper function for @var{symbol}. Any undefined reference to
1893 @var{symbol} will be resolved to @code{__wrap_@var{symbol}}. Any
1894 undefined reference to @code{__real_@var{symbol}} will be resolved to
1895 @var{symbol}.
1896
1897 This can be used to provide a wrapper for a system function. The
1898 wrapper function should be called @code{__wrap_@var{symbol}}. If it
1899 wishes to call the system function, it should call
1900 @code{__real_@var{symbol}}.
1901
1902 Here is a trivial example:
1903
1904 @smallexample
1905 void *
1906 __wrap_malloc (size_t c)
1907 @{
1908 printf ("malloc called with %zu\n", c);
1909 return __real_malloc (c);
1910 @}
1911 @end smallexample
1912
1913 If you link other code with this file using @option{--wrap malloc}, then
1914 all calls to @code{malloc} will call the function @code{__wrap_malloc}
1915 instead. The call to @code{__real_malloc} in @code{__wrap_malloc} will
1916 call the real @code{malloc} function.
1917
1918 You may wish to provide a @code{__real_malloc} function as well, so that
1919 links without the @option{--wrap} option will succeed. If you do this,
1920 you should not put the definition of @code{__real_malloc} in the same
1921 file as @code{__wrap_malloc}; if you do, the assembler may resolve the
1922 call before the linker has a chance to wrap it to @code{malloc}.
1923
1924 @kindex --eh-frame-hdr
1925 @item --eh-frame-hdr
1926 Request creation of @code{.eh_frame_hdr} section and ELF
1927 @code{PT_GNU_EH_FRAME} segment header.
1928
1929 @kindex --enable-new-dtags
1930 @kindex --disable-new-dtags
1931 @item --enable-new-dtags
1932 @itemx --disable-new-dtags
1933 This linker can create the new dynamic tags in ELF. But the older ELF
1934 systems may not understand them. If you specify
1935 @option{--enable-new-dtags}, the dynamic tags will be created as needed.
1936 If you specify @option{--disable-new-dtags}, no new dynamic tags will be
1937 created. By default, the new dynamic tags are not created. Note that
1938 those options are only available for ELF systems.
1939
1940 @kindex --hash-size=@var{number}
1941 @item --hash-size=@var{number}
1942 Set the default size of the linker's hash tables to a prime number
1943 close to @var{number}. Increasing this value can reduce the length of
1944 time it takes the linker to perform its tasks, at the expense of
1945 increasing the linker's memory requirements. Similarly reducing this
1946 value can reduce the memory requirements at the expense of speed.
1947
1948 @kindex --hash-style=@var{style}
1949 @item --hash-style=@var{style}
1950 Set the type of linker's hash table(s). @var{style} can be either
1951 @code{sysv} for classic ELF @code{.hash} section, @code{gnu} for
1952 new style GNU @code{.gnu.hash} section or @code{both} for both
1953 the classic ELF @code{.hash} and new style GNU @code{.gnu.hash}
1954 hash tables. The default is @code{sysv}.
1955
1956 @kindex --reduce-memory-overheads
1957 @item --reduce-memory-overheads
1958 This option reduces memory requirements at ld runtime, at the expense of
1959 linking speed. This was introduced to select the old O(n^2) algorithm
1960 for link map file generation, rather than the new O(n) algorithm which uses
1961 about 40% more memory for symbol storage.
1962
1963 Another effect of the switch is to set the default hash table size to
1964 1021, which again saves memory at the cost of lengthening the linker's
1965 run time. This is not done however if the @option{--hash-size} switch
1966 has been used.
1967
1968 The @option{--reduce-memory-overheads} switch may be also be used to
1969 enable other tradeoffs in future versions of the linker.
1970
1971 @end table
1972
1973 @c man end
1974
1975 @subsection Options Specific to i386 PE Targets
1976
1977 @c man begin OPTIONS
1978
1979 The i386 PE linker supports the @option{-shared} option, which causes
1980 the output to be a dynamically linked library (DLL) instead of a
1981 normal executable. You should name the output @code{*.dll} when you
1982 use this option. In addition, the linker fully supports the standard
1983 @code{*.def} files, which may be specified on the linker command line
1984 like an object file (in fact, it should precede archives it exports
1985 symbols from, to ensure that they get linked in, just like a normal
1986 object file).
1987
1988 In addition to the options common to all targets, the i386 PE linker
1989 support additional command line options that are specific to the i386
1990 PE target. Options that take values may be separated from their
1991 values by either a space or an equals sign.
1992
1993 @table @gcctabopt
1994
1995 @kindex --add-stdcall-alias
1996 @item --add-stdcall-alias
1997 If given, symbols with a stdcall suffix (@@@var{nn}) will be exported
1998 as-is and also with the suffix stripped.
1999 [This option is specific to the i386 PE targeted port of the linker]
2000
2001 @kindex --base-file
2002 @item --base-file @var{file}
2003 Use @var{file} as the name of a file in which to save the base
2004 addresses of all the relocations needed for generating DLLs with
2005 @file{dlltool}.
2006 [This is an i386 PE specific option]
2007
2008 @kindex --dll
2009 @item --dll
2010 Create a DLL instead of a regular executable. You may also use
2011 @option{-shared} or specify a @code{LIBRARY} in a given @code{.def}
2012 file.
2013 [This option is specific to the i386 PE targeted port of the linker]
2014
2015 @kindex --enable-stdcall-fixup
2016 @kindex --disable-stdcall-fixup
2017 @item --enable-stdcall-fixup
2018 @itemx --disable-stdcall-fixup
2019 If the link finds a symbol that it cannot resolve, it will attempt to
2020 do ``fuzzy linking'' by looking for another defined symbol that differs
2021 only in the format of the symbol name (cdecl vs stdcall) and will
2022 resolve that symbol by linking to the match. For example, the
2023 undefined symbol @code{_foo} might be linked to the function
2024 @code{_foo@@12}, or the undefined symbol @code{_bar@@16} might be linked
2025 to the function @code{_bar}. When the linker does this, it prints a
2026 warning, since it normally should have failed to link, but sometimes
2027 import libraries generated from third-party dlls may need this feature
2028 to be usable. If you specify @option{--enable-stdcall-fixup}, this
2029 feature is fully enabled and warnings are not printed. If you specify
2030 @option{--disable-stdcall-fixup}, this feature is disabled and such
2031 mismatches are considered to be errors.
2032 [This option is specific to the i386 PE targeted port of the linker]
2033
2034 @cindex DLLs, creating
2035 @kindex --export-all-symbols
2036 @item --export-all-symbols
2037 If given, all global symbols in the objects used to build a DLL will
2038 be exported by the DLL. Note that this is the default if there
2039 otherwise wouldn't be any exported symbols. When symbols are
2040 explicitly exported via DEF files or implicitly exported via function
2041 attributes, the default is to not export anything else unless this
2042 option is given. Note that the symbols @code{DllMain@@12},
2043 @code{DllEntryPoint@@0}, @code{DllMainCRTStartup@@12}, and
2044 @code{impure_ptr} will not be automatically
2045 exported. Also, symbols imported from other DLLs will not be
2046 re-exported, nor will symbols specifying the DLL's internal layout
2047 such as those beginning with @code{_head_} or ending with
2048 @code{_iname}. In addition, no symbols from @code{libgcc},
2049 @code{libstd++}, @code{libmingw32}, or @code{crtX.o} will be exported.
2050 Symbols whose names begin with @code{__rtti_} or @code{__builtin_} will
2051 not be exported, to help with C++ DLLs. Finally, there is an
2052 extensive list of cygwin-private symbols that are not exported
2053 (obviously, this applies on when building DLLs for cygwin targets).
2054 These cygwin-excludes are: @code{_cygwin_dll_entry@@12},
2055 @code{_cygwin_crt0_common@@8}, @code{_cygwin_noncygwin_dll_entry@@12},
2056 @code{_fmode}, @code{_impure_ptr}, @code{cygwin_attach_dll},
2057 @code{cygwin_premain0}, @code{cygwin_premain1}, @code{cygwin_premain2},
2058 @code{cygwin_premain3}, and @code{environ}.
2059 [This option is specific to the i386 PE targeted port of the linker]
2060
2061 @kindex --exclude-symbols
2062 @item --exclude-symbols @var{symbol},@var{symbol},...
2063 Specifies a list of symbols which should not be automatically
2064 exported. The symbol names may be delimited by commas or colons.
2065 [This option is specific to the i386 PE targeted port of the linker]
2066
2067 @kindex --file-alignment
2068 @item --file-alignment
2069 Specify the file alignment. Sections in the file will always begin at
2070 file offsets which are multiples of this number. This defaults to
2071 512.
2072 [This option is specific to the i386 PE targeted port of the linker]
2073
2074 @cindex heap size
2075 @kindex --heap
2076 @item --heap @var{reserve}
2077 @itemx --heap @var{reserve},@var{commit}
2078 Specify the amount of memory to reserve (and optionally commit) to be
2079 used as heap for this program. The default is 1Mb reserved, 4K
2080 committed.
2081 [This option is specific to the i386 PE targeted port of the linker]
2082
2083 @cindex image base
2084 @kindex --image-base
2085 @item --image-base @var{value}
2086 Use @var{value} as the base address of your program or dll. This is
2087 the lowest memory location that will be used when your program or dll
2088 is loaded. To reduce the need to relocate and improve performance of
2089 your dlls, each should have a unique base address and not overlap any
2090 other dlls. The default is 0x400000 for executables, and 0x10000000
2091 for dlls.
2092 [This option is specific to the i386 PE targeted port of the linker]
2093
2094 @kindex --kill-at
2095 @item --kill-at
2096 If given, the stdcall suffixes (@@@var{nn}) will be stripped from
2097 symbols before they are exported.
2098 [This option is specific to the i386 PE targeted port of the linker]
2099
2100 @kindex --large-address-aware
2101 @item --large-address-aware
2102 If given, the appropriate bit in the ``Characteristics'' field of the COFF
2103 header is set to indicate that this executable supports virtual addresses
2104 greater than 2 gigabytes. This should be used in conjunction with the /3GB
2105 or /USERVA=@var{value} megabytes switch in the ``[operating systems]''
2106 section of the BOOT.INI. Otherwise, this bit has no effect.
2107 [This option is specific to PE targeted ports of the linker]
2108
2109 @kindex --major-image-version
2110 @item --major-image-version @var{value}
2111 Sets the major number of the ``image version''. Defaults to 1.
2112 [This option is specific to the i386 PE targeted port of the linker]
2113
2114 @kindex --major-os-version
2115 @item --major-os-version @var{value}
2116 Sets the major number of the ``os version''. Defaults to 4.
2117 [This option is specific to the i386 PE targeted port of the linker]
2118
2119 @kindex --major-subsystem-version
2120 @item --major-subsystem-version @var{value}
2121 Sets the major number of the ``subsystem version''. Defaults to 4.
2122 [This option is specific to the i386 PE targeted port of the linker]
2123
2124 @kindex --minor-image-version
2125 @item --minor-image-version @var{value}
2126 Sets the minor number of the ``image version''. Defaults to 0.
2127 [This option is specific to the i386 PE targeted port of the linker]
2128
2129 @kindex --minor-os-version
2130 @item --minor-os-version @var{value}
2131 Sets the minor number of the ``os version''. Defaults to 0.
2132 [This option is specific to the i386 PE targeted port of the linker]
2133
2134 @kindex --minor-subsystem-version
2135 @item --minor-subsystem-version @var{value}
2136 Sets the minor number of the ``subsystem version''. Defaults to 0.
2137 [This option is specific to the i386 PE targeted port of the linker]
2138
2139 @cindex DEF files, creating
2140 @cindex DLLs, creating
2141 @kindex --output-def
2142 @item --output-def @var{file}
2143 The linker will create the file @var{file} which will contain a DEF
2144 file corresponding to the DLL the linker is generating. This DEF file
2145 (which should be called @code{*.def}) may be used to create an import
2146 library with @code{dlltool} or may be used as a reference to
2147 automatically or implicitly exported symbols.
2148 [This option is specific to the i386 PE targeted port of the linker]
2149
2150 @cindex DLLs, creating
2151 @kindex --out-implib
2152 @item --out-implib @var{file}
2153 The linker will create the file @var{file} which will contain an
2154 import lib corresponding to the DLL the linker is generating. This
2155 import lib (which should be called @code{*.dll.a} or @code{*.a}
2156 may be used to link clients against the generated DLL; this behaviour
2157 makes it possible to skip a separate @code{dlltool} import library
2158 creation step.
2159 [This option is specific to the i386 PE targeted port of the linker]
2160
2161 @kindex --enable-auto-image-base
2162 @item --enable-auto-image-base
2163 Automatically choose the image base for DLLs, unless one is specified
2164 using the @code{--image-base} argument. By using a hash generated
2165 from the dllname to create unique image bases for each DLL, in-memory
2166 collisions and relocations which can delay program execution are
2167 avoided.
2168 [This option is specific to the i386 PE targeted port of the linker]
2169
2170 @kindex --disable-auto-image-base
2171 @item --disable-auto-image-base
2172 Do not automatically generate a unique image base. If there is no
2173 user-specified image base (@code{--image-base}) then use the platform
2174 default.
2175 [This option is specific to the i386 PE targeted port of the linker]
2176
2177 @cindex DLLs, linking to
2178 @kindex --dll-search-prefix
2179 @item --dll-search-prefix @var{string}
2180 When linking dynamically to a dll without an import library,
2181 search for @code{<string><basename>.dll} in preference to
2182 @code{lib<basename>.dll}. This behaviour allows easy distinction
2183 between DLLs built for the various "subplatforms": native, cygwin,
2184 uwin, pw, etc. For instance, cygwin DLLs typically use
2185 @code{--dll-search-prefix=cyg}.
2186 [This option is specific to the i386 PE targeted port of the linker]
2187
2188 @kindex --enable-auto-import
2189 @item --enable-auto-import
2190 Do sophisticated linking of @code{_symbol} to @code{__imp__symbol} for
2191 DATA imports from DLLs, and create the necessary thunking symbols when
2192 building the import libraries with those DATA exports. Note: Use of the
2193 'auto-import' extension will cause the text section of the image file
2194 to be made writable. This does not conform to the PE-COFF format
2195 specification published by Microsoft.
2196
2197 Using 'auto-import' generally will 'just work' -- but sometimes you may
2198 see this message:
2199
2200 "variable '<var>' can't be auto-imported. Please read the
2201 documentation for ld's @code{--enable-auto-import} for details."
2202
2203 This message occurs when some (sub)expression accesses an address
2204 ultimately given by the sum of two constants (Win32 import tables only
2205 allow one). Instances where this may occur include accesses to member
2206 fields of struct variables imported from a DLL, as well as using a
2207 constant index into an array variable imported from a DLL. Any
2208 multiword variable (arrays, structs, long long, etc) may trigger
2209 this error condition. However, regardless of the exact data type
2210 of the offending exported variable, ld will always detect it, issue
2211 the warning, and exit.
2212
2213 There are several ways to address this difficulty, regardless of the
2214 data type of the exported variable:
2215
2216 One way is to use --enable-runtime-pseudo-reloc switch. This leaves the task
2217 of adjusting references in your client code for runtime environment, so
2218 this method works only when runtime environment supports this feature.
2219
2220 A second solution is to force one of the 'constants' to be a variable --
2221 that is, unknown and un-optimizable at compile time. For arrays,
2222 there are two possibilities: a) make the indexee (the array's address)
2223 a variable, or b) make the 'constant' index a variable. Thus:
2224
2225 @example
2226 extern type extern_array[];
2227 extern_array[1] -->
2228 @{ volatile type *t=extern_array; t[1] @}
2229 @end example
2230
2231 or
2232
2233 @example
2234 extern type extern_array[];
2235 extern_array[1] -->
2236 @{ volatile int t=1; extern_array[t] @}
2237 @end example
2238
2239 For structs (and most other multiword data types) the only option
2240 is to make the struct itself (or the long long, or the ...) variable:
2241
2242 @example
2243 extern struct s extern_struct;
2244 extern_struct.field -->
2245 @{ volatile struct s *t=&extern_struct; t->field @}
2246 @end example
2247
2248 or
2249
2250 @example
2251 extern long long extern_ll;
2252 extern_ll -->
2253 @{ volatile long long * local_ll=&extern_ll; *local_ll @}
2254 @end example
2255
2256 A third method of dealing with this difficulty is to abandon
2257 'auto-import' for the offending symbol and mark it with
2258 @code{__declspec(dllimport)}. However, in practise that
2259 requires using compile-time #defines to indicate whether you are
2260 building a DLL, building client code that will link to the DLL, or
2261 merely building/linking to a static library. In making the choice
2262 between the various methods of resolving the 'direct address with
2263 constant offset' problem, you should consider typical real-world usage:
2264
2265 Original:
2266 @example
2267 --foo.h
2268 extern int arr[];
2269 --foo.c
2270 #include "foo.h"
2271 void main(int argc, char **argv)@{
2272 printf("%d\n",arr[1]);
2273 @}
2274 @end example
2275
2276 Solution 1:
2277 @example
2278 --foo.h
2279 extern int arr[];
2280 --foo.c
2281 #include "foo.h"
2282 void main(int argc, char **argv)@{
2283 /* This workaround is for win32 and cygwin; do not "optimize" */
2284 volatile int *parr = arr;
2285 printf("%d\n",parr[1]);
2286 @}
2287 @end example
2288
2289 Solution 2:
2290 @example
2291 --foo.h
2292 /* Note: auto-export is assumed (no __declspec(dllexport)) */
2293 #if (defined(_WIN32) || defined(__CYGWIN__)) && \
2294 !(defined(FOO_BUILD_DLL) || defined(FOO_STATIC))
2295 #define FOO_IMPORT __declspec(dllimport)
2296 #else
2297 #define FOO_IMPORT
2298 #endif
2299 extern FOO_IMPORT int arr[];
2300 --foo.c
2301 #include "foo.h"
2302 void main(int argc, char **argv)@{
2303 printf("%d\n",arr[1]);
2304 @}
2305 @end example
2306
2307 A fourth way to avoid this problem is to re-code your
2308 library to use a functional interface rather than a data interface
2309 for the offending variables (e.g. set_foo() and get_foo() accessor
2310 functions).
2311 [This option is specific to the i386 PE targeted port of the linker]
2312
2313 @kindex --disable-auto-import
2314 @item --disable-auto-import
2315 Do not attempt to do sophisticated linking of @code{_symbol} to
2316 @code{__imp__symbol} for DATA imports from DLLs.
2317 [This option is specific to the i386 PE targeted port of the linker]
2318
2319 @kindex --enable-runtime-pseudo-reloc
2320 @item --enable-runtime-pseudo-reloc
2321 If your code contains expressions described in --enable-auto-import section,
2322 that is, DATA imports from DLL with non-zero offset, this switch will create
2323 a vector of 'runtime pseudo relocations' which can be used by runtime
2324 environment to adjust references to such data in your client code.
2325 [This option is specific to the i386 PE targeted port of the linker]
2326
2327 @kindex --disable-runtime-pseudo-reloc
2328 @item --disable-runtime-pseudo-reloc
2329 Do not create pseudo relocations for non-zero offset DATA imports from
2330 DLLs. This is the default.
2331 [This option is specific to the i386 PE targeted port of the linker]
2332
2333 @kindex --enable-extra-pe-debug
2334 @item --enable-extra-pe-debug
2335 Show additional debug info related to auto-import symbol thunking.
2336 [This option is specific to the i386 PE targeted port of the linker]
2337
2338 @kindex --section-alignment
2339 @item --section-alignment
2340 Sets the section alignment. Sections in memory will always begin at
2341 addresses which are a multiple of this number. Defaults to 0x1000.
2342 [This option is specific to the i386 PE targeted port of the linker]
2343
2344 @cindex stack size
2345 @kindex --stack
2346 @item --stack @var{reserve}
2347 @itemx --stack @var{reserve},@var{commit}
2348 Specify the amount of memory to reserve (and optionally commit) to be
2349 used as stack for this program. The default is 2Mb reserved, 4K
2350 committed.
2351 [This option is specific to the i386 PE targeted port of the linker]
2352
2353 @kindex --subsystem
2354 @item --subsystem @var{which}
2355 @itemx --subsystem @var{which}:@var{major}
2356 @itemx --subsystem @var{which}:@var{major}.@var{minor}
2357 Specifies the subsystem under which your program will execute. The
2358 legal values for @var{which} are @code{native}, @code{windows},
2359 @code{console}, @code{posix}, and @code{xbox}. You may optionally set
2360 the subsystem version also. Numeric values are also accepted for
2361 @var{which}.
2362 [This option is specific to the i386 PE targeted port of the linker]
2363
2364 @end table
2365
2366 @c man end
2367
2368 @ifset M68HC11
2369 @subsection Options specific to Motorola 68HC11 and 68HC12 targets
2370
2371 @c man begin OPTIONS
2372
2373 The 68HC11 and 68HC12 linkers support specific options to control the
2374 memory bank switching mapping and trampoline code generation.
2375
2376 @table @gcctabopt
2377
2378 @kindex --no-trampoline
2379 @item --no-trampoline
2380 This option disables the generation of trampoline. By default a trampoline
2381 is generated for each far function which is called using a @code{jsr}
2382 instruction (this happens when a pointer to a far function is taken).
2383
2384 @kindex --bank-window
2385 @item --bank-window @var{name}
2386 This option indicates to the linker the name of the memory region in
2387 the @samp{MEMORY} specification that describes the memory bank window.
2388 The definition of such region is then used by the linker to compute
2389 paging and addresses within the memory window.
2390
2391 @end table
2392
2393 @c man end
2394 @end ifset
2395
2396 @ifset UsesEnvVars
2397 @node Environment
2398 @section Environment Variables
2399
2400 @c man begin ENVIRONMENT
2401
2402 You can change the behaviour of @command{ld} with the environment variables
2403 @ifclear SingleFormat
2404 @code{GNUTARGET},
2405 @end ifclear
2406 @code{LDEMULATION} and @code{COLLECT_NO_DEMANGLE}.
2407
2408 @ifclear SingleFormat
2409 @kindex GNUTARGET
2410 @cindex default input format
2411 @code{GNUTARGET} determines the input-file object format if you don't
2412 use @samp{-b} (or its synonym @samp{--format}). Its value should be one
2413 of the BFD names for an input format (@pxref{BFD}). If there is no
2414 @code{GNUTARGET} in the environment, @command{ld} uses the natural format
2415 of the target. If @code{GNUTARGET} is set to @code{default} then BFD
2416 attempts to discover the input format by examining binary input files;
2417 this method often succeeds, but there are potential ambiguities, since
2418 there is no method of ensuring that the magic number used to specify
2419 object-file formats is unique. However, the configuration procedure for
2420 BFD on each system places the conventional format for that system first
2421 in the search-list, so ambiguities are resolved in favor of convention.
2422 @end ifclear
2423
2424 @kindex LDEMULATION
2425 @cindex default emulation
2426 @cindex emulation, default
2427 @code{LDEMULATION} determines the default emulation if you don't use the
2428 @samp{-m} option. The emulation can affect various aspects of linker
2429 behaviour, particularly the default linker script. You can list the
2430 available emulations with the @samp{--verbose} or @samp{-V} options. If
2431 the @samp{-m} option is not used, and the @code{LDEMULATION} environment
2432 variable is not defined, the default emulation depends upon how the
2433 linker was configured.
2434
2435 @kindex COLLECT_NO_DEMANGLE
2436 @cindex demangling, default
2437 Normally, the linker will default to demangling symbols. However, if
2438 @code{COLLECT_NO_DEMANGLE} is set in the environment, then it will
2439 default to not demangling symbols. This environment variable is used in
2440 a similar fashion by the @code{gcc} linker wrapper program. The default
2441 may be overridden by the @samp{--demangle} and @samp{--no-demangle}
2442 options.
2443
2444 @c man end
2445 @end ifset
2446
2447 @node Scripts
2448 @chapter Linker Scripts
2449
2450 @cindex scripts
2451 @cindex linker scripts
2452 @cindex command files
2453 Every link is controlled by a @dfn{linker script}. This script is
2454 written in the linker command language.
2455
2456 The main purpose of the linker script is to describe how the sections in
2457 the input files should be mapped into the output file, and to control
2458 the memory layout of the output file. Most linker scripts do nothing
2459 more than this. However, when necessary, the linker script can also
2460 direct the linker to perform many other operations, using the commands
2461 described below.
2462
2463 The linker always uses a linker script. If you do not supply one
2464 yourself, the linker will use a default script that is compiled into the
2465 linker executable. You can use the @samp{--verbose} command line option
2466 to display the default linker script. Certain command line options,
2467 such as @samp{-r} or @samp{-N}, will affect the default linker script.
2468
2469 You may supply your own linker script by using the @samp{-T} command
2470 line option. When you do this, your linker script will replace the
2471 default linker script.
2472
2473 You may also use linker scripts implicitly by naming them as input files
2474 to the linker, as though they were files to be linked. @xref{Implicit
2475 Linker Scripts}.
2476
2477 @menu
2478 * Basic Script Concepts:: Basic Linker Script Concepts
2479 * Script Format:: Linker Script Format
2480 * Simple Example:: Simple Linker Script Example
2481 * Simple Commands:: Simple Linker Script Commands
2482 * Assignments:: Assigning Values to Symbols
2483 * SECTIONS:: SECTIONS Command
2484 * MEMORY:: MEMORY Command
2485 * PHDRS:: PHDRS Command
2486 * VERSION:: VERSION Command
2487 * Expressions:: Expressions in Linker Scripts
2488 * Implicit Linker Scripts:: Implicit Linker Scripts
2489 @end menu
2490
2491 @node Basic Script Concepts
2492 @section Basic Linker Script Concepts
2493 @cindex linker script concepts
2494 We need to define some basic concepts and vocabulary in order to
2495 describe the linker script language.
2496
2497 The linker combines input files into a single output file. The output
2498 file and each input file are in a special data format known as an
2499 @dfn{object file format}. Each file is called an @dfn{object file}.
2500 The output file is often called an @dfn{executable}, but for our
2501 purposes we will also call it an object file. Each object file has,
2502 among other things, a list of @dfn{sections}. We sometimes refer to a
2503 section in an input file as an @dfn{input section}; similarly, a section
2504 in the output file is an @dfn{output section}.
2505
2506 Each section in an object file has a name and a size. Most sections
2507 also have an associated block of data, known as the @dfn{section
2508 contents}. A section may be marked as @dfn{loadable}, which mean that
2509 the contents should be loaded into memory when the output file is run.
2510 A section with no contents may be @dfn{allocatable}, which means that an
2511 area in memory should be set aside, but nothing in particular should be
2512 loaded there (in some cases this memory must be zeroed out). A section
2513 which is neither loadable nor allocatable typically contains some sort
2514 of debugging information.
2515
2516 Every loadable or allocatable output section has two addresses. The
2517 first is the @dfn{VMA}, or virtual memory address. This is the address
2518 the section will have when the output file is run. The second is the
2519 @dfn{LMA}, or load memory address. This is the address at which the
2520 section will be loaded. In most cases the two addresses will be the
2521 same. An example of when they might be different is when a data section
2522 is loaded into ROM, and then copied into RAM when the program starts up
2523 (this technique is often used to initialize global variables in a ROM
2524 based system). In this case the ROM address would be the LMA, and the
2525 RAM address would be the VMA.
2526
2527 You can see the sections in an object file by using the @code{objdump}
2528 program with the @samp{-h} option.
2529
2530 Every object file also has a list of @dfn{symbols}, known as the
2531 @dfn{symbol table}. A symbol may be defined or undefined. Each symbol
2532 has a name, and each defined symbol has an address, among other
2533 information. If you compile a C or C++ program into an object file, you
2534 will get a defined symbol for every defined function and global or
2535 static variable. Every undefined function or global variable which is
2536 referenced in the input file will become an undefined symbol.
2537
2538 You can see the symbols in an object file by using the @code{nm}
2539 program, or by using the @code{objdump} program with the @samp{-t}
2540 option.
2541
2542 @node Script Format
2543 @section Linker Script Format
2544 @cindex linker script format
2545 Linker scripts are text files.
2546
2547 You write a linker script as a series of commands. Each command is
2548 either a keyword, possibly followed by arguments, or an assignment to a
2549 symbol. You may separate commands using semicolons. Whitespace is
2550 generally ignored.
2551
2552 Strings such as file or format names can normally be entered directly.
2553 If the file name contains a character such as a comma which would
2554 otherwise serve to separate file names, you may put the file name in
2555 double quotes. There is no way to use a double quote character in a
2556 file name.
2557
2558 You may include comments in linker scripts just as in C, delimited by
2559 @samp{/*} and @samp{*/}. As in C, comments are syntactically equivalent
2560 to whitespace.
2561
2562 @node Simple Example
2563 @section Simple Linker Script Example
2564 @cindex linker script example
2565 @cindex example of linker script
2566 Many linker scripts are fairly simple.
2567
2568 The simplest possible linker script has just one command:
2569 @samp{SECTIONS}. You use the @samp{SECTIONS} command to describe the
2570 memory layout of the output file.
2571
2572 The @samp{SECTIONS} command is a powerful command. Here we will
2573 describe a simple use of it. Let's assume your program consists only of
2574 code, initialized data, and uninitialized data. These will be in the
2575 @samp{.text}, @samp{.data}, and @samp{.bss} sections, respectively.
2576 Let's assume further that these are the only sections which appear in
2577 your input files.
2578
2579 For this example, let's say that the code should be loaded at address
2580 0x10000, and that the data should start at address 0x8000000. Here is a
2581 linker script which will do that:
2582 @smallexample
2583 SECTIONS
2584 @{
2585 . = 0x10000;
2586 .text : @{ *(.text) @}
2587 . = 0x8000000;
2588 .data : @{ *(.data) @}
2589 .bss : @{ *(.bss) @}
2590 @}
2591 @end smallexample
2592
2593 You write the @samp{SECTIONS} command as the keyword @samp{SECTIONS},
2594 followed by a series of symbol assignments and output section
2595 descriptions enclosed in curly braces.
2596
2597 The first line inside the @samp{SECTIONS} command of the above example
2598 sets the value of the special symbol @samp{.}, which is the location
2599 counter. If you do not specify the address of an output section in some
2600 other way (other ways are described later), the address is set from the
2601 current value of the location counter. The location counter is then
2602 incremented by the size of the output section. At the start of the
2603 @samp{SECTIONS} command, the location counter has the value @samp{0}.
2604
2605 The second line defines an output section, @samp{.text}. The colon is
2606 required syntax which may be ignored for now. Within the curly braces
2607 after the output section name, you list the names of the input sections
2608 which should be placed into this output section. The @samp{*} is a
2609 wildcard which matches any file name. The expression @samp{*(.text)}
2610 means all @samp{.text} input sections in all input files.
2611
2612 Since the location counter is @samp{0x10000} when the output section
2613 @samp{.text} is defined, the linker will set the address of the
2614 @samp{.text} section in the output file to be @samp{0x10000}.
2615
2616 The remaining lines define the @samp{.data} and @samp{.bss} sections in
2617 the output file. The linker will place the @samp{.data} output section
2618 at address @samp{0x8000000}. After the linker places the @samp{.data}
2619 output section, the value of the location counter will be
2620 @samp{0x8000000} plus the size of the @samp{.data} output section. The
2621 effect is that the linker will place the @samp{.bss} output section
2622 immediately after the @samp{.data} output section in memory.
2623
2624 The linker will ensure that each output section has the required
2625 alignment, by increasing the location counter if necessary. In this
2626 example, the specified addresses for the @samp{.text} and @samp{.data}
2627 sections will probably satisfy any alignment constraints, but the linker
2628 may have to create a small gap between the @samp{.data} and @samp{.bss}
2629 sections.
2630
2631 That's it! That's a simple and complete linker script.
2632
2633 @node Simple Commands
2634 @section Simple Linker Script Commands
2635 @cindex linker script simple commands
2636 In this section we describe the simple linker script commands.
2637
2638 @menu
2639 * Entry Point:: Setting the entry point
2640 * File Commands:: Commands dealing with files
2641 @ifclear SingleFormat
2642 * Format Commands:: Commands dealing with object file formats
2643 @end ifclear
2644
2645 * Miscellaneous Commands:: Other linker script commands
2646 @end menu
2647
2648 @node Entry Point
2649 @subsection Setting the Entry Point
2650 @kindex ENTRY(@var{symbol})
2651 @cindex start of execution
2652 @cindex first instruction
2653 @cindex entry point
2654 The first instruction to execute in a program is called the @dfn{entry
2655 point}. You can use the @code{ENTRY} linker script command to set the
2656 entry point. The argument is a symbol name:
2657 @smallexample
2658 ENTRY(@var{symbol})
2659 @end smallexample
2660
2661 There are several ways to set the entry point. The linker will set the
2662 entry point by trying each of the following methods in order, and
2663 stopping when one of them succeeds:
2664 @itemize @bullet
2665 @item
2666 the @samp{-e} @var{entry} command-line option;
2667 @item
2668 the @code{ENTRY(@var{symbol})} command in a linker script;
2669 @item
2670 the value of the symbol @code{start}, if defined;
2671 @item
2672 the address of the first byte of the @samp{.text} section, if present;
2673 @item
2674 The address @code{0}.
2675 @end itemize
2676
2677 @node File Commands
2678 @subsection Commands Dealing with Files
2679 @cindex linker script file commands
2680 Several linker script commands deal with files.
2681
2682 @table @code
2683 @item INCLUDE @var{filename}
2684 @kindex INCLUDE @var{filename}
2685 @cindex including a linker script
2686 Include the linker script @var{filename} at this point. The file will
2687 be searched for in the current directory, and in any directory specified
2688 with the @option{-L} option. You can nest calls to @code{INCLUDE} up to
2689 10 levels deep.
2690
2691 @item INPUT(@var{file}, @var{file}, @dots{})
2692 @itemx INPUT(@var{file} @var{file} @dots{})
2693 @kindex INPUT(@var{files})
2694 @cindex input files in linker scripts
2695 @cindex input object files in linker scripts
2696 @cindex linker script input object files
2697 The @code{INPUT} command directs the linker to include the named files
2698 in the link, as though they were named on the command line.
2699
2700 For example, if you always want to include @file{subr.o} any time you do
2701 a link, but you can't be bothered to put it on every link command line,
2702 then you can put @samp{INPUT (subr.o)} in your linker script.
2703
2704 In fact, if you like, you can list all of your input files in the linker
2705 script, and then invoke the linker with nothing but a @samp{-T} option.
2706
2707 In case a @dfn{sysroot prefix} is configured, and the filename starts
2708 with the @samp{/} character, and the script being processed was
2709 located inside the @dfn{sysroot prefix}, the filename will be looked
2710 for in the @dfn{sysroot prefix}. Otherwise, the linker will try to
2711 open the file in the current directory. If it is not found, the
2712 linker will search through the archive library search path. See the
2713 description of @samp{-L} in @ref{Options,,Command Line Options}.
2714
2715 If you use @samp{INPUT (-l@var{file})}, @command{ld} will transform the
2716 name to @code{lib@var{file}.a}, as with the command line argument
2717 @samp{-l}.
2718
2719 When you use the @code{INPUT} command in an implicit linker script, the
2720 files will be included in the link at the point at which the linker
2721 script file is included. This can affect archive searching.
2722
2723 @item GROUP(@var{file}, @var{file}, @dots{})
2724 @itemx GROUP(@var{file} @var{file} @dots{})
2725 @kindex GROUP(@var{files})
2726 @cindex grouping input files
2727 The @code{GROUP} command is like @code{INPUT}, except that the named
2728 files should all be archives, and they are searched repeatedly until no
2729 new undefined references are created. See the description of @samp{-(}
2730 in @ref{Options,,Command Line Options}.
2731
2732 @item AS_NEEDED(@var{file}, @var{file}, @dots{})
2733 @itemx AS_NEEDED(@var{file} @var{file} @dots{})
2734 @kindex AS_NEEDED(@var{files})
2735 This construct can appear only inside of the @code{INPUT} or @code{GROUP}
2736 commands, among other filenames. The files listed will be handled
2737 as if they appear directly in the @code{INPUT} or @code{GROUP} commands,
2738 with the exception of ELF shared libraries, that will be added only
2739 when they are actually needed. This construct essentially enables
2740 @option{--as-needed} option for all the files listed inside of it
2741 and restores previous @option{--as-needed} resp. @option{--no-as-needed}
2742 setting afterwards.
2743
2744 @item OUTPUT(@var{filename})
2745 @kindex OUTPUT(@var{filename})
2746 @cindex output file name in linker script
2747 The @code{OUTPUT} command names the output file. Using
2748 @code{OUTPUT(@var{filename})} in the linker script is exactly like using
2749 @samp{-o @var{filename}} on the command line (@pxref{Options,,Command
2750 Line Options}). If both are used, the command line option takes
2751 precedence.
2752
2753 You can use the @code{OUTPUT} command to define a default name for the
2754 output file other than the usual default of @file{a.out}.
2755
2756 @item SEARCH_DIR(@var{path})
2757 @kindex SEARCH_DIR(@var{path})
2758 @cindex library search path in linker script
2759 @cindex archive search path in linker script
2760 @cindex search path in linker script
2761 The @code{SEARCH_DIR} command adds @var{path} to the list of paths where
2762 @command{ld} looks for archive libraries. Using
2763 @code{SEARCH_DIR(@var{path})} is exactly like using @samp{-L @var{path}}
2764 on the command line (@pxref{Options,,Command Line Options}). If both
2765 are used, then the linker will search both paths. Paths specified using
2766 the command line option are searched first.
2767
2768 @item STARTUP(@var{filename})
2769 @kindex STARTUP(@var{filename})
2770 @cindex first input file
2771 The @code{STARTUP} command is just like the @code{INPUT} command, except
2772 that @var{filename} will become the first input file to be linked, as
2773 though it were specified first on the command line. This may be useful
2774 when using a system in which the entry point is always the start of the
2775 first file.
2776 @end table
2777
2778 @ifclear SingleFormat
2779 @node Format Commands
2780 @subsection Commands Dealing with Object File Formats
2781 A couple of linker script commands deal with object file formats.
2782
2783 @table @code
2784 @item OUTPUT_FORMAT(@var{bfdname})
2785 @itemx OUTPUT_FORMAT(@var{default}, @var{big}, @var{little})
2786 @kindex OUTPUT_FORMAT(@var{bfdname})
2787 @cindex output file format in linker script
2788 The @code{OUTPUT_FORMAT} command names the BFD format to use for the
2789 output file (@pxref{BFD}). Using @code{OUTPUT_FORMAT(@var{bfdname})} is
2790 exactly like using @samp{--oformat @var{bfdname}} on the command line
2791 (@pxref{Options,,Command Line Options}). If both are used, the command
2792 line option takes precedence.
2793
2794 You can use @code{OUTPUT_FORMAT} with three arguments to use different
2795 formats based on the @samp{-EB} and @samp{-EL} command line options.
2796 This permits the linker script to set the output format based on the
2797 desired endianness.
2798
2799 If neither @samp{-EB} nor @samp{-EL} are used, then the output format
2800 will be the first argument, @var{default}. If @samp{-EB} is used, the
2801 output format will be the second argument, @var{big}. If @samp{-EL} is
2802 used, the output format will be the third argument, @var{little}.
2803
2804 For example, the default linker script for the MIPS ELF target uses this
2805 command:
2806 @smallexample
2807 OUTPUT_FORMAT(elf32-bigmips, elf32-bigmips, elf32-littlemips)
2808 @end smallexample
2809 This says that the default format for the output file is
2810 @samp{elf32-bigmips}, but if the user uses the @samp{-EL} command line
2811 option, the output file will be created in the @samp{elf32-littlemips}
2812 format.
2813
2814 @item TARGET(@var{bfdname})
2815 @kindex TARGET(@var{bfdname})
2816 @cindex input file format in linker script
2817 The @code{TARGET} command names the BFD format to use when reading input
2818 files. It affects subsequent @code{INPUT} and @code{GROUP} commands.
2819 This command is like using @samp{-b @var{bfdname}} on the command line
2820 (@pxref{Options,,Command Line Options}). If the @code{TARGET} command
2821 is used but @code{OUTPUT_FORMAT} is not, then the last @code{TARGET}
2822 command is also used to set the format for the output file. @xref{BFD}.
2823 @end table
2824 @end ifclear
2825
2826 @node Miscellaneous Commands
2827 @subsection Other Linker Script Commands
2828 There are a few other linker scripts commands.
2829
2830 @table @code
2831 @item ASSERT(@var{exp}, @var{message})
2832 @kindex ASSERT
2833 @cindex assertion in linker script
2834 Ensure that @var{exp} is non-zero. If it is zero, then exit the linker
2835 with an error code, and print @var{message}.
2836
2837 @item EXTERN(@var{symbol} @var{symbol} @dots{})
2838 @kindex EXTERN
2839 @cindex undefined symbol in linker script
2840 Force @var{symbol} to be entered in the output file as an undefined
2841 symbol. Doing this may, for example, trigger linking of additional
2842 modules from standard libraries. You may list several @var{symbol}s for
2843 each @code{EXTERN}, and you may use @code{EXTERN} multiple times. This
2844 command has the same effect as the @samp{-u} command-line option.
2845
2846 @item FORCE_COMMON_ALLOCATION
2847 @kindex FORCE_COMMON_ALLOCATION
2848 @cindex common allocation in linker script
2849 This command has the same effect as the @samp{-d} command-line option:
2850 to make @command{ld} assign space to common symbols even if a relocatable
2851 output file is specified (@samp{-r}).
2852
2853 @item INHIBIT_COMMON_ALLOCATION
2854 @kindex INHIBIT_COMMON_ALLOCATION
2855 @cindex common allocation in linker script
2856 This command has the same effect as the @samp{--no-define-common}
2857 command-line option: to make @code{ld} omit the assignment of addresses
2858 to common symbols even for a non-relocatable output file.
2859
2860 @item NOCROSSREFS(@var{section} @var{section} @dots{})
2861 @kindex NOCROSSREFS(@var{sections})
2862 @cindex cross references
2863 This command may be used to tell @command{ld} to issue an error about any
2864 references among certain output sections.
2865
2866 In certain types of programs, particularly on embedded systems when
2867 using overlays, when one section is loaded into memory, another section
2868 will not be. Any direct references between the two sections would be
2869 errors. For example, it would be an error if code in one section called
2870 a function defined in the other section.
2871
2872 The @code{NOCROSSREFS} command takes a list of output section names. If
2873 @command{ld} detects any cross references between the sections, it reports
2874 an error and returns a non-zero exit status. Note that the
2875 @code{NOCROSSREFS} command uses output section names, not input section
2876 names.
2877
2878 @ifclear SingleFormat
2879 @item OUTPUT_ARCH(@var{bfdarch})
2880 @kindex OUTPUT_ARCH(@var{bfdarch})
2881 @cindex machine architecture
2882 @cindex architecture
2883 Specify a particular output machine architecture. The argument is one
2884 of the names used by the BFD library (@pxref{BFD}). You can see the
2885 architecture of an object file by using the @code{objdump} program with
2886 the @samp{-f} option.
2887 @end ifclear
2888 @end table
2889
2890 @node Assignments
2891 @section Assigning Values to Symbols
2892 @cindex assignment in scripts
2893 @cindex symbol definition, scripts
2894 @cindex variables, defining
2895 You may assign a value to a symbol in a linker script. This will define
2896 the symbol and place it into the symbol table with a global scope.
2897
2898 @menu
2899 * Simple Assignments:: Simple Assignments
2900 * PROVIDE:: PROVIDE
2901 * PROVIDE_HIDDEN:: PROVIDE_HIDDEN
2902 * Source Code Reference:: How to use a linker script defined symbol in source code
2903 @end menu
2904
2905 @node Simple Assignments
2906 @subsection Simple Assignments
2907
2908 You may assign to a symbol using any of the C assignment operators:
2909
2910 @table @code
2911 @item @var{symbol} = @var{expression} ;
2912 @itemx @var{symbol} += @var{expression} ;
2913 @itemx @var{symbol} -= @var{expression} ;
2914 @itemx @var{symbol} *= @var{expression} ;
2915 @itemx @var{symbol} /= @var{expression} ;
2916 @itemx @var{symbol} <<= @var{expression} ;
2917 @itemx @var{symbol} >>= @var{expression} ;
2918 @itemx @var{symbol} &= @var{expression} ;
2919 @itemx @var{symbol} |= @var{expression} ;
2920 @end table
2921
2922 The first case will define @var{symbol} to the value of
2923 @var{expression}. In the other cases, @var{symbol} must already be
2924 defined, and the value will be adjusted accordingly.
2925
2926 The special symbol name @samp{.} indicates the location counter. You
2927 may only use this within a @code{SECTIONS} command. @xref{Location Counter}.
2928
2929 The semicolon after @var{expression} is required.
2930
2931 Expressions are defined below; see @ref{Expressions}.
2932
2933 You may write symbol assignments as commands in their own right, or as
2934 statements within a @code{SECTIONS} command, or as part of an output
2935 section description in a @code{SECTIONS} command.
2936
2937 The section of the symbol will be set from the section of the
2938 expression; for more information, see @ref{Expression Section}.
2939
2940 Here is an example showing the three different places that symbol
2941 assignments may be used:
2942
2943 @smallexample
2944 floating_point = 0;
2945 SECTIONS
2946 @{
2947 .text :
2948 @{
2949 *(.text)
2950 _etext = .;
2951 @}
2952 _bdata = (. + 3) & ~ 3;
2953 .data : @{ *(.data) @}
2954 @}
2955 @end smallexample
2956 @noindent
2957 In this example, the symbol @samp{floating_point} will be defined as
2958 zero. The symbol @samp{_etext} will be defined as the address following
2959 the last @samp{.text} input section. The symbol @samp{_bdata} will be
2960 defined as the address following the @samp{.text} output section aligned
2961 upward to a 4 byte boundary.
2962
2963 @node PROVIDE
2964 @subsection PROVIDE
2965 @cindex PROVIDE
2966 In some cases, it is desirable for a linker script to define a symbol
2967 only if it is referenced and is not defined by any object included in
2968 the link. For example, traditional linkers defined the symbol
2969 @samp{etext}. However, ANSI C requires that the user be able to use
2970 @samp{etext} as a function name without encountering an error. The
2971 @code{PROVIDE} keyword may be used to define a symbol, such as
2972 @samp{etext}, only if it is referenced but not defined. The syntax is
2973 @code{PROVIDE(@var{symbol} = @var{expression})}.
2974
2975 Here is an example of using @code{PROVIDE} to define @samp{etext}:
2976 @smallexample
2977 SECTIONS
2978 @{
2979 .text :
2980 @{
2981 *(.text)
2982 _etext = .;
2983 PROVIDE(etext = .);
2984 @}
2985 @}
2986 @end smallexample
2987
2988 In this example, if the program defines @samp{_etext} (with a leading
2989 underscore), the linker will give a multiple definition error. If, on
2990 the other hand, the program defines @samp{etext} (with no leading
2991 underscore), the linker will silently use the definition in the program.
2992 If the program references @samp{etext} but does not define it, the
2993 linker will use the definition in the linker script.
2994
2995 @node PROVIDE_HIDDEN
2996 @subsection PROVIDE_HIDDEN
2997 @cindex PROVIDE_HIDDEN
2998 Similar to @code{PROVIDE}. For ELF targeted ports, the symbol will be
2999 hidden and won't be exported.
3000
3001 @node Source Code Reference
3002 @subsection Source Code Reference
3003
3004 Accessing a linker script defined variable from source code is not
3005 intuitive. In particular a linker script symbol is not equivalent to
3006 a variable declaration in a high level language, it is instead a
3007 symbol that does not have a value.
3008
3009 Before going further, it is important to note that compilers often
3010 transform names in the source code into different names when they are
3011 stored in the symbol table. For example, Fortran compilers commonly
3012 prepend or append an underscore, and C++ performs extensive @samp{name
3013 mangling}. Therefore there might be a discrepancy between the name
3014 of a variable as it is used in source code and the name of the same
3015 variable as it is defined in a linker script. For example in C a
3016 linker script variable might be referred to as:
3017
3018 @smallexample
3019 extern int foo;
3020 @end smallexample
3021
3022 But in the linker script it might be defined as:
3023
3024 @smallexample
3025 _foo = 1000;
3026 @end smallexample
3027
3028 In the remaining examples however it is assumed that no name
3029 transformation has taken place.
3030
3031 When a symbol is declared in a high level language such as C, two
3032 things happen. The first is that the compiler reserves enough space
3033 in the program's memory to hold the @emph{value} of the symbol. The
3034 second is that the compiler creates an entry in the program's symbol
3035 table which holds the symbol's @emph{address}. ie the symbol table
3036 contains the address of the block of memory holding the symbol's
3037 value. So for example the following C declaration, at file scope:
3038
3039 @smallexample
3040 int foo = 1000;
3041 @end smallexample
3042
3043 creates a entry called @samp{foo} in the symbol table. This entry
3044 holds the address of an @samp{int} sized block of memory where the
3045 number 1000 is initially stored.
3046
3047 When a program references a symbol the compiler generates code that
3048 first accesses the symbol table to find the address of the symbol's
3049 memory block and then code to read the value from that memory block.
3050 So:
3051
3052 @smallexample
3053 foo = 1;
3054 @end smallexample
3055
3056 looks up the symbol @samp{foo} in the symbol table, gets the address
3057 associated with this symbol and then writes the value 1 into that
3058 address. Whereas:
3059
3060 @smallexample
3061 int * a = & foo;
3062 @end smallexample
3063
3064 looks up the symbol @samp{foo} in the symbol table, gets it address
3065 and then copies this address into the block of memory associated with
3066 the variable @samp{a}.
3067
3068 Linker scripts symbol declarations, by contrast, create an entry in
3069 the symbol table but do not assign any memory to them. Thus they are
3070 an address without a value. So for example the linker script definition:
3071
3072 @smallexample
3073 foo = 1000;
3074 @end smallexample
3075
3076 creates an entry in the symbol table called @samp{foo} which holds
3077 the address of memory location 1000, but nothing special is stored at
3078 address 1000. This means that you cannot access the @emph{value} of a
3079 linker script defined symbol - it has no value - all you can do is
3080 access the @emph{address} of a linker script defined symbol.
3081
3082 Hence when you are using a linker script defined symbol in source code
3083 you should always take the address of the symbol, and never attempt to
3084 use its value. For example suppose you want to copy the contents of a
3085 section of memory called .ROM into a section called .FLASH and the
3086 linker script contains these declarations:
3087
3088 @smallexample
3089 @group
3090 start_of_ROM = .ROM;
3091 end_of_ROM = .ROM + sizeof (.ROM) - 1;
3092 start_of_FLASH = .FLASH;
3093 @end group
3094 @end smallexample
3095
3096 Then the C source code to perform the copy would be:
3097
3098 @smallexample
3099 @group
3100 extern char start_of_ROM, end_of_ROM, start_of_FLASH;
3101
3102 memcpy (& start_of_FLASH, & start_of_ROM, & end_of_ROM - & start_of_ROM);
3103 @end group
3104 @end smallexample
3105
3106 Note the use of the @samp{&} operators. These are correct.
3107
3108 @node SECTIONS
3109 @section SECTIONS Command
3110 @kindex SECTIONS
3111 The @code{SECTIONS} command tells the linker how to map input sections
3112 into output sections, and how to place the output sections in memory.
3113
3114 The format of the @code{SECTIONS} command is:
3115 @smallexample
3116 SECTIONS
3117 @{
3118 @var{sections-command}
3119 @var{sections-command}
3120 @dots{}
3121 @}
3122 @end smallexample
3123
3124 Each @var{sections-command} may of be one of the following:
3125
3126 @itemize @bullet
3127 @item
3128 an @code{ENTRY} command (@pxref{Entry Point,,Entry command})
3129 @item
3130 a symbol assignment (@pxref{Assignments})
3131 @item
3132 an output section description
3133 @item
3134 an overlay description
3135 @end itemize
3136
3137 The @code{ENTRY} command and symbol assignments are permitted inside the
3138 @code{SECTIONS} command for convenience in using the location counter in
3139 those commands. This can also make the linker script easier to
3140 understand because you can use those commands at meaningful points in
3141 the layout of the output file.
3142
3143 Output section descriptions and overlay descriptions are described
3144 below.
3145
3146 If you do not use a @code{SECTIONS} command in your linker script, the
3147 linker will place each input section into an identically named output
3148 section in the order that the sections are first encountered in the
3149 input files. If all input sections are present in the first file, for
3150 example, the order of sections in the output file will match the order
3151 in the first input file. The first section will be at address zero.
3152
3153 @menu
3154 * Output Section Description:: Output section description
3155 * Output Section Name:: Output section name
3156 * Output Section Address:: Output section address
3157 * Input Section:: Input section description
3158 * Output Section Data:: Output section data
3159 * Output Section Keywords:: Output section keywords
3160 * Output Section Discarding:: Output section discarding
3161 * Output Section Attributes:: Output section attributes
3162 * Overlay Description:: Overlay description
3163 @end menu
3164
3165 @node Output Section Description
3166 @subsection Output Section Description
3167 The full description of an output section looks like this:
3168 @smallexample
3169 @group
3170 @var{section} [@var{address}] [(@var{type})] :
3171 [AT(@var{lma})] [ALIGN(@var{section_align})] [SUBALIGN(@var{subsection_align})]
3172 @{
3173 @var{output-section-command}
3174 @var{output-section-command}
3175 @dots{}
3176 @} [>@var{region}] [AT>@var{lma_region}] [:@var{phdr} :@var{phdr} @dots{}] [=@var{fillexp}]
3177 @end group
3178 @end smallexample
3179
3180 Most output sections do not use most of the optional section attributes.
3181
3182 The whitespace around @var{section} is required, so that the section
3183 name is unambiguous. The colon and the curly braces are also required.
3184 The line breaks and other white space are optional.
3185
3186 Each @var{output-section-command} may be one of the following:
3187
3188 @itemize @bullet
3189 @item
3190 a symbol assignment (@pxref{Assignments})
3191 @item
3192 an input section description (@pxref{Input Section})
3193 @item
3194 data values to include directly (@pxref{Output Section Data})
3195 @item
3196 a special output section keyword (@pxref{Output Section Keywords})
3197 @end itemize
3198
3199 @node Output Section Name
3200 @subsection Output Section Name
3201 @cindex name, section
3202 @cindex section name
3203 The name of the output section is @var{section}. @var{section} must
3204 meet the constraints of your output format. In formats which only
3205 support a limited number of sections, such as @code{a.out}, the name
3206 must be one of the names supported by the format (@code{a.out}, for
3207 example, allows only @samp{.text}, @samp{.data} or @samp{.bss}). If the
3208 output format supports any number of sections, but with numbers and not
3209 names (as is the case for Oasys), the name should be supplied as a
3210 quoted numeric string. A section name may consist of any sequence of
3211 characters, but a name which contains any unusual characters such as
3212 commas must be quoted.
3213
3214 The output section name @samp{/DISCARD/} is special; @ref{Output Section
3215 Discarding}.
3216
3217 @node Output Section Address
3218 @subsection Output Section Address
3219 @cindex address, section
3220 @cindex section address
3221 The @var{address} is an expression for the VMA (the virtual memory
3222 address) of the output section. If you do not provide @var{address},
3223 the linker will set it based on @var{region} if present, or otherwise
3224 based on the current value of the location counter.
3225
3226 If you provide @var{address}, the address of the output section will be
3227 set to precisely that. If you provide neither @var{address} nor
3228 @var{region}, then the address of the output section will be set to the
3229 current value of the location counter aligned to the alignment
3230 requirements of the output section. The alignment requirement of the
3231 output section is the strictest alignment of any input section contained
3232 within the output section.
3233
3234 For example,
3235 @smallexample
3236 .text . : @{ *(.text) @}
3237 @end smallexample
3238 @noindent
3239 and
3240 @smallexample
3241 .text : @{ *(.text) @}
3242 @end smallexample
3243 @noindent
3244 are subtly different. The first will set the address of the
3245 @samp{.text} output section to the current value of the location
3246 counter. The second will set it to the current value of the location
3247 counter aligned to the strictest alignment of a @samp{.text} input
3248 section.
3249
3250 The @var{address} may be an arbitrary expression; @ref{Expressions}.
3251 For example, if you want to align the section on a 0x10 byte boundary,
3252 so that the lowest four bits of the section address are zero, you could
3253 do something like this:
3254 @smallexample
3255 .text ALIGN(0x10) : @{ *(.text) @}
3256 @end smallexample
3257 @noindent
3258 This works because @code{ALIGN} returns the current location counter
3259 aligned upward to the specified value.
3260
3261 Specifying @var{address} for a section will change the value of the
3262 location counter.
3263
3264 @node Input Section
3265 @subsection Input Section Description
3266 @cindex input sections
3267 @cindex mapping input sections to output sections
3268 The most common output section command is an input section description.
3269
3270 The input section description is the most basic linker script operation.
3271 You use output sections to tell the linker how to lay out your program
3272 in memory. You use input section descriptions to tell the linker how to
3273 map the input files into your memory layout.
3274
3275 @menu
3276 * Input Section Basics:: Input section basics
3277 * Input Section Wildcards:: Input section wildcard patterns
3278 * Input Section Common:: Input section for common symbols
3279 * Input Section Keep:: Input section and garbage collection
3280 * Input Section Example:: Input section example
3281 @end menu
3282
3283 @node Input Section Basics
3284 @subsubsection Input Section Basics
3285 @cindex input section basics
3286 An input section description consists of a file name optionally followed
3287 by a list of section names in parentheses.
3288
3289 The file name and the section name may be wildcard patterns, which we
3290 describe further below (@pxref{Input Section Wildcards}).
3291
3292 The most common input section description is to include all input
3293 sections with a particular name in the output section. For example, to
3294 include all input @samp{.text} sections, you would write:
3295 @smallexample
3296 *(.text)
3297 @end smallexample
3298 @noindent
3299 Here the @samp{*} is a wildcard which matches any file name. To exclude a list
3300 of files from matching the file name wildcard, EXCLUDE_FILE may be used to
3301 match all files except the ones specified in the EXCLUDE_FILE list. For
3302 example:
3303 @smallexample
3304 (*(EXCLUDE_FILE (*crtend.o *otherfile.o) .ctors))
3305 @end smallexample
3306 will cause all .ctors sections from all files except @file{crtend.o} and
3307 @file{otherfile.o} to be included.
3308
3309 There are two ways to include more than one section:
3310 @smallexample
3311 *(.text .rdata)
3312 *(.text) *(.rdata)
3313 @end smallexample
3314 @noindent
3315 The difference between these is the order in which the @samp{.text} and
3316 @samp{.rdata} input sections will appear in the output section. In the
3317 first example, they will be intermingled, appearing in the same order as
3318 they are found in the linker input. In the second example, all
3319 @samp{.text} input sections will appear first, followed by all
3320 @samp{.rdata} input sections.
3321
3322 You can specify a file name to include sections from a particular file.
3323 You would do this if one or more of your files contain special data that
3324 needs to be at a particular location in memory. For example:
3325 @smallexample
3326 data.o(.data)
3327 @end smallexample
3328
3329 If you use a file name without a list of sections, then all sections in
3330 the input file will be included in the output section. This is not
3331 commonly done, but it may by useful on occasion. For example:
3332 @smallexample
3333 data.o
3334 @end smallexample
3335
3336 When you use a file name which does not contain any wild card
3337 characters, the linker will first see if you also specified the file
3338 name on the linker command line or in an @code{INPUT} command. If you
3339 did not, the linker will attempt to open the file as an input file, as
3340 though it appeared on the command line. Note that this differs from an
3341 @code{INPUT} command, because the linker will not search for the file in
3342 the archive search path.
3343
3344 @node Input Section Wildcards
3345 @subsubsection Input Section Wildcard Patterns
3346 @cindex input section wildcards
3347 @cindex wildcard file name patterns
3348 @cindex file name wildcard patterns
3349 @cindex section name wildcard patterns
3350 In an input section description, either the file name or the section
3351 name or both may be wildcard patterns.
3352
3353 The file name of @samp{*} seen in many examples is a simple wildcard
3354 pattern for the file name.
3355
3356 The wildcard patterns are like those used by the Unix shell.
3357
3358 @table @samp
3359 @item *
3360 matches any number of characters
3361 @item ?
3362 matches any single character
3363 @item [@var{chars}]
3364 matches a single instance of any of the @var{chars}; the @samp{-}
3365 character may be used to specify a range of characters, as in
3366 @samp{[a-z]} to match any lower case letter
3367 @item \
3368 quotes the following character
3369 @end table
3370
3371 When a file name is matched with a wildcard, the wildcard characters
3372 will not match a @samp{/} character (used to separate directory names on
3373 Unix). A pattern consisting of a single @samp{*} character is an
3374 exception; it will always match any file name, whether it contains a
3375 @samp{/} or not. In a section name, the wildcard characters will match
3376 a @samp{/} character.
3377
3378 File name wildcard patterns only match files which are explicitly
3379 specified on the command line or in an @code{INPUT} command. The linker
3380 does not search directories to expand wildcards.
3381
3382 If a file name matches more than one wildcard pattern, or if a file name
3383 appears explicitly and is also matched by a wildcard pattern, the linker
3384 will use the first match in the linker script. For example, this
3385 sequence of input section descriptions is probably in error, because the
3386 @file{data.o} rule will not be used:
3387 @smallexample
3388 .data : @{ *(.data) @}
3389 .data1 : @{ data.o(.data) @}
3390 @end smallexample
3391
3392 @cindex SORT_BY_NAME
3393 Normally, the linker will place files and sections matched by wildcards
3394 in the order in which they are seen during the link. You can change
3395 this by using the @code{SORT_BY_NAME} keyword, which appears before a wildcard
3396 pattern in parentheses (e.g., @code{SORT_BY_NAME(.text*)}). When the
3397 @code{SORT_BY_NAME} keyword is used, the linker will sort the files or sections
3398 into ascending order by name before placing them in the output file.
3399
3400 @cindex SORT_BY_ALIGNMENT
3401 @code{SORT_BY_ALIGNMENT} is very similar to @code{SORT_BY_NAME}. The
3402 difference is @code{SORT_BY_ALIGNMENT} will sort sections into
3403 ascending order by alignment before placing them in the output file.
3404
3405 @cindex SORT
3406 @code{SORT} is an alias for @code{SORT_BY_NAME}.
3407
3408 When there are nested section sorting commands in linker script, there
3409 can be at most 1 level of nesting for section sorting commands.
3410
3411 @enumerate
3412 @item
3413 @code{SORT_BY_NAME} (@code{SORT_BY_ALIGNMENT} (wildcard section pattern)).
3414 It will sort the input sections by name first, then by alignment if 2
3415 sections have the same name.
3416 @item
3417 @code{SORT_BY_ALIGNMENT} (@code{SORT_BY_NAME} (wildcard section pattern)).
3418 It will sort the input sections by alignment first, then by name if 2
3419 sections have the same alignment.
3420 @item
3421 @code{SORT_BY_NAME} (@code{SORT_BY_NAME} (wildcard section pattern)) is
3422 treated the same as @code{SORT_BY_NAME} (wildcard section pattern).
3423 @item
3424 @code{SORT_BY_ALIGNMENT} (@code{SORT_BY_ALIGNMENT} (wildcard section pattern))
3425 is treated the same as @code{SORT_BY_ALIGNMENT} (wildcard section pattern).
3426 @item
3427 All other nested section sorting commands are invalid.
3428 @end enumerate
3429
3430 When both command line section sorting option and linker script
3431 section sorting command are used, section sorting command always
3432 takes precedence over the command line option.
3433
3434 If the section sorting command in linker script isn't nested, the
3435 command line option will make the section sorting command to be
3436 treated as nested sorting command.
3437
3438 @enumerate
3439 @item
3440 @code{SORT_BY_NAME} (wildcard section pattern ) with
3441 @option{--sort-sections alignment} is equivalent to
3442 @code{SORT_BY_NAME} (@code{SORT_BY_ALIGNMENT} (wildcard section pattern)).
3443 @item
3444 @code{SORT_BY_ALIGNMENT} (wildcard section pattern) with
3445 @option{--sort-section name} is equivalent to
3446 @code{SORT_BY_ALIGNMENT} (@code{SORT_BY_NAME} (wildcard section pattern)).
3447 @end enumerate
3448
3449 If the section sorting command in linker script is nested, the
3450 command line option will be ignored.
3451
3452 If you ever get confused about where input sections are going, use the
3453 @samp{-M} linker option to generate a map file. The map file shows
3454 precisely how input sections are mapped to output sections.
3455
3456 This example shows how wildcard patterns might be used to partition
3457 files. This linker script directs the linker to place all @samp{.text}
3458 sections in @samp{.text} and all @samp{.bss} sections in @samp{.bss}.
3459 The linker will place the @samp{.data} section from all files beginning
3460 with an upper case character in @samp{.DATA}; for all other files, the
3461 linker will place the @samp{.data} section in @samp{.data}.
3462 @smallexample
3463 @group
3464 SECTIONS @{
3465 .text : @{ *(.text) @}
3466 .DATA : @{ [A-Z]*(.data) @}
3467 .data : @{ *(.data) @}
3468 .bss : @{ *(.bss) @}
3469 @}
3470 @end group
3471 @end smallexample
3472
3473 @node Input Section Common
3474 @subsubsection Input Section for Common Symbols
3475 @cindex common symbol placement
3476 @cindex uninitialized data placement
3477 A special notation is needed for common symbols, because in many object
3478 file formats common symbols do not have a particular input section. The
3479 linker treats common symbols as though they are in an input section
3480 named @samp{COMMON}.
3481
3482 You may use file names with the @samp{COMMON} section just as with any
3483 other input sections. You can use this to place common symbols from a
3484 particular input file in one section while common symbols from other
3485 input files are placed in another section.
3486
3487 In most cases, common symbols in input files will be placed in the
3488 @samp{.bss} section in the output file. For example:
3489 @smallexample
3490 .bss @{ *(.bss) *(COMMON) @}
3491 @end smallexample
3492
3493 @cindex scommon section
3494 @cindex small common symbols
3495 Some object file formats have more than one type of common symbol. For
3496 example, the MIPS ELF object file format distinguishes standard common
3497 symbols and small common symbols. In this case, the linker will use a
3498 different special section name for other types of common symbols. In
3499 the case of MIPS ELF, the linker uses @samp{COMMON} for standard common
3500 symbols and @samp{.scommon} for small common symbols. This permits you
3501 to map the different types of common symbols into memory at different
3502 locations.
3503
3504 @cindex [COMMON]
3505 You will sometimes see @samp{[COMMON]} in old linker scripts. This
3506 notation is now considered obsolete. It is equivalent to
3507 @samp{*(COMMON)}.
3508
3509 @node Input Section Keep
3510 @subsubsection Input Section and Garbage Collection
3511 @cindex KEEP
3512 @cindex garbage collection
3513 When link-time garbage collection is in use (@samp{--gc-sections}),
3514 it is often useful to mark sections that should not be eliminated.
3515 This is accomplished by surrounding an input section's wildcard entry
3516 with @code{KEEP()}, as in @code{KEEP(*(.init))} or
3517 @code{KEEP(SORT_BY_NAME(*)(.ctors))}.
3518
3519 @node Input Section Example
3520 @subsubsection Input Section Example
3521 The following example is a complete linker script. It tells the linker
3522 to read all of the sections from file @file{all.o} and place them at the
3523 start of output section @samp{outputa} which starts at location
3524 @samp{0x10000}. All of section @samp{.input1} from file @file{foo.o}
3525 follows immediately, in the same output section. All of section
3526 @samp{.input2} from @file{foo.o} goes into output section
3527 @samp{outputb}, followed by section @samp{.input1} from @file{foo1.o}.
3528 All of the remaining @samp{.input1} and @samp{.input2} sections from any
3529 files are written to output section @samp{outputc}.
3530
3531 @smallexample
3532 @group
3533 SECTIONS @{
3534 outputa 0x10000 :
3535 @{
3536 all.o
3537 foo.o (.input1)
3538 @}
3539 @end group
3540 @group
3541 outputb :
3542 @{
3543 foo.o (.input2)
3544 foo1.o (.input1)
3545 @}
3546 @end group
3547 @group
3548 outputc :
3549 @{
3550 *(.input1)
3551 *(.input2)
3552 @}
3553 @}
3554 @end group
3555 @end smallexample
3556
3557 @node Output Section Data
3558 @subsection Output Section Data
3559 @cindex data
3560 @cindex section data
3561 @cindex output section data
3562 @kindex BYTE(@var{expression})
3563 @kindex SHORT(@var{expression})
3564 @kindex LONG(@var{expression})
3565 @kindex QUAD(@var{expression})
3566 @kindex SQUAD(@var{expression})
3567 You can include explicit bytes of data in an output section by using
3568 @code{BYTE}, @code{SHORT}, @code{LONG}, @code{QUAD}, or @code{SQUAD} as
3569 an output section command. Each keyword is followed by an expression in
3570 parentheses providing the value to store (@pxref{Expressions}). The
3571 value of the expression is stored at the current value of the location
3572 counter.
3573
3574 The @code{BYTE}, @code{SHORT}, @code{LONG}, and @code{QUAD} commands
3575 store one, two, four, and eight bytes (respectively). After storing the
3576 bytes, the location counter is incremented by the number of bytes
3577 stored.
3578
3579 For example, this will store the byte 1 followed by the four byte value
3580 of the symbol @samp{addr}:
3581 @smallexample
3582 BYTE(1)
3583 LONG(addr)
3584 @end smallexample
3585
3586 When using a 64 bit host or target, @code{QUAD} and @code{SQUAD} are the
3587 same; they both store an 8 byte, or 64 bit, value. When both host and
3588 target are 32 bits, an expression is computed as 32 bits. In this case
3589 @code{QUAD} stores a 32 bit value zero extended to 64 bits, and
3590 @code{SQUAD} stores a 32 bit value sign extended to 64 bits.
3591
3592 If the object file format of the output file has an explicit endianness,
3593 which is the normal case, the value will be stored in that endianness.
3594 When the object file format does not have an explicit endianness, as is
3595 true of, for example, S-records, the value will be stored in the
3596 endianness of the first input object file.
3597
3598 Note---these commands only work inside a section description and not
3599 between them, so the following will produce an error from the linker:
3600 @smallexample
3601 SECTIONS @{@ .text : @{@ *(.text) @}@ LONG(1) .data : @{@ *(.data) @}@ @}@
3602 @end smallexample
3603 whereas this will work:
3604 @smallexample
3605 SECTIONS @{@ .text : @{@ *(.text) ; LONG(1) @}@ .data : @{@ *(.data) @}@ @}@
3606 @end smallexample
3607
3608 @kindex FILL(@var{expression})
3609 @cindex holes, filling
3610 @cindex unspecified memory
3611 You may use the @code{FILL} command to set the fill pattern for the
3612 current section. It is followed by an expression in parentheses. Any
3613 otherwise unspecified regions of memory within the section (for example,
3614 gaps left due to the required alignment of input sections) are filled
3615 with the value of the expression, repeated as
3616 necessary. A @code{FILL} statement covers memory locations after the
3617 point at which it occurs in the section definition; by including more
3618 than one @code{FILL} statement, you can have different fill patterns in
3619 different parts of an output section.
3620
3621 This example shows how to fill unspecified regions of memory with the
3622 value @samp{0x90}:
3623 @smallexample
3624 FILL(0x90909090)
3625 @end smallexample
3626
3627 The @code{FILL} command is similar to the @samp{=@var{fillexp}} output
3628 section attribute, but it only affects the
3629 part of the section following the @code{FILL} command, rather than the
3630 entire section. If both are used, the @code{FILL} command takes
3631 precedence. @xref{Output Section Fill}, for details on the fill
3632 expression.
3633
3634 @node Output Section Keywords
3635 @subsection Output Section Keywords
3636 There are a couple of keywords which can appear as output section
3637 commands.
3638
3639 @table @code
3640 @kindex CREATE_OBJECT_SYMBOLS
3641 @cindex input filename symbols
3642 @cindex filename symbols
3643 @item CREATE_OBJECT_SYMBOLS
3644 The command tells the linker to create a symbol for each input file.
3645 The name of each symbol will be the name of the corresponding input
3646 file. The section of each symbol will be the output section in which
3647 the @code{CREATE_OBJECT_SYMBOLS} command appears.
3648
3649 This is conventional for the a.out object file format. It is not
3650 normally used for any other object file format.
3651
3652 @kindex CONSTRUCTORS
3653 @cindex C++ constructors, arranging in link
3654 @cindex constructors, arranging in link
3655 @item CONSTRUCTORS
3656 When linking using the a.out object file format, the linker uses an
3657 unusual set construct to support C++ global constructors and
3658 destructors. When linking object file formats which do not support
3659 arbitrary sections, such as ECOFF and XCOFF, the linker will
3660 automatically recognize C++ global constructors and destructors by name.
3661 For these object file formats, the @code{CONSTRUCTORS} command tells the
3662 linker to place constructor information in the output section where the
3663 @code{CONSTRUCTORS} command appears. The @code{CONSTRUCTORS} command is
3664 ignored for other object file formats.
3665
3666 The symbol @w{@code{__CTOR_LIST__}} marks the start of the global
3667 constructors, and the symbol @w{@code{__CTOR_END__}} marks the end.
3668 Similarly, @w{@code{__DTOR_LIST__}} and @w{@code{__DTOR_END__}} mark
3669 the start and end of the global destructors. The
3670 first word in the list is the number of entries, followed by the address
3671 of each constructor or destructor, followed by a zero word. The
3672 compiler must arrange to actually run the code. For these object file
3673 formats @sc{gnu} C++ normally calls constructors from a subroutine
3674 @code{__main}; a call to @code{__main} is automatically inserted into
3675 the startup code for @code{main}. @sc{gnu} C++ normally runs
3676 destructors either by using @code{atexit}, or directly from the function
3677 @code{exit}.
3678
3679 For object file formats such as @code{COFF} or @code{ELF} which support
3680 arbitrary section names, @sc{gnu} C++ will normally arrange to put the
3681 addresses of global constructors and destructors into the @code{.ctors}
3682 and @code{.dtors} sections. Placing the following sequence into your
3683 linker script will build the sort of table which the @sc{gnu} C++
3684 runtime code expects to see.
3685
3686 @smallexample
3687 __CTOR_LIST__ = .;
3688 LONG((__CTOR_END__ - __CTOR_LIST__) / 4 - 2)
3689 *(.ctors)
3690 LONG(0)
3691 __CTOR_END__ = .;
3692 __DTOR_LIST__ = .;
3693 LONG((__DTOR_END__ - __DTOR_LIST__) / 4 - 2)
3694 *(.dtors)
3695 LONG(0)
3696 __DTOR_END__ = .;
3697 @end smallexample
3698
3699 If you are using the @sc{gnu} C++ support for initialization priority,
3700 which provides some control over the order in which global constructors
3701 are run, you must sort the constructors at link time to ensure that they
3702 are executed in the correct order. When using the @code{CONSTRUCTORS}
3703 command, use @samp{SORT_BY_NAME(CONSTRUCTORS)} instead. When using the
3704 @code{.ctors} and @code{.dtors} sections, use @samp{*(SORT_BY_NAME(.ctors))} and
3705 @samp{*(SORT_BY_NAME(.dtors))} instead of just @samp{*(.ctors)} and
3706 @samp{*(.dtors)}.
3707
3708 Normally the compiler and linker will handle these issues automatically,
3709 and you will not need to concern yourself with them. However, you may
3710 need to consider this if you are using C++ and writing your own linker
3711 scripts.
3712
3713 @end table
3714
3715 @node Output Section Discarding
3716 @subsection Output Section Discarding
3717 @cindex discarding sections
3718 @cindex sections, discarding
3719 @cindex removing sections
3720 The linker will not create output sections with no contents. This is
3721 for convenience when referring to input sections that may or may not
3722 be present in any of the input files. For example:
3723 @smallexample
3724 .foo : @{ *(.foo) @}
3725 @end smallexample
3726 @noindent
3727 will only create a @samp{.foo} section in the output file if there is a
3728 @samp{.foo} section in at least one input file, and if the input
3729 sections are not all empty. Other link script directives that allocate
3730 space in an output section will also create the output section.
3731
3732 The linker will ignore address assignments (@pxref{Output Section Address})
3733 on discarded output sections, except when the linker script defines
3734 symbols in the output section. In that case the linker will obey
3735 the address assignments, possibly advancing dot even though the
3736 section is discarded.
3737
3738 @cindex /DISCARD/
3739 The special output section name @samp{/DISCARD/} may be used to discard
3740 input sections. Any input sections which are assigned to an output
3741 section named @samp{/DISCARD/} are not included in the output file.
3742
3743 @node Output Section Attributes
3744 @subsection Output Section Attributes
3745 @cindex output section attributes
3746 We showed above that the full description of an output section looked
3747 like this:
3748 @smallexample
3749 @group
3750 @var{section} [@var{address}] [(@var{type})] :
3751 [AT(@var{lma})] [ALIGN(@var{section_align})] [SUBALIGN(@var{subsection_align})]
3752 @{
3753 @var{output-section-command}
3754 @var{output-section-command}
3755 @dots{}
3756 @} [>@var{region}] [AT>@var{lma_region}] [:@var{phdr} :@var{phdr} @dots{}] [=@var{fillexp}]
3757 @end group
3758 @end smallexample
3759 We've already described @var{section}, @var{address}, and
3760 @var{output-section-command}. In this section we will describe the
3761 remaining section attributes.
3762
3763 @menu
3764 * Output Section Type:: Output section type
3765 * Output Section LMA:: Output section LMA
3766 * Forced Output Alignment:: Forced Output Alignment
3767 * Forced Input Alignment:: Forced Input Alignment
3768 * Output Section Region:: Output section region
3769 * Output Section Phdr:: Output section phdr
3770 * Output Section Fill:: Output section fill
3771 @end menu
3772
3773 @node Output Section Type
3774 @subsubsection Output Section Type
3775 Each output section may have a type. The type is a keyword in
3776 parentheses. The following types are defined:
3777
3778 @table @code
3779 @item NOLOAD
3780 The section should be marked as not loadable, so that it will not be
3781 loaded into memory when the program is run.
3782 @item DSECT
3783 @itemx COPY
3784 @itemx INFO
3785 @itemx OVERLAY
3786 These type names are supported for backward compatibility, and are
3787 rarely used. They all have the same effect: the section should be
3788 marked as not allocatable, so that no memory is allocated for the
3789 section when the program is run.
3790 @end table
3791
3792 @kindex NOLOAD
3793 @cindex prevent unnecessary loading
3794 @cindex loading, preventing
3795 The linker normally sets the attributes of an output section based on
3796 the input sections which map into it. You can override this by using
3797 the section type. For example, in the script sample below, the
3798 @samp{ROM} section is addressed at memory location @samp{0} and does not
3799 need to be loaded when the program is run. The contents of the
3800 @samp{ROM} section will appear in the linker output file as usual.
3801 @smallexample
3802 @group
3803 SECTIONS @{
3804 ROM 0 (NOLOAD) : @{ @dots{} @}
3805 @dots{}
3806 @}
3807 @end group
3808 @end smallexample
3809
3810 @node Output Section LMA
3811 @subsubsection Output Section LMA
3812 @kindex AT>@var{lma_region}
3813 @kindex AT(@var{lma})
3814 @cindex load address
3815 @cindex section load address
3816 Every section has a virtual address (VMA) and a load address (LMA); see
3817 @ref{Basic Script Concepts}. The address expression which may appear in
3818 an output section description sets the VMA (@pxref{Output Section
3819 Address}).
3820
3821 The expression @var{lma} that follows the @code{AT} keyword specifies
3822 the load address of the section.
3823
3824 Alternatively, with @samp{AT>@var{lma_region}} expression, you may
3825 specify a memory region for the section's load address. @xref{MEMORY}.
3826 Note that if the section has not had a VMA assigned to it then the
3827 linker will use the @var{lma_region} as the VMA region as well.
3828
3829 If neither @code{AT} nor @code{AT>} is specified for an allocatable
3830 section, the linker will set the LMA such that the difference between
3831 VMA and LMA for the section is the same as the preceding output
3832 section in the same region. If there is no preceding output section
3833 or the section is not allocatable, the linker will set the LMA equal
3834 to the VMA.
3835 @xref{Output Section Region}.
3836
3837 @cindex ROM initialized data
3838 @cindex initialized data in ROM
3839 This feature is designed to make it easy to build a ROM image. For
3840 example, the following linker script creates three output sections: one
3841 called @samp{.text}, which starts at @code{0x1000}, one called
3842 @samp{.mdata}, which is loaded at the end of the @samp{.text} section
3843 even though its VMA is @code{0x2000}, and one called @samp{.bss} to hold
3844 uninitialized data at address @code{0x3000}. The symbol @code{_data} is
3845 defined with the value @code{0x2000}, which shows that the location
3846 counter holds the VMA value, not the LMA value.
3847
3848 @smallexample
3849 @group
3850 SECTIONS
3851 @{
3852 .text 0x1000 : @{ *(.text) _etext = . ; @}
3853 .mdata 0x2000 :
3854 AT ( ADDR (.text) + SIZEOF (.text) )
3855 @{ _data = . ; *(.data); _edata = . ; @}
3856 .bss 0x3000 :
3857 @{ _bstart = . ; *(.bss) *(COMMON) ; _bend = . ;@}
3858 @}
3859 @end group
3860 @end smallexample
3861
3862 The run-time initialization code for use with a program generated with
3863 this linker script would include something like the following, to copy
3864 the initialized data from the ROM image to its runtime address. Notice
3865 how this code takes advantage of the symbols defined by the linker
3866 script.
3867
3868 @smallexample
3869 @group
3870 extern char _etext, _data, _edata, _bstart, _bend;
3871 char *src = &_etext;
3872 char *dst = &_data;
3873
3874 /* ROM has data at end of text; copy it. */
3875 while (dst < &_edata) @{
3876 *dst++ = *src++;
3877 @}
3878
3879 /* Zero bss */
3880 for (dst = &_bstart; dst< &_bend; dst++)
3881 *dst = 0;
3882 @end group
3883 @end smallexample
3884
3885 @node Forced Output Alignment
3886 @subsubsection Forced Output Alignment
3887 @kindex ALIGN(@var{section_align})
3888 @cindex forcing output section alignment
3889 @cindex output section alignment
3890 You can increase an output section's alignment by using ALIGN.
3891
3892 @node Forced Input Alignment
3893 @subsubsection Forced Input Alignment
3894 @kindex SUBALIGN(@var{subsection_align})
3895 @cindex forcing input section alignment
3896 @cindex input section alignment
3897 You can force input section alignment within an output section by using
3898 SUBALIGN. The value specified overrides any alignment given by input
3899 sections, whether larger or smaller.
3900
3901 @node Output Section Region
3902 @subsubsection Output Section Region
3903 @kindex >@var{region}
3904 @cindex section, assigning to memory region
3905 @cindex memory regions and sections
3906 You can assign a section to a previously defined region of memory by
3907 using @samp{>@var{region}}. @xref{MEMORY}.
3908
3909 Here is a simple example:
3910 @smallexample
3911 @group
3912 MEMORY @{ rom : ORIGIN = 0x1000, LENGTH = 0x1000 @}
3913 SECTIONS @{ ROM : @{ *(.text) @} >rom @}
3914 @end group
3915 @end smallexample
3916
3917 @node Output Section Phdr
3918 @subsubsection Output Section Phdr
3919 @kindex :@var{phdr}
3920 @cindex section, assigning to program header
3921 @cindex program headers and sections
3922 You can assign a section to a previously defined program segment by
3923 using @samp{:@var{phdr}}. @xref{PHDRS}. If a section is assigned to
3924 one or more segments, then all subsequent allocated sections will be
3925 assigned to those segments as well, unless they use an explicitly
3926 @code{:@var{phdr}} modifier. You can use @code{:NONE} to tell the
3927 linker to not put the section in any segment at all.
3928
3929 Here is a simple example:
3930 @smallexample
3931 @group
3932 PHDRS @{ text PT_LOAD ; @}
3933 SECTIONS @{ .text : @{ *(.text) @} :text @}
3934 @end group
3935 @end smallexample
3936
3937 @node Output Section Fill
3938 @subsubsection Output Section Fill
3939 @kindex =@var{fillexp}
3940 @cindex section fill pattern
3941 @cindex fill pattern, entire section
3942 You can set the fill pattern for an entire section by using
3943 @samp{=@var{fillexp}}. @var{fillexp} is an expression
3944 (@pxref{Expressions}). Any otherwise unspecified regions of memory
3945 within the output section (for example, gaps left due to the required
3946 alignment of input sections) will be filled with the value, repeated as
3947 necessary. If the fill expression is a simple hex number, ie. a string
3948 of hex digit starting with @samp{0x} and without a trailing @samp{k} or @samp{M}, then
3949 an arbitrarily long sequence of hex digits can be used to specify the
3950 fill pattern; Leading zeros become part of the pattern too. For all
3951 other cases, including extra parentheses or a unary @code{+}, the fill
3952 pattern is the four least significant bytes of the value of the
3953 expression. In all cases, the number is big-endian.
3954
3955 You can also change the fill value with a @code{FILL} command in the
3956 output section commands; (@pxref{Output Section Data}).
3957
3958 Here is a simple example:
3959 @smallexample
3960 @group
3961 SECTIONS @{ .text : @{ *(.text) @} =0x90909090 @}
3962 @end group
3963 @end smallexample
3964
3965 @node Overlay Description
3966 @subsection Overlay Description
3967 @kindex OVERLAY
3968 @cindex overlays
3969 An overlay description provides an easy way to describe sections which
3970 are to be loaded as part of a single memory image but are to be run at
3971 the same memory address. At run time, some sort of overlay manager will
3972 copy the overlaid sections in and out of the runtime memory address as
3973 required, perhaps by simply manipulating addressing bits. This approach
3974 can be useful, for example, when a certain region of memory is faster
3975 than another.
3976
3977 Overlays are described using the @code{OVERLAY} command. The
3978 @code{OVERLAY} command is used within a @code{SECTIONS} command, like an
3979 output section description. The full syntax of the @code{OVERLAY}
3980 command is as follows:
3981 @smallexample
3982 @group
3983 OVERLAY [@var{start}] : [NOCROSSREFS] [AT ( @var{ldaddr} )]
3984 @{
3985 @var{secname1}
3986 @{
3987 @var{output-section-command}
3988 @var{output-section-command}
3989 @dots{}
3990 @} [:@var{phdr}@dots{}] [=@var{fill}]
3991 @var{secname2}
3992 @{
3993 @var{output-section-command}
3994 @var{output-section-command}
3995 @dots{}
3996 @} [:@var{phdr}@dots{}] [=@var{fill}]
3997 @dots{}
3998 @} [>@var{region}] [:@var{phdr}@dots{}] [=@var{fill}]
3999 @end group
4000 @end smallexample
4001
4002 Everything is optional except @code{OVERLAY} (a keyword), and each
4003 section must have a name (@var{secname1} and @var{secname2} above). The
4004 section definitions within the @code{OVERLAY} construct are identical to
4005 those within the general @code{SECTIONS} contruct (@pxref{SECTIONS}),
4006 except that no addresses and no memory regions may be defined for
4007 sections within an @code{OVERLAY}.
4008
4009 The sections are all defined with the same starting address. The load
4010 addresses of the sections are arranged such that they are consecutive in
4011 memory starting at the load address used for the @code{OVERLAY} as a
4012 whole (as with normal section definitions, the load address is optional,
4013 and defaults to the start address; the start address is also optional,
4014 and defaults to the current value of the location counter).
4015
4016 If the @code{NOCROSSREFS} keyword is used, and there any references
4017 among the sections, the linker will report an error. Since the sections
4018 all run at the same address, it normally does not make sense for one
4019 section to refer directly to another. @xref{Miscellaneous Commands,
4020 NOCROSSREFS}.
4021
4022 For each section within the @code{OVERLAY}, the linker automatically
4023 defines two symbols. The symbol @code{__load_start_@var{secname}} is
4024 defined as the starting load address of the section. The symbol
4025 @code{__load_stop_@var{secname}} is defined as the final load address of
4026 the section. Any characters within @var{secname} which are not legal
4027 within C identifiers are removed. C (or assembler) code may use these
4028 symbols to move the overlaid sections around as necessary.
4029
4030 At the end of the overlay, the value of the location counter is set to
4031 the start address of the overlay plus the size of the largest section.
4032
4033 Here is an example. Remember that this would appear inside a
4034 @code{SECTIONS} construct.
4035 @smallexample
4036 @group
4037 OVERLAY 0x1000 : AT (0x4000)
4038 @{
4039 .text0 @{ o1/*.o(.text) @}
4040 .text1 @{ o2/*.o(.text) @}
4041 @}
4042 @end group
4043 @end smallexample
4044 @noindent
4045 This will define both @samp{.text0} and @samp{.text1} to start at
4046 address 0x1000. @samp{.text0} will be loaded at address 0x4000, and
4047 @samp{.text1} will be loaded immediately after @samp{.text0}. The
4048 following symbols will be defined: @code{__load_start_text0},
4049 @code{__load_stop_text0}, @code{__load_start_text1},
4050 @code{__load_stop_text1}.
4051
4052 C code to copy overlay @code{.text1} into the overlay area might look
4053 like the following.
4054
4055 @smallexample
4056 @group
4057 extern char __load_start_text1, __load_stop_text1;
4058 memcpy ((char *) 0x1000, &__load_start_text1,
4059 &__load_stop_text1 - &__load_start_text1);
4060 @end group
4061 @end smallexample
4062
4063 Note that the @code{OVERLAY} command is just syntactic sugar, since
4064 everything it does can be done using the more basic commands. The above
4065 example could have been written identically as follows.
4066
4067 @smallexample
4068 @group
4069 .text0 0x1000 : AT (0x4000) @{ o1/*.o(.text) @}
4070 __load_start_text0 = LOADADDR (.text0);
4071 __load_stop_text0 = LOADADDR (.text0) + SIZEOF (.text0);
4072 .text1 0x1000 : AT (0x4000 + SIZEOF (.text0)) @{ o2/*.o(.text) @}
4073 __load_start_text1 = LOADADDR (.text1);
4074 __load_stop_text1 = LOADADDR (.text1) + SIZEOF (.text1);
4075 . = 0x1000 + MAX (SIZEOF (.text0), SIZEOF (.text1));
4076 @end group
4077 @end smallexample
4078
4079 @node MEMORY
4080 @section MEMORY Command
4081 @kindex MEMORY
4082 @cindex memory regions
4083 @cindex regions of memory
4084 @cindex allocating memory
4085 @cindex discontinuous memory
4086 The linker's default configuration permits allocation of all available
4087 memory. You can override this by using the @code{MEMORY} command.
4088
4089 The @code{MEMORY} command describes the location and size of blocks of
4090 memory in the target. You can use it to describe which memory regions
4091 may be used by the linker, and which memory regions it must avoid. You
4092 can then assign sections to particular memory regions. The linker will
4093 set section addresses based on the memory regions, and will warn about
4094 regions that become too full. The linker will not shuffle sections
4095 around to fit into the available regions.
4096
4097 A linker script may contain at most one use of the @code{MEMORY}
4098 command. However, you can define as many blocks of memory within it as
4099 you wish. The syntax is:
4100 @smallexample
4101 @group
4102 MEMORY
4103 @{
4104 @var{name} [(@var{attr})] : ORIGIN = @var{origin}, LENGTH = @var{len}
4105 @dots{}
4106 @}
4107 @end group
4108 @end smallexample
4109
4110 The @var{name} is a name used in the linker script to refer to the
4111 region. The region name has no meaning outside of the linker script.
4112 Region names are stored in a separate name space, and will not conflict
4113 with symbol names, file names, or section names. Each memory region
4114 must have a distinct name.
4115
4116 @cindex memory region attributes
4117 The @var{attr} string is an optional list of attributes that specify
4118 whether to use a particular memory region for an input section which is
4119 not explicitly mapped in the linker script. As described in
4120 @ref{SECTIONS}, if you do not specify an output section for some input
4121 section, the linker will create an output section with the same name as
4122 the input section. If you define region attributes, the linker will use
4123 them to select the memory region for the output section that it creates.
4124
4125 The @var{attr} string must consist only of the following characters:
4126 @table @samp
4127 @item R
4128 Read-only section
4129 @item W
4130 Read/write section
4131 @item X
4132 Executable section
4133 @item A
4134 Allocatable section
4135 @item I
4136 Initialized section
4137 @item L
4138 Same as @samp{I}
4139 @item !
4140 Invert the sense of any of the preceding attributes
4141 @end table
4142
4143 If a unmapped section matches any of the listed attributes other than
4144 @samp{!}, it will be placed in the memory region. The @samp{!}
4145 attribute reverses this test, so that an unmapped section will be placed
4146 in the memory region only if it does not match any of the listed
4147 attributes.
4148
4149 @kindex ORIGIN =
4150 @kindex o =
4151 @kindex org =
4152 The @var{origin} is an numerical expression for the start address of
4153 the memory region. The expression must evaluate to a constant and it
4154 cannot involve any symbols. The keyword @code{ORIGIN} may be
4155 abbreviated to @code{org} or @code{o} (but not, for example,
4156 @code{ORG}).
4157
4158 @kindex LENGTH =
4159 @kindex len =
4160 @kindex l =
4161 The @var{len} is an expression for the size in bytes of the memory
4162 region. As with the @var{origin} expression, the expression must
4163 be numerical only and must evaluate to a constant. The keyword
4164 @code{LENGTH} may be abbreviated to @code{len} or @code{l}.
4165
4166 In the following example, we specify that there are two memory regions
4167 available for allocation: one starting at @samp{0} for 256 kilobytes,
4168 and the other starting at @samp{0x40000000} for four megabytes. The
4169 linker will place into the @samp{rom} memory region every section which
4170 is not explicitly mapped into a memory region, and is either read-only
4171 or executable. The linker will place other sections which are not
4172 explicitly mapped into a memory region into the @samp{ram} memory
4173 region.
4174
4175 @smallexample
4176 @group
4177 MEMORY
4178 @{
4179 rom (rx) : ORIGIN = 0, LENGTH = 256K
4180 ram (!rx) : org = 0x40000000, l = 4M
4181 @}
4182 @end group
4183 @end smallexample
4184
4185 Once you define a memory region, you can direct the linker to place
4186 specific output sections into that memory region by using the
4187 @samp{>@var{region}} output section attribute. For example, if you have
4188 a memory region named @samp{mem}, you would use @samp{>mem} in the
4189 output section definition. @xref{Output Section Region}. If no address
4190 was specified for the output section, the linker will set the address to
4191 the next available address within the memory region. If the combined
4192 output sections directed to a memory region are too large for the
4193 region, the linker will issue an error message.
4194
4195 It is possible to access the origin and length of a memory in an
4196 expression via the @code{ORIGIN(@var{memory})} and
4197 @code{LENGTH(@var{memory})} functions:
4198
4199 @smallexample
4200 @group
4201 _fstack = ORIGIN(ram) + LENGTH(ram) - 4;
4202 @end group
4203 @end smallexample
4204
4205 @node PHDRS
4206 @section PHDRS Command
4207 @kindex PHDRS
4208 @cindex program headers
4209 @cindex ELF program headers
4210 @cindex program segments
4211 @cindex segments, ELF
4212 The ELF object file format uses @dfn{program headers}, also knows as
4213 @dfn{segments}. The program headers describe how the program should be
4214 loaded into memory. You can print them out by using the @code{objdump}
4215 program with the @samp{-p} option.
4216
4217 When you run an ELF program on a native ELF system, the system loader
4218 reads the program headers in order to figure out how to load the
4219 program. This will only work if the program headers are set correctly.
4220 This manual does not describe the details of how the system loader
4221 interprets program headers; for more information, see the ELF ABI.
4222
4223 The linker will create reasonable program headers by default. However,
4224 in some cases, you may need to specify the program headers more
4225 precisely. You may use the @code{PHDRS} command for this purpose. When
4226 the linker sees the @code{PHDRS} command in the linker script, it will
4227 not create any program headers other than the ones specified.
4228
4229 The linker only pays attention to the @code{PHDRS} command when
4230 generating an ELF output file. In other cases, the linker will simply
4231 ignore @code{PHDRS}.
4232
4233 This is the syntax of the @code{PHDRS} command. The words @code{PHDRS},
4234 @code{FILEHDR}, @code{AT}, and @code{FLAGS} are keywords.
4235
4236 @smallexample
4237 @group
4238 PHDRS
4239 @{
4240 @var{name} @var{type} [ FILEHDR ] [ PHDRS ] [ AT ( @var{address} ) ]
4241 [ FLAGS ( @var{flags} ) ] ;
4242 @}
4243 @end group
4244 @end smallexample
4245
4246 The @var{name} is used only for reference in the @code{SECTIONS} command
4247 of the linker script. It is not put into the output file. Program
4248 header names are stored in a separate name space, and will not conflict
4249 with symbol names, file names, or section names. Each program header
4250 must have a distinct name.
4251
4252 Certain program header types describe segments of memory which the
4253 system loader will load from the file. In the linker script, you
4254 specify the contents of these segments by placing allocatable output
4255 sections in the segments. You use the @samp{:@var{phdr}} output section
4256 attribute to place a section in a particular segment. @xref{Output
4257 Section Phdr}.
4258
4259 It is normal to put certain sections in more than one segment. This
4260 merely implies that one segment of memory contains another. You may
4261 repeat @samp{:@var{phdr}}, using it once for each segment which should
4262 contain the section.
4263
4264 If you place a section in one or more segments using @samp{:@var{phdr}},
4265 then the linker will place all subsequent allocatable sections which do
4266 not specify @samp{:@var{phdr}} in the same segments. This is for
4267 convenience, since generally a whole set of contiguous sections will be
4268 placed in a single segment. You can use @code{:NONE} to override the
4269 default segment and tell the linker to not put the section in any
4270 segment at all.
4271
4272 @kindex FILEHDR
4273 @kindex PHDRS
4274 You may use the @code{FILEHDR} and @code{PHDRS} keywords appear after
4275 the program header type to further describe the contents of the segment.
4276 The @code{FILEHDR} keyword means that the segment should include the ELF
4277 file header. The @code{PHDRS} keyword means that the segment should
4278 include the ELF program headers themselves.
4279
4280 The @var{type} may be one of the following. The numbers indicate the
4281 value of the keyword.
4282
4283 @table @asis
4284 @item @code{PT_NULL} (0)
4285 Indicates an unused program header.
4286
4287 @item @code{PT_LOAD} (1)
4288 Indicates that this program header describes a segment to be loaded from
4289 the file.
4290
4291 @item @code{PT_DYNAMIC} (2)
4292 Indicates a segment where dynamic linking information can be found.
4293
4294 @item @code{PT_INTERP} (3)
4295 Indicates a segment where the name of the program interpreter may be
4296 found.
4297
4298 @item @code{PT_NOTE} (4)
4299 Indicates a segment holding note information.
4300
4301 @item @code{PT_SHLIB} (5)
4302 A reserved program header type, defined but not specified by the ELF
4303 ABI.
4304
4305 @item @code{PT_PHDR} (6)
4306 Indicates a segment where the program headers may be found.
4307
4308 @item @var{expression}
4309 An expression giving the numeric type of the program header. This may
4310 be used for types not defined above.
4311 @end table
4312
4313 You can specify that a segment should be loaded at a particular address
4314 in memory by using an @code{AT} expression. This is identical to the
4315 @code{AT} command used as an output section attribute (@pxref{Output
4316 Section LMA}). The @code{AT} command for a program header overrides the
4317 output section attribute.
4318
4319 The linker will normally set the segment flags based on the sections
4320 which comprise the segment. You may use the @code{FLAGS} keyword to
4321 explicitly specify the segment flags. The value of @var{flags} must be
4322 an integer. It is used to set the @code{p_flags} field of the program
4323 header.
4324
4325 Here is an example of @code{PHDRS}. This shows a typical set of program
4326 headers used on a native ELF system.
4327
4328 @example
4329 @group
4330 PHDRS
4331 @{
4332 headers PT_PHDR PHDRS ;
4333 interp PT_INTERP ;
4334 text PT_LOAD FILEHDR PHDRS ;
4335 data PT_LOAD ;
4336 dynamic PT_DYNAMIC ;
4337 @}
4338
4339 SECTIONS
4340 @{
4341 . = SIZEOF_HEADERS;
4342 .interp : @{ *(.interp) @} :text :interp
4343 .text : @{ *(.text) @} :text
4344 .rodata : @{ *(.rodata) @} /* defaults to :text */
4345 @dots{}
4346 . = . + 0x1000; /* move to a new page in memory */
4347 .data : @{ *(.data) @} :data
4348 .dynamic : @{ *(.dynamic) @} :data :dynamic
4349 @dots{}
4350 @}
4351 @end group
4352 @end example
4353
4354 @node VERSION
4355 @section VERSION Command
4356 @kindex VERSION @{script text@}
4357 @cindex symbol versions
4358 @cindex version script
4359 @cindex versions of symbols
4360 The linker supports symbol versions when using ELF. Symbol versions are
4361 only useful when using shared libraries. The dynamic linker can use
4362 symbol versions to select a specific version of a function when it runs
4363 a program that may have been linked against an earlier version of the
4364 shared library.
4365
4366 You can include a version script directly in the main linker script, or
4367 you can supply the version script as an implicit linker script. You can
4368 also use the @samp{--version-script} linker option.
4369
4370 The syntax of the @code{VERSION} command is simply
4371 @smallexample
4372 VERSION @{ version-script-commands @}
4373 @end smallexample
4374
4375 The format of the version script commands is identical to that used by
4376 Sun's linker in Solaris 2.5. The version script defines a tree of
4377 version nodes. You specify the node names and interdependencies in the
4378 version script. You can specify which symbols are bound to which
4379 version nodes, and you can reduce a specified set of symbols to local
4380 scope so that they are not globally visible outside of the shared
4381 library.
4382
4383 The easiest way to demonstrate the version script language is with a few
4384 examples.
4385
4386 @smallexample
4387 VERS_1.1 @{
4388 global:
4389 foo1;
4390 local:
4391 old*;
4392 original*;
4393 new*;
4394 @};
4395
4396 VERS_1.2 @{
4397 foo2;
4398 @} VERS_1.1;
4399
4400 VERS_2.0 @{
4401 bar1; bar2;
4402 extern "C++" @{
4403 ns::*;
4404 "int f(int, double)";
4405 @}
4406 @} VERS_1.2;
4407 @end smallexample
4408
4409 This example version script defines three version nodes. The first
4410 version node defined is @samp{VERS_1.1}; it has no other dependencies.
4411 The script binds the symbol @samp{foo1} to @samp{VERS_1.1}. It reduces
4412 a number of symbols to local scope so that they are not visible outside
4413 of the shared library; this is done using wildcard patterns, so that any
4414 symbol whose name begins with @samp{old}, @samp{original}, or @samp{new}
4415 is matched. The wildcard patterns available are the same as those used
4416 in the shell when matching filenames (also known as ``globbing'').
4417 However, if you specify the symbol name inside double quotes, then the
4418 name is treated as literal, rather than as a glob pattern.
4419
4420 Next, the version script defines node @samp{VERS_1.2}. This node
4421 depends upon @samp{VERS_1.1}. The script binds the symbol @samp{foo2}
4422 to the version node @samp{VERS_1.2}.
4423
4424 Finally, the version script defines node @samp{VERS_2.0}. This node
4425 depends upon @samp{VERS_1.2}. The scripts binds the symbols @samp{bar1}
4426 and @samp{bar2} are bound to the version node @samp{VERS_2.0}.
4427
4428 When the linker finds a symbol defined in a library which is not
4429 specifically bound to a version node, it will effectively bind it to an
4430 unspecified base version of the library. You can bind all otherwise
4431 unspecified symbols to a given version node by using @samp{global: *;}
4432 somewhere in the version script.
4433
4434 The names of the version nodes have no specific meaning other than what
4435 they might suggest to the person reading them. The @samp{2.0} version
4436 could just as well have appeared in between @samp{1.1} and @samp{1.2}.
4437 However, this would be a confusing way to write a version script.
4438
4439 Node name can be omitted, provided it is the only version node
4440 in the version script. Such version script doesn't assign any versions to
4441 symbols, only selects which symbols will be globally visible out and which
4442 won't.
4443
4444 @smallexample
4445 @{ global: foo; bar; local: *; @};
4446 @end smallexample
4447
4448 When you link an application against a shared library that has versioned
4449 symbols, the application itself knows which version of each symbol it
4450 requires, and it also knows which version nodes it needs from each
4451 shared library it is linked against. Thus at runtime, the dynamic
4452 loader can make a quick check to make sure that the libraries you have
4453 linked against do in fact supply all of the version nodes that the
4454 application will need to resolve all of the dynamic symbols. In this
4455 way it is possible for the dynamic linker to know with certainty that
4456 all external symbols that it needs will be resolvable without having to
4457 search for each symbol reference.
4458
4459 The symbol versioning is in effect a much more sophisticated way of
4460 doing minor version checking that SunOS does. The fundamental problem
4461 that is being addressed here is that typically references to external
4462 functions are bound on an as-needed basis, and are not all bound when
4463 the application starts up. If a shared library is out of date, a
4464 required interface may be missing; when the application tries to use
4465 that interface, it may suddenly and unexpectedly fail. With symbol
4466 versioning, the user will get a warning when they start their program if
4467 the libraries being used with the application are too old.
4468
4469 There are several GNU extensions to Sun's versioning approach. The
4470 first of these is the ability to bind a symbol to a version node in the
4471 source file where the symbol is defined instead of in the versioning
4472 script. This was done mainly to reduce the burden on the library
4473 maintainer. You can do this by putting something like:
4474 @smallexample
4475 __asm__(".symver original_foo,foo@@VERS_1.1");
4476 @end smallexample
4477 @noindent
4478 in the C source file. This renames the function @samp{original_foo} to
4479 be an alias for @samp{foo} bound to the version node @samp{VERS_1.1}.
4480 The @samp{local:} directive can be used to prevent the symbol
4481 @samp{original_foo} from being exported. A @samp{.symver} directive
4482 takes precedence over a version script.
4483
4484 The second GNU extension is to allow multiple versions of the same
4485 function to appear in a given shared library. In this way you can make
4486 an incompatible change to an interface without increasing the major
4487 version number of the shared library, while still allowing applications
4488 linked against the old interface to continue to function.
4489
4490 To do this, you must use multiple @samp{.symver} directives in the
4491 source file. Here is an example:
4492
4493 @smallexample
4494 __asm__(".symver original_foo,foo@@");
4495 __asm__(".symver old_foo,foo@@VERS_1.1");
4496 __asm__(".symver old_foo1,foo@@VERS_1.2");
4497 __asm__(".symver new_foo,foo@@@@VERS_2.0");
4498 @end smallexample
4499
4500 In this example, @samp{foo@@} represents the symbol @samp{foo} bound to the
4501 unspecified base version of the symbol. The source file that contains this
4502 example would define 4 C functions: @samp{original_foo}, @samp{old_foo},
4503 @samp{old_foo1}, and @samp{new_foo}.
4504
4505 When you have multiple definitions of a given symbol, there needs to be
4506 some way to specify a default version to which external references to
4507 this symbol will be bound. You can do this with the
4508 @samp{foo@@@@VERS_2.0} type of @samp{.symver} directive. You can only
4509 declare one version of a symbol as the default in this manner; otherwise
4510 you would effectively have multiple definitions of the same symbol.
4511
4512 If you wish to bind a reference to a specific version of the symbol
4513 within the shared library, you can use the aliases of convenience
4514 (i.e., @samp{old_foo}), or you can use the @samp{.symver} directive to
4515 specifically bind to an external version of the function in question.
4516
4517 You can also specify the language in the version script:
4518
4519 @smallexample
4520 VERSION extern "lang" @{ version-script-commands @}
4521 @end smallexample
4522
4523 The supported @samp{lang}s are @samp{C}, @samp{C++}, and @samp{Java}.
4524 The linker will iterate over the list of symbols at the link time and
4525 demangle them according to @samp{lang} before matching them to the
4526 patterns specified in @samp{version-script-commands}.
4527
4528 Demangled names may contains spaces and other special characters. As
4529 described above, you can use a glob pattern to match demangled names,
4530 or you can use a double-quoted string to match the string exactly. In
4531 the latter case, be aware that minor differences (such as differing
4532 whitespace) between the version script and the demangler output will
4533 cause a mismatch. As the exact string generated by the demangler
4534 might change in the future, even if the mangled name does not, you
4535 should check that all of your version directives are behaving as you
4536 expect when you upgrade.
4537
4538 @node Expressions
4539 @section Expressions in Linker Scripts
4540 @cindex expressions
4541 @cindex arithmetic
4542 The syntax for expressions in the linker script language is identical to
4543 that of C expressions. All expressions are evaluated as integers. All
4544 expressions are evaluated in the same size, which is 32 bits if both the
4545 host and target are 32 bits, and is otherwise 64 bits.
4546
4547 You can use and set symbol values in expressions.
4548
4549 The linker defines several special purpose builtin functions for use in
4550 expressions.
4551
4552 @menu
4553 * Constants:: Constants
4554 * Symbols:: Symbol Names
4555 * Orphan Sections:: Orphan Sections
4556 * Location Counter:: The Location Counter
4557 * Operators:: Operators
4558 * Evaluation:: Evaluation
4559 * Expression Section:: The Section of an Expression
4560 * Builtin Functions:: Builtin Functions
4561 @end menu
4562
4563 @node Constants
4564 @subsection Constants
4565 @cindex integer notation
4566 @cindex constants in linker scripts
4567 All constants are integers.
4568
4569 As in C, the linker considers an integer beginning with @samp{0} to be
4570 octal, and an integer beginning with @samp{0x} or @samp{0X} to be
4571 hexadecimal. The linker considers other integers to be decimal.
4572
4573 @cindex scaled integers
4574 @cindex K and M integer suffixes
4575 @cindex M and K integer suffixes
4576 @cindex suffixes for integers
4577 @cindex integer suffixes
4578 In addition, you can use the suffixes @code{K} and @code{M} to scale a
4579 constant by
4580 @c TEXI2ROFF-KILL
4581 @ifnottex
4582 @c END TEXI2ROFF-KILL
4583 @code{1024} or @code{1024*1024}
4584 @c TEXI2ROFF-KILL
4585 @end ifnottex
4586 @tex
4587 ${\rm 1024}$ or ${\rm 1024}^2$
4588 @end tex
4589 @c END TEXI2ROFF-KILL
4590 respectively. For example, the following all refer to the same quantity:
4591 @smallexample
4592 _fourk_1 = 4K;
4593 _fourk_2 = 4096;
4594 _fourk_3 = 0x1000;
4595 @end smallexample
4596
4597 @node Symbols
4598 @subsection Symbol Names
4599 @cindex symbol names
4600 @cindex names
4601 @cindex quoted symbol names
4602 @kindex "
4603 Unless quoted, symbol names start with a letter, underscore, or period
4604 and may include letters, digits, underscores, periods, and hyphens.
4605 Unquoted symbol names must not conflict with any keywords. You can
4606 specify a symbol which contains odd characters or has the same name as a
4607 keyword by surrounding the symbol name in double quotes:
4608 @smallexample
4609 "SECTION" = 9;
4610 "with a space" = "also with a space" + 10;
4611 @end smallexample
4612
4613 Since symbols can contain many non-alphabetic characters, it is safest
4614 to delimit symbols with spaces. For example, @samp{A-B} is one symbol,
4615 whereas @samp{A - B} is an expression involving subtraction.
4616
4617 @node Orphan Sections
4618 @subsection Orphan Sections
4619 @cindex orphan
4620 Orphan sections are sections present in the input files which
4621 are not explicitly placed into the output file by the linker
4622 script. The linker will still copy these sections into the
4623 output file, but it has to guess as to where they should be
4624 placed. The linker uses a simple heuristic to do this. It
4625 attempts to place orphan sections after non-orphan sections of the
4626 same attribute, such as code vs data, loadable vs non-loadable, etc.
4627 If there is not enough room to do this then it places
4628 at the end of the file.
4629
4630 For ELF targets, the attribute of the section includes section type as
4631 well as section flag.
4632
4633 @node Location Counter
4634 @subsection The Location Counter
4635 @kindex .
4636 @cindex dot
4637 @cindex location counter
4638 @cindex current output location
4639 The special linker variable @dfn{dot} @samp{.} always contains the
4640 current output location counter. Since the @code{.} always refers to a
4641 location in an output section, it may only appear in an expression
4642 within a @code{SECTIONS} command. The @code{.} symbol may appear
4643 anywhere that an ordinary symbol is allowed in an expression.
4644
4645 @cindex holes
4646 Assigning a value to @code{.} will cause the location counter to be
4647 moved. This may be used to create holes in the output section. The
4648 location counter may not be moved backwards inside an output section,
4649 and may not be moved backwards outside of an output section if so
4650 doing creates areas with overlapping LMAs.
4651
4652 @smallexample
4653 SECTIONS
4654 @{
4655 output :
4656 @{
4657 file1(.text)
4658 . = . + 1000;
4659 file2(.text)
4660 . += 1000;
4661 file3(.text)
4662 @} = 0x12345678;
4663 @}
4664 @end smallexample
4665 @noindent
4666 In the previous example, the @samp{.text} section from @file{file1} is
4667 located at the beginning of the output section @samp{output}. It is
4668 followed by a 1000 byte gap. Then the @samp{.text} section from
4669 @file{file2} appears, also with a 1000 byte gap following before the
4670 @samp{.text} section from @file{file3}. The notation @samp{= 0x12345678}
4671 specifies what data to write in the gaps (@pxref{Output Section Fill}).
4672
4673 @cindex dot inside sections
4674 Note: @code{.} actually refers to the byte offset from the start of the
4675 current containing object. Normally this is the @code{SECTIONS}
4676 statement, whose start address is 0, hence @code{.} can be used as an
4677 absolute address. If @code{.} is used inside a section description
4678 however, it refers to the byte offset from the start of that section,
4679 not an absolute address. Thus in a script like this:
4680
4681 @smallexample
4682 SECTIONS
4683 @{
4684 . = 0x100
4685 .text: @{
4686 *(.text)
4687 . = 0x200
4688 @}
4689 . = 0x500
4690 .data: @{
4691 *(.data)
4692 . += 0x600
4693 @}
4694 @}
4695 @end smallexample
4696
4697 The @samp{.text} section will be assigned a starting address of 0x100
4698 and a size of exactly 0x200 bytes, even if there is not enough data in
4699 the @samp{.text} input sections to fill this area. (If there is too
4700 much data, an error will be produced because this would be an attempt to
4701 move @code{.} backwards). The @samp{.data} section will start at 0x500
4702 and it will have an extra 0x600 bytes worth of space after the end of
4703 the values from the @samp{.data} input sections and before the end of
4704 the @samp{.data} output section itself.
4705
4706 @cindex dot outside sections
4707 Setting symbols to the value of the location counter outside of an
4708 output section statement can result in unexpected values if the linker
4709 needs to place orphan sections. For example, given the following:
4710
4711 @smallexample
4712 SECTIONS
4713 @{
4714 start_of_text = . ;
4715 .text: @{ *(.text) @}
4716 end_of_text = . ;
4717
4718 start_of_data = . ;
4719 .data: @{ *(.data) @}
4720 end_of_data = . ;
4721 @}
4722 @end smallexample
4723
4724 If the linker needs to place some input section, e.g. @code{.rodata},
4725 not mentioned in the script, it might choose to place that section
4726 between @code{.text} and @code{.data}. You might think the linker
4727 should place @code{.rodata} on the blank line in the above script, but
4728 blank lines are of no particular significance to the linker. As well,
4729 the linker doesn't associate the above symbol names with their
4730 sections. Instead, it assumes that all assignments or other
4731 statements belong to the previous output section, except for the
4732 special case of an assignment to @code{.}. I.e., the linker will
4733 place the orphan @code{.rodata} section as if the script was written
4734 as follows:
4735
4736 @smallexample
4737 SECTIONS
4738 @{
4739 start_of_text = . ;
4740 .text: @{ *(.text) @}
4741 end_of_text = . ;
4742
4743 start_of_data = . ;
4744 .rodata: @{ *(.rodata) @}
4745 .data: @{ *(.data) @}
4746 end_of_data = . ;
4747 @}
4748 @end smallexample
4749
4750 This may or may not be the script author's intention for the value of
4751 @code{start_of_data}. One way to influence the orphan section
4752 placement is to assign the location counter to itself, as the linker
4753 assumes that an assignment to @code{.} is setting the start address of
4754 a following output section and thus should be grouped with that
4755 section. So you could write:
4756
4757 @smallexample
4758 SECTIONS
4759 @{
4760 start_of_text = . ;
4761 .text: @{ *(.text) @}
4762 end_of_text = . ;
4763
4764 . = . ;
4765 start_of_data = . ;
4766 .data: @{ *(.data) @}
4767 end_of_data = . ;
4768 @}
4769 @end smallexample
4770
4771 Now, the orphan @code{.rodata} section will be placed between
4772 @code{end_of_text} and @code{start_of_data}.
4773
4774 @need 2000
4775 @node Operators
4776 @subsection Operators
4777 @cindex operators for arithmetic
4778 @cindex arithmetic operators
4779 @cindex precedence in expressions
4780 The linker recognizes the standard C set of arithmetic operators, with
4781 the standard bindings and precedence levels:
4782 @c TEXI2ROFF-KILL
4783 @ifnottex
4784 @c END TEXI2ROFF-KILL
4785 @smallexample
4786 precedence associativity Operators Notes
4787 (highest)
4788 1 left ! - ~ (1)
4789 2 left * / %
4790 3 left + -
4791 4 left >> <<
4792 5 left == != > < <= >=
4793 6 left &
4794 7 left |
4795 8 left &&
4796 9 left ||
4797 10 right ? :
4798 11 right &= += -= *= /= (2)
4799 (lowest)
4800 @end smallexample
4801 Notes:
4802 (1) Prefix operators
4803 (2) @xref{Assignments}.
4804 @c TEXI2ROFF-KILL
4805 @end ifnottex
4806 @tex
4807 \vskip \baselineskip
4808 %"lispnarrowing" is the extra indent used generally for smallexample
4809 \hskip\lispnarrowing\vbox{\offinterlineskip
4810 \hrule
4811 \halign
4812 {\vrule#&\strut\hfil\ #\ \hfil&\vrule#&\strut\hfil\ #\ \hfil&\vrule#&\strut\hfil\ {\tt #}\ \hfil&\vrule#\cr
4813 height2pt&\omit&&\omit&&\omit&\cr
4814 &Precedence&& Associativity &&{\rm Operators}&\cr
4815 height2pt&\omit&&\omit&&\omit&\cr
4816 \noalign{\hrule}
4817 height2pt&\omit&&\omit&&\omit&\cr
4818 &highest&&&&&\cr
4819 % '176 is tilde, '~' in tt font
4820 &1&&left&&\qquad- \char'176\ !\qquad\dag&\cr
4821 &2&&left&&* / \%&\cr
4822 &3&&left&&+ -&\cr
4823 &4&&left&&>> <<&\cr
4824 &5&&left&&== != > < <= >=&\cr
4825 &6&&left&&\&&\cr
4826 &7&&left&&|&\cr
4827 &8&&left&&{\&\&}&\cr
4828 &9&&left&&||&\cr
4829 &10&&right&&? :&\cr
4830 &11&&right&&\qquad\&= += -= *= /=\qquad\ddag&\cr
4831 &lowest&&&&&\cr
4832 height2pt&\omit&&\omit&&\omit&\cr}
4833 \hrule}
4834 @end tex
4835 @iftex
4836 {
4837 @obeylines@parskip=0pt@parindent=0pt
4838 @dag@quad Prefix operators.
4839 @ddag@quad @xref{Assignments}.
4840 }
4841 @end iftex
4842 @c END TEXI2ROFF-KILL
4843
4844 @node Evaluation
4845 @subsection Evaluation
4846 @cindex lazy evaluation
4847 @cindex expression evaluation order
4848 The linker evaluates expressions lazily. It only computes the value of
4849 an expression when absolutely necessary.
4850
4851 The linker needs some information, such as the value of the start
4852 address of the first section, and the origins and lengths of memory
4853 regions, in order to do any linking at all. These values are computed
4854 as soon as possible when the linker reads in the linker script.
4855
4856 However, other values (such as symbol values) are not known or needed
4857 until after storage allocation. Such values are evaluated later, when
4858 other information (such as the sizes of output sections) is available
4859 for use in the symbol assignment expression.
4860
4861 The sizes of sections cannot be known until after allocation, so
4862 assignments dependent upon these are not performed until after
4863 allocation.
4864
4865 Some expressions, such as those depending upon the location counter
4866 @samp{.}, must be evaluated during section allocation.
4867
4868 If the result of an expression is required, but the value is not
4869 available, then an error results. For example, a script like the
4870 following
4871 @smallexample
4872 @group
4873 SECTIONS
4874 @{
4875 .text 9+this_isnt_constant :
4876 @{ *(.text) @}
4877 @}
4878 @end group
4879 @end smallexample
4880 @noindent
4881 will cause the error message @samp{non constant expression for initial
4882 address}.
4883
4884 @node Expression Section
4885 @subsection The Section of an Expression
4886 @cindex expression sections
4887 @cindex absolute expressions
4888 @cindex relative expressions
4889 @cindex absolute and relocatable symbols
4890 @cindex relocatable and absolute symbols
4891 @cindex symbols, relocatable and absolute
4892 When the linker evaluates an expression, the result is either absolute
4893 or relative to some section. A relative expression is expressed as a
4894 fixed offset from the base of a section.
4895
4896 The position of the expression within the linker script determines
4897 whether it is absolute or relative. An expression which appears within
4898 an output section definition is relative to the base of the output
4899 section. An expression which appears elsewhere will be absolute.
4900
4901 A symbol set to a relative expression will be relocatable if you request
4902 relocatable output using the @samp{-r} option. That means that a
4903 further link operation may change the value of the symbol. The symbol's
4904 section will be the section of the relative expression.
4905
4906 A symbol set to an absolute expression will retain the same value
4907 through any further link operation. The symbol will be absolute, and
4908 will not have any particular associated section.
4909
4910 You can use the builtin function @code{ABSOLUTE} to force an expression
4911 to be absolute when it would otherwise be relative. For example, to
4912 create an absolute symbol set to the address of the end of the output
4913 section @samp{.data}:
4914 @smallexample
4915 SECTIONS
4916 @{
4917 .data : @{ *(.data) _edata = ABSOLUTE(.); @}
4918 @}
4919 @end smallexample
4920 @noindent
4921 If @samp{ABSOLUTE} were not used, @samp{_edata} would be relative to the
4922 @samp{.data} section.
4923
4924 @node Builtin Functions
4925 @subsection Builtin Functions
4926 @cindex functions in expressions
4927 The linker script language includes a number of builtin functions for
4928 use in linker script expressions.
4929
4930 @table @code
4931 @item ABSOLUTE(@var{exp})
4932 @kindex ABSOLUTE(@var{exp})
4933 @cindex expression, absolute
4934 Return the absolute (non-relocatable, as opposed to non-negative) value
4935 of the expression @var{exp}. Primarily useful to assign an absolute
4936 value to a symbol within a section definition, where symbol values are
4937 normally section relative. @xref{Expression Section}.
4938
4939 @item ADDR(@var{section})
4940 @kindex ADDR(@var{section})
4941 @cindex section address in expression
4942 Return the absolute address (the VMA) of the named @var{section}. Your
4943 script must previously have defined the location of that section. In
4944 the following example, @code{symbol_1} and @code{symbol_2} are assigned
4945 identical values:
4946 @smallexample
4947 @group
4948 SECTIONS @{ @dots{}
4949 .output1 :
4950 @{
4951 start_of_output_1 = ABSOLUTE(.);
4952 @dots{}
4953 @}
4954 .output :
4955 @{
4956 symbol_1 = ADDR(.output1);
4957 symbol_2 = start_of_output_1;
4958 @}
4959 @dots{} @}
4960 @end group
4961 @end smallexample
4962
4963 @item ALIGN(@var{align})
4964 @itemx ALIGN(@var{exp},@var{align})
4965 @kindex ALIGN(@var{align})
4966 @kindex ALIGN(@var{exp},@var{align})
4967 @cindex round up location counter
4968 @cindex align location counter
4969 @cindex round up expression
4970 @cindex align expression
4971 Return the location counter (@code{.}) or arbitrary expression aligned
4972 to the next @var{align} boundary. The single operand @code{ALIGN}
4973 doesn't change the value of the location counter---it just does
4974 arithmetic on it. The two operand @code{ALIGN} allows an arbitrary
4975 expression to be aligned upwards (@code{ALIGN(@var{align})} is
4976 equivalent to @code{ALIGN(., @var{align})}).
4977
4978 Here is an example which aligns the output @code{.data} section to the
4979 next @code{0x2000} byte boundary after the preceding section and sets a
4980 variable within the section to the next @code{0x8000} boundary after the
4981 input sections:
4982 @smallexample
4983 @group
4984 SECTIONS @{ @dots{}
4985 .data ALIGN(0x2000): @{
4986 *(.data)
4987 variable = ALIGN(0x8000);
4988 @}
4989 @dots{} @}
4990 @end group
4991 @end smallexample
4992 @noindent
4993 The first use of @code{ALIGN} in this example specifies the location of
4994 a section because it is used as the optional @var{address} attribute of
4995 a section definition (@pxref{Output Section Address}). The second use
4996 of @code{ALIGN} is used to defines the value of a symbol.
4997
4998 The builtin function @code{NEXT} is closely related to @code{ALIGN}.
4999
5000 @item BLOCK(@var{exp})
5001 @kindex BLOCK(@var{exp})
5002 This is a synonym for @code{ALIGN}, for compatibility with older linker
5003 scripts. It is most often seen when setting the address of an output
5004 section.
5005
5006 @item DATA_SEGMENT_ALIGN(@var{maxpagesize}, @var{commonpagesize})
5007 @kindex DATA_SEGMENT_ALIGN(@var{maxpagesize}, @var{commonpagesize})
5008 This is equivalent to either
5009 @smallexample
5010 (ALIGN(@var{maxpagesize}) + (. & (@var{maxpagesize} - 1)))
5011 @end smallexample
5012 or
5013 @smallexample
5014 (ALIGN(@var{maxpagesize}) + (. & (@var{maxpagesize} - @var{commonpagesize})))
5015 @end smallexample
5016 @noindent
5017 depending on whether the latter uses fewer @var{commonpagesize} sized pages
5018 for the data segment (area between the result of this expression and
5019 @code{DATA_SEGMENT_END}) than the former or not.
5020 If the latter form is used, it means @var{commonpagesize} bytes of runtime
5021 memory will be saved at the expense of up to @var{commonpagesize} wasted
5022 bytes in the on-disk file.
5023
5024 This expression can only be used directly in @code{SECTIONS} commands, not in
5025 any output section descriptions and only once in the linker script.
5026 @var{commonpagesize} should be less or equal to @var{maxpagesize} and should
5027 be the system page size the object wants to be optimized for (while still
5028 working on system page sizes up to @var{maxpagesize}).
5029
5030 @noindent
5031 Example:
5032 @smallexample
5033 . = DATA_SEGMENT_ALIGN(0x10000, 0x2000);
5034 @end smallexample
5035
5036 @item DATA_SEGMENT_END(@var{exp})
5037 @kindex DATA_SEGMENT_END(@var{exp})
5038 This defines the end of data segment for @code{DATA_SEGMENT_ALIGN}
5039 evaluation purposes.
5040
5041 @smallexample
5042 . = DATA_SEGMENT_END(.);
5043 @end smallexample
5044
5045 @item DATA_SEGMENT_RELRO_END(@var{offset}, @var{exp})
5046 @kindex DATA_SEGMENT_RELRO_END(@var{offset}, @var{exp})
5047 This defines the end of the @code{PT_GNU_RELRO} segment when
5048 @samp{-z relro} option is used. Second argument is returned.
5049 When @samp{-z relro} option is not present, @code{DATA_SEGMENT_RELRO_END}
5050 does nothing, otherwise @code{DATA_SEGMENT_ALIGN} is padded so that
5051 @var{exp} + @var{offset} is aligned to the most commonly used page
5052 boundary for particular target. If present in the linker script,
5053 it must always come in between @code{DATA_SEGMENT_ALIGN} and
5054 @code{DATA_SEGMENT_END}.
5055
5056 @smallexample
5057 . = DATA_SEGMENT_RELRO_END(24, .);
5058 @end smallexample
5059
5060 @item DEFINED(@var{symbol})
5061 @kindex DEFINED(@var{symbol})
5062 @cindex symbol defaults
5063 Return 1 if @var{symbol} is in the linker global symbol table and is
5064 defined before the statement using DEFINED in the script, otherwise
5065 return 0. You can use this function to provide
5066 default values for symbols. For example, the following script fragment
5067 shows how to set a global symbol @samp{begin} to the first location in
5068 the @samp{.text} section---but if a symbol called @samp{begin} already
5069 existed, its value is preserved:
5070
5071 @smallexample
5072 @group
5073 SECTIONS @{ @dots{}
5074 .text : @{
5075 begin = DEFINED(begin) ? begin : . ;
5076 @dots{}
5077 @}
5078 @dots{}
5079 @}
5080 @end group
5081 @end smallexample
5082
5083 @item LENGTH(@var{memory})
5084 @kindex LENGTH(@var{memory})
5085 Return the length of the memory region named @var{memory}.
5086
5087 @item LOADADDR(@var{section})
5088 @kindex LOADADDR(@var{section})
5089 @cindex section load address in expression
5090 Return the absolute LMA of the named @var{section}. This is normally
5091 the same as @code{ADDR}, but it may be different if the @code{AT}
5092 attribute is used in the output section definition (@pxref{Output
5093 Section LMA}).
5094
5095 @kindex MAX
5096 @item MAX(@var{exp1}, @var{exp2})
5097 Returns the maximum of @var{exp1} and @var{exp2}.
5098
5099 @kindex MIN
5100 @item MIN(@var{exp1}, @var{exp2})
5101 Returns the minimum of @var{exp1} and @var{exp2}.
5102
5103 @item NEXT(@var{exp})
5104 @kindex NEXT(@var{exp})
5105 @cindex unallocated address, next
5106 Return the next unallocated address that is a multiple of @var{exp}.
5107 This function is closely related to @code{ALIGN(@var{exp})}; unless you
5108 use the @code{MEMORY} command to define discontinuous memory for the
5109 output file, the two functions are equivalent.
5110
5111 @item ORIGIN(@var{memory})
5112 @kindex ORIGIN(@var{memory})
5113 Return the origin of the memory region named @var{memory}.
5114
5115 @item SEGMENT_START(@var{segment}, @var{default})
5116 @kindex SEGMENT_START(@var{segment}, @var{default})
5117 Return the base address of the named @var{segment}. If an explicit
5118 value has been given for this segment (with a command-line @samp{-T}
5119 option) that value will be returned; otherwise the value will be
5120 @var{default}. At present, the @samp{-T} command-line option can only
5121 be used to set the base address for the ``text'', ``data'', and
5122 ``bss'' sections, but you use @code{SEGMENT_START} with any segment
5123 name.
5124
5125 @item SIZEOF(@var{section})
5126 @kindex SIZEOF(@var{section})
5127 @cindex section size
5128 Return the size in bytes of the named @var{section}, if that section has
5129 been allocated. If the section has not been allocated when this is
5130 evaluated, the linker will report an error. In the following example,
5131 @code{symbol_1} and @code{symbol_2} are assigned identical values:
5132 @smallexample
5133 @group
5134 SECTIONS@{ @dots{}
5135 .output @{
5136 .start = . ;
5137 @dots{}
5138 .end = . ;
5139 @}
5140 symbol_1 = .end - .start ;
5141 symbol_2 = SIZEOF(.output);
5142 @dots{} @}
5143 @end group
5144 @end smallexample
5145
5146 @item SIZEOF_HEADERS
5147 @itemx sizeof_headers
5148 @kindex SIZEOF_HEADERS
5149 @cindex header size
5150 Return the size in bytes of the output file's headers. This is
5151 information which appears at the start of the output file. You can use
5152 this number when setting the start address of the first section, if you
5153 choose, to facilitate paging.
5154
5155 @cindex not enough room for program headers
5156 @cindex program headers, not enough room
5157 When producing an ELF output file, if the linker script uses the
5158 @code{SIZEOF_HEADERS} builtin function, the linker must compute the
5159 number of program headers before it has determined all the section
5160 addresses and sizes. If the linker later discovers that it needs
5161 additional program headers, it will report an error @samp{not enough
5162 room for program headers}. To avoid this error, you must avoid using
5163 the @code{SIZEOF_HEADERS} function, or you must rework your linker
5164 script to avoid forcing the linker to use additional program headers, or
5165 you must define the program headers yourself using the @code{PHDRS}
5166 command (@pxref{PHDRS}).
5167 @end table
5168
5169 @node Implicit Linker Scripts
5170 @section Implicit Linker Scripts
5171 @cindex implicit linker scripts
5172 If you specify a linker input file which the linker can not recognize as
5173 an object file or an archive file, it will try to read the file as a
5174 linker script. If the file can not be parsed as a linker script, the
5175 linker will report an error.
5176
5177 An implicit linker script will not replace the default linker script.
5178
5179 Typically an implicit linker script would contain only symbol
5180 assignments, or the @code{INPUT}, @code{GROUP}, or @code{VERSION}
5181 commands.
5182
5183 Any input files read because of an implicit linker script will be read
5184 at the position in the command line where the implicit linker script was
5185 read. This can affect archive searching.
5186
5187 @ifset GENERIC
5188 @node Machine Dependent
5189 @chapter Machine Dependent Features
5190
5191 @cindex machine dependencies
5192 @command{ld} has additional features on some platforms; the following
5193 sections describe them. Machines where @command{ld} has no additional
5194 functionality are not listed.
5195
5196 @menu
5197 @ifset H8300
5198 * H8/300:: @command{ld} and the H8/300
5199 @end ifset
5200 @ifset I960
5201 * i960:: @command{ld} and the Intel 960 family
5202 @end ifset
5203 @ifset ARM
5204 * ARM:: @command{ld} and the ARM family
5205 @end ifset
5206 @ifset HPPA
5207 * HPPA ELF32:: @command{ld} and HPPA 32-bit ELF
5208 @end ifset
5209 @ifset MMIX
5210 * MMIX:: @command{ld} and MMIX
5211 @end ifset
5212 @ifset MSP430
5213 * MSP430:: @command{ld} and MSP430
5214 @end ifset
5215 @ifset M68HC11
5216 * M68HC11/68HC12:: @code{ld} and the Motorola 68HC11 and 68HC12 families
5217 @end ifset
5218 @ifset POWERPC
5219 * PowerPC ELF32:: @command{ld} and PowerPC 32-bit ELF Support
5220 @end ifset
5221 @ifset POWERPC64
5222 * PowerPC64 ELF64:: @command{ld} and PowerPC64 64-bit ELF Support
5223 @end ifset
5224 @ifset TICOFF
5225 * TI COFF:: @command{ld} and TI COFF
5226 @end ifset
5227 @ifset WIN32
5228 * WIN32:: @command{ld} and WIN32 (cygwin/mingw)
5229 @end ifset
5230 @ifset XTENSA
5231 * Xtensa:: @command{ld} and Xtensa Processors
5232 @end ifset
5233 @end menu
5234 @end ifset
5235
5236 @ifset H8300
5237 @ifclear GENERIC
5238 @raisesections
5239 @end ifclear
5240
5241 @node H8/300
5242 @section @command{ld} and the H8/300
5243
5244 @cindex H8/300 support
5245 For the H8/300, @command{ld} can perform these global optimizations when
5246 you specify the @samp{--relax} command-line option.
5247
5248 @table @emph
5249 @cindex relaxing on H8/300
5250 @item relaxing address modes
5251 @command{ld} finds all @code{jsr} and @code{jmp} instructions whose
5252 targets are within eight bits, and turns them into eight-bit
5253 program-counter relative @code{bsr} and @code{bra} instructions,
5254 respectively.
5255
5256 @cindex synthesizing on H8/300
5257 @item synthesizing instructions
5258 @c FIXME: specifically mov.b, or any mov instructions really?
5259 @command{ld} finds all @code{mov.b} instructions which use the
5260 sixteen-bit absolute address form, but refer to the top
5261 page of memory, and changes them to use the eight-bit address form.
5262 (That is: the linker turns @samp{mov.b @code{@@}@var{aa}:16} into
5263 @samp{mov.b @code{@@}@var{aa}:8} whenever the address @var{aa} is in the
5264 top page of memory).
5265
5266 @item bit manipulation instructions
5267 @command{ld} finds all bit manipulation instructions like @code{band, bclr,
5268 biand, bild, bior, bist, bixor, bld, bnot, bor, bset, bst, btst, bxor}
5269 which use 32 bit and 16 bit absolute address form, but refer to the top
5270 page of memory, and changes them to use the 8 bit address form.
5271 (That is: the linker turns @samp{bset #xx:3,@code{@@}@var{aa}:32} into
5272 @samp{bset #xx:3,@code{@@}@var{aa}:8} whenever the address @var{aa} is in
5273 the top page of memory).
5274
5275 @item system control instructions
5276 @command{ld} finds all @code{ldc.w, stc.w} instructions which use the
5277 32 bit absolute address form, but refer to the top page of memory, and
5278 changes them to use 16 bit address form.
5279 (That is: the linker turns @samp{ldc.w @code{@@}@var{aa}:32,ccr} into
5280 @samp{ldc.w @code{@@}@var{aa}:16,ccr} whenever the address @var{aa} is in
5281 the top page of memory).
5282 @end table
5283
5284 @ifclear GENERIC
5285 @lowersections
5286 @end ifclear
5287 @end ifset
5288
5289 @ifclear GENERIC
5290 @ifset Renesas
5291 @c This stuff is pointless to say unless you're especially concerned
5292 @c with Renesas chips; don't enable it for generic case, please.
5293 @node Renesas
5294 @chapter @command{ld} and Other Renesas Chips
5295
5296 @command{ld} also supports the Renesas (formerly Hitachi) H8/300H,
5297 H8/500, and SH chips. No special features, commands, or command-line
5298 options are required for these chips.
5299 @end ifset
5300 @end ifclear
5301
5302 @ifset I960
5303 @ifclear GENERIC
5304 @raisesections
5305 @end ifclear
5306
5307 @node i960
5308 @section @command{ld} and the Intel 960 Family
5309
5310 @cindex i960 support
5311
5312 You can use the @samp{-A@var{architecture}} command line option to
5313 specify one of the two-letter names identifying members of the 960
5314 family; the option specifies the desired output target, and warns of any
5315 incompatible instructions in the input files. It also modifies the
5316 linker's search strategy for archive libraries, to support the use of
5317 libraries specific to each particular architecture, by including in the
5318 search loop names suffixed with the string identifying the architecture.
5319
5320 For example, if your @command{ld} command line included @w{@samp{-ACA}} as
5321 well as @w{@samp{-ltry}}, the linker would look (in its built-in search
5322 paths, and in any paths you specify with @samp{-L}) for a library with
5323 the names
5324
5325 @smallexample
5326 @group
5327 try
5328 libtry.a
5329 tryca
5330 libtryca.a
5331 @end group
5332 @end smallexample
5333
5334 @noindent
5335 The first two possibilities would be considered in any event; the last
5336 two are due to the use of @w{@samp{-ACA}}.
5337
5338 You can meaningfully use @samp{-A} more than once on a command line, since
5339 the 960 architecture family allows combination of target architectures; each
5340 use will add another pair of name variants to search for when @w{@samp{-l}}
5341 specifies a library.
5342
5343 @cindex @option{--relax} on i960
5344 @cindex relaxing on i960
5345 @command{ld} supports the @samp{--relax} option for the i960 family. If
5346 you specify @samp{--relax}, @command{ld} finds all @code{balx} and
5347 @code{calx} instructions whose targets are within 24 bits, and turns
5348 them into 24-bit program-counter relative @code{bal} and @code{cal}
5349 instructions, respectively. @command{ld} also turns @code{cal}
5350 instructions into @code{bal} instructions when it determines that the
5351 target subroutine is a leaf routine (that is, the target subroutine does
5352 not itself call any subroutines).
5353
5354 @ifclear GENERIC
5355 @lowersections
5356 @end ifclear
5357 @end ifset
5358
5359 @ifset ARM
5360 @ifclear GENERIC
5361 @raisesections
5362 @end ifclear
5363
5364 @ifset M68HC11
5365 @ifclear GENERIC
5366 @raisesections
5367 @end ifclear
5368
5369 @node M68HC11/68HC12
5370 @section @command{ld} and the Motorola 68HC11 and 68HC12 families
5371
5372 @cindex M68HC11 and 68HC12 support
5373
5374 @subsection Linker Relaxation
5375
5376 For the Motorola 68HC11, @command{ld} can perform these global
5377 optimizations when you specify the @samp{--relax} command-line option.
5378
5379 @table @emph
5380 @cindex relaxing on M68HC11
5381 @item relaxing address modes
5382 @command{ld} finds all @code{jsr} and @code{jmp} instructions whose
5383 targets are within eight bits, and turns them into eight-bit
5384 program-counter relative @code{bsr} and @code{bra} instructions,
5385 respectively.
5386
5387 @command{ld} also looks at all 16-bit extended addressing modes and
5388 transforms them in a direct addressing mode when the address is in
5389 page 0 (between 0 and 0x0ff).
5390
5391 @item relaxing gcc instruction group
5392 When @command{gcc} is called with @option{-mrelax}, it can emit group
5393 of instructions that the linker can optimize to use a 68HC11 direct
5394 addressing mode. These instructions consists of @code{bclr} or
5395 @code{bset} instructions.
5396
5397 @end table
5398
5399 @subsection Trampoline Generation
5400
5401 @cindex trampoline generation on M68HC11
5402 @cindex trampoline generation on M68HC12
5403 For 68HC11 and 68HC12, @command{ld} can generate trampoline code to
5404 call a far function using a normal @code{jsr} instruction. The linker
5405 will also change the relocation to some far function to use the
5406 trampoline address instead of the function address. This is typically the
5407 case when a pointer to a function is taken. The pointer will in fact
5408 point to the function trampoline.
5409
5410 @ifclear GENERIC
5411 @lowersections
5412 @end ifclear
5413 @end ifset
5414
5415 @node ARM
5416 @section @command{ld} and the ARM family
5417
5418 @cindex ARM interworking support
5419 @kindex --support-old-code
5420 For the ARM, @command{ld} will generate code stubs to allow functions calls
5421 between ARM and Thumb code. These stubs only work with code that has
5422 been compiled and assembled with the @samp{-mthumb-interwork} command
5423 line option. If it is necessary to link with old ARM object files or
5424 libraries, which have not been compiled with the -mthumb-interwork
5425 option then the @samp{--support-old-code} command line switch should be
5426 given to the linker. This will make it generate larger stub functions
5427 which will work with non-interworking aware ARM code. Note, however,
5428 the linker does not support generating stubs for function calls to
5429 non-interworking aware Thumb code.
5430
5431 @cindex thumb entry point
5432 @cindex entry point, thumb
5433 @kindex --thumb-entry=@var{entry}
5434 The @samp{--thumb-entry} switch is a duplicate of the generic
5435 @samp{--entry} switch, in that it sets the program's starting address.
5436 But it also sets the bottom bit of the address, so that it can be
5437 branched to using a BX instruction, and the program will start
5438 executing in Thumb mode straight away.
5439
5440 @cindex BE8
5441 @kindex --be8
5442 The @samp{--be8} switch instructs @command{ld} to generate BE8 format
5443 executables. This option is only valid when linking big-endian objects.
5444 The resulting image will contain big-endian data and little-endian code.
5445
5446 @cindex TARGET1
5447 @kindex --target1-rel
5448 @kindex --target1-abs
5449 The @samp{R_ARM_TARGET1} relocation is typically used for entries in the
5450 @samp{.init_array} section. It is interpreted as either @samp{R_ARM_REL32}
5451 or @samp{R_ARM_ABS32}, depending on the target. The @samp{--target1-rel}
5452 and @samp{--target1-abs} switches override the default.
5453
5454 @cindex TARGET2
5455 @kindex --target2=@var{type}
5456 The @samp{--target2=type} switch overrides the default definition of the
5457 @samp{R_ARM_TARGET2} relocation. Valid values for @samp{type}, their
5458 meanings, and target defaults are as follows:
5459 @table @samp
5460 @item rel
5461 @samp{R_ARM_REL32} (arm*-*-elf, arm*-*-eabi)
5462 @item abs
5463 @samp{R_ARM_ABS32} (arm*-*-symbianelf)
5464 @item got-rel
5465 @samp{R_ARM_GOT_PREL} (arm*-*-linux, arm*-*-*bsd)
5466 @end table
5467
5468 @cindex FIX_V4BX
5469 @kindex --fix-v4bx
5470 The @samp{R_ARM_V4BX} relocation (defined by the ARM AAELF
5471 specification) enables objects compiled for the ARMv4 architecture to be
5472 interworking-safe when linked with other objects compiled for ARMv4t, but
5473 also allows pure ARMv4 binaries to be built from the same ARMv4 objects.
5474
5475 In the latter case, the switch @option{--fix-v4bx} must be passed to the
5476 linker, which causes v4t @code{BX rM} instructions to be rewritten as
5477 @code{MOV PC,rM}, since v4 processors do not have a @code{BX} instruction.
5478
5479 In the former case, the switch should not be used, and @samp{R_ARM_V4BX}
5480 relocations are ignored.
5481
5482 @cindex USE_BLX
5483 @kindex --use-blx
5484 The @samp{--use-blx} switch enables the linker to use ARM/Thumb
5485 BLX instructions (available on ARMv5t and above) in various
5486 situations. Currently it is used to perform calls via the PLT from Thumb
5487 code using BLX rather than using BX and a mode-switching stub before
5488 each PLT entry. This should lead to such calls executing slightly faster.
5489
5490 This option is enabled implicitly for SymbianOS, so there is no need to
5491 specify it if you are using that target.
5492
5493 @ifclear GENERIC
5494 @lowersections
5495 @end ifclear
5496 @end ifset
5497
5498 @ifset HPPA
5499 @ifclear GENERIC
5500 @raisesections
5501 @end ifclear
5502
5503 @node HPPA ELF32
5504 @section @command{ld} and HPPA 32-bit ELF Support
5505 @cindex HPPA multiple sub-space stubs
5506 @kindex --multi-subspace
5507 When generating a shared library, @command{ld} will by default generate
5508 import stubs suitable for use with a single sub-space application.
5509 The @samp{--multi-subspace} switch causes @command{ld} to generate export
5510 stubs, and different (larger) import stubs suitable for use with
5511 multiple sub-spaces.
5512
5513 @cindex HPPA stub grouping
5514 @kindex --stub-group-size=@var{N}
5515 Long branch stubs and import/export stubs are placed by @command{ld} in
5516 stub sections located between groups of input sections.
5517 @samp{--stub-group-size} specifies the maximum size of a group of input
5518 sections handled by one stub section. Since branch offsets are signed,
5519 a stub section may serve two groups of input sections, one group before
5520 the stub section, and one group after it. However, when using
5521 conditional branches that require stubs, it may be better (for branch
5522 prediction) that stub sections only serve one group of input sections.
5523 A negative value for @samp{N} chooses this scheme, ensuring that
5524 branches to stubs always use a negative offset. Two special values of
5525 @samp{N} are recognized, @samp{1} and @samp{-1}. These both instruct
5526 @command{ld} to automatically size input section groups for the branch types
5527 detected, with the same behaviour regarding stub placement as other
5528 positive or negative values of @samp{N} respectively.
5529
5530 Note that @samp{--stub-group-size} does not split input sections. A
5531 single input section larger than the group size specified will of course
5532 create a larger group (of one section). If input sections are too
5533 large, it may not be possible for a branch to reach its stub.
5534
5535 @ifclear GENERIC
5536 @lowersections
5537 @end ifclear
5538 @end ifset
5539
5540 @ifset MMIX
5541 @ifclear GENERIC
5542 @raisesections
5543 @end ifclear
5544
5545 @node MMIX
5546 @section @code{ld} and MMIX
5547 For MMIX, there is a choice of generating @code{ELF} object files or
5548 @code{mmo} object files when linking. The simulator @code{mmix}
5549 understands the @code{mmo} format. The binutils @code{objcopy} utility
5550 can translate between the two formats.
5551
5552 There is one special section, the @samp{.MMIX.reg_contents} section.
5553 Contents in this section is assumed to correspond to that of global
5554 registers, and symbols referring to it are translated to special symbols,
5555 equal to registers. In a final link, the start address of the
5556 @samp{.MMIX.reg_contents} section corresponds to the first allocated
5557 global register multiplied by 8. Register @code{$255} is not included in
5558 this section; it is always set to the program entry, which is at the
5559 symbol @code{Main} for @code{mmo} files.
5560
5561 Symbols with the prefix @code{__.MMIX.start.}, for example
5562 @code{__.MMIX.start..text} and @code{__.MMIX.start..data} are special;
5563 there must be only one each, even if they are local. The default linker
5564 script uses these to set the default start address of a section.
5565
5566 Initial and trailing multiples of zero-valued 32-bit words in a section,
5567 are left out from an mmo file.
5568
5569 @ifclear GENERIC
5570 @lowersections
5571 @end ifclear
5572 @end ifset
5573
5574 @ifset MSP430
5575 @ifclear GENERIC
5576 @raisesections
5577 @end ifclear
5578
5579 @node MSP430
5580 @section @code{ld} and MSP430
5581 For the MSP430 it is possible to select the MPU architecture. The flag @samp{-m [mpu type]}
5582 will select an appropriate linker script for selected MPU type. (To get a list of known MPUs
5583 just pass @samp{-m help} option to the linker).
5584
5585 @cindex MSP430 extra sections
5586 The linker will recognize some extra sections which are MSP430 specific:
5587
5588 @table @code
5589 @item @samp{.vectors}
5590 Defines a portion of ROM where interrupt vectors located.
5591
5592 @item @samp{.bootloader}
5593 Defines the bootloader portion of the ROM (if applicable). Any code
5594 in this section will be uploaded to the MPU.
5595
5596 @item @samp{.infomem}
5597 Defines an information memory section (if applicable). Any code in
5598 this section will be uploaded to the MPU.
5599
5600 @item @samp{.infomemnobits}
5601 This is the same as the @samp{.infomem} section except that any code
5602 in this section will not be uploaded to the MPU.
5603
5604 @item @samp{.noinit}
5605 Denotes a portion of RAM located above @samp{.bss} section.
5606
5607 The last two sections are used by gcc.
5608 @end table
5609
5610 @ifclear GENERIC
5611 @lowersections
5612 @end ifclear
5613 @end ifset
5614
5615 @ifset POWERPC
5616 @ifclear GENERIC
5617 @raisesections
5618 @end ifclear
5619
5620 @node PowerPC ELF32
5621 @section @command{ld} and PowerPC 32-bit ELF Support
5622 @cindex PowerPC long branches
5623 @kindex --relax on PowerPC
5624 Branches on PowerPC processors are limited to a signed 26-bit
5625 displacement, which may result in @command{ld} giving
5626 @samp{relocation truncated to fit} errors with very large programs.
5627 @samp{--relax} enables the generation of trampolines that can access
5628 the entire 32-bit address space. These trampolines are inserted at
5629 section boundaries, so may not themselves be reachable if an input
5630 section exceeds 33M in size.
5631
5632 @cindex PowerPC ELF32 options
5633 @table @option
5634 @cindex PowerPC PLT
5635 @kindex --bss-plt
5636 @item --bss-plt
5637 Current PowerPC GCC accepts a @samp{-msecure-plt} option that
5638 generates code capable of using a newer PLT and GOT layout that has
5639 the security advantage of no executable section ever needing to be
5640 writable and no writable section ever being executable. PowerPC
5641 @command{ld} will generate this layout, including stubs to access the
5642 PLT, if all input files (including startup and static libraries) were
5643 compiled with @samp{-msecure-plt}. @samp{--bss-plt} forces the old
5644 BSS PLT (and GOT layout) which can give slightly better performance.
5645
5646 @cindex PowerPC GOT
5647 @kindex --sdata-got
5648 @item --sdata-got
5649 The new secure PLT and GOT are placed differently relative to other
5650 sections compared to older BSS PLT and GOT placement. The location of
5651 @code{.plt} must change because the new secure PLT is an initialized
5652 section while the old PLT is uninitialized. The reason for the
5653 @code{.got} change is more subtle: The new placement allows
5654 @code{.got} to be read-only in applications linked with
5655 @samp{-z relro -z now}. However, this placement means that
5656 @code{.sdata} cannot always be used in shared libraries, because the
5657 PowerPC ABI accesses @code{.sdata} in shared libraries from the GOT
5658 pointer. @samp{--sdata-got} forces the old GOT placement. PowerPC
5659 GCC doesn't use @code{.sdata} in shared libraries, so this option is
5660 really only useful for other compilers that may do so.
5661
5662 @cindex PowerPC stub symbols
5663 @kindex --emit-stub-syms
5664 @item --emit-stub-syms
5665 This option causes @command{ld} to label linker stubs with a local
5666 symbol that encodes the stub type and destination.
5667
5668 @cindex PowerPC TLS optimization
5669 @kindex --no-tls-optimize
5670 @item --no-tls-optimize
5671 PowerPC @command{ld} normally performs some optimization of code
5672 sequences used to access Thread-Local Storage. Use this option to
5673 disable the optimization.
5674 @end table
5675
5676 @ifclear GENERIC
5677 @lowersections
5678 @end ifclear
5679 @end ifset
5680
5681 @ifset POWERPC64
5682 @ifclear GENERIC
5683 @raisesections
5684 @end ifclear
5685
5686 @node PowerPC64 ELF64
5687 @section @command{ld} and PowerPC64 64-bit ELF Support
5688
5689 @cindex PowerPC64 ELF64 options
5690 @table @option
5691 @cindex PowerPC64 stub grouping
5692 @kindex --stub-group-size
5693 @item --stub-group-size
5694 Long branch stubs, PLT call stubs and TOC adjusting stubs are placed
5695 by @command{ld} in stub sections located between groups of input sections.
5696 @samp{--stub-group-size} specifies the maximum size of a group of input
5697 sections handled by one stub section. Since branch offsets are signed,
5698 a stub section may serve two groups of input sections, one group before
5699 the stub section, and one group after it. However, when using
5700 conditional branches that require stubs, it may be better (for branch
5701 prediction) that stub sections only serve one group of input sections.
5702 A negative value for @samp{N} chooses this scheme, ensuring that
5703 branches to stubs always use a negative offset. Two special values of
5704 @samp{N} are recognized, @samp{1} and @samp{-1}. These both instruct
5705 @command{ld} to automatically size input section groups for the branch types
5706 detected, with the same behaviour regarding stub placement as other
5707 positive or negative values of @samp{N} respectively.
5708
5709 Note that @samp{--stub-group-size} does not split input sections. A
5710 single input section larger than the group size specified will of course
5711 create a larger group (of one section). If input sections are too
5712 large, it may not be possible for a branch to reach its stub.
5713
5714 @cindex PowerPC64 stub symbols
5715 @kindex --emit-stub-syms
5716 @item --emit-stub-syms
5717 This option causes @command{ld} to label linker stubs with a local
5718 symbol that encodes the stub type and destination.
5719
5720 @cindex PowerPC64 dot symbols
5721 @kindex --dotsyms
5722 @kindex --no-dotsyms
5723 @item --dotsyms, --no-dotsyms
5724 These two options control how @command{ld} interprets version patterns
5725 in a version script. Older PowerPC64 compilers emitted both a
5726 function descriptor symbol with the same name as the function, and a
5727 code entry symbol with the name prefixed by a dot (@samp{.}). To
5728 properly version a function @samp{foo}, the version script thus needs
5729 to control both @samp{foo} and @samp{.foo}. The option
5730 @samp{--dotsyms}, on by default, automatically adds the required
5731 dot-prefixed patterns. Use @samp{--no-dotsyms} to disable this
5732 feature.
5733
5734 @cindex PowerPC64 TLS optimization
5735 @kindex --no-tls-optimize
5736 @item --no-tls-optimize
5737 PowerPC64 @command{ld} normally performs some optimization of code
5738 sequences used to access Thread-Local Storage. Use this option to
5739 disable the optimization.
5740
5741 @cindex PowerPC64 OPD optimization
5742 @kindex --no-opd-optimize
5743 @item --no-opd-optimize
5744 PowerPC64 @command{ld} normally removes @code{.opd} section entries
5745 corresponding to deleted link-once functions, or functions removed by
5746 the action of @samp{--gc-sections} or linker scrip @code{/DISCARD/}.
5747 Use this option to disable @code{.opd} optimization.
5748
5749 @cindex PowerPC64 OPD spacing
5750 @kindex --non-overlapping-opd
5751 @item --non-overlapping-opd
5752 Some PowerPC64 compilers have an option to generate compressed
5753 @code{.opd} entries spaced 16 bytes apart, overlapping the third word,
5754 the static chain pointer (unused in C) with the first word of the next
5755 entry. This option expands such entries to the full 24 bytes.
5756
5757 @cindex PowerPC64 TOC optimization
5758 @kindex --no-toc-optimize
5759 @item --no-toc-optimize
5760 PowerPC64 @command{ld} normally removes unused @code{.toc} section
5761 entries. Such entries are detected by examining relocations that
5762 reference the TOC in code sections. A reloc in a deleted code section
5763 marks a TOC word as unneeded, while a reloc in a kept code section
5764 marks a TOC word as needed. Since the TOC may reference itself, TOC
5765 relocs are also examined. TOC words marked as both needed and
5766 unneeded will of course be kept. TOC words without any referencing
5767 reloc are assumed to be part of a multi-word entry, and are kept or
5768 discarded as per the nearest marked preceding word. This works
5769 reliably for compiler generated code, but may be incorrect if assembly
5770 code is used to insert TOC entries. Use this option to disable the
5771 optimization.
5772
5773 @cindex PowerPC64 multi-TOC
5774 @kindex --no-multi-toc
5775 @item --no-multi-toc
5776 By default, PowerPC64 GCC generates code for a TOC model where TOC
5777 entries are accessed with a 16-bit offset from r2. This limits the
5778 total TOC size to 64K. PowerPC64 @command{ld} extends this limit by
5779 grouping code sections such that each group uses less than 64K for its
5780 TOC entries, then inserts r2 adjusting stubs between inter-group
5781 calls. @command{ld} does not split apart input sections, so cannot
5782 help if a single input file has a @code{.toc} section that exceeds
5783 64K, most likely from linking multiple files with @command{ld -r}.
5784 Use this option to turn off this feature.
5785 @end table
5786
5787 @ifclear GENERIC
5788 @lowersections
5789 @end ifclear
5790 @end ifset
5791
5792 @ifset TICOFF
5793 @ifclear GENERIC
5794 @raisesections
5795 @end ifclear
5796
5797 @node TI COFF
5798 @section @command{ld}'s Support for Various TI COFF Versions
5799 @cindex TI COFF versions
5800 @kindex --format=@var{version}
5801 The @samp{--format} switch allows selection of one of the various
5802 TI COFF versions. The latest of this writing is 2; versions 0 and 1 are
5803 also supported. The TI COFF versions also vary in header byte-order
5804 format; @command{ld} will read any version or byte order, but the output
5805 header format depends on the default specified by the specific target.
5806
5807 @ifclear GENERIC
5808 @lowersections
5809 @end ifclear
5810 @end ifset
5811
5812 @ifset WIN32
5813 @ifclear GENERIC
5814 @raisesections
5815 @end ifclear
5816
5817 @node WIN32
5818 @section @command{ld} and WIN32 (cygwin/mingw)
5819
5820 This section describes some of the win32 specific @command{ld} issues.
5821 See @ref{Options,,Command Line Options} for detailed description of the
5822 command line options mentioned here.
5823
5824 @table @emph
5825 @cindex import libraries
5826 @item import libraries
5827 The standard Windows linker creates and uses so-called import
5828 libraries, which contains information for linking to dll's. They are
5829 regular static archives and are handled as any other static
5830 archive. The cygwin and mingw ports of @command{ld} have specific
5831 support for creating such libraries provided with the
5832 @samp{--out-implib} command line option.
5833
5834 @item exporting DLL symbols
5835 @cindex exporting DLL symbols
5836 The cygwin/mingw @command{ld} has several ways to export symbols for dll's.
5837
5838 @table @emph
5839 @item using auto-export functionality
5840 @cindex using auto-export functionality
5841 By default @command{ld} exports symbols with the auto-export functionality,
5842 which is controlled by the following command line options:
5843
5844 @itemize
5845 @item --export-all-symbols [This is the default]
5846 @item --exclude-symbols
5847 @item --exclude-libs
5848 @end itemize
5849
5850 If, however, @samp{--export-all-symbols} is not given explicitly on the
5851 command line, then the default auto-export behavior will be @emph{disabled}
5852 if either of the following are true:
5853
5854 @itemize
5855 @item A DEF file is used.
5856 @item Any symbol in any object file was marked with the __declspec(dllexport) attribute.
5857 @end itemize
5858
5859 @item using a DEF file
5860 @cindex using a DEF file
5861 Another way of exporting symbols is using a DEF file. A DEF file is
5862 an ASCII file containing definitions of symbols which should be
5863 exported when a dll is created. Usually it is named @samp{<dll
5864 name>.def} and is added as any other object file to the linker's
5865 command line. The file's name must end in @samp{.def} or @samp{.DEF}.
5866
5867 @example
5868 gcc -o <output> <objectfiles> <dll name>.def
5869 @end example
5870
5871 Using a DEF file turns off the normal auto-export behavior, unless the
5872 @samp{--export-all-symbols} option is also used.
5873
5874 Here is an example of a DEF file for a shared library called @samp{xyz.dll}:
5875
5876 @example
5877 LIBRARY "xyz.dll" BASE=0x20000000
5878
5879 EXPORTS
5880 foo
5881 bar
5882 _bar = bar
5883 another_foo = abc.dll.afoo
5884 var1 DATA
5885 @end example
5886
5887 This example defines a DLL with a non-default base address and five
5888 symbols in the export table. The third exported symbol @code{_bar} is an
5889 alias for the second. The fourth symbol, @code{another_foo} is resolved
5890 by "forwarding" to another module and treating it as an alias for
5891 @code{afoo} exported from the DLL @samp{abc.dll}. The final symbol
5892 @code{var1} is declared to be a data object.
5893
5894 The optional @code{LIBRARY <name>} command indicates the @emph{internal}
5895 name of the output DLL. If @samp{<name>} does not include a suffix,
5896 the default library suffix, @samp{.DLL} is appended.
5897
5898 When the .DEF file is used to build an application, rather than a
5899 library, the @code{NAME <name>} command should be used instead of
5900 @code{LIBRARY}. If @samp{<name>} does not include a suffix, the default
5901 executable suffix, @samp{.EXE} is appended.
5902
5903 With either @code{LIBRARY <name>} or @code{NAME <name>} the optional
5904 specification @code{BASE = <number>} may be used to specify a
5905 non-default base address for the image.
5906
5907 If neither @code{LIBRARY <name>} nor @code{NAME <name>} is specified,
5908 or they specify an empty string, the internal name is the same as the
5909 filename specified on the command line.
5910
5911 The complete specification of an export symbol is:
5912
5913 @example
5914 EXPORTS
5915 ( ( ( <name1> [ = <name2> ] )
5916 | ( <name1> = <module-name> . <external-name>))
5917 [ @@ <integer> ] [NONAME] [DATA] [CONSTANT] [PRIVATE] ) *
5918 @end example
5919
5920 Declares @samp{<name1>} as an exported symbol from the DLL, or declares
5921 @samp{<name1>} as an exported alias for @samp{<name2>}; or declares
5922 @samp{<name1>} as a "forward" alias for the symbol
5923 @samp{<external-name>} in the DLL @samp{<module-name>}.
5924 Optionally, the symbol may be exported by the specified ordinal
5925 @samp{<integer>} alias.
5926
5927 The optional keywords that follow the declaration indicate:
5928
5929 @code{NONAME}: Do not put the symbol name in the DLL's export table. It
5930 will still be exported by its ordinal alias (either the value specified
5931 by the .def specification or, otherwise, the value assigned by the
5932 linker). The symbol name, however, does remain visible in the import
5933 library (if any), unless @code{PRIVATE} is also specified.
5934
5935 @code{DATA}: The symbol is a variable or object, rather than a function.
5936 The import lib will export only an indirect reference to @code{foo} as
5937 the symbol @code{_imp__foo} (ie, @code{foo} must be resolved as
5938 @code{*_imp__foo}).
5939
5940 @code{CONSTANT}: Like @code{DATA}, but put the undecorated @code{foo} as
5941 well as @code{_imp__foo} into the import library. Both refer to the
5942 read-only import address table's pointer to the variable, not to the
5943 variable itself. This can be dangerous. If the user code fails to add
5944 the @code{dllimport} attribute and also fails to explicitly add the
5945 extra indirection that the use of the attribute enforces, the
5946 application will behave unexpectedly.
5947
5948 @code{PRIVATE}: Put the symbol in the DLL's export table, but do not put
5949 it into the static import library used to resolve imports at link time. The
5950 symbol can still be imported using the @code{LoadLibrary/GetProcAddress}
5951 API at runtime or by by using the GNU ld extension of linking directly to
5952 the DLL without an import library.
5953
5954 See ld/deffilep.y in the binutils sources for the full specification of
5955 other DEF file statements
5956
5957 @cindex creating a DEF file
5958 While linking a shared dll, @command{ld} is able to create a DEF file
5959 with the @samp{--output-def <file>} command line option.
5960
5961 @item Using decorations
5962 @cindex Using decorations
5963 Another way of marking symbols for export is to modify the source code
5964 itself, so that when building the DLL each symbol to be exported is
5965 declared as:
5966
5967 @example
5968 __declspec(dllexport) int a_variable
5969 __declspec(dllexport) void a_function(int with_args)
5970 @end example
5971
5972 All such symbols will be exported from the DLL. If, however,
5973 any of the object files in the DLL contain symbols decorated in
5974 this way, then the normal auto-export behavior is disabled, unless
5975 the @samp{--export-all-symbols} option is also used.
5976
5977 Note that object files that wish to access these symbols must @emph{not}
5978 decorate them with dllexport. Instead, they should use dllimport,
5979 instead:
5980
5981 @example
5982 __declspec(dllimport) int a_variable
5983 __declspec(dllimport) void a_function(int with_args)
5984 @end example
5985
5986 This complicates the structure of library header files, because
5987 when included by the library itself the header must declare the
5988 variables and functions as dllexport, but when included by client
5989 code the header must declare them as dllimport. There are a number
5990 of idioms that are typically used to do this; often client code can
5991 omit the __declspec() declaration completely. See
5992 @samp{--enable-auto-import} and @samp{automatic data imports} for more
5993 information.
5994 @end table
5995
5996 @cindex automatic data imports
5997 @item automatic data imports
5998 The standard Windows dll format supports data imports from dlls only
5999 by adding special decorations (dllimport/dllexport), which let the
6000 compiler produce specific assembler instructions to deal with this
6001 issue. This increases the effort necessary to port existing Un*x
6002 code to these platforms, especially for large
6003 c++ libraries and applications. The auto-import feature, which was
6004 initially provided by Paul Sokolovsky, allows one to omit the
6005 decorations to achieve a behavior that conforms to that on POSIX/Un*x
6006 platforms. This feature is enabled with the @samp{--enable-auto-import}
6007 command-line option, although it is enabled by default on cygwin/mingw.
6008 The @samp{--enable-auto-import} option itself now serves mainly to
6009 suppress any warnings that are ordinarily emitted when linked objects
6010 trigger the feature's use.
6011
6012 auto-import of variables does not always work flawlessly without
6013 additional assistance. Sometimes, you will see this message
6014
6015 "variable '<var>' can't be auto-imported. Please read the
6016 documentation for ld's @code{--enable-auto-import} for details."
6017
6018 The @samp{--enable-auto-import} documentation explains why this error
6019 occurs, and several methods that can be used to overcome this difficulty.
6020 One of these methods is the @emph{runtime pseudo-relocs} feature, described
6021 below.
6022
6023 @cindex runtime pseudo-relocation
6024 For complex variables imported from DLLs (such as structs or classes),
6025 object files typically contain a base address for the variable and an
6026 offset (@emph{addend}) within the variable--to specify a particular
6027 field or public member, for instance. Unfortunately, the runtime loader used
6028 in win32 environments is incapable of fixing these references at runtime
6029 without the additional information supplied by dllimport/dllexport decorations.
6030 The standard auto-import feature described above is unable to resolve these
6031 references.
6032
6033 The @samp{--enable-runtime-pseudo-relocs} switch allows these references to
6034 be resolved without error, while leaving the task of adjusting the references
6035 themselves (with their non-zero addends) to specialized code provided by the
6036 runtime environment. Recent versions of the cygwin and mingw environments and
6037 compilers provide this runtime support; older versions do not. However, the
6038 support is only necessary on the developer's platform; the compiled result will
6039 run without error on an older system.
6040
6041 @samp{--enable-runtime-pseudo-relocs} is not the default; it must be explicitly
6042 enabled as needed.
6043
6044 @cindex direct linking to a dll
6045 @item direct linking to a dll
6046 The cygwin/mingw ports of @command{ld} support the direct linking,
6047 including data symbols, to a dll without the usage of any import
6048 libraries. This is much faster and uses much less memory than does the
6049 traditional import library method, especially when linking large
6050 libraries or applications. When @command{ld} creates an import lib, each
6051 function or variable exported from the dll is stored in its own bfd, even
6052 though a single bfd could contain many exports. The overhead involved in
6053 storing, loading, and processing so many bfd's is quite large, and explains the
6054 tremendous time, memory, and storage needed to link against particularly
6055 large or complex libraries when using import libs.
6056
6057 Linking directly to a dll uses no extra command-line switches other than
6058 @samp{-L} and @samp{-l}, because @command{ld} already searches for a number
6059 of names to match each library. All that is needed from the developer's
6060 perspective is an understanding of this search, in order to force ld to
6061 select the dll instead of an import library.
6062
6063
6064 For instance, when ld is called with the argument @samp{-lxxx} it will attempt
6065 to find, in the first directory of its search path,
6066
6067 @example
6068 libxxx.dll.a
6069 xxx.dll.a
6070 libxxx.a
6071 xxx.lib
6072 cygxxx.dll (*)
6073 libxxx.dll
6074 xxx.dll
6075 @end example
6076
6077 before moving on to the next directory in the search path.
6078
6079 (*) Actually, this is not @samp{cygxxx.dll} but in fact is @samp{<prefix>xxx.dll},
6080 where @samp{<prefix>} is set by the @command{ld} option
6081 @samp{--dll-search-prefix=<prefix>}. In the case of cygwin, the standard gcc spec
6082 file includes @samp{--dll-search-prefix=cyg}, so in effect we actually search for
6083 @samp{cygxxx.dll}.
6084
6085 Other win32-based unix environments, such as mingw or pw32, may use other
6086 @samp{<prefix>}es, although at present only cygwin makes use of this feature. It
6087 was originally intended to help avoid name conflicts among dll's built for the
6088 various win32/un*x environments, so that (for example) two versions of a zlib dll
6089 could coexist on the same machine.
6090
6091 The generic cygwin/mingw path layout uses a @samp{bin} directory for
6092 applications and dll's and a @samp{lib} directory for the import
6093 libraries (using cygwin nomenclature):
6094
6095 @example
6096 bin/
6097 cygxxx.dll
6098 lib/
6099 libxxx.dll.a (in case of dll's)
6100 libxxx.a (in case of static archive)
6101 @end example
6102
6103 Linking directly to a dll without using the import library can be
6104 done two ways:
6105
6106 1. Use the dll directly by adding the @samp{bin} path to the link line
6107 @example
6108 gcc -Wl,-verbose -o a.exe -L../bin/ -lxxx
6109 @end example
6110
6111 However, as the dll's often have version numbers appended to their names
6112 (@samp{cygncurses-5.dll}) this will often fail, unless one specifies
6113 @samp{-L../bin -lncurses-5} to include the version. Import libs are generally
6114 not versioned, and do not have this difficulty.
6115
6116 2. Create a symbolic link from the dll to a file in the @samp{lib}
6117 directory according to the above mentioned search pattern. This
6118 should be used to avoid unwanted changes in the tools needed for
6119 making the app/dll.
6120
6121 @example
6122 ln -s bin/cygxxx.dll lib/[cyg|lib|]xxx.dll[.a]
6123 @end example
6124
6125 Then you can link without any make environment changes.
6126
6127 @example
6128 gcc -Wl,-verbose -o a.exe -L../lib/ -lxxx
6129 @end example
6130
6131 This technique also avoids the version number problems, because the following is
6132 perfectly legal
6133
6134 @example
6135 bin/
6136 cygxxx-5.dll
6137 lib/
6138 libxxx.dll.a -> ../bin/cygxxx-5.dll
6139 @end example
6140
6141 Linking directly to a dll without using an import lib will work
6142 even when auto-import features are exercised, and even when
6143 @samp{--enable-runtime-pseudo-relocs} is used.
6144
6145 Given the improvements in speed and memory usage, one might justifiably
6146 wonder why import libraries are used at all. There are three reasons:
6147
6148 1. Until recently, the link-directly-to-dll functionality did @emph{not}
6149 work with auto-imported data.
6150
6151 2. Sometimes it is necessary to include pure static objects within the
6152 import library (which otherwise contains only bfd's for indirection
6153 symbols that point to the exports of a dll). Again, the import lib
6154 for the cygwin kernel makes use of this ability, and it is not
6155 possible to do this without an import lib.
6156
6157 3. Symbol aliases can only be resolved using an import lib. This is
6158 critical when linking against OS-supplied dll's (eg, the win32 API)
6159 in which symbols are usually exported as undecorated aliases of their
6160 stdcall-decorated assembly names.
6161
6162 So, import libs are not going away. But the ability to replace
6163 true import libs with a simple symbolic link to (or a copy of)
6164 a dll, in many cases, is a useful addition to the suite of tools
6165 binutils makes available to the win32 developer. Given the
6166 massive improvements in memory requirements during linking, storage
6167 requirements, and linking speed, we expect that many developers
6168 will soon begin to use this feature whenever possible.
6169
6170 @item symbol aliasing
6171 @table @emph
6172 @item adding additional names
6173 Sometimes, it is useful to export symbols with additional names.
6174 A symbol @samp{foo} will be exported as @samp{foo}, but it can also be
6175 exported as @samp{_foo} by using special directives in the DEF file
6176 when creating the dll. This will affect also the optional created
6177 import library. Consider the following DEF file:
6178
6179 @example
6180 LIBRARY "xyz.dll" BASE=0x61000000
6181
6182 EXPORTS
6183 foo
6184 _foo = foo
6185 @end example
6186
6187 The line @samp{_foo = foo} maps the symbol @samp{foo} to @samp{_foo}.
6188
6189 Another method for creating a symbol alias is to create it in the
6190 source code using the "weak" attribute:
6191
6192 @example
6193 void foo () @{ /* Do something. */; @}
6194 void _foo () __attribute__ ((weak, alias ("foo")));
6195 @end example
6196
6197 See the gcc manual for more information about attributes and weak
6198 symbols.
6199
6200 @item renaming symbols
6201 Sometimes it is useful to rename exports. For instance, the cygwin
6202 kernel does this regularly. A symbol @samp{_foo} can be exported as
6203 @samp{foo} but not as @samp{_foo} by using special directives in the
6204 DEF file. (This will also affect the import library, if it is
6205 created). In the following example:
6206
6207 @example
6208 LIBRARY "xyz.dll" BASE=0x61000000
6209
6210 EXPORTS
6211 _foo = foo
6212 @end example
6213
6214 The line @samp{_foo = foo} maps the exported symbol @samp{foo} to
6215 @samp{_foo}.
6216 @end table
6217
6218 Note: using a DEF file disables the default auto-export behavior,
6219 unless the @samp{--export-all-symbols} command line option is used.
6220 If, however, you are trying to rename symbols, then you should list
6221 @emph{all} desired exports in the DEF file, including the symbols
6222 that are not being renamed, and do @emph{not} use the
6223 @samp{--export-all-symbols} option. If you list only the
6224 renamed symbols in the DEF file, and use @samp{--export-all-symbols}
6225 to handle the other symbols, then the both the new names @emph{and}
6226 the original names for the renamed symbols will be exported.
6227 In effect, you'd be aliasing those symbols, not renaming them,
6228 which is probably not what you wanted.
6229
6230 @cindex weak externals
6231 @item weak externals
6232 The Windows object format, PE, specifies a form of weak symbols called
6233 weak externals. When a weak symbol is linked and the symbol is not
6234 defined, the weak symbol becomes an alias for some other symbol. There
6235 are three variants of weak externals:
6236 @itemize
6237 @item Definition is searched for in objects and libraries, historically
6238 called lazy externals.
6239 @item Definition is searched for only in other objects, not in libraries.
6240 This form is not presently implemented.
6241 @item No search; the symbol is an alias. This form is not presently
6242 implemented.
6243 @end itemize
6244 As a GNU extension, weak symbols that do not specify an alternate symbol
6245 are supported. If the symbol is undefined when linking, the symbol
6246 uses a default value.
6247 @end table
6248
6249 @ifclear GENERIC
6250 @lowersections
6251 @end ifclear
6252 @end ifset
6253
6254 @ifset XTENSA
6255 @ifclear GENERIC
6256 @raisesections
6257 @end ifclear
6258
6259 @node Xtensa
6260 @section @code{ld} and Xtensa Processors
6261
6262 @cindex Xtensa processors
6263 The default @command{ld} behavior for Xtensa processors is to interpret
6264 @code{SECTIONS} commands so that lists of explicitly named sections in a
6265 specification with a wildcard file will be interleaved when necessary to
6266 keep literal pools within the range of PC-relative load offsets. For
6267 example, with the command:
6268
6269 @smallexample
6270 SECTIONS
6271 @{
6272 .text : @{
6273 *(.literal .text)
6274 @}
6275 @}
6276 @end smallexample
6277
6278 @noindent
6279 @command{ld} may interleave some of the @code{.literal}
6280 and @code{.text} sections from different object files to ensure that the
6281 literal pools are within the range of PC-relative load offsets. A valid
6282 interleaving might place the @code{.literal} sections from an initial
6283 group of files followed by the @code{.text} sections of that group of
6284 files. Then, the @code{.literal} sections from the rest of the files
6285 and the @code{.text} sections from the rest of the files would follow.
6286
6287 @cindex @option{--relax} on Xtensa
6288 @cindex relaxing on Xtensa
6289 Relaxation is enabled by default for the Xtensa version of @command{ld} and
6290 provides two important link-time optimizations. The first optimization
6291 is to combine identical literal values to reduce code size. A redundant
6292 literal will be removed and all the @code{L32R} instructions that use it
6293 will be changed to reference an identical literal, as long as the
6294 location of the replacement literal is within the offset range of all
6295 the @code{L32R} instructions. The second optimization is to remove
6296 unnecessary overhead from assembler-generated ``longcall'' sequences of
6297 @code{L32R}/@code{CALLX@var{n}} when the target functions are within
6298 range of direct @code{CALL@var{n}} instructions.
6299
6300 For each of these cases where an indirect call sequence can be optimized
6301 to a direct call, the linker will change the @code{CALLX@var{n}}
6302 instruction to a @code{CALL@var{n}} instruction, remove the @code{L32R}
6303 instruction, and remove the literal referenced by the @code{L32R}
6304 instruction if it is not used for anything else. Removing the
6305 @code{L32R} instruction always reduces code size but can potentially
6306 hurt performance by changing the alignment of subsequent branch targets.
6307 By default, the linker will always preserve alignments, either by
6308 switching some instructions between 24-bit encodings and the equivalent
6309 density instructions or by inserting a no-op in place of the @code{L32R}
6310 instruction that was removed. If code size is more important than
6311 performance, the @option{--size-opt} option can be used to prevent the
6312 linker from widening density instructions or inserting no-ops, except in
6313 a few cases where no-ops are required for correctness.
6314
6315 The following Xtensa-specific command-line options can be used to
6316 control the linker:
6317
6318 @cindex Xtensa options
6319 @table @option
6320 @kindex --no-relax
6321 @item --no-relax
6322 Since the Xtensa version of @code{ld} enables the @option{--relax} option
6323 by default, the @option{--no-relax} option is provided to disable
6324 relaxation.
6325
6326 @item --size-opt
6327 When optimizing indirect calls to direct calls, optimize for code size
6328 more than performance. With this option, the linker will not insert
6329 no-ops or widen density instructions to preserve branch target
6330 alignment. There may still be some cases where no-ops are required to
6331 preserve the correctness of the code.
6332 @end table
6333
6334 @ifclear GENERIC
6335 @lowersections
6336 @end ifclear
6337 @end ifset
6338
6339 @ifclear SingleFormat
6340 @node BFD
6341 @chapter BFD
6342
6343 @cindex back end
6344 @cindex object file management
6345 @cindex object formats available
6346 @kindex objdump -i
6347 The linker accesses object and archive files using the BFD libraries.
6348 These libraries allow the linker to use the same routines to operate on
6349 object files whatever the object file format. A different object file
6350 format can be supported simply by creating a new BFD back end and adding
6351 it to the library. To conserve runtime memory, however, the linker and
6352 associated tools are usually configured to support only a subset of the
6353 object file formats available. You can use @code{objdump -i}
6354 (@pxref{objdump,,objdump,binutils.info,The GNU Binary Utilities}) to
6355 list all the formats available for your configuration.
6356
6357 @cindex BFD requirements
6358 @cindex requirements for BFD
6359 As with most implementations, BFD is a compromise between
6360 several conflicting requirements. The major factor influencing
6361 BFD design was efficiency: any time used converting between
6362 formats is time which would not have been spent had BFD not
6363 been involved. This is partly offset by abstraction payback; since
6364 BFD simplifies applications and back ends, more time and care
6365 may be spent optimizing algorithms for a greater speed.
6366
6367 One minor artifact of the BFD solution which you should bear in
6368 mind is the potential for information loss. There are two places where
6369 useful information can be lost using the BFD mechanism: during
6370 conversion and during output. @xref{BFD information loss}.
6371
6372 @menu
6373 * BFD outline:: How it works: an outline of BFD
6374 @end menu
6375
6376 @node BFD outline
6377 @section How It Works: An Outline of BFD
6378 @cindex opening object files
6379 @include bfdsumm.texi
6380 @end ifclear
6381
6382 @node Reporting Bugs
6383 @chapter Reporting Bugs
6384 @cindex bugs in @command{ld}
6385 @cindex reporting bugs in @command{ld}
6386
6387 Your bug reports play an essential role in making @command{ld} reliable.
6388
6389 Reporting a bug may help you by bringing a solution to your problem, or
6390 it may not. But in any case the principal function of a bug report is
6391 to help the entire community by making the next version of @command{ld}
6392 work better. Bug reports are your contribution to the maintenance of
6393 @command{ld}.
6394
6395 In order for a bug report to serve its purpose, you must include the
6396 information that enables us to fix the bug.
6397
6398 @menu
6399 * Bug Criteria:: Have you found a bug?
6400 * Bug Reporting:: How to report bugs
6401 @end menu
6402
6403 @node Bug Criteria
6404 @section Have You Found a Bug?
6405 @cindex bug criteria
6406
6407 If you are not sure whether you have found a bug, here are some guidelines:
6408
6409 @itemize @bullet
6410 @cindex fatal signal
6411 @cindex linker crash
6412 @cindex crash of linker
6413 @item
6414 If the linker gets a fatal signal, for any input whatever, that is a
6415 @command{ld} bug. Reliable linkers never crash.
6416
6417 @cindex error on valid input
6418 @item
6419 If @command{ld} produces an error message for valid input, that is a bug.
6420
6421 @cindex invalid input
6422 @item
6423 If @command{ld} does not produce an error message for invalid input, that
6424 may be a bug. In the general case, the linker can not verify that
6425 object files are correct.
6426
6427 @item
6428 If you are an experienced user of linkers, your suggestions for
6429 improvement of @command{ld} are welcome in any case.
6430 @end itemize
6431
6432 @node Bug Reporting
6433 @section How to Report Bugs
6434 @cindex bug reports
6435 @cindex @command{ld} bugs, reporting
6436
6437 A number of companies and individuals offer support for @sc{gnu}
6438 products. If you obtained @command{ld} from a support organization, we
6439 recommend you contact that organization first.
6440
6441 You can find contact information for many support companies and
6442 individuals in the file @file{etc/SERVICE} in the @sc{gnu} Emacs
6443 distribution.
6444
6445 Otherwise, send bug reports for @command{ld} to
6446 @samp{bug-binutils@@gnu.org}.
6447
6448 The fundamental principle of reporting bugs usefully is this:
6449 @strong{report all the facts}. If you are not sure whether to state a
6450 fact or leave it out, state it!
6451
6452 Often people omit facts because they think they know what causes the
6453 problem and assume that some details do not matter. Thus, you might
6454 assume that the name of a symbol you use in an example does not
6455 matter. Well, probably it does not, but one cannot be sure. Perhaps
6456 the bug is a stray memory reference which happens to fetch from the
6457 location where that name is stored in memory; perhaps, if the name
6458 were different, the contents of that location would fool the linker
6459 into doing the right thing despite the bug. Play it safe and give a
6460 specific, complete example. That is the easiest thing for you to do,
6461 and the most helpful.
6462
6463 Keep in mind that the purpose of a bug report is to enable us to fix
6464 the bug if it is new to us. Therefore, always write your bug reports
6465 on the assumption that the bug has not been reported previously.
6466
6467 Sometimes people give a few sketchy facts and ask, ``Does this ring a
6468 bell?'' This cannot help us fix a bug, so it is basically useless. We
6469 respond by asking for enough details to enable us to investigate.
6470 You might as well expedite matters by sending them to begin with.
6471
6472 To enable us to fix the bug, you should include all these things:
6473
6474 @itemize @bullet
6475 @item
6476 The version of @command{ld}. @command{ld} announces it if you start it with
6477 the @samp{--version} argument.
6478
6479 Without this, we will not know whether there is any point in looking for
6480 the bug in the current version of @command{ld}.
6481
6482 @item
6483 Any patches you may have applied to the @command{ld} source, including any
6484 patches made to the @code{BFD} library.
6485
6486 @item
6487 The type of machine you are using, and the operating system name and
6488 version number.
6489
6490 @item
6491 What compiler (and its version) was used to compile @command{ld}---e.g.
6492 ``@code{gcc-2.7}''.
6493
6494 @item
6495 The command arguments you gave the linker to link your example and
6496 observe the bug. To guarantee you will not omit something important,
6497 list them all. A copy of the Makefile (or the output from make) is
6498 sufficient.
6499
6500 If we were to try to guess the arguments, we would probably guess wrong
6501 and then we might not encounter the bug.
6502
6503 @item
6504 A complete input file, or set of input files, that will reproduce the
6505 bug. It is generally most helpful to send the actual object files
6506 provided that they are reasonably small. Say no more than 10K. For
6507 bigger files you can either make them available by FTP or HTTP or else
6508 state that you are willing to send the object file(s) to whomever
6509 requests them. (Note - your email will be going to a mailing list, so
6510 we do not want to clog it up with large attachments). But small
6511 attachments are best.
6512
6513 If the source files were assembled using @code{gas} or compiled using
6514 @code{gcc}, then it may be OK to send the source files rather than the
6515 object files. In this case, be sure to say exactly what version of
6516 @code{gas} or @code{gcc} was used to produce the object files. Also say
6517 how @code{gas} or @code{gcc} were configured.
6518
6519 @item
6520 A description of what behavior you observe that you believe is
6521 incorrect. For example, ``It gets a fatal signal.''
6522
6523 Of course, if the bug is that @command{ld} gets a fatal signal, then we
6524 will certainly notice it. But if the bug is incorrect output, we might
6525 not notice unless it is glaringly wrong. You might as well not give us
6526 a chance to make a mistake.
6527
6528 Even if the problem you experience is a fatal signal, you should still
6529 say so explicitly. Suppose something strange is going on, such as, your
6530 copy of @command{ld} is out of sync, or you have encountered a bug in the
6531 C library on your system. (This has happened!) Your copy might crash
6532 and ours would not. If you told us to expect a crash, then when ours
6533 fails to crash, we would know that the bug was not happening for us. If
6534 you had not told us to expect a crash, then we would not be able to draw
6535 any conclusion from our observations.
6536
6537 @item
6538 If you wish to suggest changes to the @command{ld} source, send us context
6539 diffs, as generated by @code{diff} with the @samp{-u}, @samp{-c}, or
6540 @samp{-p} option. Always send diffs from the old file to the new file.
6541 If you even discuss something in the @command{ld} source, refer to it by
6542 context, not by line number.
6543
6544 The line numbers in our development sources will not match those in your
6545 sources. Your line numbers would convey no useful information to us.
6546 @end itemize
6547
6548 Here are some things that are not necessary:
6549
6550 @itemize @bullet
6551 @item
6552 A description of the envelope of the bug.
6553
6554 Often people who encounter a bug spend a lot of time investigating
6555 which changes to the input file will make the bug go away and which
6556 changes will not affect it.
6557
6558 This is often time consuming and not very useful, because the way we
6559 will find the bug is by running a single example under the debugger
6560 with breakpoints, not by pure deduction from a series of examples.
6561 We recommend that you save your time for something else.
6562
6563 Of course, if you can find a simpler example to report @emph{instead}
6564 of the original one, that is a convenience for us. Errors in the
6565 output will be easier to spot, running under the debugger will take
6566 less time, and so on.
6567
6568 However, simplification is not vital; if you do not want to do this,
6569 report the bug anyway and send us the entire test case you used.
6570
6571 @item
6572 A patch for the bug.
6573
6574 A patch for the bug does help us if it is a good one. But do not omit
6575 the necessary information, such as the test case, on the assumption that
6576 a patch is all we need. We might see problems with your patch and decide
6577 to fix the problem another way, or we might not understand it at all.
6578
6579 Sometimes with a program as complicated as @command{ld} it is very hard to
6580 construct an example that will make the program follow a certain path
6581 through the code. If you do not send us the example, we will not be
6582 able to construct one, so we will not be able to verify that the bug is
6583 fixed.
6584
6585 And if we cannot understand what bug you are trying to fix, or why your
6586 patch should be an improvement, we will not install it. A test case will
6587 help us to understand.
6588
6589 @item
6590 A guess about what the bug is or what it depends on.
6591
6592 Such guesses are usually wrong. Even we cannot guess right about such
6593 things without first using the debugger to find the facts.
6594 @end itemize
6595
6596 @node MRI
6597 @appendix MRI Compatible Script Files
6598 @cindex MRI compatibility
6599 To aid users making the transition to @sc{gnu} @command{ld} from the MRI
6600 linker, @command{ld} can use MRI compatible linker scripts as an
6601 alternative to the more general-purpose linker scripting language
6602 described in @ref{Scripts}. MRI compatible linker scripts have a much
6603 simpler command set than the scripting language otherwise used with
6604 @command{ld}. @sc{gnu} @command{ld} supports the most commonly used MRI
6605 linker commands; these commands are described here.
6606
6607 In general, MRI scripts aren't of much use with the @code{a.out} object
6608 file format, since it only has three sections and MRI scripts lack some
6609 features to make use of them.
6610
6611 You can specify a file containing an MRI-compatible script using the
6612 @samp{-c} command-line option.
6613
6614 Each command in an MRI-compatible script occupies its own line; each
6615 command line starts with the keyword that identifies the command (though
6616 blank lines are also allowed for punctuation). If a line of an
6617 MRI-compatible script begins with an unrecognized keyword, @command{ld}
6618 issues a warning message, but continues processing the script.
6619
6620 Lines beginning with @samp{*} are comments.
6621
6622 You can write these commands using all upper-case letters, or all
6623 lower case; for example, @samp{chip} is the same as @samp{CHIP}.
6624 The following list shows only the upper-case form of each command.
6625
6626 @table @code
6627 @cindex @code{ABSOLUTE} (MRI)
6628 @item ABSOLUTE @var{secname}
6629 @itemx ABSOLUTE @var{secname}, @var{secname}, @dots{} @var{secname}
6630 Normally, @command{ld} includes in the output file all sections from all
6631 the input files. However, in an MRI-compatible script, you can use the
6632 @code{ABSOLUTE} command to restrict the sections that will be present in
6633 your output program. If the @code{ABSOLUTE} command is used at all in a
6634 script, then only the sections named explicitly in @code{ABSOLUTE}
6635 commands will appear in the linker output. You can still use other
6636 input sections (whatever you select on the command line, or using
6637 @code{LOAD}) to resolve addresses in the output file.
6638
6639 @cindex @code{ALIAS} (MRI)
6640 @item ALIAS @var{out-secname}, @var{in-secname}
6641 Use this command to place the data from input section @var{in-secname}
6642 in a section called @var{out-secname} in the linker output file.
6643
6644 @var{in-secname} may be an integer.
6645
6646 @cindex @code{ALIGN} (MRI)
6647 @item ALIGN @var{secname} = @var{expression}
6648 Align the section called @var{secname} to @var{expression}. The
6649 @var{expression} should be a power of two.
6650
6651 @cindex @code{BASE} (MRI)
6652 @item BASE @var{expression}
6653 Use the value of @var{expression} as the lowest address (other than
6654 absolute addresses) in the output file.
6655
6656 @cindex @code{CHIP} (MRI)
6657 @item CHIP @var{expression}
6658 @itemx CHIP @var{expression}, @var{expression}
6659 This command does nothing; it is accepted only for compatibility.
6660
6661 @cindex @code{END} (MRI)
6662 @item END
6663 This command does nothing whatever; it's only accepted for compatibility.
6664
6665 @cindex @code{FORMAT} (MRI)
6666 @item FORMAT @var{output-format}
6667 Similar to the @code{OUTPUT_FORMAT} command in the more general linker
6668 language, but restricted to one of these output formats:
6669
6670 @enumerate
6671 @item
6672 S-records, if @var{output-format} is @samp{S}
6673
6674 @item
6675 IEEE, if @var{output-format} is @samp{IEEE}
6676
6677 @item
6678 COFF (the @samp{coff-m68k} variant in BFD), if @var{output-format} is
6679 @samp{COFF}
6680 @end enumerate
6681
6682 @cindex @code{LIST} (MRI)
6683 @item LIST @var{anything}@dots{}
6684 Print (to the standard output file) a link map, as produced by the
6685 @command{ld} command-line option @samp{-M}.
6686
6687 The keyword @code{LIST} may be followed by anything on the
6688 same line, with no change in its effect.
6689
6690 @cindex @code{LOAD} (MRI)
6691 @item LOAD @var{filename}
6692 @itemx LOAD @var{filename}, @var{filename}, @dots{} @var{filename}
6693 Include one or more object file @var{filename} in the link; this has the
6694 same effect as specifying @var{filename} directly on the @command{ld}
6695 command line.
6696
6697 @cindex @code{NAME} (MRI)
6698 @item NAME @var{output-name}
6699 @var{output-name} is the name for the program produced by @command{ld}; the
6700 MRI-compatible command @code{NAME} is equivalent to the command-line
6701 option @samp{-o} or the general script language command @code{OUTPUT}.
6702
6703 @cindex @code{ORDER} (MRI)
6704 @item ORDER @var{secname}, @var{secname}, @dots{} @var{secname}
6705 @itemx ORDER @var{secname} @var{secname} @var{secname}
6706 Normally, @command{ld} orders the sections in its output file in the
6707 order in which they first appear in the input files. In an MRI-compatible
6708 script, you can override this ordering with the @code{ORDER} command. The
6709 sections you list with @code{ORDER} will appear first in your output
6710 file, in the order specified.
6711
6712 @cindex @code{PUBLIC} (MRI)
6713 @item PUBLIC @var{name}=@var{expression}
6714 @itemx PUBLIC @var{name},@var{expression}
6715 @itemx PUBLIC @var{name} @var{expression}
6716 Supply a value (@var{expression}) for external symbol
6717 @var{name} used in the linker input files.
6718
6719 @cindex @code{SECT} (MRI)
6720 @item SECT @var{secname}, @var{expression}
6721 @itemx SECT @var{secname}=@var{expression}
6722 @itemx SECT @var{secname} @var{expression}
6723 You can use any of these three forms of the @code{SECT} command to
6724 specify the start address (@var{expression}) for section @var{secname}.
6725 If you have more than one @code{SECT} statement for the same
6726 @var{secname}, only the @emph{first} sets the start address.
6727 @end table
6728
6729 @include fdl.texi
6730
6731 @node LD Index
6732 @unnumbered LD Index
6733
6734 @printindex cp
6735
6736 @tex
6737 % I think something like @colophon should be in texinfo. In the
6738 % meantime:
6739 \long\def\colophon{\hbox to0pt{}\vfill
6740 \centerline{The body of this manual is set in}
6741 \centerline{\fontname\tenrm,}
6742 \centerline{with headings in {\bf\fontname\tenbf}}
6743 \centerline{and examples in {\tt\fontname\tentt}.}
6744 \centerline{{\it\fontname\tenit\/} and}
6745 \centerline{{\sl\fontname\tensl\/}}
6746 \centerline{are used for emphasis.}\vfill}
6747 \page\colophon
6748 % Blame: doc@cygnus.com, 28mar91.
6749 @end tex
6750
6751
6752 @contents
6753 @bye
This page took 1.008725 seconds and 5 git commands to generate.