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