19990502 sourceware import
[deliverable/binutils-gdb.git] / ld / ld.texinfo
CommitLineData
252b5132
RH
1\input texinfo
2@setfilename ld.info
3@syncodeindex ky cp
4@include configdoc.texi
5@c (configdoc.texi is generated by the Makefile)
6@include ldver.texi
7
8@c @smallbook
9
10@ifinfo
11@format
12START-INFO-DIR-ENTRY
13* Ld: (ld). The GNU linker.
14END-INFO-DIR-ENTRY
15@end format
16@end ifinfo
17
18@ifinfo
19This file documents the @sc{gnu} linker LD version @value{VERSION}.
20
21Copyright (C) 1991, 92, 93, 94, 95, 96, 97, 98, 1999 Free Software Foundation, Inc.
22
23Permission is granted to make and distribute verbatim copies of
24this manual provided the copyright notice and this permission notice
25are preserved on all copies.
26
27Permission is granted to copy and distribute modified versions of this
28manual under the conditions for verbatim copying, provided also that
29the entire resulting derived work is distributed under the terms of a
30permission notice identical to this one.
31
32Permission is granted to copy and distribute translations of this manual
33into another language, under the above conditions for modified versions.
34
35@ignore
36Permission is granted to process this file through Tex and print the
37results, provided the printed document carries copying permission
38notice identical to this one except for the removal of this paragraph
39(this paragraph not being relevant to the printed manual).
40
41@end ignore
42@end ifinfo
43@iftex
44@finalout
45@setchapternewpage odd
46@settitle Using LD, the GNU linker
47@titlepage
48@title Using ld
49@subtitle The GNU linker
50@sp 1
51@subtitle @code{ld} version 2
52@subtitle Version @value{VERSION}
53@author Steve Chamberlain
54@author Ian Lance Taylor
55@author Cygnus Solutions
56@page
57
58@tex
59{\parskip=0pt
60\hfill Cygnus Solutions\par
61\hfill ian\@cygnus.com, doc\@cygnus.com\par
62\hfill {\it Using LD, the GNU linker}\par
63\hfill Edited by Jeffrey Osier (jeffrey\@cygnus.com)\par
64}
65\global\parindent=0pt % Steve likes it this way.
66@end tex
67
68@vskip 0pt plus 1filll
69Copyright @copyright{} 1991, 92, 93, 94, 95, 96, 97, 98, 1999 Free Software Foundation, Inc.
70
71Permission is granted to make and distribute verbatim copies of
72this manual provided the copyright notice and this permission notice
73are preserved on all copies.
74
75Permission is granted to copy and distribute modified versions of this
76manual under the conditions for verbatim copying, provided also that
77the entire resulting derived work is distributed under the terms of a
78permission notice identical to this one.
79
80Permission is granted to copy and distribute translations of this manual
81into another language, under the above conditions for modified versions.
82@end titlepage
83@end iftex
84@c FIXME: Talk about importance of *order* of args, cmds to linker!
85
86@ifinfo
87@node Top
88@top Using ld
89This file documents the @sc{gnu} linker ld version @value{VERSION}.
90
91@menu
92* Overview:: Overview
93* Invocation:: Invocation
94* Scripts:: Linker Scripts
95@ifset GENERIC
96* Machine Dependent:: Machine Dependent Features
97@end ifset
98@ifclear GENERIC
99@ifset H8300
100* H8/300:: ld and the H8/300
101@end ifset
102@ifset Hitachi
103* Hitachi:: ld and other Hitachi micros
104@end ifset
105@ifset I960
106* i960:: ld and the Intel 960 family
107@end ifset
108@end ifclear
109@ifclear SingleFormat
110* BFD:: BFD
111@end ifclear
112@c Following blank line required for remaining bug in makeinfo conds/menus
113
114* Reporting Bugs:: Reporting Bugs
115* MRI:: MRI Compatible Script Files
116* Index:: Index
117@end menu
118@end ifinfo
119
120@node Overview
121@chapter Overview
122
123@cindex @sc{gnu} linker
124@cindex what is this?
125@code{ld} combines a number of object and archive files, relocates
126their data and ties up symbol references. Usually the last step in
127compiling a program is to run @code{ld}.
128
129@code{ld} accepts Linker Command Language files written in
130a superset of AT&T's Link Editor Command Language syntax,
131to provide explicit and total control over the linking process.
132
133@ifclear SingleFormat
134This version of @code{ld} uses the general purpose BFD libraries
135to operate on object files. This allows @code{ld} to read, combine, and
136write object files in many different formats---for example, COFF or
137@code{a.out}. Different formats may be linked together to produce any
138available kind of object file. @xref{BFD}, for more information.
139@end ifclear
140
141Aside from its flexibility, the @sc{gnu} linker is more helpful than other
142linkers in providing diagnostic information. Many linkers abandon
143execution immediately upon encountering an error; whenever possible,
144@code{ld} continues executing, allowing you to identify other errors
145(or, in some cases, to get an output file in spite of the error).
146
147@node Invocation
148@chapter Invocation
149
150The @sc{gnu} linker @code{ld} is meant to cover a broad range of situations,
151and to be as compatible as possible with other linkers. As a result,
152you have many choices to control its behavior.
153
154@ifset UsesEnvVars
155@menu
156* Options:: Command Line Options
157* Environment:: Environment Variables
158@end menu
159
160@node Options
161@section Command Line Options
162@end ifset
163
164@cindex command line
165@cindex options
166The linker supports a plethora of command-line options, but in actual
167practice few of them are used in any particular context.
168@cindex standard Unix system
169For instance, a frequent use of @code{ld} is to link standard Unix
170object files on a standard, supported Unix system. On such a system, to
171link a file @code{hello.o}:
172
173@smallexample
174ld -o @var{output} /lib/crt0.o hello.o -lc
175@end smallexample
176
177This tells @code{ld} to produce a file called @var{output} as the
178result of linking the file @code{/lib/crt0.o} with @code{hello.o} and
179the library @code{libc.a}, which will come from the standard search
180directories. (See the discussion of the @samp{-l} option below.)
181
182The command-line options to @code{ld} may be specified in any order, and
183may be repeated at will. Repeating most options with a different
184argument will either have no further effect, or override prior
185occurrences (those further to the left on the command line) of that
186option. Options which may be meaningfully specified more than once are
187noted in the descriptions below.
188
189@cindex object files
190Non-option arguments are objects files which are to be linked together.
191They may follow, precede, or be mixed in with command-line options,
192except that an object file argument may not be placed between an option
193and its argument.
194
195Usually the linker is invoked with at least one object file, but you can
196specify other forms of binary input files using @samp{-l}, @samp{-R},
197and the script command language. If @emph{no} binary input files at all
198are specified, the linker does not produce any output, and issues the
199message @samp{No input files}.
200
201If the linker can not recognize the format of an object file, it will
202assume that it is a linker script. A script specified in this way
203augments the main linker script used for the link (either the default
204linker script or the one specified by using @samp{-T}). This feature
205permits the linker to link against a file which appears to be an object
206or an archive, but actually merely defines some symbol values, or uses
207@code{INPUT} or @code{GROUP} to load other objects. Note that
208specifying a script in this way should only be used to augment the main
209linker script; if you want to use some command that logically can only
210appear once, such as the @code{SECTIONS} or @code{MEMORY} command, you
211must replace the default linker script using the @samp{-T} option.
212@xref{Scripts}.
213
214For options whose names are a single letter,
215option arguments must either follow the option letter without intervening
216whitespace, or be given as separate arguments immediately following the
217option that requires them.
218
219For options whose names are multiple letters, either one dash or two can
220precede the option name; for example, @samp{--oformat} and
221@samp{--oformat} are equivalent. Arguments to multiple-letter options
222must either be separated from the option name by an equals sign, or be
223given as separate arguments immediately following the option that
224requires them. For example, @samp{--oformat srec} and
225@samp{--oformat=srec} are equivalent. Unique abbreviations of the names
226of multiple-letter options are accepted.
227
228@table @code
229@kindex -a@var{keyword}
230@item -a@var{keyword}
231This option is supported for HP/UX compatibility. The @var{keyword}
232argument must be one of the strings @samp{archive}, @samp{shared}, or
233@samp{default}. @samp{-aarchive} is functionally equivalent to
234@samp{-Bstatic}, and the other two keywords are functionally equivalent
235to @samp{-Bdynamic}. This option may be used any number of times.
236
237@ifset I960
238@cindex architectures
239@kindex -A@var{arch}
240@item -A@var{architecture}
241@kindex --architecture=@var{arch}
242@itemx --architecture=@var{architecture}
243In the current release of @code{ld}, this option is useful only for the
244Intel 960 family of architectures. In that @code{ld} configuration, the
245@var{architecture} argument identifies the particular architecture in
246the 960 family, enabling some safeguards and modifying the
247archive-library search path. @xref{i960,,@code{ld} and the Intel 960
248family}, for details.
249
250Future releases of @code{ld} may support similar functionality for
251other architecture families.
252@end ifset
253
254@ifclear SingleFormat
255@cindex binary input format
256@kindex -b @var{format}
257@kindex --format=@var{format}
258@cindex input format
259@cindex input format
260@item -b @var{input-format}
261@itemx --format=@var{input-format}
262@code{ld} may be configured to support more than one kind of object
263file. If your @code{ld} is configured this way, you can use the
264@samp{-b} option to specify the binary format for input object files
265that follow this option on the command line. Even when @code{ld} is
266configured to support alternative object formats, you don't usually need
267to specify this, as @code{ld} should be configured to expect as a
268default input format the most usual format on each machine.
269@var{input-format} is a text string, the name of a particular format
270supported by the BFD libraries. (You can list the available binary
271formats with @samp{objdump -i}.)
272@xref{BFD}.
273
274You may want to use this option if you are linking files with an unusual
275binary format. You can also use @samp{-b} to switch formats explicitly (when
276linking object files of different formats), by including
277@samp{-b @var{input-format}} before each group of object files in a
278particular format.
279
280The default format is taken from the environment variable
281@code{GNUTARGET}.
282@ifset UsesEnvVars
283@xref{Environment}.
284@end ifset
285You can also define the input format from a script, using the command
286@code{TARGET}; see @ref{Format Commands}.
287@end ifclear
288
289@kindex -c @var{MRI-cmdfile}
290@kindex --mri-script=@var{MRI-cmdfile}
291@cindex compatibility, MRI
292@item -c @var{MRI-commandfile}
293@itemx --mri-script=@var{MRI-commandfile}
294For compatibility with linkers produced by MRI, @code{ld} accepts script
295files written in an alternate, restricted command language, described in
296@ref{MRI,,MRI Compatible Script Files}. Introduce MRI script files with
297the option @samp{-c}; use the @samp{-T} option to run linker
298scripts written in the general-purpose @code{ld} scripting language.
299If @var{MRI-cmdfile} does not exist, @code{ld} looks for it in the directories
300specified by any @samp{-L} options.
301
302@cindex common allocation
303@kindex -d
304@kindex -dc
305@kindex -dp
306@item -d
307@itemx -dc
308@itemx -dp
309These three options are equivalent; multiple forms are supported for
310compatibility with other linkers. They assign space to common symbols
311even if a relocatable output file is specified (with @samp{-r}). The
312script command @code{FORCE_COMMON_ALLOCATION} has the same effect.
313@xref{Miscellaneous Commands}.
314
315@cindex entry point, from command line
316@kindex -e @var{entry}
317@kindex --entry=@var{entry}
318@item -e @var{entry}
319@itemx --entry=@var{entry}
320Use @var{entry} as the explicit symbol for beginning execution of your
321program, rather than the default entry point. If there is no symbol
322named @var{entry}, the linker will try to parse @var{entry} as a number,
323and use that as the entry address (the number will be interpreted in
324base 10; you may use a leading @samp{0x} for base 16, or a leading
325@samp{0} for base 8). @xref{Entry Point}, for a discussion of defaults
326and other ways of specifying the entry point.
327
328@cindex dynamic symbol table
329@kindex -E
330@kindex --export-dynamic
331@item -E
332@itemx --export-dynamic
333When creating a dynamically linked executable, add all symbols to the
334dynamic symbol table. The dynamic symbol table is the set of symbols
335which are visible from dynamic objects at run time.
336
337If you do not use this option, the dynamic symbol table will normally
338contain only those symbols which are referenced by some dynamic object
339mentioned in the link.
340
341If you use @code{dlopen} to load a dynamic object which needs to refer
342back to the symbols defined by the program, rather than some other
343dynamic object, then you will probably need to use this option when
344linking the program itself.
345
346@cindex big-endian objects
347@cindex endianness
348@kindex -EB
349@item -EB
350Link big-endian objects. This affects the default output format.
351
352@cindex little-endian objects
353@kindex -EL
354@item -EL
355Link little-endian objects. This affects the default output format.
356
357@kindex -f
358@kindex --auxiliary
359@item -f
360@itemx --auxiliary @var{name}
361When creating an ELF shared object, set the internal DT_AUXILIARY field
362to the specified name. This tells the dynamic linker that the symbol
363table of the shared object should be used as an auxiliary filter on the
364symbol table of the shared object @var{name}.
365
366If you later link a program against this filter object, then, when you
367run the program, the dynamic linker will see the DT_AUXILIARY field. If
368the dynamic linker resolves any symbols from the filter object, it will
369first check whether there is a definition in the shared object
370@var{name}. If there is one, it will be used instead of the definition
371in the filter object. The shared object @var{name} need not exist.
372Thus the shared object @var{name} may be used to provide an alternative
373implementation of certain functions, perhaps for debugging or for
374machine specific performance.
375
376This option may be specified more than once. The DT_AUXILIARY entries
377will be created in the order in which they appear on the command line.
378
379@kindex -F
380@kindex --filter
381@item -F @var{name}
382@itemx --filter @var{name}
383When creating an ELF shared object, set the internal DT_FILTER field to
384the specified name. This tells the dynamic linker that the symbol table
385of the shared object which is being created should be used as a filter
386on the symbol table of the shared object @var{name}.
387
388If you later link a program against this filter object, then, when you
389run the program, the dynamic linker will see the DT_FILTER field. The
390dynamic linker will resolve symbols according to the symbol table of the
391filter object as usual, but it will actually link to the definitions
392found in the shared object @var{name}. Thus the filter object can be
393used to select a subset of the symbols provided by the object
394@var{name}.
395
396Some older linkers used the @code{-F} option throughout a compilation
397toolchain for specifying object-file format for both input and output
398object files. The @sc{gnu} linker uses other mechanisms for this
399purpose: the @code{-b}, @code{--format}, @code{--oformat} options, the
400@code{TARGET} command in linker scripts, and the @code{GNUTARGET}
401environment variable. The @sc{gnu} linker will ignore the @code{-F}
402option when not creating an ELF shared object.
403
404@kindex -g
405@item -g
406Ignored. Provided for compatibility with other tools.
407
408@kindex -G
409@kindex --gpsize
410@cindex object size
411@item -G@var{value}
412@itemx --gpsize=@var{value}
413Set the maximum size of objects to be optimized using the GP register to
414@var{size}. This is only meaningful for object file formats such as
415MIPS ECOFF which supports putting large and small objects into different
416sections. This is ignored for other object file formats.
417
418@cindex runtime library name
419@kindex -h@var{name}
420@kindex -soname=@var{name}
421@item -h@var{name}
422@itemx -soname=@var{name}
423When creating an ELF shared object, set the internal DT_SONAME field to
424the specified name. When an executable is linked with a shared object
425which has a DT_SONAME field, then when the executable is run the dynamic
426linker will attempt to load the shared object specified by the DT_SONAME
427field rather than the using the file name given to the linker.
428
429@kindex -i
430@cindex incremental link
431@item -i
432Perform an incremental link (same as option @samp{-r}).
433
434@cindex archive files, from cmd line
435@kindex -l@var{archive}
436@kindex --library=@var{archive}
437@item -l@var{archive}
438@itemx --library=@var{archive}
439Add archive file @var{archive} to the list of files to link. This
440option may be used any number of times. @code{ld} will search its
441path-list for occurrences of @code{lib@var{archive}.a} for every
442@var{archive} specified.
443
444On systems which support shared libraries, @code{ld} may also search for
445libraries with extensions other than @code{.a}. Specifically, on ELF
446and SunOS systems, @code{ld} will search a directory for a library with
447an extension of @code{.so} before searching for one with an extension of
448@code{.a}. By convention, a @code{.so} extension indicates a shared
449library.
450
451The linker will search an archive only once, at the location where it is
452specified on the command line. If the archive defines a symbol which
453was undefined in some object which appeared before the archive on the
454command line, the linker will include the appropriate file(s) from the
455archive. However, an undefined symbol in an object appearing later on
456the command line will not cause the linker to search the archive again.
457
458See the @code{-(} option for a way to force the linker to search
459archives multiple times.
460
461You may list the same archive multiple times on the command line.
462
463@ifset GENERIC
464This type of archive searching is standard for Unix linkers. However,
465if you are using @code{ld} on AIX, note that it is different from the
466behaviour of the AIX linker.
467@end ifset
468
469@cindex search directory, from cmd line
470@kindex -L@var{dir}
471@kindex --library-path=@var{dir}
472@item -L@var{searchdir}
473@itemx --library-path=@var{searchdir}
474Add path @var{searchdir} to the list of paths that @code{ld} will search
475for archive libraries and @code{ld} control scripts. You may use this
476option any number of times. The directories are searched in the order
477in which they are specified on the command line. Directories specified
478on the command line are searched before the default directories. All
479@code{-L} options apply to all @code{-l} options, regardless of the
480order in which the options appear.
481
482@ifset UsesEnvVars
483The default set of paths searched (without being specified with
484@samp{-L}) depends on which emulation mode @code{ld} is using, and in
485some cases also on how it was configured. @xref{Environment}.
486@end ifset
487
488The paths can also be specified in a link script with the
489@code{SEARCH_DIR} command. Directories specified this way are searched
490at the point in which the linker script appears in the command line.
491
492@cindex emulation
493@kindex -m @var{emulation}
494@item -m@var{emulation}
495Emulate the @var{emulation} linker. You can list the available
496emulations with the @samp{--verbose} or @samp{-V} options.
497
498If the @samp{-m} option is not used, the emulation is taken from the
499@code{LDEMULATION} environment variable, if that is defined.
500
501Otherwise, the default emulation depends upon how the linker was
502configured.
503
504@cindex link map
505@kindex -M
506@kindex --print-map
507@item -M
508@itemx --print-map
509Print a link map to the standard output. A link map provides
510information about the link, including the following:
511
512@itemize @bullet
513@item
514Where object files and symbols are mapped into memory.
515@item
516How common symbols are allocated.
517@item
518All archive members included in the link, with a mention of the symbol
519which caused the archive member to be brought in.
520@end itemize
521
522@kindex -n
523@cindex read-only text
524@cindex NMAGIC
525@kindex --nmagic
526@item -n
527@itemx --nmagic
528Set the text segment to be read only, and mark the output as
529@code{NMAGIC} if possible.
530
531@kindex -N
532@kindex --omagic
533@cindex read/write from cmd line
534@cindex OMAGIC
535@item -N
536@itemx --omagic
537Set the text and data sections to be readable and writable. Also, do
538not page-align the data segment. If the output format supports Unix
539style magic numbers, mark the output as @code{OMAGIC}.
540
541@kindex -o @var{output}
542@kindex --output=@var{output}
543@cindex naming the output file
544@item -o @var{output}
545@itemx --output=@var{output}
546Use @var{output} as the name for the program produced by @code{ld}; if this
547option is not specified, the name @file{a.out} is used by default. The
548script command @code{OUTPUT} can also specify the output file name.
549
550@kindex -O @var{level}
551@cindex generating optimized output
552@item -O @var{level}
553If @var{level} is a numeric values greater than zero @code{ld} optimizes
554the output. This might take significantly longer and therefore probably
555should only be enabled for the final binary.
556
557@cindex partial link
558@cindex relocatable output
559@kindex -r
560@kindex --relocateable
561@item -r
562@itemx --relocateable
563Generate relocatable output---i.e., generate an output file that can in
564turn serve as input to @code{ld}. This is often called @dfn{partial
565linking}. As a side effect, in environments that support standard Unix
566magic numbers, this option also sets the output file's magic number to
567@code{OMAGIC}.
568@c ; see @code{-N}.
569If this option is not specified, an absolute file is produced. When
570linking C++ programs, this option @emph{will not} resolve references to
571constructors; to do that, use @samp{-Ur}.
572
573This option does the same thing as @samp{-i}.
574
575@kindex -R @var{file}
576@kindex --just-symbols=@var{file}
577@cindex symbol-only input
578@item -R @var{filename}
579@itemx --just-symbols=@var{filename}
580Read symbol names and their addresses from @var{filename}, but do not
581relocate it or include it in the output. This allows your output file
582to refer symbolically to absolute locations of memory defined in other
583programs. You may use this option more than once.
584
585For compatibility with other ELF linkers, if the @code{-R} option is
586followed by a directory name, rather than a file name, it is treated as
587the @code{-rpath} option.
588
589@kindex -s
590@kindex --strip-all
591@cindex strip all symbols
592@item -s
593@itemx --strip-all
594Omit all symbol information from the output file.
595
596@kindex -S
597@kindex --strip-debug
598@cindex strip debugger symbols
599@item -S
600@itemx --strip-debug
601Omit debugger symbol information (but not all symbols) from the output file.
602
603@kindex -t
604@kindex --trace
605@cindex input files, displaying
606@item -t
607@itemx --trace
608Print the names of the input files as @code{ld} processes them.
609
610@kindex -T @var{script}
611@kindex --script=@var{script}
612@cindex script files
613@item -T @var{scriptfile}
614@itemx --script=@var{scriptfile}
615Use @var{scriptfile} as the linker script. This script replaces
616@code{ld}'s default linker script (rather than adding to it), so
617@var{commandfile} must specify everything necessary to describe the
618output file. You must use this option if you want to use a command
619which can only appear once in a linker script, such as the
620@code{SECTIONS} or @code{MEMORY} command. @xref{Scripts}. If
621@var{scriptfile} does not exist in the current directory, @code{ld}
622looks for it in the directories specified by any preceding @samp{-L}
623options. Multiple @samp{-T} options accumulate.
624
625@kindex -u @var{symbol}
626@kindex --undefined=@var{symbol}
627@cindex undefined symbol
628@item -u @var{symbol}
629@itemx --undefined=@var{symbol}
630Force @var{symbol} to be entered in the output file as an undefined
631symbol. Doing this may, for example, trigger linking of additional
632modules from standard libraries. @samp{-u} may be repeated with
633different option arguments to enter additional undefined symbols. This
634option is equivalent to the @code{EXTERN} linker script command.
635
636@kindex -Ur
637@cindex constructors
638@item -Ur
639For anything other than C++ programs, this option is equivalent to
640@samp{-r}: it generates relocatable output---i.e., an output file that can in
641turn serve as input to @code{ld}. When linking C++ programs, @samp{-Ur}
642@emph{does} resolve references to constructors, unlike @samp{-r}.
643It does not work to use @samp{-Ur} on files that were themselves linked
644with @samp{-Ur}; once the constructor table has been built, it cannot
645be added to. Use @samp{-Ur} only for the last partial link, and
646@samp{-r} for the others.
647
648@kindex -v
649@kindex -V
650@kindex --version
651@cindex version
652@item -v
653@itemx --version
654@itemx -V
655Display the version number for @code{ld}. The @code{-V} option also
656lists the supported emulations.
657
658@kindex -x
659@kindex --discard-all
660@cindex deleting local symbols
661@item -x
662@itemx --discard-all
663Delete all local symbols.
664
665@kindex -X
666@kindex --discard-locals
667@cindex local symbols, deleting
668@cindex L, deleting symbols beginning
669@item -X
670@itemx --discard-locals
671Delete all temporary local symbols. For most targets, this is all local
672symbols whose names begin with @samp{L}.
673
674@kindex -y @var{symbol}
675@kindex --trace-symbol=@var{symbol}
676@cindex symbol tracing
677@item -y @var{symbol}
678@itemx --trace-symbol=@var{symbol}
679Print the name of each linked file in which @var{symbol} appears. This
680option may be given any number of times. On many systems it is necessary
681to prepend an underscore.
682
683This option is useful when you have an undefined symbol in your link but
684don't know where the reference is coming from.
685
686@kindex -Y @var{path}
687@item -Y @var{path}
688Add @var{path} to the default library search path. This option exists
689for Solaris compatibility.
690
691@kindex -z @var{keyword}
692@item -z @var{keyword}
693This option is ignored for Solaris compatibility.
694
695@kindex -(
696@cindex groups of archives
697@item -( @var{archives} -)
698@itemx --start-group @var{archives} --end-group
699The @var{archives} should be a list of archive files. They may be
700either explicit file names, or @samp{-l} options.
701
702The specified archives are searched repeatedly until no new undefined
703references are created. Normally, an archive is searched only once in
704the order that it is specified on the command line. If a symbol in that
705archive is needed to resolve an undefined symbol referred to by an
706object in an archive that appears later on the command line, the linker
707would not be able to resolve that reference. By grouping the archives,
708they all be searched repeatedly until all possible references are
709resolved.
710
711Using this option has a significant performance cost. It is best to use
712it only when there are unavoidable circular references between two or
713more archives.
714
715@kindex -assert @var{keyword}
716@item -assert @var{keyword}
717This option is ignored for SunOS compatibility.
718
719@kindex -Bdynamic
720@kindex -dy
721@kindex -call_shared
722@item -Bdynamic
723@itemx -dy
724@itemx -call_shared
725Link against dynamic libraries. This is only meaningful on platforms
726for which shared libraries are supported. This option is normally the
727default on such platforms. The different variants of this option are
728for compatibility with various systems. You may use this option
729multiple times on the command line: it affects library searching for
730@code{-l} options which follow it.
731
732@kindex -Bstatic
733@kindex -dn
734@kindex -non_shared
735@kindex -static
736@item -Bstatic
737@itemx -dn
738@itemx -non_shared
739@itemx -static
740Do not link against shared libraries. This is only meaningful on
741platforms for which shared libraries are supported. The different
742variants of this option are for compatibility with various systems. You
743may use this option multiple times on the command line: it affects
744library searching for @code{-l} options which follow it.
745
746@kindex -Bsymbolic
747@item -Bsymbolic
748When creating a shared library, bind references to global symbols to the
749definition within the shared library, if any. Normally, it is possible
750for a program linked against a shared library to override the definition
751within the shared library. This option is only meaningful on ELF
752platforms which support shared libraries.
753
754@kindex --check-sections
755@kindex --no-check-sections
756@item --check-sections
757@item --no-check-sections
758Asks the linker @emph{not} to check section addresses after they have
759been assigned to see if there any overlaps. Normally the linker will
760perform this check, and if it finds any overlaps it will produce
761suitable error messages. The linker does know about, and does make
762allowances for sections in overlays. The default behaviour can be
763restored by using the command line switch @samp{--check-sections}.
764
765@cindex cross reference table
766@kindex --cref
767@item --cref
768Output a cross reference table. If a linker map file is being
769generated, the cross reference table is printed to the map file.
770Otherwise, it is printed on the standard output.
771
772The format of the table is intentionally simple, so that it may be
773easily processed by a script if necessary. The symbols are printed out,
774sorted by name. For each symbol, a list of file names is given. If the
775symbol is defined, the first file listed is the location of the
776definition. The remaining files contain references to the symbol.
777
778@cindex symbols, from command line
779@kindex --defsym @var{symbol}=@var{exp}
780@item --defsym @var{symbol}=@var{expression}
781Create a global symbol in the output file, containing the absolute
782address given by @var{expression}. You may use this option as many
783times as necessary to define multiple symbols in the command line. A
784limited form of arithmetic is supported for the @var{expression} in this
785context: you may give a hexadecimal constant or the name of an existing
786symbol, or use @code{+} and @code{-} to add or subtract hexadecimal
787constants or symbols. If you need more elaborate expressions, consider
788using the linker command language from a script (@pxref{Assignments,,
789Assignment: Symbol Definitions}). @emph{Note:} there should be no white
790space between @var{symbol}, the equals sign (``@key{=}''), and
791@var{expression}.
792
793@cindex demangling, from command line
794@kindex --demangle
795@kindex --no-demangle
796@item --demangle
797@itemx --no-demangle
798These options control whether to demangle symbol names in error messages
799and other output. When the linker is told to demangle, it tries to
800present symbol names in a readable fashion: it strips leading
801underscores if they are used by the object file format, and converts C++
802mangled symbol names into user readable names. The linker will demangle
803by default unless the environment variable @samp{COLLECT_NO_DEMANGLE} is
804set. These options may be used to override the default.
805
806@cindex dynamic linker, from command line
807@kindex --dynamic-linker @var{file}
808@item --dynamic-linker @var{file}
809Set the name of the dynamic linker. This is only meaningful when
810generating dynamically linked ELF executables. The default dynamic
811linker is normally correct; don't use this unless you know what you are
812doing.
813
814@cindex MIPS embedded PIC code
815@kindex --embedded-relocs
816@item --embedded-relocs
817This option is only meaningful when linking MIPS embedded PIC code,
818generated by the -membedded-pic option to the @sc{gnu} compiler and
819assembler. It causes the linker to create a table which may be used at
820runtime to relocate any data which was statically initialized to pointer
821values. See the code in testsuite/ld-empic for details.
822
823@kindex --force-exe-suffix
824@item --force-exe-suffix
825Make sure that an output file has a .exe suffix.
826
827If a successfully built fully linked output file does not have a
828@code{.exe} or @code{.dll} suffix, this option forces the linker to copy
829the output file to one of the same name with a @code{.exe} suffix. This
830option is useful when using unmodified Unix makefiles on a Microsoft
831Windows host, since some versions of Windows won't run an image unless
832it ends in a @code{.exe} suffix.
833
834@kindex --gc-sections
835@kindex --no-gc-sections
836@cindex garbage collection
837@item --no-gc-sections
838@itemx --gc-sections
839Enable garbage collection of unused input sections. It is ignored on
840targets that do not support this option. This option is not compatible
841with @samp{-r}, nor should it be used with dynamic linking. The default
842behaviour (of not performing this garbage collection) can be restored by
843specifying @samp{--no-gc-sections} on the command line.
844
845@cindex help
846@cindex usage
847@kindex --help
848@item --help
849Print a summary of the command-line options on the standard output and exit.
850
851@kindex -Map
852@item -Map @var{mapfile}
853Print a link map to the file @var{mapfile}. See the description of the
854@samp{-M} option, above.
855
856@cindex memory usage
857@kindex --no-keep-memory
858@item --no-keep-memory
859@code{ld} normally optimizes for speed over memory usage by caching the
860symbol tables of input files in memory. This option tells @code{ld} to
861instead optimize for memory usage, by rereading the symbol tables as
862necessary. This may be required if @code{ld} runs out of memory space
863while linking a large executable.
864
865@kindex --no-undefined
866@item --no-undefined
867Normally when creating a non-symbolic shared library, undefined symbols
868are allowed and left to be resolved by the runtime loader. This option
869disallows such undefined symbols.
870
871@kindex --no-warn-mismatch
872@item --no-warn-mismatch
873Normally @code{ld} will give an error if you try to link together input
874files that are mismatched for some reason, perhaps because they have
875been compiled for different processors or for different endiannesses.
876This option tells @code{ld} that it should silently permit such possible
877errors. This option should only be used with care, in cases when you
878have taken some special action that ensures that the linker errors are
879inappropriate.
880
881@kindex --no-whole-archive
882@item --no-whole-archive
883Turn off the effect of the @code{--whole-archive} option for subsequent
884archive files.
885
886@cindex output file after errors
887@kindex --noinhibit-exec
888@item --noinhibit-exec
889Retain the executable output file whenever it is still usable.
890Normally, the linker will not produce an output file if it encounters
891errors during the link process; it exits without writing an output file
892when it issues any error whatsoever.
893
894@ifclear SingleFormat
895@kindex --oformat
896@item --oformat @var{output-format}
897@code{ld} may be configured to support more than one kind of object
898file. If your @code{ld} is configured this way, you can use the
899@samp{--oformat} option to specify the binary format for the output
900object file. Even when @code{ld} is configured to support alternative
901object formats, you don't usually need to specify this, as @code{ld}
902should be configured to produce as a default output format the most
903usual format on each machine. @var{output-format} is a text string, the
904name of a particular format supported by the BFD libraries. (You can
905list the available binary formats with @samp{objdump -i}.) The script
906command @code{OUTPUT_FORMAT} can also specify the output format, but
907this option overrides it. @xref{BFD}.
908@end ifclear
909
910@kindex -qmagic
911@item -qmagic
912This option is ignored for Linux compatibility.
913
914@kindex -Qy
915@item -Qy
916This option is ignored for SVR4 compatibility.
917
918@kindex --relax
919@cindex synthesizing linker
920@cindex relaxing addressing modes
921@item --relax
922An option with machine dependent effects.
923@ifset GENERIC
924This option is only supported on a few targets.
925@end ifset
926@ifset H8300
927@xref{H8/300,,@code{ld} and the H8/300}.
928@end ifset
929@ifset I960
930@xref{i960,, @code{ld} and the Intel 960 family}.
931@end ifset
932
933
934On some platforms, the @samp{--relax} option performs global
935optimizations that become possible when the linker resolves addressing
936in the program, such as relaxing address modes and synthesizing new
937instructions in the output object file.
938
939On some platforms these link time global optimizations may make symbolic
940debugging of the resulting executable impossible.
941@ifset GENERIC
942This is known to be
943the case for the Matsushita MN10200 and MN10300 family of processors.
944@end ifset
945
946@ifset GENERIC
947On platforms where this is not supported, @samp{--relax} is accepted,
948but ignored.
949@end ifset
950
951@cindex retaining specified symbols
952@cindex stripping all but some symbols
953@cindex symbols, retaining selectively
954@item --retain-symbols-file @var{filename}
955Retain @emph{only} the symbols listed in the file @var{filename},
956discarding all others. @var{filename} is simply a flat file, with one
957symbol name per line. This option is especially useful in environments
958@ifset GENERIC
959(such as VxWorks)
960@end ifset
961where a large global symbol table is accumulated gradually, to conserve
962run-time memory.
963
964@samp{--retain-symbols-file} does @emph{not} discard undefined symbols,
965or symbols needed for relocations.
966
967You may only specify @samp{--retain-symbols-file} once in the command
968line. It overrides @samp{-s} and @samp{-S}.
969
970@ifset GENERIC
971@item -rpath @var{dir}
972@cindex runtime library search path
973@kindex -rpath
974Add a directory to the runtime library search path. This is used when
975linking an ELF executable with shared objects. All @code{-rpath}
976arguments are concatenated and passed to the runtime linker, which uses
977them to locate shared objects at runtime. The @code{-rpath} option is
978also used when locating shared objects which are needed by shared
979objects explicitly included in the link; see the description of the
980@code{-rpath-link} option. If @code{-rpath} is not used when linking an
981ELF executable, the contents of the environment variable
982@code{LD_RUN_PATH} will be used if it is defined.
983
984The @code{-rpath} option may also be used on SunOS. By default, on
985SunOS, the linker will form a runtime search patch out of all the
986@code{-L} options it is given. If a @code{-rpath} option is used, the
987runtime search path will be formed exclusively using the @code{-rpath}
988options, ignoring the @code{-L} options. This can be useful when using
989gcc, which adds many @code{-L} options which may be on NFS mounted
990filesystems.
991
992For compatibility with other ELF linkers, if the @code{-R} option is
993followed by a directory name, rather than a file name, it is treated as
994the @code{-rpath} option.
995@end ifset
996
997@ifset GENERIC
998@cindex link-time runtime library search path
999@kindex -rpath-link
1000@item -rpath-link @var{DIR}
1001When using ELF or SunOS, one shared library may require another. This
1002happens when an @code{ld -shared} link includes a shared library as one
1003of the input files.
1004
1005When the linker encounters such a dependency when doing a non-shared,
1006non-relocatable link, it will automatically try to locate the required
1007shared library and include it in the link, if it is not included
1008explicitly. In such a case, the @code{-rpath-link} option
1009specifies the first set of directories to search. The
1010@code{-rpath-link} option may specify a sequence of directory names
1011either by specifying a list of names separated by colons, or by
1012appearing multiple times.
1013
1014The linker uses the following search paths to locate required shared
1015libraries.
1016@enumerate
1017@item
1018Any directories specified by @code{-rpath-link} options.
1019@item
1020Any directories specified by @code{-rpath} options. The difference
1021between @code{-rpath} and @code{-rpath-link} is that directories
1022specified by @code{-rpath} options are included in the executable and
1023used at runtime, whereas the @code{-rpath-link} option is only effective
1024at link time.
1025@item
1026On an ELF system, if the @code{-rpath} and @code{rpath-link} options
1027were not used, search the contents of the environment variable
1028@code{LD_RUN_PATH}.
1029@item
1030On SunOS, if the @code{-rpath} option was not used, search any
1031directories specified using @code{-L} options.
1032@item
1033For a native linker, the contents of the environment variable
1034@code{LD_LIBRARY_PATH}.
1035@item
1036The default directories, normally @file{/lib} and @file{/usr/lib}.
1037@item
1038For a native linker on an ELF system, if the file @file{/etc/ld.so.conf}
1039exists, the list of directories found in that file.
1040@end enumerate
1041
1042If the required shared library is not found, the linker will issue a
1043warning and continue with the link.
1044@end ifset
1045
1046@kindex -shared
1047@kindex -Bshareable
1048@item -shared
1049@itemx -Bshareable
1050@cindex shared libraries
1051Create a shared library. This is currently only supported on ELF, XCOFF
1052and SunOS platforms. On SunOS, the linker will automatically create a
1053shared library if the @code{-e} option is not used and there are
1054undefined symbols in the link.
1055
1056@item --sort-common
1057@kindex --sort-common
1058This option tells @code{ld} to sort the common symbols by size when it
1059places them in the appropriate output sections. First come all the one
1060byte symbols, then all the two bytes, then all the four bytes, and then
1061everything else. This is to prevent gaps between symbols due to
1062alignment constraints.
1063
1064@kindex --split-by-file
1065@item --split-by-file
1066Similar to @code{--split-by-reloc} but creates a new output section for
1067each input file.
1068
1069@kindex --split-by-reloc
1070@item --split-by-reloc @var{count}
1071Trys to creates extra sections in the output file so that no single
1072output section in the file contains more than @var{count} relocations.
1073This is useful when generating huge relocatable for downloading into
1074certain real time kernels with the COFF object file format; since COFF
1075cannot represent more than 65535 relocations in a single section. Note
1076that this will fail to work with object file formats which do not
1077support arbitrary sections. The linker will not split up individual
1078input sections for redistribution, so if a single input section contains
1079more than @var{count} relocations one output section will contain that
1080many relocations.
1081
1082@kindex --stats
1083@item --stats
1084Compute and display statistics about the operation of the linker, such
1085as execution time and memory usage.
1086
1087@kindex --traditional-format
1088@cindex traditional format
1089@item --traditional-format
1090For some targets, the output of @code{ld} is different in some ways from
1091the output of some existing linker. This switch requests @code{ld} to
1092use the traditional format instead.
1093
1094@cindex dbx
1095For example, on SunOS, @code{ld} combines duplicate entries in the
1096symbol string table. This can reduce the size of an output file with
1097full debugging information by over 30 percent. Unfortunately, the SunOS
1098@code{dbx} program can not read the resulting program (@code{gdb} has no
1099trouble). The @samp{--traditional-format} switch tells @code{ld} to not
1100combine duplicate entries.
1101
1102@kindex -Tbss @var{org}
1103@kindex -Tdata @var{org}
1104@kindex -Ttext @var{org}
1105@cindex segment origins, cmd line
1106@item -Tbss @var{org}
1107@itemx -Tdata @var{org}
1108@itemx -Ttext @var{org}
1109Use @var{org} as the starting address for---respectively---the
1110@code{bss}, @code{data}, or the @code{text} segment of the output file.
1111@var{org} must be a single hexadecimal integer;
1112for compatibility with other linkers, you may omit the leading
1113@samp{0x} usually associated with hexadecimal values.
1114
1115@kindex --verbose
1116@cindex verbose
1117@item --dll-verbose
1118@item --verbose
1119Display the version number for @code{ld} and list the linker emulations
1120supported. Display which input files can and cannot be opened. Display
1121the linker script if using a default builtin script.
1122
1123@kindex --version-script=@var{version-scriptfile}
1124@cindex version script, symbol versions
1125@itemx --version-script=@var{version-scriptfile}
1126Specify the name of a version script to the linker. This is typically
1127used when creating shared libraries to specify additional information
1128about the version heirarchy for the library being created. This option
1129is only meaningful on ELF platforms which support shared libraries.
1130@xref{VERSION}.
1131
1132@kindex --warn-comon
1133@cindex warnings, on combining symbols
1134@cindex combining symbols, warnings on
1135@item --warn-common
1136Warn when a common symbol is combined with another common symbol or with
1137a symbol definition. Unix linkers allow this somewhat sloppy practice,
1138but linkers on some other operating systems do not. This option allows
1139you to find potential problems from combining global symbols.
1140Unfortunately, some C libraries use this practice, so you may get some
1141warnings about symbols in the libraries as well as in your programs.
1142
1143There are three kinds of global symbols, illustrated here by C examples:
1144
1145@table @samp
1146@item int i = 1;
1147A definition, which goes in the initialized data section of the output
1148file.
1149
1150@item extern int i;
1151An undefined reference, which does not allocate space.
1152There must be either a definition or a common symbol for the
1153variable somewhere.
1154
1155@item int i;
1156A common symbol. If there are only (one or more) common symbols for a
1157variable, it goes in the uninitialized data area of the output file.
1158The linker merges multiple common symbols for the same variable into a
1159single symbol. If they are of different sizes, it picks the largest
1160size. The linker turns a common symbol into a declaration, if there is
1161a definition of the same variable.
1162@end table
1163
1164The @samp{--warn-common} option can produce five kinds of warnings.
1165Each warning consists of a pair of lines: the first describes the symbol
1166just encountered, and the second describes the previous symbol
1167encountered with the same name. One or both of the two symbols will be
1168a common symbol.
1169
1170@enumerate
1171@item
1172Turning a common symbol into a reference, because there is already a
1173definition for the symbol.
1174@smallexample
1175@var{file}(@var{section}): warning: common of `@var{symbol}'
1176 overridden by definition
1177@var{file}(@var{section}): warning: defined here
1178@end smallexample
1179
1180@item
1181Turning a common symbol into a reference, because a later definition for
1182the symbol is encountered. This is the same as the previous case,
1183except that the symbols are encountered in a different order.
1184@smallexample
1185@var{file}(@var{section}): warning: definition of `@var{symbol}'
1186 overriding common
1187@var{file}(@var{section}): warning: common is here
1188@end smallexample
1189
1190@item
1191Merging a common symbol with a previous same-sized common symbol.
1192@smallexample
1193@var{file}(@var{section}): warning: multiple common
1194 of `@var{symbol}'
1195@var{file}(@var{section}): warning: previous common is here
1196@end smallexample
1197
1198@item
1199Merging a common symbol with a previous larger common symbol.
1200@smallexample
1201@var{file}(@var{section}): warning: common of `@var{symbol}'
1202 overridden by larger common
1203@var{file}(@var{section}): warning: larger common is here
1204@end smallexample
1205
1206@item
1207Merging a common symbol with a previous smaller common symbol. This is
1208the same as the previous case, except that the symbols are
1209encountered in a different order.
1210@smallexample
1211@var{file}(@var{section}): warning: common of `@var{symbol}'
1212 overriding smaller common
1213@var{file}(@var{section}): warning: smaller common is here
1214@end smallexample
1215@end enumerate
1216
1217@kindex --warn-constructors
1218@item --warn-constructors
1219Warn if any global constructors are used. This is only useful for a few
1220object file formats. For formats like COFF or ELF, the linker can not
1221detect the use of global constructors.
1222
1223@kindex --warn-multiple-gp
1224@item --warn-multiple-gp
1225Warn if multiple global pointer values are required in the output file.
1226This is only meaningful for certain processors, such as the Alpha.
1227Specifically, some processors put large-valued constants in a special
1228section. A special register (the global pointer) points into the middle
1229of this section, so that constants can be loaded efficiently via a
1230base-register relative addressing mode. Since the offset in
1231base-register relative mode is fixed and relatively small (e.g., 16
1232bits), this limits the maximum size of the constant pool. Thus, in
1233large programs, it is often necessary to use multiple global pointer
1234values in order to be able to address all possible constants. This
1235option causes a warning to be issued whenever this case occurs.
1236
1237@kindex --warn-once
1238@cindex warnings, on undefined symbols
1239@cindex undefined symbols, warnings on
1240@item --warn-once
1241Only warn once for each undefined symbol, rather than once per module
1242which refers to it.
1243
1244@kindex --warn-section-align
1245@cindex warnings, on section alignment
1246@cindex section alignment, warnings on
1247@item --warn-section-align
1248Warn if the address of an output section is changed because of
1249alignment. Typically, the alignment will be set by an input section.
1250The address will only be changed if it not explicitly specified; that
1251is, if the @code{SECTIONS} command does not specify a start address for
1252the section (@pxref{SECTIONS}).
1253
1254@kindex --whole-archive
1255@cindex including an entire archive
1256@item --whole-archive
1257For each archive mentioned on the command line after the
1258@code{--whole-archive} option, include every object file in the archive
1259in the link, rather than searching the archive for the required object
1260files. This is normally used to turn an archive file into a shared
1261library, forcing every object to be included in the resulting shared
1262library. This option may be used more than once.
1263
1264@kindex --wrap
1265@item --wrap @var{symbol}
1266Use a wrapper function for @var{symbol}. Any undefined reference to
1267@var{symbol} will be resolved to @code{__wrap_@var{symbol}}. Any
1268undefined reference to @code{__real_@var{symbol}} will be resolved to
1269@var{symbol}.
1270
1271This can be used to provide a wrapper for a system function. The
1272wrapper function should be called @code{__wrap_@var{symbol}}. If it
1273wishes to call the system function, it should call
1274@code{__real_@var{symbol}}.
1275
1276Here is a trivial example:
1277
1278@smallexample
1279void *
1280__wrap_malloc (int c)
1281@{
1282 printf ("malloc called with %ld\n", c);
1283 return __real_malloc (c);
1284@}
1285@end smallexample
1286
1287If you link other code with this file using @code{--wrap malloc}, then
1288all calls to @code{malloc} will call the function @code{__wrap_malloc}
1289instead. The call to @code{__real_malloc} in @code{__wrap_malloc} will
1290call the real @code{malloc} function.
1291
1292You may wish to provide a @code{__real_malloc} function as well, so that
1293links without the @code{--wrap} option will succeed. If you do this,
1294you should not put the definition of @code{__real_malloc} in the same
1295file as @code{__wrap_malloc}; if you do, the assembler may resolve the
1296call before the linker has a chance to wrap it to @code{malloc}.
1297
1298@end table
1299
1300@subsection Options specific to i386 PE targets
1301
1302The i386 PE linker supports the @code{-shared} option, which causes
1303the output to be a dynamically linked library (DLL) instead of a
1304normal executable. You should name the output @code{*.dll} when you
1305use this option. In addition, the linker fully supports the standard
1306@code{*.def} files, which may be specified on the linker command line
1307like an object file (in fact, it should precede archives it exports
1308symbols from, to ensure that they get linked in, just like a normal
1309object file).
1310
1311In addition to the options common to all targets, the i386 PE linker
1312support additional command line options that are specific to the i386
1313PE target. Options that take values may be separated from their
1314values by either a space or an equals sign.
1315
1316@table @code
1317
1318@kindex --add-stdcall-alias
1319@item --add-stdcall-alias
1320If given, symbols with a stdcall suffix (@@@var{nn}) will be exported
1321as-is and also with the suffix stripped.
1322
1323@kindex --base-file
1324@item --base-file @var{file}
1325Use @var{file} as the name of a file in which to save the base
1326addresses of all the relocations needed for generating DLLs with
1327@file{dlltool}.
1328
1329@kindex --dll
1330@item --dll
1331Create a DLL instead of a regular executable. You may also use
1332@code{-shared} or specify a @code{LIBRARY} in a given @code{.def}
1333file.
1334
1335@kindex --enable-stdcall-fixup
1336@kindex --disable-stdcall-fixup
1337@item --enable-stdcall-fixup
1338@itemx --disable-stdcall-fixup
1339If the link finds a symbol that it cannot resolve, it will attempt to
1340do "fuzzy linking" by looking for another defined symbol that differs
1341only in the format of the symbol name (cdecl vs stdcall) and will
1342resolve that symbol by linking to the match. For example, the
1343undefined symbol @code{_foo} might be linked to the function
1344@code{_foo@@12}, or the undefined symbol @code{_bar@@16} might be linked
1345to the function @code{_bar}. When the linker does this, it prints a
1346warning, since it normally should have failed to link, but sometimes
1347import libraries generated from third-party dlls may need this feature
1348to be usable. If you specify @code{--enable-stdcall-fixup}, this
1349feature is fully enabled and warnings are not printed. If you specify
1350@code{--disable-stdcall-fixup}, this feature is disabled and such
1351mismatches are considered to be errors.
1352
1353@cindex DLLs, creating
1354@kindex --export-all-symbols
1355@item --export-all-symbols
1356If given, all global symbols in the objects used to build a DLL will
1357be exported by the DLL. Note that this is the default if there
1358otherwise wouldn't be any exported symbols. When symbols are
1359explicitly exported via DEF files or implicitly exported via function
1360attributes, the default is to not export anything else unless this
1361option is given. Note that the symbols @code{DllMain@@12},
1362@code{DllEntryPoint@@0}, and @code{impure_ptr} will not be automatically
1363exported.
1364
1365@kindex --exclude-symbols
1366@item --exclude-symbols @var{symbol,symbol,...}
1367Specifies a list of symbols which should not be automatically
1368exported. The symbol names may be delimited by commas or colons.
1369
1370@kindex --file-alignment
1371@item --file-alignment
1372Specify the file alignment. Sections in the file will always begin at
1373file offsets which are multiples of this number. This defaults to
1374512.
1375
1376@cindex heap size
1377@kindex --heap
1378@item --heap @var{reserve}
1379@itemx --heap @var{reserve},@var{commit}
1380Specify the amount of memory to reserve (and optionally commit) to be
1381used as heap for this program. The default is 1Mb reserved, 4K
1382committed.
1383
1384@cindex image base
1385@kindex --image-base
1386@item --image-base @var{value}
1387Use @var{value} as the base address of your program or dll. This is
1388the lowest memory location that will be used when your program or dll
1389is loaded. To reduce the need to relocate and improve performance of
1390your dlls, each should have a unique base address and not overlap any
1391other dlls. The default is 0x400000 for executables, and 0x10000000
1392for dlls.
1393
1394@kindex --kill-at
1395@item --kill-at
1396If given, the stdcall suffixes (@@@var{nn}) will be stripped from
1397symbols before they are exported.
1398
1399@kindex --major-image-version
1400@item --major-image-version @var{value}
1401Sets the major number of the "image version". Defaults to 1.
1402
1403@kindex --major-os-version
1404@item --major-os-version @var{value}
1405Sets the major number of the "os version". Defaults to 4.
1406
1407@kindex --major-subsystem-version
1408@item --major-subsystem-version @var{value}
1409Sets the major number of the "subsystem version". Defaults to 4.
1410
1411@kindex --minor-image-version
1412@item --minor-image-version @var{value}
1413Sets the minor number of the "image version". Defaults to 0.
1414
1415@kindex --minor-os-version
1416@item --minor-os-version @var{value}
1417Sets the minor number of the "os version". Defaults to 0.
1418
1419@kindex --minor-subsystem-version
1420@item --minor-subsystem-version @var{value}
1421Sets the minor number of the "subsystem version". Defaults to 0.
1422
1423@cindex DEF files, creating
1424@cindex DLLs, creating
1425@kindex --output-def
1426@item --output-def @var{file}
1427The linker will create the file @var{file} which will contain a DEF
1428file corresponding to the DLL the linker is generating. This DEF file
1429(which should be called @code{*.def}) may be used to create an import
1430library with @code{dlltool} or may be used as a reference to
1431automatically or implicitly exported symbols.
1432
1433@kindex --section-alignment
1434@item --section-alignment
1435Sets the section alignment. Sections in memory will always begin at
1436addresses which are a multiple of this number. Defaults to 0x1000.
1437
1438@cindex stack size
1439@kindex --stack
1440@item --stack @var{reserve}
1441@itemx --stack @var{reserve},@var{commit}
1442Specify the amount of memory to reserve (and optionally commit) to be
1443used as stack for this program. The default is 32Mb reserved, 4K
1444committed.
1445
1446@kindex --subsystem
1447@item --subsystem @var{which}
1448@itemx --subsystem @var{which}:@var{major}
1449@itemx --subsystem @var{which}:@var{major}.@var{minor}
1450Specifies the subsystem under which your program will execute. The
1451legal values for @var{which} are @code{native}, @code{windows},
1452@code{console}, and @code{posix}. You may optionally set the
1453subsystem version also.
1454
1455@end table
1456
1457@ifset UsesEnvVars
1458@node Environment
1459@section Environment Variables
1460
1461You can change the behavior of @code{ld} with the environment variables
1462@code{GNUTARGET}, @code{LDEMULATION}, and @code{COLLECT_NO_DEMANGLE}.
1463
1464@kindex GNUTARGET
1465@cindex default input format
1466@code{GNUTARGET} determines the input-file object format if you don't
1467use @samp{-b} (or its synonym @samp{--format}). Its value should be one
1468of the BFD names for an input format (@pxref{BFD}). If there is no
1469@code{GNUTARGET} in the environment, @code{ld} uses the natural format
1470of the target. If @code{GNUTARGET} is set to @code{default} then BFD
1471attempts to discover the input format by examining binary input files;
1472this method often succeeds, but there are potential ambiguities, since
1473there is no method of ensuring that the magic number used to specify
1474object-file formats is unique. However, the configuration procedure for
1475BFD on each system places the conventional format for that system first
1476in the search-list, so ambiguities are resolved in favor of convention.
1477
1478@kindex LDEMULATION
1479@cindex default emulation
1480@cindex emulation, default
1481@code{LDEMULATION} determines the default emulation if you don't use the
1482@samp{-m} option. The emulation can affect various aspects of linker
1483behaviour, particularly the default linker script. You can list the
1484available emulations with the @samp{--verbose} or @samp{-V} options. If
1485the @samp{-m} option is not used, and the @code{LDEMULATION} environment
1486variable is not defined, the default emulation depends upon how the
1487linker was configured.
1488@end ifset
1489
1490@kindex COLLECT_NO_DEMANGLE
1491@cindex demangling, default
1492Normally, the linker will default to demangling symbols. However, if
1493@code{COLLECT_NO_DEMANGLE} is set in the environment, then it will
1494default to not demangling symbols. This environment variable is used in
1495a similar fashion by the @code{gcc} linker wrapper program. The default
1496may be overridden by the @samp{--demangle} and @samp{--no-demangle}
1497options.
1498
1499@node Scripts
1500@chapter Linker Scripts
1501
1502@cindex scripts
1503@cindex linker scripts
1504@cindex command files
1505Every link is controlled by a @dfn{linker script}. This script is
1506written in the linker command language.
1507
1508The main purpose of the linker script is to describe how the sections in
1509the input files should be mapped into the output file, and to control
1510the memory layout of the output file. Most linker scripts do nothing
1511more than this. However, when necessary, the linker script can also
1512direct the linker to perform many other operations, using the commands
1513described below.
1514
1515The linker always uses a linker script. If you do not supply one
1516yourself, the linker will use a default script that is compiled into the
1517linker executable. You can use the @samp{--verbose} command line option
1518to display the default linker script. Certain command line options,
1519such as @samp{-r} or @samp{-N}, will affect the default linker script.
1520
1521You may supply your own linker script by using the @samp{-T} command
1522line option. When you do this, your linker script will replace the
1523default linker script.
1524
1525You may also use linker scripts implicitly by naming them as input files
1526to the linker, as though they were files to be linked. @xref{Implicit
1527Linker Scripts}.
1528
1529@menu
1530* Basic Script Concepts:: Basic Linker Script Concepts
1531* Script Format:: Linker Script Format
1532* Simple Example:: Simple Linker Script Example
1533* Simple Commands:: Simple Linker Script Commands
1534* Assignments:: Assigning Values to Symbols
1535* SECTIONS:: SECTIONS Command
1536* MEMORY:: MEMORY Command
1537* PHDRS:: PHDRS Command
1538* VERSION:: VERSION Command
1539* Expressions:: Expressions in Linker Scripts
1540* Implicit Linker Scripts:: Implicit Linker Scripts
1541@end menu
1542
1543@node Basic Script Concepts
1544@section Basic Linker Script Concepts
1545@cindex linker script concepts
1546We need to define some basic concepts and vocabulary in order to
1547describe the linker script language.
1548
1549The linker combines input files into a single output file. The output
1550file and each input file are in a special data format known as an
1551@dfn{object file format}. Each file is called an @dfn{object file}.
1552The output file is often called an @dfn{executable}, but for our
1553purposes we will also call it an object file. Each object file has,
1554among other things, a list of @dfn{sections}. We sometimes refer to a
1555section in an input file as an @dfn{input section}; similarly, a section
1556in the output file is an @dfn{output section}.
1557
1558Each section in an object file has a name and a size. Most sections
1559also have an associated block of data, known as the @dfn{section
1560contents}. A section may be marked as @dfn{loadable}, which mean that
1561the contents should be loaded into memory when the output file is run.
1562A section with no contents may be @dfn{allocatable}, which means that an
1563area in memory should be set aside, but nothing in particular should be
1564loaded there (in some cases this memory must be zeroed out). A section
1565which is neither loadable nor allocatable typically contains some sort
1566of debugging information.
1567
1568Every loadable or allocatable output section has two addresses. The
1569first is the @dfn{VMA}, or virtual memory address. This is the address
1570the section will have when the output file is run. The second is the
1571@dfn{LMA}, or load memory address. This is the address at which the
1572section will be loaded. In most cases the two addresses will be the
1573same. An example of when they might be different is when a data section
1574is loaded into ROM, and then copied into RAM when the program starts up
1575(this technique is often used to initialize global variables in a ROM
1576based system). In this case the ROM address would be the LMA, and the
1577RAM address would be the VMA.
1578
1579You can see the sections in an object file by using the @code{objdump}
1580program with the @samp{-h} option.
1581
1582Every object file also has a list of @dfn{symbols}, known as the
1583@dfn{symbol table}. A symbol may be defined or undefined. Each symbol
1584has a name, and each defined symbol has an address, among other
1585information. If you compile a C or C++ program into an object file, you
1586will get a defined symbol for every defined function and global or
1587static variable. Every undefined function or global variable which is
1588referenced in the input file will become an undefined symbol.
1589
1590You can see the symbols in an object file by using the @code{nm}
1591program, or by using the @code{objdump} program with the @samp{-t}
1592option.
1593
1594@node Script Format
1595@section Linker Script Format
1596@cindex linker script format
1597Linker scripts are text files.
1598
1599You write a linker script as a series of commands. Each command is
1600either a keyword, possibly followed by arguments, or an assignment to a
1601symbol. You may separate commands using semicolons. Whitespace is
1602generally ignored.
1603
1604Strings such as file or format names can normally be entered directly.
1605If the file name contains a character such as a comma which would
1606otherwise serve to separate file names, you may put the file name in
1607double quotes. There is no way to use a double quote character in a
1608file name.
1609
1610You may include comments in linker scripts just as in C, delimited by
1611@samp{/*} and @samp{*/}. As in C, comments are syntactically equivalent
1612to whitespace.
1613
1614@node Simple Example
1615@section Simple Linker Script Example
1616@cindex linker script example
1617@cindex example of linker script
1618Many linker scripts are fairly simple.
1619
1620The simplest possible linker script has just one command:
1621@samp{SECTIONS}. You use the @samp{SECTIONS} command to describe the
1622memory layout of the output file.
1623
1624The @samp{SECTIONS} command is a powerful command. Here we will
1625describe a simple use of it. Let's assume your program consists only of
1626code, initialized data, and uninitialized data. These will be in the
1627@samp{.text}, @samp{.data}, and @samp{.bss} sections, respectively.
1628Let's assume further that these are the only sections which appear in
1629your input files.
1630
1631For this example, let's say that the code should be loaded at address
16320x10000, and that the data should start at address 0x8000000. Here is a
1633linker script which will do that:
1634@smallexample
1635SECTIONS
1636@{
1637 . = 0x10000;
1638 .text : @{ *(.text) @}
1639 . = 0x8000000;
1640 .data : @{ *(.data) @}
1641 .bss : @{ *(.bss) @}
1642@}
1643@end smallexample
1644
1645You write the @samp{SECTIONS} command as the keyword @samp{SECTIONS},
1646followed by a series of symbol assignments and output section
1647descriptions enclosed in curly braces.
1648
1649The first line in the above example sets the special symbol @samp{.},
1650which is the location counter. If you do not specify the address of an
1651output section in some other way (other ways are described later), the
1652address is set from the current value of the location counter. The
1653location counter is then incremented by the size of the output section.
1654
1655The first line inside the @samp{SECTIONS} command of the above example
1656sets the value of the special symbol @samp{.}, which is the location
1657counter. If you do not specify the address of an output section in some
1658other way (other ways are described later), the address is set from the
1659current value of the location counter. The location counter is then
1660incremented by the size of the output section. At the start of the
1661@samp{SECTIONS} command, the location counter has the value @samp{0}.
1662
1663The second line defines an output section, @samp{.text}. The colon is
1664required syntax which may be ignored for now. Within the curly braces
1665after the output section name, you list the names of the input sections
1666which should be placed into this output section. The @samp{*} is a
1667wildcard which matches any file name. The expression @samp{*(.text)}
1668means all @samp{.text} input sections in all input files.
1669
1670Since the location counter is @samp{0x10000} when the output section
1671@samp{.text} is defined, the linker will set the address of the
1672@samp{.text} section in the output file to be @samp{0x10000}.
1673
1674The remaining lines define the @samp{.data} and @samp{.bss} sections in
1675the output file. The linker will place the @samp{.data} output section
1676at address @samp{0x8000000}. After the linker places the @samp{.data}
1677output section, the value of the location counter will be
1678@samp{0x8000000} plus the size of the @samp{.data} output section. The
1679effect is that the linker will place the @samp{.bss} output section
1680immediately after the @samp{.data} output section in memory
1681
1682The linker will ensure that each output section has the required
1683alignment, by increasing the location counter if necessary. In this
1684example, the specified addresses for the @samp{.text} and @samp{.data}
1685sections will probably satisfy any alignment constraints, but the linker
1686may have to create a small gap between the @samp{.data} and @samp{.bss}
1687sections.
1688
1689That's it! That's a simple and complete linker script.
1690
1691@node Simple Commands
1692@section Simple Linker Script Commands
1693@cindex linker script simple commands
1694In this section we describe the simple linker script commands.
1695
1696@menu
1697* Entry Point:: Setting the entry point
1698* File Commands:: Commands dealing with files
1699@ifclear SingleFormat
1700* Format Commands:: Commands dealing with object file formats
1701@end ifclear
1702
1703* Miscellaneous Commands:: Other linker script commands
1704@end menu
1705
1706@node Entry Point
1707@subsection Setting the entry point
1708@kindex ENTRY(@var{symbol})
1709@cindex start of execution
1710@cindex first instruction
1711@cindex entry point
1712The first instruction to execute in a program is called the @dfn{entry
1713point}. You can use the @code{ENTRY} linker script command to set the
1714entry point. The argument is a symbol name:
1715@smallexample
1716ENTRY(@var{symbol})
1717@end smallexample
1718
1719There are several ways to set the entry point. The linker will set the
1720entry point by trying each of the following methods in order, and
1721stopping when one of them succeeds:
1722@itemize @bullet
1723@item
1724the @samp{-e} @var{entry} command-line option;
1725@item
1726the @code{ENTRY(@var{symbol})} command in a linker script;
1727@item
1728the value of the symbol @code{start}, if defined;
1729@item
1730the address of the first byte of the @samp{.text} section, if present;
1731@item
1732The address @code{0}.
1733@end itemize
1734
1735@node File Commands
1736@subsection Commands dealing with files
1737@cindex linker script file commands
1738Several linker script commands deal with files.
1739
1740@table @code
1741@item INCLUDE @var{filename}
1742@kindex INCLUDE @var{filename}
1743@cindex including a linker script
1744Include the linker script @var{filename} at this point. The file will
1745be searched for in the current directory, and in any directory specified
1746with the @code{-L} option. You can nest calls to @code{INCLUDE} up to
174710 levels deep.
1748
1749@item INPUT(@var{file}, @var{file}, @dots{})
1750@itemx INPUT(@var{file} @var{file} @dots{})
1751@kindex INPUT(@var{files})
1752@cindex input files in linker scripts
1753@cindex input object files in linker scripts
1754@cindex linker script input object files
1755The @code{INPUT} command directs the linker to include the named files
1756in the link, as though they were named on the command line.
1757
1758For example, if you always want to include @file{subr.o} any time you do
1759a link, but you can't be bothered to put it on every link command line,
1760then you can put @samp{INPUT (subr.o)} in your linker script.
1761
1762In fact, if you like, you can list all of your input files in the linker
1763script, and then invoke the linker with nothing but a @samp{-T} option.
1764
1765The linker will first try to open the file in the current directory. If
1766it is not found, the linker will search through the archive library
1767search path. See the description of @samp{-L} in @ref{Options,,Command
1768Line Options}.
1769
1770If you use @samp{INPUT (-l@var{file})}, @code{ld} will transform the
1771name to @code{lib@var{file}.a}, as with the command line argument
1772@samp{-l}.
1773
1774When you use the @code{INPUT} command in an implicit linker script, the
1775files will be included in the link at the point at which the linker
1776script file is included. This can affect archive searching.
1777
1778@item GROUP(@var{file}, @var{file}, @dots{})
1779@itemx GROUP(@var{file} @var{file} @dots{})
1780@kindex GROUP(@var{files})
1781@cindex grouping input files
1782The @code{GROUP} command is like @code{INPUT}, except that the named
1783files should all be archives, and they are searched repeatedly until no
1784new undefined references are created. See the description of @samp{-(}
1785in @ref{Options,,Command Line Options}.
1786
1787@item OUTPUT(@var{filename})
1788@kindex OUTPUT(@var{filename})
1789@cindex output file name in linker scripot
1790The @code{OUTPUT} command names the output file. Using
1791@code{OUTPUT(@var{filename})} in the linker script is exactly like using
1792@samp{-o @var{filename}} on the command line (@pxref{Options,,Command
1793Line Options}). If both are used, the command line option takes
1794precedence.
1795
1796You can use the @code{OUTPUT} command to define a default name for the
1797output file other than the usual default of @file{a.out}.
1798
1799@item SEARCH_DIR(@var{path})
1800@kindex SEARCH_DIR(@var{path})
1801@cindex library search path in linker script
1802@cindex archive search path in linker script
1803@cindex search path in linker script
1804The @code{SEARCH_DIR} command adds @var{path} to the list of paths where
1805@code{ld} looks for archive libraries. Using
1806@code{SEARCH_DIR(@var{path})} is exactly like using @samp{-L @var{path}}
1807on the command line (@pxref{Options,,Command Line Options}). If both
1808are used, then the linker will search both paths. Paths specified using
1809the command line option are searched first.
1810
1811@item STARTUP(@var{filename})
1812@kindex STARTUP(@var{filename})
1813@cindex first input file
1814The @code{STARTUP} command is just like the @code{INPUT} command, except
1815that @var{filename} will become the first input file to be linked, as
1816though it were specified first on the command line. This may be useful
1817when using a system in which the entry point is always the start of the
1818first file.
1819@end table
1820
1821@ifclear SingleFormat
1822@node Format Commands
1823@subsection Commands dealing with object file formats
1824A couple of linker script commands deal with object file formats.
1825
1826@table @code
1827@item OUTPUT_FORMAT(@var{bfdname})
1828@itemx OUTPUT_FORMAT(@var{default}, @var{big}, @var{little})
1829@kindex OUTPUT_FORMAT(@var{bfdname})
1830@cindex output file format in linker script
1831The @code{OUTPUT_FORMAT} command names the BFD format to use for the
1832output file (@pxref{BFD}). Using @code{OUTPUT_FORMAT(@var{bfdname})} is
1833exactly like using @samp{-oformat @var{bfdname}} on the command line
1834(@pxref{Options,,Command Line Options}). If both are used, the command
1835line option takes precedence.
1836
1837You can use @code{OUTPUT_FORMAT} with three arguments to use different
1838formats based on the @samp{-EB} and @samp{-EL} command line options.
1839This permits the linker script to set the output format based on the
1840desired endianness.
1841
1842If neither @samp{-EB} nor @samp{-EL} are used, then the output format
1843will be the first argument, @var{default}. If @samp{-EB} is used, the
1844output format will be the second argument, @var{big}. If @samp{-EL} is
1845used, the output format will be the third argument, @var{little}.
1846
1847For example, the default linker script for the MIPS ELF target uses this
1848command:
1849@smallexample
1850OUTPUT_FORMAT(elf32-bigmips, elf32-bigmips, elf32-littlemips)
1851@end smallexample
1852This says that the default format for the output file is
1853@samp{elf32-bigmips}, but if the user uses the @samp{-EL} command line
1854option, the output file will be created in the @samp{elf32-littlemips}
1855format.
1856
1857@item TARGET(@var{bfdname})
1858@kindex TARGET(@var{bfdname})
1859@cindex input file format in linker script
1860The @code{TARGET} command names the BFD format to use when reading input
1861files. It affects subsequent @code{INPUT} and @code{GROUP} commands.
1862This command is like using @samp{-b @var{bfdname}} on the command line
1863(@pxref{Options,,Command Line Options}). If the @code{TARGET} command
1864is used but @code{OUTPUT_FORMAT} is not, then the last @code{TARGET}
1865command is also used to set the format for the output file. @xref{BFD}.
1866@end table
1867@end ifclear
1868
1869@node Miscellaneous Commands
1870@subsection Other linker script commands
1871There are a few other linker scripts commands.
1872
1873@table @code
1874@item ASSERT(@var{exp}, @var{message})
1875@kindex ASSERT
1876@cindex assertion in linker script
1877Ensure that @var{exp} is non-zero. If it is zero, then exit the linker
1878with an error code, and print @var{message}.
1879
1880@item EXTERN(@var{symbol} @var{symbol} @dots{})
1881@kindex EXTERN
1882@cindex undefined symbol in linker script
1883Force @var{symbol} to be entered in the output file as an undefined
1884symbol. Doing this may, for example, trigger linking of additional
1885modules from standard libraries. You may list several @var{symbol}s for
1886each @code{EXTERN}, and you may use @code{EXTERN} multiple times. This
1887command has the same effect as the @samp{-u} command-line option.
1888
1889@item FORCE_COMMON_ALLOCATION
1890@kindex FORCE_COMMON_ALLOCATION
1891@cindex common allocation in linker script
1892This command has the same effect as the @samp{-d} command-line option:
1893to make @code{ld} assign space to common symbols even if a relocatable
1894output file is specified (@samp{-r}).
1895
1896@item NOCROSSREFS(@var{section} @var{section} @dots{})
1897@kindex NOCROSSREFS(@var{sections})
1898@cindex cross references
1899This command may be used to tell @code{ld} to issue an error about any
1900references among certain output sections.
1901
1902In certain types of programs, particularly on embedded systems when
1903using overlays, when one section is loaded into memory, another section
1904will not be. Any direct references between the two sections would be
1905errors. For example, it would be an error if code in one section called
1906a function defined in the other section.
1907
1908The @code{NOCROSSREFS} command takes a list of output section names. If
1909@code{ld} detects any cross references between the sections, it reports
1910an error and returns a non-zero exit status. Note that the
1911@code{NOCROSSREFS} command uses output section names, not input section
1912names.
1913
1914@ifclear SingleFormat
1915@item OUTPUT_ARCH(@var{bfdarch})
1916@kindex OUTPUT_ARCH(@var{bfdarch})
1917@cindex machine architecture
1918@cindex architecture
1919Specify a particular output machine architecture. The argument is one
1920of the names used by the BFD library (@pxref{BFD}). You can see the
1921architecture of an object file by using the @code{objdump} program with
1922the @samp{-f} option.
1923@end ifclear
1924@end table
1925
1926@node Assignments
1927@section Assigning Values to Symbols
1928@cindex assignment in scripts
1929@cindex symbol definition, scripts
1930@cindex variables, defining
1931You may assign a value to a symbol in a linker script. This will define
1932the symbol as a global symbol.
1933
1934@menu
1935* Simple Assignments:: Simple Assignments
1936* PROVIDE:: PROVIDE
1937@end menu
1938
1939@node Simple Assignments
1940@subsection Simple Assignments
1941
1942You may assign to a symbol using any of the C assignment operators:
1943
1944@table @code
1945@item @var{symbol} = @var{expression} ;
1946@itemx @var{symbol} += @var{expression} ;
1947@itemx @var{symbol} -= @var{expression} ;
1948@itemx @var{symbol} *= @var{expression} ;
1949@itemx @var{symbol} /= @var{expression} ;
1950@itemx @var{symbol} <<= @var{expression} ;
1951@itemx @var{symbol} >>= @var{expression} ;
1952@itemx @var{symbol} &= @var{expression} ;
1953@itemx @var{symbol} |= @var{expression} ;
1954@end table
1955
1956The first case will define @var{symbol} to the value of
1957@var{expression}. In the other cases, @var{symbol} must already be
1958defined, and the value will be adjusted accordingly.
1959
1960The special symbol name @samp{.} indicates the location counter. You
1961may only use this within a @code{SECTIONS} command.
1962
1963The semicolon after @var{expression} is required.
1964
1965Expressions are defined below; see @ref{Expressions}.
1966
1967You may write symbol assignments as commands in their own right, or as
1968statements within a @code{SECTIONS} command, or as part of an output
1969section description in a @code{SECTIONS} command.
1970
1971The section of the symbol will be set from the section of the
1972expression; for more information, see @ref{Expression Section}.
1973
1974Here is an example showing the three different places that symbol
1975assignments may be used:
1976
1977@smallexample
1978floating_point = 0;
1979SECTIONS
1980@{
1981 .text :
1982 @{
1983 *(.text)
1984 _etext = .;
1985 @}
1986 _bdata = (. + 3) & ~ 4;
1987 .data : @{ *(.data) @}
1988@}
1989@end smallexample
1990@noindent
1991In this example, the symbol @samp{floating_point} will be defined as
1992zero. The symbol @samp{_etext} will be defined as the address following
1993the last @samp{.text} input section. The symbol @samp{_bdata} will be
1994defined as the address following the @samp{.text} output section aligned
1995upward to a 4 byte boundary.
1996
1997@node PROVIDE
1998@subsection PROVIDE
1999@cindex PROVIDE
2000In some cases, it is desirable for a linker script to define a symbol
2001only if it is referenced and is not defined by any object included in
2002the link. For example, traditional linkers defined the symbol
2003@samp{etext}. However, ANSI C requires that the user be able to use
2004@samp{etext} as a function name without encountering an error. The
2005@code{PROVIDE} keyword may be used to define a symbol, such as
2006@samp{etext}, only if it is referenced but not defined. The syntax is
2007@code{PROVIDE(@var{symbol} = @var{expression})}.
2008
2009Here is an example of using @code{PROVIDE} to define @samp{etext}:
2010@smallexample
2011SECTIONS
2012@{
2013 .text :
2014 @{
2015 *(.text)
2016 _etext = .;
2017 PROVIDE(etext = .);
2018 @}
2019@}
2020@end smallexample
2021
2022In this example, if the program defines @samp{_etext} (with a leading
2023underscore), the linker will give a multiple definition error. If, on
2024the other hand, the program defines @samp{etext} (with no leading
2025underscore), the linker will silently use the definition in the program.
2026If the program references @samp{etext} but does not define it, the
2027linker will use the definition in the linker script.
2028
2029@node SECTIONS
2030@section SECTIONS command
2031@kindex SECTIONS
2032The @code{SECTIONS} command tells the linker how to map input sections
2033into output sections, and how to place the output sections in memory.
2034
2035The format of the @code{SECTIONS} command is:
2036@smallexample
2037SECTIONS
2038@{
2039 @var{sections-command}
2040 @var{sections-command}
2041 @dots{}
2042@}
2043@end smallexample
2044
2045Each @var{sections-command} may of be one of the following:
2046
2047@itemize @bullet
2048@item
2049an @code{ENTRY} command (@pxref{Entry Point,,Entry command})
2050@item
2051a symbol assignment (@pxref{Assignments})
2052@item
2053an output section description
2054@item
2055an overlay description
2056@end itemize
2057
2058The @code{ENTRY} command and symbol assignments are permitted inside the
2059@code{SECTIONS} command for convenience in using the location counter in
2060those commands. This can also make the linker script easier to
2061understand because you can use those commands at meaningful points in
2062the layout of the output file.
2063
2064Output section descriptions and overlay descriptions are described
2065below.
2066
2067If you do not use a @code{SECTIONS} command in your linker script, the
2068linker will place each input section into an identically named output
2069section in the order that the sections are first encountered in the
2070input files. If all input sections are present in the first file, for
2071example, the order of sections in the output file will match the order
2072in the first input file. The first section will be at address zero.
2073
2074@menu
2075* Output Section Description:: Output section description
2076* Output Section Name:: Output section name
2077* Output Section Address:: Output section address
2078* Input Section:: Input section description
2079* Output Section Data:: Output section data
2080* Output Section Keywords:: Output section keywords
2081* Output Section Discarding:: Output section discarding
2082* Output Section Attributes:: Output section attributes
2083* Overlay Description:: Overlay description
2084@end menu
2085
2086@node Output Section Description
2087@subsection Output section description
2088The full description of an output section looks like this:
2089@smallexample
2090@group
2091@var{section} [@var{address}] [(@var{type})] : [AT(@var{lma})]
2092 @{
2093 @var{output-section-command}
2094 @var{output-section-command}
2095 @dots{}
2096 @} [>@var{region}] [:@var{phdr} :@var{phdr} @dots{}] [=@var{fillexp}]
2097@end group
2098@end smallexample
2099
2100Most output sections do not use most of the optional section attributes.
2101
2102The whitespace around @var{section} is required, so that the section
2103name is unambiguous. The colon and the curly braces are also required.
2104The line breaks and other white space are optional.
2105
2106Each @var{output-section-command} may be one of the following:
2107
2108@itemize @bullet
2109@item
2110a symbol assignment (@pxref{Assignments})
2111@item
2112an input section description (@pxref{Input Section})
2113@item
2114data values to include directly (@pxref{Output Section Data})
2115@item
2116a special output section keyword (@pxref{Output Section Keywords})
2117@end itemize
2118
2119@node Output Section Name
2120@subsection Output section name
2121@cindex name, section
2122@cindex section name
2123The name of the output section is @var{section}. @var{section} must
2124meet the constraints of your output format. In formats which only
2125support a limited number of sections, such as @code{a.out}, the name
2126must be one of the names supported by the format (@code{a.out}, for
2127example, allows only @samp{.text}, @samp{.data} or @samp{.bss}). If the
2128output format supports any number of sections, but with numbers and not
2129names (as is the case for Oasys), the name should be supplied as a
2130quoted numeric string. A section name may consist of any sequence of
2131characters, but a name which contains any unusual characters such as
2132commas must be quoted.
2133
2134The output section name @samp{/DISCARD/} is special; @ref{Output Section
2135Discarding}.
2136
2137@node Output Section Address
2138@subsection Output section address
2139@cindex address, section
2140@cindex section address
2141The @var{address} is an expression for the VMA (the virtual memory
2142address) of the output section. If you do not provide @var{address},
2143the linker will set it based on @var{region} if present, or otherwise
2144based on the current value of the location counter.
2145
2146If you provide @var{address}, the address of the output section will be
2147set to precisely that. If you provide neither @var{address} nor
2148@var{region}, then the address of the output section will be set to the
2149current value of the location counter aligned to the alignment
2150requirements of the output section. The alignment requirement of the
2151output section is the strictest alignment of any input section contained
2152within the output section.
2153
2154For example,
2155@smallexample
2156.text . : @{ *(.text) @}
2157@end smallexample
2158@noindent
2159and
2160@smallexample
2161.text : @{ *(.text) @}
2162@end smallexample
2163@noindent
2164are subtly different. The first will set the address of the
2165@samp{.text} output section to the current value of the location
2166counter. The second will set it to the current value of the location
2167counter aligned to the strictest alignment of a @samp{.text} input
2168section.
2169
2170The @var{address} may be an arbitrary expression; @ref{Expressions}.
2171For example, if you want to align the section on a 0x10 byte boundary,
2172so that the lowest four bits of the section address are zero, you could
2173do something like this:
2174@smallexample
2175.text ALIGN(0x10) : @{ *(.text) @}
2176@end smallexample
2177@noindent
2178This works because @code{ALIGN} returns the current location counter
2179aligned upward to the specified value.
2180
2181Specifying @var{address} for a section will change the value of the
2182location counter.
2183
2184@node Input Section
2185@subsection Input section description
2186@cindex input sections
2187@cindex mapping input sections to output sections
2188The most common output section command is an input section description.
2189
2190The input section description is the most basic linker script operation.
2191You use output sections to tell the linker how to lay out your program
2192in memory. You use input section descriptions to tell the linker how to
2193map the input files into your memory layout.
2194
2195@menu
2196* Input Section Basics:: Input section basics
2197* Input Section Wildcards:: Input section wildcard patterns
2198* Input Section Common:: Input section for common symbols
2199* Input Section Keep:: Input section and garbage collection
2200* Input Section Example:: Input section example
2201@end menu
2202
2203@node Input Section Basics
2204@subsubsection Input section basics
2205@cindex input section basics
2206An input section description consists of a file name optionally followed
2207by a list of section names in parentheses.
2208
2209The file name and the section name may be wildcard patterns, which we
2210describe further below (@pxref{Input Section Wildcards}).
2211
2212The most common input section description is to include all input
2213sections with a particular name in the output section. For example, to
2214include all input @samp{.text} sections, you would write:
2215@smallexample
2216*(.text)
2217@end smallexample
2218@noindent
2219Here the @samp{*} is a wildcard which matches any file name. To exclude a file
2220from matching the file name wildcard, EXCLUDE_FILE may be used to match all files
2221except the one specified by EXCLUDE_FILE. For example:
2222@smallexample
2223(*(EXCLUDE_FILE (*crtend.o) .ctors))
2224@end smallexample
2225will cause all .ctors sections from all files except crtend.o to be included.
2226
2227There are two ways to include more than one section:
2228@smallexample
2229*(.text .rdata)
2230*(.text) *(.rdata)
2231@end smallexample
2232@noindent
2233The difference between these is the order in which the @samp{.text} and
2234@samp{.rdata} input sections will appear in the output section. In the
2235first example, they will be intermingled. In the second example, all
2236@samp{.text} input sections will appear first, followed by all
2237@samp{.rdata} input sections.
2238
2239You can specify a file name to include sections from a particular file.
2240You would do this if one or more of your files contain special data that
2241needs to be at a particular location in memory. For example:
2242@smallexample
2243data.o(.data)
2244@end smallexample
2245
2246If you use a file name without a list of sections, then all sections in
2247the input file will be included in the output section. This is not
2248commonly done, but it may by useful on occasion. For example:
2249@smallexample
2250data.o
2251@end smallexample
2252
2253When you use a file name which does not contain any wild card
2254characters, the linker will first see if you also specified the file
2255name on the linker command line or in an @code{INPUT} command. If you
2256did not, the linker will attempt to open the file as an input file, as
2257though it appeared on the command line. Note that this differs from an
2258@code{INPUT} command, because the linker will not search for the file in
2259the archive search path.
2260
2261@node Input Section Wildcards
2262@subsubsection Input section wildcard patterns
2263@cindex input section wildcards
2264@cindex wildcard file name patterns
2265@cindex file name wildcard patterns
2266@cindex section name wildcard patterns
2267In an input section description, either the file name or the section
2268name or both may be wildcard patterns.
2269
2270The file name of @samp{*} seen in many examples is a simple wildcard
2271pattern for the file name.
2272
2273The wildcard patterns are like those used by the Unix shell.
2274
2275@table @samp
2276@item *
2277matches any number of characters
2278@item ?
2279matches any single character
2280@item [@var{chars}]
2281matches a single instance of any of the @var{chars}; the @samp{-}
2282character may be used to specify a range of characters, as in
2283@samp{[a-z]} to match any lower case letter
2284@item \
2285quotes the following character
2286@end table
2287
2288When a file name is matched with a wildcard, the wildcard characters
2289will not match a @samp{/} character (used to separate directory names on
2290Unix). A pattern consisting of a single @samp{*} character is an
2291exception; it will always match any file name, whether it contains a
2292@samp{/} or not. In a section name, the wildcard characters will match
2293a @samp{/} character.
2294
2295File name wildcard patterns only match files which are explicitly
2296specified on the command line or in an @code{INPUT} command. The linker
2297does not search directories to expand wildcards.
2298
2299If a file name matches more than one wildcard pattern, or if a file name
2300appears explicitly and is also matched by a wildcard pattern, the linker
2301will use the first match in the linker script. For example, this
2302sequence of input section descriptions is probably in error, because the
2303@file{data.o} rule will not be used:
2304@smallexample
2305.data : @{ *(.data) @}
2306.data1 : @{ data.o(.data) @}
2307@end smallexample
2308
2309@cindex SORT
2310Normally, the linker will place files and sections matched by wildcards
2311in the order in which they are seen during the link. You can change
2312this by using the @code{SORT} keyword, which appears before a wildcard
2313pattern in parentheses (e.g., @code{SORT(.text*)}). When the
2314@code{SORT} keyword is used, the linker will sort the files or sections
2315into ascending order by name before placing them in the output file.
2316
2317If you ever get confused about where input sections are going, use the
2318@samp{-M} linker option to generate a map file. The map file shows
2319precisely how input sections are mapped to output sections.
2320
2321This example shows how wildcard patterns might be used to partition
2322files. This linker script directs the linker to place all @samp{.text}
2323sections in @samp{.text} and all @samp{.bss} sections in @samp{.bss}.
2324The linker will place the @samp{.data} section from all files beginning
2325with an upper case character in @samp{.DATA}; for all other files, the
2326linker will place the @samp{.data} section in @samp{.data}.
2327@smallexample
2328@group
2329SECTIONS @{
2330 .text : @{ *(.text) @}
2331 .DATA : @{ [A-Z]*(.data) @}
2332 .data : @{ *(.data) @}
2333 .bss : @{ *(.bss) @}
2334@}
2335@end group
2336@end smallexample
2337
2338@node Input Section Common
2339@subsubsection Input section for common symbols
2340@cindex common symbol placement
2341@cindex uninitialized data placement
2342A special notation is needed for common symbols, because in many object
2343file formats common symbols do not have a particular input section. The
2344linker treats common symbols as though they are in an input section
2345named @samp{COMMON}.
2346
2347You may use file names with the @samp{COMMON} section just as with any
2348other input sections. You can use this to place common symbols from a
2349particular input file in one section while common symbols from other
2350input files are placed in another section.
2351
2352In most cases, common symbols in input files will be placed in the
2353@samp{.bss} section in the output file. For example:
2354@smallexample
2355.bss @{ *(.bss) *(COMMON) @}
2356@end smallexample
2357
2358@cindex scommon section
2359@cindex small common symbols
2360Some object file formats have more than one type of common symbol. For
2361example, the MIPS ELF object file format distinguishes standard common
2362symbols and small common symbols. In this case, the linker will use a
2363different special section name for other types of common symbols. In
2364the case of MIPS ELF, the linker uses @samp{COMMON} for standard common
2365symbols and @samp{.scommon} for small common symbols. This permits you
2366to map the different types of common symbols into memory at different
2367locations.
2368
2369@cindex [COMMON]
2370You will sometimes see @samp{[COMMON]} in old linker scripts. This
2371notation is now considered obsolete. It is equivalent to
2372@samp{*(COMMON)}.
2373
2374@node Input Section Keep
2375@subsubsection Input section and garbage collection
2376@cindex KEEP
2377@cindex garbage collection
2378When link-time garbage collection is in use (@samp{--gc-sections}),
2379it is often useful to mark sections that should not be eliminated.
2380This is accomplished by surrounding an input section's wildcard entry
2381with @code{KEEP()}, as in @code{KEEP(*(.init))} or
2382@code{KEEP(SORT(*)(.ctors))}.
2383
2384@node Input Section Example
2385@subsubsection Input section example
2386The following example is a complete linker script. It tells the linker
2387to read all of the sections from file @file{all.o} and place them at the
2388start of output section @samp{outputa} which starts at location
2389@samp{0x10000}. All of section @samp{.input1} from file @file{foo.o}
2390follows immediately, in the same output section. All of section
2391@samp{.input2} from @file{foo.o} goes into output section
2392@samp{outputb}, followed by section @samp{.input1} from @file{foo1.o}.
2393All of the remaining @samp{.input1} and @samp{.input2} sections from any
2394files are written to output section @samp{outputc}.
2395
2396@smallexample
2397@group
2398SECTIONS @{
2399 outputa 0x10000 :
2400 @{
2401 all.o
2402 foo.o (.input1)
2403 @}
2404 outputb :
2405 @{
2406 foo.o (.input2)
2407 foo1.o (.input1)
2408 @}
2409 outputc :
2410 @{
2411 *(.input1)
2412 *(.input2)
2413 @}
2414@}
2415@end group
2416@end smallexample
2417
2418@node Output Section Data
2419@subsection Output section data
2420@cindex data
2421@cindex section data
2422@cindex output section data
2423@kindex BYTE(@var{expression})
2424@kindex SHORT(@var{expression})
2425@kindex LONG(@var{expression})
2426@kindex QUAD(@var{expression})
2427@kindex SQUAD(@var{expression})
2428You can include explicit bytes of data in an output section by using
2429@code{BYTE}, @code{SHORT}, @code{LONG}, @code{QUAD}, or @code{SQUAD} as
2430an output section command. Each keyword is followed by an expression in
2431parentheses providing the value to store (@pxref{Expressions}). The
2432value of the expression is stored at the current value of the location
2433counter.
2434
2435The @code{BYTE}, @code{SHORT}, @code{LONG}, and @code{QUAD} commands
2436store one, two, four, and eight bytes (respectively). After storing the
2437bytes, the location counter is incremented by the number of bytes
2438stored.
2439
2440For example, this will store the byte 1 followed by the four byte value
2441of the symbol @samp{addr}:
2442@smallexample
2443BYTE(1)
2444LONG(addr)
2445@end smallexample
2446
2447When using a 64 bit host or target, @code{QUAD} and @code{SQUAD} are the
2448same; they both store an 8 byte, or 64 bit, value. When both host and
2449target are 32 bits, an expression is computed as 32 bits. In this case
2450@code{QUAD} stores a 32 bit value zero extended to 64 bits, and
2451@code{SQUAD} stores a 32 bit value sign extended to 64 bits.
2452
2453If the object file format of the output file has an explicit endianness,
2454which is the normal case, the value will be stored in that endianness.
2455When the object file format does not have an explicit endianness, as is
2456true of, for example, S-records, the value will be stored in the
2457endianness of the first input object file.
2458
2459@kindex FILL(@var{expression})
2460@cindex holes, filling
2461@cindex unspecified memory
2462You may use the @code{FILL} command to set the fill pattern for the
2463current section. It is followed by an expression in parentheses. Any
2464otherwise unspecified regions of memory within the section (for example,
2465gaps left due to the required alignment of input sections) are filled
2466with the two least significant bytes of the expression, repeated as
2467necessary. A @code{FILL} statement covers memory locations after the
2468point at which it occurs in the section definition; by including more
2469than one @code{FILL} statement, you can have different fill patterns in
2470different parts of an output section.
2471
2472This example shows how to fill unspecified regions of memory with the
2473value @samp{0x9090}:
2474@smallexample
2475FILL(0x9090)
2476@end smallexample
2477
2478The @code{FILL} command is similar to the @samp{=@var{fillexp}} output
2479section attribute (@pxref{Output Section Fill}), but it only affects the
2480part of the section following the @code{FILL} command, rather than the
2481entire section. If both are used, the @code{FILL} command takes
2482precedence.
2483
2484@node Output Section Keywords
2485@subsection Output section keywords
2486There are a couple of keywords which can appear as output section
2487commands.
2488
2489@table @code
2490@kindex CREATE_OBJECT_SYMBOLS
2491@cindex input filename symbols
2492@cindex filename symbols
2493@item CREATE_OBJECT_SYMBOLS
2494The command tells the linker to create a symbol for each input file.
2495The name of each symbol will be the name of the corresponding input
2496file. The section of each symbol will be the output section in which
2497the @code{CREATE_OBJECT_SYMBOLS} command appears.
2498
2499This is conventional for the a.out object file format. It is not
2500normally used for any other object file format.
2501
2502@kindex CONSTRUCTORS
2503@cindex C++ constructors, arranging in link
2504@cindex constructors, arranging in link
2505@item CONSTRUCTORS
2506When linking using the a.out object file format, the linker uses an
2507unusual set construct to support C++ global constructors and
2508destructors. When linking object file formats which do not support
2509arbitrary sections, such as ECOFF and XCOFF, the linker will
2510automatically recognize C++ global constructors and destructors by name.
2511For these object file formats, the @code{CONSTRUCTORS} command tells the
2512linker to place constructor information in the output section where the
2513@code{CONSTRUCTORS} command appears. The @code{CONSTRUCTORS} command is
2514ignored for other object file formats.
2515
2516The symbol @w{@code{__CTOR_LIST__}} marks the start of the global
2517constructors, and the symbol @w{@code{__DTOR_LIST}} marks the end. The
2518first word in the list is the number of entries, followed by the address
2519of each constructor or destructor, followed by a zero word. The
2520compiler must arrange to actually run the code. For these object file
2521formats @sc{gnu} C++ normally calls constructors from a subroutine
2522@code{__main}; a call to @code{__main} is automatically inserted into
2523the startup code for @code{main}. @sc{gnu} C++ normally runs
2524destructors either by using @code{atexit}, or directly from the function
2525@code{exit}.
2526
2527For object file formats such as @code{COFF} or @code{ELF} which support
2528arbitrary section names, @sc{gnu} C++ will normally arrange to put the
2529addresses of global constructors and destructors into the @code{.ctors}
2530and @code{.dtors} sections. Placing the following sequence into your
2531linker script will build the sort of table which the @sc{gnu} C++
2532runtime code expects to see.
2533
2534@smallexample
2535 __CTOR_LIST__ = .;
2536 LONG((__CTOR_END__ - __CTOR_LIST__) / 4 - 2)
2537 *(.ctors)
2538 LONG(0)
2539 __CTOR_END__ = .;
2540 __DTOR_LIST__ = .;
2541 LONG((__DTOR_END__ - __DTOR_LIST__) / 4 - 2)
2542 *(.dtors)
2543 LONG(0)
2544 __DTOR_END__ = .;
2545@end smallexample
2546
2547If you are using the @sc{gnu} C++ support for initialization priority,
2548which provides some control over the order in which global constructors
2549are run, you must sort the constructors at link time to ensure that they
2550are executed in the correct order. When using the @code{CONSTRUCTORS}
2551command, use @samp{SORT(CONSTRUCTORS)} instead. When using the
2552@code{.ctors} and @code{.dtors} sections, use @samp{*(SORT(.ctors))} and
2553@samp{*(SORT(.dtors))} instead of just @samp{*(.ctors)} and
2554@samp{*(.dtors)}.
2555
2556Normally the compiler and linker will handle these issues automatically,
2557and you will not need to concern yourself with them. However, you may
2558need to consider this if you are using C++ and writing your own linker
2559scripts.
2560
2561@end table
2562
2563@node Output Section Discarding
2564@subsection Output section discarding
2565@cindex discarding sections
2566@cindex sections, discarding
2567@cindex removing sections
2568The linker will not create output section which do not have any
2569contents. This is for convenience when referring to input sections that
2570may or may not be present in any of the input files. For example:
2571@smallexample
2572.foo @{ *(.foo) @}
2573@end smallexample
2574@noindent
2575will only create a @samp{.foo} section in the output file if there is a
2576@samp{.foo} section in at least one input file.
2577
2578If you use anything other than an input section description as an output
2579section command, such as a symbol assignment, then the output section
2580will always be created, even if there are no matching input sections.
2581
2582@cindex /DISCARD/
2583The special output section name @samp{/DISCARD/} may be used to discard
2584input sections. Any input sections which are assigned to an output
2585section named @samp{/DISCARD/} are not included in the output file.
2586
2587@node Output Section Attributes
2588@subsection Output section attributes
2589@cindex output section attributes
2590We showed above that the full description of an output section looked
2591like this:
2592@smallexample
2593@group
2594@var{section} [@var{address}] [(@var{type})] : [AT(@var{lma})]
2595 @{
2596 @var{output-section-command}
2597 @var{output-section-command}
2598 @dots{}
2599 @} [>@var{region}] [:@var{phdr} :@var{phdr} @dots{}] [=@var{fillexp}]
2600@end group
2601@end smallexample
2602We've already described @var{section}, @var{address}, and
2603@var{output-section-command}. In this section we will describe the
2604remaining section attributes.
2605
2606@menu
2607* Output Section Type:: Output section type
2608* Output Section LMA:: Output section LMA
2609* Output Section Region:: Output section region
2610* Output Section Phdr:: Output section phdr
2611* Output Section Fill:: Output section fill
2612@end menu
2613
2614@node Output Section Type
2615@subsubsection Output section type
2616Each output section may have a type. The type is a keyword in
2617parentheses. The following types are defined:
2618
2619@table @code
2620@item NOLOAD
2621The section should be marked as not loadable, so that it will not be
2622loaded into memory when the program is run.
2623@item DSECT
2624@itemx COPY
2625@itemx INFO
2626@itemx OVERLAY
2627These type names are supported for backward compatibility, and are
2628rarely used. They all have the same effect: the section should be
2629marked as not allocatable, so that no memory is allocated for the
2630section when the program is run.
2631@end table
2632
2633@kindex NOLOAD
2634@cindex prevent unnecessary loading
2635@cindex loading, preventing
2636The linker normally sets the attributes of an output section based on
2637the input sections which map into it. You can override this by using
2638the section type. For example, in the script sample below, the
2639@samp{ROM} section is addressed at memory location @samp{0} and does not
2640need to be loaded when the program is run. The contents of the
2641@samp{ROM} section will appear in the linker output file as usual.
2642@smallexample
2643@group
2644SECTIONS @{
2645 ROM 0 (NOLOAD) : @{ @dots{} @}
2646 @dots{}
2647@}
2648@end group
2649@end smallexample
2650
2651@node Output Section LMA
2652@subsubsection Output section LMA
2653@kindex AT(@var{lma})
2654@cindex load address
2655@cindex section load address
2656Every section has a virtual address (VMA) and a load address (LMA); see
2657@ref{Basic Script Concepts}. The address expression which may appear in
2658an output section description sets the VMA (@pxref{Output Section
2659Address}).
2660
2661The linker will normally set the LMA equal to the VMA. You can change
2662that by using the @code{AT} keyword. The expression @var{lma} that
2663follows the @code{AT} keyword specifies the load address of the section.
2664
2665@cindex ROM initialized data
2666@cindex initialized data in ROM
2667This feature is designed to make it easy to build a ROM image. For
2668example, the following linker script creates three output sections: one
2669called @samp{.text}, which starts at @code{0x1000}, one called
2670@samp{.mdata}, which is loaded at the end of the @samp{.text} section
2671even though its VMA is @code{0x2000}, and one called @samp{.bss} to hold
2672uninitialized data at address @code{0x3000}. The symbol @code{_data} is
2673defined with the value @code{0x2000}, which shows that the location
2674counter holds the VMA value, not the LMA value.
2675
2676@smallexample
2677@group
2678SECTIONS
2679 @{
2680 .text 0x1000 : @{ *(.text) _etext = . ; @}
2681 .mdata 0x2000 :
2682 AT ( ADDR (.text) + SIZEOF (.text) )
2683 @{ _data = . ; *(.data); _edata = . ; @}
2684 .bss 0x3000 :
2685 @{ _bstart = . ; *(.bss) *(COMMON) ; _bend = . ;@}
2686@}
2687@end group
2688@end smallexample
2689
2690The run-time initialization code for use with a program generated with
2691this linker script would include something like the following, to copy
2692the initialized data from the ROM image to its runtime address. Notice
2693how this code takes advantage of the symbols defined by the linker
2694script.
2695
2696@smallexample
2697@group
2698extern char _etext, _data, _edata, _bstart, _bend;
2699char *src = &_etext;
2700char *dst = &_data;
2701
2702/* ROM has data at end of text; copy it. */
2703while (dst < &_edata) @{
2704 *dst++ = *src++;
2705@}
2706
2707/* Zero bss */
2708for (dst = &_bstart; dst< &_bend; dst++)
2709 *dst = 0;
2710@end group
2711@end smallexample
2712
2713@node Output Section Region
2714@subsubsection Output section region
2715@kindex >@var{region}
2716@cindex section, assigning to memory region
2717@cindex memory regions and sections
2718You can assign a section to a previously defined region of memory by
2719using @samp{>@var{region}}. @xref{MEMORY}.
2720
2721Here is a simple example:
2722@smallexample
2723@group
2724MEMORY @{ rom : ORIGIN = 0x1000, LENGTH = 0x1000 @}
2725SECTIONS @{ ROM : @{ *(.text) @} >rom @}
2726@end group
2727@end smallexample
2728
2729@node Output Section Phdr
2730@subsubsection Output section phdr
2731@kindex :@var{phdr}
2732@cindex section, assigning to program header
2733@cindex program headers and sections
2734You can assign a section to a previously defined program segment by
2735using @samp{:@var{phdr}}. @xref{PHDRS}. If a section is assigned to
2736one or more segments, then all subsequent allocated sections will be
2737assigned to those segments as well, unless they use an explicitly
2738@code{:@var{phdr}} modifier. You can use @code{:NONE} to tell the
2739linker to not put the section in any segment at all.
2740
2741Here is a simple example:
2742@smallexample
2743@group
2744PHDRS @{ text PT_LOAD ; @}
2745SECTIONS @{ .text : @{ *(.text) @} :text @}
2746@end group
2747@end smallexample
2748
2749@node Output Section Fill
2750@subsubsection Output section fill
2751@kindex =@var{fillexp}
2752@cindex section fill pattern
2753@cindex fill pattern, entire section
2754You can set the fill pattern for an entire section by using
2755@samp{=@var{fillexp}}. @var{fillexp} is an expression
2756(@pxref{Expressions}). Any otherwise unspecified regions of memory
2757within the output section (for example, gaps left due to the required
2758alignment of input sections) will be filled with the two least
2759significant bytes of the value, repeated as necessary.
2760
2761You can also change the fill value with a @code{FILL} command in the
2762output section commands; see @ref{Output Section Data}.
2763
2764Here is a simple example:
2765@smallexample
2766@group
2767SECTIONS @{ .text : @{ *(.text) @} =0x9090 @}
2768@end group
2769@end smallexample
2770
2771@node Overlay Description
2772@subsection Overlay description
2773@kindex OVERLAY
2774@cindex overlays
2775An overlay description provides an easy way to describe sections which
2776are to be loaded as part of a single memory image but are to be run at
2777the same memory address. At run time, some sort of overlay manager will
2778copy the overlaid sections in and out of the runtime memory address as
2779required, perhaps by simply manipulating addressing bits. This approach
2780can be useful, for example, when a certain region of memory is faster
2781than another.
2782
2783Overlays are described using the @code{OVERLAY} command. The
2784@code{OVERLAY} command is used within a @code{SECTIONS} command, like an
2785output section description. The full syntax of the @code{OVERLAY}
2786command is as follows:
2787@smallexample
2788@group
2789OVERLAY [@var{start}] : [NOCROSSREFS] [AT ( @var{ldaddr} )]
2790 @{
2791 @var{secname1}
2792 @{
2793 @var{output-section-command}
2794 @var{output-section-command}
2795 @dots{}
2796 @} [:@var{phdr}@dots{}] [=@var{fill}]
2797 @var{secname2}
2798 @{
2799 @var{output-section-command}
2800 @var{output-section-command}
2801 @dots{}
2802 @} [:@var{phdr}@dots{}] [=@var{fill}]
2803 @dots{}
2804 @} [>@var{region}] [:@var{phdr}@dots{}] [=@var{fill}]
2805@end group
2806@end smallexample
2807
2808Everything is optional except @code{OVERLAY} (a keyword), and each
2809section must have a name (@var{secname1} and @var{secname2} above). The
2810section definitions within the @code{OVERLAY} construct are identical to
2811those within the general @code{SECTIONS} contruct (@pxref{SECTIONS}),
2812except that no addresses and no memory regions may be defined for
2813sections within an @code{OVERLAY}.
2814
2815The sections are all defined with the same starting address. The load
2816addresses of the sections are arranged such that they are consecutive in
2817memory starting at the load address used for the @code{OVERLAY} as a
2818whole (as with normal section definitions, the load address is optional,
2819and defaults to the start address; the start address is also optional,
2820and defaults to the current value of the location counter).
2821
2822If the @code{NOCROSSREFS} keyword is used, and there any references
2823among the sections, the linker will report an error. Since the sections
2824all run at the same address, it normally does not make sense for one
2825section to refer directly to another. @xref{Miscellaneous Commands,
2826NOCROSSREFS}.
2827
2828For each section within the @code{OVERLAY}, the linker automatically
2829defines two symbols. The symbol @code{__load_start_@var{secname}} is
2830defined as the starting load address of the section. The symbol
2831@code{__load_stop_@var{secname}} is defined as the final load address of
2832the section. Any characters within @var{secname} which are not legal
2833within C identifiers are removed. C (or assembler) code may use these
2834symbols to move the overlaid sections around as necessary.
2835
2836At the end of the overlay, the value of the location counter is set to
2837the start address of the overlay plus the size of the largest section.
2838
2839Here is an example. Remember that this would appear inside a
2840@code{SECTIONS} construct.
2841@smallexample
2842@group
2843 OVERLAY 0x1000 : AT (0x4000)
2844 @{
2845 .text0 @{ o1/*.o(.text) @}
2846 .text1 @{ o2/*.o(.text) @}
2847 @}
2848@end group
2849@end smallexample
2850@noindent
2851This will define both @samp{.text0} and @samp{.text1} to start at
2852address 0x1000. @samp{.text0} will be loaded at address 0x4000, and
2853@samp{.text1} will be loaded immediately after @samp{.text0}. The
2854following symbols will be defined: @code{__load_start_text0},
2855@code{__load_stop_text0}, @code{__load_start_text1},
2856@code{__load_stop_text1}.
2857
2858C code to copy overlay @code{.text1} into the overlay area might look
2859like the following.
2860
2861@smallexample
2862@group
2863 extern char __load_start_text1, __load_stop_text1;
2864 memcpy ((char *) 0x1000, &__load_start_text1,
2865 &__load_stop_text1 - &__load_start_text1);
2866@end group
2867@end smallexample
2868
2869Note that the @code{OVERLAY} command is just syntactic sugar, since
2870everything it does can be done using the more basic commands. The above
2871example could have been written identically as follows.
2872
2873@smallexample
2874@group
2875 .text0 0x1000 : AT (0x4000) @{ o1/*.o(.text) @}
2876 __load_start_text0 = LOADADDR (.text0);
2877 __load_stop_text0 = LOADADDR (.text0) + SIZEOF (.text0);
2878 .text1 0x1000 : AT (0x4000 + SIZEOF (.text0)) @{ o2/*.o(.text) @}
2879 __load_start_text1 = LOADADDR (.text1);
2880 __load_stop_text1 = LOADADDR (.text1) + SIZEOF (.text1);
2881 . = 0x1000 + MAX (SIZEOF (.text0), SIZEOF (.text1));
2882@end group
2883@end smallexample
2884
2885@node MEMORY
2886@section MEMORY command
2887@kindex MEMORY
2888@cindex memory regions
2889@cindex regions of memory
2890@cindex allocating memory
2891@cindex discontinuous memory
2892The linker's default configuration permits allocation of all available
2893memory. You can override this by using the @code{MEMORY} command.
2894
2895The @code{MEMORY} command describes the location and size of blocks of
2896memory in the target. You can use it to describe which memory regions
2897may be used by the linker, and which memory regions it must avoid. You
2898can then assign sections to particular memory regions. The linker will
2899set section addresses based on the memory regions, and will warn about
2900regions that become too full. The linker will not shuffle sections
2901around to fit into the available regions.
2902
2903A linker script may contain at most one use of the @code{MEMORY}
2904command. However, you can define as many blocks of memory within it as
2905you wish. The syntax is:
2906@smallexample
2907@group
2908MEMORY
2909 @{
2910 @var{name} [(@var{attr})] : ORIGIN = @var{origin}, LENGTH = @var{len}
2911 @dots{}
2912 @}
2913@end group
2914@end smallexample
2915
2916The @var{name} is a name used in the linker script to refer to the
2917region. The region name has no meaning outside of the linker script.
2918Region names are stored in a separate name space, and will not conflict
2919with symbol names, file names, or section names. Each memory region
2920must have a distinct name.
2921
2922@cindex memory region attributes
2923The @var{attr} string is an optional list of attributes that specify
2924whether to use a particular memory region for an input section which is
2925not explicitly mapped in the linker script. As described in
2926@ref{SECTIONS}, if you do not specify an output section for some input
2927section, the linker will create an output section with the same name as
2928the input section. If you define region attributes, the linker will use
2929them to select the memory region for the output section that it creates.
2930
2931The @var{attr} string must consist only of the following characters:
2932@table @samp
2933@item R
2934Read-only section
2935@item W
2936Read/write section
2937@item X
2938Executable section
2939@item A
2940Allocatable section
2941@item I
2942Initialized section
2943@item L
2944Same as @samp{I}
2945@item !
2946Invert the sense of any of the preceding attributes
2947@end table
2948
2949If a unmapped section matches any of the listed attributes other than
2950@samp{!}, it will be placed in the memory region. The @samp{!}
2951attribute reverses this test, so that an unmapped section will be placed
2952in the memory region only if it does not match any of the listed
2953attributes.
2954
2955@kindex ORIGIN =
2956@kindex o =
2957@kindex org =
2958The @var{origin} is an expression for the start address of the memory
2959region. The expression must evaluate to a constant before memory
2960allocation is performed, which means that you may not use any section
2961relative symbols. The keyword @code{ORIGIN} may be abbreviated to
2962@code{org} or @code{o} (but not, for example, @code{ORG}).
2963
2964@kindex LENGTH =
2965@kindex len =
2966@kindex l =
2967The @var{len} is an expression for the size in bytes of the memory
2968region. As with the @var{origin} expression, the expression must
2969evaluate to a constant before memory allocation is performed. The
2970keyword @code{LENGTH} may be abbreviated to @code{len} or @code{l}.
2971
2972In the following example, we specify that there are two memory regions
2973available for allocation: one starting at @samp{0} for 256 kilobytes,
2974and the other starting at @samp{0x40000000} for four megabytes. The
2975linker will place into the @samp{rom} memory region every section which
2976is not explicitly mapped into a memory region, and is either read-only
2977or executable. The linker will place other sections which are not
2978explicitly mapped into a memory region into the @samp{ram} memory
2979region.
2980
2981@smallexample
2982@group
2983MEMORY
2984 @{
2985 rom (rx) : ORIGIN = 0, LENGTH = 256K
2986 ram (!rx) : org = 0x40000000, l = 4M
2987 @}
2988@end group
2989@end smallexample
2990
2991Once you define a memory region, you can direct the linker to place
2992specific output sections into that memory region by using the
2993@samp{>@var{region}} output section attribute. For example, if you have
2994a memory region named @samp{mem}, you would use @samp{>mem} in the
2995output section definition. @xref{Output Section Region}. If no address
2996was specified for the output section, the linker will set the address to
2997the next available address within the memory region. If the combined
2998output sections directed to a memory region are too large for the
2999region, the linker will issue an error message.
3000
3001@node PHDRS
3002@section PHDRS Command
3003@kindex PHDRS
3004@cindex program headers
3005@cindex ELF program headers
3006@cindex program segments
3007@cindex segments, ELF
3008The ELF object file format uses @dfn{program headers}, also knows as
3009@dfn{segments}. The program headers describe how the program should be
3010loaded into memory. You can print them out by using the @code{objdump}
3011program with the @samp{-p} option.
3012
3013When you run an ELF program on a native ELF system, the system loader
3014reads the program headers in order to figure out how to load the
3015program. This will only work if the program headers are set correctly.
3016This manual does not describe the details of how the system loader
3017interprets program headers; for more information, see the ELF ABI.
3018
3019The linker will create reasonable program headers by default. However,
3020in some cases, you may need to specify the program headers more
3021precisely. You may use the @code{PHDRS} command for this purpose. When
3022the linker sees the @code{PHDRS} command in the linker script, it will
3023not create any program headers other than the ones specified.
3024
3025The linker only pays attention to the @code{PHDRS} command when
3026generating an ELF output file. In other cases, the linker will simply
3027ignore @code{PHDRS}.
3028
3029This is the syntax of the @code{PHDRS} command. The words @code{PHDRS},
3030@code{FILEHDR}, @code{AT}, and @code{FLAGS} are keywords.
3031
3032@smallexample
3033@group
3034PHDRS
3035@{
3036 @var{name} @var{type} [ FILEHDR ] [ PHDRS ] [ AT ( @var{address} ) ]
3037 [ FLAGS ( @var{flags} ) ] ;
3038@}
3039@end group
3040@end smallexample
3041
3042The @var{name} is used only for reference in the @code{SECTIONS} command
3043of the linker script. It is not put into the output file. Program
3044header names are stored in a separate name space, and will not conflict
3045with symbol names, file names, or section names. Each program header
3046must have a distinct name.
3047
3048Certain program header types describe segments of memory which the
3049system loader will load from the file. In the linker script, you
3050specify the contents of these segments by placing allocatable output
3051sections in the segments. You use the @samp{:@var{phdr}} output section
3052attribute to place a section in a particular segment. @xref{Output
3053Section Phdr}.
3054
3055It is normal to put certain sections in more than one segment. This
3056merely implies that one segment of memory contains another. You may
3057repeat @samp{:@var{phdr}}, using it once for each segment which should
3058contain the section.
3059
3060If you place a section in one or more segments using @samp{:@var{phdr}},
3061then the linker will place all subsequent allocatable sections which do
3062not specify @samp{:@var{phdr}} in the same segments. This is for
3063convenience, since generally a whole set of contiguous sections will be
3064placed in a single segment. You can use @code{:NONE} to override the
3065default segment and tell the linker to not put the section in any
3066segment at all.
3067
3068@kindex FILEHDR
3069@kindex PHDRS
3070You may use the @code{FILEHDR} and @code{PHDRS} keywords appear after
3071the program header type to further describe the contents of the segment.
3072The @code{FILEHDR} keyword means that the segment should include the ELF
3073file header. The @code{PHDRS} keyword means that the segment should
3074include the ELF program headers themselves.
3075
3076The @var{type} may be one of the following. The numbers indicate the
3077value of the keyword.
3078
3079@table @asis
3080@item @code{PT_NULL} (0)
3081Indicates an unused program header.
3082
3083@item @code{PT_LOAD} (1)
3084Indicates that this program header describes a segment to be loaded from
3085the file.
3086
3087@item @code{PT_DYNAMIC} (2)
3088Indicates a segment where dynamic linking information can be found.
3089
3090@item @code{PT_INTERP} (3)
3091Indicates a segment where the name of the program interpreter may be
3092found.
3093
3094@item @code{PT_NOTE} (4)
3095Indicates a segment holding note information.
3096
3097@item @code{PT_SHLIB} (5)
3098A reserved program header type, defined but not specified by the ELF
3099ABI.
3100
3101@item @code{PT_PHDR} (6)
3102Indicates a segment where the program headers may be found.
3103
3104@item @var{expression}
3105An expression giving the numeric type of the program header. This may
3106be used for types not defined above.
3107@end table
3108
3109You can specify that a segment should be loaded at a particular address
3110in memory by using an @code{AT} expression. This is identical to the
3111@code{AT} command used as an output section attribute (@pxref{Output
3112Section LMA}). The @code{AT} command for a program header overrides the
3113output section attribute.
3114
3115The linker will normally set the segment flags based on the sections
3116which comprise the segment. You may use the @code{FLAGS} keyword to
3117explicitly specify the segment flags. The value of @var{flags} must be
3118an integer. It is used to set the @code{p_flags} field of the program
3119header.
3120
3121Here is an example of @code{PHDRS}. This shows a typical set of program
3122headers used on a native ELF system.
3123
3124@example
3125@group
3126PHDRS
3127@{
3128 headers PT_PHDR PHDRS ;
3129 interp PT_INTERP ;
3130 text PT_LOAD FILEHDR PHDRS ;
3131 data PT_LOAD ;
3132 dynamic PT_DYNAMIC ;
3133@}
3134
3135SECTIONS
3136@{
3137 . = SIZEOF_HEADERS;
3138 .interp : @{ *(.interp) @} :text :interp
3139 .text : @{ *(.text) @} :text
3140 .rodata : @{ *(.rodata) @} /* defaults to :text */
3141 @dots{}
3142 . = . + 0x1000; /* move to a new page in memory */
3143 .data : @{ *(.data) @} :data
3144 .dynamic : @{ *(.dynamic) @} :data :dynamic
3145 @dots{}
3146@}
3147@end group
3148@end example
3149
3150@node VERSION
3151@section VERSION Command
3152@kindex VERSION @{script text@}
3153@cindex symbol versions
3154@cindex version script
3155@cindex versions of symbols
3156The linker supports symbol versions when using ELF. Symbol versions are
3157only useful when using shared libraries. The dynamic linker can use
3158symbol versions to select a specific version of a function when it runs
3159a program that may have been linked against an earlier version of the
3160shared library.
3161
3162You can include a version script directly in the main linker script, or
3163you can supply the version script as an implicit linker script. You can
3164also use the @samp{--version-script} linker option.
3165
3166The syntax of the @code{VERSION} command is simply
3167@smallexample
3168VERSION @{ version-script-commands @}
3169@end smallexample
3170
3171The format of the version script commands is identical to that used by
3172Sun's linker in Solaris 2.5. The version script defines a tree of
3173version nodes. You specify the node names and interdependencies in the
3174version script. You can specify which symbols are bound to which
3175version nodes, and you can reduce a specified set of symbols to local
3176scope so that they are not globally visible outside of the shared
3177library.
3178
3179The easiest way to demonstrate the version script language is with a few
3180examples.
3181
3182@smallexample
3183VERS_1.1 @{
3184 global:
3185 foo1;
3186 local:
3187 old*;
3188 original*;
3189 new*;
3190@};
3191
3192VERS_1.2 @{
3193 foo2;
3194@} VERS_1.1;
3195
3196VERS_2.0 @{
3197 bar1; bar2;
3198@} VERS_1.2;
3199@end smallexample
3200
3201This example version script defines three version nodes. The first
3202version node defined is @samp{VERS_1.1}; it has no other dependencies.
3203The script binds the symbol @samp{foo1} to @samp{VERS_1.1}. It reduces
3204a number of symbols to local scope so that they are not visible outside
3205of the shared library.
3206
3207Next, the version script defines node @samp{VERS_1.2}. This node
3208depends upon @samp{VERS_1.1}. The script binds the symbol @samp{foo2}
3209to the version node @samp{VERS_1.2}.
3210
3211Finally, the version script defines node @samp{VERS_2.0}. This node
3212depends upon @samp{VERS_1.2}. The scripts binds the symbols @samp{bar1}
3213and @samp{bar2} are bound to the version node @samp{VERS_2.0}.
3214
3215When the linker finds a symbol defined in a library which is not
3216specifically bound to a version node, it will effectively bind it to an
3217unspecified base version of the library. You can bind all otherwise
3218unspecified symbols to a given version node by using @samp{global: *}
3219somewhere in the version script.
3220
3221The names of the version nodes have no specific meaning other than what
3222they might suggest to the person reading them. The @samp{2.0} version
3223could just as well have appeared in between @samp{1.1} and @samp{1.2}.
3224However, this would be a confusing way to write a version script.
3225
3226When you link an application against a shared library that has versioned
3227symbols, the application itself knows which version of each symbol it
3228requires, and it also knows which version nodes it needs from each
3229shared library it is linked against. Thus at runtime, the dynamic
3230loader can make a quick check to make sure that the libraries you have
3231linked against do in fact supply all of the version nodes that the
3232application will need to resolve all of the dynamic symbols. In this
3233way it is possible for the dynamic linker to know with certainty that
3234all external symbols that it needs will be resolvable without having to
3235search for each symbol reference.
3236
3237The symbol versioning is in effect a much more sophisticated way of
3238doing minor version checking that SunOS does. The fundamental problem
3239that is being addressed here is that typically references to external
3240functions are bound on an as-needed basis, and are not all bound when
3241the application starts up. If a shared library is out of date, a
3242required interface may be missing; when the application tries to use
3243that interface, it may suddenly and unexpectedly fail. With symbol
3244versioning, the user will get a warning when they start their program if
3245the libraries being used with the application are too old.
3246
3247There are several GNU extensions to Sun's versioning approach. The
3248first of these is the ability to bind a symbol to a version node in the
3249source file where the symbol is defined instead of in the versioning
3250script. This was done mainly to reduce the burden on the library
3251maintainer. You can do this by putting something like:
3252@smallexample
3253__asm__(".symver original_foo,foo@@VERS_1.1");
3254@end smallexample
3255@noindent
3256in the C source file. This renames the function @samp{original_foo} to
3257be an alias for @samp{foo} bound to the version node @samp{VERS_1.1}.
3258The @samp{local:} directive can be used to prevent the symbol
3259@samp{original_foo} from being exported.
3260
3261The second GNU extension is to allow multiple versions of the same
3262function to appear in a given shared library. In this way you can make
3263an incompatible change to an interface without increasing the major
3264version number of the shared library, while still allowing applications
3265linked against the old interface to continue to function.
3266
3267To do this, you must use multiple @samp{.symver} directives in the
3268source file. Here is an example:
3269
3270@smallexample
3271__asm__(".symver original_foo,foo@@");
3272__asm__(".symver old_foo,foo@@VERS_1.1");
3273__asm__(".symver old_foo1,foo@@VERS_1.2");
3274__asm__(".symver new_foo,foo@@@@VERS_2.0");
3275@end smallexample
3276
3277In this example, @samp{foo@@} represents the symbol @samp{foo} bound to the
3278unspecified base version of the symbol. The source file that contains this
3279example would define 4 C functions: @samp{original_foo}, @samp{old_foo},
3280@samp{old_foo1}, and @samp{new_foo}.
3281
3282When you have multiple definitions of a given symbol, there needs to be
3283some way to specify a default version to which external references to
3284this symbol will be bound. You can do this with the
3285@samp{foo@@@@VERS_2.0} type of @samp{.symver} directive. You can only
3286declare one version of a symbol as the default in this manner; otherwise
3287you would effectively have multiple definitions of the same symbol.
3288
3289If you wish to bind a reference to a specific version of the symbol
3290within the shared library, you can use the aliases of convenience
3291(i.e. @samp{old_foo}), or you can use the @samp{.symver} directive to
3292specifically bind to an external version of the function in question.
3293
3294@node Expressions
3295@section Expressions in Linker Scripts
3296@cindex expressions
3297@cindex arithmetic
3298The syntax for expressions in the linker script language is identical to
3299that of C expressions. All expressions are evaluated as integers. All
3300expressions are evaluated in the same size, which is 32 bits if both the
3301host and target are 32 bits, and is otherwise 64 bits.
3302
3303You can use and set symbol values in expressions.
3304
3305The linker defines several special purpose builtin functions for use in
3306expressions.
3307
3308@menu
3309* Constants:: Constants
3310* Symbols:: Symbol Names
3311* Location Counter:: The Location Counter
3312* Operators:: Operators
3313* Evaluation:: Evaluation
3314* Expression Section:: The Section of an Expression
3315* Builtin Functions:: Builtin Functions
3316@end menu
3317
3318@node Constants
3319@subsection Constants
3320@cindex integer notation
3321@cindex constants in linker scripts
3322All constants are integers.
3323
3324As in C, the linker considers an integer beginning with @samp{0} to be
3325octal, and an integer beginning with @samp{0x} or @samp{0X} to be
3326hexadecimal. The linker considers other integers to be decimal.
3327
3328@cindex scaled integers
3329@cindex K and M integer suffixes
3330@cindex M and K integer suffixes
3331@cindex suffixes for integers
3332@cindex integer suffixes
3333In addition, you can use the suffixes @code{K} and @code{M} to scale a
3334constant by
3335@c TEXI2ROFF-KILL
3336@ifinfo
3337@c END TEXI2ROFF-KILL
3338@code{1024} or @code{1024*1024}
3339@c TEXI2ROFF-KILL
3340@end ifinfo
3341@tex
3342${\rm 1024}$ or ${\rm 1024}^2$
3343@end tex
3344@c END TEXI2ROFF-KILL
3345respectively. For example, the following all refer to the same quantity:
3346@smallexample
3347 _fourk_1 = 4K;
3348 _fourk_2 = 4096;
3349 _fourk_3 = 0x1000;
3350@end smallexample
3351
3352@node Symbols
3353@subsection Symbol Names
3354@cindex symbol names
3355@cindex names
3356@cindex quoted symbol names
3357@kindex "
3358Unless quoted, symbol names start with a letter, underscore, or period
3359and may include letters, digits, underscores, periods, and hyphens.
3360Unquoted symbol names must not conflict with any keywords. You can
3361specify a symbol which contains odd characters or has the same name as a
3362keyword by surrounding the symbol name in double quotes:
3363@smallexample
3364 "SECTION" = 9;
3365 "with a space" = "also with a space" + 10;
3366@end smallexample
3367
3368Since symbols can contain many non-alphabetic characters, it is safest
3369to delimit symbols with spaces. For example, @samp{A-B} is one symbol,
3370whereas @samp{A - B} is an expression involving subtraction.
3371
3372@node Location Counter
3373@subsection The Location Counter
3374@kindex .
3375@cindex dot
3376@cindex location counter
3377@cindex current output location
3378The special linker variable @dfn{dot} @samp{.} always contains the
3379current output location counter. Since the @code{.} always refers to a
3380location in an output section, it may only appear in an expression
3381within a @code{SECTIONS} command. The @code{.} symbol may appear
3382anywhere that an ordinary symbol is allowed in an expression.
3383
3384@cindex holes
3385Assigning a value to @code{.} will cause the location counter to be
3386moved. This may be used to create holes in the output section. The
3387location counter may never be moved backwards.
3388
3389@smallexample
3390SECTIONS
3391@{
3392 output :
3393 @{
3394 file1(.text)
3395 . = . + 1000;
3396 file2(.text)
3397 . += 1000;
3398 file3(.text)
3399 @} = 0x1234;
3400@}
3401@end smallexample
3402@noindent
3403In the previous example, the @samp{.text} section from @file{file1} is
3404located at the beginning of the output section @samp{output}. It is
3405followed by a 1000 byte gap. Then the @samp{.text} section from
3406@file{file2} appears, also with a 1000 byte gap following before the
3407@samp{.text} section from @file{file3}. The notation @samp{= 0x1234}
3408specifies what data to write in the gaps (@pxref{Output Section Fill}).
3409
3410@need 2000
3411@node Operators
3412@subsection Operators
3413@cindex operators for arithmetic
3414@cindex arithmetic operators
3415@cindex precedence in expressions
3416The linker recognizes the standard C set of arithmetic operators, with
3417the standard bindings and precedence levels:
3418@c TEXI2ROFF-KILL
3419@ifinfo
3420@c END TEXI2ROFF-KILL
3421@smallexample
3422precedence associativity Operators Notes
3423(highest)
34241 left ! - ~ (1)
34252 left * / %
34263 left + -
34274 left >> <<
34285 left == != > < <= >=
34296 left &
34307 left |
34318 left &&
34329 left ||
343310 right ? :
343411 right &= += -= *= /= (2)
3435(lowest)
3436@end smallexample
3437Notes:
3438(1) Prefix operators
3439(2) @xref{Assignments}.
3440@c TEXI2ROFF-KILL
3441@end ifinfo
3442@tex
3443\vskip \baselineskip
3444%"lispnarrowing" is the extra indent used generally for smallexample
3445\hskip\lispnarrowing\vbox{\offinterlineskip
3446\hrule
3447\halign
3448{\vrule#&\strut\hfil\ #\ \hfil&\vrule#&\strut\hfil\ #\ \hfil&\vrule#&\strut\hfil\ {\tt #}\ \hfil&\vrule#\cr
3449height2pt&\omit&&\omit&&\omit&\cr
3450&Precedence&& Associativity &&{\rm Operators}&\cr
3451height2pt&\omit&&\omit&&\omit&\cr
3452\noalign{\hrule}
3453height2pt&\omit&&\omit&&\omit&\cr
3454&highest&&&&&\cr
3455% '176 is tilde, '~' in tt font
3456&1&&left&&\qquad- \char'176\ !\qquad\dag&\cr
3457&2&&left&&* / \%&\cr
3458&3&&left&&+ -&\cr
3459&4&&left&&>> <<&\cr
3460&5&&left&&== != > < <= >=&\cr
3461&6&&left&&\&&\cr
3462&7&&left&&|&\cr
3463&8&&left&&{\&\&}&\cr
3464&9&&left&&||&\cr
3465&10&&right&&? :&\cr
3466&11&&right&&\qquad\&= += -= *= /=\qquad\ddag&\cr
3467&lowest&&&&&\cr
3468height2pt&\omit&&\omit&&\omit&\cr}
3469\hrule}
3470@end tex
3471@iftex
3472{
3473@obeylines@parskip=0pt@parindent=0pt
3474@dag@quad Prefix operators.
3475@ddag@quad @xref{Assignments}.
3476}
3477@end iftex
3478@c END TEXI2ROFF-KILL
3479
3480@node Evaluation
3481@subsection Evaluation
3482@cindex lazy evaluation
3483@cindex expression evaluation order
3484The linker evaluates expressions lazily. It only computes the value of
3485an expression when absolutely necessary.
3486
3487The linker needs some information, such as the value of the start
3488address of the first section, and the origins and lengths of memory
3489regions, in order to do any linking at all. These values are computed
3490as soon as possible when the linker reads in the linker script.
3491
3492However, other values (such as symbol values) are not known or needed
3493until after storage allocation. Such values are evaluated later, when
3494other information (such as the sizes of output sections) is available
3495for use in the symbol assignment expression.
3496
3497The sizes of sections cannot be known until after allocation, so
3498assignments dependent upon these are not performed until after
3499allocation.
3500
3501Some expressions, such as those depending upon the location counter
3502@samp{.}, must be evaluated during section allocation.
3503
3504If the result of an expression is required, but the value is not
3505available, then an error results. For example, a script like the
3506following
3507@smallexample
3508@group
3509SECTIONS
3510 @{
3511 .text 9+this_isnt_constant :
3512 @{ *(.text) @}
3513 @}
3514@end group
3515@end smallexample
3516@noindent
3517will cause the error message @samp{non constant expression for initial
3518address}.
3519
3520@node Expression Section
3521@subsection The Section of an Expression
3522@cindex expression sections
3523@cindex absolute expressions
3524@cindex relative expressions
3525@cindex absolute and relocatable symbols
3526@cindex relocatable and absolute symbols
3527@cindex symbols, relocatable and absolute
3528When the linker evaluates an expression, the result is either absolute
3529or relative to some section. A relative expression is expressed as a
3530fixed offset from the base of a section.
3531
3532The position of the expression within the linker script determines
3533whether it is absolute or relative. An expression which appears within
3534an output section definition is relative to the base of the output
3535section. An expression which appears elsewhere will be absolute.
3536
3537A symbol set to a relative expression will be relocatable if you request
3538relocatable output using the @samp{-r} option. That means that a
3539further link operation may change the value of the symbol. The symbol's
3540section will be the section of the relative expression.
3541
3542A symbol set to an absolute expression will retain the same value
3543through any further link operation. The symbol will be absolute, and
3544will not have any particular associated section.
3545
3546You can use the builtin function @code{ABSOLUTE} to force an expression
3547to be absolute when it would otherwise be relative. For example, to
3548create an absolute symbol set to the address of the end of the output
3549section @samp{.data}:
3550@smallexample
3551SECTIONS
3552 @{
3553 .data : @{ *(.data) _edata = ABSOLUTE(.); @}
3554 @}
3555@end smallexample
3556@noindent
3557If @samp{ABSOLUTE} were not used, @samp{_edata} would be relative to the
3558@samp{.data} section.
3559
3560@node Builtin Functions
3561@subsection Builtin Functions
3562@cindex functions in expressions
3563The linker script language includes a number of builtin functions for
3564use in linker script expressions.
3565
3566@table @code
3567@item ABSOLUTE(@var{exp})
3568@kindex ABSOLUTE(@var{exp})
3569@cindex expression, absolute
3570Return the absolute (non-relocatable, as opposed to non-negative) value
3571of the expression @var{exp}. Primarily useful to assign an absolute
3572value to a symbol within a section definition, where symbol values are
3573normally section relative. @xref{Expression Section}.
3574
3575@item ADDR(@var{section})
3576@kindex ADDR(@var{section})
3577@cindex section address in expression
3578Return the absolute address (the VMA) of the named @var{section}. Your
3579script must previously have defined the location of that section. In
3580the following example, @code{symbol_1} and @code{symbol_2} are assigned
3581identical values:
3582@smallexample
3583@group
3584SECTIONS @{ @dots{}
3585 .output1 :
3586 @{
3587 start_of_output_1 = ABSOLUTE(.);
3588 @dots{}
3589 @}
3590 .output :
3591 @{
3592 symbol_1 = ADDR(.output1);
3593 symbol_2 = start_of_output_1;
3594 @}
3595@dots{} @}
3596@end group
3597@end smallexample
3598
3599@item ALIGN(@var{exp})
3600@kindex ALIGN(@var{exp})
3601@cindex round up location counter
3602@cindex align location counter
3603Return the location counter (@code{.}) aligned to the next @var{exp}
3604boundary. @var{exp} must be an expression whose value is a power of
3605two. This is equivalent to
3606@smallexample
3607(. + @var{exp} - 1) & ~(@var{exp} - 1)
3608@end smallexample
3609
3610@code{ALIGN} doesn't change the value of the location counter---it just
3611does arithmetic on it. Here is an example which aligns the output
3612@code{.data} section to the next @code{0x2000} byte boundary after the
3613preceding section and sets a variable within the section to the next
3614@code{0x8000} boundary after the input sections:
3615@smallexample
3616@group
3617SECTIONS @{ @dots{}
3618 .data ALIGN(0x2000): @{
3619 *(.data)
3620 variable = ALIGN(0x8000);
3621 @}
3622@dots{} @}
3623@end group
3624@end smallexample
3625@noindent
3626The first use of @code{ALIGN} in this example specifies the location of
3627a section because it is used as the optional @var{address} attribute of
3628a section definition (@pxref{Output Section Address}). The second use
3629of @code{ALIGN} is used to defines the value of a symbol.
3630
3631The builtin function @code{NEXT} is closely related to @code{ALIGN}.
3632
3633@item BLOCK(@var{exp})
3634@kindex BLOCK(@var{exp})
3635This is a synonym for @code{ALIGN}, for compatibility with older linker
3636scripts. It is most often seen when setting the address of an output
3637section.
3638
3639@item DEFINED(@var{symbol})
3640@kindex DEFINED(@var{symbol})
3641@cindex symbol defaults
3642Return 1 if @var{symbol} is in the linker global symbol table and is
3643defined, otherwise return 0. You can use this function to provide
3644default values for symbols. For example, the following script fragment
3645shows how to set a global symbol @samp{begin} to the first location in
3646the @samp{.text} section---but if a symbol called @samp{begin} already
3647existed, its value is preserved:
3648
3649@smallexample
3650@group
3651SECTIONS @{ @dots{}
3652 .text : @{
3653 begin = DEFINED(begin) ? begin : . ;
3654 @dots{}
3655 @}
3656 @dots{}
3657@}
3658@end group
3659@end smallexample
3660
3661@item LOADADDR(@var{section})
3662@kindex LOADADDR(@var{section})
3663@cindex section load address in expression
3664Return the absolute LMA of the named @var{section}. This is normally
3665the same as @code{ADDR}, but it may be different if the @code{AT}
3666attribute is used in the output section definition (@pxref{Output
3667Section LMA}).
3668
3669@kindex MAX
3670@item MAX(@var{exp1}, @var{exp2})
3671Returns the maximum of @var{exp1} and @var{exp2}.
3672
3673@kindex MIN
3674@item MIN(@var{exp1}, @var{exp2})
3675Returns the minimum of @var{exp1} and @var{exp2}.
3676
3677@item NEXT(@var{exp})
3678@kindex NEXT(@var{exp})
3679@cindex unallocated address, next
3680Return the next unallocated address that is a multiple of @var{exp}.
3681This function is closely related to @code{ALIGN(@var{exp})}; unless you
3682use the @code{MEMORY} command to define discontinuous memory for the
3683output file, the two functions are equivalent.
3684
3685@item SIZEOF(@var{section})
3686@kindex SIZEOF(@var{section})
3687@cindex section size
3688Return the size in bytes of the named @var{section}, if that section has
3689been allocated. If the section has not been allocated when this is
3690evaluated, the linker will report an error. In the following example,
3691@code{symbol_1} and @code{symbol_2} are assigned identical values:
3692@smallexample
3693@group
3694SECTIONS@{ @dots{}
3695 .output @{
3696 .start = . ;
3697 @dots{}
3698 .end = . ;
3699 @}
3700 symbol_1 = .end - .start ;
3701 symbol_2 = SIZEOF(.output);
3702@dots{} @}
3703@end group
3704@end smallexample
3705
3706@item SIZEOF_HEADERS
3707@itemx sizeof_headers
3708@kindex SIZEOF_HEADERS
3709@cindex header size
3710Return the size in bytes of the output file's headers. This is
3711information which appears at the start of the output file. You can use
3712this number when setting the start address of the first section, if you
3713choose, to facilitate paging.
3714
3715@cindex not enough room for program headers
3716@cindex program headers, not enough room
3717When producing an ELF output file, if the linker script uses the
3718@code{SIZEOF_HEADERS} builtin function, the linker must compute the
3719number of program headers before it has determined all the section
3720addresses and sizes. If the linker later discovers that it needs
3721additional program headers, it will report an error @samp{not enough
3722room for program headers}. To avoid this error, you must avoid using
3723the @code{SIZEOF_HEADERS} function, or you must rework your linker
3724script to avoid forcing the linker to use additional program headers, or
3725you must define the program headers yourself using the @code{PHDRS}
3726command (@pxref{PHDRS}).
3727@end table
3728
3729@node Implicit Linker Scripts
3730@section Implicit Linker Scripts
3731@cindex implicit linker scripts
3732If you specify a linker input file which the linker can not recognize as
3733an object file or an archive file, it will try to read the file as a
3734linker script. If the file can not be parsed as a linker script, the
3735linker will report an error.
3736
3737An implicit linker script will not replace the default linker script.
3738
3739Typically an implicit linker script would contain only symbol
3740assignments, or the @code{INPUT}, @code{GROUP}, or @code{VERSION}
3741commands.
3742
3743Any input files read because of an implicit linker script will be read
3744at the position in the command line where the implicit linker script was
3745read. This can affect archive searching.
3746
3747@ifset GENERIC
3748@node Machine Dependent
3749@chapter Machine Dependent Features
3750
3751@cindex machine dependencies
3752@code{ld} has additional features on some platforms; the following
3753sections describe them. Machines where @code{ld} has no additional
3754functionality are not listed.
3755
3756@menu
3757* H8/300:: @code{ld} and the H8/300
3758* i960:: @code{ld} and the Intel 960 family
3759* ARM:: @code{ld} and the ARM family
3760@end menu
3761@end ifset
3762
3763@c FIXME! This could use @raisesections/@lowersections, but there seems to be a conflict
3764@c between those and node-defaulting.
3765@ifset H8300
3766@ifclear GENERIC
3767@raisesections
3768@end ifclear
3769
3770@node H8/300
3771@section @code{ld} and the H8/300
3772
3773@cindex H8/300 support
3774For the H8/300, @code{ld} can perform these global optimizations when
3775you specify the @samp{--relax} command-line option.
3776
3777@table @emph
3778@cindex relaxing on H8/300
3779@item relaxing address modes
3780@code{ld} finds all @code{jsr} and @code{jmp} instructions whose
3781targets are within eight bits, and turns them into eight-bit
3782program-counter relative @code{bsr} and @code{bra} instructions,
3783respectively.
3784
3785@cindex synthesizing on H8/300
3786@item synthesizing instructions
3787@c FIXME: specifically mov.b, or any mov instructions really?
3788@code{ld} finds all @code{mov.b} instructions which use the
3789sixteen-bit absolute address form, but refer to the top
3790page of memory, and changes them to use the eight-bit address form.
3791(That is: the linker turns @samp{mov.b @code{@@}@var{aa}:16} into
3792@samp{mov.b @code{@@}@var{aa}:8} whenever the address @var{aa} is in the
3793top page of memory).
3794@end table
3795
3796@ifclear GENERIC
3797@lowersections
3798@end ifclear
3799@end ifset
3800
3801@ifclear GENERIC
3802@ifset Hitachi
3803@c This stuff is pointless to say unless you're especially concerned
3804@c with Hitachi chips; don't enable it for generic case, please.
3805@node Hitachi
3806@chapter @code{ld} and other Hitachi chips
3807
3808@code{ld} also supports the H8/300H, the H8/500, and the Hitachi SH. No
3809special features, commands, or command-line options are required for
3810these chips.
3811@end ifset
3812@end ifclear
3813
3814@ifset I960
3815@ifclear GENERIC
3816@raisesections
3817@end ifclear
3818
3819@node i960
3820@section @code{ld} and the Intel 960 family
3821
3822@cindex i960 support
3823
3824You can use the @samp{-A@var{architecture}} command line option to
3825specify one of the two-letter names identifying members of the 960
3826family; the option specifies the desired output target, and warns of any
3827incompatible instructions in the input files. It also modifies the
3828linker's search strategy for archive libraries, to support the use of
3829libraries specific to each particular architecture, by including in the
3830search loop names suffixed with the string identifying the architecture.
3831
3832For example, if your @code{ld} command line included @w{@samp{-ACA}} as
3833well as @w{@samp{-ltry}}, the linker would look (in its built-in search
3834paths, and in any paths you specify with @samp{-L}) for a library with
3835the names
3836
3837@smallexample
3838@group
3839try
3840libtry.a
3841tryca
3842libtryca.a
3843@end group
3844@end smallexample
3845
3846@noindent
3847The first two possibilities would be considered in any event; the last
3848two are due to the use of @w{@samp{-ACA}}.
3849
3850You can meaningfully use @samp{-A} more than once on a command line, since
3851the 960 architecture family allows combination of target architectures; each
3852use will add another pair of name variants to search for when @w{@samp{-l}}
3853specifies a library.
3854
3855@cindex @code{--relax} on i960
3856@cindex relaxing on i960
3857@code{ld} supports the @samp{--relax} option for the i960 family. If
3858you specify @samp{--relax}, @code{ld} finds all @code{balx} and
3859@code{calx} instructions whose targets are within 24 bits, and turns
3860them into 24-bit program-counter relative @code{bal} and @code{cal}
3861instructions, respectively. @code{ld} also turns @code{cal}
3862instructions into @code{bal} instructions when it determines that the
3863target subroutine is a leaf routine (that is, the target subroutine does
3864not itself call any subroutines).
3865
3866@ifclear GENERIC
3867@lowersections
3868@end ifclear
3869@end ifset
3870
3871@ifclear GENERIC
3872@raisesections
3873@end ifclear
3874
3875@node ARM
3876@section @code{ld}'s support for interworking between ARM and Thumb code
3877
3878@cindex ARM interworking support
3879@cindex --support-old-code
3880For the ARM, @code{ld} will generate code stubs to allow functions calls
3881betweem ARM and Thumb code. These stubs only work with code that has
3882been compiled and assembled with the @samp{-mthumb-interwork} command
3883line option. If it is necessary to link with old ARM object files or
3884libraries, which have not been compiled with the -mthumb-interwork
3885option then the @samp{--support-old-code} command line switch should be
3886given to the linker. This will make it generate larger stub functions
3887which will work with non-interworking aware ARM code. Note, however,
3888the linker does not support generating stubs for function calls to
3889non-interworking aware Thumb code.
3890
3891@ifclear GENERIC
3892@lowersections
3893@end ifclear
3894
3895@ifclear SingleFormat
3896@node BFD
3897@chapter BFD
3898
3899@cindex back end
3900@cindex object file management
3901@cindex object formats available
3902@kindex objdump -i
3903The linker accesses object and archive files using the BFD libraries.
3904These libraries allow the linker to use the same routines to operate on
3905object files whatever the object file format. A different object file
3906format can be supported simply by creating a new BFD back end and adding
3907it to the library. To conserve runtime memory, however, the linker and
3908associated tools are usually configured to support only a subset of the
3909object file formats available. You can use @code{objdump -i}
3910(@pxref{objdump,,objdump,binutils.info,The GNU Binary Utilities}) to
3911list all the formats available for your configuration.
3912
3913@cindex BFD requirements
3914@cindex requirements for BFD
3915As with most implementations, BFD is a compromise between
3916several conflicting requirements. The major factor influencing
3917BFD design was efficiency: any time used converting between
3918formats is time which would not have been spent had BFD not
3919been involved. This is partly offset by abstraction payback; since
3920BFD simplifies applications and back ends, more time and care
3921may be spent optimizing algorithms for a greater speed.
3922
3923One minor artifact of the BFD solution which you should bear in
3924mind is the potential for information loss. There are two places where
3925useful information can be lost using the BFD mechanism: during
3926conversion and during output. @xref{BFD information loss}.
3927
3928@menu
3929* BFD outline:: How it works: an outline of BFD
3930@end menu
3931
3932@node BFD outline
3933@section How it works: an outline of BFD
3934@cindex opening object files
3935@include bfdsumm.texi
3936@end ifclear
3937
3938@node Reporting Bugs
3939@chapter Reporting Bugs
3940@cindex bugs in @code{ld}
3941@cindex reporting bugs in @code{ld}
3942
3943Your bug reports play an essential role in making @code{ld} reliable.
3944
3945Reporting a bug may help you by bringing a solution to your problem, or
3946it may not. But in any case the principal function of a bug report is
3947to help the entire community by making the next version of @code{ld}
3948work better. Bug reports are your contribution to the maintenance of
3949@code{ld}.
3950
3951In order for a bug report to serve its purpose, you must include the
3952information that enables us to fix the bug.
3953
3954@menu
3955* Bug Criteria:: Have you found a bug?
3956* Bug Reporting:: How to report bugs
3957@end menu
3958
3959@node Bug Criteria
3960@section Have you found a bug?
3961@cindex bug criteria
3962
3963If you are not sure whether you have found a bug, here are some guidelines:
3964
3965@itemize @bullet
3966@cindex fatal signal
3967@cindex linker crash
3968@cindex crash of linker
3969@item
3970If the linker gets a fatal signal, for any input whatever, that is a
3971@code{ld} bug. Reliable linkers never crash.
3972
3973@cindex error on valid input
3974@item
3975If @code{ld} produces an error message for valid input, that is a bug.
3976
3977@cindex invalid input
3978@item
3979If @code{ld} does not produce an error message for invalid input, that
3980may be a bug. In the general case, the linker can not verify that
3981object files are correct.
3982
3983@item
3984If you are an experienced user of linkers, your suggestions for
3985improvement of @code{ld} are welcome in any case.
3986@end itemize
3987
3988@node Bug Reporting
3989@section How to report bugs
3990@cindex bug reports
3991@cindex @code{ld} bugs, reporting
3992
3993A number of companies and individuals offer support for @sc{gnu}
3994products. If you obtained @code{ld} from a support organization, we
3995recommend you contact that organization first.
3996
3997You can find contact information for many support companies and
3998individuals in the file @file{etc/SERVICE} in the @sc{gnu} Emacs
3999distribution.
4000
4001Otherwise, send bug reports for @code{ld} to
4002@samp{bug-gnu-utils@@gnu.org}.
4003
4004The fundamental principle of reporting bugs usefully is this:
4005@strong{report all the facts}. If you are not sure whether to state a
4006fact or leave it out, state it!
4007
4008Often people omit facts because they think they know what causes the
4009problem and assume that some details do not matter. Thus, you might
4010assume that the name of a symbol you use in an example does not matter.
4011Well, probably it does not, but one cannot be sure. Perhaps the bug is
4012a stray memory reference which happens to fetch from the location where
4013that name is stored in memory; perhaps, if the name were different, the
4014contents of that location would fool the linker into doing the right
4015thing despite the bug. Play it safe and give a specific, complete
4016example. That is the easiest thing for you to do, and the most helpful.
4017
4018Keep in mind that the purpose of a bug report is to enable us to fix the bug if
4019it is new to us. Therefore, always write your bug reports on the assumption
4020that the bug has not been reported previously.
4021
4022Sometimes people give a few sketchy facts and ask, ``Does this ring a
4023bell?'' Those bug reports are useless, and we urge everyone to
4024@emph{refuse to respond to them} except to chide the sender to report
4025bugs properly.
4026
4027To enable us to fix the bug, you should include all these things:
4028
4029@itemize @bullet
4030@item
4031The version of @code{ld}. @code{ld} announces it if you start it with
4032the @samp{--version} argument.
4033
4034Without this, we will not know whether there is any point in looking for
4035the bug in the current version of @code{ld}.
4036
4037@item
4038Any patches you may have applied to the @code{ld} source, including any
4039patches made to the @code{BFD} library.
4040
4041@item
4042The type of machine you are using, and the operating system name and
4043version number.
4044
4045@item
4046What compiler (and its version) was used to compile @code{ld}---e.g.
4047``@code{gcc-2.7}''.
4048
4049@item
4050The command arguments you gave the linker to link your example and
4051observe the bug. To guarantee you will not omit something important,
4052list them all. A copy of the Makefile (or the output from make) is
4053sufficient.
4054
4055If we were to try to guess the arguments, we would probably guess wrong
4056and then we might not encounter the bug.
4057
4058@item
4059A complete input file, or set of input files, that will reproduce the
4060bug. It is generally most helpful to send the actual object files,
4061uuencoded if necessary to get them through the mail system. Making them
4062available for anonymous FTP is not as good, but may be the only
4063reasonable choice for large object files.
4064
4065If the source files were assembled using @code{gas} or compiled using
4066@code{gcc}, then it may be OK to send the source files rather than the
4067object files. In this case, be sure to say exactly what version of
4068@code{gas} or @code{gcc} was used to produce the object files. Also say
4069how @code{gas} or @code{gcc} were configured.
4070
4071@item
4072A description of what behavior you observe that you believe is
4073incorrect. For example, ``It gets a fatal signal.''
4074
4075Of course, if the bug is that @code{ld} gets a fatal signal, then we
4076will certainly notice it. But if the bug is incorrect output, we might
4077not notice unless it is glaringly wrong. You might as well not give us
4078a chance to make a mistake.
4079
4080Even if the problem you experience is a fatal signal, you should still
4081say so explicitly. Suppose something strange is going on, such as, your
4082copy of @code{ld} is out of synch, or you have encountered a bug in the
4083C library on your system. (This has happened!) Your copy might crash
4084and ours would not. If you told us to expect a crash, then when ours
4085fails to crash, we would know that the bug was not happening for us. If
4086you had not told us to expect a crash, then we would not be able to draw
4087any conclusion from our observations.
4088
4089@item
4090If you wish to suggest changes to the @code{ld} source, send us context
4091diffs, as generated by @code{diff} with the @samp{-u}, @samp{-c}, or
4092@samp{-p} option. Always send diffs from the old file to the new file.
4093If you even discuss something in the @code{ld} source, refer to it by
4094context, not by line number.
4095
4096The line numbers in our development sources will not match those in your
4097sources. Your line numbers would convey no useful information to us.
4098@end itemize
4099
4100Here are some things that are not necessary:
4101
4102@itemize @bullet
4103@item
4104A description of the envelope of the bug.
4105
4106Often people who encounter a bug spend a lot of time investigating
4107which changes to the input file will make the bug go away and which
4108changes will not affect it.
4109
4110This is often time consuming and not very useful, because the way we
4111will find the bug is by running a single example under the debugger
4112with breakpoints, not by pure deduction from a series of examples.
4113We recommend that you save your time for something else.
4114
4115Of course, if you can find a simpler example to report @emph{instead}
4116of the original one, that is a convenience for us. Errors in the
4117output will be easier to spot, running under the debugger will take
4118less time, and so on.
4119
4120However, simplification is not vital; if you do not want to do this,
4121report the bug anyway and send us the entire test case you used.
4122
4123@item
4124A patch for the bug.
4125
4126A patch for the bug does help us if it is a good one. But do not omit
4127the necessary information, such as the test case, on the assumption that
4128a patch is all we need. We might see problems with your patch and decide
4129to fix the problem another way, or we might not understand it at all.
4130
4131Sometimes with a program as complicated as @code{ld} it is very hard to
4132construct an example that will make the program follow a certain path
4133through the code. If you do not send us the example, we will not be
4134able to construct one, so we will not be able to verify that the bug is
4135fixed.
4136
4137And if we cannot understand what bug you are trying to fix, or why your
4138patch should be an improvement, we will not install it. A test case will
4139help us to understand.
4140
4141@item
4142A guess about what the bug is or what it depends on.
4143
4144Such guesses are usually wrong. Even we cannot guess right about such
4145things without first using the debugger to find the facts.
4146@end itemize
4147
4148@node MRI
4149@appendix MRI Compatible Script Files
4150@cindex MRI compatibility
4151To aid users making the transition to @sc{gnu} @code{ld} from the MRI
4152linker, @code{ld} can use MRI compatible linker scripts as an
4153alternative to the more general-purpose linker scripting language
4154described in @ref{Scripts}. MRI compatible linker scripts have a much
4155simpler command set than the scripting language otherwise used with
4156@code{ld}. @sc{gnu} @code{ld} supports the most commonly used MRI
4157linker commands; these commands are described here.
4158
4159In general, MRI scripts aren't of much use with the @code{a.out} object
4160file format, since it only has three sections and MRI scripts lack some
4161features to make use of them.
4162
4163You can specify a file containing an MRI-compatible script using the
4164@samp{-c} command-line option.
4165
4166Each command in an MRI-compatible script occupies its own line; each
4167command line starts with the keyword that identifies the command (though
4168blank lines are also allowed for punctuation). If a line of an
4169MRI-compatible script begins with an unrecognized keyword, @code{ld}
4170issues a warning message, but continues processing the script.
4171
4172Lines beginning with @samp{*} are comments.
4173
4174You can write these commands using all upper-case letters, or all
4175lower case; for example, @samp{chip} is the same as @samp{CHIP}.
4176The following list shows only the upper-case form of each command.
4177
4178@table @code
4179@cindex @code{ABSOLUTE} (MRI)
4180@item ABSOLUTE @var{secname}
4181@itemx ABSOLUTE @var{secname}, @var{secname}, @dots{} @var{secname}
4182Normally, @code{ld} includes in the output file all sections from all
4183the input files. However, in an MRI-compatible script, you can use the
4184@code{ABSOLUTE} command to restrict the sections that will be present in
4185your output program. If the @code{ABSOLUTE} command is used at all in a
4186script, then only the sections named explicitly in @code{ABSOLUTE}
4187commands will appear in the linker output. You can still use other
4188input sections (whatever you select on the command line, or using
4189@code{LOAD}) to resolve addresses in the output file.
4190
4191@cindex @code{ALIAS} (MRI)
4192@item ALIAS @var{out-secname}, @var{in-secname}
4193Use this command to place the data from input section @var{in-secname}
4194in a section called @var{out-secname} in the linker output file.
4195
4196@var{in-secname} may be an integer.
4197
4198@cindex @code{ALIGN} (MRI)
4199@item ALIGN @var{secname} = @var{expression}
4200Align the section called @var{secname} to @var{expression}. The
4201@var{expression} should be a power of two.
4202
4203@cindex @code{BASE} (MRI)
4204@item BASE @var{expression}
4205Use the value of @var{expression} as the lowest address (other than
4206absolute addresses) in the output file.
4207
4208@cindex @code{CHIP} (MRI)
4209@item CHIP @var{expression}
4210@itemx CHIP @var{expression}, @var{expression}
4211This command does nothing; it is accepted only for compatibility.
4212
4213@cindex @code{END} (MRI)
4214@item END
4215This command does nothing whatever; it's only accepted for compatibility.
4216
4217@cindex @code{FORMAT} (MRI)
4218@item FORMAT @var{output-format}
4219Similar to the @code{OUTPUT_FORMAT} command in the more general linker
4220language, but restricted to one of these output formats:
4221
4222@enumerate
4223@item
4224S-records, if @var{output-format} is @samp{S}
4225
4226@item
4227IEEE, if @var{output-format} is @samp{IEEE}
4228
4229@item
4230COFF (the @samp{coff-m68k} variant in BFD), if @var{output-format} is
4231@samp{COFF}
4232@end enumerate
4233
4234@cindex @code{LIST} (MRI)
4235@item LIST @var{anything}@dots{}
4236Print (to the standard output file) a link map, as produced by the
4237@code{ld} command-line option @samp{-M}.
4238
4239The keyword @code{LIST} may be followed by anything on the
4240same line, with no change in its effect.
4241
4242@cindex @code{LOAD} (MRI)
4243@item LOAD @var{filename}
4244@itemx LOAD @var{filename}, @var{filename}, @dots{} @var{filename}
4245Include one or more object file @var{filename} in the link; this has the
4246same effect as specifying @var{filename} directly on the @code{ld}
4247command line.
4248
4249@cindex @code{NAME} (MRI)
4250@item NAME @var{output-name}
4251@var{output-name} is the name for the program produced by @code{ld}; the
4252MRI-compatible command @code{NAME} is equivalent to the command-line
4253option @samp{-o} or the general script language command @code{OUTPUT}.
4254
4255@cindex @code{ORDER} (MRI)
4256@item ORDER @var{secname}, @var{secname}, @dots{} @var{secname}
4257@itemx ORDER @var{secname} @var{secname} @var{secname}
4258Normally, @code{ld} orders the sections in its output file in the
4259order in which they first appear in the input files. In an MRI-compatible
4260script, you can override this ordering with the @code{ORDER} command. The
4261sections you list with @code{ORDER} will appear first in your output
4262file, in the order specified.
4263
4264@cindex @code{PUBLIC} (MRI)
4265@item PUBLIC @var{name}=@var{expression}
4266@itemx PUBLIC @var{name},@var{expression}
4267@itemx PUBLIC @var{name} @var{expression}
4268Supply a value (@var{expression}) for external symbol
4269@var{name} used in the linker input files.
4270
4271@cindex @code{SECT} (MRI)
4272@item SECT @var{secname}, @var{expression}
4273@itemx SECT @var{secname}=@var{expression}
4274@itemx SECT @var{secname} @var{expression}
4275You can use any of these three forms of the @code{SECT} command to
4276specify the start address (@var{expression}) for section @var{secname}.
4277If you have more than one @code{SECT} statement for the same
4278@var{secname}, only the @emph{first} sets the start address.
4279@end table
4280
4281@node Index
4282@unnumbered Index
4283
4284@printindex cp
4285
4286@tex
4287% I think something like @colophon should be in texinfo. In the
4288% meantime:
4289\long\def\colophon{\hbox to0pt{}\vfill
4290\centerline{The body of this manual is set in}
4291\centerline{\fontname\tenrm,}
4292\centerline{with headings in {\bf\fontname\tenbf}}
4293\centerline{and examples in {\tt\fontname\tentt}.}
4294\centerline{{\it\fontname\tenit\/} and}
4295\centerline{{\sl\fontname\tensl\/}}
4296\centerline{are used for emphasis.}\vfill}
4297\page\colophon
4298% Blame: doc@cygnus.com, 28mar91.
4299@end tex
4300
4301
4302@contents
4303@bye
4304
4305
This page took 0.316713 seconds and 4 git commands to generate.