2005-09-06 H.J. Lu <hongjiu.lu@intel.com>
[deliverable/binutils-gdb.git] / ld / ld.texinfo
1 \input texinfo
2 @setfilename ld.info
3 @c Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
4 @c 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
5 @syncodeindex ky cp
6 @include configdoc.texi
7 @c (configdoc.texi is generated by the Makefile)
8 @include ldver.texi
9
10 @c @smallbook
11
12 @macro gcctabopt{body}
13 @code{\body\}
14 @end macro
15
16 @c man begin NAME
17 @ifset man
18 @c Configure for the generation of man pages
19 @set UsesEnvVars
20 @set GENERIC
21 @set ARC
22 @set ARM
23 @set D10V
24 @set D30V
25 @set H8/300
26 @set H8/500
27 @set HPPA
28 @set I370
29 @set I80386
30 @set I860
31 @set I960
32 @set M32R
33 @set M68HC11
34 @set M680X0
35 @set MCORE
36 @set MIPS
37 @set MMIX
38 @set MSP430
39 @set PDP11
40 @set PJ
41 @set SH
42 @set SPARC
43 @set TIC54X
44 @set V850
45 @set VAX
46 @set WIN32
47 @set XTENSA
48 @end ifset
49 @c man end
50
51 @ifinfo
52 @format
53 START-INFO-DIR-ENTRY
54 * Ld: (ld). The GNU linker.
55 END-INFO-DIR-ENTRY
56 @end format
57 @end ifinfo
58
59 @ifinfo
60 This file documents the @sc{gnu} linker LD version @value{VERSION}.
61
62 Copyright (C) 1991, 92, 93, 94, 95, 96, 97, 98, 99, 2000,
63 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
64
65 @ignore
66
67 Permission is granted to copy, distribute and/or modify this document
68 under the terms of the GNU Free Documentation License, Version 1.1
69 or any later version published by the Free Software Foundation;
70 with no Invariant Sections, with no Front-Cover Texts, and with no
71 Back-Cover Texts. A copy of the license is included in the
72 section entitled ``GNU Free Documentation License''.
73
74 Permission is granted to process this file through Tex and print the
75 results, provided the printed document carries copying permission
76 notice identical to this one except for the removal of this paragraph
77 (this paragraph not being relevant to the printed manual).
78
79 @end ignore
80 @end ifinfo
81 @iftex
82 @finalout
83 @setchapternewpage odd
84 @settitle Using LD, the GNU linker
85 @titlepage
86 @title Using ld
87 @subtitle The GNU linker
88 @sp 1
89 @subtitle @code{ld} version 2
90 @subtitle Version @value{VERSION}
91 @author Steve Chamberlain
92 @author Ian Lance Taylor
93 @page
94
95 @tex
96 {\parskip=0pt
97 \hfill Red Hat Inc\par
98 \hfill nickc\@credhat.com, doc\@redhat.com\par
99 \hfill {\it Using LD, the GNU linker}\par
100 \hfill Edited by Jeffrey Osier (jeffrey\@cygnus.com)\par
101 }
102 \global\parindent=0pt % Steve likes it this way.
103 @end tex
104
105 @vskip 0pt plus 1filll
106 @c man begin COPYRIGHT
107 Copyright @copyright{} 1991, 92, 93, 94, 95, 96, 97, 98, 99, 2000, 2001,
108 2002, 2003, 2004 Free Software Foundation, Inc.
109
110 Permission is granted to copy, distribute and/or modify this document
111 under the terms of the GNU Free Documentation License, Version 1.1
112 or any later version published by the Free Software Foundation;
113 with no Invariant Sections, with no Front-Cover Texts, and with no
114 Back-Cover Texts. A copy of the license is included in the
115 section entitled ``GNU Free Documentation License''.
116 @c man end
117
118 @end titlepage
119 @end iftex
120 @c FIXME: Talk about importance of *order* of args, cmds to linker!
121
122 @ifnottex
123 @node Top
124 @top Using ld
125 This file documents the @sc{gnu} linker ld version @value{VERSION}.
126
127 This document is distributed under the terms of the GNU Free
128 Documentation License. A copy of the license is included in the
129 section entitled ``GNU Free Documentation License''.
130
131 @menu
132 * Overview:: Overview
133 * Invocation:: Invocation
134 * Scripts:: Linker Scripts
135 @ifset GENERIC
136 * Machine Dependent:: Machine Dependent Features
137 @end ifset
138 @ifclear GENERIC
139 @ifset H8300
140 * H8/300:: ld and the H8/300
141 @end ifset
142 @ifset Renesas
143 * Renesas:: ld and other Renesas micros
144 @end ifset
145 @ifset I960
146 * i960:: ld and the Intel 960 family
147 @end ifset
148 @ifset ARM
149 * ARM:: ld and the ARM family
150 @end ifset
151 @ifset HPPA
152 * HPPA ELF32:: ld and HPPA 32-bit ELF
153 @end ifset
154 @ifset M68HC11
155 * M68HC11/68HC12:: ld and the Motorola 68HC11 and 68HC12 families
156 @end ifset
157 @ifset TICOFF
158 * TI COFF:: ld and the TI COFF
159 @end ifset
160 @ifset WIN32
161 * Win32:: ld and WIN32 (cygwin/mingw)
162 @end ifset
163 @ifset XTENSA
164 * Xtensa:: ld and Xtensa Processors
165 @end ifset
166 @end ifclear
167 @ifclear SingleFormat
168 * BFD:: BFD
169 @end ifclear
170 @c Following blank line required for remaining bug in makeinfo conds/menus
171
172 * Reporting Bugs:: Reporting Bugs
173 * MRI:: MRI Compatible Script Files
174 * GNU Free Documentation License:: GNU Free Documentation License
175 * Index:: Index
176 @end menu
177 @end ifnottex
178
179 @node Overview
180 @chapter Overview
181
182 @cindex @sc{gnu} linker
183 @cindex what is this?
184
185 @ifset man
186 @c man begin SYNOPSIS
187 ld [@b{options}] @var{objfile} @dots{}
188 @c man end
189
190 @c man begin SEEALSO
191 ar(1), nm(1), objcopy(1), objdump(1), readelf(1) and
192 the Info entries for @file{binutils} and
193 @file{ld}.
194 @c man end
195 @end ifset
196
197 @c man begin DESCRIPTION
198
199 @command{ld} combines a number of object and archive files, relocates
200 their data and ties up symbol references. Usually the last step in
201 compiling a program is to run @command{ld}.
202
203 @command{ld} accepts Linker Command Language files written in
204 a superset of AT&T's Link Editor Command Language syntax,
205 to provide explicit and total control over the linking process.
206
207 @ifset man
208 @c For the man only
209 This man page does not describe the command language; see the
210 @command{ld} entry in @code{info}, or the manual
211 ld: the GNU linker, for full details on the command language and
212 on other aspects of the GNU linker.
213 @end ifset
214
215 @ifclear SingleFormat
216 This version of @command{ld} uses the general purpose BFD libraries
217 to operate on object files. This allows @command{ld} to read, combine, and
218 write object files in many different formats---for example, COFF or
219 @code{a.out}. Different formats may be linked together to produce any
220 available kind of object file. @xref{BFD}, for more information.
221 @end ifclear
222
223 Aside from its flexibility, the @sc{gnu} linker is more helpful than other
224 linkers in providing diagnostic information. Many linkers abandon
225 execution immediately upon encountering an error; whenever possible,
226 @command{ld} continues executing, allowing you to identify other errors
227 (or, in some cases, to get an output file in spite of the error).
228
229 @c man end
230
231 @node Invocation
232 @chapter Invocation
233
234 @c man begin DESCRIPTION
235
236 The @sc{gnu} linker @command{ld} is meant to cover a broad range of situations,
237 and to be as compatible as possible with other linkers. As a result,
238 you have many choices to control its behavior.
239
240 @c man end
241
242 @ifset UsesEnvVars
243 @menu
244 * Options:: Command Line Options
245 * Environment:: Environment Variables
246 @end menu
247
248 @node Options
249 @section Command Line Options
250 @end ifset
251
252 @cindex command line
253 @cindex options
254
255 @c man begin OPTIONS
256
257 The linker supports a plethora of command-line options, but in actual
258 practice few of them are used in any particular context.
259 @cindex standard Unix system
260 For instance, a frequent use of @command{ld} is to link standard Unix
261 object files on a standard, supported Unix system. On such a system, to
262 link a file @code{hello.o}:
263
264 @smallexample
265 ld -o @var{output} /lib/crt0.o hello.o -lc
266 @end smallexample
267
268 This tells @command{ld} to produce a file called @var{output} as the
269 result of linking the file @code{/lib/crt0.o} with @code{hello.o} and
270 the library @code{libc.a}, which will come from the standard search
271 directories. (See the discussion of the @samp{-l} option below.)
272
273 Some of the command-line options to @command{ld} may be specified at any
274 point in the command line. However, options which refer to files, such
275 as @samp{-l} or @samp{-T}, cause the file to be read at the point at
276 which the option appears in the command line, relative to the object
277 files and other file options. Repeating non-file options with a
278 different argument will either have no further effect, or override prior
279 occurrences (those further to the left on the command line) of that
280 option. Options which may be meaningfully specified more than once are
281 noted in the descriptions below.
282
283 @cindex object files
284 Non-option arguments are object files or archives which are to be linked
285 together. They may follow, precede, or be mixed in with command-line
286 options, except that an object file argument may not be placed between
287 an option and its argument.
288
289 Usually the linker is invoked with at least one object file, but you can
290 specify other forms of binary input files using @samp{-l}, @samp{-R},
291 and the script command language. If @emph{no} binary input files at all
292 are specified, the linker does not produce any output, and issues the
293 message @samp{No input files}.
294
295 If the linker cannot recognize the format of an object file, it will
296 assume that it is a linker script. A script specified in this way
297 augments the main linker script used for the link (either the default
298 linker script or the one specified by using @samp{-T}). This feature
299 permits the linker to link against a file which appears to be an object
300 or an archive, but actually merely defines some symbol values, or uses
301 @code{INPUT} or @code{GROUP} to load other objects. Note that
302 specifying a script in this way merely augments the main linker script;
303 use the @samp{-T} option to replace the default linker script entirely.
304 @xref{Scripts}.
305
306 For options whose names are a single letter,
307 option arguments must either follow the option letter without intervening
308 whitespace, or be given as separate arguments immediately following the
309 option that requires them.
310
311 For options whose names are multiple letters, either one dash or two can
312 precede the option name; for example, @samp{-trace-symbol} and
313 @samp{--trace-symbol} are equivalent. Note---there is one exception to
314 this rule. Multiple letter options that start with a lower case 'o' can
315 only be preceeded by two dashes. This is to reduce confusion with the
316 @samp{-o} option. So for example @samp{-omagic} sets the output file
317 name to @samp{magic} whereas @samp{--omagic} sets the NMAGIC flag on the
318 output.
319
320 Arguments to multiple-letter options must either be separated from the
321 option name by an equals sign, or be given as separate arguments
322 immediately following the option that requires them. For example,
323 @samp{--trace-symbol foo} and @samp{--trace-symbol=foo} are equivalent.
324 Unique abbreviations of the names of multiple-letter options are
325 accepted.
326
327 Note---if the linker is being invoked indirectly, via a compiler driver
328 (e.g. @samp{gcc}) then all the linker command line options should be
329 prefixed by @samp{-Wl,} (or whatever is appropriate for the particular
330 compiler driver) like this:
331
332 @smallexample
333 gcc -Wl,--startgroup foo.o bar.o -Wl,--endgroup
334 @end smallexample
335
336 This is important, because otherwise the compiler driver program may
337 silently drop the linker options, resulting in a bad link.
338
339 Here is a table of the generic command line switches accepted by the GNU
340 linker:
341
342 @table @gcctabopt
343 @kindex -a@var{keyword}
344 @item -a@var{keyword}
345 This option is supported for HP/UX compatibility. The @var{keyword}
346 argument must be one of the strings @samp{archive}, @samp{shared}, or
347 @samp{default}. @samp{-aarchive} is functionally equivalent to
348 @samp{-Bstatic}, and the other two keywords are functionally equivalent
349 to @samp{-Bdynamic}. This option may be used any number of times.
350
351 @ifset I960
352 @cindex architectures
353 @kindex -A@var{arch}
354 @item -A@var{architecture}
355 @kindex --architecture=@var{arch}
356 @itemx --architecture=@var{architecture}
357 In the current release of @command{ld}, this option is useful only for the
358 Intel 960 family of architectures. In that @command{ld} configuration, the
359 @var{architecture} argument identifies the particular architecture in
360 the 960 family, enabling some safeguards and modifying the
361 archive-library search path. @xref{i960,,@command{ld} and the Intel 960
362 family}, for details.
363
364 Future releases of @command{ld} may support similar functionality for
365 other architecture families.
366 @end ifset
367
368 @ifclear SingleFormat
369 @cindex binary input format
370 @kindex -b @var{format}
371 @kindex --format=@var{format}
372 @cindex input format
373 @cindex input format
374 @item -b @var{input-format}
375 @itemx --format=@var{input-format}
376 @command{ld} may be configured to support more than one kind of object
377 file. If your @command{ld} is configured this way, you can use the
378 @samp{-b} option to specify the binary format for input object files
379 that follow this option on the command line. Even when @command{ld} is
380 configured to support alternative object formats, you don't usually need
381 to specify this, as @command{ld} should be configured to expect as a
382 default input format the most usual format on each machine.
383 @var{input-format} is a text string, the name of a particular format
384 supported by the BFD libraries. (You can list the available binary
385 formats with @samp{objdump -i}.)
386 @xref{BFD}.
387
388 You may want to use this option if you are linking files with an unusual
389 binary format. You can also use @samp{-b} to switch formats explicitly (when
390 linking object files of different formats), by including
391 @samp{-b @var{input-format}} before each group of object files in a
392 particular format.
393
394 The default format is taken from the environment variable
395 @code{GNUTARGET}.
396 @ifset UsesEnvVars
397 @xref{Environment}.
398 @end ifset
399 You can also define the input format from a script, using the command
400 @code{TARGET};
401 @ifclear man
402 see @ref{Format Commands}.
403 @end ifclear
404 @end ifclear
405
406 @kindex -c @var{MRI-cmdfile}
407 @kindex --mri-script=@var{MRI-cmdfile}
408 @cindex compatibility, MRI
409 @item -c @var{MRI-commandfile}
410 @itemx --mri-script=@var{MRI-commandfile}
411 For compatibility with linkers produced by MRI, @command{ld} accepts script
412 files written in an alternate, restricted command language, described in
413 @ifclear man
414 @ref{MRI,,MRI Compatible Script Files}.
415 @end ifclear
416 @ifset man
417 the MRI Compatible Script Files section of GNU ld documentation.
418 @end ifset
419 Introduce MRI script files with
420 the option @samp{-c}; use the @samp{-T} option to run linker
421 scripts written in the general-purpose @command{ld} scripting language.
422 If @var{MRI-cmdfile} does not exist, @command{ld} looks for it in the directories
423 specified by any @samp{-L} options.
424
425 @cindex common allocation
426 @kindex -d
427 @kindex -dc
428 @kindex -dp
429 @item -d
430 @itemx -dc
431 @itemx -dp
432 These three options are equivalent; multiple forms are supported for
433 compatibility with other linkers. They assign space to common symbols
434 even if a relocatable output file is specified (with @samp{-r}). The
435 script command @code{FORCE_COMMON_ALLOCATION} has the same effect.
436 @xref{Miscellaneous Commands}.
437
438 @cindex entry point, from command line
439 @kindex -e @var{entry}
440 @kindex --entry=@var{entry}
441 @item -e @var{entry}
442 @itemx --entry=@var{entry}
443 Use @var{entry} as the explicit symbol for beginning execution of your
444 program, rather than the default entry point. If there is no symbol
445 named @var{entry}, the linker will try to parse @var{entry} as a number,
446 and use that as the entry address (the number will be interpreted in
447 base 10; you may use a leading @samp{0x} for base 16, or a leading
448 @samp{0} for base 8). @xref{Entry Point}, for a discussion of defaults
449 and other ways of specifying the entry point.
450
451 @kindex --exclude-libs
452 @item --exclude-libs @var{lib},@var{lib},...
453 Specifies a list of archive libraries from which symbols should not be automatically
454 exported. The library names may be delimited by commas or colons. Specifying
455 @code{--exclude-libs ALL} excludes symbols in all archive libraries from
456 automatic export. This option is available only for the i386 PE targeted
457 port of the linker and for ELF targeted ports. For i386 PE, symbols
458 explicitly listed in a .def file are still exported, regardless of this
459 option. For ELF targeted ports, symbols affected by this option will
460 be treated as hidden.
461
462 @cindex dynamic symbol table
463 @kindex -E
464 @kindex --export-dynamic
465 @item -E
466 @itemx --export-dynamic
467 When creating a dynamically linked executable, add all symbols to the
468 dynamic symbol table. The dynamic symbol table is the set of symbols
469 which are visible from dynamic objects at run time.
470
471 If you do not use this option, the dynamic symbol table will normally
472 contain only those symbols which are referenced by some dynamic object
473 mentioned in the link.
474
475 If you use @code{dlopen} to load a dynamic object which needs to refer
476 back to the symbols defined by the program, rather than some other
477 dynamic object, then you will probably need to use this option when
478 linking the program itself.
479
480 You can also use the version script to control what symbols should
481 be added to the dynamic symbol table if the output format supports it.
482 See the description of @samp{--version-script} in @ref{VERSION}.
483
484 @ifclear SingleFormat
485 @cindex big-endian objects
486 @cindex endianness
487 @kindex -EB
488 @item -EB
489 Link big-endian objects. This affects the default output format.
490
491 @cindex little-endian objects
492 @kindex -EL
493 @item -EL
494 Link little-endian objects. This affects the default output format.
495 @end ifclear
496
497 @kindex -f
498 @kindex --auxiliary
499 @item -f
500 @itemx --auxiliary @var{name}
501 When creating an ELF shared object, set the internal DT_AUXILIARY field
502 to the specified name. This tells the dynamic linker that the symbol
503 table of the shared object should be used as an auxiliary filter on the
504 symbol table of the shared object @var{name}.
505
506 If you later link a program against this filter object, then, when you
507 run the program, the dynamic linker will see the DT_AUXILIARY field. If
508 the dynamic linker resolves any symbols from the filter object, it will
509 first check whether there is a definition in the shared object
510 @var{name}. If there is one, it will be used instead of the definition
511 in the filter object. The shared object @var{name} need not exist.
512 Thus the shared object @var{name} may be used to provide an alternative
513 implementation of certain functions, perhaps for debugging or for
514 machine specific performance.
515
516 This option may be specified more than once. The DT_AUXILIARY entries
517 will be created in the order in which they appear on the command line.
518
519 @kindex -F
520 @kindex --filter
521 @item -F @var{name}
522 @itemx --filter @var{name}
523 When creating an ELF shared object, set the internal DT_FILTER field to
524 the specified name. This tells the dynamic linker that the symbol table
525 of the shared object which is being created should be used as a filter
526 on the symbol table of the shared object @var{name}.
527
528 If you later link a program against this filter object, then, when you
529 run the program, the dynamic linker will see the DT_FILTER field. The
530 dynamic linker will resolve symbols according to the symbol table of the
531 filter object as usual, but it will actually link to the definitions
532 found in the shared object @var{name}. Thus the filter object can be
533 used to select a subset of the symbols provided by the object
534 @var{name}.
535
536 Some older linkers used the @option{-F} option throughout a compilation
537 toolchain for specifying object-file format for both input and output
538 object files.
539 @ifclear SingleFormat
540 The @sc{gnu} linker uses other mechanisms for this purpose: the
541 @option{-b}, @option{--format}, @option{--oformat} options, the
542 @code{TARGET} command in linker scripts, and the @code{GNUTARGET}
543 environment variable.
544 @end ifclear
545 The @sc{gnu} linker will ignore the @option{-F} option when not
546 creating an ELF shared object.
547
548 @cindex finalization function
549 @kindex -fini
550 @item -fini @var{name}
551 When creating an ELF executable or shared object, call NAME when the
552 executable or shared object is unloaded, by setting DT_FINI to the
553 address of the function. By default, the linker uses @code{_fini} as
554 the function to call.
555
556 @kindex -g
557 @item -g
558 Ignored. Provided for compatibility with other tools.
559
560 @kindex -G
561 @kindex --gpsize
562 @cindex object size
563 @item -G@var{value}
564 @itemx --gpsize=@var{value}
565 Set the maximum size of objects to be optimized using the GP register to
566 @var{size}. This is only meaningful for object file formats such as
567 MIPS ECOFF which supports putting large and small objects into different
568 sections. This is ignored for other object file formats.
569
570 @cindex runtime library name
571 @kindex -h@var{name}
572 @kindex -soname=@var{name}
573 @item -h@var{name}
574 @itemx -soname=@var{name}
575 When creating an ELF shared object, set the internal DT_SONAME field to
576 the specified name. When an executable is linked with a shared object
577 which has a DT_SONAME field, then when the executable is run the dynamic
578 linker will attempt to load the shared object specified by the DT_SONAME
579 field rather than the using the file name given to the linker.
580
581 @kindex -i
582 @cindex incremental link
583 @item -i
584 Perform an incremental link (same as option @samp{-r}).
585
586 @cindex initialization function
587 @kindex -init
588 @item -init @var{name}
589 When creating an ELF executable or shared object, call NAME when the
590 executable or shared object is loaded, by setting DT_INIT to the address
591 of the function. By default, the linker uses @code{_init} as the
592 function to call.
593
594 @cindex archive files, from cmd line
595 @kindex -l@var{archive}
596 @kindex --library=@var{archive}
597 @item -l@var{archive}
598 @itemx --library=@var{archive}
599 Add archive file @var{archive} to the list of files to link. This
600 option may be used any number of times. @command{ld} will search its
601 path-list for occurrences of @code{lib@var{archive}.a} for every
602 @var{archive} specified.
603
604 On systems which support shared libraries, @command{ld} may also search for
605 libraries with extensions other than @code{.a}. Specifically, on ELF
606 and SunOS systems, @command{ld} will search a directory for a library with
607 an extension of @code{.so} before searching for one with an extension of
608 @code{.a}. By convention, a @code{.so} extension indicates a shared
609 library.
610
611 The linker will search an archive only once, at the location where it is
612 specified on the command line. If the archive defines a symbol which
613 was undefined in some object which appeared before the archive on the
614 command line, the linker will include the appropriate file(s) from the
615 archive. However, an undefined symbol in an object appearing later on
616 the command line will not cause the linker to search the archive again.
617
618 See the @option{-(} option for a way to force the linker to search
619 archives multiple times.
620
621 You may list the same archive multiple times on the command line.
622
623 @ifset GENERIC
624 This type of archive searching is standard for Unix linkers. However,
625 if you are using @command{ld} on AIX, note that it is different from the
626 behaviour of the AIX linker.
627 @end ifset
628
629 @cindex search directory, from cmd line
630 @kindex -L@var{dir}
631 @kindex --library-path=@var{dir}
632 @item -L@var{searchdir}
633 @itemx --library-path=@var{searchdir}
634 Add path @var{searchdir} to the list of paths that @command{ld} will search
635 for archive libraries and @command{ld} control scripts. You may use this
636 option any number of times. The directories are searched in the order
637 in which they are specified on the command line. Directories specified
638 on the command line are searched before the default directories. All
639 @option{-L} options apply to all @option{-l} options, regardless of the
640 order in which the options appear.
641
642 If @var{searchdir} begins with @code{=}, then the @code{=} will be replaced
643 by the @dfn{sysroot prefix}, a path specified when the linker is configured.
644
645 @ifset UsesEnvVars
646 The default set of paths searched (without being specified with
647 @samp{-L}) depends on which emulation mode @command{ld} is using, and in
648 some cases also on how it was configured. @xref{Environment}.
649 @end ifset
650
651 The paths can also be specified in a link script with the
652 @code{SEARCH_DIR} command. Directories specified this way are searched
653 at the point in which the linker script appears in the command line.
654
655 @cindex emulation
656 @kindex -m @var{emulation}
657 @item -m@var{emulation}
658 Emulate the @var{emulation} linker. You can list the available
659 emulations with the @samp{--verbose} or @samp{-V} options.
660
661 If the @samp{-m} option is not used, the emulation is taken from the
662 @code{LDEMULATION} environment variable, if that is defined.
663
664 Otherwise, the default emulation depends upon how the linker was
665 configured.
666
667 @cindex link map
668 @kindex -M
669 @kindex --print-map
670 @item -M
671 @itemx --print-map
672 Print a link map to the standard output. A link map provides
673 information about the link, including the following:
674
675 @itemize @bullet
676 @item
677 Where object files are mapped into memory.
678 @item
679 How common symbols are allocated.
680 @item
681 All archive members included in the link, with a mention of the symbol
682 which caused the archive member to be brought in.
683 @item
684 The values assigned to symbols.
685
686 Note - symbols whose values are computed by an expression which
687 involves a reference to a previous value of the same symbol may not
688 have correct result displayed in the link map. This is because the
689 linker discards intermediate results and only retains the final value
690 of an expression. Under such circumstances the linker will display
691 the final value enclosed by square brackets. Thus for example a
692 linker script containing:
693
694 @smallexample
695 foo = 1
696 foo = foo * 4
697 foo = foo + 8
698 @end smallexample
699
700 will produce the following output in the link map if the @option{-M}
701 option is used:
702
703 @smallexample
704 0x00000001 foo = 0x1
705 [0x0000000c] foo = (foo * 0x4)
706 [0x0000000c] foo = (foo + 0x8)
707 @end smallexample
708
709 See @ref{Expressions} for more information about expressions in linker
710 scripts.
711 @end itemize
712
713 @kindex -n
714 @cindex read-only text
715 @cindex NMAGIC
716 @kindex --nmagic
717 @item -n
718 @itemx --nmagic
719 Turn off page alignment of sections, and mark the output as
720 @code{NMAGIC} if possible.
721
722 @kindex -N
723 @kindex --omagic
724 @cindex read/write from cmd line
725 @cindex OMAGIC
726 @item -N
727 @itemx --omagic
728 Set the text and data sections to be readable and writable. Also, do
729 not page-align the data segment, and disable linking against shared
730 libraries. If the output format supports Unix style magic numbers,
731 mark the output as @code{OMAGIC}. Note: Although a writable text section
732 is allowed for PE-COFF targets, it does not conform to the format
733 specification published by Microsoft.
734
735 @kindex --no-omagic
736 @cindex OMAGIC
737 @item --no-omagic
738 This option negates most of the effects of the @option{-N} option. It
739 sets the text section to be read-only, and forces the data segment to
740 be page-aligned. Note - this option does not enable linking against
741 shared libraries. Use @option{-Bdynamic} for this.
742
743 @kindex -o @var{output}
744 @kindex --output=@var{output}
745 @cindex naming the output file
746 @item -o @var{output}
747 @itemx --output=@var{output}
748 Use @var{output} as the name for the program produced by @command{ld}; if this
749 option is not specified, the name @file{a.out} is used by default. The
750 script command @code{OUTPUT} can also specify the output file name.
751
752 @kindex -O @var{level}
753 @cindex generating optimized output
754 @item -O @var{level}
755 If @var{level} is a numeric values greater than zero @command{ld} optimizes
756 the output. This might take significantly longer and therefore probably
757 should only be enabled for the final binary.
758
759 @kindex -q
760 @kindex --emit-relocs
761 @cindex retain relocations in final executable
762 @item -q
763 @itemx --emit-relocs
764 Leave relocation sections and contents in fully linked exececutables.
765 Post link analysis and optimization tools may need this information in
766 order to perform correct modifications of executables. This results
767 in larger executables.
768
769 This option is currently only supported on ELF platforms.
770
771 @cindex partial link
772 @cindex relocatable output
773 @kindex -r
774 @kindex --relocatable
775 @item -r
776 @itemx --relocatable
777 Generate relocatable output---i.e., generate an output file that can in
778 turn serve as input to @command{ld}. This is often called @dfn{partial
779 linking}. As a side effect, in environments that support standard Unix
780 magic numbers, this option also sets the output file's magic number to
781 @code{OMAGIC}.
782 @c ; see @option{-N}.
783 If this option is not specified, an absolute file is produced. When
784 linking C++ programs, this option @emph{will not} resolve references to
785 constructors; to do that, use @samp{-Ur}.
786
787 When an input file does not have the same format as the output file,
788 partial linking is only supported if that input file does not contain any
789 relocations. Different output formats can have further restrictions; for
790 example some @code{a.out}-based formats do not support partial linking
791 with input files in other formats at all.
792
793 This option does the same thing as @samp{-i}.
794
795 @kindex -R @var{file}
796 @kindex --just-symbols=@var{file}
797 @cindex symbol-only input
798 @item -R @var{filename}
799 @itemx --just-symbols=@var{filename}
800 Read symbol names and their addresses from @var{filename}, but do not
801 relocate it or include it in the output. This allows your output file
802 to refer symbolically to absolute locations of memory defined in other
803 programs. You may use this option more than once.
804
805 For compatibility with other ELF linkers, if the @option{-R} option is
806 followed by a directory name, rather than a file name, it is treated as
807 the @option{-rpath} option.
808
809 @kindex -s
810 @kindex --strip-all
811 @cindex strip all symbols
812 @item -s
813 @itemx --strip-all
814 Omit all symbol information from the output file.
815
816 @kindex -S
817 @kindex --strip-debug
818 @cindex strip debugger symbols
819 @item -S
820 @itemx --strip-debug
821 Omit debugger symbol information (but not all symbols) from the output file.
822
823 @kindex -t
824 @kindex --trace
825 @cindex input files, displaying
826 @item -t
827 @itemx --trace
828 Print the names of the input files as @command{ld} processes them.
829
830 @kindex -T @var{script}
831 @kindex --script=@var{script}
832 @cindex script files
833 @item -T @var{scriptfile}
834 @itemx --script=@var{scriptfile}
835 Use @var{scriptfile} as the linker script. This script replaces
836 @command{ld}'s default linker script (rather than adding to it), so
837 @var{commandfile} must specify everything necessary to describe the
838 output file. @xref{Scripts}. If @var{scriptfile} does not exist in
839 the current directory, @code{ld} looks for it in the directories
840 specified by any preceding @samp{-L} options. Multiple @samp{-T}
841 options accumulate.
842
843 @kindex -u @var{symbol}
844 @kindex --undefined=@var{symbol}
845 @cindex undefined symbol
846 @item -u @var{symbol}
847 @itemx --undefined=@var{symbol}
848 Force @var{symbol} to be entered in the output file as an undefined
849 symbol. Doing this may, for example, trigger linking of additional
850 modules from standard libraries. @samp{-u} may be repeated with
851 different option arguments to enter additional undefined symbols. This
852 option is equivalent to the @code{EXTERN} linker script command.
853
854 @kindex -Ur
855 @cindex constructors
856 @item -Ur
857 For anything other than C++ programs, this option is equivalent to
858 @samp{-r}: it generates relocatable output---i.e., an output file that can in
859 turn serve as input to @command{ld}. When linking C++ programs, @samp{-Ur}
860 @emph{does} resolve references to constructors, unlike @samp{-r}.
861 It does not work to use @samp{-Ur} on files that were themselves linked
862 with @samp{-Ur}; once the constructor table has been built, it cannot
863 be added to. Use @samp{-Ur} only for the last partial link, and
864 @samp{-r} for the others.
865
866 @kindex --unique[=@var{SECTION}]
867 @item --unique[=@var{SECTION}]
868 Creates a separate output section for every input section matching
869 @var{SECTION}, or if the optional wildcard @var{SECTION} argument is
870 missing, for every orphan input section. An orphan section is one not
871 specifically mentioned in a linker script. You may use this option
872 multiple times on the command line; It prevents the normal merging of
873 input sections with the same name, overriding output section assignments
874 in a linker script.
875
876 @kindex -v
877 @kindex -V
878 @kindex --version
879 @cindex version
880 @item -v
881 @itemx --version
882 @itemx -V
883 Display the version number for @command{ld}. The @option{-V} option also
884 lists the supported emulations.
885
886 @kindex -x
887 @kindex --discard-all
888 @cindex deleting local symbols
889 @item -x
890 @itemx --discard-all
891 Delete all local symbols.
892
893 @kindex -X
894 @kindex --discard-locals
895 @cindex local symbols, deleting
896 @cindex L, deleting symbols beginning
897 @item -X
898 @itemx --discard-locals
899 Delete all temporary local symbols. For most targets, this is all local
900 symbols whose names begin with @samp{L}.
901
902 @kindex -y @var{symbol}
903 @kindex --trace-symbol=@var{symbol}
904 @cindex symbol tracing
905 @item -y @var{symbol}
906 @itemx --trace-symbol=@var{symbol}
907 Print the name of each linked file in which @var{symbol} appears. This
908 option may be given any number of times. On many systems it is necessary
909 to prepend an underscore.
910
911 This option is useful when you have an undefined symbol in your link but
912 don't know where the reference is coming from.
913
914 @kindex -Y @var{path}
915 @item -Y @var{path}
916 Add @var{path} to the default library search path. This option exists
917 for Solaris compatibility.
918
919 @kindex -z @var{keyword}
920 @item -z @var{keyword}
921 The recognized keywords are:
922 @table @samp
923
924 @item combreloc
925 Combines multiple reloc sections and sorts them to make dynamic symbol
926 lookup caching possible.
927
928 @item defs
929 Disallows undefined symbols in object files. Undefined symbols in
930 shared libraries are still allowed.
931
932 @item initfirst
933 This option is only meaningful when building a shared object.
934 It marks the object so that its runtime initialization will occur
935 before the runtime initialization of any other objects brought into
936 the process at the same time. Similarly the runtime finalization of
937 the object will occur after the runtime finalization of any other
938 objects.
939
940 @item interpose
941 Marks the object that its symbol table interposes before all symbols
942 but the primary executable.
943
944 @item loadfltr
945 Marks the object that its filters be processed immediately at
946 runtime.
947
948 @item muldefs
949 Allows multiple definitions.
950
951 @item nocombreloc
952 Disables multiple reloc sections combining.
953
954 @item nocopyreloc
955 Disables production of copy relocs.
956
957 @item nodefaultlib
958 Marks the object that the search for dependencies of this object will
959 ignore any default library search paths.
960
961 @item nodelete
962 Marks the object shouldn't be unloaded at runtime.
963
964 @item nodlopen
965 Marks the object not available to @code{dlopen}.
966
967 @item nodump
968 Marks the object can not be dumped by @code{dldump}.
969
970 @item now
971 When generating an executable or shared library, mark it to tell the
972 dynamic linker to resolve all symbols when the program is started, or
973 when the shared library is linked to using dlopen, instead of
974 deferring function call resolution to the point when the function is
975 first called.
976
977 @item origin
978 Marks the object may contain $ORIGIN.
979
980 @end table
981
982 Other keywords are ignored for Solaris compatibility.
983
984 @kindex -(
985 @cindex groups of archives
986 @item -( @var{archives} -)
987 @itemx --start-group @var{archives} --end-group
988 The @var{archives} should be a list of archive files. They may be
989 either explicit file names, or @samp{-l} options.
990
991 The specified archives are searched repeatedly until no new undefined
992 references are created. Normally, an archive is searched only once in
993 the order that it is specified on the command line. If a symbol in that
994 archive is needed to resolve an undefined symbol referred to by an
995 object in an archive that appears later on the command line, the linker
996 would not be able to resolve that reference. By grouping the archives,
997 they all be searched repeatedly until all possible references are
998 resolved.
999
1000 Using this option has a significant performance cost. It is best to use
1001 it only when there are unavoidable circular references between two or
1002 more archives.
1003
1004 @kindex --accept-unknown-input-arch
1005 @kindex --no-accept-unknown-input-arch
1006 @item --accept-unknown-input-arch
1007 @itemx --no-accept-unknown-input-arch
1008 Tells the linker to accept input files whose architecture cannot be
1009 recognised. The assumption is that the user knows what they are doing
1010 and deliberately wants to link in these unknown input files. This was
1011 the default behaviour of the linker, before release 2.14. The default
1012 behaviour from release 2.14 onwards is to reject such input files, and
1013 so the @samp{--accept-unknown-input-arch} option has been added to
1014 restore the old behaviour.
1015
1016 @kindex --as-needed
1017 @kindex --no-as-needed
1018 @item --as-needed
1019 @itemx --no-as-needed
1020 This option affects ELF DT_NEEDED tags for dynamic libraries mentioned
1021 on the command line after the @option{--as-needed} option. Normally,
1022 the linker will add a DT_NEEDED tag for each dynamic library mentioned
1023 on the command line, regardless of whether the library is actually
1024 needed. @option{--as-needed} causes DT_NEEDED tags to only be emitted
1025 for libraries that satisfy some symbol reference from regular objects
1026 which is undefined at the point that the library was linked.
1027 @option{--no-as-needed} restores the default behaviour.
1028
1029 @kindex --add-needed
1030 @kindex --no-add-needed
1031 @item --add-needed
1032 @itemx --no-add-needed
1033 This option affects the treatment of dynamic libraries from ELF
1034 DT_NEEDED tags in dynamic libraries mentioned on the command line after
1035 the @option{--no-add-needed} option. Normally, the linker will add
1036 a DT_NEEDED tag for each dynamic library from DT_NEEDED tags.
1037 @option{--no-add-needed} causes DT_NEEDED tags will never be emitted
1038 for those libraries from DT_NEEDED tags. @option{--add-needed} restores
1039 the default behaviour.
1040
1041 @kindex -assert @var{keyword}
1042 @item -assert @var{keyword}
1043 This option is ignored for SunOS compatibility.
1044
1045 @kindex -Bdynamic
1046 @kindex -dy
1047 @kindex -call_shared
1048 @item -Bdynamic
1049 @itemx -dy
1050 @itemx -call_shared
1051 Link against dynamic libraries. This is only meaningful on platforms
1052 for which shared libraries are supported. This option is normally the
1053 default on such platforms. The different variants of this option are
1054 for compatibility with various systems. You may use this option
1055 multiple times on the command line: it affects library searching for
1056 @option{-l} options which follow it.
1057
1058 @kindex -Bgroup
1059 @item -Bgroup
1060 Set the @code{DF_1_GROUP} flag in the @code{DT_FLAGS_1} entry in the dynamic
1061 section. This causes the runtime linker to handle lookups in this
1062 object and its dependencies to be performed only inside the group.
1063 @option{--unresolved-symbols=report-all} is implied. This option is
1064 only meaningful on ELF platforms which support shared libraries.
1065
1066 @kindex -Bstatic
1067 @kindex -dn
1068 @kindex -non_shared
1069 @kindex -static
1070 @item -Bstatic
1071 @itemx -dn
1072 @itemx -non_shared
1073 @itemx -static
1074 Do not link against shared libraries. This is only meaningful on
1075 platforms for which shared libraries are supported. The different
1076 variants of this option are for compatibility with various systems. You
1077 may use this option multiple times on the command line: it affects
1078 library searching for @option{-l} options which follow it. This
1079 option also implies @option{--unresolved-symbols=report-all}. This
1080 option can be used with @option{-shared}. Doing so means that a
1081 shared library is being created but that all of the library's external
1082 references must be resolved by pulling in entries from static
1083 libraries.
1084
1085 @kindex -Bsymbolic
1086 @item -Bsymbolic
1087 When creating a shared library, bind references to global symbols to the
1088 definition within the shared library, if any. Normally, it is possible
1089 for a program linked against a shared library to override the definition
1090 within the shared library. This option is only meaningful on ELF
1091 platforms which support shared libraries.
1092
1093 @kindex --check-sections
1094 @kindex --no-check-sections
1095 @item --check-sections
1096 @itemx --no-check-sections
1097 Asks the linker @emph{not} to check section addresses after they have
1098 been assigned to see if there any overlaps. Normally the linker will
1099 perform this check, and if it finds any overlaps it will produce
1100 suitable error messages. The linker does know about, and does make
1101 allowances for sections in overlays. The default behaviour can be
1102 restored by using the command line switch @option{--check-sections}.
1103
1104 @cindex cross reference table
1105 @kindex --cref
1106 @item --cref
1107 Output a cross reference table. If a linker map file is being
1108 generated, the cross reference table is printed to the map file.
1109 Otherwise, it is printed on the standard output.
1110
1111 The format of the table is intentionally simple, so that it may be
1112 easily processed by a script if necessary. The symbols are printed out,
1113 sorted by name. For each symbol, a list of file names is given. If the
1114 symbol is defined, the first file listed is the location of the
1115 definition. The remaining files contain references to the symbol.
1116
1117 @cindex common allocation
1118 @kindex --no-define-common
1119 @item --no-define-common
1120 This option inhibits the assignment of addresses to common symbols.
1121 The script command @code{INHIBIT_COMMON_ALLOCATION} has the same effect.
1122 @xref{Miscellaneous Commands}.
1123
1124 The @samp{--no-define-common} option allows decoupling
1125 the decision to assign addresses to Common symbols from the choice
1126 of the output file type; otherwise a non-Relocatable output type
1127 forces assigning addresses to Common symbols.
1128 Using @samp{--no-define-common} allows Common symbols that are referenced
1129 from a shared library to be assigned addresses only in the main program.
1130 This eliminates the unused duplicate space in the shared library,
1131 and also prevents any possible confusion over resolving to the wrong
1132 duplicate when there are many dynamic modules with specialized search
1133 paths for runtime symbol resolution.
1134
1135 @cindex symbols, from command line
1136 @kindex --defsym @var{symbol}=@var{exp}
1137 @item --defsym @var{symbol}=@var{expression}
1138 Create a global symbol in the output file, containing the absolute
1139 address given by @var{expression}. You may use this option as many
1140 times as necessary to define multiple symbols in the command line. A
1141 limited form of arithmetic is supported for the @var{expression} in this
1142 context: you may give a hexadecimal constant or the name of an existing
1143 symbol, or use @code{+} and @code{-} to add or subtract hexadecimal
1144 constants or symbols. If you need more elaborate expressions, consider
1145 using the linker command language from a script (@pxref{Assignments,,
1146 Assignment: Symbol Definitions}). @emph{Note:} there should be no white
1147 space between @var{symbol}, the equals sign (``@key{=}''), and
1148 @var{expression}.
1149
1150 @cindex demangling, from command line
1151 @kindex --demangle[=@var{style}]
1152 @kindex --no-demangle
1153 @item --demangle[=@var{style}]
1154 @itemx --no-demangle
1155 These options control whether to demangle symbol names in error messages
1156 and other output. When the linker is told to demangle, it tries to
1157 present symbol names in a readable fashion: it strips leading
1158 underscores if they are used by the object file format, and converts C++
1159 mangled symbol names into user readable names. Different compilers have
1160 different mangling styles. The optional demangling style argument can be used
1161 to choose an appropriate demangling style for your compiler. The linker will
1162 demangle by default unless the environment variable @samp{COLLECT_NO_DEMANGLE}
1163 is set. These options may be used to override the default.
1164
1165 @cindex dynamic linker, from command line
1166 @kindex -I@var{file}
1167 @kindex --dynamic-linker @var{file}
1168 @item --dynamic-linker @var{file}
1169 Set the name of the dynamic linker. This is only meaningful when
1170 generating dynamically linked ELF executables. The default dynamic
1171 linker is normally correct; don't use this unless you know what you are
1172 doing.
1173
1174
1175 @kindex --fatal-warnings
1176 @item --fatal-warnings
1177 Treat all warnings as errors.
1178
1179 @kindex --force-exe-suffix
1180 @item --force-exe-suffix
1181 Make sure that an output file has a .exe suffix.
1182
1183 If a successfully built fully linked output file does not have a
1184 @code{.exe} or @code{.dll} suffix, this option forces the linker to copy
1185 the output file to one of the same name with a @code{.exe} suffix. This
1186 option is useful when using unmodified Unix makefiles on a Microsoft
1187 Windows host, since some versions of Windows won't run an image unless
1188 it ends in a @code{.exe} suffix.
1189
1190 @kindex --gc-sections
1191 @kindex --no-gc-sections
1192 @cindex garbage collection
1193 @item --no-gc-sections
1194 @itemx --gc-sections
1195 Enable garbage collection of unused input sections. It is ignored on
1196 targets that do not support this option. This option is not compatible
1197 with @samp{-r}. The default behaviour (of not performing this garbage
1198 collection) can be restored by specifying @samp{--no-gc-sections} on
1199 the command line.
1200
1201 @cindex help
1202 @cindex usage
1203 @kindex --help
1204 @item --help
1205 Print a summary of the command-line options on the standard output and exit.
1206
1207 @kindex --target-help
1208 @item --target-help
1209 Print a summary of all target specific options on the standard output and exit.
1210
1211 @kindex -Map
1212 @item -Map @var{mapfile}
1213 Print a link map to the file @var{mapfile}. See the description of the
1214 @option{-M} option, above.
1215
1216 @cindex memory usage
1217 @kindex --no-keep-memory
1218 @item --no-keep-memory
1219 @command{ld} normally optimizes for speed over memory usage by caching the
1220 symbol tables of input files in memory. This option tells @command{ld} to
1221 instead optimize for memory usage, by rereading the symbol tables as
1222 necessary. This may be required if @command{ld} runs out of memory space
1223 while linking a large executable.
1224
1225 @kindex --no-undefined
1226 @kindex -z defs
1227 @item --no-undefined
1228 @itemx -z defs
1229 Report unresolved symbol references from regular object files. This
1230 is done even if the linker is creating a non-symbolic shared library.
1231 The switch @option{--[no-]allow-shlib-undefined} controls the
1232 behaviour for reporting unresolved references found in shared
1233 libraries being linked in.
1234
1235 @kindex --allow-multiple-definition
1236 @kindex -z muldefs
1237 @item --allow-multiple-definition
1238 @itemx -z muldefs
1239 Normally when a symbol is defined multiple times, the linker will
1240 report a fatal error. These options allow multiple definitions and the
1241 first definition will be used.
1242
1243 @kindex --allow-shlib-undefined
1244 @kindex --no-allow-shlib-undefined
1245 @item --allow-shlib-undefined
1246 @itemx --no-allow-shlib-undefined
1247 Allows (the default) or disallows undefined symbols in shared libraries.
1248 This switch is similar to @option{--no-undefined} except that it
1249 determines the behaviour when the undefined symbols are in a
1250 shared library rather than a regular object file. It does not affect
1251 how undefined symbols in regular object files are handled.
1252
1253 The reason that @option{--allow-shlib-undefined} is the default is that
1254 the shared library being specified at link time may not be the same as
1255 the one that is available at load time, so the symbols might actually be
1256 resolvable at load time. Plus there are some systems, (eg BeOS) where
1257 undefined symbols in shared libraries is normal. (The kernel patches
1258 them at load time to select which function is most appropriate
1259 for the current architecture. This is used for example to dynamically
1260 select an appropriate memset function). Apparently it is also normal
1261 for HPPA shared libraries to have undefined symbols.
1262
1263 @kindex --no-undefined-version
1264 @item --no-undefined-version
1265 Normally when a symbol has an undefined version, the linker will ignore
1266 it. This option disallows symbols with undefined version and a fatal error
1267 will be issued instead.
1268
1269 @kindex --default-symver
1270 @item --default-symver
1271 Create and use a default symbol version (the soname) for unversioned
1272 exported symbols.
1273
1274 @kindex --default-imported-symver
1275 @item --default-imported-symver
1276 Create and use a default symbol version (the soname) for unversioned
1277 imported symbols.
1278
1279 @kindex --no-warn-mismatch
1280 @item --no-warn-mismatch
1281 Normally @command{ld} will give an error if you try to link together input
1282 files that are mismatched for some reason, perhaps because they have
1283 been compiled for different processors or for different endiannesses.
1284 This option tells @command{ld} that it should silently permit such possible
1285 errors. This option should only be used with care, in cases when you
1286 have taken some special action that ensures that the linker errors are
1287 inappropriate.
1288
1289 @kindex --no-whole-archive
1290 @item --no-whole-archive
1291 Turn off the effect of the @option{--whole-archive} option for subsequent
1292 archive files.
1293
1294 @cindex output file after errors
1295 @kindex --noinhibit-exec
1296 @item --noinhibit-exec
1297 Retain the executable output file whenever it is still usable.
1298 Normally, the linker will not produce an output file if it encounters
1299 errors during the link process; it exits without writing an output file
1300 when it issues any error whatsoever.
1301
1302 @kindex -nostdlib
1303 @item -nostdlib
1304 Only search library directories explicitly specified on the
1305 command line. Library directories specified in linker scripts
1306 (including linker scripts specified on the command line) are ignored.
1307
1308 @ifclear SingleFormat
1309 @kindex --oformat
1310 @item --oformat @var{output-format}
1311 @command{ld} may be configured to support more than one kind of object
1312 file. If your @command{ld} is configured this way, you can use the
1313 @samp{--oformat} option to specify the binary format for the output
1314 object file. Even when @command{ld} is configured to support alternative
1315 object formats, you don't usually need to specify this, as @command{ld}
1316 should be configured to produce as a default output format the most
1317 usual format on each machine. @var{output-format} is a text string, the
1318 name of a particular format supported by the BFD libraries. (You can
1319 list the available binary formats with @samp{objdump -i}.) The script
1320 command @code{OUTPUT_FORMAT} can also specify the output format, but
1321 this option overrides it. @xref{BFD}.
1322 @end ifclear
1323
1324 @kindex -pie
1325 @kindex --pic-executable
1326 @item -pie
1327 @itemx --pic-executable
1328 @cindex position independent executables
1329 Create a position independent executable. This is currently only supported on
1330 ELF platforms. Position independent executables are similar to shared
1331 libraries in that they are relocated by the dynamic linker to the virtual
1332 address the OS chooses for them (which can vary between invocations). Like
1333 normal dynamically linked executables they can be executed and symbols
1334 defined in the executable cannot be overridden by shared libraries.
1335
1336 @kindex -qmagic
1337 @item -qmagic
1338 This option is ignored for Linux compatibility.
1339
1340 @kindex -Qy
1341 @item -Qy
1342 This option is ignored for SVR4 compatibility.
1343
1344 @kindex --relax
1345 @cindex synthesizing linker
1346 @cindex relaxing addressing modes
1347 @item --relax
1348 An option with machine dependent effects.
1349 @ifset GENERIC
1350 This option is only supported on a few targets.
1351 @end ifset
1352 @ifset H8300
1353 @xref{H8/300,,@command{ld} and the H8/300}.
1354 @end ifset
1355 @ifset I960
1356 @xref{i960,, @command{ld} and the Intel 960 family}.
1357 @end ifset
1358 @ifset XTENSA
1359 @xref{Xtensa,, @command{ld} and Xtensa Processors}.
1360 @end ifset
1361 @ifset M68HC11
1362 @xref{M68HC11/68HC12,,@command{ld} and the 68HC11 and 68HC12}.
1363 @end ifset
1364
1365 On some platforms, the @samp{--relax} option performs global
1366 optimizations that become possible when the linker resolves addressing
1367 in the program, such as relaxing address modes and synthesizing new
1368 instructions in the output object file.
1369
1370 On some platforms these link time global optimizations may make symbolic
1371 debugging of the resulting executable impossible.
1372 @ifset GENERIC
1373 This is known to be
1374 the case for the Matsushita MN10200 and MN10300 family of processors.
1375 @end ifset
1376
1377 @ifset GENERIC
1378 On platforms where this is not supported, @samp{--relax} is accepted,
1379 but ignored.
1380 @end ifset
1381
1382 @cindex retaining specified symbols
1383 @cindex stripping all but some symbols
1384 @cindex symbols, retaining selectively
1385 @item --retain-symbols-file @var{filename}
1386 Retain @emph{only} the symbols listed in the file @var{filename},
1387 discarding all others. @var{filename} is simply a flat file, with one
1388 symbol name per line. This option is especially useful in environments
1389 @ifset GENERIC
1390 (such as VxWorks)
1391 @end ifset
1392 where a large global symbol table is accumulated gradually, to conserve
1393 run-time memory.
1394
1395 @samp{--retain-symbols-file} does @emph{not} discard undefined symbols,
1396 or symbols needed for relocations.
1397
1398 You may only specify @samp{--retain-symbols-file} once in the command
1399 line. It overrides @samp{-s} and @samp{-S}.
1400
1401 @ifset GENERIC
1402 @item -rpath @var{dir}
1403 @cindex runtime library search path
1404 @kindex -rpath
1405 Add a directory to the runtime library search path. This is used when
1406 linking an ELF executable with shared objects. All @option{-rpath}
1407 arguments are concatenated and passed to the runtime linker, which uses
1408 them to locate shared objects at runtime. The @option{-rpath} option is
1409 also used when locating shared objects which are needed by shared
1410 objects explicitly included in the link; see the description of the
1411 @option{-rpath-link} option. If @option{-rpath} is not used when linking an
1412 ELF executable, the contents of the environment variable
1413 @code{LD_RUN_PATH} will be used if it is defined.
1414
1415 The @option{-rpath} option may also be used on SunOS. By default, on
1416 SunOS, the linker will form a runtime search patch out of all the
1417 @option{-L} options it is given. If a @option{-rpath} option is used, the
1418 runtime search path will be formed exclusively using the @option{-rpath}
1419 options, ignoring the @option{-L} options. This can be useful when using
1420 gcc, which adds many @option{-L} options which may be on NFS mounted
1421 filesystems.
1422
1423 For compatibility with other ELF linkers, if the @option{-R} option is
1424 followed by a directory name, rather than a file name, it is treated as
1425 the @option{-rpath} option.
1426 @end ifset
1427
1428 @ifset GENERIC
1429 @cindex link-time runtime library search path
1430 @kindex -rpath-link
1431 @item -rpath-link @var{DIR}
1432 When using ELF or SunOS, one shared library may require another. This
1433 happens when an @code{ld -shared} link includes a shared library as one
1434 of the input files.
1435
1436 When the linker encounters such a dependency when doing a non-shared,
1437 non-relocatable link, it will automatically try to locate the required
1438 shared library and include it in the link, if it is not included
1439 explicitly. In such a case, the @option{-rpath-link} option
1440 specifies the first set of directories to search. The
1441 @option{-rpath-link} option may specify a sequence of directory names
1442 either by specifying a list of names separated by colons, or by
1443 appearing multiple times.
1444
1445 This option should be used with caution as it overrides the search path
1446 that may have been hard compiled into a shared library. In such a case it
1447 is possible to use unintentionally a different search path than the
1448 runtime linker would do.
1449
1450 The linker uses the following search paths to locate required shared
1451 libraries.
1452 @enumerate
1453 @item
1454 Any directories specified by @option{-rpath-link} options.
1455 @item
1456 Any directories specified by @option{-rpath} options. The difference
1457 between @option{-rpath} and @option{-rpath-link} is that directories
1458 specified by @option{-rpath} options are included in the executable and
1459 used at runtime, whereas the @option{-rpath-link} option is only effective
1460 at link time. It is for the native linker only.
1461 @item
1462 On an ELF system, if the @option{-rpath} and @code{rpath-link} options
1463 were not used, search the contents of the environment variable
1464 @code{LD_RUN_PATH}. It is for the native linker only.
1465 @item
1466 On SunOS, if the @option{-rpath} option was not used, search any
1467 directories specified using @option{-L} options.
1468 @item
1469 For a native linker, the contents of the environment variable
1470 @code{LD_LIBRARY_PATH}.
1471 @item
1472 For a native ELF linker, the directories in @code{DT_RUNPATH} or
1473 @code{DT_RPATH} of a shared library are searched for shared
1474 libraries needed by it. The @code{DT_RPATH} entries are ignored if
1475 @code{DT_RUNPATH} entries exist.
1476 @item
1477 The default directories, normally @file{/lib} and @file{/usr/lib}.
1478 @item
1479 For a native linker on an ELF system, if the file @file{/etc/ld.so.conf}
1480 exists, the list of directories found in that file.
1481 @end enumerate
1482
1483 If the required shared library is not found, the linker will issue a
1484 warning and continue with the link.
1485 @end ifset
1486
1487 @kindex -shared
1488 @kindex -Bshareable
1489 @item -shared
1490 @itemx -Bshareable
1491 @cindex shared libraries
1492 Create a shared library. This is currently only supported on ELF, XCOFF
1493 and SunOS platforms. On SunOS, the linker will automatically create a
1494 shared library if the @option{-e} option is not used and there are
1495 undefined symbols in the link.
1496
1497 @item --sort-common
1498 @kindex --sort-common
1499 This option tells @command{ld} to sort the common symbols by size when it
1500 places them in the appropriate output sections. First come all the one
1501 byte symbols, then all the two byte, then all the four byte, and then
1502 everything else. This is to prevent gaps between symbols due to
1503 alignment constraints.
1504
1505 @kindex --sort-section name
1506 @item --sort-section name
1507 This option will apply @code{SORT_BY_NAME} to all wildcard section
1508 patterns in the linker script.
1509
1510 @kindex --sort-section alignment
1511 @item --sort-section alignment
1512 This option will apply @code{SORT_BY_ALIGNMENT} to all wildcard section
1513 patterns in the linker script.
1514
1515 @kindex --split-by-file
1516 @item --split-by-file [@var{size}]
1517 Similar to @option{--split-by-reloc} but creates a new output section for
1518 each input file when @var{size} is reached. @var{size} defaults to a
1519 size of 1 if not given.
1520
1521 @kindex --split-by-reloc
1522 @item --split-by-reloc [@var{count}]
1523 Tries to creates extra sections in the output file so that no single
1524 output section in the file contains more than @var{count} relocations.
1525 This is useful when generating huge relocatable files for downloading into
1526 certain real time kernels with the COFF object file format; since COFF
1527 cannot represent more than 65535 relocations in a single section. Note
1528 that this will fail to work with object file formats which do not
1529 support arbitrary sections. The linker will not split up individual
1530 input sections for redistribution, so if a single input section contains
1531 more than @var{count} relocations one output section will contain that
1532 many relocations. @var{count} defaults to a value of 32768.
1533
1534 @kindex --stats
1535 @item --stats
1536 Compute and display statistics about the operation of the linker, such
1537 as execution time and memory usage.
1538
1539 @kindex --sysroot
1540 @item --sysroot=@var{directory}
1541 Use @var{directory} as the location of the sysroot, overriding the
1542 configure-time default. This option is only supported by linkers
1543 that were configured using @option{--with-sysroot}.
1544
1545 @kindex --traditional-format
1546 @cindex traditional format
1547 @item --traditional-format
1548 For some targets, the output of @command{ld} is different in some ways from
1549 the output of some existing linker. This switch requests @command{ld} to
1550 use the traditional format instead.
1551
1552 @cindex dbx
1553 For example, on SunOS, @command{ld} combines duplicate entries in the
1554 symbol string table. This can reduce the size of an output file with
1555 full debugging information by over 30 percent. Unfortunately, the SunOS
1556 @code{dbx} program can not read the resulting program (@code{gdb} has no
1557 trouble). The @samp{--traditional-format} switch tells @command{ld} to not
1558 combine duplicate entries.
1559
1560 @kindex --section-start @var{sectionname}=@var{org}
1561 @item --section-start @var{sectionname}=@var{org}
1562 Locate a section in the output file at the absolute
1563 address given by @var{org}. You may use this option as many
1564 times as necessary to locate multiple sections in the command
1565 line.
1566 @var{org} must be a single hexadecimal integer;
1567 for compatibility with other linkers, you may omit the leading
1568 @samp{0x} usually associated with hexadecimal values. @emph{Note:} there
1569 should be no white space between @var{sectionname}, the equals
1570 sign (``@key{=}''), and @var{org}.
1571
1572 @kindex -Tbss @var{org}
1573 @kindex -Tdata @var{org}
1574 @kindex -Ttext @var{org}
1575 @cindex segment origins, cmd line
1576 @item -Tbss @var{org}
1577 @itemx -Tdata @var{org}
1578 @itemx -Ttext @var{org}
1579 Same as --section-start, with @code{.bss}, @code{.data} or
1580 @code{.text} as the @var{sectionname}.
1581
1582 @kindex --unresolved-symbols
1583 @item --unresolved-symbols=@var{method}
1584 Determine how to handle unresolved symbols. There are four possible
1585 values for @samp{method}:
1586
1587 @table @samp
1588 @item ignore-all
1589 Do not report any unresolved symbols.
1590
1591 @item report-all
1592 Report all unresolved symbols. This is the default.
1593
1594 @item ignore-in-object-files
1595 Report unresolved symbols that are contained in shared libraries, but
1596 ignore them if they come from regular object files.
1597
1598 @item ignore-in-shared-libs
1599 Report unresolved symbols that come from regular object files, but
1600 ignore them if they come from shared libraries. This can be useful
1601 when creating a dynamic binary and it is known that all the shared
1602 libraries that it should be referencing are included on the linker's
1603 command line.
1604 @end table
1605
1606 The behaviour for shared libraries on their own can also be controlled
1607 by the @option{--[no-]allow-shlib-undefined} option.
1608
1609 Normally the linker will generate an error message for each reported
1610 unresolved symbol but the option @option{--warn-unresolved-symbols}
1611 can change this to a warning.
1612
1613 @kindex --verbose
1614 @cindex verbose
1615 @item --dll-verbose
1616 @itemx --verbose
1617 Display the version number for @command{ld} and list the linker emulations
1618 supported. Display which input files can and cannot be opened. Display
1619 the linker script being used by the linker.
1620
1621 @kindex --version-script=@var{version-scriptfile}
1622 @cindex version script, symbol versions
1623 @itemx --version-script=@var{version-scriptfile}
1624 Specify the name of a version script to the linker. This is typically
1625 used when creating shared libraries to specify additional information
1626 about the version hierarchy for the library being created. This option
1627 is only meaningful on ELF platforms which support shared libraries.
1628 @xref{VERSION}.
1629
1630 @kindex --warn-common
1631 @cindex warnings, on combining symbols
1632 @cindex combining symbols, warnings on
1633 @item --warn-common
1634 Warn when a common symbol is combined with another common symbol or with
1635 a symbol definition. Unix linkers allow this somewhat sloppy practise,
1636 but linkers on some other operating systems do not. This option allows
1637 you to find potential problems from combining global symbols.
1638 Unfortunately, some C libraries use this practise, so you may get some
1639 warnings about symbols in the libraries as well as in your programs.
1640
1641 There are three kinds of global symbols, illustrated here by C examples:
1642
1643 @table @samp
1644 @item int i = 1;
1645 A definition, which goes in the initialized data section of the output
1646 file.
1647
1648 @item extern int i;
1649 An undefined reference, which does not allocate space.
1650 There must be either a definition or a common symbol for the
1651 variable somewhere.
1652
1653 @item int i;
1654 A common symbol. If there are only (one or more) common symbols for a
1655 variable, it goes in the uninitialized data area of the output file.
1656 The linker merges multiple common symbols for the same variable into a
1657 single symbol. If they are of different sizes, it picks the largest
1658 size. The linker turns a common symbol into a declaration, if there is
1659 a definition of the same variable.
1660 @end table
1661
1662 The @samp{--warn-common} option can produce five kinds of warnings.
1663 Each warning consists of a pair of lines: the first describes the symbol
1664 just encountered, and the second describes the previous symbol
1665 encountered with the same name. One or both of the two symbols will be
1666 a common symbol.
1667
1668 @enumerate
1669 @item
1670 Turning a common symbol into a reference, because there is already a
1671 definition for the symbol.
1672 @smallexample
1673 @var{file}(@var{section}): warning: common of `@var{symbol}'
1674 overridden by definition
1675 @var{file}(@var{section}): warning: defined here
1676 @end smallexample
1677
1678 @item
1679 Turning a common symbol into a reference, because a later definition for
1680 the symbol is encountered. This is the same as the previous case,
1681 except that the symbols are encountered in a different order.
1682 @smallexample
1683 @var{file}(@var{section}): warning: definition of `@var{symbol}'
1684 overriding common
1685 @var{file}(@var{section}): warning: common is here
1686 @end smallexample
1687
1688 @item
1689 Merging a common symbol with a previous same-sized common symbol.
1690 @smallexample
1691 @var{file}(@var{section}): warning: multiple common
1692 of `@var{symbol}'
1693 @var{file}(@var{section}): warning: previous common is here
1694 @end smallexample
1695
1696 @item
1697 Merging a common symbol with a previous larger common symbol.
1698 @smallexample
1699 @var{file}(@var{section}): warning: common of `@var{symbol}'
1700 overridden by larger common
1701 @var{file}(@var{section}): warning: larger common is here
1702 @end smallexample
1703
1704 @item
1705 Merging a common symbol with a previous smaller common symbol. This is
1706 the same as the previous case, except that the symbols are
1707 encountered in a different order.
1708 @smallexample
1709 @var{file}(@var{section}): warning: common of `@var{symbol}'
1710 overriding smaller common
1711 @var{file}(@var{section}): warning: smaller common is here
1712 @end smallexample
1713 @end enumerate
1714
1715 @kindex --warn-constructors
1716 @item --warn-constructors
1717 Warn if any global constructors are used. This is only useful for a few
1718 object file formats. For formats like COFF or ELF, the linker can not
1719 detect the use of global constructors.
1720
1721 @kindex --warn-multiple-gp
1722 @item --warn-multiple-gp
1723 Warn if multiple global pointer values are required in the output file.
1724 This is only meaningful for certain processors, such as the Alpha.
1725 Specifically, some processors put large-valued constants in a special
1726 section. A special register (the global pointer) points into the middle
1727 of this section, so that constants can be loaded efficiently via a
1728 base-register relative addressing mode. Since the offset in
1729 base-register relative mode is fixed and relatively small (e.g., 16
1730 bits), this limits the maximum size of the constant pool. Thus, in
1731 large programs, it is often necessary to use multiple global pointer
1732 values in order to be able to address all possible constants. This
1733 option causes a warning to be issued whenever this case occurs.
1734
1735 @kindex --warn-once
1736 @cindex warnings, on undefined symbols
1737 @cindex undefined symbols, warnings on
1738 @item --warn-once
1739 Only warn once for each undefined symbol, rather than once per module
1740 which refers to it.
1741
1742 @kindex --warn-section-align
1743 @cindex warnings, on section alignment
1744 @cindex section alignment, warnings on
1745 @item --warn-section-align
1746 Warn if the address of an output section is changed because of
1747 alignment. Typically, the alignment will be set by an input section.
1748 The address will only be changed if it not explicitly specified; that
1749 is, if the @code{SECTIONS} command does not specify a start address for
1750 the section (@pxref{SECTIONS}).
1751
1752 @kindex --warn-shared-textrel
1753 @item --warn-shared-textrel
1754 Warn if the linker adds a DT_TEXTREL to a shared object.
1755
1756 @kindex --warn-unresolved-symbols
1757 @item --warn-unresolved-symbols
1758 If the linker is going to report an unresolved symbol (see the option
1759 @option{--unresolved-symbols}) it will normally generate an error.
1760 This option makes it generate a warning instead.
1761
1762 @kindex --error-unresolved-symbols
1763 @item --error-unresolved-symbols
1764 This restores the linker's default behaviour of generating errors when
1765 it is reporting unresolved symbols.
1766
1767 @kindex --whole-archive
1768 @cindex including an entire archive
1769 @item --whole-archive
1770 For each archive mentioned on the command line after the
1771 @option{--whole-archive} option, include every object file in the archive
1772 in the link, rather than searching the archive for the required object
1773 files. This is normally used to turn an archive file into a shared
1774 library, forcing every object to be included in the resulting shared
1775 library. This option may be used more than once.
1776
1777 Two notes when using this option from gcc: First, gcc doesn't know
1778 about this option, so you have to use @option{-Wl,-whole-archive}.
1779 Second, don't forget to use @option{-Wl,-no-whole-archive} after your
1780 list of archives, because gcc will add its own list of archives to
1781 your link and you may not want this flag to affect those as well.
1782
1783 @kindex --wrap
1784 @item --wrap @var{symbol}
1785 Use a wrapper function for @var{symbol}. Any undefined reference to
1786 @var{symbol} will be resolved to @code{__wrap_@var{symbol}}. Any
1787 undefined reference to @code{__real_@var{symbol}} will be resolved to
1788 @var{symbol}.
1789
1790 This can be used to provide a wrapper for a system function. The
1791 wrapper function should be called @code{__wrap_@var{symbol}}. If it
1792 wishes to call the system function, it should call
1793 @code{__real_@var{symbol}}.
1794
1795 Here is a trivial example:
1796
1797 @smallexample
1798 void *
1799 __wrap_malloc (size_t c)
1800 @{
1801 printf ("malloc called with %zu\n", c);
1802 return __real_malloc (c);
1803 @}
1804 @end smallexample
1805
1806 If you link other code with this file using @option{--wrap malloc}, then
1807 all calls to @code{malloc} will call the function @code{__wrap_malloc}
1808 instead. The call to @code{__real_malloc} in @code{__wrap_malloc} will
1809 call the real @code{malloc} function.
1810
1811 You may wish to provide a @code{__real_malloc} function as well, so that
1812 links without the @option{--wrap} option will succeed. If you do this,
1813 you should not put the definition of @code{__real_malloc} in the same
1814 file as @code{__wrap_malloc}; if you do, the assembler may resolve the
1815 call before the linker has a chance to wrap it to @code{malloc}.
1816
1817 @kindex --enable-new-dtags
1818 @kindex --disable-new-dtags
1819 @item --enable-new-dtags
1820 @itemx --disable-new-dtags
1821 This linker can create the new dynamic tags in ELF. But the older ELF
1822 systems may not understand them. If you specify
1823 @option{--enable-new-dtags}, the dynamic tags will be created as needed.
1824 If you specify @option{--disable-new-dtags}, no new dynamic tags will be
1825 created. By default, the new dynamic tags are not created. Note that
1826 those options are only available for ELF systems.
1827
1828 @kindex --hash-size=@var{number}
1829 @item --hash-size=@var{number}
1830 Set the default size of the linker's hash tables to a prime number
1831 close to @var{number}. Increasing this value can reduce the length of
1832 time it takes the linker to perform its tasks, at the expense of
1833 increasing the linker's memory requirements. Similarly reducing this
1834 value can reduce the memory requirements at the expense of speed.
1835
1836 @kindex --reduce-memory-overheads
1837 @item --reduce-memory-overheads
1838 This option reduces memory requirements at ld runtime, at the expense of
1839 linking speed. This was introduced to to select the old O(n^2) algorithm
1840 for link map file generation, rather than the new O(n) algorithm which uses
1841 about 40% more memory for symbol storage.
1842
1843 Another affect of the switch is to set the default hash table size to
1844 1021, which again saves memory at the cost of lengthening the linker's
1845 run time. This is not done however if the @option{--hash-size} switch
1846 has been used.
1847
1848 The @option{--reduce-memory-overheads} switch may be also be used to
1849 enable other tradeoffs in future versions of the linker.
1850
1851 @end table
1852
1853 @c man end
1854
1855 @subsection Options Specific to i386 PE Targets
1856
1857 @c man begin OPTIONS
1858
1859 The i386 PE linker supports the @option{-shared} option, which causes
1860 the output to be a dynamically linked library (DLL) instead of a
1861 normal executable. You should name the output @code{*.dll} when you
1862 use this option. In addition, the linker fully supports the standard
1863 @code{*.def} files, which may be specified on the linker command line
1864 like an object file (in fact, it should precede archives it exports
1865 symbols from, to ensure that they get linked in, just like a normal
1866 object file).
1867
1868 In addition to the options common to all targets, the i386 PE linker
1869 support additional command line options that are specific to the i386
1870 PE target. Options that take values may be separated from their
1871 values by either a space or an equals sign.
1872
1873 @table @gcctabopt
1874
1875 @kindex --add-stdcall-alias
1876 @item --add-stdcall-alias
1877 If given, symbols with a stdcall suffix (@@@var{nn}) will be exported
1878 as-is and also with the suffix stripped.
1879 [This option is specific to the i386 PE targeted port of the linker]
1880
1881 @kindex --base-file
1882 @item --base-file @var{file}
1883 Use @var{file} as the name of a file in which to save the base
1884 addresses of all the relocations needed for generating DLLs with
1885 @file{dlltool}.
1886 [This is an i386 PE specific option]
1887
1888 @kindex --dll
1889 @item --dll
1890 Create a DLL instead of a regular executable. You may also use
1891 @option{-shared} or specify a @code{LIBRARY} in a given @code{.def}
1892 file.
1893 [This option is specific to the i386 PE targeted port of the linker]
1894
1895 @kindex --enable-stdcall-fixup
1896 @kindex --disable-stdcall-fixup
1897 @item --enable-stdcall-fixup
1898 @itemx --disable-stdcall-fixup
1899 If the link finds a symbol that it cannot resolve, it will attempt to
1900 do ``fuzzy linking'' by looking for another defined symbol that differs
1901 only in the format of the symbol name (cdecl vs stdcall) and will
1902 resolve that symbol by linking to the match. For example, the
1903 undefined symbol @code{_foo} might be linked to the function
1904 @code{_foo@@12}, or the undefined symbol @code{_bar@@16} might be linked
1905 to the function @code{_bar}. When the linker does this, it prints a
1906 warning, since it normally should have failed to link, but sometimes
1907 import libraries generated from third-party dlls may need this feature
1908 to be usable. If you specify @option{--enable-stdcall-fixup}, this
1909 feature is fully enabled and warnings are not printed. If you specify
1910 @option{--disable-stdcall-fixup}, this feature is disabled and such
1911 mismatches are considered to be errors.
1912 [This option is specific to the i386 PE targeted port of the linker]
1913
1914 @cindex DLLs, creating
1915 @kindex --export-all-symbols
1916 @item --export-all-symbols
1917 If given, all global symbols in the objects used to build a DLL will
1918 be exported by the DLL. Note that this is the default if there
1919 otherwise wouldn't be any exported symbols. When symbols are
1920 explicitly exported via DEF files or implicitly exported via function
1921 attributes, the default is to not export anything else unless this
1922 option is given. Note that the symbols @code{DllMain@@12},
1923 @code{DllEntryPoint@@0}, @code{DllMainCRTStartup@@12}, and
1924 @code{impure_ptr} will not be automatically
1925 exported. Also, symbols imported from other DLLs will not be
1926 re-exported, nor will symbols specifying the DLL's internal layout
1927 such as those beginning with @code{_head_} or ending with
1928 @code{_iname}. In addition, no symbols from @code{libgcc},
1929 @code{libstd++}, @code{libmingw32}, or @code{crtX.o} will be exported.
1930 Symbols whose names begin with @code{__rtti_} or @code{__builtin_} will
1931 not be exported, to help with C++ DLLs. Finally, there is an
1932 extensive list of cygwin-private symbols that are not exported
1933 (obviously, this applies on when building DLLs for cygwin targets).
1934 These cygwin-excludes are: @code{_cygwin_dll_entry@@12},
1935 @code{_cygwin_crt0_common@@8}, @code{_cygwin_noncygwin_dll_entry@@12},
1936 @code{_fmode}, @code{_impure_ptr}, @code{cygwin_attach_dll},
1937 @code{cygwin_premain0}, @code{cygwin_premain1}, @code{cygwin_premain2},
1938 @code{cygwin_premain3}, and @code{environ}.
1939 [This option is specific to the i386 PE targeted port of the linker]
1940
1941 @kindex --exclude-symbols
1942 @item --exclude-symbols @var{symbol},@var{symbol},...
1943 Specifies a list of symbols which should not be automatically
1944 exported. The symbol names may be delimited by commas or colons.
1945 [This option is specific to the i386 PE targeted port of the linker]
1946
1947 @kindex --file-alignment
1948 @item --file-alignment
1949 Specify the file alignment. Sections in the file will always begin at
1950 file offsets which are multiples of this number. This defaults to
1951 512.
1952 [This option is specific to the i386 PE targeted port of the linker]
1953
1954 @cindex heap size
1955 @kindex --heap
1956 @item --heap @var{reserve}
1957 @itemx --heap @var{reserve},@var{commit}
1958 Specify the amount of memory to reserve (and optionally commit) to be
1959 used as heap for this program. The default is 1Mb reserved, 4K
1960 committed.
1961 [This option is specific to the i386 PE targeted port of the linker]
1962
1963 @cindex image base
1964 @kindex --image-base
1965 @item --image-base @var{value}
1966 Use @var{value} as the base address of your program or dll. This is
1967 the lowest memory location that will be used when your program or dll
1968 is loaded. To reduce the need to relocate and improve performance of
1969 your dlls, each should have a unique base address and not overlap any
1970 other dlls. The default is 0x400000 for executables, and 0x10000000
1971 for dlls.
1972 [This option is specific to the i386 PE targeted port of the linker]
1973
1974 @kindex --kill-at
1975 @item --kill-at
1976 If given, the stdcall suffixes (@@@var{nn}) will be stripped from
1977 symbols before they are exported.
1978 [This option is specific to the i386 PE targeted port of the linker]
1979
1980 @kindex --large-address-aware
1981 @item --large-address-aware
1982 If given, the appropriate bit in the ``Charateristics'' field of the COFF
1983 header is set to indicate that this executable supports virtual addresses
1984 greater than 2 gigabytes. This should be used in conjuction with the /3GB
1985 or /USERVA=@var{value} megabytes switch in the ``[operating systems]''
1986 section of the BOOT.INI. Otherwise, this bit has no effect.
1987 [This option is specific to PE targeted ports of the linker]
1988
1989 @kindex --major-image-version
1990 @item --major-image-version @var{value}
1991 Sets the major number of the ``image version''. Defaults to 1.
1992 [This option is specific to the i386 PE targeted port of the linker]
1993
1994 @kindex --major-os-version
1995 @item --major-os-version @var{value}
1996 Sets the major number of the ``os version''. Defaults to 4.
1997 [This option is specific to the i386 PE targeted port of the linker]
1998
1999 @kindex --major-subsystem-version
2000 @item --major-subsystem-version @var{value}
2001 Sets the major number of the ``subsystem version''. Defaults to 4.
2002 [This option is specific to the i386 PE targeted port of the linker]
2003
2004 @kindex --minor-image-version
2005 @item --minor-image-version @var{value}
2006 Sets the minor number of the ``image version''. Defaults to 0.
2007 [This option is specific to the i386 PE targeted port of the linker]
2008
2009 @kindex --minor-os-version
2010 @item --minor-os-version @var{value}
2011 Sets the minor number of the ``os version''. Defaults to 0.
2012 [This option is specific to the i386 PE targeted port of the linker]
2013
2014 @kindex --minor-subsystem-version
2015 @item --minor-subsystem-version @var{value}
2016 Sets the minor number of the ``subsystem version''. Defaults to 0.
2017 [This option is specific to the i386 PE targeted port of the linker]
2018
2019 @cindex DEF files, creating
2020 @cindex DLLs, creating
2021 @kindex --output-def
2022 @item --output-def @var{file}
2023 The linker will create the file @var{file} which will contain a DEF
2024 file corresponding to the DLL the linker is generating. This DEF file
2025 (which should be called @code{*.def}) may be used to create an import
2026 library with @code{dlltool} or may be used as a reference to
2027 automatically or implicitly exported symbols.
2028 [This option is specific to the i386 PE targeted port of the linker]
2029
2030 @cindex DLLs, creating
2031 @kindex --out-implib
2032 @item --out-implib @var{file}
2033 The linker will create the file @var{file} which will contain an
2034 import lib corresponding to the DLL the linker is generating. This
2035 import lib (which should be called @code{*.dll.a} or @code{*.a}
2036 may be used to link clients against the generated DLL; this behaviour
2037 makes it possible to skip a separate @code{dlltool} import library
2038 creation step.
2039 [This option is specific to the i386 PE targeted port of the linker]
2040
2041 @kindex --enable-auto-image-base
2042 @item --enable-auto-image-base
2043 Automatically choose the image base for DLLs, unless one is specified
2044 using the @code{--image-base} argument. By using a hash generated
2045 from the dllname to create unique image bases for each DLL, in-memory
2046 collisions and relocations which can delay program execution are
2047 avoided.
2048 [This option is specific to the i386 PE targeted port of the linker]
2049
2050 @kindex --disable-auto-image-base
2051 @item --disable-auto-image-base
2052 Do not automatically generate a unique image base. If there is no
2053 user-specified image base (@code{--image-base}) then use the platform
2054 default.
2055 [This option is specific to the i386 PE targeted port of the linker]
2056
2057 @cindex DLLs, linking to
2058 @kindex --dll-search-prefix
2059 @item --dll-search-prefix @var{string}
2060 When linking dynamically to a dll without an import library,
2061 search for @code{<string><basename>.dll} in preference to
2062 @code{lib<basename>.dll}. This behaviour allows easy distinction
2063 between DLLs built for the various "subplatforms": native, cygwin,
2064 uwin, pw, etc. For instance, cygwin DLLs typically use
2065 @code{--dll-search-prefix=cyg}.
2066 [This option is specific to the i386 PE targeted port of the linker]
2067
2068 @kindex --enable-auto-import
2069 @item --enable-auto-import
2070 Do sophisticated linking of @code{_symbol} to @code{__imp__symbol} for
2071 DATA imports from DLLs, and create the necessary thunking symbols when
2072 building the import libraries with those DATA exports. Note: Use of the
2073 'auto-import' extension will cause the text section of the image file
2074 to be made writable. This does not conform to the PE-COFF format
2075 specification published by Microsoft.
2076
2077 Using 'auto-import' generally will 'just work' -- but sometimes you may
2078 see this message:
2079
2080 "variable '<var>' can't be auto-imported. Please read the
2081 documentation for ld's @code{--enable-auto-import} for details."
2082
2083 This message occurs when some (sub)expression accesses an address
2084 ultimately given by the sum of two constants (Win32 import tables only
2085 allow one). Instances where this may occur include accesses to member
2086 fields of struct variables imported from a DLL, as well as using a
2087 constant index into an array variable imported from a DLL. Any
2088 multiword variable (arrays, structs, long long, etc) may trigger
2089 this error condition. However, regardless of the exact data type
2090 of the offending exported variable, ld will always detect it, issue
2091 the warning, and exit.
2092
2093 There are several ways to address this difficulty, regardless of the
2094 data type of the exported variable:
2095
2096 One way is to use --enable-runtime-pseudo-reloc switch. This leaves the task
2097 of adjusting references in your client code for runtime environment, so
2098 this method works only when runtime environment supports this feature.
2099
2100 A second solution is to force one of the 'constants' to be a variable --
2101 that is, unknown and un-optimizable at compile time. For arrays,
2102 there are two possibilities: a) make the indexee (the array's address)
2103 a variable, or b) make the 'constant' index a variable. Thus:
2104
2105 @example
2106 extern type extern_array[];
2107 extern_array[1] -->
2108 @{ volatile type *t=extern_array; t[1] @}
2109 @end example
2110
2111 or
2112
2113 @example
2114 extern type extern_array[];
2115 extern_array[1] -->
2116 @{ volatile int t=1; extern_array[t] @}
2117 @end example
2118
2119 For structs (and most other multiword data types) the only option
2120 is to make the struct itself (or the long long, or the ...) variable:
2121
2122 @example
2123 extern struct s extern_struct;
2124 extern_struct.field -->
2125 @{ volatile struct s *t=&extern_struct; t->field @}
2126 @end example
2127
2128 or
2129
2130 @example
2131 extern long long extern_ll;
2132 extern_ll -->
2133 @{ volatile long long * local_ll=&extern_ll; *local_ll @}
2134 @end example
2135
2136 A third method of dealing with this difficulty is to abandon
2137 'auto-import' for the offending symbol and mark it with
2138 @code{__declspec(dllimport)}. However, in practise that
2139 requires using compile-time #defines to indicate whether you are
2140 building a DLL, building client code that will link to the DLL, or
2141 merely building/linking to a static library. In making the choice
2142 between the various methods of resolving the 'direct address with
2143 constant offset' problem, you should consider typical real-world usage:
2144
2145 Original:
2146 @example
2147 --foo.h
2148 extern int arr[];
2149 --foo.c
2150 #include "foo.h"
2151 void main(int argc, char **argv)@{
2152 printf("%d\n",arr[1]);
2153 @}
2154 @end example
2155
2156 Solution 1:
2157 @example
2158 --foo.h
2159 extern int arr[];
2160 --foo.c
2161 #include "foo.h"
2162 void main(int argc, char **argv)@{
2163 /* This workaround is for win32 and cygwin; do not "optimize" */
2164 volatile int *parr = arr;
2165 printf("%d\n",parr[1]);
2166 @}
2167 @end example
2168
2169 Solution 2:
2170 @example
2171 --foo.h
2172 /* Note: auto-export is assumed (no __declspec(dllexport)) */
2173 #if (defined(_WIN32) || defined(__CYGWIN__)) && \
2174 !(defined(FOO_BUILD_DLL) || defined(FOO_STATIC))
2175 #define FOO_IMPORT __declspec(dllimport)
2176 #else
2177 #define FOO_IMPORT
2178 #endif
2179 extern FOO_IMPORT int arr[];
2180 --foo.c
2181 #include "foo.h"
2182 void main(int argc, char **argv)@{
2183 printf("%d\n",arr[1]);
2184 @}
2185 @end example
2186
2187 A fourth way to avoid this problem is to re-code your
2188 library to use a functional interface rather than a data interface
2189 for the offending variables (e.g. set_foo() and get_foo() accessor
2190 functions).
2191 [This option is specific to the i386 PE targeted port of the linker]
2192
2193 @kindex --disable-auto-import
2194 @item --disable-auto-import
2195 Do not attempt to do sophisticated linking of @code{_symbol} to
2196 @code{__imp__symbol} for DATA imports from DLLs.
2197 [This option is specific to the i386 PE targeted port of the linker]
2198
2199 @kindex --enable-runtime-pseudo-reloc
2200 @item --enable-runtime-pseudo-reloc
2201 If your code contains expressions described in --enable-auto-import section,
2202 that is, DATA imports from DLL with non-zero offset, this switch will create
2203 a vector of 'runtime pseudo relocations' which can be used by runtime
2204 environment to adjust references to such data in your client code.
2205 [This option is specific to the i386 PE targeted port of the linker]
2206
2207 @kindex --disable-runtime-pseudo-reloc
2208 @item --disable-runtime-pseudo-reloc
2209 Do not create pseudo relocations for non-zero offset DATA imports from
2210 DLLs. This is the default.
2211 [This option is specific to the i386 PE targeted port of the linker]
2212
2213 @kindex --enable-extra-pe-debug
2214 @item --enable-extra-pe-debug
2215 Show additional debug info related to auto-import symbol thunking.
2216 [This option is specific to the i386 PE targeted port of the linker]
2217
2218 @kindex --section-alignment
2219 @item --section-alignment
2220 Sets the section alignment. Sections in memory will always begin at
2221 addresses which are a multiple of this number. Defaults to 0x1000.
2222 [This option is specific to the i386 PE targeted port of the linker]
2223
2224 @cindex stack size
2225 @kindex --stack
2226 @item --stack @var{reserve}
2227 @itemx --stack @var{reserve},@var{commit}
2228 Specify the amount of memory to reserve (and optionally commit) to be
2229 used as stack for this program. The default is 2Mb reserved, 4K
2230 committed.
2231 [This option is specific to the i386 PE targeted port of the linker]
2232
2233 @kindex --subsystem
2234 @item --subsystem @var{which}
2235 @itemx --subsystem @var{which}:@var{major}
2236 @itemx --subsystem @var{which}:@var{major}.@var{minor}
2237 Specifies the subsystem under which your program will execute. The
2238 legal values for @var{which} are @code{native}, @code{windows},
2239 @code{console}, @code{posix}, and @code{xbox}. You may optionally set
2240 the subsystem version also. Numeric values are also accepted for
2241 @var{which}.
2242 [This option is specific to the i386 PE targeted port of the linker]
2243
2244 @end table
2245
2246 @c man end
2247
2248 @ifset M68HC11
2249 @subsection Options specific to Motorola 68HC11 and 68HC12 targets
2250
2251 @c man begin OPTIONS
2252
2253 The 68HC11 and 68HC12 linkers support specific options to control the
2254 memory bank switching mapping and trampoline code generation.
2255
2256 @table @gcctabopt
2257
2258 @kindex --no-trampoline
2259 @item --no-trampoline
2260 This option disables the generation of trampoline. By default a trampoline
2261 is generated for each far function which is called using a @code{jsr}
2262 instruction (this happens when a pointer to a far function is taken).
2263
2264 @kindex --bank-window
2265 @item --bank-window @var{name}
2266 This option indicates to the linker the name of the memory region in
2267 the @samp{MEMORY} specification that describes the memory bank window.
2268 The definition of such region is then used by the linker to compute
2269 paging and addresses within the memory window.
2270
2271 @end table
2272
2273 @c man end
2274 @end ifset
2275
2276 @ifset UsesEnvVars
2277 @node Environment
2278 @section Environment Variables
2279
2280 @c man begin ENVIRONMENT
2281
2282 You can change the behaviour of @command{ld} with the environment variables
2283 @ifclear SingleFormat
2284 @code{GNUTARGET},
2285 @end ifclear
2286 @code{LDEMULATION} and @code{COLLECT_NO_DEMANGLE}.
2287
2288 @ifclear SingleFormat
2289 @kindex GNUTARGET
2290 @cindex default input format
2291 @code{GNUTARGET} determines the input-file object format if you don't
2292 use @samp{-b} (or its synonym @samp{--format}). Its value should be one
2293 of the BFD names for an input format (@pxref{BFD}). If there is no
2294 @code{GNUTARGET} in the environment, @command{ld} uses the natural format
2295 of the target. If @code{GNUTARGET} is set to @code{default} then BFD
2296 attempts to discover the input format by examining binary input files;
2297 this method often succeeds, but there are potential ambiguities, since
2298 there is no method of ensuring that the magic number used to specify
2299 object-file formats is unique. However, the configuration procedure for
2300 BFD on each system places the conventional format for that system first
2301 in the search-list, so ambiguities are resolved in favor of convention.
2302 @end ifclear
2303
2304 @kindex LDEMULATION
2305 @cindex default emulation
2306 @cindex emulation, default
2307 @code{LDEMULATION} determines the default emulation if you don't use the
2308 @samp{-m} option. The emulation can affect various aspects of linker
2309 behaviour, particularly the default linker script. You can list the
2310 available emulations with the @samp{--verbose} or @samp{-V} options. If
2311 the @samp{-m} option is not used, and the @code{LDEMULATION} environment
2312 variable is not defined, the default emulation depends upon how the
2313 linker was configured.
2314
2315 @kindex COLLECT_NO_DEMANGLE
2316 @cindex demangling, default
2317 Normally, the linker will default to demangling symbols. However, if
2318 @code{COLLECT_NO_DEMANGLE} is set in the environment, then it will
2319 default to not demangling symbols. This environment variable is used in
2320 a similar fashion by the @code{gcc} linker wrapper program. The default
2321 may be overridden by the @samp{--demangle} and @samp{--no-demangle}
2322 options.
2323
2324 @c man end
2325 @end ifset
2326
2327 @node Scripts
2328 @chapter Linker Scripts
2329
2330 @cindex scripts
2331 @cindex linker scripts
2332 @cindex command files
2333 Every link is controlled by a @dfn{linker script}. This script is
2334 written in the linker command language.
2335
2336 The main purpose of the linker script is to describe how the sections in
2337 the input files should be mapped into the output file, and to control
2338 the memory layout of the output file. Most linker scripts do nothing
2339 more than this. However, when necessary, the linker script can also
2340 direct the linker to perform many other operations, using the commands
2341 described below.
2342
2343 The linker always uses a linker script. If you do not supply one
2344 yourself, the linker will use a default script that is compiled into the
2345 linker executable. You can use the @samp{--verbose} command line option
2346 to display the default linker script. Certain command line options,
2347 such as @samp{-r} or @samp{-N}, will affect the default linker script.
2348
2349 You may supply your own linker script by using the @samp{-T} command
2350 line option. When you do this, your linker script will replace the
2351 default linker script.
2352
2353 You may also use linker scripts implicitly by naming them as input files
2354 to the linker, as though they were files to be linked. @xref{Implicit
2355 Linker Scripts}.
2356
2357 @menu
2358 * Basic Script Concepts:: Basic Linker Script Concepts
2359 * Script Format:: Linker Script Format
2360 * Simple Example:: Simple Linker Script Example
2361 * Simple Commands:: Simple Linker Script Commands
2362 * Assignments:: Assigning Values to Symbols
2363 * SECTIONS:: SECTIONS Command
2364 * MEMORY:: MEMORY Command
2365 * PHDRS:: PHDRS Command
2366 * VERSION:: VERSION Command
2367 * Expressions:: Expressions in Linker Scripts
2368 * Implicit Linker Scripts:: Implicit Linker Scripts
2369 @end menu
2370
2371 @node Basic Script Concepts
2372 @section Basic Linker Script Concepts
2373 @cindex linker script concepts
2374 We need to define some basic concepts and vocabulary in order to
2375 describe the linker script language.
2376
2377 The linker combines input files into a single output file. The output
2378 file and each input file are in a special data format known as an
2379 @dfn{object file format}. Each file is called an @dfn{object file}.
2380 The output file is often called an @dfn{executable}, but for our
2381 purposes we will also call it an object file. Each object file has,
2382 among other things, a list of @dfn{sections}. We sometimes refer to a
2383 section in an input file as an @dfn{input section}; similarly, a section
2384 in the output file is an @dfn{output section}.
2385
2386 Each section in an object file has a name and a size. Most sections
2387 also have an associated block of data, known as the @dfn{section
2388 contents}. A section may be marked as @dfn{loadable}, which mean that
2389 the contents should be loaded into memory when the output file is run.
2390 A section with no contents may be @dfn{allocatable}, which means that an
2391 area in memory should be set aside, but nothing in particular should be
2392 loaded there (in some cases this memory must be zeroed out). A section
2393 which is neither loadable nor allocatable typically contains some sort
2394 of debugging information.
2395
2396 Every loadable or allocatable output section has two addresses. The
2397 first is the @dfn{VMA}, or virtual memory address. This is the address
2398 the section will have when the output file is run. The second is the
2399 @dfn{LMA}, or load memory address. This is the address at which the
2400 section will be loaded. In most cases the two addresses will be the
2401 same. An example of when they might be different is when a data section
2402 is loaded into ROM, and then copied into RAM when the program starts up
2403 (this technique is often used to initialize global variables in a ROM
2404 based system). In this case the ROM address would be the LMA, and the
2405 RAM address would be the VMA.
2406
2407 You can see the sections in an object file by using the @code{objdump}
2408 program with the @samp{-h} option.
2409
2410 Every object file also has a list of @dfn{symbols}, known as the
2411 @dfn{symbol table}. A symbol may be defined or undefined. Each symbol
2412 has a name, and each defined symbol has an address, among other
2413 information. If you compile a C or C++ program into an object file, you
2414 will get a defined symbol for every defined function and global or
2415 static variable. Every undefined function or global variable which is
2416 referenced in the input file will become an undefined symbol.
2417
2418 You can see the symbols in an object file by using the @code{nm}
2419 program, or by using the @code{objdump} program with the @samp{-t}
2420 option.
2421
2422 @node Script Format
2423 @section Linker Script Format
2424 @cindex linker script format
2425 Linker scripts are text files.
2426
2427 You write a linker script as a series of commands. Each command is
2428 either a keyword, possibly followed by arguments, or an assignment to a
2429 symbol. You may separate commands using semicolons. Whitespace is
2430 generally ignored.
2431
2432 Strings such as file or format names can normally be entered directly.
2433 If the file name contains a character such as a comma which would
2434 otherwise serve to separate file names, you may put the file name in
2435 double quotes. There is no way to use a double quote character in a
2436 file name.
2437
2438 You may include comments in linker scripts just as in C, delimited by
2439 @samp{/*} and @samp{*/}. As in C, comments are syntactically equivalent
2440 to whitespace.
2441
2442 @node Simple Example
2443 @section Simple Linker Script Example
2444 @cindex linker script example
2445 @cindex example of linker script
2446 Many linker scripts are fairly simple.
2447
2448 The simplest possible linker script has just one command:
2449 @samp{SECTIONS}. You use the @samp{SECTIONS} command to describe the
2450 memory layout of the output file.
2451
2452 The @samp{SECTIONS} command is a powerful command. Here we will
2453 describe a simple use of it. Let's assume your program consists only of
2454 code, initialized data, and uninitialized data. These will be in the
2455 @samp{.text}, @samp{.data}, and @samp{.bss} sections, respectively.
2456 Let's assume further that these are the only sections which appear in
2457 your input files.
2458
2459 For this example, let's say that the code should be loaded at address
2460 0x10000, and that the data should start at address 0x8000000. Here is a
2461 linker script which will do that:
2462 @smallexample
2463 SECTIONS
2464 @{
2465 . = 0x10000;
2466 .text : @{ *(.text) @}
2467 . = 0x8000000;
2468 .data : @{ *(.data) @}
2469 .bss : @{ *(.bss) @}
2470 @}
2471 @end smallexample
2472
2473 You write the @samp{SECTIONS} command as the keyword @samp{SECTIONS},
2474 followed by a series of symbol assignments and output section
2475 descriptions enclosed in curly braces.
2476
2477 The first line inside the @samp{SECTIONS} command of the above example
2478 sets the value of the special symbol @samp{.}, which is the location
2479 counter. If you do not specify the address of an output section in some
2480 other way (other ways are described later), the address is set from the
2481 current value of the location counter. The location counter is then
2482 incremented by the size of the output section. At the start of the
2483 @samp{SECTIONS} command, the location counter has the value @samp{0}.
2484
2485 The second line defines an output section, @samp{.text}. The colon is
2486 required syntax which may be ignored for now. Within the curly braces
2487 after the output section name, you list the names of the input sections
2488 which should be placed into this output section. The @samp{*} is a
2489 wildcard which matches any file name. The expression @samp{*(.text)}
2490 means all @samp{.text} input sections in all input files.
2491
2492 Since the location counter is @samp{0x10000} when the output section
2493 @samp{.text} is defined, the linker will set the address of the
2494 @samp{.text} section in the output file to be @samp{0x10000}.
2495
2496 The remaining lines define the @samp{.data} and @samp{.bss} sections in
2497 the output file. The linker will place the @samp{.data} output section
2498 at address @samp{0x8000000}. After the linker places the @samp{.data}
2499 output section, the value of the location counter will be
2500 @samp{0x8000000} plus the size of the @samp{.data} output section. The
2501 effect is that the linker will place the @samp{.bss} output section
2502 immediately after the @samp{.data} output section in memory.
2503
2504 The linker will ensure that each output section has the required
2505 alignment, by increasing the location counter if necessary. In this
2506 example, the specified addresses for the @samp{.text} and @samp{.data}
2507 sections will probably satisfy any alignment constraints, but the linker
2508 may have to create a small gap between the @samp{.data} and @samp{.bss}
2509 sections.
2510
2511 That's it! That's a simple and complete linker script.
2512
2513 @node Simple Commands
2514 @section Simple Linker Script Commands
2515 @cindex linker script simple commands
2516 In this section we describe the simple linker script commands.
2517
2518 @menu
2519 * Entry Point:: Setting the entry point
2520 * File Commands:: Commands dealing with files
2521 @ifclear SingleFormat
2522 * Format Commands:: Commands dealing with object file formats
2523 @end ifclear
2524
2525 * Miscellaneous Commands:: Other linker script commands
2526 @end menu
2527
2528 @node Entry Point
2529 @subsection Setting the Entry Point
2530 @kindex ENTRY(@var{symbol})
2531 @cindex start of execution
2532 @cindex first instruction
2533 @cindex entry point
2534 The first instruction to execute in a program is called the @dfn{entry
2535 point}. You can use the @code{ENTRY} linker script command to set the
2536 entry point. The argument is a symbol name:
2537 @smallexample
2538 ENTRY(@var{symbol})
2539 @end smallexample
2540
2541 There are several ways to set the entry point. The linker will set the
2542 entry point by trying each of the following methods in order, and
2543 stopping when one of them succeeds:
2544 @itemize @bullet
2545 @item
2546 the @samp{-e} @var{entry} command-line option;
2547 @item
2548 the @code{ENTRY(@var{symbol})} command in a linker script;
2549 @item
2550 the value of the symbol @code{start}, if defined;
2551 @item
2552 the address of the first byte of the @samp{.text} section, if present;
2553 @item
2554 The address @code{0}.
2555 @end itemize
2556
2557 @node File Commands
2558 @subsection Commands Dealing with Files
2559 @cindex linker script file commands
2560 Several linker script commands deal with files.
2561
2562 @table @code
2563 @item INCLUDE @var{filename}
2564 @kindex INCLUDE @var{filename}
2565 @cindex including a linker script
2566 Include the linker script @var{filename} at this point. The file will
2567 be searched for in the current directory, and in any directory specified
2568 with the @option{-L} option. You can nest calls to @code{INCLUDE} up to
2569 10 levels deep.
2570
2571 @item INPUT(@var{file}, @var{file}, @dots{})
2572 @itemx INPUT(@var{file} @var{file} @dots{})
2573 @kindex INPUT(@var{files})
2574 @cindex input files in linker scripts
2575 @cindex input object files in linker scripts
2576 @cindex linker script input object files
2577 The @code{INPUT} command directs the linker to include the named files
2578 in the link, as though they were named on the command line.
2579
2580 For example, if you always want to include @file{subr.o} any time you do
2581 a link, but you can't be bothered to put it on every link command line,
2582 then you can put @samp{INPUT (subr.o)} in your linker script.
2583
2584 In fact, if you like, you can list all of your input files in the linker
2585 script, and then invoke the linker with nothing but a @samp{-T} option.
2586
2587 In case a @dfn{sysroot prefix} is configured, and the filename starts
2588 with the @samp{/} character, and the script being processed was
2589 located inside the @dfn{sysroot prefix}, the filename will be looked
2590 for in the @dfn{sysroot prefix}. Otherwise, the linker will try to
2591 open the file in the current directory. If it is not found, the
2592 linker will search through the archive library search path. See the
2593 description of @samp{-L} in @ref{Options,,Command Line Options}.
2594
2595 If you use @samp{INPUT (-l@var{file})}, @command{ld} will transform the
2596 name to @code{lib@var{file}.a}, as with the command line argument
2597 @samp{-l}.
2598
2599 When you use the @code{INPUT} command in an implicit linker script, the
2600 files will be included in the link at the point at which the linker
2601 script file is included. This can affect archive searching.
2602
2603 @item GROUP(@var{file}, @var{file}, @dots{})
2604 @itemx GROUP(@var{file} @var{file} @dots{})
2605 @kindex GROUP(@var{files})
2606 @cindex grouping input files
2607 The @code{GROUP} command is like @code{INPUT}, except that the named
2608 files should all be archives, and they are searched repeatedly until no
2609 new undefined references are created. See the description of @samp{-(}
2610 in @ref{Options,,Command Line Options}.
2611
2612 @item AS_NEEDED(@var{file}, @var{file}, @dots{})
2613 @itemx AS_NEEDED(@var{file} @var{file} @dots{})
2614 @kindex AS_NEEDED(@var{files})
2615 This construct can appear only inside of the @code{INPUT} or @code{GROUP}
2616 commands, among other filenames. The files listed will be handled
2617 as if they appear directly in the @code{INPUT} or @code{GROUP} commands,
2618 with the exception of ELF shared libraries, that will be added only
2619 when they are actually needed. This construct essentially enables
2620 @option{--as-needed} option for all the files listed inside of it
2621 and restores previous @option{--as-needed} resp. @option{--no-as-needed}
2622 setting afterwards.
2623
2624 @item OUTPUT(@var{filename})
2625 @kindex OUTPUT(@var{filename})
2626 @cindex output file name in linker scripot
2627 The @code{OUTPUT} command names the output file. Using
2628 @code{OUTPUT(@var{filename})} in the linker script is exactly like using
2629 @samp{-o @var{filename}} on the command line (@pxref{Options,,Command
2630 Line Options}). If both are used, the command line option takes
2631 precedence.
2632
2633 You can use the @code{OUTPUT} command to define a default name for the
2634 output file other than the usual default of @file{a.out}.
2635
2636 @item SEARCH_DIR(@var{path})
2637 @kindex SEARCH_DIR(@var{path})
2638 @cindex library search path in linker script
2639 @cindex archive search path in linker script
2640 @cindex search path in linker script
2641 The @code{SEARCH_DIR} command adds @var{path} to the list of paths where
2642 @command{ld} looks for archive libraries. Using
2643 @code{SEARCH_DIR(@var{path})} is exactly like using @samp{-L @var{path}}
2644 on the command line (@pxref{Options,,Command Line Options}). If both
2645 are used, then the linker will search both paths. Paths specified using
2646 the command line option are searched first.
2647
2648 @item STARTUP(@var{filename})
2649 @kindex STARTUP(@var{filename})
2650 @cindex first input file
2651 The @code{STARTUP} command is just like the @code{INPUT} command, except
2652 that @var{filename} will become the first input file to be linked, as
2653 though it were specified first on the command line. This may be useful
2654 when using a system in which the entry point is always the start of the
2655 first file.
2656 @end table
2657
2658 @ifclear SingleFormat
2659 @node Format Commands
2660 @subsection Commands Dealing with Object File Formats
2661 A couple of linker script commands deal with object file formats.
2662
2663 @table @code
2664 @item OUTPUT_FORMAT(@var{bfdname})
2665 @itemx OUTPUT_FORMAT(@var{default}, @var{big}, @var{little})
2666 @kindex OUTPUT_FORMAT(@var{bfdname})
2667 @cindex output file format in linker script
2668 The @code{OUTPUT_FORMAT} command names the BFD format to use for the
2669 output file (@pxref{BFD}). Using @code{OUTPUT_FORMAT(@var{bfdname})} is
2670 exactly like using @samp{--oformat @var{bfdname}} on the command line
2671 (@pxref{Options,,Command Line Options}). If both are used, the command
2672 line option takes precedence.
2673
2674 You can use @code{OUTPUT_FORMAT} with three arguments to use different
2675 formats based on the @samp{-EB} and @samp{-EL} command line options.
2676 This permits the linker script to set the output format based on the
2677 desired endianness.
2678
2679 If neither @samp{-EB} nor @samp{-EL} are used, then the output format
2680 will be the first argument, @var{default}. If @samp{-EB} is used, the
2681 output format will be the second argument, @var{big}. If @samp{-EL} is
2682 used, the output format will be the third argument, @var{little}.
2683
2684 For example, the default linker script for the MIPS ELF target uses this
2685 command:
2686 @smallexample
2687 OUTPUT_FORMAT(elf32-bigmips, elf32-bigmips, elf32-littlemips)
2688 @end smallexample
2689 This says that the default format for the output file is
2690 @samp{elf32-bigmips}, but if the user uses the @samp{-EL} command line
2691 option, the output file will be created in the @samp{elf32-littlemips}
2692 format.
2693
2694 @item TARGET(@var{bfdname})
2695 @kindex TARGET(@var{bfdname})
2696 @cindex input file format in linker script
2697 The @code{TARGET} command names the BFD format to use when reading input
2698 files. It affects subsequent @code{INPUT} and @code{GROUP} commands.
2699 This command is like using @samp{-b @var{bfdname}} on the command line
2700 (@pxref{Options,,Command Line Options}). If the @code{TARGET} command
2701 is used but @code{OUTPUT_FORMAT} is not, then the last @code{TARGET}
2702 command is also used to set the format for the output file. @xref{BFD}.
2703 @end table
2704 @end ifclear
2705
2706 @node Miscellaneous Commands
2707 @subsection Other Linker Script Commands
2708 There are a few other linker scripts commands.
2709
2710 @table @code
2711 @item ASSERT(@var{exp}, @var{message})
2712 @kindex ASSERT
2713 @cindex assertion in linker script
2714 Ensure that @var{exp} is non-zero. If it is zero, then exit the linker
2715 with an error code, and print @var{message}.
2716
2717 @item EXTERN(@var{symbol} @var{symbol} @dots{})
2718 @kindex EXTERN
2719 @cindex undefined symbol in linker script
2720 Force @var{symbol} to be entered in the output file as an undefined
2721 symbol. Doing this may, for example, trigger linking of additional
2722 modules from standard libraries. You may list several @var{symbol}s for
2723 each @code{EXTERN}, and you may use @code{EXTERN} multiple times. This
2724 command has the same effect as the @samp{-u} command-line option.
2725
2726 @item FORCE_COMMON_ALLOCATION
2727 @kindex FORCE_COMMON_ALLOCATION
2728 @cindex common allocation in linker script
2729 This command has the same effect as the @samp{-d} command-line option:
2730 to make @command{ld} assign space to common symbols even if a relocatable
2731 output file is specified (@samp{-r}).
2732
2733 @item INHIBIT_COMMON_ALLOCATION
2734 @kindex INHIBIT_COMMON_ALLOCATION
2735 @cindex common allocation in linker script
2736 This command has the same effect as the @samp{--no-define-common}
2737 command-line option: to make @code{ld} omit the assignment of addresses
2738 to common symbols even for a non-relocatable output file.
2739
2740 @item NOCROSSREFS(@var{section} @var{section} @dots{})
2741 @kindex NOCROSSREFS(@var{sections})
2742 @cindex cross references
2743 This command may be used to tell @command{ld} to issue an error about any
2744 references among certain output sections.
2745
2746 In certain types of programs, particularly on embedded systems when
2747 using overlays, when one section is loaded into memory, another section
2748 will not be. Any direct references between the two sections would be
2749 errors. For example, it would be an error if code in one section called
2750 a function defined in the other section.
2751
2752 The @code{NOCROSSREFS} command takes a list of output section names. If
2753 @command{ld} detects any cross references between the sections, it reports
2754 an error and returns a non-zero exit status. Note that the
2755 @code{NOCROSSREFS} command uses output section names, not input section
2756 names.
2757
2758 @ifclear SingleFormat
2759 @item OUTPUT_ARCH(@var{bfdarch})
2760 @kindex OUTPUT_ARCH(@var{bfdarch})
2761 @cindex machine architecture
2762 @cindex architecture
2763 Specify a particular output machine architecture. The argument is one
2764 of the names used by the BFD library (@pxref{BFD}). You can see the
2765 architecture of an object file by using the @code{objdump} program with
2766 the @samp{-f} option.
2767 @end ifclear
2768 @end table
2769
2770 @node Assignments
2771 @section Assigning Values to Symbols
2772 @cindex assignment in scripts
2773 @cindex symbol definition, scripts
2774 @cindex variables, defining
2775 You may assign a value to a symbol in a linker script. This will define
2776 the symbol and place it into the symbol table with a global scope.
2777
2778 @menu
2779 * Simple Assignments:: Simple Assignments
2780 * PROVIDE:: PROVIDE
2781 * PROVIDE_HIDDEN:: PROVIDE_HIDDEN
2782 * Source Code Reference:: How to use a linker script defined symbol in source code
2783 @end menu
2784
2785 @node Simple Assignments
2786 @subsection Simple Assignments
2787
2788 You may assign to a symbol using any of the C assignment operators:
2789
2790 @table @code
2791 @item @var{symbol} = @var{expression} ;
2792 @itemx @var{symbol} += @var{expression} ;
2793 @itemx @var{symbol} -= @var{expression} ;
2794 @itemx @var{symbol} *= @var{expression} ;
2795 @itemx @var{symbol} /= @var{expression} ;
2796 @itemx @var{symbol} <<= @var{expression} ;
2797 @itemx @var{symbol} >>= @var{expression} ;
2798 @itemx @var{symbol} &= @var{expression} ;
2799 @itemx @var{symbol} |= @var{expression} ;
2800 @end table
2801
2802 The first case will define @var{symbol} to the value of
2803 @var{expression}. In the other cases, @var{symbol} must already be
2804 defined, and the value will be adjusted accordingly.
2805
2806 The special symbol name @samp{.} indicates the location counter. You
2807 may only use this within a @code{SECTIONS} command. @xref{Location Counter}.
2808
2809 The semicolon after @var{expression} is required.
2810
2811 Expressions are defined below; see @ref{Expressions}.
2812
2813 You may write symbol assignments as commands in their own right, or as
2814 statements within a @code{SECTIONS} command, or as part of an output
2815 section description in a @code{SECTIONS} command.
2816
2817 The section of the symbol will be set from the section of the
2818 expression; for more information, see @ref{Expression Section}.
2819
2820 Here is an example showing the three different places that symbol
2821 assignments may be used:
2822
2823 @smallexample
2824 floating_point = 0;
2825 SECTIONS
2826 @{
2827 .text :
2828 @{
2829 *(.text)
2830 _etext = .;
2831 @}
2832 _bdata = (. + 3) & ~ 3;
2833 .data : @{ *(.data) @}
2834 @}
2835 @end smallexample
2836 @noindent
2837 In this example, the symbol @samp{floating_point} will be defined as
2838 zero. The symbol @samp{_etext} will be defined as the address following
2839 the last @samp{.text} input section. The symbol @samp{_bdata} will be
2840 defined as the address following the @samp{.text} output section aligned
2841 upward to a 4 byte boundary.
2842
2843 @node PROVIDE
2844 @subsection PROVIDE
2845 @cindex PROVIDE
2846 In some cases, it is desirable for a linker script to define a symbol
2847 only if it is referenced and is not defined by any object included in
2848 the link. For example, traditional linkers defined the symbol
2849 @samp{etext}. However, ANSI C requires that the user be able to use
2850 @samp{etext} as a function name without encountering an error. The
2851 @code{PROVIDE} keyword may be used to define a symbol, such as
2852 @samp{etext}, only if it is referenced but not defined. The syntax is
2853 @code{PROVIDE(@var{symbol} = @var{expression})}.
2854
2855 Here is an example of using @code{PROVIDE} to define @samp{etext}:
2856 @smallexample
2857 SECTIONS
2858 @{
2859 .text :
2860 @{
2861 *(.text)
2862 _etext = .;
2863 PROVIDE(etext = .);
2864 @}
2865 @}
2866 @end smallexample
2867
2868 In this example, if the program defines @samp{_etext} (with a leading
2869 underscore), the linker will give a multiple definition error. If, on
2870 the other hand, the program defines @samp{etext} (with no leading
2871 underscore), the linker will silently use the definition in the program.
2872 If the program references @samp{etext} but does not define it, the
2873 linker will use the definition in the linker script.
2874
2875 @node PROVIDE_HIDDEN
2876 @subsection PROVIDE_HIDDEN
2877 @cindex PROVIDE_HIDDEN
2878 Similar to @code{PROVIDE}. For ELF targeted ports, the symbol will be
2879 hidden and won't be exported.
2880
2881 @node Source Code Reference
2882 @subsection Source Code Reference
2883
2884 Accessing a linker script defined variable from source code is not
2885 intuitive. In particular a linker script symbol is not equivalent to
2886 a variable declaration in a high level language, it is instead a
2887 symbol that does not have a value.
2888
2889 Before going further, it is important to note that compilers often
2890 transform names in the source code into different names when they are
2891 stored in the symbol table. For example, Fortran compilers commonly
2892 prepend or append an underscore, and C++ performs extensive @samp{name
2893 mangling}. Therefore there might be a discrepancy between the name
2894 of a variable as it is used in source code and the name of the same
2895 variable as it is defined in a linker script. For example in C a
2896 linker script variable might be referred to as:
2897
2898 @smallexample
2899 extern int foo;
2900 @end smallexample
2901
2902 But in the linker script it might be defined as:
2903
2904 @smallexample
2905 _foo = 1000;
2906 @end smallexample
2907
2908 In the remaining examples however it is assumed that no name
2909 transformation has taken place.
2910
2911 When a symbol is declared in a high level language such as C, two
2912 things happen. The first is that the compiler reserves enough space
2913 in the program's memory to hold the @emph{value} of the symbol. The
2914 second is that the compiler creates an entry in the program's symbol
2915 table which holds the symbol's @emph{address}. ie the symbol table
2916 contains the address of the block of memory holding the symbol's
2917 value. So for example the following C declaration, at file scope:
2918
2919 @smallexample
2920 int foo = 1000;
2921 @end smallexample
2922
2923 creates a entry called @samp{foo} in the symbol table. This entry
2924 holds the address of an @samp{int} sized block of memory where the
2925 number 1000 is initially stored.
2926
2927 When a program references a symbol the compiler generates code that
2928 first accesses the symbol table to find the address of the symbol's
2929 memory block and then code to read the value from that memory block.
2930 So:
2931
2932 @smallexample
2933 foo = 1;
2934 @end smallexample
2935
2936 looks up the symbol @samp{foo} in the symbol table, gets the address
2937 associated with this symbol and then writes the value 1 into that
2938 address. Whereas:
2939
2940 @smallexample
2941 int * a = & foo;
2942 @end smallexample
2943
2944 looks up the symbol @samp{foo} in the symbol table, gets it address
2945 and then copies this address into the block of memory associated with
2946 the variable @samp{a}.
2947
2948 Linker scripts symbol declarations, by contrast, create an entry in
2949 the symbol table but do not assign any memory to them. Thus they are
2950 an address without a value. So for example the linker script definition:
2951
2952 @smallexample
2953 foo = 1000;
2954 @end smallexample
2955
2956 creates an entry in the symbol table called @samp{foo} which holds
2957 the address of memory location 1000, but nothing special is stored at
2958 address 1000. This means that you cannot access the @emph{value} of a
2959 linker script defined symbol - it has no value - all you can do is
2960 access the @emph{address} of a linker script defined symbol.
2961
2962 Hence when you are using a linker script defined symbol in source code
2963 you should always take the address of the symbol, and never attempt to
2964 use its value. For example suppose you want to copy the contents of a
2965 section of memory called .ROM into a section called .FLASH and the
2966 linker script contains these declarations:
2967
2968 @smallexample
2969 @group
2970 start_of_ROM = .ROM;
2971 end_of_ROM = .ROM + sizeof (.ROM) - 1;
2972 start_of_FLASH = .FLASH;
2973 @end group
2974 @end smallexample
2975
2976 Then the C source code to perform the copy would be:
2977
2978 @smallexample
2979 @group
2980 extern char start_of_ROM, end_of_ROM, start_of_FLASH;
2981
2982 memcpy (& start_of_FLASH, & start_of_ROM, & end_of_ROM - & start_of_ROM);
2983 @end group
2984 @end smallexample
2985
2986 Note the use of the @samp{&} operators. These are correct.
2987
2988 @node SECTIONS
2989 @section SECTIONS Command
2990 @kindex SECTIONS
2991 The @code{SECTIONS} command tells the linker how to map input sections
2992 into output sections, and how to place the output sections in memory.
2993
2994 The format of the @code{SECTIONS} command is:
2995 @smallexample
2996 SECTIONS
2997 @{
2998 @var{sections-command}
2999 @var{sections-command}
3000 @dots{}
3001 @}
3002 @end smallexample
3003
3004 Each @var{sections-command} may of be one of the following:
3005
3006 @itemize @bullet
3007 @item
3008 an @code{ENTRY} command (@pxref{Entry Point,,Entry command})
3009 @item
3010 a symbol assignment (@pxref{Assignments})
3011 @item
3012 an output section description
3013 @item
3014 an overlay description
3015 @end itemize
3016
3017 The @code{ENTRY} command and symbol assignments are permitted inside the
3018 @code{SECTIONS} command for convenience in using the location counter in
3019 those commands. This can also make the linker script easier to
3020 understand because you can use those commands at meaningful points in
3021 the layout of the output file.
3022
3023 Output section descriptions and overlay descriptions are described
3024 below.
3025
3026 If you do not use a @code{SECTIONS} command in your linker script, the
3027 linker will place each input section into an identically named output
3028 section in the order that the sections are first encountered in the
3029 input files. If all input sections are present in the first file, for
3030 example, the order of sections in the output file will match the order
3031 in the first input file. The first section will be at address zero.
3032
3033 @menu
3034 * Output Section Description:: Output section description
3035 * Output Section Name:: Output section name
3036 * Output Section Address:: Output section address
3037 * Input Section:: Input section description
3038 * Output Section Data:: Output section data
3039 * Output Section Keywords:: Output section keywords
3040 * Output Section Discarding:: Output section discarding
3041 * Output Section Attributes:: Output section attributes
3042 * Overlay Description:: Overlay description
3043 @end menu
3044
3045 @node Output Section Description
3046 @subsection Output Section Description
3047 The full description of an output section looks like this:
3048 @smallexample
3049 @group
3050 @var{section} [@var{address}] [(@var{type})] :
3051 [AT(@var{lma})] [SUBALIGN(@var{subsection_align})]
3052 @{
3053 @var{output-section-command}
3054 @var{output-section-command}
3055 @dots{}
3056 @} [>@var{region}] [AT>@var{lma_region}] [:@var{phdr} :@var{phdr} @dots{}] [=@var{fillexp}]
3057 @end group
3058 @end smallexample
3059
3060 Most output sections do not use most of the optional section attributes.
3061
3062 The whitespace around @var{section} is required, so that the section
3063 name is unambiguous. The colon and the curly braces are also required.
3064 The line breaks and other white space are optional.
3065
3066 Each @var{output-section-command} may be one of the following:
3067
3068 @itemize @bullet
3069 @item
3070 a symbol assignment (@pxref{Assignments})
3071 @item
3072 an input section description (@pxref{Input Section})
3073 @item
3074 data values to include directly (@pxref{Output Section Data})
3075 @item
3076 a special output section keyword (@pxref{Output Section Keywords})
3077 @end itemize
3078
3079 @node Output Section Name
3080 @subsection Output Section Name
3081 @cindex name, section
3082 @cindex section name
3083 The name of the output section is @var{section}. @var{section} must
3084 meet the constraints of your output format. In formats which only
3085 support a limited number of sections, such as @code{a.out}, the name
3086 must be one of the names supported by the format (@code{a.out}, for
3087 example, allows only @samp{.text}, @samp{.data} or @samp{.bss}). If the
3088 output format supports any number of sections, but with numbers and not
3089 names (as is the case for Oasys), the name should be supplied as a
3090 quoted numeric string. A section name may consist of any sequence of
3091 characters, but a name which contains any unusual characters such as
3092 commas must be quoted.
3093
3094 The output section name @samp{/DISCARD/} is special; @ref{Output Section
3095 Discarding}.
3096
3097 @node Output Section Address
3098 @subsection Output Section Address
3099 @cindex address, section
3100 @cindex section address
3101 The @var{address} is an expression for the VMA (the virtual memory
3102 address) of the output section. If you do not provide @var{address},
3103 the linker will set it based on @var{region} if present, or otherwise
3104 based on the current value of the location counter.
3105
3106 If you provide @var{address}, the address of the output section will be
3107 set to precisely that. If you provide neither @var{address} nor
3108 @var{region}, then the address of the output section will be set to the
3109 current value of the location counter aligned to the alignment
3110 requirements of the output section. The alignment requirement of the
3111 output section is the strictest alignment of any input section contained
3112 within the output section.
3113
3114 For example,
3115 @smallexample
3116 .text . : @{ *(.text) @}
3117 @end smallexample
3118 @noindent
3119 and
3120 @smallexample
3121 .text : @{ *(.text) @}
3122 @end smallexample
3123 @noindent
3124 are subtly different. The first will set the address of the
3125 @samp{.text} output section to the current value of the location
3126 counter. The second will set it to the current value of the location
3127 counter aligned to the strictest alignment of a @samp{.text} input
3128 section.
3129
3130 The @var{address} may be an arbitrary expression; @ref{Expressions}.
3131 For example, if you want to align the section on a 0x10 byte boundary,
3132 so that the lowest four bits of the section address are zero, you could
3133 do something like this:
3134 @smallexample
3135 .text ALIGN(0x10) : @{ *(.text) @}
3136 @end smallexample
3137 @noindent
3138 This works because @code{ALIGN} returns the current location counter
3139 aligned upward to the specified value.
3140
3141 Specifying @var{address} for a section will change the value of the
3142 location counter.
3143
3144 @node Input Section
3145 @subsection Input Section Description
3146 @cindex input sections
3147 @cindex mapping input sections to output sections
3148 The most common output section command is an input section description.
3149
3150 The input section description is the most basic linker script operation.
3151 You use output sections to tell the linker how to lay out your program
3152 in memory. You use input section descriptions to tell the linker how to
3153 map the input files into your memory layout.
3154
3155 @menu
3156 * Input Section Basics:: Input section basics
3157 * Input Section Wildcards:: Input section wildcard patterns
3158 * Input Section Common:: Input section for common symbols
3159 * Input Section Keep:: Input section and garbage collection
3160 * Input Section Example:: Input section example
3161 @end menu
3162
3163 @node Input Section Basics
3164 @subsubsection Input Section Basics
3165 @cindex input section basics
3166 An input section description consists of a file name optionally followed
3167 by a list of section names in parentheses.
3168
3169 The file name and the section name may be wildcard patterns, which we
3170 describe further below (@pxref{Input Section Wildcards}).
3171
3172 The most common input section description is to include all input
3173 sections with a particular name in the output section. For example, to
3174 include all input @samp{.text} sections, you would write:
3175 @smallexample
3176 *(.text)
3177 @end smallexample
3178 @noindent
3179 Here the @samp{*} is a wildcard which matches any file name. To exclude a list
3180 of files from matching the file name wildcard, EXCLUDE_FILE may be used to
3181 match all files except the ones specified in the EXCLUDE_FILE list. For
3182 example:
3183 @smallexample
3184 (*(EXCLUDE_FILE (*crtend.o *otherfile.o) .ctors))
3185 @end smallexample
3186 will cause all .ctors sections from all files except @file{crtend.o} and
3187 @file{otherfile.o} to be included.
3188
3189 There are two ways to include more than one section:
3190 @smallexample
3191 *(.text .rdata)
3192 *(.text) *(.rdata)
3193 @end smallexample
3194 @noindent
3195 The difference between these is the order in which the @samp{.text} and
3196 @samp{.rdata} input sections will appear in the output section. In the
3197 first example, they will be intermingled, appearing in the same order as
3198 they are found in the linker input. In the second example, all
3199 @samp{.text} input sections will appear first, followed by all
3200 @samp{.rdata} input sections.
3201
3202 You can specify a file name to include sections from a particular file.
3203 You would do this if one or more of your files contain special data that
3204 needs to be at a particular location in memory. For example:
3205 @smallexample
3206 data.o(.data)
3207 @end smallexample
3208
3209 If you use a file name without a list of sections, then all sections in
3210 the input file will be included in the output section. This is not
3211 commonly done, but it may by useful on occasion. For example:
3212 @smallexample
3213 data.o
3214 @end smallexample
3215
3216 When you use a file name which does not contain any wild card
3217 characters, the linker will first see if you also specified the file
3218 name on the linker command line or in an @code{INPUT} command. If you
3219 did not, the linker will attempt to open the file as an input file, as
3220 though it appeared on the command line. Note that this differs from an
3221 @code{INPUT} command, because the linker will not search for the file in
3222 the archive search path.
3223
3224 @node Input Section Wildcards
3225 @subsubsection Input Section Wildcard Patterns
3226 @cindex input section wildcards
3227 @cindex wildcard file name patterns
3228 @cindex file name wildcard patterns
3229 @cindex section name wildcard patterns
3230 In an input section description, either the file name or the section
3231 name or both may be wildcard patterns.
3232
3233 The file name of @samp{*} seen in many examples is a simple wildcard
3234 pattern for the file name.
3235
3236 The wildcard patterns are like those used by the Unix shell.
3237
3238 @table @samp
3239 @item *
3240 matches any number of characters
3241 @item ?
3242 matches any single character
3243 @item [@var{chars}]
3244 matches a single instance of any of the @var{chars}; the @samp{-}
3245 character may be used to specify a range of characters, as in
3246 @samp{[a-z]} to match any lower case letter
3247 @item \
3248 quotes the following character
3249 @end table
3250
3251 When a file name is matched with a wildcard, the wildcard characters
3252 will not match a @samp{/} character (used to separate directory names on
3253 Unix). A pattern consisting of a single @samp{*} character is an
3254 exception; it will always match any file name, whether it contains a
3255 @samp{/} or not. In a section name, the wildcard characters will match
3256 a @samp{/} character.
3257
3258 File name wildcard patterns only match files which are explicitly
3259 specified on the command line or in an @code{INPUT} command. The linker
3260 does not search directories to expand wildcards.
3261
3262 If a file name matches more than one wildcard pattern, or if a file name
3263 appears explicitly and is also matched by a wildcard pattern, the linker
3264 will use the first match in the linker script. For example, this
3265 sequence of input section descriptions is probably in error, because the
3266 @file{data.o} rule will not be used:
3267 @smallexample
3268 .data : @{ *(.data) @}
3269 .data1 : @{ data.o(.data) @}
3270 @end smallexample
3271
3272 @cindex SORT_BY_NAME
3273 Normally, the linker will place files and sections matched by wildcards
3274 in the order in which they are seen during the link. You can change
3275 this by using the @code{SORT_BY_NAME} keyword, which appears before a wildcard
3276 pattern in parentheses (e.g., @code{SORT_BY_NAME(.text*)}). When the
3277 @code{SORT_BY_NAME} keyword is used, the linker will sort the files or sections
3278 into ascending order by name before placing them in the output file.
3279
3280 @cindex SORT_BY_ALIGNMENT
3281 @code{SORT_BY_ALIGNMENT} is very similar to @code{SORT_BY_NAME}. The
3282 difference is @code{SORT_BY_ALIGNMENT} will sort sections into
3283 ascending order by alignment before placing them in the output file.
3284
3285 @cindex SORT
3286 @code{SORT} is an alias for @code{SORT_BY_NAME}.
3287
3288 When there are nested section sorting commands in linker script, there
3289 can be at most 1 level of nesting for section sorting commands.
3290
3291 @enumerate
3292 @item
3293 @code{SORT_BY_NAME} (@code{SORT_BY_ALIGNMENT} (wildcard section pattern)).
3294 It will sort the input sections by name first, then by alignment if 2
3295 sections have the same name.
3296 @item
3297 @code{SORT_BY_ALIGNMENT} (@code{SORT_BY_NAME} (wildcard section pattern)).
3298 It will sort the input sections by alignment first, then by name if 2
3299 sections have the same alignment.
3300 @item
3301 @code{SORT_BY_NAME} (@code{SORT_BY_NAME} (wildcard section pattern)) is
3302 treated the same as @code{SORT_BY_NAME} (wildcard section pattern).
3303 @item
3304 @code{SORT_BY_ALIGNMENT} (@code{SORT_BY_ALIGNMENT} (wildcard section pattern))
3305 is treated the same as @code{SORT_BY_ALIGNMENT} (wildcard section pattern).
3306 @item
3307 All other nested section sorting commands are invalid.
3308 @end enumerate
3309
3310 When both command line section sorting option and linker script
3311 section sorting command are used, section sorting command always
3312 takes precedence over the command line option.
3313
3314 If the section sorting command in linker script isn't nested, the
3315 command line option will make the section sorting command to be
3316 treated as nested sorting command.
3317
3318 @enumerate
3319 @item
3320 @code{SORT_BY_NAME} (wildcard section pattern ) with
3321 @option{--sort-sections alignment} is equivalent to
3322 @code{SORT_BY_NAME} (@code{SORT_BY_ALIGNMENT} (wildcard section pattern)).
3323 @item
3324 @code{SORT_BY_ALIGNMENT} (wildcard section pattern) with
3325 @option{--sort-section name} is equivalent to
3326 @code{SORT_BY_ALIGNMENT} (@code{SORT_BY_NAME} (wildcard section pattern)).
3327 @end enumerate
3328
3329 If the section sorting command in linker script is nested, the
3330 command line option will be ignored.
3331
3332 If you ever get confused about where input sections are going, use the
3333 @samp{-M} linker option to generate a map file. The map file shows
3334 precisely how input sections are mapped to output sections.
3335
3336 This example shows how wildcard patterns might be used to partition
3337 files. This linker script directs the linker to place all @samp{.text}
3338 sections in @samp{.text} and all @samp{.bss} sections in @samp{.bss}.
3339 The linker will place the @samp{.data} section from all files beginning
3340 with an upper case character in @samp{.DATA}; for all other files, the
3341 linker will place the @samp{.data} section in @samp{.data}.
3342 @smallexample
3343 @group
3344 SECTIONS @{
3345 .text : @{ *(.text) @}
3346 .DATA : @{ [A-Z]*(.data) @}
3347 .data : @{ *(.data) @}
3348 .bss : @{ *(.bss) @}
3349 @}
3350 @end group
3351 @end smallexample
3352
3353 @node Input Section Common
3354 @subsubsection Input Section for Common Symbols
3355 @cindex common symbol placement
3356 @cindex uninitialized data placement
3357 A special notation is needed for common symbols, because in many object
3358 file formats common symbols do not have a particular input section. The
3359 linker treats common symbols as though they are in an input section
3360 named @samp{COMMON}.
3361
3362 You may use file names with the @samp{COMMON} section just as with any
3363 other input sections. You can use this to place common symbols from a
3364 particular input file in one section while common symbols from other
3365 input files are placed in another section.
3366
3367 In most cases, common symbols in input files will be placed in the
3368 @samp{.bss} section in the output file. For example:
3369 @smallexample
3370 .bss @{ *(.bss) *(COMMON) @}
3371 @end smallexample
3372
3373 @cindex scommon section
3374 @cindex small common symbols
3375 Some object file formats have more than one type of common symbol. For
3376 example, the MIPS ELF object file format distinguishes standard common
3377 symbols and small common symbols. In this case, the linker will use a
3378 different special section name for other types of common symbols. In
3379 the case of MIPS ELF, the linker uses @samp{COMMON} for standard common
3380 symbols and @samp{.scommon} for small common symbols. This permits you
3381 to map the different types of common symbols into memory at different
3382 locations.
3383
3384 @cindex [COMMON]
3385 You will sometimes see @samp{[COMMON]} in old linker scripts. This
3386 notation is now considered obsolete. It is equivalent to
3387 @samp{*(COMMON)}.
3388
3389 @node Input Section Keep
3390 @subsubsection Input Section and Garbage Collection
3391 @cindex KEEP
3392 @cindex garbage collection
3393 When link-time garbage collection is in use (@samp{--gc-sections}),
3394 it is often useful to mark sections that should not be eliminated.
3395 This is accomplished by surrounding an input section's wildcard entry
3396 with @code{KEEP()}, as in @code{KEEP(*(.init))} or
3397 @code{KEEP(SORT_BY_NAME(*)(.ctors))}.
3398
3399 @node Input Section Example
3400 @subsubsection Input Section Example
3401 The following example is a complete linker script. It tells the linker
3402 to read all of the sections from file @file{all.o} and place them at the
3403 start of output section @samp{outputa} which starts at location
3404 @samp{0x10000}. All of section @samp{.input1} from file @file{foo.o}
3405 follows immediately, in the same output section. All of section
3406 @samp{.input2} from @file{foo.o} goes into output section
3407 @samp{outputb}, followed by section @samp{.input1} from @file{foo1.o}.
3408 All of the remaining @samp{.input1} and @samp{.input2} sections from any
3409 files are written to output section @samp{outputc}.
3410
3411 @smallexample
3412 @group
3413 SECTIONS @{
3414 outputa 0x10000 :
3415 @{
3416 all.o
3417 foo.o (.input1)
3418 @}
3419 @end group
3420 @group
3421 outputb :
3422 @{
3423 foo.o (.input2)
3424 foo1.o (.input1)
3425 @}
3426 @end group
3427 @group
3428 outputc :
3429 @{
3430 *(.input1)
3431 *(.input2)
3432 @}
3433 @}
3434 @end group
3435 @end smallexample
3436
3437 @node Output Section Data
3438 @subsection Output Section Data
3439 @cindex data
3440 @cindex section data
3441 @cindex output section data
3442 @kindex BYTE(@var{expression})
3443 @kindex SHORT(@var{expression})
3444 @kindex LONG(@var{expression})
3445 @kindex QUAD(@var{expression})
3446 @kindex SQUAD(@var{expression})
3447 You can include explicit bytes of data in an output section by using
3448 @code{BYTE}, @code{SHORT}, @code{LONG}, @code{QUAD}, or @code{SQUAD} as
3449 an output section command. Each keyword is followed by an expression in
3450 parentheses providing the value to store (@pxref{Expressions}). The
3451 value of the expression is stored at the current value of the location
3452 counter.
3453
3454 The @code{BYTE}, @code{SHORT}, @code{LONG}, and @code{QUAD} commands
3455 store one, two, four, and eight bytes (respectively). After storing the
3456 bytes, the location counter is incremented by the number of bytes
3457 stored.
3458
3459 For example, this will store the byte 1 followed by the four byte value
3460 of the symbol @samp{addr}:
3461 @smallexample
3462 BYTE(1)
3463 LONG(addr)
3464 @end smallexample
3465
3466 When using a 64 bit host or target, @code{QUAD} and @code{SQUAD} are the
3467 same; they both store an 8 byte, or 64 bit, value. When both host and
3468 target are 32 bits, an expression is computed as 32 bits. In this case
3469 @code{QUAD} stores a 32 bit value zero extended to 64 bits, and
3470 @code{SQUAD} stores a 32 bit value sign extended to 64 bits.
3471
3472 If the object file format of the output file has an explicit endianness,
3473 which is the normal case, the value will be stored in that endianness.
3474 When the object file format does not have an explicit endianness, as is
3475 true of, for example, S-records, the value will be stored in the
3476 endianness of the first input object file.
3477
3478 Note---these commands only work inside a section description and not
3479 between them, so the following will produce an error from the linker:
3480 @smallexample
3481 SECTIONS @{@ .text : @{@ *(.text) @}@ LONG(1) .data : @{@ *(.data) @}@ @}@
3482 @end smallexample
3483 whereas this will work:
3484 @smallexample
3485 SECTIONS @{@ .text : @{@ *(.text) ; LONG(1) @}@ .data : @{@ *(.data) @}@ @}@
3486 @end smallexample
3487
3488 @kindex FILL(@var{expression})
3489 @cindex holes, filling
3490 @cindex unspecified memory
3491 You may use the @code{FILL} command to set the fill pattern for the
3492 current section. It is followed by an expression in parentheses. Any
3493 otherwise unspecified regions of memory within the section (for example,
3494 gaps left due to the required alignment of input sections) are filled
3495 with the value of the expression, repeated as
3496 necessary. A @code{FILL} statement covers memory locations after the
3497 point at which it occurs in the section definition; by including more
3498 than one @code{FILL} statement, you can have different fill patterns in
3499 different parts of an output section.
3500
3501 This example shows how to fill unspecified regions of memory with the
3502 value @samp{0x90}:
3503 @smallexample
3504 FILL(0x90909090)
3505 @end smallexample
3506
3507 The @code{FILL} command is similar to the @samp{=@var{fillexp}} output
3508 section attribute, but it only affects the
3509 part of the section following the @code{FILL} command, rather than the
3510 entire section. If both are used, the @code{FILL} command takes
3511 precedence. @xref{Output Section Fill}, for details on the fill
3512 expression.
3513
3514 @node Output Section Keywords
3515 @subsection Output Section Keywords
3516 There are a couple of keywords which can appear as output section
3517 commands.
3518
3519 @table @code
3520 @kindex CREATE_OBJECT_SYMBOLS
3521 @cindex input filename symbols
3522 @cindex filename symbols
3523 @item CREATE_OBJECT_SYMBOLS
3524 The command tells the linker to create a symbol for each input file.
3525 The name of each symbol will be the name of the corresponding input
3526 file. The section of each symbol will be the output section in which
3527 the @code{CREATE_OBJECT_SYMBOLS} command appears.
3528
3529 This is conventional for the a.out object file format. It is not
3530 normally used for any other object file format.
3531
3532 @kindex CONSTRUCTORS
3533 @cindex C++ constructors, arranging in link
3534 @cindex constructors, arranging in link
3535 @item CONSTRUCTORS
3536 When linking using the a.out object file format, the linker uses an
3537 unusual set construct to support C++ global constructors and
3538 destructors. When linking object file formats which do not support
3539 arbitrary sections, such as ECOFF and XCOFF, the linker will
3540 automatically recognize C++ global constructors and destructors by name.
3541 For these object file formats, the @code{CONSTRUCTORS} command tells the
3542 linker to place constructor information in the output section where the
3543 @code{CONSTRUCTORS} command appears. The @code{CONSTRUCTORS} command is
3544 ignored for other object file formats.
3545
3546 The symbol @w{@code{__CTOR_LIST__}} marks the start of the global
3547 constructors, and the symbol @w{@code{__CTOR_END__}} marks the end.
3548 Similarly, @w{@code{__DTOR_LIST__}} and @w{@code{__DTOR_END__}} mark
3549 the start and end of the global destructors. The
3550 first word in the list is the number of entries, followed by the address
3551 of each constructor or destructor, followed by a zero word. The
3552 compiler must arrange to actually run the code. For these object file
3553 formats @sc{gnu} C++ normally calls constructors from a subroutine
3554 @code{__main}; a call to @code{__main} is automatically inserted into
3555 the startup code for @code{main}. @sc{gnu} C++ normally runs
3556 destructors either by using @code{atexit}, or directly from the function
3557 @code{exit}.
3558
3559 For object file formats such as @code{COFF} or @code{ELF} which support
3560 arbitrary section names, @sc{gnu} C++ will normally arrange to put the
3561 addresses of global constructors and destructors into the @code{.ctors}
3562 and @code{.dtors} sections. Placing the following sequence into your
3563 linker script will build the sort of table which the @sc{gnu} C++
3564 runtime code expects to see.
3565
3566 @smallexample
3567 __CTOR_LIST__ = .;
3568 LONG((__CTOR_END__ - __CTOR_LIST__) / 4 - 2)
3569 *(.ctors)
3570 LONG(0)
3571 __CTOR_END__ = .;
3572 __DTOR_LIST__ = .;
3573 LONG((__DTOR_END__ - __DTOR_LIST__) / 4 - 2)
3574 *(.dtors)
3575 LONG(0)
3576 __DTOR_END__ = .;
3577 @end smallexample
3578
3579 If you are using the @sc{gnu} C++ support for initialization priority,
3580 which provides some control over the order in which global constructors
3581 are run, you must sort the constructors at link time to ensure that they
3582 are executed in the correct order. When using the @code{CONSTRUCTORS}
3583 command, use @samp{SORT_BY_NAME(CONSTRUCTORS)} instead. When using the
3584 @code{.ctors} and @code{.dtors} sections, use @samp{*(SORT_BY_NAME(.ctors))} and
3585 @samp{*(SORT_BY_NAME(.dtors))} instead of just @samp{*(.ctors)} and
3586 @samp{*(.dtors)}.
3587
3588 Normally the compiler and linker will handle these issues automatically,
3589 and you will not need to concern yourself with them. However, you may
3590 need to consider this if you are using C++ and writing your own linker
3591 scripts.
3592
3593 @end table
3594
3595 @node Output Section Discarding
3596 @subsection Output Section Discarding
3597 @cindex discarding sections
3598 @cindex sections, discarding
3599 @cindex removing sections
3600 The linker will not create output section which do not have any
3601 contents. This is for convenience when referring to input sections that
3602 may or may not be present in any of the input files. For example:
3603 @smallexample
3604 .foo @{ *(.foo) @}
3605 @end smallexample
3606 @noindent
3607 will only create a @samp{.foo} section in the output file if there is a
3608 @samp{.foo} section in at least one input file.
3609
3610 If you use anything other than an input section description as an output
3611 section command, such as a symbol assignment, then the output section
3612 will always be created, even if there are no matching input sections.
3613
3614 @cindex /DISCARD/
3615 The special output section name @samp{/DISCARD/} may be used to discard
3616 input sections. Any input sections which are assigned to an output
3617 section named @samp{/DISCARD/} are not included in the output file.
3618
3619 @node Output Section Attributes
3620 @subsection Output Section Attributes
3621 @cindex output section attributes
3622 We showed above that the full description of an output section looked
3623 like this:
3624 @smallexample
3625 @group
3626 @var{section} [@var{address}] [(@var{type})] :
3627 [AT(@var{lma})] [SUBALIGN(@var{subsection_align})]
3628 @{
3629 @var{output-section-command}
3630 @var{output-section-command}
3631 @dots{}
3632 @} [>@var{region}] [AT>@var{lma_region}] [:@var{phdr} :@var{phdr} @dots{}] [=@var{fillexp}]
3633 @end group
3634 @end smallexample
3635 We've already described @var{section}, @var{address}, and
3636 @var{output-section-command}. In this section we will describe the
3637 remaining section attributes.
3638
3639 @menu
3640 * Output Section Type:: Output section type
3641 * Output Section LMA:: Output section LMA
3642 * Forced Input Alignment:: Forced Input Alignment
3643 * Output Section Region:: Output section region
3644 * Output Section Phdr:: Output section phdr
3645 * Output Section Fill:: Output section fill
3646 @end menu
3647
3648 @node Output Section Type
3649 @subsubsection Output Section Type
3650 Each output section may have a type. The type is a keyword in
3651 parentheses. The following types are defined:
3652
3653 @table @code
3654 @item NOLOAD
3655 The section should be marked as not loadable, so that it will not be
3656 loaded into memory when the program is run.
3657 @item DSECT
3658 @itemx COPY
3659 @itemx INFO
3660 @itemx OVERLAY
3661 These type names are supported for backward compatibility, and are
3662 rarely used. They all have the same effect: the section should be
3663 marked as not allocatable, so that no memory is allocated for the
3664 section when the program is run.
3665 @end table
3666
3667 @kindex NOLOAD
3668 @cindex prevent unnecessary loading
3669 @cindex loading, preventing
3670 The linker normally sets the attributes of an output section based on
3671 the input sections which map into it. You can override this by using
3672 the section type. For example, in the script sample below, the
3673 @samp{ROM} section is addressed at memory location @samp{0} and does not
3674 need to be loaded when the program is run. The contents of the
3675 @samp{ROM} section will appear in the linker output file as usual.
3676 @smallexample
3677 @group
3678 SECTIONS @{
3679 ROM 0 (NOLOAD) : @{ @dots{} @}
3680 @dots{}
3681 @}
3682 @end group
3683 @end smallexample
3684
3685 @node Output Section LMA
3686 @subsubsection Output Section LMA
3687 @kindex AT>@var{lma_region}
3688 @kindex AT(@var{lma})
3689 @cindex load address
3690 @cindex section load address
3691 Every section has a virtual address (VMA) and a load address (LMA); see
3692 @ref{Basic Script Concepts}. The address expression which may appear in
3693 an output section description sets the VMA (@pxref{Output Section
3694 Address}).
3695
3696 The linker will normally set the LMA equal to the VMA. You can change
3697 that by using the @code{AT} keyword. The expression @var{lma} that
3698 follows the @code{AT} keyword specifies the load address of the
3699 section.
3700
3701 Alternatively, with @samp{AT>@var{lma_region}} expression, you may
3702 specify a memory region for the section's load address. @xref{MEMORY}.
3703 Note that if the section has not had a VMA assigned to it then the
3704 linker will use the @var{lma_region} as the VMA region as well.
3705 @xref{Output Section Region}.
3706
3707 @cindex ROM initialized data
3708 @cindex initialized data in ROM
3709 This feature is designed to make it easy to build a ROM image. For
3710 example, the following linker script creates three output sections: one
3711 called @samp{.text}, which starts at @code{0x1000}, one called
3712 @samp{.mdata}, which is loaded at the end of the @samp{.text} section
3713 even though its VMA is @code{0x2000}, and one called @samp{.bss} to hold
3714 uninitialized data at address @code{0x3000}. The symbol @code{_data} is
3715 defined with the value @code{0x2000}, which shows that the location
3716 counter holds the VMA value, not the LMA value.
3717
3718 @smallexample
3719 @group
3720 SECTIONS
3721 @{
3722 .text 0x1000 : @{ *(.text) _etext = . ; @}
3723 .mdata 0x2000 :
3724 AT ( ADDR (.text) + SIZEOF (.text) )
3725 @{ _data = . ; *(.data); _edata = . ; @}
3726 .bss 0x3000 :
3727 @{ _bstart = . ; *(.bss) *(COMMON) ; _bend = . ;@}
3728 @}
3729 @end group
3730 @end smallexample
3731
3732 The run-time initialization code for use with a program generated with
3733 this linker script would include something like the following, to copy
3734 the initialized data from the ROM image to its runtime address. Notice
3735 how this code takes advantage of the symbols defined by the linker
3736 script.
3737
3738 @smallexample
3739 @group
3740 extern char _etext, _data, _edata, _bstart, _bend;
3741 char *src = &_etext;
3742 char *dst = &_data;
3743
3744 /* ROM has data at end of text; copy it. */
3745 while (dst < &_edata) @{
3746 *dst++ = *src++;
3747 @}
3748
3749 /* Zero bss */
3750 for (dst = &_bstart; dst< &_bend; dst++)
3751 *dst = 0;
3752 @end group
3753 @end smallexample
3754
3755 @node Forced Input Alignment
3756 @subsubsection Forced Input Alignment
3757 @kindex SUBALIGN(@var{subsection_align})
3758 @cindex forcing input section alignment
3759 @cindex input section alignment
3760 You can force input section alignment within an output section by using
3761 SUBALIGN. The value specified overrides any alignment given by input
3762 sections, whether larger or smaller.
3763
3764 @node Output Section Region
3765 @subsubsection Output Section Region
3766 @kindex >@var{region}
3767 @cindex section, assigning to memory region
3768 @cindex memory regions and sections
3769 You can assign a section to a previously defined region of memory by
3770 using @samp{>@var{region}}. @xref{MEMORY}.
3771
3772 Here is a simple example:
3773 @smallexample
3774 @group
3775 MEMORY @{ rom : ORIGIN = 0x1000, LENGTH = 0x1000 @}
3776 SECTIONS @{ ROM : @{ *(.text) @} >rom @}
3777 @end group
3778 @end smallexample
3779
3780 @node Output Section Phdr
3781 @subsubsection Output Section Phdr
3782 @kindex :@var{phdr}
3783 @cindex section, assigning to program header
3784 @cindex program headers and sections
3785 You can assign a section to a previously defined program segment by
3786 using @samp{:@var{phdr}}. @xref{PHDRS}. If a section is assigned to
3787 one or more segments, then all subsequent allocated sections will be
3788 assigned to those segments as well, unless they use an explicitly
3789 @code{:@var{phdr}} modifier. You can use @code{:NONE} to tell the
3790 linker to not put the section in any segment at all.
3791
3792 Here is a simple example:
3793 @smallexample
3794 @group
3795 PHDRS @{ text PT_LOAD ; @}
3796 SECTIONS @{ .text : @{ *(.text) @} :text @}
3797 @end group
3798 @end smallexample
3799
3800 @node Output Section Fill
3801 @subsubsection Output Section Fill
3802 @kindex =@var{fillexp}
3803 @cindex section fill pattern
3804 @cindex fill pattern, entire section
3805 You can set the fill pattern for an entire section by using
3806 @samp{=@var{fillexp}}. @var{fillexp} is an expression
3807 (@pxref{Expressions}). Any otherwise unspecified regions of memory
3808 within the output section (for example, gaps left due to the required
3809 alignment of input sections) will be filled with the value, repeated as
3810 necessary. If the fill expression is a simple hex number, ie. a string
3811 of hex digit starting with @samp{0x} and without a trailing @samp{k} or @samp{M}, then
3812 an arbitrarily long sequence of hex digits can be used to specify the
3813 fill pattern; Leading zeros become part of the pattern too. For all
3814 other cases, including extra parentheses or a unary @code{+}, the fill
3815 pattern is the four least significant bytes of the value of the
3816 expression. In all cases, the number is big-endian.
3817
3818 You can also change the fill value with a @code{FILL} command in the
3819 output section commands; (@pxref{Output Section Data}).
3820
3821 Here is a simple example:
3822 @smallexample
3823 @group
3824 SECTIONS @{ .text : @{ *(.text) @} =0x90909090 @}
3825 @end group
3826 @end smallexample
3827
3828 @node Overlay Description
3829 @subsection Overlay Description
3830 @kindex OVERLAY
3831 @cindex overlays
3832 An overlay description provides an easy way to describe sections which
3833 are to be loaded as part of a single memory image but are to be run at
3834 the same memory address. At run time, some sort of overlay manager will
3835 copy the overlaid sections in and out of the runtime memory address as
3836 required, perhaps by simply manipulating addressing bits. This approach
3837 can be useful, for example, when a certain region of memory is faster
3838 than another.
3839
3840 Overlays are described using the @code{OVERLAY} command. The
3841 @code{OVERLAY} command is used within a @code{SECTIONS} command, like an
3842 output section description. The full syntax of the @code{OVERLAY}
3843 command is as follows:
3844 @smallexample
3845 @group
3846 OVERLAY [@var{start}] : [NOCROSSREFS] [AT ( @var{ldaddr} )]
3847 @{
3848 @var{secname1}
3849 @{
3850 @var{output-section-command}
3851 @var{output-section-command}
3852 @dots{}
3853 @} [:@var{phdr}@dots{}] [=@var{fill}]
3854 @var{secname2}
3855 @{
3856 @var{output-section-command}
3857 @var{output-section-command}
3858 @dots{}
3859 @} [:@var{phdr}@dots{}] [=@var{fill}]
3860 @dots{}
3861 @} [>@var{region}] [:@var{phdr}@dots{}] [=@var{fill}]
3862 @end group
3863 @end smallexample
3864
3865 Everything is optional except @code{OVERLAY} (a keyword), and each
3866 section must have a name (@var{secname1} and @var{secname2} above). The
3867 section definitions within the @code{OVERLAY} construct are identical to
3868 those within the general @code{SECTIONS} contruct (@pxref{SECTIONS}),
3869 except that no addresses and no memory regions may be defined for
3870 sections within an @code{OVERLAY}.
3871
3872 The sections are all defined with the same starting address. The load
3873 addresses of the sections are arranged such that they are consecutive in
3874 memory starting at the load address used for the @code{OVERLAY} as a
3875 whole (as with normal section definitions, the load address is optional,
3876 and defaults to the start address; the start address is also optional,
3877 and defaults to the current value of the location counter).
3878
3879 If the @code{NOCROSSREFS} keyword is used, and there any references
3880 among the sections, the linker will report an error. Since the sections
3881 all run at the same address, it normally does not make sense for one
3882 section to refer directly to another. @xref{Miscellaneous Commands,
3883 NOCROSSREFS}.
3884
3885 For each section within the @code{OVERLAY}, the linker automatically
3886 defines two symbols. The symbol @code{__load_start_@var{secname}} is
3887 defined as the starting load address of the section. The symbol
3888 @code{__load_stop_@var{secname}} is defined as the final load address of
3889 the section. Any characters within @var{secname} which are not legal
3890 within C identifiers are removed. C (or assembler) code may use these
3891 symbols to move the overlaid sections around as necessary.
3892
3893 At the end of the overlay, the value of the location counter is set to
3894 the start address of the overlay plus the size of the largest section.
3895
3896 Here is an example. Remember that this would appear inside a
3897 @code{SECTIONS} construct.
3898 @smallexample
3899 @group
3900 OVERLAY 0x1000 : AT (0x4000)
3901 @{
3902 .text0 @{ o1/*.o(.text) @}
3903 .text1 @{ o2/*.o(.text) @}
3904 @}
3905 @end group
3906 @end smallexample
3907 @noindent
3908 This will define both @samp{.text0} and @samp{.text1} to start at
3909 address 0x1000. @samp{.text0} will be loaded at address 0x4000, and
3910 @samp{.text1} will be loaded immediately after @samp{.text0}. The
3911 following symbols will be defined: @code{__load_start_text0},
3912 @code{__load_stop_text0}, @code{__load_start_text1},
3913 @code{__load_stop_text1}.
3914
3915 C code to copy overlay @code{.text1} into the overlay area might look
3916 like the following.
3917
3918 @smallexample
3919 @group
3920 extern char __load_start_text1, __load_stop_text1;
3921 memcpy ((char *) 0x1000, &__load_start_text1,
3922 &__load_stop_text1 - &__load_start_text1);
3923 @end group
3924 @end smallexample
3925
3926 Note that the @code{OVERLAY} command is just syntactic sugar, since
3927 everything it does can be done using the more basic commands. The above
3928 example could have been written identically as follows.
3929
3930 @smallexample
3931 @group
3932 .text0 0x1000 : AT (0x4000) @{ o1/*.o(.text) @}
3933 __load_start_text0 = LOADADDR (.text0);
3934 __load_stop_text0 = LOADADDR (.text0) + SIZEOF (.text0);
3935 .text1 0x1000 : AT (0x4000 + SIZEOF (.text0)) @{ o2/*.o(.text) @}
3936 __load_start_text1 = LOADADDR (.text1);
3937 __load_stop_text1 = LOADADDR (.text1) + SIZEOF (.text1);
3938 . = 0x1000 + MAX (SIZEOF (.text0), SIZEOF (.text1));
3939 @end group
3940 @end smallexample
3941
3942 @node MEMORY
3943 @section MEMORY Command
3944 @kindex MEMORY
3945 @cindex memory regions
3946 @cindex regions of memory
3947 @cindex allocating memory
3948 @cindex discontinuous memory
3949 The linker's default configuration permits allocation of all available
3950 memory. You can override this by using the @code{MEMORY} command.
3951
3952 The @code{MEMORY} command describes the location and size of blocks of
3953 memory in the target. You can use it to describe which memory regions
3954 may be used by the linker, and which memory regions it must avoid. You
3955 can then assign sections to particular memory regions. The linker will
3956 set section addresses based on the memory regions, and will warn about
3957 regions that become too full. The linker will not shuffle sections
3958 around to fit into the available regions.
3959
3960 A linker script may contain at most one use of the @code{MEMORY}
3961 command. However, you can define as many blocks of memory within it as
3962 you wish. The syntax is:
3963 @smallexample
3964 @group
3965 MEMORY
3966 @{
3967 @var{name} [(@var{attr})] : ORIGIN = @var{origin}, LENGTH = @var{len}
3968 @dots{}
3969 @}
3970 @end group
3971 @end smallexample
3972
3973 The @var{name} is a name used in the linker script to refer to the
3974 region. The region name has no meaning outside of the linker script.
3975 Region names are stored in a separate name space, and will not conflict
3976 with symbol names, file names, or section names. Each memory region
3977 must have a distinct name.
3978
3979 @cindex memory region attributes
3980 The @var{attr} string is an optional list of attributes that specify
3981 whether to use a particular memory region for an input section which is
3982 not explicitly mapped in the linker script. As described in
3983 @ref{SECTIONS}, if you do not specify an output section for some input
3984 section, the linker will create an output section with the same name as
3985 the input section. If you define region attributes, the linker will use
3986 them to select the memory region for the output section that it creates.
3987
3988 The @var{attr} string must consist only of the following characters:
3989 @table @samp
3990 @item R
3991 Read-only section
3992 @item W
3993 Read/write section
3994 @item X
3995 Executable section
3996 @item A
3997 Allocatable section
3998 @item I
3999 Initialized section
4000 @item L
4001 Same as @samp{I}
4002 @item !
4003 Invert the sense of any of the preceding attributes
4004 @end table
4005
4006 If a unmapped section matches any of the listed attributes other than
4007 @samp{!}, it will be placed in the memory region. The @samp{!}
4008 attribute reverses this test, so that an unmapped section will be placed
4009 in the memory region only if it does not match any of the listed
4010 attributes.
4011
4012 @kindex ORIGIN =
4013 @kindex o =
4014 @kindex org =
4015 The @var{origin} is an numerical expression for the start address of
4016 the memory region. The expression must evaluate to a constant and it
4017 cannot involve any symbols. The keyword @code{ORIGIN} may be
4018 abbreviated to @code{org} or @code{o} (but not, for example,
4019 @code{ORG}).
4020
4021 @kindex LENGTH =
4022 @kindex len =
4023 @kindex l =
4024 The @var{len} is an expression for the size in bytes of the memory
4025 region. As with the @var{origin} expression, the expression must
4026 be numerical only and must evaluate to a constant. The keyword
4027 @code{LENGTH} may be abbreviated to @code{len} or @code{l}.
4028
4029 In the following example, we specify that there are two memory regions
4030 available for allocation: one starting at @samp{0} for 256 kilobytes,
4031 and the other starting at @samp{0x40000000} for four megabytes. The
4032 linker will place into the @samp{rom} memory region every section which
4033 is not explicitly mapped into a memory region, and is either read-only
4034 or executable. The linker will place other sections which are not
4035 explicitly mapped into a memory region into the @samp{ram} memory
4036 region.
4037
4038 @smallexample
4039 @group
4040 MEMORY
4041 @{
4042 rom (rx) : ORIGIN = 0, LENGTH = 256K
4043 ram (!rx) : org = 0x40000000, l = 4M
4044 @}
4045 @end group
4046 @end smallexample
4047
4048 Once you define a memory region, you can direct the linker to place
4049 specific output sections into that memory region by using the
4050 @samp{>@var{region}} output section attribute. For example, if you have
4051 a memory region named @samp{mem}, you would use @samp{>mem} in the
4052 output section definition. @xref{Output Section Region}. If no address
4053 was specified for the output section, the linker will set the address to
4054 the next available address within the memory region. If the combined
4055 output sections directed to a memory region are too large for the
4056 region, the linker will issue an error message.
4057
4058 It is possible to access the origin and length of a memory in an
4059 expression via the @code{ORIGIN(@var{memory})} and
4060 @code{LENGTH(@var{memory})} functions:
4061
4062 @smallexample
4063 @group
4064 _fstack = ORIGIN(ram) + LENGTH(ram) - 4;
4065 @end group
4066 @end smallexample
4067
4068 @node PHDRS
4069 @section PHDRS Command
4070 @kindex PHDRS
4071 @cindex program headers
4072 @cindex ELF program headers
4073 @cindex program segments
4074 @cindex segments, ELF
4075 The ELF object file format uses @dfn{program headers}, also knows as
4076 @dfn{segments}. The program headers describe how the program should be
4077 loaded into memory. You can print them out by using the @code{objdump}
4078 program with the @samp{-p} option.
4079
4080 When you run an ELF program on a native ELF system, the system loader
4081 reads the program headers in order to figure out how to load the
4082 program. This will only work if the program headers are set correctly.
4083 This manual does not describe the details of how the system loader
4084 interprets program headers; for more information, see the ELF ABI.
4085
4086 The linker will create reasonable program headers by default. However,
4087 in some cases, you may need to specify the program headers more
4088 precisely. You may use the @code{PHDRS} command for this purpose. When
4089 the linker sees the @code{PHDRS} command in the linker script, it will
4090 not create any program headers other than the ones specified.
4091
4092 The linker only pays attention to the @code{PHDRS} command when
4093 generating an ELF output file. In other cases, the linker will simply
4094 ignore @code{PHDRS}.
4095
4096 This is the syntax of the @code{PHDRS} command. The words @code{PHDRS},
4097 @code{FILEHDR}, @code{AT}, and @code{FLAGS} are keywords.
4098
4099 @smallexample
4100 @group
4101 PHDRS
4102 @{
4103 @var{name} @var{type} [ FILEHDR ] [ PHDRS ] [ AT ( @var{address} ) ]
4104 [ FLAGS ( @var{flags} ) ] ;
4105 @}
4106 @end group
4107 @end smallexample
4108
4109 The @var{name} is used only for reference in the @code{SECTIONS} command
4110 of the linker script. It is not put into the output file. Program
4111 header names are stored in a separate name space, and will not conflict
4112 with symbol names, file names, or section names. Each program header
4113 must have a distinct name.
4114
4115 Certain program header types describe segments of memory which the
4116 system loader will load from the file. In the linker script, you
4117 specify the contents of these segments by placing allocatable output
4118 sections in the segments. You use the @samp{:@var{phdr}} output section
4119 attribute to place a section in a particular segment. @xref{Output
4120 Section Phdr}.
4121
4122 It is normal to put certain sections in more than one segment. This
4123 merely implies that one segment of memory contains another. You may
4124 repeat @samp{:@var{phdr}}, using it once for each segment which should
4125 contain the section.
4126
4127 If you place a section in one or more segments using @samp{:@var{phdr}},
4128 then the linker will place all subsequent allocatable sections which do
4129 not specify @samp{:@var{phdr}} in the same segments. This is for
4130 convenience, since generally a whole set of contiguous sections will be
4131 placed in a single segment. You can use @code{:NONE} to override the
4132 default segment and tell the linker to not put the section in any
4133 segment at all.
4134
4135 @kindex FILEHDR
4136 @kindex PHDRS
4137 You may use the @code{FILEHDR} and @code{PHDRS} keywords appear after
4138 the program header type to further describe the contents of the segment.
4139 The @code{FILEHDR} keyword means that the segment should include the ELF
4140 file header. The @code{PHDRS} keyword means that the segment should
4141 include the ELF program headers themselves.
4142
4143 The @var{type} may be one of the following. The numbers indicate the
4144 value of the keyword.
4145
4146 @table @asis
4147 @item @code{PT_NULL} (0)
4148 Indicates an unused program header.
4149
4150 @item @code{PT_LOAD} (1)
4151 Indicates that this program header describes a segment to be loaded from
4152 the file.
4153
4154 @item @code{PT_DYNAMIC} (2)
4155 Indicates a segment where dynamic linking information can be found.
4156
4157 @item @code{PT_INTERP} (3)
4158 Indicates a segment where the name of the program interpreter may be
4159 found.
4160
4161 @item @code{PT_NOTE} (4)
4162 Indicates a segment holding note information.
4163
4164 @item @code{PT_SHLIB} (5)
4165 A reserved program header type, defined but not specified by the ELF
4166 ABI.
4167
4168 @item @code{PT_PHDR} (6)
4169 Indicates a segment where the program headers may be found.
4170
4171 @item @var{expression}
4172 An expression giving the numeric type of the program header. This may
4173 be used for types not defined above.
4174 @end table
4175
4176 You can specify that a segment should be loaded at a particular address
4177 in memory by using an @code{AT} expression. This is identical to the
4178 @code{AT} command used as an output section attribute (@pxref{Output
4179 Section LMA}). The @code{AT} command for a program header overrides the
4180 output section attribute.
4181
4182 The linker will normally set the segment flags based on the sections
4183 which comprise the segment. You may use the @code{FLAGS} keyword to
4184 explicitly specify the segment flags. The value of @var{flags} must be
4185 an integer. It is used to set the @code{p_flags} field of the program
4186 header.
4187
4188 Here is an example of @code{PHDRS}. This shows a typical set of program
4189 headers used on a native ELF system.
4190
4191 @example
4192 @group
4193 PHDRS
4194 @{
4195 headers PT_PHDR PHDRS ;
4196 interp PT_INTERP ;
4197 text PT_LOAD FILEHDR PHDRS ;
4198 data PT_LOAD ;
4199 dynamic PT_DYNAMIC ;
4200 @}
4201
4202 SECTIONS
4203 @{
4204 . = SIZEOF_HEADERS;
4205 .interp : @{ *(.interp) @} :text :interp
4206 .text : @{ *(.text) @} :text
4207 .rodata : @{ *(.rodata) @} /* defaults to :text */
4208 @dots{}
4209 . = . + 0x1000; /* move to a new page in memory */
4210 .data : @{ *(.data) @} :data
4211 .dynamic : @{ *(.dynamic) @} :data :dynamic
4212 @dots{}
4213 @}
4214 @end group
4215 @end example
4216
4217 @node VERSION
4218 @section VERSION Command
4219 @kindex VERSION @{script text@}
4220 @cindex symbol versions
4221 @cindex version script
4222 @cindex versions of symbols
4223 The linker supports symbol versions when using ELF. Symbol versions are
4224 only useful when using shared libraries. The dynamic linker can use
4225 symbol versions to select a specific version of a function when it runs
4226 a program that may have been linked against an earlier version of the
4227 shared library.
4228
4229 You can include a version script directly in the main linker script, or
4230 you can supply the version script as an implicit linker script. You can
4231 also use the @samp{--version-script} linker option.
4232
4233 The syntax of the @code{VERSION} command is simply
4234 @smallexample
4235 VERSION @{ version-script-commands @}
4236 @end smallexample
4237
4238 The format of the version script commands is identical to that used by
4239 Sun's linker in Solaris 2.5. The version script defines a tree of
4240 version nodes. You specify the node names and interdependencies in the
4241 version script. You can specify which symbols are bound to which
4242 version nodes, and you can reduce a specified set of symbols to local
4243 scope so that they are not globally visible outside of the shared
4244 library.
4245
4246 The easiest way to demonstrate the version script language is with a few
4247 examples.
4248
4249 @smallexample
4250 VERS_1.1 @{
4251 global:
4252 foo1;
4253 local:
4254 old*;
4255 original*;
4256 new*;
4257 @};
4258
4259 VERS_1.2 @{
4260 foo2;
4261 @} VERS_1.1;
4262
4263 VERS_2.0 @{
4264 bar1; bar2;
4265 @} VERS_1.2;
4266 @end smallexample
4267
4268 This example version script defines three version nodes. The first
4269 version node defined is @samp{VERS_1.1}; it has no other dependencies.
4270 The script binds the symbol @samp{foo1} to @samp{VERS_1.1}. It reduces
4271 a number of symbols to local scope so that they are not visible outside
4272 of the shared library; this is done using wildcard patterns, so that any
4273 symbol whose name begins with @samp{old}, @samp{original}, or @samp{new}
4274 is matched. The wildcard patterns available are the same as those used
4275 in the shell when matching filenames (also known as ``globbing'').
4276
4277 Next, the version script defines node @samp{VERS_1.2}. This node
4278 depends upon @samp{VERS_1.1}. The script binds the symbol @samp{foo2}
4279 to the version node @samp{VERS_1.2}.
4280
4281 Finally, the version script defines node @samp{VERS_2.0}. This node
4282 depends upon @samp{VERS_1.2}. The scripts binds the symbols @samp{bar1}
4283 and @samp{bar2} are bound to the version node @samp{VERS_2.0}.
4284
4285 When the linker finds a symbol defined in a library which is not
4286 specifically bound to a version node, it will effectively bind it to an
4287 unspecified base version of the library. You can bind all otherwise
4288 unspecified symbols to a given version node by using @samp{global: *;}
4289 somewhere in the version script.
4290
4291 The names of the version nodes have no specific meaning other than what
4292 they might suggest to the person reading them. The @samp{2.0} version
4293 could just as well have appeared in between @samp{1.1} and @samp{1.2}.
4294 However, this would be a confusing way to write a version script.
4295
4296 Node name can be omited, provided it is the only version node
4297 in the version script. Such version script doesn't assign any versions to
4298 symbols, only selects which symbols will be globally visible out and which
4299 won't.
4300
4301 @smallexample
4302 @{ global: foo; bar; local: *; @};
4303 @end smallexample
4304
4305 When you link an application against a shared library that has versioned
4306 symbols, the application itself knows which version of each symbol it
4307 requires, and it also knows which version nodes it needs from each
4308 shared library it is linked against. Thus at runtime, the dynamic
4309 loader can make a quick check to make sure that the libraries you have
4310 linked against do in fact supply all of the version nodes that the
4311 application will need to resolve all of the dynamic symbols. In this
4312 way it is possible for the dynamic linker to know with certainty that
4313 all external symbols that it needs will be resolvable without having to
4314 search for each symbol reference.
4315
4316 The symbol versioning is in effect a much more sophisticated way of
4317 doing minor version checking that SunOS does. The fundamental problem
4318 that is being addressed here is that typically references to external
4319 functions are bound on an as-needed basis, and are not all bound when
4320 the application starts up. If a shared library is out of date, a
4321 required interface may be missing; when the application tries to use
4322 that interface, it may suddenly and unexpectedly fail. With symbol
4323 versioning, the user will get a warning when they start their program if
4324 the libraries being used with the application are too old.
4325
4326 There are several GNU extensions to Sun's versioning approach. The
4327 first of these is the ability to bind a symbol to a version node in the
4328 source file where the symbol is defined instead of in the versioning
4329 script. This was done mainly to reduce the burden on the library
4330 maintainer. You can do this by putting something like:
4331 @smallexample
4332 __asm__(".symver original_foo,foo@@VERS_1.1");
4333 @end smallexample
4334 @noindent
4335 in the C source file. This renames the function @samp{original_foo} to
4336 be an alias for @samp{foo} bound to the version node @samp{VERS_1.1}.
4337 The @samp{local:} directive can be used to prevent the symbol
4338 @samp{original_foo} from being exported. A @samp{.symver} directive
4339 takes precedence over a version script.
4340
4341 The second GNU extension is to allow multiple versions of the same
4342 function to appear in a given shared library. In this way you can make
4343 an incompatible change to an interface without increasing the major
4344 version number of the shared library, while still allowing applications
4345 linked against the old interface to continue to function.
4346
4347 To do this, you must use multiple @samp{.symver} directives in the
4348 source file. Here is an example:
4349
4350 @smallexample
4351 __asm__(".symver original_foo,foo@@");
4352 __asm__(".symver old_foo,foo@@VERS_1.1");
4353 __asm__(".symver old_foo1,foo@@VERS_1.2");
4354 __asm__(".symver new_foo,foo@@@@VERS_2.0");
4355 @end smallexample
4356
4357 In this example, @samp{foo@@} represents the symbol @samp{foo} bound to the
4358 unspecified base version of the symbol. The source file that contains this
4359 example would define 4 C functions: @samp{original_foo}, @samp{old_foo},
4360 @samp{old_foo1}, and @samp{new_foo}.
4361
4362 When you have multiple definitions of a given symbol, there needs to be
4363 some way to specify a default version to which external references to
4364 this symbol will be bound. You can do this with the
4365 @samp{foo@@@@VERS_2.0} type of @samp{.symver} directive. You can only
4366 declare one version of a symbol as the default in this manner; otherwise
4367 you would effectively have multiple definitions of the same symbol.
4368
4369 If you wish to bind a reference to a specific version of the symbol
4370 within the shared library, you can use the aliases of convenience
4371 (i.e., @samp{old_foo}), or you can use the @samp{.symver} directive to
4372 specifically bind to an external version of the function in question.
4373
4374 You can also specify the language in the version script:
4375
4376 @smallexample
4377 VERSION extern "lang" @{ version-script-commands @}
4378 @end smallexample
4379
4380 The supported @samp{lang}s are @samp{C}, @samp{C++}, and @samp{Java}.
4381 The linker will iterate over the list of symbols at the link time and
4382 demangle them according to @samp{lang} before matching them to the
4383 patterns specified in @samp{version-script-commands}.
4384
4385 @node Expressions
4386 @section Expressions in Linker Scripts
4387 @cindex expressions
4388 @cindex arithmetic
4389 The syntax for expressions in the linker script language is identical to
4390 that of C expressions. All expressions are evaluated as integers. All
4391 expressions are evaluated in the same size, which is 32 bits if both the
4392 host and target are 32 bits, and is otherwise 64 bits.
4393
4394 You can use and set symbol values in expressions.
4395
4396 The linker defines several special purpose builtin functions for use in
4397 expressions.
4398
4399 @menu
4400 * Constants:: Constants
4401 * Symbols:: Symbol Names
4402 * Location Counter:: The Location Counter
4403 * Operators:: Operators
4404 * Evaluation:: Evaluation
4405 * Expression Section:: The Section of an Expression
4406 * Builtin Functions:: Builtin Functions
4407 @end menu
4408
4409 @node Constants
4410 @subsection Constants
4411 @cindex integer notation
4412 @cindex constants in linker scripts
4413 All constants are integers.
4414
4415 As in C, the linker considers an integer beginning with @samp{0} to be
4416 octal, and an integer beginning with @samp{0x} or @samp{0X} to be
4417 hexadecimal. The linker considers other integers to be decimal.
4418
4419 @cindex scaled integers
4420 @cindex K and M integer suffixes
4421 @cindex M and K integer suffixes
4422 @cindex suffixes for integers
4423 @cindex integer suffixes
4424 In addition, you can use the suffixes @code{K} and @code{M} to scale a
4425 constant by
4426 @c TEXI2ROFF-KILL
4427 @ifnottex
4428 @c END TEXI2ROFF-KILL
4429 @code{1024} or @code{1024*1024}
4430 @c TEXI2ROFF-KILL
4431 @end ifnottex
4432 @tex
4433 ${\rm 1024}$ or ${\rm 1024}^2$
4434 @end tex
4435 @c END TEXI2ROFF-KILL
4436 respectively. For example, the following all refer to the same quantity:
4437 @smallexample
4438 _fourk_1 = 4K;
4439 _fourk_2 = 4096;
4440 _fourk_3 = 0x1000;
4441 @end smallexample
4442
4443 @node Symbols
4444 @subsection Symbol Names
4445 @cindex symbol names
4446 @cindex names
4447 @cindex quoted symbol names
4448 @kindex "
4449 Unless quoted, symbol names start with a letter, underscore, or period
4450 and may include letters, digits, underscores, periods, and hyphens.
4451 Unquoted symbol names must not conflict with any keywords. You can
4452 specify a symbol which contains odd characters or has the same name as a
4453 keyword by surrounding the symbol name in double quotes:
4454 @smallexample
4455 "SECTION" = 9;
4456 "with a space" = "also with a space" + 10;
4457 @end smallexample
4458
4459 Since symbols can contain many non-alphabetic characters, it is safest
4460 to delimit symbols with spaces. For example, @samp{A-B} is one symbol,
4461 whereas @samp{A - B} is an expression involving subtraction.
4462
4463 @node Location Counter
4464 @subsection The Location Counter
4465 @kindex .
4466 @cindex dot
4467 @cindex location counter
4468 @cindex current output location
4469 The special linker variable @dfn{dot} @samp{.} always contains the
4470 current output location counter. Since the @code{.} always refers to a
4471 location in an output section, it may only appear in an expression
4472 within a @code{SECTIONS} command. The @code{.} symbol may appear
4473 anywhere that an ordinary symbol is allowed in an expression.
4474
4475 @cindex holes
4476 Assigning a value to @code{.} will cause the location counter to be
4477 moved. This may be used to create holes in the output section. The
4478 location counter may never be moved backwards.
4479
4480 @smallexample
4481 SECTIONS
4482 @{
4483 output :
4484 @{
4485 file1(.text)
4486 . = . + 1000;
4487 file2(.text)
4488 . += 1000;
4489 file3(.text)
4490 @} = 0x12345678;
4491 @}
4492 @end smallexample
4493 @noindent
4494 In the previous example, the @samp{.text} section from @file{file1} is
4495 located at the beginning of the output section @samp{output}. It is
4496 followed by a 1000 byte gap. Then the @samp{.text} section from
4497 @file{file2} appears, also with a 1000 byte gap following before the
4498 @samp{.text} section from @file{file3}. The notation @samp{= 0x12345678}
4499 specifies what data to write in the gaps (@pxref{Output Section Fill}).
4500
4501 @cindex dot inside sections
4502 Note: @code{.} actually refers to the byte offset from the start of the
4503 current containing object. Normally this is the @code{SECTIONS}
4504 statement, whose start address is 0, hence @code{.} can be used as an
4505 absolute address. If @code{.} is used inside a section description
4506 however, it refers to the byte offset from the start of that section,
4507 not an absolute address. Thus in a script like this:
4508
4509 @smallexample
4510 SECTIONS
4511 @{
4512 . = 0x100
4513 .text: @{
4514 *(.text)
4515 . = 0x200
4516 @}
4517 . = 0x500
4518 .data: @{
4519 *(.data)
4520 . += 0x600
4521 @}
4522 @}
4523 @end smallexample
4524
4525 The @samp{.text} section will be assigned a starting address of 0x100
4526 and a size of exactly 0x200 bytes, even if there is not enough data in
4527 the @samp{.text} input sections to fill this area. (If there is too
4528 much data, an error will be produced because this would be an attempt to
4529 move @code{.} backwards). The @samp{.data} section will start at 0x500
4530 and it will have an extra 0x600 bytes worth of space after the end of
4531 the values from the @samp{.data} input sections and before the end of
4532 the @samp{.data} output section itself.
4533
4534 @cindex dot outside sections
4535 Setting symbols to the value of the location counter outside of an
4536 output section statement can result in unexpected values if the linker
4537 needs to place orphan sections. For example, given the following:
4538
4539 @smallexample
4540 SECTIONS
4541 @{
4542 start_of_text = . ;
4543 .text: @{ *(.text) @}
4544 end_of_text = . ;
4545
4546 start_of_data = . ;
4547 .data: @{ *(.data) @}
4548 end_of_data = . ;
4549 @}
4550 @end smallexample
4551
4552 If the linker needs to place some input section, e.g. @code{.rodata},
4553 not mentioned in the script, it might choose to place that section
4554 between @code{.text} and @code{.data}. You might think the linker
4555 should place @code{.rodata} on the blank line in the above script, but
4556 blank lines are of no particular significance to the linker. As well,
4557 the linker doesn't associate the above symbol names with their
4558 sections. Instead, it assumes that all assignments or other
4559 statements belong to the previous output section, except for the
4560 special case of an assignment to @code{.}. I.e., the linker will
4561 place the orphan @code{.rodata} section as if the script was written
4562 as follows:
4563
4564 @smallexample
4565 SECTIONS
4566 @{
4567 start_of_text = . ;
4568 .text: @{ *(.text) @}
4569 end_of_text = . ;
4570
4571 start_of_data = . ;
4572 .rodata: @{ *(.rodata) @}
4573 .data: @{ *(.data) @}
4574 end_of_data = . ;
4575 @}
4576 @end smallexample
4577
4578 This may or may not be the script author's intention for the value of
4579 @code{start_of_data}. One way to influence the orphan section
4580 placement is to assign the location counter to itself, as the linker
4581 assumes that an assignment to @code{.} is setting the start address of
4582 a following output section and thus should be grouped with that
4583 section. So you could write:
4584
4585 @smallexample
4586 SECTIONS
4587 @{
4588 start_of_text = . ;
4589 .text: @{ *(.text) @}
4590 end_of_text = . ;
4591
4592 . = . ;
4593 start_of_data = . ;
4594 .data: @{ *(.data) @}
4595 end_of_data = . ;
4596 @}
4597 @end smallexample
4598
4599 Now, the orphan @code{.rodata} section will be placed between
4600 @code{end_of_text} and @code{start_of_data}.
4601
4602 @need 2000
4603 @node Operators
4604 @subsection Operators
4605 @cindex operators for arithmetic
4606 @cindex arithmetic operators
4607 @cindex precedence in expressions
4608 The linker recognizes the standard C set of arithmetic operators, with
4609 the standard bindings and precedence levels:
4610 @c TEXI2ROFF-KILL
4611 @ifnottex
4612 @c END TEXI2ROFF-KILL
4613 @smallexample
4614 precedence associativity Operators Notes
4615 (highest)
4616 1 left ! - ~ (1)
4617 2 left * / %
4618 3 left + -
4619 4 left >> <<
4620 5 left == != > < <= >=
4621 6 left &
4622 7 left |
4623 8 left &&
4624 9 left ||
4625 10 right ? :
4626 11 right &= += -= *= /= (2)
4627 (lowest)
4628 @end smallexample
4629 Notes:
4630 (1) Prefix operators
4631 (2) @xref{Assignments}.
4632 @c TEXI2ROFF-KILL
4633 @end ifnottex
4634 @tex
4635 \vskip \baselineskip
4636 %"lispnarrowing" is the extra indent used generally for smallexample
4637 \hskip\lispnarrowing\vbox{\offinterlineskip
4638 \hrule
4639 \halign
4640 {\vrule#&\strut\hfil\ #\ \hfil&\vrule#&\strut\hfil\ #\ \hfil&\vrule#&\strut\hfil\ {\tt #}\ \hfil&\vrule#\cr
4641 height2pt&\omit&&\omit&&\omit&\cr
4642 &Precedence&& Associativity &&{\rm Operators}&\cr
4643 height2pt&\omit&&\omit&&\omit&\cr
4644 \noalign{\hrule}
4645 height2pt&\omit&&\omit&&\omit&\cr
4646 &highest&&&&&\cr
4647 % '176 is tilde, '~' in tt font
4648 &1&&left&&\qquad- \char'176\ !\qquad\dag&\cr
4649 &2&&left&&* / \%&\cr
4650 &3&&left&&+ -&\cr
4651 &4&&left&&>> <<&\cr
4652 &5&&left&&== != > < <= >=&\cr
4653 &6&&left&&\&&\cr
4654 &7&&left&&|&\cr
4655 &8&&left&&{\&\&}&\cr
4656 &9&&left&&||&\cr
4657 &10&&right&&? :&\cr
4658 &11&&right&&\qquad\&= += -= *= /=\qquad\ddag&\cr
4659 &lowest&&&&&\cr
4660 height2pt&\omit&&\omit&&\omit&\cr}
4661 \hrule}
4662 @end tex
4663 @iftex
4664 {
4665 @obeylines@parskip=0pt@parindent=0pt
4666 @dag@quad Prefix operators.
4667 @ddag@quad @xref{Assignments}.
4668 }
4669 @end iftex
4670 @c END TEXI2ROFF-KILL
4671
4672 @node Evaluation
4673 @subsection Evaluation
4674 @cindex lazy evaluation
4675 @cindex expression evaluation order
4676 The linker evaluates expressions lazily. It only computes the value of
4677 an expression when absolutely necessary.
4678
4679 The linker needs some information, such as the value of the start
4680 address of the first section, and the origins and lengths of memory
4681 regions, in order to do any linking at all. These values are computed
4682 as soon as possible when the linker reads in the linker script.
4683
4684 However, other values (such as symbol values) are not known or needed
4685 until after storage allocation. Such values are evaluated later, when
4686 other information (such as the sizes of output sections) is available
4687 for use in the symbol assignment expression.
4688
4689 The sizes of sections cannot be known until after allocation, so
4690 assignments dependent upon these are not performed until after
4691 allocation.
4692
4693 Some expressions, such as those depending upon the location counter
4694 @samp{.}, must be evaluated during section allocation.
4695
4696 If the result of an expression is required, but the value is not
4697 available, then an error results. For example, a script like the
4698 following
4699 @smallexample
4700 @group
4701 SECTIONS
4702 @{
4703 .text 9+this_isnt_constant :
4704 @{ *(.text) @}
4705 @}
4706 @end group
4707 @end smallexample
4708 @noindent
4709 will cause the error message @samp{non constant expression for initial
4710 address}.
4711
4712 @node Expression Section
4713 @subsection The Section of an Expression
4714 @cindex expression sections
4715 @cindex absolute expressions
4716 @cindex relative expressions
4717 @cindex absolute and relocatable symbols
4718 @cindex relocatable and absolute symbols
4719 @cindex symbols, relocatable and absolute
4720 When the linker evaluates an expression, the result is either absolute
4721 or relative to some section. A relative expression is expressed as a
4722 fixed offset from the base of a section.
4723
4724 The position of the expression within the linker script determines
4725 whether it is absolute or relative. An expression which appears within
4726 an output section definition is relative to the base of the output
4727 section. An expression which appears elsewhere will be absolute.
4728
4729 A symbol set to a relative expression will be relocatable if you request
4730 relocatable output using the @samp{-r} option. That means that a
4731 further link operation may change the value of the symbol. The symbol's
4732 section will be the section of the relative expression.
4733
4734 A symbol set to an absolute expression will retain the same value
4735 through any further link operation. The symbol will be absolute, and
4736 will not have any particular associated section.
4737
4738 You can use the builtin function @code{ABSOLUTE} to force an expression
4739 to be absolute when it would otherwise be relative. For example, to
4740 create an absolute symbol set to the address of the end of the output
4741 section @samp{.data}:
4742 @smallexample
4743 SECTIONS
4744 @{
4745 .data : @{ *(.data) _edata = ABSOLUTE(.); @}
4746 @}
4747 @end smallexample
4748 @noindent
4749 If @samp{ABSOLUTE} were not used, @samp{_edata} would be relative to the
4750 @samp{.data} section.
4751
4752 @node Builtin Functions
4753 @subsection Builtin Functions
4754 @cindex functions in expressions
4755 The linker script language includes a number of builtin functions for
4756 use in linker script expressions.
4757
4758 @table @code
4759 @item ABSOLUTE(@var{exp})
4760 @kindex ABSOLUTE(@var{exp})
4761 @cindex expression, absolute
4762 Return the absolute (non-relocatable, as opposed to non-negative) value
4763 of the expression @var{exp}. Primarily useful to assign an absolute
4764 value to a symbol within a section definition, where symbol values are
4765 normally section relative. @xref{Expression Section}.
4766
4767 @item ADDR(@var{section})
4768 @kindex ADDR(@var{section})
4769 @cindex section address in expression
4770 Return the absolute address (the VMA) of the named @var{section}. Your
4771 script must previously have defined the location of that section. In
4772 the following example, @code{symbol_1} and @code{symbol_2} are assigned
4773 identical values:
4774 @smallexample
4775 @group
4776 SECTIONS @{ @dots{}
4777 .output1 :
4778 @{
4779 start_of_output_1 = ABSOLUTE(.);
4780 @dots{}
4781 @}
4782 .output :
4783 @{
4784 symbol_1 = ADDR(.output1);
4785 symbol_2 = start_of_output_1;
4786 @}
4787 @dots{} @}
4788 @end group
4789 @end smallexample
4790
4791 @item ALIGN(@var{align})
4792 @itemx ALIGN(@var{exp},@var{align})
4793 @kindex ALIGN(@var{align})
4794 @kindex ALIGN(@var{exp},@var{align})
4795 @cindex round up location counter
4796 @cindex align location counter
4797 @cindex round up expression
4798 @cindex align expression
4799 Return the location counter (@code{.}) or arbitrary expression aligned
4800 to the next @var{align} boundary. The single operand @code{ALIGN}
4801 doesn't change the value of the location counter---it just does
4802 arithmetic on it. The two operand @code{ALIGN} allows an arbitrary
4803 expression to be aligned upwards (@code{ALIGN(@var{align})} is
4804 equivalent to @code{ALIGN(., @var{align})}).
4805
4806 Here is an example which aligns the output @code{.data} section to the
4807 next @code{0x2000} byte boundary after the preceding section and sets a
4808 variable within the section to the next @code{0x8000} boundary after the
4809 input sections:
4810 @smallexample
4811 @group
4812 SECTIONS @{ @dots{}
4813 .data ALIGN(0x2000): @{
4814 *(.data)
4815 variable = ALIGN(0x8000);
4816 @}
4817 @dots{} @}
4818 @end group
4819 @end smallexample
4820 @noindent
4821 The first use of @code{ALIGN} in this example specifies the location of
4822 a section because it is used as the optional @var{address} attribute of
4823 a section definition (@pxref{Output Section Address}). The second use
4824 of @code{ALIGN} is used to defines the value of a symbol.
4825
4826 The builtin function @code{NEXT} is closely related to @code{ALIGN}.
4827
4828 @item BLOCK(@var{exp})
4829 @kindex BLOCK(@var{exp})
4830 This is a synonym for @code{ALIGN}, for compatibility with older linker
4831 scripts. It is most often seen when setting the address of an output
4832 section.
4833
4834 @item DATA_SEGMENT_ALIGN(@var{maxpagesize}, @var{commonpagesize})
4835 @kindex DATA_SEGMENT_ALIGN(@var{maxpagesize}, @var{commonpagesize})
4836 This is equivalent to either
4837 @smallexample
4838 (ALIGN(@var{maxpagesize}) + (. & (@var{maxpagesize} - 1)))
4839 @end smallexample
4840 or
4841 @smallexample
4842 (ALIGN(@var{maxpagesize}) + (. & (@var{maxpagesize} - @var{commonpagesize})))
4843 @end smallexample
4844 @noindent
4845 depending on whether the latter uses fewer @var{commonpagesize} sized pages
4846 for the data segment (area between the result of this expression and
4847 @code{DATA_SEGMENT_END}) than the former or not.
4848 If the latter form is used, it means @var{commonpagesize} bytes of runtime
4849 memory will be saved at the expense of up to @var{commonpagesize} wasted
4850 bytes in the on-disk file.
4851
4852 This expression can only be used directly in @code{SECTIONS} commands, not in
4853 any output section descriptions and only once in the linker script.
4854 @var{commonpagesize} should be less or equal to @var{maxpagesize} and should
4855 be the system page size the object wants to be optimized for (while still
4856 working on system page sizes up to @var{maxpagesize}).
4857
4858 @noindent
4859 Example:
4860 @smallexample
4861 . = DATA_SEGMENT_ALIGN(0x10000, 0x2000);
4862 @end smallexample
4863
4864 @item DATA_SEGMENT_END(@var{exp})
4865 @kindex DATA_SEGMENT_END(@var{exp})
4866 This defines the end of data segment for @code{DATA_SEGMENT_ALIGN}
4867 evaluation purposes.
4868
4869 @smallexample
4870 . = DATA_SEGMENT_END(.);
4871 @end smallexample
4872
4873 @item DATA_SEGMENT_RELRO_END(@var{offset}, @var{exp})
4874 @kindex DATA_SEGMENT_RELRO_END(@var{offset}, @var{exp})
4875 This defines the end of the @code{PT_GNU_RELRO} segment when
4876 @samp{-z relro} option is used. Second argument is returned.
4877 When @samp{-z relro} option is not present, @code{DATA_SEGMENT_RELRO_END}
4878 does nothing, otherwise @code{DATA_SEGMENT_ALIGN} is padded so that
4879 @var{exp} + @var{offset} is aligned to the most commonly used page
4880 boundary for particular target. If present in the linker script,
4881 it must always come in between @code{DATA_SEGMENT_ALIGN} and
4882 @code{DATA_SEGMENT_END}.
4883
4884 @smallexample
4885 . = DATA_SEGMENT_RELRO_END(24, .);
4886 @end smallexample
4887
4888 @item DEFINED(@var{symbol})
4889 @kindex DEFINED(@var{symbol})
4890 @cindex symbol defaults
4891 Return 1 if @var{symbol} is in the linker global symbol table and is
4892 defined before the statement using DEFINED in the script, otherwise
4893 return 0. You can use this function to provide
4894 default values for symbols. For example, the following script fragment
4895 shows how to set a global symbol @samp{begin} to the first location in
4896 the @samp{.text} section---but if a symbol called @samp{begin} already
4897 existed, its value is preserved:
4898
4899 @smallexample
4900 @group
4901 SECTIONS @{ @dots{}
4902 .text : @{
4903 begin = DEFINED(begin) ? begin : . ;
4904 @dots{}
4905 @}
4906 @dots{}
4907 @}
4908 @end group
4909 @end smallexample
4910
4911 @item LENGTH(@var{memory})
4912 @kindex LENGTH(@var{memory})
4913 Return the length of the memory region named @var{memory}.
4914
4915 @item LOADADDR(@var{section})
4916 @kindex LOADADDR(@var{section})
4917 @cindex section load address in expression
4918 Return the absolute LMA of the named @var{section}. This is normally
4919 the same as @code{ADDR}, but it may be different if the @code{AT}
4920 attribute is used in the output section definition (@pxref{Output
4921 Section LMA}).
4922
4923 @kindex MAX
4924 @item MAX(@var{exp1}, @var{exp2})
4925 Returns the maximum of @var{exp1} and @var{exp2}.
4926
4927 @kindex MIN
4928 @item MIN(@var{exp1}, @var{exp2})
4929 Returns the minimum of @var{exp1} and @var{exp2}.
4930
4931 @item NEXT(@var{exp})
4932 @kindex NEXT(@var{exp})
4933 @cindex unallocated address, next
4934 Return the next unallocated address that is a multiple of @var{exp}.
4935 This function is closely related to @code{ALIGN(@var{exp})}; unless you
4936 use the @code{MEMORY} command to define discontinuous memory for the
4937 output file, the two functions are equivalent.
4938
4939 @item ORIGIN(@var{memory})
4940 @kindex ORIGIN(@var{memory})
4941 Return the origin of the memory region named @var{memory}.
4942
4943 @item SEGMENT_START(@var{segment}, @var{default})
4944 @kindex SEGMENT_START(@var{segment}, @var{default})
4945 Return the base address of the named @var{segment}. If an explicit
4946 value has been given for this segment (with a command-line @samp{-T}
4947 option) that value will be returned; otherwise the value will be
4948 @var{default}. At present, the @samp{-T} command-line option can only
4949 be used to set the base address for the ``text'', ``data'', and
4950 ``bss'' sections, but you use @code{SEGMENT_START} with any segment
4951 name.
4952
4953 @item SIZEOF(@var{section})
4954 @kindex SIZEOF(@var{section})
4955 @cindex section size
4956 Return the size in bytes of the named @var{section}, if that section has
4957 been allocated. If the section has not been allocated when this is
4958 evaluated, the linker will report an error. In the following example,
4959 @code{symbol_1} and @code{symbol_2} are assigned identical values:
4960 @smallexample
4961 @group
4962 SECTIONS@{ @dots{}
4963 .output @{
4964 .start = . ;
4965 @dots{}
4966 .end = . ;
4967 @}
4968 symbol_1 = .end - .start ;
4969 symbol_2 = SIZEOF(.output);
4970 @dots{} @}
4971 @end group
4972 @end smallexample
4973
4974 @item SIZEOF_HEADERS
4975 @itemx sizeof_headers
4976 @kindex SIZEOF_HEADERS
4977 @cindex header size
4978 Return the size in bytes of the output file's headers. This is
4979 information which appears at the start of the output file. You can use
4980 this number when setting the start address of the first section, if you
4981 choose, to facilitate paging.
4982
4983 @cindex not enough room for program headers
4984 @cindex program headers, not enough room
4985 When producing an ELF output file, if the linker script uses the
4986 @code{SIZEOF_HEADERS} builtin function, the linker must compute the
4987 number of program headers before it has determined all the section
4988 addresses and sizes. If the linker later discovers that it needs
4989 additional program headers, it will report an error @samp{not enough
4990 room for program headers}. To avoid this error, you must avoid using
4991 the @code{SIZEOF_HEADERS} function, or you must rework your linker
4992 script to avoid forcing the linker to use additional program headers, or
4993 you must define the program headers yourself using the @code{PHDRS}
4994 command (@pxref{PHDRS}).
4995 @end table
4996
4997 @node Implicit Linker Scripts
4998 @section Implicit Linker Scripts
4999 @cindex implicit linker scripts
5000 If you specify a linker input file which the linker can not recognize as
5001 an object file or an archive file, it will try to read the file as a
5002 linker script. If the file can not be parsed as a linker script, the
5003 linker will report an error.
5004
5005 An implicit linker script will not replace the default linker script.
5006
5007 Typically an implicit linker script would contain only symbol
5008 assignments, or the @code{INPUT}, @code{GROUP}, or @code{VERSION}
5009 commands.
5010
5011 Any input files read because of an implicit linker script will be read
5012 at the position in the command line where the implicit linker script was
5013 read. This can affect archive searching.
5014
5015 @ifset GENERIC
5016 @node Machine Dependent
5017 @chapter Machine Dependent Features
5018
5019 @cindex machine dependencies
5020 @command{ld} has additional features on some platforms; the following
5021 sections describe them. Machines where @command{ld} has no additional
5022 functionality are not listed.
5023
5024 @menu
5025 @ifset H8300
5026 * H8/300:: @command{ld} and the H8/300
5027 @end ifset
5028 @ifset I960
5029 * i960:: @command{ld} and the Intel 960 family
5030 @end ifset
5031 @ifset ARM
5032 * ARM:: @command{ld} and the ARM family
5033 @end ifset
5034 @ifset HPPA
5035 * HPPA ELF32:: @command{ld} and HPPA 32-bit ELF
5036 @end ifset
5037 @ifset MMIX
5038 * MMIX:: @command{ld} and MMIX
5039 @end ifset
5040 @ifset MSP430
5041 * MSP430:: @command{ld} and MSP430
5042 @end ifset
5043 @ifset M68HC11
5044 * M68HC11/68HC12:: @code{ld} and the Motorola 68HC11 and 68HC12 families
5045 @end ifset
5046 @ifset TICOFF
5047 * TI COFF:: @command{ld} and TI COFF
5048 @end ifset
5049 @ifset WIN32
5050 * WIN32:: @command{ld} and WIN32 (cygwin/mingw)
5051 @end ifset
5052 @ifset XTENSA
5053 * Xtensa:: @command{ld} and Xtensa Processors
5054 @end ifset
5055 @end menu
5056 @end ifset
5057
5058 @ifset H8300
5059 @ifclear GENERIC
5060 @raisesections
5061 @end ifclear
5062
5063 @node H8/300
5064 @section @command{ld} and the H8/300
5065
5066 @cindex H8/300 support
5067 For the H8/300, @command{ld} can perform these global optimizations when
5068 you specify the @samp{--relax} command-line option.
5069
5070 @table @emph
5071 @cindex relaxing on H8/300
5072 @item relaxing address modes
5073 @command{ld} finds all @code{jsr} and @code{jmp} instructions whose
5074 targets are within eight bits, and turns them into eight-bit
5075 program-counter relative @code{bsr} and @code{bra} instructions,
5076 respectively.
5077
5078 @cindex synthesizing on H8/300
5079 @item synthesizing instructions
5080 @c FIXME: specifically mov.b, or any mov instructions really?
5081 @command{ld} finds all @code{mov.b} instructions which use the
5082 sixteen-bit absolute address form, but refer to the top
5083 page of memory, and changes them to use the eight-bit address form.
5084 (That is: the linker turns @samp{mov.b @code{@@}@var{aa}:16} into
5085 @samp{mov.b @code{@@}@var{aa}:8} whenever the address @var{aa} is in the
5086 top page of memory).
5087
5088 @item bit manipulation instructions
5089 @command{ld} finds all bit manipulation instructions like @code{band, bclr,
5090 biand, bild, bior, bist, bixor, bld, bnot, bor, bset, bst, btst, bxor}
5091 which use 32 bit and 16 bit absolute address form, but refer to the top
5092 page of memory, and changes them to use the 8 bit address form.
5093 (That is: the linker turns @samp{bset #xx:3,@code{@@}@var{aa}:32} into
5094 @samp{bset #xx:3,@code{@@}@var{aa}:8} whenever the address @var{aa} is in
5095 the top page of memory).
5096
5097 @item system control instructions
5098 @command{ld} finds all @code{ldc.w, stc.w} instrcutions which use the
5099 32 bit absolute address form, but refer to the top page of memory, and
5100 changes them to use 16 bit address form.
5101 (That is: the linker turns @samp{ldc.w @code{@@}@var{aa}:32,ccr} into
5102 @samp{ldc.w @code{@@}@var{aa}:16,ccr} whenever the address @var{aa} is in
5103 the top page of memory).
5104 @end table
5105
5106 @ifclear GENERIC
5107 @lowersections
5108 @end ifclear
5109 @end ifset
5110
5111 @ifclear GENERIC
5112 @ifset Renesas
5113 @c This stuff is pointless to say unless you're especially concerned
5114 @c with Renesas chips; don't enable it for generic case, please.
5115 @node Renesas
5116 @chapter @command{ld} and Other Renesas Chips
5117
5118 @command{ld} also supports the Renesas (formerly Hitachi) H8/300H,
5119 H8/500, and SH chips. No special features, commands, or command-line
5120 options are required for these chips.
5121 @end ifset
5122 @end ifclear
5123
5124 @ifset I960
5125 @ifclear GENERIC
5126 @raisesections
5127 @end ifclear
5128
5129 @node i960
5130 @section @command{ld} and the Intel 960 Family
5131
5132 @cindex i960 support
5133
5134 You can use the @samp{-A@var{architecture}} command line option to
5135 specify one of the two-letter names identifying members of the 960
5136 family; the option specifies the desired output target, and warns of any
5137 incompatible instructions in the input files. It also modifies the
5138 linker's search strategy for archive libraries, to support the use of
5139 libraries specific to each particular architecture, by including in the
5140 search loop names suffixed with the string identifying the architecture.
5141
5142 For example, if your @command{ld} command line included @w{@samp{-ACA}} as
5143 well as @w{@samp{-ltry}}, the linker would look (in its built-in search
5144 paths, and in any paths you specify with @samp{-L}) for a library with
5145 the names
5146
5147 @smallexample
5148 @group
5149 try
5150 libtry.a
5151 tryca
5152 libtryca.a
5153 @end group
5154 @end smallexample
5155
5156 @noindent
5157 The first two possibilities would be considered in any event; the last
5158 two are due to the use of @w{@samp{-ACA}}.
5159
5160 You can meaningfully use @samp{-A} more than once on a command line, since
5161 the 960 architecture family allows combination of target architectures; each
5162 use will add another pair of name variants to search for when @w{@samp{-l}}
5163 specifies a library.
5164
5165 @cindex @option{--relax} on i960
5166 @cindex relaxing on i960
5167 @command{ld} supports the @samp{--relax} option for the i960 family. If
5168 you specify @samp{--relax}, @command{ld} finds all @code{balx} and
5169 @code{calx} instructions whose targets are within 24 bits, and turns
5170 them into 24-bit program-counter relative @code{bal} and @code{cal}
5171 instructions, respectively. @command{ld} also turns @code{cal}
5172 instructions into @code{bal} instructions when it determines that the
5173 target subroutine is a leaf routine (that is, the target subroutine does
5174 not itself call any subroutines).
5175
5176 @ifclear GENERIC
5177 @lowersections
5178 @end ifclear
5179 @end ifset
5180
5181 @ifset ARM
5182 @ifclear GENERIC
5183 @raisesections
5184 @end ifclear
5185
5186 @ifset M68HC11
5187 @ifclear GENERIC
5188 @raisesections
5189 @end ifclear
5190
5191 @node M68HC11/68HC12
5192 @section @command{ld} and the Motorola 68HC11 and 68HC12 families
5193
5194 @cindex M68HC11 and 68HC12 support
5195
5196 @subsection Linker Relaxation
5197
5198 For the Motorola 68HC11, @command{ld} can perform these global
5199 optimizations when you specify the @samp{--relax} command-line option.
5200
5201 @table @emph
5202 @cindex relaxing on M68HC11
5203 @item relaxing address modes
5204 @command{ld} finds all @code{jsr} and @code{jmp} instructions whose
5205 targets are within eight bits, and turns them into eight-bit
5206 program-counter relative @code{bsr} and @code{bra} instructions,
5207 respectively.
5208
5209 @command{ld} also looks at all 16-bit extended addressing modes and
5210 transforms them in a direct addressing mode when the address is in
5211 page 0 (between 0 and 0x0ff).
5212
5213 @item relaxing gcc instruction group
5214 When @command{gcc} is called with @option{-mrelax}, it can emit group
5215 of instructions that the linker can optimize to use a 68HC11 direct
5216 addressing mode. These instructions consists of @code{bclr} or
5217 @code{bset} instructions.
5218
5219 @end table
5220
5221 @subsection Trampoline Generation
5222
5223 @cindex trampoline generation on M68HC11
5224 @cindex trampoline generation on M68HC12
5225 For 68HC11 and 68HC12, @command{ld} can generate trampoline code to
5226 call a far function using a normal @code{jsr} instruction. The linker
5227 will also change the relocation to some far function to use the
5228 trampoline address instead of the function address. This is typically the
5229 case when a pointer to a function is taken. The pointer will in fact
5230 point to the function trampoline.
5231
5232 @ifclear GENERIC
5233 @lowersections
5234 @end ifclear
5235 @end ifset
5236
5237 @node ARM
5238 @section @command{ld} and the ARM family
5239
5240 @cindex ARM interworking support
5241 @kindex --support-old-code
5242 For the ARM, @command{ld} will generate code stubs to allow functions calls
5243 betweem ARM and Thumb code. These stubs only work with code that has
5244 been compiled and assembled with the @samp{-mthumb-interwork} command
5245 line option. If it is necessary to link with old ARM object files or
5246 libraries, which have not been compiled with the -mthumb-interwork
5247 option then the @samp{--support-old-code} command line switch should be
5248 given to the linker. This will make it generate larger stub functions
5249 which will work with non-interworking aware ARM code. Note, however,
5250 the linker does not support generating stubs for function calls to
5251 non-interworking aware Thumb code.
5252
5253 @cindex thumb entry point
5254 @cindex entry point, thumb
5255 @kindex --thumb-entry=@var{entry}
5256 The @samp{--thumb-entry} switch is a duplicate of the generic
5257 @samp{--entry} switch, in that it sets the program's starting address.
5258 But it also sets the bottom bit of the address, so that it can be
5259 branched to using a BX instruction, and the program will start
5260 executing in Thumb mode straight away.
5261
5262 @cindex BE8
5263 @kindex --be8
5264 The @samp{--be8} switch instructs @command{ld} to generate BE8 format
5265 executables. This option is only valid when linking big-endian objects.
5266 The resulting image will contain big-endian data and little-endian code.
5267
5268 @cindex TARGET1
5269 @kindex --target1-rel
5270 @kindex --target1-abs
5271 The @samp{R_ARM_TARGET1} relocation is typically used for entries in the
5272 @samp{.init_array} section. It is interpreted as either @samp{R_ARM_REL32}
5273 or @samp{R_ARM_ABS32}, depending on the target. The @samp{--target1-rel}
5274 and @samp{--target1-abs} switches override the default.
5275
5276 @cindex TARGET2
5277 @kindex --target2=@var{type}
5278 The @samp{--target2=type} switch overrides the default definition of the
5279 @samp{R_ARM_TARGET2} relocation. Valid values for @samp{type}, their
5280 meanings, and target defaults are as follows:
5281 @table @samp
5282 @item rel
5283 @samp{R_ARM_REL32} (arm*-*-elf, arm*-*-eabi)
5284 @item abs
5285 @samp{R_ARM_ABS32} (arm*-*-symbianelf)
5286 @item got-rel
5287 @samp{R_ARM_GOT_PREL} (arm*-*-linux, arm*-*-*bsd)
5288 @end table
5289
5290 @cindex FIX_V4BX
5291 @kindex --fix-v4bx
5292 The @samp{R_ARM_V4BX} relocation (defined by the ARM AAELF
5293 specification) enables objects compiled for the ARMv4 architecture to be
5294 interworking-safe when linked with other objects compiled for ARMv4t, but
5295 also allows pure ARMv4 binaries to be built from the same ARMv4 objects.
5296
5297 In the latter case, the switch @option{--fix-v4bx} must be passed to the
5298 linker, which causes v4t @code{BX rM} instructions to be rewritten as
5299 @code{MOV PC,rM}, since v4 processors do not have a @code{BX} instruction.
5300
5301 In the former case, the switch should not be used, and @samp{R_ARM_V4BX}
5302 relocations are ignored.
5303
5304 @cindex USE_BLX
5305 @kindex --use-blx
5306 The @samp{--use-blx} switch enables the linker to use ARM/Thumb
5307 BLX instructions (available on ARMv5t and above) in various
5308 situations. Currently it is used to perform calls via the PLT from Thumb
5309 code using BLX rather than using BX and a mode-switching stub before
5310 each PLT entry. This should lead to such calls executing slightly faster.
5311
5312 This option is enabled implicitly for SymbianOS, so there is no need to
5313 specify it if you are using that target.
5314
5315 @ifclear GENERIC
5316 @lowersections
5317 @end ifclear
5318 @end ifset
5319
5320 @ifset HPPA
5321 @ifclear GENERIC
5322 @raisesections
5323 @end ifclear
5324
5325 @node HPPA ELF32
5326 @section @command{ld} and HPPA 32-bit ELF Support
5327 @cindex HPPA multiple sub-space stubs
5328 @kindex --multi-subspace
5329 When generating a shared library, @command{ld} will by default generate
5330 import stubs suitable for use with a single sub-space application.
5331 The @samp{--multi-subspace} switch causes @command{ld} to generate export
5332 stubs, and different (larger) import stubs suitable for use with
5333 multiple sub-spaces.
5334
5335 @cindex HPPA stub grouping
5336 @kindex --stub-group-size=@var{N}
5337 Long branch stubs and import/export stubs are placed by @command{ld} in
5338 stub sections located between groups of input sections.
5339 @samp{--stub-group-size} specifies the maximum size of a group of input
5340 sections handled by one stub section. Since branch offsets are signed,
5341 a stub section may serve two groups of input sections, one group before
5342 the stub section, and one group after it. However, when using
5343 conditional branches that require stubs, it may be better (for branch
5344 prediction) that stub sections only serve one group of input sections.
5345 A negative value for @samp{N} chooses this scheme, ensuring that
5346 branches to stubs always use a negative offset. Two special values of
5347 @samp{N} are recognized, @samp{1} and @samp{-1}. These both instruct
5348 @command{ld} to automatically size input section groups for the branch types
5349 detected, with the same behaviour regarding stub placement as other
5350 positive or negative values of @samp{N} respectively.
5351
5352 Note that @samp{--stub-group-size} does not split input sections. A
5353 single input section larger than the group size specified will of course
5354 create a larger group (of one section). If input sections are too
5355 large, it may not be possible for a branch to reach its stub.
5356
5357 @ifclear GENERIC
5358 @lowersections
5359 @end ifclear
5360 @end ifset
5361
5362 @ifset MMIX
5363 @ifclear GENERIC
5364 @raisesections
5365 @end ifclear
5366
5367 @node MMIX
5368 @section @code{ld} and MMIX
5369 For MMIX, there is a choice of generating @code{ELF} object files or
5370 @code{mmo} object files when linking. The simulator @code{mmix}
5371 understands the @code{mmo} format. The binutils @code{objcopy} utility
5372 can translate between the two formats.
5373
5374 There is one special section, the @samp{.MMIX.reg_contents} section.
5375 Contents in this section is assumed to correspond to that of global
5376 registers, and symbols referring to it are translated to special symbols,
5377 equal to registers. In a final link, the start address of the
5378 @samp{.MMIX.reg_contents} section corresponds to the first allocated
5379 global register multiplied by 8. Register @code{$255} is not included in
5380 this section; it is always set to the program entry, which is at the
5381 symbol @code{Main} for @code{mmo} files.
5382
5383 Symbols with the prefix @code{__.MMIX.start.}, for example
5384 @code{__.MMIX.start..text} and @code{__.MMIX.start..data} are special;
5385 there must be only one each, even if they are local. The default linker
5386 script uses these to set the default start address of a section.
5387
5388 Initial and trailing multiples of zero-valued 32-bit words in a section,
5389 are left out from an mmo file.
5390
5391 @ifclear GENERIC
5392 @lowersections
5393 @end ifclear
5394 @end ifset
5395
5396 @ifset MSP430
5397 @ifclear GENERIC
5398 @raisesections
5399 @end ifclear
5400
5401 @node MSP430
5402 @section @code{ld} and MSP430
5403 For the MSP430 it is possible to select the MPU architecture. The flag @samp{-m [mpu type]}
5404 will select an appropriate linker script for selected MPU type. (To get a list of known MPUs
5405 just pass @samp{-m help} option to the linker).
5406
5407 @cindex MSP430 extra sections
5408 The linker will recognize some extra sections which are MSP430 specific:
5409
5410 @table @code
5411 @item @samp{.vectors}
5412 Defines a portion of ROM where interrupt vectors located.
5413
5414 @item @samp{.bootloader}
5415 Defines the bootloader portion of the ROM (if applicable). Any code
5416 in this section will be uploaded to the MPU.
5417
5418 @item @samp{.infomem}
5419 Defines an information memory section (if applicable). Any code in
5420 this section will be uploaded to the MPU.
5421
5422 @item @samp{.infomemnobits}
5423 This is the same as the @samp{.infomem} section except that any code
5424 in this section will not be uploaded to the MPU.
5425
5426 @item @samp{.noinit}
5427 Denotes a portion of RAM located above @samp{.bss} section.
5428
5429 The last two sections are used by gcc.
5430 @end table
5431
5432 @ifclear GENERIC
5433 @lowersections
5434 @end ifclear
5435 @end ifset
5436
5437 @ifset TICOFF
5438 @ifclear GENERIC
5439 @raisesections
5440 @end ifclear
5441
5442 @node TI COFF
5443 @section @command{ld}'s Support for Various TI COFF Versions
5444 @cindex TI COFF versions
5445 @kindex --format=@var{version}
5446 The @samp{--format} switch allows selection of one of the various
5447 TI COFF versions. The latest of this writing is 2; versions 0 and 1 are
5448 also supported. The TI COFF versions also vary in header byte-order
5449 format; @command{ld} will read any version or byte order, but the output
5450 header format depends on the default specified by the specific target.
5451
5452 @ifclear GENERIC
5453 @lowersections
5454 @end ifclear
5455 @end ifset
5456
5457 @ifset WIN32
5458 @ifclear GENERIC
5459 @raisesections
5460 @end ifclear
5461
5462 @node WIN32
5463 @section @command{ld} and WIN32 (cygwin/mingw)
5464
5465 This section describes some of the win32 specific @command{ld} issues.
5466 See @ref{Options,,Command Line Options} for detailed decription of the
5467 command line options mentioned here.
5468
5469 @table @emph
5470 @cindex import libraries
5471 @item import libraries
5472 The standard Windows linker creates and uses so-called import
5473 libraries, which contains information for linking to dll's. They are
5474 regular static archives and are handled as any other static
5475 archive. The cygwin and mingw ports of @command{ld} have specific
5476 support for creating such libraries provided with the
5477 @samp{--out-implib} command line option.
5478
5479 @item exporting DLL symbols
5480 @cindex exporting DLL symbols
5481 The cygwin/mingw @command{ld} has several ways to export symbols for dll's.
5482
5483 @table @emph
5484 @item using auto-export functionality
5485 @cindex using auto-export functionality
5486 By default @command{ld} exports symbols with the auto-export functionality,
5487 which is controlled by the following command line options:
5488
5489 @itemize
5490 @item --export-all-symbols [This is the default]
5491 @item --exclude-symbols
5492 @item --exclude-libs
5493 @end itemize
5494
5495 If, however, @samp{--export-all-symbols} is not given explicitly on the
5496 command line, then the default auto-export behavior will be @emph{disabled}
5497 if either of the following are true:
5498
5499 @itemize
5500 @item A DEF file is used.
5501 @item Any symbol in any object file was marked with the __declspec(dllexport) attribute.
5502 @end itemize
5503
5504 @item using a DEF file
5505 @cindex using a DEF file
5506 Another way of exporting symbols is using a DEF file. A DEF file is
5507 an ASCII file containing definitions of symbols which should be
5508 exported when a dll is created. Usually it is named @samp{<dll
5509 name>.def} and is added as any other object file to the linker's
5510 command line. The file's name must end in @samp{.def} or @samp{.DEF}.
5511
5512 @example
5513 gcc -o <output> <objectfiles> <dll name>.def
5514 @end example
5515
5516 Using a DEF file turns off the normal auto-export behavior, unless the
5517 @samp{--export-all-symbols} option is also used.
5518
5519 Here is an example of a DEF file for a shared library called @samp{xyz.dll}:
5520
5521 @example
5522 LIBRARY "xyz.dll" BASE=0x10000000
5523
5524 EXPORTS
5525 foo
5526 bar
5527 _bar = bar
5528 @end example
5529
5530 This example defines a base address and three symbols. The third
5531 symbol is an alias for the second. For the complete format
5532 specification see ld/deffilep.y in the binutils sources.
5533
5534 @cindex creating a DEF file
5535 While linking a shared dll, @command{ld} is able to create a DEF file
5536 with the @samp{--output-def <file>} command line option.
5537
5538 @item Using decorations
5539 @cindex Using decorations
5540 Another way of marking symbols for export is to modify the source code
5541 itself, so that when building the DLL each symbol to be exported is
5542 declared as:
5543
5544 @example
5545 __declspec(dllexport) int a_variable
5546 __declspec(dllexport) void a_function(int with_args)
5547 @end example
5548
5549 All such symbols will be exported from the DLL. If, however,
5550 any of the object files in the DLL contain symbols decorated in
5551 this way, then the normal auto-export behavior is disabled, unless
5552 the @samp{--export-all-symbols} option is also used.
5553
5554 Note that object files that wish to access these symbols must @emph{not}
5555 decorate them with dllexport. Instead, they should use dllimport,
5556 instead:
5557
5558 @example
5559 __declspec(dllimport) int a_variable
5560 __declspec(dllimport) void a_function(int with_args)
5561 @end example
5562
5563 This complicates the structure of library header files, because
5564 when included by the library itself the header must declare the
5565 variables and functions as dllexport, but when included by client
5566 code the header must declare them as dllimport. There are a number
5567 of idioms that are typically used to do this; often client code can
5568 omit the __declspec() declaration completely. See
5569 @samp{--enable-auto-import} and @samp{automatic data imports} for more
5570 imformation.
5571 @end table
5572
5573 @cindex automatic data imports
5574 @item automatic data imports
5575 The standard Windows dll format supports data imports from dlls only
5576 by adding special decorations (dllimport/dllexport), which let the
5577 compiler produce specific assembler instructions to deal with this
5578 issue. This increases the effort necessary to port existing Un*x
5579 code to these platforms, especially for large
5580 c++ libraries and applications. The auto-import feature, which was
5581 initially provided by Paul Sokolovsky, allows one to omit the
5582 decorations to archieve a behavior that conforms to that on POSIX/Un*x
5583 platforms. This feature is enabled with the @samp{--enable-auto-import}
5584 command-line option, although it is enabled by default on cygwin/mingw.
5585 The @samp{--enable-auto-import} option itself now serves mainly to
5586 suppress any warnings that are ordinarily emitted when linked objects
5587 trigger the feature's use.
5588
5589 auto-import of variables does not always work flawlessly without
5590 additional assistance. Sometimes, you will see this message
5591
5592 "variable '<var>' can't be auto-imported. Please read the
5593 documentation for ld's @code{--enable-auto-import} for details."
5594
5595 The @samp{--enable-auto-import} documentation explains why this error
5596 occurs, and several methods that can be used to overcome this difficulty.
5597 One of these methods is the @emph{runtime pseudo-relocs} feature, described
5598 below.
5599
5600 @cindex runtime pseudo-relocation
5601 For complex variables imported from DLLs (such as structs or classes),
5602 object files typically contain a base address for the variable and an
5603 offset (@emph{addend}) within the variable--to specify a particular
5604 field or public member, for instance. Unfortunately, the runtime loader used
5605 in win32 environments is incapable of fixing these references at runtime
5606 without the additional information supplied by dllimport/dllexport decorations.
5607 The standard auto-import feature described above is unable to resolve these
5608 references.
5609
5610 The @samp{--enable-runtime-pseudo-relocs} switch allows these references to
5611 be resolved without error, while leaving the task of adjusting the references
5612 themselves (with their non-zero addends) to specialized code provided by the
5613 runtime environment. Recent versions of the cygwin and mingw environments and
5614 compilers provide this runtime support; older versions do not. However, the
5615 support is only necessary on the developer's platform; the compiled result will
5616 run without error on an older system.
5617
5618 @samp{--enable-runtime-pseudo-relocs} is not the default; it must be explicitly
5619 enabled as needed.
5620
5621 @cindex direct linking to a dll
5622 @item direct linking to a dll
5623 The cygwin/mingw ports of @command{ld} support the direct linking,
5624 including data symbols, to a dll without the usage of any import
5625 libraries. This is much faster and uses much less memory than does the
5626 traditional import library method, expecially when linking large
5627 libraries or applications. When @command{ld} creates an import lib, each
5628 function or variable exported from the dll is stored in its own bfd, even
5629 though a single bfd could contain many exports. The overhead involved in
5630 storing, loading, and processing so many bfd's is quite large, and explains the
5631 tremendous time, memory, and storage needed to link against particularly
5632 large or complex libraries when using import libs.
5633
5634 Linking directly to a dll uses no extra command-line switches other than
5635 @samp{-L} and @samp{-l}, because @command{ld} already searches for a number
5636 of names to match each library. All that is needed from the developer's
5637 perspective is an understanding of this search, in order to force ld to
5638 select the dll instead of an import library.
5639
5640
5641 For instance, when ld is called with the argument @samp{-lxxx} it will attempt
5642 to find, in the first directory of its search path,
5643
5644 @example
5645 libxxx.dll.a
5646 xxx.dll.a
5647 libxxx.a
5648 cygxxx.dll (*)
5649 libxxx.dll
5650 xxx.dll
5651 @end example
5652
5653 before moving on to the next directory in the search path.
5654
5655 (*) Actually, this is not @samp{cygxxx.dll} but in fact is @samp{<prefix>xxx.dll},
5656 where @samp{<prefix>} is set by the @command{ld} option
5657 @samp{--dll-search-prefix=<prefix>}. In the case of cygwin, the standard gcc spec
5658 file includes @samp{--dll-search-prefix=cyg}, so in effect we actually search for
5659 @samp{cygxxx.dll}.
5660
5661 Other win32-based unix environments, such as mingw or pw32, may use other
5662 @samp{<prefix>}es, although at present only cygwin makes use of this feature. It
5663 was originally intended to help avoid name conflicts among dll's built for the
5664 various win32/un*x environments, so that (for example) two versions of a zlib dll
5665 could coexist on the same machine.
5666
5667 The generic cygwin/mingw path layout uses a @samp{bin} directory for
5668 applications and dll's and a @samp{lib} directory for the import
5669 libraries (using cygwin nomenclature):
5670
5671 @example
5672 bin/
5673 cygxxx.dll
5674 lib/
5675 libxxx.dll.a (in case of dll's)
5676 libxxx.a (in case of static archive)
5677 @end example
5678
5679 Linking directly to a dll without using the import library can be
5680 done two ways:
5681
5682 1. Use the dll directly by adding the @samp{bin} path to the link line
5683 @example
5684 gcc -Wl,-verbose -o a.exe -L../bin/ -lxxx
5685 @end example
5686
5687 However, as the dll's often have version numbers appended to their names
5688 (@samp{cygncurses-5.dll}) this will often fail, unless one specifies
5689 @samp{-L../bin -lncurses-5} to include the version. Import libs are generally
5690 not versioned, and do not have this difficulty.
5691
5692 2. Create a symbolic link from the dll to a file in the @samp{lib}
5693 directory according to the above mentioned search pattern. This
5694 should be used to avoid unwanted changes in the tools needed for
5695 making the app/dll.
5696
5697 @example
5698 ln -s bin/cygxxx.dll lib/[cyg|lib|]xxx.dll[.a]
5699 @end example
5700
5701 Then you can link without any make environment changes.
5702
5703 @example
5704 gcc -Wl,-verbose -o a.exe -L../lib/ -lxxx
5705 @end example
5706
5707 This technique also avoids the version number problems, because the following is
5708 perfectly legal
5709
5710 @example
5711 bin/
5712 cygxxx-5.dll
5713 lib/
5714 libxxx.dll.a -> ../bin/cygxxx-5.dll
5715 @end example
5716
5717 Linking directly to a dll without using an import lib will work
5718 even when auto-import features are exercised, and even when
5719 @samp{--enable-runtime-pseudo-relocs} is used.
5720
5721 Given the improvements in speed and memory usage, one might justifiably
5722 wonder why import libraries are used at all. There are two reasons:
5723
5724 1. Until recently, the link-directly-to-dll functionality did @emph{not}
5725 work with auto-imported data.
5726
5727 2. Sometimes it is necessary to include pure static objects within the
5728 import library (which otherwise contains only bfd's for indirection
5729 symbols that point to the exports of a dll). Again, the import lib
5730 for the cygwin kernel makes use of this ability, and it is not
5731 possible to do this without an import lib.
5732
5733 So, import libs are not going away. But the ability to replace
5734 true import libs with a simple symbolic link to (or a copy of)
5735 a dll, in most cases, is a useful addition to the suite of tools
5736 binutils makes available to the win32 developer. Given the
5737 massive improvements in memory requirements during linking, storage
5738 requirements, and linking speed, we expect that many developers
5739 will soon begin to use this feature whenever possible.
5740
5741 @item symbol aliasing
5742 @table @emph
5743 @item adding additional names
5744 Sometimes, it is useful to export symbols with additional names.
5745 A symbol @samp{foo} will be exported as @samp{foo}, but it can also be
5746 exported as @samp{_foo} by using special directives in the DEF file
5747 when creating the dll. This will affect also the optional created
5748 import library. Consider the following DEF file:
5749
5750 @example
5751 LIBRARY "xyz.dll" BASE=0x61000000
5752
5753 EXPORTS
5754 foo
5755 _foo = foo
5756 @end example
5757
5758 The line @samp{_foo = foo} maps the symbol @samp{foo} to @samp{_foo}.
5759
5760 Another method for creating a symbol alias is to create it in the
5761 source code using the "weak" attribute:
5762
5763 @example
5764 void foo () @{ /* Do something. */; @}
5765 void _foo () __attribute__ ((weak, alias ("foo")));
5766 @end example
5767
5768 See the gcc manual for more information about attributes and weak
5769 symbols.
5770
5771 @item renaming symbols
5772 Sometimes it is useful to rename exports. For instance, the cygwin
5773 kernel does this regularly. A symbol @samp{_foo} can be exported as
5774 @samp{foo} but not as @samp{_foo} by using special directives in the
5775 DEF file. (This will also affect the import library, if it is
5776 created). In the following example:
5777
5778 @example
5779 LIBRARY "xyz.dll" BASE=0x61000000
5780
5781 EXPORTS
5782 _foo = foo
5783 @end example
5784
5785 The line @samp{_foo = foo} maps the exported symbol @samp{foo} to
5786 @samp{_foo}.
5787 @end table
5788
5789 Note: using a DEF file disables the default auto-export behavior,
5790 unless the @samp{--export-all-symbols} command line option is used.
5791 If, however, you are trying to rename symbols, then you should list
5792 @emph{all} desired exports in the DEF file, including the symbols
5793 that are not being renamed, and do @emph{not} use the
5794 @samp{--export-all-symbols} option. If you list only the
5795 renamed symbols in the DEF file, and use @samp{--export-all-symbols}
5796 to handle the other symbols, then the both the new names @emph{and}
5797 the original names for the renamed symbols will be exported.
5798 In effect, you'd be aliasing those symbols, not renaming them,
5799 which is probably not what you wanted.
5800
5801 @cindex weak externals
5802 @item weak externals
5803 The Windows object format, PE, specifies a form of weak symbols called
5804 weak externals. When a weak symbol is linked and the symbol is not
5805 defined, the weak symbol becomes an alias for some other symbol. There
5806 are three variants of weak externals:
5807 @itemize
5808 @item Definition is searched for in objects and libraries, historically
5809 called lazy externals.
5810 @item Definition is searched for only in other objects, not in libraries.
5811 This form is not presently implemented.
5812 @item No search; the symbol is an alias. This form is not presently
5813 implemented.
5814 @end itemize
5815 As a GNU extension, weak symbols that do not specify an alternate symbol
5816 are supported. If the symbol is undefined when linking, the symbol
5817 uses a default value.
5818 @end table
5819
5820 @ifclear GENERIC
5821 @lowersections
5822 @end ifclear
5823 @end ifset
5824
5825 @ifset XTENSA
5826 @ifclear GENERIC
5827 @raisesections
5828 @end ifclear
5829
5830 @node Xtensa
5831 @section @code{ld} and Xtensa Processors
5832
5833 @cindex Xtensa processors
5834 The default @command{ld} behavior for Xtensa processors is to interpret
5835 @code{SECTIONS} commands so that lists of explicitly named sections in a
5836 specification with a wildcard file will be interleaved when necessary to
5837 keep literal pools within the range of PC-relative load offsets. For
5838 example, with the command:
5839
5840 @smallexample
5841 SECTIONS
5842 @{
5843 .text : @{
5844 *(.literal .text)
5845 @}
5846 @}
5847 @end smallexample
5848
5849 @noindent
5850 @command{ld} may interleave some of the @code{.literal}
5851 and @code{.text} sections from different object files to ensure that the
5852 literal pools are within the range of PC-relative load offsets. A valid
5853 interleaving might place the @code{.literal} sections from an initial
5854 group of files followed by the @code{.text} sections of that group of
5855 files. Then, the @code{.literal} sections from the rest of the files
5856 and the @code{.text} sections from the rest of the files would follow.
5857
5858 @cindex @option{--relax} on Xtensa
5859 @cindex relaxing on Xtensa
5860 Relaxation is enabled by default for the Xtensa version of @command{ld} and
5861 provides two important link-time optimizations. The first optimization
5862 is to combine identical literal values to reduce code size. A redundant
5863 literal will be removed and all the @code{L32R} instructions that use it
5864 will be changed to reference an identical literal, as long as the
5865 location of the replacement literal is within the offset range of all
5866 the @code{L32R} instructions. The second optimization is to remove
5867 unnecessary overhead from assembler-generated ``longcall'' sequences of
5868 @code{L32R}/@code{CALLX@var{n}} when the target functions are within
5869 range of direct @code{CALL@var{n}} instructions.
5870
5871 For each of these cases where an indirect call sequence can be optimized
5872 to a direct call, the linker will change the @code{CALLX@var{n}}
5873 instruction to a @code{CALL@var{n}} instruction, remove the @code{L32R}
5874 instruction, and remove the literal referenced by the @code{L32R}
5875 instruction if it is not used for anything else. Removing the
5876 @code{L32R} instruction always reduces code size but can potentially
5877 hurt performance by changing the alignment of subsequent branch targets.
5878 By default, the linker will always preserve alignments, either by
5879 switching some instructions between 24-bit encodings and the equivalent
5880 density instructions or by inserting a no-op in place of the @code{L32R}
5881 instruction that was removed. If code size is more important than
5882 performance, the @option{--size-opt} option can be used to prevent the
5883 linker from widening density instructions or inserting no-ops, except in
5884 a few cases where no-ops are required for correctness.
5885
5886 The following Xtensa-specific command-line options can be used to
5887 control the linker:
5888
5889 @cindex Xtensa options
5890 @table @option
5891 @kindex --no-relax
5892 @item --no-relax
5893 Since the Xtensa version of @code{ld} enables the @option{--relax} option
5894 by default, the @option{--no-relax} option is provided to disable
5895 relaxation.
5896
5897 @item --size-opt
5898 When optimizing indirect calls to direct calls, optimize for code size
5899 more than performance. With this option, the linker will not insert
5900 no-ops or widen density instructions to preserve branch target
5901 alignment. There may still be some cases where no-ops are required to
5902 preserve the correctness of the code.
5903 @end table
5904
5905 @ifclear GENERIC
5906 @lowersections
5907 @end ifclear
5908 @end ifset
5909
5910 @ifclear SingleFormat
5911 @node BFD
5912 @chapter BFD
5913
5914 @cindex back end
5915 @cindex object file management
5916 @cindex object formats available
5917 @kindex objdump -i
5918 The linker accesses object and archive files using the BFD libraries.
5919 These libraries allow the linker to use the same routines to operate on
5920 object files whatever the object file format. A different object file
5921 format can be supported simply by creating a new BFD back end and adding
5922 it to the library. To conserve runtime memory, however, the linker and
5923 associated tools are usually configured to support only a subset of the
5924 object file formats available. You can use @code{objdump -i}
5925 (@pxref{objdump,,objdump,binutils.info,The GNU Binary Utilities}) to
5926 list all the formats available for your configuration.
5927
5928 @cindex BFD requirements
5929 @cindex requirements for BFD
5930 As with most implementations, BFD is a compromise between
5931 several conflicting requirements. The major factor influencing
5932 BFD design was efficiency: any time used converting between
5933 formats is time which would not have been spent had BFD not
5934 been involved. This is partly offset by abstraction payback; since
5935 BFD simplifies applications and back ends, more time and care
5936 may be spent optimizing algorithms for a greater speed.
5937
5938 One minor artifact of the BFD solution which you should bear in
5939 mind is the potential for information loss. There are two places where
5940 useful information can be lost using the BFD mechanism: during
5941 conversion and during output. @xref{BFD information loss}.
5942
5943 @menu
5944 * BFD outline:: How it works: an outline of BFD
5945 @end menu
5946
5947 @node BFD outline
5948 @section How It Works: An Outline of BFD
5949 @cindex opening object files
5950 @include bfdsumm.texi
5951 @end ifclear
5952
5953 @node Reporting Bugs
5954 @chapter Reporting Bugs
5955 @cindex bugs in @command{ld}
5956 @cindex reporting bugs in @command{ld}
5957
5958 Your bug reports play an essential role in making @command{ld} reliable.
5959
5960 Reporting a bug may help you by bringing a solution to your problem, or
5961 it may not. But in any case the principal function of a bug report is
5962 to help the entire community by making the next version of @command{ld}
5963 work better. Bug reports are your contribution to the maintenance of
5964 @command{ld}.
5965
5966 In order for a bug report to serve its purpose, you must include the
5967 information that enables us to fix the bug.
5968
5969 @menu
5970 * Bug Criteria:: Have you found a bug?
5971 * Bug Reporting:: How to report bugs
5972 @end menu
5973
5974 @node Bug Criteria
5975 @section Have You Found a Bug?
5976 @cindex bug criteria
5977
5978 If you are not sure whether you have found a bug, here are some guidelines:
5979
5980 @itemize @bullet
5981 @cindex fatal signal
5982 @cindex linker crash
5983 @cindex crash of linker
5984 @item
5985 If the linker gets a fatal signal, for any input whatever, that is a
5986 @command{ld} bug. Reliable linkers never crash.
5987
5988 @cindex error on valid input
5989 @item
5990 If @command{ld} produces an error message for valid input, that is a bug.
5991
5992 @cindex invalid input
5993 @item
5994 If @command{ld} does not produce an error message for invalid input, that
5995 may be a bug. In the general case, the linker can not verify that
5996 object files are correct.
5997
5998 @item
5999 If you are an experienced user of linkers, your suggestions for
6000 improvement of @command{ld} are welcome in any case.
6001 @end itemize
6002
6003 @node Bug Reporting
6004 @section How to Report Bugs
6005 @cindex bug reports
6006 @cindex @command{ld} bugs, reporting
6007
6008 A number of companies and individuals offer support for @sc{gnu}
6009 products. If you obtained @command{ld} from a support organization, we
6010 recommend you contact that organization first.
6011
6012 You can find contact information for many support companies and
6013 individuals in the file @file{etc/SERVICE} in the @sc{gnu} Emacs
6014 distribution.
6015
6016 Otherwise, send bug reports for @command{ld} to
6017 @samp{bug-binutils@@gnu.org}.
6018
6019 The fundamental principle of reporting bugs usefully is this:
6020 @strong{report all the facts}. If you are not sure whether to state a
6021 fact or leave it out, state it!
6022
6023 Often people omit facts because they think they know what causes the
6024 problem and assume that some details do not matter. Thus, you might
6025 assume that the name of a symbol you use in an example does not
6026 matter. Well, probably it does not, but one cannot be sure. Perhaps
6027 the bug is a stray memory reference which happens to fetch from the
6028 location where that name is stored in memory; perhaps, if the name
6029 were different, the contents of that location would fool the linker
6030 into doing the right thing despite the bug. Play it safe and give a
6031 specific, complete example. That is the easiest thing for you to do,
6032 and the most helpful.
6033
6034 Keep in mind that the purpose of a bug report is to enable us to fix
6035 the bug if it is new to us. Therefore, always write your bug reports
6036 on the assumption that the bug has not been reported previously.
6037
6038 Sometimes people give a few sketchy facts and ask, ``Does this ring a
6039 bell?'' This cannot help us fix a bug, so it is basically useless. We
6040 respond by asking for enough details to enable us to investigate.
6041 You might as well expedite matters by sending them to begin with.
6042
6043 To enable us to fix the bug, you should include all these things:
6044
6045 @itemize @bullet
6046 @item
6047 The version of @command{ld}. @command{ld} announces it if you start it with
6048 the @samp{--version} argument.
6049
6050 Without this, we will not know whether there is any point in looking for
6051 the bug in the current version of @command{ld}.
6052
6053 @item
6054 Any patches you may have applied to the @command{ld} source, including any
6055 patches made to the @code{BFD} library.
6056
6057 @item
6058 The type of machine you are using, and the operating system name and
6059 version number.
6060
6061 @item
6062 What compiler (and its version) was used to compile @command{ld}---e.g.
6063 ``@code{gcc-2.7}''.
6064
6065 @item
6066 The command arguments you gave the linker to link your example and
6067 observe the bug. To guarantee you will not omit something important,
6068 list them all. A copy of the Makefile (or the output from make) is
6069 sufficient.
6070
6071 If we were to try to guess the arguments, we would probably guess wrong
6072 and then we might not encounter the bug.
6073
6074 @item
6075 A complete input file, or set of input files, that will reproduce the
6076 bug. It is generally most helpful to send the actual object files
6077 provided that they are reasonably small. Say no more than 10K. For
6078 bigger files you can either make them available by FTP or HTTP or else
6079 state that you are willing to send the object file(s) to whomever
6080 requests them. (Note - your email will be going to a mailing list, so
6081 we do not want to clog it up with large attachments). But small
6082 attachments are best.
6083
6084 If the source files were assembled using @code{gas} or compiled using
6085 @code{gcc}, then it may be OK to send the source files rather than the
6086 object files. In this case, be sure to say exactly what version of
6087 @code{gas} or @code{gcc} was used to produce the object files. Also say
6088 how @code{gas} or @code{gcc} were configured.
6089
6090 @item
6091 A description of what behavior you observe that you believe is
6092 incorrect. For example, ``It gets a fatal signal.''
6093
6094 Of course, if the bug is that @command{ld} gets a fatal signal, then we
6095 will certainly notice it. But if the bug is incorrect output, we might
6096 not notice unless it is glaringly wrong. You might as well not give us
6097 a chance to make a mistake.
6098
6099 Even if the problem you experience is a fatal signal, you should still
6100 say so explicitly. Suppose something strange is going on, such as, your
6101 copy of @command{ld} is out of synch, or you have encountered a bug in the
6102 C library on your system. (This has happened!) Your copy might crash
6103 and ours would not. If you told us to expect a crash, then when ours
6104 fails to crash, we would know that the bug was not happening for us. If
6105 you had not told us to expect a crash, then we would not be able to draw
6106 any conclusion from our observations.
6107
6108 @item
6109 If you wish to suggest changes to the @command{ld} source, send us context
6110 diffs, as generated by @code{diff} with the @samp{-u}, @samp{-c}, or
6111 @samp{-p} option. Always send diffs from the old file to the new file.
6112 If you even discuss something in the @command{ld} source, refer to it by
6113 context, not by line number.
6114
6115 The line numbers in our development sources will not match those in your
6116 sources. Your line numbers would convey no useful information to us.
6117 @end itemize
6118
6119 Here are some things that are not necessary:
6120
6121 @itemize @bullet
6122 @item
6123 A description of the envelope of the bug.
6124
6125 Often people who encounter a bug spend a lot of time investigating
6126 which changes to the input file will make the bug go away and which
6127 changes will not affect it.
6128
6129 This is often time consuming and not very useful, because the way we
6130 will find the bug is by running a single example under the debugger
6131 with breakpoints, not by pure deduction from a series of examples.
6132 We recommend that you save your time for something else.
6133
6134 Of course, if you can find a simpler example to report @emph{instead}
6135 of the original one, that is a convenience for us. Errors in the
6136 output will be easier to spot, running under the debugger will take
6137 less time, and so on.
6138
6139 However, simplification is not vital; if you do not want to do this,
6140 report the bug anyway and send us the entire test case you used.
6141
6142 @item
6143 A patch for the bug.
6144
6145 A patch for the bug does help us if it is a good one. But do not omit
6146 the necessary information, such as the test case, on the assumption that
6147 a patch is all we need. We might see problems with your patch and decide
6148 to fix the problem another way, or we might not understand it at all.
6149
6150 Sometimes with a program as complicated as @command{ld} it is very hard to
6151 construct an example that will make the program follow a certain path
6152 through the code. If you do not send us the example, we will not be
6153 able to construct one, so we will not be able to verify that the bug is
6154 fixed.
6155
6156 And if we cannot understand what bug you are trying to fix, or why your
6157 patch should be an improvement, we will not install it. A test case will
6158 help us to understand.
6159
6160 @item
6161 A guess about what the bug is or what it depends on.
6162
6163 Such guesses are usually wrong. Even we cannot guess right about such
6164 things without first using the debugger to find the facts.
6165 @end itemize
6166
6167 @node MRI
6168 @appendix MRI Compatible Script Files
6169 @cindex MRI compatibility
6170 To aid users making the transition to @sc{gnu} @command{ld} from the MRI
6171 linker, @command{ld} can use MRI compatible linker scripts as an
6172 alternative to the more general-purpose linker scripting language
6173 described in @ref{Scripts}. MRI compatible linker scripts have a much
6174 simpler command set than the scripting language otherwise used with
6175 @command{ld}. @sc{gnu} @command{ld} supports the most commonly used MRI
6176 linker commands; these commands are described here.
6177
6178 In general, MRI scripts aren't of much use with the @code{a.out} object
6179 file format, since it only has three sections and MRI scripts lack some
6180 features to make use of them.
6181
6182 You can specify a file containing an MRI-compatible script using the
6183 @samp{-c} command-line option.
6184
6185 Each command in an MRI-compatible script occupies its own line; each
6186 command line starts with the keyword that identifies the command (though
6187 blank lines are also allowed for punctuation). If a line of an
6188 MRI-compatible script begins with an unrecognized keyword, @command{ld}
6189 issues a warning message, but continues processing the script.
6190
6191 Lines beginning with @samp{*} are comments.
6192
6193 You can write these commands using all upper-case letters, or all
6194 lower case; for example, @samp{chip} is the same as @samp{CHIP}.
6195 The following list shows only the upper-case form of each command.
6196
6197 @table @code
6198 @cindex @code{ABSOLUTE} (MRI)
6199 @item ABSOLUTE @var{secname}
6200 @itemx ABSOLUTE @var{secname}, @var{secname}, @dots{} @var{secname}
6201 Normally, @command{ld} includes in the output file all sections from all
6202 the input files. However, in an MRI-compatible script, you can use the
6203 @code{ABSOLUTE} command to restrict the sections that will be present in
6204 your output program. If the @code{ABSOLUTE} command is used at all in a
6205 script, then only the sections named explicitly in @code{ABSOLUTE}
6206 commands will appear in the linker output. You can still use other
6207 input sections (whatever you select on the command line, or using
6208 @code{LOAD}) to resolve addresses in the output file.
6209
6210 @cindex @code{ALIAS} (MRI)
6211 @item ALIAS @var{out-secname}, @var{in-secname}
6212 Use this command to place the data from input section @var{in-secname}
6213 in a section called @var{out-secname} in the linker output file.
6214
6215 @var{in-secname} may be an integer.
6216
6217 @cindex @code{ALIGN} (MRI)
6218 @item ALIGN @var{secname} = @var{expression}
6219 Align the section called @var{secname} to @var{expression}. The
6220 @var{expression} should be a power of two.
6221
6222 @cindex @code{BASE} (MRI)
6223 @item BASE @var{expression}
6224 Use the value of @var{expression} as the lowest address (other than
6225 absolute addresses) in the output file.
6226
6227 @cindex @code{CHIP} (MRI)
6228 @item CHIP @var{expression}
6229 @itemx CHIP @var{expression}, @var{expression}
6230 This command does nothing; it is accepted only for compatibility.
6231
6232 @cindex @code{END} (MRI)
6233 @item END
6234 This command does nothing whatever; it's only accepted for compatibility.
6235
6236 @cindex @code{FORMAT} (MRI)
6237 @item FORMAT @var{output-format}
6238 Similar to the @code{OUTPUT_FORMAT} command in the more general linker
6239 language, but restricted to one of these output formats:
6240
6241 @enumerate
6242 @item
6243 S-records, if @var{output-format} is @samp{S}
6244
6245 @item
6246 IEEE, if @var{output-format} is @samp{IEEE}
6247
6248 @item
6249 COFF (the @samp{coff-m68k} variant in BFD), if @var{output-format} is
6250 @samp{COFF}
6251 @end enumerate
6252
6253 @cindex @code{LIST} (MRI)
6254 @item LIST @var{anything}@dots{}
6255 Print (to the standard output file) a link map, as produced by the
6256 @command{ld} command-line option @samp{-M}.
6257
6258 The keyword @code{LIST} may be followed by anything on the
6259 same line, with no change in its effect.
6260
6261 @cindex @code{LOAD} (MRI)
6262 @item LOAD @var{filename}
6263 @itemx LOAD @var{filename}, @var{filename}, @dots{} @var{filename}
6264 Include one or more object file @var{filename} in the link; this has the
6265 same effect as specifying @var{filename} directly on the @command{ld}
6266 command line.
6267
6268 @cindex @code{NAME} (MRI)
6269 @item NAME @var{output-name}
6270 @var{output-name} is the name for the program produced by @command{ld}; the
6271 MRI-compatible command @code{NAME} is equivalent to the command-line
6272 option @samp{-o} or the general script language command @code{OUTPUT}.
6273
6274 @cindex @code{ORDER} (MRI)
6275 @item ORDER @var{secname}, @var{secname}, @dots{} @var{secname}
6276 @itemx ORDER @var{secname} @var{secname} @var{secname}
6277 Normally, @command{ld} orders the sections in its output file in the
6278 order in which they first appear in the input files. In an MRI-compatible
6279 script, you can override this ordering with the @code{ORDER} command. The
6280 sections you list with @code{ORDER} will appear first in your output
6281 file, in the order specified.
6282
6283 @cindex @code{PUBLIC} (MRI)
6284 @item PUBLIC @var{name}=@var{expression}
6285 @itemx PUBLIC @var{name},@var{expression}
6286 @itemx PUBLIC @var{name} @var{expression}
6287 Supply a value (@var{expression}) for external symbol
6288 @var{name} used in the linker input files.
6289
6290 @cindex @code{SECT} (MRI)
6291 @item SECT @var{secname}, @var{expression}
6292 @itemx SECT @var{secname}=@var{expression}
6293 @itemx SECT @var{secname} @var{expression}
6294 You can use any of these three forms of the @code{SECT} command to
6295 specify the start address (@var{expression}) for section @var{secname}.
6296 If you have more than one @code{SECT} statement for the same
6297 @var{secname}, only the @emph{first} sets the start address.
6298 @end table
6299
6300 @include fdl.texi
6301
6302 @node Index
6303 @unnumbered Index
6304
6305 @printindex cp
6306
6307 @tex
6308 % I think something like @colophon should be in texinfo. In the
6309 % meantime:
6310 \long\def\colophon{\hbox to0pt{}\vfill
6311 \centerline{The body of this manual is set in}
6312 \centerline{\fontname\tenrm,}
6313 \centerline{with headings in {\bf\fontname\tenbf}}
6314 \centerline{and examples in {\tt\fontname\tentt}.}
6315 \centerline{{\it\fontname\tenit\/} and}
6316 \centerline{{\sl\fontname\tensl\/}}
6317 \centerline{are used for emphasis.}\vfill}
6318 \page\colophon
6319 % Blame: doc@cygnus.com, 28mar91.
6320 @end tex
6321
6322
6323 @contents
6324 @bye
This page took 0.158723 seconds and 4 git commands to generate.