| [609] | 1 | This is ld.info, produced by makeinfo version 4.3 from ./ld.texinfo.
|
|---|
| [10] | 2 |
|
|---|
| 3 | START-INFO-DIR-ENTRY
|
|---|
| 4 | * Ld: (ld). The GNU linker.
|
|---|
| 5 | END-INFO-DIR-ENTRY
|
|---|
| 6 |
|
|---|
| [609] | 7 | This file documents the GNU linker LD version 2.14.
|
|---|
| [10] | 8 |
|
|---|
| [609] | 9 | Copyright (C) 1991, 92, 93, 94, 95, 96, 97, 98, 99, 2000, 2001,
|
|---|
| 10 | 2002, 2003 Free Software Foundation, Inc.
|
|---|
| [10] | 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
|
|---|
| [609] | 19 | `GNUTARGET', `LDEMULATION' and `COLLECT_NO_DEMANGLE'.
|
|---|
| [10] | 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 |
|
|---|
| [609] | 257 | Setting the Entry Point
|
|---|
| [10] | 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 |
|
|---|
| [609] | 281 | Commands Dealing with Files
|
|---|
| [10] | 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 |
|
|---|
| [609] | 305 | In case a "sysroot prefix" is configured, and the filename starts
|
|---|
| 306 | with the `/' character, and the script being processed was located
|
|---|
| 307 | inside the "sysroot prefix", the filename will be looked for in
|
|---|
| 308 | the "sysroot prefix". Otherwise, the linker will try to open the
|
|---|
| 309 | file in the current directory. If it is not found, the linker
|
|---|
| 310 | will search through the archive library search path. See the
|
|---|
| 311 | description of `-L' in *Note Command Line Options: Options.
|
|---|
| [10] | 312 |
|
|---|
| 313 | If you use `INPUT (-lFILE)', `ld' will transform the name to
|
|---|
| 314 | `libFILE.a', as with the command line argument `-l'.
|
|---|
| 315 |
|
|---|
| 316 | When you use the `INPUT' command in an implicit linker script, the
|
|---|
| 317 | files will be included in the link at the point at which the linker
|
|---|
| |
|---|