87a5e5459fd3eb96ce784763bb5278043af00f6c
[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 GNU linker LD.
19
20 Copyright (C) 1991, 1992, 1993, 1994 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 and Roland Pesch
53 @author Cygnus Support
54 @page
55
56 @tex
57 {\parskip=0pt
58 \hfill Cygnus Support\par
59 \hfill steve\@cygnus.com, pesch\@cygnus.com\par
60 \hfill {\it Using LD, the GNU linker}\par
61 \hfill Edited by Jeffrey Osier (jeffrey\@cygnus.com)\par
62 \hfill and Roland Pesch (pesch\@cygnus.com)\par
63 }
64 \global\parindent=0pt % Steve likes it this way.
65 @end tex
66
67 @vskip 0pt plus 1filll
68 Copyright @copyright{} 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
69
70 Permission is granted to make and distribute verbatim copies of
71 this manual provided the copyright notice and this permission notice
72 are preserved on all copies.
73
74 Permission is granted to copy and distribute modified versions of this
75 manual under the conditions for verbatim copying, provided also that
76 the entire resulting derived work is distributed under the terms of a
77 permission notice identical to this one.
78
79 Permission is granted to copy and distribute translations of this manual
80 into another language, under the above conditions for modified versions.
81 @end titlepage
82 @end iftex
83 @c FIXME: Talk about importance of *order* of args, cmds to linker!
84
85 @ifinfo
86 @node Top
87 @top Using ld
88 This file documents the GNU linker ld.
89
90 @menu
91 * Overview:: Overview
92 * Invocation:: Invocation
93 * Commands:: Command Language
94 @ifset GENERIC
95 * Machine Dependent:: Machine Dependent Features
96 @end ifset
97 @ifclear GENERIC
98 @ifset H8300
99 * H8/300:: ld and the H8/300
100 @end ifset
101 @ifset Hitachi
102 * Hitachi:: ld and other Hitachi micros
103 @end ifset
104 @ifset I960
105 * i960:: ld and the Intel 960 family
106 @end ifset
107 @end ifclear
108 @ifclear SingleFormat
109 * BFD:: BFD
110 @end ifclear
111 @c Following blank line required for remaining bug in makeinfo conds/menus
112
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 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 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 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 Here is a summary of the options you can use on the @code{ld} command
165 line:
166
167 @c FIXME! -relax only avail h8/300, i960. Conditionals screwed in examples.
168 @smallexample
169 ld [ -o @var{output} ] @var{objfile}@dots{}
170 [ -A@var{architecture} ] [ -b @var{input-format} ]
171 [ -Bstatic ] [ -Bdynamic ] [ -Bsymbolic ]
172 [ -c @var{MRI-commandfile} ] [ -d | -dc | -dp ]
173 [ -defsym @var{symbol}=@var{expression} ]
174 [ -dynamic-linker @var{file} ] [ -embedded-relocs ]
175 [ -e @var{entry} ] [ -F ] [ -F @var{format} ]
176 [ -format @var{input-format} ] [ -g ] [ -G @var{size} ] [ -help ]
177 [ -i ] [ -l@var{archive} ] [ -L@var{searchdir} ] [ -M ]
178 [ -Map @var{mapfile} ] [ -m @var{emulation} ] [ -N | -n ]
179 [ -noinhibit-exec ] [ -no-keep-memory ] [ -oformat @var{output-format} ]
180 [ -R @var{filename} ] [ -relax ] [ -retain-symbols-file @var{filename} ]
181 [ -r | -Ur ] [ -rpath @var{dir} ] [ -S ] [ -s ] [ -soname @var{name} ]
182 [ -shared ] [ -sort-common ] [ -stats ] [ -T @var{commandfile} ]
183 [ -Ttext @var{org} ] [ -Tdata @var{org} ]
184 [ -Tbss @var{org} ] [ -t ] [ -traditional-format ]
185 [ -u @var{symbol}] [-V] [-v] [ -verbose] [ -version ]
186 [ -warn-common ] [ -warn-once ] [ -y @var{symbol} ] [ -X ] [-x ]
187 [ -( [ archives ] -) ] [ --start-group [ archives ] --end-group ]
188 [ -split-by-reloc @var{count} ] [ -split-by-file ] [ --whole-archive ]
189 @end smallexample
190
191 This plethora of command-line options may seem intimidating, but in
192 actual practice few of them are used in any particular context.
193 @cindex standard Unix system
194 For instance, a frequent use of @code{ld} is to link standard Unix
195 object files on a standard, supported Unix system. On such a system, to
196 link a file @code{hello.o}:
197
198 @example
199 ld -o @var{output} /lib/crt0.o hello.o -lc
200 @end example
201
202 This tells @code{ld} to produce a file called @var{output} as the
203 result of linking the file @code{/lib/crt0.o} with @code{hello.o} and
204 the library @code{libc.a}, which will come from the standard search
205 directories. (See the discussion of the @samp{-l} option below.)
206
207 The command-line options to @code{ld} may be specified in any order, and
208 may be repeated at will. Repeating most options with a
209 different argument will either have no further effect, or override prior
210 occurrences (those further to the left on the command line) of that
211 option.
212
213 @ifclear SingleFormat
214 The exceptions---which may meaningfully be used more than once---are
215 @samp{-A}, @samp{-b} (or its synonym @samp{-format}), @samp{-defsym},
216 @samp{-L}, @samp{-l}, @samp{-R}, @samp{-u}, and @samp{-(} (or its
217 synonym @samp{--start-group})..
218 @end ifclear
219 @ifset SingleFormat
220 The exceptions---which may meaningfully be used more than once---are
221 @samp{-A}, @samp{-defsym}, @samp{-L}, @samp{-l}, @samp{-R}, @samp{-u},
222 and @samp{-(} (or its synonym @samp{--start-group}).
223 @end ifset
224
225 @cindex object files
226 The list of object files to be linked together, shown as @var{objfile}@dots{},
227 may follow, precede, or be mixed in with command-line options, except that
228 an @var{objfile} argument may not be placed between an option and
229 its argument.
230
231 Usually the linker is invoked with at least one object file, but you can
232 specify other forms of binary input files using @samp{-l}, @samp{-R},
233 and the script command language. If @emph{no} binary input files at all
234 are specified, the linker does not produce any output, and issues the
235 message @samp{No input files}.
236
237 If the linker can not recognize the format of an object file, it will
238 assume that it is a linker script. A script specified in this way
239 augments the main linker script used for the link (either the default
240 linker script or the one specified by using @samp{-T}). This feature
241 permits the linker to link against a file which appears to be an object
242 or an archive, but actually merely defines some symbol values, or uses
243 @code{INPUT} or @code{GROUP} to load other objects. @xref{Commands}.
244
245 For options whose names are a single letter,
246 option arguments must either follow the option letter without intervening
247 whitespace, or be given as separate arguments immediately following the
248 option that requires them.
249
250 For options whose names are multiple letters, either one dash or two can
251 precede the option name; for example, @samp{--oformat} and
252 @samp{-oformat} are equivalent. Arguments to multiple-letter options
253 must either be separated from the option name by an equals sign, or be
254 given as separate arguments immediately following the option that
255 requires them. For example, @samp{--oformat srec} and
256 @samp{--oformat=srec} are equivalent. Unique abbreviations of the names
257 of multiple-letter options are accepted.
258
259 @table @code
260 @ifset I960
261 @cindex architectures
262 @kindex -A@var{arch}
263 @item -A@var{architecture}
264 In the current release of @code{ld}, this option is useful only for the
265 Intel 960 family of architectures. In that @code{ld} configuration, the
266 @var{architecture} argument identifies the particular architecture in
267 the 960 family, enabling some safeguards and modifying the
268 archive-library search path. @xref{i960,,@code{ld} and the Intel 960
269 family}, for details.
270
271 Future releases of @code{ld} may support similar functionality for
272 other architecture families.
273 @end ifset
274
275 @ifclear SingleFormat
276 @cindex binary input format
277 @kindex -b @var{format}
278 @cindex input format
279 @item -b @var{input-format}
280 @cindex input format
281 @code{ld} may be configured to support more than one kind of object
282 file. If your @code{ld} is configured this way, you can use the
283 @samp{-b} option to specify the binary format for input object files
284 that follow this option on the command line. Even when @code{ld} is
285 configured to support alternative object formats, you don't usually need
286 to specify this, as @code{ld} should be configured to expect as a
287 default input format the most usual format on each machine.
288 @var{input-format} is a text string, the name of a particular format
289 supported by the BFD libraries. (You can list the available binary
290 formats with @samp{objdump -i}.) @w{@samp{-format @var{input-format}}}
291 has the same effect, as does the script command @code{TARGET}.
292 @xref{BFD}.
293
294 You may want to use this option if you are linking files with an unusual
295 binary format. You can also use @samp{-b} to switch formats explicitly (when
296 linking object files of different formats), by including
297 @samp{-b @var{input-format}} before each group of object files in a
298 particular format.
299
300 The default format is taken from the environment variable
301 @code{GNUTARGET}.
302 @ifset UsesEnvVars
303 @xref{Environment}.
304 @end ifset
305 You can also define the input
306 format from a script, using the command @code{TARGET}; see @ref{Option
307 Commands}.
308 @end ifclear
309
310 @kindex -Bstatic
311 @item -Bstatic
312 Do not link against shared libraries. This is only meaningful on
313 platforms for which shared libraries are supported.
314
315 @kindex -Bdynamic
316 @item -Bdynamic
317 Link against dynamic libraries. This is only meaningful on platforms
318 for which shared libraries are supported. This option is normally the
319 default on such platforms.
320
321 @kindex -Bsymbolic
322 @item -Bsymbolic
323 When creating a shared library, bind references to global symbols to the
324 definition within the shared library, if any. Normally, it is possible
325 for a program linked against a shared library to override the definition
326 within the shared library. This option is only meaningful on ELF
327 platforms which support shared libraries.
328
329 @kindex -c @var{MRI-cmdfile}
330 @cindex compatibility, MRI
331 @item -c @var{MRI-commandfile}
332 For compatibility with linkers produced by MRI, @code{ld} accepts script
333 files written in an alternate, restricted command language, described in
334 @ref{MRI,,MRI Compatible Script Files}. Introduce MRI script files with
335 the option @samp{-c}; use the @samp{-T} option to run linker
336 scripts written in the general-purpose @code{ld} scripting language.
337 If @var{MRI-cmdfile} does not exist, @code{ld} looks for it in the directories
338 specified by any @samp{-L} options.
339
340 @cindex common allocation
341 @kindex -d
342 @item -d
343 @kindex -dc
344 @itemx -dc
345 @kindex -dp
346 @itemx -dp
347 These three options are equivalent; multiple forms are supported for
348 compatibility with other linkers. They
349 assign space to common symbols even if a relocatable output file is
350 specified (with @samp{-r}). The script command
351 @code{FORCE_COMMON_ALLOCATION} has the same effect. @xref{Option
352 Commands}.
353
354 @cindex symbols, from command line
355 @kindex -defsym @var{symbol}=@var{exp}
356 @item -defsym @var{symbol}=@var{expression}
357 Create a global symbol in the output file, containing the absolute
358 address given by @var{expression}. You may use this option as many
359 times as necessary to define multiple symbols in the command line. A
360 limited form of arithmetic is supported for the @var{expression} in this
361 context: you may give a hexadecimal constant or the name of an existing
362 symbol, or use @code{+} and @code{-} to add or subtract hexadecimal
363 constants or symbols. If you need more elaborate expressions, consider
364 using the linker command language from a script (@pxref{Assignment, ,
365 Assignment: Symbol Definitions}). @emph{Note:} there should be no
366 white space between @var{symbol}, the equals sign (``@key{=}''), and
367 @var{expression}.
368
369 @ifset GENERIC
370 @cindex dynamic linker, from command line
371 @kindex -dynamic-linker @var{file}
372 @item -dynamic-linker @var{file}
373 Set the name of the dynamic linker. This is only meaningful when
374 generating dynamically linked ELF executables. The default dynamic
375 linker is normally correct; don't use this unless you know what you are
376 doing.
377 @end ifset
378
379 @cindex MIPS embedded PIC code
380 @kindex -embedded-relocs
381 @item -embedded-relocs
382 This option is only meaningful when linking MIPS embedded PIC code,
383 generated by the -membedded-pic option to the GNU compiler and
384 assembler. It causes the linker to create a table which may be used at
385 runtime to relocate any data which was statically initialized to pointer
386 values. See the code in testsuite/ld-empic for details.
387
388 @cindex entry point, from command line
389 @kindex -e @var{entry}
390 @item -e @var{entry}
391 Use @var{entry} as the explicit symbol for beginning execution of your
392 program, rather than the default entry point. @xref{Entry Point}, for a
393 discussion of defaults and other ways of specifying the
394 entry point.
395
396 @ifclear SingleFormat
397 @kindex -F
398 @item -F
399 @itemx -F@var{format}
400 Ignored. Some older linkers used this option throughout a compilation
401 toolchain for specifying object-file format for both input and output
402 object files. The mechanisms @code{ld} uses for this purpose (the
403 @samp{-b} or @samp{-format} options for input files, @samp{-oformat}
404 option or the @code{TARGET} command in linker scripts for output files,
405 the @code{GNUTARGET} environment variable) are more flexible, but
406 @code{ld} accepts the @samp{-F} option for compatibility with scripts
407 written to call the old linker.
408
409 @kindex -format
410 @item -format @var{input-format}
411 Synonym for @samp{-b @var{input-format}}.
412 @end ifclear
413
414 @kindex -g
415 @item -g
416 Ignored. Provided for compatibility with other tools.
417
418 @kindex -G
419 @cindex object size
420 @item -G@var{value}
421 @itemx -G @var{value}
422 Set the maximum size of objects to be optimized using the GP register to
423 @var{size} under MIPS ECOFF. Ignored for other object file formats.
424
425 @cindex help
426 @cindex usage
427 @kindex -help
428 @item -help
429 Print a summary of the command-line options on the standard output and exit.
430
431 @kindex -i
432 @cindex incremental link
433 @item -i
434 Perform an incremental link (same as option @samp{-r}).
435
436 @cindex archive files, from cmd line
437 @kindex -l@var{archive}
438 @item -l@var{ar}
439 Add archive file @var{archive} to the list of files to link. This
440 option may be used any number of times. @code{ld} will search its
441 path-list for occurrences of @code{lib@var{ar}.a} for every @var{archive}
442 specified.
443
444 @cindex search directory, from cmd line
445 @kindex -L@var{dir}
446 @item -L@var{searchdir}
447 @itemx -L @var{searchdir}
448 Add path @var{searchdir} to the list of paths that @code{ld} will search
449 for archive libraries and @code{ld} control scripts. You may use this
450 option any number of times.
451
452 @ifset UsesEnvVars
453 The default set of paths searched (without being specified with
454 @samp{-L}) depends on which emulation mode @code{ld} is using, and in
455 some cases also on how it was configured. @xref{Environment}.
456 @end ifset
457
458 The paths can also be specified in a link script with the
459 @code{SEARCH_DIR} command.
460
461 @cindex link map
462 @kindex -M
463 @item -M
464 Print (to the standard output) a link map---diagnostic information
465 about where symbols are mapped by @code{ld}, and information on global
466 common storage allocation.
467
468 @cindex link map
469 @kindex -Map
470 @item -Map @var{mapfile}
471 Print to the file @var{mapfile} a link map---diagnostic information
472 about where symbols are mapped by @code{ld}, and information on global
473 common storage allocation.
474
475 @cindex emulation
476 @kindex -m @var{emulation}
477 @item -m@var{emulation}
478 @itemx -m @var{emulation}
479 Emulate the @var{emulation} linker. You can list the available
480 emulations with the @samp{--verbose} or @samp{-V} options. The default
481 depends on how your @code{ld} was configured.
482
483 @kindex -N
484 @cindex read/write from cmd line
485 @kindex OMAGIC
486 @item -N
487 Set the text and data sections to be readable and writable. Also, do
488 not page-align the data segment. If the output format supports Unix
489 style magic numbers, mark the output as @code{OMAGIC}.
490
491 @kindex -n
492 @cindex read-only text
493 @kindex NMAGIC
494 @item -n
495 Set the text segment to be read only, and mark the output as
496 @code{NMAGIC} if possible.
497
498 @item -noinhibit-exec
499 @cindex output file after errors
500 @kindex -noinhibit-exec
501 Retain the executable output file whenever it is still usable.
502 Normally, the linker will not produce an output file if it encounters
503 errors during the link process; it exits without writing an output file
504 when it issues any error whatsoever.
505
506 @item -no-keep-memory
507 @cindex memory usage
508 @kindex -no-keep-memory
509 @code{ld} normally optimizes for speed over memory usage by caching the
510 symbol tables of input files in memory. This option tells @code{ld} to
511 instead optimize for memory usage, by rereading the symbol tables as
512 necessary. This may be required if @code{ld} runs out of memory space
513 while linking a large executable.
514
515 @item -o @var{output}
516 @kindex -o @var{output}
517 @cindex naming the output file
518 Use @var{output} as the name for the program produced by @code{ld}; if this
519 option is not specified, the name @file{a.out} is used by default. The
520 script command @code{OUTPUT} can also specify the output file name.
521
522 @ifclear SingleFormat
523 @kindex -oformat
524 @item -oformat @var{output-format}
525 @code{ld} may be configured to support more than one kind of object
526 file. If your @code{ld} is configured this way, you can use the
527 @samp{-oformat} option to specify the binary format for the output
528 object file. Even when @code{ld} is configured to support alternative
529 object formats, you don't usually need to specify this, as @code{ld}
530 should be configured to produce as a default output format the most
531 usual format on each machine. @var{output-format} is a text string, the
532 name of a particular format supported by the BFD libraries. (You can
533 list the available binary formats with @samp{objdump -i}.) The script
534 command @code{OUTPUT_FORMAT} can also specify the output format, but
535 this option overrides it. @xref{BFD}.
536 @end ifclear
537
538 @item -R @var{filename}
539 @kindex -R @var{file}
540 @cindex symbol-only input
541 Read symbol names and their addresses from @var{filename}, but do not
542 relocate it or include it in the output. This allows your output file
543 to refer symbolically to absolute locations of memory defined in other
544 programs.
545
546 @kindex -relax
547 @cindex synthesizing linker
548 @cindex relaxing addressing modes
549 @item -relax
550 An option with machine dependent effects.
551 @ifset GENERIC
552 Currently this option is only supported on the H8/300 and the Intel 960.
553 @end ifset
554 @ifset H8300
555 @xref{H8/300,,@code{ld} and the H8/300}.
556 @end ifset
557 @ifset I960
558 @xref{i960,, @code{ld} and the Intel 960 family}.
559 @end ifset
560
561 On some platforms, the @samp{-relax} option performs global optimizations that
562 become possible when the linker resolves addressing in the program, such
563 as relaxing address modes and synthesizing new instructions in the
564 output object file.
565
566 @ifset GENERIC
567 On platforms where this is not supported, @samp{-relax} is accepted, but
568 ignored.
569 @end ifset
570
571 @item -retain-symbols-file @var{filename}
572 @cindex retaining specified symbols
573 @cindex stripping all but some symbols
574 @cindex symbols, retaining selectively
575 Retain @emph{only} the symbols listed in the file @var{filename},
576 discarding all others. @var{filename} is simply a flat file, with one
577 symbol name per line. This option is especially useful in environments
578 @ifset GENERIC
579 (such as VxWorks)
580 @end ifset
581 where a large global symbol table is accumulated gradually, to conserve
582 run-time memory.
583
584 @samp{-retain-symbols-file} does @emph{not} discard undefined symbols,
585 or symbols needed for relocations.
586
587 You may only specify @samp{-retain-symbols-file} once in the command
588 line. It overrides @samp{-s} and @samp{-S}.
589
590 @ifset GENERIC
591 @item -rpath @var{dir}
592 @cindex runtime library search path
593 @kindex -rpath
594 Add a directory to the runtime library search path. This is only
595 meaningful when linking an ELF executable with shared objects. All
596 -rpath arguments are concatenated and passed to the runtime linker,
597 which uses them to locate shared objects at runtime.
598 @end ifset
599
600 @cindex partial link
601 @cindex relocatable output
602 @kindex -r
603 @item -r
604 Generate relocatable output---i.e., generate an output file that can in
605 turn serve as input to @code{ld}. This is often called @dfn{partial
606 linking}. As a side effect, in environments that support standard Unix
607 magic numbers, this option also sets the output file's magic number to
608 @code{OMAGIC}.
609 @c ; see @code{-N}.
610 If this option is not specified, an absolute file is produced. When
611 linking C++ programs, this option @emph{will not} resolve references to
612 constructors; to do that, use @samp{-Ur}.
613
614 This option does the same thing as @samp{-i}.
615
616 @kindex -S
617 @cindex strip debugger symbols
618 @item -S
619 Omit debugger symbol information (but not all symbols) from the output file.
620
621 @kindex -s
622 @cindex strip all symbols
623 @item -s
624 Omit all symbol information from the output file.
625
626 @ifset GENERIC
627 @item -soname @var{name}
628 @cindex runtime library name
629 @kindex -soname
630 When creating an ELF shared object, set the internal DT_SONAME field to
631 the specified name. When an executable is linked with a shared object
632 which has a DT_SONAME field, then when the executable is run the dynamic
633 linker will attempt to load the shared object specified by the DT_SONAME
634 field rather than the using the file name given to the linker.
635 @end ifset
636
637 @item -shared
638 @cindex shared libraries
639 @kindex -shared
640 Create a shared library. This is currently only supported on ELF
641 platforms.
642
643 @item -sort-common
644 @kindex -sort-common
645 Normally, when @code{ld} places the global common symbols in the
646 appropriate output sections, it sorts them by size. First come all the
647 one byte symbols, then all the two bytes, then all the four bytes, and
648 then everything else. This is to prevent gaps between symbols due to
649 alignment constraints. This option disables that sorting.
650
651 @item -split-by-reloc @var{count}
652 @kindex split
653 Trys to creates extra sections in the output file so that no single output section
654 in the file contains more than @var{count} relocations. This
655 is useful when generating huge relocatable for downloading into
656 certain real time kernels with the COFF object file format; since
657 COFF cannot represent more than 65535 relocations in a single section.
658 Note that this will fail to work with object file formats which do not
659 support arbitrary sections. The linker will not split up individual input
660 sections for redistribution, so if a single input section contains
661 more than @var{count} relocations one output section will contain that
662 many relocations.
663
664 @item -split-by-file
665 @kindex split
666 Similar to -split-by-reloc but creates a new output section for each
667 input file.
668
669 @item -stats
670 Compute and display statistics about the operation of the linker,
671 such as execution time and memory usage.
672
673 @item -Tbss @var{org}
674 @kindex -Tbss @var{org}
675 @itemx -Tdata @var{org}
676 @kindex -Tdata @var{org}
677 @itemx -Ttext @var{org}
678 @kindex -Ttext @var{org}
679 @cindex segment origins, cmd line
680 Use @var{org} as the starting address for---respectively---the
681 @code{bss}, @code{data}, or the @code{text} segment of the output file.
682 @var{org} must be a single hexadecimal integer;
683 for compatibility with other linkers, you may omit the leading
684 @samp{0x} usually associated with hexadecimal values.
685
686 @item -T @var{commandfile}
687 @itemx -T@var{commandfile}
688 @kindex -T @var{script}
689 @cindex script files
690 Read link commands from the file @var{commandfile}. These commands
691 replace @code{ld}'s default link script (rather than adding
692 to it), so @var{commandfile} must specify everything necessary to describe
693 the target format. @xref{Commands}. If @var{commandfile} does not
694 exist, @code{ld} looks for it in the directories specified by any
695 preceding @samp{-L} options. Multiple @samp{-T} options accumulate.
696
697 @kindex -t
698 @cindex verbose
699 @cindex input files, displaying
700 @item -t
701 Print the names of the input files as @code{ld} processes them.
702
703 @kindex -traditional-format
704 @cindex traditional format
705 @item -traditional-format
706 For some targets, the output of @code{ld} is different in some ways from
707 the output of some existing linker. This switch requests @code{ld} to
708 use the traditional format instead.
709
710 @cindex dbx
711 For example, on SunOS, @code{ld} combines duplicate entries in the
712 symbol string table. This can reduce the size of an output file with
713 full debugging information by over 30 percent. Unfortunately, the SunOS
714 @code{dbx} program can not read the resulting program (@code{gdb} has no
715 trouble). The @samp{-traditional-format} switch tells @code{ld} to not
716 combine duplicate entries.
717
718 @item -u @var{symbol}
719 @kindex -u @var{symbol}
720 @cindex undefined symbol
721 Force @var{symbol} to be entered in the output file as an undefined symbol.
722 Doing this may, for example, trigger linking of additional modules from
723 standard libraries. @samp{-u} may be repeated with different option
724 arguments to enter additional undefined symbols.
725 @c Nice idea, but no such command: This option is equivalent
726 @c to the @code{EXTERN} linker command.
727
728 @kindex -Ur
729 @cindex constructors
730 @item -Ur
731 For anything other than C++ programs, this option is equivalent to
732 @samp{-r}: it generates relocatable output---i.e., an output file that can in
733 turn serve as input to @code{ld}. When linking C++ programs, @samp{-Ur}
734 @emph{does} resolve references to constructors, unlike @samp{-r}.
735 It does not work to use @samp{-Ur} on files that were themselves linked
736 with @samp{-Ur}; once the constructor table has been built, it cannot
737 be added to. Use @samp{-Ur} only for the last partial link, and
738 @samp{-r} for the others.
739
740 @kindex --verbose
741 @cindex version
742 @item --verbose
743 Display the version number for @code{ld} and list the linker emulations
744 supported. Display which input files can and cannot be opened.
745
746 @kindex -v
747 @kindex -V
748 @cindex version
749 @item -v
750 @itemx -V
751 Display the version number for @code{ld}. The @code{-V} option also
752 lists the supported emulations.
753
754 @item -version
755 @kindex -version
756 Display the version number for @code{ld} and exit.
757
758 @item -warn-common
759 @kindex -warn-comon
760 @cindex warnings, on combining symbols
761 @cindex combining symbols, warnings on
762 Warn when a common symbol is combined with another common symbol or with
763 a symbol definition. Unix linkers allow this somewhat sloppy practice,
764 but linkers on some other operating systems do not. This option allows
765 you to find potential problems from combining global symbols.
766 Unfortunately, some C libraries use this practice, so you may get some
767 warnings about symbols in the libraries as well as in your programs.
768
769 There are three kinds of global symbols, illustrated here by C examples:
770
771 @table @samp
772 @item int i = 1;
773 A definition, which goes in the initialized data section of the output
774 file.
775
776 @item extern int i;
777 An undefined reference, which does not allocate space.
778 There must be either a definition or a common symbol for the
779 variable somewhere.
780
781 @item int i;
782 A common symbol. If there are only (one or more) common symbols for a
783 variable, it goes in the uninitialized data area of the output file.
784 The linker merges multiple common symbols for the same variable into a
785 single symbol. If they are of different sizes, it picks the largest
786 size. The linker turns a common symbol into a declaration, if there is
787 a definition of the same variable.
788 @end table
789
790 The @samp{-warn-common} option can produce five kinds of warnings. Each
791 warning consists of a pair of lines: the first describes the symbol just
792 encountered, and the second describes the previous symbol encountered
793 with the same name. One or both of the two symbols will be a common
794 symbol.
795
796 @enumerate
797 @item
798 Turning a common symbol into a reference, because there is already a
799 definition for the symbol.
800 @smallexample
801 @var{file}(@var{section}): warning: common of `@var{symbol}'
802 overridden by definition
803 @var{file}(@var{section}): warning: defined here
804 @end smallexample
805
806 @item
807 Turning a common symbol into a reference, because a later definition for
808 the symbol is encountered. This is the same as the previous case,
809 except that the symbols are encountered in a different order.
810 @smallexample
811 @var{file}(@var{section}): warning: definition of `@var{symbol}'
812 overriding common
813 @var{file}(@var{section}): warning: common is here
814 @end smallexample
815
816 @item
817 Merging a common symbol with a previous same-sized common symbol.
818 @smallexample
819 @var{file}(@var{section}): warning: multiple common
820 of `@var{symbol}'
821 @var{file}(@var{section}): warning: previous common is here
822 @end smallexample
823
824 @item
825 Merging a common symbol with a previous larger common symbol.
826 @smallexample
827 @var{file}(@var{section}): warning: common of `@var{symbol}'
828 overridden by larger common
829 @var{file}(@var{section}): warning: larger common is here
830 @end smallexample
831
832 @item
833 Merging a common symbol with a previous smaller common symbol. This is
834 the same as the previous case, except that the symbols are
835 encountered in a different order.
836 @smallexample
837 @var{file}(@var{section}): warning: common of `@var{symbol}'
838 overriding smaller common
839 @var{file}(@var{section}): warning: smaller common is here
840 @end smallexample
841 @end enumerate
842
843 @kindex -warn-once
844 @cindex warnings, on undefined symbols
845 @cindex undefined symbols, warnings on
846 @item -warn-once
847 Only warn once for each undefined symbol, rather than once per module
848 which refers to it.
849
850 @kindex --whole-archive
851 @cindex including an entire archive
852 For each archive mentioned on the command line, include every object
853 file in the archive in the link, rather than searching the archive for
854 the required object files. This is normally used to turn an archive
855 file into a shared library, forcing every object to be included in the
856 resulting shared library.
857
858 @kindex -X
859 @cindex local symbols, deleting
860 @cindex L, deleting symbols beginning
861 @item -X
862 Delete all temporary local symbols. For most targets, this is all local
863 symbols whose names begin with @samp{L}.
864
865 @kindex -x
866 @cindex deleting local symbols
867 @item -x
868 Delete all local symbols.
869
870 @item -y @var{symbol}
871 @kindex -y @var{symbol}
872 @cindex symbol tracing
873 Print the name of each linked file in which @var{symbol} appears. This
874 option may be given any number of times. On many systems it is necessary
875 to prepend an underscore.
876
877 This option is useful when you have an undefined symbol in your link but
878 don't know where the reference is coming from.
879
880 @item -( @var{archives} -)
881 @itemx --start-group @var{archives} --end-group
882 @kindex -(
883 @cindex groups of archives
884 The @var{archives} should be a list of archive files. They may be
885 either explicit file names, or @samp{-l} options.
886
887 The specified archives are searched repeatedly until no new undefined
888 references are created. Normally, an archive is searched only once in
889 the order that it is specified on the command line. If a symbol in that
890 archive is needed to resolve an undefined symbol referred to by an
891 object in an archive that appears later on the command line, the linker
892 would not be able to resolve that reference. By grouping the archives,
893 they all be searched repeatedly until all possible references are
894 resolved.
895
896 Using this option has a significant performance cost. It is best to use
897 it only when there are unavoidable circular references between two or
898 more archives.
899 @end table
900
901 @ifset UsesEnvVars
902 @node Environment
903 @section Environment Variables
904
905 You can change the behavior of @code{ld} with the environment
906 variable @code{GNUTARGET}.
907
908 @kindex GNUTARGET
909 @cindex default input format
910 @code{GNUTARGET} determines the input-file object format if you don't
911 use @samp{-b} (or its synonym @samp{-format}). Its value should be one
912 of the BFD names for an input format (@pxref{BFD}). If there is no
913 @code{GNUTARGET} in the environment, @code{ld} uses the natural format
914 of the target. If @code{GNUTARGET} is set to @code{default} then BFD attempts to discover the
915 input format by examining binary input files; this method often
916 succeeds, but there are potential ambiguities, since there is no method
917 of ensuring that the magic number used to specify object-file formats is
918 unique. However, the configuration procedure for BFD on each system
919 places the conventional format for that system first in the search-list,
920 so ambiguities are resolved in favor of convention.
921 @end ifset
922
923 @node Commands
924 @chapter Command Language
925
926 @cindex command files
927 The command language provides explicit control over the link process,
928 allowing complete specification of the mapping between the linker's
929 input files and its output. It controls:
930 @itemize @bullet
931 @item
932 input files
933 @item
934 file formats
935 @item
936 output file layout
937 @item
938 addresses of sections
939 @item
940 placement of common blocks
941 @end itemize
942
943 You may supply a command file (also known as a link script) to the
944 linker either explicitly through the @samp{-T} option, or implicitly as
945 an ordinary file. If the linker opens a file which it cannot recognize
946 as a supported object or archive format, it reports an error.
947
948 @menu
949 * Scripts:: Linker Scripts
950 * Expressions:: Expressions
951 * MEMORY:: MEMORY Command
952 * SECTIONS:: SECTIONS Command
953 * Entry Point:: The Entry Point
954 * Option Commands:: Option Commands
955 @end menu
956
957 @node Scripts
958 @section Linker Scripts
959 The @code{ld} command language is a collection of statements; some are
960 simple keywords setting a particular option, some are used to select and
961 group input files or name output files; and two statement
962 types have a fundamental and pervasive impact on the linking process.
963
964 @cindex fundamental script commands
965 @cindex commands, fundamental
966 @cindex output file layout
967 @cindex layout of output file
968 The most fundamental command of the @code{ld} command language is the
969 @code{SECTIONS} command (@pxref{SECTIONS}). Every meaningful command
970 script must have a @code{SECTIONS} command: it specifies a
971 ``picture'' of the output file's layout, in varying degrees of detail.
972 No other command is required in all cases.
973
974 The @code{MEMORY} command complements @code{SECTIONS} by describing the
975 available memory in the target architecture. This command is optional;
976 if you don't use a @code{MEMORY} command, @code{ld} assumes sufficient
977 memory is available in a contiguous block for all output.
978 @xref{MEMORY}.
979
980 @cindex comments
981 You may include comments in linker scripts just as in C: delimited
982 by @samp{/*} and @samp{*/}. As in C, comments are syntactically
983 equivalent to whitespace.
984
985 @node Expressions
986 @section Expressions
987 @cindex expression syntax
988 @cindex arithmetic
989 Many useful commands involve arithmetic expressions. The syntax for
990 expressions in the command language is identical to that of C
991 expressions, with the following features:
992 @itemize @bullet
993 @item
994 All expressions evaluated as integers and
995 are of ``long'' or ``unsigned long'' type.
996 @item
997 All constants are integers.
998 @item
999 All of the C arithmetic operators are provided.
1000 @item
1001 You may reference, define, and create global variables.
1002 @item
1003 You may call special purpose built-in functions.
1004 @end itemize
1005
1006 @menu
1007 * Integers:: Integers
1008 * Symbols:: Symbol Names
1009 * Location Counter:: The Location Counter
1010 * Operators:: Operators
1011 * Evaluation:: Evaluation
1012 * Assignment:: Assignment: Defining Symbols
1013 * Arithmetic Functions:: Built-In Functions
1014 @end menu
1015
1016 @node Integers
1017 @subsection Integers
1018 @cindex integer notation
1019 @cindex octal integers
1020 An octal integer is @samp{0} followed by zero or more of the octal
1021 digits (@samp{01234567}).
1022 @example
1023 _as_octal = 0157255;
1024 @end example
1025
1026 @cindex decimal integers
1027 A decimal integer starts with a non-zero digit followed by zero or
1028 more digits (@samp{0123456789}).
1029 @example
1030 _as_decimal = 57005;
1031 @end example
1032
1033 @cindex hexadecimal integers
1034 @kindex 0x
1035 A hexadecimal integer is @samp{0x} or @samp{0X} followed by one or
1036 more hexadecimal digits chosen from @samp{0123456789abcdefABCDEF}.
1037 @example
1038 _as_hex = 0xdead;
1039 @end example
1040
1041 @cindex negative integers
1042 To write a negative integer, use
1043 the prefix operator @samp{-}; @pxref{Operators}.
1044 @example
1045 _as_neg = -57005;
1046 @end example
1047
1048 @cindex scaled integers
1049 @cindex K and M integer suffixes
1050 @cindex M and K integer suffixes
1051 @cindex suffixes for integers
1052 @cindex integer suffixes
1053 Additionally the suffixes @code{K} and @code{M} may be used to scale a
1054 constant by
1055 @c TEXI2ROFF-KILL
1056 @ifinfo
1057 @c END TEXI2ROFF-KILL
1058 @code{1024} or @code{1024*1024}
1059 @c TEXI2ROFF-KILL
1060 @end ifinfo
1061 @tex
1062 ${\rm 1024}$ or ${\rm 1024}^2$
1063 @end tex
1064 @c END TEXI2ROFF-KILL
1065 respectively. For example, the following all refer to the same quantity:
1066
1067 @example
1068 _fourk_1 = 4K;
1069 _fourk_2 = 4096;
1070 _fourk_3 = 0x1000;
1071 @end example
1072
1073 @node Symbols
1074 @subsection Symbol Names
1075 @cindex symbol names
1076 @cindex names
1077 @cindex quoted symbol names
1078 @kindex "
1079 Unless quoted, symbol names start with a letter, underscore, or point
1080 and may include any letters, underscores, digits, points,
1081 and hyphens. Unquoted symbol names must not conflict with any
1082 keywords. You can specify a symbol which contains odd characters or has
1083 the same name as a keyword, by surrounding the symbol name in double quotes:
1084 @example
1085 "SECTION" = 9;
1086 "with a space" = "also with a space" + 10;
1087 @end example
1088
1089 Since symbols can contain many non-alphabetic characters, it is safest
1090 to delimit symbols with spaces. For example, @samp{A-B} is one symbol,
1091 whereas @samp{A - B} is an expression involving subtraction.
1092
1093 @node Location Counter
1094 @subsection The Location Counter
1095 @kindex .
1096 @cindex dot
1097 @cindex location counter
1098 @cindex current output location
1099 The special linker variable @dfn{dot} @samp{.} always contains the
1100 current output location counter. Since the @code{.} always refers to
1101 a location in an output section, it must always appear in an
1102 expression within a @code{SECTIONS} command. The @code{.} symbol
1103 may appear anywhere that an ordinary symbol is allowed in an
1104 expression, but its assignments have a side effect. Assigning a value
1105 to the @code{.} symbol will cause the location counter to be moved.
1106 @cindex holes
1107 This may be used to create holes in the output section. The location
1108 counter may never be moved backwards.
1109 @example
1110 SECTIONS
1111 @{
1112 output :
1113 @{
1114 file1(.text)
1115 . = . + 1000;
1116 file2(.text)
1117 . += 1000;
1118 file3(.text)
1119 @} = 0x1234;
1120 @}
1121 @end example
1122 @noindent
1123 In the previous example, @code{file1} is located at the beginning of the
1124 output section, then there is a 1000 byte gap. Then @code{file2}
1125 appears, also with a 1000 byte gap following before @code{file3} is
1126 loaded. The notation @samp{= 0x1234} specifies what data to write in
1127 the gaps (@pxref{Section Options}).
1128
1129 @iftex
1130 @vfill
1131 @end iftex
1132
1133 @need 5000
1134 @node Operators
1135 @subsection Operators
1136 @cindex Operators for arithmetic
1137 @cindex arithmetic operators
1138 @cindex precedence in expressions
1139 The linker recognizes the standard C set of arithmetic operators, with
1140 the standard bindings and precedence levels:
1141 @c TEXI2ROFF-KILL
1142 @ifinfo
1143 @c END TEXI2ROFF-KILL
1144 @example
1145 precedence associativity Operators Notes
1146 (highest)
1147 1 left ! - ~ (1)
1148 2 left * / %
1149 3 left + -
1150 4 left >> <<
1151 5 left == != > < <= >=
1152 6 left &
1153 7 left |
1154 8 left &&
1155 9 left ||
1156 10 right ? :
1157 11 right &= += -= *= /= (2)
1158 (lowest)
1159 @end example
1160 Notes:
1161 (1) Prefix operators
1162 (2) @xref{Assignment}
1163 @c TEXI2ROFF-KILL
1164 @end ifinfo
1165 @tex
1166 \vskip \baselineskip
1167 %"lispnarrowing" is the extra indent used generally for @example
1168 \hskip\lispnarrowing\vbox{\offinterlineskip
1169 \hrule
1170 \halign
1171 {\vrule#&\strut\hfil\ #\ \hfil&\vrule#&\strut\hfil\ #\ \hfil&\vrule#&\strut\hfil\ {\tt #}\ \hfil&\vrule#\cr
1172 height2pt&\omit&&\omit&&\omit&\cr
1173 &Precedence&& Associativity &&{\rm Operators}&\cr
1174 height2pt&\omit&&\omit&&\omit&\cr
1175 \noalign{\hrule}
1176 height2pt&\omit&&\omit&&\omit&\cr
1177 &highest&&&&&\cr
1178 % '176 is tilde, '~' in tt font
1179 &1&&left&&\qquad- \char'176\ !\qquad\dag&\cr
1180 &2&&left&&* / \%&\cr
1181 &3&&left&&+ -&\cr
1182 &4&&left&&>> <<&\cr
1183 &5&&left&&== != > < <= >=&\cr
1184 &6&&left&&\&&\cr
1185 &7&&left&&|&\cr
1186 &8&&left&&{\&\&}&\cr
1187 &9&&left&&||&\cr
1188 &10&&right&&? :&\cr
1189 &11&&right&&\qquad\&= += -= *= /=\qquad\ddag&\cr
1190 &lowest&&&&&\cr
1191 height2pt&\omit&&\omit&&\omit&\cr}
1192 \hrule}
1193 @end tex
1194 @iftex
1195 {
1196 @obeylines@parskip=0pt@parindent=0pt
1197 @dag@quad Prefix operators.
1198 @ddag@quad @xref{Assignment}.
1199 }
1200 @end iftex
1201 @c END TEXI2ROFF-KILL
1202
1203 @node Evaluation
1204 @subsection Evaluation
1205
1206 @cindex lazy evaluation
1207 @cindex expression evaluation order
1208 The linker uses ``lazy evaluation'' for expressions; it only calculates
1209 an expression when absolutely necessary. The linker needs the value of
1210 the start address, and the lengths of memory regions, in order to do any
1211 linking at all; these values are computed as soon as possible when the
1212 linker reads in the command file. However, other values (such as symbol
1213 values) are not known or needed until after storage allocation. Such
1214 values are evaluated later, when other information (such as the sizes of
1215 output sections) is available for use in the symbol assignment
1216 expression.
1217
1218 @node Assignment
1219 @subsection Assignment: Defining Symbols
1220 @cindex assignment in scripts
1221 @cindex symbol definition, scripts
1222 @cindex variables, defining
1223 You may create global symbols, and assign values (addresses) to global
1224 symbols, using any of the C assignment operators:
1225
1226 @table @code
1227 @item @var{symbol} = @var{expression} ;
1228 @itemx @var{symbol} &= @var{expression} ;
1229 @itemx @var{symbol} += @var{expression} ;
1230 @itemx @var{symbol} -= @var{expression} ;
1231 @itemx @var{symbol} *= @var{expression} ;
1232 @itemx @var{symbol} /= @var{expression} ;
1233 @end table
1234
1235 Two things distinguish assignment from other operators in @code{ld}
1236 expressions.
1237 @itemize @bullet
1238 @item
1239 Assignment may only be used at the root of an expression;
1240 @samp{a=b+3;} is allowed, but @samp{a+b=3;} is an error.
1241
1242 @kindex ;
1243 @cindex semicolon
1244 @item
1245 You must place a trailing semicolon (``@key{;}'') at the end of an
1246 assignment statement.
1247 @end itemize
1248
1249 Assignment statements may appear:
1250 @itemize @bullet
1251 @item
1252 as commands in their own right in an @code{ld} script; or
1253 @item
1254 as independent statements within a @code{SECTIONS} command; or
1255 @item
1256 as part of the contents of a section definition in a
1257 @code{SECTIONS} command.
1258 @end itemize
1259
1260 The first two cases are equivalent in effect---both define a symbol with
1261 an absolute address. The last case defines a symbol whose address is
1262 relative to a particular section (@pxref{SECTIONS}).
1263
1264 @cindex absolute and relocatable symbols
1265 @cindex relocatable and absolute symbols
1266 @cindex symbols, relocatable and absolute
1267 When a linker expression is evaluated and assigned to a variable, it is
1268 given either an absolute or a relocatable type. An absolute expression
1269 type is one in which the symbol contains the value that it will have in
1270 the output file; a relocatable expression type is one in which the
1271 value is expressed as a fixed offset from the base of a section.
1272
1273 The type of the expression is controlled by its position in the script
1274 file. A symbol assigned within a section definition is created relative
1275 to the base of the section; a symbol assigned in any other place is
1276 created as an absolute symbol. Since a symbol created within a
1277 section definition is relative to the base of the section, it
1278 will remain relocatable if relocatable output is requested. A symbol
1279 may be created with an absolute value even when assigned to within a
1280 section definition by using the absolute assignment function
1281 @code{ABSOLUTE}. For example, to create an absolute symbol whose address
1282 is the last byte of an output section named @code{.data}:
1283 @example
1284 SECTIONS@{ @dots{}
1285 .data :
1286 @{
1287 *(.data)
1288 _edata = ABSOLUTE(.) ;
1289 @}
1290 @dots{} @}
1291 @end example
1292
1293 The linker tries to put off the evaluation of an assignment until all
1294 the terms in the source expression are known (@pxref{Evaluation}). For
1295 instance, the sizes of sections cannot be known until after allocation,
1296 so assignments dependent upon these are not performed until after
1297 allocation. Some expressions, such as those depending upon the location
1298 counter @dfn{dot}, @samp{.} must be evaluated during allocation. If the
1299 result of an expression is required, but the value is not available,
1300 then an error results. For example, a script like the following
1301 @example
1302 SECTIONS @{ @dots{}
1303 text 9+this_isnt_constant :
1304 @{ @dots{}
1305 @}
1306 @dots{} @}
1307 @end example
1308 @kindex Non constant expression
1309 @noindent
1310 will cause the error message ``@code{Non constant expression for initial
1311 address}''.
1312
1313 @cindex provide
1314 In some cases, it is desirable for a linker script to define a symbol
1315 only if it is referenced, and only if it is not defined by any object
1316 included in the link. For example, traditional linkers defined the
1317 symbol @samp{etext}. However, ANSI C requires that the user be able to
1318 use @samp{etext} as a function name without encountering an error.
1319 The @code{PROVIDE} keyword may be used to define a symbol, such as
1320 @samp{etext}, only if it is referenced but not defined. The syntax is
1321 @code{PROVIDE(@var{symbol} = @var{expression})}.
1322
1323 @node Arithmetic Functions
1324 @subsection Arithmetic Functions
1325 @cindex functions in expression language
1326 The command language includes a number of built-in
1327 functions for use in link script expressions.
1328 @table @code
1329 @item ABSOLUTE(@var{exp})
1330 @kindex ABSOLUTE(@var{exp})
1331 @cindex expression, absolute
1332 Return the absolute (non-relocatable, as opposed to non-negative) value
1333 of the expression @var{exp}. Primarily useful to assign an absolute
1334 value to a symbol within a section definition, where symbol values are
1335 normally section-relative.
1336
1337 @item ADDR(@var{section})
1338 @kindex ADDR(@var{section})
1339 @cindex section address
1340 Return the absolute address of the named @var{section}. Your script must
1341 previously have defined the location of that section. In the following
1342 example, @code{symbol_1} and @code{symbol_2} are assigned identical
1343 values:
1344 @example
1345 SECTIONS@{ @dots{}
1346 .output1 :
1347 @{
1348 start_of_output_1 = ABSOLUTE(.);
1349 @dots{}
1350 @}
1351 .output :
1352 @{
1353 symbol_1 = ADDR(.output1);
1354 symbol_2 = start_of_output_1;
1355 @}
1356 @dots{} @}
1357 @end example
1358
1359 @item ALIGN(@var{exp})
1360 @kindex ALIGN(@var{exp})
1361 @cindex rounding up location counter
1362 Return the result of the current location counter (@code{.}) aligned to
1363 the next @var{exp} boundary. @var{exp} must be an expression whose
1364 value is a power of two. This is equivalent to
1365 @example
1366 (. + @var{exp} - 1) & ~(@var{exp} - 1)
1367 @end example
1368
1369 @code{ALIGN} doesn't change the value of the location counter---it just
1370 does arithmetic on it. As an example, to align the output @code{.data}
1371 section to the next @code{0x2000} byte boundary after the preceding
1372 section and to set a variable within the section to the next
1373 @code{0x8000} boundary after the input sections:
1374 @example
1375 SECTIONS@{ @dots{}
1376 .data ALIGN(0x2000): @{
1377 *(.data)
1378 variable = ALIGN(0x8000);
1379 @}
1380 @dots{} @}
1381 @end example
1382 @noindent
1383 The first use of @code{ALIGN} in this example specifies the location of
1384 a section because it is used as the optional @var{start} attribute of a
1385 section definition (@pxref{Section Options}). The second use simply
1386 defines the value of a variable.
1387
1388 The built-in @code{NEXT} is closely related to @code{ALIGN}.
1389
1390 @item DEFINED(@var{symbol})
1391 @kindex DEFINED(@var{symbol})
1392 @cindex symbol defaults
1393 Return 1 if @var{symbol} is in the linker global symbol table and is
1394 defined, otherwise return 0. You can use this function to provide default
1395 values for symbols. For example, the following command-file fragment shows how
1396 to set a global symbol @code{begin} to the first location in the
1397 @code{.text} section---but if a symbol called @code{begin} already
1398 existed, its value is preserved:
1399
1400 @smallexample
1401 SECTIONS@{ @dots{}
1402 .text : @{
1403 begin = DEFINED(begin) ? begin : . ;
1404 @dots{}
1405 @}
1406 @dots{} @}
1407 @end smallexample
1408
1409 @item NEXT(@var{exp})
1410 @kindex NEXT(@var{exp})
1411 @cindex unallocated address, next
1412 Return the next unallocated address that is a multiple of @var{exp}.
1413 This function is closely related to @code{ALIGN(@var{exp})}; unless you
1414 use the @code{MEMORY} command to define discontinuous memory for the
1415 output file, the two functions are equivalent.
1416
1417 @item SIZEOF(@var{section})
1418 @kindex SIZEOF(@var{section})
1419 @cindex section size
1420 Return the size in bytes of the named @var{section}, if that section has
1421 been allocated. In the following example, @code{symbol_1} and
1422 @code{symbol_2} are assigned identical values:
1423 @c What does it return if the section hasn't been allocated? 0?
1424 @example
1425 SECTIONS@{ @dots{}
1426 .output @{
1427 .start = . ;
1428 @dots{}
1429 .end = . ;
1430 @}
1431 symbol_1 = .end - .start ;
1432 symbol_2 = SIZEOF(.output);
1433 @dots{} @}
1434
1435 @end example
1436
1437 @item SIZEOF_HEADERS
1438 @kindex SIZEOF_HEADERS
1439 @cindex header size
1440 @itemx sizeof_headers
1441 @kindex sizeof_headers
1442 Return the size in bytes of the output file's headers. You can use this number
1443 as the start address of the first section, if you choose, to facilitate
1444 paging.
1445
1446 @end table
1447
1448 @node MEMORY
1449 @section Memory Layout
1450 @kindex MEMORY
1451 @cindex regions of memory
1452 @cindex discontinuous memory
1453 @cindex allocating memory
1454 The linker's default configuration permits allocation of all available memory.
1455 You can override this configuration by using the @code{MEMORY} command. The
1456 @code{MEMORY} command describes the location and size of blocks of
1457 memory in the target. By using it carefully, you can describe which
1458 memory regions may be used by the linker, and which memory regions it
1459 must avoid. The linker does not shuffle sections to fit into the
1460 available regions, but does move the requested sections into the correct
1461 regions and issue errors when the regions become too full.
1462
1463 A command file may contain at most one use of the @code{MEMORY}
1464 command; however, you can define as many blocks of memory within it as
1465 you wish. The syntax is:
1466
1467 @example
1468 MEMORY
1469 @{
1470 @var{name} (@var{attr}) : ORIGIN = @var{origin}, LENGTH = @var{len}
1471 @dots{}
1472 @}
1473 @end example
1474 @table @code
1475 @cindex naming memory regions
1476 @item @var{name}
1477 is a name used internally by the linker to refer to the region. Any
1478 symbol name may be used. The region names are stored in a separate
1479 name space, and will not conflict with symbols, file names or section
1480 names. Use distinct names to specify multiple regions.
1481
1482 @cindex memory region attributes
1483 @item (@var{attr})
1484 is an optional list of attributes, permitted for compatibility with the
1485 AT&T linker but not used by @code{ld} beyond checking that the
1486 attribute list is valid. Valid attribute lists must be made up of the
1487 characters ``@code{LIRWX}''. If you omit the attribute list, you may
1488 omit the parentheses around it as well.
1489
1490 @kindex ORIGIN =
1491 @kindex o =
1492 @kindex org =
1493 @item @var{origin}
1494 is the start address of the region in physical memory. It is
1495 an expression that must evaluate to a constant before
1496 memory allocation is performed. The keyword @code{ORIGIN} may be
1497 abbreviated to @code{org} or @code{o} (but not, for example, @samp{ORG}).
1498
1499 @kindex LENGTH =
1500 @kindex len =
1501 @kindex l =
1502 @item @var{len}
1503 is the size in bytes of the region (an expression).
1504 The keyword @code{LENGTH} may be abbreviated to @code{len} or @code{l}.
1505 @end table
1506
1507 For example, to specify that memory has two regions available for
1508 allocation---one starting at 0 for 256 kilobytes, and the other
1509 starting at @code{0x40000000} for four megabytes:
1510
1511 @example
1512 MEMORY
1513 @{
1514 rom : ORIGIN = 0, LENGTH = 256K
1515 ram : org = 0x40000000, l = 4M
1516 @}
1517 @end example
1518
1519 Once you have defined a region of memory named @var{mem}, you can direct
1520 specific output sections there by using a command ending in
1521 @samp{>@var{mem}} within the @code{SECTIONS} command (@pxref{Section
1522 Options}). If the combined output sections directed to a region are too
1523 big for the region, the linker will issue an error message.
1524
1525 @node SECTIONS
1526 @section Specifying Output Sections
1527
1528 @kindex SECTIONS
1529 The @code{SECTIONS} command controls exactly where input sections are
1530 placed into output sections, their order in the output file, and to
1531 which output sections they are allocated.
1532
1533 You may use at most one @code{SECTIONS} command in a script file,
1534 but you can have as many statements within it as you wish. Statements
1535 within the @code{SECTIONS} command can do one of three things:
1536
1537 @itemize @bullet
1538 @item
1539 define the entry point;
1540
1541 @item
1542 assign a value to a symbol;
1543
1544 @item
1545 describe the placement of a named output section, and which input
1546 sections go into it.
1547 @end itemize
1548
1549 You can also use the first two operations---defining the entry point and
1550 defining symbols---outside the @code{SECTIONS} command: @pxref{Entry
1551 Point}, and @pxref{Assignment}. They are permitted here as well for
1552 your convenience in reading the script, so that symbols and the entry
1553 point can be defined at meaningful points in your output-file layout.
1554
1555 If you do not use a @code{SECTIONS} command, the linker places each input
1556 section into an identically named output section in the order that the
1557 sections are first encountered in the input files. If all input sections
1558 are present in the first file, for example, the order of sections in the
1559 output file will match the order in the first input file.
1560
1561 @menu
1562 * Section Definition:: Section Definitions
1563 * Section Placement:: Section Placement
1564 * Section Data Expressions:: Section Data Expressions
1565 * Section Options:: Optional Section Attributes
1566 @end menu
1567
1568 @node Section Definition
1569 @subsection Section Definitions
1570 @cindex section definition
1571 The most frequently used statement in the @code{SECTIONS} command is
1572 the @dfn{section definition}, which specifies the
1573 properties of an output section: its location, alignment, contents,
1574 fill pattern, and target memory region. Most of
1575 these specifications are optional; the simplest form of a section
1576 definition is
1577 @example
1578 SECTIONS @{ @dots{}
1579 @var{secname} : @{
1580 @var{contents}
1581 @}
1582 @dots{} @}
1583 @end example
1584 @cindex naming output sections
1585 @noindent
1586 @var{secname} is the name of the output section, and @var{contents} a
1587 specification of what goes there---for example, a list of input files or
1588 sections of input files (@pxref{Section Placement}). As you might
1589 assume, the whitespace shown is optional. You do need the colon
1590 @samp{:} and the braces @samp{@{@}}, however.
1591
1592 @var{secname} must meet the constraints of your output format. In
1593 formats which only support a limited number of sections, such as
1594 @code{a.out}, the name must be one of the names supported by the format
1595 (@code{a.out}, for example, allows only @code{.text}, @code{.data} or
1596 @code{.bss}). If the output format supports any number of sections, but
1597 with numbers and not names (as is the case for Oasys), the name should be
1598 supplied as a quoted numeric string. A section name may consist of any
1599 sequence of characters, but any name which does not conform to the standard
1600 @code{ld} symbol name syntax must be quoted.
1601 @xref{Symbols, , Symbol Names}.
1602
1603 The linker will not create output sections which do not have any
1604 contents. This is for convenience when referring to input sections that
1605 may or may not exist. For example,
1606 @example
1607 .foo @{ *(.foo @}
1608 @end example
1609 will only create a @samp{.foo} section in the output file if there is a
1610 @samp{.foo} section in at least one input file.
1611
1612 @node Section Placement
1613 @subsection Section Placement
1614
1615 @cindex contents of a section
1616 In a section definition, you can specify the contents of an output
1617 section by listing particular input files, by listing particular
1618 input-file sections, or by a combination of the two. You can also place
1619 arbitrary data in the section, and define symbols relative to the
1620 beginning of the section.
1621
1622 The @var{contents} of a section definition may include any of the
1623 following kinds of statement. You can include as many of these as you
1624 like in a single section definition, separated from one another by
1625 whitespace.
1626
1627 @table @code
1628 @kindex @var{filename}
1629 @cindex input files, section defn
1630 @cindex files, including in output sections
1631 @item @var{filename}
1632 You may simply name a particular input file to be placed in the current
1633 output section; @emph{all} sections from that file are placed in the
1634 current section definition. If the file name has already been mentioned
1635 in another section definition, with an explicit section name list, then
1636 only those sections which have not yet been allocated are used.
1637
1638 To specify a list of particular files by name:
1639 @example
1640 .data : @{ afile.o bfile.o cfile.o @}
1641 @end example
1642 @noindent
1643 The example also illustrates that multiple statements can be included in
1644 the contents of a section definition, since each file name is a separate
1645 statement.
1646
1647 @kindex @var{filename}(@var{section})
1648 @cindex files and sections, section defn
1649 @item @var{filename}( @var{section} )
1650 @itemx @var{filename}( @var{section}, @var{section}, @dots{} )
1651 @itemx @var{filename}( @var{section} @var{section} @dots{} )
1652 You can name one or more sections from your input files, for
1653 insertion in the current output section. If you wish to specify a list
1654 of input-file sections inside the parentheses, you may separate the
1655 section names by either commas or whitespace.
1656
1657 @cindex input sections to output section
1658 @kindex *(@var{section})
1659 @item * (@var{section})
1660 @itemx * (@var{section}, @var{section}, @dots{})
1661 @itemx * (@var{section} @var{section} @dots{})
1662 Instead of explicitly naming particular input files in a link control
1663 script, you can refer to @emph{all} files from the @code{ld} command
1664 line: use @samp{*} instead of a particular file name before the
1665 parenthesized input-file section list.
1666
1667 If you have already explicitly included some files by name, @samp{*}
1668 refers to all @emph{remaining} files---those whose places in the output
1669 file have not yet been defined.
1670
1671 For example, to copy sections @code{1} through @code{4} from an Oasys file
1672 into the @code{.text} section of an @code{a.out} file, and sections @code{13}
1673 and @code{14} into the @code{.data} section:
1674 @example
1675 SECTIONS @{
1676 .text :@{
1677 *("1" "2" "3" "4")
1678 @}
1679
1680 .data :@{
1681 *("13" "14")
1682 @}
1683 @}
1684 @end example
1685
1686 @cindex @code{[@var{section}@dots{}]}, not supported
1687 @samp{[ @var{section} @dots{} ]} used to be accepted as an alternate way
1688 to specify named sections from all unallocated input files. Because
1689 some operating systems (VMS) allow brackets in file names, that notation
1690 is no longer supported.
1691
1692 @cindex uninitialized data
1693 @cindex commons in output
1694 @kindex *( COMMON )
1695 @item @var{filename}@code{( COMMON )}
1696 @itemx *( COMMON )
1697 Specify where in your output file to place uninitialized data
1698 with this notation. @code{*(COMMON)} by itself refers to all
1699 uninitialized data from all input files (so far as it is not yet
1700 allocated); @var{filename}@code{(COMMON)} refers to uninitialized data
1701 from a particular file. Both are special cases of the general
1702 mechanisms for specifying where to place input-file sections:
1703 @code{ld} permits you to refer to uninitialized data as if it
1704 were in an input-file section named @code{COMMON}, regardless of the
1705 input file's format.
1706 @end table
1707
1708 For example, the following command script arranges the output file into
1709 three consecutive sections, named @code{.text}, @code{.data}, and
1710 @code{.bss}, taking the input for each from the correspondingly named
1711 sections of all the input files:
1712
1713 @example
1714 SECTIONS @{
1715 .text : @{ *(.text) @}
1716 .data : @{ *(.data) @}
1717 .bss : @{ *(.bss) *(COMMON) @}
1718 @}
1719 @end example
1720
1721 The following example reads all of the sections from file @code{all.o}
1722 and places them at the start of output section @code{outputa} which
1723 starts at location @code{0x10000}. All of section @code{.input1} from
1724 file @code{foo.o} follows immediately, in the same output section. All
1725 of section @code{.input2} from @code{foo.o} goes into output section
1726 @code{outputb}, followed by section @code{.input1} from @code{foo1.o}.
1727 All of the remaining @code{.input1} and @code{.input2} sections from any
1728 files are written to output section @code{outputc}.
1729
1730 @example
1731 SECTIONS @{
1732 outputa 0x10000 :
1733 @{
1734 all.o
1735 foo.o (.input1)
1736 @}
1737 outputb :
1738 @{
1739 foo.o (.input2)
1740 foo1.o (.input1)
1741 @}
1742 outputc :
1743 @{
1744 *(.input1)
1745 *(.input2)
1746 @}
1747 @}
1748 @end example
1749
1750 @node Section Data Expressions
1751 @subsection Section Data Expressions
1752
1753 @cindex expressions in a section
1754 The foregoing statements arrange, in your output file, data originating
1755 from your input files. You can also place data directly in an output
1756 section from the link command script. Most of these additional
1757 statements involve expressions; @pxref{Expressions}. Although these
1758 statements are shown separately here for ease of presentation, no such
1759 segregation is needed within a section definition in the @code{SECTIONS}
1760 command; you can intermix them freely with any of the statements we've
1761 just described.
1762
1763 @table @code
1764 @cindex input filename symbols
1765 @cindex filename symbols
1766 @kindex CREATE_OBJECT_SYMBOLS
1767 @item CREATE_OBJECT_SYMBOLS
1768 Create a symbol for each input file
1769 in the current section, set to the address of the first byte of
1770 data written from that input file. For instance, with @code{a.out}
1771 files it is conventional to have a symbol for each input file. You can
1772 accomplish this by defining the output @code{.text} section as follows:
1773 @example
1774 SECTIONS @{
1775 .text 0x2020 :
1776 @{
1777 CREATE_OBJECT_SYMBOLS
1778 *(.text)
1779 _etext = ALIGN(0x2000);
1780 @}
1781 @dots{}
1782 @}
1783 @end example
1784
1785 If @code{sample.ld} is a file containing this script, and @code{a.o},
1786 @code{b.o}, @code{c.o}, and @code{d.o} are four input files with
1787 contents like the following---
1788 @example
1789 /* a.c */
1790
1791 afunction() @{ @}
1792 int adata=1;
1793 int abss;
1794 @end example
1795
1796 @noindent
1797 @samp{ld -M -T sample.ld a.o b.o c.o d.o} would create a map like this,
1798 containing symbols matching the object file names:
1799 @example
1800 00000000 A __DYNAMIC
1801 00004020 B _abss
1802 00004000 D _adata
1803 00002020 T _afunction
1804 00004024 B _bbss
1805 00004008 D _bdata
1806 00002038 T _bfunction
1807 00004028 B _cbss
1808 00004010 D _cdata
1809 00002050 T _cfunction
1810 0000402c B _dbss
1811 00004018 D _ddata
1812 00002068 T _dfunction
1813 00004020 D _edata
1814 00004030 B _end
1815 00004000 T _etext
1816 00002020 t a.o
1817 00002038 t b.o
1818 00002050 t c.o
1819 00002068 t d.o
1820 @end example
1821
1822 @kindex @var{symbol} = @var{expression} ;
1823 @kindex @var{symbol} @var{f}= @var{expression} ;
1824 @item @var{symbol} = @var{expression} ;
1825 @itemx @var{symbol} @var{f}= @var{expression} ;
1826 @var{symbol} is any symbol name (@pxref{Symbols}). ``@var{f}=''
1827 refers to any of the operators @code{&= += -= *= /=} which combine
1828 arithmetic and assignment.
1829
1830 @cindex assignment, in section defn
1831 When you assign a value to a symbol within a particular section
1832 definition, the value is relative to the beginning of the section
1833 (@pxref{Assignment}). If you write
1834
1835 @example
1836 SECTIONS @{
1837 abs = 14 ;
1838 @dots{}
1839 .data : @{ @dots{} rel = 14 ; @dots{} @}
1840 abs2 = 14 + ADDR(.data);
1841 @dots{}
1842 @}
1843 @end example
1844
1845 @c FIXME: Try above example!
1846 @noindent
1847 @code{abs} and @code{rel} do not have the same value; @code{rel} has the
1848 same value as @code{abs2}.
1849
1850 @kindex BYTE(@var{expression})
1851 @kindex SHORT(@var{expression})
1852 @kindex LONG(@var{expression})
1853 @kindex QUAD(@var{expression})
1854 @cindex direct output
1855 @item BYTE(@var{expression})
1856 @itemx SHORT(@var{expression})
1857 @itemx LONG(@var{expression})
1858 @itemx QUAD(@var{expression})
1859 By including one of these four statements in a section definition, you
1860 can explicitly place one, two, four, or eight bytes (respectively) at
1861 the current address of that section. @code{QUAD} is only supported when
1862 using a 64 bit host or target.
1863
1864 @ifclear SingleFormat
1865 Multiple-byte quantities are represented in whatever byte order is
1866 appropriate for the output file format (@pxref{BFD}).
1867 @end ifclear
1868
1869 @item FILL(@var{expression})
1870 @kindex FILL(@var{expression})
1871 @cindex holes, filling
1872 @cindex unspecified memory
1873 Specify the ``fill pattern'' for the current section. Any otherwise
1874 unspecified regions of memory within the section (for example, regions
1875 you skip over by assigning a new value to the location counter @samp{.})
1876 are filled with the two least significant bytes from the
1877 @var{expression} argument. A @code{FILL} statement covers memory
1878 locations @emph{after} the point it occurs in the section definition; by
1879 including more than one @code{FILL} statement, you can have different
1880 fill patterns in different parts of an output section.
1881 @end table
1882
1883 @node Section Options
1884 @subsection Optional Section Attributes
1885 @cindex section defn, full syntax
1886 Here is the full syntax of a section definition, including all the
1887 optional portions:
1888
1889 @smallexample
1890 SECTIONS @{
1891 @dots{}
1892 @var{secname} @var{start} BLOCK(@var{align}) (NOLOAD) : AT ( @var{ldadr} )
1893 @{ @var{contents} @} >@var{region} =@var{fill}
1894 @dots{}
1895 @}
1896 @end smallexample
1897
1898 @var{secname} and @var{contents} are required. @xref{Section
1899 Definition}, and @pxref{Section Placement} for details on
1900 @var{contents}. The remaining elements---@var{start},
1901 @code{BLOCK(@var{align)}}, @code{(NOLOAD)}, @code{AT ( @var{ldadr} )},
1902 @code{>@var{region}}, and @code{=@var{fill}}---are all optional.
1903
1904 @table @code
1905 @cindex start address, section
1906 @cindex section start
1907 @cindex section address
1908 @item @var{start}
1909 You can force the output section to be loaded at a specified address by
1910 specifying @var{start} immediately following the section name.
1911 @var{start} can be represented as any expression. The following
1912 example generates section @var{output} at location
1913 @code{0x40000000}:
1914
1915 @example
1916 SECTIONS @{
1917 @dots{}
1918 output 0x40000000: @{
1919 @dots{}
1920 @}
1921 @dots{}
1922 @}
1923 @end example
1924
1925 @kindex BLOCK(@var{align})
1926 @cindex section alignment
1927 @cindex aligning sections
1928 @item BLOCK(@var{align})
1929 You can include @code{BLOCK()} specification to advance
1930 the location counter @code{.} prior to the beginning of the section, so
1931 that the section will begin at the specified alignment. @var{align} is
1932 an expression.
1933
1934 @kindex NOLOAD
1935 @cindex prevent unnecessary loading
1936 @cindex loading, preventing
1937 @item (NOLOAD)
1938 Use @samp{(NOLOAD)} to prevent a section from being loaded into memory
1939 each time it is accessed. For example, in the script sample below, the
1940 @code{ROM} segment is addressed at memory location @samp{0} and does not
1941 need to be loaded into each object file:
1942
1943 @example
1944 SECTIONS @{
1945 ROM 0 (NOLOAD) : @{ @dots{} @}
1946 @dots{}
1947 @}
1948 @end example
1949
1950 @kindex AT ( @var{ldadr} )
1951 @cindex specify load address
1952 @cindex load address, specifying
1953 @item AT ( @var{ldadr} )
1954 The expression @var{ldadr} that follows the @code{AT} keyword specifies
1955 the load address of the section. The default (if you do not use the
1956 @code{AT} keyword) is to make the load address the same as the
1957 relocation address. This feature is designed to make it easy to build a
1958 ROM image. For example, this @code{SECTIONS} definition creates two
1959 output sections: one called @samp{.text}, which starts at @code{0x1000},
1960 and one called @samp{.mdata}, which is loaded at the end of the
1961 @samp{.text} section even though its relocation address is
1962 @code{0x2000}. The symbol @code{_data} is defined with the value
1963 @code{0x2000}:
1964
1965 @smallexample
1966 SECTIONS
1967 @{
1968 .text 0x1000 : @{ *(.text) _etext = . ; @}
1969 .mdata 0x2000 :
1970 AT ( ADDR(.text) + SIZEOF ( .text ) )
1971 @{ _data = . ; *(.data); _edata = . ; @}
1972 .bss 0x3000 :
1973 @{ _bstart = . ; *(.bss) *(COMMON) ; _bend = . ;@}
1974 @}
1975 @end smallexample
1976
1977 The run-time initialization code (for C programs, usually @code{crt0})
1978 for use with a ROM generated this way has to include something like
1979 the following, to copy the initialized data from the ROM image to its runtime
1980 address:
1981
1982 @smallexample
1983 char *src = _etext;
1984 char *dst = _data;
1985
1986 /* ROM has data at end of text; copy it. */
1987 while (dst < _edata) @{
1988 *dst++ = *src++;
1989 @}
1990
1991 /* Zero bss */
1992 for (dst = _bstart; dst< _bend; dst++)
1993 *dst = 0;
1994 @end smallexample
1995
1996 @kindex >@var{region}
1997 @cindex section, assigning to memory region
1998 @cindex memory regions and sections
1999 @item >@var{region}
2000 Assign this section to a previously defined region of memory.
2001 @xref{MEMORY}.
2002
2003 @kindex =@var{fill}
2004 @cindex section fill pattern
2005 @cindex fill pattern, entire section
2006 @item =@var{fill}
2007 Including @code{=@var{fill}} in a section definition specifies the
2008 initial fill value for that section. You may use any expression to
2009 specify @var{fill}. Any unallocated holes in the current output section
2010 when written to the output file will be filled with the two least
2011 significant bytes of the value, repeated as necessary. You can also
2012 change the fill value with a @code{FILL} statement in the @var{contents}
2013 of a section definition.
2014
2015 @end table
2016
2017 @node Entry Point
2018 @section The Entry Point
2019 @kindex ENTRY(@var{symbol})
2020 @cindex start of execution
2021 @cindex first instruction
2022 The linker command language includes a command specifically for
2023 defining the first executable instruction in an output file (its
2024 @dfn{entry point}). Its argument is a symbol name:
2025 @example
2026 ENTRY(@var{symbol})
2027 @end example
2028
2029 Like symbol assignments, the @code{ENTRY} command may be placed either
2030 as an independent command in the command file, or among the section
2031 definitions within the @code{SECTIONS} command---whatever makes the most
2032 sense for your layout.
2033
2034 @cindex entry point, defaults
2035 @code{ENTRY} is only one of several ways of choosing the entry point.
2036 You may indicate it in any of the following ways (shown in descending
2037 order of priority: methods higher in the list override methods lower down).
2038 @itemize @bullet
2039 @item
2040 the @samp{-e} @var{entry} command-line option;
2041 @item
2042 the @code{ENTRY(@var{symbol})} command in a linker control script;
2043 @item
2044 the value of the symbol @code{start}, if present;
2045 @item
2046 the value of the symbol @code{_main}, if present;
2047 @item
2048 the address of the first byte of the @code{.text} section, if present;
2049 @item
2050 The address @code{0}.
2051 @end itemize
2052
2053 For example, you can use these rules to generate an entry point with an
2054 assignment statement: if no symbol @code{start} is defined within your
2055 input files, you can simply define it, assigning it an appropriate
2056 value---
2057
2058 @example
2059 start = 0x2020;
2060 @end example
2061
2062 @noindent
2063 The example shows an absolute address, but you can use any expression.
2064 For example, if your input object files use some other symbol-name
2065 convention for the entry point, you can just assign the value of
2066 whatever symbol contains the start address to @code{start}:
2067
2068 @example
2069 start = other_symbol ;
2070 @end example
2071
2072 @node Option Commands
2073 @section Option Commands
2074 The command language includes a number of other commands that you can
2075 use for specialized purposes. They are similar in purpose to
2076 command-line options.
2077
2078 @table @code
2079 @kindex CONSTRUCTORS
2080 @cindex C++ constructors, arranging in link
2081 @cindex constructors, arranging in link
2082 @item CONSTRUCTORS
2083 This command ties up C++ style constructor and destructor records. The
2084 details of the constructor representation vary from one object format to
2085 another, but usually lists of constructors and destructors appear as
2086 special sections. The @code{CONSTRUCTORS} command specifies where the
2087 linker is to place the data from these sections, relative to the rest of
2088 the linked output. Constructor data is marked by the symbol
2089 @w{@code{__CTOR_LIST__}} at the start, and @w{@code{__CTOR_LIST_END}} at
2090 the end; destructor data is bracketed similarly, between
2091 @w{@code{__DTOR_LIST__}} and @w{@code{__DTOR_LIST_END}}. (The compiler
2092 must arrange to actually run this code; GNU C++ calls constructors from
2093 a subroutine @code{__main}, which it inserts automatically into the
2094 startup code for @code{main}, and destructors from @code{_exit}.)
2095
2096 @need 1000
2097 @kindex FLOAT
2098 @kindex NOFLOAT
2099 @item FLOAT
2100 @itemx NOFLOAT
2101 These keywords were used in some older linkers to request a particular
2102 math subroutine library. @code{ld} doesn't use the keywords, assuming
2103 instead that any necessary subroutines are in libraries specified using
2104 the general mechanisms for linking to archives; but to permit the use of
2105 scripts that were written for the older linkers, the keywords
2106 @code{FLOAT} and @code{NOFLOAT} are accepted and ignored.
2107
2108 @kindex FORCE_COMMON_ALLOCATION
2109 @cindex common allocation
2110 @item FORCE_COMMON_ALLOCATION
2111 This command has the same effect as the @samp{-d} command-line option:
2112 to make @code{ld} assign space to common symbols even if a relocatable
2113 output file is specified (@samp{-r}).
2114
2115 @kindex INPUT ( @var{files} )
2116 @cindex binary input files
2117 @item INPUT ( @var{file}, @var{file}, @dots{} )
2118 @itemx INPUT ( @var{file} @var{file} @dots{} )
2119 Use this command to include binary input files in the link, without
2120 including them in a particular section definition.
2121 Specify the full name for each @var{file}, including @samp{.a} if
2122 required.
2123
2124 @code{ld} searches for each @var{file} through the archive-library
2125 search path, just as for files you specify on the command line.
2126 See the description of @samp{-L} in @ref{Options,,Command Line
2127 Options}.
2128
2129 If you use @samp{-l@var{file}}, @code{ld} will transform the name to
2130 @code{lib@var{file}.a} as with the command line argument @samp{-l}.
2131
2132 @kindex GROUP ( @var{files} )
2133 @cindex grouping input files
2134 @item GROUP ( @var{file}, @var{file}, @dots{} )
2135 @itemx GROUP ( @var{file} @var{file} @dots{} )
2136 This command is like @code{INPUT}, except that the named files should
2137 all be archives, and they are searched repeatedly until no new undefined
2138 references are created. See the description of @samp{-(} in
2139 @ref{Options,,Command Line Options}.
2140
2141 @ignore
2142 @item MAP ( @var{name} )
2143 @kindex MAP ( @var{name} )
2144 @c MAP(...) appears to look for an F in the arg, ignoring all other
2145 @c chars; if it finds one, it sets "map_option_f" to true. But nothing
2146 @c checks map_option_f. Apparently a stub for the future...
2147 @end ignore
2148
2149 @item OUTPUT ( @var{filename} )
2150 @kindex OUTPUT ( @var{filename} )
2151 @cindex naming the output file
2152 Use this command to name the link output file @var{filename}. The
2153 effect of @code{OUTPUT(@var{filename})} is identical to the effect of
2154 @w{@samp{-o @var{filename}}}, which overrides it. You can use this
2155 command to supply a default output-file name other than @code{a.out}.
2156
2157 @ifclear SingleFormat
2158 @item OUTPUT_ARCH ( @var{bfdname} )
2159 @kindex OUTPUT_ARCH ( @var{bfdname} )
2160 @cindex machine architecture, output
2161 Specify a particular output machine architecture, with one of the names
2162 used by the BFD back-end routines (@pxref{BFD}). This command is often
2163 unnecessary; the architecture is most often set implicitly by either the
2164 system BFD configuration or as a side effect of the @code{OUTPUT_FORMAT}
2165 command.
2166
2167 @item OUTPUT_FORMAT ( @var{bfdname} )
2168 @kindex OUTPUT_FORMAT ( @var{bfdname} )
2169 @cindex format, output file
2170 When @code{ld} is configured to support multiple object code formats,
2171 you can use this command to specify a particular output format.
2172 @var{bfdname} is one of the names used by the BFD back-end routines
2173 (@pxref{BFD}). The effect is identical to the effect of the
2174 @samp{-oformat} command-line option. This selection affects only
2175 the output file; the related command @code{TARGET} affects primarily
2176 input files.
2177 @end ifclear
2178
2179 @item SEARCH_DIR ( @var{path} )
2180 @kindex SEARCH_DIR ( @var{path} )
2181 @cindex path for libraries
2182 @cindex search path, libraries
2183 Add @var{path} to the list of paths where @code{ld} looks for
2184 archive libraries. @code{SEARCH_DIR(@var{path})} has the same
2185 effect as @samp{-L@var{path}} on the command line.
2186
2187 @item STARTUP ( @var{filename} )
2188 @kindex STARTUP ( @var{filename} )
2189 @cindex first input file
2190 Ensure that @var{filename} is the first input file used in the link
2191 process.
2192
2193 @ifclear SingleFormat
2194 @item TARGET ( @var{format} )
2195 @cindex input file format
2196 @kindex TARGET ( @var{format} )
2197 When @code{ld} is configured to support multiple object code formats,
2198 you can use this command to change the input-file object code format
2199 (like the command-line option @samp{-b} or its synonym @samp{-format}).
2200 The argument @var{format} is one of the strings used by BFD to name
2201 binary formats. If @code{TARGET} is specified but @code{OUTPUT_FORMAT}
2202 is not, the last @code{TARGET} argument is also used as the default
2203 format for the @code{ld} output file. @xref{BFD}.
2204
2205 @kindex GNUTARGET
2206 If you don't use the @code{TARGET} command, @code{ld} uses the value of
2207 the environment variable @code{GNUTARGET}, if available, to select the
2208 output file format. If that variable is also absent, @code{ld} uses
2209 the default format configured for your machine in the BFD libraries.
2210 @end ifclear
2211 @end table
2212
2213 @ifset GENERIC
2214 @node Machine Dependent
2215 @chapter Machine Dependent Features
2216
2217 @cindex machine dependencies
2218 @code{ld} has additional features on some platforms; the following
2219 sections describe them. Machines where @code{ld} has no additional
2220 functionality are not listed.
2221
2222 @menu
2223 * H8/300:: @code{ld} and the H8/300
2224 * i960:: @code{ld} and the Intel 960 family
2225 @end menu
2226 @end ifset
2227
2228 @c FIXME! This could use @raisesections/@lowersections, but there seems to be a conflict
2229 @c between those and node-defaulting.
2230 @ifset H8300
2231 @ifclear GENERIC
2232 @raisesections
2233 @end ifclear
2234 @node H8/300
2235 @section @code{ld} and the H8/300
2236
2237 @cindex H8/300 support
2238 For the H8/300, @code{ld} can perform these global optimizations when
2239 you specify the @samp{-relax} command-line option.
2240
2241 @table @emph
2242 @item relaxing address modes
2243 @cindex relaxing on H8/300
2244 @code{ld} finds all @code{jsr} and @code{jmp} instructions whose
2245 targets are within eight bits, and turns them into eight-bit
2246 program-counter relative @code{bsr} and @code{bra} instructions,
2247 respectively.
2248
2249 @item synthesizing instructions
2250 @cindex synthesizing on H8/300
2251 @c FIXME: specifically mov.b, or any mov instructions really?
2252 @code{ld} finds all @code{mov.b} instructions which use the
2253 sixteen-bit absolute address form, but refer to the top
2254 page of memory, and changes them to use the eight-bit address form.
2255 (That is: the linker turns @samp{mov.b @code{@@}@var{aa}:16} into
2256 @samp{mov.b @code{@@}@var{aa}:8} whenever the address @var{aa} is in the
2257 top page of memory).
2258 @end table
2259 @ifclear GENERIC
2260 @lowersections
2261 @end ifclear
2262 @end ifset
2263
2264 @ifclear GENERIC
2265 @ifset Hitachi
2266 @c This stuff is pointless to say unless you're especially concerned
2267 @c with Hitachi chips; don't enable it for generic case, please.
2268 @node Hitachi
2269 @chapter @code{ld} and other Hitachi chips
2270
2271 @code{ld} also supports the H8/300H, the H8/500, and the Hitachi SH. No
2272 special features, commands, or command-line options are required for
2273 these chips.
2274 @end ifset
2275 @end ifclear
2276
2277 @ifset I960
2278 @ifclear GENERIC
2279 @raisesections
2280 @end ifclear
2281 @node i960
2282 @section @code{ld} and the Intel 960 family
2283
2284 @cindex i960 support
2285
2286 You can use the @samp{-A@var{architecture}} command line option to
2287 specify one of the two-letter names identifying members of the 960
2288 family; the option specifies the desired output target, and warns of any
2289 incompatible instructions in the input files. It also modifies the
2290 linker's search strategy for archive libraries, to support the use of
2291 libraries specific to each particular architecture, by including in the
2292 search loop names suffixed with the string identifying the architecture.
2293
2294 For example, if your @code{ld} command line included @w{@samp{-ACA}} as
2295 well as @w{@samp{-ltry}}, the linker would look (in its built-in search
2296 paths, and in any paths you specify with @samp{-L}) for a library with
2297 the names
2298
2299 @example
2300 try
2301 libtry.a
2302 tryca
2303 libtryca.a
2304 @end example
2305
2306 @noindent
2307 The first two possibilities would be considered in any event; the last
2308 two are due to the use of @w{@samp{-ACA}}.
2309
2310 You can meaningfully use @samp{-A} more than once on a command line, since
2311 the 960 architecture family allows combination of target architectures; each
2312 use will add another pair of name variants to search for when @w{@samp{-l}}
2313 specifies a library.
2314
2315 @cindex @code{-relax} on i960
2316 @cindex relaxing on i960
2317 @code{ld} supports the @samp{-relax} option for the i960 family. If you
2318 specify @samp{-relax}, @code{ld} finds all @code{balx} and @code{calx}
2319 instructions whose targets are within 24 bits, and turns them into
2320 24-bit program-counter relative @code{bal} and @code{cal}
2321 instructions, respectively. @code{ld} also turns @code{cal}
2322 instructions into @code{bal} instructions when it determines that the
2323 target subroutine is a leaf routine (that is, the target subroutine does
2324 not itself call any subroutines).
2325
2326 @ifclear GENERIC
2327 @lowersections
2328 @end ifclear
2329 @end ifset
2330
2331 @ifclear SingleFormat
2332 @node BFD
2333 @chapter BFD
2334
2335 @cindex back end
2336 @cindex object file management
2337 @cindex object formats available
2338 @kindex objdump -i
2339 The linker accesses object and archive files using the BFD libraries.
2340 These libraries allow the linker to use the same routines to operate on
2341 object files whatever the object file format. A different object file
2342 format can be supported simply by creating a new BFD back end and adding
2343 it to the library. To conserve runtime memory, however, the linker and
2344 associated tools are usually configured to support only a subset of the
2345 object file formats available. You can use @code{objdump -i}
2346 (@pxref{objdump,,objdump,binutils.info,The GNU Binary Utilities}) to
2347 list all the formats available for your configuration.
2348
2349 @cindex BFD requirements
2350 @cindex requirements for BFD
2351 As with most implementations, BFD is a compromise between
2352 several conflicting requirements. The major factor influencing
2353 BFD design was efficiency: any time used converting between
2354 formats is time which would not have been spent had BFD not
2355 been involved. This is partly offset by abstraction payback; since
2356 BFD simplifies applications and back ends, more time and care
2357 may be spent optimizing algorithms for a greater speed.
2358
2359 One minor artifact of the BFD solution which you should bear in
2360 mind is the potential for information loss. There are two places where
2361 useful information can be lost using the BFD mechanism: during
2362 conversion and during output. @xref{BFD information loss}.
2363
2364 @menu
2365 * BFD outline:: How it works: an outline of BFD
2366 @end menu
2367
2368 @node BFD outline
2369 @section How it works: an outline of BFD
2370 @cindex opening object files
2371 @include bfdsumm.texi
2372 @end ifclear
2373
2374 @node MRI
2375 @appendix MRI Compatible Script Files
2376 @cindex MRI compatibility
2377 To aid users making the transition to @sc{gnu} @code{ld} from the MRI
2378 linker, @code{ld} can use MRI compatible linker scripts as an
2379 alternative to the more general-purpose linker scripting language
2380 described in @ref{Commands,,Command Language}. MRI compatible linker
2381 scripts have a much simpler command set than the scripting language
2382 otherwise used with @code{ld}. @sc{gnu} @code{ld} supports the most
2383 commonly used MRI linker commands; these commands are described here.
2384
2385 In general, MRI scripts aren't of much use with the @code{a.out} object
2386 file format, since it only has three sections and MRI scripts lack some
2387 features to make use of them.
2388
2389 You can specify a file containing an MRI-compatible script using the
2390 @samp{-c} command-line option.
2391
2392 Each command in an MRI-compatible script occupies its own line; each
2393 command line starts with the keyword that identifies the command (though
2394 blank lines are also allowed for punctuation). If a line of an
2395 MRI-compatible script begins with an unrecognized keyword, @code{ld}
2396 issues a warning message, but continues processing the script.
2397
2398 Lines beginning with @samp{*} are comments.
2399
2400 You can write these commands using all upper-case letters, or all
2401 lower case; for example, @samp{chip} is the same as @samp{CHIP}.
2402 The following list shows only the upper-case form of each command.
2403
2404 @table @code
2405 @cindex @code{ABSOLUTE} (MRI)
2406 @item ABSOLUTE @var{secname}
2407 @item ABSOLUTE @var{secname}, @var{secname}, @dots{} @var{secname}
2408 Normally, @code{ld} includes in the output file all sections from all
2409 the input files. However, in an MRI-compatible script, you can use the
2410 @code{ABSOLUTE} command to restrict the sections that will be present in
2411 your output program. If the @code{ABSOLUTE} command is used at all in a
2412 script, then only the sections named explicitly in @code{ABSOLUTE}
2413 commands will appear in the linker output. You can still use other
2414 input sections (whatever you select on the command line, or using
2415 @code{LOAD}) to resolve addresses in the output file.
2416
2417 @cindex @code{ALIAS} (MRI)
2418 @item ALIAS @var{out-secname}, @var{in-secname}
2419 Use this command to place the data from input section @var{in-secname}
2420 in a section called @var{out-secname} in the linker output file.
2421
2422 @var{in-secname} may be an integer.
2423
2424 @cindex @code{BASE} (MRI)
2425 @item BASE @var{expression}
2426 Use the value of @var{expression} as the lowest address (other than
2427 absolute addresses) in the output file.
2428
2429 @cindex @code{CHIP} (MRI)
2430 @item CHIP @var{expression}
2431 @itemx CHIP @var{expression}, @var{expression}
2432 This command does nothing; it is accepted only for compatibility.
2433
2434 @cindex @code{END} (MRI)
2435 @item END
2436 This command does nothing whatever; it's only accepted for compatibility.
2437
2438 @cindex @code{FORMAT} (MRI)
2439 @item FORMAT @var{output-format}
2440 Similar to the @code{OUTPUT_FORMAT} command in the more general linker
2441 language, but restricted to one of these output formats:
2442
2443 @enumerate
2444 @item
2445 S-records, if @var{output-format} is @samp{S}
2446
2447 @item
2448 IEEE, if @var{output-format} is @samp{IEEE}
2449
2450 @item
2451 COFF (the @samp{coff-m68k} variant in BFD), if @var{output-format} is
2452 @samp{COFF}
2453 @end enumerate
2454
2455 @cindex @code{LIST} (MRI)
2456 @item LIST @var{anything}@dots{}
2457 Print (to the standard output file) a link map, as produced by the
2458 @code{ld} command-line option @samp{-M}.
2459
2460 The keyword @code{LIST} may be followed by anything on the
2461 same line, with no change in its effect.
2462
2463 @cindex @code{LOAD} (MRI)
2464 @item LOAD @var{filename}
2465 @item LOAD @var{filename}, @var{filename}, @dots{} @var{filename}
2466 Include one or more object file @var{filename} in the link; this has the
2467 same effect as specifying @var{filename} directly on the @code{ld}
2468 command line.
2469
2470 @cindex @code{NAME} (MRI)
2471 @item NAME @var{output-name}
2472 @var{output-name} is the name for the program produced by @code{ld}; the
2473 MRI-compatible command @code{NAME} is equivalent to the command-line
2474 option @samp{-o} or the general script language command @code{OUTPUT}.
2475
2476 @cindex @code{ORDER} (MRI)
2477 @item ORDER @var{secname}, @var{secname}, @dots{} @var{secname}
2478 @itemx ORDER @var{secname} @var{secname} @var{secname}
2479 Normally, @code{ld} orders the sections in its output file in the
2480 order in which they first appear in the input files. In an MRI-compatible
2481 script, you can override this ordering with the @code{ORDER} command. The
2482 sections you list with @code{ORDER} will appear first in your output
2483 file, in the order specified.
2484
2485 @cindex @code{PUBLIC} (MRI)
2486 @item PUBLIC @var{name}=@var{expression}
2487 @itemx PUBLIC @var{name},@var{expression}
2488 @itemx PUBLIC @var{name} @var{expression}
2489 Supply a value (@var{expression}) for external symbol
2490 @var{name} used in the linker input files.
2491
2492 @cindex @code{SECT} (MRI)
2493 @item SECT @var{secname}, @var{expression}
2494 @itemx SECT @var{secname}=@var{expression}
2495 @itemx SECT @var{secname} @var{expression}
2496 You can use any of these three forms of the @code{SECT} command to
2497 specify the start address (@var{expression}) for section @var{secname}.
2498 If you have more than one @code{SECT} statement for the same
2499 @var{secname}, only the @emph{first} sets the start address.
2500 @end table
2501
2502
2503 @node Index
2504 @unnumbered Index
2505
2506 @printindex cp
2507
2508 @tex
2509 % I think something like @colophon should be in texinfo. In the
2510 % meantime:
2511 \long\def\colophon{\hbox to0pt{}\vfill
2512 \centerline{The body of this manual is set in}
2513 \centerline{\fontname\tenrm,}
2514 \centerline{with headings in {\bf\fontname\tenbf}}
2515 \centerline{and examples in {\tt\fontname\tentt}.}
2516 \centerline{{\it\fontname\tenit\/} and}
2517 \centerline{{\sl\fontname\tensl\/}}
2518 \centerline{are used for emphasis.}\vfill}
2519 \page\colophon
2520 % Blame: pesch@cygnus.com, 28mar91.
2521 @end tex
2522
2523
2524 @contents
2525 @bye
2526
2527