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