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