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

Last change on this file since 947 was 610, checked in by bird, 22 years ago

This commit was generated by cvs2svn to compensate for changes in r609,
which included commits to RCS files with non-trunk default branches.

  • Property cvs2svn:cvs-rev set to 1.1.1.2
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 43.4 KB
Line 
1This is ld.info, produced by makeinfo version 4.3 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.14.
8
9 Copyright (C) 1991, 92, 93, 94, 95, 96, 97, 98, 99, 2000, 2001,
102002, 2003 Free Software 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; this is done using wildcard patterns, so that any
400symbol whose name begins with `old', `original', or `new' is matched.
401The wildcard patterns available are the same as those used in the shell
402when matching filenames (also known as "globbing").
403
404 Next, the version script defines node `VERS_1.2'. This node depends
405upon `VERS_1.1'. The script binds the symbol `foo2' to the version
406node `VERS_1.2'.
407
408 Finally, the version script defines node `VERS_2.0'. This node
409depends 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
413specifically bound to a version node, it will effectively bind it to an
414unspecified base version of the library. You can bind all otherwise
415unspecified symbols to a given version node by using `global: *;'
416somewhere in the version script.
417
418 The names of the version nodes have no specific meaning other than
419what they might suggest to the person reading them. The `2.0' version
420could just as well have appeared in between `1.1' and `1.2'. However,
421this 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
424version script. Such version script doesn't assign any versions to
425symbols, only selects which symbols will be globally visible out and
426which won't.
427
428 { global: foo; bar; local: *; };
429
430 When you link an application against a shared library that has
431versioned symbols, the application itself knows which version of each
432symbol it requires, and it also knows which version nodes it needs from
433each shared library it is linked against. Thus at runtime, the dynamic
434loader can make a quick check to make sure that the libraries you have
435linked against do in fact supply all of the version nodes that the
436application will need to resolve all of the dynamic symbols. In this
437way it is possible for the dynamic linker to know with certainty that
438all external symbols that it needs will be resolvable without having to
439search for each symbol reference.
440
441 The symbol versioning is in effect a much more sophisticated way of
442doing minor version checking that SunOS does. The fundamental problem
443that is being addressed here is that typically references to external
444functions are bound on an as-needed basis, and are not all bound when
445the application starts up. If a shared library is out of date, a
446required interface may be missing; when the application tries to use
447that interface, it may suddenly and unexpectedly fail. With symbol
448versioning, the user will get a warning when they start their program if
449the libraries being used with the application are too old.
450