| 1 | This is ld.info, produced by makeinfo version 4.3 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.14. | 
|---|
| 8 |  | 
|---|
| 9 | Copyright (C) 1991, 92, 93, 94, 95, 96, 97, 98, 99, 2000, 2001, | 
|---|
| 10 | 2002, 2003 Free Software Foundation, Inc. | 
|---|
| 11 |  | 
|---|
| 12 |  | 
|---|
| 13 | File: ld.info,  Node: Overlay Description,  Prev: Output Section Attributes,  Up: SECTIONS | 
|---|
| 14 |  | 
|---|
| 15 | Overlay Description | 
|---|
| 16 | ------------------- | 
|---|
| 17 |  | 
|---|
| 18 | An overlay description provides an easy way to describe sections | 
|---|
| 19 | which are to be loaded as part of a single memory image but are to be | 
|---|
| 20 | run at the same memory address.  At run time, some sort of overlay | 
|---|
| 21 | manager will copy the overlaid sections in and out of the runtime | 
|---|
| 22 | memory address as required, perhaps by simply manipulating addressing | 
|---|
| 23 | bits.  This approach can be useful, for example, when a certain region | 
|---|
| 24 | of memory is faster than another. | 
|---|
| 25 |  | 
|---|
| 26 | Overlays are described using the `OVERLAY' command.  The `OVERLAY' | 
|---|
| 27 | command is used within a `SECTIONS' command, like an output section | 
|---|
| 28 | description.  The full syntax of the `OVERLAY' command is as follows: | 
|---|
| 29 | OVERLAY [START] : [NOCROSSREFS] [AT ( LDADDR )] | 
|---|
| 30 | { | 
|---|
| 31 | SECNAME1 | 
|---|
| 32 | { | 
|---|
| 33 | OUTPUT-SECTION-COMMAND | 
|---|
| 34 | OUTPUT-SECTION-COMMAND | 
|---|
| 35 | ... | 
|---|
| 36 | } [:PHDR...] [=FILL] | 
|---|
| 37 | SECNAME2 | 
|---|
| 38 | { | 
|---|
| 39 | OUTPUT-SECTION-COMMAND | 
|---|
| 40 | OUTPUT-SECTION-COMMAND | 
|---|
| 41 | ... | 
|---|
| 42 | } [:PHDR...] [=FILL] | 
|---|
| 43 | ... | 
|---|
| 44 | } [>REGION] [:PHDR...] [=FILL] | 
|---|
| 45 |  | 
|---|
| 46 | Everything is optional except `OVERLAY' (a keyword), and each | 
|---|
| 47 | section must have a name (SECNAME1 and SECNAME2 above).  The section | 
|---|
| 48 | definitions within the `OVERLAY' construct are identical to those | 
|---|
| 49 | within the general `SECTIONS' contruct (*note SECTIONS::), except that | 
|---|
| 50 | no addresses and no memory regions may be defined for sections within | 
|---|
| 51 | an `OVERLAY'. | 
|---|
| 52 |  | 
|---|
| 53 | The sections are all defined with the same starting address.  The | 
|---|
| 54 | load addresses of the sections are arranged such that they are | 
|---|
| 55 | consecutive in memory starting at the load address used for the | 
|---|
| 56 | `OVERLAY' as a whole (as with normal section definitions, the load | 
|---|
| 57 | address is optional, and defaults to the start address; the start | 
|---|
| 58 | address is also optional, and defaults to the current value of the | 
|---|
| 59 | location counter). | 
|---|
| 60 |  | 
|---|
| 61 | If the `NOCROSSREFS' keyword is used, and there any references among | 
|---|
| 62 | the sections, the linker will report an error.  Since the sections all | 
|---|
| 63 | run at the same address, it normally does not make sense for one | 
|---|
| 64 | section to refer directly to another.  *Note NOCROSSREFS: Miscellaneous | 
|---|
| 65 | Commands. | 
|---|
| 66 |  | 
|---|
| 67 | For each section within the `OVERLAY', the linker automatically | 
|---|
| 68 | defines two symbols.  The symbol `__load_start_SECNAME' is defined as | 
|---|
| 69 | the starting load address of the section.  The symbol | 
|---|
| 70 | `__load_stop_SECNAME' is defined as the final load address of the | 
|---|
| 71 | section.  Any characters within SECNAME which are not legal within C | 
|---|
| 72 | identifiers are removed.  C (or assembler) code may use these symbols | 
|---|
| 73 | to move the overlaid sections around as necessary. | 
|---|
| 74 |  | 
|---|
| 75 | At the end of the overlay, the value of the location counter is set | 
|---|
| 76 | to the start address of the overlay plus the size of the largest | 
|---|
| 77 | section. | 
|---|
| 78 |  | 
|---|
| 79 | Here is an example.  Remember that this would appear inside a | 
|---|
| 80 | `SECTIONS' construct. | 
|---|
| 81 | OVERLAY 0x1000 : AT (0x4000) | 
|---|
| 82 | { | 
|---|
| 83 | .text0 { o1/*.o(.text) } | 
|---|
| 84 | .text1 { o2/*.o(.text) } | 
|---|
| 85 | } | 
|---|
| 86 |  | 
|---|
| 87 | This will define both `.text0' and `.text1' to start at address 0x1000. | 
|---|
| 88 | `.text0' will be loaded at address 0x4000, and `.text1' will be loaded | 
|---|
| 89 | immediately after `.text0'.  The following symbols will be defined: | 
|---|
| 90 | `__load_start_text0', `__load_stop_text0', `__load_start_text1', | 
|---|
| 91 | `__load_stop_text1'. | 
|---|
| 92 |  | 
|---|
| 93 | C code to copy overlay `.text1' into the overlay area might look | 
|---|
| 94 | like the following. | 
|---|
| 95 |  | 
|---|
| 96 | extern char __load_start_text1, __load_stop_text1; | 
|---|
| 97 | memcpy ((char *) 0x1000, &__load_start_text1, | 
|---|
| 98 | &__load_stop_text1 - &__load_start_text1); | 
|---|
| 99 |  | 
|---|
| 100 | Note that the `OVERLAY' command is just syntactic sugar, since | 
|---|
| 101 | everything it does can be done using the more basic commands.  The above | 
|---|
| 102 | example could have been written identically as follows. | 
|---|
| 103 |  | 
|---|
| 104 | .text0 0x1000 : AT (0x4000) { o1/*.o(.text) } | 
|---|
| 105 | __load_start_text0 = LOADADDR (.text0); | 
|---|
| 106 | __load_stop_text0 = LOADADDR (.text0) + SIZEOF (.text0); | 
|---|
| 107 | .text1 0x1000 : AT (0x4000 + SIZEOF (.text0)) { o2/*.o(.text) } | 
|---|
| 108 | __load_start_text1 = LOADADDR (.text1); | 
|---|
| 109 | __load_stop_text1 = LOADADDR (.text1) + SIZEOF (.text1); | 
|---|
| 110 | . = 0x1000 + MAX (SIZEOF (.text0), SIZEOF (.text1)); | 
|---|
| 111 |  | 
|---|
| 112 |  | 
|---|
| 113 | File: ld.info,  Node: MEMORY,  Next: PHDRS,  Prev: SECTIONS,  Up: Scripts | 
|---|
| 114 |  | 
|---|
| 115 | MEMORY Command | 
|---|
| 116 | ============== | 
|---|
| 117 |  | 
|---|
| 118 | The linker's default configuration permits allocation of all | 
|---|
| 119 | available memory.  You can override this by using the `MEMORY' command. | 
|---|
| 120 |  | 
|---|
| 121 | The `MEMORY' command describes the location and size of blocks of | 
|---|
| 122 | memory in the target.  You can use it to describe which memory regions | 
|---|
| 123 | may be used by the linker, and which memory regions it must avoid.  You | 
|---|
| 124 | can then assign sections to particular memory regions.  The linker will | 
|---|
| 125 | set section addresses based on the memory regions, and will warn about | 
|---|
| 126 | regions that become too full.  The linker will not shuffle sections | 
|---|
| 127 | around to fit into the available regions. | 
|---|
| 128 |  | 
|---|
| 129 | A linker script may contain at most one use of the `MEMORY' command. | 
|---|
| 130 | However, you can define as many blocks of memory within it as you | 
|---|
| 131 | wish.  The syntax is: | 
|---|
| 132 | MEMORY | 
|---|
| 133 | { | 
|---|
| 134 | NAME [(ATTR)] : ORIGIN = ORIGIN, LENGTH = LEN | 
|---|
| 135 | ... | 
|---|
| 136 | } | 
|---|
| 137 |  | 
|---|
| 138 | The NAME is a name used in the linker script to refer to the region. | 
|---|
| 139 | The region name has no meaning outside of the linker script.  Region | 
|---|
| 140 | names are stored in a separate name space, and will not conflict with | 
|---|
| 141 | symbol names, file names, or section names.  Each memory region must | 
|---|
| 142 | have a distinct name. | 
|---|
| 143 |  | 
|---|
| 144 | The ATTR string is an optional list of attributes that specify | 
|---|
| 145 | whether to use a particular memory region for an input section which is | 
|---|
| 146 | not explicitly mapped in the linker script.  As described in *Note | 
|---|
| 147 | SECTIONS::, if you do not specify an output section for some input | 
|---|
| 148 | section, the linker will create an output section with the same name as | 
|---|
| 149 | the input section.  If you define region attributes, the linker will use | 
|---|
| 150 | them to select the memory region for the output section that it creates. | 
|---|
| 151 |  | 
|---|
| 152 | The ATTR string must consist only of the following characters: | 
|---|
| 153 | `R' | 
|---|
| 154 | Read-only section | 
|---|
| 155 |  | 
|---|
| 156 | `W' | 
|---|
| 157 | Read/write section | 
|---|
| 158 |  | 
|---|
| 159 | `X' | 
|---|
| 160 | Executable section | 
|---|
| 161 |  | 
|---|
| 162 | `A' | 
|---|
| 163 | Allocatable section | 
|---|
| 164 |  | 
|---|
| 165 | `I' | 
|---|
| 166 | Initialized section | 
|---|
| 167 |  | 
|---|
| 168 | `L' | 
|---|
| 169 | Same as `I' | 
|---|
| 170 |  | 
|---|
| 171 | `!' | 
|---|
| 172 | Invert the sense of any of the preceding attributes | 
|---|
| 173 |  | 
|---|
| 174 | If a unmapped section matches any of the listed attributes other than | 
|---|
| 175 | `!', it will be placed in the memory region.  The `!' attribute | 
|---|
| 176 | reverses this test, so that an unmapped section will be placed in the | 
|---|
| 177 | memory region only if it does not match any of the listed attributes. | 
|---|
| 178 |  | 
|---|
| 179 | The ORIGIN is an expression for the start address of the memory | 
|---|
| 180 | region.  The expression must evaluate to a constant before memory | 
|---|
| 181 | allocation is performed, which means that you may not use any section | 
|---|
| 182 | relative symbols.  The keyword `ORIGIN' may be abbreviated to `org' or | 
|---|
| 183 | `o' (but not, for example, `ORG'). | 
|---|
| 184 |  | 
|---|
| 185 | The LEN is an expression for the size in bytes of the memory region. | 
|---|
| 186 | As with the ORIGIN expression, the expression must evaluate to a | 
|---|
| 187 | constant before memory allocation is performed.  The keyword `LENGTH' | 
|---|
| 188 | may be abbreviated to `len' or `l'. | 
|---|
| 189 |  | 
|---|
| 190 | In the following example, we specify that there are two memory | 
|---|
| 191 | regions available for allocation: one starting at `0' for 256 kilobytes, | 
|---|
| 192 | and the other starting at `0x40000000' for four megabytes.  The linker | 
|---|
| 193 | will place into the `rom' memory region every section which is not | 
|---|
| 194 | explicitly mapped into a memory region, and is either read-only or | 
|---|
| 195 | executable.  The linker will place other sections which are not | 
|---|
| 196 | explicitly mapped into a memory region into the `ram' memory region. | 
|---|
| 197 |  | 
|---|
| 198 | MEMORY | 
|---|
| 199 | { | 
|---|
| 200 | rom (rx)  : ORIGIN = 0, LENGTH = 256K | 
|---|
| 201 | ram (!rx) : org = 0x40000000, l = 4M | 
|---|
| 202 | } | 
|---|
| 203 |  | 
|---|
| 204 | Once you define a memory region, you can direct the linker to place | 
|---|
| 205 | specific output sections into that memory region by using the `>REGION' | 
|---|
| 206 | output section attribute.  For example, if you have a memory region | 
|---|
| 207 | named `mem', you would use `>mem' in the output section definition. | 
|---|
| 208 | *Note Output Section Region::.  If no address was specified for the | 
|---|
| 209 | output section, the linker will set the address to the next available | 
|---|
| 210 | address within the memory region.  If the combined output sections | 
|---|
| 211 | directed to a memory region are too large for the region, the linker | 
|---|
| 212 | will issue an error message. | 
|---|
| 213 |  | 
|---|
| 214 |  | 
|---|
| 215 | File: ld.info,  Node: PHDRS,  Next: VERSION,  Prev: MEMORY,  Up: Scripts | 
|---|
| 216 |  | 
|---|
| 217 | PHDRS Command | 
|---|
| 218 | ============= | 
|---|
| 219 |  | 
|---|
| 220 | The ELF object file format uses "program headers", also knows as | 
|---|
| 221 | "segments".  The program headers describe how the program should be | 
|---|
| 222 | loaded into memory.  You can print them out by using the `objdump' | 
|---|
| 223 | program with the `-p' option. | 
|---|
| 224 |  | 
|---|
| 225 | When you run an ELF program on a native ELF system, the system loader | 
|---|
| 226 | reads the program headers in order to figure out how to load the | 
|---|
| 227 | program.  This will only work if the program headers are set correctly. | 
|---|
| 228 | This manual does not describe the details of how the system loader | 
|---|
| 229 | interprets program headers; for more information, see the ELF ABI. | 
|---|
| 230 |  | 
|---|
| 231 | The linker will create reasonable program headers by default. | 
|---|
| 232 | However, in some cases, you may need to specify the program headers more | 
|---|
| 233 | precisely.  You may use the `PHDRS' command for this purpose.  When the | 
|---|
| 234 | linker sees the `PHDRS' command in the linker script, it will not | 
|---|
| 235 | create any program headers other than the ones specified. | 
|---|
| 236 |  | 
|---|
| 237 | The linker only pays attention to the `PHDRS' command when | 
|---|
| 238 | generating an ELF output file.  In other cases, the linker will simply | 
|---|
| 239 | ignore `PHDRS'. | 
|---|
| 240 |  | 
|---|
| 241 | This is the syntax of the `PHDRS' command.  The words `PHDRS', | 
|---|
| 242 | `FILEHDR', `AT', and `FLAGS' are keywords. | 
|---|
| 243 |  | 
|---|
| 244 | PHDRS | 
|---|
| 245 | { | 
|---|
| 246 | NAME TYPE [ FILEHDR ] [ PHDRS ] [ AT ( ADDRESS ) ] | 
|---|
| 247 | [ FLAGS ( FLAGS ) ] ; | 
|---|
| 248 | } | 
|---|
| 249 |  | 
|---|
| 250 | The NAME is used only for reference in the `SECTIONS' command of the | 
|---|
| 251 | linker script.  It is not put into the output file.  Program header | 
|---|
| 252 | names are stored in a separate name space, and will not conflict with | 
|---|
| 253 | symbol names, file names, or section names.  Each program header must | 
|---|
| 254 | have a distinct name. | 
|---|
| 255 |  | 
|---|
| 256 | Certain program header types describe segments of memory which the | 
|---|
| 257 | system loader will load from the file.  In the linker script, you | 
|---|
| 258 | specify the contents of these segments by placing allocatable output | 
|---|
| 259 | sections in the segments.  You use the `:PHDR' output section attribute | 
|---|
| 260 | to place a section in a particular segment.  *Note Output Section | 
|---|
| 261 | Phdr::. | 
|---|
| 262 |  | 
|---|
| 263 | It is normal to put certain sections in more than one segment.  This | 
|---|
| 264 | merely implies that one segment of memory contains another.  You may | 
|---|
| 265 | repeat `:PHDR', using it once for each segment which should contain the | 
|---|
| 266 | section. | 
|---|
| 267 |  | 
|---|
| 268 | If you place a section in one or more segments using `:PHDR', then | 
|---|
| 269 | the linker will place all subsequent allocatable sections which do not | 
|---|
| 270 | specify `:PHDR' in the same segments.  This is for convenience, since | 
|---|
| 271 | generally a whole set of contiguous sections will be placed in a single | 
|---|
| 272 | segment.  You can use `:NONE' to override the default segment and tell | 
|---|
| 273 | the linker to not put the section in any segment at all. | 
|---|
| 274 |  | 
|---|
| 275 | You may use the `FILEHDR' and `PHDRS' keywords appear after the | 
|---|
| 276 | program header type to further describe the contents of the segment. | 
|---|
| 277 | The `FILEHDR' keyword means that the segment should include the ELF | 
|---|
| 278 | file header.  The `PHDRS' keyword means that the segment should include | 
|---|
| 279 | the ELF program headers themselves. | 
|---|
| 280 |  | 
|---|
| 281 | The TYPE may be one of the following.  The numbers indicate the | 
|---|
| 282 | value of the keyword. | 
|---|
| 283 |  | 
|---|
| 284 | `PT_NULL' (0) | 
|---|
| 285 | Indicates an unused program header. | 
|---|
| 286 |  | 
|---|
| 287 | `PT_LOAD' (1) | 
|---|
| 288 | Indicates that this program header describes a segment to be | 
|---|
| 289 | loaded from the file. | 
|---|
| 290 |  | 
|---|
| 291 | `PT_DYNAMIC' (2) | 
|---|
| 292 | Indicates a segment where dynamic linking information can be found. | 
|---|
| 293 |  | 
|---|
| 294 | `PT_INTERP' (3) | 
|---|
| 295 | Indicates a segment where the name of the program interpreter may | 
|---|
| 296 | be found. | 
|---|
| 297 |  | 
|---|
| 298 | `PT_NOTE' (4) | 
|---|
| 299 | Indicates a segment holding note information. | 
|---|
| 300 |  | 
|---|
| 301 | `PT_SHLIB' (5) | 
|---|
| 302 | A reserved program header type, defined but not specified by the | 
|---|
| 303 | ELF ABI. | 
|---|
| 304 |  | 
|---|
| 305 | `PT_PHDR' (6) | 
|---|
| 306 | Indicates a segment where the program headers may be found. | 
|---|
| 307 |  | 
|---|
| 308 | EXPRESSION | 
|---|
| 309 | An expression giving the numeric type of the program header.  This | 
|---|
| 310 | may be used for types not defined above. | 
|---|
| 311 |  | 
|---|
| 312 | You can specify that a segment should be loaded at a particular | 
|---|
| 313 | address in memory by using an `AT' expression.  This is identical to the | 
|---|
| 314 | `AT' command used as an output section attribute (*note Output Section | 
|---|
| 315 | LMA::).  The `AT' command for a program header overrides the output | 
|---|
| 316 | section attribute. | 
|---|
| 317 |  | 
|---|
| 318 | The linker will normally set the segment flags based on the sections | 
|---|
| 319 | which comprise the segment.  You may use the `FLAGS' keyword to | 
|---|
| 320 | explicitly specify the segment flags.  The value of FLAGS must be an | 
|---|
| 321 | integer.  It is used to set the `p_flags' field of the program header. | 
|---|
| 322 |  | 
|---|
| 323 | Here is an example of `PHDRS'.  This shows a typical set of program | 
|---|
| 324 | headers used on a native ELF system. | 
|---|
| 325 |  | 
|---|
| 326 | PHDRS | 
|---|
| 327 | { | 
|---|
| 328 | headers PT_PHDR PHDRS ; | 
|---|
| 329 | interp PT_INTERP ; | 
|---|
| 330 | text PT_LOAD FILEHDR PHDRS ; | 
|---|
| 331 | data PT_LOAD ; | 
|---|
| 332 | dynamic PT_DYNAMIC ; | 
|---|
| 333 | } | 
|---|
| 334 |  | 
|---|
| 335 | SECTIONS | 
|---|
| 336 | { | 
|---|
| 337 | . = SIZEOF_HEADERS; | 
|---|
| 338 | .interp : { *(.interp) } :text :interp | 
|---|
| 339 | .text : { *(.text) } :text | 
|---|
| 340 | .rodata : { *(.rodata) } /* defaults to :text */ | 
|---|
| 341 | ... | 
|---|
| 342 | . = . + 0x1000; /* move to a new page in memory */ | 
|---|
| 343 | .data : { *(.data) } :data | 
|---|
| 344 | .dynamic : { *(.dynamic) } :data :dynamic | 
|---|
| 345 | ... | 
|---|
| 346 | } | 
|---|
| 347 |  | 
|---|
| 348 |  | 
|---|
| 349 | File: ld.info,  Node: VERSION,  Next: Expressions,  Prev: PHDRS,  Up: Scripts | 
|---|
| 350 |  | 
|---|
| 351 | VERSION Command | 
|---|
| 352 | =============== | 
|---|
| 353 |  | 
|---|
| 354 | The linker supports symbol versions when using ELF.  Symbol versions | 
|---|
| 355 | are only useful when using shared libraries.  The dynamic linker can use | 
|---|
| 356 | symbol versions to select a specific version of a function when it runs | 
|---|
| 357 | a program that may have been linked against an earlier version of the | 
|---|
| 358 | shared library. | 
|---|
| 359 |  | 
|---|
| 360 | You can include a version script directly in the main linker script, | 
|---|
| 361 | or you can supply the version script as an implicit linker script.  You | 
|---|
| 362 | can also use the `--version-script' linker option. | 
|---|
| 363 |  | 
|---|
| 364 | The syntax of the `VERSION' command is simply | 
|---|
| 365 | VERSION { version-script-commands } | 
|---|
| 366 |  | 
|---|
| 367 | The format of the version script commands is identical to that used | 
|---|
| 368 | by Sun's linker in Solaris 2.5.  The version script defines a tree of | 
|---|
| 369 | version nodes.  You specify the node names and interdependencies in the | 
|---|
| 370 | version script.  You can specify which symbols are bound to which | 
|---|
| 371 | version nodes, and you can reduce a specified set of symbols to local | 
|---|
| 372 | scope so that they are not globally visible outside of the shared | 
|---|
| 373 | library. | 
|---|
| 374 |  | 
|---|
| 375 | The easiest way to demonstrate the version script language is with a | 
|---|
| 376 | few examples. | 
|---|
| 377 |  | 
|---|
| 378 | VERS_1.1 { | 
|---|
| 379 | global: | 
|---|
| 380 | foo1; | 
|---|
| 381 | local: | 
|---|
| 382 | old*; | 
|---|
| 383 | original*; | 
|---|
| 384 | new*; | 
|---|
| 385 | }; | 
|---|
| 386 |  | 
|---|
| 387 | VERS_1.2 { | 
|---|
| 388 | foo2; | 
|---|
| 389 | } VERS_1.1; | 
|---|
| 390 |  | 
|---|
| 391 | VERS_2.0 { | 
|---|
| 392 | bar1; bar2; | 
|---|
| 393 | } VERS_1.2; | 
|---|
| 394 |  | 
|---|
| 395 | This example version script defines three version nodes.  The first | 
|---|
| 396 | version node defined is `VERS_1.1'; it has no other dependencies.  The | 
|---|
| 397 | script binds the symbol `foo1' to `VERS_1.1'.  It reduces a number of | 
|---|
| 398 | symbols to local scope so that they are not visible outside of the | 
|---|
| 399 | shared library; this is done using wildcard patterns, so that any | 
|---|
| 400 | symbol whose name begins with `old', `original', or `new' is matched. | 
|---|
| 401 | The wildcard patterns available are the same as those used in the shell | 
|---|
| 402 | when matching filenames (also known as "globbing"). | 
|---|
| 403 |  | 
|---|
| 404 | Next, the version script defines node `VERS_1.2'.  This node depends | 
|---|
| 405 | upon `VERS_1.1'.  The script binds the symbol `foo2' to the version | 
|---|
| 406 | node `VERS_1.2'. | 
|---|
| 407 |  | 
|---|
| 408 | Finally, the version script defines node `VERS_2.0'.  This node | 
|---|
| 409 | depends upon `VERS_1.2'.  The scripts binds the symbols `bar1' and | 
|---|
| 410 | `bar2' are bound to the version node `VERS_2.0'. | 
|---|
| 411 |  | 
|---|
| 412 | When the linker finds a symbol defined in a library which is not | 
|---|
| 413 | specifically bound to a version node, it will effectively bind it to an | 
|---|
| 414 | unspecified base version of the library.  You can bind all otherwise | 
|---|
| 415 | unspecified symbols to a given version node by using `global: *;' | 
|---|
| 416 | somewhere in the version script. | 
|---|
| 417 |  | 
|---|
| 418 | The names of the version nodes have no specific meaning other than | 
|---|
| 419 | what they might suggest to the person reading them.  The `2.0' version | 
|---|
| 420 | could just as well have appeared in between `1.1' and `1.2'.  However, | 
|---|
| 421 | this would be a confusing way to write a version script. | 
|---|
| 422 |  | 
|---|
| 423 | Node name can be omited, provided it is the only version node in the | 
|---|
| 424 | version script.  Such version script doesn't assign any versions to | 
|---|
| 425 | symbols, only selects which symbols will be globally visible out and | 
|---|
| 426 | which won't. | 
|---|
| 427 |  | 
|---|
| 428 | { global: foo; bar; local: *; }; | 
|---|
| 429 |  | 
|---|
| 430 | When you link an application against a shared library that has | 
|---|
| 431 | versioned symbols, the application itself knows which version of each | 
|---|
| 432 | symbol it requires, and it also knows which version nodes it needs from | 
|---|
| 433 | each shared library it is linked against.  Thus at runtime, the dynamic | 
|---|
| 434 | loader can make a quick check to make sure that the libraries you have | 
|---|
| 435 | linked against do in fact supply all of the version nodes that the | 
|---|
| 436 | application will need to resolve all of the dynamic symbols.  In this | 
|---|
| 437 | way it is possible for the dynamic linker to know with certainty that | 
|---|
| 438 | all external symbols that it needs will be resolvable without having to | 
|---|
| 439 | search for each symbol reference. | 
|---|
| 440 |  | 
|---|
| 441 | The symbol versioning is in effect a much more sophisticated way of | 
|---|
| 442 | doing minor version checking that SunOS does.  The fundamental problem | 
|---|
| 443 | that is being addressed here is that typically references to external | 
|---|
| 444 | functions are bound on an as-needed basis, and are not all bound when | 
|---|
| 445 | the application starts up.  If a shared library is out of date, a | 
|---|
| 446 | required interface may be missing; when the application tries to use | 
|---|
| 447 | that interface, it may suddenly and unexpectedly fail.  With symbol | 
|---|
| 448 | versioning, the user will get a warning when they start their program if | 
|---|
| 449 | the libraries being used with the application are too old. | 
|---|
| 450 |  | 
|---|
| 451 | There are several GNU extensions to Sun's versioning approach.  The | 
|---|
| 452 | first of these is the ability to bind a symbol to a version node in the | 
|---|
| 453 | source file where the symbol is defined instead of in the versioning | 
|---|
| 454 | script.  This was done mainly to reduce the burden on the library | 
|---|
| 455 | maintainer.  You can do this by putting something like: | 
|---|
| 456 | __asm__(".symver original_foo,foo@VERS_1.1"); | 
|---|
| 457 |  | 
|---|
| 458 | in the C source file.  This renames the function `original_foo' to be | 
|---|
| 459 | an alias for `foo' bound to the version node `VERS_1.1'.  The `local:' | 
|---|
| 460 | directive can be used to prevent the symbol `original_foo' from being | 
|---|
| 461 | exported. A `.symver' directive takes precedence over a version script. | 
|---|
| 462 |  | 
|---|
| 463 | The second GNU extension is to allow multiple versions of the same | 
|---|
| 464 | function to appear in a given shared library.  In this way you can make | 
|---|
| 465 | an incompatible change to an interface without increasing the major | 
|---|
| 466 | version number of the shared library, while still allowing applications | 
|---|
| 467 | linked against the old interface to continue to function. | 
|---|
| 468 |  | 
|---|
| 469 | To do this, you must use multiple `.symver' directives in the source | 
|---|
| 470 | file.  Here is an example: | 
|---|
| 471 |  | 
|---|
| 472 | __asm__(".symver original_foo,foo@"); | 
|---|
| 473 | __asm__(".symver old_foo,foo@VERS_1.1"); | 
|---|
| 474 | __asm__(".symver old_foo1,foo@VERS_1.2"); | 
|---|
| 475 | __asm__(".symver new_foo,foo@@VERS_2.0"); | 
|---|
| 476 |  | 
|---|
| 477 | In this example, `foo@' represents the symbol `foo' bound to the | 
|---|
| 478 | unspecified base version of the symbol.  The source file that contains | 
|---|
| 479 | this example would define 4 C functions: `original_foo', `old_foo', | 
|---|
| 480 | `old_foo1', and `new_foo'. | 
|---|
| 481 |  | 
|---|
| 482 | When you have multiple definitions of a given symbol, there needs to | 
|---|
| 483 | be some way to specify a default version to which external references to | 
|---|
| 484 | this symbol will be bound.  You can do this with the `foo@@VERS_2.0' | 
|---|
| 485 | type of `.symver' directive.  You can only declare one version of a | 
|---|
| 486 | symbol as the default in this manner; otherwise you would effectively | 
|---|
| 487 | have multiple definitions of the same symbol. | 
|---|
| 488 |  | 
|---|
| 489 | If you wish to bind a reference to a specific version of the symbol | 
|---|
| 490 | within the shared library, you can use the aliases of convenience | 
|---|
| 491 | (i.e., `old_foo'), or you can use the `.symver' directive to | 
|---|
| 492 | specifically bind to an external version of the function in question. | 
|---|
| 493 |  | 
|---|
| 494 | You can also specify the language in the version script: | 
|---|
| 495 |  | 
|---|
| 496 | VERSION extern "lang" { version-script-commands } | 
|---|
| 497 |  | 
|---|
| 498 | The supported `lang's are `C', `C++', and `Java'.  The linker will | 
|---|
| 499 | iterate over the list of symbols at the link time and demangle them | 
|---|
| 500 | according to `lang' before matching them to the patterns specified in | 
|---|
| 501 | `version-script-commands'. | 
|---|
| 502 |  | 
|---|
| 503 |  | 
|---|
| 504 | File: ld.info,  Node: Expressions,  Next: Implicit Linker Scripts,  Prev: VERSION,  Up: Scripts | 
|---|
| 505 |  | 
|---|
| 506 | Expressions in Linker Scripts | 
|---|
| 507 | ============================= | 
|---|
| 508 |  | 
|---|
| 509 | The syntax for expressions in the linker script language is | 
|---|
| 510 | identical to that of C expressions.  All expressions are evaluated as | 
|---|
| 511 | integers.  All expressions are evaluated in the same size, which is 32 | 
|---|
| 512 | bits if both the host and target are 32 bits, and is otherwise 64 bits. | 
|---|
| 513 |  | 
|---|
| 514 | You can use and set symbol values in expressions. | 
|---|
| 515 |  | 
|---|
| 516 | The linker defines several special purpose builtin functions for use | 
|---|
| 517 | in expressions. | 
|---|
| 518 |  | 
|---|
| 519 | * Menu: | 
|---|
| 520 |  | 
|---|
| 521 | * Constants::                   Constants | 
|---|
| 522 | * Symbols::                     Symbol Names | 
|---|
| 523 | * Location Counter::            The Location Counter | 
|---|
| 524 | * Operators::                   Operators | 
|---|
| 525 | * Evaluation::                  Evaluation | 
|---|
| 526 | * Expression Section::          The Section of an Expression | 
|---|
| 527 | * Builtin Functions::           Builtin Functions | 
|---|
| 528 |  | 
|---|
| 529 |  | 
|---|
| 530 | File: ld.info,  Node: Constants,  Next: Symbols,  Up: Expressions | 
|---|
| 531 |  | 
|---|
| 532 | Constants | 
|---|
| 533 | --------- | 
|---|
| 534 |  | 
|---|
| 535 | All constants are integers. | 
|---|
| 536 |  | 
|---|
| 537 | As in C, the linker considers an integer beginning with `0' to be | 
|---|
| 538 | octal, and an integer beginning with `0x' or `0X' to be hexadecimal. | 
|---|
| 539 | The linker considers other integers to be decimal. | 
|---|
| 540 |  | 
|---|
| 541 | In addition, you can use the suffixes `K' and `M' to scale a | 
|---|
| 542 | constant by `1024' or `1024*1024' respectively. For example, the | 
|---|
| 543 | following all refer to the same quantity: | 
|---|
| 544 | _fourk_1 = 4K; | 
|---|
| 545 | _fourk_2 = 4096; | 
|---|
| 546 | _fourk_3 = 0x1000; | 
|---|
| 547 |  | 
|---|
| 548 |  | 
|---|
| 549 | File: ld.info,  Node: Symbols,  Next: Location Counter,  Prev: Constants,  Up: Expressions | 
|---|
| 550 |  | 
|---|
| 551 | Symbol Names | 
|---|
| 552 | ------------ | 
|---|
| 553 |  | 
|---|
| 554 | Unless quoted, symbol names start with a letter, underscore, or | 
|---|
| 555 | period and may include letters, digits, underscores, periods, and | 
|---|
| 556 | hyphens.  Unquoted symbol names must not conflict with any keywords. | 
|---|
| 557 | You can specify a symbol which contains odd characters or has the same | 
|---|
| 558 | name as a keyword by surrounding the symbol name in double quotes: | 
|---|
| 559 | "SECTION" = 9; | 
|---|
| 560 | "with a space" = "also with a space" + 10; | 
|---|
| 561 |  | 
|---|
| 562 | Since symbols can contain many non-alphabetic characters, it is | 
|---|
| 563 | safest to delimit symbols with spaces.  For example, `A-B' is one | 
|---|
| 564 | symbol, whereas `A - B' is an expression involving subtraction. | 
|---|
| 565 |  | 
|---|
| 566 |  | 
|---|
| 567 | File: ld.info,  Node: Location Counter,  Next: Operators,  Prev: Symbols,  Up: Expressions | 
|---|
| 568 |  | 
|---|
| 569 | The Location Counter | 
|---|
| 570 | -------------------- | 
|---|
| 571 |  | 
|---|
| 572 | The special linker variable "dot" `.' always contains the current | 
|---|
| 573 | output location counter.  Since the `.' always refers to a location in | 
|---|
| 574 | an output section, it may only appear in an expression within a | 
|---|
| 575 | `SECTIONS' command.  The `.' symbol may appear anywhere that an | 
|---|
| 576 | ordinary symbol is allowed in an expression. | 
|---|
| 577 |  | 
|---|
| 578 | Assigning a value to `.' will cause the location counter to be | 
|---|
| 579 | moved.  This may be used to create holes in the output section.  The | 
|---|
| 580 | location counter may never be moved backwards. | 
|---|
| 581 |  | 
|---|
| 582 | SECTIONS | 
|---|
| 583 | { | 
|---|
| 584 | output : | 
|---|
| 585 | { | 
|---|
| 586 | file1(.text) | 
|---|
| 587 | . = . + 1000; | 
|---|
| 588 | file2(.text) | 
|---|
| 589 | . += 1000; | 
|---|
| 590 | file3(.text) | 
|---|
| 591 | } = 0x12345678; | 
|---|
| 592 | } | 
|---|
| 593 |  | 
|---|
| 594 | In the previous example, the `.text' section from `file1' is located at | 
|---|
| 595 | the beginning of the output section `output'.  It is followed by a 1000 | 
|---|
| 596 | byte gap.  Then the `.text' section from `file2' appears, also with a | 
|---|
| 597 | 1000 byte gap following before the `.text' section from `file3'.  The | 
|---|
| 598 | notation `= 0x12345678' specifies what data to write in the gaps (*note | 
|---|
| 599 | Output Section Fill::). | 
|---|
| 600 |  | 
|---|
| 601 | Note: `.' actually refers to the byte offset from the start of the | 
|---|
| 602 | current containing object.  Normally this is the `SECTIONS' statement, | 
|---|
| 603 | whose start address is 0, hence `.' can be used as an absolute address. | 
|---|
| 604 | If `.' is used inside a section description however, it refers to the | 
|---|
| 605 | byte offset from the start of that section, not an absolute address. | 
|---|
| 606 | Thus in a script like this: | 
|---|
| 607 |  | 
|---|
| 608 | SECTIONS | 
|---|
| 609 | { | 
|---|
| 610 | . = 0x100 | 
|---|
| 611 | .text: { | 
|---|
| 612 | *(.text) | 
|---|
| 613 | . = 0x200 | 
|---|
| 614 | } | 
|---|
| 615 | . = 0x500 | 
|---|
| 616 | .data: { | 
|---|
| 617 | *(.data) | 
|---|
| 618 | . += 0x600 | 
|---|
| 619 | } | 
|---|
| 620 | } | 
|---|
| 621 |  | 
|---|
| 622 | The `.text' section will be assigned a starting address of 0x100 and | 
|---|
| 623 | a size of exactly 0x200 bytes, even if there is not enough data in the | 
|---|
| 624 | `.text' input sections to fill this area.  (If there is too much data, | 
|---|
| 625 | an error will be produced because this would be an attempt to move `.' | 
|---|
| 626 | backwards).  The `.data' section will start at 0x500 and it will have | 
|---|
| 627 | an extra 0x600 bytes worth of space after the end of the values from | 
|---|
| 628 | the `.data' input sections and before the end of the `.data' output | 
|---|
| 629 | section itself. | 
|---|
| 630 |  | 
|---|
| 631 |  | 
|---|
| 632 | File: ld.info,  Node: Operators,  Next: Evaluation,  Prev: Location Counter,  Up: Expressions | 
|---|
| 633 |  | 
|---|
| 634 | Operators | 
|---|
| 635 | --------- | 
|---|
| 636 |  | 
|---|
| 637 | The linker recognizes the standard C set of arithmetic operators, | 
|---|
| 638 | with the standard bindings and precedence levels: | 
|---|
| 639 | precedence      associativity   Operators                Notes | 
|---|
| 640 | (highest) | 
|---|
| 641 | 1               left            !  -  ~                  (1) | 
|---|
| 642 | 2               left            *  /  % | 
|---|
| 643 | 3               left            +  - | 
|---|
| 644 | 4               left            >>  << | 
|---|
| 645 | 5               left            ==  !=  >  <  <=  >= | 
|---|
| 646 | 6               left            & | 
|---|
| 647 | 7               left            | | 
|---|
| 648 | 8               left            && | 
|---|
| 649 | 9               left            || | 
|---|
| 650 | 10              right           ? : | 
|---|
| 651 | 11              right           &=  +=  -=  *=  /=       (2) | 
|---|
| 652 | (lowest) | 
|---|
| 653 | Notes: (1) Prefix operators (2) *Note Assignments::. | 
|---|
| 654 |  | 
|---|
| 655 |  | 
|---|
| 656 | File: ld.info,  Node: Evaluation,  Next: Expression Section,  Prev: Operators,  Up: Expressions | 
|---|
| 657 |  | 
|---|
| 658 | Evaluation | 
|---|
| 659 | ---------- | 
|---|
| 660 |  | 
|---|
| 661 | The linker evaluates expressions lazily.  It only computes the value | 
|---|
| 662 | of an expression when absolutely necessary. | 
|---|
| 663 |  | 
|---|
| 664 | The linker needs some information, such as the value of the start | 
|---|
| 665 | address of the first section, and the origins and lengths of memory | 
|---|
| 666 | regions, in order to do any linking at all.  These values are computed | 
|---|
| 667 | as soon as possible when the linker reads in the linker script. | 
|---|
| 668 |  | 
|---|
| 669 | However, other values (such as symbol values) are not known or needed | 
|---|
| 670 | until after storage allocation.  Such values are evaluated later, when | 
|---|
| 671 | other information (such as the sizes of output sections) is available | 
|---|
| 672 | for use in the symbol assignment expression. | 
|---|
| 673 |  | 
|---|
| 674 | The sizes of sections cannot be known until after allocation, so | 
|---|
| 675 | assignments dependent upon these are not performed until after | 
|---|
| 676 | allocation. | 
|---|
| 677 |  | 
|---|
| 678 | Some expressions, such as those depending upon the location counter | 
|---|
| 679 | `.', must be evaluated during section allocation. | 
|---|
| 680 |  | 
|---|
| 681 | If the result of an expression is required, but the value is not | 
|---|
| 682 | available, then an error results.  For example, a script like the | 
|---|
| 683 | following | 
|---|
| 684 | SECTIONS | 
|---|
| 685 | { | 
|---|
| 686 | .text 9+this_isnt_constant : | 
|---|
| 687 | { *(.text) } | 
|---|
| 688 | } | 
|---|
| 689 |  | 
|---|
| 690 | will cause the error message `non constant expression for initial | 
|---|
| 691 | address'. | 
|---|
| 692 |  | 
|---|
| 693 |  | 
|---|
| 694 | File: ld.info,  Node: Expression Section,  Next: Builtin Functions,  Prev: Evaluation,  Up: Expressions | 
|---|
| 695 |  | 
|---|
| 696 | The Section of an Expression | 
|---|
| 697 | ---------------------------- | 
|---|
| 698 |  | 
|---|
| 699 | When the linker evaluates an expression, the result is either | 
|---|
| 700 | absolute or relative to some section.  A relative expression is | 
|---|
| 701 | expressed as a fixed offset from the base of a section. | 
|---|
| 702 |  | 
|---|
| 703 | The position of the expression within the linker script determines | 
|---|
| 704 | whether it is absolute or relative.  An expression which appears within | 
|---|
| 705 | an output section definition is relative to the base of the output | 
|---|
| 706 | section.  An expression which appears elsewhere will be absolute. | 
|---|
| 707 |  | 
|---|
| 708 | A symbol set to a relative expression will be relocatable if you | 
|---|
| 709 | request relocatable output using the `-r' option.  That means that a | 
|---|
| 710 | further link operation may change the value of the symbol.  The symbol's | 
|---|
| 711 | section will be the section of the relative expression. | 
|---|
| 712 |  | 
|---|
| 713 | A symbol set to an absolute expression will retain the same value | 
|---|
| 714 | through any further link operation.  The symbol will be absolute, and | 
|---|
| 715 | will not have any particular associated section. | 
|---|
| 716 |  | 
|---|
| 717 | You can use the builtin function `ABSOLUTE' to force an expression | 
|---|
| 718 | to be absolute when it would otherwise be relative.  For example, to | 
|---|
| 719 | create an absolute symbol set to the address of the end of the output | 
|---|
| 720 | section `.data': | 
|---|
| 721 | SECTIONS | 
|---|
| 722 | { | 
|---|
| 723 | .data : { *(.data) _edata = ABSOLUTE(.); } | 
|---|
| 724 | } | 
|---|
| 725 |  | 
|---|
| 726 | If `ABSOLUTE' were not used, `_edata' would be relative to the `.data' | 
|---|
| 727 | section. | 
|---|
| 728 |  | 
|---|
| 729 |  | 
|---|
| 730 | File: ld.info,  Node: Builtin Functions,  Prev: Expression Section,  Up: Expressions | 
|---|
| 731 |  | 
|---|
| 732 | Builtin Functions | 
|---|
| 733 | ----------------- | 
|---|
| 734 |  | 
|---|
| 735 | The linker script language includes a number of builtin functions for | 
|---|
| 736 | use in linker script expressions. | 
|---|
| 737 |  | 
|---|
| 738 | `ABSOLUTE(EXP)' | 
|---|
| 739 | Return the absolute (non-relocatable, as opposed to non-negative) | 
|---|
| 740 | value of the expression EXP.  Primarily useful to assign an | 
|---|
| 741 | absolute value to a symbol within a section definition, where | 
|---|
| 742 | symbol values are normally section relative.  *Note Expression | 
|---|
| 743 | Section::. | 
|---|
| 744 |  | 
|---|
| 745 | `ADDR(SECTION)' | 
|---|
| 746 | Return the absolute address (the VMA) of the named SECTION.  Your | 
|---|
| 747 | script must previously have defined the location of that section. | 
|---|
| 748 | In the following example, `symbol_1' and `symbol_2' are assigned | 
|---|
| 749 | identical values: | 
|---|
| 750 | SECTIONS { ... | 
|---|
| 751 | .output1 : | 
|---|
| 752 | { | 
|---|
| 753 | start_of_output_1 = ABSOLUTE(.); | 
|---|
| 754 | ... | 
|---|
| 755 | } | 
|---|
| 756 | .output : | 
|---|
| 757 | { | 
|---|
| 758 | symbol_1 = ADDR(.output1); | 
|---|
| 759 | symbol_2 = start_of_output_1; | 
|---|
| 760 | } | 
|---|
| 761 | ... } | 
|---|
| 762 |  | 
|---|
| 763 | `ALIGN(EXP)' | 
|---|
| 764 | Return the location counter (`.') aligned to the next EXP boundary. | 
|---|
| 765 | `ALIGN' doesn't change the value of the location counter--it just | 
|---|
| 766 | does arithmetic on it.  Here is an example which aligns the output | 
|---|
| 767 | `.data' section to the next `0x2000' byte boundary after the | 
|---|
| 768 | preceding section and sets a variable within the section to the | 
|---|
| 769 | next `0x8000' boundary after the input sections: | 
|---|
| 770 | SECTIONS { ... | 
|---|
| 771 | .data ALIGN(0x2000): { | 
|---|
| 772 | *(.data) | 
|---|
| 773 | variable = ALIGN(0x8000); | 
|---|
| 774 | } | 
|---|
| 775 | ... } | 
|---|
| 776 |  | 
|---|
| 777 | The first use of `ALIGN' in this example specifies the location of | 
|---|
| 778 | a section because it is used as the optional ADDRESS attribute of | 
|---|
| 779 | a section definition (*note Output Section Address::).  The second | 
|---|
| 780 | use of `ALIGN' is used to defines the value of a symbol. | 
|---|
| 781 |  | 
|---|
| 782 | The builtin function `NEXT' is closely related to `ALIGN'. | 
|---|
| 783 |  | 
|---|
| 784 | `BLOCK(EXP)' | 
|---|
| 785 | This is a synonym for `ALIGN', for compatibility with older linker | 
|---|
| 786 | scripts.  It is most often seen when setting the address of an | 
|---|
| 787 | output section. | 
|---|
| 788 |  | 
|---|
| 789 | `DATA_SEGMENT_ALIGN(MAXPAGESIZE, COMMONPAGESIZE)' | 
|---|
| 790 | This is equivalent to either | 
|---|
| 791 | (ALIGN(MAXPAGESIZE) + (. & (MAXPAGESIZE - 1))) | 
|---|
| 792 | or | 
|---|
| 793 | (ALIGN(MAXPAGESIZE) + (. & (MAXPAGESIZE - COMMONPAGESIZE))) | 
|---|
| 794 |  | 
|---|
| 795 | depending on whether the latter uses fewer COMMONPAGESIZE sized | 
|---|
| 796 | pages for the data segment (area between the result of this | 
|---|
| 797 | expression and `DATA_SEGMENT_END') than the former or not.  If the | 
|---|
| 798 | latter form is used, it means COMMONPAGESIZE bytes of runtime | 
|---|
| 799 | memory will be saved at the expense of up to COMMONPAGESIZE wasted | 
|---|
| 800 | bytes in the on-disk file. | 
|---|
| 801 |  | 
|---|
| 802 | This expression can only be used directly in `SECTIONS' commands, | 
|---|
| 803 | not in any output section descriptions and only once in the linker | 
|---|
| 804 | script.  COMMONPAGESIZE should be less or equal to MAXPAGESIZE and | 
|---|
| 805 | should be the system page size the object wants to be optimized | 
|---|
| 806 | for (while still working on system page sizes up to MAXPAGESIZE). | 
|---|
| 807 |  | 
|---|
| 808 | Example: | 
|---|
| 809 | . = DATA_SEGMENT_ALIGN(0x10000, 0x2000); | 
|---|
| 810 |  | 
|---|
| 811 | `DATA_SEGMENT_END(EXP)' | 
|---|
| 812 | This defines the end of data segment for `DATA_SEGMENT_ALIGN' | 
|---|
| 813 | evaluation purposes. | 
|---|
| 814 |  | 
|---|
| 815 | . = DATA_SEGMENT_END(.); | 
|---|
| 816 |  | 
|---|
| 817 | `DEFINED(SYMBOL)' | 
|---|
| 818 | Return 1 if SYMBOL is in the linker global symbol table and is | 
|---|
| 819 | defined, otherwise return 0.  You can use this function to provide | 
|---|
| 820 | default values for symbols.  For example, the following script | 
|---|
| 821 | fragment shows how to set a global symbol `begin' to the first | 
|---|
| 822 | location in the `.text' section--but if a symbol called `begin' | 
|---|
| 823 | already existed, its value is preserved: | 
|---|
| 824 |  | 
|---|
| 825 | SECTIONS { ... | 
|---|
| 826 | .text : { | 
|---|
| 827 | begin = DEFINED(begin) ? begin : . ; | 
|---|
| 828 | ... | 
|---|
| 829 | } | 
|---|
| 830 | ... | 
|---|
| 831 | } | 
|---|
| 832 |  | 
|---|
| 833 | `LOADADDR(SECTION)' | 
|---|
| 834 | Return the absolute LMA of the named SECTION.  This is normally | 
|---|
| 835 | the same as `ADDR', but it may be different if the `AT' attribute | 
|---|
| 836 | is used in the output section definition (*note Output Section | 
|---|
| 837 | LMA::). | 
|---|
| 838 |  | 
|---|
| 839 | `MAX(EXP1, EXP2)' | 
|---|
| 840 | Returns the maximum of EXP1 and EXP2. | 
|---|
| 841 |  | 
|---|
| 842 | `MIN(EXP1, EXP2)' | 
|---|
| 843 | Returns the minimum of EXP1 and EXP2. | 
|---|
| 844 |  | 
|---|
| 845 | `NEXT(EXP)' | 
|---|
| 846 | Return the next unallocated address that is a multiple of EXP. | 
|---|
| 847 | This function is closely related to `ALIGN(EXP)'; unless you use | 
|---|
| 848 | the `MEMORY' command to define discontinuous memory for the output | 
|---|
| 849 | file, the two functions are equivalent. | 
|---|
| 850 |  | 
|---|
| 851 | `SIZEOF(SECTION)' | 
|---|
| 852 | Return the size in bytes of the named SECTION, if that section has | 
|---|
| 853 | been allocated.  If the section has not been allocated when this is | 
|---|
| 854 | evaluated, the linker will report an error.  In the following | 
|---|
| 855 | example, `symbol_1' and `symbol_2' are assigned identical values: | 
|---|
| 856 | SECTIONS{ ... | 
|---|
| 857 | .output { | 
|---|
| 858 | .start = . ; | 
|---|
| 859 | ... | 
|---|
| 860 | .end = . ; | 
|---|
| 861 | } | 
|---|
| 862 | symbol_1 = .end - .start ; | 
|---|
| 863 | symbol_2 = SIZEOF(.output); | 
|---|
| 864 | ... } | 
|---|
| 865 |  | 
|---|
| 866 | `SIZEOF_HEADERS' | 
|---|
| 867 | `sizeof_headers' | 
|---|
| 868 | Return the size in bytes of the output file's headers.  This is | 
|---|
| 869 | information which appears at the start of the output file.  You | 
|---|
| 870 | can use this number when setting the start address of the first | 
|---|
| 871 | section, if you choose, to facilitate paging. | 
|---|
| 872 |  | 
|---|
| 873 | When producing an ELF output file, if the linker script uses the | 
|---|
| 874 | `SIZEOF_HEADERS' builtin function, the linker must compute the | 
|---|
| 875 | number of program headers before it has determined all the section | 
|---|
| 876 | addresses and sizes.  If the linker later discovers that it needs | 
|---|
| 877 | additional program headers, it will report an error `not enough | 
|---|
| 878 | room for program headers'.  To avoid this error, you must avoid | 
|---|
| 879 | using the `SIZEOF_HEADERS' function, or you must rework your linker | 
|---|
| 880 | script to avoid forcing the linker to use additional program | 
|---|
| 881 | headers, or you must define the program headers yourself using the | 
|---|
| 882 | `PHDRS' command (*note PHDRS::). | 
|---|
| 883 |  | 
|---|
| 884 |  | 
|---|
| 885 | File: ld.info,  Node: Implicit Linker Scripts,  Prev: Expressions,  Up: Scripts | 
|---|
| 886 |  | 
|---|
| 887 | Implicit Linker Scripts | 
|---|
| 888 | ======================= | 
|---|
| 889 |  | 
|---|
| 890 | If you specify a linker input file which the linker can not | 
|---|
| 891 | recognize as an object file or an archive file, it will try to read the | 
|---|
| 892 | file as a linker script.  If the file can not be parsed as a linker | 
|---|
| 893 | script, the linker will report an error. | 
|---|
| 894 |  | 
|---|
| 895 | An implicit linker script will not replace the default linker script. | 
|---|
| 896 |  | 
|---|
| 897 | Typically an implicit linker script would contain only symbol | 
|---|
| 898 | assignments, or the `INPUT', `GROUP', or `VERSION' commands. | 
|---|
| 899 |  | 
|---|
| 900 | Any input files read because of an implicit linker script will be | 
|---|
| 901 | read at the position in the command line where the implicit linker | 
|---|
| 902 | script was read.  This can affect archive searching. | 
|---|
| 903 |  | 
|---|
| 904 |  | 
|---|
| 905 | File: ld.info,  Node: Machine Dependent,  Next: BFD,  Prev: Scripts,  Up: Top | 
|---|
| 906 |  | 
|---|
| 907 | Machine Dependent Features | 
|---|
| 908 | ************************** | 
|---|
| 909 |  | 
|---|
| 910 | `ld' has additional features on some platforms; the following | 
|---|
| 911 | sections describe them.  Machines where `ld' has no additional | 
|---|
| 912 | functionality are not listed. | 
|---|
| 913 |  | 
|---|
| 914 | * Menu: | 
|---|
| 915 |  | 
|---|
| 916 |  | 
|---|
| 917 | * H8/300::                      `ld' and the H8/300 | 
|---|
| 918 |  | 
|---|
| 919 | * i960::                        `ld' and the Intel 960 family | 
|---|
| 920 |  | 
|---|
| 921 | * ARM::                         `ld' and the ARM family | 
|---|
| 922 |  | 
|---|
| 923 | * HPPA ELF32::                  `ld' and HPPA 32-bit ELF | 
|---|
| 924 |  | 
|---|
| 925 | * MMIX::                        `ld' and MMIX | 
|---|
| 926 |  | 
|---|
| 927 | * MSP430::                      `ld' and MSP430 | 
|---|
| 928 |  | 
|---|
| 929 | * TI COFF::                     `ld' and TI COFF | 
|---|
| 930 |  | 
|---|
| 931 | * WIN32::                       `ld' and WIN32 (cygwin/mingw) | 
|---|
| 932 |  | 
|---|
| 933 | * Xtensa::                      `ld' and Xtensa Processors | 
|---|
| 934 |  | 
|---|
| 935 |  | 
|---|
| 936 | File: ld.info,  Node: H8/300,  Next: i960,  Up: Machine Dependent | 
|---|
| 937 |  | 
|---|
| 938 | `ld' and the H8/300 | 
|---|
| 939 | =================== | 
|---|
| 940 |  | 
|---|
| 941 | For the H8/300, `ld' can perform these global optimizations when you | 
|---|
| 942 | specify the `--relax' command-line option. | 
|---|
| 943 |  | 
|---|
| 944 | _relaxing address modes_ | 
|---|
| 945 | `ld' finds all `jsr' and `jmp' instructions whose targets are | 
|---|
| 946 | within eight bits, and turns them into eight-bit program-counter | 
|---|
| 947 | relative `bsr' and `bra' instructions, respectively. | 
|---|
| 948 |  | 
|---|
| 949 | _synthesizing instructions_ | 
|---|
| 950 | `ld' finds all `mov.b' instructions which use the sixteen-bit | 
|---|
| 951 | absolute address form, but refer to the top page of memory, and | 
|---|
| 952 | changes them to use the eight-bit address form.  (That is: the | 
|---|
| 953 | linker turns `mov.b `@'AA:16' into `mov.b `@'AA:8' whenever the | 
|---|
| 954 | address AA is in the top page of memory). | 
|---|
| 955 |  | 
|---|
| 956 |  | 
|---|
| 957 | File: ld.info,  Node: i960,  Next: ARM,  Prev: H8/300,  Up: Machine Dependent | 
|---|
| 958 |  | 
|---|
| 959 | `ld' and the Intel 960 Family | 
|---|
| 960 | ============================= | 
|---|
| 961 |  | 
|---|
| 962 | You can use the `-AARCHITECTURE' command line option to specify one | 
|---|
| 963 | of the two-letter names identifying members of the 960 family; the | 
|---|
| 964 | option specifies the desired output target, and warns of any | 
|---|
| 965 | incompatible instructions in the input files.  It also modifies the | 
|---|
| 966 | linker's search strategy for archive libraries, to support the use of | 
|---|
| 967 | libraries specific to each particular architecture, by including in the | 
|---|
| 968 | search loop names suffixed with the string identifying the architecture. | 
|---|
| 969 |  | 
|---|
| 970 | For example, if your `ld' command line included `-ACA' as well as | 
|---|
| 971 | `-ltry', the linker would look (in its built-in search paths, and in | 
|---|
| 972 | any paths you specify with `-L') for a library with the names | 
|---|
| 973 |  | 
|---|
| 974 | try | 
|---|
| 975 | libtry.a | 
|---|
| 976 | tryca | 
|---|
| 977 | libtryca.a | 
|---|
| 978 |  | 
|---|
| 979 | The first two possibilities would be considered in any event; the last | 
|---|
| 980 | two are due to the use of `-ACA'. | 
|---|
| 981 |  | 
|---|
| 982 | You can meaningfully use `-A' more than once on a command line, since | 
|---|
| 983 | the 960 architecture family allows combination of target architectures; | 
|---|
| 984 | each use will add another pair of name variants to search for when `-l' | 
|---|
| 985 | specifies a library. | 
|---|
| 986 |  | 
|---|
| 987 | `ld' supports the `--relax' option for the i960 family.  If you | 
|---|
| 988 | specify `--relax', `ld' finds all `balx' and `calx' instructions whose | 
|---|
| 989 | targets are within 24 bits, and turns them into 24-bit program-counter | 
|---|
| 990 | relative `bal' and `cal' instructions, respectively.  `ld' also turns | 
|---|
| 991 | `cal' instructions into `bal' instructions when it determines that the | 
|---|
| 992 | target subroutine is a leaf routine (that is, the target subroutine does | 
|---|
| 993 | not itself call any subroutines). | 
|---|
| 994 |  | 
|---|
| 995 |  | 
|---|
| 996 | File: ld.info,  Node: ARM,  Next: HPPA ELF32,  Prev: i960,  Up: Machine Dependent | 
|---|
| 997 |  | 
|---|
| 998 | `ld''s Support for Interworking Between ARM and Thumb Code | 
|---|
| 999 | ========================================================== | 
|---|
| 1000 |  | 
|---|
| 1001 | For the ARM, `ld' will generate code stubs to allow functions calls | 
|---|
| 1002 | betweem ARM and Thumb code.  These stubs only work with code that has | 
|---|
| 1003 | been compiled and assembled with the `-mthumb-interwork' command line | 
|---|
| 1004 | option.  If it is necessary to link with old ARM object files or | 
|---|
| 1005 | libraries, which have not been compiled with the -mthumb-interwork | 
|---|
| 1006 | option then the `--support-old-code' command line switch should be | 
|---|
| 1007 | given to the linker.  This will make it generate larger stub functions | 
|---|
| 1008 | which will work with non-interworking aware ARM code.  Note, however, | 
|---|
| 1009 | the linker does not support generating stubs for function calls to | 
|---|
| 1010 | non-interworking aware Thumb code. | 
|---|
| 1011 |  | 
|---|
| 1012 | The `--thumb-entry' switch is a duplicate of the generic `--entry' | 
|---|
| 1013 | switch, in that it sets the program's starting address.  But it also | 
|---|
| 1014 | sets the bottom bit of the address, so that it can be branched to using | 
|---|
| 1015 | a BX instruction, and the program will start executing in Thumb mode | 
|---|
| 1016 | straight away. | 
|---|
| 1017 |  | 
|---|
| 1018 |  | 
|---|
| 1019 | File: ld.info,  Node: HPPA ELF32,  Next: MMIX,  Prev: ARM,  Up: Machine Dependent | 
|---|
| 1020 |  | 
|---|
| 1021 | `ld' and HPPA 32-bit ELF Support | 
|---|
| 1022 | ================================ | 
|---|
| 1023 |  | 
|---|
| 1024 | When generating a shared library, `ld' will by default generate | 
|---|
| 1025 | import stubs suitable for use with a single sub-space application.  The | 
|---|
| 1026 | `--multi-subspace' switch causes `ld' to generate export stubs, and | 
|---|
| 1027 | different (larger) import stubs suitable for use with multiple | 
|---|
| 1028 | sub-spaces. | 
|---|
| 1029 |  | 
|---|
| 1030 | Long branch stubs and import/export stubs are placed by `ld' in stub | 
|---|
| 1031 | sections located between groups of input sections.  `--stub-group-size' | 
|---|
| 1032 | specifies the maximum size of a group of input sections handled by one | 
|---|
| 1033 | stub section.  Since branch offsets are signed, a stub section may | 
|---|
| 1034 | serve two groups of input sections, one group before the stub section, | 
|---|
| 1035 | and one group after it.  However, when using conditional branches that | 
|---|
| 1036 | require stubs, it may be better (for branch prediction) that stub | 
|---|
| 1037 | sections only serve one group of input sections.  A negative value for | 
|---|
| 1038 | `N' chooses this scheme, ensuring that branches to stubs always use a | 
|---|
| 1039 | negative offset.  Two special values of `N' are recognized, `1' and | 
|---|
| 1040 | `-1'.  These both instruct `ld' to automatically size input section | 
|---|
| 1041 | groups for the branch types detected, with the same behaviour regarding | 
|---|
| 1042 | stub placement as other positive or negative values of `N' respectively. | 
|---|
| 1043 |  | 
|---|
| 1044 | Note that `--stub-group-size' does not split input sections.  A | 
|---|
| 1045 | single input section larger than the group size specified will of course | 
|---|
| 1046 | create a larger group (of one section).  If input sections are too | 
|---|
| 1047 | large, it may not be possible for a branch to reach its stub. | 
|---|
| 1048 |  | 
|---|
| 1049 |  | 
|---|
| 1050 | File: ld.info,  Node: MMIX,  Next: MSP430,  Prev: HPPA ELF32,  Up: Machine Dependent | 
|---|
| 1051 |  | 
|---|
| 1052 | `ld' and MMIX | 
|---|
| 1053 | ============= | 
|---|
| 1054 |  | 
|---|
| 1055 | For MMIX, there is a choice of generating `ELF' object files or | 
|---|
| 1056 | `mmo' object files when linking.  The simulator `mmix' understands the | 
|---|
| 1057 | `mmo' format.  The binutils `objcopy' utility can translate between the | 
|---|
| 1058 | two formats. | 
|---|
| 1059 |  | 
|---|
| 1060 | There is one special section, the `.MMIX.reg_contents' section. | 
|---|
| 1061 | Contents in this section is assumed to correspond to that of global | 
|---|
| 1062 | registers, and symbols referring to it are translated to special | 
|---|
| 1063 | symbols, equal to registers.  In a final link, the start address of the | 
|---|
| 1064 | `.MMIX.reg_contents' section corresponds to the first allocated global | 
|---|
| 1065 | register multiplied by 8.  Register `$255' is not included in this | 
|---|
| 1066 | section; it is always set to the program entry, which is at the symbol | 
|---|
| 1067 | `Main' for `mmo' files. | 
|---|
| 1068 |  | 
|---|
| 1069 | Symbols with the prefix `__.MMIX.start.', for example | 
|---|
| 1070 | `__.MMIX.start..text' and `__.MMIX.start..data' are special; there must | 
|---|
| 1071 | be only one each, even if they are local.  The default linker script | 
|---|
| 1072 | uses these to set the default start address of a section. | 
|---|
| 1073 |  | 
|---|
| 1074 | Initial and trailing multiples of zero-valued 32-bit words in a | 
|---|
| 1075 | section, are left out from an mmo file. | 
|---|
| 1076 |  | 
|---|
| 1077 |  | 
|---|
| 1078 | File: ld.info,  Node: MSP430,  Next: TI COFF,  Prev: MMIX,  Up: Machine Dependent | 
|---|
| 1079 |  | 
|---|
| 1080 | `ld' and MSP430 | 
|---|
| 1081 | =============== | 
|---|
| 1082 |  | 
|---|
| 1083 | For the MSP430 it is possible to select the MPU architecture.  The | 
|---|
| 1084 | flag `-m [mpu type]' will select an appropriate linker script for | 
|---|
| 1085 | selected MPU type.  (To get a list of known MPUs just pass `-m help' | 
|---|
| 1086 | option to the linker). | 
|---|
| 1087 |  | 
|---|
| 1088 | The linker will recognize some extra sections which are MSP430 | 
|---|
| 1089 | specific: | 
|---|
| 1090 |  | 
|---|
| 1091 | ``.vectors'' | 
|---|
| 1092 | Defines a portion of ROM where interrupt vectors located. | 
|---|
| 1093 |  | 
|---|
| 1094 | ``.bootloader'' | 
|---|
| 1095 | Defines the bootloader portion of the ROM (if applicable).  Any | 
|---|
| 1096 | code in this section will be uploaded to the MPU. | 
|---|
| 1097 |  | 
|---|
| 1098 | ``.infomem'' | 
|---|
| 1099 | Defines an information memory section (if applicable).  Any code in | 
|---|
| 1100 | this section will be uploaded to the MPU. | 
|---|
| 1101 |  | 
|---|
| 1102 | ``.infomemnobits'' | 
|---|
| 1103 | This is the same as the `.infomem' section except that any code in | 
|---|
| 1104 | this section will not be uploaded to the MPU. | 
|---|
| 1105 |  | 
|---|
| 1106 | ``.noinit'' | 
|---|
| 1107 | Denotes a portion of RAM located above `.bss' section. | 
|---|
| 1108 |  | 
|---|
| 1109 | The last two sections are used by gcc. | 
|---|
| 1110 |  | 
|---|
| 1111 |  | 
|---|
| 1112 | File: ld.info,  Node: TI COFF,  Next: WIN32,  Prev: MSP430,  Up: Machine Dependent | 
|---|
| 1113 |  | 
|---|
| 1114 | `ld''s Support for Various TI COFF Versions | 
|---|
| 1115 | =========================================== | 
|---|
| 1116 |  | 
|---|
| 1117 | The `--format' switch allows selection of one of the various TI COFF | 
|---|
| 1118 | versions.  The latest of this writing is 2; versions 0 and 1 are also | 
|---|
| 1119 | supported.  The TI COFF versions also vary in header byte-order format; | 
|---|
| 1120 | `ld' will read any version or byte order, but the output header format | 
|---|
| 1121 | depends on the default specified by the specific target. | 
|---|
| 1122 |  | 
|---|