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