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