Add win32 machine depending section.
[deliverable/binutils-gdb.git] / ld / ld.texinfo
1 \input texinfo
2 @setfilename ld.info
3 @c Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
4 @c 2001, 2002 Free Software Foundation, Inc.
5 @syncodeindex ky cp
6 @include configdoc.texi
7 @c (configdoc.texi is generated by the Makefile)
8 @include ldver.texi
9
10 @c @smallbook
11
12 @macro gcctabopt{body}
13 @code{\body\}
14 @end macro
15
16 @c man begin NAME
17 @ifset man
18 @c Configure for the generation of man pages
19 @set UsesEnvVars
20 @set GENERIC
21 @set A29K
22 @set ARC
23 @set ARM
24 @set D10V
25 @set D30V
26 @set H8/300
27 @set H8/500
28 @set HPPA
29 @set I370
30 @set I80386
31 @set I860
32 @set I960
33 @set M32R
34 @set M68HC11
35 @set M680X0
36 @set MCORE
37 @set MIPS
38 @set MMIX
39 @set PDP11
40 @set PJ
41 @set SH
42 @set SPARC
43 @set C54X
44 @set V850
45 @set VAX
46 @set WIN32
47 @end ifset
48 @c man end
49
50 @ifinfo
51 @format
52 START-INFO-DIR-ENTRY
53 * Ld: (ld). The GNU linker.
54 END-INFO-DIR-ENTRY
55 @end format
56 @end ifinfo
57
58 @ifinfo
59 This file documents the @sc{gnu} linker LD version @value{VERSION}.
60
61 Copyright (C) 1991, 92, 93, 94, 95, 96, 97, 98, 99, 2000,
62 2001, 2002 Free Software Foundation, Inc.
63
64 @ignore
65
66 Permission is granted to copy, distribute and/or modify this document
67 under the terms of the GNU Free Documentation License, Version 1.1
68 or any later version published by the Free Software Foundation;
69 with no Invariant Sections, with no Front-Cover Texts, and with no
70 Back-Cover Texts. A copy of the license is included in the
71 section entitled "GNU Free Documentation License".
72
73 Permission is granted to process this file through Tex and print the
74 results, provided the printed document carries copying permission
75 notice identical to this one except for the removal of this paragraph
76 (this paragraph not being relevant to the printed manual).
77
78 @end ignore
79 @end ifinfo
80 @iftex
81 @finalout
82 @setchapternewpage odd
83 @settitle Using LD, the GNU linker
84 @titlepage
85 @title Using ld
86 @subtitle The GNU linker
87 @sp 1
88 @subtitle @code{ld} version 2
89 @subtitle Version @value{VERSION}
90 @author Steve Chamberlain
91 @author Ian Lance Taylor
92 @page
93
94 @tex
95 {\parskip=0pt
96 \hfill Red Hat Inc\par
97 \hfill nickc\@credhat.com, doc\@redhat.com\par
98 \hfill {\it Using LD, the GNU linker}\par
99 \hfill Edited by Jeffrey Osier (jeffrey\@cygnus.com)\par
100 }
101 \global\parindent=0pt % Steve likes it this way.
102 @end tex
103
104 @vskip 0pt plus 1filll
105 @c man begin COPYRIGHT
106 Copyright @copyright{} 1991, 92, 93, 94, 95, 96, 97, 98, 99, 2000, 2001 Free Software Foundation, Inc.
107
108 Permission is granted to copy, distribute and/or modify this document
109 under the terms of the GNU Free Documentation License, Version 1.1
110 or any later version published by the Free Software Foundation;
111 with no Invariant Sections, with no Front-Cover Texts, and with no
112 Back-Cover Texts. A copy of the license is included in the
113 section entitled "GNU Free Documentation License".
114 @c man end
115
116 @end titlepage
117 @end iftex
118 @c FIXME: Talk about importance of *order* of args, cmds to linker!
119
120 @ifnottex
121 @node Top
122 @top Using ld
123 This file documents the @sc{gnu} linker ld version @value{VERSION}.
124
125 This document is distributed under the terms of the GNU Free
126 Documentation License. A copy of the license is included in the
127 section entitled "GNU Free Documentation License".
128
129 @menu
130 * Overview:: Overview
131 * Invocation:: Invocation
132 * Scripts:: Linker Scripts
133 @ifset GENERIC
134 * Machine Dependent:: Machine Dependent Features
135 @end ifset
136 @ifclear GENERIC
137 @ifset H8300
138 * H8/300:: ld and the H8/300
139 @end ifset
140 @ifset Hitachi
141 * Hitachi:: ld and other Hitachi micros
142 @end ifset
143 @ifset I960
144 * i960:: ld and the Intel 960 family
145 @end ifset
146 @ifset TICOFF
147 * TI COFF:: ld and the TI COFF
148 @end ifset
149 @ifset WIN32
150 * Win32:: ld and WIN32 (cygwin/mingw)
151 @end ifset
152 @end ifclear
153 @ifclear SingleFormat
154 * BFD:: BFD
155 @end ifclear
156 @c Following blank line required for remaining bug in makeinfo conds/menus
157
158 * Reporting Bugs:: Reporting Bugs
159 * MRI:: MRI Compatible Script Files
160 * GNU Free Documentation License:: GNU Free Documentation License
161 * Index:: Index
162 @end menu
163 @end ifnottex
164
165 @node Overview
166 @chapter Overview
167
168 @cindex @sc{gnu} linker
169 @cindex what is this?
170
171 @ifset man
172 @c man begin SYNOPSIS
173 ld [@b{options}] @var{objfile} @dots{}
174 @c man end
175
176 @c man begin SEEALSO
177 ar(1), nm(1), objcopy(1), objdump(1), readelf(1) and
178 the Info entries for @file{binutils} and
179 @file{ld}.
180 @c man end
181 @end ifset
182
183 @c man begin DESCRIPTION
184
185 @command{ld} combines a number of object and archive files, relocates
186 their data and ties up symbol references. Usually the last step in
187 compiling a program is to run @command{ld}.
188
189 @command{ld} accepts Linker Command Language files written in
190 a superset of AT&T's Link Editor Command Language syntax,
191 to provide explicit and total control over the linking process.
192
193 @ifset man
194 @c For the man only
195 This man page does not describe the command language; see the
196 @command{ld} entry in @code{info}, or the manual
197 ld: the GNU linker, for full details on the command language and
198 on other aspects of the GNU linker.
199 @end ifset
200
201 @ifclear SingleFormat
202 This version of @command{ld} uses the general purpose BFD libraries
203 to operate on object files. This allows @command{ld} to read, combine, and
204 write object files in many different formats---for example, COFF or
205 @code{a.out}. Different formats may be linked together to produce any
206 available kind of object file. @xref{BFD}, for more information.
207 @end ifclear
208
209 Aside from its flexibility, the @sc{gnu} linker is more helpful than other
210 linkers in providing diagnostic information. Many linkers abandon
211 execution immediately upon encountering an error; whenever possible,
212 @command{ld} continues executing, allowing you to identify other errors
213 (or, in some cases, to get an output file in spite of the error).
214
215 @c man end
216
217 @node Invocation
218 @chapter Invocation
219
220 @c man begin DESCRIPTION
221
222 The @sc{gnu} linker @command{ld} is meant to cover a broad range of situations,
223 and to be as compatible as possible with other linkers. As a result,
224 you have many choices to control its behavior.
225
226 @c man end
227
228 @ifset UsesEnvVars
229 @menu
230 * Options:: Command Line Options
231 * Environment:: Environment Variables
232 @end menu
233
234 @node Options
235 @section Command Line Options
236 @end ifset
237
238 @cindex command line
239 @cindex options
240
241 @c man begin OPTIONS
242
243 The linker supports a plethora of command-line options, but in actual
244 practice few of them are used in any particular context.
245 @cindex standard Unix system
246 For instance, a frequent use of @command{ld} is to link standard Unix
247 object files on a standard, supported Unix system. On such a system, to
248 link a file @code{hello.o}:
249
250 @smallexample
251 ld -o @var{output} /lib/crt0.o hello.o -lc
252 @end smallexample
253
254 This tells @command{ld} to produce a file called @var{output} as the
255 result of linking the file @code{/lib/crt0.o} with @code{hello.o} and
256 the library @code{libc.a}, which will come from the standard search
257 directories. (See the discussion of the @samp{-l} option below.)
258
259 Some of the command-line options to @command{ld} may be specified at any
260 point in the command line. However, options which refer to files, such
261 as @samp{-l} or @samp{-T}, cause the file to be read at the point at
262 which the option appears in the command line, relative to the object
263 files and other file options. Repeating non-file options with a
264 different argument will either have no further effect, or override prior
265 occurrences (those further to the left on the command line) of that
266 option. Options which may be meaningfully specified more than once are
267 noted in the descriptions below.
268
269 @cindex object files
270 Non-option arguments are object files or archives which are to be linked
271 together. They may follow, precede, or be mixed in with command-line
272 options, except that an object file argument may not be placed between
273 an option and its argument.
274
275 Usually the linker is invoked with at least one object file, but you can
276 specify other forms of binary input files using @samp{-l}, @samp{-R},
277 and the script command language. If @emph{no} binary input files at all
278 are specified, the linker does not produce any output, and issues the
279 message @samp{No input files}.
280
281 If the linker can not recognize the format of an object file, it will
282 assume that it is a linker script. A script specified in this way
283 augments the main linker script used for the link (either the default
284 linker script or the one specified by using @samp{-T}). This feature
285 permits the linker to link against a file which appears to be an object
286 or an archive, but actually merely defines some symbol values, or uses
287 @code{INPUT} or @code{GROUP} to load other objects. Note that
288 specifying a script in this way merely augments the main linker script;
289 use the @samp{-T} option to replace the default linker script entirely.
290 @xref{Scripts}.
291
292 For options whose names are a single letter,
293 option arguments must either follow the option letter without intervening
294 whitespace, or be given as separate arguments immediately following the
295 option that requires them.
296
297 For options whose names are multiple letters, either one dash or two can
298 precede the option name; for example, @samp{-trace-symbol} and
299 @samp{--trace-symbol} are equivalent. Note - there is one exception to
300 this rule. Multiple letter options that start with a lower case 'o' can
301 only be preceeded by two dashes. This is to reduce confusion with the
302 @samp{-o} option. So for example @samp{-omagic} sets the output file
303 name to @samp{magic} whereas @samp{--omagic} sets the NMAGIC flag on the
304 output.
305
306 Arguments to multiple-letter options must either be separated from the
307 option name by an equals sign, or be given as separate arguments
308 immediately following the option that requires them. For example,
309 @samp{--trace-symbol foo} and @samp{--trace-symbol=foo} are equivalent.
310 Unique abbreviations of the names of multiple-letter options are
311 accepted.
312
313 Note - if the linker is being invoked indirectly, via a compiler driver
314 (eg @samp{gcc}) then all the linker command line options should be
315 prefixed by @samp{-Wl,} (or whatever is appropriate for the particular
316 compiler driver) like this:
317
318 @smallexample
319 gcc -Wl,--startgroup foo.o bar.o -Wl,--endgroup
320 @end smallexample
321
322 This is important, because otherwise the compiler driver program may
323 silently drop the linker options, resulting in a bad link.
324
325 Here is a table of the generic command line switches accepted by the GNU
326 linker:
327
328 @table @gcctabopt
329 @kindex -a@var{keyword}
330 @item -a@var{keyword}
331 This option is supported for HP/UX compatibility. The @var{keyword}
332 argument must be one of the strings @samp{archive}, @samp{shared}, or
333 @samp{default}. @samp{-aarchive} is functionally equivalent to
334 @samp{-Bstatic}, and the other two keywords are functionally equivalent
335 to @samp{-Bdynamic}. This option may be used any number of times.
336
337 @ifset I960
338 @cindex architectures
339 @kindex -A@var{arch}
340 @item -A@var{architecture}
341 @kindex --architecture=@var{arch}
342 @itemx --architecture=@var{architecture}
343 In the current release of @command{ld}, this option is useful only for the
344 Intel 960 family of architectures. In that @command{ld} configuration, the
345 @var{architecture} argument identifies the particular architecture in
346 the 960 family, enabling some safeguards and modifying the
347 archive-library search path. @xref{i960,,@command{ld} and the Intel 960
348 family}, for details.
349
350 Future releases of @command{ld} may support similar functionality for
351 other architecture families.
352 @end ifset
353
354 @ifclear SingleFormat
355 @cindex binary input format
356 @kindex -b @var{format}
357 @kindex --format=@var{format}
358 @cindex input format
359 @cindex input format
360 @item -b @var{input-format}
361 @itemx --format=@var{input-format}
362 @command{ld} may be configured to support more than one kind of object
363 file. If your @command{ld} is configured this way, you can use the
364 @samp{-b} option to specify the binary format for input object files
365 that follow this option on the command line. Even when @command{ld} is
366 configured to support alternative object formats, you don't usually need
367 to specify this, as @command{ld} should be configured to expect as a
368 default input format the most usual format on each machine.
369 @var{input-format} is a text string, the name of a particular format
370 supported by the BFD libraries. (You can list the available binary
371 formats with @samp{objdump -i}.)
372 @xref{BFD}.
373
374 You may want to use this option if you are linking files with an unusual
375 binary format. You can also use @samp{-b} to switch formats explicitly (when
376 linking object files of different formats), by including
377 @samp{-b @var{input-format}} before each group of object files in a
378 particular format.
379
380 The default format is taken from the environment variable
381 @code{GNUTARGET}.
382 @ifset UsesEnvVars
383 @xref{Environment}.
384 @end ifset
385 You can also define the input format from a script, using the command
386 @code{TARGET};
387 @ifclear man
388 see @ref{Format Commands}.
389 @end ifclear
390 @end ifclear
391
392 @kindex -c @var{MRI-cmdfile}
393 @kindex --mri-script=@var{MRI-cmdfile}
394 @cindex compatibility, MRI
395 @item -c @var{MRI-commandfile}
396 @itemx --mri-script=@var{MRI-commandfile}
397 For compatibility with linkers produced by MRI, @command{ld} accepts script
398 files written in an alternate, restricted command language, described in
399 @ifclear man
400 @ref{MRI,,MRI Compatible Script Files}.
401 @end ifclear
402 @ifset man
403 the MRI Compatible Script Files section of GNU ld documentation.
404 @end ifset
405 Introduce MRI script files with
406 the option @samp{-c}; use the @samp{-T} option to run linker
407 scripts written in the general-purpose @command{ld} scripting language.
408 If @var{MRI-cmdfile} does not exist, @command{ld} looks for it in the directories
409 specified by any @samp{-L} options.
410
411 @cindex common allocation
412 @kindex -d
413 @kindex -dc
414 @kindex -dp
415 @item -d
416 @itemx -dc
417 @itemx -dp
418 These three options are equivalent; multiple forms are supported for
419 compatibility with other linkers. They assign space to common symbols
420 even if a relocatable output file is specified (with @samp{-r}). The
421 script command @code{FORCE_COMMON_ALLOCATION} has the same effect.
422 @xref{Miscellaneous Commands}.
423
424 @cindex entry point, from command line
425 @kindex -e @var{entry}
426 @kindex --entry=@var{entry}
427 @item -e @var{entry}
428 @itemx --entry=@var{entry}
429 Use @var{entry} as the explicit symbol for beginning execution of your
430 program, rather than the default entry point. If there is no symbol
431 named @var{entry}, the linker will try to parse @var{entry} as a number,
432 and use that as the entry address (the number will be interpreted in
433 base 10; you may use a leading @samp{0x} for base 16, or a leading
434 @samp{0} for base 8). @xref{Entry Point}, for a discussion of defaults
435 and other ways of specifying the entry point.
436
437 @cindex dynamic symbol table
438 @kindex -E
439 @kindex --export-dynamic
440 @item -E
441 @itemx --export-dynamic
442 When creating a dynamically linked executable, add all symbols to the
443 dynamic symbol table. The dynamic symbol table is the set of symbols
444 which are visible from dynamic objects at run time.
445
446 If you do not use this option, the dynamic symbol table will normally
447 contain only those symbols which are referenced by some dynamic object
448 mentioned in the link.
449
450 If you use @code{dlopen} to load a dynamic object which needs to refer
451 back to the symbols defined by the program, rather than some other
452 dynamic object, then you will probably need to use this option when
453 linking the program itself.
454
455 You can also use the version script to control what symbols should
456 be added to the dynamic symbol table if the output format supports it.
457 See the description of @samp{--version-script} in @ref{VERSION}.
458
459 @cindex big-endian objects
460 @cindex endianness
461 @kindex -EB
462 @item -EB
463 Link big-endian objects. This affects the default output format.
464
465 @cindex little-endian objects
466 @kindex -EL
467 @item -EL
468 Link little-endian objects. This affects the default output format.
469
470 @kindex -f
471 @kindex --auxiliary
472 @item -f
473 @itemx --auxiliary @var{name}
474 When creating an ELF shared object, set the internal DT_AUXILIARY field
475 to the specified name. This tells the dynamic linker that the symbol
476 table of the shared object should be used as an auxiliary filter on the
477 symbol table of the shared object @var{name}.
478
479 If you later link a program against this filter object, then, when you
480 run the program, the dynamic linker will see the DT_AUXILIARY field. If
481 the dynamic linker resolves any symbols from the filter object, it will
482 first check whether there is a definition in the shared object
483 @var{name}. If there is one, it will be used instead of the definition
484 in the filter object. The shared object @var{name} need not exist.
485 Thus the shared object @var{name} may be used to provide an alternative
486 implementation of certain functions, perhaps for debugging or for
487 machine specific performance.
488
489 This option may be specified more than once. The DT_AUXILIARY entries
490 will be created in the order in which they appear on the command line.
491
492 @kindex -F
493 @kindex --filter
494 @item -F @var{name}
495 @itemx --filter @var{name}
496 When creating an ELF shared object, set the internal DT_FILTER field to
497 the specified name. This tells the dynamic linker that the symbol table
498 of the shared object which is being created should be used as a filter
499 on the symbol table of the shared object @var{name}.
500
501 If you later link a program against this filter object, then, when you
502 run the program, the dynamic linker will see the DT_FILTER field. The
503 dynamic linker will resolve symbols according to the symbol table of the
504 filter object as usual, but it will actually link to the definitions
505 found in the shared object @var{name}. Thus the filter object can be
506 used to select a subset of the symbols provided by the object
507 @var{name}.
508
509 Some older linkers used the @option{-F} option throughout a compilation
510 toolchain for specifying object-file format for both input and output
511 object files. The @sc{gnu} linker uses other mechanisms for this
512 purpose: the @option{-b}, @option{--format}, @option{--oformat} options, the
513 @code{TARGET} command in linker scripts, and the @code{GNUTARGET}
514 environment variable. The @sc{gnu} linker will ignore the @option{-F}
515 option when not creating an ELF shared object.
516
517 @cindex finalization function
518 @kindex -fini
519 @item -fini @var{name}
520 When creating an ELF executable or shared object, call NAME when the
521 executable or shared object is unloaded, by setting DT_FINI to the
522 address of the function. By default, the linker uses @code{_fini} as
523 the function to call.
524
525 @kindex -g
526 @item -g
527 Ignored. Provided for compatibility with other tools.
528
529 @kindex -G
530 @kindex --gpsize
531 @cindex object size
532 @item -G@var{value}
533 @itemx --gpsize=@var{value}
534 Set the maximum size of objects to be optimized using the GP register to
535 @var{size}. This is only meaningful for object file formats such as
536 MIPS ECOFF which supports putting large and small objects into different
537 sections. This is ignored for other object file formats.
538
539 @cindex runtime library name
540 @kindex -h@var{name}
541 @kindex -soname=@var{name}
542 @item -h@var{name}
543 @itemx -soname=@var{name}
544 When creating an ELF shared object, set the internal DT_SONAME field to
545 the specified name. When an executable is linked with a shared object
546 which has a DT_SONAME field, then when the executable is run the dynamic
547 linker will attempt to load the shared object specified by the DT_SONAME
548 field rather than the using the file name given to the linker.
549
550 @kindex -i
551 @cindex incremental link
552 @item -i
553 Perform an incremental link (same as option @samp{-r}).
554
555 @cindex initialization function
556 @kindex -init
557 @item -init @var{name}
558 When creating an ELF executable or shared object, call NAME when the
559 executable or shared object is loaded, by setting DT_INIT to the address
560 of the function. By default, the linker uses @code{_init} as the
561 function to call.
562
563 @cindex archive files, from cmd line
564 @kindex -l@var{archive}
565 @kindex --library=@var{archive}
566 @item -l@var{archive}
567 @itemx --library=@var{archive}
568 Add archive file @var{archive} to the list of files to link. This
569 option may be used any number of times. @command{ld} will search its
570 path-list for occurrences of @code{lib@var{archive}.a} for every
571 @var{archive} specified.
572
573 On systems which support shared libraries, @command{ld} may also search for
574 libraries with extensions other than @code{.a}. Specifically, on ELF
575 and SunOS systems, @command{ld} will search a directory for a library with
576 an extension of @code{.so} before searching for one with an extension of
577 @code{.a}. By convention, a @code{.so} extension indicates a shared
578 library.
579
580 The linker will search an archive only once, at the location where it is
581 specified on the command line. If the archive defines a symbol which
582 was undefined in some object which appeared before the archive on the
583 command line, the linker will include the appropriate file(s) from the
584 archive. However, an undefined symbol in an object appearing later on
585 the command line will not cause the linker to search the archive again.
586
587 See the @option{-(} option for a way to force the linker to search
588 archives multiple times.
589
590 You may list the same archive multiple times on the command line.
591
592 @ifset GENERIC
593 This type of archive searching is standard for Unix linkers. However,
594 if you are using @command{ld} on AIX, note that it is different from the
595 behaviour of the AIX linker.
596 @end ifset
597
598 @cindex search directory, from cmd line
599 @kindex -L@var{dir}
600 @kindex --library-path=@var{dir}
601 @item -L@var{searchdir}
602 @itemx --library-path=@var{searchdir}
603 Add path @var{searchdir} to the list of paths that @command{ld} will search
604 for archive libraries and @command{ld} control scripts. You may use this
605 option any number of times. The directories are searched in the order
606 in which they are specified on the command line. Directories specified
607 on the command line are searched before the default directories. All
608 @option{-L} options apply to all @option{-l} options, regardless of the
609 order in which the options appear.
610
611 @ifset UsesEnvVars
612 The default set of paths searched (without being specified with
613 @samp{-L}) depends on which emulation mode @command{ld} is using, and in
614 some cases also on how it was configured. @xref{Environment}.
615 @end ifset
616
617 The paths can also be specified in a link script with the
618 @code{SEARCH_DIR} command. Directories specified this way are searched
619 at the point in which the linker script appears in the command line.
620
621 @cindex emulation
622 @kindex -m @var{emulation}
623 @item -m@var{emulation}
624 Emulate the @var{emulation} linker. You can list the available
625 emulations with the @samp{--verbose} or @samp{-V} options.
626
627 If the @samp{-m} option is not used, the emulation is taken from the
628 @code{LDEMULATION} environment variable, if that is defined.
629
630 Otherwise, the default emulation depends upon how the linker was
631 configured.
632
633 @cindex link map
634 @kindex -M
635 @kindex --print-map
636 @item -M
637 @itemx --print-map
638 Print a link map to the standard output. A link map provides
639 information about the link, including the following:
640
641 @itemize @bullet
642 @item
643 Where object files and symbols are mapped into memory.
644 @item
645 How common symbols are allocated.
646 @item
647 All archive members included in the link, with a mention of the symbol
648 which caused the archive member to be brought in.
649 @end itemize
650
651 @kindex -n
652 @cindex read-only text
653 @cindex NMAGIC
654 @kindex --nmagic
655 @item -n
656 @itemx --nmagic
657 Turn off page alignment of sections, and mark the output as
658 @code{NMAGIC} if possible.
659
660 @kindex -N
661 @kindex --omagic
662 @cindex read/write from cmd line
663 @cindex OMAGIC
664 @item -N
665 @itemx --omagic
666 Set the text and data sections to be readable and writable. Also, do
667 not page-align the data segment, and disable linking against shared
668 libraries. If the output format supports Unix style magic numbers,
669 mark the output as @code{OMAGIC}.
670
671 @kindex --no-omagic
672 @cindex OMAGIC
673 @item --no-omagic
674 This option negates most of the effects of the @option{-N} option. It
675 sets the text section to be read-only, and forces the data segment to
676 be page-aligned. Note - this option does not enable linking against
677 shared libraries. Use @option{-Bdynamic} for this.
678
679 @kindex -o @var{output}
680 @kindex --output=@var{output}
681 @cindex naming the output file
682 @item -o @var{output}
683 @itemx --output=@var{output}
684 Use @var{output} as the name for the program produced by @command{ld}; if this
685 option is not specified, the name @file{a.out} is used by default. The
686 script command @code{OUTPUT} can also specify the output file name.
687
688 @kindex -O @var{level}
689 @cindex generating optimized output
690 @item -O @var{level}
691 If @var{level} is a numeric values greater than zero @command{ld} optimizes
692 the output. This might take significantly longer and therefore probably
693 should only be enabled for the final binary.
694
695 @kindex -q
696 @kindex --emit-relocs
697 @cindex retain relocations in final executable
698 @item -q
699 @itemx --emit-relocs
700 Leave relocation sections and contents in fully linked exececutables.
701 Post link analysis and optimization tools may need this information in
702 order to perform correct modifications of executables. This results
703 in larger executables.
704
705 This option is currently only supported on ELF platforms.
706
707 @cindex partial link
708 @cindex relocatable output
709 @kindex -r
710 @kindex --relocateable
711 @item -r
712 @itemx --relocateable
713 Generate relocatable output---i.e., generate an output file that can in
714 turn serve as input to @command{ld}. This is often called @dfn{partial
715 linking}. As a side effect, in environments that support standard Unix
716 magic numbers, this option also sets the output file's magic number to
717 @code{OMAGIC}.
718 @c ; see @option{-N}.
719 If this option is not specified, an absolute file is produced. When
720 linking C++ programs, this option @emph{will not} resolve references to
721 constructors; to do that, use @samp{-Ur}.
722
723 When an input file does not have the same format as the output file,
724 partial linking is only supported if that input file does not contain any
725 relocations. Different output formats can have further restrictions; for
726 example some @code{a.out}-based formats do not support partial linking
727 with input files in other formats at all.
728
729 This option does the same thing as @samp{-i}.
730
731 @kindex -R @var{file}
732 @kindex --just-symbols=@var{file}
733 @cindex symbol-only input
734 @item -R @var{filename}
735 @itemx --just-symbols=@var{filename}
736 Read symbol names and their addresses from @var{filename}, but do not
737 relocate it or include it in the output. This allows your output file
738 to refer symbolically to absolute locations of memory defined in other
739 programs. You may use this option more than once.
740
741 For compatibility with other ELF linkers, if the @option{-R} option is
742 followed by a directory name, rather than a file name, it is treated as
743 the @option{-rpath} option.
744
745 @kindex -s
746 @kindex --strip-all
747 @cindex strip all symbols
748 @item -s
749 @itemx --strip-all
750 Omit all symbol information from the output file.
751
752 @kindex -S
753 @kindex --strip-debug
754 @cindex strip debugger symbols
755 @item -S
756 @itemx --strip-debug
757 Omit debugger symbol information (but not all symbols) from the output file.
758
759 @kindex -t
760 @kindex --trace
761 @cindex input files, displaying
762 @item -t
763 @itemx --trace
764 Print the names of the input files as @command{ld} processes them.
765
766 @kindex -T @var{script}
767 @kindex --script=@var{script}
768 @cindex script files
769 @item -T @var{scriptfile}
770 @itemx --script=@var{scriptfile}
771 Use @var{scriptfile} as the linker script. This script replaces
772 @command{ld}'s default linker script (rather than adding to it), so
773 @var{commandfile} must specify everything necessary to describe the
774 output file. @xref{Scripts}. If @var{scriptfile} does not exist in
775 the current directory, @code{ld} looks for it in the directories
776 specified by any preceding @samp{-L} options. Multiple @samp{-T}
777 options accumulate.
778
779 @kindex -u @var{symbol}
780 @kindex --undefined=@var{symbol}
781 @cindex undefined symbol
782 @item -u @var{symbol}
783 @itemx --undefined=@var{symbol}
784 Force @var{symbol} to be entered in the output file as an undefined
785 symbol. Doing this may, for example, trigger linking of additional
786 modules from standard libraries. @samp{-u} may be repeated with
787 different option arguments to enter additional undefined symbols. This
788 option is equivalent to the @code{EXTERN} linker script command.
789
790 @kindex -Ur
791 @cindex constructors
792 @item -Ur
793 For anything other than C++ programs, this option is equivalent to
794 @samp{-r}: it generates relocatable output---i.e., an output file that can in
795 turn serve as input to @command{ld}. When linking C++ programs, @samp{-Ur}
796 @emph{does} resolve references to constructors, unlike @samp{-r}.
797 It does not work to use @samp{-Ur} on files that were themselves linked
798 with @samp{-Ur}; once the constructor table has been built, it cannot
799 be added to. Use @samp{-Ur} only for the last partial link, and
800 @samp{-r} for the others.
801
802 @kindex --unique[=@var{SECTION}]
803 @item --unique[=@var{SECTION}]
804 Creates a separate output section for every input section matching
805 @var{SECTION}, or if the optional wildcard @var{SECTION} argument is
806 missing, for every orphan input section. An orphan section is one not
807 specifically mentioned in a linker script. You may use this option
808 multiple times on the command line; It prevents the normal merging of
809 input sections with the same name, overriding output section assignments
810 in a linker script.
811
812 @kindex -v
813 @kindex -V
814 @kindex --version
815 @cindex version
816 @item -v
817 @itemx --version
818 @itemx -V
819 Display the version number for @command{ld}. The @option{-V} option also
820 lists the supported emulations.
821
822 @kindex -x
823 @kindex --discard-all
824 @cindex deleting local symbols
825 @item -x
826 @itemx --discard-all
827 Delete all local symbols.
828
829 @kindex -X
830 @kindex --discard-locals
831 @cindex local symbols, deleting
832 @cindex L, deleting symbols beginning
833 @item -X
834 @itemx --discard-locals
835 Delete all temporary local symbols. For most targets, this is all local
836 symbols whose names begin with @samp{L}.
837
838 @kindex -y @var{symbol}
839 @kindex --trace-symbol=@var{symbol}
840 @cindex symbol tracing
841 @item -y @var{symbol}
842 @itemx --trace-symbol=@var{symbol}
843 Print the name of each linked file in which @var{symbol} appears. This
844 option may be given any number of times. On many systems it is necessary
845 to prepend an underscore.
846
847 This option is useful when you have an undefined symbol in your link but
848 don't know where the reference is coming from.
849
850 @kindex -Y @var{path}
851 @item -Y @var{path}
852 Add @var{path} to the default library search path. This option exists
853 for Solaris compatibility.
854
855 @kindex -z @var{keyword}
856 @item -z @var{keyword}
857 The recognized keywords are @code{initfirst}, @code{interpose},
858 @code{loadfltr}, @code{nodefaultlib}, @code{nodelete}, @code{nodlopen},
859 @code{nodump}, @code{now}, @code{origin}, @code{combreloc}, @code{nocombreloc}
860 and @code{nocopyreloc}.
861 The other keywords are
862 ignored for Solaris compatibility. @code{initfirst} marks the object
863 to be initialized first at runtime before any other objects.
864 @code{interpose} marks the object that its symbol table interposes
865 before all symbols but the primary executable. @code{loadfltr} marks
866 the object that its filtees be processed immediately at runtime.
867 @code{nodefaultlib} marks the object that the search for dependencies
868 of this object will ignore any default library search paths.
869 @code{nodelete} marks the object shouldn't be unloaded at runtime.
870 @code{nodlopen} marks the object not available to @code{dlopen}.
871 @code{nodump} marks the object can not be dumped by @code{dldump}.
872 @code{now} marks the object with the non-lazy runtime binding.
873 @code{origin} marks the object may contain $ORIGIN.
874 @code{defs} disallows undefined symbols.
875 @code{muldefs} allows multiple definitions.
876 @code{combreloc} combines multiple reloc sections and sorts them
877 to make dynamic symbol lookup caching possible.
878 @code{nocombreloc} disables multiple reloc sections combining.
879 @code{nocopyreloc} disables production of copy relocs.
880
881 @kindex -(
882 @cindex groups of archives
883 @item -( @var{archives} -)
884 @itemx --start-group @var{archives} --end-group
885 The @var{archives} should be a list of archive files. They may be
886 either explicit file names, or @samp{-l} options.
887
888 The specified archives are searched repeatedly until no new undefined
889 references are created. Normally, an archive is searched only once in
890 the order that it is specified on the command line. If a symbol in that
891 archive is needed to resolve an undefined symbol referred to by an
892 object in an archive that appears later on the command line, the linker
893 would not be able to resolve that reference. By grouping the archives,
894 they all be searched repeatedly until all possible references are
895 resolved.
896
897 Using this option has a significant performance cost. It is best to use
898 it only when there are unavoidable circular references between two or
899 more archives.
900
901 @kindex --accept-unknown-input-format
902 @kindex --no-accept-unknown-input-format
903 @item --accept-unknown-input-format
904 @itemx --no-accept-unknown-input-format
905 Tells the linker to accept input files whoes format cannot be
906 recognised. The assumption is that the user knows what they are doing
907 and deliberately wants to link in these unknown format input files.
908 This was the default behaviour of the linker, before release 2.14.
909 The default behaviour from release 2.14 onwards is to reject such
910 input files, and so the @samp{--accept-unknown-input-format} option
911 has been added to restore the old behaviour.
912
913 @kindex -assert @var{keyword}
914 @item -assert @var{keyword}
915 This option is ignored for SunOS compatibility.
916
917 @kindex -Bdynamic
918 @kindex -dy
919 @kindex -call_shared
920 @item -Bdynamic
921 @itemx -dy
922 @itemx -call_shared
923 Link against dynamic libraries. This is only meaningful on platforms
924 for which shared libraries are supported. This option is normally the
925 default on such platforms. The different variants of this option are
926 for compatibility with various systems. You may use this option
927 multiple times on the command line: it affects library searching for
928 @option{-l} options which follow it.
929
930 @kindex -Bgroup
931 @item -Bgroup
932 Set the @code{DF_1_GROUP} flag in the @code{DT_FLAGS_1} entry in the dynamic
933 section. This causes the runtime linker to handle lookups in this
934 object and its dependencies to be performed only inside the group.
935 @option{--no-undefined} is implied. This option is only meaningful on ELF
936 platforms which support shared libraries.
937
938 @kindex -Bstatic
939 @kindex -dn
940 @kindex -non_shared
941 @kindex -static
942 @item -Bstatic
943 @itemx -dn
944 @itemx -non_shared
945 @itemx -static
946 Do not link against shared libraries. This is only meaningful on
947 platforms for which shared libraries are supported. The different
948 variants of this option are for compatibility with various systems. You
949 may use this option multiple times on the command line: it affects
950 library searching for @option{-l} options which follow it.
951
952 @kindex -Bsymbolic
953 @item -Bsymbolic
954 When creating a shared library, bind references to global symbols to the
955 definition within the shared library, if any. Normally, it is possible
956 for a program linked against a shared library to override the definition
957 within the shared library. This option is only meaningful on ELF
958 platforms which support shared libraries.
959
960 @kindex --check-sections
961 @kindex --no-check-sections
962 @item --check-sections
963 @itemx --no-check-sections
964 Asks the linker @emph{not} to check section addresses after they have
965 been assigned to see if there any overlaps. Normally the linker will
966 perform this check, and if it finds any overlaps it will produce
967 suitable error messages. The linker does know about, and does make
968 allowances for sections in overlays. The default behaviour can be
969 restored by using the command line switch @samp{--check-sections}.
970
971 @cindex cross reference table
972 @kindex --cref
973 @item --cref
974 Output a cross reference table. If a linker map file is being
975 generated, the cross reference table is printed to the map file.
976 Otherwise, it is printed on the standard output.
977
978 The format of the table is intentionally simple, so that it may be
979 easily processed by a script if necessary. The symbols are printed out,
980 sorted by name. For each symbol, a list of file names is given. If the
981 symbol is defined, the first file listed is the location of the
982 definition. The remaining files contain references to the symbol.
983
984 @cindex common allocation
985 @kindex --no-define-common
986 @item --no-define-common
987 This option inhibits the assignment of addresses to common symbols.
988 The script command @code{INHIBIT_COMMON_ALLOCATION} has the same effect.
989 @xref{Miscellaneous Commands}.
990
991 The @samp{--no-define-common} option allows decoupling
992 the decision to assign addresses to Common symbols from the choice
993 of the output file type; otherwise a non-Relocatable output type
994 forces assigning addresses to Common symbols.
995 Using @samp{--no-define-common} allows Common symbols that are referenced
996 from a shared library to be assigned addresses only in the main program.
997 This eliminates the unused duplicate space in the shared library,
998 and also prevents any possible confusion over resolving to the wrong
999 duplicate when there are many dynamic modules with specialized search
1000 paths for runtime symbol resolution.
1001
1002 @cindex symbols, from command line
1003 @kindex --defsym @var{symbol}=@var{exp}
1004 @item --defsym @var{symbol}=@var{expression}
1005 Create a global symbol in the output file, containing the absolute
1006 address given by @var{expression}. You may use this option as many
1007 times as necessary to define multiple symbols in the command line. A
1008 limited form of arithmetic is supported for the @var{expression} in this
1009 context: you may give a hexadecimal constant or the name of an existing
1010 symbol, or use @code{+} and @code{-} to add or subtract hexadecimal
1011 constants or symbols. If you need more elaborate expressions, consider
1012 using the linker command language from a script (@pxref{Assignments,,
1013 Assignment: Symbol Definitions}). @emph{Note:} there should be no white
1014 space between @var{symbol}, the equals sign (``@key{=}''), and
1015 @var{expression}.
1016
1017 @cindex demangling, from command line
1018 @kindex --demangle[=@var{style}]
1019 @kindex --no-demangle
1020 @item --demangle[=@var{style}]
1021 @itemx --no-demangle
1022 These options control whether to demangle symbol names in error messages
1023 and other output. When the linker is told to demangle, it tries to
1024 present symbol names in a readable fashion: it strips leading
1025 underscores if they are used by the object file format, and converts C++
1026 mangled symbol names into user readable names. Different compilers have
1027 different mangling styles. The optional demangling style argument can be used
1028 to choose an appropriate demangling style for your compiler. The linker will
1029 demangle by default unless the environment variable @samp{COLLECT_NO_DEMANGLE}
1030 is set. These options may be used to override the default.
1031
1032 @cindex dynamic linker, from command line
1033 @kindex -I@var{file}
1034 @kindex --dynamic-linker @var{file}
1035 @item --dynamic-linker @var{file}
1036 Set the name of the dynamic linker. This is only meaningful when
1037 generating dynamically linked ELF executables. The default dynamic
1038 linker is normally correct; don't use this unless you know what you are
1039 doing.
1040
1041 @cindex MIPS embedded PIC code
1042 @kindex --embedded-relocs
1043 @item --embedded-relocs
1044 This option is only meaningful when linking MIPS embedded PIC code,
1045 generated by the -membedded-pic option to the @sc{gnu} compiler and
1046 assembler. It causes the linker to create a table which may be used at
1047 runtime to relocate any data which was statically initialized to pointer
1048 values. See the code in testsuite/ld-empic for details.
1049
1050
1051 @kindex --fatal-warnings
1052 @item --fatal-warnings
1053 Treat all warnings as errors.
1054
1055 @kindex --force-exe-suffix
1056 @item --force-exe-suffix
1057 Make sure that an output file has a .exe suffix.
1058
1059 If a successfully built fully linked output file does not have a
1060 @code{.exe} or @code{.dll} suffix, this option forces the linker to copy
1061 the output file to one of the same name with a @code{.exe} suffix. This
1062 option is useful when using unmodified Unix makefiles on a Microsoft
1063 Windows host, since some versions of Windows won't run an image unless
1064 it ends in a @code{.exe} suffix.
1065
1066 @kindex --gc-sections
1067 @kindex --no-gc-sections
1068 @cindex garbage collection
1069 @item --no-gc-sections
1070 @itemx --gc-sections
1071 Enable garbage collection of unused input sections. It is ignored on
1072 targets that do not support this option. This option is not compatible
1073 with @samp{-r}, nor should it be used with dynamic linking. The default
1074 behaviour (of not performing this garbage collection) can be restored by
1075 specifying @samp{--no-gc-sections} on the command line.
1076
1077 @cindex help
1078 @cindex usage
1079 @kindex --help
1080 @item --help
1081 Print a summary of the command-line options on the standard output and exit.
1082
1083 @kindex --target-help
1084 @item --target-help
1085 Print a summary of all target specific options on the standard output and exit.
1086
1087 @kindex -Map
1088 @item -Map @var{mapfile}
1089 Print a link map to the file @var{mapfile}. See the description of the
1090 @samp{-M} option, above.
1091
1092 @cindex memory usage
1093 @kindex --no-keep-memory
1094 @item --no-keep-memory
1095 @command{ld} normally optimizes for speed over memory usage by caching the
1096 symbol tables of input files in memory. This option tells @command{ld} to
1097 instead optimize for memory usage, by rereading the symbol tables as
1098 necessary. This may be required if @command{ld} runs out of memory space
1099 while linking a large executable.
1100
1101 @kindex --no-undefined
1102 @kindex -z defs
1103 @item --no-undefined
1104 @itemx -z defs
1105 Normally when creating a non-symbolic shared library, undefined symbols
1106 are allowed and left to be resolved by the runtime loader. These options
1107 disallows such undefined symbols.
1108
1109 @kindex --allow-multiple-definition
1110 @kindex -z muldefs
1111 @item --allow-multiple-definition
1112 @itemx -z muldefs
1113 Normally when a symbol is defined multiple times, the linker will
1114 report a fatal error. These options allow multiple definitions and the
1115 first definition will be used.
1116
1117 @kindex --allow-shlib-undefined
1118 @item --allow-shlib-undefined
1119 Allow undefined symbols in shared objects even when --no-undefined is
1120 set. The net result will be that undefined symbols in regular objects
1121 will still trigger an error, but undefined symbols in shared objects
1122 will be ignored. The implementation of no_undefined makes the
1123 assumption that the runtime linker will choke on undefined symbols.
1124 However there is at least one system (BeOS) where undefined symbols in
1125 shared libraries is normal since the kernel patches them at load time to
1126 select which function is most appropriate for the current architecture.
1127 I.E. dynamically select an appropriate memset function. Apparently it
1128 is also normal for HPPA shared libraries to have undefined symbols.
1129
1130 @kindex --no-undefined-version
1131 @item --no-undefined-version
1132 Normally when a symbol has an undefined version, the linker will ignore
1133 it. This option disallows symbols with undefined version and a fatal error
1134 will be issued instead.
1135
1136 @kindex --no-warn-mismatch
1137 @item --no-warn-mismatch
1138 Normally @command{ld} will give an error if you try to link together input
1139 files that are mismatched for some reason, perhaps because they have
1140 been compiled for different processors or for different endiannesses.
1141 This option tells @command{ld} that it should silently permit such possible
1142 errors. This option should only be used with care, in cases when you
1143 have taken some special action that ensures that the linker errors are
1144 inappropriate.
1145
1146 @kindex --no-whole-archive
1147 @item --no-whole-archive
1148 Turn off the effect of the @option{--whole-archive} option for subsequent
1149 archive files.
1150
1151 @cindex output file after errors
1152 @kindex --noinhibit-exec
1153 @item --noinhibit-exec
1154 Retain the executable output file whenever it is still usable.
1155 Normally, the linker will not produce an output file if it encounters
1156 errors during the link process; it exits without writing an output file
1157 when it issues any error whatsoever.
1158
1159 @kindex -nostdlib
1160 @item -nostdlib
1161 Only search library directories explicitly specified on the
1162 command line. Library directories specified in linker scripts
1163 (including linker scripts specified on the command line) are ignored.
1164
1165 @ifclear SingleFormat
1166 @kindex --oformat
1167 @item --oformat @var{output-format}
1168 @command{ld} may be configured to support more than one kind of object
1169 file. If your @command{ld} is configured this way, you can use the
1170 @samp{--oformat} option to specify the binary format for the output
1171 object file. Even when @command{ld} is configured to support alternative
1172 object formats, you don't usually need to specify this, as @command{ld}
1173 should be configured to produce as a default output format the most
1174 usual format on each machine. @var{output-format} is a text string, the
1175 name of a particular format supported by the BFD libraries. (You can
1176 list the available binary formats with @samp{objdump -i}.) The script
1177 command @code{OUTPUT_FORMAT} can also specify the output format, but
1178 this option overrides it. @xref{BFD}.
1179 @end ifclear
1180
1181 @kindex -qmagic
1182 @item -qmagic
1183 This option is ignored for Linux compatibility.
1184
1185 @kindex -Qy
1186 @item -Qy
1187 This option is ignored for SVR4 compatibility.
1188
1189 @kindex --relax
1190 @cindex synthesizing linker
1191 @cindex relaxing addressing modes
1192 @item --relax
1193 An option with machine dependent effects.
1194 @ifset GENERIC
1195 This option is only supported on a few targets.
1196 @end ifset
1197 @ifset H8300
1198 @xref{H8/300,,@command{ld} and the H8/300}.
1199 @end ifset
1200 @ifset I960
1201 @xref{i960,, @command{ld} and the Intel 960 family}.
1202 @end ifset
1203
1204
1205 On some platforms, the @samp{--relax} option performs global
1206 optimizations that become possible when the linker resolves addressing
1207 in the program, such as relaxing address modes and synthesizing new
1208 instructions in the output object file.
1209
1210 On some platforms these link time global optimizations may make symbolic
1211 debugging of the resulting executable impossible.
1212 @ifset GENERIC
1213 This is known to be
1214 the case for the Matsushita MN10200 and MN10300 family of processors.
1215 @end ifset
1216
1217 @ifset GENERIC
1218 On platforms where this is not supported, @samp{--relax} is accepted,
1219 but ignored.
1220 @end ifset
1221
1222 @cindex retaining specified symbols
1223 @cindex stripping all but some symbols
1224 @cindex symbols, retaining selectively
1225 @item --retain-symbols-file @var{filename}
1226 Retain @emph{only} the symbols listed in the file @var{filename},
1227 discarding all others. @var{filename} is simply a flat file, with one
1228 symbol name per line. This option is especially useful in environments
1229 @ifset GENERIC
1230 (such as VxWorks)
1231 @end ifset
1232 where a large global symbol table is accumulated gradually, to conserve
1233 run-time memory.
1234
1235 @samp{--retain-symbols-file} does @emph{not} discard undefined symbols,
1236 or symbols needed for relocations.
1237
1238 You may only specify @samp{--retain-symbols-file} once in the command
1239 line. It overrides @samp{-s} and @samp{-S}.
1240
1241 @ifset GENERIC
1242 @item -rpath @var{dir}
1243 @cindex runtime library search path
1244 @kindex -rpath
1245 Add a directory to the runtime library search path. This is used when
1246 linking an ELF executable with shared objects. All @option{-rpath}
1247 arguments are concatenated and passed to the runtime linker, which uses
1248 them to locate shared objects at runtime. The @option{-rpath} option is
1249 also used when locating shared objects which are needed by shared
1250 objects explicitly included in the link; see the description of the
1251 @option{-rpath-link} option. If @option{-rpath} is not used when linking an
1252 ELF executable, the contents of the environment variable
1253 @code{LD_RUN_PATH} will be used if it is defined.
1254
1255 The @option{-rpath} option may also be used on SunOS. By default, on
1256 SunOS, the linker will form a runtime search patch out of all the
1257 @option{-L} options it is given. If a @option{-rpath} option is used, the
1258 runtime search path will be formed exclusively using the @option{-rpath}
1259 options, ignoring the @option{-L} options. This can be useful when using
1260 gcc, which adds many @option{-L} options which may be on NFS mounted
1261 filesystems.
1262
1263 For compatibility with other ELF linkers, if the @option{-R} option is
1264 followed by a directory name, rather than a file name, it is treated as
1265 the @option{-rpath} option.
1266 @end ifset
1267
1268 @ifset GENERIC
1269 @cindex link-time runtime library search path
1270 @kindex -rpath-link
1271 @item -rpath-link @var{DIR}
1272 When using ELF or SunOS, one shared library may require another. This
1273 happens when an @code{ld -shared} link includes a shared library as one
1274 of the input files.
1275
1276 When the linker encounters such a dependency when doing a non-shared,
1277 non-relocatable link, it will automatically try to locate the required
1278 shared library and include it in the link, if it is not included
1279 explicitly. In such a case, the @option{-rpath-link} option
1280 specifies the first set of directories to search. The
1281 @option{-rpath-link} option may specify a sequence of directory names
1282 either by specifying a list of names separated by colons, or by
1283 appearing multiple times.
1284
1285 This option should be used with caution as it overrides the search path
1286 that may have been hard compiled into a shared library. In such a case it
1287 is possible to use unintentionally a different search path than the
1288 runtime linker would do.
1289
1290 The linker uses the following search paths to locate required shared
1291 libraries.
1292 @enumerate
1293 @item
1294 Any directories specified by @option{-rpath-link} options.
1295 @item
1296 Any directories specified by @option{-rpath} options. The difference
1297 between @option{-rpath} and @option{-rpath-link} is that directories
1298 specified by @option{-rpath} options are included in the executable and
1299 used at runtime, whereas the @option{-rpath-link} option is only effective
1300 at link time. It is for the native linker only.
1301 @item
1302 On an ELF system, if the @option{-rpath} and @code{rpath-link} options
1303 were not used, search the contents of the environment variable
1304 @code{LD_RUN_PATH}. It is for the native linker only.
1305 @item
1306 On SunOS, if the @option{-rpath} option was not used, search any
1307 directories specified using @option{-L} options.
1308 @item
1309 For a native linker, the contents of the environment variable
1310 @code{LD_LIBRARY_PATH}.
1311 @item
1312 For a native ELF linker, the directories in @code{DT_RUNPATH} or
1313 @code{DT_RPATH} of a shared library are searched for shared
1314 libraries needed by it. The @code{DT_RPATH} entries are ignored if
1315 @code{DT_RUNPATH} entries exist.
1316 @item
1317 The default directories, normally @file{/lib} and @file{/usr/lib}.
1318 @item
1319 For a native linker on an ELF system, if the file @file{/etc/ld.so.conf}
1320 exists, the list of directories found in that file.
1321 @end enumerate
1322
1323 If the required shared library is not found, the linker will issue a
1324 warning and continue with the link.
1325 @end ifset
1326
1327 @kindex -shared
1328 @kindex -Bshareable
1329 @item -shared
1330 @itemx -Bshareable
1331 @cindex shared libraries
1332 Create a shared library. This is currently only supported on ELF, XCOFF
1333 and SunOS platforms. On SunOS, the linker will automatically create a
1334 shared library if the @option{-e} option is not used and there are
1335 undefined symbols in the link.
1336
1337 @item --sort-common
1338 @kindex --sort-common
1339 This option tells @command{ld} to sort the common symbols by size when it
1340 places them in the appropriate output sections. First come all the one
1341 byte symbols, then all the two byte, then all the four byte, and then
1342 everything else. This is to prevent gaps between symbols due to
1343 alignment constraints.
1344
1345 @kindex --split-by-file
1346 @item --split-by-file [@var{size}]
1347 Similar to @option{--split-by-reloc} but creates a new output section for
1348 each input file when @var{size} is reached. @var{size} defaults to a
1349 size of 1 if not given.
1350
1351 @kindex --split-by-reloc
1352 @item --split-by-reloc [@var{count}]
1353 Tries to creates extra sections in the output file so that no single
1354 output section in the file contains more than @var{count} relocations.
1355 This is useful when generating huge relocatable files for downloading into
1356 certain real time kernels with the COFF object file format; since COFF
1357 cannot represent more than 65535 relocations in a single section. Note
1358 that this will fail to work with object file formats which do not
1359 support arbitrary sections. The linker will not split up individual
1360 input sections for redistribution, so if a single input section contains
1361 more than @var{count} relocations one output section will contain that
1362 many relocations. @var{count} defaults to a value of 32768.
1363
1364 @kindex --stats
1365 @item --stats
1366 Compute and display statistics about the operation of the linker, such
1367 as execution time and memory usage.
1368
1369 @kindex --traditional-format
1370 @cindex traditional format
1371 @item --traditional-format
1372 For some targets, the output of @command{ld} is different in some ways from
1373 the output of some existing linker. This switch requests @command{ld} to
1374 use the traditional format instead.
1375
1376 @cindex dbx
1377 For example, on SunOS, @command{ld} combines duplicate entries in the
1378 symbol string table. This can reduce the size of an output file with
1379 full debugging information by over 30 percent. Unfortunately, the SunOS
1380 @code{dbx} program can not read the resulting program (@code{gdb} has no
1381 trouble). The @samp{--traditional-format} switch tells @command{ld} to not
1382 combine duplicate entries.
1383
1384 @kindex --section-start @var{sectionname}=@var{org}
1385 @item --section-start @var{sectionname}=@var{org}
1386 Locate a section in the output file at the absolute
1387 address given by @var{org}. You may use this option as many
1388 times as necessary to locate multiple sections in the command
1389 line.
1390 @var{org} must be a single hexadecimal integer;
1391 for compatibility with other linkers, you may omit the leading
1392 @samp{0x} usually associated with hexadecimal values. @emph{Note:} there
1393 should be no white space between @var{sectionname}, the equals
1394 sign (``@key{=}''), and @var{org}.
1395
1396 @kindex -Tbss @var{org}
1397 @kindex -Tdata @var{org}
1398 @kindex -Ttext @var{org}
1399 @cindex segment origins, cmd line
1400 @item -Tbss @var{org}
1401 @itemx -Tdata @var{org}
1402 @itemx -Ttext @var{org}
1403 Use @var{org} as the starting address for---respectively---the
1404 @code{bss}, @code{data}, or the @code{text} segment of the output file.
1405 @var{org} must be a single hexadecimal integer;
1406 for compatibility with other linkers, you may omit the leading
1407 @samp{0x} usually associated with hexadecimal values.
1408
1409 @kindex --verbose
1410 @cindex verbose
1411 @item --dll-verbose
1412 @itemx --verbose
1413 Display the version number for @command{ld} and list the linker emulations
1414 supported. Display which input files can and cannot be opened. Display
1415 the linker script being used by the linker.
1416
1417 @kindex --version-script=@var{version-scriptfile}
1418 @cindex version script, symbol versions
1419 @itemx --version-script=@var{version-scriptfile}
1420 Specify the name of a version script to the linker. This is typically
1421 used when creating shared libraries to specify additional information
1422 about the version heirarchy for the library being created. This option
1423 is only meaningful on ELF platforms which support shared libraries.
1424 @xref{VERSION}.
1425
1426 @kindex --warn-common
1427 @cindex warnings, on combining symbols
1428 @cindex combining symbols, warnings on
1429 @item --warn-common
1430 Warn when a common symbol is combined with another common symbol or with
1431 a symbol definition. Unix linkers allow this somewhat sloppy practice,
1432 but linkers on some other operating systems do not. This option allows
1433 you to find potential problems from combining global symbols.
1434 Unfortunately, some C libraries use this practice, so you may get some
1435 warnings about symbols in the libraries as well as in your programs.
1436
1437 There are three kinds of global symbols, illustrated here by C examples:
1438
1439 @table @samp
1440 @item int i = 1;
1441 A definition, which goes in the initialized data section of the output
1442 file.
1443
1444 @item extern int i;
1445 An undefined reference, which does not allocate space.
1446 There must be either a definition or a common symbol for the
1447 variable somewhere.
1448
1449 @item int i;
1450 A common symbol. If there are only (one or more) common symbols for a
1451 variable, it goes in the uninitialized data area of the output file.
1452 The linker merges multiple common symbols for the same variable into a
1453 single symbol. If they are of different sizes, it picks the largest
1454 size. The linker turns a common symbol into a declaration, if there is
1455 a definition of the same variable.
1456 @end table
1457
1458 The @samp{--warn-common} option can produce five kinds of warnings.
1459 Each warning consists of a pair of lines: the first describes the symbol
1460 just encountered, and the second describes the previous symbol
1461 encountered with the same name. One or both of the two symbols will be
1462 a common symbol.
1463
1464 @enumerate
1465 @item
1466 Turning a common symbol into a reference, because there is already a
1467 definition for the symbol.
1468 @smallexample
1469 @var{file}(@var{section}): warning: common of `@var{symbol}'
1470 overridden by definition
1471 @var{file}(@var{section}): warning: defined here
1472 @end smallexample
1473
1474 @item
1475 Turning a common symbol into a reference, because a later definition for
1476 the symbol is encountered. This is the same as the previous case,
1477 except that the symbols are encountered in a different order.
1478 @smallexample
1479 @var{file}(@var{section}): warning: definition of `@var{symbol}'
1480 overriding common
1481 @var{file}(@var{section}): warning: common is here
1482 @end smallexample
1483
1484 @item
1485 Merging a common symbol with a previous same-sized common symbol.
1486 @smallexample
1487 @var{file}(@var{section}): warning: multiple common
1488 of `@var{symbol}'
1489 @var{file}(@var{section}): warning: previous common is here
1490 @end smallexample
1491
1492 @item
1493 Merging a common symbol with a previous larger common symbol.
1494 @smallexample
1495 @var{file}(@var{section}): warning: common of `@var{symbol}'
1496 overridden by larger common
1497 @var{file}(@var{section}): warning: larger common is here
1498 @end smallexample
1499
1500 @item
1501 Merging a common symbol with a previous smaller common symbol. This is
1502 the same as the previous case, except that the symbols are
1503 encountered in a different order.
1504 @smallexample
1505 @var{file}(@var{section}): warning: common of `@var{symbol}'
1506 overriding smaller common
1507 @var{file}(@var{section}): warning: smaller common is here
1508 @end smallexample
1509 @end enumerate
1510
1511 @kindex --warn-constructors
1512 @item --warn-constructors
1513 Warn if any global constructors are used. This is only useful for a few
1514 object file formats. For formats like COFF or ELF, the linker can not
1515 detect the use of global constructors.
1516
1517 @kindex --warn-multiple-gp
1518 @item --warn-multiple-gp
1519 Warn if multiple global pointer values are required in the output file.
1520 This is only meaningful for certain processors, such as the Alpha.
1521 Specifically, some processors put large-valued constants in a special
1522 section. A special register (the global pointer) points into the middle
1523 of this section, so that constants can be loaded efficiently via a
1524 base-register relative addressing mode. Since the offset in
1525 base-register relative mode is fixed and relatively small (e.g., 16
1526 bits), this limits the maximum size of the constant pool. Thus, in
1527 large programs, it is often necessary to use multiple global pointer
1528 values in order to be able to address all possible constants. This
1529 option causes a warning to be issued whenever this case occurs.
1530
1531 @kindex --warn-once
1532 @cindex warnings, on undefined symbols
1533 @cindex undefined symbols, warnings on
1534 @item --warn-once
1535 Only warn once for each undefined symbol, rather than once per module
1536 which refers to it.
1537
1538 @kindex --warn-section-align
1539 @cindex warnings, on section alignment
1540 @cindex section alignment, warnings on
1541 @item --warn-section-align
1542 Warn if the address of an output section is changed because of
1543 alignment. Typically, the alignment will be set by an input section.
1544 The address will only be changed if it not explicitly specified; that
1545 is, if the @code{SECTIONS} command does not specify a start address for
1546 the section (@pxref{SECTIONS}).
1547
1548 @kindex --whole-archive
1549 @cindex including an entire archive
1550 @item --whole-archive
1551 For each archive mentioned on the command line after the
1552 @option{--whole-archive} option, include every object file in the archive
1553 in the link, rather than searching the archive for the required object
1554 files. This is normally used to turn an archive file into a shared
1555 library, forcing every object to be included in the resulting shared
1556 library. This option may be used more than once.
1557
1558 Two notes when using this option from gcc: First, gcc doesn't know
1559 about this option, so you have to use @option{-Wl,-whole-archive}.
1560 Second, don't forget to use @option{-Wl,-no-whole-archive} after your
1561 list of archives, because gcc will add its own list of archives to
1562 your link and you may not want this flag to affect those as well.
1563
1564 @kindex --wrap
1565 @item --wrap @var{symbol}
1566 Use a wrapper function for @var{symbol}. Any undefined reference to
1567 @var{symbol} will be resolved to @code{__wrap_@var{symbol}}. Any
1568 undefined reference to @code{__real_@var{symbol}} will be resolved to
1569 @var{symbol}.
1570
1571 This can be used to provide a wrapper for a system function. The
1572 wrapper function should be called @code{__wrap_@var{symbol}}. If it
1573 wishes to call the system function, it should call
1574 @code{__real_@var{symbol}}.
1575
1576 Here is a trivial example:
1577
1578 @smallexample
1579 void *
1580 __wrap_malloc (int c)
1581 @{
1582 printf ("malloc called with %ld\n", c);
1583 return __real_malloc (c);
1584 @}
1585 @end smallexample
1586
1587 If you link other code with this file using @option{--wrap malloc}, then
1588 all calls to @code{malloc} will call the function @code{__wrap_malloc}
1589 instead. The call to @code{__real_malloc} in @code{__wrap_malloc} will
1590 call the real @code{malloc} function.
1591
1592 You may wish to provide a @code{__real_malloc} function as well, so that
1593 links without the @option{--wrap} option will succeed. If you do this,
1594 you should not put the definition of @code{__real_malloc} in the same
1595 file as @code{__wrap_malloc}; if you do, the assembler may resolve the
1596 call before the linker has a chance to wrap it to @code{malloc}.
1597
1598 @kindex --enable-new-dtags
1599 @kindex --disable-new-dtags
1600 @item --enable-new-dtags
1601 @itemx --disable-new-dtags
1602 This linker can create the new dynamic tags in ELF. But the older ELF
1603 systems may not understand them. If you specify
1604 @option{--enable-new-dtags}, the dynamic tags will be created as needed.
1605 If you specify @option{--disable-new-dtags}, no new dynamic tags will be
1606 created. By default, the new dynamic tags are not created. Note that
1607 those options are only available for ELF systems.
1608
1609 @end table
1610
1611 @c man end
1612
1613 @subsection Options specific to i386 PE targets
1614
1615 @c man begin OPTIONS
1616
1617 The i386 PE linker supports the @option{-shared} option, which causes
1618 the output to be a dynamically linked library (DLL) instead of a
1619 normal executable. You should name the output @code{*.dll} when you
1620 use this option. In addition, the linker fully supports the standard
1621 @code{*.def} files, which may be specified on the linker command line
1622 like an object file (in fact, it should precede archives it exports
1623 symbols from, to ensure that they get linked in, just like a normal
1624 object file).
1625
1626 In addition to the options common to all targets, the i386 PE linker
1627 support additional command line options that are specific to the i386
1628 PE target. Options that take values may be separated from their
1629 values by either a space or an equals sign.
1630
1631 @table @gcctabopt
1632
1633 @kindex --add-stdcall-alias
1634 @item --add-stdcall-alias
1635 If given, symbols with a stdcall suffix (@@@var{nn}) will be exported
1636 as-is and also with the suffix stripped.
1637
1638 @kindex --base-file
1639 @item --base-file @var{file}
1640 Use @var{file} as the name of a file in which to save the base
1641 addresses of all the relocations needed for generating DLLs with
1642 @file{dlltool}.
1643
1644 @kindex --dll
1645 @item --dll
1646 Create a DLL instead of a regular executable. You may also use
1647 @option{-shared} or specify a @code{LIBRARY} in a given @code{.def}
1648 file.
1649
1650 @kindex --enable-stdcall-fixup
1651 @kindex --disable-stdcall-fixup
1652 @item --enable-stdcall-fixup
1653 @itemx --disable-stdcall-fixup
1654 If the link finds a symbol that it cannot resolve, it will attempt to
1655 do "fuzzy linking" by looking for another defined symbol that differs
1656 only in the format of the symbol name (cdecl vs stdcall) and will
1657 resolve that symbol by linking to the match. For example, the
1658 undefined symbol @code{_foo} might be linked to the function
1659 @code{_foo@@12}, or the undefined symbol @code{_bar@@16} might be linked
1660 to the function @code{_bar}. When the linker does this, it prints a
1661 warning, since it normally should have failed to link, but sometimes
1662 import libraries generated from third-party dlls may need this feature
1663 to be usable. If you specify @option{--enable-stdcall-fixup}, this
1664 feature is fully enabled and warnings are not printed. If you specify
1665 @option{--disable-stdcall-fixup}, this feature is disabled and such
1666 mismatches are considered to be errors.
1667
1668 @cindex DLLs, creating
1669 @kindex --export-all-symbols
1670 @item --export-all-symbols
1671 If given, all global symbols in the objects used to build a DLL will
1672 be exported by the DLL. Note that this is the default if there
1673 otherwise wouldn't be any exported symbols. When symbols are
1674 explicitly exported via DEF files or implicitly exported via function
1675 attributes, the default is to not export anything else unless this
1676 option is given. Note that the symbols @code{DllMain@@12},
1677 @code{DllEntryPoint@@0}, @code{DllMainCRTStartup@@12}, and
1678 @code{impure_ptr} will not be automatically
1679 exported. Also, symbols imported from other DLLs will not be
1680 re-exported, nor will symbols specifying the DLL's internal layout
1681 such as those beginning with @code{_head_} or ending with
1682 @code{_iname}. In addition, no symbols from @code{libgcc},
1683 @code{libstd++}, @code{libmingw32}, or @code{crtX.o} will be exported.
1684 Symbols whose names begin with @code{__rtti_} or @code{__builtin_} will
1685 not be exported, to help with C++ DLLs. Finally, there is an
1686 extensive list of cygwin-private symbols that are not exported
1687 (obviously, this applies on when building DLLs for cygwin targets).
1688 These cygwin-excludes are: @code{_cygwin_dll_entry@@12},
1689 @code{_cygwin_crt0_common@@8}, @code{_cygwin_noncygwin_dll_entry@@12},
1690 @code{_fmode}, @code{_impure_ptr}, @code{cygwin_attach_dll},
1691 @code{cygwin_premain0}, @code{cygwin_premain1}, @code{cygwin_premain2},
1692 @code{cygwin_premain3}, and @code{environ}.
1693
1694 @kindex --exclude-symbols
1695 @item --exclude-symbols @var{symbol},@var{symbol},...
1696 Specifies a list of symbols which should not be automatically
1697 exported. The symbol names may be delimited by commas or colons.
1698
1699 @kindex --exclude-libs
1700 @item --exclude-libs @var{lib},@var{lib},...
1701 Specifies a list of archive libraries from which symbols should not be automatically
1702 exported. The library names may be delimited by commas or colons. Specifying
1703 @code{--exclude-libs ALL} excludes symbols in all archive libraries from
1704 automatic export. Symbols explicitly listed in a .def file are still exported,
1705 regardless of this option.
1706
1707 @kindex --file-alignment
1708 @item --file-alignment
1709 Specify the file alignment. Sections in the file will always begin at
1710 file offsets which are multiples of this number. This defaults to
1711 512.
1712
1713 @cindex heap size
1714 @kindex --heap
1715 @item --heap @var{reserve}
1716 @itemx --heap @var{reserve},@var{commit}
1717 Specify the amount of memory to reserve (and optionally commit) to be
1718 used as heap for this program. The default is 1Mb reserved, 4K
1719 committed.
1720
1721 @cindex image base
1722 @kindex --image-base
1723 @item --image-base @var{value}
1724 Use @var{value} as the base address of your program or dll. This is
1725 the lowest memory location that will be used when your program or dll
1726 is loaded. To reduce the need to relocate and improve performance of
1727 your dlls, each should have a unique base address and not overlap any
1728 other dlls. The default is 0x400000 for executables, and 0x10000000
1729 for dlls.
1730
1731 @kindex --kill-at
1732 @item --kill-at
1733 If given, the stdcall suffixes (@@@var{nn}) will be stripped from
1734 symbols before they are exported.
1735
1736 @kindex --major-image-version
1737 @item --major-image-version @var{value}
1738 Sets the major number of the "image version". Defaults to 1.
1739
1740 @kindex --major-os-version
1741 @item --major-os-version @var{value}
1742 Sets the major number of the "os version". Defaults to 4.
1743
1744 @kindex --major-subsystem-version
1745 @item --major-subsystem-version @var{value}
1746 Sets the major number of the "subsystem version". Defaults to 4.
1747
1748 @kindex --minor-image-version
1749 @item --minor-image-version @var{value}
1750 Sets the minor number of the "image version". Defaults to 0.
1751
1752 @kindex --minor-os-version
1753 @item --minor-os-version @var{value}
1754 Sets the minor number of the "os version". Defaults to 0.
1755
1756 @kindex --minor-subsystem-version
1757 @item --minor-subsystem-version @var{value}
1758 Sets the minor number of the "subsystem version". Defaults to 0.
1759
1760 @cindex DEF files, creating
1761 @cindex DLLs, creating
1762 @kindex --output-def
1763 @item --output-def @var{file}
1764 The linker will create the file @var{file} which will contain a DEF
1765 file corresponding to the DLL the linker is generating. This DEF file
1766 (which should be called @code{*.def}) may be used to create an import
1767 library with @code{dlltool} or may be used as a reference to
1768 automatically or implicitly exported symbols.
1769
1770 @cindex DLLs, creating
1771 @kindex --out-implib
1772 @item --out-implib @var{file}
1773 The linker will create the file @var{file} which will contain an
1774 import lib corresponding to the DLL the linker is generating. This
1775 import lib (which should be called @code{*.dll.a} or @code{*.a}
1776 may be used to link clients against the generated DLL; this behavior
1777 makes it possible to skip a separate @code{dlltool} import library
1778 creation step.
1779
1780 @kindex --enable-auto-image-base
1781 @item --enable-auto-image-base
1782 Automatically choose the image base for DLLs, unless one is specified
1783 using the @code{--image-base} argument. By using a hash generated
1784 from the dllname to create unique image bases for each DLL, in-memory
1785 collisions and relocations which can delay program execution are
1786 avoided.
1787
1788 @kindex --disable-auto-image-base
1789 @item --disable-auto-image-base
1790 Do not automatically generate a unique image base. If there is no
1791 user-specified image base (@code{--image-base}) then use the platform
1792 default.
1793
1794 @cindex DLLs, linking to
1795 @kindex --dll-search-prefix
1796 @item --dll-search-prefix @var{string}
1797 When linking dynamically to a dll without an import library, i
1798 search for @code{<string><basename>.dll} in preference to
1799 @code{lib<basename>.dll}. This behavior allows easy distinction
1800 between DLLs built for the various "subplatforms": native, cygwin,
1801 uwin, pw, etc. For instance, cygwin DLLs typically use
1802 @code{--dll-search-prefix=cyg}.
1803
1804 @kindex --enable-auto-import
1805 @item --enable-auto-import
1806 Do sophisticated linking of @code{_symbol} to @code{__imp__symbol} for
1807 DATA imports from DLLs, and create the necessary thunking symbols when
1808 building the import libraries with those DATA exports. This generally
1809 will 'just work' -- but sometimes you may see this message:
1810
1811 "variable '<var>' can't be auto-imported. Please read the
1812 documentation for ld's @code{--enable-auto-import} for details."
1813
1814 This message occurs when some (sub)expression accesses an address
1815 ultimately given by the sum of two constants (Win32 import tables only
1816 allow one). Instances where this may occur include accesses to member
1817 fields of struct variables imported from a DLL, as well as using a
1818 constant index into an array variable imported from a DLL. Any
1819 multiword variable (arrays, structs, long long, etc) may trigger
1820 this error condition. However, regardless of the exact data type
1821 of the offending exported variable, ld will always detect it, issue
1822 the warning, and exit.
1823
1824 There are several ways to address this difficulty, regardless of the
1825 data type of the exported variable:
1826
1827 One way is to use --enable-runtime-pseudo-reloc switch. This leaves the task
1828 of adjusting references in your client code for runtime environment, so
1829 this method works only when runtime environtment supports this feature.
1830
1831 A second solution is to force one of the 'constants' to be a variable --
1832 that is, unknown and un-optimizable at compile time. For arrays,
1833 there are two possibilities: a) make the indexee (the array's address)
1834 a variable, or b) make the 'constant' index a variable. Thus:
1835
1836 @example
1837 extern type extern_array[];
1838 extern_array[1] -->
1839 @{ volatile type *t=extern_array; t[1] @}
1840 @end example
1841
1842 or
1843
1844 @example
1845 extern type extern_array[];
1846 extern_array[1] -->
1847 @{ volatile int t=1; extern_array[t] @}
1848 @end example
1849
1850 For structs (and most other multiword data types) the only option
1851 is to make the struct itself (or the long long, or the ...) variable:
1852
1853 @example
1854 extern struct s extern_struct;
1855 extern_struct.field -->
1856 @{ volatile struct s *t=&extern_struct; t->field @}
1857 @end example
1858
1859 or
1860
1861 @example
1862 extern long long extern_ll;
1863 extern_ll -->
1864 @{ volatile long long * local_ll=&extern_ll; *local_ll @}
1865 @end example
1866
1867 A third method of dealing with this difficulty is to abandon
1868 'auto-import' for the offending symbol and mark it with
1869 @code{__declspec(dllimport)}. However, in practice that
1870 requires using compile-time #defines to indicate whether you are
1871 building a DLL, building client code that will link to the DLL, or
1872 merely building/linking to a static library. In making the choice
1873 between the various methods of resolving the 'direct address with
1874 constant offset' problem, you should consider typical real-world usage:
1875
1876 Original:
1877 @example
1878 --foo.h
1879 extern int arr[];
1880 --foo.c
1881 #include "foo.h"
1882 void main(int argc, char **argv)@{
1883 printf("%d\n",arr[1]);
1884 @}
1885 @end example
1886
1887 Solution 1:
1888 @example
1889 --foo.h
1890 extern int arr[];
1891 --foo.c
1892 #include "foo.h"
1893 void main(int argc, char **argv)@{
1894 /* This workaround is for win32 and cygwin; do not "optimize" */
1895 volatile int *parr = arr;
1896 printf("%d\n",parr[1]);
1897 @}
1898 @end example
1899
1900 Solution 2:
1901 @example
1902 --foo.h
1903 /* Note: auto-export is assumed (no __declspec(dllexport)) */
1904 #if (defined(_WIN32) || defined(__CYGWIN__)) && \
1905 !(defined(FOO_BUILD_DLL) || defined(FOO_STATIC))
1906 #define FOO_IMPORT __declspec(dllimport)
1907 #else
1908 #define FOO_IMPORT
1909 #endif
1910 extern FOO_IMPORT int arr[];
1911 --foo.c
1912 #include "foo.h"
1913 void main(int argc, char **argv)@{
1914 printf("%d\n",arr[1]);
1915 @}
1916 @end example
1917
1918 A fourth way to avoid this problem is to re-code your
1919 library to use a functional interface rather than a data interface
1920 for the offending variables (e.g. set_foo() and get_foo() accessor
1921 functions).
1922
1923 @kindex --disable-auto-import
1924 @item --disable-auto-import
1925 Do not attempt to do sophisticalted linking of @code{_symbol} to
1926 @code{__imp__symbol} for DATA imports from DLLs.
1927
1928 @kindex --enable-runtime-pseudo-reloc
1929 @item --enable-runtime-pseudo-reloc
1930 If your code contains expressions described in --enable-auto-import section,
1931 that is, DATA imports from DLL with non-zero offset, this switch will create
1932 a vector of 'runtime pseudo relocations' which can be used by runtime
1933 environment to adjust references to such data in your client code.
1934
1935 @kindex --disable-runtime-pseudo-reloc
1936 @item --disable-runtime-pseudo-reloc
1937 Do not create pseudo relocations for non-zero offset DATA imports from
1938 DLLs. This is the default.
1939
1940 @kindex --enable-extra-pe-debug
1941 @item --enable-extra-pe-debug
1942 Show additional debug info related to auto-import symbol thunking.
1943
1944 @kindex --section-alignment
1945 @item --section-alignment
1946 Sets the section alignment. Sections in memory will always begin at
1947 addresses which are a multiple of this number. Defaults to 0x1000.
1948
1949 @cindex stack size
1950 @kindex --stack
1951 @item --stack @var{reserve}
1952 @itemx --stack @var{reserve},@var{commit}
1953 Specify the amount of memory to reserve (and optionally commit) to be
1954 used as stack for this program. The default is 2Mb reserved, 4K
1955 committed.
1956
1957 @kindex --subsystem
1958 @item --subsystem @var{which}
1959 @itemx --subsystem @var{which}:@var{major}
1960 @itemx --subsystem @var{which}:@var{major}.@var{minor}
1961 Specifies the subsystem under which your program will execute. The
1962 legal values for @var{which} are @code{native}, @code{windows},
1963 @code{console}, and @code{posix}. You may optionally set the
1964 subsystem version also.
1965
1966 @end table
1967
1968 @c man end
1969
1970 @ifset UsesEnvVars
1971 @node Environment
1972 @section Environment Variables
1973
1974 @c man begin ENVIRONMENT
1975
1976 You can change the behavior of @command{ld} with the environment variables
1977 @code{GNUTARGET}, @code{LDEMULATION}, and @code{COLLECT_NO_DEMANGLE}.
1978
1979 @kindex GNUTARGET
1980 @cindex default input format
1981 @code{GNUTARGET} determines the input-file object format if you don't
1982 use @samp{-b} (or its synonym @samp{--format}). Its value should be one
1983 of the BFD names for an input format (@pxref{BFD}). If there is no
1984 @code{GNUTARGET} in the environment, @command{ld} uses the natural format
1985 of the target. If @code{GNUTARGET} is set to @code{default} then BFD
1986 attempts to discover the input format by examining binary input files;
1987 this method often succeeds, but there are potential ambiguities, since
1988 there is no method of ensuring that the magic number used to specify
1989 object-file formats is unique. However, the configuration procedure for
1990 BFD on each system places the conventional format for that system first
1991 in the search-list, so ambiguities are resolved in favor of convention.
1992
1993 @kindex LDEMULATION
1994 @cindex default emulation
1995 @cindex emulation, default
1996 @code{LDEMULATION} determines the default emulation if you don't use the
1997 @samp{-m} option. The emulation can affect various aspects of linker
1998 behaviour, particularly the default linker script. You can list the
1999 available emulations with the @samp{--verbose} or @samp{-V} options. If
2000 the @samp{-m} option is not used, and the @code{LDEMULATION} environment
2001 variable is not defined, the default emulation depends upon how the
2002 linker was configured.
2003
2004 @kindex COLLECT_NO_DEMANGLE
2005 @cindex demangling, default
2006 Normally, the linker will default to demangling symbols. However, if
2007 @code{COLLECT_NO_DEMANGLE} is set in the environment, then it will
2008 default to not demangling symbols. This environment variable is used in
2009 a similar fashion by the @code{gcc} linker wrapper program. The default
2010 may be overridden by the @samp{--demangle} and @samp{--no-demangle}
2011 options.
2012
2013 @c man end
2014 @end ifset
2015
2016 @node Scripts
2017 @chapter Linker Scripts
2018
2019 @cindex scripts
2020 @cindex linker scripts
2021 @cindex command files
2022 Every link is controlled by a @dfn{linker script}. This script is
2023 written in the linker command language.
2024
2025 The main purpose of the linker script is to describe how the sections in
2026 the input files should be mapped into the output file, and to control
2027 the memory layout of the output file. Most linker scripts do nothing
2028 more than this. However, when necessary, the linker script can also
2029 direct the linker to perform many other operations, using the commands
2030 described below.
2031
2032 The linker always uses a linker script. If you do not supply one
2033 yourself, the linker will use a default script that is compiled into the
2034 linker executable. You can use the @samp{--verbose} command line option
2035 to display the default linker script. Certain command line options,
2036 such as @samp{-r} or @samp{-N}, will affect the default linker script.
2037
2038 You may supply your own linker script by using the @samp{-T} command
2039 line option. When you do this, your linker script will replace the
2040 default linker script.
2041
2042 You may also use linker scripts implicitly by naming them as input files
2043 to the linker, as though they were files to be linked. @xref{Implicit
2044 Linker Scripts}.
2045
2046 @menu
2047 * Basic Script Concepts:: Basic Linker Script Concepts
2048 * Script Format:: Linker Script Format
2049 * Simple Example:: Simple Linker Script Example
2050 * Simple Commands:: Simple Linker Script Commands
2051 * Assignments:: Assigning Values to Symbols
2052 * SECTIONS:: SECTIONS Command
2053 * MEMORY:: MEMORY Command
2054 * PHDRS:: PHDRS Command
2055 * VERSION:: VERSION Command
2056 * Expressions:: Expressions in Linker Scripts
2057 * Implicit Linker Scripts:: Implicit Linker Scripts
2058 @end menu
2059
2060 @node Basic Script Concepts
2061 @section Basic Linker Script Concepts
2062 @cindex linker script concepts
2063 We need to define some basic concepts and vocabulary in order to
2064 describe the linker script language.
2065
2066 The linker combines input files into a single output file. The output
2067 file and each input file are in a special data format known as an
2068 @dfn{object file format}. Each file is called an @dfn{object file}.
2069 The output file is often called an @dfn{executable}, but for our
2070 purposes we will also call it an object file. Each object file has,
2071 among other things, a list of @dfn{sections}. We sometimes refer to a
2072 section in an input file as an @dfn{input section}; similarly, a section
2073 in the output file is an @dfn{output section}.
2074
2075 Each section in an object file has a name and a size. Most sections
2076 also have an associated block of data, known as the @dfn{section
2077 contents}. A section may be marked as @dfn{loadable}, which mean that
2078 the contents should be loaded into memory when the output file is run.
2079 A section with no contents may be @dfn{allocatable}, which means that an
2080 area in memory should be set aside, but nothing in particular should be
2081 loaded there (in some cases this memory must be zeroed out). A section
2082 which is neither loadable nor allocatable typically contains some sort
2083 of debugging information.
2084
2085 Every loadable or allocatable output section has two addresses. The
2086 first is the @dfn{VMA}, or virtual memory address. This is the address
2087 the section will have when the output file is run. The second is the
2088 @dfn{LMA}, or load memory address. This is the address at which the
2089 section will be loaded. In most cases the two addresses will be the
2090 same. An example of when they might be different is when a data section
2091 is loaded into ROM, and then copied into RAM when the program starts up
2092 (this technique is often used to initialize global variables in a ROM
2093 based system). In this case the ROM address would be the LMA, and the
2094 RAM address would be the VMA.
2095
2096 You can see the sections in an object file by using the @code{objdump}
2097 program with the @samp{-h} option.
2098
2099 Every object file also has a list of @dfn{symbols}, known as the
2100 @dfn{symbol table}. A symbol may be defined or undefined. Each symbol
2101 has a name, and each defined symbol has an address, among other
2102 information. If you compile a C or C++ program into an object file, you
2103 will get a defined symbol for every defined function and global or
2104 static variable. Every undefined function or global variable which is
2105 referenced in the input file will become an undefined symbol.
2106
2107 You can see the symbols in an object file by using the @code{nm}
2108 program, or by using the @code{objdump} program with the @samp{-t}
2109 option.
2110
2111 @node Script Format
2112 @section Linker Script Format
2113 @cindex linker script format
2114 Linker scripts are text files.
2115
2116 You write a linker script as a series of commands. Each command is
2117 either a keyword, possibly followed by arguments, or an assignment to a
2118 symbol. You may separate commands using semicolons. Whitespace is
2119 generally ignored.
2120
2121 Strings such as file or format names can normally be entered directly.
2122 If the file name contains a character such as a comma which would
2123 otherwise serve to separate file names, you may put the file name in
2124 double quotes. There is no way to use a double quote character in a
2125 file name.
2126
2127 You may include comments in linker scripts just as in C, delimited by
2128 @samp{/*} and @samp{*/}. As in C, comments are syntactically equivalent
2129 to whitespace.
2130
2131 @node Simple Example
2132 @section Simple Linker Script Example
2133 @cindex linker script example
2134 @cindex example of linker script
2135 Many linker scripts are fairly simple.
2136
2137 The simplest possible linker script has just one command:
2138 @samp{SECTIONS}. You use the @samp{SECTIONS} command to describe the
2139 memory layout of the output file.
2140
2141 The @samp{SECTIONS} command is a powerful command. Here we will
2142 describe a simple use of it. Let's assume your program consists only of
2143 code, initialized data, and uninitialized data. These will be in the
2144 @samp{.text}, @samp{.data}, and @samp{.bss} sections, respectively.
2145 Let's assume further that these are the only sections which appear in
2146 your input files.
2147
2148 For this example, let's say that the code should be loaded at address
2149 0x10000, and that the data should start at address 0x8000000. Here is a
2150 linker script which will do that:
2151 @smallexample
2152 SECTIONS
2153 @{
2154 . = 0x10000;
2155 .text : @{ *(.text) @}
2156 . = 0x8000000;
2157 .data : @{ *(.data) @}
2158 .bss : @{ *(.bss) @}
2159 @}
2160 @end smallexample
2161
2162 You write the @samp{SECTIONS} command as the keyword @samp{SECTIONS},
2163 followed by a series of symbol assignments and output section
2164 descriptions enclosed in curly braces.
2165
2166 The first line inside the @samp{SECTIONS} command of the above example
2167 sets the value of the special symbol @samp{.}, which is the location
2168 counter. If you do not specify the address of an output section in some
2169 other way (other ways are described later), the address is set from the
2170 current value of the location counter. The location counter is then
2171 incremented by the size of the output section. At the start of the
2172 @samp{SECTIONS} command, the location counter has the value @samp{0}.
2173
2174 The second line defines an output section, @samp{.text}. The colon is
2175 required syntax which may be ignored for now. Within the curly braces
2176 after the output section name, you list the names of the input sections
2177 which should be placed into this output section. The @samp{*} is a
2178 wildcard which matches any file name. The expression @samp{*(.text)}
2179 means all @samp{.text} input sections in all input files.
2180
2181 Since the location counter is @samp{0x10000} when the output section
2182 @samp{.text} is defined, the linker will set the address of the
2183 @samp{.text} section in the output file to be @samp{0x10000}.
2184
2185 The remaining lines define the @samp{.data} and @samp{.bss} sections in
2186 the output file. The linker will place the @samp{.data} output section
2187 at address @samp{0x8000000}. After the linker places the @samp{.data}
2188 output section, the value of the location counter will be
2189 @samp{0x8000000} plus the size of the @samp{.data} output section. The
2190 effect is that the linker will place the @samp{.bss} output section
2191 immediately after the @samp{.data} output section in memory
2192
2193 The linker will ensure that each output section has the required
2194 alignment, by increasing the location counter if necessary. In this
2195 example, the specified addresses for the @samp{.text} and @samp{.data}
2196 sections will probably satisfy any alignment constraints, but the linker
2197 may have to create a small gap between the @samp{.data} and @samp{.bss}
2198 sections.
2199
2200 That's it! That's a simple and complete linker script.
2201
2202 @node Simple Commands
2203 @section Simple Linker Script Commands
2204 @cindex linker script simple commands
2205 In this section we describe the simple linker script commands.
2206
2207 @menu
2208 * Entry Point:: Setting the entry point
2209 * File Commands:: Commands dealing with files
2210 @ifclear SingleFormat
2211 * Format Commands:: Commands dealing with object file formats
2212 @end ifclear
2213
2214 * Miscellaneous Commands:: Other linker script commands
2215 @end menu
2216
2217 @node Entry Point
2218 @subsection Setting the entry point
2219 @kindex ENTRY(@var{symbol})
2220 @cindex start of execution
2221 @cindex first instruction
2222 @cindex entry point
2223 The first instruction to execute in a program is called the @dfn{entry
2224 point}. You can use the @code{ENTRY} linker script command to set the
2225 entry point. The argument is a symbol name:
2226 @smallexample
2227 ENTRY(@var{symbol})
2228 @end smallexample
2229
2230 There are several ways to set the entry point. The linker will set the
2231 entry point by trying each of the following methods in order, and
2232 stopping when one of them succeeds:
2233 @itemize @bullet
2234 @item
2235 the @samp{-e} @var{entry} command-line option;
2236 @item
2237 the @code{ENTRY(@var{symbol})} command in a linker script;
2238 @item
2239 the value of the symbol @code{start}, if defined;
2240 @item
2241 the address of the first byte of the @samp{.text} section, if present;
2242 @item
2243 The address @code{0}.
2244 @end itemize
2245
2246 @node File Commands
2247 @subsection Commands dealing with files
2248 @cindex linker script file commands
2249 Several linker script commands deal with files.
2250
2251 @table @code
2252 @item INCLUDE @var{filename}
2253 @kindex INCLUDE @var{filename}
2254 @cindex including a linker script
2255 Include the linker script @var{filename} at this point. The file will
2256 be searched for in the current directory, and in any directory specified
2257 with the @option{-L} option. You can nest calls to @code{INCLUDE} up to
2258 10 levels deep.
2259
2260 @item INPUT(@var{file}, @var{file}, @dots{})
2261 @itemx INPUT(@var{file} @var{file} @dots{})
2262 @kindex INPUT(@var{files})
2263 @cindex input files in linker scripts
2264 @cindex input object files in linker scripts
2265 @cindex linker script input object files
2266 The @code{INPUT} command directs the linker to include the named files
2267 in the link, as though they were named on the command line.
2268
2269 For example, if you always want to include @file{subr.o} any time you do
2270 a link, but you can't be bothered to put it on every link command line,
2271 then you can put @samp{INPUT (subr.o)} in your linker script.
2272
2273 In fact, if you like, you can list all of your input files in the linker
2274 script, and then invoke the linker with nothing but a @samp{-T} option.
2275
2276 The linker will first try to open the file in the current directory. If
2277 it is not found, the linker will search through the archive library
2278 search path. See the description of @samp{-L} in @ref{Options,,Command
2279 Line Options}.
2280
2281 If you use @samp{INPUT (-l@var{file})}, @command{ld} will transform the
2282 name to @code{lib@var{file}.a}, as with the command line argument
2283 @samp{-l}.
2284
2285 When you use the @code{INPUT} command in an implicit linker script, the
2286 files will be included in the link at the point at which the linker
2287 script file is included. This can affect archive searching.
2288
2289 @item GROUP(@var{file}, @var{file}, @dots{})
2290 @itemx GROUP(@var{file} @var{file} @dots{})
2291 @kindex GROUP(@var{files})
2292 @cindex grouping input files
2293 The @code{GROUP} command is like @code{INPUT}, except that the named
2294 files should all be archives, and they are searched repeatedly until no
2295 new undefined references are created. See the description of @samp{-(}
2296 in @ref{Options,,Command Line Options}.
2297
2298 @item OUTPUT(@var{filename})
2299 @kindex OUTPUT(@var{filename})
2300 @cindex output file name in linker scripot
2301 The @code{OUTPUT} command names the output file. Using
2302 @code{OUTPUT(@var{filename})} in the linker script is exactly like using
2303 @samp{-o @var{filename}} on the command line (@pxref{Options,,Command
2304 Line Options}). If both are used, the command line option takes
2305 precedence.
2306
2307 You can use the @code{OUTPUT} command to define a default name for the
2308 output file other than the usual default of @file{a.out}.
2309
2310 @item SEARCH_DIR(@var{path})
2311 @kindex SEARCH_DIR(@var{path})
2312 @cindex library search path in linker script
2313 @cindex archive search path in linker script
2314 @cindex search path in linker script
2315 The @code{SEARCH_DIR} command adds @var{path} to the list of paths where
2316 @command{ld} looks for archive libraries. Using
2317 @code{SEARCH_DIR(@var{path})} is exactly like using @samp{-L @var{path}}
2318 on the command line (@pxref{Options,,Command Line Options}). If both
2319 are used, then the linker will search both paths. Paths specified using
2320 the command line option are searched first.
2321
2322 @item STARTUP(@var{filename})
2323 @kindex STARTUP(@var{filename})
2324 @cindex first input file
2325 The @code{STARTUP} command is just like the @code{INPUT} command, except
2326 that @var{filename} will become the first input file to be linked, as
2327 though it were specified first on the command line. This may be useful
2328 when using a system in which the entry point is always the start of the
2329 first file.
2330 @end table
2331
2332 @ifclear SingleFormat
2333 @node Format Commands
2334 @subsection Commands dealing with object file formats
2335 A couple of linker script commands deal with object file formats.
2336
2337 @table @code
2338 @item OUTPUT_FORMAT(@var{bfdname})
2339 @itemx OUTPUT_FORMAT(@var{default}, @var{big}, @var{little})
2340 @kindex OUTPUT_FORMAT(@var{bfdname})
2341 @cindex output file format in linker script
2342 The @code{OUTPUT_FORMAT} command names the BFD format to use for the
2343 output file (@pxref{BFD}). Using @code{OUTPUT_FORMAT(@var{bfdname})} is
2344 exactly like using @samp{--oformat @var{bfdname}} on the command line
2345 (@pxref{Options,,Command Line Options}). If both are used, the command
2346 line option takes precedence.
2347
2348 You can use @code{OUTPUT_FORMAT} with three arguments to use different
2349 formats based on the @samp{-EB} and @samp{-EL} command line options.
2350 This permits the linker script to set the output format based on the
2351 desired endianness.
2352
2353 If neither @samp{-EB} nor @samp{-EL} are used, then the output format
2354 will be the first argument, @var{default}. If @samp{-EB} is used, the
2355 output format will be the second argument, @var{big}. If @samp{-EL} is
2356 used, the output format will be the third argument, @var{little}.
2357
2358 For example, the default linker script for the MIPS ELF target uses this
2359 command:
2360 @smallexample
2361 OUTPUT_FORMAT(elf32-bigmips, elf32-bigmips, elf32-littlemips)
2362 @end smallexample
2363 This says that the default format for the output file is
2364 @samp{elf32-bigmips}, but if the user uses the @samp{-EL} command line
2365 option, the output file will be created in the @samp{elf32-littlemips}
2366 format.
2367
2368 @item TARGET(@var{bfdname})
2369 @kindex TARGET(@var{bfdname})
2370 @cindex input file format in linker script
2371 The @code{TARGET} command names the BFD format to use when reading input
2372 files. It affects subsequent @code{INPUT} and @code{GROUP} commands.
2373 This command is like using @samp{-b @var{bfdname}} on the command line
2374 (@pxref{Options,,Command Line Options}). If the @code{TARGET} command
2375 is used but @code{OUTPUT_FORMAT} is not, then the last @code{TARGET}
2376 command is also used to set the format for the output file. @xref{BFD}.
2377 @end table
2378 @end ifclear
2379
2380 @node Miscellaneous Commands
2381 @subsection Other linker script commands
2382 There are a few other linker scripts commands.
2383
2384 @table @code
2385 @item ASSERT(@var{exp}, @var{message})
2386 @kindex ASSERT
2387 @cindex assertion in linker script
2388 Ensure that @var{exp} is non-zero. If it is zero, then exit the linker
2389 with an error code, and print @var{message}.
2390
2391 @item EXTERN(@var{symbol} @var{symbol} @dots{})
2392 @kindex EXTERN
2393 @cindex undefined symbol in linker script
2394 Force @var{symbol} to be entered in the output file as an undefined
2395 symbol. Doing this may, for example, trigger linking of additional
2396 modules from standard libraries. You may list several @var{symbol}s for
2397 each @code{EXTERN}, and you may use @code{EXTERN} multiple times. This
2398 command has the same effect as the @samp{-u} command-line option.
2399
2400 @item FORCE_COMMON_ALLOCATION
2401 @kindex FORCE_COMMON_ALLOCATION
2402 @cindex common allocation in linker script
2403 This command has the same effect as the @samp{-d} command-line option:
2404 to make @command{ld} assign space to common symbols even if a relocatable
2405 output file is specified (@samp{-r}).
2406
2407 @item INHIBIT_COMMON_ALLOCATION
2408 @kindex INHIBIT_COMMON_ALLOCATION
2409 @cindex common allocation in linker script
2410 This command has the same effect as the @samp{--no-define-common}
2411 command-line option: to make @code{ld} omit the assignment of addresses
2412 to common symbols even for a non-relocatable output file.
2413
2414 @item NOCROSSREFS(@var{section} @var{section} @dots{})
2415 @kindex NOCROSSREFS(@var{sections})
2416 @cindex cross references
2417 This command may be used to tell @command{ld} to issue an error about any
2418 references among certain output sections.
2419
2420 In certain types of programs, particularly on embedded systems when
2421 using overlays, when one section is loaded into memory, another section
2422 will not be. Any direct references between the two sections would be
2423 errors. For example, it would be an error if code in one section called
2424 a function defined in the other section.
2425
2426 The @code{NOCROSSREFS} command takes a list of output section names. If
2427 @command{ld} detects any cross references between the sections, it reports
2428 an error and returns a non-zero exit status. Note that the
2429 @code{NOCROSSREFS} command uses output section names, not input section
2430 names.
2431
2432 @ifclear SingleFormat
2433 @item OUTPUT_ARCH(@var{bfdarch})
2434 @kindex OUTPUT_ARCH(@var{bfdarch})
2435 @cindex machine architecture
2436 @cindex architecture
2437 Specify a particular output machine architecture. The argument is one
2438 of the names used by the BFD library (@pxref{BFD}). You can see the
2439 architecture of an object file by using the @code{objdump} program with
2440 the @samp{-f} option.
2441 @end ifclear
2442 @end table
2443
2444 @node Assignments
2445 @section Assigning Values to Symbols
2446 @cindex assignment in scripts
2447 @cindex symbol definition, scripts
2448 @cindex variables, defining
2449 You may assign a value to a symbol in a linker script. This will define
2450 the symbol as a global symbol.
2451
2452 @menu
2453 * Simple Assignments:: Simple Assignments
2454 * PROVIDE:: PROVIDE
2455 @end menu
2456
2457 @node Simple Assignments
2458 @subsection Simple Assignments
2459
2460 You may assign to a symbol using any of the C assignment operators:
2461
2462 @table @code
2463 @item @var{symbol} = @var{expression} ;
2464 @itemx @var{symbol} += @var{expression} ;
2465 @itemx @var{symbol} -= @var{expression} ;
2466 @itemx @var{symbol} *= @var{expression} ;
2467 @itemx @var{symbol} /= @var{expression} ;
2468 @itemx @var{symbol} <<= @var{expression} ;
2469 @itemx @var{symbol} >>= @var{expression} ;
2470 @itemx @var{symbol} &= @var{expression} ;
2471 @itemx @var{symbol} |= @var{expression} ;
2472 @end table
2473
2474 The first case will define @var{symbol} to the value of
2475 @var{expression}. In the other cases, @var{symbol} must already be
2476 defined, and the value will be adjusted accordingly.
2477
2478 The special symbol name @samp{.} indicates the location counter. You
2479 may only use this within a @code{SECTIONS} command.
2480
2481 The semicolon after @var{expression} is required.
2482
2483 Expressions are defined below; see @ref{Expressions}.
2484
2485 You may write symbol assignments as commands in their own right, or as
2486 statements within a @code{SECTIONS} command, or as part of an output
2487 section description in a @code{SECTIONS} command.
2488
2489 The section of the symbol will be set from the section of the
2490 expression; for more information, see @ref{Expression Section}.
2491
2492 Here is an example showing the three different places that symbol
2493 assignments may be used:
2494
2495 @smallexample
2496 floating_point = 0;
2497 SECTIONS
2498 @{
2499 .text :
2500 @{
2501 *(.text)
2502 _etext = .;
2503 @}
2504 _bdata = (. + 3) & ~ 3;
2505 .data : @{ *(.data) @}
2506 @}
2507 @end smallexample
2508 @noindent
2509 In this example, the symbol @samp{floating_point} will be defined as
2510 zero. The symbol @samp{_etext} will be defined as the address following
2511 the last @samp{.text} input section. The symbol @samp{_bdata} will be
2512 defined as the address following the @samp{.text} output section aligned
2513 upward to a 4 byte boundary.
2514
2515 @node PROVIDE
2516 @subsection PROVIDE
2517 @cindex PROVIDE
2518 In some cases, it is desirable for a linker script to define a symbol
2519 only if it is referenced and is not defined by any object included in
2520 the link. For example, traditional linkers defined the symbol
2521 @samp{etext}. However, ANSI C requires that the user be able to use
2522 @samp{etext} as a function name without encountering an error. The
2523 @code{PROVIDE} keyword may be used to define a symbol, such as
2524 @samp{etext}, only if it is referenced but not defined. The syntax is
2525 @code{PROVIDE(@var{symbol} = @var{expression})}.
2526
2527 Here is an example of using @code{PROVIDE} to define @samp{etext}:
2528 @smallexample
2529 SECTIONS
2530 @{
2531 .text :
2532 @{
2533 *(.text)
2534 _etext = .;
2535 PROVIDE(etext = .);
2536 @}
2537 @}
2538 @end smallexample
2539
2540 In this example, if the program defines @samp{_etext} (with a leading
2541 underscore), the linker will give a multiple definition error. If, on
2542 the other hand, the program defines @samp{etext} (with no leading
2543 underscore), the linker will silently use the definition in the program.
2544 If the program references @samp{etext} but does not define it, the
2545 linker will use the definition in the linker script.
2546
2547 @node SECTIONS
2548 @section SECTIONS command
2549 @kindex SECTIONS
2550 The @code{SECTIONS} command tells the linker how to map input sections
2551 into output sections, and how to place the output sections in memory.
2552
2553 The format of the @code{SECTIONS} command is:
2554 @smallexample
2555 SECTIONS
2556 @{
2557 @var{sections-command}
2558 @var{sections-command}
2559 @dots{}
2560 @}
2561 @end smallexample
2562
2563 Each @var{sections-command} may of be one of the following:
2564
2565 @itemize @bullet
2566 @item
2567 an @code{ENTRY} command (@pxref{Entry Point,,Entry command})
2568 @item
2569 a symbol assignment (@pxref{Assignments})
2570 @item
2571 an output section description
2572 @item
2573 an overlay description
2574 @end itemize
2575
2576 The @code{ENTRY} command and symbol assignments are permitted inside the
2577 @code{SECTIONS} command for convenience in using the location counter in
2578 those commands. This can also make the linker script easier to
2579 understand because you can use those commands at meaningful points in
2580 the layout of the output file.
2581
2582 Output section descriptions and overlay descriptions are described
2583 below.
2584
2585 If you do not use a @code{SECTIONS} command in your linker script, the
2586 linker will place each input section into an identically named output
2587 section in the order that the sections are first encountered in the
2588 input files. If all input sections are present in the first file, for
2589 example, the order of sections in the output file will match the order
2590 in the first input file. The first section will be at address zero.
2591
2592 @menu
2593 * Output Section Description:: Output section description
2594 * Output Section Name:: Output section name
2595 * Output Section Address:: Output section address
2596 * Input Section:: Input section description
2597 * Output Section Data:: Output section data
2598 * Output Section Keywords:: Output section keywords
2599 * Output Section Discarding:: Output section discarding
2600 * Output Section Attributes:: Output section attributes
2601 * Overlay Description:: Overlay description
2602 @end menu
2603
2604 @node Output Section Description
2605 @subsection Output section description
2606 The full description of an output section looks like this:
2607 @smallexample
2608 @group
2609 @var{section} [@var{address}] [(@var{type})] : [AT(@var{lma})]
2610 @{
2611 @var{output-section-command}
2612 @var{output-section-command}
2613 @dots{}
2614 @} [>@var{region}] [AT>@var{lma_region}] [:@var{phdr} :@var{phdr} @dots{}] [=@var{fillexp}]
2615 @end group
2616 @end smallexample
2617
2618 Most output sections do not use most of the optional section attributes.
2619
2620 The whitespace around @var{section} is required, so that the section
2621 name is unambiguous. The colon and the curly braces are also required.
2622 The line breaks and other white space are optional.
2623
2624 Each @var{output-section-command} may be one of the following:
2625
2626 @itemize @bullet
2627 @item
2628 a symbol assignment (@pxref{Assignments})
2629 @item
2630 an input section description (@pxref{Input Section})
2631 @item
2632 data values to include directly (@pxref{Output Section Data})
2633 @item
2634 a special output section keyword (@pxref{Output Section Keywords})
2635 @end itemize
2636
2637 @node Output Section Name
2638 @subsection Output section name
2639 @cindex name, section
2640 @cindex section name
2641 The name of the output section is @var{section}. @var{section} must
2642 meet the constraints of your output format. In formats which only
2643 support a limited number of sections, such as @code{a.out}, the name
2644 must be one of the names supported by the format (@code{a.out}, for
2645 example, allows only @samp{.text}, @samp{.data} or @samp{.bss}). If the
2646 output format supports any number of sections, but with numbers and not
2647 names (as is the case for Oasys), the name should be supplied as a
2648 quoted numeric string. A section name may consist of any sequence of
2649 characters, but a name which contains any unusual characters such as
2650 commas must be quoted.
2651
2652 The output section name @samp{/DISCARD/} is special; @ref{Output Section
2653 Discarding}.
2654
2655 @node Output Section Address
2656 @subsection Output section address
2657 @cindex address, section
2658 @cindex section address
2659 The @var{address} is an expression for the VMA (the virtual memory
2660 address) of the output section. If you do not provide @var{address},
2661 the linker will set it based on @var{region} if present, or otherwise
2662 based on the current value of the location counter.
2663
2664 If you provide @var{address}, the address of the output section will be
2665 set to precisely that. If you provide neither @var{address} nor
2666 @var{region}, then the address of the output section will be set to the
2667 current value of the location counter aligned to the alignment
2668 requirements of the output section. The alignment requirement of the
2669 output section is the strictest alignment of any input section contained
2670 within the output section.
2671
2672 For example,
2673 @smallexample
2674 .text . : @{ *(.text) @}
2675 @end smallexample
2676 @noindent
2677 and
2678 @smallexample
2679 .text : @{ *(.text) @}
2680 @end smallexample
2681 @noindent
2682 are subtly different. The first will set the address of the
2683 @samp{.text} output section to the current value of the location
2684 counter. The second will set it to the current value of the location
2685 counter aligned to the strictest alignment of a @samp{.text} input
2686 section.
2687
2688 The @var{address} may be an arbitrary expression; @ref{Expressions}.
2689 For example, if you want to align the section on a 0x10 byte boundary,
2690 so that the lowest four bits of the section address are zero, you could
2691 do something like this:
2692 @smallexample
2693 .text ALIGN(0x10) : @{ *(.text) @}
2694 @end smallexample
2695 @noindent
2696 This works because @code{ALIGN} returns the current location counter
2697 aligned upward to the specified value.
2698
2699 Specifying @var{address} for a section will change the value of the
2700 location counter.
2701
2702 @node Input Section
2703 @subsection Input section description
2704 @cindex input sections
2705 @cindex mapping input sections to output sections
2706 The most common output section command is an input section description.
2707
2708 The input section description is the most basic linker script operation.
2709 You use output sections to tell the linker how to lay out your program
2710 in memory. You use input section descriptions to tell the linker how to
2711 map the input files into your memory layout.
2712
2713 @menu
2714 * Input Section Basics:: Input section basics
2715 * Input Section Wildcards:: Input section wildcard patterns
2716 * Input Section Common:: Input section for common symbols
2717 * Input Section Keep:: Input section and garbage collection
2718 * Input Section Example:: Input section example
2719 @end menu
2720
2721 @node Input Section Basics
2722 @subsubsection Input section basics
2723 @cindex input section basics
2724 An input section description consists of a file name optionally followed
2725 by a list of section names in parentheses.
2726
2727 The file name and the section name may be wildcard patterns, which we
2728 describe further below (@pxref{Input Section Wildcards}).
2729
2730 The most common input section description is to include all input
2731 sections with a particular name in the output section. For example, to
2732 include all input @samp{.text} sections, you would write:
2733 @smallexample
2734 *(.text)
2735 @end smallexample
2736 @noindent
2737 Here the @samp{*} is a wildcard which matches any file name. To exclude a list
2738 of files from matching the file name wildcard, EXCLUDE_FILE may be used to
2739 match all files except the ones specified in the EXCLUDE_FILE list. For
2740 example:
2741 @smallexample
2742 (*(EXCLUDE_FILE (*crtend.o *otherfile.o) .ctors))
2743 @end smallexample
2744 will cause all .ctors sections from all files except @file{crtend.o} and
2745 @file{otherfile.o} to be included.
2746
2747 There are two ways to include more than one section:
2748 @smallexample
2749 *(.text .rdata)
2750 *(.text) *(.rdata)
2751 @end smallexample
2752 @noindent
2753 The difference between these is the order in which the @samp{.text} and
2754 @samp{.rdata} input sections will appear in the output section. In the
2755 first example, they will be intermingled, appearing in the same order as
2756 they are found in the linker input. In the second example, all
2757 @samp{.text} input sections will appear first, followed by all
2758 @samp{.rdata} input sections.
2759
2760 You can specify a file name to include sections from a particular file.
2761 You would do this if one or more of your files contain special data that
2762 needs to be at a particular location in memory. For example:
2763 @smallexample
2764 data.o(.data)
2765 @end smallexample
2766
2767 If you use a file name without a list of sections, then all sections in
2768 the input file will be included in the output section. This is not
2769 commonly done, but it may by useful on occasion. For example:
2770 @smallexample
2771 data.o
2772 @end smallexample
2773
2774 When you use a file name which does not contain any wild card
2775 characters, the linker will first see if you also specified the file
2776 name on the linker command line or in an @code{INPUT} command. If you
2777 did not, the linker will attempt to open the file as an input file, as
2778 though it appeared on the command line. Note that this differs from an
2779 @code{INPUT} command, because the linker will not search for the file in
2780 the archive search path.
2781
2782 @node Input Section Wildcards
2783 @subsubsection Input section wildcard patterns
2784 @cindex input section wildcards
2785 @cindex wildcard file name patterns
2786 @cindex file name wildcard patterns
2787 @cindex section name wildcard patterns
2788 In an input section description, either the file name or the section
2789 name or both may be wildcard patterns.
2790
2791 The file name of @samp{*} seen in many examples is a simple wildcard
2792 pattern for the file name.
2793
2794 The wildcard patterns are like those used by the Unix shell.
2795
2796 @table @samp
2797 @item *
2798 matches any number of characters
2799 @item ?
2800 matches any single character
2801 @item [@var{chars}]
2802 matches a single instance of any of the @var{chars}; the @samp{-}
2803 character may be used to specify a range of characters, as in
2804 @samp{[a-z]} to match any lower case letter
2805 @item \
2806 quotes the following character
2807 @end table
2808
2809 When a file name is matched with a wildcard, the wildcard characters
2810 will not match a @samp{/} character (used to separate directory names on
2811 Unix). A pattern consisting of a single @samp{*} character is an
2812 exception; it will always match any file name, whether it contains a
2813 @samp{/} or not. In a section name, the wildcard characters will match
2814 a @samp{/} character.
2815
2816 File name wildcard patterns only match files which are explicitly
2817 specified on the command line or in an @code{INPUT} command. The linker
2818 does not search directories to expand wildcards.
2819
2820 If a file name matches more than one wildcard pattern, or if a file name
2821 appears explicitly and is also matched by a wildcard pattern, the linker
2822 will use the first match in the linker script. For example, this
2823 sequence of input section descriptions is probably in error, because the
2824 @file{data.o} rule will not be used:
2825 @smallexample
2826 .data : @{ *(.data) @}
2827 .data1 : @{ data.o(.data) @}
2828 @end smallexample
2829
2830 @cindex SORT
2831 Normally, the linker will place files and sections matched by wildcards
2832 in the order in which they are seen during the link. You can change
2833 this by using the @code{SORT} keyword, which appears before a wildcard
2834 pattern in parentheses (e.g., @code{SORT(.text*)}). When the
2835 @code{SORT} keyword is used, the linker will sort the files or sections
2836 into ascending order by name before placing them in the output file.
2837
2838 If you ever get confused about where input sections are going, use the
2839 @samp{-M} linker option to generate a map file. The map file shows
2840 precisely how input sections are mapped to output sections.
2841
2842 This example shows how wildcard patterns might be used to partition
2843 files. This linker script directs the linker to place all @samp{.text}
2844 sections in @samp{.text} and all @samp{.bss} sections in @samp{.bss}.
2845 The linker will place the @samp{.data} section from all files beginning
2846 with an upper case character in @samp{.DATA}; for all other files, the
2847 linker will place the @samp{.data} section in @samp{.data}.
2848 @smallexample
2849 @group
2850 SECTIONS @{
2851 .text : @{ *(.text) @}
2852 .DATA : @{ [A-Z]*(.data) @}
2853 .data : @{ *(.data) @}
2854 .bss : @{ *(.bss) @}
2855 @}
2856 @end group
2857 @end smallexample
2858
2859 @node Input Section Common
2860 @subsubsection Input section for common symbols
2861 @cindex common symbol placement
2862 @cindex uninitialized data placement
2863 A special notation is needed for common symbols, because in many object
2864 file formats common symbols do not have a particular input section. The
2865 linker treats common symbols as though they are in an input section
2866 named @samp{COMMON}.
2867
2868 You may use file names with the @samp{COMMON} section just as with any
2869 other input sections. You can use this to place common symbols from a
2870 particular input file in one section while common symbols from other
2871 input files are placed in another section.
2872
2873 In most cases, common symbols in input files will be placed in the
2874 @samp{.bss} section in the output file. For example:
2875 @smallexample
2876 .bss @{ *(.bss) *(COMMON) @}
2877 @end smallexample
2878
2879 @cindex scommon section
2880 @cindex small common symbols
2881 Some object file formats have more than one type of common symbol. For
2882 example, the MIPS ELF object file format distinguishes standard common
2883 symbols and small common symbols. In this case, the linker will use a
2884 different special section name for other types of common symbols. In
2885 the case of MIPS ELF, the linker uses @samp{COMMON} for standard common
2886 symbols and @samp{.scommon} for small common symbols. This permits you
2887 to map the different types of common symbols into memory at different
2888 locations.
2889
2890 @cindex [COMMON]
2891 You will sometimes see @samp{[COMMON]} in old linker scripts. This
2892 notation is now considered obsolete. It is equivalent to
2893 @samp{*(COMMON)}.
2894
2895 @node Input Section Keep
2896 @subsubsection Input section and garbage collection
2897 @cindex KEEP
2898 @cindex garbage collection
2899 When link-time garbage collection is in use (@samp{--gc-sections}),
2900 it is often useful to mark sections that should not be eliminated.
2901 This is accomplished by surrounding an input section's wildcard entry
2902 with @code{KEEP()}, as in @code{KEEP(*(.init))} or
2903 @code{KEEP(SORT(*)(.ctors))}.
2904
2905 @node Input Section Example
2906 @subsubsection Input section example
2907 The following example is a complete linker script. It tells the linker
2908 to read all of the sections from file @file{all.o} and place them at the
2909 start of output section @samp{outputa} which starts at location
2910 @samp{0x10000}. All of section @samp{.input1} from file @file{foo.o}
2911 follows immediately, in the same output section. All of section
2912 @samp{.input2} from @file{foo.o} goes into output section
2913 @samp{outputb}, followed by section @samp{.input1} from @file{foo1.o}.
2914 All of the remaining @samp{.input1} and @samp{.input2} sections from any
2915 files are written to output section @samp{outputc}.
2916
2917 @smallexample
2918 @group
2919 SECTIONS @{
2920 outputa 0x10000 :
2921 @{
2922 all.o
2923 foo.o (.input1)
2924 @}
2925 outputb :
2926 @{
2927 foo.o (.input2)
2928 foo1.o (.input1)
2929 @}
2930 outputc :
2931 @{
2932 *(.input1)
2933 *(.input2)
2934 @}
2935 @}
2936 @end group
2937 @end smallexample
2938
2939 @node Output Section Data
2940 @subsection Output section data
2941 @cindex data
2942 @cindex section data
2943 @cindex output section data
2944 @kindex BYTE(@var{expression})
2945 @kindex SHORT(@var{expression})
2946 @kindex LONG(@var{expression})
2947 @kindex QUAD(@var{expression})
2948 @kindex SQUAD(@var{expression})
2949 You can include explicit bytes of data in an output section by using
2950 @code{BYTE}, @code{SHORT}, @code{LONG}, @code{QUAD}, or @code{SQUAD} as
2951 an output section command. Each keyword is followed by an expression in
2952 parentheses providing the value to store (@pxref{Expressions}). The
2953 value of the expression is stored at the current value of the location
2954 counter.
2955
2956 The @code{BYTE}, @code{SHORT}, @code{LONG}, and @code{QUAD} commands
2957 store one, two, four, and eight bytes (respectively). After storing the
2958 bytes, the location counter is incremented by the number of bytes
2959 stored.
2960
2961 For example, this will store the byte 1 followed by the four byte value
2962 of the symbol @samp{addr}:
2963 @smallexample
2964 BYTE(1)
2965 LONG(addr)
2966 @end smallexample
2967
2968 When using a 64 bit host or target, @code{QUAD} and @code{SQUAD} are the
2969 same; they both store an 8 byte, or 64 bit, value. When both host and
2970 target are 32 bits, an expression is computed as 32 bits. In this case
2971 @code{QUAD} stores a 32 bit value zero extended to 64 bits, and
2972 @code{SQUAD} stores a 32 bit value sign extended to 64 bits.
2973
2974 If the object file format of the output file has an explicit endianness,
2975 which is the normal case, the value will be stored in that endianness.
2976 When the object file format does not have an explicit endianness, as is
2977 true of, for example, S-records, the value will be stored in the
2978 endianness of the first input object file.
2979
2980 Note - these commands only work inside a section description and not
2981 between them, so the following will produce an error from the linker:
2982 @smallexample
2983 SECTIONS @{@ .text : @{@ *(.text) @}@ LONG(1) .data : @{@ *(.data) @}@ @}@
2984 @end smallexample
2985 whereas this will work:
2986 @smallexample
2987 SECTIONS @{@ .text : @{@ *(.text) ; LONG(1) @}@ .data : @{@ *(.data) @}@ @}@
2988 @end smallexample
2989
2990 @kindex FILL(@var{expression})
2991 @cindex holes, filling
2992 @cindex unspecified memory
2993 You may use the @code{FILL} command to set the fill pattern for the
2994 current section. It is followed by an expression in parentheses. Any
2995 otherwise unspecified regions of memory within the section (for example,
2996 gaps left due to the required alignment of input sections) are filled
2997 with the value of the expression, repeated as
2998 necessary. A @code{FILL} statement covers memory locations after the
2999 point at which it occurs in the section definition; by including more
3000 than one @code{FILL} statement, you can have different fill patterns in
3001 different parts of an output section.
3002
3003 This example shows how to fill unspecified regions of memory with the
3004 value @samp{0x90}:
3005 @smallexample
3006 FILL(0x90909090)
3007 @end smallexample
3008
3009 The @code{FILL} command is similar to the @samp{=@var{fillexp}} output
3010 section attribute, but it only affects the
3011 part of the section following the @code{FILL} command, rather than the
3012 entire section. If both are used, the @code{FILL} command takes
3013 precedence. @xref{Output Section Fill}, for details on the fill
3014 expression.
3015
3016 @node Output Section Keywords
3017 @subsection Output section keywords
3018 There are a couple of keywords which can appear as output section
3019 commands.
3020
3021 @table @code
3022 @kindex CREATE_OBJECT_SYMBOLS
3023 @cindex input filename symbols
3024 @cindex filename symbols
3025 @item CREATE_OBJECT_SYMBOLS
3026 The command tells the linker to create a symbol for each input file.
3027 The name of each symbol will be the name of the corresponding input
3028 file. The section of each symbol will be the output section in which
3029 the @code{CREATE_OBJECT_SYMBOLS} command appears.
3030
3031 This is conventional for the a.out object file format. It is not
3032 normally used for any other object file format.
3033
3034 @kindex CONSTRUCTORS
3035 @cindex C++ constructors, arranging in link
3036 @cindex constructors, arranging in link
3037 @item CONSTRUCTORS
3038 When linking using the a.out object file format, the linker uses an
3039 unusual set construct to support C++ global constructors and
3040 destructors. When linking object file formats which do not support
3041 arbitrary sections, such as ECOFF and XCOFF, the linker will
3042 automatically recognize C++ global constructors and destructors by name.
3043 For these object file formats, the @code{CONSTRUCTORS} command tells the
3044 linker to place constructor information in the output section where the
3045 @code{CONSTRUCTORS} command appears. The @code{CONSTRUCTORS} command is
3046 ignored for other object file formats.
3047
3048 The symbol @w{@code{__CTOR_LIST__}} marks the start of the global
3049 constructors, and the symbol @w{@code{__DTOR_LIST}} marks the end. The
3050 first word in the list is the number of entries, followed by the address
3051 of each constructor or destructor, followed by a zero word. The
3052 compiler must arrange to actually run the code. For these object file
3053 formats @sc{gnu} C++ normally calls constructors from a subroutine
3054 @code{__main}; a call to @code{__main} is automatically inserted into
3055 the startup code for @code{main}. @sc{gnu} C++ normally runs
3056 destructors either by using @code{atexit}, or directly from the function
3057 @code{exit}.
3058
3059 For object file formats such as @code{COFF} or @code{ELF} which support
3060 arbitrary section names, @sc{gnu} C++ will normally arrange to put the
3061 addresses of global constructors and destructors into the @code{.ctors}
3062 and @code{.dtors} sections. Placing the following sequence into your
3063 linker script will build the sort of table which the @sc{gnu} C++
3064 runtime code expects to see.
3065
3066 @smallexample
3067 __CTOR_LIST__ = .;
3068 LONG((__CTOR_END__ - __CTOR_LIST__) / 4 - 2)
3069 *(.ctors)
3070 LONG(0)
3071 __CTOR_END__ = .;
3072 __DTOR_LIST__ = .;
3073 LONG((__DTOR_END__ - __DTOR_LIST__) / 4 - 2)
3074 *(.dtors)
3075 LONG(0)
3076 __DTOR_END__ = .;
3077 @end smallexample
3078
3079 If you are using the @sc{gnu} C++ support for initialization priority,
3080 which provides some control over the order in which global constructors
3081 are run, you must sort the constructors at link time to ensure that they
3082 are executed in the correct order. When using the @code{CONSTRUCTORS}
3083 command, use @samp{SORT(CONSTRUCTORS)} instead. When using the
3084 @code{.ctors} and @code{.dtors} sections, use @samp{*(SORT(.ctors))} and
3085 @samp{*(SORT(.dtors))} instead of just @samp{*(.ctors)} and
3086 @samp{*(.dtors)}.
3087
3088 Normally the compiler and linker will handle these issues automatically,
3089 and you will not need to concern yourself with them. However, you may
3090 need to consider this if you are using C++ and writing your own linker
3091 scripts.
3092
3093 @end table
3094
3095 @node Output Section Discarding
3096 @subsection Output section discarding
3097 @cindex discarding sections
3098 @cindex sections, discarding
3099 @cindex removing sections
3100 The linker will not create output section which do not have any
3101 contents. This is for convenience when referring to input sections that
3102 may or may not be present in any of the input files. For example:
3103 @smallexample
3104 .foo @{ *(.foo) @}
3105 @end smallexample
3106 @noindent
3107 will only create a @samp{.foo} section in the output file if there is a
3108 @samp{.foo} section in at least one input file.
3109
3110 If you use anything other than an input section description as an output
3111 section command, such as a symbol assignment, then the output section
3112 will always be created, even if there are no matching input sections.
3113
3114 @cindex /DISCARD/
3115 The special output section name @samp{/DISCARD/} may be used to discard
3116 input sections. Any input sections which are assigned to an output
3117 section named @samp{/DISCARD/} are not included in the output file.
3118
3119 @node Output Section Attributes
3120 @subsection Output section attributes
3121 @cindex output section attributes
3122 We showed above that the full description of an output section looked
3123 like this:
3124 @smallexample
3125 @group
3126 @var{section} [@var{address}] [(@var{type})] : [AT(@var{lma})]
3127 @{
3128 @var{output-section-command}
3129 @var{output-section-command}
3130 @dots{}
3131 @} [>@var{region}] [AT>@var{lma_region}] [:@var{phdr} :@var{phdr} @dots{}] [=@var{fillexp}]
3132 @end group
3133 @end smallexample
3134 We've already described @var{section}, @var{address}, and
3135 @var{output-section-command}. In this section we will describe the
3136 remaining section attributes.
3137
3138 @menu
3139 * Output Section Type:: Output section type
3140 * Output Section LMA:: Output section LMA
3141 * Output Section Region:: Output section region
3142 * Output Section Phdr:: Output section phdr
3143 * Output Section Fill:: Output section fill
3144 @end menu
3145
3146 @node Output Section Type
3147 @subsubsection Output section type
3148 Each output section may have a type. The type is a keyword in
3149 parentheses. The following types are defined:
3150
3151 @table @code
3152 @item NOLOAD
3153 The section should be marked as not loadable, so that it will not be
3154 loaded into memory when the program is run.
3155 @item DSECT
3156 @itemx COPY
3157 @itemx INFO
3158 @itemx OVERLAY
3159 These type names are supported for backward compatibility, and are
3160 rarely used. They all have the same effect: the section should be
3161 marked as not allocatable, so that no memory is allocated for the
3162 section when the program is run.
3163 @end table
3164
3165 @kindex NOLOAD
3166 @cindex prevent unnecessary loading
3167 @cindex loading, preventing
3168 The linker normally sets the attributes of an output section based on
3169 the input sections which map into it. You can override this by using
3170 the section type. For example, in the script sample below, the
3171 @samp{ROM} section is addressed at memory location @samp{0} and does not
3172 need to be loaded when the program is run. The contents of the
3173 @samp{ROM} section will appear in the linker output file as usual.
3174 @smallexample
3175 @group
3176 SECTIONS @{
3177 ROM 0 (NOLOAD) : @{ @dots{} @}
3178 @dots{}
3179 @}
3180 @end group
3181 @end smallexample
3182
3183 @node Output Section LMA
3184 @subsubsection Output section LMA
3185 @kindex AT>@var{lma_region}
3186 @kindex AT(@var{lma})
3187 @cindex load address
3188 @cindex section load address
3189 Every section has a virtual address (VMA) and a load address (LMA); see
3190 @ref{Basic Script Concepts}. The address expression which may appear in
3191 an output section description sets the VMA (@pxref{Output Section
3192 Address}).
3193
3194 The linker will normally set the LMA equal to the VMA. You can change
3195 that by using the @code{AT} keyword. The expression @var{lma} that
3196 follows the @code{AT} keyword specifies the load address of the
3197 section. Alternatively, with @samp{AT>@var{lma_region}} expression,
3198 you may specify a memory region for the section's load address. @xref{MEMORY}.
3199
3200 @cindex ROM initialized data
3201 @cindex initialized data in ROM
3202 This feature is designed to make it easy to build a ROM image. For
3203 example, the following linker script creates three output sections: one
3204 called @samp{.text}, which starts at @code{0x1000}, one called
3205 @samp{.mdata}, which is loaded at the end of the @samp{.text} section
3206 even though its VMA is @code{0x2000}, and one called @samp{.bss} to hold
3207 uninitialized data at address @code{0x3000}. The symbol @code{_data} is
3208 defined with the value @code{0x2000}, which shows that the location
3209 counter holds the VMA value, not the LMA value.
3210
3211 @smallexample
3212 @group
3213 SECTIONS
3214 @{
3215 .text 0x1000 : @{ *(.text) _etext = . ; @}
3216 .mdata 0x2000 :
3217 AT ( ADDR (.text) + SIZEOF (.text) )
3218 @{ _data = . ; *(.data); _edata = . ; @}
3219 .bss 0x3000 :
3220 @{ _bstart = . ; *(.bss) *(COMMON) ; _bend = . ;@}
3221 @}
3222 @end group
3223 @end smallexample
3224
3225 The run-time initialization code for use with a program generated with
3226 this linker script would include something like the following, to copy
3227 the initialized data from the ROM image to its runtime address. Notice
3228 how this code takes advantage of the symbols defined by the linker
3229 script.
3230
3231 @smallexample
3232 @group
3233 extern char _etext, _data, _edata, _bstart, _bend;
3234 char *src = &_etext;
3235 char *dst = &_data;
3236
3237 /* ROM has data at end of text; copy it. */
3238 while (dst < &_edata) @{
3239 *dst++ = *src++;
3240 @}
3241
3242 /* Zero bss */
3243 for (dst = &_bstart; dst< &_bend; dst++)
3244 *dst = 0;
3245 @end group
3246 @end smallexample
3247
3248 @node Output Section Region
3249 @subsubsection Output section region
3250 @kindex >@var{region}
3251 @cindex section, assigning to memory region
3252 @cindex memory regions and sections
3253 You can assign a section to a previously defined region of memory by
3254 using @samp{>@var{region}}. @xref{MEMORY}.
3255
3256 Here is a simple example:
3257 @smallexample
3258 @group
3259 MEMORY @{ rom : ORIGIN = 0x1000, LENGTH = 0x1000 @}
3260 SECTIONS @{ ROM : @{ *(.text) @} >rom @}
3261 @end group
3262 @end smallexample
3263
3264 @node Output Section Phdr
3265 @subsubsection Output section phdr
3266 @kindex :@var{phdr}
3267 @cindex section, assigning to program header
3268 @cindex program headers and sections
3269 You can assign a section to a previously defined program segment by
3270 using @samp{:@var{phdr}}. @xref{PHDRS}. If a section is assigned to
3271 one or more segments, then all subsequent allocated sections will be
3272 assigned to those segments as well, unless they use an explicitly
3273 @code{:@var{phdr}} modifier. You can use @code{:NONE} to tell the
3274 linker to not put the section in any segment at all.
3275
3276 Here is a simple example:
3277 @smallexample
3278 @group
3279 PHDRS @{ text PT_LOAD ; @}
3280 SECTIONS @{ .text : @{ *(.text) @} :text @}
3281 @end group
3282 @end smallexample
3283
3284 @node Output Section Fill
3285 @subsubsection Output section fill
3286 @kindex =@var{fillexp}
3287 @cindex section fill pattern
3288 @cindex fill pattern, entire section
3289 You can set the fill pattern for an entire section by using
3290 @samp{=@var{fillexp}}. @var{fillexp} is an expression
3291 (@pxref{Expressions}). Any otherwise unspecified regions of memory
3292 within the output section (for example, gaps left due to the required
3293 alignment of input sections) will be filled with the value, repeated as
3294 necessary. If the fill expression is a simple hex number, ie. a string
3295 of hex digit starting with @samp{0x} and without a trailing @samp{k} or @samp{M}, then
3296 an arbitrarily long sequence of hex digits can be used to specify the
3297 fill pattern; Leading zeros become part of the pattern too. For all
3298 other cases, including extra parentheses or a unary @code{+}, the fill
3299 pattern is the four least significant bytes of the value of the
3300 expression. In all cases, the number is big-endian.
3301
3302 You can also change the fill value with a @code{FILL} command in the
3303 output section commands; (@pxref{Output Section Data}).
3304
3305 Here is a simple example:
3306 @smallexample
3307 @group
3308 SECTIONS @{ .text : @{ *(.text) @} =0x90909090 @}
3309 @end group
3310 @end smallexample
3311
3312 @node Overlay Description
3313 @subsection Overlay description
3314 @kindex OVERLAY
3315 @cindex overlays
3316 An overlay description provides an easy way to describe sections which
3317 are to be loaded as part of a single memory image but are to be run at
3318 the same memory address. At run time, some sort of overlay manager will
3319 copy the overlaid sections in and out of the runtime memory address as
3320 required, perhaps by simply manipulating addressing bits. This approach
3321 can be useful, for example, when a certain region of memory is faster
3322 than another.
3323
3324 Overlays are described using the @code{OVERLAY} command. The
3325 @code{OVERLAY} command is used within a @code{SECTIONS} command, like an
3326 output section description. The full syntax of the @code{OVERLAY}
3327 command is as follows:
3328 @smallexample
3329 @group
3330 OVERLAY [@var{start}] : [NOCROSSREFS] [AT ( @var{ldaddr} )]
3331 @{
3332 @var{secname1}
3333 @{
3334 @var{output-section-command}
3335 @var{output-section-command}
3336 @dots{}
3337 @} [:@var{phdr}@dots{}] [=@var{fill}]
3338 @var{secname2}
3339 @{
3340 @var{output-section-command}
3341 @var{output-section-command}
3342 @dots{}
3343 @} [:@var{phdr}@dots{}] [=@var{fill}]
3344 @dots{}
3345 @} [>@var{region}] [:@var{phdr}@dots{}] [=@var{fill}]
3346 @end group
3347 @end smallexample
3348
3349 Everything is optional except @code{OVERLAY} (a keyword), and each
3350 section must have a name (@var{secname1} and @var{secname2} above). The
3351 section definitions within the @code{OVERLAY} construct are identical to
3352 those within the general @code{SECTIONS} contruct (@pxref{SECTIONS}),
3353 except that no addresses and no memory regions may be defined for
3354 sections within an @code{OVERLAY}.
3355
3356 The sections are all defined with the same starting address. The load
3357 addresses of the sections are arranged such that they are consecutive in
3358 memory starting at the load address used for the @code{OVERLAY} as a
3359 whole (as with normal section definitions, the load address is optional,
3360 and defaults to the start address; the start address is also optional,
3361 and defaults to the current value of the location counter).
3362
3363 If the @code{NOCROSSREFS} keyword is used, and there any references
3364 among the sections, the linker will report an error. Since the sections
3365 all run at the same address, it normally does not make sense for one
3366 section to refer directly to another. @xref{Miscellaneous Commands,
3367 NOCROSSREFS}.
3368
3369 For each section within the @code{OVERLAY}, the linker automatically
3370 defines two symbols. The symbol @code{__load_start_@var{secname}} is
3371 defined as the starting load address of the section. The symbol
3372 @code{__load_stop_@var{secname}} is defined as the final load address of
3373 the section. Any characters within @var{secname} which are not legal
3374 within C identifiers are removed. C (or assembler) code may use these
3375 symbols to move the overlaid sections around as necessary.
3376
3377 At the end of the overlay, the value of the location counter is set to
3378 the start address of the overlay plus the size of the largest section.
3379
3380 Here is an example. Remember that this would appear inside a
3381 @code{SECTIONS} construct.
3382 @smallexample
3383 @group
3384 OVERLAY 0x1000 : AT (0x4000)
3385 @{
3386 .text0 @{ o1/*.o(.text) @}
3387 .text1 @{ o2/*.o(.text) @}
3388 @}
3389 @end group
3390 @end smallexample
3391 @noindent
3392 This will define both @samp{.text0} and @samp{.text1} to start at
3393 address 0x1000. @samp{.text0} will be loaded at address 0x4000, and
3394 @samp{.text1} will be loaded immediately after @samp{.text0}. The
3395 following symbols will be defined: @code{__load_start_text0},
3396 @code{__load_stop_text0}, @code{__load_start_text1},
3397 @code{__load_stop_text1}.
3398
3399 C code to copy overlay @code{.text1} into the overlay area might look
3400 like the following.
3401
3402 @smallexample
3403 @group
3404 extern char __load_start_text1, __load_stop_text1;
3405 memcpy ((char *) 0x1000, &__load_start_text1,
3406 &__load_stop_text1 - &__load_start_text1);
3407 @end group
3408 @end smallexample
3409
3410 Note that the @code{OVERLAY} command is just syntactic sugar, since
3411 everything it does can be done using the more basic commands. The above
3412 example could have been written identically as follows.
3413
3414 @smallexample
3415 @group
3416 .text0 0x1000 : AT (0x4000) @{ o1/*.o(.text) @}
3417 __load_start_text0 = LOADADDR (.text0);
3418 __load_stop_text0 = LOADADDR (.text0) + SIZEOF (.text0);
3419 .text1 0x1000 : AT (0x4000 + SIZEOF (.text0)) @{ o2/*.o(.text) @}
3420 __load_start_text1 = LOADADDR (.text1);
3421 __load_stop_text1 = LOADADDR (.text1) + SIZEOF (.text1);
3422 . = 0x1000 + MAX (SIZEOF (.text0), SIZEOF (.text1));
3423 @end group
3424 @end smallexample
3425
3426 @node MEMORY
3427 @section MEMORY command
3428 @kindex MEMORY
3429 @cindex memory regions
3430 @cindex regions of memory
3431 @cindex allocating memory
3432 @cindex discontinuous memory
3433 The linker's default configuration permits allocation of all available
3434 memory. You can override this by using the @code{MEMORY} command.
3435
3436 The @code{MEMORY} command describes the location and size of blocks of
3437 memory in the target. You can use it to describe which memory regions
3438 may be used by the linker, and which memory regions it must avoid. You
3439 can then assign sections to particular memory regions. The linker will
3440 set section addresses based on the memory regions, and will warn about
3441 regions that become too full. The linker will not shuffle sections
3442 around to fit into the available regions.
3443
3444 A linker script may contain at most one use of the @code{MEMORY}
3445 command. However, you can define as many blocks of memory within it as
3446 you wish. The syntax is:
3447 @smallexample
3448 @group
3449 MEMORY
3450 @{
3451 @var{name} [(@var{attr})] : ORIGIN = @var{origin}, LENGTH = @var{len}
3452 @dots{}
3453 @}
3454 @end group
3455 @end smallexample
3456
3457 The @var{name} is a name used in the linker script to refer to the
3458 region. The region name has no meaning outside of the linker script.
3459 Region names are stored in a separate name space, and will not conflict
3460 with symbol names, file names, or section names. Each memory region
3461 must have a distinct name.
3462
3463 @cindex memory region attributes
3464 The @var{attr} string is an optional list of attributes that specify
3465 whether to use a particular memory region for an input section which is
3466 not explicitly mapped in the linker script. As described in
3467 @ref{SECTIONS}, if you do not specify an output section for some input
3468 section, the linker will create an output section with the same name as
3469 the input section. If you define region attributes, the linker will use
3470 them to select the memory region for the output section that it creates.
3471
3472 The @var{attr} string must consist only of the following characters:
3473 @table @samp
3474 @item R
3475 Read-only section
3476 @item W
3477 Read/write section
3478 @item X
3479 Executable section
3480 @item A
3481 Allocatable section
3482 @item I
3483 Initialized section
3484 @item L
3485 Same as @samp{I}
3486 @item !
3487 Invert the sense of any of the preceding attributes
3488 @end table
3489
3490 If a unmapped section matches any of the listed attributes other than
3491 @samp{!}, it will be placed in the memory region. The @samp{!}
3492 attribute reverses this test, so that an unmapped section will be placed
3493 in the memory region only if it does not match any of the listed
3494 attributes.
3495
3496 @kindex ORIGIN =
3497 @kindex o =
3498 @kindex org =
3499 The @var{origin} is an expression for the start address of the memory
3500 region. The expression must evaluate to a constant before memory
3501 allocation is performed, which means that you may not use any section
3502 relative symbols. The keyword @code{ORIGIN} may be abbreviated to
3503 @code{org} or @code{o} (but not, for example, @code{ORG}).
3504
3505 @kindex LENGTH =
3506 @kindex len =
3507 @kindex l =
3508 The @var{len} is an expression for the size in bytes of the memory
3509 region. As with the @var{origin} expression, the expression must
3510 evaluate to a constant before memory allocation is performed. The
3511 keyword @code{LENGTH} may be abbreviated to @code{len} or @code{l}.
3512
3513 In the following example, we specify that there are two memory regions
3514 available for allocation: one starting at @samp{0} for 256 kilobytes,
3515 and the other starting at @samp{0x40000000} for four megabytes. The
3516 linker will place into the @samp{rom} memory region every section which
3517 is not explicitly mapped into a memory region, and is either read-only
3518 or executable. The linker will place other sections which are not
3519 explicitly mapped into a memory region into the @samp{ram} memory
3520 region.
3521
3522 @smallexample
3523 @group
3524 MEMORY
3525 @{
3526 rom (rx) : ORIGIN = 0, LENGTH = 256K
3527 ram (!rx) : org = 0x40000000, l = 4M
3528 @}
3529 @end group
3530 @end smallexample
3531
3532 Once you define a memory region, you can direct the linker to place
3533 specific output sections into that memory region by using the
3534 @samp{>@var{region}} output section attribute. For example, if you have
3535 a memory region named @samp{mem}, you would use @samp{>mem} in the
3536 output section definition. @xref{Output Section Region}. If no address
3537 was specified for the output section, the linker will set the address to
3538 the next available address within the memory region. If the combined
3539 output sections directed to a memory region are too large for the
3540 region, the linker will issue an error message.
3541
3542 @node PHDRS
3543 @section PHDRS Command
3544 @kindex PHDRS
3545 @cindex program headers
3546 @cindex ELF program headers
3547 @cindex program segments
3548 @cindex segments, ELF
3549 The ELF object file format uses @dfn{program headers}, also knows as
3550 @dfn{segments}. The program headers describe how the program should be
3551 loaded into memory. You can print them out by using the @code{objdump}
3552 program with the @samp{-p} option.
3553
3554 When you run an ELF program on a native ELF system, the system loader
3555 reads the program headers in order to figure out how to load the
3556 program. This will only work if the program headers are set correctly.
3557 This manual does not describe the details of how the system loader
3558 interprets program headers; for more information, see the ELF ABI.
3559
3560 The linker will create reasonable program headers by default. However,
3561 in some cases, you may need to specify the program headers more
3562 precisely. You may use the @code{PHDRS} command for this purpose. When
3563 the linker sees the @code{PHDRS} command in the linker script, it will
3564 not create any program headers other than the ones specified.
3565
3566 The linker only pays attention to the @code{PHDRS} command when
3567 generating an ELF output file. In other cases, the linker will simply
3568 ignore @code{PHDRS}.
3569
3570 This is the syntax of the @code{PHDRS} command. The words @code{PHDRS},
3571 @code{FILEHDR}, @code{AT}, and @code{FLAGS} are keywords.
3572
3573 @smallexample
3574 @group
3575 PHDRS
3576 @{
3577 @var{name} @var{type} [ FILEHDR ] [ PHDRS ] [ AT ( @var{address} ) ]
3578 [ FLAGS ( @var{flags} ) ] ;
3579 @}
3580 @end group
3581 @end smallexample
3582
3583 The @var{name} is used only for reference in the @code{SECTIONS} command
3584 of the linker script. It is not put into the output file. Program
3585 header names are stored in a separate name space, and will not conflict
3586 with symbol names, file names, or section names. Each program header
3587 must have a distinct name.
3588
3589 Certain program header types describe segments of memory which the
3590 system loader will load from the file. In the linker script, you
3591 specify the contents of these segments by placing allocatable output
3592 sections in the segments. You use the @samp{:@var{phdr}} output section
3593 attribute to place a section in a particular segment. @xref{Output
3594 Section Phdr}.
3595
3596 It is normal to put certain sections in more than one segment. This
3597 merely implies that one segment of memory contains another. You may
3598 repeat @samp{:@var{phdr}}, using it once for each segment which should
3599 contain the section.
3600
3601 If you place a section in one or more segments using @samp{:@var{phdr}},
3602 then the linker will place all subsequent allocatable sections which do
3603 not specify @samp{:@var{phdr}} in the same segments. This is for
3604 convenience, since generally a whole set of contiguous sections will be
3605 placed in a single segment. You can use @code{:NONE} to override the
3606 default segment and tell the linker to not put the section in any
3607 segment at all.
3608
3609 @kindex FILEHDR
3610 @kindex PHDRS
3611 You may use the @code{FILEHDR} and @code{PHDRS} keywords appear after
3612 the program header type to further describe the contents of the segment.
3613 The @code{FILEHDR} keyword means that the segment should include the ELF
3614 file header. The @code{PHDRS} keyword means that the segment should
3615 include the ELF program headers themselves.
3616
3617 The @var{type} may be one of the following. The numbers indicate the
3618 value of the keyword.
3619
3620 @table @asis
3621 @item @code{PT_NULL} (0)
3622 Indicates an unused program header.
3623
3624 @item @code{PT_LOAD} (1)
3625 Indicates that this program header describes a segment to be loaded from
3626 the file.
3627
3628 @item @code{PT_DYNAMIC} (2)
3629 Indicates a segment where dynamic linking information can be found.
3630
3631 @item @code{PT_INTERP} (3)
3632 Indicates a segment where the name of the program interpreter may be
3633 found.
3634
3635 @item @code{PT_NOTE} (4)
3636 Indicates a segment holding note information.
3637
3638 @item @code{PT_SHLIB} (5)
3639 A reserved program header type, defined but not specified by the ELF
3640 ABI.
3641
3642 @item @code{PT_PHDR} (6)
3643 Indicates a segment where the program headers may be found.
3644
3645 @item @var{expression}
3646 An expression giving the numeric type of the program header. This may
3647 be used for types not defined above.
3648 @end table
3649
3650 You can specify that a segment should be loaded at a particular address
3651 in memory by using an @code{AT} expression. This is identical to the
3652 @code{AT} command used as an output section attribute (@pxref{Output
3653 Section LMA}). The @code{AT} command for a program header overrides the
3654 output section attribute.
3655
3656 The linker will normally set the segment flags based on the sections
3657 which comprise the segment. You may use the @code{FLAGS} keyword to
3658 explicitly specify the segment flags. The value of @var{flags} must be
3659 an integer. It is used to set the @code{p_flags} field of the program
3660 header.
3661
3662 Here is an example of @code{PHDRS}. This shows a typical set of program
3663 headers used on a native ELF system.
3664
3665 @example
3666 @group
3667 PHDRS
3668 @{
3669 headers PT_PHDR PHDRS ;
3670 interp PT_INTERP ;
3671 text PT_LOAD FILEHDR PHDRS ;
3672 data PT_LOAD ;
3673 dynamic PT_DYNAMIC ;
3674 @}
3675
3676 SECTIONS
3677 @{
3678 . = SIZEOF_HEADERS;
3679 .interp : @{ *(.interp) @} :text :interp
3680 .text : @{ *(.text) @} :text
3681 .rodata : @{ *(.rodata) @} /* defaults to :text */
3682 @dots{}
3683 . = . + 0x1000; /* move to a new page in memory */
3684 .data : @{ *(.data) @} :data
3685 .dynamic : @{ *(.dynamic) @} :data :dynamic
3686 @dots{}
3687 @}
3688 @end group
3689 @end example
3690
3691 @node VERSION
3692 @section VERSION Command
3693 @kindex VERSION @{script text@}
3694 @cindex symbol versions
3695 @cindex version script
3696 @cindex versions of symbols
3697 The linker supports symbol versions when using ELF. Symbol versions are
3698 only useful when using shared libraries. The dynamic linker can use
3699 symbol versions to select a specific version of a function when it runs
3700 a program that may have been linked against an earlier version of the
3701 shared library.
3702
3703 You can include a version script directly in the main linker script, or
3704 you can supply the version script as an implicit linker script. You can
3705 also use the @samp{--version-script} linker option.
3706
3707 The syntax of the @code{VERSION} command is simply
3708 @smallexample
3709 VERSION @{ version-script-commands @}
3710 @end smallexample
3711
3712 The format of the version script commands is identical to that used by
3713 Sun's linker in Solaris 2.5. The version script defines a tree of
3714 version nodes. You specify the node names and interdependencies in the
3715 version script. You can specify which symbols are bound to which
3716 version nodes, and you can reduce a specified set of symbols to local
3717 scope so that they are not globally visible outside of the shared
3718 library.
3719
3720 The easiest way to demonstrate the version script language is with a few
3721 examples.
3722
3723 @smallexample
3724 VERS_1.1 @{
3725 global:
3726 foo1;
3727 local:
3728 old*;
3729 original*;
3730 new*;
3731 @};
3732
3733 VERS_1.2 @{
3734 foo2;
3735 @} VERS_1.1;
3736
3737 VERS_2.0 @{
3738 bar1; bar2;
3739 @} VERS_1.2;
3740 @end smallexample
3741
3742 This example version script defines three version nodes. The first
3743 version node defined is @samp{VERS_1.1}; it has no other dependencies.
3744 The script binds the symbol @samp{foo1} to @samp{VERS_1.1}. It reduces
3745 a number of symbols to local scope so that they are not visible outside
3746 of the shared library; this is done using wildcard patterns, so that any
3747 symbol whose name begins with @samp{old}, @samp{original}, or @samp{new}
3748 is matched. The wildcard patterns available are the same as those used
3749 in the shell when matching filenames (also known as ``globbing'').
3750
3751 Next, the version script defines node @samp{VERS_1.2}. This node
3752 depends upon @samp{VERS_1.1}. The script binds the symbol @samp{foo2}
3753 to the version node @samp{VERS_1.2}.
3754
3755 Finally, the version script defines node @samp{VERS_2.0}. This node
3756 depends upon @samp{VERS_1.2}. The scripts binds the symbols @samp{bar1}
3757 and @samp{bar2} are bound to the version node @samp{VERS_2.0}.
3758
3759 When the linker finds a symbol defined in a library which is not
3760 specifically bound to a version node, it will effectively bind it to an
3761 unspecified base version of the library. You can bind all otherwise
3762 unspecified symbols to a given version node by using @samp{global: *;}
3763 somewhere in the version script.
3764
3765 The names of the version nodes have no specific meaning other than what
3766 they might suggest to the person reading them. The @samp{2.0} version
3767 could just as well have appeared in between @samp{1.1} and @samp{1.2}.
3768 However, this would be a confusing way to write a version script.
3769
3770 Node name can be omited, provided it is the only version node
3771 in the version script. Such version script doesn't assign any versions to
3772 symbols, only selects which symbols will be globally visible out and which
3773 won't.
3774
3775 @smallexample
3776 @{ global: foo; bar; local: *; @};
3777 @end smallexample
3778
3779 When you link an application against a shared library that has versioned
3780 symbols, the application itself knows which version of each symbol it
3781 requires, and it also knows which version nodes it needs from each
3782 shared library it is linked against. Thus at runtime, the dynamic
3783 loader can make a quick check to make sure that the libraries you have
3784 linked against do in fact supply all of the version nodes that the
3785 application will need to resolve all of the dynamic symbols. In this
3786 way it is possible for the dynamic linker to know with certainty that
3787 all external symbols that it needs will be resolvable without having to
3788 search for each symbol reference.
3789
3790 The symbol versioning is in effect a much more sophisticated way of
3791 doing minor version checking that SunOS does. The fundamental problem
3792 that is being addressed here is that typically references to external
3793 functions are bound on an as-needed basis, and are not all bound when
3794 the application starts up. If a shared library is out of date, a
3795 required interface may be missing; when the application tries to use
3796 that interface, it may suddenly and unexpectedly fail. With symbol
3797 versioning, the user will get a warning when they start their program if
3798 the libraries being used with the application are too old.
3799
3800 There are several GNU extensions to Sun's versioning approach. The
3801 first of these is the ability to bind a symbol to a version node in the
3802 source file where the symbol is defined instead of in the versioning
3803 script. This was done mainly to reduce the burden on the library
3804 maintainer. You can do this by putting something like:
3805 @smallexample
3806 __asm__(".symver original_foo,foo@@VERS_1.1");
3807 @end smallexample
3808 @noindent
3809 in the C source file. This renames the function @samp{original_foo} to
3810 be an alias for @samp{foo} bound to the version node @samp{VERS_1.1}.
3811 The @samp{local:} directive can be used to prevent the symbol
3812 @samp{original_foo} from being exported. A @samp{.symver} directive
3813 takes precedence over a version script.
3814
3815 The second GNU extension is to allow multiple versions of the same
3816 function to appear in a given shared library. In this way you can make
3817 an incompatible change to an interface without increasing the major
3818 version number of the shared library, while still allowing applications
3819 linked against the old interface to continue to function.
3820
3821 To do this, you must use multiple @samp{.symver} directives in the
3822 source file. Here is an example:
3823
3824 @smallexample
3825 __asm__(".symver original_foo,foo@@");
3826 __asm__(".symver old_foo,foo@@VERS_1.1");
3827 __asm__(".symver old_foo1,foo@@VERS_1.2");
3828 __asm__(".symver new_foo,foo@@@@VERS_2.0");
3829 @end smallexample
3830
3831 In this example, @samp{foo@@} represents the symbol @samp{foo} bound to the
3832 unspecified base version of the symbol. The source file that contains this
3833 example would define 4 C functions: @samp{original_foo}, @samp{old_foo},
3834 @samp{old_foo1}, and @samp{new_foo}.
3835
3836 When you have multiple definitions of a given symbol, there needs to be
3837 some way to specify a default version to which external references to
3838 this symbol will be bound. You can do this with the
3839 @samp{foo@@@@VERS_2.0} type of @samp{.symver} directive. You can only
3840 declare one version of a symbol as the default in this manner; otherwise
3841 you would effectively have multiple definitions of the same symbol.
3842
3843 If you wish to bind a reference to a specific version of the symbol
3844 within the shared library, you can use the aliases of convenience
3845 (i.e. @samp{old_foo}), or you can use the @samp{.symver} directive to
3846 specifically bind to an external version of the function in question.
3847
3848 You can also specify the language in the version script:
3849
3850 @smallexample
3851 VERSION extern "lang" @{ version-script-commands @}
3852 @end smallexample
3853
3854 The supported @samp{lang}s are @samp{C}, @samp{C++}, and @samp{Java}.
3855 The linker will iterate over the list of symbols at the link time and
3856 demangle them according to @samp{lang} before matching them to the
3857 patterns specified in @samp{version-script-commands}.
3858
3859 @node Expressions
3860 @section Expressions in Linker Scripts
3861 @cindex expressions
3862 @cindex arithmetic
3863 The syntax for expressions in the linker script language is identical to
3864 that of C expressions. All expressions are evaluated as integers. All
3865 expressions are evaluated in the same size, which is 32 bits if both the
3866 host and target are 32 bits, and is otherwise 64 bits.
3867
3868 You can use and set symbol values in expressions.
3869
3870 The linker defines several special purpose builtin functions for use in
3871 expressions.
3872
3873 @menu
3874 * Constants:: Constants
3875 * Symbols:: Symbol Names
3876 * Location Counter:: The Location Counter
3877 * Operators:: Operators
3878 * Evaluation:: Evaluation
3879 * Expression Section:: The Section of an Expression
3880 * Builtin Functions:: Builtin Functions
3881 @end menu
3882
3883 @node Constants
3884 @subsection Constants
3885 @cindex integer notation
3886 @cindex constants in linker scripts
3887 All constants are integers.
3888
3889 As in C, the linker considers an integer beginning with @samp{0} to be
3890 octal, and an integer beginning with @samp{0x} or @samp{0X} to be
3891 hexadecimal. The linker considers other integers to be decimal.
3892
3893 @cindex scaled integers
3894 @cindex K and M integer suffixes
3895 @cindex M and K integer suffixes
3896 @cindex suffixes for integers
3897 @cindex integer suffixes
3898 In addition, you can use the suffixes @code{K} and @code{M} to scale a
3899 constant by
3900 @c TEXI2ROFF-KILL
3901 @ifinfo
3902 @c END TEXI2ROFF-KILL
3903 @code{1024} or @code{1024*1024}
3904 @c TEXI2ROFF-KILL
3905 @end ifinfo
3906 @tex
3907 ${\rm 1024}$ or ${\rm 1024}^2$
3908 @end tex
3909 @c END TEXI2ROFF-KILL
3910 respectively. For example, the following all refer to the same quantity:
3911 @smallexample
3912 _fourk_1 = 4K;
3913 _fourk_2 = 4096;
3914 _fourk_3 = 0x1000;
3915 @end smallexample
3916
3917 @node Symbols
3918 @subsection Symbol Names
3919 @cindex symbol names
3920 @cindex names
3921 @cindex quoted symbol names
3922 @kindex "
3923 Unless quoted, symbol names start with a letter, underscore, or period
3924 and may include letters, digits, underscores, periods, and hyphens.
3925 Unquoted symbol names must not conflict with any keywords. You can
3926 specify a symbol which contains odd characters or has the same name as a
3927 keyword by surrounding the symbol name in double quotes:
3928 @smallexample
3929 "SECTION" = 9;
3930 "with a space" = "also with a space" + 10;
3931 @end smallexample
3932
3933 Since symbols can contain many non-alphabetic characters, it is safest
3934 to delimit symbols with spaces. For example, @samp{A-B} is one symbol,
3935 whereas @samp{A - B} is an expression involving subtraction.
3936
3937 @node Location Counter
3938 @subsection The Location Counter
3939 @kindex .
3940 @cindex dot
3941 @cindex location counter
3942 @cindex current output location
3943 The special linker variable @dfn{dot} @samp{.} always contains the
3944 current output location counter. Since the @code{.} always refers to a
3945 location in an output section, it may only appear in an expression
3946 within a @code{SECTIONS} command. The @code{.} symbol may appear
3947 anywhere that an ordinary symbol is allowed in an expression.
3948
3949 @cindex holes
3950 Assigning a value to @code{.} will cause the location counter to be
3951 moved. This may be used to create holes in the output section. The
3952 location counter may never be moved backwards.
3953
3954 @smallexample
3955 SECTIONS
3956 @{
3957 output :
3958 @{
3959 file1(.text)
3960 . = . + 1000;
3961 file2(.text)
3962 . += 1000;
3963 file3(.text)
3964 @} = 0x12345678;
3965 @}
3966 @end smallexample
3967 @noindent
3968 In the previous example, the @samp{.text} section from @file{file1} is
3969 located at the beginning of the output section @samp{output}. It is
3970 followed by a 1000 byte gap. Then the @samp{.text} section from
3971 @file{file2} appears, also with a 1000 byte gap following before the
3972 @samp{.text} section from @file{file3}. The notation @samp{= 0x12345678}
3973 specifies what data to write in the gaps (@pxref{Output Section Fill}).
3974
3975 @cindex dot inside sections
3976 Note: @code{.} actually refers to the byte offset from the start of the
3977 current containing object. Normally this is the @code{SECTIONS}
3978 statement, whoes start address is 0, hence @code{.} can be used as an
3979 absolute address. If @code{.} is used inside a section description
3980 however, it refers to the byte offset from the start of that section,
3981 not an absolute address. Thus in a script like this:
3982
3983 @smallexample
3984 SECTIONS
3985 @{
3986 . = 0x100
3987 .text: @{
3988 *(.text)
3989 . = 0x200
3990 @}
3991 . = 0x500
3992 .data: @{
3993 *(.data)
3994 . += 0x600
3995 @}
3996 @}
3997 @end smallexample
3998
3999 The @samp{.text} section will be assigned a starting address of 0x100
4000 and a size of exactly 0x200 bytes, even if there is not enough data in
4001 the @samp{.text} input sections to fill this area. (If there is too
4002 much data, an error will be produced because this would be an attempt to
4003 move @code{.} backwards). The @samp{.data} section will start at 0x500
4004 and it will have an extra 0x600 bytes worth of space after the end of
4005 the values from the @samp{.data} input sections and before the end of
4006 the @samp{.data} output section itself.
4007
4008 @need 2000
4009 @node Operators
4010 @subsection Operators
4011 @cindex operators for arithmetic
4012 @cindex arithmetic operators
4013 @cindex precedence in expressions
4014 The linker recognizes the standard C set of arithmetic operators, with
4015 the standard bindings and precedence levels:
4016 @c TEXI2ROFF-KILL
4017 @ifinfo
4018 @c END TEXI2ROFF-KILL
4019 @smallexample
4020 precedence associativity Operators Notes
4021 (highest)
4022 1 left ! - ~ (1)
4023 2 left * / %
4024 3 left + -
4025 4 left >> <<
4026 5 left == != > < <= >=
4027 6 left &
4028 7 left |
4029 8 left &&
4030 9 left ||
4031 10 right ? :
4032 11 right &= += -= *= /= (2)
4033 (lowest)
4034 @end smallexample
4035 Notes:
4036 (1) Prefix operators
4037 (2) @xref{Assignments}.
4038 @c TEXI2ROFF-KILL
4039 @end ifinfo
4040 @tex
4041 \vskip \baselineskip
4042 %"lispnarrowing" is the extra indent used generally for smallexample
4043 \hskip\lispnarrowing\vbox{\offinterlineskip
4044 \hrule
4045 \halign
4046 {\vrule#&\strut\hfil\ #\ \hfil&\vrule#&\strut\hfil\ #\ \hfil&\vrule#&\strut\hfil\ {\tt #}\ \hfil&\vrule#\cr
4047 height2pt&\omit&&\omit&&\omit&\cr
4048 &Precedence&& Associativity &&{\rm Operators}&\cr
4049 height2pt&\omit&&\omit&&\omit&\cr
4050 \noalign{\hrule}
4051 height2pt&\omit&&\omit&&\omit&\cr
4052 &highest&&&&&\cr
4053 % '176 is tilde, '~' in tt font
4054 &1&&left&&\qquad- \char'176\ !\qquad\dag&\cr
4055 &2&&left&&* / \%&\cr
4056 &3&&left&&+ -&\cr
4057 &4&&left&&>> <<&\cr
4058 &5&&left&&== != > < <= >=&\cr
4059 &6&&left&&\&&\cr
4060 &7&&left&&|&\cr
4061 &8&&left&&{\&\&}&\cr
4062 &9&&left&&||&\cr
4063 &10&&right&&? :&\cr
4064 &11&&right&&\qquad\&= += -= *= /=\qquad\ddag&\cr
4065 &lowest&&&&&\cr
4066 height2pt&\omit&&\omit&&\omit&\cr}
4067 \hrule}
4068 @end tex
4069 @iftex
4070 {
4071 @obeylines@parskip=0pt@parindent=0pt
4072 @dag@quad Prefix operators.
4073 @ddag@quad @xref{Assignments}.
4074 }
4075 @end iftex
4076 @c END TEXI2ROFF-KILL
4077
4078 @node Evaluation
4079 @subsection Evaluation
4080 @cindex lazy evaluation
4081 @cindex expression evaluation order
4082 The linker evaluates expressions lazily. It only computes the value of
4083 an expression when absolutely necessary.
4084
4085 The linker needs some information, such as the value of the start
4086 address of the first section, and the origins and lengths of memory
4087 regions, in order to do any linking at all. These values are computed
4088 as soon as possible when the linker reads in the linker script.
4089
4090 However, other values (such as symbol values) are not known or needed
4091 until after storage allocation. Such values are evaluated later, when
4092 other information (such as the sizes of output sections) is available
4093 for use in the symbol assignment expression.
4094
4095 The sizes of sections cannot be known until after allocation, so
4096 assignments dependent upon these are not performed until after
4097 allocation.
4098
4099 Some expressions, such as those depending upon the location counter
4100 @samp{.}, must be evaluated during section allocation.
4101
4102 If the result of an expression is required, but the value is not
4103 available, then an error results. For example, a script like the
4104 following
4105 @smallexample
4106 @group
4107 SECTIONS
4108 @{
4109 .text 9+this_isnt_constant :
4110 @{ *(.text) @}
4111 @}
4112 @end group
4113 @end smallexample
4114 @noindent
4115 will cause the error message @samp{non constant expression for initial
4116 address}.
4117
4118 @node Expression Section
4119 @subsection The Section of an Expression
4120 @cindex expression sections
4121 @cindex absolute expressions
4122 @cindex relative expressions
4123 @cindex absolute and relocatable symbols
4124 @cindex relocatable and absolute symbols
4125 @cindex symbols, relocatable and absolute
4126 When the linker evaluates an expression, the result is either absolute
4127 or relative to some section. A relative expression is expressed as a
4128 fixed offset from the base of a section.
4129
4130 The position of the expression within the linker script determines
4131 whether it is absolute or relative. An expression which appears within
4132 an output section definition is relative to the base of the output
4133 section. An expression which appears elsewhere will be absolute.
4134
4135 A symbol set to a relative expression will be relocatable if you request
4136 relocatable output using the @samp{-r} option. That means that a
4137 further link operation may change the value of the symbol. The symbol's
4138 section will be the section of the relative expression.
4139
4140 A symbol set to an absolute expression will retain the same value
4141 through any further link operation. The symbol will be absolute, and
4142 will not have any particular associated section.
4143
4144 You can use the builtin function @code{ABSOLUTE} to force an expression
4145 to be absolute when it would otherwise be relative. For example, to
4146 create an absolute symbol set to the address of the end of the output
4147 section @samp{.data}:
4148 @smallexample
4149 SECTIONS
4150 @{
4151 .data : @{ *(.data) _edata = ABSOLUTE(.); @}
4152 @}
4153 @end smallexample
4154 @noindent
4155 If @samp{ABSOLUTE} were not used, @samp{_edata} would be relative to the
4156 @samp{.data} section.
4157
4158 @node Builtin Functions
4159 @subsection Builtin Functions
4160 @cindex functions in expressions
4161 The linker script language includes a number of builtin functions for
4162 use in linker script expressions.
4163
4164 @table @code
4165 @item ABSOLUTE(@var{exp})
4166 @kindex ABSOLUTE(@var{exp})
4167 @cindex expression, absolute
4168 Return the absolute (non-relocatable, as opposed to non-negative) value
4169 of the expression @var{exp}. Primarily useful to assign an absolute
4170 value to a symbol within a section definition, where symbol values are
4171 normally section relative. @xref{Expression Section}.
4172
4173 @item ADDR(@var{section})
4174 @kindex ADDR(@var{section})
4175 @cindex section address in expression
4176 Return the absolute address (the VMA) of the named @var{section}. Your
4177 script must previously have defined the location of that section. In
4178 the following example, @code{symbol_1} and @code{symbol_2} are assigned
4179 identical values:
4180 @smallexample
4181 @group
4182 SECTIONS @{ @dots{}
4183 .output1 :
4184 @{
4185 start_of_output_1 = ABSOLUTE(.);
4186 @dots{}
4187 @}
4188 .output :
4189 @{
4190 symbol_1 = ADDR(.output1);
4191 symbol_2 = start_of_output_1;
4192 @}
4193 @dots{} @}
4194 @end group
4195 @end smallexample
4196
4197 @item ALIGN(@var{exp})
4198 @kindex ALIGN(@var{exp})
4199 @cindex round up location counter
4200 @cindex align location counter
4201 Return the location counter (@code{.}) aligned to the next @var{exp}
4202 boundary.
4203 @code{ALIGN} doesn't change the value of the location counter---it just
4204 does arithmetic on it. Here is an example which aligns the output
4205 @code{.data} section to the next @code{0x2000} byte boundary after the
4206 preceding section and sets a variable within the section to the next
4207 @code{0x8000} boundary after the input sections:
4208 @smallexample
4209 @group
4210 SECTIONS @{ @dots{}
4211 .data ALIGN(0x2000): @{
4212 *(.data)
4213 variable = ALIGN(0x8000);
4214 @}
4215 @dots{} @}
4216 @end group
4217 @end smallexample
4218 @noindent
4219 The first use of @code{ALIGN} in this example specifies the location of
4220 a section because it is used as the optional @var{address} attribute of
4221 a section definition (@pxref{Output Section Address}). The second use
4222 of @code{ALIGN} is used to defines the value of a symbol.
4223
4224 The builtin function @code{NEXT} is closely related to @code{ALIGN}.
4225
4226 @item BLOCK(@var{exp})
4227 @kindex BLOCK(@var{exp})
4228 This is a synonym for @code{ALIGN}, for compatibility with older linker
4229 scripts. It is most often seen when setting the address of an output
4230 section.
4231
4232 @item DATA_SEGMENT_ALIGN(@var{maxpagesize}, @var{commonpagesize})
4233 @kindex DATA_SEGMENT_ALIGN(@var{maxpagesize}, @var{commonpagesize})
4234 This is equivalent to either
4235 @smallexample
4236 (ALIGN(@var{maxpagesize}) + (. & (@var{maxpagesize} - 1)))
4237 @end smallexample
4238 or
4239 @smallexample
4240 (ALIGN(@var{maxpagesize}) + (. & (@var{maxpagesize} - @var{commonpagesize})))
4241 @end smallexample
4242 @noindent
4243 depending on whether the latter uses fewer @var{commonpagesize} sized pages
4244 for the data segment (area between the result of this expression and
4245 @code{DATA_SEGMENT_END}) than the former or not.
4246 If the latter form is used, it means @var{commonpagesize} bytes of runtime
4247 memory will be saved at the expense of up to @var{commonpagesize} wasted
4248 bytes in the on-disk file.
4249
4250 This expression can only be used directly in @code{SECTIONS} commands, not in
4251 any output section descriptions and only once in the linker script.
4252 @var{commonpagesize} should be less or equal to @var{maxpagesize} and should
4253 be the system page size the object wants to be optimized for (while still
4254 working on system page sizes up to @var{maxpagesize}).
4255
4256 @noindent
4257 Example:
4258 @smallexample
4259 . = DATA_SEGMENT_ALIGN(0x10000, 0x2000);
4260 @end smallexample
4261
4262 @item DATA_SEGMENT_END(@var{exp})
4263 @kindex DATA_SEGMENT_END(@var{exp})
4264 This defines the end of data segment for @code{DATA_SEGMENT_ALIGN}
4265 evaluation purposes.
4266
4267 @smallexample
4268 . = DATA_SEGMENT_END(.);
4269 @end smallexample
4270
4271 @item DEFINED(@var{symbol})
4272 @kindex DEFINED(@var{symbol})
4273 @cindex symbol defaults
4274 Return 1 if @var{symbol} is in the linker global symbol table and is
4275 defined, otherwise return 0. You can use this function to provide
4276 default values for symbols. For example, the following script fragment
4277 shows how to set a global symbol @samp{begin} to the first location in
4278 the @samp{.text} section---but if a symbol called @samp{begin} already
4279 existed, its value is preserved:
4280
4281 @smallexample
4282 @group
4283 SECTIONS @{ @dots{}
4284 .text : @{
4285 begin = DEFINED(begin) ? begin : . ;
4286 @dots{}
4287 @}
4288 @dots{}
4289 @}
4290 @end group
4291 @end smallexample
4292
4293 @item LOADADDR(@var{section})
4294 @kindex LOADADDR(@var{section})
4295 @cindex section load address in expression
4296 Return the absolute LMA of the named @var{section}. This is normally
4297 the same as @code{ADDR}, but it may be different if the @code{AT}
4298 attribute is used in the output section definition (@pxref{Output
4299 Section LMA}).
4300
4301 @kindex MAX
4302 @item MAX(@var{exp1}, @var{exp2})
4303 Returns the maximum of @var{exp1} and @var{exp2}.
4304
4305 @kindex MIN
4306 @item MIN(@var{exp1}, @var{exp2})
4307 Returns the minimum of @var{exp1} and @var{exp2}.
4308
4309 @item NEXT(@var{exp})
4310 @kindex NEXT(@var{exp})
4311 @cindex unallocated address, next
4312 Return the next unallocated address that is a multiple of @var{exp}.
4313 This function is closely related to @code{ALIGN(@var{exp})}; unless you
4314 use the @code{MEMORY} command to define discontinuous memory for the
4315 output file, the two functions are equivalent.
4316
4317 @item SIZEOF(@var{section})
4318 @kindex SIZEOF(@var{section})
4319 @cindex section size
4320 Return the size in bytes of the named @var{section}, if that section has
4321 been allocated. If the section has not been allocated when this is
4322 evaluated, the linker will report an error. In the following example,
4323 @code{symbol_1} and @code{symbol_2} are assigned identical values:
4324 @smallexample
4325 @group
4326 SECTIONS@{ @dots{}
4327 .output @{
4328 .start = . ;
4329 @dots{}
4330 .end = . ;
4331 @}
4332 symbol_1 = .end - .start ;
4333 symbol_2 = SIZEOF(.output);
4334 @dots{} @}
4335 @end group
4336 @end smallexample
4337
4338 @item SIZEOF_HEADERS
4339 @itemx sizeof_headers
4340 @kindex SIZEOF_HEADERS
4341 @cindex header size
4342 Return the size in bytes of the output file's headers. This is
4343 information which appears at the start of the output file. You can use
4344 this number when setting the start address of the first section, if you
4345 choose, to facilitate paging.
4346
4347 @cindex not enough room for program headers
4348 @cindex program headers, not enough room
4349 When producing an ELF output file, if the linker script uses the
4350 @code{SIZEOF_HEADERS} builtin function, the linker must compute the
4351 number of program headers before it has determined all the section
4352 addresses and sizes. If the linker later discovers that it needs
4353 additional program headers, it will report an error @samp{not enough
4354 room for program headers}. To avoid this error, you must avoid using
4355 the @code{SIZEOF_HEADERS} function, or you must rework your linker
4356 script to avoid forcing the linker to use additional program headers, or
4357 you must define the program headers yourself using the @code{PHDRS}
4358 command (@pxref{PHDRS}).
4359 @end table
4360
4361 @node Implicit Linker Scripts
4362 @section Implicit Linker Scripts
4363 @cindex implicit linker scripts
4364 If you specify a linker input file which the linker can not recognize as
4365 an object file or an archive file, it will try to read the file as a
4366 linker script. If the file can not be parsed as a linker script, the
4367 linker will report an error.
4368
4369 An implicit linker script will not replace the default linker script.
4370
4371 Typically an implicit linker script would contain only symbol
4372 assignments, or the @code{INPUT}, @code{GROUP}, or @code{VERSION}
4373 commands.
4374
4375 Any input files read because of an implicit linker script will be read
4376 at the position in the command line where the implicit linker script was
4377 read. This can affect archive searching.
4378
4379 @ifset GENERIC
4380 @node Machine Dependent
4381 @chapter Machine Dependent Features
4382
4383 @cindex machine dependencies
4384 @command{ld} has additional features on some platforms; the following
4385 sections describe them. Machines where @command{ld} has no additional
4386 functionality are not listed.
4387
4388 @menu
4389 * H8/300:: @code{ld} and the H8/300
4390 * i960:: @code{ld} and the Intel 960 family
4391 * ARM:: @code{ld} and the ARM family
4392 * HPPA ELF32:: @code{ld} and HPPA 32-bit ELF
4393 @ifset MMIX
4394 * MMIX:: @code{ld} and MMIX
4395 @end ifset
4396 @ifset TICOFF
4397 * TI COFF:: @command{ld} and TI COFF
4398 @end ifset
4399 @ifset WIN32
4400 * WIN32:: @command{ld} and WIN32 (cygwin/mingw)
4401 @end ifset
4402 @end menu
4403 @end ifset
4404
4405 @c FIXME! This could use @raisesections/@lowersections, but there seems to be a conflict
4406 @c between those and node-defaulting.
4407 @ifset H8300
4408 @ifclear GENERIC
4409 @raisesections
4410 @end ifclear
4411
4412 @node H8/300
4413 @section @command{ld} and the H8/300
4414
4415 @cindex H8/300 support
4416 For the H8/300, @command{ld} can perform these global optimizations when
4417 you specify the @samp{--relax} command-line option.
4418
4419 @table @emph
4420 @cindex relaxing on H8/300
4421 @item relaxing address modes
4422 @command{ld} finds all @code{jsr} and @code{jmp} instructions whose
4423 targets are within eight bits, and turns them into eight-bit
4424 program-counter relative @code{bsr} and @code{bra} instructions,
4425 respectively.
4426
4427 @cindex synthesizing on H8/300
4428 @item synthesizing instructions
4429 @c FIXME: specifically mov.b, or any mov instructions really?
4430 @command{ld} finds all @code{mov.b} instructions which use the
4431 sixteen-bit absolute address form, but refer to the top
4432 page of memory, and changes them to use the eight-bit address form.
4433 (That is: the linker turns @samp{mov.b @code{@@}@var{aa}:16} into
4434 @samp{mov.b @code{@@}@var{aa}:8} whenever the address @var{aa} is in the
4435 top page of memory).
4436 @end table
4437
4438 @ifclear GENERIC
4439 @lowersections
4440 @end ifclear
4441 @end ifset
4442
4443 @ifset WIN32
4444 @ifclear GENERIC
4445 @raisesections
4446 @end ifclear
4447
4448 @node WIN32
4449 @section @command{ld} and WIN32 (cygwin/mingw)
4450
4451 This section describes some of the win32 specific @command{ld} issues.
4452
4453 @table @emph
4454 @cindex import libraries
4455 @item import libraries
4456 The standard Windows linker creates and uses so called import
4457 libraries, which contains information for linking to dll's. They are
4458 regular static archives and could be handled as any other static
4459 archive. The cygwin and mingw ports of @command{ld} has specific
4460 support for creating such libraries provided with the
4461 @samp{--out-implib} command line option.
4462
4463 @cindex automatic data imports
4464 @item automatic data imports
4465 The standard Windows dll format supports data imports from dlls only
4466 by adding special decorations (dllimport/dllexport), which lets the
4467 compiler produce specific assembler instructions to deal with this
4468 issue. This increase the needed porting efforts, especially for big
4469 c++ libraries and applications. The auto-import feature, which was
4470 initially provided by Paul Sokolovsky, allows this decoration to be
4471 skipped, archiving a behavior more like @command{ld} on other
4472 platforms. This feature is enabled with the @samp{--enable-auto-import}
4473 command line option.
4474
4475 @cindex direct linking to a dll
4476 @item direct linking to a dll
4477 The cygwin/mingw ports of @command{ld} support the direct linking,
4478 including data symbols, to a dll without the usage of any import
4479 libraries. Using this feature can save a lot of linking time and
4480 memory, especially in case of bigger libraries or applications. This
4481 is because bigger libraries can have very large import libraries. (The
4482 author has seen import libraries about 10 MB size).
4483
4484 Linking directly to a dll can done with the standard command line
4485 options @samp{-L} and @samp{-l}. This is because @command{ld} has
4486 built in support for several dll names, as shown below. In this list
4487 @samp{xxx} means the basic library name like "png" for the png
4488 library:
4489
4490 @example
4491 libxxx.dll.a
4492 xxx.dll.a
4493 libxxx.a
4494 cygxxx.dll
4495 libxxx.dll
4496 xxx.dll
4497 @end example
4498
4499 The generic cygwin/mingw path layout uses a @samp{bin} directory for
4500 applications and dll's and a @samp{lib} directory for the import
4501 libraries.
4502
4503 @example
4504 bin/
4505 cygxxx.dll
4506 lib/
4507 libxxx.dll.a (in case of dll's)
4508 libxxx.a (in case of static archive)
4509 @end example
4510
4511 Linking to a dll can be done by two ways:
4512
4513 1. Use the dll directly by adding the @samp{bin} path to the link line
4514 @example
4515 gcc -Wl,-verbose -o a.exe -L../bin/ -lxxx
4516 @end example
4517
4518 2. Create a symbolic link from the dll to a file in the @samp{lib}
4519 directory according to the above mentioned search pattern. This
4520 should be used to avoid unwanted changes in the tools needed for
4521 making the app/dll.
4522
4523 @example
4524 ln -s bin/cygxxx.dll lib/[cyg|lib|]xxx.dll[.a]
4525 @end example
4526
4527 Then you can link without any make environment changes.
4528
4529 @example
4530 gcc -Wl,-verbose -o a.exe -L../lib/ -lxxx
4531 @end example
4532 @end table
4533
4534 @ifclear GENERIC
4535 @lowersections
4536 @end ifclear
4537 @end ifset
4538
4539 @ifclear GENERIC
4540 @ifset Hitachi
4541 @c This stuff is pointless to say unless you're especially concerned
4542 @c with Hitachi chips; don't enable it for generic case, please.
4543 @node Hitachi
4544 @chapter @command{ld} and other Hitachi chips
4545
4546 @command{ld} also supports the H8/300H, the H8/500, and the Hitachi SH. No
4547 special features, commands, or command-line options are required for
4548 these chips.
4549 @end ifset
4550 @end ifclear
4551
4552 @ifset I960
4553 @ifclear GENERIC
4554 @raisesections
4555 @end ifclear
4556
4557 @node i960
4558 @section @command{ld} and the Intel 960 family
4559
4560 @cindex i960 support
4561
4562 You can use the @samp{-A@var{architecture}} command line option to
4563 specify one of the two-letter names identifying members of the 960
4564 family; the option specifies the desired output target, and warns of any
4565 incompatible instructions in the input files. It also modifies the
4566 linker's search strategy for archive libraries, to support the use of
4567 libraries specific to each particular architecture, by including in the
4568 search loop names suffixed with the string identifying the architecture.
4569
4570 For example, if your @command{ld} command line included @w{@samp{-ACA}} as
4571 well as @w{@samp{-ltry}}, the linker would look (in its built-in search
4572 paths, and in any paths you specify with @samp{-L}) for a library with
4573 the names
4574
4575 @smallexample
4576 @group
4577 try
4578 libtry.a
4579 tryca
4580 libtryca.a
4581 @end group
4582 @end smallexample
4583
4584 @noindent
4585 The first two possibilities would be considered in any event; the last
4586 two are due to the use of @w{@samp{-ACA}}.
4587
4588 You can meaningfully use @samp{-A} more than once on a command line, since
4589 the 960 architecture family allows combination of target architectures; each
4590 use will add another pair of name variants to search for when @w{@samp{-l}}
4591 specifies a library.
4592
4593 @cindex @option{--relax} on i960
4594 @cindex relaxing on i960
4595 @command{ld} supports the @samp{--relax} option for the i960 family. If
4596 you specify @samp{--relax}, @command{ld} finds all @code{balx} and
4597 @code{calx} instructions whose targets are within 24 bits, and turns
4598 them into 24-bit program-counter relative @code{bal} and @code{cal}
4599 instructions, respectively. @command{ld} also turns @code{cal}
4600 instructions into @code{bal} instructions when it determines that the
4601 target subroutine is a leaf routine (that is, the target subroutine does
4602 not itself call any subroutines).
4603
4604 @ifclear GENERIC
4605 @lowersections
4606 @end ifclear
4607 @end ifset
4608
4609 @ifclear GENERIC
4610 @raisesections
4611 @end ifclear
4612
4613 @node ARM
4614 @section @command{ld}'s support for interworking between ARM and Thumb code
4615
4616 @cindex ARM interworking support
4617 @kindex --support-old-code
4618 For the ARM, @command{ld} will generate code stubs to allow functions calls
4619 betweem ARM and Thumb code. These stubs only work with code that has
4620 been compiled and assembled with the @samp{-mthumb-interwork} command
4621 line option. If it is necessary to link with old ARM object files or
4622 libraries, which have not been compiled with the -mthumb-interwork
4623 option then the @samp{--support-old-code} command line switch should be
4624 given to the linker. This will make it generate larger stub functions
4625 which will work with non-interworking aware ARM code. Note, however,
4626 the linker does not support generating stubs for function calls to
4627 non-interworking aware Thumb code.
4628
4629 @cindex thumb entry point
4630 @cindex entry point, thumb
4631 @kindex --thumb-entry=@var{entry}
4632 The @samp{--thumb-entry} switch is a duplicate of the generic
4633 @samp{--entry} switch, in that it sets the program's starting address.
4634 But it also sets the bottom bit of the address, so that it can be
4635 branched to using a BX instruction, and the program will start
4636 executing in Thumb mode straight away.
4637
4638 @node HPPA ELF32
4639 @section @command{ld} and HPPA 32-bit ELF support
4640 @cindex HPPA multiple sub-space stubs
4641 @kindex --multi-subspace
4642 When generating a shared library, @command{ld} will by default generate
4643 import stubs suitable for use with a single sub-space application.
4644 The @samp{--multi-subspace} switch causes @command{ld} to generate export
4645 stubs, and different (larger) import stubs suitable for use with
4646 multiple sub-spaces.
4647
4648 @cindex HPPA stub grouping
4649 @kindex --stub-group-size=@var{N}
4650 Long branch stubs and import/export stubs are placed by @command{ld} in
4651 stub sections located between groups of input sections.
4652 @samp{--stub-group-size} specifies the maximum size of a group of input
4653 sections handled by one stub section. Since branch offsets are signed,
4654 a stub section may serve two groups of input sections, one group before
4655 the stub section, and one group after it. However, when using
4656 conditional branches that require stubs, it may be better (for branch
4657 prediction) that stub sections only serve one group of input sections.
4658 A negative value for @samp{N} chooses this scheme, ensuring that
4659 branches to stubs always use a negative offset. Two special values of
4660 @samp{N} are recognized, @samp{1} and @samp{-1}. These both instruct
4661 @command{ld} to automatically size input section groups for the branch types
4662 detected, with the same behaviour regarding stub placement as other
4663 positive or negative values of @samp{N} respectively.
4664
4665 Note that @samp{--stub-group-size} does not split input sections. A
4666 single input section larger than the group size specified will of course
4667 create a larger group (of one section). If input sections are too
4668 large, it may not be possible for a branch to reach its stub.
4669
4670 @ifset MMIX
4671 @node MMIX
4672 @section @code{ld} and MMIX
4673 For MMIX, there is choice of generating @code{ELF} object files or
4674 @code{mmo} object files when linking. The simulator @code{mmix}
4675 understands the @code{mmo} format. The binutils @code{objcopy} utility
4676 can translate between the two formats.
4677
4678 There is one special section, the @samp{.MMIX.reg_contents} section.
4679 Contents in this section is assumed to correspond to that of global
4680 registers, and symbols referring to it are translated to special symbols,
4681 equal to registers. In a final link, the start address of the
4682 @samp{.MMIX.reg_contents} section corresponds to the first allocated
4683 global register multiplied by 8. Register @code{$255} is not included in
4684 this section; it is always set to the program entry, which is at the
4685 symbol @code{Main} for @code{mmo} files.
4686
4687 Symbols with the prefix @code{__.MMIX.start.}, for example
4688 @code{__.MMIX.start..text} and @code{__.MMIX.start..data} are special;
4689 there must be only one each, even if they are local. The default linker
4690 script uses these to set the default start address of a section.
4691
4692 Initial and trailing multiples of zero-valued 32-bit words in a section,
4693 are left out from an mmo file.
4694 @end ifset
4695
4696 @ifset TICOFF
4697 @node TI COFF
4698 @section @command{ld}'s support for various TI COFF versions
4699 @cindex TI COFF versions
4700 @kindex --format=@var{version}
4701 The @samp{--format} switch allows selection of one of the various
4702 TI COFF versions. The latest of this writing is 2; versions 0 and 1 are
4703 also supported. The TI COFF versions also vary in header byte-order
4704 format; @command{ld} will read any version or byte order, but the output
4705 header format depends on the default specified by the specific target.
4706 @end ifset
4707
4708 @ifclear GENERIC
4709 @lowersections
4710 @end ifclear
4711
4712 @ifclear SingleFormat
4713 @node BFD
4714 @chapter BFD
4715
4716 @cindex back end
4717 @cindex object file management
4718 @cindex object formats available
4719 @kindex objdump -i
4720 The linker accesses object and archive files using the BFD libraries.
4721 These libraries allow the linker to use the same routines to operate on
4722 object files whatever the object file format. A different object file
4723 format can be supported simply by creating a new BFD back end and adding
4724 it to the library. To conserve runtime memory, however, the linker and
4725 associated tools are usually configured to support only a subset of the
4726 object file formats available. You can use @code{objdump -i}
4727 (@pxref{objdump,,objdump,binutils.info,The GNU Binary Utilities}) to
4728 list all the formats available for your configuration.
4729
4730 @cindex BFD requirements
4731 @cindex requirements for BFD
4732 As with most implementations, BFD is a compromise between
4733 several conflicting requirements. The major factor influencing
4734 BFD design was efficiency: any time used converting between
4735 formats is time which would not have been spent had BFD not
4736 been involved. This is partly offset by abstraction payback; since
4737 BFD simplifies applications and back ends, more time and care
4738 may be spent optimizing algorithms for a greater speed.
4739
4740 One minor artifact of the BFD solution which you should bear in
4741 mind is the potential for information loss. There are two places where
4742 useful information can be lost using the BFD mechanism: during
4743 conversion and during output. @xref{BFD information loss}.
4744
4745 @menu
4746 * BFD outline:: How it works: an outline of BFD
4747 @end menu
4748
4749 @node BFD outline
4750 @section How it works: an outline of BFD
4751 @cindex opening object files
4752 @include bfdsumm.texi
4753 @end ifclear
4754
4755 @node Reporting Bugs
4756 @chapter Reporting Bugs
4757 @cindex bugs in @command{ld}
4758 @cindex reporting bugs in @command{ld}
4759
4760 Your bug reports play an essential role in making @command{ld} reliable.
4761
4762 Reporting a bug may help you by bringing a solution to your problem, or
4763 it may not. But in any case the principal function of a bug report is
4764 to help the entire community by making the next version of @command{ld}
4765 work better. Bug reports are your contribution to the maintenance of
4766 @command{ld}.
4767
4768 In order for a bug report to serve its purpose, you must include the
4769 information that enables us to fix the bug.
4770
4771 @menu
4772 * Bug Criteria:: Have you found a bug?
4773 * Bug Reporting:: How to report bugs
4774 @end menu
4775
4776 @node Bug Criteria
4777 @section Have you found a bug?
4778 @cindex bug criteria
4779
4780 If you are not sure whether you have found a bug, here are some guidelines:
4781
4782 @itemize @bullet
4783 @cindex fatal signal
4784 @cindex linker crash
4785 @cindex crash of linker
4786 @item
4787 If the linker gets a fatal signal, for any input whatever, that is a
4788 @command{ld} bug. Reliable linkers never crash.
4789
4790 @cindex error on valid input
4791 @item
4792 If @command{ld} produces an error message for valid input, that is a bug.
4793
4794 @cindex invalid input
4795 @item
4796 If @command{ld} does not produce an error message for invalid input, that
4797 may be a bug. In the general case, the linker can not verify that
4798 object files are correct.
4799
4800 @item
4801 If you are an experienced user of linkers, your suggestions for
4802 improvement of @command{ld} are welcome in any case.
4803 @end itemize
4804
4805 @node Bug Reporting
4806 @section How to report bugs
4807 @cindex bug reports
4808 @cindex @command{ld} bugs, reporting
4809
4810 A number of companies and individuals offer support for @sc{gnu}
4811 products. If you obtained @command{ld} from a support organization, we
4812 recommend you contact that organization first.
4813
4814 You can find contact information for many support companies and
4815 individuals in the file @file{etc/SERVICE} in the @sc{gnu} Emacs
4816 distribution.
4817
4818 Otherwise, send bug reports for @command{ld} to
4819 @samp{bug-binutils@@gnu.org}.
4820
4821 The fundamental principle of reporting bugs usefully is this:
4822 @strong{report all the facts}. If you are not sure whether to state a
4823 fact or leave it out, state it!
4824
4825 Often people omit facts because they think they know what causes the
4826 problem and assume that some details do not matter. Thus, you might
4827 assume that the name of a symbol you use in an example does not
4828 matter. Well, probably it does not, but one cannot be sure. Perhaps
4829 the bug is a stray memory reference which happens to fetch from the
4830 location where that name is stored in memory; perhaps, if the name
4831 were different, the contents of that location would fool the linker
4832 into doing the right thing despite the bug. Play it safe and give a
4833 specific, complete example. That is the easiest thing for you to do,
4834 and the most helpful.
4835
4836 Keep in mind that the purpose of a bug report is to enable us to fix
4837 the bug if it is new to us. Therefore, always write your bug reports
4838 on the assumption that the bug has not been reported previously.
4839
4840 Sometimes people give a few sketchy facts and ask, ``Does this ring a
4841 bell?'' Those bug reports are useless, and we urge everyone to
4842 @emph{refuse to respond to them} except to chide the sender to report
4843 bugs properly.
4844
4845 To enable us to fix the bug, you should include all these things:
4846
4847 @itemize @bullet
4848 @item
4849 The version of @command{ld}. @command{ld} announces it if you start it with
4850 the @samp{--version} argument.
4851
4852 Without this, we will not know whether there is any point in looking for
4853 the bug in the current version of @command{ld}.
4854
4855 @item
4856 Any patches you may have applied to the @command{ld} source, including any
4857 patches made to the @code{BFD} library.
4858
4859 @item
4860 The type of machine you are using, and the operating system name and
4861 version number.
4862
4863 @item
4864 What compiler (and its version) was used to compile @command{ld}---e.g.
4865 ``@code{gcc-2.7}''.
4866
4867 @item
4868 The command arguments you gave the linker to link your example and
4869 observe the bug. To guarantee you will not omit something important,
4870 list them all. A copy of the Makefile (or the output from make) is
4871 sufficient.
4872
4873 If we were to try to guess the arguments, we would probably guess wrong
4874 and then we might not encounter the bug.
4875
4876 @item
4877 A complete input file, or set of input files, that will reproduce the
4878 bug. It is generally most helpful to send the actual object files
4879 provided that they are reasonably small. Say no more than 10K. For
4880 bigger files you can either make them available by FTP or HTTP or else
4881 state that you are willing to send the object file(s) to whomever
4882 requests them. (Note - your email will be going to a mailing list, so
4883 we do not want to clog it up with large attachments). But small
4884 attachments are best.
4885
4886 If the source files were assembled using @code{gas} or compiled using
4887 @code{gcc}, then it may be OK to send the source files rather than the
4888 object files. In this case, be sure to say exactly what version of
4889 @code{gas} or @code{gcc} was used to produce the object files. Also say
4890 how @code{gas} or @code{gcc} were configured.
4891
4892 @item
4893 A description of what behavior you observe that you believe is
4894 incorrect. For example, ``It gets a fatal signal.''
4895
4896 Of course, if the bug is that @command{ld} gets a fatal signal, then we
4897 will certainly notice it. But if the bug is incorrect output, we might
4898 not notice unless it is glaringly wrong. You might as well not give us
4899 a chance to make a mistake.
4900
4901 Even if the problem you experience is a fatal signal, you should still
4902 say so explicitly. Suppose something strange is going on, such as, your
4903 copy of @command{ld} is out of synch, or you have encountered a bug in the
4904 C library on your system. (This has happened!) Your copy might crash
4905 and ours would not. If you told us to expect a crash, then when ours
4906 fails to crash, we would know that the bug was not happening for us. If
4907 you had not told us to expect a crash, then we would not be able to draw
4908 any conclusion from our observations.
4909
4910 @item
4911 If you wish to suggest changes to the @command{ld} source, send us context
4912 diffs, as generated by @code{diff} with the @samp{-u}, @samp{-c}, or
4913 @samp{-p} option. Always send diffs from the old file to the new file.
4914 If you even discuss something in the @command{ld} source, refer to it by
4915 context, not by line number.
4916
4917 The line numbers in our development sources will not match those in your
4918 sources. Your line numbers would convey no useful information to us.
4919 @end itemize
4920
4921 Here are some things that are not necessary:
4922
4923 @itemize @bullet
4924 @item
4925 A description of the envelope of the bug.
4926
4927 Often people who encounter a bug spend a lot of time investigating
4928 which changes to the input file will make the bug go away and which
4929 changes will not affect it.
4930
4931 This is often time consuming and not very useful, because the way we
4932 will find the bug is by running a single example under the debugger
4933 with breakpoints, not by pure deduction from a series of examples.
4934 We recommend that you save your time for something else.
4935
4936 Of course, if you can find a simpler example to report @emph{instead}
4937 of the original one, that is a convenience for us. Errors in the
4938 output will be easier to spot, running under the debugger will take
4939 less time, and so on.
4940
4941 However, simplification is not vital; if you do not want to do this,
4942 report the bug anyway and send us the entire test case you used.
4943
4944 @item
4945 A patch for the bug.
4946
4947 A patch for the bug does help us if it is a good one. But do not omit
4948 the necessary information, such as the test case, on the assumption that
4949 a patch is all we need. We might see problems with your patch and decide
4950 to fix the problem another way, or we might not understand it at all.
4951
4952 Sometimes with a program as complicated as @command{ld} it is very hard to
4953 construct an example that will make the program follow a certain path
4954 through the code. If you do not send us the example, we will not be
4955 able to construct one, so we will not be able to verify that the bug is
4956 fixed.
4957
4958 And if we cannot understand what bug you are trying to fix, or why your
4959 patch should be an improvement, we will not install it. A test case will
4960 help us to understand.
4961
4962 @item
4963 A guess about what the bug is or what it depends on.
4964
4965 Such guesses are usually wrong. Even we cannot guess right about such
4966 things without first using the debugger to find the facts.
4967 @end itemize
4968
4969 @node MRI
4970 @appendix MRI Compatible Script Files
4971 @cindex MRI compatibility
4972 To aid users making the transition to @sc{gnu} @command{ld} from the MRI
4973 linker, @command{ld} can use MRI compatible linker scripts as an
4974 alternative to the more general-purpose linker scripting language
4975 described in @ref{Scripts}. MRI compatible linker scripts have a much
4976 simpler command set than the scripting language otherwise used with
4977 @command{ld}. @sc{gnu} @command{ld} supports the most commonly used MRI
4978 linker commands; these commands are described here.
4979
4980 In general, MRI scripts aren't of much use with the @code{a.out} object
4981 file format, since it only has three sections and MRI scripts lack some
4982 features to make use of them.
4983
4984 You can specify a file containing an MRI-compatible script using the
4985 @samp{-c} command-line option.
4986
4987 Each command in an MRI-compatible script occupies its own line; each
4988 command line starts with the keyword that identifies the command (though
4989 blank lines are also allowed for punctuation). If a line of an
4990 MRI-compatible script begins with an unrecognized keyword, @command{ld}
4991 issues a warning message, but continues processing the script.
4992
4993 Lines beginning with @samp{*} are comments.
4994
4995 You can write these commands using all upper-case letters, or all
4996 lower case; for example, @samp{chip} is the same as @samp{CHIP}.
4997 The following list shows only the upper-case form of each command.
4998
4999 @table @code
5000 @cindex @code{ABSOLUTE} (MRI)
5001 @item ABSOLUTE @var{secname}
5002 @itemx ABSOLUTE @var{secname}, @var{secname}, @dots{} @var{secname}
5003 Normally, @command{ld} includes in the output file all sections from all
5004 the input files. However, in an MRI-compatible script, you can use the
5005 @code{ABSOLUTE} command to restrict the sections that will be present in
5006 your output program. If the @code{ABSOLUTE} command is used at all in a
5007 script, then only the sections named explicitly in @code{ABSOLUTE}
5008 commands will appear in the linker output. You can still use other
5009 input sections (whatever you select on the command line, or using
5010 @code{LOAD}) to resolve addresses in the output file.
5011
5012 @cindex @code{ALIAS} (MRI)
5013 @item ALIAS @var{out-secname}, @var{in-secname}
5014 Use this command to place the data from input section @var{in-secname}
5015 in a section called @var{out-secname} in the linker output file.
5016
5017 @var{in-secname} may be an integer.
5018
5019 @cindex @code{ALIGN} (MRI)
5020 @item ALIGN @var{secname} = @var{expression}
5021 Align the section called @var{secname} to @var{expression}. The
5022 @var{expression} should be a power of two.
5023
5024 @cindex @code{BASE} (MRI)
5025 @item BASE @var{expression}
5026 Use the value of @var{expression} as the lowest address (other than
5027 absolute addresses) in the output file.
5028
5029 @cindex @code{CHIP} (MRI)
5030 @item CHIP @var{expression}
5031 @itemx CHIP @var{expression}, @var{expression}
5032 This command does nothing; it is accepted only for compatibility.
5033
5034 @cindex @code{END} (MRI)
5035 @item END
5036 This command does nothing whatever; it's only accepted for compatibility.
5037
5038 @cindex @code{FORMAT} (MRI)
5039 @item FORMAT @var{output-format}
5040 Similar to the @code{OUTPUT_FORMAT} command in the more general linker
5041 language, but restricted to one of these output formats:
5042
5043 @enumerate
5044 @item
5045 S-records, if @var{output-format} is @samp{S}
5046
5047 @item
5048 IEEE, if @var{output-format} is @samp{IEEE}
5049
5050 @item
5051 COFF (the @samp{coff-m68k} variant in BFD), if @var{output-format} is
5052 @samp{COFF}
5053 @end enumerate
5054
5055 @cindex @code{LIST} (MRI)
5056 @item LIST @var{anything}@dots{}
5057 Print (to the standard output file) a link map, as produced by the
5058 @command{ld} command-line option @samp{-M}.
5059
5060 The keyword @code{LIST} may be followed by anything on the
5061 same line, with no change in its effect.
5062
5063 @cindex @code{LOAD} (MRI)
5064 @item LOAD @var{filename}
5065 @itemx LOAD @var{filename}, @var{filename}, @dots{} @var{filename}
5066 Include one or more object file @var{filename} in the link; this has the
5067 same effect as specifying @var{filename} directly on the @command{ld}
5068 command line.
5069
5070 @cindex @code{NAME} (MRI)
5071 @item NAME @var{output-name}
5072 @var{output-name} is the name for the program produced by @command{ld}; the
5073 MRI-compatible command @code{NAME} is equivalent to the command-line
5074 option @samp{-o} or the general script language command @code{OUTPUT}.
5075
5076 @cindex @code{ORDER} (MRI)
5077 @item ORDER @var{secname}, @var{secname}, @dots{} @var{secname}
5078 @itemx ORDER @var{secname} @var{secname} @var{secname}
5079 Normally, @command{ld} orders the sections in its output file in the
5080 order in which they first appear in the input files. In an MRI-compatible
5081 script, you can override this ordering with the @code{ORDER} command. The
5082 sections you list with @code{ORDER} will appear first in your output
5083 file, in the order specified.
5084
5085 @cindex @code{PUBLIC} (MRI)
5086 @item PUBLIC @var{name}=@var{expression}
5087 @itemx PUBLIC @var{name},@var{expression}
5088 @itemx PUBLIC @var{name} @var{expression}
5089 Supply a value (@var{expression}) for external symbol
5090 @var{name} used in the linker input files.
5091
5092 @cindex @code{SECT} (MRI)
5093 @item SECT @var{secname}, @var{expression}
5094 @itemx SECT @var{secname}=@var{expression}
5095 @itemx SECT @var{secname} @var{expression}
5096 You can use any of these three forms of the @code{SECT} command to
5097 specify the start address (@var{expression}) for section @var{secname}.
5098 If you have more than one @code{SECT} statement for the same
5099 @var{secname}, only the @emph{first} sets the start address.
5100 @end table
5101
5102 @node GNU Free Documentation License
5103 @appendix GNU Free Documentation License
5104 @cindex GNU Free Documentation License
5105
5106 GNU Free Documentation License
5107
5108 Version 1.1, March 2000
5109
5110 Copyright (C) 2000 Free Software Foundation, Inc.
5111 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
5112
5113 Everyone is permitted to copy and distribute verbatim copies
5114 of this license document, but changing it is not allowed.
5115
5116
5117 0. PREAMBLE
5118
5119 The purpose of this License is to make a manual, textbook, or other
5120 written document "free" in the sense of freedom: to assure everyone
5121 the effective freedom to copy and redistribute it, with or without
5122 modifying it, either commercially or noncommercially. Secondarily,
5123 this License preserves for the author and publisher a way to get
5124 credit for their work, while not being considered responsible for
5125 modifications made by others.
5126
5127 This License is a kind of "copyleft", which means that derivative
5128 works of the document must themselves be free in the same sense. It
5129 complements the GNU General Public License, which is a copyleft
5130 license designed for free software.
5131
5132 We have designed this License in order to use it for manuals for free
5133 software, because free software needs free documentation: a free
5134 program should come with manuals providing the same freedoms that the
5135 software does. But this License is not limited to software manuals;
5136 it can be used for any textual work, regardless of subject matter or
5137 whether it is published as a printed book. We recommend this License
5138 principally for works whose purpose is instruction or reference.
5139
5140
5141 1. APPLICABILITY AND DEFINITIONS
5142
5143 This License applies to any manual or other work that contains a
5144 notice placed by the copyright holder saying it can be distributed
5145 under the terms of this License. The "Document", below, refers to any
5146 such manual or work. Any member of the public is a licensee, and is
5147 addressed as "you".
5148
5149 A "Modified Version" of the Document means any work containing the
5150 Document or a portion of it, either copied verbatim, or with
5151 modifications and/or translated into another language.
5152
5153 A "Secondary Section" is a named appendix or a front-matter section of
5154 the Document that deals exclusively with the relationship of the
5155 publishers or authors of the Document to the Document's overall subject
5156 (or to related matters) and contains nothing that could fall directly
5157 within that overall subject. (For example, if the Document is in part a
5158 textbook of mathematics, a Secondary Section may not explain any
5159 mathematics.) The relationship could be a matter of historical
5160 connection with the subject or with related matters, or of legal,
5161 commercial, philosophical, ethical or political position regarding
5162 them.
5163
5164 The "Invariant Sections" are certain Secondary Sections whose titles
5165 are designated, as being those of Invariant Sections, in the notice
5166 that says that the Document is released under this License.
5167
5168 The "Cover Texts" are certain short passages of text that are listed,
5169 as Front-Cover Texts or Back-Cover Texts, in the notice that says that
5170 the Document is released under this License.
5171
5172 A "Transparent" copy of the Document means a machine-readable copy,
5173 represented in a format whose specification is available to the
5174 general public, whose contents can be viewed and edited directly and
5175 straightforwardly with generic text editors or (for images composed of
5176 pixels) generic paint programs or (for drawings) some widely available
5177 drawing editor, and that is suitable for input to text formatters or
5178 for automatic translation to a variety of formats suitable for input
5179 to text formatters. A copy made in an otherwise Transparent file
5180 format whose markup has been designed to thwart or discourage
5181 subsequent modification by readers is not Transparent. A copy that is
5182 not "Transparent" is called "Opaque".
5183
5184 Examples of suitable formats for Transparent copies include plain
5185 ASCII without markup, Texinfo input format, LaTeX input format, SGML
5186 or XML using a publicly available DTD, and standard-conforming simple
5187 HTML designed for human modification. Opaque formats include
5188 PostScript, PDF, proprietary formats that can be read and edited only
5189 by proprietary word processors, SGML or XML for which the DTD and/or
5190 processing tools are not generally available, and the
5191 machine-generated HTML produced by some word processors for output
5192 purposes only.
5193
5194 The "Title Page" means, for a printed book, the title page itself,
5195 plus such following pages as are needed to hold, legibly, the material
5196 this License requires to appear in the title page. For works in
5197 formats which do not have any title page as such, "Title Page" means
5198 the text near the most prominent appearance of the work's title,
5199 preceding the beginning of the body of the text.
5200
5201
5202 2. VERBATIM COPYING
5203
5204 You may copy and distribute the Document in any medium, either
5205 commercially or noncommercially, provided that this License, the
5206 copyright notices, and the license notice saying this License applies
5207 to the Document are reproduced in all copies, and that you add no other
5208 conditions whatsoever to those of this License. You may not use
5209 technical measures to obstruct or control the reading or further
5210 copying of the copies you make or distribute. However, you may accept
5211 compensation in exchange for copies. If you distribute a large enough
5212 number of copies you must also follow the conditions in section 3.
5213
5214 You may also lend copies, under the same conditions stated above, and
5215 you may publicly display copies.
5216
5217
5218 3. COPYING IN QUANTITY
5219
5220 If you publish printed copies of the Document numbering more than 100,
5221 and the Document's license notice requires Cover Texts, you must enclose
5222 the copies in covers that carry, clearly and legibly, all these Cover
5223 Texts: Front-Cover Texts on the front cover, and Back-Cover Texts on
5224 the back cover. Both covers must also clearly and legibly identify
5225 you as the publisher of these copies. The front cover must present
5226 the full title with all words of the title equally prominent and
5227 visible. You may add other material on the covers in addition.
5228 Copying with changes limited to the covers, as long as they preserve
5229 the title of the Document and satisfy these conditions, can be treated
5230 as verbatim copying in other respects.
5231
5232 If the required texts for either cover are too voluminous to fit
5233 legibly, you should put the first ones listed (as many as fit
5234 reasonably) on the actual cover, and continue the rest onto adjacent
5235 pages.
5236
5237 If you publish or distribute Opaque copies of the Document numbering
5238 more than 100, you must either include a machine-readable Transparent
5239 copy along with each Opaque copy, or state in or with each Opaque copy
5240 a publicly-accessible computer-network location containing a complete
5241 Transparent copy of the Document, free of added material, which the
5242 general network-using public has access to download anonymously at no
5243 charge using public-standard network protocols. If you use the latter
5244 option, you must take reasonably prudent steps, when you begin
5245 distribution of Opaque copies in quantity, to ensure that this
5246 Transparent copy will remain thus accessible at the stated location
5247 until at least one year after the last time you distribute an Opaque
5248 copy (directly or through your agents or retailers) of that edition to
5249 the public.
5250
5251 It is requested, but not required, that you contact the authors of the
5252 Document well before redistributing any large number of copies, to give
5253 them a chance to provide you with an updated version of the Document.
5254
5255
5256 4. MODIFICATIONS
5257
5258 You may copy and distribute a Modified Version of the Document under
5259 the conditions of sections 2 and 3 above, provided that you release
5260 the Modified Version under precisely this License, with the Modified
5261 Version filling the role of the Document, thus licensing distribution
5262 and modification of the Modified Version to whoever possesses a copy
5263 of it. In addition, you must do these things in the Modified Version:
5264
5265 A. Use in the Title Page (and on the covers, if any) a title distinct
5266 from that of the Document, and from those of previous versions
5267 (which should, if there were any, be listed in the History section
5268 of the Document). You may use the same title as a previous version
5269 if the original publisher of that version gives permission.
5270 B. List on the Title Page, as authors, one or more persons or entities
5271 responsible for authorship of the modifications in the Modified
5272 Version, together with at least five of the principal authors of the
5273 Document (all of its principal authors, if it has less than five).
5274 C. State on the Title page the name of the publisher of the
5275 Modified Version, as the publisher.
5276 D. Preserve all the copyright notices of the Document.
5277 E. Add an appropriate copyright notice for your modifications
5278 adjacent to the other copyright notices.
5279 F. Include, immediately after the copyright notices, a license notice
5280 giving the public permission to use the Modified Version under the
5281 terms of this License, in the form shown in the Addendum below.
5282 G. Preserve in that license notice the full lists of Invariant Sections
5283 and required Cover Texts given in the Document's license notice.
5284 H. Include an unaltered copy of this License.
5285 I. Preserve the section entitled "History", and its title, and add to
5286 it an item stating at least the title, year, new authors, and
5287 publisher of the Modified Version as given on the Title Page. If
5288 there is no section entitled "History" in the Document, create one
5289 stating the title, year, authors, and publisher of the Document as
5290 given on its Title Page, then add an item describing the Modified
5291 Version as stated in the previous sentence.
5292 J. Preserve the network location, if any, given in the Document for
5293 public access to a Transparent copy of the Document, and likewise
5294 the network locations given in the Document for previous versions
5295 it was based on. These may be placed in the "History" section.
5296 You may omit a network location for a work that was published at
5297 least four years before the Document itself, or if the original
5298 publisher of the version it refers to gives permission.
5299 K. In any section entitled "Acknowledgements" or "Dedications",
5300 preserve the section's title, and preserve in the section all the
5301 substance and tone of each of the contributor acknowledgements
5302 and/or dedications given therein.
5303 L. Preserve all the Invariant Sections of the Document,
5304 unaltered in their text and in their titles. Section numbers
5305 or the equivalent are not considered part of the section titles.
5306 M. Delete any section entitled "Endorsements". Such a section
5307 may not be included in the Modified Version.
5308 N. Do not retitle any existing section as "Endorsements"
5309 or to conflict in title with any Invariant Section.
5310
5311 If the Modified Version includes new front-matter sections or
5312 appendices that qualify as Secondary Sections and contain no material
5313 copied from the Document, you may at your option designate some or all
5314 of these sections as invariant. To do this, add their titles to the
5315 list of Invariant Sections in the Modified Version's license notice.
5316 These titles must be distinct from any other section titles.
5317
5318 You may add a section entitled "Endorsements", provided it contains
5319 nothing but endorsements of your Modified Version by various
5320 parties--for example, statements of peer review or that the text has
5321 been approved by an organization as the authoritative definition of a
5322 standard.
5323
5324 You may add a passage of up to five words as a Front-Cover Text, and a
5325 passage of up to 25 words as a Back-Cover Text, to the end of the list
5326 of Cover Texts in the Modified Version. Only one passage of
5327 Front-Cover Text and one of Back-Cover Text may be added by (or
5328 through arrangements made by) any one entity. If the Document already
5329 includes a cover text for the same cover, previously added by you or
5330 by arrangement made by the same entity you are acting on behalf of,
5331 you may not add another; but you may replace the old one, on explicit
5332 permission from the previous publisher that added the old one.
5333
5334 The author(s) and publisher(s) of the Document do not by this License
5335 give permission to use their names for publicity for or to assert or
5336 imply endorsement of any Modified Version.
5337
5338
5339 5. COMBINING DOCUMENTS
5340
5341 You may combine the Document with other documents released under this
5342 License, under the terms defined in section 4 above for modified
5343 versions, provided that you include in the combination all of the
5344 Invariant Sections of all of the original documents, unmodified, and
5345 list them all as Invariant Sections of your combined work in its
5346 license notice.
5347
5348 The combined work need only contain one copy of this License, and
5349 multiple identical Invariant Sections may be replaced with a single
5350 copy. If there are multiple Invariant Sections with the same name but
5351 different contents, make the title of each such section unique by
5352 adding at the end of it, in parentheses, the name of the original
5353 author or publisher of that section if known, or else a unique number.
5354 Make the same adjustment to the section titles in the list of
5355 Invariant Sections in the license notice of the combined work.
5356
5357 In the combination, you must combine any sections entitled "History"
5358 in the various original documents, forming one section entitled
5359 "History"; likewise combine any sections entitled "Acknowledgements",
5360 and any sections entitled "Dedications". You must delete all sections
5361 entitled "Endorsements."
5362
5363
5364 6. COLLECTIONS OF DOCUMENTS
5365
5366 You may make a collection consisting of the Document and other documents
5367 released under this License, and replace the individual copies of this
5368 License in the various documents with a single copy that is included in
5369 the collection, provided that you follow the rules of this License for
5370 verbatim copying of each of the documents in all other respects.
5371
5372 You may extract a single document from such a collection, and distribute
5373 it individually under this License, provided you insert a copy of this
5374 License into the extracted document, and follow this License in all
5375 other respects regarding verbatim copying of that document.
5376
5377
5378 7. AGGREGATION WITH INDEPENDENT WORKS
5379
5380 A compilation of the Document or its derivatives with other separate
5381 and independent documents or works, in or on a volume of a storage or
5382 distribution medium, does not as a whole count as a Modified Version
5383 of the Document, provided no compilation copyright is claimed for the
5384 compilation. Such a compilation is called an "aggregate", and this
5385 License does not apply to the other self-contained works thus compiled
5386 with the Document, on account of their being thus compiled, if they
5387 are not themselves derivative works of the Document.
5388
5389 If the Cover Text requirement of section 3 is applicable to these
5390 copies of the Document, then if the Document is less than one quarter
5391 of the entire aggregate, the Document's Cover Texts may be placed on
5392 covers that surround only the Document within the aggregate.
5393 Otherwise they must appear on covers around the whole aggregate.
5394
5395
5396 8. TRANSLATION
5397
5398 Translation is considered a kind of modification, so you may
5399 distribute translations of the Document under the terms of section 4.
5400 Replacing Invariant Sections with translations requires special
5401 permission from their copyright holders, but you may include
5402 translations of some or all Invariant Sections in addition to the
5403 original versions of these Invariant Sections. You may include a
5404 translation of this License provided that you also include the
5405 original English version of this License. In case of a disagreement
5406 between the translation and the original English version of this
5407 License, the original English version will prevail.
5408
5409
5410 9. TERMINATION
5411
5412 You may not copy, modify, sublicense, or distribute the Document except
5413 as expressly provided for under this License. Any other attempt to
5414 copy, modify, sublicense or distribute the Document is void, and will
5415 automatically terminate your rights under this License. However,
5416 parties who have received copies, or rights, from you under this
5417 License will not have their licenses terminated so long as such
5418 parties remain in full compliance.
5419
5420
5421 10. FUTURE REVISIONS OF THIS LICENSE
5422
5423 The Free Software Foundation may publish new, revised versions
5424 of the GNU Free Documentation License from time to time. Such new
5425 versions will be similar in spirit to the present version, but may
5426 differ in detail to address new problems or concerns. See
5427 http://www.gnu.org/copyleft/.
5428
5429 Each version of the License is given a distinguishing version number.
5430 If the Document specifies that a particular numbered version of this
5431 License "or any later version" applies to it, you have the option of
5432 following the terms and conditions either of that specified version or
5433 of any later version that has been published (not as a draft) by the
5434 Free Software Foundation. If the Document does not specify a version
5435 number of this License, you may choose any version ever published (not
5436 as a draft) by the Free Software Foundation.
5437
5438
5439 ADDENDUM: How to use this License for your documents
5440
5441 To use this License in a document you have written, include a copy of
5442 the License in the document and put the following copyright and
5443 license notices just after the title page:
5444
5445 @smallexample
5446 Copyright (c) YEAR YOUR NAME.
5447 Permission is granted to copy, distribute and/or modify this document
5448 under the terms of the GNU Free Documentation License, Version 1.1
5449 or any later version published by the Free Software Foundation;
5450 with the Invariant Sections being LIST THEIR TITLES, with the
5451 Front-Cover Texts being LIST, and with the Back-Cover Texts being LIST.
5452 A copy of the license is included in the section entitled "GNU
5453 Free Documentation License".
5454 @end smallexample
5455
5456 If you have no Invariant Sections, write "with no Invariant Sections"
5457 instead of saying which ones are invariant. If you have no
5458 Front-Cover Texts, write "no Front-Cover Texts" instead of
5459 "Front-Cover Texts being LIST"; likewise for Back-Cover Texts.
5460
5461 If your document contains nontrivial examples of program code, we
5462 recommend releasing these examples in parallel under your choice of
5463 free software license, such as the GNU General Public License,
5464 to permit their use in free software.
5465
5466 @node Index
5467 @unnumbered Index
5468
5469 @printindex cp
5470
5471 @tex
5472 % I think something like @colophon should be in texinfo. In the
5473 % meantime:
5474 \long\def\colophon{\hbox to0pt{}\vfill
5475 \centerline{The body of this manual is set in}
5476 \centerline{\fontname\tenrm,}
5477 \centerline{with headings in {\bf\fontname\tenbf}}
5478 \centerline{and examples in {\tt\fontname\tentt}.}
5479 \centerline{{\it\fontname\tenit\/} and}
5480 \centerline{{\sl\fontname\tensl\/}}
5481 \centerline{are used for emphasis.}\vfill}
5482 \page\colophon
5483 % Blame: doc@cygnus.com, 28mar91.
5484 @end tex
5485
5486
5487 @contents
5488 @bye
This page took 0.206527 seconds and 5 git commands to generate.