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