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: Environment, Prev: Options, Up: Invocation
|
---|
14 |
|
---|
15 | Environment Variables
|
---|
16 | =====================
|
---|
17 |
|
---|
18 | You can change the behavior of `ld' with the environment variables
|
---|
19 | `GNUTARGET', `LDEMULATION' and `COLLECT_NO_DEMANGLE'.
|
---|
20 |
|
---|
21 | `GNUTARGET' determines the input-file object format if you don't use
|
---|
22 | `-b' (or its synonym `--format'). Its value should be one of the BFD
|
---|
23 | names for an input format (*note BFD::). If there is no `GNUTARGET' in
|
---|
24 | the environment, `ld' uses the natural format of the target. If
|
---|
25 | `GNUTARGET' is set to `default' then BFD attempts to discover the input
|
---|
26 | format by examining binary input files; this method often succeeds, but
|
---|
27 | there are potential ambiguities, since there is no method of ensuring
|
---|
28 | that the magic number used to specify object-file formats is unique.
|
---|
29 | However, the configuration procedure for BFD on each system places the
|
---|
30 | conventional format for that system first in the search-list, so
|
---|
31 | ambiguities are resolved in favor of convention.
|
---|
32 |
|
---|
33 | `LDEMULATION' determines the default emulation if you don't use the
|
---|
34 | `-m' option. The emulation can affect various aspects of linker
|
---|
35 | behaviour, particularly the default linker script. You can list the
|
---|
36 | available emulations with the `--verbose' or `-V' options. If the `-m'
|
---|
37 | option is not used, and the `LDEMULATION' environment variable is not
|
---|
38 | defined, the default emulation depends upon how the linker was
|
---|
39 | configured.
|
---|
40 |
|
---|
41 | Normally, the linker will default to demangling symbols. However, if
|
---|
42 | `COLLECT_NO_DEMANGLE' is set in the environment, then it will default
|
---|
43 | to not demangling symbols. This environment variable is used in a
|
---|
44 | similar fashion by the `gcc' linker wrapper program. The default may
|
---|
45 | be overridden by the `--demangle' and `--no-demangle' options.
|
---|
46 |
|
---|
47 |
|
---|
48 | File: ld.info, Node: Scripts, Next: Machine Dependent, Prev: Invocation, Up: Top
|
---|
49 |
|
---|
50 | Linker Scripts
|
---|
51 | **************
|
---|
52 |
|
---|
53 | Every link is controlled by a "linker script". This script is
|
---|
54 | written in the linker command language.
|
---|
55 |
|
---|
56 | The main purpose of the linker script is to describe how the
|
---|
57 | sections in the input files should be mapped into the output file, and
|
---|
58 | to control the memory layout of the output file. Most linker scripts
|
---|
59 | do nothing more than this. However, when necessary, the linker script
|
---|
60 | can also direct the linker to perform many other operations, using the
|
---|
61 | commands described below.
|
---|
62 |
|
---|
63 | The linker always uses a linker script. If you do not supply one
|
---|
64 | yourself, the linker will use a default script that is compiled into the
|
---|
65 | linker executable. You can use the `--verbose' command line option to
|
---|
66 | display the default linker script. Certain command line options, such
|
---|
67 | as `-r' or `-N', will affect the default linker script.
|
---|
68 |
|
---|
69 | You may supply your own linker script by using the `-T' command line
|
---|
70 | option. When you do this, your linker script will replace the default
|
---|
71 | linker script.
|
---|
72 |
|
---|
73 | You may also use linker scripts implicitly by naming them as input
|
---|
74 | files to the linker, as though they were files to be linked. *Note
|
---|
75 | Implicit Linker Scripts::.
|
---|
76 |
|
---|
77 | * Menu:
|
---|
78 |
|
---|
79 | * Basic Script Concepts:: Basic Linker Script Concepts
|
---|
80 | * Script Format:: Linker Script Format
|
---|
81 | * Simple Example:: Simple Linker Script Example
|
---|
82 | * Simple Commands:: Simple Linker Script Commands
|
---|
83 | * Assignments:: Assigning Values to Symbols
|
---|
84 | * SECTIONS:: SECTIONS Command
|
---|
85 | * MEMORY:: MEMORY Command
|
---|
86 | * PHDRS:: PHDRS Command
|
---|
87 | * VERSION:: VERSION Command
|
---|
88 | * Expressions:: Expressions in Linker Scripts
|
---|
89 | * Implicit Linker Scripts:: Implicit Linker Scripts
|
---|
90 |
|
---|
91 |
|
---|
92 | File: ld.info, Node: Basic Script Concepts, Next: Script Format, Up: Scripts
|
---|
93 |
|
---|
94 | Basic Linker Script Concepts
|
---|
95 | ============================
|
---|
96 |
|
---|
97 | We need to define some basic concepts and vocabulary in order to
|
---|
98 | describe the linker script language.
|
---|
99 |
|
---|
100 | The linker combines input files into a single output file. The
|
---|
101 | output file and each input file are in a special data format known as an
|
---|
102 | "object file format". Each file is called an "object file". The
|
---|
103 | output file is often called an "executable", but for our purposes we
|
---|
104 | will also call it an object file. Each object file has, among other
|
---|
105 | things, a list of "sections". We sometimes refer to a section in an
|
---|
106 | input file as an "input section"; similarly, a section in the output
|
---|
107 | file is an "output section".
|
---|
108 |
|
---|
109 | Each section in an object file has a name and a size. Most sections
|
---|
110 | also have an associated block of data, known as the "section contents".
|
---|
111 | A section may be marked as "loadable", which mean that the contents
|
---|
112 | should be loaded into memory when the output file is run. A section
|
---|
113 | with no contents may be "allocatable", which means that an area in
|
---|
114 | memory should be set aside, but nothing in particular should be loaded
|
---|
115 | there (in some cases this memory must be zeroed out). A section which
|
---|
116 | is neither loadable nor allocatable typically contains some sort of
|
---|
117 | debugging information.
|
---|
118 |
|
---|
119 | Every loadable or allocatable output section has two addresses. The
|
---|
120 | first is the "VMA", or virtual memory address. This is the address the
|
---|
121 | section will have when the output file is run. The second is the
|
---|
122 | "LMA", or load memory address. This is the address at which the
|
---|
123 | section will be loaded. In most cases the two addresses will be the
|
---|
124 | same. An example of when they might be different is when a data section
|
---|
125 | is loaded into ROM, and then copied into RAM when the program starts up
|
---|
126 | (this technique is often used to initialize global variables in a ROM
|
---|
127 | based system). In this case the ROM address would be the LMA, and the
|
---|
128 | RAM address would be the VMA.
|
---|
129 |
|
---|
130 | You can see the sections in an object file by using the `objdump'
|
---|
131 | program with the `-h' option.
|
---|
132 |
|
---|
133 | Every object file also has a list of "symbols", known as the "symbol
|
---|
134 | table". A symbol may be defined or undefined. Each symbol has a name,
|
---|
135 | and each defined symbol has an address, among other information. If
|
---|
136 | you compile a C or C++ program into an object file, you will get a
|
---|
137 | defined symbol for every defined function and global or static
|
---|
138 | variable. Every undefined function or global variable which is
|
---|
139 | referenced in the input file will become an undefined symbol.
|
---|
140 |
|
---|
141 | You can see the symbols in an object file by using the `nm' program,
|
---|
142 | or by using the `objdump' program with the `-t' option.
|
---|
143 |
|
---|
144 |
|
---|
145 | File: ld.info, Node: Script Format, Next: Simple Example, Prev: Basic Script Concepts, Up: Scripts
|
---|
146 |
|
---|
147 | Linker Script Format
|
---|
148 | ====================
|
---|
149 |
|
---|
150 | Linker scripts are text files.
|
---|
151 |
|
---|
152 | You write a linker script as a series of commands. Each command is
|
---|
153 | either a keyword, possibly followed by arguments, or an assignment to a
|
---|
154 | symbol. You may separate commands using semicolons. Whitespace is
|
---|
155 | generally ignored.
|
---|
156 |
|
---|
157 | Strings such as file or format names can normally be entered
|
---|
158 | directly. If the file name contains a character such as a comma which
|
---|
159 | would otherwise serve to separate file names, you may put the file name
|
---|
160 | in double quotes. There is no way to use a double quote character in a
|
---|
161 | file name.
|
---|
162 |
|
---|
163 | You may include comments in linker scripts just as in C, delimited by
|
---|
164 | `/*' and `*/'. As in C, comments are syntactically equivalent to
|
---|
165 | whitespace.
|
---|
166 |
|
---|
167 |
|
---|
168 | File: ld.info, Node: Simple Example, Next: Simple Commands, Prev: Script Format, Up: Scripts
|
---|
169 |
|
---|
170 | Simple Linker Script Example
|
---|
171 | ============================
|
---|
172 |
|
---|
173 | Many linker scripts are fairly simple.
|
---|
174 |
|
---|
175 | The simplest possible linker script has just one command:
|
---|
176 | `SECTIONS'. You use the `SECTIONS' command to describe the memory
|
---|
177 | layout of the output file.
|
---|
178 |
|
---|
179 | The `SECTIONS' command is a powerful command. Here we will describe
|
---|
180 | a simple use of it. Let's assume your program consists only of code,
|
---|
181 | initialized data, and uninitialized data. These will be in the
|
---|
182 | `.text', `.data', and `.bss' sections, respectively. Let's assume
|
---|
183 | further that these are the only sections which appear in your input
|
---|
184 | files.
|
---|
185 |
|
---|
186 | For this example, let's say that the code should be loaded at address
|
---|
187 | 0x10000, and that the data should start at address 0x8000000. Here is a
|
---|
188 | linker script which will do that:
|
---|
189 | SECTIONS
|
---|
190 | {
|
---|
191 | . = 0x10000;
|
---|
192 | .text : { *(.text) }
|
---|
193 | . = 0x8000000;
|
---|
194 | .data : { *(.data) }
|
---|
195 | .bss : { *(.bss) }
|
---|
196 | }
|
---|
197 |
|
---|
198 | You write the `SECTIONS' command as the keyword `SECTIONS', followed
|
---|
199 | by a series of symbol assignments and output section descriptions
|
---|
200 | enclosed in curly braces.
|
---|
201 |
|
---|
202 | The first line inside the `SECTIONS' command of the above example
|
---|
203 | sets the value of the special symbol `.', which is the location
|
---|
204 | counter. If you do not specify the address of an output section in some
|
---|
205 | other way (other ways are described later), the address is set from the
|
---|
206 | current value of the location counter. The location counter is then
|
---|
207 | incremented by the size of the output section. At the start of the
|
---|
208 | `SECTIONS' command, the location counter has the value `0'.
|
---|
209 |
|
---|
210 | The second line defines an output section, `.text'. The colon is
|
---|
211 | required syntax which may be ignored for now. Within the curly braces
|
---|
212 | after the output section name, you list the names of the input sections
|
---|
213 | which should be placed into this output section. The `*' is a wildcard
|
---|
214 | which matches any file name. The expression `*(.text)' means all
|
---|
215 | `.text' input sections in all input files.
|
---|
216 |
|
---|
217 | Since the location counter is `0x10000' when the output section
|
---|
218 | `.text' is defined, the linker will set the address of the `.text'
|
---|
219 | section in the output file to be `0x10000'.
|
---|
220 |
|
---|
221 | The remaining lines define the `.data' and `.bss' sections in the
|
---|
222 | output file. The linker will place the `.data' output section at
|
---|
223 | address `0x8000000'. After the linker places the `.data' output
|
---|
224 | section, the value of the location counter will be `0x8000000' plus the
|
---|
225 | size of the `.data' output section. The effect is that the linker will
|
---|
226 | place the `.bss' output section immediately after the `.data' output
|
---|
227 | section in memory
|
---|
228 |
|
---|
229 | The linker will ensure that each output section has the required
|
---|
230 | alignment, by increasing the location counter if necessary. In this
|
---|
231 | example, the specified addresses for the `.text' and `.data' sections
|
---|
232 | will probably satisfy any alignment constraints, but the linker may
|
---|
233 | have to create a small gap between the `.data' and `.bss' sections.
|
---|
234 |
|
---|
235 | That's it! That's a simple and complete linker script.
|
---|
236 |
|
---|
237 |
|
---|
238 | File: ld.info, Node: Simple Commands, Next: Assignments, Prev: Simple Example, Up: Scripts
|
---|
239 |
|
---|
240 | Simple Linker Script Commands
|
---|
241 | =============================
|
---|
242 |
|
---|
243 | In this section we describe the simple linker script commands.
|
---|
244 |
|
---|
245 | * Menu:
|
---|
246 |
|
---|
247 | * Entry Point:: Setting the entry point
|
---|
248 | * File Commands:: Commands dealing with files
|
---|
249 |
|
---|
250 | * Format Commands:: Commands dealing with object file formats
|
---|
251 |
|
---|
252 | * Miscellaneous Commands:: Other linker script commands
|
---|
253 |
|
---|
254 |
|
---|
255 | File: ld.info, Node: Entry Point, Next: File Commands, Up: Simple Commands
|
---|
256 |
|
---|
257 | Setting the Entry Point
|
---|
258 | -----------------------
|
---|
259 |
|
---|
260 | The first instruction to execute in a program is called the "entry
|
---|
261 | point". You can use the `ENTRY' linker script command to set the entry
|
---|
262 | point. The argument is a symbol name:
|
---|
263 | ENTRY(SYMBOL)
|
---|
264 |
|
---|
265 | There are several ways to set the entry point. The linker will set
|
---|
266 | the entry point by trying each of the following methods in order, and
|
---|
267 | stopping when one of them succeeds:
|
---|
268 | * the `-e' ENTRY command-line option;
|
---|
269 |
|
---|
270 | * the `ENTRY(SYMBOL)' command in a linker script;
|
---|
271 |
|
---|
272 | * the value of the symbol `start', if defined;
|
---|
273 |
|
---|
274 | * the address of the first byte of the `.text' section, if present;
|
---|
275 |
|
---|
276 | * The address `0'.
|
---|
277 |
|
---|
278 |
|
---|
279 | File: ld.info, Node: File Commands, Next: Format Commands, Prev: Entry Point, Up: Simple Commands
|
---|
280 |
|
---|
281 | Commands Dealing with Files
|
---|
282 | ---------------------------
|
---|
283 |
|
---|
284 | Several linker script commands deal with files.
|
---|
285 |
|
---|
286 | `INCLUDE FILENAME'
|
---|
287 | Include the linker script FILENAME at this point. The file will
|
---|
288 | be searched for in the current directory, and in any directory
|
---|
289 | specified with the `-L' option. You can nest calls to `INCLUDE'
|
---|
290 | up to 10 levels deep.
|
---|
291 |
|
---|
292 | `INPUT(FILE, FILE, ...)'
|
---|
293 | `INPUT(FILE FILE ...)'
|
---|
294 | The `INPUT' command directs the linker to include the named files
|
---|
295 | in the link, as though they were named on the command line.
|
---|
296 |
|
---|
297 | For example, if you always want to include `subr.o' any time you do
|
---|
298 | a link, but you can't be bothered to put it on every link command
|
---|
299 | line, then you can put `INPUT (subr.o)' in your linker script.
|
---|
300 |
|
---|
301 | In fact, if you like, you can list all of your input files in the
|
---|
302 | linker script, and then invoke the linker with nothing but a `-T'
|
---|
303 | option.
|
---|
304 |
|
---|
305 | In case a "sysroot prefix" is configured, and the filename starts
|
---|
306 | with the `/' character, and the script being processed was located
|
---|
307 | inside the "sysroot prefix", the filename will be looked for in
|
---|
308 | the "sysroot prefix". Otherwise, the linker will try to open the
|
---|
309 | file in the current directory. If it is not found, the linker
|
---|
310 | will search through the archive library search path. See the
|
---|
311 | description of `-L' in *Note Command Line Options: Options.
|
---|
312 |
|
---|
313 | If you use `INPUT (-lFILE)', `ld' will transform the name to
|
---|
314 | `libFILE.a', as with the command line argument `-l'.
|
---|
315 |
|
---|
316 | When you use the `INPUT' command in an implicit linker script, the
|
---|
317 | files will be included in the link at the point at which the linker
|
---|
318 | script file is included. This can affect archive searching.
|
---|
319 |
|
---|
320 | `GROUP(FILE, FILE, ...)'
|
---|
321 | `GROUP(FILE FILE ...)'
|
---|
322 | The `GROUP' command is like `INPUT', except that the named files
|
---|
323 | should all be archives, and they are searched repeatedly until no
|
---|
324 | new undefined references are created. See the description of `-('
|
---|
325 | in *Note Command Line Options: Options.
|
---|
326 |
|
---|
327 | `OUTPUT(FILENAME)'
|
---|
328 | The `OUTPUT' command names the output file. Using
|
---|
329 | `OUTPUT(FILENAME)' in the linker script is exactly like using `-o
|
---|
330 | FILENAME' on the command line (*note Command Line Options:
|
---|
331 | Options.). If both are used, the command line option takes
|
---|
332 | precedence.
|
---|
333 |
|
---|
334 | You can use the `OUTPUT' command to define a default name for the
|
---|
335 | output file other than the usual default of `a.out'.
|
---|
336 |
|
---|
337 | `SEARCH_DIR(PATH)'
|
---|
338 | The `SEARCH_DIR' command adds PATH to the list of paths where `ld'
|
---|
339 | looks for archive libraries. Using `SEARCH_DIR(PATH)' is exactly
|
---|
340 | like using `-L PATH' on the command line (*note Command Line
|
---|
341 | Options: Options.). If both are used, then the linker will search
|
---|
342 | both paths. Paths specified using the command line option are
|
---|
343 | searched first.
|
---|
344 |
|
---|
345 | `STARTUP(FILENAME)'
|
---|
346 | The `STARTUP' command is just like the `INPUT' command, except
|
---|
347 | that FILENAME will become the first input file to be linked, as
|
---|
348 | though it were specified first on the command line. This may be
|
---|
349 | useful when using a system in which the entry point is always the
|
---|
350 | start of the first file.
|
---|
351 |
|
---|
352 |
|
---|
353 | File: ld.info, Node: Format Commands, Next: Miscellaneous Commands, Prev: File Commands, Up: Simple Commands
|
---|
354 |
|
---|
355 | Commands Dealing with Object File Formats
|
---|
356 | -----------------------------------------
|
---|
357 |
|
---|
358 | A couple of linker script commands deal with object file formats.
|
---|
359 |
|
---|
360 | `OUTPUT_FORMAT(BFDNAME)'
|
---|
361 | `OUTPUT_FORMAT(DEFAULT, BIG, LITTLE)'
|
---|
362 | The `OUTPUT_FORMAT' command names the BFD format to use for the
|
---|
363 | output file (*note BFD::). Using `OUTPUT_FORMAT(BFDNAME)' is
|
---|
364 | exactly like using `--oformat BFDNAME' on the command line (*note
|
---|
365 | Command Line Options: Options.). If both are used, the command
|
---|
366 | line option takes precedence.
|
---|
367 |
|
---|
368 | You can use `OUTPUT_FORMAT' with three arguments to use different
|
---|
369 | formats based on the `-EB' and `-EL' command line options. This
|
---|
370 | permits the linker script to set the output format based on the
|
---|
371 | desired endianness.
|
---|
372 |
|
---|
373 | If neither `-EB' nor `-EL' are used, then the output format will
|
---|
374 | be the first argument, DEFAULT. If `-EB' is used, the output
|
---|
375 | format will be the second argument, BIG. If `-EL' is used, the
|
---|
376 | output format will be the third argument, LITTLE.
|
---|
377 |
|
---|
378 | For example, the default linker script for the MIPS ELF target
|
---|
379 | uses this command:
|
---|
380 | OUTPUT_FORMAT(elf32-bigmips, elf32-bigmips, elf32-littlemips)
|
---|
381 | This says that the default format for the output file is
|
---|
382 | `elf32-bigmips', but if the user uses the `-EL' command line
|
---|
383 | option, the output file will be created in the `elf32-littlemips'
|
---|
384 | format.
|
---|
385 |
|
---|
386 | `TARGET(BFDNAME)'
|
---|
387 | The `TARGET' command names the BFD format to use when reading input
|
---|
388 | files. It affects subsequent `INPUT' and `GROUP' commands. This
|
---|
389 | command is like using `-b BFDNAME' on the command line (*note
|
---|
390 | Command Line Options: Options.). If the `TARGET' command is used
|
---|
391 | but `OUTPUT_FORMAT' is not, then the last `TARGET' command is also
|
---|
392 | used to set the format for the output file. *Note BFD::.
|
---|
393 |
|
---|
394 |
|
---|
395 | File: ld.info, Node: Miscellaneous Commands, Prev: Format Commands, Up: Simple Commands
|
---|
396 |
|
---|
397 | Other Linker Script Commands
|
---|
398 | ----------------------------
|
---|
399 |
|
---|
400 | There are a few other linker scripts commands.
|
---|
401 |
|
---|
402 | `ASSERT(EXP, MESSAGE)'
|
---|
403 | Ensure that EXP is non-zero. If it is zero, then exit the linker
|
---|
404 | with an error code, and print MESSAGE.
|
---|
405 |
|
---|
406 | `EXTERN(SYMBOL SYMBOL ...)'
|
---|
407 | Force SYMBOL to be entered in the output file as an undefined
|
---|
408 | symbol. Doing this may, for example, trigger linking of additional
|
---|
409 | modules from standard libraries. You may list several SYMBOLs for
|
---|
410 | each `EXTERN', and you may use `EXTERN' multiple times. This
|
---|
411 | command has the same effect as the `-u' command-line option.
|
---|
412 |
|
---|
413 | `FORCE_COMMON_ALLOCATION'
|
---|
414 | This command has the same effect as the `-d' command-line option:
|
---|
415 | to make `ld' assign space to common symbols even if a relocatable
|
---|
416 | output file is specified (`-r').
|
---|
417 |
|
---|
418 | `INHIBIT_COMMON_ALLOCATION'
|
---|
419 | This command has the same effect as the `--no-define-common'
|
---|
420 | command-line option: to make `ld' omit the assignment of addresses
|
---|
421 | to common symbols even for a non-relocatable output file.
|
---|
422 |
|
---|
423 | `NOCROSSREFS(SECTION SECTION ...)'
|
---|
424 | This command may be used to tell `ld' to issue an error about any
|
---|
425 | references among certain output sections.
|
---|
426 |
|
---|
427 | In certain types of programs, particularly on embedded systems when
|
---|
428 | using overlays, when one section is loaded into memory, another
|
---|
429 | section will not be. Any direct references between the two
|
---|
430 | sections would be errors. For example, it would be an error if
|
---|
431 | code in one section called a function defined in the other section.
|
---|
432 |
|
---|
433 | The `NOCROSSREFS' command takes a list of output section names. If
|
---|
434 | `ld' detects any cross references between the sections, it reports
|
---|
435 | an error and returns a non-zero exit status. Note that the
|
---|
436 | `NOCROSSREFS' command uses output section names, not input section
|
---|
437 | names.
|
---|
438 |
|
---|
439 | `OUTPUT_ARCH(BFDARCH)'
|
---|
440 | Specify a particular output machine architecture. The argument is
|
---|
441 | one of the names used by the BFD library (*note BFD::). You can
|
---|
442 | see the architecture of an object file by using the `objdump'
|
---|
443 | program with the `-f' option.
|
---|
444 |
|
---|
445 |
|
---|
446 | File: ld.info, Node: Assignments, Next: SECTIONS, Prev: Simple Commands, Up: Scripts
|
---|
447 |
|
---|
448 | Assigning Values to Symbols
|
---|
449 | ===========================
|
---|
450 |
|
---|
451 | You may assign a value to a symbol in a linker script. This will
|
---|
452 | define the symbol as a global symbol.
|
---|
453 |
|
---|
454 | * Menu:
|
---|
455 |
|
---|
456 | * Simple Assignments:: Simple Assignments
|
---|
457 | * PROVIDE:: PROVIDE
|
---|
458 |
|
---|
459 |
|
---|
460 | File: ld.info, Node: Simple Assignments, Next: PROVIDE, Up: Assignments
|
---|
461 |
|
---|
462 | Simple Assignments
|
---|
463 | ------------------
|
---|
464 |
|
---|
465 | You may assign to a symbol using any of the C assignment operators:
|
---|
466 |
|
---|
467 | `SYMBOL = EXPRESSION ;'
|
---|
468 | `SYMBOL += EXPRESSION ;'
|
---|
469 | `SYMBOL -= EXPRESSION ;'
|
---|
470 | `SYMBOL *= EXPRESSION ;'
|
---|
471 | `SYMBOL /= EXPRESSION ;'
|
---|
472 | `SYMBOL <<= EXPRESSION ;'
|
---|
473 | `SYMBOL >>= EXPRESSION ;'
|
---|
474 | `SYMBOL &= EXPRESSION ;'
|
---|
475 | `SYMBOL |= EXPRESSION ;'
|
---|
476 | The first case will define SYMBOL to the value of EXPRESSION. In
|
---|
477 | the other cases, SYMBOL must already be defined, and the value will be
|
---|
478 | adjusted accordingly.
|
---|
479 |
|
---|
480 | The special symbol name `.' indicates the location counter. You may
|
---|
481 | only use this within a `SECTIONS' command.
|
---|
482 |
|
---|
483 | The semicolon after EXPRESSION is required.
|
---|
484 |
|
---|
485 | Expressions are defined below; see *Note Expressions::.
|
---|
486 |
|
---|
487 | You may write symbol assignments as commands in their own right, or
|
---|
488 | as statements within a `SECTIONS' command, or as part of an output
|
---|
489 | section description in a `SECTIONS' command.
|
---|
490 |
|
---|
491 | The section of the symbol will be set from the section of the
|
---|
492 | expression; for more information, see *Note Expression Section::.
|
---|
493 |
|
---|
494 | Here is an example showing the three different places that symbol
|
---|
495 | assignments may be used:
|
---|
496 |
|
---|
497 | floating_point = 0;
|
---|
498 | SECTIONS
|
---|
499 | {
|
---|
500 | .text :
|
---|
501 | {
|
---|
502 | *(.text)
|
---|
503 | _etext = .;
|
---|
504 | }
|
---|
505 | _bdata = (. + 3) & ~ 3;
|
---|
506 | .data : { *(.data) }
|
---|
507 | }
|
---|
508 |
|
---|
509 | In this example, the symbol `floating_point' will be defined as zero.
|
---|
510 | The symbol `_etext' will be defined as the address following the last
|
---|
511 | `.text' input section. The symbol `_bdata' will be defined as the
|
---|
512 | address following the `.text' output section aligned upward to a 4 byte
|
---|
513 | boundary.
|
---|
514 |
|
---|
515 |
|
---|
516 | File: ld.info, Node: PROVIDE, Prev: Simple Assignments, Up: Assignments
|
---|
517 |
|
---|
518 | PROVIDE
|
---|
519 | -------
|
---|
520 |
|
---|
521 | In some cases, it is desirable for a linker script to define a symbol
|
---|
522 | only if it is referenced and is not defined by any object included in
|
---|
523 | the link. For example, traditional linkers defined the symbol `etext'.
|
---|
524 | However, ANSI C requires that the user be able to use `etext' as a
|
---|
525 | function name without encountering an error. The `PROVIDE' keyword may
|
---|
526 | be used to define a symbol, such as `etext', only if it is referenced
|
---|
527 | but not defined. The syntax is `PROVIDE(SYMBOL = EXPRESSION)'.
|
---|
528 |
|
---|
529 | Here is an example of using `PROVIDE' to define `etext':
|
---|
530 | SECTIONS
|
---|
531 | {
|
---|
532 | .text :
|
---|
533 | {
|
---|
534 | *(.text)
|
---|
535 | _etext = .;
|
---|
536 | PROVIDE(etext = .);
|
---|
537 | }
|
---|
538 | }
|
---|
539 |
|
---|
540 | In this example, if the program defines `_etext' (with a leading
|
---|
541 | underscore), the linker will give a multiple definition error. If, on
|
---|
542 | the other hand, the program defines `etext' (with no leading
|
---|
543 | underscore), the linker will silently use the definition in the program.
|
---|
544 | If the program references `etext' but does not define it, the linker
|
---|
545 | will use the definition in the linker script.
|
---|
546 |
|
---|
547 |
|
---|
548 | File: ld.info, Node: SECTIONS, Next: MEMORY, Prev: Assignments, Up: Scripts
|
---|
549 |
|
---|
550 | SECTIONS Command
|
---|
551 | ================
|
---|
552 |
|
---|
553 | The `SECTIONS' command tells the linker how to map input sections
|
---|
554 | into output sections, and how to place the output sections in memory.
|
---|
555 |
|
---|
556 | The format of the `SECTIONS' command is:
|
---|
557 | SECTIONS
|
---|
558 | {
|
---|
559 | SECTIONS-COMMAND
|
---|
560 | SECTIONS-COMMAND
|
---|
561 | ...
|
---|
562 | }
|
---|
563 |
|
---|
564 | Each SECTIONS-COMMAND may of be one of the following:
|
---|
565 |
|
---|
566 | * an `ENTRY' command (*note Entry command: Entry Point.)
|
---|
567 |
|
---|
568 | * a symbol assignment (*note Assignments::)
|
---|
569 |
|
---|
570 | * an output section description
|
---|
571 |
|
---|
572 | * an overlay description
|
---|
573 |
|
---|
574 | The `ENTRY' command and symbol assignments are permitted inside the
|
---|
575 | `SECTIONS' command for convenience in using the location counter in
|
---|
576 | those commands. This can also make the linker script easier to
|
---|
577 | understand because you can use those commands at meaningful points in
|
---|
578 | the layout of the output file.
|
---|
579 |
|
---|
580 | Output section descriptions and overlay descriptions are described
|
---|
581 | below.
|
---|
582 |
|
---|
583 | If you do not use a `SECTIONS' command in your linker script, the
|
---|
584 | linker will place each input section into an identically named output
|
---|
585 | section in the order that the sections are first encountered in the
|
---|
586 | input files. If all input sections are present in the first file, for
|
---|
587 | example, the order of sections in the output file will match the order
|
---|
588 | in the first input file. The first section will be at address zero.
|
---|
589 |
|
---|
590 | * Menu:
|
---|
591 |
|
---|
592 | * Output Section Description:: Output section description
|
---|
593 | * Output Section Name:: Output section name
|
---|
594 | * Output Section Address:: Output section address
|
---|
595 | * Input Section:: Input section description
|
---|
596 | * Output Section Data:: Output section data
|
---|
597 | * Output Section Keywords:: Output section keywords
|
---|
598 | * Output Section Discarding:: Output section discarding
|
---|
599 | * Output Section Attributes:: Output section attributes
|
---|
600 | * Overlay Description:: Overlay description
|
---|
601 |
|
---|
602 |
|
---|
603 | File: ld.info, Node: Output Section Description, Next: Output Section Name, Up: SECTIONS
|
---|
604 |
|
---|
605 | Output Section Description
|
---|
606 | --------------------------
|
---|
607 |
|
---|
608 | The full description of an output section looks like this:
|
---|
609 | SECTION [ADDRESS] [(TYPE)] : [AT(LMA)]
|
---|
610 | {
|
---|
611 | OUTPUT-SECTION-COMMAND
|
---|
612 | OUTPUT-SECTION-COMMAND
|
---|
613 | ...
|
---|
614 | } [>REGION] [AT>LMA_REGION] [:PHDR :PHDR ...] [=FILLEXP]
|
---|
615 |
|
---|
616 | Most output sections do not use most of the optional section
|
---|
617 | attributes.
|
---|
618 |
|
---|
619 | The whitespace around SECTION is required, so that the section name
|
---|
620 | is unambiguous. The colon and the curly braces are also required. The
|
---|
621 | line breaks and other white space are optional.
|
---|
622 |
|
---|
623 | Each OUTPUT-SECTION-COMMAND may be one of the following:
|
---|
624 |
|
---|
625 | * a symbol assignment (*note Assignments::)
|
---|
626 |
|
---|
627 | * an input section description (*note Input Section::)
|
---|
628 |
|
---|
629 | * data values to include directly (*note Output Section Data::)
|
---|
630 |
|
---|
631 | * a special output section keyword (*note Output Section Keywords::)
|
---|
632 |
|
---|
633 |
|
---|
634 | File: ld.info, Node: Output Section Name, Next: Output Section Address, Prev: Output Section Description, Up: SECTIONS
|
---|
635 |
|
---|
636 | Output Section Name
|
---|
637 | -------------------
|
---|
638 |
|
---|
639 | The name of the output section is SECTION. SECTION must meet the
|
---|
640 | constraints of your output format. In formats which only support a
|
---|
641 | limited number of sections, such as `a.out', the name must be one of
|
---|
642 | the names supported by the format (`a.out', for example, allows only
|
---|
643 | `.text', `.data' or `.bss'). If the output format supports any number
|
---|
644 | of sections, but with numbers and not names (as is the case for Oasys),
|
---|
645 | the name should be supplied as a quoted numeric string. A section name
|
---|
646 | may consist of any sequence of characters, but a name which contains
|
---|
647 | any unusual characters such as commas must be quoted.
|
---|
648 |
|
---|
649 | The output section name `/DISCARD/' is special; *Note Output Section
|
---|
650 | Discarding::.
|
---|
651 |
|
---|
652 |
|
---|
653 | File: ld.info, Node: Output Section Address, Next: Input Section, Prev: Output Section Name, Up: SECTIONS
|
---|
654 |
|
---|
655 | Output Section Description
|
---|
656 | --------------------------
|
---|
657 |
|
---|
658 | The ADDRESS is an expression for the VMA (the virtual memory
|
---|
659 | address) of the output section. If you do not provide ADDRESS, the
|
---|
660 | linker will set it based on REGION if present, or otherwise based on
|
---|
661 | the current value of the location counter.
|
---|
662 |
|
---|
663 | If you provide ADDRESS, the address of the output section will be
|
---|
664 | set to precisely that. If you provide neither ADDRESS nor REGION, then
|
---|
665 | the address of the output section will be set to the current value of
|
---|
666 | the location counter aligned to the alignment requirements of the
|
---|
667 | output section. The alignment requirement of the output section is the
|
---|
668 | strictest alignment of any input section contained within the output
|
---|
669 | section.
|
---|
670 |
|
---|
671 | For example,
|
---|
672 | .text . : { *(.text) }
|
---|
673 |
|
---|
674 | and
|
---|
675 | .text : { *(.text) }
|
---|
676 |
|
---|
677 | are subtly different. The first will set the address of the `.text'
|
---|
678 | output section to the current value of the location counter. The
|
---|
679 | second will set it to the current value of the location counter aligned
|
---|
680 | to the strictest alignment of a `.text' input section.
|
---|
681 |
|
---|
682 | The ADDRESS may be an arbitrary expression; *Note Expressions::.
|
---|
683 | For example, if you want to align the section on a 0x10 byte boundary,
|
---|
684 | so that the lowest four bits of the section address are zero, you could
|
---|
685 | do something like this:
|
---|
686 | .text ALIGN(0x10) : { *(.text) }
|
---|
687 |
|
---|
688 | This works because `ALIGN' returns the current location counter aligned
|
---|
689 | upward to the specified value.
|
---|
690 |
|
---|
691 | Specifying ADDRESS for a section will change the value of the
|
---|
692 | location counter.
|
---|
693 |
|
---|
694 |
|
---|
695 | File: ld.info, Node: Input Section, Next: Output Section Data, Prev: Output Section Address, Up: SECTIONS
|
---|
696 |
|
---|
697 | Input Section Description
|
---|
698 | -------------------------
|
---|
699 |
|
---|
700 | The most common output section command is an input section
|
---|
701 | description.
|
---|
702 |
|
---|
703 | The input section description is the most basic linker script
|
---|
704 | operation. You use output sections to tell the linker how to lay out
|
---|
705 | your program in memory. You use input section descriptions to tell the
|
---|
706 | linker how to map the input files into your memory layout.
|
---|
707 |
|
---|
708 | * Menu:
|
---|
709 |
|
---|
710 | * Input Section Basics:: Input section basics
|
---|
711 | * Input Section Wildcards:: Input section wildcard patterns
|
---|
712 | * Input Section Common:: Input section for common symbols
|
---|
713 | * Input Section Keep:: Input section and garbage collection
|
---|
714 | * Input Section Example:: Input section example
|
---|
715 |
|
---|
716 |
|
---|
717 | File: ld.info, Node: Input Section Basics, Next: Input Section Wildcards, Up: Input Section
|
---|
718 |
|
---|
719 | Input Section Basics
|
---|
720 | ....................
|
---|
721 |
|
---|
722 | An input section description consists of a file name optionally
|
---|
723 | followed by a list of section names in parentheses.
|
---|
724 |
|
---|
725 | The file name and the section name may be wildcard patterns, which we
|
---|
726 | describe further below (*note Input Section Wildcards::).
|
---|
727 |
|
---|
728 | The most common input section description is to include all input
|
---|
729 | sections with a particular name in the output section. For example, to
|
---|
730 | include all input `.text' sections, you would write:
|
---|
731 | *(.text)
|
---|
732 |
|
---|
733 | Here the `*' is a wildcard which matches any file name. To exclude a
|
---|
734 | list of files from matching the file name wildcard, EXCLUDE_FILE may be
|
---|
735 | used to match all files except the ones specified in the EXCLUDE_FILE
|
---|
736 | list. For example:
|
---|
737 | (*(EXCLUDE_FILE (*crtend.o *otherfile.o) .ctors))
|
---|
738 | will cause all .ctors sections from all files except `crtend.o' and
|
---|
739 | `otherfile.o' to be included.
|
---|
740 |
|
---|
741 | There are two ways to include more than one section:
|
---|
742 | *(.text .rdata)
|
---|
743 | *(.text) *(.rdata)
|
---|
744 |
|
---|
745 | The difference between these is the order in which the `.text' and
|
---|
746 | `.rdata' input sections will appear in the output section. In the
|
---|
747 | first example, they will be intermingled, appearing in the same order as
|
---|
748 | they are found in the linker input. In the second example, all `.text'
|
---|
749 | input sections will appear first, followed by all `.rdata' input
|
---|
750 | sections.
|
---|
751 |
|
---|
752 | You can specify a file name to include sections from a particular
|
---|
753 | file. You would do this if one or more of your files contain special
|
---|
754 | data that needs to be at a particular location in memory. For example:
|
---|
755 | data.o(.data)
|
---|
756 |
|
---|
757 | If you use a file name without a list of sections, then all sections
|
---|
758 | in the input file will be included in the output section. This is not
|
---|
759 | commonly done, but it may by useful on occasion. For example:
|
---|
760 | data.o
|
---|
761 |
|
---|
762 | When you use a file name which does not contain any wild card
|
---|
763 | characters, the linker will first see if you also specified the file
|
---|
764 | name on the linker command line or in an `INPUT' command. If you did
|
---|
765 | not, the linker will attempt to open the file as an input file, as
|
---|
766 | though it appeared on the command line. Note that this differs from an
|
---|
767 | `INPUT' command, because the linker will not search for the file in the
|
---|
768 | archive search path.
|
---|
769 |
|
---|
770 |
|
---|
771 | File: ld.info, Node: Input Section Wildcards, Next: Input Section Common, Prev: Input Section Basics, Up: Input Section
|
---|
772 |
|
---|
773 | Input Section Wildcard Patterns
|
---|
774 | ...............................
|
---|
775 |
|
---|
776 | In an input section description, either the file name or the section
|
---|
777 | name or both may be wildcard patterns.
|
---|
778 |
|
---|
779 | The file name of `*' seen in many examples is a simple wildcard
|
---|
780 | pattern for the file name.
|
---|
781 |
|
---|
782 | The wildcard patterns are like those used by the Unix shell.
|
---|
783 |
|
---|
784 | `*'
|
---|
785 | matches any number of characters
|
---|
786 |
|
---|
787 | `?'
|
---|
788 | matches any single character
|
---|
789 |
|
---|
790 | `[CHARS]'
|
---|
791 | matches a single instance of any of the CHARS; the `-' character
|
---|
792 | may be used to specify a range of characters, as in `[a-z]' to
|
---|
793 | match any lower case letter
|
---|
794 |
|
---|
795 | `\'
|
---|
796 | quotes the following character
|
---|
797 |
|
---|
798 | When a file name is matched with a wildcard, the wildcard characters
|
---|
799 | will not match a `/' character (used to separate directory names on
|
---|
800 | Unix). A pattern consisting of a single `*' character is an exception;
|
---|
801 | it will always match any file name, whether it contains a `/' or not.
|
---|
802 | In a section name, the wildcard characters will match a `/' character.
|
---|
803 |
|
---|
804 | File name wildcard patterns only match files which are explicitly
|
---|
805 | specified on the command line or in an `INPUT' command. The linker
|
---|
806 | does not search directories to expand wildcards.
|
---|
807 |
|
---|
808 | If a file name matches more than one wildcard pattern, or if a file
|
---|
809 | name appears explicitly and is also matched by a wildcard pattern, the
|
---|
810 | linker will use the first match in the linker script. For example, this
|
---|
811 | sequence of input section descriptions is probably in error, because the
|
---|
812 | `data.o' rule will not be used:
|
---|
813 | .data : { *(.data) }
|
---|
814 | .data1 : { data.o(.data) }
|
---|
815 |
|
---|
816 | Normally, the linker will place files and sections matched by
|
---|
817 | wildcards in the order in which they are seen during the link. You can
|
---|
818 | change this by using the `SORT' keyword, which appears before a wildcard
|
---|
819 | pattern in parentheses (e.g., `SORT(.text*)'). When the `SORT' keyword
|
---|
820 | is used, the linker will sort the files or sections into ascending
|
---|
821 | order by name before placing them in the output file.
|
---|
822 |
|
---|
823 | If you ever get confused about where input sections are going, use
|
---|
824 | the `-M' linker option to generate a map file. The map file shows
|
---|
825 | precisely how input sections are mapped to output sections.
|
---|
826 |
|
---|
827 | This example shows how wildcard patterns might be used to partition
|
---|
828 | files. This linker script directs the linker to place all `.text'
|
---|
829 | sections in `.text' and all `.bss' sections in `.bss'. The linker will
|
---|
830 | place the `.data' section from all files beginning with an upper case
|
---|
831 | character in `.DATA'; for all other files, the linker will place the
|
---|
832 | `.data' section in `.data'.
|
---|
833 | SECTIONS {
|
---|
834 | .text : { *(.text) }
|
---|
835 | .DATA : { [A-Z]*(.data) }
|
---|
836 | .data : { *(.data) }
|
---|
837 | .bss : { *(.bss) }
|
---|
838 | }
|
---|
839 |
|
---|
840 |
|
---|
841 | File: ld.info, Node: Input Section Common, Next: Input Section Keep, Prev: Input Section Wildcards, Up: Input Section
|
---|
842 |
|
---|
843 | Input Section for Common Symbols
|
---|
844 | ................................
|
---|
845 |
|
---|
846 | A special notation is needed for common symbols, because in many
|
---|
847 | object file formats common symbols do not have a particular input
|
---|
848 | section. The linker treats common symbols as though they are in an
|
---|
849 | input section named `COMMON'.
|
---|
850 |
|
---|
851 | You may use file names with the `COMMON' section just as with any
|
---|
852 | other input sections. You can use this to place common symbols from a
|
---|
853 | particular input file in one section while common symbols from other
|
---|
854 | input files are placed in another section.
|
---|
855 |
|
---|
856 | In most cases, common symbols in input files will be placed in the
|
---|
857 | `.bss' section in the output file. For example:
|
---|
858 | .bss { *(.bss) *(COMMON) }
|
---|
859 |
|
---|
860 | Some object file formats have more than one type of common symbol.
|
---|
861 | For example, the MIPS ELF object file format distinguishes standard
|
---|
862 | common symbols and small common symbols. In this case, the linker will
|
---|
863 | use a different special section name for other types of common symbols.
|
---|
864 | In the case of MIPS ELF, the linker uses `COMMON' for standard common
|
---|
865 | symbols and `.scommon' for small common symbols. This permits you to
|
---|
866 | map the different types of common symbols into memory at different
|
---|
867 | locations.
|
---|
868 |
|
---|
869 | You will sometimes see `[COMMON]' in old linker scripts. This
|
---|
870 | notation is now considered obsolete. It is equivalent to `*(COMMON)'.
|
---|
871 |
|
---|
872 |
|
---|
873 | File: ld.info, Node: Input Section Keep, Next: Input Section Example, Prev: Input Section Common, Up: Input Section
|
---|
874 |
|
---|
875 | Input Section and Garbage Collection
|
---|
876 | ....................................
|
---|
877 |
|
---|
878 | When link-time garbage collection is in use (`--gc-sections'), it is
|
---|
879 | often useful to mark sections that should not be eliminated. This is
|
---|
880 | accomplished by surrounding an input section's wildcard entry with
|
---|
881 | `KEEP()', as in `KEEP(*(.init))' or `KEEP(SORT(*)(.ctors))'.
|
---|
882 |
|
---|
883 |
|
---|
884 | File: ld.info, Node: Input Section Example, Prev: Input Section Keep, Up: Input Section
|
---|
885 |
|
---|
886 | Input Section Example
|
---|
887 | .....................
|
---|
888 |
|
---|
889 | The following example is a complete linker script. It tells the
|
---|
890 | linker to read all of the sections from file `all.o' and place them at
|
---|
891 | the start of output section `outputa' which starts at location
|
---|
892 | `0x10000'. All of section `.input1' from file `foo.o' follows
|
---|
893 | immediately, in the same output section. All of section `.input2' from
|
---|
894 | `foo.o' goes into output section `outputb', followed by section
|
---|
895 | `.input1' from `foo1.o'. All of the remaining `.input1' and `.input2'
|
---|
896 | sections from any files are written to output section `outputc'.
|
---|
897 |
|
---|
898 | SECTIONS {
|
---|
899 | outputa 0x10000 :
|
---|
900 | {
|
---|
901 | all.o
|
---|
902 | foo.o (.input1)
|
---|
903 | }
|
---|
904 | outputb :
|
---|
905 | {
|
---|
906 | foo.o (.input2)
|
---|
907 | foo1.o (.input1)
|
---|
908 | }
|
---|
909 | outputc :
|
---|
910 | {
|
---|
911 | *(.input1)
|
---|
912 | *(.input2)
|
---|
913 | }
|
---|
914 | }
|
---|
915 |
|
---|
916 |
|
---|
917 | File: ld.info, Node: Output Section Data, Next: Output Section Keywords, Prev: Input Section, Up: SECTIONS
|
---|
918 |
|
---|
919 | Output Section Data
|
---|
920 | -------------------
|
---|
921 |
|
---|
922 | You can include explicit bytes of data in an output section by using
|
---|
923 | `BYTE', `SHORT', `LONG', `QUAD', or `SQUAD' as an output section
|
---|
924 | command. Each keyword is followed by an expression in parentheses
|
---|
925 | providing the value to store (*note Expressions::). The value of the
|
---|
926 | expression is stored at the current value of the location counter.
|
---|
927 |
|
---|
928 | The `BYTE', `SHORT', `LONG', and `QUAD' commands store one, two,
|
---|
929 | four, and eight bytes (respectively). After storing the bytes, the
|
---|
930 | location counter is incremented by the number of bytes stored.
|
---|
931 |
|
---|
932 | For example, this will store the byte 1 followed by the four byte
|
---|
933 | value of the symbol `addr':
|
---|
934 | BYTE(1)
|
---|
935 | LONG(addr)
|
---|
936 |
|
---|
937 | When using a 64 bit host or target, `QUAD' and `SQUAD' are the same;
|
---|
938 | they both store an 8 byte, or 64 bit, value. When both host and target
|
---|
939 | are 32 bits, an expression is computed as 32 bits. In this case `QUAD'
|
---|
940 | stores a 32 bit value zero extended to 64 bits, and `SQUAD' stores a 32
|
---|
941 | bit value sign extended to 64 bits.
|
---|
942 |
|
---|
943 | If the object file format of the output file has an explicit
|
---|
944 | endianness, which is the normal case, the value will be stored in that
|
---|
945 | endianness. When the object file format does not have an explicit
|
---|
946 | endianness, as is true of, for example, S-records, the value will be
|
---|
947 | stored in the endianness of the first input object file.
|
---|
948 |
|
---|
949 | Note--these commands only work inside a section description and not
|
---|
950 | between them, so the following will produce an error from the linker:
|
---|
951 | SECTIONS { .text : { *(.text) } LONG(1) .data : { *(.data) } }
|
---|
952 | whereas this will work:
|
---|
953 | SECTIONS { .text : { *(.text) ; LONG(1) } .data : { *(.data) } }
|
---|
954 |
|
---|
955 | You may use the `FILL' command to set the fill pattern for the
|
---|
956 | current section. It is followed by an expression in parentheses. Any
|
---|
957 | otherwise unspecified regions of memory within the section (for example,
|
---|
958 | gaps left due to the required alignment of input sections) are filled
|
---|
959 | with the value of the expression, repeated as necessary. A `FILL'
|
---|
960 | statement covers memory locations after the point at which it occurs in
|
---|
961 | the section definition; by including more than one `FILL' statement,
|
---|
962 | you can have different fill patterns in different parts of an output
|
---|
963 | section.
|
---|
964 |
|
---|
965 | This example shows how to fill unspecified regions of memory with the
|
---|
966 | value `0x90':
|
---|
967 | FILL(0x90909090)
|
---|
968 |
|
---|
969 | The `FILL' command is similar to the `=FILLEXP' output section
|
---|
970 | attribute, but it only affects the part of the section following the
|
---|
971 | `FILL' command, rather than the entire section. If both are used, the
|
---|
972 | `FILL' command takes precedence. *Note Output Section Fill::, for
|
---|
973 | details on the fill expression.
|
---|
974 |
|
---|
975 |
|
---|
976 | File: ld.info, Node: Output Section Keywords, Next: Output Section Discarding, Prev: Output Section Data, Up: SECTIONS
|
---|
977 |
|
---|
978 | Output Section Keywords
|
---|
979 | -----------------------
|
---|
980 |
|
---|
981 | There are a couple of keywords which can appear as output section
|
---|
982 | commands.
|
---|
983 |
|
---|
984 | `CREATE_OBJECT_SYMBOLS'
|
---|
985 | The command tells the linker to create a symbol for each input
|
---|
986 | file. The name of each symbol will be the name of the
|
---|
987 | corresponding input file. The section of each symbol will be the
|
---|
988 | output section in which the `CREATE_OBJECT_SYMBOLS' command
|
---|
989 | appears.
|
---|
990 |
|
---|
991 | This is conventional for the a.out object file format. It is not
|
---|
992 | normally used for any other object file format.
|
---|
993 |
|
---|
994 | `CONSTRUCTORS'
|
---|
995 | When linking using the a.out object file format, the linker uses an
|
---|
996 | unusual set construct to support C++ global constructors and
|
---|
997 | destructors. When linking object file formats which do not support
|
---|
998 | arbitrary sections, such as ECOFF and XCOFF, the linker will
|
---|
999 | automatically recognize C++ global constructors and destructors by
|
---|
1000 | name. For these object file formats, the `CONSTRUCTORS' command
|
---|
1001 | tells the linker to place constructor information in the output
|
---|
1002 | section where the `CONSTRUCTORS' command appears. The
|
---|
1003 | `CONSTRUCTORS' command is ignored for other object file formats.
|
---|
1004 |
|
---|
1005 | The symbol `__CTOR_LIST__' marks the start of the global
|
---|
1006 | constructors, and the symbol `__DTOR_LIST' marks the end. The
|
---|
1007 | first word in the list is the number of entries, followed by the
|
---|
1008 | address of each constructor or destructor, followed by a zero
|
---|
1009 | word. The compiler must arrange to actually run the code. For
|
---|
1010 | these object file formats GNU C++ normally calls constructors from
|
---|
1011 | a subroutine `__main'; a call to `__main' is automatically
|
---|
1012 | inserted into the startup code for `main'. GNU C++ normally runs
|
---|
1013 | destructors either by using `atexit', or directly from the function
|
---|
1014 | `exit'.
|
---|
1015 |
|
---|
1016 | For object file formats such as `COFF' or `ELF' which support
|
---|
1017 | arbitrary section names, GNU C++ will normally arrange to put the
|
---|
1018 | addresses of global constructors and destructors into the `.ctors'
|
---|
1019 | and `.dtors' sections. Placing the following sequence into your
|
---|
1020 | linker script will build the sort of table which the GNU C++
|
---|
1021 | runtime code expects to see.
|
---|
1022 |
|
---|
1023 | __CTOR_LIST__ = .;
|
---|
1024 | LONG((__CTOR_END__ - __CTOR_LIST__) / 4 - 2)
|
---|
1025 | *(.ctors)
|
---|
1026 | LONG(0)
|
---|
1027 | __CTOR_END__ = .;
|
---|
1028 | __DTOR_LIST__ = .;
|
---|
1029 | LONG((__DTOR_END__ - __DTOR_LIST__) / 4 - 2)
|
---|
1030 | *(.dtors)
|
---|
1031 | LONG(0)
|
---|
1032 | __DTOR_END__ = .;
|
---|
1033 |
|
---|
1034 | If you are using the GNU C++ support for initialization priority,
|
---|
1035 | which provides some control over the order in which global
|
---|
1036 | constructors are run, you must sort the constructors at link time
|
---|
1037 | to ensure that they are executed in the correct order. When using
|
---|
1038 | the `CONSTRUCTORS' command, use `SORT(CONSTRUCTORS)' instead.
|
---|
1039 | When using the `.ctors' and `.dtors' sections, use
|
---|
1040 | `*(SORT(.ctors))' and `*(SORT(.dtors))' instead of just
|
---|
1041 | `*(.ctors)' and `*(.dtors)'.
|
---|
1042 |
|
---|
1043 | Normally the compiler and linker will handle these issues
|
---|
1044 | automatically, and you will not need to concern yourself with
|
---|
1045 | them. However, you may need to consider this if you are using C++
|
---|
1046 | and writing your own linker scripts.
|
---|
1047 |
|
---|
1048 |
|
---|
1049 | File: ld.info, Node: Output Section Discarding, Next: Output Section Attributes, Prev: Output Section Keywords, Up: SECTIONS
|
---|
1050 |
|
---|
1051 | Output Section Discarding
|
---|
1052 | -------------------------
|
---|
1053 |
|
---|
1054 | The linker will not create output section which do not have any
|
---|
1055 | contents. This is for convenience when referring to input sections that
|
---|
1056 | may or may not be present in any of the input files. For example:
|
---|
1057 | .foo { *(.foo) }
|
---|
1058 |
|
---|
1059 | will only create a `.foo' section in the output file if there is a
|
---|
1060 | `.foo' section in at least one input file.
|
---|
1061 |
|
---|
1062 | If you use anything other than an input section description as an
|
---|
1063 | output section command, such as a symbol assignment, then the output
|
---|
1064 | section will always be created, even if there are no matching input
|
---|
1065 | sections.
|
---|
1066 |
|
---|
1067 | The special output section name `/DISCARD/' may be used to discard
|
---|
1068 | input sections. Any input sections which are assigned to an output
|
---|
1069 | section named `/DISCARD/' are not included in the output file.
|
---|
1070 |
|
---|
1071 |
|
---|
1072 | File: ld.info, Node: Output Section Attributes, Next: Overlay Description, Prev: Output Section Discarding, Up: SECTIONS
|
---|
1073 |
|
---|
1074 | Output Section Attributes
|
---|
1075 | -------------------------
|
---|
1076 |
|
---|
1077 | We showed above that the full description of an output section looked
|
---|
1078 | like this:
|
---|
1079 | SECTION [ADDRESS] [(TYPE)] : [AT(LMA)]
|
---|
1080 | {
|
---|
1081 | OUTPUT-SECTION-COMMAND
|
---|
1082 | OUTPUT-SECTION-COMMAND
|
---|
1083 | ...
|
---|
1084 | } [>REGION] [AT>LMA_REGION] [:PHDR :PHDR ...] [=FILLEXP]
|
---|
1085 | We've already described SECTION, ADDRESS, and
|
---|
1086 | OUTPUT-SECTION-COMMAND. In this section we will describe the remaining
|
---|
1087 | section attributes.
|
---|
1088 |
|
---|
1089 | * Menu:
|
---|
1090 |
|
---|
1091 | * Output Section Type:: Output section type
|
---|
1092 | * Output Section LMA:: Output section LMA
|
---|
1093 | * Output Section Region:: Output section region
|
---|
1094 | * Output Section Phdr:: Output section phdr
|
---|
1095 | * Output Section Fill:: Output section fill
|
---|
1096 |
|
---|
1097 |
|
---|
1098 | File: ld.info, Node: Output Section Type, Next: Output Section LMA, Up: Output Section Attributes
|
---|
1099 |
|
---|
1100 | Output Section Type
|
---|
1101 | ...................
|
---|
1102 |
|
---|
1103 | Each output section may have a type. The type is a keyword in
|
---|
1104 | parentheses. The following types are defined:
|
---|
1105 |
|
---|
1106 | `NOLOAD'
|
---|
1107 | The section should be marked as not loadable, so that it will not
|
---|
1108 | be loaded into memory when the program is run.
|
---|
1109 |
|
---|
1110 | `DSECT'
|
---|
1111 | `COPY'
|
---|
1112 | `INFO'
|
---|
1113 | `OVERLAY'
|
---|
1114 | These type names are supported for backward compatibility, and are
|
---|
1115 | rarely used. They all have the same effect: the section should be
|
---|
1116 | marked as not allocatable, so that no memory is allocated for the
|
---|
1117 | section when the program is run.
|
---|
1118 |
|
---|
1119 | The linker normally sets the attributes of an output section based on
|
---|
1120 | the input sections which map into it. You can override this by using
|
---|
1121 | the section type. For example, in the script sample below, the `ROM'
|
---|
1122 | section is addressed at memory location `0' and does not need to be
|
---|
1123 | loaded when the program is run. The contents of the `ROM' section will
|
---|
1124 | appear in the linker output file as usual.
|
---|
1125 | SECTIONS {
|
---|
1126 | ROM 0 (NOLOAD) : { ... }
|
---|
1127 | ...
|
---|
1128 | }
|
---|
1129 |
|
---|
1130 |
|
---|
1131 | File: ld.info, Node: Output Section LMA, Next: Output Section Region, Prev: Output Section Type, Up: Output Section Attributes
|
---|
1132 |
|
---|
1133 | Output Section LMA
|
---|
1134 | ..................
|
---|
1135 |
|
---|
1136 | Every section has a virtual address (VMA) and a load address (LMA);
|
---|
1137 | see *Note Basic Script Concepts::. The address expression which may
|
---|
1138 | appear in an output section description sets the VMA (*note Output
|
---|
1139 | Section Address::).
|
---|
1140 |
|
---|
1141 | The linker will normally set the LMA equal to the VMA. You can
|
---|
1142 | change that by using the `AT' keyword. The expression LMA that follows
|
---|
1143 | the `AT' keyword specifies the load address of the section.
|
---|
1144 | Alternatively, with `AT>LMA_REGION' expression, you may specify a
|
---|
1145 | memory region for the section's load address. *Note MEMORY::.
|
---|
1146 |
|
---|
1147 | This feature is designed to make it easy to build a ROM image. For
|
---|
1148 | example, the following linker script creates three output sections: one
|
---|
1149 | called `.text', which starts at `0x1000', one called `.mdata', which is
|
---|
1150 | loaded at the end of the `.text' section even though its VMA is
|
---|
1151 | `0x2000', and one called `.bss' to hold uninitialized data at address
|
---|
1152 | `0x3000'. The symbol `_data' is defined with the value `0x2000', which
|
---|
1153 | shows that the location counter holds the VMA value, not the LMA value.
|
---|
1154 |
|
---|
1155 | SECTIONS
|
---|
1156 | {
|
---|
1157 | .text 0x1000 : { *(.text) _etext = . ; }
|
---|
1158 | .mdata 0x2000 :
|
---|
1159 | AT ( ADDR (.text) + SIZEOF (.text) )
|
---|
1160 | { _data = . ; *(.data); _edata = . ; }
|
---|
1161 | .bss 0x3000 :
|
---|
1162 | { _bstart = . ; *(.bss) *(COMMON) ; _bend = . ;}
|
---|
1163 | }
|
---|
1164 |
|
---|
1165 | The run-time initialization code for use with a program generated
|
---|
1166 | with this linker script would include something like the following, to
|
---|
1167 | copy the initialized data from the ROM image to its runtime address.
|
---|
1168 | Notice how this code takes advantage of the symbols defined by the
|
---|
1169 | linker script.
|
---|
1170 |
|
---|
1171 | extern char _etext, _data, _edata, _bstart, _bend;
|
---|
1172 | char *src = &_etext;
|
---|
1173 | char *dst = &_data;
|
---|
1174 |
|
---|
1175 | /* ROM has data at end of text; copy it. */
|
---|
1176 | while (dst < &_edata) {
|
---|
1177 | *dst++ = *src++;
|
---|
1178 | }
|
---|
1179 |
|
---|
1180 | /* Zero bss */
|
---|
1181 | for (dst = &_bstart; dst< &_bend; dst++)
|
---|
1182 | *dst = 0;
|
---|
1183 |
|
---|
1184 |
|
---|
1185 | File: ld.info, Node: Output Section Region, Next: Output Section Phdr, Prev: Output Section LMA, Up: Output Section Attributes
|
---|
1186 |
|
---|
1187 | Output Section Region
|
---|
1188 | .....................
|
---|
1189 |
|
---|
1190 | You can assign a section to a previously defined region of memory by
|
---|
1191 | using `>REGION'. *Note MEMORY::.
|
---|
1192 |
|
---|
1193 | Here is a simple example:
|
---|
1194 | MEMORY { rom : ORIGIN = 0x1000, LENGTH = 0x1000 }
|
---|
1195 | SECTIONS { ROM : { *(.text) } >rom }
|
---|
1196 |
|
---|
1197 |
|
---|
1198 | File: ld.info, Node: Output Section Phdr, Next: Output Section Fill, Prev: Output Section Region, Up: Output Section Attributes
|
---|
1199 |
|
---|
1200 | Output Section Phdr
|
---|
1201 | ...................
|
---|
1202 |
|
---|
1203 | You can assign a section to a previously defined program segment by
|
---|
1204 | using `:PHDR'. *Note PHDRS::. If a section is assigned to one or more
|
---|
1205 | segments, then all subsequent allocated sections will be assigned to
|
---|
1206 | those segments as well, unless they use an explicitly `:PHDR' modifier.
|
---|
1207 | You can use `:NONE' to tell the linker to not put the section in any
|
---|
1208 | segment at all.
|
---|
1209 |
|
---|
1210 | Here is a simple example:
|
---|
1211 | PHDRS { text PT_LOAD ; }
|
---|
1212 | SECTIONS { .text : { *(.text) } :text }
|
---|
1213 |
|
---|
1214 |
|
---|
1215 | File: ld.info, Node: Output Section Fill, Prev: Output Section Phdr, Up: Output Section Attributes
|
---|
1216 |
|
---|
1217 | Output Section Fill
|
---|
1218 | ...................
|
---|
1219 |
|
---|
1220 | You can set the fill pattern for an entire section by using
|
---|
1221 | `=FILLEXP'. FILLEXP is an expression (*note Expressions::). Any
|
---|
1222 | otherwise unspecified regions of memory within the output section (for
|
---|
1223 | example, gaps left due to the required alignment of input sections)
|
---|
1224 | will be filled with the value, repeated as necessary. If the fill
|
---|
1225 | expression is a simple hex number, ie. a string of hex digit starting
|
---|
1226 | with `0x' and without a trailing `k' or `M', then an arbitrarily long
|
---|
1227 | sequence of hex digits can be used to specify the fill pattern;
|
---|
1228 | Leading zeros become part of the pattern too. For all other cases,
|
---|
1229 | including extra parentheses or a unary `+', the fill pattern is the
|
---|
1230 | four least significant bytes of the value of the expression. In all
|
---|
1231 | cases, the number is big-endian.
|
---|
1232 |
|
---|
1233 | You can also change the fill value with a `FILL' command in the
|
---|
1234 | output section commands; (*note Output Section Data::).
|
---|
1235 |
|
---|
1236 | Here is a simple example:
|
---|
1237 | SECTIONS { .text : { *(.text) } =0x90909090 }
|
---|
1238 |
|
---|