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