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