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