| 1 | This is ld.info, produced by makeinfo version 4.0 from ./ld.texinfo. | 
|---|
| 2 |  | 
|---|
| 3 | START-INFO-DIR-ENTRY | 
|---|
| 4 | * Ld: (ld).                       The GNU linker. | 
|---|
| 5 | END-INFO-DIR-ENTRY | 
|---|
| 6 |  | 
|---|
| 7 | This file documents the GNU linker LD version 2.11.2. | 
|---|
| 8 |  | 
|---|
| 9 | Copyright (C) 1991, 92, 93, 94, 95, 96, 97, 98, 99, 2000 Free | 
|---|
| 10 | Software Foundation, Inc. | 
|---|
| 11 |  | 
|---|
| 12 |  | 
|---|
| 13 | File: ld.info,  Node: Environment,  Prev: Options,  Up: Invocation | 
|---|
| 14 |  | 
|---|
| 15 | Environment Variables | 
|---|
| 16 | ===================== | 
|---|
| 17 |  | 
|---|
| 18 | You can change the behavior of `ld' with the environment variables | 
|---|
| 19 | `GNUTARGET', `LDEMULATION', and `COLLECT_NO_DEMANGLE'. | 
|---|
| 20 |  | 
|---|
| 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. | 
|---|
| 32 |  | 
|---|
| 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 | 
|---|
| 39 | configured. | 
|---|
| 40 |  | 
|---|
| 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. | 
|---|
| 46 |  | 
|---|
| 47 |  | 
|---|
| 48 | File: ld.info,  Node: Scripts,  Next: Machine Dependent,  Prev: Invocation,  Up: Top | 
|---|
| 49 |  | 
|---|
| 50 | Linker Scripts | 
|---|
| 51 | ************** | 
|---|
| 52 |  | 
|---|
| 53 | Every link is controlled by a "linker script".  This script is | 
|---|
| 54 | written in the linker command language. | 
|---|
| 55 |  | 
|---|
| 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. | 
|---|
| 62 |  | 
|---|
| 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. | 
|---|
| 68 |  | 
|---|
| 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 | 
|---|
| 71 | linker script. | 
|---|
| 72 |  | 
|---|
| 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::. | 
|---|
| 76 |  | 
|---|
| 77 | * Menu: | 
|---|
| 78 |  | 
|---|
| 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 | 
|---|
| 90 |  | 
|---|
| 91 |  | 
|---|
| 92 | File: ld.info,  Node: Basic Script Concepts,  Next: Script Format,  Up: Scripts | 
|---|
| 93 |  | 
|---|
| 94 | Basic Linker Script Concepts | 
|---|
| 95 | ============================ | 
|---|
| 96 |  | 
|---|
| 97 | We need to define some basic concepts and vocabulary in order to | 
|---|
| 98 | describe the linker script language. | 
|---|
| 99 |  | 
|---|
| 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". | 
|---|
| 108 |  | 
|---|
| 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. | 
|---|
| 118 |  | 
|---|
| 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. | 
|---|
| 129 |  | 
|---|
| 130 | You can see the sections in an object file by using the `objdump' | 
|---|
| 131 | program with the `-h' option. | 
|---|
| 132 |  | 
|---|
| 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. | 
|---|
| 140 |  | 
|---|
| 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. | 
|---|
| 143 |  | 
|---|
| 144 |  | 
|---|
| 145 | File: ld.info,  Node: Script Format,  Next: Simple Example,  Prev: Basic Script Concepts,  Up: Scripts | 
|---|
| 146 |  | 
|---|
| 147 | Linker Script Format | 
|---|
| 148 | ==================== | 
|---|
| 149 |  | 
|---|
| 150 | Linker scripts are text files. | 
|---|
| 151 |  | 
|---|
| 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 | 
|---|
| 155 | generally ignored. | 
|---|
| 156 |  | 
|---|
| 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 | 
|---|
| 161 | file name. | 
|---|
| 162 |  | 
|---|
| 163 | You may include comments in linker scripts just as in C, delimited by | 
|---|
| 164 | `/*' and `*/'.  As in C, comments are syntactically equivalent to | 
|---|
| 165 | whitespace. | 
|---|
| 166 |  | 
|---|
| 167 |  | 
|---|
| 168 | File: ld.info,  Node: Simple Example,  Next: Simple Commands,  Prev: Script Format,  Up: Scripts | 
|---|
| 169 |  | 
|---|
| 170 | Simple Linker Script Example | 
|---|
| 171 | ============================ | 
|---|
| 172 |  | 
|---|
| 173 | Many linker scripts are fairly simple. | 
|---|
| 174 |  | 
|---|
| 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. | 
|---|
| 178 |  | 
|---|
| 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 | 
|---|
| 184 | files. | 
|---|
| 185 |  | 
|---|
| 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: | 
|---|
| 189 | SECTIONS | 
|---|
| 190 | { | 
|---|
| 191 | . = 0x10000; | 
|---|
| 192 | .text : { *(.text) } | 
|---|
| 193 | . = 0x8000000; | 
|---|
| 194 | .data : { *(.data) } | 
|---|
| 195 | .bss : { *(.bss) } | 
|---|
| 196 | } | 
|---|
| 197 |  | 
|---|
| 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. | 
|---|
| 201 |  | 
|---|
| 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'. | 
|---|
| 209 |  | 
|---|
| 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. | 
|---|
| 216 |  | 
|---|
| 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'. | 
|---|
| 220 |  | 
|---|
| 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 | 
|---|
| 227 | section in memory | 
|---|
| 228 |  | 
|---|
| 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. | 
|---|
| 234 |  | 
|---|
| 235 | That's it!  That's a simple and complete linker script. | 
|---|
| 236 |  | 
|---|
| 237 |  | 
|---|
| 238 | File: ld.info,  Node: Simple Commands,  Next: Assignments,  Prev: Simple Example,  Up: Scripts | 
|---|
| 239 |  | 
|---|
| 240 | Simple Linker Script Commands | 
|---|
| 241 | ============================= | 
|---|
| 242 |  | 
|---|
| 243 | In this section we describe the simple linker script commands. | 
|---|
| 244 |  | 
|---|
| 245 | * Menu: | 
|---|
| 246 |  | 
|---|
| 247 | * Entry Point::                 Setting the entry point | 
|---|
| 248 | * File Commands::               Commands dealing with files | 
|---|
| 249 |  | 
|---|
| 250 | * Format Commands::             Commands dealing with object file formats | 
|---|
| 251 |  | 
|---|
| 252 | * Miscellaneous Commands::      Other linker script commands | 
|---|
| 253 |  | 
|---|
| 254 |  | 
|---|
| 255 | File: ld.info,  Node: Entry Point,  Next: File Commands,  Up: Simple Commands | 
|---|
| 256 |  | 
|---|
| 257 | Setting the entry point | 
|---|
| 258 | ----------------------- | 
|---|
| 259 |  | 
|---|
| 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: | 
|---|
| 263 | ENTRY(SYMBOL) | 
|---|
| 264 |  | 
|---|
| 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; | 
|---|
| 269 |  | 
|---|
| 270 | * the `ENTRY(SYMBOL)' command in a linker script; | 
|---|
| 271 |  | 
|---|
| 272 | * the value of the symbol `start', if defined; | 
|---|
| 273 |  | 
|---|
| 274 | * the address of the first byte of the `.text' section, if present; | 
|---|
| 275 |  | 
|---|
| 276 | * The address `0'. | 
|---|
| 277 |  | 
|---|
| 278 |  | 
|---|
| 279 | File: ld.info,  Node: File Commands,  Next: Format Commands,  Prev: Entry Point,  Up: Simple Commands | 
|---|
| 280 |  | 
|---|
| 281 | Commands dealing with files | 
|---|
| 282 | --------------------------- | 
|---|
| 283 |  | 
|---|
| 284 | Several linker script commands deal with files. | 
|---|
| 285 |  | 
|---|
| 286 | `INCLUDE FILENAME' | 
|---|
| 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. | 
|---|
| 291 |  | 
|---|
| 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. | 
|---|
| 296 |  | 
|---|
| 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. | 
|---|
| 300 |  | 
|---|
| 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' | 
|---|
| 303 | option. | 
|---|
| 304 |  | 
|---|
| 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. | 
|---|
| 309 |  | 
|---|
| 310 | If you use `INPUT (-lFILE)', `ld' will transform the name to | 
|---|
| 311 | `libFILE.a', as with the command line argument `-l'. | 
|---|
| 312 |  | 
|---|
| 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. | 
|---|
| 316 |  | 
|---|
| 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. | 
|---|
| 323 |  | 
|---|
| 324 | `OUTPUT(FILENAME)' | 
|---|
| 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 | 
|---|
| 329 | precedence. | 
|---|
| 330 |  | 
|---|
| 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'. | 
|---|
| 333 |  | 
|---|
| 334 | `SEARCH_DIR(PATH)' | 
|---|
| 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 | 
|---|
| 340 | searched first. | 
|---|
| 341 |  | 
|---|
| 342 | `STARTUP(FILENAME)' | 
|---|
| 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. | 
|---|
| 348 |  | 
|---|
| 349 |  | 
|---|
| 350 | File: ld.info,  Node: Format Commands,  Next: Miscellaneous Commands,  Prev: File Commands,  Up: Simple Commands | 
|---|
| 351 |  | 
|---|
| 352 | Commands dealing with object file formats | 
|---|
| 353 | ----------------------------------------- | 
|---|
| 354 |  | 
|---|
| 355 | A couple of linker script commands deal with object file formats. | 
|---|
| 356 |  | 
|---|
| 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. | 
|---|
| 364 |  | 
|---|
| 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 | 
|---|
| 368 | desired endianness. | 
|---|
| 369 |  | 
|---|
| 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. | 
|---|
| 374 |  | 
|---|
| 375 | For example, the default linker script for the MIPS ELF target | 
|---|
| 376 | uses this command: | 
|---|
| 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' | 
|---|
| 381 | format. | 
|---|
| 382 |  | 
|---|
| 383 | `TARGET(BFDNAME)' | 
|---|
| 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::. | 
|---|
| 390 |  | 
|---|
| 391 |  | 
|---|
| 392 | File: ld.info,  Node: Miscellaneous Commands,  Prev: Format Commands,  Up: Simple Commands | 
|---|
| 393 |  | 
|---|
| 394 | Other linker script commands | 
|---|
| 395 | ---------------------------- | 
|---|
| 396 |  | 
|---|
| 397 | There are a few other linker scripts commands. | 
|---|
| 398 |  | 
|---|
| 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. | 
|---|
| 402 |  | 
|---|
| 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. | 
|---|
| 409 |  | 
|---|
| 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'). | 
|---|
| 414 |  | 
|---|
| 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. | 
|---|
| 418 |  | 
|---|
| 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. | 
|---|
| 424 |  | 
|---|
| 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 | 
|---|
| 429 | names. | 
|---|
| 430 |  | 
|---|
| 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. | 
|---|
| 436 |  | 
|---|
| 437 |  | 
|---|
| 438 | File: ld.info,  Node: Assignments,  Next: SECTIONS,  Prev: Simple Commands,  Up: Scripts | 
|---|
| 439 |  | 
|---|
| 440 | Assigning Values to Symbols | 
|---|
| 441 | =========================== | 
|---|
| 442 |  | 
|---|
| 443 | You may assign a value to a symbol in a linker script.  This will | 
|---|
| 444 | define the symbol as a global symbol. | 
|---|
| 445 |  | 
|---|
| 446 | * Menu: | 
|---|
| 447 |  | 
|---|
| 448 | * Simple Assignments::          Simple Assignments | 
|---|
| 449 | * PROVIDE::                     PROVIDE | 
|---|
| 450 |  | 
|---|
| 451 |  | 
|---|
| 452 | File: ld.info,  Node: Simple Assignments,  Next: PROVIDE,  Up: Assignments | 
|---|
| 453 |  | 
|---|
| 454 | Simple Assignments | 
|---|
| 455 | ------------------ | 
|---|
| 456 |  | 
|---|
| 457 | You may assign to a symbol using any of the C assignment operators: | 
|---|
| 458 |  | 
|---|
| 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. | 
|---|
| 471 |  | 
|---|
| 472 | The special symbol name `.' indicates the location counter.  You may | 
|---|
| 473 | only use this within a `SECTIONS' command. | 
|---|
| 474 |  | 
|---|
| 475 | The semicolon after EXPRESSION is required. | 
|---|
| 476 |  | 
|---|
| 477 | Expressions are defined below; see *Note Expressions::. | 
|---|
| 478 |  | 
|---|
| 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. | 
|---|
| 482 |  | 
|---|
| 483 | The section of the symbol will be set from the section of the | 
|---|
| 484 | expression; for more information, see *Note Expression Section::. | 
|---|
| 485 |  | 
|---|
| 486 | Here is an example showing the three different places that symbol | 
|---|
| 487 | assignments may be used: | 
|---|
| 488 |  | 
|---|
| 489 | floating_point = 0; | 
|---|
| 490 | SECTIONS | 
|---|
| 491 | { | 
|---|
| 492 | .text : | 
|---|
| 493 | { | 
|---|
| 494 | *(.text) | 
|---|
| 495 | _etext = .; | 
|---|
| 496 | } | 
|---|
| 497 | _bdata = (. + 3) & ~ 4; | 
|---|
| 498 | .data : { *(.data) } | 
|---|
| 499 | } | 
|---|
| 500 |  | 
|---|
| 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 | 
|---|
| 505 | boundary. | 
|---|
| 506 |  | 
|---|
| 507 |  | 
|---|
| 508 | File: ld.info,  Node: PROVIDE,  Prev: Simple Assignments,  Up: Assignments | 
|---|
| 509 |  | 
|---|
| 510 | PROVIDE | 
|---|
| 511 | ------- | 
|---|
| 512 |  | 
|---|
| 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)'. | 
|---|
| 520 |  | 
|---|
| 521 | Here is an example of using `PROVIDE' to define `etext': | 
|---|
| 522 | SECTIONS | 
|---|
| 523 | { | 
|---|
| 524 | .text : | 
|---|
| 525 | { | 
|---|
| 526 | *(.text) | 
|---|
| 527 | _etext = .; | 
|---|
| 528 | PROVIDE(etext = .); | 
|---|
| 529 | } | 
|---|
| 530 | } | 
|---|
| 531 |  | 
|---|
| 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. | 
|---|
| 538 |  | 
|---|
| 539 |  | 
|---|
| 540 | File: ld.info,  Node: SECTIONS,  Next: MEMORY,  Prev: Assignments,  Up: Scripts | 
|---|
| 541 |  | 
|---|
| 542 | SECTIONS command | 
|---|
| 543 | ================ | 
|---|
| 544 |  | 
|---|
| 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. | 
|---|
| 547 |  | 
|---|
| 548 | The format of the `SECTIONS' command is: | 
|---|
| 549 | SECTIONS | 
|---|
| 550 | { | 
|---|
| 551 | SECTIONS-COMMAND | 
|---|
| 552 | SECTIONS-COMMAND | 
|---|
| 553 | ... | 
|---|
| 554 | } | 
|---|
| 555 |  | 
|---|
| 556 | Each SECTIONS-COMMAND may of be one of the following: | 
|---|
| 557 |  | 
|---|
| 558 | * an `ENTRY' command (*note Entry command: Entry Point.) | 
|---|
| 559 |  | 
|---|
| 560 | * a symbol assignment (*note Assignments::) | 
|---|
| 561 |  | 
|---|
| 562 | * an output section description | 
|---|
| 563 |  | 
|---|
| 564 | * an overlay description | 
|---|
| 565 |  | 
|---|
| 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. | 
|---|
| 571 |  | 
|---|
| 572 | Output section descriptions and overlay descriptions are described | 
|---|
| 573 | below. | 
|---|
| 574 |  | 
|---|
| 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. | 
|---|
| 581 |  | 
|---|
| 582 | * Menu: | 
|---|
| 583 |  | 
|---|
| 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 | 
|---|
| 593 |  | 
|---|
| 594 |  | 
|---|
| 595 | File: ld.info,  Node: Output Section Description,  Next: Output Section Name,  Up: SECTIONS | 
|---|
| 596 |  | 
|---|
| 597 | Output section description | 
|---|
| 598 | -------------------------- | 
|---|
| 599 |  | 
|---|
| 600 | The full description of an output section looks like this: | 
|---|
| 601 | SECTION [ADDRESS] [(TYPE)] : [AT(LMA)] | 
|---|
| 602 | { | 
|---|
| 603 | OUTPUT-SECTION-COMMAND | 
|---|
| 604 | OUTPUT-SECTION-COMMAND | 
|---|
| 605 | ... | 
|---|
| 606 | } [>REGION] [AT>LMA_REGION] [:PHDR :PHDR ...] [=FILLEXP] | 
|---|
| 607 |  | 
|---|
| 608 | Most output sections do not use most of the optional section | 
|---|
| 609 | attributes. | 
|---|
| 610 |  | 
|---|
| 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. | 
|---|
| 614 |  | 
|---|
| 615 | Each OUTPUT-SECTION-COMMAND may be one of the following: | 
|---|
| 616 |  | 
|---|
| 617 | * a symbol assignment (*note Assignments::) | 
|---|
| 618 |  | 
|---|
| 619 | * an input section description (*note Input Section::) | 
|---|
| 620 |  | 
|---|
| 621 | * data values to include directly (*note Output Section Data::) | 
|---|
| 622 |  | 
|---|
| 623 | * a special output section keyword (*note Output Section Keywords::) | 
|---|
| 624 |  | 
|---|
| 625 |  | 
|---|
| 626 | File: ld.info,  Node: Output Section Name,  Next: Output Section Address,  Prev: Output Section Description,  Up: SECTIONS | 
|---|
| 627 |  | 
|---|
| 628 | Output section name | 
|---|
| 629 | ------------------- | 
|---|
| 630 |  | 
|---|
| 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. | 
|---|
| 640 |  | 
|---|
| 641 | The output section name `/DISCARD/' is special; *Note Output Section | 
|---|
| 642 | Discarding::. | 
|---|
| 643 |  | 
|---|
| 644 |  | 
|---|
| 645 | File: ld.info,  Node: Output Section Address,  Next: Input Section,  Prev: Output Section Name,  Up: SECTIONS | 
|---|
| 646 |  | 
|---|
| 647 | Output section address | 
|---|
| 648 | ---------------------- | 
|---|
| 649 |  | 
|---|
| 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. | 
|---|
| 654 |  | 
|---|
| 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 | 
|---|
| 661 | section. | 
|---|
| 662 |  | 
|---|
| 663 | For example, | 
|---|
| 664 | .text . : { *(.text) } | 
|---|
| 665 |  | 
|---|
| 666 | and | 
|---|
| 667 | .text : { *(.text) } | 
|---|
| 668 |  | 
|---|
| 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. | 
|---|
| 673 |  | 
|---|
| 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) } | 
|---|
| 679 |  | 
|---|
| 680 | This works because `ALIGN' returns the current location counter aligned | 
|---|
| 681 | upward to the specified value. | 
|---|
| 682 |  | 
|---|
| 683 | Specifying ADDRESS for a section will change the value of the | 
|---|
| 684 | location counter. | 
|---|
| 685 |  | 
|---|
| 686 |  | 
|---|
| 687 | File: ld.info,  Node: Input Section,  Next: Output Section Data,  Prev: Output Section Address,  Up: SECTIONS | 
|---|
| 688 |  | 
|---|
| 689 | Input section description | 
|---|
| 690 | ------------------------- | 
|---|
| 691 |  | 
|---|
| 692 | The most common output section command is an input section | 
|---|
| 693 | description. | 
|---|
| 694 |  | 
|---|
| 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. | 
|---|
| 699 |  | 
|---|
| 700 | * Menu: | 
|---|
| 701 |  | 
|---|
| 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 | 
|---|
| 707 |  | 
|---|
| 708 |  | 
|---|
| 709 | File: ld.info,  Node: Input Section Basics,  Next: Input Section Wildcards,  Up: Input Section | 
|---|
| 710 |  | 
|---|
| 711 | Input section basics | 
|---|
| 712 | .................... | 
|---|
| 713 |  | 
|---|
| 714 | An input section description consists of a file name optionally | 
|---|
| 715 | followed by a list of section names in parentheses. | 
|---|
| 716 |  | 
|---|
| 717 | The file name and the section name may be wildcard patterns, which we | 
|---|
| 718 | describe further below (*note Input Section Wildcards::). | 
|---|
| 719 |  | 
|---|
| 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: | 
|---|
| 723 | *(.text) | 
|---|
| 724 |  | 
|---|
| 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 | 
|---|
| 728 | list.  For example: | 
|---|
| 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. | 
|---|
| 732 |  | 
|---|
| 733 | There are two ways to include more than one section: | 
|---|
| 734 | *(.text .rdata) | 
|---|
| 735 | *(.text) *(.rdata) | 
|---|
| 736 |  | 
|---|
| 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' | 
|---|
| 741 | input sections. | 
|---|
| 742 |  | 
|---|
| 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: | 
|---|
| 746 | data.o(.data) | 
|---|
| 747 |  | 
|---|
| 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: | 
|---|
| 751 | data.o | 
|---|
| 752 |  | 
|---|
| 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 | 
|---|
| 759 | archive search path. | 
|---|
| 760 |  | 
|---|
| 761 |  | 
|---|
| 762 | File: ld.info,  Node: Input Section Wildcards,  Next: Input Section Common,  Prev: Input Section Basics,  Up: Input Section | 
|---|
| 763 |  | 
|---|
| 764 | Input section wildcard patterns | 
|---|
| 765 | ............................... | 
|---|
| 766 |  | 
|---|
| 767 | In an input section description, either the file name or the section | 
|---|
| 768 | name or both may be wildcard patterns. | 
|---|
| 769 |  | 
|---|
| 770 | The file name of `*' seen in many examples is a simple wildcard | 
|---|
| 771 | pattern for the file name. | 
|---|
| 772 |  | 
|---|
| 773 | The wildcard patterns are like those used by the Unix shell. | 
|---|
| 774 |  | 
|---|
| 775 | `*' | 
|---|
| 776 | matches any number of characters | 
|---|
| 777 |  | 
|---|
| 778 | `?' | 
|---|
| 779 | matches any single character | 
|---|
| 780 |  | 
|---|
| 781 | `[CHARS]' | 
|---|
| 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 | 
|---|
| 785 |  | 
|---|
| 786 | `\' | 
|---|
| 787 | quotes the following character | 
|---|
| 788 |  | 
|---|
| 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. | 
|---|
| 794 |  | 
|---|
| 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. | 
|---|
| 798 |  | 
|---|
| 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: | 
|---|
| 804 | .data : { *(.data) } | 
|---|
| 805 | .data1 : { data.o(.data) } | 
|---|
| 806 |  | 
|---|
| 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. | 
|---|
| 813 |  | 
|---|
| 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. | 
|---|
| 817 |  | 
|---|
| 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'. | 
|---|
| 824 | SECTIONS { | 
|---|
| 825 | .text : { *(.text) } | 
|---|
| 826 | .DATA : { [A-Z]*(.data) } | 
|---|
| 827 | .data : { *(.data) } | 
|---|
| 828 | .bss : { *(.bss) } | 
|---|
| 829 | } | 
|---|
| 830 |  | 
|---|
| 831 |  | 
|---|
| 832 | File: ld.info,  Node: Input Section Common,  Next: Input Section Keep,  Prev: Input Section Wildcards,  Up: Input Section | 
|---|
| 833 |  | 
|---|
| 834 | Input section for common symbols | 
|---|
| 835 | ................................ | 
|---|
| 836 |  | 
|---|
| 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'. | 
|---|
| 841 |  | 
|---|
| 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. | 
|---|
| 846 |  | 
|---|
| 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) } | 
|---|
| 850 |  | 
|---|
| 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 | 
|---|
| 858 | locations. | 
|---|
| 859 |  | 
|---|
| 860 | You will sometimes see `[COMMON]' in old linker scripts.  This | 
|---|
| 861 | notation is now considered obsolete.  It is equivalent to `*(COMMON)'. | 
|---|
| 862 |  | 
|---|
| 863 |  | 
|---|
| 864 | File: ld.info,  Node: Input Section Keep,  Next: Input Section Example,  Prev: Input Section Common,  Up: Input Section | 
|---|
| 865 |  | 
|---|
| 866 | Input section and garbage collection | 
|---|
| 867 | .................................... | 
|---|
| 868 |  | 
|---|
| 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))'. | 
|---|
| 873 |  | 
|---|
| 874 |  | 
|---|
| 875 | File: ld.info,  Node: Input Section Example,  Prev: Input Section Keep,  Up: Input Section | 
|---|
| 876 |  | 
|---|
| 877 | Input section example | 
|---|
| 878 | ..................... | 
|---|
| 879 |  | 
|---|
| 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'. | 
|---|
| 888 |  | 
|---|
| 889 | SECTIONS { | 
|---|
| 890 | outputa 0x10000 : | 
|---|
| 891 | { | 
|---|
| 892 | all.o | 
|---|
| 893 | foo.o (.input1) | 
|---|
| 894 | } | 
|---|
| 895 | outputb : | 
|---|
| 896 | { | 
|---|
| 897 | foo.o (.input2) | 
|---|
| 898 | foo1.o (.input1) | 
|---|
| 899 | } | 
|---|
| 900 | outputc : | 
|---|
| 901 | { | 
|---|
| 902 | *(.input1) | 
|---|
| 903 | *(.input2) | 
|---|
| 904 | } | 
|---|
| 905 | } | 
|---|
| 906 |  | 
|---|
| 907 |  | 
|---|
| 908 | File: ld.info,  Node: Output Section Data,  Next: Output Section Keywords,  Prev: Input Section,  Up: SECTIONS | 
|---|
| 909 |  | 
|---|
| 910 | Output section data | 
|---|
| 911 | ------------------- | 
|---|
| 912 |  | 
|---|
| 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. | 
|---|
| 918 |  | 
|---|
| 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. | 
|---|
| 922 |  | 
|---|
| 923 | For example, this will store the byte 1 followed by the four byte | 
|---|
| 924 | value of the symbol `addr': | 
|---|
| 925 | BYTE(1) | 
|---|
| 926 | LONG(addr) | 
|---|
| 927 |  | 
|---|
| 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. | 
|---|
| 933 |  | 
|---|
| 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. | 
|---|
| 939 |  | 
|---|
| 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) } } | 
|---|
| 945 |  | 
|---|
| 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. | 
|---|
| 955 |  | 
|---|
| 956 | This example shows how to fill unspecified regions of memory with the | 
|---|
| 957 | value `0x9090': | 
|---|
| 958 | FILL(0x9090) | 
|---|
| 959 |  | 
|---|
| 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. | 
|---|
| 964 |  | 
|---|
| 965 |  | 
|---|
| 966 | File: ld.info,  Node: Output Section Keywords,  Next: Output Section Discarding,  Prev: Output Section Data,  Up: SECTIONS | 
|---|
| 967 |  | 
|---|
| 968 | Output section keywords | 
|---|
| 969 | ----------------------- | 
|---|
| 970 |  | 
|---|
| 971 | There are a couple of keywords which can appear as output section | 
|---|
| 972 | commands. | 
|---|
| 973 |  | 
|---|
| 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 | 
|---|
| 979 | appears. | 
|---|
| 980 |  | 
|---|
| 981 | This is conventional for the a.out object file format.  It is not | 
|---|
| 982 | normally used for any other object file format. | 
|---|
| 983 |  | 
|---|
| 984 | `CONSTRUCTORS' | 
|---|
| 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. | 
|---|
| 994 |  | 
|---|
| 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 | 
|---|
| 1004 | `exit'. | 
|---|
| 1005 |  | 
|---|
| 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. | 
|---|
| 1012 |  | 
|---|
| 1013 | __CTOR_LIST__ = .; | 
|---|
| 1014 | LONG((__CTOR_END__ - __CTOR_LIST__) / 4 - 2) | 
|---|
| 1015 | *(.ctors) | 
|---|
| 1016 | LONG(0) | 
|---|
| 1017 | __CTOR_END__ = .; | 
|---|
| 1018 | __DTOR_LIST__ = .; | 
|---|
| 1019 | LONG((__DTOR_END__ - __DTOR_LIST__) / 4 - 2) | 
|---|
| 1020 | *(.dtors) | 
|---|
| 1021 | LONG(0) | 
|---|
| 1022 | __DTOR_END__ = .; | 
|---|
| 1023 |  | 
|---|
| 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)'. | 
|---|
| 1032 |  | 
|---|
| 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. | 
|---|
| 1037 |  | 
|---|
| 1038 |  | 
|---|
| 1039 | File: ld.info,  Node: Output Section Discarding,  Next: Output Section Attributes,  Prev: Output Section Keywords,  Up: SECTIONS | 
|---|
| 1040 |  | 
|---|
| 1041 | Output section discarding | 
|---|
| 1042 | ------------------------- | 
|---|
| 1043 |  | 
|---|
| 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: | 
|---|
| 1047 | .foo { *(.foo) } | 
|---|
| 1048 |  | 
|---|
| 1049 | will only create a `.foo' section in the output file if there is a | 
|---|
| 1050 | `.foo' section in at least one input file. | 
|---|
| 1051 |  | 
|---|
| 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 | 
|---|
| 1055 | sections. | 
|---|
| 1056 |  | 
|---|
| 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. | 
|---|
| 1060 |  | 
|---|
| 1061 |  | 
|---|
| 1062 | File: ld.info,  Node: Output Section Attributes,  Next: Overlay Description,  Prev: Output Section Discarding,  Up: SECTIONS | 
|---|
| 1063 |  | 
|---|
| 1064 | Output section attributes | 
|---|
| 1065 | ------------------------- | 
|---|
| 1066 |  | 
|---|
| 1067 | We showed above that the full description of an output section looked | 
|---|
| 1068 | like this: | 
|---|
| 1069 | SECTION [ADDRESS] [(TYPE)] : [AT(LMA)] | 
|---|
| 1070 | { | 
|---|
| 1071 | OUTPUT-SECTION-COMMAND | 
|---|
| 1072 | OUTPUT-SECTION-COMMAND | 
|---|
| 1073 | ... | 
|---|
| 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 | 
|---|
| 1077 | section attributes. | 
|---|
| 1078 |  | 
|---|
| 1079 | * Menu: | 
|---|
| 1080 |  | 
|---|
| 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 | 
|---|
| 1086 |  | 
|---|
| 1087 |  | 
|---|
| 1088 | File: ld.info,  Node: Output Section Type,  Next: Output Section LMA,  Up: Output Section Attributes | 
|---|
| 1089 |  | 
|---|
| 1090 | Output section type | 
|---|
| 1091 | ................... | 
|---|
| 1092 |  | 
|---|
| 1093 | Each output section may have a type.  The type is a keyword in | 
|---|
| 1094 | parentheses.  The following types are defined: | 
|---|
| 1095 |  | 
|---|
| 1096 | `NOLOAD' | 
|---|
| 1097 | The section should be marked as not loadable, so that it will not | 
|---|
| 1098 | be loaded into memory when the program is run. | 
|---|
| 1099 |  | 
|---|
| 1100 | `DSECT' | 
|---|
| 1101 | `COPY' | 
|---|
| 1102 | `INFO' | 
|---|
| 1103 | `OVERLAY' | 
|---|
| 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. | 
|---|
| 1108 |  | 
|---|
| 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. | 
|---|
| 1115 | SECTIONS { | 
|---|
| 1116 | ROM 0 (NOLOAD) : { ... } | 
|---|
| 1117 | ... | 
|---|
| 1118 | } | 
|---|
| 1119 |  | 
|---|
| 1120 |  | 
|---|
| 1121 | File: ld.info,  Node: Output Section LMA,  Next: Output Section Region,  Prev: Output Section Type,  Up: Output Section Attributes | 
|---|
| 1122 |  | 
|---|
| 1123 | Output section LMA | 
|---|
| 1124 | .................. | 
|---|
| 1125 |  | 
|---|
| 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 | 
|---|
| 1129 | Section Address::). | 
|---|
| 1130 |  | 
|---|
| 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::. | 
|---|
| 1136 |  | 
|---|
| 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. | 
|---|
| 1144 |  | 
|---|
| 1145 | SECTIONS | 
|---|
| 1146 | { | 
|---|
| 1147 | .text 0x1000 : { *(.text) _etext = . ; } | 
|---|
| 1148 | .mdata 0x2000 : | 
|---|
| 1149 | AT ( ADDR (.text) + SIZEOF (.text) ) | 
|---|
| 1150 | { _data = . ; *(.data); _edata = . ;  } | 
|---|
| 1151 | .bss 0x3000 : | 
|---|
| 1152 | { _bstart = . ;  *(.bss) *(COMMON) ; _bend = . ;} | 
|---|
| 1153 | } | 
|---|
| 1154 |  | 
|---|
| 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 | 
|---|
| 1159 | linker script. | 
|---|
| 1160 |  | 
|---|
| 1161 | extern char _etext, _data, _edata, _bstart, _bend; | 
|---|
| 1162 | char *src = &_etext; | 
|---|
| 1163 | char *dst = &_data; | 
|---|
| 1164 |  | 
|---|
| 1165 | /* ROM has data at end of text; copy it. */ | 
|---|
| 1166 | while (dst < &_edata) { | 
|---|
| 1167 | *dst++ = *src++; | 
|---|
| 1168 | } | 
|---|
| 1169 |  | 
|---|
| 1170 | /* Zero bss */ | 
|---|
| 1171 | for (dst = &_bstart; dst< &_bend; dst++) | 
|---|
| 1172 | *dst = 0; | 
|---|
| 1173 |  | 
|---|
| 1174 |  | 
|---|
| 1175 | File: ld.info,  Node: Output Section Region,  Next: Output Section Phdr,  Prev: Output Section LMA,  Up: Output Section Attributes | 
|---|
| 1176 |  | 
|---|
| 1177 | Output section region | 
|---|
| 1178 | ..................... | 
|---|
| 1179 |  | 
|---|
| 1180 | You can assign a section to a previously defined region of memory by | 
|---|
| 1181 | using `>REGION'.  *Note MEMORY::. | 
|---|
| 1182 |  | 
|---|
| 1183 | Here is a simple example: | 
|---|
| 1184 | MEMORY { rom : ORIGIN = 0x1000, LENGTH = 0x1000 } | 
|---|
| 1185 | SECTIONS { ROM : { *(.text) } >rom } | 
|---|
| 1186 |  | 
|---|
| 1187 |  | 
|---|
| 1188 | File: ld.info,  Node: Output Section Phdr,  Next: Output Section Fill,  Prev: Output Section Region,  Up: Output Section Attributes | 
|---|
| 1189 |  | 
|---|
| 1190 | Output section phdr | 
|---|
| 1191 | ................... | 
|---|
| 1192 |  | 
|---|
| 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 | 
|---|
| 1198 | segment at all. | 
|---|
| 1199 |  | 
|---|
| 1200 | Here is a simple example: | 
|---|
| 1201 | PHDRS { text PT_LOAD ; } | 
|---|
| 1202 | SECTIONS { .text : { *(.text) } :text } | 
|---|
| 1203 |  | 
|---|
| 1204 |  | 
|---|
| 1205 | File: ld.info,  Node: Output Section Fill,  Prev: Output Section Phdr,  Up: Output Section Attributes | 
|---|
| 1206 |  | 
|---|
| 1207 | Output section fill | 
|---|
| 1208 | ................... | 
|---|
| 1209 |  | 
|---|
| 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. | 
|---|
| 1216 |  | 
|---|
| 1217 | You can also change the fill value with a `FILL' command in the | 
|---|
| 1218 | output section commands; see *Note Output Section Data::. | 
|---|
| 1219 |  | 
|---|
| 1220 | Here is a simple example: | 
|---|
| 1221 | SECTIONS { .text : { *(.text) } =0x9090 } | 
|---|
| 1222 |  | 
|---|