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