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