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