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