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