One step closer.
[deliverable/binutils-gdb.git] / ld / ld.texinfo
CommitLineData
f22eee08 1\input texinfo
c8072296 2@setfilename ld.info
b4d4e8e3
RP
3@c $Id$
4@syncodeindex ky cp
c8072296
RP
5@c @smallbook
6@c @cropmarks
b4d4e8e3
RP
7@ifinfo
8This file documents the GNU linker GLD.
9
10Copyright (C) 1991 Free Software Foundation, Inc.
11
12Permission is granted to make and distribute verbatim copies of
13this manual provided the copyright notice and this permission notice
14are preserved on all copies.
15
16@ignore
17Permission is granted to process this file through Tex and print the
18results, provided the printed document carries copying permission
19notice identical to this one except for the removal of this paragraph
20(this paragraph not being relevant to the printed manual).
21
22@end ignore
23Permission is granted to copy and distribute modified versions of this
24manual under the conditions for verbatim copying, provided also that the
25section entitled ``GNU General Public License'' is included exactly as
26in the original, and provided that the entire resulting derived work is
27distributed under the terms of a permission notice identical to this
28one.
29
30Permission is granted to copy and distribute translations of this manual
31into another language, under the above conditions for modified versions,
32except that the section entitled ``GNU General Public License'' may be
33included in a translation approved by the author instead of in the
34original English.
35@end ifinfo
2c5c0674
RP
36@iftex
37@finalout
b4d4e8e3
RP
38@setchapternewpage odd
39@settitle GLD, the GNU linker
f22eee08 40@titlepage
c8072296
RP
41@title gld
42@subtitle The GNU linker
f22eee08 43@sp 1
b4d4e8e3
RP
44@subtitle Second Edition---@code{gld} version 2.0
45@subtitle April 1991
c8072296
RP
46@author Steve Chamberlain and Roland Pesch
47@author Cygnus Support
b4d4e8e3
RP
48@page
49
50@tex
51\def\$#1${{#1}} % Kluge: collect RCS revision info without $...$
52\xdef\manvers{\$Revision$} % For use in headers, footers too
53{\parskip=0pt
54\hfill Cygnus Support\par
2c5c0674 55\hfill steve\@cygnus.com, pesch\@cygnus.com\par
b4d4e8e3
RP
56\hfill {\it GLD, the GNU linker}, \manvers\par
57\hfill \TeX{}info \texinfoversion\par
b4d4e8e3
RP
58}
59\global\parindent=0pt % Steve likes it this way.
60@end tex
61
f22eee08
RP
62@vskip 0pt plus 1filll
63Copyright @copyright{} 1991 Free Software Foundation, Inc.
64
65Permission is granted to make and distribute verbatim copies of
66this manual provided the copyright notice and this permission notice
67are preserved on all copies.
68
69Permission is granted to copy and distribute modified versions of this
70manual under the conditions for verbatim copying, provided also that
71the entire resulting derived work is distributed under the terms of a
72permission notice identical to this one.
73
74Permission is granted to copy and distribute translations of this manual
75into another language, under the above conditions for modified versions.
f22eee08 76@end titlepage
2c5c0674 77@end iftex
b4d4e8e3 78@c FIXME: Talk about importance of *order* of args, cmds to linker!
f22eee08 79
2c5c0674 80@node Top, Overview, (dir), (dir)
f22eee08
RP
81@ifinfo
82This file documents the GNU linker gld.
83@end ifinfo
84
2c5c0674 85@menu
c8072296
RP
86* Overview:: Overview
87* Invocation:: Invocation
88* Commands:: Command Language
89* BFD:: BFD
90* Index:: Index
2c5c0674
RP
91
92 --- The Detailed Node Listing ---
93
94Invocation
95
c8072296
RP
96* Options:: Command Line Options
97* Environment:: Environment Variables
2c5c0674
RP
98
99Command Language
100
c8072296
RP
101* Scripts:: Linker Scripts
102* Expressions:: Expressions
103* MEMORY:: MEMORY Command
104* SECTIONS:: SECTIONS Command
105* Entry Point:: The Entry Point
106* Other Commands:: Other Commands
2c5c0674
RP
107
108Expressions
109
c8072296
RP
110* Integers:: Integers
111* Symbols:: Symbol Names
112* Location Counter:: The Location Counter
113* Operators:: Operators
114* Evaluation:: Evaluation
115* Assignment:: Assignment: Defining Symbols
116* Built-ins:: Built-In Functions
2c5c0674
RP
117
118SECTIONS Command
119
c8072296
RP
120* Section Definition:: Section Definitions
121* Section Contents:: Section Contents
122* Section Options:: Optional Section Attributes
2c5c0674
RP
123
124BFD
125
c8072296
RP
126* BFD outline:: How it works: an outline of BFD
127* BFD information loss:: Information Loss
128* Mechanism:: Mechanism
2c5c0674
RP
129@end menu
130
131@node Overview, Invocation, Top, Top
f22eee08
RP
132@chapter Overview
133
2c5c0674
RP
134@cindex GNU linker
135@cindex what is this?
b4d4e8e3
RP
136@code{gld} combines a number of object and archive files, relocates
137their data and ties up symbol references. Often the last step in
138building a new compiled program to run is a call to @code{gld}.
f22eee08 139
b4d4e8e3 140@code{gld} accepts Linker Command Language files written in
2c5c0674 141a superset of AT&T's Link Editor Command Language syntax,
b4d4e8e3 142to provide explicit and total control over the linking process.
f22eee08 143
2c5c0674 144This version of @code{gld} uses the general purpose BFD libraries
b4d4e8e3
RP
145to operate on object files. This allows @code{gld} to read, combine, and
146write object files in many different formats---for example, COFF or
147@code{a.out}. Different formats may be linked together to produce any
148available kind of object file. @xref{BFD} for a list of formats
149supported on various architectures.
f22eee08 150
2c5c0674
RP
151Aside from its flexibility, the GNU linker is more helpful than other
152linkers in providing diagnostic information. Many linkers abandon
153execution immediately upon encountering an error; whenever possible,
154@code{gld} continues executing, allowing you to identify other errors
155(or, in some cases, to get an output file in spite of the error).
156
157@node Invocation, Commands, Overview, Top
158@chapter Invocation
159
160The GNU linker @code{gld} is meant to cover a broad range of situations,
161and to be as compatible as possible with other linkers. As a result,
162you have many choices to control its behavior through the command line,
163and through environment variables.
164
165@menu
c8072296
RP
166* Options:: Command Line Options
167* Environment:: Environment Variables
2c5c0674 168@end menu
f22eee08 169
2c5c0674
RP
170@node Options, Environment, Invocation, Invocation
171@section Command Line Options
172
173@cindex command line
174@cindex options
175Here is a sketch of the options you can use on the @code{gld} command
176line:
f22eee08 177
c8072296 178@smallexample
b4d4e8e3 179gld [-o @var{output} ] @var{objfiles}@dots{}
2c5c0674
RP
180 [ -A@var{architecture} ] [ -b @var{input-format} ] [ -Bstatic ]
181 [ -c @var{commandfile} ] [ -d | -dc | -dp ]
182 [ -defsym @var{symbol} = @var{expression} ]
183 [ -e @var{entry} ] [ -F ] [ -F @var{format} ]
184@c -f was in old GNU linker, not currently in new
185@c [ -f @var{fill} ]
186 [ -format @var{input-format} ] [ -g ] [ -i ]
b4d4e8e3 187 [ -l@var{ar} ] [ -L@var{searchdir} ] [ -M | -m ]
2c5c0674
RP
188 [ -n ] [ -noinhibit-exec ] [ -R @var{filename} ]
189@c -N and -z were alternatives to -n in old GNU linker, not curr in new
190@c [ -N | -n | -z ] [ -noinhibit-exec ] [ -R @var{filename} ]
191 [ -r | -Ur ] [ -S ] [ -s ] [ -T @var{commandfile} ]
b4d4e8e3
RP
192 [ -Ttext @var{textorg} ] [ -Tdata @var{dataorg} ] [ -Tbss @var{bssorg} ]
193 [ -t ] [ -u @var{sym}] [-v] [ -X ] [ -x ]
2c5c0674 194 [ @{ @var{script} @} ]
c8072296 195@end smallexample
b4d4e8e3
RP
196
197This plethora of command-line options may seem intimidating, but in
198actual practice few of them are used in any particular context.
2c5c0674 199@cindex standard Unix system
b4d4e8e3
RP
200For instance, a frequent use of @code{gld} is to link standard Unix
201object files on a standard, supported Unix system. On such a system, to
202link a file @code{hello.o}:
f22eee08 203@example
b4d4e8e3 204$ gld -o output /lib/crt0.o hello.o -lc
f22eee08 205@end example
b4d4e8e3
RP
206This tells @code{gld} to produce a file called @code{output} as the
207result of linking the file @code{/lib/crt0.o} with @code{hello.o} and
208the library @code{libc.a} which will come from the standard search
209directories.
f22eee08 210
b4d4e8e3 211The command-line options to @code{gld} may be specified in any order, and
f22eee08
RP
212may be repeated at will. For the most part, repeating an option with a
213different argument will either have no further effect, or override prior
214occurrences (those further to the left on the command line) of an
215option.
216
2c5c0674
RP
217The exceptions---which may meaningfully be used more than once---are
218@code{-A}, @code{-b} (or its synonym @code{-format}), @code{-defsym},
219@code{-L}, @code{-l}, @code{-R}, and @code{-u}.
f22eee08 220
2c5c0674 221@cindex object files
b4d4e8e3
RP
222The list of object files to be linked together, shown as @var{objfiles},
223may follow, precede, or be mixed in with command-line options; save that
224an @var{objfiles} argument may not be placed between an option flag and
225its argument.
f22eee08 226
2c5c0674
RP
227Usually the linker is invoked with at least one object file, but other
228forms of binary input files can also be specified with @code{-l},
229@code{-R}, and the script command language. If @emph{no} binary input
230files at all are specified, the linker does not produce any output, and
231issues the message @samp{No input files}.
232
233Option arguments must either follow the option letter without intervening
f22eee08
RP
234whitespace, or be given as separate arguments immediately following the
235option that requires them.
236
237@table @code
238@item @var{objfiles}@dots{}
2c5c0674 239The object files @var{objfiles} to be linked.
f22eee08 240
2c5c0674
RP
241@cindex architectures
242@kindex -A@var{arch}
b4d4e8e3
RP
243@item -A@var{architecture}
244In the current release of @code{gld}, this option is useful only for the
2c5c0674 245Intel 960 family of architectures. In that @code{gld} configuration, the
b4d4e8e3
RP
246@var{architecture} argument is one of the two-letter names identifying
247members of the 960 family; the option specifies the desired output
248target, and warns of any incompatible instructions in the input files.
2c5c0674
RP
249It also modifies the linker's search strategy for archive libraries, to
250support the use of libraries specific to each particular
251architecture, by including in the search loop names suffixed with the
252string identifying the architecture.
253
254For example, if your @code{gld} command line included @w{@samp{-ACA}} as
255well as @w{@samp{-ltry}}, the linker would look (in its built-in search
256paths, and in any paths you specify with @code{-L}) for a library with
257the names
258@example
259try
260libtry.a
261tryca
262libtryca.a
263@end example
264@noindent
265The first two possibilities would be considered in any event; the last
266two are due to the use of @w{@samp{-ACA}}.
b4d4e8e3
RP
267
268Future releases of @code{gld} may support similar functionality for
269other architecture families.
270
2c5c0674
RP
271You can meaningfully use @code{-A} more than once on a command line, if
272an architecture family allows combination of target architectures; each
273use will add another pair of name variants to search for when @w{@code{-l}}
274specifies a library.
275
276@cindex binary input format
277@kindex -b @var{format}
278@cindex input format
279@item -b @var{input-format}
280@cindex input format
281Specify the binary format for input object files that follow this option
282on the command line. You don't usually need to specify this, as
283@code{gld} is configured to expect as a default input format the most
284usual format on each machine. @var{input-format} is a text string, the
285name of a particular format supported by the BFD libraries. @xref{BFD}.
286@code{-format @var{input-format}} has the same effect.@refill
287
288You may want to use this option if you are linking files with an unusual
289binary format. You can also use @code{-b} to switch formats explicitly (when
290linking object files of different formats), by including
291@code{-b @var{input-format}} before each group of object files in a
292particular format.
293
294The default format is taken from the environment variable
295@code{GNUTARGET}. @xref{Environment}. You can also define the input
296format from a script, using the command @code{TARGET}.
297
298@kindex -Bstatic
f22eee08
RP
299@item -Bstatic
300This flag is accepted for command-line compatibility with the SunOS linker,
301but has no effect on @code{gld}.
302
2c5c0674
RP
303@kindex -c @var{cmdfile}
304@cindex script files
f22eee08 305@item -c @var{commandfile}
b4d4e8e3 306Directs @code{gld} to read link commands from the file
2c5c0674
RP
307@var{commandfile}. These commands will completely override @code{gld}'s
308default link format (rather than adding to it); @var{commandfile} must
309specify everything necessary to describe the target format.
310@xref{Commands}.
f22eee08 311
b4d4e8e3 312You may also include a script of link commands directly in the command
2c5c0674 313line by bracketing it between @samp{@{} and @samp{@}} characters.
b4d4e8e3 314
2c5c0674
RP
315@cindex common allocation
316@kindex -d
b4d4e8e3 317@item -d
2c5c0674 318@kindex -dc
b4d4e8e3 319@itemx -dc
2c5c0674 320@kindex -dp
b4d4e8e3
RP
321@itemx -dp
322These three options are equivalent; multiple forms are supported for
2c5c0674
RP
323compatibility with other linkers. Use any of them to make @code{ld}
324assign space to common symbols even if a relocatable output file is
325specified (@code{-r}). The script command
326@code{FORCE_COMMON_ALLOCATION} has the same effect.
b4d4e8e3 327
2c5c0674
RP
328@cindex symbols, from command line
329@kindex -defsym @var{symbol}=@var{exp}
b4d4e8e3 330@item -defsym @var{symbol} = @var{expression}
2c5c0674
RP
331Create a global symbol in the output file, containing the absolute
332address given by @var{expression}. You may use this option as many
333times as necessary to define multiple symbols in the command line. A
334limited form of arithmetic is supported for the @var{expression} in this
335context: you may give a hexadecimal constant or the name of an existing
336symbol, or use @code{+} and @code{-} to add or subtract hexadecimal
337constants or symbols. If you need more elaborate expressions, consider
338using the linker command language from a script.
339
340@cindex entry point, from command line
341@kindex -e @var{entry}
f22eee08
RP
342@item -e @var{entry}
343Use @var{entry} as the explicit symbol for beginning execution of your
b4d4e8e3
RP
344program, rather than the default entry point. @xref{Entry Point}, for a
345discussion of defaults and other ways of specifying the
346entry point.
f22eee08 347
2c5c0674
RP
348@ignore
349@cindex fill, from command line
350@kindex -f @var{fill}
351@c -f in older GNU linker, not in new
f22eee08
RP
352@item -f @var{fill}
353Sets the default fill pattern for ``holes'' in the output file to
2c5c0674
RP
354the lowest two bytes of the expression specified. Holes are created
355when you advance the location counter (@xref{Location Counter}), or when
356there is a gap between explicitly specified section addresses
357(@xref{Section Options}).
358@end ignore
f22eee08 359
2c5c0674 360@kindex -F
b4d4e8e3 361@item -F
2c5c0674
RP
362@itemx -F@var{format}
363Some older linkers used this option throughout a compilation toolchain
364for specifying object-file format for both input and output object
365files. @code{gld}'s mechanisms (the @code{-b} or @code{-format} options
366for input files, the @code{TARGET} command in linker scripts for output
367files, the @code{GNUTARGET} environment variable) are more flexible, but
368but it accepts (and ignores) the @code{-F} option flag for compatibility
369with scripts written to call the old linker.
370
371@kindex -format
372@item -format @var{input-format}
373Synonym for @code{-b} @var{input-format}.
374
375@kindex -g
b4d4e8e3
RP
376@item -g
377Accepted, but ignored; provided for compatibility with other tools.
378
2c5c0674
RP
379@kindex -i
380@cindex incremental link
f22eee08 381@item -i
2c5c0674 382Perform an incremental link (same as option @code{-r}).
f22eee08 383
2c5c0674
RP
384@cindex archive files, from cmd line
385@kindex -l@var{ar}
b4d4e8e3
RP
386@item -l@var{ar}
387Add an archive file @var{ar} to the list of files to link. This
f22eee08 388option may be used any number of times. @code{ld} will search its
b4d4e8e3 389path-list for occurrences of @code{lib@var{ar}.a} for every @var{ar}
f22eee08
RP
390specified.
391
2c5c0674
RP
392@cindex search directory, from cmd line
393@kindex -L@var{dir}
b4d4e8e3
RP
394@item -L@var{searchdir}
395This command adds path @var{searchdir} to the list of paths that
396@code{gld} will search for archive libraries. You may use this option
397any number of times.
f22eee08 398
2c5c0674
RP
399The default set of paths searched (without being specified with
400@code{-L}) depends on what emulation mode @code{gld} is using, and in
401some cases also on how it was configured. @xref{Environment}. The
402paths can also be specified in a link script with the @code{SEARCH_DIR}
403command.
f22eee08 404
2c5c0674
RP
405@cindex link map
406@kindex -M
f22eee08 407@item -M
2c5c0674 408@kindex -m
f22eee08
RP
409@itemx -m
410Print (to the standard output file) a link map---diagnostic information
411about where symbols are mapped by @code{ld}, and information on global
412common storage allocation.
413
2c5c0674
RP
414@ignore
415@c -N in older GNU linker, not in new
416@kindex -N
417@cindex read/write from cmd line
418@kindex OMAGIC
f22eee08 419@item -N
b4d4e8e3
RP
420specifies readable and writable @code{text} and @code{data} sections. If
421the output format supports Unix style magic numbers, the output is
422marked as @code{OMAGIC}.
2c5c0674 423@end ignore
f22eee08
RP
424
425@item -n
2c5c0674
RP
426@kindex -n
427@cindex read-only text
428@kindex NMAGIC
f22eee08
RP
429sets the text segment to be read only, and @code{NMAGIC} is written
430if possible.
431
b4d4e8e3 432@item -noinhibit-exec
2c5c0674
RP
433@cindex output file after errors
434@kindex -noinhibit-exec
b4d4e8e3
RP
435Normally, the linker will not produce an output file if it encounters
436errors during the link process. With this flag, you can specify that
2c5c0674 437you wish the output file retained even after non-fatal errors.
b4d4e8e3 438
f22eee08 439@item -o @var{output}
2c5c0674
RP
440@kindex -o @var{output}
441@cindex naming the output file
f22eee08 442@var{output} is a name for the program produced by @code{ld}; if this
2c5c0674
RP
443option is not specified, the name @samp{a.out} is used by default. The
444script command @code{OUTPUT} can also specify the output file name.
f22eee08 445
b4d4e8e3 446@item -R @var{filename}
2c5c0674
RP
447@kindex -R @var{file}
448@cindex symbol-only input
b4d4e8e3
RP
449Read symbol names and their addresses from @var{filename}, but do not
450relocate it or include it in the output. This allows your output file
451to refer symbolically to absolute locations of memory defined in other
452programs.
b4d4e8e3 453
f22eee08 454@item -r
b4d4e8e3 455@cindex partial link
2c5c0674
RP
456@cindex relocatable output
457@kindex -r
f22eee08 458Generates relocatable output---i.e., generate an output file that can in
b4d4e8e3 459turn serve as input to @code{gld}. This is often called @dfn{partial
2c5c0674
RP
460linking}. As a side effect, in environments that support standard Unix
461magic numbers, this option also sets the output file's magic number to
462@code{OMAGIC}.
463@c ; see @code{-N}.
464If this option is not specified, an absolute file is produced. When
465linking C++ programs, this option @emph{will not} resolve references to
466constructors; @code{-Ur} is an alternative. @refill
467
468This option does the same as @code{-i}.
f22eee08
RP
469
470@item -S
2c5c0674
RP
471@kindex -S
472@cindex strip debugger symbols
f22eee08
RP
473Omits debugger symbol information (but not all symbols) from the output file.
474
475@item -s
2c5c0674
RP
476@kindex -s
477@cindex strip all symbols
f22eee08
RP
478Omits all symbol information from the output file.
479
2c5c0674
RP
480@item @{ @var{script} @}
481@kindex @{ @var{script} @}
482@cindex scripts on command line
b4d4e8e3
RP
483You can, if you wish, include a script of linker commands directly in
484the command line instead of referring to it via an input file. When the
2c5c0674 485character @samp{@{} occurs on the command line, the linker switches to
b4d4e8e3 486interpreting the command language until the end of the list of commands
2c5c0674
RP
487is reached---flagged with a closing brace @samp{@}}. Other command-line
488options will not be recognized while parsing the script.
489@xref{Commands} for a description of the command language.
490
491@item -Tbss @var{org}
492@kindex -Tbss @var{org}
493@itemx -Tdata @var{org}
494@kindex -Tdata @var{org}
495@itemx -Ttext @var{org}
496@kindex -Ttext @var{org}
497@cindex segment origins, cmd line
498Use @var{org} as the starting address for---respectively---the
b4d4e8e3
RP
499@code{bss}, @code{data}, or the @code{text} segment of the output file.
500@var{textorg} must be a hexadecimal integer.
f22eee08 501
b4d4e8e3
RP
502@item -T @var{commandfile}
503@itemx -T@var{commandfile}
2c5c0674 504@kindex -T @var{script}
b4d4e8e3
RP
505Equivalent to @code{-c @var{commandfile}}; supported for compatibility with
506other tools.
f22eee08
RP
507
508@item -t
2c5c0674
RP
509@kindex -t
510@cindex verbose
511@cindex input files, displaying
f22eee08
RP
512Prints names of input files as @code{ld} processes them.
513
514@item -u @var{sym}
2c5c0674
RP
515@kindex -u @var{sym}
516@cindex undefined symbol
f22eee08
RP
517Forces @var{sym} to be entered in the output file as an undefined symbol.
518This may, for example, trigger linking of additional modules from
519standard libraries. @code{-u} may be repeated with different option
2c5c0674
RP
520arguments to enter additional undefined symbols.
521@c Nice idea, but no such command: This option is equivalent
522@c to the @code{EXTERN} linker command.
f22eee08 523
b4d4e8e3 524@item -Ur
2c5c0674 525@kindex -Ur
b4d4e8e3
RP
526@cindex constructors
527For anything other than C++ programs, this option is equivalent to
2c5c0674
RP
528@code{-r}: it generates relocatable output---i.e., an output file that can in
529turn serve as input to @code{gld}. When linking C++ programs, @code{-Ur}
530@emph{will} resolve references to constructors, unlike @code{-r}.
b4d4e8e3
RP
531
532@item -v
2c5c0674 533@kindex -v
b4d4e8e3 534@cindex version
2c5c0674 535Display the version number for @code{gld}.
b4d4e8e3 536
f22eee08 537@item -X
2c5c0674
RP
538@kindex -X
539@cindex local symbols, deleting
540@cindex L, deleting symbols beginning
f22eee08
RP
541If @code{-s} or @code{-S} is also specified, delete only local symbols
542beginning with @samp{L}.
543
b4d4e8e3 544@item -x
2c5c0674
RP
545@kindex -x
546@cindex deleting local symbols
b4d4e8e3
RP
547If @code{-s} or @code{-S} is also specified, delete all local symbols,
548not just those beginning with @samp{L}.
549
2c5c0674
RP
550@ignore
551@c -z in older GNU linker, not in new
f22eee08 552@item -z
2c5c0674
RP
553@kindex -z
554@cindex read-only text
b4d4e8e3
RP
555Specifies a read-only, demand pageable, and shared @code{text} segment.
556If the output format supports Unix-style magic numbers, @code{-z} also
557marks the output as @code{ZMAGIC}, the default.
f22eee08 558
2c5c0674
RP
559@c why was following here?. Is it useful to say '-z -r' for
560@c instance, or is this just a ref to other ways of setting
561@c magic no?
f22eee08
RP
562Specifying a relocatable output file (@code{-r}) will also set the magic
563number to @code{OMAGIC}.
564
2c5c0674
RP
565See description of @code{-N}.
566@end ignore
f22eee08 567
f22eee08 568@end table
b4d4e8e3 569
2c5c0674
RP
570@node Environment, , Options, Invocation
571@section Environment Variables
572
573@code{gld} always consults two environment variables: @code{GNUTARGET}
574and @code{LDEMULATION}. Depending on the setting of the latter, other
575environment variables may be used as well.
576
577@kindex GNUTARGET
578@cindex default input format
579@code{GNUTARGET} determines the input-file object format if you don't
580use @code{-b} (or its synonym @code{-format}). Its value should be one
581of the BFD names for an input format (@pxref{BFD}). If there is no
582@code{GNUTARGET} in the environment, @code{gld} uses the natural format
583of the host. If @code{GNUTARGET} is set to @code{default} then BFD attempts to discover the
584input format by examining binary input files; this method often
585succeeds, but there are potential ambiguities, since there is no method
586of ensuring that the magic number used to flag object-file formats is
587unique. However, the configuration procedure for BFD on each system
588places the conventional format for that system first in the search-list,
589so ambiguities are resolved in favor of convention.
590
591@kindex LDEMULATION
592@cindex emulation
593@cindex environment vars
594@code{LDEMULATION} controls some aspects of @code{gld}'s dominant
595personality. Although @code{gld} is flexible enough to permit its use
596in many contexts regardless of configuration, you can use this variable
597to make it act more like one or another older linker by default.
598
599@cindex defaults
600@cindex library paths, default
601In particular, the value of @code{LDEMULATION} controls what default
602linker script is used (thereby controlling the default input and output
603formats; @pxref{BFD}); what default paths are searched for
604archive libraries; and in some cases whether additional linker script
605commands are available.
606
607Here is the current set of emulations available:
608@table @code
609
610@item LDEMULATION=gld
611@kindex gld
612@cindex emulating old GNU linker
613Emulate the older GNU linker. When this emulation is selected, the
614default library search paths are
615@example
616/lib
617/usr/lib
618/usr/local/lib/lib
619@end example
620@noindent
621The default output format is set to @code{a.out-generic-big}, and the
622default machine is the system's configured BFD default.
623
624@item LDEMULATION=gld68k
625@kindex gld68k
626@cindex m68k
627A variant of the @code{gld} emulation; only differs in specifically
628setting the default BFD machine as @code{m68k}.
629
630@item LDEMULATION=gld960
631@kindex gld960
632@kindex G960LIB
633@kindex G960BASE
634@cindex i960
635Emulate the Intel port of the older @code{gld} for the i960
636architectures. The default library search paths are taken from two
637other environment variables, @code{G960LIB} and @code{G960BASE}. The
638default architecture is @code{i960}. The default output format is set
639to @code{b.out.big}, and in fact the default output file name (if
640@code{-o} is not specified) is @code{b.out}, to reflect this variant
641format, for this emulation.
642
643@kindex GNU960
644This emulation can behave slightly differently depending on the setting
645of the @code{gld} compile-time switch @code{GNU960}. If @code{gld} is
646compiled with @code{GNU960} defined, then an additional environment
647variable---@code{GNUTARGET}---is available; its value, if available,
648specifies some other default output format than @code{b.out.big}.
649
650@item LDEMULATION=gldm88kbcs
651@kindex gldm88kbcs
652@cindex m88k
653Sets the output format to @code{m88kbcs} and the architecture to
654@code{m88k}. Default library search paths are
655@example
656/lib
657/usr/lib
658/usr/local/lib
659@end example
f22eee08 660
2c5c0674
RP
661@item LDEMULATION=lnk960
662@kindex lnk960
663@cindex i960
664@cindex Architectures, i960 family
665Emulate the Intel linker @code{lnk960}. The default output format is
666@code{coff-Intel-big}. With this emulation, @code{gld}
667supports the additional script commands @code{HLL} and @code{SYSLIB} for
668specification of library archives. This is the only emulation with
669extensive support for the @code{-A} (architecture) command-line option.
670By default, the architecture @code{CORE} is assumed, but you can choose
671additional features from the i960 architecture family by using one of
672the following with @code{-A} (or by using the @code{OUTPUT_ARCH} command
673from a script):
674@example
675CORE
676KB
677SB
678MC
679XA
680CA
681KA
682SA
683@end example
684
685The default libraries are chosen with some attention to the architecture
686selected; the core library @file{cg} is always included, but the library
687@code{fpg} is also used if you've specified any of the architectures
688@code{KA}, @code{SA}, or @code{CA}.
689
690@kindex GNU960
691Like @code{gld960}, this emulation uses additional environment variables
692to set the default library search paths. Also like @code{gld960}, the
693behavior of this emulation is slightly different depending on whether
694@code{gld} itself was compiled with @code{GNU960} defined.
695
696@kindex G960BASE
697@kindex G960LIB
698@kindex I960BASE
699If your @code{gld} was compiled with @code{GNU960} defined, the default
700paths are taken from all three of @code{G960LIB}, @code{G960BASE}, and
701@code{I960BASE}. For the first two, paths you supply are automatically
702suffixed with @samp{/lib/libcoff}; for the last, your path is
703automatically suffixed with @samp{/lib}.
704
705If your @code{gld} was @emph{not} compiled with @code{GNU960} defined,
706the default paths are taken from @code{I960BASE}, and @code{G960BASE} is
707only consulted if @code{I960BASE} is undefined. In this case
708@code{G960LIB} is not used at all.
709
710@item LDEMULATION=vanilla
711@kindex vanilla
712@cindex emulation, disabling
713@cindex disabling emulation
714This is the least specific setting for @code{gld}. You can set
715@code{LDEMULATION=vanilla} to disable emulation of other linkers. This
716setting makes @code{gld} take the default machine from the BFD
717configuration on your system; @code{a.out-generic-big} is the default
718target. No other defaults are specified.
719
720@end table
721
722@node Commands, BFD, Invocation, Top
723@chapter Command Language
f22eee08 724
2c5c0674 725@cindex command files
b4d4e8e3
RP
726The command language allows explicit control over the link process,
727allowing complete specification of the mapping between the linker's
728input files and its output. This includes:
729@itemize @bullet
2c5c0674
RP
730@item
731input files
732@item
733file formats
734@item
735output file format
736@item
737addresses of sections
738@item
739placement of common blocks
b4d4e8e3 740@end itemize
f22eee08 741
2c5c0674
RP
742You may supply a command file (also known as a link script) to the
743linker either explicitly through the @code{-c} option, or implicitly as
744an ordinary file. If the linker opens a file which it cannot recognize
745as a supported object or archive format, it tries to interpret the file
746as a command file.
747
748You can also include a script directly on the @code{gld} command line,
749delimited by the characters @samp{@{} and @samp{@}}.
750
751@menu
c8072296
RP
752* Scripts:: Linker Scripts
753* Expressions:: Expressions
754* MEMORY:: MEMORY Command
755* SECTIONS:: SECTIONS Command
756* Entry Point:: The Entry Point
757* Other Commands:: Other Commands
2c5c0674
RP
758@end menu
759
760@node Scripts, Expressions, Commands, Commands
b4d4e8e3
RP
761@section Linker Scripts
762The @code{gld} command language is a collection of statements; some are
763simple keywords setting a particular flag, some are used to select and
764group input files or name output files; and two particular statement
765types have a fundamental and pervasive impact on the linking process.
766
2c5c0674
RP
767@cindex fundamental script commands
768@cindex commands, fundamental
769@cindex output file layout
770@cindex layout of output file
b4d4e8e3
RP
771The most fundamental command of the @code{gld} command language is the
772@code{SECTIONS} command (@pxref{SECTIONS}). Every meaningful command
773script must have a @code{SECTIONS} command: it specifies a
774``picture'' of the output file's layout, in varying degrees of detail.
775No other command is required in all cases.
776
777The @code{MEMORY} command complements @code{SECTIONS} by describing the
2c5c0674
RP
778available memory in the target architecture. This command is optional;
779if you don't use a @code{MEMORY} command, @code{gld} assumes sufficient
780memory is available in a contiguous block for all output.
781@xref{MEMORY}.
b4d4e8e3 782
2c5c0674
RP
783@cindex comments
784You may include comments in linker scripts just as in C: delimited
785by @samp{/*} and @samp{*/}. As in C, comments are syntactically
786equivalent to whitespace.
787
788@node Expressions, MEMORY, Scripts, Commands
f22eee08 789@section Expressions
2c5c0674
RP
790@cindex expression syntax
791@cindex arithmetic
b4d4e8e3
RP
792Many useful commands involve arithmetic expressions. The syntax for
793expressions in the command language is identical to that of C
794expressions, with the following features:
795@itemize @bullet
2c5c0674
RP
796@item
797All expressions evaluated as integers and
f22eee08 798are of ``long'' or ``unsigned long'' type.
2c5c0674
RP
799@item
800All constants are integers.
801@item
802All of the C arithmetic operators are provided.
803@item
804You may reference, define, and create global variables.
805@item
806You may call special purpose built-in functions.
b4d4e8e3 807@end itemize
f22eee08 808
2c5c0674 809@menu
c8072296
RP
810* Integers:: Integers
811* Symbols:: Symbol Names
812* Location Counter:: The Location Counter
813* Operators:: Operators
814* Evaluation:: Evaluation
815* Assignment:: Assignment: Defining Symbols
816* Built-ins:: Built-In Functions
2c5c0674
RP
817@end menu
818
819@node Integers, Symbols, Expressions, Expressions
f22eee08 820@subsection Integers
2c5c0674
RP
821@cindex integer notation
822@cindex octal integers
f22eee08
RP
823An octal integer is @samp{0} followed by zero or more of the octal
824digits (@samp{01234567}).
b4d4e8e3 825@example
2c5c0674 826_as_octal = 0157255;
b4d4e8e3 827@end example
f22eee08 828
2c5c0674 829@cindex decimal integers
f22eee08
RP
830A decimal integer starts with a non-zero digit followed by zero or
831more digits (@samp{0123456789}).
b4d4e8e3 832@example
2c5c0674 833_as_decimal = 57005;
b4d4e8e3 834@end example
f22eee08 835
2c5c0674
RP
836@cindex hexadecimal integers
837@kindex 0x
f22eee08
RP
838A hexadecimal integer is @samp{0x} or @samp{0X} followed by one or
839more hexadecimal digits chosen from @samp{0123456789abcdefABCDEF}.
b4d4e8e3
RP
840@example
841_as_hex = 0xdead;
842@end example
f22eee08 843
2c5c0674
RP
844@cindex negative integers
845Decimal integers have the usual values. To write a negative integer, use
b4d4e8e3
RP
846the prefix operator @samp{-}; @pxref{Operators}.
847@example
b4d4e8e3
RP
848_as_neg = -57005;
849@end example
f22eee08 850
2c5c0674
RP
851@cindex scaled integers
852@cindex K and M integer suffixes
853@cindex M and K integer suffixes
854@cindex suffixes for integers
855@cindex integer suffixes
b4d4e8e3
RP
856Additionally the suffixes @code{K} and @code{M} may be used to scale a
857constant by
c8072296
RP
858@c TEXI2ROFF-KILL
859@ifinfo
860@c END TEXI2ROFF-KILL
861@code{1024} or @code{1024*1024}
862@c TEXI2ROFF-KILL
863@end ifinfo
f22eee08 864@tex
b4d4e8e3 865${\rm 1024}$ or ${\rm 1024}^2$
f22eee08 866@end tex
c8072296 867@c END TEXI2ROFF-KILL
b4d4e8e3 868respectively. For example, the following all refer to the same quantity:@refill
f22eee08
RP
869
870@example
2c5c0674
RP
871 _fourk_1 = 4K;
872 _fourk_2 = 4096;
873 _fourk_3 = 0x1000;
f22eee08 874@end example
b4d4e8e3 875
2c5c0674 876@node Symbols, Location Counter, Integers, Expressions
b4d4e8e3 877@subsection Symbol Names
2c5c0674
RP
878@cindex symbol names
879@cindex names
880@cindex quoted symbol names
881@kindex "
b4d4e8e3 882Unless quoted, symbol names start with a letter, underscore, point or
2c5c0674 883hyphen and may include any letters, underscores, digits, points,
b4d4e8e3
RP
884and minus signs. Unquoted symbol names must not conflict with any
885keywords. You can specify a symbol which contains odd characters or has
886the same name as a keyword, by surrounding the symbol name in double quotes:
887@example
888 "SECTION" = 9;
889 "with a space" = "also with a space" + 10;
890@end example
891
2c5c0674 892@node Location Counter, Operators, Symbols, Expressions
b4d4e8e3 893@subsection The Location Counter
2c5c0674
RP
894@kindex .
895@cindex dot
896@cindex location counter
897@cindex current output location
b4d4e8e3
RP
898The special linker variable @dfn{dot} @samp{.} always contains the
899current output location counter. Since the @code{.} always refers to
900a location in an output section, it must always appear in an
901expression within a @code{SECTIONS} command. The @code{.} symbol
902may appear anywhere that an ordinary symbol is allowed in an
903expression, but its assignments have a side effect. Assigning a value
904to the @code{.} symbol will cause the location counter to be moved.
2c5c0674 905@cindex holes
b4d4e8e3
RP
906This may be used to create holes in the output section. The location
907counter may never be moved backwards.
908@example
2c5c0674
RP
909SECTIONS
910@{
911 output :
b4d4e8e3 912 @{
2c5c0674
RP
913 file1(.text)
914 . = . + 1000;
915 file2(.text)
916 . += 1000;
917 file3(.text)
918 @} = 0x1234;
919@}
b4d4e8e3 920@end example
2c5c0674
RP
921@noindent
922In the previous example, @code{file1} is located at the beginning of the
923output section, then there is a 1000 byte gap. Then @code{file2}
924appears, also with a 1000 byte gap following before @code{file3} is
925loaded. The notation @samp{= 0x1234} specifies what data to write in
926the gaps (@pxref{Section Options}).
b4d4e8e3 927
2c5c0674 928@node Operators, Evaluation, Location Counter, Expressions
f22eee08 929@subsection Operators
2c5c0674
RP
930@cindex Operators for arithmetic
931@cindex arithmetic operators
932@cindex precedence in expressions
b4d4e8e3 933The linker recognizes the standard C set of arithmetic operators, with
f22eee08 934the standard bindings and precedence levels:
c8072296 935@c TEXI2ROFF-KILL
b4d4e8e3 936@ifinfo
c8072296 937@c END TEXI2ROFF-KILL
f22eee08 938@example
c8072296 939precedence associativity Operators Notes
b4d4e8e3 940(highest)
c8072296
RP
9411 left ! - ~ (1)
9422 left * / %
9433 left + -
9444 left >> <<
9455 left == != > < <= >=
9466 left &
9477 left |
9488 left &&
9499 left ||
95010 right ? :
95111 right &= += -= *= /= (2)
b4d4e8e3 952(lowest)
f22eee08 953@end example
2c5c0674
RP
954Notes:
955(1) Prefix operators
956(2) @xref{Assignment}
c8072296 957@c TEXI2ROFF-KILL
b4d4e8e3 958@end ifinfo
f22eee08 959@tex
2c5c0674
RP
960\vskip \baselineskip
961%"lispnarrowing" is the extra indent used generally for @example
962\hskip\lispnarrowing\vbox{\offinterlineskip
f22eee08
RP
963\hrule
964\halign
2c5c0674
RP
965{\vrule#&\strut\hfil\ #\ \hfil&\vrule#&\strut\hfil\ #\ \hfil&\vrule#&\strut\hfil\ {\tt #}\ \hfil&\vrule#\cr
966height2pt&\omit&&\omit&&\omit&\cr
967&Precedence&& Associativity &&{\rm Operators}&\cr
968height2pt&\omit&&\omit&&\omit&\cr
f22eee08 969\noalign{\hrule}
2c5c0674 970height2pt&\omit&&\omit&&\omit&\cr
b4d4e8e3 971&highest&&&&&\cr
2c5c0674
RP
972% '176 is tilde, '~' in tt font
973&1&&left&&\qquad- \char'176\ !\qquad\dag&\cr
974&2&&left&&* / \%&\cr
975&3&&left&&+ -&\cr
976&4&&left&&>> <<&\cr
977&5&&left&&== != > < <= >=&\cr
f22eee08 978&6&&left&&\&&\cr
f22eee08 979&7&&left&&|&\cr
f22eee08 980&8&&left&&{\&\&}&\cr
f22eee08 981&9&&left&&||&\cr
2c5c0674
RP
982&10&&right&&? :&\cr
983&11&&right&&\qquad\&= += -= *= /=\qquad\ddag&\cr
b4d4e8e3 984&lowest&&&&&\cr
2c5c0674 985height2pt&\omit&&\omit&&\omit&\cr}
f22eee08
RP
986\hrule}
987@end tex
2c5c0674
RP
988@iftex
989{
990@obeylines@parskip=0pt@parindent=0pt
991@dag@quad Prefix operators.
992@ddag@quad @xref{Assignment}.
993}
994@end iftex
c8072296 995@c END TEXI2ROFF-KILL
f22eee08 996
2c5c0674 997@node Evaluation, Assignment, Operators, Expressions
b4d4e8e3
RP
998@subsection Evaluation
999
2c5c0674
RP
1000@cindex lazy evaluation
1001@cindex expression evaluation order
b4d4e8e3
RP
1002The linker uses ``lazy evaluation'' for expressions; it only calculates
1003an expression when absolutely necessary. The linker needs the value of
1004the start address, and the lengths of memory regions, in order to do any
1005linking at all; these values are computed as soon as possible when the
1006linker reads in the command file. However, other values (such as symbol
1007values) are not known or needed until after storage allocation. Such
1008values are evaluated later, when other information (such as the sizes of
1009output sections) is available for use in the symbol assignment
1010expression.
1011
2c5c0674 1012@node Assignment, Built-ins, Evaluation, Expressions
b4d4e8e3 1013@subsection Assignment: Defining Symbols
2c5c0674
RP
1014@cindex assignment in scripts
1015@cindex symbol definition, scripts
1016@cindex variables, defining
b4d4e8e3
RP
1017You may create global symbols, and assign values (addresses) to global
1018symbols, using any of the C assignment operators:
1019
1020@table @code
1021@item @var{symbol} = @var{expression} ;
2c5c0674 1022@itemx @var{symbol} &= @var{expression} ;
b4d4e8e3
RP
1023@itemx @var{symbol} += @var{expression} ;
1024@itemx @var{symbol} -= @var{expression} ;
1025@itemx @var{symbol} *= @var{expression} ;
1026@itemx @var{symbol} /= @var{expression} ;
1027@end table
1028
1029Two things distinguish assignment from other operators in @code{gld}
1030expressions.
1031@itemize @bullet
2c5c0674
RP
1032@item
1033Assignment may only be used at the root of an expression;
b4d4e8e3 1034@samp{a=b+3;} is allowed, but @samp{a+b=3;} is an error.
2c5c0674
RP
1035
1036@kindex ;
1037@cindex semicolon
1038@item
1039A trailing semicolon is required at the end of an assignment
b4d4e8e3
RP
1040statement.
1041@end itemize
1042
1043Assignment statements may appear:
1044@itemize @bullet
2c5c0674
RP
1045@item
1046as commands in their own right in a @code{gld} script; or
1047@item
1048as independent statements within a @code{SECTIONS} command; or
1049@item
1050as part of the contents of a section definition in a
b4d4e8e3
RP
1051@code{SECTIONS} command.
1052@end itemize
1053
1054The first two cases are equivalent in effect---both define a symbol with
1055an absolute address; the last case defines a symbol whose address is
1056relative to a particular section (@pxref{SECTIONS}).
1057
2c5c0674
RP
1058@cindex absolute and relocatable symbols
1059@cindex relocatable and absolute symbols
1060@cindex symbols, relocatable and absolute
1061When a linker expression is evaluated and assigned to a variable, it is
1062given either an absolute or a relocatable type. An absolute expression
1063type is one in which the symbol contains the value that it will have in
1064the output file, a relocateable expression type is one in which the
1065value is expressed as a fixed offset from the base of a section.
b4d4e8e3
RP
1066
1067The type of the expression is controlled by its position in the script
2c5c0674
RP
1068file. A symbol assigned within a section definition is created relative
1069to the base of the section; a symbol assigned in any other place is
1070created as an absolute symbol. Since a symbol created within a
1071section definition is relative to the base of the section, it
1072will remain relocatable if relocatable output is requested. A symbol
1073may be created with an absolute value even when assigned to within a
1074section definition by using the absolute assignment function
1075@code{ABSOLUTE}. For example, to create an absolute symbol whose address
1076is the last byte of an output section named @code{.data}:
b4d4e8e3 1077@example
2c5c0674 1078SECTIONS@{ @dots{}
b4d4e8e3
RP
1079.data :
1080 @{
1081 *(.data)
1082 _edata = ABSOLUTE(.) ;
1083 @}
2c5c0674 1084@dots{} @}
b4d4e8e3
RP
1085@end example
1086
2c5c0674
RP
1087The linker tries to put off the evaluation of an assignment until all
1088the terms in the source expression are known (@pxref{Evaluation}). For
1089instance the sizes of sections cannot be known until after allocation,
1090so assignments dependent upon these are not performed until after
1091allocation. Some expressions, such as those depending upon the location
1092counter @dfn{dot}, @samp{.} must be evaluated during allocation. If the
1093result of an expression is required, but the value is not available,
1094then an error results. For example, a script like the following
b4d4e8e3 1095@example
2c5c0674 1096SECTIONS @{ @dots{}
b4d4e8e3
RP
1097 text 9+this_isnt_constant:
1098 @{ @dots{}
1099 @}
2c5c0674 1100@dots{} @}
b4d4e8e3 1101@end example
2c5c0674
RP
1102@kindex Non constant expression
1103@noindent
1104will cause the error message ``@code{Non constant expression for initial
b4d4e8e3
RP
1105address}''.
1106
2c5c0674
RP
1107@node Built-ins, , Assignment, Expressions
1108@subsection Built-In Functions
1109@cindex functions in expression language
1110The command language includes a number of special purpose built-in
1111functions for use in link script expressions.
1112@table @code
1113@item ABSOLUTE(@var{exp})
1114@kindex ABSOLUTE(@var{exp})
1115@cindex expression, absolute
1116returns the absolute value of the expression @var{exp}. Primarily
1117useful to assign an absolute value to a symbol within a section
1118definition, where symbol values are normally section-relative.
1119
1120@item ADDR(@var{section})
1121@kindex ADDR(@var{section})
1122@cindex section address
1123returns the absolute address of the named @var{section}. Your script must
b4d4e8e3
RP
1124previously have defined the location of that section. In the following
1125example the @code{symbol_1} and @code{symbol_2} are assigned identical
1126values:
f22eee08 1127@example
2c5c0674 1128SECTIONS@{ @dots{}
f22eee08
RP
1129 .output1:
1130 @{
2c5c0674
RP
1131 start_of_output_1 = ABSOLUTE(.);
1132 @dots{}
f22eee08
RP
1133 @}
1134 .output:
1135 @{
1136 symbol_1 = ADDR(.output1);
1137 symbol_2 = start_of_output_1;
1138 @}
2c5c0674
RP
1139@dots{} @}
1140@end example
1141
1142@item ALIGN(@var{exp})
1143@kindex ALIGN(@var{exp})
1144@cindex rounding up location counter
1145returns the result of the current location counter (@code{.}) aligned to
1146the next @var{exp} boundary. @var{exp} must be an expression whose
1147value is a power of two. This is equivalent to
1148@example
1149(. + @var{exp} -1) & ~(@var{exp}-1)
1150@end example
1151
1152@code{ALIGN} doesn't change the value of the location counter---it just
1153does arithmetic on it. As an example, to align the output @code{.data}
1154section to the next @code{0x2000} byte boundary after the preceding
1155section and to set a variable within the section to the next
1156@code{0x8000} boundary after the input sections:
1157@example
1158SECTIONS@{ @dots{}
1159 .data ALIGN(0x2000): @{
1160 *(.data)
1161 variable = ALIGN(0x8000);
1162 @}
1163@dots{} @}
1164@end example
1165@noindent
1166The first use of @code{ALIGN} in this example specifies the location of
1167a section because it is used as the optional @var{start} attribute of a
1168section definition (@pxref{Section Options}). The second use simply
1169defines the value of a variable.
1170
1171The built-in @code{NEXT} is closely related to @code{ALIGN}.
1172
1173@item DEFINED(@var{symbol})
1174@kindex DEFINED(@var{symbol})
1175@cindex symbol defaults
1176Returns @code{1} if @var{symbol} is in the linker global symbol table and is
1177defined, otherwise it returns @code{0}. You can use this to provide default
1178values for symbols. For example, this command-file fragment shows how
1179to set a global symbol @code{begin} to the first location in the
1180@code{.text} section---but if a symbol called @code{begin} already
1181existed, its value is preserved:
c8072296 1182@smallexample
2c5c0674
RP
1183SECTIONS@{ @dots{}
1184 .text: @{
1185 begin = DEFINED(begin) ? begin : . ;
1186 @dots{}
1187 @}
1188@dots{} @}
c8072296 1189@end smallexample
f22eee08 1190
2c5c0674
RP
1191@item NEXT(@var{exp})
1192@kindex NEXT(@var{exp})
1193@cindex unallocated address, next
1194Returns the next unallocated address that is a multiple of @var{exp}.
1195This command is closely related to @code{ALIGN(@var{exp})}; unless you
1196use the @code{MEMORY} command to define discontinuous memory for the
1197output file, the two commands are equivalent.
1198
1199@item SIZEOF(@var{section})
1200@kindex SIZEOF(@var{section})
1201@cindex section size
1202returns the size in bytes of the named @var{section}, if the section has
f22eee08
RP
1203been allocated. In the following example the @code{symbol_1} and
1204@code{symbol_2} are assigned identical values:
1205@example
2c5c0674 1206SECTIONS@{ @dots{}
f22eee08
RP
1207 .output @{
1208 .start = . ;
2c5c0674 1209 @dots{}
f22eee08
RP
1210 .end = .;
1211 @}
1212 symbol_1 = .end - .start;
1213 symbol_2 = SIZEOF(.output);
2c5c0674 1214@dots{} @}
f22eee08 1215
f22eee08 1216@end example
b4d4e8e3 1217
2c5c0674
RP
1218@item SIZEOF_HEADERS
1219@kindex SIZEOF_HEADERS
1220@cindex header size
1221@itemx sizeof_headers
1222@kindex sizeof_headers
1223the size in bytes of the output file's headers. You can use this number
1224as the start address of the first section, if you choose, to facilitate
1225paging.
1226
1227@end table
1228
1229@node MEMORY, SECTIONS, Expressions, Commands
b4d4e8e3 1230@section MEMORY Command
2c5c0674
RP
1231@kindex MEMORY
1232@cindex regions of memory
1233@cindex discontinuous memory
1234@cindex allocating memory
b4d4e8e3
RP
1235The linker's default configuration permits allocation of all memory.
1236You can override this by using the @code{MEMORY} command. The
1237@code{MEMORY} command describes the location and size of blocks of
1238memory in the target. By using it carefully, you can describe which
1239memory regions may be used by the linker, and which memory regions it
1240must avoid. The linker does not shuffle sections to fit into the
1241available regions, but does move the requested sections into the correct
1242regions and issue errors when the regions become too full.
1243
1244Command files may contain at most one use of the @code{MEMORY}
1245command; however, you can define as many blocks of memory within it as
1246you wish. The syntax is:
c8072296 1247
f22eee08 1248@example
b4d4e8e3
RP
1249MEMORY
1250 @{
1251 @var{name} (@var{attr}): ORIGIN = @var{origin}, LENGTH = @var{len}
2c5c0674 1252 @dots{}
b4d4e8e3 1253 @}
f22eee08
RP
1254@end example
1255@table @code
1256@item @var{name}
2c5c0674 1257@cindex naming memory regions
f22eee08
RP
1258is a name used internally by the linker to refer to the region. Any
1259symbol name may be used. The region names are stored in a separate
1260name space, and will not conflict with symbols, filenames or section
b4d4e8e3
RP
1261names. Use distinct names to specify multiple regions.
1262@item (@var{attr})
2c5c0674
RP
1263@cindex memory region attributes
1264is an optional list of attributes, permitted for compatibility with the
1265AT&T linker but not used by @code{gld} beyond checking that the
1266attribute list is valid. Valid attribute lists must be made up of the
1267characters ``@code{LIRWX}''. If you omit the attribute list, you may
1268omit the parentheses around it as well.
f22eee08 1269@item @var{origin}
2c5c0674
RP
1270@kindex ORIGIN=
1271@kindex o=
1272@kindex org=
b4d4e8e3
RP
1273is the start address of the region in physical memory. It is expressed as
1274an expression, which must evaluate to a constant before
f22eee08
RP
1275memory allocation is performed. The keyword @code{ORIGIN} may be
1276abbreviated to @code{org} or @code{o}.
1277@item @var{len}
2c5c0674
RP
1278@kindex LENGTH=
1279@kindex len=
1280@kindex l=
b4d4e8e3 1281is the size in bytes of the region (an expression).
2c5c0674 1282The keyword @code{LENGTH} may be abbreviated to @code{len} or @code{l}.
f22eee08
RP
1283@end table
1284
1285For example, to specify that memory has two regions available for
2c5c0674
RP
1286allocation---one starting at @code{0} for 256 kilobytes, and the other
1287starting at @code{0x40000000} for four megabytes:
f22eee08
RP
1288
1289@example
b4d4e8e3
RP
1290MEMORY
1291 @{
1292 rom : ORIGIN= 0, LENGTH = 256K
1293 ram : org= 0x40000000, l = 4M
1294 @}
f22eee08
RP
1295@end example
1296
b4d4e8e3 1297Once you have defined a region of memory named @var{mem}, you can direct
2c5c0674
RP
1298specific output sections there by using a command ending in
1299@samp{>@var{mem}} within the @code{SECTIONS} command (@pxref{Section
1300Options}). If the combined output sections directed to a region are too
1301big for the region, the linker will issue an error message.
b4d4e8e3 1302
2c5c0674 1303@node SECTIONS, Entry Point, MEMORY, Commands
b4d4e8e3 1304@section SECTIONS Command
2c5c0674 1305@kindex SECTIONS
b4d4e8e3
RP
1306The @code{SECTIONS} command controls exactly where input sections are
1307placed into output sections, their order and to which output sections
1308they are allocated.
1309
1310You may use at most one @code{SECTIONS} command in a commands file,
1311but you can have as many statements within it as you wish. Statements
1312within the @code{SECTIONS} command can do one of three things:
1313@itemize @bullet
1314@item
1315define the entry point;
1316@item
1317assign a value to a symbol;
1318@item
1319describe the placement of a named output section, and what input
1320sections make it up.
1321@end itemize
1322
1323The first two possibilities---defining the entry point, and defining
2c5c0674 1324symbols---can also be done outside the @code{SECTIONS} command:
b4d4e8e3
RP
1325@pxref{Entry Point}, @pxref{Assignment}. They are permitted here as
1326well for your convenience in reading the script, so that symbols or the
1327entry point can be defined at meaningful points in your output-file
1328layout.
f22eee08 1329
b4d4e8e3 1330When no @code{SECTIONS} command is specified, the default action
f22eee08 1331of the linker is to place each input section into an identically named
b4d4e8e3
RP
1332output section in the order that the sections are first encountered in
1333the input files; if all input sections are present in the first file,
1334for example, the order of sections in the output file will match the
1335order in the first input file.
1336
2c5c0674 1337@menu
c8072296
RP
1338* Section Definition:: Section Definitions
1339* Section Contents:: Section Contents
1340* Section Options:: Optional Section Attributes
2c5c0674
RP
1341@end menu
1342
1343@node Section Definition, Section Contents, SECTIONS, SECTIONS
b4d4e8e3 1344@subsection Section Definitions
2c5c0674 1345@cindex section definition
b4d4e8e3
RP
1346The most frequently used statement in the @code{SECTIONS} command is
1347the @dfn{section definition}, which you can use to specify the
1348properties of an output section: its location, alignment, contents,
1349fill pattern, and target memory region can all be specified. Most of
1350these specifications are optional; the simplest form of a section
1351definition is
1352@example
2c5c0674 1353SECTIONS @{ @dots{}
b4d4e8e3
RP
1354@var{secname} : @{
1355 @var{contents}
1356 @}
2c5c0674 1357@dots{} @}
b4d4e8e3 1358@end example
2c5c0674 1359@cindex naming output sections
b4d4e8e3
RP
1360@noindent
1361@var{secname} is the name of the output section, and @var{contents} a
1362specification of what goes there---for example a list of input files or
1363sections of input files. As you might assume, the whitespace shown is
1364optional; you do need the colon @samp{:} and the braces @samp{@{@}},
1365however.
1366
1367@var{secname} must meet the constraints of your output format. In
1368formats which only support a limited number of sections, such as
1369@code{a.out}, the name must be one of the names supported by the format
2c5c0674
RP
1370(@code{a.out}, for example, allows only @code{.text}, @code{.data} or
1371@code{.bss}). If the output format supports any number of sections, but
1372with numbers and not names (as is the case for Oasys), the name should be
1373supplied as a quoted numeric string. A section name may consist of any
1374sequence characters, but any name which does not conform to the standard
1375@code{gld} symbol name syntax must be quoted.
1376
1377@node Section Contents, Section Options, Section Definition, SECTIONS
b4d4e8e3 1378@subsection Section Contents
2c5c0674 1379@cindex contents of a section
b4d4e8e3
RP
1380In a section definition, you can specify the contents of an output section by
1381listing particular object files; by listing particular input-file
1382sections; or a combination of the two. You can also place arbitrary
1383data in the section, and define symbols relative to the beginning of the
1384section.
1385
1386The @var{contents} of a section definition may include any of the
1387following kinds of statement. You can include as many of these as you
1388like in a single section definition, separated from one another by
1389whitespace.
f22eee08 1390
b4d4e8e3 1391@table @code
b4d4e8e3 1392@item @var{filename}
2c5c0674
RP
1393@kindex @var{filename}
1394@cindex input files, section defn
1395@cindex files, including in output sections
b4d4e8e3 1396You may simply name a particular input file to be placed in the current
2c5c0674
RP
1397output section; @emph{all} sections from that file are placed in the
1398current section definition. To specify a list of particular files by
1399name:
f22eee08 1400@example
b4d4e8e3 1401.data: @{ afile.o bfile.o cfile.o @}
f22eee08 1402@end example
2c5c0674
RP
1403@noindent
1404The example also illustrates that multiple statements can be included in
1405the contents of a section definition, since each filename is a separate
1406statement.
f22eee08 1407
b4d4e8e3
RP
1408If the file name has already been mentioned in another section
1409definition, with an explicit section name list, then only those sections
1410which have not yet been allocated are used.
1411
2c5c0674
RP
1412@item @var{filename}( @var{section} )
1413@itemx @var{filename}( @var{section}, @var{section}, @dots{} )
1414@itemx @var{filename}( @var{section} @var{section} @dots{} )
1415@kindex @var{filename}(@var{section})
1416@cindex files and sections, section defn
1417You can name one or more sections from your input files, for
1418insertion in the current output section. If you wish to specify a list
1419of input-file sections inside the parentheses, you may separate the
1420section names by either commas or whitespace.
1421
b4d4e8e3
RP
1422@item * (@var{section})
1423@itemx * (@var{section}, @var{section}, @dots{})
2c5c0674
RP
1424@itemx * (@var{section} @var{section} @dots{}
1425@cindex input sections to output section
1426@kindex *(@var{section})
b4d4e8e3
RP
1427Instead of explicitly naming particular input files in a link control
1428script, you can refer to @emph{all} files from the @code{gld} command
1429line: use @samp{*} instead of a particular filename before the
1430parenthesized input-file section list.
1431
1432For example, to copy sections @code{1} through @code{4} from a Oasys file
1433into the @code{.text} section of an @code{a.out} file, and sections @code{13}
1434and @code{14} into the @code{.data} section:
1435@example
2c5c0674 1436SECTIONS @{
b4d4e8e3
RP
1437 .text :@{
1438 *("1" "2" "3" "4")
1439 @}
f22eee08 1440
b4d4e8e3
RP
1441 .data :@{
1442 *("13" "14")
f22eee08 1443 @}
b4d4e8e3 1444@}
f22eee08
RP
1445@end example
1446
b4d4e8e3
RP
1447If you have already explicitly included some files by name, @samp{*}
1448refers to all @emph{remaining} files---those whose places in the output
1449file have not yet been defined.
1450
1451@item [ @var{section} ]
1452@itemx [ @var{section}, @var{section}, @dots{} ]
1453@itemx [ @var{section} @var{section} @dots{} ]
2c5c0674 1454@kindex [ @var{sections} ]
b4d4e8e3
RP
1455This is an alternate notation to specify named sections from all
1456unallocated input files; its effect is exactly the same as that of
1457@samp{* (@var{section}@dots{})}
1458
1459@item @var{filename}@code{( COMMON )}
1460@itemx [ COMMON ]
2c5c0674
RP
1461@kindex [COMMON]
1462@cindex uninitialized data
1463@cindex commons in output
b4d4e8e3
RP
1464Specify where in your output file to place uninitialized data
1465with this notation. @code{[COMMON]} by itself refers to all
1466uninitialized data from all input files (so far as it is not yet
1467allocated); @var{filename}@code{(COMMON)} refers to uninitialized data
1468from a particular file. Both are special cases of the general
1469mechanisms for specifying where to place input-file sections:
1470@code{gld} permits you to refer to uninitialized data as if it
1471were in an input-file section named @code{COMMON}, regardless of the
1472input file's format.
1473@end table
1474
2c5c0674 1475For example, the following command script arranges the output file into
b4d4e8e3
RP
1476three consecutive sections, named @code{.text}, @code{.data}, and
1477@code{.bss}, taking the input for each from the correspondingly named
1478sections of all the input files:
f22eee08 1479@example
2c5c0674
RP
1480SECTIONS @{
1481 .text: @{ *(.text) @}
1482 .data: @{ *(.data) @}
1483 .bss: @{ *(.bss) [COMMON] @}
1484@}
f22eee08 1485@end example
b4d4e8e3
RP
1486
1487The following example reads all of the sections from file @code{all.o}
1488and places them at the start of output section @code{outputa} which
1489starts at location @code{0x10000}. All of section @code{.input1} from
1490file @code{foo.o} follows immediately, in the same output section. All
1491of section @code{.input2} from @code{foo.o} goes into output section
1492@code{outputb}, followed by section @code{.input1} from @code{foo1.o}.
1493All of the remaining @code{.input1} and @code{.input2} sections from any
1494files are written to output section @code{outputc}.
1495
1496@example
2c5c0674 1497SECTIONS @{
b4d4e8e3
RP
1498 outputa 0x10000 :
1499 @{
1500 all.o
1501 foo.o (.input1)
f22eee08 1502 @}
b4d4e8e3
RP
1503 outputb :
1504 @{
1505 foo.o (.input2)
1506 foo1.o (.input1)
f22eee08 1507 @}
b4d4e8e3
RP
1508 outputc :
1509 @{
1510 *(.input1)
1511 *(.input2)
f22eee08 1512 @}
2c5c0674 1513@}
b4d4e8e3
RP
1514@end example
1515
1516There are still more kinds of statements permitted in the contents of
2c5c0674 1517output section definitions. The foregoing statements permitted you to
b4d4e8e3
RP
1518arrange, in your output file, data originating from your input files.
1519You can also place data directly in an output section from the link
1520command script. Most of these additional statements involve
1521expressions; @pxref{Expressions}. Although these statements are shown
1522separately here for ease of presentation, no such segregation is needed
1523within a section definition in the @code{SECTIONS} command; you can
1524intermix them freely with any of the statements we've just described.
f22eee08 1525
b4d4e8e3
RP
1526@table @code
1527@item CREATE_OBJECT_SYMBOLS
2c5c0674
RP
1528@kindex CREATE_OBJECT_SYMBOLS
1529@cindex input filename symbols
1530@cindex filename symbols
1531instructs the linker to create a symbol for each input file
1532in the current section, set with the address of the first byte of
f22eee08 1533data written from the input file. For instance, with @code{a.out}
b4d4e8e3
RP
1534files it is conventional to have a symbol for each input file. You can
1535accomplish this by defining the output @code{.text} section as follows:
1536@example
1537SECTIONS @{
1538 .text 0x2020 :
1539 @{
1540 CREATE_OBJECT_SYMBOLS
1541 *(.text)
1542 _etext = ALIGN(0x2000);
f22eee08 1543 @}
2c5c0674
RP
1544 @dots{}
1545@}
f22eee08 1546@end example
b4d4e8e3
RP
1547
1548If @code{objsym} is a file containing this script, and @code{a.o},
1549@code{b.o}, @code{c.o}, and @code{d.o} are four input files with
1550contents like the following---
f22eee08 1551@example
b4d4e8e3
RP
1552/* a.c */
1553
2c5c0674 1554afunction() @{ @}
b4d4e8e3
RP
1555int adata=1;
1556int abss;
1557@end example
f22eee08 1558
b4d4e8e3
RP
1559@noindent
1560@samp{gld -M sample a.o b.o c.o d.o} would create a map like this,
1561containing symbols matching the object file names:
1562@example
f22eee08
RP
156300000000 A __DYNAMIC
156400004020 B _abss
156500004000 D _adata
156600002020 T _afunction
156700004024 B _bbss
156800004008 D _bdata
156900002038 T _bfunction
157000004028 B _cbss
157100004010 D _cdata
157200002050 T _cfunction
15730000402c B _dbss
157400004018 D _ddata
157500002068 T _dfunction
157600004020 D _edata
157700004030 B _end
157800004000 T _etext
157900002020 t a.o
158000002038 t b.o
158100002050 t c.o
158200002068 t d.o
f22eee08
RP
1583@end example
1584
b4d4e8e3 1585@item @var{symbol} = @var{expression} ;
2c5c0674 1586@kindex @var{symbol} = @var{expression} ;
b4d4e8e3 1587@itemx @var{symbol} @var{f}= @var{expression} ;
2c5c0674
RP
1588@kindex @var{symbol} @var{f}= @var{expression} ;
1589@var{symbol} is any symbol name (@pxref{Symbols}). ``@var{f}=''
1590refers to any of the operators @code{&= += -= *= /=} which combine
1591arithmetic and assignment.
1592
1593@cindex assignment, in section defn
1594When you assign a value to a symbol within a particular section
1595definition, the value is relative to the beginning of the section
1596(@pxref{Assignment}). If you write
b4d4e8e3 1597@example
2c5c0674 1598SECTIONS @{
b4d4e8e3 1599 abs = 14 ;
2c5c0674
RP
1600 @dots{}
1601 .data: @{ @dots{} rel = 14 ; @dots{} @}
b4d4e8e3 1602 abs2 = 14 + ADDR(.data);
2c5c0674
RP
1603 @dots{}
1604@}
f22eee08 1605@end example
2c5c0674 1606@c FIXME: Try above example!
b4d4e8e3
RP
1607@noindent
1608@code{abs} and @var{rel} do not have the same value; @code{rel} has the
1609same value as @code{abs2}.
1610
b4d4e8e3 1611@item BYTE(@var{expression})
2c5c0674 1612@kindex BYTE(@var{expression})
b4d4e8e3 1613@itemx SHORT(@var{expression})
2c5c0674 1614@kindex SHORT(@var{expression})
b4d4e8e3 1615@itemx LONG(@var{expression})
2c5c0674
RP
1616@kindex LONG(@var{expression})
1617@cindex direct output
b4d4e8e3
RP
1618By including one of these three statements in a section definition, you
1619can explicitly place one, two, or four bytes (respectively) at the
1620current address of that section. Multiple-byte quantities are
1621represented in whatever byte order is appropriate for the output file
1622format (@pxref{BFD}).
1623
1624@item FILL(@var{expression})
2c5c0674
RP
1625@kindex FILL(@var{expression})
1626@cindex holes, filling
1627@cindex unspecified memory
b4d4e8e3
RP
1628Specifies the ``fill pattern'' for the current section. Any otherwise
1629unspecified regions of memory within the section (for example, regions
1630you skip over by assigning a new value to the location counter @samp{.})
1631are filled with the two least significant bytes from the
1632@var{expression} argument. A @code{FILL} statement covers memory
1633locations @emph{after} the point it occurs in the section definition; by
1634including more than one @code{FILL} statement, you can have different
1635fill patterns in different parts of an output section.
1636@end table
1637
2c5c0674 1638@node Section Options, , Section Contents, SECTIONS
b4d4e8e3 1639@subsection Optional Section Attributes
2c5c0674 1640@cindex section defn, full syntax
b4d4e8e3
RP
1641Here is the full syntax of a section definition, including all the
1642optional portions:
1643
f22eee08 1644@example
2c5c0674
RP
1645SECTIONS @{
1646@dots{}
1647@var{secname} @var{start} BLOCK(@var{align}) : @{ @var{contents} @} =@var{fill} >@var{region}
1648@dots{}
b4d4e8e3
RP
1649@}
1650@end example
1651
1652@var{secname} and @var{contents} are required. @xref{Section
2c5c0674
RP
1653Definition}, and @pxref{Section Contents} for details on @var{contents}.
1654The remaining elements---@var{start}, @code{BLOCK(@var{align)}},
1655@code{=@var{fill}}, and @code{>@var{region}}---are all optional.
f22eee08 1656
b4d4e8e3
RP
1657@table @code
1658@item @var{start}
2c5c0674
RP
1659@cindex start address, section
1660@cindex section start
1661@cindex section address
b4d4e8e3
RP
1662You can force the output section to be loaded at a specified address by
1663specifying @var{start} immediately following the section name.
1664@var{start} can be represented as any expression. The following
1665example generates section @var{output} at location
1666@code{0x40000000}:
1667@example
1668SECTIONS @{
2c5c0674 1669 @dots{}
b4d4e8e3
RP
1670 output 0x40000000: @{
1671 @dots{}
1672 @}
2c5c0674 1673 @dots{}
b4d4e8e3 1674@}
f22eee08 1675@end example
f22eee08 1676
b4d4e8e3 1677@item BLOCK(@var{align})
2c5c0674
RP
1678@kindex BLOCK(@var{align})
1679@cindex section alignment
1680@cindex aligning sections
1681You can include @code{BLOCK()} specification to advance the location of
1682the location counter @code{.} prior to the beginning of the section, so
1683that the section will begin at the specified alignment. @var{align} is
1684an expression.
f22eee08 1685
b4d4e8e3 1686@item =@var{fill}
2c5c0674
RP
1687@kindex =@var{fill}
1688@cindex section fill pattern
1689@cindex fill pattern, entire section
b4d4e8e3
RP
1690You may use any expression to specify @var{fill}. Including
1691@code{=@var{fill}} in a section definition specifies the initial fill
1692value for that section. Any unallocated holes in the current output
1693section when written to the output file will be filled with the two
1694least significant bytes of the value, repeated as necessary. You can
1695also change the fill value with a @code{FILL} statement in the
1696@var{contents} of a section definition.
f22eee08 1697
b4d4e8e3 1698@item >@var{region}
2c5c0674
RP
1699@kindex >@var{region}
1700@cindex section, assigning to memory region
1701@cindex memory regions and sections
1702Assign this section to a previously defined region of memory.
1703@xref{MEMORY}.
f22eee08 1704
f22eee08 1705@end table
b4d4e8e3 1706
2c5c0674 1707@node Entry Point, Other Commands, SECTIONS, Commands
b4d4e8e3 1708@section The Entry Point
2c5c0674
RP
1709@kindex ENTRY(@var{symbol})
1710@cindex start of execution
1711@cindex first instruction
b4d4e8e3
RP
1712The linker command language includes a command specifically for
1713defining the first executable instruction in an output file (its
1714@dfn{entry point}). Its argument is a symbol name:
f22eee08 1715@example
b4d4e8e3 1716ENTRY(@var{symbol})
f22eee08 1717@end example
b4d4e8e3
RP
1718
1719Like symbol assignments, the @code{ENTRY} command may be placed either
1720as an independent command in the command file, or among the section
1721definitions within the @code{SECTIONS} command---whatever makes the most
1722sense for your layout.
1723
2c5c0674 1724@cindex entry point, defaults
b4d4e8e3
RP
1725@code{ENTRY} is only one of several ways of choosing the entry point.
1726You may indicate it in any of the following ways (shown in descending
1727order of priority: methods higher in the list override methods lower down).
f22eee08
RP
1728@itemize @bullet
1729@item
b4d4e8e3 1730the @code{-e} @var{entry} command-line option;
f22eee08 1731@item
2c5c0674 1732the @code{ENTRY(@var{symbol}} command in a linker control script;
f22eee08 1733@item
b4d4e8e3 1734the value of the symbol @code{start}, if present;
f22eee08 1735@item
b4d4e8e3 1736the value of the symbol @code{_main}, if present;
f22eee08 1737@item
b4d4e8e3 1738the address of the first byte of the @code{.text} section, if present;
f22eee08 1739@item
b4d4e8e3 1740The address @code{0}.
f22eee08 1741@end itemize
b4d4e8e3 1742
2c5c0674
RP
1743For example, you can use these rules to generate an entry point with an
1744assignment statement: if no symbol @code{start} is defined within your
1745input files, you can simply define it, assigning it an appropriate
1746value---
f22eee08 1747@example
b4d4e8e3 1748start = 0x2020;
f22eee08 1749@end example
b4d4e8e3
RP
1750
1751@noindent
1752The example shows an absolute address, but you can use any expression.
1753For example, if your input object files use some other symbol-name
1754convention for the entry point, you can just assign the value of
1755whatever symbol contains the start address to @code{start}:
f22eee08 1756@example
b4d4e8e3 1757start = other_symbol;
f22eee08 1758@end example
f22eee08 1759
2c5c0674 1760@node Other Commands, , Entry Point, Commands
b4d4e8e3
RP
1761@section Other Commands
1762The command language includes a number of other commands that you can
1763use for specialized purposes. They are similar in purpose to
1764command-line options.
1765
1766@table @code
1767@item FLOAT
2c5c0674 1768@kindex FLOAT
b4d4e8e3 1769@itemx NOFLOAT
2c5c0674
RP
1770@kindex NOFLOAT
1771These keywords were used in some older linkers to request a particular
1772math subroutine library. @code{gld} doesn't use the keywords, assuming
1773instead that any necessary subroutines are in libraries specified using
1774the general mechanisms for linking to archives; but to permit the use of
1775scripts that were written for the older linkers, the keywords
1776@code{FLOAT} and @code{NOFLOAT} are accepted and ignored.
1777
1778@item FORCE_COMMON_ALLOCATION
1779@kindex FORCE_COMMON_ALLOCATION
1780@cindex common allocation
1781This command has the same effect as the @code{-d} command-line option:
1782to make @code{ld} assign space to common symbols even if a relocatable
1783output file is specified (@code{-r}).
b4d4e8e3
RP
1784
1785@item HLL ( @var{file}, @var{file}, @dots{} )
2c5c0674 1786@kindex HLL ( @var{files} )
b4d4e8e3 1787@itemx HLL ( @var{file} @var{file} @dots{} )
2c5c0674
RP
1788@itemx HLL ( )
1789Include ``high-level libraries'' or archives as input files in the link.
1790Using @code{HLL(@var{file}} in a linker script is equivalent to
1791including @code{-l}@var{file} on the command line.
1792
1793@cindex @code{lnk960} command @code{HLL}
1794The @code{HLL} command is only supported when @code{gld} emulates
1795@code{lnk960}, as specified by the @code{LDEMULATION} environment
1796variable.
b4d4e8e3
RP
1797
1798@item INPUT ( @var{file}, @var{file}, @dots{} )
2c5c0674 1799@kindex INPUT ( @var{files} )
b4d4e8e3 1800@itemx INPUT ( @var{file} @var{file} @dots{} )
2c5c0674
RP
1801@cindex binary input files
1802Use this command to include binary input files in the link, without
1803including them in a particular section definition. Files specified this
1804way are treated identically to object files listed on the command line.
b4d4e8e3 1805
2c5c0674 1806@ignore
b4d4e8e3 1807@item MAP ( @var{name} )
2c5c0674
RP
1808@kindex MAP ( @var{name} )
1809@c MAP(...) appears to look for an F in the arg, ignoring all other
1810@c chars; if it finds one, it sets "map_option_f" to true. But nothing
1811@c checks map_option_f. Apparently a stub for the future...
1812@end ignore
b4d4e8e3
RP
1813
1814@item OUTPUT ( @var{filename} )
2c5c0674
RP
1815@kindex OUTPUT ( @var{filename} )
1816@cindex naming the output file
1817Name the link output file @var{filename}. The effect of
1818@code{OUTPUT(@var{filename})} is identical to the effect of
1819@w{@code{-o @var{filename}}}, and whichever is encountered last will
1820control the name actually used to name the output file. In particular,
1821you can use this command to supply a default output-file name other than
1822@code{a.out}.
1823
1824@item OUTPUT_ARCH ( @var{bfdname} )
1825@kindex OUTPUT_ARCH ( @var{bfdname} )
1826@cindex machine architecture, output
1827Specify a particular output machine architecture, with one of the names
1828used by the BFD back-end routines (@pxref{BFD}). This command is often
1829unnecessary; the architecture is most often set implicitly by either the
1830system BFD configuration or as a side effect of the @code{OUTPUT_FORMAT}
1831command. @refill
1832
1833@item OUTPUT_FORMAT ( @var{bfdname} )
1834@kindex OUTPUT_FORMAT ( @var{bfdname} )
1835@cindex format, output file
1836Specify a particular output format, with one of the names used by the
1837BFD back-end routines (@pxref{BFD}). This selection will only affect
1838the output file; the related command @code{TARGET} affects primarily
1839input files.@refill
1840
1841@item SEARCH_DIR ( @var{path} )
1842@kindex SEARCH_DIR ( @var{path} )
1843@cindex path for libraries
1844@cindex search path, libraries
1845Add @var{path} to the list of paths where @code{gld} looks for
1846archive libraries. @code{SEARCH_DIR(@var{path})} has the same
1847effect as @code{-L@var{path})} on the command line.
1848
1849@item STARTUP ( @var{filename} )
1850@kindex STARTUP ( @var{filename} )
1851@cindex first input file
1852Ensure that @var{filename} is the first input file used in the link
1853process.
b4d4e8e3
RP
1854
1855@item SYSLIB ( @var{file}, @var{file}, @dots{} )
2c5c0674 1856@kindex SYSLIB ( @var{file}, @var{file}, @dots{} )
b4d4e8e3 1857@itemx SYSLIB ( @var{file} @var{file} @dots{} )
2c5c0674
RP
1858Use the named @var{file}s as binary input files, searching for them in
1859the same list of paths as archives.
1860
1861@cindex @code{lnk960} command @code{SYSLIB}
1862The @code{SYSLIB} command is only supported when @code{gld} emulates
1863@code{lnk960}, as specified by the @code{LDEMULATION} environment
1864variable.
b4d4e8e3
RP
1865
1866@item TARGET ( @var{format} )
2c5c0674
RP
1867@cindex input file format
1868@kindex TARGET ( @var{format} )
1869Change the input-file object code format (like the command-line option
1870@code{-b} or its synonym @code{-format}). The argument @var{format} is
1871one of the strings used by BFD to name binary formats. In the current
1872@code{gld} implementation, if @code{TARGET} is specified but
1873@code{OUTPUT_FORMAT} is not, the last @code{TARGET} argument is also
1874used as the default format for the @code{gld} output file.
1875@xref{BFD}.@refill
1876
1877@kindex GNUTARGET
1878If you don't use the @code{TARGET} command, @code{gld} uses the value of
1879the environment variable @code{GNUTARGET}, if available, to select the
1880output file format. If that variable is also absent, @code{gld} uses
1881the default format configured for your machine in the BFD libraries.
b4d4e8e3
RP
1882
1883@end table
1884
2c5c0674 1885@node BFD, Index, Commands, Top
f22eee08
RP
1886@chapter BFD
1887
2c5c0674
RP
1888@cindex back end
1889@cindex object file management
1890The linker accesses object and archive files using the BFD libraries.
1891These libraries allow the linker to use the same routines to operate on
1892object files whatever the object file format. A different object file
1893format can be supported simply by creating a new BFD back end and adding
1894it to the library. BFD supports the following combinations of
1895architectures (row labels below) and object formats (column headings):
1896@cindex formats available
1897@cindex architectures available
c8072296 1898@c TEXI2ROFF-KILL
2c5c0674 1899@ifinfo
c8072296 1900@c END TEXI2ROFF-KILL
2c5c0674 1901@example
2c5c0674
RP
1902 |ieee
1903 | |oasys
1904 | | |a.out-generic-little
1905 | | | |a.out-generic-big
1906 | | | | |m88kbcs
1907 | | | | | |srec
1908 | | | | | | |coff-Intel-little
1909 | | | | | | | |coff-Intel-big
1910 | | | | | | | | |b.out.little
c8072296 1911 | | | | | | | | | |b.out.big
2c5c0674
RP
1912 | | | | | | | | | |
1913 m68k|**|**| | | |**| | | |
1914 vax|**|**| | | |**| | | |
1915 i960|**|**| | | |**|**|**|**|**
1916 a29k|**|**|**|**| |**| | | |
1917 sparc|**|**|**|**| |**| | | |
1918 mips|**|**| | | |**| | | |
1919 i386|**|**|**|**| |**| | | |
1920 ns32k|**|**| | | |**| | | |
1921 tahoe|**|**| | | |**| | | |
1922 i860|**|**| | | |**| | | |
1923 romp|**|**| | | |**| | | |
1924 alliant|**|**| | | |**| | | |
1925 convex|**|**| | | |**| | | |
1926 m88k|**|**| | |**|**| | | |
1927 pyramid|**|**| | | |**| | | |
1928 H8/300|**|**| | | |**| | | |
f22eee08 1929
2c5c0674 1930@end example
c8072296 1931@c TEXI2ROFF-KILL
2c5c0674
RP
1932@end ifinfo
1933@tex
1934\def\sqbull{\vrule height12pt width 10pt depth 4pt}
1935\vskip\baselineskip
1936\vbox{\offinterlineskip
1937\halign
c8072296
RP
1938{\strut\hfil #\ &\vrule#&\hskip .5em #\hskip .5em &\vrule#&\hskip .5em #\hskip .5em
1939&\vrule#&\hskip .5em #\hskip .5em &\vrule#&\hskip .5em #\hskip .5em &\vrule#&\hskip .5em #\hskip .5em &\vrule#&\hskip .5em #\hskip .5em &\vrule#&\hskip .5em #\hskip .5em &\vrule#&\hskip .5em #\hskip .5em &\vrule#&\hskip .5em #\hskip .5em &\vrule#&\hskip .5em #\hskip .5em &\vrule#\cr
1940 &&\multispan{20}\quad\vbox{\hrule}\cr
1941 &&\multispan{20}\quad\code{ieee}\hfil\cr
1942 && &&\multispan{18}\quad\code{oasys}\hfil\cr
1943 && && &&\multispan{16}\quad\code{a.out-generic-little}\hfil\cr
1944 && && && &&\multispan{14}\quad\code{a.out-generic-big}\hfil\cr
1945 && && && && &&\multispan{12}\quad\code{m88kbcs}\hfil\cr
1946 && && && && && &&\multispan{10}\quad\code{srec}\hfil\cr
1947 && && && && && && &&\multispan8\quad\code{coff-Intel-little}\hfil\cr
1948 && && && && && && && &&\multispan6\quad\code{coff-Intel-big}\hfil\cr
1949 && && && && && && && &&
2c5c0674 1950&&\multispan4\quad\code{b.out.little}\hfil \cr
c8072296 1951 && && && && && && && && && &&\multispan2\quad\code{b.out.big}\hidewidth\cr
2c5c0674
RP
1952 \code{m68k}&&\sqbull&&\sqbull&& && && &&\sqbull&& && && && &\cr
1953 \code{vax}&&\sqbull&&\sqbull&& && && &&\sqbull&& && && && &\cr
1954 \code{i960}&&\sqbull&&\sqbull&& && && &&\sqbull&&\sqbull&&\sqbull&&\sqbull&&\sqbull &\cr
1955 \code{a29k}&&\sqbull&&\sqbull&&\sqbull&&\sqbull&& &&\sqbull&& && && && &\cr
1956 \code{sparc}&&\sqbull&&\sqbull&&\sqbull&&\sqbull&& &&\sqbull&& && && && &\cr
1957 \code{mips}&&\sqbull&&\sqbull&& && && &&\sqbull&& && && && &\cr
1958 \code{i386}&&\sqbull&&\sqbull&&\sqbull&&\sqbull&& &&\sqbull&& && && && &\cr
1959 \code{ns32k}&&\sqbull&&\sqbull&& && && &&\sqbull&& && && && &\cr
1960 \code{tahoe}&&\sqbull&&\sqbull&& && && &&\sqbull&& && && && &\cr
1961 \code{i860}&&\sqbull&&\sqbull&& && && &&\sqbull&& && && && &\cr
1962 \code{romp}&&\sqbull&&\sqbull&& && && &&\sqbull&& && && && &\cr
1963 \code{alliant}&&\sqbull&&\sqbull&& && && &&\sqbull&& && && && &\cr
1964 \code{convex}&&\sqbull&&\sqbull&& && && &&\sqbull&& && && && &\cr
1965 \code{m88k}&&\sqbull&&\sqbull&& && &&\sqbull&&\sqbull&& && && && &\cr
1966 \code{pyramid}&&\sqbull&&\sqbull&& && && &&\sqbull&& && && && &\cr
1967 \code{H8/300}&&\sqbull&&\sqbull&& && && &&\sqbull&& && && && &\cr
1968}}
1969@end tex
c8072296 1970@c END TEXI2ROFF-KILL
f22eee08 1971
2c5c0674
RP
1972@cindex BFD requirements
1973@cindex requirements for BFD
1974As with most implementations, BFD is a compromise between
f22eee08 1975several conflicting requirements. The major factor influencing
2c5c0674
RP
1976BFD design was efficiency: any time used converting between
1977formats is time which would not have been spent had BFD not
f22eee08 1978been involved. This is partly offset by abstraction payback; since
2c5c0674 1979BFD simplifies applications and back ends, more time and care
f22eee08
RP
1980may be spent optimizing algorithms for a greater speed.
1981
2c5c0674
RP
1982One minor artifact of the BFD solution which you should bear in
1983mind is the potential for information loss. There are two places where
1984useful information can be lost using the BFD mechanism; during
1985conversion and during output. @xref{BFD information loss}.
1986
1987@menu
c8072296
RP
1988* BFD outline:: How it works: an outline of BFD
1989* BFD information loss:: Information Loss
1990* Mechanism:: Mechanism
2c5c0674 1991@end menu
f22eee08 1992
2c5c0674 1993@node BFD outline, BFD information loss, BFD, BFD
b4d4e8e3 1994@section How it works: an outline of BFD
2c5c0674
RP
1995@cindex opening object files
1996When an object file is opened, BFD subroutines automatically
b4d4e8e3
RP
1997determine the format of the input object file, and build a descriptor in
1998memory with pointers to routines that will be used to access elements of
1999the object file's data structures.
f22eee08
RP
2000
2001As different information from the the object files is required
2c5c0674 2002BFD reads from different sections of the file and processes them.
b4d4e8e3 2003For example a very common operation for the linker is processing symbol
2c5c0674 2004tables. Each BFD back end provides a routine for converting
b4d4e8e3
RP
2005between the object file's representation of symbols and an internal
2006canonical format. When the linker asks for the symbol table of an object
2c5c0674 2007file, it calls through the memory pointer to the relevant BFD
b4d4e8e3
RP
2008back end routine which reads and converts the table into a canonical
2009form. The linker then operates upon the common form. When the link is
2010finished and the linker writes the symbol table of the output file,
2c5c0674 2011another BFD back end routine is called which takes the newly
b4d4e8e3
RP
2012created symbol table and converts it into the chosen output format.
2013
2c5c0674 2014@node BFD information loss, Mechanism, BFD outline, BFD
b4d4e8e3
RP
2015@section Information Loss
2016@emph{Information can be lost during output.} The output formats
2c5c0674 2017supported by BFD do not provide identical facilities, and
b4d4e8e3
RP
2018information which may be described in one form has nowhere to go in
2019another format. One example of this is alignment information in
2020@code{b.out}. There is nowhere in an @code{a.out} format file to store
2021alignment information on the contained data, so when a file is linked
2022from @code{b.out} and an @code{a.out} image is produced, alignment
2023information will not propagate to the output file. (The linker will
2024still use the alignment information internally, so the link is performed
2025correctly).
f22eee08
RP
2026
2027Another example is COFF section names. COFF files may contain an
2028unlimited number of sections, each one with a textual section name. If
b4d4e8e3
RP
2029the target of the link is a format which does not have many sections (eg
2030@code{a.out}) or has sections without names (eg the Oasys format) the
2031link cannot be done simply. You can circumvent this problem by
2032describing the desired input-to-output section mapping with the command
2033language.
2034
2c5c0674
RP
2035@emph{Information can be lost during canonicalization.} The BFD
2036internal canonical form of the external formats is not exhaustive; there
2037are structures in input formats for which there is no direct
2038representation internally. This means that the BFD back ends
2039cannot maintain all possible data richness through the transformation
2040between external to internal and back to external formats.
f22eee08
RP
2041
2042This limitation is only a problem when using the linker to read one
2c5c0674
RP
2043format and write another. Each BFD back end is responsible for
2044maintaining as much data as possible, and the internal BFD
2045canonical form has structures which are opaque to the BFD core,
f22eee08 2046and exported only to the back ends. When a file is read in one format,
2c5c0674 2047the canonical form is generated for BFD and the linker. At the
f22eee08 2048same time, the back end saves away any information which may otherwise
2c5c0674 2049be lost. If the data is then written back in the same format, the back
b4d4e8e3 2050end routine will be able to use the canonical form provided by the
2c5c0674 2051BFD core as well as the information it prepared earlier. Since
b4d4e8e3
RP
2052there is a great deal of commonality between back ends, this mechanism
2053is very useful. There is no information lost for this reason when
2054linking big endian COFF to little endian COFF, or from @code{a.out} to
2055@code{b.out}. When a mixture of formats is linked, the information is
2056only lost from the files whose format differs from the destination.
2057
2c5c0674 2058@node Mechanism, , BFD information loss, BFD
f22eee08 2059@section Mechanism
b4d4e8e3
RP
2060The greatest potential for loss of information is when there is least
2061overlap between the information provided by the source format, that
2c5c0674 2062stored by the canonical format, and the information needed by the
b4d4e8e3
RP
2063destination format. A brief description of the canonical form may help
2064you appreciate what kinds of data you can count on preserving across
2065conversions.
2c5c0674
RP
2066@cindex BFD canonical format
2067@cindex internal object-file format
b4d4e8e3
RP
2068
2069@table @emph
2070@item files
2071Information on target machine architecture, particular implementation
2072and format type are stored on a per-file basis. Other information
2073includes a demand pageable bit and a write protected bit. Note that
2074information like Unix magic numbers is not stored here---only the magic
2075numbers' meaning, so a @code{ZMAGIC} file would have both the demand pageable
2076bit and the write protected text bit set.
2077
2c5c0674
RP
2078The byte order of the target is stored on a per-file basis, so that big-
2079and little-endian object files may be linked with one another.
b4d4e8e3
RP
2080
2081@item sections
f22eee08
RP
2082Each section in the input file contains the name of the section, the
2083original address in the object file, various flags, size and alignment
2c5c0674 2084information and pointers into other BFD data structures.
b4d4e8e3
RP
2085
2086@item symbols
f22eee08 2087Each symbol contains a pointer to the object file which originally
2c5c0674
RP
2088defined it, its name, its value, and various flag bits. When a
2089BFD back end reads in a symbol table, the back end relocates all
2090symbols to make them relative to the base of the section where they were
2091defined. This ensures that each symbol points to its containing
2092section. Each symbol also has a varying amount of hidden data to contain
2093private data for the BFD back end. Since the symbol points to the
2094original file, the private data format for that symbol is accessible.
2095@code{gld} can operate on a collection of symbols of wildly different
2096formats without problems.
f22eee08
RP
2097
2098Normal global and simple local symbols are maintained on output, so an
b4d4e8e3
RP
2099output file (no matter its format) will retain symbols pointing to
2100functions and to global, static, and common variables. Some symbol
2101information is not worth retaining; in @code{a.out} type information is
2102stored in the symbol table as long symbol names. This information would
2103be useless to most COFF debuggers and may be thrown away with
2104appropriate command line switches. (The GNU debugger @code{gdb} does
2105support @code{a.out} style debugging information in COFF).
f22eee08
RP
2106
2107There is one word of type information within the symbol, so if the
2c5c0674 2108format supports symbol type information within symbols (for example COFF,
f22eee08
RP
2109IEEE, Oasys) and the type is simple enough to fit within one word
2110(nearly everything but aggregates) the information will be preserved.
2111
2112@item relocation level
2c5c0674 2113Each canonical BFD relocation record contains a pointer to the symbol to
f22eee08
RP
2114relocate to, the offset of the data to relocate, the section the data
2115is in and a pointer to a relocation type descriptor. Relocation is
2116performed effectively by message passing through the relocation type
2117descriptor and symbol pointer. It allows relocations to be performed
2118on output data using a relocation method only available in one of the
2119input formats. For instance, Oasys provides a byte relocation format.
2120A relocation record requesting this relocation type would point
2121indirectly to a routine to perform this, so the relocation may be
2122performed on a byte being written to a COFF file, even though 68k COFF
2123has no such relocation type.
2124
2125@item line numbers
2c5c0674
RP
2126Object formats can contain, for debugging purposes, some form of mapping
2127between symbols, source line numbers, and addresses in the output file.
2128These addresses have to be relocated along with the symbol information.
b4d4e8e3
RP
2129Each symbol with an associated list of line number records points to the
2130first record of the list. The head of a line number list consists of a
2131pointer to the symbol, which allows divination of the address of the
2132function whose line number is being described. The rest of the list is
2133made up of pairs: offsets into the section and line numbers. Any format
2134which can simply derive this information can pass it successfully
f22eee08
RP
2135between formats (COFF, IEEE and Oasys).
2136@end table
2137
2c5c0674
RP
2138@node Index, , BFD, Top
2139@unnumbered Index
2140
2141@printindex cp
2142
2143@tex
2144% I think something like @colophon should be in texinfo. In the
2145% meantime:
2146\long\def\colophon{\hbox to0pt{}\vfill
2147\centerline{The body of this manual is set in}
2148\centerline{\fontname\tenrm,}
2149\centerline{with headings in {\bf\fontname\tenbf}}
2150\centerline{and examples in {\tt\fontname\tentt}.}
2151\centerline{{\it\fontname\tenit\/} and}
2152\centerline{{\sl\fontname\tensl\/}}
2153\centerline{are used for emphasis.}\vfill}
2154\page\colophon
2155% Blame: pesch@cygnus.com, 28mar91.
2156@end tex
2157
2158
b4d4e8e3 2159@contents
f22eee08
RP
2160@bye
2161
2162
This page took 0.136078 seconds and 4 git commands to generate.