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