source: trunk/src/binutils/ld/ld.info-4@ 201

Last change on this file since 201 was 10, checked in by bird, 22 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 49.1 KB
Line 
1This is ld.info, produced by makeinfo version 4.0 from ./ld.texinfo.
2
3START-INFO-DIR-ENTRY
4* Ld: (ld). The GNU linker.
5END-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
10Software Foundation, Inc.
11
12
13File: ld.info, Node: Overlay Description, Prev: Output Section Attributes, Up: SECTIONS
14
15Overlay description
16-------------------
17
18 An overlay description provides an easy way to describe sections
19which are to be loaded as part of a single memory image but are to be
20run at the same memory address. At run time, some sort of overlay
21manager will copy the overlaid sections in and out of the runtime
22memory address as required, perhaps by simply manipulating addressing
23bits. This approach can be useful, for example, when a certain region
24of memory is faster than another.
25
26 Overlays are described using the `OVERLAY' command. The `OVERLAY'
27command is used within a `SECTIONS' command, like an output section
28description. 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
47section must have a name (SECNAME1 and SECNAME2 above). The section
48definitions within the `OVERLAY' construct are identical to those
49within the general `SECTIONS' contruct (*note SECTIONS::), except that
50no addresses and no memory regions may be defined for sections within
51an `OVERLAY'.
52
53 The sections are all defined with the same starting address. The
54load addresses of the sections are arranged such that they are
55consecutive in memory starting at the load address used for the
56`OVERLAY' as a whole (as with normal section definitions, the load
57address is optional, and defaults to the start address; the start
58address is also optional, and defaults to the current value of the
59location counter).
60
61 If the `NOCROSSREFS' keyword is used, and there any references among
62the sections, the linker will report an error. Since the sections all
63run at the same address, it normally does not make sense for one
64section to refer directly to another. *Note NOCROSSREFS: Miscellaneous
65Commands.
66
67 For each section within the `OVERLAY', the linker automatically
68defines two symbols. The symbol `__load_start_SECNAME' is defined as
69the starting load address of the section. The symbol
70`__load_stop_SECNAME' is defined as the final load address of the
71section. Any characters within SECNAME which are not legal within C
72identifiers are removed. C (or assembler) code may use these symbols
73to move the overlaid sections around as necessary.
74
75 At the end of the overlay, the value of the location counter is set
76to the start address of the overlay plus the size of the largest
77section.
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
87This will define both `.text0' and `.text1' to start at address 0x1000.
88`.text0' will be loaded at address 0x4000, and `.text1' will be loaded
89immediately 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
94like 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
101everything it does can be done using the more basic commands. The above
102example 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
113File: ld.info, Node: MEMORY, Next: PHDRS, Prev: SECTIONS, Up: Scripts
114
115MEMORY command
116==============
117
118 The linker's default configuration permits allocation of all
119available memory. You can override this by using the `MEMORY' command.
120
121 The `MEMORY' command describes the location and size of blocks of
122memory in the target. You can use it to describe which memory regions
123may be used by the linker, and which memory regions it must avoid. You
124can then assign sections to particular memory regions. The linker will
125set section addresses based on the memory regions, and will warn about
126regions that become too full. The linker will not shuffle sections
127around to fit into the available regions.
128
129 A linker script may contain at most one use of the `MEMORY' command.
130However, you can define as many blocks of memory within it as you
131wish. 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.
139The region name has no meaning outside of the linker script. Region
140names are stored in a separate name space, and will not conflict with
141symbol names, file names, or section names. Each memory region must
142have a distinct name.
143
144 The ATTR string is an optional list of attributes that specify
145whether to use a particular memory region for an input section which is
146not explicitly mapped in the linker script. As described in *Note
147SECTIONS::, if you do not specify an output section for some input
148section, the linker will create an output section with the same name as
149the input section. If you define region attributes, the linker will use
150them 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
176reverses this test, so that an unmapped section will be placed in the
177memory 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
180region. The expression must evaluate to a constant before memory
181allocation is performed, which means that you may not use any section
182relative 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.
186As with the ORIGIN expression, the expression must evaluate to a
187constant before memory allocation is performed. The keyword `LENGTH'
188may be abbreviated to `len' or `l'.
189
190 In the following example, we specify that there are two memory
191regions available for allocation: one starting at `0' for 256 kilobytes,
192and the other starting at `0x40000000' for four megabytes. The linker
193will place into the `rom' memory region every section which is not
194explicitly mapped into a memory region, and is either read-only or
195executable. The linker will place other sections which are not
196explicitly 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
205specific output sections into that memory region by using the `>REGION'
206output section attribute. For example, if you have a memory region
207named `mem', you would use `>mem' in the output section definition.
208*Note Output Section Region::. If no address was specified for the
209output section, the linker will set the address to the next available
210address within the memory region. If the combined output sections
211directed to a memory region are too large for the region, the linker
212will issue an error message.
213
214
215File: ld.info, Node: PHDRS, Next: VERSION, Prev: MEMORY, Up: Scripts
216
217PHDRS 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
222loaded into memory. You can print them out by using the `objdump'
223program with the `-p' option.
224
225 When you run an ELF program on a native ELF system, the system loader
226reads the program headers in order to figure out how to load the
227program. This will only work if the program headers are set correctly.
228This manual does not describe the details of how the system loader
229interprets program headers; for more information, see the ELF ABI.
230
231 The linker will create reasonable program headers by default.
232However, in some cases, you may need to specify the program headers more
233precisely. You may use the `PHDRS' command for this purpose. When the
234linker sees the `PHDRS' command in the linker script, it will not
235create any program headers other than the ones specified.
236
237 The linker only pays attention to the `PHDRS' command when
238generating an ELF output file. In other cases, the linker will simply
239ignore `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
251linker script. It is not put into the output file. Program header
252names are stored in a separate name space, and will not conflict with
253symbol names, file names, or section names. Each program header must
254have a distinct name.
255
256 Certain program header types describe segments of memory which the
257system loader will load from the file. In the linker script, you
258specify the contents of these segments by placing allocatable output
259sections in the segments. You use the `:PHDR' output section attribute
260to place a section in a particular segment. *Note Output Section
261Phdr::.
262
263 It is normal to put certain sections in more than one segment. This
264merely implies that one segment of memory contains another. You may
265repeat `:PHDR', using it once for each segment which should contain the
266section.
267
268 If you place a section in one or more segments using `:PHDR', then
269the linker will place all subsequent allocatable sections which do not
270specify `:PHDR' in the same segments. This is for convenience, since
271generally a whole set of contiguous sections will be placed in a single
272segment. You can use `:NONE' to override the default segment and tell
273the linker to not put the section in any segment at all.
274
275 You may use the `FILEHDR' and `PHDRS' keywords appear after the
276program header type to further describe the contents of the segment.
277The `FILEHDR' keyword means that the segment should include the ELF
278file header. The `PHDRS' keyword means that the segment should include
279the ELF program headers themselves.
280
281 The TYPE may be one of the following. The numbers indicate the
282value 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
308EXPRESSION
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
313address in memory by using an `AT' expression. This is identical to the
314`AT' command used as an output section attribute (*note Output Section
315LMA::). The `AT' command for a program header overrides the output
316section attribute.
317
318 The linker will normally set the segment flags based on the sections
319which comprise the segment. You may use the `FLAGS' keyword to
320explicitly specify the segment flags. The value of FLAGS must be an
321integer. 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
324headers 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
349File: ld.info, Node: VERSION, Next: Expressions, Prev: PHDRS, Up: Scripts
350
351VERSION Command
352===============
353
354 The linker supports symbol versions when using ELF. Symbol versions
355are only useful when using shared libraries. The dynamic linker can use
356symbol versions to select a specific version of a function when it runs
357a program that may have been linked against an earlier version of the
358shared library.
359
360 You can include a version script directly in the main linker script,
361or you can supply the version script as an implicit linker script. You
362can 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
368by Sun's linker in Solaris 2.5. The version script defines a tree of
369version nodes. You specify the node names and interdependencies in the
370version script. You can specify which symbols are bound to which
371version nodes, and you can reduce a specified set of symbols to local
372scope so that they are not globally visible outside of the shared
373library.
374
375 The easiest way to demonstrate the version script language is with a
376few 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
396version node defined is `VERS_1.1'; it has no other dependencies. The
397script binds the symbol `foo1' to `VERS_1.1'. It reduces a number of
398symbols to local scope so that they are not visible outside of the
399shared library.
400
401 Next, the version script defines node `VERS_1.2'. This node depends
402upon `VERS_1.1'. The script binds the symbol `foo2' to the version
403node `VERS_1.2'.
404
405 Finally, the version script defines node `VERS_2.0'. This node
406depends upon `VERS_1.2'. The scripts binds the symbols `bar1' and
407`bar2' are bound to the version node `VERS_2.0'.
408
409 When the linker finds a symbol defined in a library which is not
410specifically bound to a version node, it will effectively bind it to an
411unspecified base version of the library. You can bind all otherwise
412unspecified symbols to a given version node by using `global: *'
413somewhere in the version script.
414
415 The names of the version nodes have no specific meaning other than
416what they might suggest to the person reading them. The `2.0' version
417could just as well have appeared in between `1.1' and `1.2'. However,
418this would be a confusing way to write a version script.
419
420 When you link an application against a shared library that has
421versioned symbols, the application itself knows which version of each
422symbol it requires, and it also knows which version nodes it needs from
423each shared library it is linked against. Thus at runtime, the dynamic
424loader can make a quick check to make sure that the libraries you have
425linked against do in fact supply all of the version nodes that the
426application will need to resolve all of the dynamic symbols. In this
427way it is possible for the dynamic linker to know with certainty that
428all external symbols that it needs will be resolvable without having to
429search for each symbol reference.
430
431 The symbol versioning is in effect a much more sophisticated way of
432doing minor version checking that SunOS does. The fundamental problem
433that is being addressed here is that typically references to external
434functions are bound on an as-needed basis, and are not all bound when
435the application starts up. If a shared library is out of date, a
436required interface may be missing; when the application tries to use
437that interface, it may suddenly and unexpectedly fail. With symbol
438versioning, the user will get a warning when they start their program if
439the libraries being used with the application are too old.
440
441 There are several GNU extensions to Sun's versioning approach. The
442first of these is the ability to bind a symbol to a version node in the
443source file where the symbol is defined instead of in the versioning
444script. This was done mainly to reduce the burden on the library
445maintainer. You can do this by putting something like:
446 __asm__(".symver original_foo,foo@VERS_1.1");
447
448in the C source file. This renames the function `original_foo' to be
449an alias for `foo' bound to the version node `VERS_1.1'. The `local:'
450directive can be used to prevent the symbol `original_foo' from being
451exported.
452
453 The second GNU extension is to allow multiple versions of the same
454function to appear in a given shared library. In this way you can make
455an incompatible change to an interface without increasing the major
456version number of the shared library, while still allowing applications
457linked against the old interface to continue to function.
458
459 To do this, you must use multiple `.symver' directives in the source
460file. Here is an example:
461
462 __asm__(".symver original_foo,foo@");
463 __asm__(".symver old_foo,foo@VERS_1.1");
464 __asm__(".symver old_foo1,foo@VERS_1.2");
465 __asm__(".symver new_foo,foo@@VERS_2.0");
466
467 In this example, `foo@' represents the symbol `foo' bound to the
468unspecified base version of the symbol. The source file that contains
469this example would define 4 C functions: `original_foo', `old_foo',
470`old_foo1', and `new_foo'.
471
472 When you have multiple definitions of a given symbol, there needs to
473be some way to specify a default version to which external references to
474this symbol will be bound. You can do this with the `foo@@VERS_2.0'
475type of `.symver' directive. You can only declare one version of a
476symbol as the default in this manner; otherwise you would effectively
477have multiple definitions of the same symbol.
478
479 If you wish to bind a reference to a specific version of the symbol
480within the shared library, you can use the aliases of convenience (i.e.
481`old_foo'), or you can use the `.symver' directive to specifically bind
482to an external version of the function in question.
483
484
485File: ld.info, Node: Expressions, Next: Implicit Linker Scripts, Prev: VERSION, Up: Scripts
486
487Expressions in Linker Scripts
488=============================
489
490 The syntax for expressions in the linker script language is
491identical to that of C expressions. All expressions are evaluated as
492integers. All expressions are evaluated in the same size, which is 32
493bits if both the host and target are 32 bits, and is otherwise 64 bits.
494
495 You can use and set symbol values in expressions.
496
497 The linker defines several special purpose builtin functions for use
498in expressions.
499
500* Menu:
501
502* Constants:: Constants
503* Symbols:: Symbol Names
504* Location Counter:: The Location Counter
505* Operators:: Operators
506* Evaluation:: Evaluation
507* Expression Section:: The Section of an Expression
508* Builtin Functions:: Builtin Functions
509
510
511File: ld.info, Node: Constants, Next: Symbols, Up: Expressions
512
513Constants
514---------
515
516 All constants are integers.
517
518 As in C, the linker considers an integer beginning with `0' to be
519octal, and an integer beginning with `0x' or `0X' to be hexadecimal.
520The linker considers other integers to be decimal.
521
522 In addition, you can use the suffixes `K' and `M' to scale a
523constant by `1024' or `1024*1024' respectively. For example, the
524following all refer to the same quantity:
525 _fourk_1 = 4K;
526 _fourk_2 = 4096;
527 _fourk_3 = 0x1000;
528
529
530File: ld.info, Node: Symbols, Next: Location Counter, Prev: Constants, Up: Expressions
531
532Symbol Names
533------------
534
535 Unless quoted, symbol names start with a letter, underscore, or
536period and may include letters, digits, underscores, periods, and
537hyphens. Unquoted symbol names must not conflict with any keywords.
538You can specify a symbol which contains odd characters or has the same
539name as a keyword by surrounding the symbol name in double quotes:
540 "SECTION" = 9;
541 "with a space" = "also with a space" + 10;
542
543 Since symbols can contain many non-alphabetic characters, it is
544safest to delimit symbols with spaces. For example, `A-B' is one
545symbol, whereas `A - B' is an expression involving subtraction.
546
547
548File: ld.info, Node: Location Counter, Next: Operators, Prev: Symbols, Up: Expressions
549
550The Location Counter
551--------------------
552
553 The special linker variable "dot" `.' always contains the current
554output location counter. Since the `.' always refers to a location in
555an output section, it may only appear in an expression within a
556`SECTIONS' command. The `.' symbol may appear anywhere that an
557ordinary symbol is allowed in an expression.
558
559 Assigning a value to `.' will cause the location counter to be
560moved. This may be used to create holes in the output section. The
561location counter may never be moved backwards.
562
563 SECTIONS
564 {
565 output :
566 {
567 file1(.text)
568 . = . + 1000;
569 file2(.text)
570 . += 1000;
571 file3(.text)
572 } = 0x1234;
573 }
574
575In the previous example, the `.text' section from `file1' is located at
576the beginning of the output section `output'. It is followed by a 1000
577byte gap. Then the `.text' section from `file2' appears, also with a
5781000 byte gap following before the `.text' section from `file3'. The
579notation `= 0x1234' specifies what data to write in the gaps (*note
580Output Section Fill::).
581
582 Note: `.' actually refers to the byte offset from the start of the
583current containing object. Normally this is the `SECTIONS' statement,
584whoes start address is 0, hence `.' can be used as an absolute address.
585If `.' is used inside a section description however, it refers to the
586byte offset from the start of that section, not an absolute address.
587Thus in a script like this:
588
589 SECTIONS
590 {
591 . = 0x100
592 .text: {
593 *(.text)
594 . = 0x200
595 }
596 . = 0x500
597 .data: {
598 *(.data)
599 . += 0x600
600 }
601 }
602
603 The `.text' section will be assigned a starting address of 0x100 and
604a size of exactly 0x200 bytes, even if there is not enough data in the
605`.text' input sections to fill this area. (If there is too much data,
606an error will be produced because this would be an attempt to move `.'
607backwards). The `.data' section will start at 0x500 and it will have
608an extra 0x600 bytes worth of space after the end of the values from
609the `.data' input sections and before the end of the `.data' output
610section itself.
611
612
613File: ld.info, Node: Operators, Next: Evaluation, Prev: Location Counter, Up: Expressions
614
615Operators
616---------
617
618 The linker recognizes the standard C set of arithmetic operators,
619with the standard bindings and precedence levels:
620 precedence associativity Operators Notes
621 (highest)
622 1 left ! - ~ (1)
623 2 left * / %
624 3 left + -
625 4 left >> <<
626 5 left == != > < <= >=
627 6 left &
628 7 left |
629 8 left &&
630 9 left ||
631 10 right ? :
632 11 right &= += -= *= /= (2)
633 (lowest)
634 Notes: (1) Prefix operators (2) *Note Assignments::.
635
636
637File: ld.info, Node: Evaluation, Next: Expression Section, Prev: Operators, Up: Expressions
638
639Evaluation
640----------
641
642 The linker evaluates expressions lazily. It only computes the value
643of an expression when absolutely necessary.
644
645 The linker needs some information, such as the value of the start
646address of the first section, and the origins and lengths of memory
647regions, in order to do any linking at all. These values are computed
648as soon as possible when the linker reads in the linker script.
649
650 However, other values (such as symbol values) are not known or needed
651until after storage allocation. Such values are evaluated later, when
652other information (such as the sizes of output sections) is available
653for use in the symbol assignment expression.
654
655 The sizes of sections cannot be known until after allocation, so
656assignments dependent upon these are not performed until after
657allocation.
658
659 Some expressions, such as those depending upon the location counter
660`.', must be evaluated during section allocation.
661
662 If the result of an expression is required, but the value is not
663available, then an error results. For example, a script like the
664following
665 SECTIONS
666 {
667 .text 9+this_isnt_constant :
668 { *(.text) }
669 }
670
671will cause the error message `non constant expression for initial
672address'.
673
674
675File: ld.info, Node: Expression Section, Next: Builtin Functions, Prev: Evaluation, Up: Expressions
676
677The Section of an Expression
678----------------------------
679
680 When the linker evaluates an expression, the result is either
681absolute or relative to some section. A relative expression is
682expressed as a fixed offset from the base of a section.
683
684 The position of the expression within the linker script determines
685whether it is absolute or relative. An expression which appears within
686an output section definition is relative to the base of the output
687section. An expression which appears elsewhere will be absolute.
688
689 A symbol set to a relative expression will be relocatable if you
690request relocatable output using the `-r' option. That means that a
691further link operation may change the value of the symbol. The symbol's
692section will be the section of the relative expression.
693
694 A symbol set to an absolute expression will retain the same value
695through any further link operation. The symbol will be absolute, and
696will not have any particular associated section.
697
698 You can use the builtin function `ABSOLUTE' to force an expression
699to be absolute when it would otherwise be relative. For example, to
700create an absolute symbol set to the address of the end of the output
701section `.data':
702 SECTIONS
703 {
704 .data : { *(.data) _edata = ABSOLUTE(.); }
705 }
706
707If `ABSOLUTE' were not used, `_edata' would be relative to the `.data'
708section.
709
710
711File: ld.info, Node: Builtin Functions, Prev: Expression Section, Up: Expressions
712
713Builtin Functions
714-----------------
715
716 The linker script language includes a number of builtin functions for
717use in linker script expressions.
718
719`ABSOLUTE(EXP)'
720 Return the absolute (non-relocatable, as opposed to non-negative)
721 value of the expression EXP. Primarily useful to assign an
722 absolute value to a symbol within a section definition, where
723 symbol values are normally section relative. *Note Expression
724 Section::.
725
726`ADDR(SECTION)'
727 Return the absolute address (the VMA) of the named SECTION. Your
728 script must previously have defined the location of that section.
729 In the following example, `symbol_1' and `symbol_2' are assigned
730 identical values:
731 SECTIONS { ...
732 .output1 :
733 {
734 start_of_output_1 = ABSOLUTE(.);
735 ...
736 }
737 .output :
738 {
739 symbol_1 = ADDR(.output1);
740 symbol_2 = start_of_output_1;
741 }
742 ... }
743
744`ALIGN(EXP)'
745 Return the location counter (`.') aligned to the next EXP
746 boundary. EXP must be an expression whose value is a power of
747 two. This is equivalent to
748 (. + EXP - 1) & ~(EXP - 1)
749
750 `ALIGN' doesn't change the value of the location counter--it just
751 does arithmetic on it. Here is an example which aligns the output
752 `.data' section to the next `0x2000' byte boundary after the
753 preceding section and sets a variable within the section to the
754 next `0x8000' boundary after the input sections:
755 SECTIONS { ...
756 .data ALIGN(0x2000): {
757 *(.data)
758 variable = ALIGN(0x8000);
759 }
760 ... }
761
762 The first use of `ALIGN' in this example specifies the location of
763 a section because it is used as the optional ADDRESS attribute of
764 a section definition (*note Output Section Address::). The second
765 use of `ALIGN' is used to defines the value of a symbol.
766
767 The builtin function `NEXT' is closely related to `ALIGN'.
768
769`BLOCK(EXP)'
770 This is a synonym for `ALIGN', for compatibility with older linker
771 scripts. It is most often seen when setting the address of an
772 output section.
773
774`DEFINED(SYMBOL)'
775 Return 1 if SYMBOL is in the linker global symbol table and is
776 defined, otherwise return 0. You can use this function to provide
777 default values for symbols. For example, the following script
778 fragment shows how to set a global symbol `begin' to the first
779 location in the `.text' section--but if a symbol called `begin'
780 already existed, its value is preserved:
781
782 SECTIONS { ...
783 .text : {
784 begin = DEFINED(begin) ? begin : . ;
785 ...
786 }
787 ...
788 }
789
790`LOADADDR(SECTION)'
791 Return the absolute LMA of the named SECTION. This is normally
792 the same as `ADDR', but it may be different if the `AT' attribute
793 is used in the output section definition (*note Output Section
794 LMA::).
795
796`MAX(EXP1, EXP2)'
797 Returns the maximum of EXP1 and EXP2.
798
799`MIN(EXP1, EXP2)'
800 Returns the minimum of EXP1 and EXP2.
801
802`NEXT(EXP)'
803 Return the next unallocated address that is a multiple of EXP.
804 This function is closely related to `ALIGN(EXP)'; unless you use
805 the `MEMORY' command to define discontinuous memory for the output
806 file, the two functions are equivalent.
807
808`SIZEOF(SECTION)'
809 Return the size in bytes of the named SECTION, if that section has
810 been allocated. If the section has not been allocated when this is
811 evaluated, the linker will report an error. In the following
812 example, `symbol_1' and `symbol_2' are assigned identical values:
813 SECTIONS{ ...
814 .output {
815 .start = . ;
816 ...
817 .end = . ;
818 }
819 symbol_1 = .end - .start ;
820 symbol_2 = SIZEOF(.output);
821 ... }
822
823`SIZEOF_HEADERS'
824`sizeof_headers'
825 Return the size in bytes of the output file's headers. This is
826 information which appears at the start of the output file. You
827 can use this number when setting the start address of the first
828 section, if you choose, to facilitate paging.
829
830 When producing an ELF output file, if the linker script uses the
831 `SIZEOF_HEADERS' builtin function, the linker must compute the
832 number of program headers before it has determined all the section
833 addresses and sizes. If the linker later discovers that it needs
834 additional program headers, it will report an error `not enough
835 room for program headers'. To avoid this error, you must avoid
836 using the `SIZEOF_HEADERS' function, or you must rework your linker
837 script to avoid forcing the linker to use additional program
838 headers, or you must define the program headers yourself using the
839 `PHDRS' command (*note PHDRS::).
840
841
842File: ld.info, Node: Implicit Linker Scripts, Prev: Expressions, Up: Scripts
843
844Implicit Linker Scripts
845=======================
846
847 If you specify a linker input file which the linker can not
848recognize as an object file or an archive file, it will try to read the
849file as a linker script. If the file can not be parsed as a linker
850script, the linker will report an error.
851
852 An implicit linker script will not replace the default linker script.
853
854 Typically an implicit linker script would contain only symbol
855assignments, or the `INPUT', `GROUP', or `VERSION' commands.
856
857 Any input files read because of an implicit linker script will be
858read at the position in the command line where the implicit linker
859script was read. This can affect archive searching.
860
861
862File: ld.info, Node: Machine Dependent, Next: BFD, Prev: Scripts, Up: Top
863
864Machine Dependent Features
865**************************
866
867 `ld' has additional features on some platforms; the following
868sections describe them. Machines where `ld' has no additional
869functionality are not listed.
870
871* Menu:
872
873* H8/300:: `ld' and the H8/300
874* i960:: `ld' and the Intel 960 family
875* ARM:: `ld' and the ARM family
876* HPPA ELF32:: `ld' and HPPA 32-bit ELF
877
878* TI COFF:: `ld' and TI COFF
879
880
881File: ld.info, Node: H8/300, Next: i960, Up: Machine Dependent
882
883`ld' and the H8/300
884===================
885
886 For the H8/300, `ld' can perform these global optimizations when you
887specify the `--relax' command-line option.
888
889_relaxing address modes_
890 `ld' finds all `jsr' and `jmp' instructions whose targets are
891 within eight bits, and turns them into eight-bit program-counter
892 relative `bsr' and `bra' instructions, respectively.
893
894_synthesizing instructions_
895 `ld' finds all `mov.b' instructions which use the sixteen-bit
896 absolute address form, but refer to the top page of memory, and
897 changes them to use the eight-bit address form. (That is: the
898 linker turns `mov.b `@'AA:16' into `mov.b `@'AA:8' whenever the
899 address AA is in the top page of memory).
900
901
902File: ld.info, Node: i960, Next: ARM, Prev: H8/300, Up: Machine Dependent
903
904`ld' and the Intel 960 family
905=============================
906
907 You can use the `-AARCHITECTURE' command line option to specify one
908of the two-letter names identifying members of the 960 family; the
909option specifies the desired output target, and warns of any
910incompatible instructions in the input files. It also modifies the
911linker's search strategy for archive libraries, to support the use of
912libraries specific to each particular architecture, by including in the
913search loop names suffixed with the string identifying the architecture.
914
915 For example, if your `ld' command line included `-ACA' as well as
916`-ltry', the linker would look (in its built-in search paths, and in
917any paths you specify with `-L') for a library with the names
918
919 try
920 libtry.a
921 tryca
922 libtryca.a
923
924The first two possibilities would be considered in any event; the last
925two are due to the use of `-ACA'.
926
927 You can meaningfully use `-A' more than once on a command line, since
928the 960 architecture family allows combination of target architectures;
929each use will add another pair of name variants to search for when `-l'
930specifies a library.
931
932 `ld' supports the `--relax' option for the i960 family. If you
933specify `--relax', `ld' finds all `balx' and `calx' instructions whose
934targets are within 24 bits, and turns them into 24-bit program-counter
935relative `bal' and `cal' instructions, respectively. `ld' also turns
936`cal' instructions into `bal' instructions when it determines that the
937target subroutine is a leaf routine (that is, the target subroutine does
938not itself call any subroutines).
939
940
941File: ld.info, Node: ARM, Next: HPPA ELF32, Prev: i960, Up: Machine Dependent
942
943`ld''s support for interworking between ARM and Thumb code
944==========================================================
945
946 For the ARM, `ld' will generate code stubs to allow functions calls
947betweem ARM and Thumb code. These stubs only work with code that has
948been compiled and assembled with the `-mthumb-interwork' command line
949option. If it is necessary to link with old ARM object files or
950libraries, which have not been compiled with the -mthumb-interwork
951option then the `--support-old-code' command line switch should be
952given to the linker. This will make it generate larger stub functions
953which will work with non-interworking aware ARM code. Note, however,
954the linker does not support generating stubs for function calls to
955non-interworking aware Thumb code.
956
957 The `--thumb-entry' switch is a duplicate of the generic `--entry'
958switch, in that it sets the program's starting address. But it also
959sets the bottom bit of the address, so that it can be branched to using
960a BX instruction, and the program will start executing in Thumb mode
961straight away.
962
963
964File: ld.info, Node: HPPA ELF32, Next: TI COFF, Prev: ARM, Up: Machine Dependent
965
966`ld' and HPPA 32-bit ELF support
967================================
968
969 When generating a shared library, `ld' will by default generate
970import stubs suitable for use with a single sub-space application. The
971`--multi-subspace' switch causes `ld' to generate export stubs, and
972different (larger) import stubs suitable for use with multiple
973sub-spaces.
974
975 Long branch stubs and import/export stubs are placed by `ld' in stub
976sections located between groups of input sections. `--stub-group-size'
977specifies the maximum size of a group of input sections handled by one
978stub section. Since branch offsets are signed, a stub section may
979serve two groups of input sections, one group before the stub section,
980and one group after it. However, when using conditional branches that
981require stubs, it may be better (for branch prediction) that stub
982sections only serve one group of input sections. A negative value for
983`N' chooses this scheme, ensuring that branches to stubs always use a
984negative offset. Two special values of `N' are recognized, `1' and
985`-1'. These both instruct `ld' to automatically size input section
986groups for the branch types detected, with the same behaviour regarding
987stub placement as other positive or negative values of `N' respectively.
988
989 Note that `--stub-group-size' does not split input sections. A
990single input section larger than the group size specified will of course
991create a larger group (of one section). If input sections are too
992large, it may not be possible for a branch to reach its stub.
993
994
995File: ld.info, Node: TI COFF, Prev: HPPA ELF32, Up: Machine Dependent
996
997`ld''s support for various TI COFF versions
998===========================================
999
1000 The `--format' switch allows selection of one of the various TI COFF
1001versions. The latest of this writing is 2; versions 0 and 1 are also
1002supported. The TI COFF versions also vary in header byte-order format;
1003`ld' will read any version or byte order, but the output header format
1004depends on the default specified by the specific target.
1005
1006
1007File: ld.info, Node: BFD, Next: Reporting Bugs, Prev: Machine Dependent, Up: Top
1008
1009BFD
1010***
1011
1012 The linker accesses object and archive files using the BFD libraries.
1013These libraries allow the linker to use the same routines to operate on
1014object files whatever the object file format. A different object file
1015format can be supported simply by creating a new BFD back end and adding
1016it to the library. To conserve runtime memory, however, the linker and
1017associated tools are usually configured to support only a subset of the
1018object file formats available. You can use `objdump -i' (*note
1019objdump: (binutils.info)objdump.) to list all the formats available for
1020your configuration.
1021
1022 As with most implementations, BFD is a compromise between several
1023conflicting requirements. The major factor influencing BFD design was
1024efficiency: any time used converting between formats is time which
1025would not have been spent had BFD not been involved. This is partly
1026offset by abstraction payback; since BFD simplifies applications and
1027back ends, more time and care may be spent optimizing algorithms for a
1028greater speed.
1029
1030 One minor artifact of the BFD solution which you should bear in mind
1031is the potential for information loss. There are two places where
1032useful information can be lost using the BFD mechanism: during
1033conversion and during output. *Note BFD information loss::.
1034
1035* Menu:
1036
1037* BFD outline:: How it works: an outline of BFD
1038
1039
1040File: ld.info, Node: BFD outline, Up: BFD
1041
1042How it works: an outline of BFD
1043===============================
1044
1045 When an object file is opened, BFD subroutines automatically
1046determine the format of the input object file. They then build a
1047descriptor in memory with pointers to routines that will be used to
1048access elements of the object file's data structures.
1049
1050 As different information from the the object files is required, BFD
1051reads from different sections of the file and processes them. For
1052example, a very common operation for the linker is processing symbol
1053tables. Each BFD back end provides a routine for converting between
1054the object file's representation of symbols and an internal canonical
1055format. When the linker asks for the symbol table of an object file, it
1056calls through a memory pointer to the routine from the relevant BFD
1057back end which reads and converts the table into a canonical form. The
1058linker then operates upon the canonical form. When the link is finished
1059and the linker writes the output file's symbol table, another BFD back
1060end routine is called to take the newly created symbol table and
1061convert it into the chosen output format.
1062
1063* Menu:
1064
1065* BFD information loss:: Information Loss
1066* Canonical format:: The BFD canonical object-file format
1067
1068
1069File: ld.info, Node: BFD information loss, Next: Canonical format, Up: BFD outline
1070
1071Information Loss
1072----------------
1073
1074 _Information can be lost during output._ The output formats
1075supported by BFD do not provide identical facilities, and information
1076which can be described in one form has nowhere to go in another format.
1077One example of this is alignment information in `b.out'. There is
1078nowhere in an `a.out' format file to store alignment information on the
1079contained data, so when a file is linked from `b.out' and an `a.out'
1080image is produced, alignment information will not propagate to the
1081output file. (The linker will still use the alignment information
1082internally, so the link is performed correctly).
1083
1084 Another example is COFF section names. COFF files may contain an
1085unlimited number of sections, each one with a textual section name. If
1086the target of the link is a format which does not have many sections
1087(e.g., `a.out') or has sections without names (e.g., the Oasys format),
1088the link cannot be done simply. You can circumvent this problem by
1089describing the desired input-to-output section mapping with the linker
1090command language.
1091
1092 _Information can be lost during canonicalization._ The BFD internal
1093canonical form of the external formats is not exhaustive; there are
1094structures in input formats for which there is no direct representation
1095internally. This means that the BFD back ends cannot maintain all
1096possible data richness through the transformation between external to
1097internal and back to external formats.
1098
1099 This limitation is only a problem when an application reads one
1100format and writes another. Each BFD back end is responsible for
1101maintaining as much data as possible, and the internal BFD canonical
1102form has structures which are opaque to the BFD core, and exported only
1103to the back ends. When a file is read in one format, the canonical form
1104is generated for BFD and the application. At the same time, the back
1105end saves away any information which may otherwise be lost. If the data
1106is then written back in the same format, the back end routine will be
1107able to use the canonical form provided by the BFD core as well as the
1108information it prepared earlier. Since there is a great deal of
1109commonality between back ends, there is no information lost when
1110linking or copying big endian COFF to little endian COFF, or `a.out' to
1111`b.out'. When a mixture of formats is linked, the information is only
1112lost from the files whose format differs from the destination.
1113
1114
1115File: ld.info, Node: Canonical format, Prev: BFD information loss, Up: BFD outline
1116
1117The BFD canonical object-file format
1118------------------------------------
1119
1120 The greatest potential for loss of information occurs when there is
1121the least overlap between the information provided by the source
1122format, that stored by the canonical format, and that needed by the
1123destination format. A brief description of the canonical form may help
1124you understand which kinds of data you can count on preserving across
1125conversions.
1126
1127_files_
1128 Information stored on a per-file basis includes target machine
1129 architecture, particular implementation format type, a demand
1130 pageable bit, and a write protected bit. Information like Unix
1131 magic numbers is not stored here--only the magic numbers' meaning,
1132 so a `ZMAGIC' file would have both the demand pageable bit and the
1133 write protected text bit set. The byte order of the target is
1134 stored on a per-file basis, so that big- and little-endian object
1135 files may be used with one another.
1136
1137_sections_
1138 Each section in the input file contains the name of the section,
1139 the section's original address in the object file, size and
1140 alignment information, various flags, and pointers into other BFD
1141 data structures.
1142
1143_symbols_
1144 Each symbol contains a pointer to the information for the object
1145 file which originally defined it, its name, its value, and various
1146 flag bits. When a BFD back end reads in a symbol table, it
1147 relocates all symbols to make them relative to the base of the
1148 section where they were defined. Doing this ensures that each
1149 symbol points to its containing section. Each symbol also has a
1150 varying amount of hidden private data for the BFD back end. Since
1151 the symbol points to the original file, the private data format
1152 for that symbol is accessible. `ld' can operate on a collection
1153 of symbols of wildly different formats without problems.
1154
1155 Normal global and simple local symbols are maintained on output,
1156 so an output file (no matter its format) will retain symbols
1157 pointing to functions and to global, static, and common variables.
1158 Some symbol information is not worth retaining; in `a.out', type
1159 information is stored in the symbol table as long symbol names.
1160 This information would be useless to most COFF debuggers; the
1161 linker has command line switches to allow users to throw it away.
1162
1163 There is one word of type information within the symbol, so if the
1164 format supports symbol type information within symbols (for
1165 example, COFF, IEEE, Oasys) and the type is simple enough to fit
1166 within one word (nearly everything but aggregates), the
1167 information will be preserved.
1168
1169_relocation level_
1170 Each canonical BFD relocation record contains a pointer to the
1171 symbol to relocate to, the offset of the data to relocate, the
1172 section the data is in, and a pointer to a relocation type
1173 descriptor. Relocation is performed by passing messages through
1174 the relocation type descriptor and the symbol pointer. Therefore,
1175 relocations can be performed on output data using a relocation
1176 method that is only available in one of the input formats. For
1177 instance, Oasys provides a byte relocation format. A relocation
1178 record requesting this relocation type would point indirectly to a
1179 routine to perform this, so the relocation may be performed on a
1180 byte being written to a 68k COFF file, even though 68k COFF has no
1181 such relocation type.
1182
1183_line numbers_
1184 Object formats can contain, for debugging purposes, some form of
1185 mapping between symbols, source line numbers, and addresses in the
1186 output file. These addresses have to be relocated along with the
1187 symbol information. Each symbol with an associated list of line
1188 number records points to the first record of the list. The head
1189 of a line number list consists of a pointer to the symbol, which
1190 allows finding out the address of the function whose line number
1191 is being described. The rest of the list is made up of pairs:
1192 offsets into the section and line numbers. Any format which can
1193 simply derive this information can pass it successfully between
1194 formats (COFF, IEEE and Oasys).
1195
1196
1197File: ld.info, Node: Reporting Bugs, Next: MRI, Prev: BFD, Up: Top
1198
1199Reporting Bugs
1200**************
1201
1202 Your bug reports play an essential role in making `ld' reliable.
1203
1204 Reporting a bug may help you by bringing a solution to your problem,
1205or it may not. But in any case the principal function of a bug report
1206is to help the entire community by making the next version of `ld' work
1207better. Bug reports are your contribution to the maintenance of `ld'.
1208
1209 In order for a bug report to serve its purpose, you must include the
1210information that enables us to fix the bug.
1211
1212* Menu:
1213
1214* Bug Criteria:: Have you found a bug?
1215* Bug Reporting:: How to report bugs
1216
Note: See TracBrowser for help on using the repository browser.