* elf32-m68k.c (elf32_m68k_set_private_flags): New.
[deliverable/binutils-gdb.git] / ld / ld.texinfo
1 \input texinfo
2 @setfilename ld.info
3 @syncodeindex ky cp
4 @include configdoc.texi
5 @c (configdoc.texi is generated by the Makefile)
6 @include ldver.texi
7
8 @c @smallbook
9
10 @ifinfo
11 @format
12 START-INFO-DIR-ENTRY
13 * Ld: (ld). The GNU linker.
14 END-INFO-DIR-ENTRY
15 @end format
16 @end ifinfo
17
18 @ifinfo
19 This file documents the @sc{gnu} linker LD version @value{VERSION}.
20
21 Copyright (C) 1991, 92, 93, 94, 95, 96, 97, 98, 1999 Free Software Foundation, Inc.
22
23 Permission is granted to make and distribute verbatim copies of
24 this manual provided the copyright notice and this permission notice
25 are preserved on all copies.
26
27 Permission is granted to copy and distribute modified versions of this
28 manual under the conditions for verbatim copying, provided also that
29 the entire resulting derived work is distributed under the terms of a
30 permission notice identical to this one.
31
32 Permission is granted to copy and distribute translations of this manual
33 into another language, under the above conditions for modified versions.
34
35 @ignore
36 Permission is granted to process this file through Tex and print the
37 results, provided the printed document carries copying permission
38 notice identical to this one except for the removal of this paragraph
39 (this paragraph not being relevant to the printed manual).
40
41 @end ignore
42 @end ifinfo
43 @iftex
44 @finalout
45 @setchapternewpage odd
46 @settitle Using LD, the GNU linker
47 @titlepage
48 @title Using ld
49 @subtitle The GNU linker
50 @sp 1
51 @subtitle @code{ld} version 2
52 @subtitle Version @value{VERSION}
53 @author Steve Chamberlain
54 @author Ian Lance Taylor
55 @author Cygnus Solutions
56 @page
57
58 @tex
59 {\parskip=0pt
60 \hfill Cygnus Solutions\par
61 \hfill ian\@cygnus.com, doc\@cygnus.com\par
62 \hfill {\it Using LD, the GNU linker}\par
63 \hfill Edited by Jeffrey Osier (jeffrey\@cygnus.com)\par
64 }
65 \global\parindent=0pt % Steve likes it this way.
66 @end tex
67
68 @vskip 0pt plus 1filll
69 Copyright @copyright{} 1991, 92, 93, 94, 95, 96, 97, 98, 1999 Free Software Foundation, Inc.
70
71 Permission is granted to make and distribute verbatim copies of
72 this manual provided the copyright notice and this permission notice
73 are preserved on all copies.
74
75 Permission is granted to copy and distribute modified versions of this
76 manual under the conditions for verbatim copying, provided also that
77 the entire resulting derived work is distributed under the terms of a
78 permission notice identical to this one.
79
80 Permission is granted to copy and distribute translations of this manual
81 into another language, under the above conditions for modified versions.
82 @end titlepage
83 @end iftex
84 @c FIXME: Talk about importance of *order* of args, cmds to linker!
85
86 @ifinfo
87 @node Top
88 @top Using ld
89 This file documents the @sc{gnu} linker ld version @value{VERSION}.
90
91 @menu
92 * Overview:: Overview
93 * Invocation:: Invocation
94 * Scripts:: Linker Scripts
95 @ifset GENERIC
96 * Machine Dependent:: Machine Dependent Features
97 @end ifset
98 @ifclear GENERIC
99 @ifset H8300
100 * H8/300:: ld and the H8/300
101 @end ifset
102 @ifset Hitachi
103 * Hitachi:: ld and other Hitachi micros
104 @end ifset
105 @ifset I960
106 * i960:: ld and the Intel 960 family
107 @end ifset
108 @end ifclear
109 @ifclear SingleFormat
110 * BFD:: BFD
111 @end ifclear
112 @c Following blank line required for remaining bug in makeinfo conds/menus
113
114 * Reporting Bugs:: Reporting Bugs
115 * MRI:: MRI Compatible Script Files
116 * Index:: Index
117 @end menu
118 @end ifinfo
119
120 @node Overview
121 @chapter Overview
122
123 @cindex @sc{gnu} linker
124 @cindex what is this?
125 @code{ld} combines a number of object and archive files, relocates
126 their data and ties up symbol references. Usually the last step in
127 compiling a program is to run @code{ld}.
128
129 @code{ld} accepts Linker Command Language files written in
130 a superset of AT&T's Link Editor Command Language syntax,
131 to provide explicit and total control over the linking process.
132
133 @ifclear SingleFormat
134 This version of @code{ld} uses the general purpose BFD libraries
135 to operate on object files. This allows @code{ld} to read, combine, and
136 write object files in many different formats---for example, COFF or
137 @code{a.out}. Different formats may be linked together to produce any
138 available kind of object file. @xref{BFD}, for more information.
139 @end ifclear
140
141 Aside from its flexibility, the @sc{gnu} linker is more helpful than other
142 linkers in providing diagnostic information. Many linkers abandon
143 execution immediately upon encountering an error; whenever possible,
144 @code{ld} continues executing, allowing you to identify other errors
145 (or, in some cases, to get an output file in spite of the error).
146
147 @node Invocation
148 @chapter Invocation
149
150 The @sc{gnu} linker @code{ld} is meant to cover a broad range of situations,
151 and to be as compatible as possible with other linkers. As a result,
152 you have many choices to control its behavior.
153
154 @ifset UsesEnvVars
155 @menu
156 * Options:: Command Line Options
157 * Environment:: Environment Variables
158 @end menu
159
160 @node Options
161 @section Command Line Options
162 @end ifset
163
164 @cindex command line
165 @cindex options
166 The linker supports a plethora of command-line options, but in actual
167 practice few of them are used in any particular context.
168 @cindex standard Unix system
169 For instance, a frequent use of @code{ld} is to link standard Unix
170 object files on a standard, supported Unix system. On such a system, to
171 link a file @code{hello.o}:
172
173 @smallexample
174 ld -o @var{output} /lib/crt0.o hello.o -lc
175 @end smallexample
176
177 This tells @code{ld} to produce a file called @var{output} as the
178 result of linking the file @code{/lib/crt0.o} with @code{hello.o} and
179 the library @code{libc.a}, which will come from the standard search
180 directories. (See the discussion of the @samp{-l} option below.)
181
182 The command-line options to @code{ld} may be specified in any order, and
183 may be repeated at will. Repeating most options with a different
184 argument will either have no further effect, or override prior
185 occurrences (those further to the left on the command line) of that
186 option. Options which may be meaningfully specified more than once are
187 noted in the descriptions below.
188
189 @cindex object files
190 Non-option arguments are objects files which are to be linked together.
191 They may follow, precede, or be mixed in with command-line options,
192 except that an object file argument may not be placed between an option
193 and its argument.
194
195 Usually the linker is invoked with at least one object file, but you can
196 specify other forms of binary input files using @samp{-l}, @samp{-R},
197 and the script command language. If @emph{no} binary input files at all
198 are specified, the linker does not produce any output, and issues the
199 message @samp{No input files}.
200
201 If the linker can not recognize the format of an object file, it will
202 assume that it is a linker script. A script specified in this way
203 augments the main linker script used for the link (either the default
204 linker script or the one specified by using @samp{-T}). This feature
205 permits the linker to link against a file which appears to be an object
206 or an archive, but actually merely defines some symbol values, or uses
207 @code{INPUT} or @code{GROUP} to load other objects. Note that
208 specifying a script in this way should only be used to augment the main
209 linker script; if you want to use some command that logically can only
210 appear once, such as the @code{SECTIONS} or @code{MEMORY} command, you
211 must replace the default linker script using the @samp{-T} option.
212 @xref{Scripts}.
213
214 For options whose names are a single letter,
215 option arguments must either follow the option letter without intervening
216 whitespace, or be given as separate arguments immediately following the
217 option that requires them.
218
219 For options whose names are multiple letters, either one dash or two can
220 precede the option name; for example, @samp{--oformat} and
221 @samp{--oformat} are equivalent. Arguments to multiple-letter options
222 must either be separated from the option name by an equals sign, or be
223 given as separate arguments immediately following the option that
224 requires them. For example, @samp{--oformat srec} and
225 @samp{--oformat=srec} are equivalent. Unique abbreviations of the names
226 of multiple-letter options are accepted.
227
228 @table @code
229 @kindex -a@var{keyword}
230 @item -a@var{keyword}
231 This option is supported for HP/UX compatibility. The @var{keyword}
232 argument must be one of the strings @samp{archive}, @samp{shared}, or
233 @samp{default}. @samp{-aarchive} is functionally equivalent to
234 @samp{-Bstatic}, and the other two keywords are functionally equivalent
235 to @samp{-Bdynamic}. This option may be used any number of times.
236
237 @ifset I960
238 @cindex architectures
239 @kindex -A@var{arch}
240 @item -A@var{architecture}
241 @kindex --architecture=@var{arch}
242 @itemx --architecture=@var{architecture}
243 In the current release of @code{ld}, this option is useful only for the
244 Intel 960 family of architectures. In that @code{ld} configuration, the
245 @var{architecture} argument identifies the particular architecture in
246 the 960 family, enabling some safeguards and modifying the
247 archive-library search path. @xref{i960,,@code{ld} and the Intel 960
248 family}, for details.
249
250 Future releases of @code{ld} may support similar functionality for
251 other architecture families.
252 @end ifset
253
254 @ifclear SingleFormat
255 @cindex binary input format
256 @kindex -b @var{format}
257 @kindex --format=@var{format}
258 @cindex input format
259 @cindex input format
260 @item -b @var{input-format}
261 @itemx --format=@var{input-format}
262 @code{ld} may be configured to support more than one kind of object
263 file. If your @code{ld} is configured this way, you can use the
264 @samp{-b} option to specify the binary format for input object files
265 that follow this option on the command line. Even when @code{ld} is
266 configured to support alternative object formats, you don't usually need
267 to specify this, as @code{ld} should be configured to expect as a
268 default input format the most usual format on each machine.
269 @var{input-format} is a text string, the name of a particular format
270 supported by the BFD libraries. (You can list the available binary
271 formats with @samp{objdump -i}.)
272 @xref{BFD}.
273
274 You may want to use this option if you are linking files with an unusual
275 binary format. You can also use @samp{-b} to switch formats explicitly (when
276 linking object files of different formats), by including
277 @samp{-b @var{input-format}} before each group of object files in a
278 particular format.
279
280 The default format is taken from the environment variable
281 @code{GNUTARGET}.
282 @ifset UsesEnvVars
283 @xref{Environment}.
284 @end ifset
285 You can also define the input format from a script, using the command
286 @code{TARGET}; see @ref{Format Commands}.
287 @end ifclear
288
289 @kindex -c @var{MRI-cmdfile}
290 @kindex --mri-script=@var{MRI-cmdfile}
291 @cindex compatibility, MRI
292 @item -c @var{MRI-commandfile}
293 @itemx --mri-script=@var{MRI-commandfile}
294 For compatibility with linkers produced by MRI, @code{ld} accepts script
295 files written in an alternate, restricted command language, described in
296 @ref{MRI,,MRI Compatible Script Files}. Introduce MRI script files with
297 the option @samp{-c}; use the @samp{-T} option to run linker
298 scripts written in the general-purpose @code{ld} scripting language.
299 If @var{MRI-cmdfile} does not exist, @code{ld} looks for it in the directories
300 specified by any @samp{-L} options.
301
302 @cindex common allocation
303 @kindex -d
304 @kindex -dc
305 @kindex -dp
306 @item -d
307 @itemx -dc
308 @itemx -dp
309 These three options are equivalent; multiple forms are supported for
310 compatibility with other linkers. They assign space to common symbols
311 even if a relocatable output file is specified (with @samp{-r}). The
312 script command @code{FORCE_COMMON_ALLOCATION} has the same effect.
313 @xref{Miscellaneous Commands}.
314
315 @cindex entry point, from command line
316 @kindex -e @var{entry}
317 @kindex --entry=@var{entry}
318 @item -e @var{entry}
319 @itemx --entry=@var{entry}
320 Use @var{entry} as the explicit symbol for beginning execution of your
321 program, rather than the default entry point. If there is no symbol
322 named @var{entry}, the linker will try to parse @var{entry} as a number,
323 and use that as the entry address (the number will be interpreted in
324 base 10; you may use a leading @samp{0x} for base 16, or a leading
325 @samp{0} for base 8). @xref{Entry Point}, for a discussion of defaults
326 and other ways of specifying the entry point.
327
328 @cindex dynamic symbol table
329 @kindex -E
330 @kindex --export-dynamic
331 @item -E
332 @itemx --export-dynamic
333 When creating a dynamically linked executable, add all symbols to the
334 dynamic symbol table. The dynamic symbol table is the set of symbols
335 which are visible from dynamic objects at run time.
336
337 If you do not use this option, the dynamic symbol table will normally
338 contain only those symbols which are referenced by some dynamic object
339 mentioned in the link.
340
341 If you use @code{dlopen} to load a dynamic object which needs to refer
342 back to the symbols defined by the program, rather than some other
343 dynamic object, then you will probably need to use this option when
344 linking the program itself.
345
346 @cindex big-endian objects
347 @cindex endianness
348 @kindex -EB
349 @item -EB
350 Link big-endian objects. This affects the default output format.
351
352 @cindex little-endian objects
353 @kindex -EL
354 @item -EL
355 Link little-endian objects. This affects the default output format.
356
357 @kindex -f
358 @kindex --auxiliary
359 @item -f
360 @itemx --auxiliary @var{name}
361 When creating an ELF shared object, set the internal DT_AUXILIARY field
362 to the specified name. This tells the dynamic linker that the symbol
363 table of the shared object should be used as an auxiliary filter on the
364 symbol table of the shared object @var{name}.
365
366 If you later link a program against this filter object, then, when you
367 run the program, the dynamic linker will see the DT_AUXILIARY field. If
368 the dynamic linker resolves any symbols from the filter object, it will
369 first check whether there is a definition in the shared object
370 @var{name}. If there is one, it will be used instead of the definition
371 in the filter object. The shared object @var{name} need not exist.
372 Thus the shared object @var{name} may be used to provide an alternative
373 implementation of certain functions, perhaps for debugging or for
374 machine specific performance.
375
376 This option may be specified more than once. The DT_AUXILIARY entries
377 will be created in the order in which they appear on the command line.
378
379 @kindex -F
380 @kindex --filter
381 @item -F @var{name}
382 @itemx --filter @var{name}
383 When creating an ELF shared object, set the internal DT_FILTER field to
384 the specified name. This tells the dynamic linker that the symbol table
385 of the shared object which is being created should be used as a filter
386 on the symbol table of the shared object @var{name}.
387
388 If you later link a program against this filter object, then, when you
389 run the program, the dynamic linker will see the DT_FILTER field. The
390 dynamic linker will resolve symbols according to the symbol table of the
391 filter object as usual, but it will actually link to the definitions
392 found in the shared object @var{name}. Thus the filter object can be
393 used to select a subset of the symbols provided by the object
394 @var{name}.
395
396 Some older linkers used the @code{-F} option throughout a compilation
397 toolchain for specifying object-file format for both input and output
398 object files. The @sc{gnu} linker uses other mechanisms for this
399 purpose: the @code{-b}, @code{--format}, @code{--oformat} options, the
400 @code{TARGET} command in linker scripts, and the @code{GNUTARGET}
401 environment variable. The @sc{gnu} linker will ignore the @code{-F}
402 option when not creating an ELF shared object.
403
404 @kindex -g
405 @item -g
406 Ignored. Provided for compatibility with other tools.
407
408 @kindex -G
409 @kindex --gpsize
410 @cindex object size
411 @item -G@var{value}
412 @itemx --gpsize=@var{value}
413 Set the maximum size of objects to be optimized using the GP register to
414 @var{size}. This is only meaningful for object file formats such as
415 MIPS ECOFF which supports putting large and small objects into different
416 sections. This is ignored for other object file formats.
417
418 @cindex runtime library name
419 @kindex -h@var{name}
420 @kindex -soname=@var{name}
421 @item -h@var{name}
422 @itemx -soname=@var{name}
423 When creating an ELF shared object, set the internal DT_SONAME field to
424 the specified name. When an executable is linked with a shared object
425 which has a DT_SONAME field, then when the executable is run the dynamic
426 linker will attempt to load the shared object specified by the DT_SONAME
427 field rather than the using the file name given to the linker.
428
429 @kindex -i
430 @cindex incremental link
431 @item -i
432 Perform an incremental link (same as option @samp{-r}).
433
434 @cindex archive files, from cmd line
435 @kindex -l@var{archive}
436 @kindex --library=@var{archive}
437 @item -l@var{archive}
438 @itemx --library=@var{archive}
439 Add archive file @var{archive} to the list of files to link. This
440 option may be used any number of times. @code{ld} will search its
441 path-list for occurrences of @code{lib@var{archive}.a} for every
442 @var{archive} specified.
443
444 On systems which support shared libraries, @code{ld} may also search for
445 libraries with extensions other than @code{.a}. Specifically, on ELF
446 and SunOS systems, @code{ld} will search a directory for a library with
447 an extension of @code{.so} before searching for one with an extension of
448 @code{.a}. By convention, a @code{.so} extension indicates a shared
449 library.
450
451 The linker will search an archive only once, at the location where it is
452 specified on the command line. If the archive defines a symbol which
453 was undefined in some object which appeared before the archive on the
454 command line, the linker will include the appropriate file(s) from the
455 archive. However, an undefined symbol in an object appearing later on
456 the command line will not cause the linker to search the archive again.
457
458 See the @code{-(} option for a way to force the linker to search
459 archives multiple times.
460
461 You may list the same archive multiple times on the command line.
462
463 @ifset GENERIC
464 This type of archive searching is standard for Unix linkers. However,
465 if you are using @code{ld} on AIX, note that it is different from the
466 behaviour of the AIX linker.
467 @end ifset
468
469 @cindex search directory, from cmd line
470 @kindex -L@var{dir}
471 @kindex --library-path=@var{dir}
472 @item -L@var{searchdir}
473 @itemx --library-path=@var{searchdir}
474 Add path @var{searchdir} to the list of paths that @code{ld} will search
475 for archive libraries and @code{ld} control scripts. You may use this
476 option any number of times. The directories are searched in the order
477 in which they are specified on the command line. Directories specified
478 on the command line are searched before the default directories. All
479 @code{-L} options apply to all @code{-l} options, regardless of the
480 order in which the options appear.
481
482 @ifset UsesEnvVars
483 The default set of paths searched (without being specified with
484 @samp{-L}) depends on which emulation mode @code{ld} is using, and in
485 some cases also on how it was configured. @xref{Environment}.
486 @end ifset
487
488 The paths can also be specified in a link script with the
489 @code{SEARCH_DIR} command. Directories specified this way are searched
490 at the point in which the linker script appears in the command line.
491
492 @cindex emulation
493 @kindex -m @var{emulation}
494 @item -m@var{emulation}
495 Emulate the @var{emulation} linker. You can list the available
496 emulations with the @samp{--verbose} or @samp{-V} options.
497
498 If the @samp{-m} option is not used, the emulation is taken from the
499 @code{LDEMULATION} environment variable, if that is defined.
500
501 Otherwise, the default emulation depends upon how the linker was
502 configured.
503
504 @cindex link map
505 @kindex -M
506 @kindex --print-map
507 @item -M
508 @itemx --print-map
509 Print a link map to the standard output. A link map provides
510 information about the link, including the following:
511
512 @itemize @bullet
513 @item
514 Where object files and symbols are mapped into memory.
515 @item
516 How common symbols are allocated.
517 @item
518 All archive members included in the link, with a mention of the symbol
519 which caused the archive member to be brought in.
520 @end itemize
521
522 @kindex -n
523 @cindex read-only text
524 @cindex NMAGIC
525 @kindex --nmagic
526 @item -n
527 @itemx --nmagic
528 Set the text segment to be read only, and mark the output as
529 @code{NMAGIC} if possible.
530
531 @kindex -N
532 @kindex --omagic
533 @cindex read/write from cmd line
534 @cindex OMAGIC
535 @item -N
536 @itemx --omagic
537 Set the text and data sections to be readable and writable. Also, do
538 not page-align the data segment. If the output format supports Unix
539 style magic numbers, mark the output as @code{OMAGIC}.
540
541 @kindex -o @var{output}
542 @kindex --output=@var{output}
543 @cindex naming the output file
544 @item -o @var{output}
545 @itemx --output=@var{output}
546 Use @var{output} as the name for the program produced by @code{ld}; if this
547 option is not specified, the name @file{a.out} is used by default. The
548 script command @code{OUTPUT} can also specify the output file name.
549
550 @kindex -O @var{level}
551 @cindex generating optimized output
552 @item -O @var{level}
553 If @var{level} is a numeric values greater than zero @code{ld} optimizes
554 the output. This might take significantly longer and therefore probably
555 should only be enabled for the final binary.
556
557 @cindex partial link
558 @cindex relocatable output
559 @kindex -r
560 @kindex --relocateable
561 @item -r
562 @itemx --relocateable
563 Generate relocatable output---i.e., generate an output file that can in
564 turn serve as input to @code{ld}. This is often called @dfn{partial
565 linking}. As a side effect, in environments that support standard Unix
566 magic numbers, this option also sets the output file's magic number to
567 @code{OMAGIC}.
568 @c ; see @code{-N}.
569 If this option is not specified, an absolute file is produced. When
570 linking C++ programs, this option @emph{will not} resolve references to
571 constructors; to do that, use @samp{-Ur}.
572
573 This option does the same thing as @samp{-i}.
574
575 @kindex -R @var{file}
576 @kindex --just-symbols=@var{file}
577 @cindex symbol-only input
578 @item -R @var{filename}
579 @itemx --just-symbols=@var{filename}
580 Read symbol names and their addresses from @var{filename}, but do not
581 relocate it or include it in the output. This allows your output file
582 to refer symbolically to absolute locations of memory defined in other
583 programs. You may use this option more than once.
584
585 For compatibility with other ELF linkers, if the @code{-R} option is
586 followed by a directory name, rather than a file name, it is treated as
587 the @code{-rpath} option.
588
589 @kindex -s
590 @kindex --strip-all
591 @cindex strip all symbols
592 @item -s
593 @itemx --strip-all
594 Omit all symbol information from the output file.
595
596 @kindex -S
597 @kindex --strip-debug
598 @cindex strip debugger symbols
599 @item -S
600 @itemx --strip-debug
601 Omit debugger symbol information (but not all symbols) from the output file.
602
603 @kindex -t
604 @kindex --trace
605 @cindex input files, displaying
606 @item -t
607 @itemx --trace
608 Print the names of the input files as @code{ld} processes them.
609
610 @kindex -T @var{script}
611 @kindex --script=@var{script}
612 @cindex script files
613 @item -T @var{scriptfile}
614 @itemx --script=@var{scriptfile}
615 Use @var{scriptfile} as the linker script. This script replaces
616 @code{ld}'s default linker script (rather than adding to it), so
617 @var{commandfile} must specify everything necessary to describe the
618 output file. You must use this option if you want to use a command
619 which can only appear once in a linker script, such as the
620 @code{SECTIONS} or @code{MEMORY} command. @xref{Scripts}. If
621 @var{scriptfile} does not exist in the current directory, @code{ld}
622 looks for it in the directories specified by any preceding @samp{-L}
623 options. Multiple @samp{-T} options accumulate.
624
625 @kindex -u @var{symbol}
626 @kindex --undefined=@var{symbol}
627 @cindex undefined symbol
628 @item -u @var{symbol}
629 @itemx --undefined=@var{symbol}
630 Force @var{symbol} to be entered in the output file as an undefined
631 symbol. Doing this may, for example, trigger linking of additional
632 modules from standard libraries. @samp{-u} may be repeated with
633 different option arguments to enter additional undefined symbols. This
634 option is equivalent to the @code{EXTERN} linker script command.
635
636 @kindex -Ur
637 @cindex constructors
638 @item -Ur
639 For anything other than C++ programs, this option is equivalent to
640 @samp{-r}: it generates relocatable output---i.e., an output file that can in
641 turn serve as input to @code{ld}. When linking C++ programs, @samp{-Ur}
642 @emph{does} resolve references to constructors, unlike @samp{-r}.
643 It does not work to use @samp{-Ur} on files that were themselves linked
644 with @samp{-Ur}; once the constructor table has been built, it cannot
645 be added to. Use @samp{-Ur} only for the last partial link, and
646 @samp{-r} for the others.
647
648 @kindex -v
649 @kindex -V
650 @kindex --version
651 @cindex version
652 @item -v
653 @itemx --version
654 @itemx -V
655 Display the version number for @code{ld}. The @code{-V} option also
656 lists the supported emulations.
657
658 @kindex -x
659 @kindex --discard-all
660 @cindex deleting local symbols
661 @item -x
662 @itemx --discard-all
663 Delete all local symbols.
664
665 @kindex -X
666 @kindex --discard-locals
667 @cindex local symbols, deleting
668 @cindex L, deleting symbols beginning
669 @item -X
670 @itemx --discard-locals
671 Delete all temporary local symbols. For most targets, this is all local
672 symbols whose names begin with @samp{L}.
673
674 @kindex -y @var{symbol}
675 @kindex --trace-symbol=@var{symbol}
676 @cindex symbol tracing
677 @item -y @var{symbol}
678 @itemx --trace-symbol=@var{symbol}
679 Print the name of each linked file in which @var{symbol} appears. This
680 option may be given any number of times. On many systems it is necessary
681 to prepend an underscore.
682
683 This option is useful when you have an undefined symbol in your link but
684 don't know where the reference is coming from.
685
686 @kindex -Y @var{path}
687 @item -Y @var{path}
688 Add @var{path} to the default library search path. This option exists
689 for Solaris compatibility.
690
691 @kindex -z @var{keyword}
692 @item -z @var{keyword}
693 This option is ignored for Solaris compatibility.
694
695 @kindex -(
696 @cindex groups of archives
697 @item -( @var{archives} -)
698 @itemx --start-group @var{archives} --end-group
699 The @var{archives} should be a list of archive files. They may be
700 either explicit file names, or @samp{-l} options.
701
702 The specified archives are searched repeatedly until no new undefined
703 references are created. Normally, an archive is searched only once in
704 the order that it is specified on the command line. If a symbol in that
705 archive is needed to resolve an undefined symbol referred to by an
706 object in an archive that appears later on the command line, the linker
707 would not be able to resolve that reference. By grouping the archives,
708 they all be searched repeatedly until all possible references are
709 resolved.
710
711 Using this option has a significant performance cost. It is best to use
712 it only when there are unavoidable circular references between two or
713 more archives.
714
715 @kindex -assert @var{keyword}
716 @item -assert @var{keyword}
717 This option is ignored for SunOS compatibility.
718
719 @kindex -Bdynamic
720 @kindex -dy
721 @kindex -call_shared
722 @item -Bdynamic
723 @itemx -dy
724 @itemx -call_shared
725 Link against dynamic libraries. This is only meaningful on platforms
726 for which shared libraries are supported. This option is normally the
727 default on such platforms. The different variants of this option are
728 for compatibility with various systems. You may use this option
729 multiple times on the command line: it affects library searching for
730 @code{-l} options which follow it.
731
732 @kindex -Bstatic
733 @kindex -dn
734 @kindex -non_shared
735 @kindex -static
736 @item -Bstatic
737 @itemx -dn
738 @itemx -non_shared
739 @itemx -static
740 Do not link against shared libraries. This is only meaningful on
741 platforms for which shared libraries are supported. The different
742 variants of this option are for compatibility with various systems. You
743 may use this option multiple times on the command line: it affects
744 library searching for @code{-l} options which follow it.
745
746 @kindex -Bsymbolic
747 @item -Bsymbolic
748 When creating a shared library, bind references to global symbols to the
749 definition within the shared library, if any. Normally, it is possible
750 for a program linked against a shared library to override the definition
751 within the shared library. This option is only meaningful on ELF
752 platforms which support shared libraries.
753
754 @kindex --check-sections
755 @kindex --no-check-sections
756 @item --check-sections
757 @item --no-check-sections
758 Asks the linker @emph{not} to check section addresses after they have
759 been assigned to see if there any overlaps. Normally the linker will
760 perform this check, and if it finds any overlaps it will produce
761 suitable error messages. The linker does know about, and does make
762 allowances for sections in overlays. The default behaviour can be
763 restored by using the command line switch @samp{--check-sections}.
764
765 @cindex cross reference table
766 @kindex --cref
767 @item --cref
768 Output a cross reference table. If a linker map file is being
769 generated, the cross reference table is printed to the map file.
770 Otherwise, it is printed on the standard output.
771
772 The format of the table is intentionally simple, so that it may be
773 easily processed by a script if necessary. The symbols are printed out,
774 sorted by name. For each symbol, a list of file names is given. If the
775 symbol is defined, the first file listed is the location of the
776 definition. The remaining files contain references to the symbol.
777
778 @cindex symbols, from command line
779 @kindex --defsym @var{symbol}=@var{exp}
780 @item --defsym @var{symbol}=@var{expression}
781 Create a global symbol in the output file, containing the absolute
782 address given by @var{expression}. You may use this option as many
783 times as necessary to define multiple symbols in the command line. A
784 limited form of arithmetic is supported for the @var{expression} in this
785 context: you may give a hexadecimal constant or the name of an existing
786 symbol, or use @code{+} and @code{-} to add or subtract hexadecimal
787 constants or symbols. If you need more elaborate expressions, consider
788 using the linker command language from a script (@pxref{Assignments,,
789 Assignment: Symbol Definitions}). @emph{Note:} there should be no white
790 space between @var{symbol}, the equals sign (``@key{=}''), and
791 @var{expression}.
792
793 @cindex demangling, from command line
794 @kindex --demangle
795 @kindex --no-demangle
796 @item --demangle
797 @itemx --no-demangle
798 These options control whether to demangle symbol names in error messages
799 and other output. When the linker is told to demangle, it tries to
800 present symbol names in a readable fashion: it strips leading
801 underscores if they are used by the object file format, and converts C++
802 mangled symbol names into user readable names. The linker will demangle
803 by default unless the environment variable @samp{COLLECT_NO_DEMANGLE} is
804 set. These options may be used to override the default.
805
806 @cindex dynamic linker, from command line
807 @kindex --dynamic-linker @var{file}
808 @item --dynamic-linker @var{file}
809 Set the name of the dynamic linker. This is only meaningful when
810 generating dynamically linked ELF executables. The default dynamic
811 linker is normally correct; don't use this unless you know what you are
812 doing.
813
814 @cindex MIPS embedded PIC code
815 @kindex --embedded-relocs
816 @item --embedded-relocs
817 This option is only meaningful when linking MIPS embedded PIC code,
818 generated by the -membedded-pic option to the @sc{gnu} compiler and
819 assembler. It causes the linker to create a table which may be used at
820 runtime to relocate any data which was statically initialized to pointer
821 values. See the code in testsuite/ld-empic for details.
822
823 @kindex --force-exe-suffix
824 @item --force-exe-suffix
825 Make sure that an output file has a .exe suffix.
826
827 If a successfully built fully linked output file does not have a
828 @code{.exe} or @code{.dll} suffix, this option forces the linker to copy
829 the output file to one of the same name with a @code{.exe} suffix. This
830 option is useful when using unmodified Unix makefiles on a Microsoft
831 Windows host, since some versions of Windows won't run an image unless
832 it ends in a @code{.exe} suffix.
833
834 @kindex --gc-sections
835 @kindex --no-gc-sections
836 @cindex garbage collection
837 @item --no-gc-sections
838 @itemx --gc-sections
839 Enable garbage collection of unused input sections. It is ignored on
840 targets that do not support this option. This option is not compatible
841 with @samp{-r}, nor should it be used with dynamic linking. The default
842 behaviour (of not performing this garbage collection) can be restored by
843 specifying @samp{--no-gc-sections} on the command line.
844
845 @cindex help
846 @cindex usage
847 @kindex --help
848 @item --help
849 Print a summary of the command-line options on the standard output and exit.
850
851 @kindex -Map
852 @item -Map @var{mapfile}
853 Print a link map to the file @var{mapfile}. See the description of the
854 @samp{-M} option, above.
855
856 @cindex memory usage
857 @kindex --no-keep-memory
858 @item --no-keep-memory
859 @code{ld} normally optimizes for speed over memory usage by caching the
860 symbol tables of input files in memory. This option tells @code{ld} to
861 instead optimize for memory usage, by rereading the symbol tables as
862 necessary. This may be required if @code{ld} runs out of memory space
863 while linking a large executable.
864
865 @kindex --no-undefined
866 @item --no-undefined
867 Normally when creating a non-symbolic shared library, undefined symbols
868 are allowed and left to be resolved by the runtime loader. This option
869 disallows such undefined symbols.
870
871 @kindex --no-warn-mismatch
872 @item --no-warn-mismatch
873 Normally @code{ld} will give an error if you try to link together input
874 files that are mismatched for some reason, perhaps because they have
875 been compiled for different processors or for different endiannesses.
876 This option tells @code{ld} that it should silently permit such possible
877 errors. This option should only be used with care, in cases when you
878 have taken some special action that ensures that the linker errors are
879 inappropriate.
880
881 @kindex --no-whole-archive
882 @item --no-whole-archive
883 Turn off the effect of the @code{--whole-archive} option for subsequent
884 archive files.
885
886 @cindex output file after errors
887 @kindex --noinhibit-exec
888 @item --noinhibit-exec
889 Retain the executable output file whenever it is still usable.
890 Normally, the linker will not produce an output file if it encounters
891 errors during the link process; it exits without writing an output file
892 when it issues any error whatsoever.
893
894 @ifclear SingleFormat
895 @kindex --oformat
896 @item --oformat @var{output-format}
897 @code{ld} may be configured to support more than one kind of object
898 file. If your @code{ld} is configured this way, you can use the
899 @samp{--oformat} option to specify the binary format for the output
900 object file. Even when @code{ld} is configured to support alternative
901 object formats, you don't usually need to specify this, as @code{ld}
902 should be configured to produce as a default output format the most
903 usual format on each machine. @var{output-format} is a text string, the
904 name of a particular format supported by the BFD libraries. (You can
905 list the available binary formats with @samp{objdump -i}.) The script
906 command @code{OUTPUT_FORMAT} can also specify the output format, but
907 this option overrides it. @xref{BFD}.
908 @end ifclear
909
910 @kindex -qmagic
911 @item -qmagic
912 This option is ignored for Linux compatibility.
913
914 @kindex -Qy
915 @item -Qy
916 This option is ignored for SVR4 compatibility.
917
918 @kindex --relax
919 @cindex synthesizing linker
920 @cindex relaxing addressing modes
921 @item --relax
922 An option with machine dependent effects.
923 @ifset GENERIC
924 This option is only supported on a few targets.
925 @end ifset
926 @ifset H8300
927 @xref{H8/300,,@code{ld} and the H8/300}.
928 @end ifset
929 @ifset I960
930 @xref{i960,, @code{ld} and the Intel 960 family}.
931 @end ifset
932
933
934 On some platforms, the @samp{--relax} option performs global
935 optimizations that become possible when the linker resolves addressing
936 in the program, such as relaxing address modes and synthesizing new
937 instructions in the output object file.
938
939 On some platforms these link time global optimizations may make symbolic
940 debugging of the resulting executable impossible.
941 @ifset GENERIC
942 This is known to be
943 the case for the Matsushita MN10200 and MN10300 family of processors.
944 @end ifset
945
946 @ifset GENERIC
947 On platforms where this is not supported, @samp{--relax} is accepted,
948 but ignored.
949 @end ifset
950
951 @cindex retaining specified symbols
952 @cindex stripping all but some symbols
953 @cindex symbols, retaining selectively
954 @item --retain-symbols-file @var{filename}
955 Retain @emph{only} the symbols listed in the file @var{filename},
956 discarding all others. @var{filename} is simply a flat file, with one
957 symbol name per line. This option is especially useful in environments
958 @ifset GENERIC
959 (such as VxWorks)
960 @end ifset
961 where a large global symbol table is accumulated gradually, to conserve
962 run-time memory.
963
964 @samp{--retain-symbols-file} does @emph{not} discard undefined symbols,
965 or symbols needed for relocations.
966
967 You may only specify @samp{--retain-symbols-file} once in the command
968 line. It overrides @samp{-s} and @samp{-S}.
969
970 @ifset GENERIC
971 @item -rpath @var{dir}
972 @cindex runtime library search path
973 @kindex -rpath
974 Add a directory to the runtime library search path. This is used when
975 linking an ELF executable with shared objects. All @code{-rpath}
976 arguments are concatenated and passed to the runtime linker, which uses
977 them to locate shared objects at runtime. The @code{-rpath} option is
978 also used when locating shared objects which are needed by shared
979 objects explicitly included in the link; see the description of the
980 @code{-rpath-link} option. If @code{-rpath} is not used when linking an
981 ELF executable, the contents of the environment variable
982 @code{LD_RUN_PATH} will be used if it is defined.
983
984 The @code{-rpath} option may also be used on SunOS. By default, on
985 SunOS, the linker will form a runtime search patch out of all the
986 @code{-L} options it is given. If a @code{-rpath} option is used, the
987 runtime search path will be formed exclusively using the @code{-rpath}
988 options, ignoring the @code{-L} options. This can be useful when using
989 gcc, which adds many @code{-L} options which may be on NFS mounted
990 filesystems.
991
992 For compatibility with other ELF linkers, if the @code{-R} option is
993 followed by a directory name, rather than a file name, it is treated as
994 the @code{-rpath} option.
995 @end ifset
996
997 @ifset GENERIC
998 @cindex link-time runtime library search path
999 @kindex -rpath-link
1000 @item -rpath-link @var{DIR}
1001 When using ELF or SunOS, one shared library may require another. This
1002 happens when an @code{ld -shared} link includes a shared library as one
1003 of the input files.
1004
1005 When the linker encounters such a dependency when doing a non-shared,
1006 non-relocatable link, it will automatically try to locate the required
1007 shared library and include it in the link, if it is not included
1008 explicitly. In such a case, the @code{-rpath-link} option
1009 specifies the first set of directories to search. The
1010 @code{-rpath-link} option may specify a sequence of directory names
1011 either by specifying a list of names separated by colons, or by
1012 appearing multiple times.
1013
1014 The linker uses the following search paths to locate required shared
1015 libraries.
1016 @enumerate
1017 @item
1018 Any directories specified by @code{-rpath-link} options.
1019 @item
1020 Any directories specified by @code{-rpath} options. The difference
1021 between @code{-rpath} and @code{-rpath-link} is that directories
1022 specified by @code{-rpath} options are included in the executable and
1023 used at runtime, whereas the @code{-rpath-link} option is only effective
1024 at link time.
1025 @item
1026 On an ELF system, if the @code{-rpath} and @code{rpath-link} options
1027 were not used, search the contents of the environment variable
1028 @code{LD_RUN_PATH}.
1029 @item
1030 On SunOS, if the @code{-rpath} option was not used, search any
1031 directories specified using @code{-L} options.
1032 @item
1033 For a native linker, the contents of the environment variable
1034 @code{LD_LIBRARY_PATH}.
1035 @item
1036 The default directories, normally @file{/lib} and @file{/usr/lib}.
1037 @item
1038 For a native linker on an ELF system, if the file @file{/etc/ld.so.conf}
1039 exists, the list of directories found in that file.
1040 @end enumerate
1041
1042 If the required shared library is not found, the linker will issue a
1043 warning and continue with the link.
1044 @end ifset
1045
1046 @kindex -shared
1047 @kindex -Bshareable
1048 @item -shared
1049 @itemx -Bshareable
1050 @cindex shared libraries
1051 Create a shared library. This is currently only supported on ELF, XCOFF
1052 and SunOS platforms. On SunOS, the linker will automatically create a
1053 shared library if the @code{-e} option is not used and there are
1054 undefined symbols in the link.
1055
1056 @item --sort-common
1057 @kindex --sort-common
1058 This option tells @code{ld} to sort the common symbols by size when it
1059 places them in the appropriate output sections. First come all the one
1060 byte symbols, then all the two bytes, then all the four bytes, and then
1061 everything else. This is to prevent gaps between symbols due to
1062 alignment constraints.
1063
1064 @kindex --split-by-file
1065 @item --split-by-file
1066 Similar to @code{--split-by-reloc} but creates a new output section for
1067 each input file.
1068
1069 @kindex --split-by-reloc
1070 @item --split-by-reloc @var{count}
1071 Trys to creates extra sections in the output file so that no single
1072 output section in the file contains more than @var{count} relocations.
1073 This is useful when generating huge relocatable for downloading into
1074 certain real time kernels with the COFF object file format; since COFF
1075 cannot represent more than 65535 relocations in a single section. Note
1076 that this will fail to work with object file formats which do not
1077 support arbitrary sections. The linker will not split up individual
1078 input sections for redistribution, so if a single input section contains
1079 more than @var{count} relocations one output section will contain that
1080 many relocations.
1081
1082 @kindex --stats
1083 @item --stats
1084 Compute and display statistics about the operation of the linker, such
1085 as execution time and memory usage.
1086
1087 @kindex --traditional-format
1088 @cindex traditional format
1089 @item --traditional-format
1090 For some targets, the output of @code{ld} is different in some ways from
1091 the output of some existing linker. This switch requests @code{ld} to
1092 use the traditional format instead.
1093
1094 @cindex dbx
1095 For example, on SunOS, @code{ld} combines duplicate entries in the
1096 symbol string table. This can reduce the size of an output file with
1097 full debugging information by over 30 percent. Unfortunately, the SunOS
1098 @code{dbx} program can not read the resulting program (@code{gdb} has no
1099 trouble). The @samp{--traditional-format} switch tells @code{ld} to not
1100 combine duplicate entries.
1101
1102 @kindex -Tbss @var{org}
1103 @kindex -Tdata @var{org}
1104 @kindex -Ttext @var{org}
1105 @cindex segment origins, cmd line
1106 @item -Tbss @var{org}
1107 @itemx -Tdata @var{org}
1108 @itemx -Ttext @var{org}
1109 Use @var{org} as the starting address for---respectively---the
1110 @code{bss}, @code{data}, or the @code{text} segment of the output file.
1111 @var{org} must be a single hexadecimal integer;
1112 for compatibility with other linkers, you may omit the leading
1113 @samp{0x} usually associated with hexadecimal values.
1114
1115 @kindex --verbose
1116 @cindex verbose
1117 @item --dll-verbose
1118 @item --verbose
1119 Display the version number for @code{ld} and list the linker emulations
1120 supported. Display which input files can and cannot be opened. Display
1121 the linker script if using a default builtin script.
1122
1123 @kindex --version-script=@var{version-scriptfile}
1124 @cindex version script, symbol versions
1125 @itemx --version-script=@var{version-scriptfile}
1126 Specify the name of a version script to the linker. This is typically
1127 used when creating shared libraries to specify additional information
1128 about the version heirarchy for the library being created. This option
1129 is only meaningful on ELF platforms which support shared libraries.
1130 @xref{VERSION}.
1131
1132 @kindex --warn-comon
1133 @cindex warnings, on combining symbols
1134 @cindex combining symbols, warnings on
1135 @item --warn-common
1136 Warn when a common symbol is combined with another common symbol or with
1137 a symbol definition. Unix linkers allow this somewhat sloppy practice,
1138 but linkers on some other operating systems do not. This option allows
1139 you to find potential problems from combining global symbols.
1140 Unfortunately, some C libraries use this practice, so you may get some
1141 warnings about symbols in the libraries as well as in your programs.
1142
1143 There are three kinds of global symbols, illustrated here by C examples:
1144
1145 @table @samp
1146 @item int i = 1;
1147 A definition, which goes in the initialized data section of the output
1148 file.
1149
1150 @item extern int i;
1151 An undefined reference, which does not allocate space.
1152 There must be either a definition or a common symbol for the
1153 variable somewhere.
1154
1155 @item int i;
1156 A common symbol. If there are only (one or more) common symbols for a
1157 variable, it goes in the uninitialized data area of the output file.
1158 The linker merges multiple common symbols for the same variable into a
1159 single symbol. If they are of different sizes, it picks the largest
1160 size. The linker turns a common symbol into a declaration, if there is
1161 a definition of the same variable.
1162 @end table
1163
1164 The @samp{--warn-common} option can produce five kinds of warnings.
1165 Each warning consists of a pair of lines: the first describes the symbol
1166 just encountered, and the second describes the previous symbol
1167 encountered with the same name. One or both of the two symbols will be
1168 a common symbol.
1169
1170 @enumerate
1171 @item
1172 Turning a common symbol into a reference, because there is already a
1173 definition for the symbol.
1174 @smallexample
1175 @var{file}(@var{section}): warning: common of `@var{symbol}'
1176 overridden by definition
1177 @var{file}(@var{section}): warning: defined here
1178 @end smallexample
1179
1180 @item
1181 Turning a common symbol into a reference, because a later definition for
1182 the symbol is encountered. This is the same as the previous case,
1183 except that the symbols are encountered in a different order.
1184 @smallexample
1185 @var{file}(@var{section}): warning: definition of `@var{symbol}'
1186 overriding common
1187 @var{file}(@var{section}): warning: common is here
1188 @end smallexample
1189
1190 @item
1191 Merging a common symbol with a previous same-sized common symbol.
1192 @smallexample
1193 @var{file}(@var{section}): warning: multiple common
1194 of `@var{symbol}'
1195 @var{file}(@var{section}): warning: previous common is here
1196 @end smallexample
1197
1198 @item
1199 Merging a common symbol with a previous larger common symbol.
1200 @smallexample
1201 @var{file}(@var{section}): warning: common of `@var{symbol}'
1202 overridden by larger common
1203 @var{file}(@var{section}): warning: larger common is here
1204 @end smallexample
1205
1206 @item
1207 Merging a common symbol with a previous smaller common symbol. This is
1208 the same as the previous case, except that the symbols are
1209 encountered in a different order.
1210 @smallexample
1211 @var{file}(@var{section}): warning: common of `@var{symbol}'
1212 overriding smaller common
1213 @var{file}(@var{section}): warning: smaller common is here
1214 @end smallexample
1215 @end enumerate
1216
1217 @kindex --warn-constructors
1218 @item --warn-constructors
1219 Warn if any global constructors are used. This is only useful for a few
1220 object file formats. For formats like COFF or ELF, the linker can not
1221 detect the use of global constructors.
1222
1223 @kindex --warn-multiple-gp
1224 @item --warn-multiple-gp
1225 Warn if multiple global pointer values are required in the output file.
1226 This is only meaningful for certain processors, such as the Alpha.
1227 Specifically, some processors put large-valued constants in a special
1228 section. A special register (the global pointer) points into the middle
1229 of this section, so that constants can be loaded efficiently via a
1230 base-register relative addressing mode. Since the offset in
1231 base-register relative mode is fixed and relatively small (e.g., 16
1232 bits), this limits the maximum size of the constant pool. Thus, in
1233 large programs, it is often necessary to use multiple global pointer
1234 values in order to be able to address all possible constants. This
1235 option causes a warning to be issued whenever this case occurs.
1236
1237 @kindex --warn-once
1238 @cindex warnings, on undefined symbols
1239 @cindex undefined symbols, warnings on
1240 @item --warn-once
1241 Only warn once for each undefined symbol, rather than once per module
1242 which refers to it.
1243
1244 @kindex --warn-section-align
1245 @cindex warnings, on section alignment
1246 @cindex section alignment, warnings on
1247 @item --warn-section-align
1248 Warn if the address of an output section is changed because of
1249 alignment. Typically, the alignment will be set by an input section.
1250 The address will only be changed if it not explicitly specified; that
1251 is, if the @code{SECTIONS} command does not specify a start address for
1252 the section (@pxref{SECTIONS}).
1253
1254 @kindex --whole-archive
1255 @cindex including an entire archive
1256 @item --whole-archive
1257 For each archive mentioned on the command line after the
1258 @code{--whole-archive} option, include every object file in the archive
1259 in the link, rather than searching the archive for the required object
1260 files. This is normally used to turn an archive file into a shared
1261 library, forcing every object to be included in the resulting shared
1262 library. This option may be used more than once.
1263
1264 @kindex --wrap
1265 @item --wrap @var{symbol}
1266 Use a wrapper function for @var{symbol}. Any undefined reference to
1267 @var{symbol} will be resolved to @code{__wrap_@var{symbol}}. Any
1268 undefined reference to @code{__real_@var{symbol}} will be resolved to
1269 @var{symbol}.
1270
1271 This can be used to provide a wrapper for a system function. The
1272 wrapper function should be called @code{__wrap_@var{symbol}}. If it
1273 wishes to call the system function, it should call
1274 @code{__real_@var{symbol}}.
1275
1276 Here is a trivial example:
1277
1278 @smallexample
1279 void *
1280 __wrap_malloc (int c)
1281 @{
1282 printf ("malloc called with %ld\n", c);
1283 return __real_malloc (c);
1284 @}
1285 @end smallexample
1286
1287 If you link other code with this file using @code{--wrap malloc}, then
1288 all calls to @code{malloc} will call the function @code{__wrap_malloc}
1289 instead. The call to @code{__real_malloc} in @code{__wrap_malloc} will
1290 call the real @code{malloc} function.
1291
1292 You may wish to provide a @code{__real_malloc} function as well, so that
1293 links without the @code{--wrap} option will succeed. If you do this,
1294 you should not put the definition of @code{__real_malloc} in the same
1295 file as @code{__wrap_malloc}; if you do, the assembler may resolve the
1296 call before the linker has a chance to wrap it to @code{malloc}.
1297
1298 @end table
1299
1300 @subsection Options specific to i386 PE targets
1301
1302 The i386 PE linker supports the @code{-shared} option, which causes
1303 the output to be a dynamically linked library (DLL) instead of a
1304 normal executable. You should name the output @code{*.dll} when you
1305 use this option. In addition, the linker fully supports the standard
1306 @code{*.def} files, which may be specified on the linker command line
1307 like an object file (in fact, it should precede archives it exports
1308 symbols from, to ensure that they get linked in, just like a normal
1309 object file).
1310
1311 In addition to the options common to all targets, the i386 PE linker
1312 support additional command line options that are specific to the i386
1313 PE target. Options that take values may be separated from their
1314 values by either a space or an equals sign.
1315
1316 @table @code
1317
1318 @kindex --add-stdcall-alias
1319 @item --add-stdcall-alias
1320 If given, symbols with a stdcall suffix (@@@var{nn}) will be exported
1321 as-is and also with the suffix stripped.
1322
1323 @kindex --base-file
1324 @item --base-file @var{file}
1325 Use @var{file} as the name of a file in which to save the base
1326 addresses of all the relocations needed for generating DLLs with
1327 @file{dlltool}.
1328
1329 @kindex --dll
1330 @item --dll
1331 Create a DLL instead of a regular executable. You may also use
1332 @code{-shared} or specify a @code{LIBRARY} in a given @code{.def}
1333 file.
1334
1335 @kindex --enable-stdcall-fixup
1336 @kindex --disable-stdcall-fixup
1337 @item --enable-stdcall-fixup
1338 @itemx --disable-stdcall-fixup
1339 If the link finds a symbol that it cannot resolve, it will attempt to
1340 do "fuzzy linking" by looking for another defined symbol that differs
1341 only in the format of the symbol name (cdecl vs stdcall) and will
1342 resolve that symbol by linking to the match. For example, the
1343 undefined symbol @code{_foo} might be linked to the function
1344 @code{_foo@@12}, or the undefined symbol @code{_bar@@16} might be linked
1345 to the function @code{_bar}. When the linker does this, it prints a
1346 warning, since it normally should have failed to link, but sometimes
1347 import libraries generated from third-party dlls may need this feature
1348 to be usable. If you specify @code{--enable-stdcall-fixup}, this
1349 feature is fully enabled and warnings are not printed. If you specify
1350 @code{--disable-stdcall-fixup}, this feature is disabled and such
1351 mismatches are considered to be errors.
1352
1353 @cindex DLLs, creating
1354 @kindex --export-all-symbols
1355 @item --export-all-symbols
1356 If given, all global symbols in the objects used to build a DLL will
1357 be exported by the DLL. Note that this is the default if there
1358 otherwise wouldn't be any exported symbols. When symbols are
1359 explicitly exported via DEF files or implicitly exported via function
1360 attributes, the default is to not export anything else unless this
1361 option is given. Note that the symbols @code{DllMain@@12},
1362 @code{DllEntryPoint@@0}, and @code{impure_ptr} will not be automatically
1363 exported.
1364
1365 @kindex --exclude-symbols
1366 @item --exclude-symbols @var{symbol,symbol,...}
1367 Specifies a list of symbols which should not be automatically
1368 exported. The symbol names may be delimited by commas or colons.
1369
1370 @kindex --file-alignment
1371 @item --file-alignment
1372 Specify the file alignment. Sections in the file will always begin at
1373 file offsets which are multiples of this number. This defaults to
1374 512.
1375
1376 @cindex heap size
1377 @kindex --heap
1378 @item --heap @var{reserve}
1379 @itemx --heap @var{reserve},@var{commit}
1380 Specify the amount of memory to reserve (and optionally commit) to be
1381 used as heap for this program. The default is 1Mb reserved, 4K
1382 committed.
1383
1384 @cindex image base
1385 @kindex --image-base
1386 @item --image-base @var{value}
1387 Use @var{value} as the base address of your program or dll. This is
1388 the lowest memory location that will be used when your program or dll
1389 is loaded. To reduce the need to relocate and improve performance of
1390 your dlls, each should have a unique base address and not overlap any
1391 other dlls. The default is 0x400000 for executables, and 0x10000000
1392 for dlls.
1393
1394 @kindex --kill-at
1395 @item --kill-at
1396 If given, the stdcall suffixes (@@@var{nn}) will be stripped from
1397 symbols before they are exported.
1398
1399 @kindex --major-image-version
1400 @item --major-image-version @var{value}
1401 Sets the major number of the "image version". Defaults to 1.
1402
1403 @kindex --major-os-version
1404 @item --major-os-version @var{value}
1405 Sets the major number of the "os version". Defaults to 4.
1406
1407 @kindex --major-subsystem-version
1408 @item --major-subsystem-version @var{value}
1409 Sets the major number of the "subsystem version". Defaults to 4.
1410
1411 @kindex --minor-image-version
1412 @item --minor-image-version @var{value}
1413 Sets the minor number of the "image version". Defaults to 0.
1414
1415 @kindex --minor-os-version
1416 @item --minor-os-version @var{value}
1417 Sets the minor number of the "os version". Defaults to 0.
1418
1419 @kindex --minor-subsystem-version
1420 @item --minor-subsystem-version @var{value}
1421 Sets the minor number of the "subsystem version". Defaults to 0.
1422
1423 @cindex DEF files, creating
1424 @cindex DLLs, creating
1425 @kindex --output-def
1426 @item --output-def @var{file}
1427 The linker will create the file @var{file} which will contain a DEF
1428 file corresponding to the DLL the linker is generating. This DEF file
1429 (which should be called @code{*.def}) may be used to create an import
1430 library with @code{dlltool} or may be used as a reference to
1431 automatically or implicitly exported symbols.
1432
1433 @kindex --section-alignment
1434 @item --section-alignment
1435 Sets the section alignment. Sections in memory will always begin at
1436 addresses which are a multiple of this number. Defaults to 0x1000.
1437
1438 @cindex stack size
1439 @kindex --stack
1440 @item --stack @var{reserve}
1441 @itemx --stack @var{reserve},@var{commit}
1442 Specify the amount of memory to reserve (and optionally commit) to be
1443 used as stack for this program. The default is 32Mb reserved, 4K
1444 committed.
1445
1446 @kindex --subsystem
1447 @item --subsystem @var{which}
1448 @itemx --subsystem @var{which}:@var{major}
1449 @itemx --subsystem @var{which}:@var{major}.@var{minor}
1450 Specifies the subsystem under which your program will execute. The
1451 legal values for @var{which} are @code{native}, @code{windows},
1452 @code{console}, and @code{posix}. You may optionally set the
1453 subsystem version also.
1454
1455 @end table
1456
1457 @ifset UsesEnvVars
1458 @node Environment
1459 @section Environment Variables
1460
1461 You can change the behavior of @code{ld} with the environment variables
1462 @code{GNUTARGET}, @code{LDEMULATION}, and @code{COLLECT_NO_DEMANGLE}.
1463
1464 @kindex GNUTARGET
1465 @cindex default input format
1466 @code{GNUTARGET} determines the input-file object format if you don't
1467 use @samp{-b} (or its synonym @samp{--format}). Its value should be one
1468 of the BFD names for an input format (@pxref{BFD}). If there is no
1469 @code{GNUTARGET} in the environment, @code{ld} uses the natural format
1470 of the target. If @code{GNUTARGET} is set to @code{default} then BFD
1471 attempts to discover the input format by examining binary input files;
1472 this method often succeeds, but there are potential ambiguities, since
1473 there is no method of ensuring that the magic number used to specify
1474 object-file formats is unique. However, the configuration procedure for
1475 BFD on each system places the conventional format for that system first
1476 in the search-list, so ambiguities are resolved in favor of convention.
1477
1478 @kindex LDEMULATION
1479 @cindex default emulation
1480 @cindex emulation, default
1481 @code{LDEMULATION} determines the default emulation if you don't use the
1482 @samp{-m} option. The emulation can affect various aspects of linker
1483 behaviour, particularly the default linker script. You can list the
1484 available emulations with the @samp{--verbose} or @samp{-V} options. If
1485 the @samp{-m} option is not used, and the @code{LDEMULATION} environment
1486 variable is not defined, the default emulation depends upon how the
1487 linker was configured.
1488 @end ifset
1489
1490 @kindex COLLECT_NO_DEMANGLE
1491 @cindex demangling, default
1492 Normally, the linker will default to demangling symbols. However, if
1493 @code{COLLECT_NO_DEMANGLE} is set in the environment, then it will
1494 default to not demangling symbols. This environment variable is used in
1495 a similar fashion by the @code{gcc} linker wrapper program. The default
1496 may be overridden by the @samp{--demangle} and @samp{--no-demangle}
1497 options.
1498
1499 @node Scripts
1500 @chapter Linker Scripts
1501
1502 @cindex scripts
1503 @cindex linker scripts
1504 @cindex command files
1505 Every link is controlled by a @dfn{linker script}. This script is
1506 written in the linker command language.
1507
1508 The main purpose of the linker script is to describe how the sections in
1509 the input files should be mapped into the output file, and to control
1510 the memory layout of the output file. Most linker scripts do nothing
1511 more than this. However, when necessary, the linker script can also
1512 direct the linker to perform many other operations, using the commands
1513 described below.
1514
1515 The linker always uses a linker script. If you do not supply one
1516 yourself, the linker will use a default script that is compiled into the
1517 linker executable. You can use the @samp{--verbose} command line option
1518 to display the default linker script. Certain command line options,
1519 such as @samp{-r} or @samp{-N}, will affect the default linker script.
1520
1521 You may supply your own linker script by using the @samp{-T} command
1522 line option. When you do this, your linker script will replace the
1523 default linker script.
1524
1525 You may also use linker scripts implicitly by naming them as input files
1526 to the linker, as though they were files to be linked. @xref{Implicit
1527 Linker Scripts}.
1528
1529 @menu
1530 * Basic Script Concepts:: Basic Linker Script Concepts
1531 * Script Format:: Linker Script Format
1532 * Simple Example:: Simple Linker Script Example
1533 * Simple Commands:: Simple Linker Script Commands
1534 * Assignments:: Assigning Values to Symbols
1535 * SECTIONS:: SECTIONS Command
1536 * MEMORY:: MEMORY Command
1537 * PHDRS:: PHDRS Command
1538 * VERSION:: VERSION Command
1539 * Expressions:: Expressions in Linker Scripts
1540 * Implicit Linker Scripts:: Implicit Linker Scripts
1541 @end menu
1542
1543 @node Basic Script Concepts
1544 @section Basic Linker Script Concepts
1545 @cindex linker script concepts
1546 We need to define some basic concepts and vocabulary in order to
1547 describe the linker script language.
1548
1549 The linker combines input files into a single output file. The output
1550 file and each input file are in a special data format known as an
1551 @dfn{object file format}. Each file is called an @dfn{object file}.
1552 The output file is often called an @dfn{executable}, but for our
1553 purposes we will also call it an object file. Each object file has,
1554 among other things, a list of @dfn{sections}. We sometimes refer to a
1555 section in an input file as an @dfn{input section}; similarly, a section
1556 in the output file is an @dfn{output section}.
1557
1558 Each section in an object file has a name and a size. Most sections
1559 also have an associated block of data, known as the @dfn{section
1560 contents}. A section may be marked as @dfn{loadable}, which mean that
1561 the contents should be loaded into memory when the output file is run.
1562 A section with no contents may be @dfn{allocatable}, which means that an
1563 area in memory should be set aside, but nothing in particular should be
1564 loaded there (in some cases this memory must be zeroed out). A section
1565 which is neither loadable nor allocatable typically contains some sort
1566 of debugging information.
1567
1568 Every loadable or allocatable output section has two addresses. The
1569 first is the @dfn{VMA}, or virtual memory address. This is the address
1570 the section will have when the output file is run. The second is the
1571 @dfn{LMA}, or load memory address. This is the address at which the
1572 section will be loaded. In most cases the two addresses will be the
1573 same. An example of when they might be different is when a data section
1574 is loaded into ROM, and then copied into RAM when the program starts up
1575 (this technique is often used to initialize global variables in a ROM
1576 based system). In this case the ROM address would be the LMA, and the
1577 RAM address would be the VMA.
1578
1579 You can see the sections in an object file by using the @code{objdump}
1580 program with the @samp{-h} option.
1581
1582 Every object file also has a list of @dfn{symbols}, known as the
1583 @dfn{symbol table}. A symbol may be defined or undefined. Each symbol
1584 has a name, and each defined symbol has an address, among other
1585 information. If you compile a C or C++ program into an object file, you
1586 will get a defined symbol for every defined function and global or
1587 static variable. Every undefined function or global variable which is
1588 referenced in the input file will become an undefined symbol.
1589
1590 You can see the symbols in an object file by using the @code{nm}
1591 program, or by using the @code{objdump} program with the @samp{-t}
1592 option.
1593
1594 @node Script Format
1595 @section Linker Script Format
1596 @cindex linker script format
1597 Linker scripts are text files.
1598
1599 You write a linker script as a series of commands. Each command is
1600 either a keyword, possibly followed by arguments, or an assignment to a
1601 symbol. You may separate commands using semicolons. Whitespace is
1602 generally ignored.
1603
1604 Strings such as file or format names can normally be entered directly.
1605 If the file name contains a character such as a comma which would
1606 otherwise serve to separate file names, you may put the file name in
1607 double quotes. There is no way to use a double quote character in a
1608 file name.
1609
1610 You may include comments in linker scripts just as in C, delimited by
1611 @samp{/*} and @samp{*/}. As in C, comments are syntactically equivalent
1612 to whitespace.
1613
1614 @node Simple Example
1615 @section Simple Linker Script Example
1616 @cindex linker script example
1617 @cindex example of linker script
1618 Many linker scripts are fairly simple.
1619
1620 The simplest possible linker script has just one command:
1621 @samp{SECTIONS}. You use the @samp{SECTIONS} command to describe the
1622 memory layout of the output file.
1623
1624 The @samp{SECTIONS} command is a powerful command. Here we will
1625 describe a simple use of it. Let's assume your program consists only of
1626 code, initialized data, and uninitialized data. These will be in the
1627 @samp{.text}, @samp{.data}, and @samp{.bss} sections, respectively.
1628 Let's assume further that these are the only sections which appear in
1629 your input files.
1630
1631 For this example, let's say that the code should be loaded at address
1632 0x10000, and that the data should start at address 0x8000000. Here is a
1633 linker script which will do that:
1634 @smallexample
1635 SECTIONS
1636 @{
1637 . = 0x10000;
1638 .text : @{ *(.text) @}
1639 . = 0x8000000;
1640 .data : @{ *(.data) @}
1641 .bss : @{ *(.bss) @}
1642 @}
1643 @end smallexample
1644
1645 You write the @samp{SECTIONS} command as the keyword @samp{SECTIONS},
1646 followed by a series of symbol assignments and output section
1647 descriptions enclosed in curly braces.
1648
1649 The first line in the above example sets the special symbol @samp{.},
1650 which is the location counter. If you do not specify the address of an
1651 output section in some other way (other ways are described later), the
1652 address is set from the current value of the location counter. The
1653 location counter is then incremented by the size of the output section.
1654
1655 The first line inside the @samp{SECTIONS} command of the above example
1656 sets the value of the special symbol @samp{.}, which is the location
1657 counter. If you do not specify the address of an output section in some
1658 other way (other ways are described later), the address is set from the
1659 current value of the location counter. The location counter is then
1660 incremented by the size of the output section. At the start of the
1661 @samp{SECTIONS} command, the location counter has the value @samp{0}.
1662
1663 The second line defines an output section, @samp{.text}. The colon is
1664 required syntax which may be ignored for now. Within the curly braces
1665 after the output section name, you list the names of the input sections
1666 which should be placed into this output section. The @samp{*} is a
1667 wildcard which matches any file name. The expression @samp{*(.text)}
1668 means all @samp{.text} input sections in all input files.
1669
1670 Since the location counter is @samp{0x10000} when the output section
1671 @samp{.text} is defined, the linker will set the address of the
1672 @samp{.text} section in the output file to be @samp{0x10000}.
1673
1674 The remaining lines define the @samp{.data} and @samp{.bss} sections in
1675 the output file. The linker will place the @samp{.data} output section
1676 at address @samp{0x8000000}. After the linker places the @samp{.data}
1677 output section, the value of the location counter will be
1678 @samp{0x8000000} plus the size of the @samp{.data} output section. The
1679 effect is that the linker will place the @samp{.bss} output section
1680 immediately after the @samp{.data} output section in memory
1681
1682 The linker will ensure that each output section has the required
1683 alignment, by increasing the location counter if necessary. In this
1684 example, the specified addresses for the @samp{.text} and @samp{.data}
1685 sections will probably satisfy any alignment constraints, but the linker
1686 may have to create a small gap between the @samp{.data} and @samp{.bss}
1687 sections.
1688
1689 That's it! That's a simple and complete linker script.
1690
1691 @node Simple Commands
1692 @section Simple Linker Script Commands
1693 @cindex linker script simple commands
1694 In this section we describe the simple linker script commands.
1695
1696 @menu
1697 * Entry Point:: Setting the entry point
1698 * File Commands:: Commands dealing with files
1699 @ifclear SingleFormat
1700 * Format Commands:: Commands dealing with object file formats
1701 @end ifclear
1702
1703 * Miscellaneous Commands:: Other linker script commands
1704 @end menu
1705
1706 @node Entry Point
1707 @subsection Setting the entry point
1708 @kindex ENTRY(@var{symbol})
1709 @cindex start of execution
1710 @cindex first instruction
1711 @cindex entry point
1712 The first instruction to execute in a program is called the @dfn{entry
1713 point}. You can use the @code{ENTRY} linker script command to set the
1714 entry point. The argument is a symbol name:
1715 @smallexample
1716 ENTRY(@var{symbol})
1717 @end smallexample
1718
1719 There are several ways to set the entry point. The linker will set the
1720 entry point by trying each of the following methods in order, and
1721 stopping when one of them succeeds:
1722 @itemize @bullet
1723 @item
1724 the @samp{-e} @var{entry} command-line option;
1725 @item
1726 the @code{ENTRY(@var{symbol})} command in a linker script;
1727 @item
1728 the value of the symbol @code{start}, if defined;
1729 @item
1730 the address of the first byte of the @samp{.text} section, if present;
1731 @item
1732 The address @code{0}.
1733 @end itemize
1734
1735 @node File Commands
1736 @subsection Commands dealing with files
1737 @cindex linker script file commands
1738 Several linker script commands deal with files.
1739
1740 @table @code
1741 @item INCLUDE @var{filename}
1742 @kindex INCLUDE @var{filename}
1743 @cindex including a linker script
1744 Include the linker script @var{filename} at this point. The file will
1745 be searched for in the current directory, and in any directory specified
1746 with the @code{-L} option. You can nest calls to @code{INCLUDE} up to
1747 10 levels deep.
1748
1749 @item INPUT(@var{file}, @var{file}, @dots{})
1750 @itemx INPUT(@var{file} @var{file} @dots{})
1751 @kindex INPUT(@var{files})
1752 @cindex input files in linker scripts
1753 @cindex input object files in linker scripts
1754 @cindex linker script input object files
1755 The @code{INPUT} command directs the linker to include the named files
1756 in the link, as though they were named on the command line.
1757
1758 For example, if you always want to include @file{subr.o} any time you do
1759 a link, but you can't be bothered to put it on every link command line,
1760 then you can put @samp{INPUT (subr.o)} in your linker script.
1761
1762 In fact, if you like, you can list all of your input files in the linker
1763 script, and then invoke the linker with nothing but a @samp{-T} option.
1764
1765 The linker will first try to open the file in the current directory. If
1766 it is not found, the linker will search through the archive library
1767 search path. See the description of @samp{-L} in @ref{Options,,Command
1768 Line Options}.
1769
1770 If you use @samp{INPUT (-l@var{file})}, @code{ld} will transform the
1771 name to @code{lib@var{file}.a}, as with the command line argument
1772 @samp{-l}.
1773
1774 When you use the @code{INPUT} command in an implicit linker script, the
1775 files will be included in the link at the point at which the linker
1776 script file is included. This can affect archive searching.
1777
1778 @item GROUP(@var{file}, @var{file}, @dots{})
1779 @itemx GROUP(@var{file} @var{file} @dots{})
1780 @kindex GROUP(@var{files})
1781 @cindex grouping input files
1782 The @code{GROUP} command is like @code{INPUT}, except that the named
1783 files should all be archives, and they are searched repeatedly until no
1784 new undefined references are created. See the description of @samp{-(}
1785 in @ref{Options,,Command Line Options}.
1786
1787 @item OUTPUT(@var{filename})
1788 @kindex OUTPUT(@var{filename})
1789 @cindex output file name in linker scripot
1790 The @code{OUTPUT} command names the output file. Using
1791 @code{OUTPUT(@var{filename})} in the linker script is exactly like using
1792 @samp{-o @var{filename}} on the command line (@pxref{Options,,Command
1793 Line Options}). If both are used, the command line option takes
1794 precedence.
1795
1796 You can use the @code{OUTPUT} command to define a default name for the
1797 output file other than the usual default of @file{a.out}.
1798
1799 @item SEARCH_DIR(@var{path})
1800 @kindex SEARCH_DIR(@var{path})
1801 @cindex library search path in linker script
1802 @cindex archive search path in linker script
1803 @cindex search path in linker script
1804 The @code{SEARCH_DIR} command adds @var{path} to the list of paths where
1805 @code{ld} looks for archive libraries. Using
1806 @code{SEARCH_DIR(@var{path})} is exactly like using @samp{-L @var{path}}
1807 on the command line (@pxref{Options,,Command Line Options}). If both
1808 are used, then the linker will search both paths. Paths specified using
1809 the command line option are searched first.
1810
1811 @item STARTUP(@var{filename})
1812 @kindex STARTUP(@var{filename})
1813 @cindex first input file
1814 The @code{STARTUP} command is just like the @code{INPUT} command, except
1815 that @var{filename} will become the first input file to be linked, as
1816 though it were specified first on the command line. This may be useful
1817 when using a system in which the entry point is always the start of the
1818 first file.
1819 @end table
1820
1821 @ifclear SingleFormat
1822 @node Format Commands
1823 @subsection Commands dealing with object file formats
1824 A couple of linker script commands deal with object file formats.
1825
1826 @table @code
1827 @item OUTPUT_FORMAT(@var{bfdname})
1828 @itemx OUTPUT_FORMAT(@var{default}, @var{big}, @var{little})
1829 @kindex OUTPUT_FORMAT(@var{bfdname})
1830 @cindex output file format in linker script
1831 The @code{OUTPUT_FORMAT} command names the BFD format to use for the
1832 output file (@pxref{BFD}). Using @code{OUTPUT_FORMAT(@var{bfdname})} is
1833 exactly like using @samp{-oformat @var{bfdname}} on the command line
1834 (@pxref{Options,,Command Line Options}). If both are used, the command
1835 line option takes precedence.
1836
1837 You can use @code{OUTPUT_FORMAT} with three arguments to use different
1838 formats based on the @samp{-EB} and @samp{-EL} command line options.
1839 This permits the linker script to set the output format based on the
1840 desired endianness.
1841
1842 If neither @samp{-EB} nor @samp{-EL} are used, then the output format
1843 will be the first argument, @var{default}. If @samp{-EB} is used, the
1844 output format will be the second argument, @var{big}. If @samp{-EL} is
1845 used, the output format will be the third argument, @var{little}.
1846
1847 For example, the default linker script for the MIPS ELF target uses this
1848 command:
1849 @smallexample
1850 OUTPUT_FORMAT(elf32-bigmips, elf32-bigmips, elf32-littlemips)
1851 @end smallexample
1852 This says that the default format for the output file is
1853 @samp{elf32-bigmips}, but if the user uses the @samp{-EL} command line
1854 option, the output file will be created in the @samp{elf32-littlemips}
1855 format.
1856
1857 @item TARGET(@var{bfdname})
1858 @kindex TARGET(@var{bfdname})
1859 @cindex input file format in linker script
1860 The @code{TARGET} command names the BFD format to use when reading input
1861 files. It affects subsequent @code{INPUT} and @code{GROUP} commands.
1862 This command is like using @samp{-b @var{bfdname}} on the command line
1863 (@pxref{Options,,Command Line Options}). If the @code{TARGET} command
1864 is used but @code{OUTPUT_FORMAT} is not, then the last @code{TARGET}
1865 command is also used to set the format for the output file. @xref{BFD}.
1866 @end table
1867 @end ifclear
1868
1869 @node Miscellaneous Commands
1870 @subsection Other linker script commands
1871 There are a few other linker scripts commands.
1872
1873 @table @code
1874 @item ASSERT(@var{exp}, @var{message})
1875 @kindex ASSERT
1876 @cindex assertion in linker script
1877 Ensure that @var{exp} is non-zero. If it is zero, then exit the linker
1878 with an error code, and print @var{message}.
1879
1880 @item EXTERN(@var{symbol} @var{symbol} @dots{})
1881 @kindex EXTERN
1882 @cindex undefined symbol in linker script
1883 Force @var{symbol} to be entered in the output file as an undefined
1884 symbol. Doing this may, for example, trigger linking of additional
1885 modules from standard libraries. You may list several @var{symbol}s for
1886 each @code{EXTERN}, and you may use @code{EXTERN} multiple times. This
1887 command has the same effect as the @samp{-u} command-line option.
1888
1889 @item FORCE_COMMON_ALLOCATION
1890 @kindex FORCE_COMMON_ALLOCATION
1891 @cindex common allocation in linker script
1892 This command has the same effect as the @samp{-d} command-line option:
1893 to make @code{ld} assign space to common symbols even if a relocatable
1894 output file is specified (@samp{-r}).
1895
1896 @item NOCROSSREFS(@var{section} @var{section} @dots{})
1897 @kindex NOCROSSREFS(@var{sections})
1898 @cindex cross references
1899 This command may be used to tell @code{ld} to issue an error about any
1900 references among certain output sections.
1901
1902 In certain types of programs, particularly on embedded systems when
1903 using overlays, when one section is loaded into memory, another section
1904 will not be. Any direct references between the two sections would be
1905 errors. For example, it would be an error if code in one section called
1906 a function defined in the other section.
1907
1908 The @code{NOCROSSREFS} command takes a list of output section names. If
1909 @code{ld} detects any cross references between the sections, it reports
1910 an error and returns a non-zero exit status. Note that the
1911 @code{NOCROSSREFS} command uses output section names, not input section
1912 names.
1913
1914 @ifclear SingleFormat
1915 @item OUTPUT_ARCH(@var{bfdarch})
1916 @kindex OUTPUT_ARCH(@var{bfdarch})
1917 @cindex machine architecture
1918 @cindex architecture
1919 Specify a particular output machine architecture. The argument is one
1920 of the names used by the BFD library (@pxref{BFD}). You can see the
1921 architecture of an object file by using the @code{objdump} program with
1922 the @samp{-f} option.
1923 @end ifclear
1924 @end table
1925
1926 @node Assignments
1927 @section Assigning Values to Symbols
1928 @cindex assignment in scripts
1929 @cindex symbol definition, scripts
1930 @cindex variables, defining
1931 You may assign a value to a symbol in a linker script. This will define
1932 the symbol as a global symbol.
1933
1934 @menu
1935 * Simple Assignments:: Simple Assignments
1936 * PROVIDE:: PROVIDE
1937 @end menu
1938
1939 @node Simple Assignments
1940 @subsection Simple Assignments
1941
1942 You may assign to a symbol using any of the C assignment operators:
1943
1944 @table @code
1945 @item @var{symbol} = @var{expression} ;
1946 @itemx @var{symbol} += @var{expression} ;
1947 @itemx @var{symbol} -= @var{expression} ;
1948 @itemx @var{symbol} *= @var{expression} ;
1949 @itemx @var{symbol} /= @var{expression} ;
1950 @itemx @var{symbol} <<= @var{expression} ;
1951 @itemx @var{symbol} >>= @var{expression} ;
1952 @itemx @var{symbol} &= @var{expression} ;
1953 @itemx @var{symbol} |= @var{expression} ;
1954 @end table
1955
1956 The first case will define @var{symbol} to the value of
1957 @var{expression}. In the other cases, @var{symbol} must already be
1958 defined, and the value will be adjusted accordingly.
1959
1960 The special symbol name @samp{.} indicates the location counter. You
1961 may only use this within a @code{SECTIONS} command.
1962
1963 The semicolon after @var{expression} is required.
1964
1965 Expressions are defined below; see @ref{Expressions}.
1966
1967 You may write symbol assignments as commands in their own right, or as
1968 statements within a @code{SECTIONS} command, or as part of an output
1969 section description in a @code{SECTIONS} command.
1970
1971 The section of the symbol will be set from the section of the
1972 expression; for more information, see @ref{Expression Section}.
1973
1974 Here is an example showing the three different places that symbol
1975 assignments may be used:
1976
1977 @smallexample
1978 floating_point = 0;
1979 SECTIONS
1980 @{
1981 .text :
1982 @{
1983 *(.text)
1984 _etext = .;
1985 @}
1986 _bdata = (. + 3) & ~ 4;
1987 .data : @{ *(.data) @}
1988 @}
1989 @end smallexample
1990 @noindent
1991 In this example, the symbol @samp{floating_point} will be defined as
1992 zero. The symbol @samp{_etext} will be defined as the address following
1993 the last @samp{.text} input section. The symbol @samp{_bdata} will be
1994 defined as the address following the @samp{.text} output section aligned
1995 upward to a 4 byte boundary.
1996
1997 @node PROVIDE
1998 @subsection PROVIDE
1999 @cindex PROVIDE
2000 In some cases, it is desirable for a linker script to define a symbol
2001 only if it is referenced and is not defined by any object included in
2002 the link. For example, traditional linkers defined the symbol
2003 @samp{etext}. However, ANSI C requires that the user be able to use
2004 @samp{etext} as a function name without encountering an error. The
2005 @code{PROVIDE} keyword may be used to define a symbol, such as
2006 @samp{etext}, only if it is referenced but not defined. The syntax is
2007 @code{PROVIDE(@var{symbol} = @var{expression})}.
2008
2009 Here is an example of using @code{PROVIDE} to define @samp{etext}:
2010 @smallexample
2011 SECTIONS
2012 @{
2013 .text :
2014 @{
2015 *(.text)
2016 _etext = .;
2017 PROVIDE(etext = .);
2018 @}
2019 @}
2020 @end smallexample
2021
2022 In this example, if the program defines @samp{_etext} (with a leading
2023 underscore), the linker will give a multiple definition error. If, on
2024 the other hand, the program defines @samp{etext} (with no leading
2025 underscore), the linker will silently use the definition in the program.
2026 If the program references @samp{etext} but does not define it, the
2027 linker will use the definition in the linker script.
2028
2029 @node SECTIONS
2030 @section SECTIONS command
2031 @kindex SECTIONS
2032 The @code{SECTIONS} command tells the linker how to map input sections
2033 into output sections, and how to place the output sections in memory.
2034
2035 The format of the @code{SECTIONS} command is:
2036 @smallexample
2037 SECTIONS
2038 @{
2039 @var{sections-command}
2040 @var{sections-command}
2041 @dots{}
2042 @}
2043 @end smallexample
2044
2045 Each @var{sections-command} may of be one of the following:
2046
2047 @itemize @bullet
2048 @item
2049 an @code{ENTRY} command (@pxref{Entry Point,,Entry command})
2050 @item
2051 a symbol assignment (@pxref{Assignments})
2052 @item
2053 an output section description
2054 @item
2055 an overlay description
2056 @end itemize
2057
2058 The @code{ENTRY} command and symbol assignments are permitted inside the
2059 @code{SECTIONS} command for convenience in using the location counter in
2060 those commands. This can also make the linker script easier to
2061 understand because you can use those commands at meaningful points in
2062 the layout of the output file.
2063
2064 Output section descriptions and overlay descriptions are described
2065 below.
2066
2067 If you do not use a @code{SECTIONS} command in your linker script, the
2068 linker will place each input section into an identically named output
2069 section in the order that the sections are first encountered in the
2070 input files. If all input sections are present in the first file, for
2071 example, the order of sections in the output file will match the order
2072 in the first input file. The first section will be at address zero.
2073
2074 @menu
2075 * Output Section Description:: Output section description
2076 * Output Section Name:: Output section name
2077 * Output Section Address:: Output section address
2078 * Input Section:: Input section description
2079 * Output Section Data:: Output section data
2080 * Output Section Keywords:: Output section keywords
2081 * Output Section Discarding:: Output section discarding
2082 * Output Section Attributes:: Output section attributes
2083 * Overlay Description:: Overlay description
2084 @end menu
2085
2086 @node Output Section Description
2087 @subsection Output section description
2088 The full description of an output section looks like this:
2089 @smallexample
2090 @group
2091 @var{section} [@var{address}] [(@var{type})] : [AT(@var{lma})]
2092 @{
2093 @var{output-section-command}
2094 @var{output-section-command}
2095 @dots{}
2096 @} [>@var{region}] [:@var{phdr} :@var{phdr} @dots{}] [=@var{fillexp}]
2097 @end group
2098 @end smallexample
2099
2100 Most output sections do not use most of the optional section attributes.
2101
2102 The whitespace around @var{section} is required, so that the section
2103 name is unambiguous. The colon and the curly braces are also required.
2104 The line breaks and other white space are optional.
2105
2106 Each @var{output-section-command} may be one of the following:
2107
2108 @itemize @bullet
2109 @item
2110 a symbol assignment (@pxref{Assignments})
2111 @item
2112 an input section description (@pxref{Input Section})
2113 @item
2114 data values to include directly (@pxref{Output Section Data})
2115 @item
2116 a special output section keyword (@pxref{Output Section Keywords})
2117 @end itemize
2118
2119 @node Output Section Name
2120 @subsection Output section name
2121 @cindex name, section
2122 @cindex section name
2123 The name of the output section is @var{section}. @var{section} must
2124 meet the constraints of your output format. In formats which only
2125 support a limited number of sections, such as @code{a.out}, the name
2126 must be one of the names supported by the format (@code{a.out}, for
2127 example, allows only @samp{.text}, @samp{.data} or @samp{.bss}). If the
2128 output format supports any number of sections, but with numbers and not
2129 names (as is the case for Oasys), the name should be supplied as a
2130 quoted numeric string. A section name may consist of any sequence of
2131 characters, but a name which contains any unusual characters such as
2132 commas must be quoted.
2133
2134 The output section name @samp{/DISCARD/} is special; @ref{Output Section
2135 Discarding}.
2136
2137 @node Output Section Address
2138 @subsection Output section address
2139 @cindex address, section
2140 @cindex section address
2141 The @var{address} is an expression for the VMA (the virtual memory
2142 address) of the output section. If you do not provide @var{address},
2143 the linker will set it based on @var{region} if present, or otherwise
2144 based on the current value of the location counter.
2145
2146 If you provide @var{address}, the address of the output section will be
2147 set to precisely that. If you provide neither @var{address} nor
2148 @var{region}, then the address of the output section will be set to the
2149 current value of the location counter aligned to the alignment
2150 requirements of the output section. The alignment requirement of the
2151 output section is the strictest alignment of any input section contained
2152 within the output section.
2153
2154 For example,
2155 @smallexample
2156 .text . : @{ *(.text) @}
2157 @end smallexample
2158 @noindent
2159 and
2160 @smallexample
2161 .text : @{ *(.text) @}
2162 @end smallexample
2163 @noindent
2164 are subtly different. The first will set the address of the
2165 @samp{.text} output section to the current value of the location
2166 counter. The second will set it to the current value of the location
2167 counter aligned to the strictest alignment of a @samp{.text} input
2168 section.
2169
2170 The @var{address} may be an arbitrary expression; @ref{Expressions}.
2171 For example, if you want to align the section on a 0x10 byte boundary,
2172 so that the lowest four bits of the section address are zero, you could
2173 do something like this:
2174 @smallexample
2175 .text ALIGN(0x10) : @{ *(.text) @}
2176 @end smallexample
2177 @noindent
2178 This works because @code{ALIGN} returns the current location counter
2179 aligned upward to the specified value.
2180
2181 Specifying @var{address} for a section will change the value of the
2182 location counter.
2183
2184 @node Input Section
2185 @subsection Input section description
2186 @cindex input sections
2187 @cindex mapping input sections to output sections
2188 The most common output section command is an input section description.
2189
2190 The input section description is the most basic linker script operation.
2191 You use output sections to tell the linker how to lay out your program
2192 in memory. You use input section descriptions to tell the linker how to
2193 map the input files into your memory layout.
2194
2195 @menu
2196 * Input Section Basics:: Input section basics
2197 * Input Section Wildcards:: Input section wildcard patterns
2198 * Input Section Common:: Input section for common symbols
2199 * Input Section Keep:: Input section and garbage collection
2200 * Input Section Example:: Input section example
2201 @end menu
2202
2203 @node Input Section Basics
2204 @subsubsection Input section basics
2205 @cindex input section basics
2206 An input section description consists of a file name optionally followed
2207 by a list of section names in parentheses.
2208
2209 The file name and the section name may be wildcard patterns, which we
2210 describe further below (@pxref{Input Section Wildcards}).
2211
2212 The most common input section description is to include all input
2213 sections with a particular name in the output section. For example, to
2214 include all input @samp{.text} sections, you would write:
2215 @smallexample
2216 *(.text)
2217 @end smallexample
2218 @noindent
2219 Here the @samp{*} is a wildcard which matches any file name. To exclude a file
2220 from matching the file name wildcard, EXCLUDE_FILE may be used to match all files
2221 except the one specified by EXCLUDE_FILE. For example:
2222 @smallexample
2223 (*(EXCLUDE_FILE (*crtend.o) .ctors))
2224 @end smallexample
2225 will cause all .ctors sections from all files except crtend.o to be included.
2226
2227 There are two ways to include more than one section:
2228 @smallexample
2229 *(.text .rdata)
2230 *(.text) *(.rdata)
2231 @end smallexample
2232 @noindent
2233 The difference between these is the order in which the @samp{.text} and
2234 @samp{.rdata} input sections will appear in the output section. In the
2235 first example, they will be intermingled. In the second example, all
2236 @samp{.text} input sections will appear first, followed by all
2237 @samp{.rdata} input sections.
2238
2239 You can specify a file name to include sections from a particular file.
2240 You would do this if one or more of your files contain special data that
2241 needs to be at a particular location in memory. For example:
2242 @smallexample
2243 data.o(.data)
2244 @end smallexample
2245
2246 If you use a file name without a list of sections, then all sections in
2247 the input file will be included in the output section. This is not
2248 commonly done, but it may by useful on occasion. For example:
2249 @smallexample
2250 data.o
2251 @end smallexample
2252
2253 When you use a file name which does not contain any wild card
2254 characters, the linker will first see if you also specified the file
2255 name on the linker command line or in an @code{INPUT} command. If you
2256 did not, the linker will attempt to open the file as an input file, as
2257 though it appeared on the command line. Note that this differs from an
2258 @code{INPUT} command, because the linker will not search for the file in
2259 the archive search path.
2260
2261 @node Input Section Wildcards
2262 @subsubsection Input section wildcard patterns
2263 @cindex input section wildcards
2264 @cindex wildcard file name patterns
2265 @cindex file name wildcard patterns
2266 @cindex section name wildcard patterns
2267 In an input section description, either the file name or the section
2268 name or both may be wildcard patterns.
2269
2270 The file name of @samp{*} seen in many examples is a simple wildcard
2271 pattern for the file name.
2272
2273 The wildcard patterns are like those used by the Unix shell.
2274
2275 @table @samp
2276 @item *
2277 matches any number of characters
2278 @item ?
2279 matches any single character
2280 @item [@var{chars}]
2281 matches a single instance of any of the @var{chars}; the @samp{-}
2282 character may be used to specify a range of characters, as in
2283 @samp{[a-z]} to match any lower case letter
2284 @item \
2285 quotes the following character
2286 @end table
2287
2288 When a file name is matched with a wildcard, the wildcard characters
2289 will not match a @samp{/} character (used to separate directory names on
2290 Unix). A pattern consisting of a single @samp{*} character is an
2291 exception; it will always match any file name, whether it contains a
2292 @samp{/} or not. In a section name, the wildcard characters will match
2293 a @samp{/} character.
2294
2295 File name wildcard patterns only match files which are explicitly
2296 specified on the command line or in an @code{INPUT} command. The linker
2297 does not search directories to expand wildcards.
2298
2299 If a file name matches more than one wildcard pattern, or if a file name
2300 appears explicitly and is also matched by a wildcard pattern, the linker
2301 will use the first match in the linker script. For example, this
2302 sequence of input section descriptions is probably in error, because the
2303 @file{data.o} rule will not be used:
2304 @smallexample
2305 .data : @{ *(.data) @}
2306 .data1 : @{ data.o(.data) @}
2307 @end smallexample
2308
2309 @cindex SORT
2310 Normally, the linker will place files and sections matched by wildcards
2311 in the order in which they are seen during the link. You can change
2312 this by using the @code{SORT} keyword, which appears before a wildcard
2313 pattern in parentheses (e.g., @code{SORT(.text*)}). When the
2314 @code{SORT} keyword is used, the linker will sort the files or sections
2315 into ascending order by name before placing them in the output file.
2316
2317 If you ever get confused about where input sections are going, use the
2318 @samp{-M} linker option to generate a map file. The map file shows
2319 precisely how input sections are mapped to output sections.
2320
2321 This example shows how wildcard patterns might be used to partition
2322 files. This linker script directs the linker to place all @samp{.text}
2323 sections in @samp{.text} and all @samp{.bss} sections in @samp{.bss}.
2324 The linker will place the @samp{.data} section from all files beginning
2325 with an upper case character in @samp{.DATA}; for all other files, the
2326 linker will place the @samp{.data} section in @samp{.data}.
2327 @smallexample
2328 @group
2329 SECTIONS @{
2330 .text : @{ *(.text) @}
2331 .DATA : @{ [A-Z]*(.data) @}
2332 .data : @{ *(.data) @}
2333 .bss : @{ *(.bss) @}
2334 @}
2335 @end group
2336 @end smallexample
2337
2338 @node Input Section Common
2339 @subsubsection Input section for common symbols
2340 @cindex common symbol placement
2341 @cindex uninitialized data placement
2342 A special notation is needed for common symbols, because in many object
2343 file formats common symbols do not have a particular input section. The
2344 linker treats common symbols as though they are in an input section
2345 named @samp{COMMON}.
2346
2347 You may use file names with the @samp{COMMON} section just as with any
2348 other input sections. You can use this to place common symbols from a
2349 particular input file in one section while common symbols from other
2350 input files are placed in another section.
2351
2352 In most cases, common symbols in input files will be placed in the
2353 @samp{.bss} section in the output file. For example:
2354 @smallexample
2355 .bss @{ *(.bss) *(COMMON) @}
2356 @end smallexample
2357
2358 @cindex scommon section
2359 @cindex small common symbols
2360 Some object file formats have more than one type of common symbol. For
2361 example, the MIPS ELF object file format distinguishes standard common
2362 symbols and small common symbols. In this case, the linker will use a
2363 different special section name for other types of common symbols. In
2364 the case of MIPS ELF, the linker uses @samp{COMMON} for standard common
2365 symbols and @samp{.scommon} for small common symbols. This permits you
2366 to map the different types of common symbols into memory at different
2367 locations.
2368
2369 @cindex [COMMON]
2370 You will sometimes see @samp{[COMMON]} in old linker scripts. This
2371 notation is now considered obsolete. It is equivalent to
2372 @samp{*(COMMON)}.
2373
2374 @node Input Section Keep
2375 @subsubsection Input section and garbage collection
2376 @cindex KEEP
2377 @cindex garbage collection
2378 When link-time garbage collection is in use (@samp{--gc-sections}),
2379 it is often useful to mark sections that should not be eliminated.
2380 This is accomplished by surrounding an input section's wildcard entry
2381 with @code{KEEP()}, as in @code{KEEP(*(.init))} or
2382 @code{KEEP(SORT(*)(.ctors))}.
2383
2384 @node Input Section Example
2385 @subsubsection Input section example
2386 The following example is a complete linker script. It tells the linker
2387 to read all of the sections from file @file{all.o} and place them at the
2388 start of output section @samp{outputa} which starts at location
2389 @samp{0x10000}. All of section @samp{.input1} from file @file{foo.o}
2390 follows immediately, in the same output section. All of section
2391 @samp{.input2} from @file{foo.o} goes into output section
2392 @samp{outputb}, followed by section @samp{.input1} from @file{foo1.o}.
2393 All of the remaining @samp{.input1} and @samp{.input2} sections from any
2394 files are written to output section @samp{outputc}.
2395
2396 @smallexample
2397 @group
2398 SECTIONS @{
2399 outputa 0x10000 :
2400 @{
2401 all.o
2402 foo.o (.input1)
2403 @}
2404 outputb :
2405 @{
2406 foo.o (.input2)
2407 foo1.o (.input1)
2408 @}
2409 outputc :
2410 @{
2411 *(.input1)
2412 *(.input2)
2413 @}
2414 @}
2415 @end group
2416 @end smallexample
2417
2418 @node Output Section Data
2419 @subsection Output section data
2420 @cindex data
2421 @cindex section data
2422 @cindex output section data
2423 @kindex BYTE(@var{expression})
2424 @kindex SHORT(@var{expression})
2425 @kindex LONG(@var{expression})
2426 @kindex QUAD(@var{expression})
2427 @kindex SQUAD(@var{expression})
2428 You can include explicit bytes of data in an output section by using
2429 @code{BYTE}, @code{SHORT}, @code{LONG}, @code{QUAD}, or @code{SQUAD} as
2430 an output section command. Each keyword is followed by an expression in
2431 parentheses providing the value to store (@pxref{Expressions}). The
2432 value of the expression is stored at the current value of the location
2433 counter.
2434
2435 The @code{BYTE}, @code{SHORT}, @code{LONG}, and @code{QUAD} commands
2436 store one, two, four, and eight bytes (respectively). After storing the
2437 bytes, the location counter is incremented by the number of bytes
2438 stored.
2439
2440 For example, this will store the byte 1 followed by the four byte value
2441 of the symbol @samp{addr}:
2442 @smallexample
2443 BYTE(1)
2444 LONG(addr)
2445 @end smallexample
2446
2447 When using a 64 bit host or target, @code{QUAD} and @code{SQUAD} are the
2448 same; they both store an 8 byte, or 64 bit, value. When both host and
2449 target are 32 bits, an expression is computed as 32 bits. In this case
2450 @code{QUAD} stores a 32 bit value zero extended to 64 bits, and
2451 @code{SQUAD} stores a 32 bit value sign extended to 64 bits.
2452
2453 If the object file format of the output file has an explicit endianness,
2454 which is the normal case, the value will be stored in that endianness.
2455 When the object file format does not have an explicit endianness, as is
2456 true of, for example, S-records, the value will be stored in the
2457 endianness of the first input object file.
2458
2459 @kindex FILL(@var{expression})
2460 @cindex holes, filling
2461 @cindex unspecified memory
2462 You may use the @code{FILL} command to set the fill pattern for the
2463 current section. It is followed by an expression in parentheses. Any
2464 otherwise unspecified regions of memory within the section (for example,
2465 gaps left due to the required alignment of input sections) are filled
2466 with the two least significant bytes of the expression, repeated as
2467 necessary. A @code{FILL} statement covers memory locations after the
2468 point at which it occurs in the section definition; by including more
2469 than one @code{FILL} statement, you can have different fill patterns in
2470 different parts of an output section.
2471
2472 This example shows how to fill unspecified regions of memory with the
2473 value @samp{0x9090}:
2474 @smallexample
2475 FILL(0x9090)
2476 @end smallexample
2477
2478 The @code{FILL} command is similar to the @samp{=@var{fillexp}} output
2479 section attribute (@pxref{Output Section Fill}), but it only affects the
2480 part of the section following the @code{FILL} command, rather than the
2481 entire section. If both are used, the @code{FILL} command takes
2482 precedence.
2483
2484 @node Output Section Keywords
2485 @subsection Output section keywords
2486 There are a couple of keywords which can appear as output section
2487 commands.
2488
2489 @table @code
2490 @kindex CREATE_OBJECT_SYMBOLS
2491 @cindex input filename symbols
2492 @cindex filename symbols
2493 @item CREATE_OBJECT_SYMBOLS
2494 The command tells the linker to create a symbol for each input file.
2495 The name of each symbol will be the name of the corresponding input
2496 file. The section of each symbol will be the output section in which
2497 the @code{CREATE_OBJECT_SYMBOLS} command appears.
2498
2499 This is conventional for the a.out object file format. It is not
2500 normally used for any other object file format.
2501
2502 @kindex CONSTRUCTORS
2503 @cindex C++ constructors, arranging in link
2504 @cindex constructors, arranging in link
2505 @item CONSTRUCTORS
2506 When linking using the a.out object file format, the linker uses an
2507 unusual set construct to support C++ global constructors and
2508 destructors. When linking object file formats which do not support
2509 arbitrary sections, such as ECOFF and XCOFF, the linker will
2510 automatically recognize C++ global constructors and destructors by name.
2511 For these object file formats, the @code{CONSTRUCTORS} command tells the
2512 linker to place constructor information in the output section where the
2513 @code{CONSTRUCTORS} command appears. The @code{CONSTRUCTORS} command is
2514 ignored for other object file formats.
2515
2516 The symbol @w{@code{__CTOR_LIST__}} marks the start of the global
2517 constructors, and the symbol @w{@code{__DTOR_LIST}} marks the end. The
2518 first word in the list is the number of entries, followed by the address
2519 of each constructor or destructor, followed by a zero word. The
2520 compiler must arrange to actually run the code. For these object file
2521 formats @sc{gnu} C++ normally calls constructors from a subroutine
2522 @code{__main}; a call to @code{__main} is automatically inserted into
2523 the startup code for @code{main}. @sc{gnu} C++ normally runs
2524 destructors either by using @code{atexit}, or directly from the function
2525 @code{exit}.
2526
2527 For object file formats such as @code{COFF} or @code{ELF} which support
2528 arbitrary section names, @sc{gnu} C++ will normally arrange to put the
2529 addresses of global constructors and destructors into the @code{.ctors}
2530 and @code{.dtors} sections. Placing the following sequence into your
2531 linker script will build the sort of table which the @sc{gnu} C++
2532 runtime code expects to see.
2533
2534 @smallexample
2535 __CTOR_LIST__ = .;
2536 LONG((__CTOR_END__ - __CTOR_LIST__) / 4 - 2)
2537 *(.ctors)
2538 LONG(0)
2539 __CTOR_END__ = .;
2540 __DTOR_LIST__ = .;
2541 LONG((__DTOR_END__ - __DTOR_LIST__) / 4 - 2)
2542 *(.dtors)
2543 LONG(0)
2544 __DTOR_END__ = .;
2545 @end smallexample
2546
2547 If you are using the @sc{gnu} C++ support for initialization priority,
2548 which provides some control over the order in which global constructors
2549 are run, you must sort the constructors at link time to ensure that they
2550 are executed in the correct order. When using the @code{CONSTRUCTORS}
2551 command, use @samp{SORT(CONSTRUCTORS)} instead. When using the
2552 @code{.ctors} and @code{.dtors} sections, use @samp{*(SORT(.ctors))} and
2553 @samp{*(SORT(.dtors))} instead of just @samp{*(.ctors)} and
2554 @samp{*(.dtors)}.
2555
2556 Normally the compiler and linker will handle these issues automatically,
2557 and you will not need to concern yourself with them. However, you may
2558 need to consider this if you are using C++ and writing your own linker
2559 scripts.
2560
2561 @end table
2562
2563 @node Output Section Discarding
2564 @subsection Output section discarding
2565 @cindex discarding sections
2566 @cindex sections, discarding
2567 @cindex removing sections
2568 The linker will not create output section which do not have any
2569 contents. This is for convenience when referring to input sections that
2570 may or may not be present in any of the input files. For example:
2571 @smallexample
2572 .foo @{ *(.foo) @}
2573 @end smallexample
2574 @noindent
2575 will only create a @samp{.foo} section in the output file if there is a
2576 @samp{.foo} section in at least one input file.
2577
2578 If you use anything other than an input section description as an output
2579 section command, such as a symbol assignment, then the output section
2580 will always be created, even if there are no matching input sections.
2581
2582 @cindex /DISCARD/
2583 The special output section name @samp{/DISCARD/} may be used to discard
2584 input sections. Any input sections which are assigned to an output
2585 section named @samp{/DISCARD/} are not included in the output file.
2586
2587 @node Output Section Attributes
2588 @subsection Output section attributes
2589 @cindex output section attributes
2590 We showed above that the full description of an output section looked
2591 like this:
2592 @smallexample
2593 @group
2594 @var{section} [@var{address}] [(@var{type})] : [AT(@var{lma})]
2595 @{
2596 @var{output-section-command}
2597 @var{output-section-command}
2598 @dots{}
2599 @} [>@var{region}] [:@var{phdr} :@var{phdr} @dots{}] [=@var{fillexp}]
2600 @end group
2601 @end smallexample
2602 We've already described @var{section}, @var{address}, and
2603 @var{output-section-command}. In this section we will describe the
2604 remaining section attributes.
2605
2606 @menu
2607 * Output Section Type:: Output section type
2608 * Output Section LMA:: Output section LMA
2609 * Output Section Region:: Output section region
2610 * Output Section Phdr:: Output section phdr
2611 * Output Section Fill:: Output section fill
2612 @end menu
2613
2614 @node Output Section Type
2615 @subsubsection Output section type
2616 Each output section may have a type. The type is a keyword in
2617 parentheses. The following types are defined:
2618
2619 @table @code
2620 @item NOLOAD
2621 The section should be marked as not loadable, so that it will not be
2622 loaded into memory when the program is run.
2623 @item DSECT
2624 @itemx COPY
2625 @itemx INFO
2626 @itemx OVERLAY
2627 These type names are supported for backward compatibility, and are
2628 rarely used. They all have the same effect: the section should be
2629 marked as not allocatable, so that no memory is allocated for the
2630 section when the program is run.
2631 @end table
2632
2633 @kindex NOLOAD
2634 @cindex prevent unnecessary loading
2635 @cindex loading, preventing
2636 The linker normally sets the attributes of an output section based on
2637 the input sections which map into it. You can override this by using
2638 the section type. For example, in the script sample below, the
2639 @samp{ROM} section is addressed at memory location @samp{0} and does not
2640 need to be loaded when the program is run. The contents of the
2641 @samp{ROM} section will appear in the linker output file as usual.
2642 @smallexample
2643 @group
2644 SECTIONS @{
2645 ROM 0 (NOLOAD) : @{ @dots{} @}
2646 @dots{}
2647 @}
2648 @end group
2649 @end smallexample
2650
2651 @node Output Section LMA
2652 @subsubsection Output section LMA
2653 @kindex AT(@var{lma})
2654 @cindex load address
2655 @cindex section load address
2656 Every section has a virtual address (VMA) and a load address (LMA); see
2657 @ref{Basic Script Concepts}. The address expression which may appear in
2658 an output section description sets the VMA (@pxref{Output Section
2659 Address}).
2660
2661 The linker will normally set the LMA equal to the VMA. You can change
2662 that by using the @code{AT} keyword. The expression @var{lma} that
2663 follows the @code{AT} keyword specifies the load address of the section.
2664
2665 @cindex ROM initialized data
2666 @cindex initialized data in ROM
2667 This feature is designed to make it easy to build a ROM image. For
2668 example, the following linker script creates three output sections: one
2669 called @samp{.text}, which starts at @code{0x1000}, one called
2670 @samp{.mdata}, which is loaded at the end of the @samp{.text} section
2671 even though its VMA is @code{0x2000}, and one called @samp{.bss} to hold
2672 uninitialized data at address @code{0x3000}. The symbol @code{_data} is
2673 defined with the value @code{0x2000}, which shows that the location
2674 counter holds the VMA value, not the LMA value.
2675
2676 @smallexample
2677 @group
2678 SECTIONS
2679 @{
2680 .text 0x1000 : @{ *(.text) _etext = . ; @}
2681 .mdata 0x2000 :
2682 AT ( ADDR (.text) + SIZEOF (.text) )
2683 @{ _data = . ; *(.data); _edata = . ; @}
2684 .bss 0x3000 :
2685 @{ _bstart = . ; *(.bss) *(COMMON) ; _bend = . ;@}
2686 @}
2687 @end group
2688 @end smallexample
2689
2690 The run-time initialization code for use with a program generated with
2691 this linker script would include something like the following, to copy
2692 the initialized data from the ROM image to its runtime address. Notice
2693 how this code takes advantage of the symbols defined by the linker
2694 script.
2695
2696 @smallexample
2697 @group
2698 extern char _etext, _data, _edata, _bstart, _bend;
2699 char *src = &_etext;
2700 char *dst = &_data;
2701
2702 /* ROM has data at end of text; copy it. */
2703 while (dst < &_edata) @{
2704 *dst++ = *src++;
2705 @}
2706
2707 /* Zero bss */
2708 for (dst = &_bstart; dst< &_bend; dst++)
2709 *dst = 0;
2710 @end group
2711 @end smallexample
2712
2713 @node Output Section Region
2714 @subsubsection Output section region
2715 @kindex >@var{region}
2716 @cindex section, assigning to memory region
2717 @cindex memory regions and sections
2718 You can assign a section to a previously defined region of memory by
2719 using @samp{>@var{region}}. @xref{MEMORY}.
2720
2721 Here is a simple example:
2722 @smallexample
2723 @group
2724 MEMORY @{ rom : ORIGIN = 0x1000, LENGTH = 0x1000 @}
2725 SECTIONS @{ ROM : @{ *(.text) @} >rom @}
2726 @end group
2727 @end smallexample
2728
2729 @node Output Section Phdr
2730 @subsubsection Output section phdr
2731 @kindex :@var{phdr}
2732 @cindex section, assigning to program header
2733 @cindex program headers and sections
2734 You can assign a section to a previously defined program segment by
2735 using @samp{:@var{phdr}}. @xref{PHDRS}. If a section is assigned to
2736 one or more segments, then all subsequent allocated sections will be
2737 assigned to those segments as well, unless they use an explicitly
2738 @code{:@var{phdr}} modifier. You can use @code{:NONE} to tell the
2739 linker to not put the section in any segment at all.
2740
2741 Here is a simple example:
2742 @smallexample
2743 @group
2744 PHDRS @{ text PT_LOAD ; @}
2745 SECTIONS @{ .text : @{ *(.text) @} :text @}
2746 @end group
2747 @end smallexample
2748
2749 @node Output Section Fill
2750 @subsubsection Output section fill
2751 @kindex =@var{fillexp}
2752 @cindex section fill pattern
2753 @cindex fill pattern, entire section
2754 You can set the fill pattern for an entire section by using
2755 @samp{=@var{fillexp}}. @var{fillexp} is an expression
2756 (@pxref{Expressions}). Any otherwise unspecified regions of memory
2757 within the output section (for example, gaps left due to the required
2758 alignment of input sections) will be filled with the two least
2759 significant bytes of the value, repeated as necessary.
2760
2761 You can also change the fill value with a @code{FILL} command in the
2762 output section commands; see @ref{Output Section Data}.
2763
2764 Here is a simple example:
2765 @smallexample
2766 @group
2767 SECTIONS @{ .text : @{ *(.text) @} =0x9090 @}
2768 @end group
2769 @end smallexample
2770
2771 @node Overlay Description
2772 @subsection Overlay description
2773 @kindex OVERLAY
2774 @cindex overlays
2775 An overlay description provides an easy way to describe sections which
2776 are to be loaded as part of a single memory image but are to be run at
2777 the same memory address. At run time, some sort of overlay manager will
2778 copy the overlaid sections in and out of the runtime memory address as
2779 required, perhaps by simply manipulating addressing bits. This approach
2780 can be useful, for example, when a certain region of memory is faster
2781 than another.
2782
2783 Overlays are described using the @code{OVERLAY} command. The
2784 @code{OVERLAY} command is used within a @code{SECTIONS} command, like an
2785 output section description. The full syntax of the @code{OVERLAY}
2786 command is as follows:
2787 @smallexample
2788 @group
2789 OVERLAY [@var{start}] : [NOCROSSREFS] [AT ( @var{ldaddr} )]
2790 @{
2791 @var{secname1}
2792 @{
2793 @var{output-section-command}
2794 @var{output-section-command}
2795 @dots{}
2796 @} [:@var{phdr}@dots{}] [=@var{fill}]
2797 @var{secname2}
2798 @{
2799 @var{output-section-command}
2800 @var{output-section-command}
2801 @dots{}
2802 @} [:@var{phdr}@dots{}] [=@var{fill}]
2803 @dots{}
2804 @} [>@var{region}] [:@var{phdr}@dots{}] [=@var{fill}]
2805 @end group
2806 @end smallexample
2807
2808 Everything is optional except @code{OVERLAY} (a keyword), and each
2809 section must have a name (@var{secname1} and @var{secname2} above). The
2810 section definitions within the @code{OVERLAY} construct are identical to
2811 those within the general @code{SECTIONS} contruct (@pxref{SECTIONS}),
2812 except that no addresses and no memory regions may be defined for
2813 sections within an @code{OVERLAY}.
2814
2815 The sections are all defined with the same starting address. The load
2816 addresses of the sections are arranged such that they are consecutive in
2817 memory starting at the load address used for the @code{OVERLAY} as a
2818 whole (as with normal section definitions, the load address is optional,
2819 and defaults to the start address; the start address is also optional,
2820 and defaults to the current value of the location counter).
2821
2822 If the @code{NOCROSSREFS} keyword is used, and there any references
2823 among the sections, the linker will report an error. Since the sections
2824 all run at the same address, it normally does not make sense for one
2825 section to refer directly to another. @xref{Miscellaneous Commands,
2826 NOCROSSREFS}.
2827
2828 For each section within the @code{OVERLAY}, the linker automatically
2829 defines two symbols. The symbol @code{__load_start_@var{secname}} is
2830 defined as the starting load address of the section. The symbol
2831 @code{__load_stop_@var{secname}} is defined as the final load address of
2832 the section. Any characters within @var{secname} which are not legal
2833 within C identifiers are removed. C (or assembler) code may use these
2834 symbols to move the overlaid sections around as necessary.
2835
2836 At the end of the overlay, the value of the location counter is set to
2837 the start address of the overlay plus the size of the largest section.
2838
2839 Here is an example. Remember that this would appear inside a
2840 @code{SECTIONS} construct.
2841 @smallexample
2842 @group
2843 OVERLAY 0x1000 : AT (0x4000)
2844 @{
2845 .text0 @{ o1/*.o(.text) @}
2846 .text1 @{ o2/*.o(.text) @}
2847 @}
2848 @end group
2849 @end smallexample
2850 @noindent
2851 This will define both @samp{.text0} and @samp{.text1} to start at
2852 address 0x1000. @samp{.text0} will be loaded at address 0x4000, and
2853 @samp{.text1} will be loaded immediately after @samp{.text0}. The
2854 following symbols will be defined: @code{__load_start_text0},
2855 @code{__load_stop_text0}, @code{__load_start_text1},
2856 @code{__load_stop_text1}.
2857
2858 C code to copy overlay @code{.text1} into the overlay area might look
2859 like the following.
2860
2861 @smallexample
2862 @group
2863 extern char __load_start_text1, __load_stop_text1;
2864 memcpy ((char *) 0x1000, &__load_start_text1,
2865 &__load_stop_text1 - &__load_start_text1);
2866 @end group
2867 @end smallexample
2868
2869 Note that the @code{OVERLAY} command is just syntactic sugar, since
2870 everything it does can be done using the more basic commands. The above
2871 example could have been written identically as follows.
2872
2873 @smallexample
2874 @group
2875 .text0 0x1000 : AT (0x4000) @{ o1/*.o(.text) @}
2876 __load_start_text0 = LOADADDR (.text0);
2877 __load_stop_text0 = LOADADDR (.text0) + SIZEOF (.text0);
2878 .text1 0x1000 : AT (0x4000 + SIZEOF (.text0)) @{ o2/*.o(.text) @}
2879 __load_start_text1 = LOADADDR (.text1);
2880 __load_stop_text1 = LOADADDR (.text1) + SIZEOF (.text1);
2881 . = 0x1000 + MAX (SIZEOF (.text0), SIZEOF (.text1));
2882 @end group
2883 @end smallexample
2884
2885 @node MEMORY
2886 @section MEMORY command
2887 @kindex MEMORY
2888 @cindex memory regions
2889 @cindex regions of memory
2890 @cindex allocating memory
2891 @cindex discontinuous memory
2892 The linker's default configuration permits allocation of all available
2893 memory. You can override this by using the @code{MEMORY} command.
2894
2895 The @code{MEMORY} command describes the location and size of blocks of
2896 memory in the target. You can use it to describe which memory regions
2897 may be used by the linker, and which memory regions it must avoid. You
2898 can then assign sections to particular memory regions. The linker will
2899 set section addresses based on the memory regions, and will warn about
2900 regions that become too full. The linker will not shuffle sections
2901 around to fit into the available regions.
2902
2903 A linker script may contain at most one use of the @code{MEMORY}
2904 command. However, you can define as many blocks of memory within it as
2905 you wish. The syntax is:
2906 @smallexample
2907 @group
2908 MEMORY
2909 @{
2910 @var{name} [(@var{attr})] : ORIGIN = @var{origin}, LENGTH = @var{len}
2911 @dots{}
2912 @}
2913 @end group
2914 @end smallexample
2915
2916 The @var{name} is a name used in the linker script to refer to the
2917 region. The region name has no meaning outside of the linker script.
2918 Region names are stored in a separate name space, and will not conflict
2919 with symbol names, file names, or section names. Each memory region
2920 must have a distinct name.
2921
2922 @cindex memory region attributes
2923 The @var{attr} string is an optional list of attributes that specify
2924 whether to use a particular memory region for an input section which is
2925 not explicitly mapped in the linker script. As described in
2926 @ref{SECTIONS}, if you do not specify an output section for some input
2927 section, the linker will create an output section with the same name as
2928 the input section. If you define region attributes, the linker will use
2929 them to select the memory region for the output section that it creates.
2930
2931 The @var{attr} string must consist only of the following characters:
2932 @table @samp
2933 @item R
2934 Read-only section
2935 @item W
2936 Read/write section
2937 @item X
2938 Executable section
2939 @item A
2940 Allocatable section
2941 @item I
2942 Initialized section
2943 @item L
2944 Same as @samp{I}
2945 @item !
2946 Invert the sense of any of the preceding attributes
2947 @end table
2948
2949 If a unmapped section matches any of the listed attributes other than
2950 @samp{!}, it will be placed in the memory region. The @samp{!}
2951 attribute reverses this test, so that an unmapped section will be placed
2952 in the memory region only if it does not match any of the listed
2953 attributes.
2954
2955 @kindex ORIGIN =
2956 @kindex o =
2957 @kindex org =
2958 The @var{origin} is an expression for the start address of the memory
2959 region. The expression must evaluate to a constant before memory
2960 allocation is performed, which means that you may not use any section
2961 relative symbols. The keyword @code{ORIGIN} may be abbreviated to
2962 @code{org} or @code{o} (but not, for example, @code{ORG}).
2963
2964 @kindex LENGTH =
2965 @kindex len =
2966 @kindex l =
2967 The @var{len} is an expression for the size in bytes of the memory
2968 region. As with the @var{origin} expression, the expression must
2969 evaluate to a constant before memory allocation is performed. The
2970 keyword @code{LENGTH} may be abbreviated to @code{len} or @code{l}.
2971
2972 In the following example, we specify that there are two memory regions
2973 available for allocation: one starting at @samp{0} for 256 kilobytes,
2974 and the other starting at @samp{0x40000000} for four megabytes. The
2975 linker will place into the @samp{rom} memory region every section which
2976 is not explicitly mapped into a memory region, and is either read-only
2977 or executable. The linker will place other sections which are not
2978 explicitly mapped into a memory region into the @samp{ram} memory
2979 region.
2980
2981 @smallexample
2982 @group
2983 MEMORY
2984 @{
2985 rom (rx) : ORIGIN = 0, LENGTH = 256K
2986 ram (!rx) : org = 0x40000000, l = 4M
2987 @}
2988 @end group
2989 @end smallexample
2990
2991 Once you define a memory region, you can direct the linker to place
2992 specific output sections into that memory region by using the
2993 @samp{>@var{region}} output section attribute. For example, if you have
2994 a memory region named @samp{mem}, you would use @samp{>mem} in the
2995 output section definition. @xref{Output Section Region}. If no address
2996 was specified for the output section, the linker will set the address to
2997 the next available address within the memory region. If the combined
2998 output sections directed to a memory region are too large for the
2999 region, the linker will issue an error message.
3000
3001 @node PHDRS
3002 @section PHDRS Command
3003 @kindex PHDRS
3004 @cindex program headers
3005 @cindex ELF program headers
3006 @cindex program segments
3007 @cindex segments, ELF
3008 The ELF object file format uses @dfn{program headers}, also knows as
3009 @dfn{segments}. The program headers describe how the program should be
3010 loaded into memory. You can print them out by using the @code{objdump}
3011 program with the @samp{-p} option.
3012
3013 When you run an ELF program on a native ELF system, the system loader
3014 reads the program headers in order to figure out how to load the
3015 program. This will only work if the program headers are set correctly.
3016 This manual does not describe the details of how the system loader
3017 interprets program headers; for more information, see the ELF ABI.
3018
3019 The linker will create reasonable program headers by default. However,
3020 in some cases, you may need to specify the program headers more
3021 precisely. You may use the @code{PHDRS} command for this purpose. When
3022 the linker sees the @code{PHDRS} command in the linker script, it will
3023 not create any program headers other than the ones specified.
3024
3025 The linker only pays attention to the @code{PHDRS} command when
3026 generating an ELF output file. In other cases, the linker will simply
3027 ignore @code{PHDRS}.
3028
3029 This is the syntax of the @code{PHDRS} command. The words @code{PHDRS},
3030 @code{FILEHDR}, @code{AT}, and @code{FLAGS} are keywords.
3031
3032 @smallexample
3033 @group
3034 PHDRS
3035 @{
3036 @var{name} @var{type} [ FILEHDR ] [ PHDRS ] [ AT ( @var{address} ) ]
3037 [ FLAGS ( @var{flags} ) ] ;
3038 @}
3039 @end group
3040 @end smallexample
3041
3042 The @var{name} is used only for reference in the @code{SECTIONS} command
3043 of the linker script. It is not put into the output file. Program
3044 header names are stored in a separate name space, and will not conflict
3045 with symbol names, file names, or section names. Each program header
3046 must have a distinct name.
3047
3048 Certain program header types describe segments of memory which the
3049 system loader will load from the file. In the linker script, you
3050 specify the contents of these segments by placing allocatable output
3051 sections in the segments. You use the @samp{:@var{phdr}} output section
3052 attribute to place a section in a particular segment. @xref{Output
3053 Section Phdr}.
3054
3055 It is normal to put certain sections in more than one segment. This
3056 merely implies that one segment of memory contains another. You may
3057 repeat @samp{:@var{phdr}}, using it once for each segment which should
3058 contain the section.
3059
3060 If you place a section in one or more segments using @samp{:@var{phdr}},
3061 then the linker will place all subsequent allocatable sections which do
3062 not specify @samp{:@var{phdr}} in the same segments. This is for
3063 convenience, since generally a whole set of contiguous sections will be
3064 placed in a single segment. You can use @code{:NONE} to override the
3065 default segment and tell the linker to not put the section in any
3066 segment at all.
3067
3068 @kindex FILEHDR
3069 @kindex PHDRS
3070 You may use the @code{FILEHDR} and @code{PHDRS} keywords appear after
3071 the program header type to further describe the contents of the segment.
3072 The @code{FILEHDR} keyword means that the segment should include the ELF
3073 file header. The @code{PHDRS} keyword means that the segment should
3074 include the ELF program headers themselves.
3075
3076 The @var{type} may be one of the following. The numbers indicate the
3077 value of the keyword.
3078
3079 @table @asis
3080 @item @code{PT_NULL} (0)
3081 Indicates an unused program header.
3082
3083 @item @code{PT_LOAD} (1)
3084 Indicates that this program header describes a segment to be loaded from
3085 the file.
3086
3087 @item @code{PT_DYNAMIC} (2)
3088 Indicates a segment where dynamic linking information can be found.
3089
3090 @item @code{PT_INTERP} (3)
3091 Indicates a segment where the name of the program interpreter may be
3092 found.
3093
3094 @item @code{PT_NOTE} (4)
3095 Indicates a segment holding note information.
3096
3097 @item @code{PT_SHLIB} (5)
3098 A reserved program header type, defined but not specified by the ELF
3099 ABI.
3100
3101 @item @code{PT_PHDR} (6)
3102 Indicates a segment where the program headers may be found.
3103
3104 @item @var{expression}
3105 An expression giving the numeric type of the program header. This may
3106 be used for types not defined above.
3107 @end table
3108
3109 You can specify that a segment should be loaded at a particular address
3110 in memory by using an @code{AT} expression. This is identical to the
3111 @code{AT} command used as an output section attribute (@pxref{Output
3112 Section LMA}). The @code{AT} command for a program header overrides the
3113 output section attribute.
3114
3115 The linker will normally set the segment flags based on the sections
3116 which comprise the segment. You may use the @code{FLAGS} keyword to
3117 explicitly specify the segment flags. The value of @var{flags} must be
3118 an integer. It is used to set the @code{p_flags} field of the program
3119 header.
3120
3121 Here is an example of @code{PHDRS}. This shows a typical set of program
3122 headers used on a native ELF system.
3123
3124 @example
3125 @group
3126 PHDRS
3127 @{
3128 headers PT_PHDR PHDRS ;
3129 interp PT_INTERP ;
3130 text PT_LOAD FILEHDR PHDRS ;
3131 data PT_LOAD ;
3132 dynamic PT_DYNAMIC ;
3133 @}
3134
3135 SECTIONS
3136 @{
3137 . = SIZEOF_HEADERS;
3138 .interp : @{ *(.interp) @} :text :interp
3139 .text : @{ *(.text) @} :text
3140 .rodata : @{ *(.rodata) @} /* defaults to :text */
3141 @dots{}
3142 . = . + 0x1000; /* move to a new page in memory */
3143 .data : @{ *(.data) @} :data
3144 .dynamic : @{ *(.dynamic) @} :data :dynamic
3145 @dots{}
3146 @}
3147 @end group
3148 @end example
3149
3150 @node VERSION
3151 @section VERSION Command
3152 @kindex VERSION @{script text@}
3153 @cindex symbol versions
3154 @cindex version script
3155 @cindex versions of symbols
3156 The linker supports symbol versions when using ELF. Symbol versions are
3157 only useful when using shared libraries. The dynamic linker can use
3158 symbol versions to select a specific version of a function when it runs
3159 a program that may have been linked against an earlier version of the
3160 shared library.
3161
3162 You can include a version script directly in the main linker script, or
3163 you can supply the version script as an implicit linker script. You can
3164 also use the @samp{--version-script} linker option.
3165
3166 The syntax of the @code{VERSION} command is simply
3167 @smallexample
3168 VERSION @{ version-script-commands @}
3169 @end smallexample
3170
3171 The format of the version script commands is identical to that used by
3172 Sun's linker in Solaris 2.5. The version script defines a tree of
3173 version nodes. You specify the node names and interdependencies in the
3174 version script. You can specify which symbols are bound to which
3175 version nodes, and you can reduce a specified set of symbols to local
3176 scope so that they are not globally visible outside of the shared
3177 library.
3178
3179 The easiest way to demonstrate the version script language is with a few
3180 examples.
3181
3182 @smallexample
3183 VERS_1.1 @{
3184 global:
3185 foo1;
3186 local:
3187 old*;
3188 original*;
3189 new*;
3190 @};
3191
3192 VERS_1.2 @{
3193 foo2;
3194 @} VERS_1.1;
3195
3196 VERS_2.0 @{
3197 bar1; bar2;
3198 @} VERS_1.2;
3199 @end smallexample
3200
3201 This example version script defines three version nodes. The first
3202 version node defined is @samp{VERS_1.1}; it has no other dependencies.
3203 The script binds the symbol @samp{foo1} to @samp{VERS_1.1}. It reduces
3204 a number of symbols to local scope so that they are not visible outside
3205 of the shared library.
3206
3207 Next, the version script defines node @samp{VERS_1.2}. This node
3208 depends upon @samp{VERS_1.1}. The script binds the symbol @samp{foo2}
3209 to the version node @samp{VERS_1.2}.
3210
3211 Finally, the version script defines node @samp{VERS_2.0}. This node
3212 depends upon @samp{VERS_1.2}. The scripts binds the symbols @samp{bar1}
3213 and @samp{bar2} are bound to the version node @samp{VERS_2.0}.
3214
3215 When the linker finds a symbol defined in a library which is not
3216 specifically bound to a version node, it will effectively bind it to an
3217 unspecified base version of the library. You can bind all otherwise
3218 unspecified symbols to a given version node by using @samp{global: *}
3219 somewhere in the version script.
3220
3221 The names of the version nodes have no specific meaning other than what
3222 they might suggest to the person reading them. The @samp{2.0} version
3223 could just as well have appeared in between @samp{1.1} and @samp{1.2}.
3224 However, this would be a confusing way to write a version script.
3225
3226 When you link an application against a shared library that has versioned
3227 symbols, the application itself knows which version of each symbol it
3228 requires, and it also knows which version nodes it needs from each
3229 shared library it is linked against. Thus at runtime, the dynamic
3230 loader can make a quick check to make sure that the libraries you have
3231 linked against do in fact supply all of the version nodes that the
3232 application will need to resolve all of the dynamic symbols. In this
3233 way it is possible for the dynamic linker to know with certainty that
3234 all external symbols that it needs will be resolvable without having to
3235 search for each symbol reference.
3236
3237 The symbol versioning is in effect a much more sophisticated way of
3238 doing minor version checking that SunOS does. The fundamental problem
3239 that is being addressed here is that typically references to external
3240 functions are bound on an as-needed basis, and are not all bound when
3241 the application starts up. If a shared library is out of date, a
3242 required interface may be missing; when the application tries to use
3243 that interface, it may suddenly and unexpectedly fail. With symbol
3244 versioning, the user will get a warning when they start their program if
3245 the libraries being used with the application are too old.
3246
3247 There are several GNU extensions to Sun's versioning approach. The
3248 first of these is the ability to bind a symbol to a version node in the
3249 source file where the symbol is defined instead of in the versioning
3250 script. This was done mainly to reduce the burden on the library
3251 maintainer. You can do this by putting something like:
3252 @smallexample
3253 __asm__(".symver original_foo,foo@@VERS_1.1");
3254 @end smallexample
3255 @noindent
3256 in the C source file. This renames the function @samp{original_foo} to
3257 be an alias for @samp{foo} bound to the version node @samp{VERS_1.1}.
3258 The @samp{local:} directive can be used to prevent the symbol
3259 @samp{original_foo} from being exported.
3260
3261 The second GNU extension is to allow multiple versions of the same
3262 function to appear in a given shared library. In this way you can make
3263 an incompatible change to an interface without increasing the major
3264 version number of the shared library, while still allowing applications
3265 linked against the old interface to continue to function.
3266
3267 To do this, you must use multiple @samp{.symver} directives in the
3268 source file. Here is an example:
3269
3270 @smallexample
3271 __asm__(".symver original_foo,foo@@");
3272 __asm__(".symver old_foo,foo@@VERS_1.1");
3273 __asm__(".symver old_foo1,foo@@VERS_1.2");
3274 __asm__(".symver new_foo,foo@@@@VERS_2.0");
3275 @end smallexample
3276
3277 In this example, @samp{foo@@} represents the symbol @samp{foo} bound to the
3278 unspecified base version of the symbol. The source file that contains this
3279 example would define 4 C functions: @samp{original_foo}, @samp{old_foo},
3280 @samp{old_foo1}, and @samp{new_foo}.
3281
3282 When you have multiple definitions of a given symbol, there needs to be
3283 some way to specify a default version to which external references to
3284 this symbol will be bound. You can do this with the
3285 @samp{foo@@@@VERS_2.0} type of @samp{.symver} directive. You can only
3286 declare one version of a symbol as the default in this manner; otherwise
3287 you would effectively have multiple definitions of the same symbol.
3288
3289 If you wish to bind a reference to a specific version of the symbol
3290 within the shared library, you can use the aliases of convenience
3291 (i.e. @samp{old_foo}), or you can use the @samp{.symver} directive to
3292 specifically bind to an external version of the function in question.
3293
3294 @node Expressions
3295 @section Expressions in Linker Scripts
3296 @cindex expressions
3297 @cindex arithmetic
3298 The syntax for expressions in the linker script language is identical to
3299 that of C expressions. All expressions are evaluated as integers. All
3300 expressions are evaluated in the same size, which is 32 bits if both the
3301 host and target are 32 bits, and is otherwise 64 bits.
3302
3303 You can use and set symbol values in expressions.
3304
3305 The linker defines several special purpose builtin functions for use in
3306 expressions.
3307
3308 @menu
3309 * Constants:: Constants
3310 * Symbols:: Symbol Names
3311 * Location Counter:: The Location Counter
3312 * Operators:: Operators
3313 * Evaluation:: Evaluation
3314 * Expression Section:: The Section of an Expression
3315 * Builtin Functions:: Builtin Functions
3316 @end menu
3317
3318 @node Constants
3319 @subsection Constants
3320 @cindex integer notation
3321 @cindex constants in linker scripts
3322 All constants are integers.
3323
3324 As in C, the linker considers an integer beginning with @samp{0} to be
3325 octal, and an integer beginning with @samp{0x} or @samp{0X} to be
3326 hexadecimal. The linker considers other integers to be decimal.
3327
3328 @cindex scaled integers
3329 @cindex K and M integer suffixes
3330 @cindex M and K integer suffixes
3331 @cindex suffixes for integers
3332 @cindex integer suffixes
3333 In addition, you can use the suffixes @code{K} and @code{M} to scale a
3334 constant by
3335 @c TEXI2ROFF-KILL
3336 @ifinfo
3337 @c END TEXI2ROFF-KILL
3338 @code{1024} or @code{1024*1024}
3339 @c TEXI2ROFF-KILL
3340 @end ifinfo
3341 @tex
3342 ${\rm 1024}$ or ${\rm 1024}^2$
3343 @end tex
3344 @c END TEXI2ROFF-KILL
3345 respectively. For example, the following all refer to the same quantity:
3346 @smallexample
3347 _fourk_1 = 4K;
3348 _fourk_2 = 4096;
3349 _fourk_3 = 0x1000;
3350 @end smallexample
3351
3352 @node Symbols
3353 @subsection Symbol Names
3354 @cindex symbol names
3355 @cindex names
3356 @cindex quoted symbol names
3357 @kindex "
3358 Unless quoted, symbol names start with a letter, underscore, or period
3359 and may include letters, digits, underscores, periods, and hyphens.
3360 Unquoted symbol names must not conflict with any keywords. You can
3361 specify a symbol which contains odd characters or has the same name as a
3362 keyword by surrounding the symbol name in double quotes:
3363 @smallexample
3364 "SECTION" = 9;
3365 "with a space" = "also with a space" + 10;
3366 @end smallexample
3367
3368 Since symbols can contain many non-alphabetic characters, it is safest
3369 to delimit symbols with spaces. For example, @samp{A-B} is one symbol,
3370 whereas @samp{A - B} is an expression involving subtraction.
3371
3372 @node Location Counter
3373 @subsection The Location Counter
3374 @kindex .
3375 @cindex dot
3376 @cindex location counter
3377 @cindex current output location
3378 The special linker variable @dfn{dot} @samp{.} always contains the
3379 current output location counter. Since the @code{.} always refers to a
3380 location in an output section, it may only appear in an expression
3381 within a @code{SECTIONS} command. The @code{.} symbol may appear
3382 anywhere that an ordinary symbol is allowed in an expression.
3383
3384 @cindex holes
3385 Assigning a value to @code{.} will cause the location counter to be
3386 moved. This may be used to create holes in the output section. The
3387 location counter may never be moved backwards.
3388
3389 @smallexample
3390 SECTIONS
3391 @{
3392 output :
3393 @{
3394 file1(.text)
3395 . = . + 1000;
3396 file2(.text)
3397 . += 1000;
3398 file3(.text)
3399 @} = 0x1234;
3400 @}
3401 @end smallexample
3402 @noindent
3403 In the previous example, the @samp{.text} section from @file{file1} is
3404 located at the beginning of the output section @samp{output}. It is
3405 followed by a 1000 byte gap. Then the @samp{.text} section from
3406 @file{file2} appears, also with a 1000 byte gap following before the
3407 @samp{.text} section from @file{file3}. The notation @samp{= 0x1234}
3408 specifies what data to write in the gaps (@pxref{Output Section Fill}).
3409
3410 @need 2000
3411 @node Operators
3412 @subsection Operators
3413 @cindex operators for arithmetic
3414 @cindex arithmetic operators
3415 @cindex precedence in expressions
3416 The linker recognizes the standard C set of arithmetic operators, with
3417 the standard bindings and precedence levels:
3418 @c TEXI2ROFF-KILL
3419 @ifinfo
3420 @c END TEXI2ROFF-KILL
3421 @smallexample
3422 precedence associativity Operators Notes
3423 (highest)
3424 1 left ! - ~ (1)
3425 2 left * / %
3426 3 left + -
3427 4 left >> <<
3428 5 left == != > < <= >=
3429 6 left &
3430 7 left |
3431 8 left &&
3432 9 left ||
3433 10 right ? :
3434 11 right &= += -= *= /= (2)
3435 (lowest)
3436 @end smallexample
3437 Notes:
3438 (1) Prefix operators
3439 (2) @xref{Assignments}.
3440 @c TEXI2ROFF-KILL
3441 @end ifinfo
3442 @tex
3443 \vskip \baselineskip
3444 %"lispnarrowing" is the extra indent used generally for smallexample
3445 \hskip\lispnarrowing\vbox{\offinterlineskip
3446 \hrule
3447 \halign
3448 {\vrule#&\strut\hfil\ #\ \hfil&\vrule#&\strut\hfil\ #\ \hfil&\vrule#&\strut\hfil\ {\tt #}\ \hfil&\vrule#\cr
3449 height2pt&\omit&&\omit&&\omit&\cr
3450 &Precedence&& Associativity &&{\rm Operators}&\cr
3451 height2pt&\omit&&\omit&&\omit&\cr
3452 \noalign{\hrule}
3453 height2pt&\omit&&\omit&&\omit&\cr
3454 &highest&&&&&\cr
3455 % '176 is tilde, '~' in tt font
3456 &1&&left&&\qquad- \char'176\ !\qquad\dag&\cr
3457 &2&&left&&* / \%&\cr
3458 &3&&left&&+ -&\cr
3459 &4&&left&&>> <<&\cr
3460 &5&&left&&== != > < <= >=&\cr
3461 &6&&left&&\&&\cr
3462 &7&&left&&|&\cr
3463 &8&&left&&{\&\&}&\cr
3464 &9&&left&&||&\cr
3465 &10&&right&&? :&\cr
3466 &11&&right&&\qquad\&= += -= *= /=\qquad\ddag&\cr
3467 &lowest&&&&&\cr
3468 height2pt&\omit&&\omit&&\omit&\cr}
3469 \hrule}
3470 @end tex
3471 @iftex
3472 {
3473 @obeylines@parskip=0pt@parindent=0pt
3474 @dag@quad Prefix operators.
3475 @ddag@quad @xref{Assignments}.
3476 }
3477 @end iftex
3478 @c END TEXI2ROFF-KILL
3479
3480 @node Evaluation
3481 @subsection Evaluation
3482 @cindex lazy evaluation
3483 @cindex expression evaluation order
3484 The linker evaluates expressions lazily. It only computes the value of
3485 an expression when absolutely necessary.
3486
3487 The linker needs some information, such as the value of the start
3488 address of the first section, and the origins and lengths of memory
3489 regions, in order to do any linking at all. These values are computed
3490 as soon as possible when the linker reads in the linker script.
3491
3492 However, other values (such as symbol values) are not known or needed
3493 until after storage allocation. Such values are evaluated later, when
3494 other information (such as the sizes of output sections) is available
3495 for use in the symbol assignment expression.
3496
3497 The sizes of sections cannot be known until after allocation, so
3498 assignments dependent upon these are not performed until after
3499 allocation.
3500
3501 Some expressions, such as those depending upon the location counter
3502 @samp{.}, must be evaluated during section allocation.
3503
3504 If the result of an expression is required, but the value is not
3505 available, then an error results. For example, a script like the
3506 following
3507 @smallexample
3508 @group
3509 SECTIONS
3510 @{
3511 .text 9+this_isnt_constant :
3512 @{ *(.text) @}
3513 @}
3514 @end group
3515 @end smallexample
3516 @noindent
3517 will cause the error message @samp{non constant expression for initial
3518 address}.
3519
3520 @node Expression Section
3521 @subsection The Section of an Expression
3522 @cindex expression sections
3523 @cindex absolute expressions
3524 @cindex relative expressions
3525 @cindex absolute and relocatable symbols
3526 @cindex relocatable and absolute symbols
3527 @cindex symbols, relocatable and absolute
3528 When the linker evaluates an expression, the result is either absolute
3529 or relative to some section. A relative expression is expressed as a
3530 fixed offset from the base of a section.
3531
3532 The position of the expression within the linker script determines
3533 whether it is absolute or relative. An expression which appears within
3534 an output section definition is relative to the base of the output
3535 section. An expression which appears elsewhere will be absolute.
3536
3537 A symbol set to a relative expression will be relocatable if you request
3538 relocatable output using the @samp{-r} option. That means that a
3539 further link operation may change the value of the symbol. The symbol's
3540 section will be the section of the relative expression.
3541
3542 A symbol set to an absolute expression will retain the same value
3543 through any further link operation. The symbol will be absolute, and
3544 will not have any particular associated section.
3545
3546 You can use the builtin function @code{ABSOLUTE} to force an expression
3547 to be absolute when it would otherwise be relative. For example, to
3548 create an absolute symbol set to the address of the end of the output
3549 section @samp{.data}:
3550 @smallexample
3551 SECTIONS
3552 @{
3553 .data : @{ *(.data) _edata = ABSOLUTE(.); @}
3554 @}
3555 @end smallexample
3556 @noindent
3557 If @samp{ABSOLUTE} were not used, @samp{_edata} would be relative to the
3558 @samp{.data} section.
3559
3560 @node Builtin Functions
3561 @subsection Builtin Functions
3562 @cindex functions in expressions
3563 The linker script language includes a number of builtin functions for
3564 use in linker script expressions.
3565
3566 @table @code
3567 @item ABSOLUTE(@var{exp})
3568 @kindex ABSOLUTE(@var{exp})
3569 @cindex expression, absolute
3570 Return the absolute (non-relocatable, as opposed to non-negative) value
3571 of the expression @var{exp}. Primarily useful to assign an absolute
3572 value to a symbol within a section definition, where symbol values are
3573 normally section relative. @xref{Expression Section}.
3574
3575 @item ADDR(@var{section})
3576 @kindex ADDR(@var{section})
3577 @cindex section address in expression
3578 Return the absolute address (the VMA) of the named @var{section}. Your
3579 script must previously have defined the location of that section. In
3580 the following example, @code{symbol_1} and @code{symbol_2} are assigned
3581 identical values:
3582 @smallexample
3583 @group
3584 SECTIONS @{ @dots{}
3585 .output1 :
3586 @{
3587 start_of_output_1 = ABSOLUTE(.);
3588 @dots{}
3589 @}
3590 .output :
3591 @{
3592 symbol_1 = ADDR(.output1);
3593 symbol_2 = start_of_output_1;
3594 @}
3595 @dots{} @}
3596 @end group
3597 @end smallexample
3598
3599 @item ALIGN(@var{exp})
3600 @kindex ALIGN(@var{exp})
3601 @cindex round up location counter
3602 @cindex align location counter
3603 Return the location counter (@code{.}) aligned to the next @var{exp}
3604 boundary. @var{exp} must be an expression whose value is a power of
3605 two. This is equivalent to
3606 @smallexample
3607 (. + @var{exp} - 1) & ~(@var{exp} - 1)
3608 @end smallexample
3609
3610 @code{ALIGN} doesn't change the value of the location counter---it just
3611 does arithmetic on it. Here is an example which aligns the output
3612 @code{.data} section to the next @code{0x2000} byte boundary after the
3613 preceding section and sets a variable within the section to the next
3614 @code{0x8000} boundary after the input sections:
3615 @smallexample
3616 @group
3617 SECTIONS @{ @dots{}
3618 .data ALIGN(0x2000): @{
3619 *(.data)
3620 variable = ALIGN(0x8000);
3621 @}
3622 @dots{} @}
3623 @end group
3624 @end smallexample
3625 @noindent
3626 The first use of @code{ALIGN} in this example specifies the location of
3627 a section because it is used as the optional @var{address} attribute of
3628 a section definition (@pxref{Output Section Address}). The second use
3629 of @code{ALIGN} is used to defines the value of a symbol.
3630
3631 The builtin function @code{NEXT} is closely related to @code{ALIGN}.
3632
3633 @item BLOCK(@var{exp})
3634 @kindex BLOCK(@var{exp})
3635 This is a synonym for @code{ALIGN}, for compatibility with older linker
3636 scripts. It is most often seen when setting the address of an output
3637 section.
3638
3639 @item DEFINED(@var{symbol})
3640 @kindex DEFINED(@var{symbol})
3641 @cindex symbol defaults
3642 Return 1 if @var{symbol} is in the linker global symbol table and is
3643 defined, otherwise return 0. You can use this function to provide
3644 default values for symbols. For example, the following script fragment
3645 shows how to set a global symbol @samp{begin} to the first location in
3646 the @samp{.text} section---but if a symbol called @samp{begin} already
3647 existed, its value is preserved:
3648
3649 @smallexample
3650 @group
3651 SECTIONS @{ @dots{}
3652 .text : @{
3653 begin = DEFINED(begin) ? begin : . ;
3654 @dots{}
3655 @}
3656 @dots{}
3657 @}
3658 @end group
3659 @end smallexample
3660
3661 @item LOADADDR(@var{section})
3662 @kindex LOADADDR(@var{section})
3663 @cindex section load address in expression
3664 Return the absolute LMA of the named @var{section}. This is normally
3665 the same as @code{ADDR}, but it may be different if the @code{AT}
3666 attribute is used in the output section definition (@pxref{Output
3667 Section LMA}).
3668
3669 @kindex MAX
3670 @item MAX(@var{exp1}, @var{exp2})
3671 Returns the maximum of @var{exp1} and @var{exp2}.
3672
3673 @kindex MIN
3674 @item MIN(@var{exp1}, @var{exp2})
3675 Returns the minimum of @var{exp1} and @var{exp2}.
3676
3677 @item NEXT(@var{exp})
3678 @kindex NEXT(@var{exp})
3679 @cindex unallocated address, next
3680 Return the next unallocated address that is a multiple of @var{exp}.
3681 This function is closely related to @code{ALIGN(@var{exp})}; unless you
3682 use the @code{MEMORY} command to define discontinuous memory for the
3683 output file, the two functions are equivalent.
3684
3685 @item SIZEOF(@var{section})
3686 @kindex SIZEOF(@var{section})
3687 @cindex section size
3688 Return the size in bytes of the named @var{section}, if that section has
3689 been allocated. If the section has not been allocated when this is
3690 evaluated, the linker will report an error. In the following example,
3691 @code{symbol_1} and @code{symbol_2} are assigned identical values:
3692 @smallexample
3693 @group
3694 SECTIONS@{ @dots{}
3695 .output @{
3696 .start = . ;
3697 @dots{}
3698 .end = . ;
3699 @}
3700 symbol_1 = .end - .start ;
3701 symbol_2 = SIZEOF(.output);
3702 @dots{} @}
3703 @end group
3704 @end smallexample
3705
3706 @item SIZEOF_HEADERS
3707 @itemx sizeof_headers
3708 @kindex SIZEOF_HEADERS
3709 @cindex header size
3710 Return the size in bytes of the output file's headers. This is
3711 information which appears at the start of the output file. You can use
3712 this number when setting the start address of the first section, if you
3713 choose, to facilitate paging.
3714
3715 @cindex not enough room for program headers
3716 @cindex program headers, not enough room
3717 When producing an ELF output file, if the linker script uses the
3718 @code{SIZEOF_HEADERS} builtin function, the linker must compute the
3719 number of program headers before it has determined all the section
3720 addresses and sizes. If the linker later discovers that it needs
3721 additional program headers, it will report an error @samp{not enough
3722 room for program headers}. To avoid this error, you must avoid using
3723 the @code{SIZEOF_HEADERS} function, or you must rework your linker
3724 script to avoid forcing the linker to use additional program headers, or
3725 you must define the program headers yourself using the @code{PHDRS}
3726 command (@pxref{PHDRS}).
3727 @end table
3728
3729 @node Implicit Linker Scripts
3730 @section Implicit Linker Scripts
3731 @cindex implicit linker scripts
3732 If you specify a linker input file which the linker can not recognize as
3733 an object file or an archive file, it will try to read the file as a
3734 linker script. If the file can not be parsed as a linker script, the
3735 linker will report an error.
3736
3737 An implicit linker script will not replace the default linker script.
3738
3739 Typically an implicit linker script would contain only symbol
3740 assignments, or the @code{INPUT}, @code{GROUP}, or @code{VERSION}
3741 commands.
3742
3743 Any input files read because of an implicit linker script will be read
3744 at the position in the command line where the implicit linker script was
3745 read. This can affect archive searching.
3746
3747 @ifset GENERIC
3748 @node Machine Dependent
3749 @chapter Machine Dependent Features
3750
3751 @cindex machine dependencies
3752 @code{ld} has additional features on some platforms; the following
3753 sections describe them. Machines where @code{ld} has no additional
3754 functionality are not listed.
3755
3756 @menu
3757 * H8/300:: @code{ld} and the H8/300
3758 * i960:: @code{ld} and the Intel 960 family
3759 * ARM:: @code{ld} and the ARM family
3760 @end menu
3761 @end ifset
3762
3763 @c FIXME! This could use @raisesections/@lowersections, but there seems to be a conflict
3764 @c between those and node-defaulting.
3765 @ifset H8300
3766 @ifclear GENERIC
3767 @raisesections
3768 @end ifclear
3769
3770 @node H8/300
3771 @section @code{ld} and the H8/300
3772
3773 @cindex H8/300 support
3774 For the H8/300, @code{ld} can perform these global optimizations when
3775 you specify the @samp{--relax} command-line option.
3776
3777 @table @emph
3778 @cindex relaxing on H8/300
3779 @item relaxing address modes
3780 @code{ld} finds all @code{jsr} and @code{jmp} instructions whose
3781 targets are within eight bits, and turns them into eight-bit
3782 program-counter relative @code{bsr} and @code{bra} instructions,
3783 respectively.
3784
3785 @cindex synthesizing on H8/300
3786 @item synthesizing instructions
3787 @c FIXME: specifically mov.b, or any mov instructions really?
3788 @code{ld} finds all @code{mov.b} instructions which use the
3789 sixteen-bit absolute address form, but refer to the top
3790 page of memory, and changes them to use the eight-bit address form.
3791 (That is: the linker turns @samp{mov.b @code{@@}@var{aa}:16} into
3792 @samp{mov.b @code{@@}@var{aa}:8} whenever the address @var{aa} is in the
3793 top page of memory).
3794 @end table
3795
3796 @ifclear GENERIC
3797 @lowersections
3798 @end ifclear
3799 @end ifset
3800
3801 @ifclear GENERIC
3802 @ifset Hitachi
3803 @c This stuff is pointless to say unless you're especially concerned
3804 @c with Hitachi chips; don't enable it for generic case, please.
3805 @node Hitachi
3806 @chapter @code{ld} and other Hitachi chips
3807
3808 @code{ld} also supports the H8/300H, the H8/500, and the Hitachi SH. No
3809 special features, commands, or command-line options are required for
3810 these chips.
3811 @end ifset
3812 @end ifclear
3813
3814 @ifset I960
3815 @ifclear GENERIC
3816 @raisesections
3817 @end ifclear
3818
3819 @node i960
3820 @section @code{ld} and the Intel 960 family
3821
3822 @cindex i960 support
3823
3824 You can use the @samp{-A@var{architecture}} command line option to
3825 specify one of the two-letter names identifying members of the 960
3826 family; the option specifies the desired output target, and warns of any
3827 incompatible instructions in the input files. It also modifies the
3828 linker's search strategy for archive libraries, to support the use of
3829 libraries specific to each particular architecture, by including in the
3830 search loop names suffixed with the string identifying the architecture.
3831
3832 For example, if your @code{ld} command line included @w{@samp{-ACA}} as
3833 well as @w{@samp{-ltry}}, the linker would look (in its built-in search
3834 paths, and in any paths you specify with @samp{-L}) for a library with
3835 the names
3836
3837 @smallexample
3838 @group
3839 try
3840 libtry.a
3841 tryca
3842 libtryca.a
3843 @end group
3844 @end smallexample
3845
3846 @noindent
3847 The first two possibilities would be considered in any event; the last
3848 two are due to the use of @w{@samp{-ACA}}.
3849
3850 You can meaningfully use @samp{-A} more than once on a command line, since
3851 the 960 architecture family allows combination of target architectures; each
3852 use will add another pair of name variants to search for when @w{@samp{-l}}
3853 specifies a library.
3854
3855 @cindex @code{--relax} on i960
3856 @cindex relaxing on i960
3857 @code{ld} supports the @samp{--relax} option for the i960 family. If
3858 you specify @samp{--relax}, @code{ld} finds all @code{balx} and
3859 @code{calx} instructions whose targets are within 24 bits, and turns
3860 them into 24-bit program-counter relative @code{bal} and @code{cal}
3861 instructions, respectively. @code{ld} also turns @code{cal}
3862 instructions into @code{bal} instructions when it determines that the
3863 target subroutine is a leaf routine (that is, the target subroutine does
3864 not itself call any subroutines).
3865
3866 @ifclear GENERIC
3867 @lowersections
3868 @end ifclear
3869 @end ifset
3870
3871 @ifclear GENERIC
3872 @raisesections
3873 @end ifclear
3874
3875 @node ARM
3876 @section @code{ld}'s support for interworking between ARM and Thumb code
3877
3878 @cindex ARM interworking support
3879 @cindex --support-old-code
3880 For the ARM, @code{ld} will generate code stubs to allow functions calls
3881 betweem ARM and Thumb code. These stubs only work with code that has
3882 been compiled and assembled with the @samp{-mthumb-interwork} command
3883 line option. If it is necessary to link with old ARM object files or
3884 libraries, which have not been compiled with the -mthumb-interwork
3885 option then the @samp{--support-old-code} command line switch should be
3886 given to the linker. This will make it generate larger stub functions
3887 which will work with non-interworking aware ARM code. Note, however,
3888 the linker does not support generating stubs for function calls to
3889 non-interworking aware Thumb code.
3890
3891 @ifclear GENERIC
3892 @lowersections
3893 @end ifclear
3894
3895 @ifclear SingleFormat
3896 @node BFD
3897 @chapter BFD
3898
3899 @cindex back end
3900 @cindex object file management
3901 @cindex object formats available
3902 @kindex objdump -i
3903 The linker accesses object and archive files using the BFD libraries.
3904 These libraries allow the linker to use the same routines to operate on
3905 object files whatever the object file format. A different object file
3906 format can be supported simply by creating a new BFD back end and adding
3907 it to the library. To conserve runtime memory, however, the linker and
3908 associated tools are usually configured to support only a subset of the
3909 object file formats available. You can use @code{objdump -i}
3910 (@pxref{objdump,,objdump,binutils.info,The GNU Binary Utilities}) to
3911 list all the formats available for your configuration.
3912
3913 @cindex BFD requirements
3914 @cindex requirements for BFD
3915 As with most implementations, BFD is a compromise between
3916 several conflicting requirements. The major factor influencing
3917 BFD design was efficiency: any time used converting between
3918 formats is time which would not have been spent had BFD not
3919 been involved. This is partly offset by abstraction payback; since
3920 BFD simplifies applications and back ends, more time and care
3921 may be spent optimizing algorithms for a greater speed.
3922
3923 One minor artifact of the BFD solution which you should bear in
3924 mind is the potential for information loss. There are two places where
3925 useful information can be lost using the BFD mechanism: during
3926 conversion and during output. @xref{BFD information loss}.
3927
3928 @menu
3929 * BFD outline:: How it works: an outline of BFD
3930 @end menu
3931
3932 @node BFD outline
3933 @section How it works: an outline of BFD
3934 @cindex opening object files
3935 @include bfdsumm.texi
3936 @end ifclear
3937
3938 @node Reporting Bugs
3939 @chapter Reporting Bugs
3940 @cindex bugs in @code{ld}
3941 @cindex reporting bugs in @code{ld}
3942
3943 Your bug reports play an essential role in making @code{ld} reliable.
3944
3945 Reporting a bug may help you by bringing a solution to your problem, or
3946 it may not. But in any case the principal function of a bug report is
3947 to help the entire community by making the next version of @code{ld}
3948 work better. Bug reports are your contribution to the maintenance of
3949 @code{ld}.
3950
3951 In order for a bug report to serve its purpose, you must include the
3952 information that enables us to fix the bug.
3953
3954 @menu
3955 * Bug Criteria:: Have you found a bug?
3956 * Bug Reporting:: How to report bugs
3957 @end menu
3958
3959 @node Bug Criteria
3960 @section Have you found a bug?
3961 @cindex bug criteria
3962
3963 If you are not sure whether you have found a bug, here are some guidelines:
3964
3965 @itemize @bullet
3966 @cindex fatal signal
3967 @cindex linker crash
3968 @cindex crash of linker
3969 @item
3970 If the linker gets a fatal signal, for any input whatever, that is a
3971 @code{ld} bug. Reliable linkers never crash.
3972
3973 @cindex error on valid input
3974 @item
3975 If @code{ld} produces an error message for valid input, that is a bug.
3976
3977 @cindex invalid input
3978 @item
3979 If @code{ld} does not produce an error message for invalid input, that
3980 may be a bug. In the general case, the linker can not verify that
3981 object files are correct.
3982
3983 @item
3984 If you are an experienced user of linkers, your suggestions for
3985 improvement of @code{ld} are welcome in any case.
3986 @end itemize
3987
3988 @node Bug Reporting
3989 @section How to report bugs
3990 @cindex bug reports
3991 @cindex @code{ld} bugs, reporting
3992
3993 A number of companies and individuals offer support for @sc{gnu}
3994 products. If you obtained @code{ld} from a support organization, we
3995 recommend you contact that organization first.
3996
3997 You can find contact information for many support companies and
3998 individuals in the file @file{etc/SERVICE} in the @sc{gnu} Emacs
3999 distribution.
4000
4001 Otherwise, send bug reports for @code{ld} to
4002 @samp{bug-gnu-utils@@gnu.org}.
4003
4004 The fundamental principle of reporting bugs usefully is this:
4005 @strong{report all the facts}. If you are not sure whether to state a
4006 fact or leave it out, state it!
4007
4008 Often people omit facts because they think they know what causes the
4009 problem and assume that some details do not matter. Thus, you might
4010 assume that the name of a symbol you use in an example does not matter.
4011 Well, probably it does not, but one cannot be sure. Perhaps the bug is
4012 a stray memory reference which happens to fetch from the location where
4013 that name is stored in memory; perhaps, if the name were different, the
4014 contents of that location would fool the linker into doing the right
4015 thing despite the bug. Play it safe and give a specific, complete
4016 example. That is the easiest thing for you to do, and the most helpful.
4017
4018 Keep in mind that the purpose of a bug report is to enable us to fix the bug if
4019 it is new to us. Therefore, always write your bug reports on the assumption
4020 that the bug has not been reported previously.
4021
4022 Sometimes people give a few sketchy facts and ask, ``Does this ring a
4023 bell?'' Those bug reports are useless, and we urge everyone to
4024 @emph{refuse to respond to them} except to chide the sender to report
4025 bugs properly.
4026
4027 To enable us to fix the bug, you should include all these things:
4028
4029 @itemize @bullet
4030 @item
4031 The version of @code{ld}. @code{ld} announces it if you start it with
4032 the @samp{--version} argument.
4033
4034 Without this, we will not know whether there is any point in looking for
4035 the bug in the current version of @code{ld}.
4036
4037 @item
4038 Any patches you may have applied to the @code{ld} source, including any
4039 patches made to the @code{BFD} library.
4040
4041 @item
4042 The type of machine you are using, and the operating system name and
4043 version number.
4044
4045 @item
4046 What compiler (and its version) was used to compile @code{ld}---e.g.
4047 ``@code{gcc-2.7}''.
4048
4049 @item
4050 The command arguments you gave the linker to link your example and
4051 observe the bug. To guarantee you will not omit something important,
4052 list them all. A copy of the Makefile (or the output from make) is
4053 sufficient.
4054
4055 If we were to try to guess the arguments, we would probably guess wrong
4056 and then we might not encounter the bug.
4057
4058 @item
4059 A complete input file, or set of input files, that will reproduce the
4060 bug. It is generally most helpful to send the actual object files,
4061 uuencoded if necessary to get them through the mail system. Making them
4062 available for anonymous FTP is not as good, but may be the only
4063 reasonable choice for large object files.
4064
4065 If the source files were assembled using @code{gas} or compiled using
4066 @code{gcc}, then it may be OK to send the source files rather than the
4067 object files. In this case, be sure to say exactly what version of
4068 @code{gas} or @code{gcc} was used to produce the object files. Also say
4069 how @code{gas} or @code{gcc} were configured.
4070
4071 @item
4072 A description of what behavior you observe that you believe is
4073 incorrect. For example, ``It gets a fatal signal.''
4074
4075 Of course, if the bug is that @code{ld} gets a fatal signal, then we
4076 will certainly notice it. But if the bug is incorrect output, we might
4077 not notice unless it is glaringly wrong. You might as well not give us
4078 a chance to make a mistake.
4079
4080 Even if the problem you experience is a fatal signal, you should still
4081 say so explicitly. Suppose something strange is going on, such as, your
4082 copy of @code{ld} is out of synch, or you have encountered a bug in the
4083 C library on your system. (This has happened!) Your copy might crash
4084 and ours would not. If you told us to expect a crash, then when ours
4085 fails to crash, we would know that the bug was not happening for us. If
4086 you had not told us to expect a crash, then we would not be able to draw
4087 any conclusion from our observations.
4088
4089 @item
4090 If you wish to suggest changes to the @code{ld} source, send us context
4091 diffs, as generated by @code{diff} with the @samp{-u}, @samp{-c}, or
4092 @samp{-p} option. Always send diffs from the old file to the new file.
4093 If you even discuss something in the @code{ld} source, refer to it by
4094 context, not by line number.
4095
4096 The line numbers in our development sources will not match those in your
4097 sources. Your line numbers would convey no useful information to us.
4098 @end itemize
4099
4100 Here are some things that are not necessary:
4101
4102 @itemize @bullet
4103 @item
4104 A description of the envelope of the bug.
4105
4106 Often people who encounter a bug spend a lot of time investigating
4107 which changes to the input file will make the bug go away and which
4108 changes will not affect it.
4109
4110 This is often time consuming and not very useful, because the way we
4111 will find the bug is by running a single example under the debugger
4112 with breakpoints, not by pure deduction from a series of examples.
4113 We recommend that you save your time for something else.
4114
4115 Of course, if you can find a simpler example to report @emph{instead}
4116 of the original one, that is a convenience for us. Errors in the
4117 output will be easier to spot, running under the debugger will take
4118 less time, and so on.
4119
4120 However, simplification is not vital; if you do not want to do this,
4121 report the bug anyway and send us the entire test case you used.
4122
4123 @item
4124 A patch for the bug.
4125
4126 A patch for the bug does help us if it is a good one. But do not omit
4127 the necessary information, such as the test case, on the assumption that
4128 a patch is all we need. We might see problems with your patch and decide
4129 to fix the problem another way, or we might not understand it at all.
4130
4131 Sometimes with a program as complicated as @code{ld} it is very hard to
4132 construct an example that will make the program follow a certain path
4133 through the code. If you do not send us the example, we will not be
4134 able to construct one, so we will not be able to verify that the bug is
4135 fixed.
4136
4137 And if we cannot understand what bug you are trying to fix, or why your
4138 patch should be an improvement, we will not install it. A test case will
4139 help us to understand.
4140
4141 @item
4142 A guess about what the bug is or what it depends on.
4143
4144 Such guesses are usually wrong. Even we cannot guess right about such
4145 things without first using the debugger to find the facts.
4146 @end itemize
4147
4148 @node MRI
4149 @appendix MRI Compatible Script Files
4150 @cindex MRI compatibility
4151 To aid users making the transition to @sc{gnu} @code{ld} from the MRI
4152 linker, @code{ld} can use MRI compatible linker scripts as an
4153 alternative to the more general-purpose linker scripting language
4154 described in @ref{Scripts}. MRI compatible linker scripts have a much
4155 simpler command set than the scripting language otherwise used with
4156 @code{ld}. @sc{gnu} @code{ld} supports the most commonly used MRI
4157 linker commands; these commands are described here.
4158
4159 In general, MRI scripts aren't of much use with the @code{a.out} object
4160 file format, since it only has three sections and MRI scripts lack some
4161 features to make use of them.
4162
4163 You can specify a file containing an MRI-compatible script using the
4164 @samp{-c} command-line option.
4165
4166 Each command in an MRI-compatible script occupies its own line; each
4167 command line starts with the keyword that identifies the command (though
4168 blank lines are also allowed for punctuation). If a line of an
4169 MRI-compatible script begins with an unrecognized keyword, @code{ld}
4170 issues a warning message, but continues processing the script.
4171
4172 Lines beginning with @samp{*} are comments.
4173
4174 You can write these commands using all upper-case letters, or all
4175 lower case; for example, @samp{chip} is the same as @samp{CHIP}.
4176 The following list shows only the upper-case form of each command.
4177
4178 @table @code
4179 @cindex @code{ABSOLUTE} (MRI)
4180 @item ABSOLUTE @var{secname}
4181 @itemx ABSOLUTE @var{secname}, @var{secname}, @dots{} @var{secname}
4182 Normally, @code{ld} includes in the output file all sections from all
4183 the input files. However, in an MRI-compatible script, you can use the
4184 @code{ABSOLUTE} command to restrict the sections that will be present in
4185 your output program. If the @code{ABSOLUTE} command is used at all in a
4186 script, then only the sections named explicitly in @code{ABSOLUTE}
4187 commands will appear in the linker output. You can still use other
4188 input sections (whatever you select on the command line, or using
4189 @code{LOAD}) to resolve addresses in the output file.
4190
4191 @cindex @code{ALIAS} (MRI)
4192 @item ALIAS @var{out-secname}, @var{in-secname}
4193 Use this command to place the data from input section @var{in-secname}
4194 in a section called @var{out-secname} in the linker output file.
4195
4196 @var{in-secname} may be an integer.
4197
4198 @cindex @code{ALIGN} (MRI)
4199 @item ALIGN @var{secname} = @var{expression}
4200 Align the section called @var{secname} to @var{expression}. The
4201 @var{expression} should be a power of two.
4202
4203 @cindex @code{BASE} (MRI)
4204 @item BASE @var{expression}
4205 Use the value of @var{expression} as the lowest address (other than
4206 absolute addresses) in the output file.
4207
4208 @cindex @code{CHIP} (MRI)
4209 @item CHIP @var{expression}
4210 @itemx CHIP @var{expression}, @var{expression}
4211 This command does nothing; it is accepted only for compatibility.
4212
4213 @cindex @code{END} (MRI)
4214 @item END
4215 This command does nothing whatever; it's only accepted for compatibility.
4216
4217 @cindex @code{FORMAT} (MRI)
4218 @item FORMAT @var{output-format}
4219 Similar to the @code{OUTPUT_FORMAT} command in the more general linker
4220 language, but restricted to one of these output formats:
4221
4222 @enumerate
4223 @item
4224 S-records, if @var{output-format} is @samp{S}
4225
4226 @item
4227 IEEE, if @var{output-format} is @samp{IEEE}
4228
4229 @item
4230 COFF (the @samp{coff-m68k} variant in BFD), if @var{output-format} is
4231 @samp{COFF}
4232 @end enumerate
4233
4234 @cindex @code{LIST} (MRI)
4235 @item LIST @var{anything}@dots{}
4236 Print (to the standard output file) a link map, as produced by the
4237 @code{ld} command-line option @samp{-M}.
4238
4239 The keyword @code{LIST} may be followed by anything on the
4240 same line, with no change in its effect.
4241
4242 @cindex @code{LOAD} (MRI)
4243 @item LOAD @var{filename}
4244 @itemx LOAD @var{filename}, @var{filename}, @dots{} @var{filename}
4245 Include one or more object file @var{filename} in the link; this has the
4246 same effect as specifying @var{filename} directly on the @code{ld}
4247 command line.
4248
4249 @cindex @code{NAME} (MRI)
4250 @item NAME @var{output-name}
4251 @var{output-name} is the name for the program produced by @code{ld}; the
4252 MRI-compatible command @code{NAME} is equivalent to the command-line
4253 option @samp{-o} or the general script language command @code{OUTPUT}.
4254
4255 @cindex @code{ORDER} (MRI)
4256 @item ORDER @var{secname}, @var{secname}, @dots{} @var{secname}
4257 @itemx ORDER @var{secname} @var{secname} @var{secname}
4258 Normally, @code{ld} orders the sections in its output file in the
4259 order in which they first appear in the input files. In an MRI-compatible
4260 script, you can override this ordering with the @code{ORDER} command. The
4261 sections you list with @code{ORDER} will appear first in your output
4262 file, in the order specified.
4263
4264 @cindex @code{PUBLIC} (MRI)
4265 @item PUBLIC @var{name}=@var{expression}
4266 @itemx PUBLIC @var{name},@var{expression}
4267 @itemx PUBLIC @var{name} @var{expression}
4268 Supply a value (@var{expression}) for external symbol
4269 @var{name} used in the linker input files.
4270
4271 @cindex @code{SECT} (MRI)
4272 @item SECT @var{secname}, @var{expression}
4273 @itemx SECT @var{secname}=@var{expression}
4274 @itemx SECT @var{secname} @var{expression}
4275 You can use any of these three forms of the @code{SECT} command to
4276 specify the start address (@var{expression}) for section @var{secname}.
4277 If you have more than one @code{SECT} statement for the same
4278 @var{secname}, only the @emph{first} sets the start address.
4279 @end table
4280
4281 @node Index
4282 @unnumbered Index
4283
4284 @printindex cp
4285
4286 @tex
4287 % I think something like @colophon should be in texinfo. In the
4288 % meantime:
4289 \long\def\colophon{\hbox to0pt{}\vfill
4290 \centerline{The body of this manual is set in}
4291 \centerline{\fontname\tenrm,}
4292 \centerline{with headings in {\bf\fontname\tenbf}}
4293 \centerline{and examples in {\tt\fontname\tentt}.}
4294 \centerline{{\it\fontname\tenit\/} and}
4295 \centerline{{\sl\fontname\tensl\/}}
4296 \centerline{are used for emphasis.}\vfill}
4297 \page\colophon
4298 % Blame: doc@cygnus.com, 28mar91.
4299 @end tex
4300
4301
4302 @contents
4303 @bye
4304
4305
This page took 0.138306 seconds and 4 git commands to generate.