1 This is ld.info, produced by makeinfo version 4.0 from ./ld.texinfo.
4 * Ld: (ld). The GNU linker.
7 This file documents the GNU linker LD version 2.11.1.
9 Copyright (C) 1991, 92, 93, 94, 95, 96, 97, 98, 99, 2000 Free
10 Software Foundation, Inc.
13 File: ld.info, Node: Environment, Prev: Options, Up: Invocation
18 You can change the behavior of `ld' with the environment variables
19 `GNUTARGET', `LDEMULATION', and `COLLECT_NO_DEMANGLE'.
21 `GNUTARGET' determines the input-file object format if you don't use
22 `-b' (or its synonym `--format'). Its value should be one of the BFD
23 names for an input format (*note BFD::). If there is no `GNUTARGET' in
24 the environment, `ld' uses the natural format of the target. If
25 `GNUTARGET' is set to `default' then BFD attempts to discover the input
26 format by examining binary input files; this method often succeeds, but
27 there are potential ambiguities, since there is no method of ensuring
28 that the magic number used to specify object-file formats is unique.
29 However, the configuration procedure for BFD on each system places the
30 conventional format for that system first in the search-list, so
31 ambiguities are resolved in favor of convention.
33 `LDEMULATION' determines the default emulation if you don't use the
34 `-m' option. The emulation can affect various aspects of linker
35 behaviour, particularly the default linker script. You can list the
36 available emulations with the `--verbose' or `-V' options. If the `-m'
37 option is not used, and the `LDEMULATION' environment variable is not
38 defined, the default emulation depends upon how the linker was
41 Normally, the linker will default to demangling symbols. However, if
42 `COLLECT_NO_DEMANGLE' is set in the environment, then it will default
43 to not demangling symbols. This environment variable is used in a
44 similar fashion by the `gcc' linker wrapper program. The default may
45 be overridden by the `--demangle' and `--no-demangle' options.
48 File: ld.info, Node: Scripts, Next: Machine Dependent, Prev: Invocation, Up: Top
53 Every link is controlled by a "linker script". This script is
54 written in the linker command language.
56 The main purpose of the linker script is to describe how the
57 sections in the input files should be mapped into the output file, and
58 to control the memory layout of the output file. Most linker scripts
59 do nothing more than this. However, when necessary, the linker script
60 can also direct the linker to perform many other operations, using the
61 commands described below.
63 The linker always uses a linker script. If you do not supply one
64 yourself, the linker will use a default script that is compiled into the
65 linker executable. You can use the `--verbose' command line option to
66 display the default linker script. Certain command line options, such
67 as `-r' or `-N', will affect the default linker script.
69 You may supply your own linker script by using the `-T' command line
70 option. When you do this, your linker script will replace the default
73 You may also use linker scripts implicitly by naming them as input
74 files to the linker, as though they were files to be linked. *Note
75 Implicit Linker Scripts::.
79 * Basic Script Concepts:: Basic Linker Script Concepts
80 * Script Format:: Linker Script Format
81 * Simple Example:: Simple Linker Script Example
82 * Simple Commands:: Simple Linker Script Commands
83 * Assignments:: Assigning Values to Symbols
84 * SECTIONS:: SECTIONS Command
85 * MEMORY:: MEMORY Command
86 * PHDRS:: PHDRS Command
87 * VERSION:: VERSION Command
88 * Expressions:: Expressions in Linker Scripts
89 * Implicit Linker Scripts:: Implicit Linker Scripts
92 File: ld.info, Node: Basic Script Concepts, Next: Script Format, Up: Scripts
94 Basic Linker Script Concepts
95 ============================
97 We need to define some basic concepts and vocabulary in order to
98 describe the linker script language.
100 The linker combines input files into a single output file. The
101 output file and each input file are in a special data format known as an
102 "object file format". Each file is called an "object file". The
103 output file is often called an "executable", but for our purposes we
104 will also call it an object file. Each object file has, among other
105 things, a list of "sections". We sometimes refer to a section in an
106 input file as an "input section"; similarly, a section in the output
107 file is an "output section".
109 Each section in an object file has a name and a size. Most sections
110 also have an associated block of data, known as the "section contents".
111 A section may be marked as "loadable", which mean that the contents
112 should be loaded into memory when the output file is run. A section
113 with no contents may be "allocatable", which means that an area in
114 memory should be set aside, but nothing in particular should be loaded
115 there (in some cases this memory must be zeroed out). A section which
116 is neither loadable nor allocatable typically contains some sort of
117 debugging information.
119 Every loadable or allocatable output section has two addresses. The
120 first is the "VMA", or virtual memory address. This is the address the
121 section will have when the output file is run. The second is the
122 "LMA", or load memory address. This is the address at which the
123 section will be loaded. In most cases the two addresses will be the
124 same. An example of when they might be different is when a data section
125 is loaded into ROM, and then copied into RAM when the program starts up
126 (this technique is often used to initialize global variables in a ROM
127 based system). In this case the ROM address would be the LMA, and the
128 RAM address would be the VMA.
130 You can see the sections in an object file by using the `objdump'
131 program with the `-h' option.
133 Every object file also has a list of "symbols", known as the "symbol
134 table". A symbol may be defined or undefined. Each symbol has a name,
135 and each defined symbol has an address, among other information. If
136 you compile a C or C++ program into an object file, you will get a
137 defined symbol for every defined function and global or static
138 variable. Every undefined function or global variable which is
139 referenced in the input file will become an undefined symbol.
141 You can see the symbols in an object file by using the `nm' program,
142 or by using the `objdump' program with the `-t' option.
145 File: ld.info, Node: Script Format, Next: Simple Example, Prev: Basic Script Concepts, Up: Scripts
150 Linker scripts are text files.
152 You write a linker script as a series of commands. Each command is
153 either a keyword, possibly followed by arguments, or an assignment to a
154 symbol. You may separate commands using semicolons. Whitespace is
157 Strings such as file or format names can normally be entered
158 directly. If the file name contains a character such as a comma which
159 would otherwise serve to separate file names, you may put the file name
160 in double quotes. There is no way to use a double quote character in a
163 You may include comments in linker scripts just as in C, delimited by
164 `/*' and `*/'. As in C, comments are syntactically equivalent to
168 File: ld.info, Node: Simple Example, Next: Simple Commands, Prev: Script Format, Up: Scripts
170 Simple Linker Script Example
171 ============================
173 Many linker scripts are fairly simple.
175 The simplest possible linker script has just one command:
176 `SECTIONS'. You use the `SECTIONS' command to describe the memory
177 layout of the output file.
179 The `SECTIONS' command is a powerful command. Here we will describe
180 a simple use of it. Let's assume your program consists only of code,
181 initialized data, and uninitialized data. These will be in the
182 `.text', `.data', and `.bss' sections, respectively. Let's assume
183 further that these are the only sections which appear in your input
186 For this example, let's say that the code should be loaded at address
187 0x10000, and that the data should start at address 0x8000000. Here is a
188 linker script which will do that:
198 You write the `SECTIONS' command as the keyword `SECTIONS', followed
199 by a series of symbol assignments and output section descriptions
200 enclosed in curly braces.
202 The first line inside the `SECTIONS' command of the above example
203 sets the value of the special symbol `.', which is the location
204 counter. If you do not specify the address of an output section in some
205 other way (other ways are described later), the address is set from the
206 current value of the location counter. The location counter is then
207 incremented by the size of the output section. At the start of the
208 `SECTIONS' command, the location counter has the value `0'.
210 The second line defines an output section, `.text'. The colon is
211 required syntax which may be ignored for now. Within the curly braces
212 after the output section name, you list the names of the input sections
213 which should be placed into this output section. The `*' is a wildcard
214 which matches any file name. The expression `*(.text)' means all
215 `.text' input sections in all input files.
217 Since the location counter is `0x10000' when the output section
218 `.text' is defined, the linker will set the address of the `.text'
219 section in the output file to be `0x10000'.
221 The remaining lines define the `.data' and `.bss' sections in the
222 output file. The linker will place the `.data' output section at
223 address `0x8000000'. After the linker places the `.data' output
224 section, the value of the location counter will be `0x8000000' plus the
225 size of the `.data' output section. The effect is that the linker will
226 place the `.bss' output section immediately after the `.data' output
229 The linker will ensure that each output section has the required
230 alignment, by increasing the location counter if necessary. In this
231 example, the specified addresses for the `.text' and `.data' sections
232 will probably satisfy any alignment constraints, but the linker may
233 have to create a small gap between the `.data' and `.bss' sections.
235 That's it! That's a simple and complete linker script.
238 File: ld.info, Node: Simple Commands, Next: Assignments, Prev: Simple Example, Up: Scripts
240 Simple Linker Script Commands
241 =============================
243 In this section we describe the simple linker script commands.
247 * Entry Point:: Setting the entry point
248 * File Commands:: Commands dealing with files
250 * Format Commands:: Commands dealing with object file formats
252 * Miscellaneous Commands:: Other linker script commands
255 File: ld.info, Node: Entry Point, Next: File Commands, Up: Simple Commands
257 Setting the entry point
258 -----------------------
260 The first instruction to execute in a program is called the "entry
261 point". You can use the `ENTRY' linker script command to set the entry
262 point. The argument is a symbol name:
265 There are several ways to set the entry point. The linker will set
266 the entry point by trying each of the following methods in order, and
267 stopping when one of them succeeds:
268 * the `-e' ENTRY command-line option;
270 * the `ENTRY(SYMBOL)' command in a linker script;
272 * the value of the symbol `start', if defined;
274 * the address of the first byte of the `.text' section, if present;
279 File: ld.info, Node: File Commands, Next: Format Commands, Prev: Entry Point, Up: Simple Commands
281 Commands dealing with files
282 ---------------------------
284 Several linker script commands deal with files.
287 Include the linker script FILENAME at this point. The file will
288 be searched for in the current directory, and in any directory
289 specified with the `-L' option. You can nest calls to `INCLUDE'
290 up to 10 levels deep.
292 `INPUT(FILE, FILE, ...)'
293 `INPUT(FILE FILE ...)'
294 The `INPUT' command directs the linker to include the named files
295 in the link, as though they were named on the command line.
297 For example, if you always want to include `subr.o' any time you do
298 a link, but you can't be bothered to put it on every link command
299 line, then you can put `INPUT (subr.o)' in your linker script.
301 In fact, if you like, you can list all of your input files in the
302 linker script, and then invoke the linker with nothing but a `-T'
305 The linker will first try to open the file in the current
306 directory. If it is not found, the linker will search through the
307 archive library search path. See the description of `-L' in *Note
308 Command Line Options: Options.
310 If you use `INPUT (-lFILE)', `ld' will transform the name to
311 `libFILE.a', as with the command line argument `-l'.
313 When you use the `INPUT' command in an implicit linker script, the
314 files will be included in the link at the point at which the linker
315 script file is included. This can affect archive searching.
317 `GROUP(FILE, FILE, ...)'
318 `GROUP(FILE FILE ...)'
319 The `GROUP' command is like `INPUT', except that the named files
320 should all be archives, and they are searched repeatedly until no
321 new undefined references are created. See the description of `-('
322 in *Note Command Line Options: Options.
325 The `OUTPUT' command names the output file. Using
326 `OUTPUT(FILENAME)' in the linker script is exactly like using `-o
327 FILENAME' on the command line (*note Command Line Options:
328 Options.). If both are used, the command line option takes
331 You can use the `OUTPUT' command to define a default name for the
332 output file other than the usual default of `a.out'.
335 The `SEARCH_DIR' command adds PATH to the list of paths where `ld'
336 looks for archive libraries. Using `SEARCH_DIR(PATH)' is exactly
337 like using `-L PATH' on the command line (*note Command Line
338 Options: Options.). If both are used, then the linker will search
339 both paths. Paths specified using the command line option are
343 The `STARTUP' command is just like the `INPUT' command, except
344 that FILENAME will become the first input file to be linked, as
345 though it were specified first on the command line. This may be
346 useful when using a system in which the entry point is always the
347 start of the first file.
350 File: ld.info, Node: Format Commands, Next: Miscellaneous Commands, Prev: File Commands, Up: Simple Commands
352 Commands dealing with object file formats
353 -----------------------------------------
355 A couple of linker script commands deal with object file formats.
357 `OUTPUT_FORMAT(BFDNAME)'
358 `OUTPUT_FORMAT(DEFAULT, BIG, LITTLE)'
359 The `OUTPUT_FORMAT' command names the BFD format to use for the
360 output file (*note BFD::). Using `OUTPUT_FORMAT(BFDNAME)' is
361 exactly like using `-oformat BFDNAME' on the command line (*note
362 Command Line Options: Options.). If both are used, the command
363 line option takes precedence.
365 You can use `OUTPUT_FORMAT' with three arguments to use different
366 formats based on the `-EB' and `-EL' command line options. This
367 permits the linker script to set the output format based on the
370 If neither `-EB' nor `-EL' are used, then the output format will
371 be the first argument, DEFAULT. If `-EB' is used, the output
372 format will be the second argument, BIG. If `-EL' is used, the
373 output format will be the third argument, LITTLE.
375 For example, the default linker script for the MIPS ELF target
377 OUTPUT_FORMAT(elf32-bigmips, elf32-bigmips, elf32-littlemips)
378 This says that the default format for the output file is
379 `elf32-bigmips', but if the user uses the `-EL' command line
380 option, the output file will be created in the `elf32-littlemips'
384 The `TARGET' command names the BFD format to use when reading input
385 files. It affects subsequent `INPUT' and `GROUP' commands. This
386 command is like using `-b BFDNAME' on the command line (*note
387 Command Line Options: Options.). If the `TARGET' command is used
388 but `OUTPUT_FORMAT' is not, then the last `TARGET' command is also
389 used to set the format for the output file. *Note BFD::.
392 File: ld.info, Node: Miscellaneous Commands, Prev: Format Commands, Up: Simple Commands
394 Other linker script commands
395 ----------------------------
397 There are a few other linker scripts commands.
399 `ASSERT(EXP, MESSAGE)'
400 Ensure that EXP is non-zero. If it is zero, then exit the linker
401 with an error code, and print MESSAGE.
403 `EXTERN(SYMBOL SYMBOL ...)'
404 Force SYMBOL to be entered in the output file as an undefined
405 symbol. Doing this may, for example, trigger linking of additional
406 modules from standard libraries. You may list several SYMBOLs for
407 each `EXTERN', and you may use `EXTERN' multiple times. This
408 command has the same effect as the `-u' command-line option.
410 `FORCE_COMMON_ALLOCATION'
411 This command has the same effect as the `-d' command-line option:
412 to make `ld' assign space to common symbols even if a relocatable
413 output file is specified (`-r').
415 `NOCROSSREFS(SECTION SECTION ...)'
416 This command may be used to tell `ld' to issue an error about any
417 references among certain output sections.
419 In certain types of programs, particularly on embedded systems when
420 using overlays, when one section is loaded into memory, another
421 section will not be. Any direct references between the two
422 sections would be errors. For example, it would be an error if
423 code in one section called a function defined in the other section.
425 The `NOCROSSREFS' command takes a list of output section names. If
426 `ld' detects any cross references between the sections, it reports
427 an error and returns a non-zero exit status. Note that the
428 `NOCROSSREFS' command uses output section names, not input section
431 `OUTPUT_ARCH(BFDARCH)'
432 Specify a particular output machine architecture. The argument is
433 one of the names used by the BFD library (*note BFD::). You can
434 see the architecture of an object file by using the `objdump'
435 program with the `-f' option.
438 File: ld.info, Node: Assignments, Next: SECTIONS, Prev: Simple Commands, Up: Scripts
440 Assigning Values to Symbols
441 ===========================
443 You may assign a value to a symbol in a linker script. This will
444 define the symbol as a global symbol.
448 * Simple Assignments:: Simple Assignments
452 File: ld.info, Node: Simple Assignments, Next: PROVIDE, Up: Assignments
457 You may assign to a symbol using any of the C assignment operators:
459 `SYMBOL = EXPRESSION ;'
460 `SYMBOL += EXPRESSION ;'
461 `SYMBOL -= EXPRESSION ;'
462 `SYMBOL *= EXPRESSION ;'
463 `SYMBOL /= EXPRESSION ;'
464 `SYMBOL <<= EXPRESSION ;'
465 `SYMBOL >>= EXPRESSION ;'
466 `SYMBOL &= EXPRESSION ;'
467 `SYMBOL |= EXPRESSION ;'
468 The first case will define SYMBOL to the value of EXPRESSION. In
469 the other cases, SYMBOL must already be defined, and the value will be
470 adjusted accordingly.
472 The special symbol name `.' indicates the location counter. You may
473 only use this within a `SECTIONS' command.
475 The semicolon after EXPRESSION is required.
477 Expressions are defined below; see *Note Expressions::.
479 You may write symbol assignments as commands in their own right, or
480 as statements within a `SECTIONS' command, or as part of an output
481 section description in a `SECTIONS' command.
483 The section of the symbol will be set from the section of the
484 expression; for more information, see *Note Expression Section::.
486 Here is an example showing the three different places that symbol
487 assignments may be used:
497 _bdata = (. + 3) & ~ 4;
501 In this example, the symbol `floating_point' will be defined as zero.
502 The symbol `_etext' will be defined as the address following the last
503 `.text' input section. The symbol `_bdata' will be defined as the
504 address following the `.text' output section aligned upward to a 4 byte
508 File: ld.info, Node: PROVIDE, Prev: Simple Assignments, Up: Assignments
513 In some cases, it is desirable for a linker script to define a symbol
514 only if it is referenced and is not defined by any object included in
515 the link. For example, traditional linkers defined the symbol `etext'.
516 However, ANSI C requires that the user be able to use `etext' as a
517 function name without encountering an error. The `PROVIDE' keyword may
518 be used to define a symbol, such as `etext', only if it is referenced
519 but not defined. The syntax is `PROVIDE(SYMBOL = EXPRESSION)'.
521 Here is an example of using `PROVIDE' to define `etext':
532 In this example, if the program defines `_etext' (with a leading
533 underscore), the linker will give a multiple definition error. If, on
534 the other hand, the program defines `etext' (with no leading
535 underscore), the linker will silently use the definition in the program.
536 If the program references `etext' but does not define it, the linker
537 will use the definition in the linker script.
540 File: ld.info, Node: SECTIONS, Next: MEMORY, Prev: Assignments, Up: Scripts
545 The `SECTIONS' command tells the linker how to map input sections
546 into output sections, and how to place the output sections in memory.
548 The format of the `SECTIONS' command is:
556 Each SECTIONS-COMMAND may of be one of the following:
558 * an `ENTRY' command (*note Entry command: Entry Point.)
560 * a symbol assignment (*note Assignments::)
562 * an output section description
564 * an overlay description
566 The `ENTRY' command and symbol assignments are permitted inside the
567 `SECTIONS' command for convenience in using the location counter in
568 those commands. This can also make the linker script easier to
569 understand because you can use those commands at meaningful points in
570 the layout of the output file.
572 Output section descriptions and overlay descriptions are described
575 If you do not use a `SECTIONS' command in your linker script, the
576 linker will place each input section into an identically named output
577 section in the order that the sections are first encountered in the
578 input files. If all input sections are present in the first file, for
579 example, the order of sections in the output file will match the order
580 in the first input file. The first section will be at address zero.
584 * Output Section Description:: Output section description
585 * Output Section Name:: Output section name
586 * Output Section Address:: Output section address
587 * Input Section:: Input section description
588 * Output Section Data:: Output section data
589 * Output Section Keywords:: Output section keywords
590 * Output Section Discarding:: Output section discarding
591 * Output Section Attributes:: Output section attributes
592 * Overlay Description:: Overlay description
595 File: ld.info, Node: Output Section Description, Next: Output Section Name, Up: SECTIONS
597 Output section description
598 --------------------------
600 The full description of an output section looks like this:
601 SECTION [ADDRESS] [(TYPE)] : [AT(LMA)]
603 OUTPUT-SECTION-COMMAND
604 OUTPUT-SECTION-COMMAND
606 } [>REGION] [AT>LMA_REGION] [:PHDR :PHDR ...] [=FILLEXP]
608 Most output sections do not use most of the optional section
611 The whitespace around SECTION is required, so that the section name
612 is unambiguous. The colon and the curly braces are also required. The
613 line breaks and other white space are optional.
615 Each OUTPUT-SECTION-COMMAND may be one of the following:
617 * a symbol assignment (*note Assignments::)
619 * an input section description (*note Input Section::)
621 * data values to include directly (*note Output Section Data::)
623 * a special output section keyword (*note Output Section Keywords::)
626 File: ld.info, Node: Output Section Name, Next: Output Section Address, Prev: Output Section Description, Up: SECTIONS
631 The name of the output section is SECTION. SECTION must meet the
632 constraints of your output format. In formats which only support a
633 limited number of sections, such as `a.out', the name must be one of
634 the names supported by the format (`a.out', for example, allows only
635 `.text', `.data' or `.bss'). If the output format supports any number
636 of sections, but with numbers and not names (as is the case for Oasys),
637 the name should be supplied as a quoted numeric string. A section name
638 may consist of any sequence of characters, but a name which contains
639 any unusual characters such as commas must be quoted.
641 The output section name `/DISCARD/' is special; *Note Output Section
645 File: ld.info, Node: Output Section Address, Next: Input Section, Prev: Output Section Name, Up: SECTIONS
647 Output section address
648 ----------------------
650 The ADDRESS is an expression for the VMA (the virtual memory
651 address) of the output section. If you do not provide ADDRESS, the
652 linker will set it based on REGION if present, or otherwise based on
653 the current value of the location counter.
655 If you provide ADDRESS, the address of the output section will be
656 set to precisely that. If you provide neither ADDRESS nor REGION, then
657 the address of the output section will be set to the current value of
658 the location counter aligned to the alignment requirements of the
659 output section. The alignment requirement of the output section is the
660 strictest alignment of any input section contained within the output
664 .text . : { *(.text) }
669 are subtly different. The first will set the address of the `.text'
670 output section to the current value of the location counter. The
671 second will set it to the current value of the location counter aligned
672 to the strictest alignment of a `.text' input section.
674 The ADDRESS may be an arbitrary expression; *Note Expressions::.
675 For example, if you want to align the section on a 0x10 byte boundary,
676 so that the lowest four bits of the section address are zero, you could
677 do something like this:
678 .text ALIGN(0x10) : { *(.text) }
680 This works because `ALIGN' returns the current location counter aligned
681 upward to the specified value.
683 Specifying ADDRESS for a section will change the value of the
687 File: ld.info, Node: Input Section, Next: Output Section Data, Prev: Output Section Address, Up: SECTIONS
689 Input section description
690 -------------------------
692 The most common output section command is an input section
695 The input section description is the most basic linker script
696 operation. You use output sections to tell the linker how to lay out
697 your program in memory. You use input section descriptions to tell the
698 linker how to map the input files into your memory layout.
702 * Input Section Basics:: Input section basics
703 * Input Section Wildcards:: Input section wildcard patterns
704 * Input Section Common:: Input section for common symbols
705 * Input Section Keep:: Input section and garbage collection
706 * Input Section Example:: Input section example
709 File: ld.info, Node: Input Section Basics, Next: Input Section Wildcards, Up: Input Section
714 An input section description consists of a file name optionally
715 followed by a list of section names in parentheses.
717 The file name and the section name may be wildcard patterns, which we
718 describe further below (*note Input Section Wildcards::).
720 The most common input section description is to include all input
721 sections with a particular name in the output section. For example, to
722 include all input `.text' sections, you would write:
725 Here the `*' is a wildcard which matches any file name. To exclude a
726 list of files from matching the file name wildcard, EXCLUDE_FILE may be
727 used to match all files except the ones specified in the EXCLUDE_FILE
729 (*(EXCLUDE_FILE (*crtend.o *otherfile.o) .ctors))
730 will cause all .ctors sections from all files except `crtend.o' and
731 `otherfile.o' to be included.
733 There are two ways to include more than one section:
737 The difference between these is the order in which the `.text' and
738 `.rdata' input sections will appear in the output section. In the
739 first example, they will be intermingled. In the second example, all
740 `.text' input sections will appear first, followed by all `.rdata'
743 You can specify a file name to include sections from a particular
744 file. You would do this if one or more of your files contain special
745 data that needs to be at a particular location in memory. For example:
748 If you use a file name without a list of sections, then all sections
749 in the input file will be included in the output section. This is not
750 commonly done, but it may by useful on occasion. For example:
753 When you use a file name which does not contain any wild card
754 characters, the linker will first see if you also specified the file
755 name on the linker command line or in an `INPUT' command. If you did
756 not, the linker will attempt to open the file as an input file, as
757 though it appeared on the command line. Note that this differs from an
758 `INPUT' command, because the linker will not search for the file in the
762 File: ld.info, Node: Input Section Wildcards, Next: Input Section Common, Prev: Input Section Basics, Up: Input Section
764 Input section wildcard patterns
765 ...............................
767 In an input section description, either the file name or the section
768 name or both may be wildcard patterns.
770 The file name of `*' seen in many examples is a simple wildcard
771 pattern for the file name.
773 The wildcard patterns are like those used by the Unix shell.
776 matches any number of characters
779 matches any single character
782 matches a single instance of any of the CHARS; the `-' character
783 may be used to specify a range of characters, as in `[a-z]' to
784 match any lower case letter
787 quotes the following character
789 When a file name is matched with a wildcard, the wildcard characters
790 will not match a `/' character (used to separate directory names on
791 Unix). A pattern consisting of a single `*' character is an exception;
792 it will always match any file name, whether it contains a `/' or not.
793 In a section name, the wildcard characters will match a `/' character.
795 File name wildcard patterns only match files which are explicitly
796 specified on the command line or in an `INPUT' command. The linker
797 does not search directories to expand wildcards.
799 If a file name matches more than one wildcard pattern, or if a file
800 name appears explicitly and is also matched by a wildcard pattern, the
801 linker will use the first match in the linker script. For example, this
802 sequence of input section descriptions is probably in error, because the
803 `data.o' rule will not be used:
805 .data1 : { data.o(.data) }
807 Normally, the linker will place files and sections matched by
808 wildcards in the order in which they are seen during the link. You can
809 change this by using the `SORT' keyword, which appears before a wildcard
810 pattern in parentheses (e.g., `SORT(.text*)'). When the `SORT' keyword
811 is used, the linker will sort the files or sections into ascending
812 order by name before placing them in the output file.
814 If you ever get confused about where input sections are going, use
815 the `-M' linker option to generate a map file. The map file shows
816 precisely how input sections are mapped to output sections.
818 This example shows how wildcard patterns might be used to partition
819 files. This linker script directs the linker to place all `.text'
820 sections in `.text' and all `.bss' sections in `.bss'. The linker will
821 place the `.data' section from all files beginning with an upper case
822 character in `.DATA'; for all other files, the linker will place the
823 `.data' section in `.data'.
826 .DATA : { [A-Z]*(.data) }
832 File: ld.info, Node: Input Section Common, Next: Input Section Keep, Prev: Input Section Wildcards, Up: Input Section
834 Input section for common symbols
835 ................................
837 A special notation is needed for common symbols, because in many
838 object file formats common symbols do not have a particular input
839 section. The linker treats common symbols as though they are in an
840 input section named `COMMON'.
842 You may use file names with the `COMMON' section just as with any
843 other input sections. You can use this to place common symbols from a
844 particular input file in one section while common symbols from other
845 input files are placed in another section.
847 In most cases, common symbols in input files will be placed in the
848 `.bss' section in the output file. For example:
849 .bss { *(.bss) *(COMMON) }
851 Some object file formats have more than one type of common symbol.
852 For example, the MIPS ELF object file format distinguishes standard
853 common symbols and small common symbols. In this case, the linker will
854 use a different special section name for other types of common symbols.
855 In the case of MIPS ELF, the linker uses `COMMON' for standard common
856 symbols and `.scommon' for small common symbols. This permits you to
857 map the different types of common symbols into memory at different
860 You will sometimes see `[COMMON]' in old linker scripts. This
861 notation is now considered obsolete. It is equivalent to `*(COMMON)'.
864 File: ld.info, Node: Input Section Keep, Next: Input Section Example, Prev: Input Section Common, Up: Input Section
866 Input section and garbage collection
867 ....................................
869 When link-time garbage collection is in use (`--gc-sections'), it is
870 often useful to mark sections that should not be eliminated. This is
871 accomplished by surrounding an input section's wildcard entry with
872 `KEEP()', as in `KEEP(*(.init))' or `KEEP(SORT(*)(.ctors))'.
875 File: ld.info, Node: Input Section Example, Prev: Input Section Keep, Up: Input Section
877 Input section example
878 .....................
880 The following example is a complete linker script. It tells the
881 linker to read all of the sections from file `all.o' and place them at
882 the start of output section `outputa' which starts at location
883 `0x10000'. All of section `.input1' from file `foo.o' follows
884 immediately, in the same output section. All of section `.input2' from
885 `foo.o' goes into output section `outputb', followed by section
886 `.input1' from `foo1.o'. All of the remaining `.input1' and `.input2'
887 sections from any files are written to output section `outputc'.
908 File: ld.info, Node: Output Section Data, Next: Output Section Keywords, Prev: Input Section, Up: SECTIONS
913 You can include explicit bytes of data in an output section by using
914 `BYTE', `SHORT', `LONG', `QUAD', or `SQUAD' as an output section
915 command. Each keyword is followed by an expression in parentheses
916 providing the value to store (*note Expressions::). The value of the
917 expression is stored at the current value of the location counter.
919 The `BYTE', `SHORT', `LONG', and `QUAD' commands store one, two,
920 four, and eight bytes (respectively). After storing the bytes, the
921 location counter is incremented by the number of bytes stored.
923 For example, this will store the byte 1 followed by the four byte
924 value of the symbol `addr':
928 When using a 64 bit host or target, `QUAD' and `SQUAD' are the same;
929 they both store an 8 byte, or 64 bit, value. When both host and target
930 are 32 bits, an expression is computed as 32 bits. In this case `QUAD'
931 stores a 32 bit value zero extended to 64 bits, and `SQUAD' stores a 32
932 bit value sign extended to 64 bits.
934 If the object file format of the output file has an explicit
935 endianness, which is the normal case, the value will be stored in that
936 endianness. When the object file format does not have an explicit
937 endianness, as is true of, for example, S-records, the value will be
938 stored in the endianness of the first input object file.
940 Note - these commands only work inside a section description and not
941 between them, so the following will produce an error from the linker:
942 SECTIONS { .text : { *(.text) } LONG(1) .data : { *(.data) } }
943 whereas this will work:
944 SECTIONS { .text : { *(.text) ; LONG(1) } .data : { *(.data) } }
946 You may use the `FILL' command to set the fill pattern for the
947 current section. It is followed by an expression in parentheses. Any
948 otherwise unspecified regions of memory within the section (for example,
949 gaps left due to the required alignment of input sections) are filled
950 with the two least significant bytes of the expression, repeated as
951 necessary. A `FILL' statement covers memory locations after the point
952 at which it occurs in the section definition; by including more than
953 one `FILL' statement, you can have different fill patterns in different
954 parts of an output section.
956 This example shows how to fill unspecified regions of memory with the
960 The `FILL' command is similar to the `=FILLEXP' output section
961 attribute (*note Output Section Fill::), but it only affects the part
962 of the section following the `FILL' command, rather than the entire
963 section. If both are used, the `FILL' command takes precedence.
966 File: ld.info, Node: Output Section Keywords, Next: Output Section Discarding, Prev: Output Section Data, Up: SECTIONS
968 Output section keywords
969 -----------------------
971 There are a couple of keywords which can appear as output section
974 `CREATE_OBJECT_SYMBOLS'
975 The command tells the linker to create a symbol for each input
976 file. The name of each symbol will be the name of the
977 corresponding input file. The section of each symbol will be the
978 output section in which the `CREATE_OBJECT_SYMBOLS' command
981 This is conventional for the a.out object file format. It is not
982 normally used for any other object file format.
985 When linking using the a.out object file format, the linker uses an
986 unusual set construct to support C++ global constructors and
987 destructors. When linking object file formats which do not support
988 arbitrary sections, such as ECOFF and XCOFF, the linker will
989 automatically recognize C++ global constructors and destructors by
990 name. For these object file formats, the `CONSTRUCTORS' command
991 tells the linker to place constructor information in the output
992 section where the `CONSTRUCTORS' command appears. The
993 `CONSTRUCTORS' command is ignored for other object file formats.
995 The symbol `__CTOR_LIST__' marks the start of the global
996 constructors, and the symbol `__DTOR_LIST' marks the end. The
997 first word in the list is the number of entries, followed by the
998 address of each constructor or destructor, followed by a zero
999 word. The compiler must arrange to actually run the code. For
1000 these object file formats GNU C++ normally calls constructors from
1001 a subroutine `__main'; a call to `__main' is automatically
1002 inserted into the startup code for `main'. GNU C++ normally runs
1003 destructors either by using `atexit', or directly from the function
1006 For object file formats such as `COFF' or `ELF' which support
1007 arbitrary section names, GNU C++ will normally arrange to put the
1008 addresses of global constructors and destructors into the `.ctors'
1009 and `.dtors' sections. Placing the following sequence into your
1010 linker script will build the sort of table which the GNU C++
1011 runtime code expects to see.
1014 LONG((__CTOR_END__ - __CTOR_LIST__) / 4 - 2)
1019 LONG((__DTOR_END__ - __DTOR_LIST__) / 4 - 2)
1024 If you are using the GNU C++ support for initialization priority,
1025 which provides some control over the order in which global
1026 constructors are run, you must sort the constructors at link time
1027 to ensure that they are executed in the correct order. When using
1028 the `CONSTRUCTORS' command, use `SORT(CONSTRUCTORS)' instead.
1029 When using the `.ctors' and `.dtors' sections, use
1030 `*(SORT(.ctors))' and `*(SORT(.dtors))' instead of just
1031 `*(.ctors)' and `*(.dtors)'.
1033 Normally the compiler and linker will handle these issues
1034 automatically, and you will not need to concern yourself with
1035 them. However, you may need to consider this if you are using C++
1036 and writing your own linker scripts.
1039 File: ld.info, Node: Output Section Discarding, Next: Output Section Attributes, Prev: Output Section Keywords, Up: SECTIONS
1041 Output section discarding
1042 -------------------------
1044 The linker will not create output section which do not have any
1045 contents. This is for convenience when referring to input sections that
1046 may or may not be present in any of the input files. For example:
1049 will only create a `.foo' section in the output file if there is a
1050 `.foo' section in at least one input file.
1052 If you use anything other than an input section description as an
1053 output section command, such as a symbol assignment, then the output
1054 section will always be created, even if there are no matching input
1057 The special output section name `/DISCARD/' may be used to discard
1058 input sections. Any input sections which are assigned to an output
1059 section named `/DISCARD/' are not included in the output file.
1062 File: ld.info, Node: Output Section Attributes, Next: Overlay Description, Prev: Output Section Discarding, Up: SECTIONS
1064 Output section attributes
1065 -------------------------
1067 We showed above that the full description of an output section looked
1069 SECTION [ADDRESS] [(TYPE)] : [AT(LMA)]
1071 OUTPUT-SECTION-COMMAND
1072 OUTPUT-SECTION-COMMAND
1074 } [>REGION] [AT>LMA_REGION] [:PHDR :PHDR ...] [=FILLEXP]
1075 We've already described SECTION, ADDRESS, and
1076 OUTPUT-SECTION-COMMAND. In this section we will describe the remaining
1081 * Output Section Type:: Output section type
1082 * Output Section LMA:: Output section LMA
1083 * Output Section Region:: Output section region
1084 * Output Section Phdr:: Output section phdr
1085 * Output Section Fill:: Output section fill
1088 File: ld.info, Node: Output Section Type, Next: Output Section LMA, Up: Output Section Attributes
1093 Each output section may have a type. The type is a keyword in
1094 parentheses. The following types are defined:
1097 The section should be marked as not loadable, so that it will not
1098 be loaded into memory when the program is run.
1104 These type names are supported for backward compatibility, and are
1105 rarely used. They all have the same effect: the section should be
1106 marked as not allocatable, so that no memory is allocated for the
1107 section when the program is run.
1109 The linker normally sets the attributes of an output section based on
1110 the input sections which map into it. You can override this by using
1111 the section type. For example, in the script sample below, the `ROM'
1112 section is addressed at memory location `0' and does not need to be
1113 loaded when the program is run. The contents of the `ROM' section will
1114 appear in the linker output file as usual.
1116 ROM 0 (NOLOAD) : { ... }
1121 File: ld.info, Node: Output Section LMA, Next: Output Section Region, Prev: Output Section Type, Up: Output Section Attributes
1126 Every section has a virtual address (VMA) and a load address (LMA);
1127 see *Note Basic Script Concepts::. The address expression which may
1128 appear in an output section description sets the VMA (*note Output
1131 The linker will normally set the LMA equal to the VMA. You can
1132 change that by using the `AT' keyword. The expression LMA that follows
1133 the `AT' keyword specifies the load address of the section.
1134 Alternatively, with `AT>LMA_REGION' expression, you may specify a
1135 memory region for the section's load address. *Note MEMORY::.
1137 This feature is designed to make it easy to build a ROM image. For
1138 example, the following linker script creates three output sections: one
1139 called `.text', which starts at `0x1000', one called `.mdata', which is
1140 loaded at the end of the `.text' section even though its VMA is
1141 `0x2000', and one called `.bss' to hold uninitialized data at address
1142 `0x3000'. The symbol `_data' is defined with the value `0x2000', which
1143 shows that the location counter holds the VMA value, not the LMA value.
1147 .text 0x1000 : { *(.text) _etext = . ; }
1149 AT ( ADDR (.text) + SIZEOF (.text) )
1150 { _data = . ; *(.data); _edata = . ; }
1152 { _bstart = . ; *(.bss) *(COMMON) ; _bend = . ;}
1155 The run-time initialization code for use with a program generated
1156 with this linker script would include something like the following, to
1157 copy the initialized data from the ROM image to its runtime address.
1158 Notice how this code takes advantage of the symbols defined by the
1161 extern char _etext, _data, _edata, _bstart, _bend;
1162 char *src = &_etext;
1165 /* ROM has data at end of text; copy it. */
1166 while (dst < &_edata) {
1171 for (dst = &_bstart; dst< &_bend; dst++)
1175 File: ld.info, Node: Output Section Region, Next: Output Section Phdr, Prev: Output Section LMA, Up: Output Section Attributes
1177 Output section region
1178 .....................
1180 You can assign a section to a previously defined region of memory by
1181 using `>REGION'. *Note MEMORY::.
1183 Here is a simple example:
1184 MEMORY { rom : ORIGIN = 0x1000, LENGTH = 0x1000 }
1185 SECTIONS { ROM : { *(.text) } >rom }
1188 File: ld.info, Node: Output Section Phdr, Next: Output Section Fill, Prev: Output Section Region, Up: Output Section Attributes
1193 You can assign a section to a previously defined program segment by
1194 using `:PHDR'. *Note PHDRS::. If a section is assigned to one or more
1195 segments, then all subsequent allocated sections will be assigned to
1196 those segments as well, unless they use an explicitly `:PHDR' modifier.
1197 You can use `:NONE' to tell the linker to not put the section in any
1200 Here is a simple example:
1201 PHDRS { text PT_LOAD ; }
1202 SECTIONS { .text : { *(.text) } :text }
1205 File: ld.info, Node: Output Section Fill, Prev: Output Section Phdr, Up: Output Section Attributes
1210 You can set the fill pattern for an entire section by using
1211 `=FILLEXP'. FILLEXP is an expression (*note Expressions::). Any
1212 otherwise unspecified regions of memory within the output section (for
1213 example, gaps left due to the required alignment of input sections)
1214 will be filled with the two least significant bytes of the value,
1215 repeated as necessary.
1217 You can also change the fill value with a `FILL' command in the
1218 output section commands; see *Note Output Section Data::.
1220 Here is a simple example:
1221 SECTIONS { .text : { *(.text) } =0x9090 }