]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - contrib/binutils/ld/ldint.texinfo
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / contrib / binutils / ld / ldint.texinfo
1 \input texinfo
2 @setfilename ldint.info
3 @c Copyright 1992, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
4 @c 2003, 2007
5 @c Free Software Foundation, Inc.
6
7 @ifinfo
8 @format
9 START-INFO-DIR-ENTRY
10 * Ld-Internals: (ldint).        The GNU linker internals.
11 END-INFO-DIR-ENTRY
12 @end format
13 @end ifinfo
14
15 @copying
16 This file documents the internals of the GNU linker ld.
17
18 Copyright @copyright{} 1992, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2007
19 Free Software Foundation, Inc.
20 Contributed by Cygnus Support.
21
22 Permission is granted to copy, distribute and/or modify this document
23 under the terms of the GNU Free Documentation License, Version 1.1 or
24 any later version published by the Free Software Foundation; with the
25 Invariant Sections being ``GNU General Public License'' and ``Funding
26 Free Software'', the Front-Cover texts being (a) (see below), and with
27 the Back-Cover Texts being (b) (see below).  A copy of the license is
28 included in the section entitled ``GNU Free Documentation License''.
29
30 (a) The FSF's Front-Cover Text is:
31
32      A GNU Manual
33
34 (b) The FSF's Back-Cover Text is:
35
36      You have freedom to copy and modify this GNU Manual, like GNU
37      software.  Copies published by the Free Software Foundation raise
38      funds for GNU development.
39 @end copying
40
41 @iftex
42 @finalout
43 @setchapternewpage off
44 @settitle GNU Linker Internals
45 @titlepage
46 @title{A guide to the internals of the GNU linker}
47 @author Per Bothner, Steve Chamberlain, Ian Lance Taylor, DJ Delorie
48 @author Cygnus Support
49 @page
50
51 @tex
52 \def\$#1${{#1}}  % Kluge: collect RCS revision info without $...$
53 \xdef\manvers{2.10.91}  % For use in headers, footers too
54 {\parskip=0pt
55 \hfill Cygnus Support\par
56 \hfill \manvers\par
57 \hfill \TeX{}info \texinfoversion\par
58 }
59 @end tex
60
61 @vskip 0pt plus 1filll
62 Copyright @copyright{} 1992, 93, 94, 95, 96, 97, 1998, 2000
63 Free Software Foundation, Inc.
64
65       Permission is granted to copy, distribute and/or modify this document
66       under the terms of the GNU Free Documentation License, Version 1.1
67       or any later version published by the Free Software Foundation;
68       with no Invariant Sections, with no Front-Cover Texts, and with no
69       Back-Cover Texts.  A copy of the license is included in the
70       section entitled "GNU Free Documentation License".
71
72 @end titlepage
73 @end iftex
74
75 @node Top
76 @top
77
78 This file documents the internals of the GNU linker @code{ld}.  It is a
79 collection of miscellaneous information with little form at this point.
80 Mostly, it is a repository into which you can put information about
81 GNU @code{ld} as you discover it (or as you design changes to @code{ld}).
82
83 This document is distributed under the terms of the GNU Free
84 Documentation License.  A copy of the license is included in the
85 section entitled "GNU Free Documentation License".
86
87 @menu
88 * README::                      The README File
89 * Emulations::                  How linker emulations are generated
90 * Emulation Walkthrough::       A Walkthrough of a Typical Emulation
91 * Architecture Specific::       Some Architecture Specific Notes
92 * GNU Free Documentation License::  GNU Free Documentation License
93 @end menu
94
95 @node README
96 @chapter The @file{README} File
97
98 Check the @file{README} file; it often has useful information that does not
99 appear anywhere else in the directory.
100
101 @node Emulations
102 @chapter How linker emulations are generated
103
104 Each linker target has an @dfn{emulation}.  The emulation includes the
105 default linker script, and certain emulations also modify certain types
106 of linker behaviour.
107
108 Emulations are created during the build process by the shell script
109 @file{genscripts.sh}.
110
111 The @file{genscripts.sh} script starts by reading a file in the
112 @file{emulparams} directory.  This is a shell script which sets various
113 shell variables used by @file{genscripts.sh} and the other shell scripts
114 it invokes.
115
116 The @file{genscripts.sh} script will invoke a shell script in the
117 @file{scripttempl} directory in order to create default linker scripts
118 written in the linker command language.  The @file{scripttempl} script
119 will be invoked 5 (or, in some cases, 6) times, with different
120 assignments to shell variables, to create different default scripts.
121 The choice of script is made based on the command line options.
122
123 After creating the scripts, @file{genscripts.sh} will invoke yet another
124 shell script, this time in the @file{emultempl} directory.  That shell
125 script will create the emulation source file, which contains C code.
126 This C code permits the linker emulation to override various linker
127 behaviours.  Most targets use the generic emulation code, which is in
128 @file{emultempl/generic.em}.
129
130 To summarize, @file{genscripts.sh} reads three shell scripts: an
131 emulation parameters script in the @file{emulparams} directory, a linker
132 script generation script in the @file{scripttempl} directory, and an
133 emulation source file generation script in the @file{emultempl}
134 directory.
135
136 For example, the Sun 4 linker sets up variables in
137 @file{emulparams/sun4.sh}, creates linker scripts using
138 @file{scripttempl/aout.sc}, and creates the emulation code using
139 @file{emultempl/sunos.em}.
140
141 Note that the linker can support several emulations simultaneously,
142 depending upon how it is configured.  An emulation can be selected with
143 the @code{-m} option.  The @code{-V} option will list all supported
144 emulations.
145
146 @menu
147 * emulation parameters::        @file{emulparams} scripts
148 * linker scripts::              @file{scripttempl} scripts
149 * linker emulations::           @file{emultempl} scripts
150 @end menu
151
152 @node emulation parameters
153 @section @file{emulparams} scripts
154
155 Each target selects a particular file in the @file{emulparams} directory
156 by setting the shell variable @code{targ_emul} in @file{configure.tgt}.
157 This shell variable is used by the @file{configure} script to control
158 building an emulation source file.
159
160 Certain conventions are enforced.  Suppose the @code{targ_emul} variable
161 is set to @var{emul} in @file{configure.tgt}.  The name of the emulation
162 shell script will be @file{emulparams/@var{emul}.sh}.  The
163 @file{Makefile} must have a target named @file{e@var{emul}.c}; this
164 target must depend upon @file{emulparams/@var{emul}.sh}, as well as the
165 appropriate scripts in the @file{scripttempl} and @file{emultempl}
166 directories.  The @file{Makefile} target must invoke @code{GENSCRIPTS}
167 with two arguments: @var{emul}, and the value of the make variable
168 @code{tdir_@var{emul}}.  The value of the latter variable will be set by
169 the @file{configure} script, and is used to set the default target
170 directory to search.
171
172 By convention, the @file{emulparams/@var{emul}.sh} shell script should
173 only set shell variables.  It may set shell variables which are to be
174 interpreted by the @file{scripttempl} and the @file{emultempl} scripts.
175 Certain shell variables are interpreted directly by the
176 @file{genscripts.sh} script.
177
178 Here is a list of shell variables interpreted by @file{genscripts.sh},
179 as well as some conventional shell variables interpreted by the
180 @file{scripttempl} and @file{emultempl} scripts.
181
182 @table @code
183 @item SCRIPT_NAME
184 This is the name of the @file{scripttempl} script to use.  If
185 @code{SCRIPT_NAME} is set to @var{script}, @file{genscripts.sh} will use
186 the script @file{scripttempl/@var{script}.sc}.
187
188 @item TEMPLATE_NAME
189 This is the name of the @file{emultempl} script to use.  If
190 @code{TEMPLATE_NAME} is set to @var{template}, @file{genscripts.sh} will
191 use the script @file{emultempl/@var{template}.em}.  If this variable is
192 not set, the default value is @samp{generic}.
193
194 @item GENERATE_SHLIB_SCRIPT
195 If this is set to a nonempty string, @file{genscripts.sh} will invoke
196 the @file{scripttempl} script an extra time to create a shared library
197 script.  @ref{linker scripts}.
198
199 @item OUTPUT_FORMAT
200 This is normally set to indicate the BFD output format use (e.g.,
201 @samp{"a.out-sunos-big"}.  The @file{scripttempl} script will normally
202 use it in an @code{OUTPUT_FORMAT} expression in the linker script.
203
204 @item ARCH
205 This is normally set to indicate the architecture to use (e.g.,
206 @samp{sparc}).  The @file{scripttempl} script will normally use it in an
207 @code{OUTPUT_ARCH} expression in the linker script.
208
209 @item ENTRY
210 Some @file{scripttempl} scripts use this to set the entry address, in an
211 @code{ENTRY} expression in the linker script.
212
213 @item TEXT_START_ADDR
214 Some @file{scripttempl} scripts use this to set the start address of the
215 @samp{.text} section.
216
217 @item NONPAGED_TEXT_START_ADDR
218 If this is defined, the @file{genscripts.sh} script sets
219 @code{TEXT_START_ADDR} to its value before running the
220 @file{scripttempl} script for the @code{-n} and @code{-N} options
221 (@pxref{linker scripts}).
222
223 @item SEGMENT_SIZE
224 The @file{genscripts.sh} script uses this to set the default value of
225 @code{DATA_ALIGNMENT} when running the @file{scripttempl} script.
226
227 @item TARGET_PAGE_SIZE
228 If @code{SEGMENT_SIZE} is not defined, the @file{genscripts.sh} script
229 uses this to define it.
230
231 @item ALIGNMENT
232 Some @file{scripttempl} scripts set this to a number to pass to
233 @code{ALIGN} to set the required alignment for the @code{end} symbol.
234 @end table
235
236 @node linker scripts
237 @section @file{scripttempl} scripts
238
239 Each linker target uses a @file{scripttempl} script to generate the
240 default linker scripts.  The name of the @file{scripttempl} script is
241 set by the @code{SCRIPT_NAME} variable in the @file{emulparams} script.
242 If @code{SCRIPT_NAME} is set to @var{script}, @code{genscripts.sh} will
243 invoke @file{scripttempl/@var{script}.sc}.
244
245 The @file{genscripts.sh} script will invoke the @file{scripttempl}
246 script 5 to 8 times.  Each time it will set the shell variable
247 @code{LD_FLAG} to a different value.  When the linker is run, the
248 options used will direct it to select a particular script.  (Script
249 selection is controlled by the @code{get_script} emulation entry point;
250 this describes the conventional behaviour).
251
252 The @file{scripttempl} script should just write a linker script, written
253 in the linker command language, to standard output.  If the emulation
254 name--the name of the @file{emulparams} file without the @file{.sc}
255 extension--is @var{emul}, then the output will be directed to
256 @file{ldscripts/@var{emul}.@var{extension}} in the build directory,
257 where @var{extension} changes each time the @file{scripttempl} script is
258 invoked.
259
260 Here is the list of values assigned to @code{LD_FLAG}.
261
262 @table @code
263 @item (empty)
264 The script generated is used by default (when none of the following
265 cases apply).  The output has an extension of @file{.x}.
266 @item n
267 The script generated is used when the linker is invoked with the
268 @code{-n} option.  The output has an extension of @file{.xn}.
269 @item N
270 The script generated is used when the linker is invoked with the
271 @code{-N} option.  The output has an extension of @file{.xbn}.
272 @item r
273 The script generated is used when the linker is invoked with the
274 @code{-r} option.  The output has an extension of @file{.xr}.
275 @item u
276 The script generated is used when the linker is invoked with the
277 @code{-Ur} option.  The output has an extension of @file{.xu}.
278 @item shared
279 The @file{scripttempl} script is only invoked with @code{LD_FLAG} set to
280 this value if @code{GENERATE_SHLIB_SCRIPT} is defined in the
281 @file{emulparams} file.  The @file{emultempl} script must arrange to use
282 this script at the appropriate time, normally when the linker is invoked
283 with the @code{-shared} option.  The output has an extension of
284 @file{.xs}.
285 @item c
286 The @file{scripttempl} script is only invoked with @code{LD_FLAG} set to
287 this value if @code{GENERATE_COMBRELOC_SCRIPT} is defined in the
288 @file{emulparams} file or if @code{SCRIPT_NAME} is @code{elf}. The
289 @file{emultempl} script must arrange to use this script at the appropriate
290 time, normally when the linker is invoked with the @code{-z combreloc}
291 option.  The output has an extension of
292 @file{.xc}.
293 @item cshared
294 The @file{scripttempl} script is only invoked with @code{LD_FLAG} set to
295 this value if @code{GENERATE_COMBRELOC_SCRIPT} is defined in the
296 @file{emulparams} file or if @code{SCRIPT_NAME} is @code{elf} and
297 @code{GENERATE_SHLIB_SCRIPT} is defined in the @file{emulparams} file.
298 The @file{emultempl} script must arrange to use this script at the
299 appropriate time, normally when the linker is invoked with the @code{-shared
300 -z combreloc} option.  The output has an extension of @file{.xsc}.
301 @end table
302
303 Besides the shell variables set by the @file{emulparams} script, and the
304 @code{LD_FLAG} variable, the @file{genscripts.sh} script will set
305 certain variables for each run of the @file{scripttempl} script.
306
307 @table @code
308 @item RELOCATING
309 This will be set to a non-empty string when the linker is doing a final
310 relocation (e.g., all scripts other than @code{-r} and @code{-Ur}).
311
312 @item CONSTRUCTING
313 This will be set to a non-empty string when the linker is building
314 global constructor and destructor tables (e.g., all scripts other than
315 @code{-r}).
316
317 @item DATA_ALIGNMENT
318 This will be set to an @code{ALIGN} expression when the output should be
319 page aligned, or to @samp{.} when generating the @code{-N} script.
320
321 @item CREATE_SHLIB
322 This will be set to a non-empty string when generating a @code{-shared}
323 script.
324
325 @item COMBRELOC
326 This will be set to a non-empty string when generating @code{-z combreloc}
327 scripts to a temporary file name which can be used during script generation.
328 @end table
329
330 The conventional way to write a @file{scripttempl} script is to first
331 set a few shell variables, and then write out a linker script using
332 @code{cat} with a here document.  The linker script will use variable
333 substitutions, based on the above variables and those set in the
334 @file{emulparams} script, to control its behaviour.
335
336 When there are parts of the @file{scripttempl} script which should only
337 be run when doing a final relocation, they should be enclosed within a
338 variable substitution based on @code{RELOCATING}.  For example, on many
339 targets special symbols such as @code{_end} should be defined when doing
340 a final link.  Naturally, those symbols should not be defined when doing
341 a relocatable link using @code{-r}.  The @file{scripttempl} script
342 could use a construct like this to define those symbols:
343 @smallexample
344   $@{RELOCATING+ _end = .;@}
345 @end smallexample
346 This will do the symbol assignment only if the @code{RELOCATING}
347 variable is defined.
348
349 The basic job of the linker script is to put the sections in the correct
350 order, and at the correct memory addresses.  For some targets, the
351 linker script may have to do some other operations.
352
353 For example, on most MIPS platforms, the linker is responsible for
354 defining the special symbol @code{_gp}, used to initialize the
355 @code{$gp} register.  It must be set to the start of the small data
356 section plus @code{0x8000}.  Naturally, it should only be defined when
357 doing a final relocation.  This will typically be done like this:
358 @smallexample
359   $@{RELOCATING+ _gp = ALIGN(16) + 0x8000;@}
360 @end smallexample
361 This line would appear just before the sections which compose the small
362 data section (@samp{.sdata}, @samp{.sbss}).  All those sections would be
363 contiguous in memory.
364
365 Many COFF systems build constructor tables in the linker script.  The
366 compiler will arrange to output the address of each global constructor
367 in a @samp{.ctor} section, and the address of each global destructor in
368 a @samp{.dtor} section (this is done by defining
369 @code{ASM_OUTPUT_CONSTRUCTOR} and @code{ASM_OUTPUT_DESTRUCTOR} in the
370 @code{gcc} configuration files).  The @code{gcc} runtime support
371 routines expect the constructor table to be named @code{__CTOR_LIST__}.
372 They expect it to be a list of words, with the first word being the
373 count of the number of entries.  There should be a trailing zero word.
374 (Actually, the count may be -1 if the trailing word is present, and the
375 trailing word may be omitted if the count is correct, but, as the
376 @code{gcc} behaviour has changed slightly over the years, it is safest
377 to provide both).  Here is a typical way that might be handled in a
378 @file{scripttempl} file.
379 @smallexample
380     $@{CONSTRUCTING+ __CTOR_LIST__ = .;@}
381     $@{CONSTRUCTING+ LONG((__CTOR_END__ - __CTOR_LIST__) / 4 - 2)@}
382     $@{CONSTRUCTING+ *(.ctors)@}
383     $@{CONSTRUCTING+ LONG(0)@}
384     $@{CONSTRUCTING+ __CTOR_END__ = .;@}
385     $@{CONSTRUCTING+ __DTOR_LIST__ = .;@}
386     $@{CONSTRUCTING+ LONG((__DTOR_END__ - __DTOR_LIST__) / 4 - 2)@}
387     $@{CONSTRUCTING+ *(.dtors)@}
388     $@{CONSTRUCTING+ LONG(0)@}
389     $@{CONSTRUCTING+ __DTOR_END__ = .;@}
390 @end smallexample
391 The use of @code{CONSTRUCTING} ensures that these linker script commands
392 will only appear when the linker is supposed to be building the
393 constructor and destructor tables.  This example is written for a target
394 which uses 4 byte pointers.
395
396 Embedded systems often need to set a stack address.  This is normally
397 best done by using the @code{PROVIDE} construct with a default stack
398 address.  This permits the user to easily override the stack address
399 using the @code{--defsym} option.  Here is an example:
400 @smallexample
401   $@{RELOCATING+ PROVIDE (__stack = 0x80000000);@}
402 @end smallexample
403 The value of the symbol @code{__stack} would then be used in the startup
404 code to initialize the stack pointer.
405
406 @node linker emulations
407 @section @file{emultempl} scripts
408
409 Each linker target uses an @file{emultempl} script to generate the
410 emulation code.  The name of the @file{emultempl} script is set by the
411 @code{TEMPLATE_NAME} variable in the @file{emulparams} script.  If the
412 @code{TEMPLATE_NAME} variable is not set, the default is
413 @samp{generic}.  If the value of @code{TEMPLATE_NAME} is @var{template},
414 @file{genscripts.sh} will use @file{emultempl/@var{template}.em}.
415
416 Most targets use the generic @file{emultempl} script,
417 @file{emultempl/generic.em}.  A different @file{emultempl} script is
418 only needed if the linker must support unusual actions, such as linking
419 against shared libraries.
420
421 The @file{emultempl} script is normally written as a simple invocation
422 of @code{cat} with a here document.  The document will use a few
423 variable substitutions.  Typically each function names uses a
424 substitution involving @code{EMULATION_NAME}, for ease of debugging when
425 the linker supports multiple emulations.
426
427 Every function and variable in the emitted file should be static.  The
428 only globally visible object must be named
429 @code{ld_@var{EMULATION_NAME}_emulation}, where @var{EMULATION_NAME} is
430 the name of the emulation set in @file{configure.tgt} (this is also the
431 name of the @file{emulparams} file without the @file{.sh} extension).
432 The @file{genscripts.sh} script will set the shell variable
433 @code{EMULATION_NAME} before invoking the @file{emultempl} script.
434
435 The @code{ld_@var{EMULATION_NAME}_emulation} variable must be a
436 @code{struct ld_emulation_xfer_struct}, as defined in @file{ldemul.h}.
437 It defines a set of function pointers which are invoked by the linker,
438 as well as strings for the emulation name (normally set from the shell
439 variable @code{EMULATION_NAME} and the default BFD target name (normally
440 set from the shell variable @code{OUTPUT_FORMAT} which is normally set
441 by the @file{emulparams} file).
442
443 The @file{genscripts.sh} script will set the shell variable
444 @code{COMPILE_IN} when it invokes the @file{emultempl} script for the
445 default emulation.  In this case, the @file{emultempl} script should
446 include the linker scripts directly, and return them from the
447 @code{get_scripts} entry point.  When the emulation is not the default,
448 the @code{get_scripts} entry point should just return a file name.  See
449 @file{emultempl/generic.em} for an example of how this is done.
450
451 At some point, the linker emulation entry points should be documented.
452
453 @node Emulation Walkthrough
454 @chapter A Walkthrough of a Typical Emulation
455
456 This chapter is to help people who are new to the way emulations
457 interact with the linker, or who are suddenly thrust into the position
458 of having to work with existing emulations.  It will discuss the files
459 you need to be aware of.  It will tell you when the given "hooks" in
460 the emulation will be called.  It will, hopefully, give you enough
461 information about when and how things happen that you'll be able to
462 get by.  As always, the source is the definitive reference to this.
463
464 The starting point for the linker is in @file{ldmain.c} where
465 @code{main} is defined.  The bulk of the code that's emulation
466 specific will initially be in @code{emultempl/@var{emulation}.em} but
467 will end up in @code{e@var{emulation}.c} when the build is done.
468 Most of the work to select and interface with emulations is in
469 @code{ldemul.h} and @code{ldemul.c}.  Specifically, @code{ldemul.h}
470 defines the @code{ld_emulation_xfer_struct} structure your emulation
471 exports.
472
473 Your emulation file exports a symbol
474 @code{ld_@var{EMULATION_NAME}_emulation}.  If your emulation is
475 selected (it usually is, since usually there's only one),
476 @code{ldemul.c} sets the variable @var{ld_emulation} to point to it.
477 @code{ldemul.c} also defines a number of API functions that interface
478 to your emulation, like @code{ldemul_after_parse} which simply calls
479 your @code{ld_@var{EMULATION}_emulation.after_parse} function.  For
480 the rest of this section, the functions will be mentioned, but you
481 should assume the indirect reference to your emulation also.
482
483 We will also skip or gloss over parts of the link process that don't
484 relate to emulations, like setting up internationalization.
485
486 After initialization, @code{main} selects an emulation by pre-scanning
487 the command line arguments.  It calls @code{ldemul_choose_target} to
488 choose a target.  If you set @code{choose_target} to
489 @code{ldemul_default_target}, it picks your @code{target_name} by
490 default.
491
492 @code{main} calls @code{ldemul_before_parse}, then @code{parse_args}.
493 @code{parse_args} calls @code{ldemul_parse_args} for each arg, which
494 must update the @code{getopt} globals if it recognizes the argument.
495 If the emulation doesn't recognize it, then parse_args checks to see
496 if it recognizes it.
497
498 Now that the emulation has had access to all its command-line options,
499 @code{main} calls @code{ldemul_set_symbols}.  This can be used for any
500 initialization that may be affected by options.  It is also supposed
501 to set up any variables needed by the emulation script.
502
503 @code{main} now calls @code{ldemul_get_script} to get the emulation
504 script to use (based on arguments, no doubt, @pxref{Emulations}) and
505 runs it.  While parsing, @code{ldgram.y} may call @code{ldemul_hll} or
506 @code{ldemul_syslib} to handle the @code{HLL} or @code{SYSLIB}
507 commands.  It may call @code{ldemul_unrecognized_file} if you asked
508 the linker to link a file it doesn't recognize.  It will call
509 @code{ldemul_recognized_file} for each file it does recognize, in case
510 the emulation wants to handle some files specially.  All the while,
511 it's loading the files (possibly calling
512 @code{ldemul_open_dynamic_archive}) and symbols and stuff.  After it's
513 done reading the script, @code{main} calls @code{ldemul_after_parse}.
514 Use the after-parse hook to set up anything that depends on stuff the
515 script might have set up, like the entry point.
516
517 @code{main} next calls @code{lang_process} in @code{ldlang.c}.  This
518 appears to be the main core of the linking itself, as far as emulation
519 hooks are concerned(*).  It first opens the output file's BFD, calling
520 @code{ldemul_set_output_arch}, and calls
521 @code{ldemul_create_output_section_statements} in case you need to use
522 other means to find or create object files (i.e. shared libraries
523 found on a path, or fake stub objects).  Despite the name, nobody
524 creates output sections here.
525
526 (*) In most cases, the BFD library does the bulk of the actual
527 linking, handling symbol tables, symbol resolution, relocations, and
528 building the final output file.  See the BFD reference for all the
529 details.  Your emulation is usually concerned more with managing
530 things at the file and section level, like "put this here, add this
531 section", etc.
532
533 Next, the objects to be linked are opened and BFDs created for them,
534 and @code{ldemul_after_open} is called.  At this point, you have all
535 the objects and symbols loaded, but none of the data has been placed
536 yet.
537
538 Next comes the Big Linking Thingy (except for the parts BFD does).
539 All input sections are mapped to output sections according to the
540 script.  If a section doesn't get mapped by default,
541 @code{ldemul_place_orphan} will get called to figure out where it goes.
542 Next it figures out the offsets for each section, calling
543 @code{ldemul_before_allocation} before and
544 @code{ldemul_after_allocation} after deciding where each input section
545 ends up in the output sections.
546
547 The last part of @code{lang_process} is to figure out all the symbols'
548 values.  After assigning final values to the symbols,
549 @code{ldemul_finish} is called, and after that, any undefined symbols
550 are turned into fatal errors.
551
552 OK, back to @code{main}, which calls @code{ldwrite} in
553 @file{ldwrite.c}.  @code{ldwrite} calls BFD's final_link, which does
554 all the relocation fixups and writes the output bfd to disk, and we're
555 done.
556
557 In summary,
558
559 @itemize @bullet
560
561 @item @code{main()} in @file{ldmain.c}
562 @item @file{emultempl/@var{EMULATION}.em} has your code
563 @item @code{ldemul_choose_target} (defaults to your @code{target_name})
564 @item @code{ldemul_before_parse}
565 @item Parse argv, calls @code{ldemul_parse_args} for each
566 @item @code{ldemul_set_symbols}
567 @item @code{ldemul_get_script}
568 @item parse script
569
570 @itemize @bullet
571 @item may call @code{ldemul_hll} or @code{ldemul_syslib}
572 @item may call @code{ldemul_open_dynamic_archive}
573 @end itemize
574
575 @item @code{ldemul_after_parse}
576 @item @code{lang_process()} in @file{ldlang.c}
577
578 @itemize @bullet
579 @item create @code{output_bfd}
580 @item @code{ldemul_set_output_arch}
581 @item @code{ldemul_create_output_section_statements}
582 @item read objects, create input bfds - all symbols exist, but have no values
583 @item may call @code{ldemul_unrecognized_file}
584 @item will call @code{ldemul_recognized_file}
585 @item @code{ldemul_after_open}
586 @item map input sections to output sections
587 @item may call @code{ldemul_place_orphan} for remaining sections
588 @item @code{ldemul_before_allocation}
589 @item gives input sections offsets into output sections, places output sections
590 @item @code{ldemul_after_allocation} - section addresses valid
591 @item assigns values to symbols
592 @item @code{ldemul_finish} - symbol values valid
593 @end itemize
594
595 @item output bfd is written to disk
596
597 @end itemize
598
599 @node Architecture Specific
600 @chapter Some Architecture Specific Notes
601
602 This is the place for notes on the behavior of @code{ld} on
603 specific platforms.  Currently, only Intel x86 is documented (and 
604 of that, only the auto-import behavior for DLLs).
605
606 @menu
607 * ix86::                        Intel x86
608 @end menu
609
610 @node ix86
611 @section Intel x86
612
613 @table @emph
614 @code{ld} can create DLLs that operate with various runtimes available
615 on a common x86 operating system.  These runtimes include native (using 
616 the mingw "platform"), cygwin, and pw.
617
618 @item auto-import from DLLs 
619 @enumerate
620 @item
621 With this feature on, DLL clients can import variables from DLL 
622 without any concern from their side (for example, without any source
623 code modifications).  Auto-import can be enabled using the 
624 @code{--enable-auto-import} flag, or disabled via the 
625 @code{--disable-auto-import} flag.  Auto-import is disabled by default.
626
627 @item
628 This is done completely in bounds of the PE specification (to be fair,
629 there's a minor violation of the spec at one point, but in practice 
630 auto-import works on all known variants of that common x86 operating
631 system)  So, the resulting DLL can be used with any other PE 
632 compiler/linker.
633
634 @item
635 Auto-import is fully compatible with standard import method, in which
636 variables are decorated using attribute modifiers. Libraries of either
637 type may be mixed together.
638
639 @item
640 Overhead (space): 8 bytes per imported symbol, plus 20 for each
641 reference to it; Overhead (load time): negligible; Overhead 
642 (virtual/physical memory): should be less than effect of DLL 
643 relocation.
644 @end enumerate
645
646 Motivation
647
648 The obvious and only way to get rid of dllimport insanity is 
649 to make client access variable directly in the DLL, bypassing 
650 the extra dereference imposed by ordinary DLL runtime linking.
651 I.e., whenever client contains something like
652
653 @code{mov dll_var,%eax,}
654
655 address of dll_var in the command should be relocated to point 
656 into loaded DLL. The aim is to make OS loader do so, and than 
657 make ld help with that.  Import section of PE made following 
658 way: there's a vector of structures each describing imports 
659 from particular DLL. Each such structure points to two other 
660 parallel vectors: one holding imported names, and one which 
661 will hold address of corresponding imported name. So, the 
662 solution is de-vectorize these structures, making import 
663 locations be sparse and pointing directly into code.
664
665 Implementation
666
667 For each reference of data symbol to be imported from DLL (to 
668 set of which belong symbols with name <sym>, if __imp_<sym> is 
669 found in implib), the import fixup entry is generated. That 
670 entry is of type IMAGE_IMPORT_DESCRIPTOR and stored in .idata$3 
671 subsection. Each fixup entry contains pointer to symbol's address 
672 within .text section (marked with __fuN_<sym> symbol, where N is 
673 integer), pointer to DLL name (so, DLL name is referenced by 
674 multiple entries), and pointer to symbol name thunk. Symbol name 
675 thunk is singleton vector (__nm_th_<symbol>) pointing to 
676 IMAGE_IMPORT_BY_NAME structure (__nm_<symbol>) directly containing 
677 imported name. Here comes that "om the edge" problem mentioned above: 
678 PE specification rambles that name vector (OriginalFirstThunk) should 
679 run in parallel with addresses vector (FirstThunk), i.e. that they 
680 should have same number of elements and terminated with zero. We violate
681 this, since FirstThunk points directly into machine code. But in 
682 practice, OS loader implemented the sane way: it goes thru 
683 OriginalFirstThunk and puts addresses to FirstThunk, not something 
684 else. It once again should be noted that dll and symbol name 
685 structures are reused across fixup entries and should be there 
686 anyway to support standard import stuff, so sustained overhead is 
687 20 bytes per reference. Other question is whether having several 
688 IMAGE_IMPORT_DESCRIPTORS for the same DLL is possible. Answer is yes, 
689 it is done even by native compiler/linker (libth32's functions are in 
690 fact resident in windows9x kernel32.dll, so if you use it, you have 
691 two IMAGE_IMPORT_DESCRIPTORS for kernel32.dll). Yet other question is 
692 whether referencing the same PE structures several times is valid. 
693 The answer is why not, prohibiting that (detecting violation) would 
694 require more work on behalf of loader than not doing it.
695
696 @end table
697
698 @node GNU Free Documentation License
699 @chapter GNU Free Documentation License
700
701                 GNU Free Documentation License
702                 
703                    Version 1.1, March 2000
704
705  Copyright (C) 2000  Free Software Foundation, Inc.
706   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
707      
708  Everyone is permitted to copy and distribute verbatim copies
709  of this license document, but changing it is not allowed.
710
711
712 0. PREAMBLE
713
714 The purpose of this License is to make a manual, textbook, or other
715 written document "free" in the sense of freedom: to assure everyone
716 the effective freedom to copy and redistribute it, with or without
717 modifying it, either commercially or noncommercially.  Secondarily,
718 this License preserves for the author and publisher a way to get
719 credit for their work, while not being considered responsible for
720 modifications made by others.
721
722 This License is a kind of "copyleft", which means that derivative
723 works of the document must themselves be free in the same sense.  It
724 complements the GNU General Public License, which is a copyleft
725 license designed for free software.
726
727 We have designed this License in order to use it for manuals for free
728 software, because free software needs free documentation: a free
729 program should come with manuals providing the same freedoms that the
730 software does.  But this License is not limited to software manuals;
731 it can be used for any textual work, regardless of subject matter or
732 whether it is published as a printed book.  We recommend this License
733 principally for works whose purpose is instruction or reference.
734
735
736 1. APPLICABILITY AND DEFINITIONS
737
738 This License applies to any manual or other work that contains a
739 notice placed by the copyright holder saying it can be distributed
740 under the terms of this License.  The "Document", below, refers to any
741 such manual or work.  Any member of the public is a licensee, and is
742 addressed as "you".
743
744 A "Modified Version" of the Document means any work containing the
745 Document or a portion of it, either copied verbatim, or with
746 modifications and/or translated into another language.
747
748 A "Secondary Section" is a named appendix or a front-matter section of
749 the Document that deals exclusively with the relationship of the
750 publishers or authors of the Document to the Document's overall subject
751 (or to related matters) and contains nothing that could fall directly
752 within that overall subject.  (For example, if the Document is in part a
753 textbook of mathematics, a Secondary Section may not explain any
754 mathematics.)  The relationship could be a matter of historical
755 connection with the subject or with related matters, or of legal,
756 commercial, philosophical, ethical or political position regarding
757 them.
758
759 The "Invariant Sections" are certain Secondary Sections whose titles
760 are designated, as being those of Invariant Sections, in the notice
761 that says that the Document is released under this License.
762
763 The "Cover Texts" are certain short passages of text that are listed,
764 as Front-Cover Texts or Back-Cover Texts, in the notice that says that
765 the Document is released under this License.
766
767 A "Transparent" copy of the Document means a machine-readable copy,
768 represented in a format whose specification is available to the
769 general public, whose contents can be viewed and edited directly and
770 straightforwardly with generic text editors or (for images composed of
771 pixels) generic paint programs or (for drawings) some widely available
772 drawing editor, and that is suitable for input to text formatters or
773 for automatic translation to a variety of formats suitable for input
774 to text formatters.  A copy made in an otherwise Transparent file
775 format whose markup has been designed to thwart or discourage
776 subsequent modification by readers is not Transparent.  A copy that is
777 not "Transparent" is called "Opaque".
778
779 Examples of suitable formats for Transparent copies include plain
780 ASCII without markup, Texinfo input format, LaTeX input format, SGML
781 or XML using a publicly available DTD, and standard-conforming simple
782 HTML designed for human modification.  Opaque formats include
783 PostScript, PDF, proprietary formats that can be read and edited only
784 by proprietary word processors, SGML or XML for which the DTD and/or
785 processing tools are not generally available, and the
786 machine-generated HTML produced by some word processors for output
787 purposes only.
788
789 The "Title Page" means, for a printed book, the title page itself,
790 plus such following pages as are needed to hold, legibly, the material
791 this License requires to appear in the title page.  For works in
792 formats which do not have any title page as such, "Title Page" means
793 the text near the most prominent appearance of the work's title,
794 preceding the beginning of the body of the text.
795
796
797 2. VERBATIM COPYING
798
799 You may copy and distribute the Document in any medium, either
800 commercially or noncommercially, provided that this License, the
801 copyright notices, and the license notice saying this License applies
802 to the Document are reproduced in all copies, and that you add no other
803 conditions whatsoever to those of this License.  You may not use
804 technical measures to obstruct or control the reading or further
805 copying of the copies you make or distribute.  However, you may accept
806 compensation in exchange for copies.  If you distribute a large enough
807 number of copies you must also follow the conditions in section 3.
808
809 You may also lend copies, under the same conditions stated above, and
810 you may publicly display copies.
811
812
813 3. COPYING IN QUANTITY
814
815 If you publish printed copies of the Document numbering more than 100,
816 and the Document's license notice requires Cover Texts, you must enclose
817 the copies in covers that carry, clearly and legibly, all these Cover
818 Texts: Front-Cover Texts on the front cover, and Back-Cover Texts on
819 the back cover.  Both covers must also clearly and legibly identify
820 you as the publisher of these copies.  The front cover must present
821 the full title with all words of the title equally prominent and
822 visible.  You may add other material on the covers in addition.
823 Copying with changes limited to the covers, as long as they preserve
824 the title of the Document and satisfy these conditions, can be treated
825 as verbatim copying in other respects.
826
827 If the required texts for either cover are too voluminous to fit
828 legibly, you should put the first ones listed (as many as fit
829 reasonably) on the actual cover, and continue the rest onto adjacent
830 pages.
831
832 If you publish or distribute Opaque copies of the Document numbering
833 more than 100, you must either include a machine-readable Transparent
834 copy along with each Opaque copy, or state in or with each Opaque copy
835 a publicly-accessible computer-network location containing a complete
836 Transparent copy of the Document, free of added material, which the
837 general network-using public has access to download anonymously at no
838 charge using public-standard network protocols.  If you use the latter
839 option, you must take reasonably prudent steps, when you begin
840 distribution of Opaque copies in quantity, to ensure that this
841 Transparent copy will remain thus accessible at the stated location
842 until at least one year after the last time you distribute an Opaque
843 copy (directly or through your agents or retailers) of that edition to
844 the public.
845
846 It is requested, but not required, that you contact the authors of the
847 Document well before redistributing any large number of copies, to give
848 them a chance to provide you with an updated version of the Document.
849
850
851 4. MODIFICATIONS
852
853 You may copy and distribute a Modified Version of the Document under
854 the conditions of sections 2 and 3 above, provided that you release
855 the Modified Version under precisely this License, with the Modified
856 Version filling the role of the Document, thus licensing distribution
857 and modification of the Modified Version to whoever possesses a copy
858 of it.  In addition, you must do these things in the Modified Version:
859
860 A. Use in the Title Page (and on the covers, if any) a title distinct
861    from that of the Document, and from those of previous versions
862    (which should, if there were any, be listed in the History section
863    of the Document).  You may use the same title as a previous version
864    if the original publisher of that version gives permission.
865 B. List on the Title Page, as authors, one or more persons or entities
866    responsible for authorship of the modifications in the Modified
867    Version, together with at least five of the principal authors of the
868    Document (all of its principal authors, if it has less than five).
869 C. State on the Title page the name of the publisher of the
870    Modified Version, as the publisher.
871 D. Preserve all the copyright notices of the Document.
872 E. Add an appropriate copyright notice for your modifications
873    adjacent to the other copyright notices.
874 F. Include, immediately after the copyright notices, a license notice
875    giving the public permission to use the Modified Version under the
876    terms of this License, in the form shown in the Addendum below.
877 G. Preserve in that license notice the full lists of Invariant Sections
878    and required Cover Texts given in the Document's license notice.
879 H. Include an unaltered copy of this License.
880 I. Preserve the section entitled "History", and its title, and add to
881    it an item stating at least the title, year, new authors, and
882    publisher of the Modified Version as given on the Title Page.  If
883    there is no section entitled "History" in the Document, create one
884    stating the title, year, authors, and publisher of the Document as
885    given on its Title Page, then add an item describing the Modified
886    Version as stated in the previous sentence.
887 J. Preserve the network location, if any, given in the Document for
888    public access to a Transparent copy of the Document, and likewise
889    the network locations given in the Document for previous versions
890    it was based on.  These may be placed in the "History" section.
891    You may omit a network location for a work that was published at
892    least four years before the Document itself, or if the original
893    publisher of the version it refers to gives permission.
894 K. In any section entitled "Acknowledgements" or "Dedications",
895    preserve the section's title, and preserve in the section all the
896    substance and tone of each of the contributor acknowledgements
897    and/or dedications given therein.
898 L. Preserve all the Invariant Sections of the Document,
899    unaltered in their text and in their titles.  Section numbers
900    or the equivalent are not considered part of the section titles.
901 M. Delete any section entitled "Endorsements".  Such a section
902    may not be included in the Modified Version.
903 N. Do not retitle any existing section as "Endorsements"
904    or to conflict in title with any Invariant Section.
905
906 If the Modified Version includes new front-matter sections or
907 appendices that qualify as Secondary Sections and contain no material
908 copied from the Document, you may at your option designate some or all
909 of these sections as invariant.  To do this, add their titles to the
910 list of Invariant Sections in the Modified Version's license notice.
911 These titles must be distinct from any other section titles.
912
913 You may add a section entitled "Endorsements", provided it contains
914 nothing but endorsements of your Modified Version by various
915 parties--for example, statements of peer review or that the text has
916 been approved by an organization as the authoritative definition of a
917 standard.
918
919 You may add a passage of up to five words as a Front-Cover Text, and a
920 passage of up to 25 words as a Back-Cover Text, to the end of the list
921 of Cover Texts in the Modified Version.  Only one passage of
922 Front-Cover Text and one of Back-Cover Text may be added by (or
923 through arrangements made by) any one entity.  If the Document already
924 includes a cover text for the same cover, previously added by you or
925 by arrangement made by the same entity you are acting on behalf of,
926 you may not add another; but you may replace the old one, on explicit
927 permission from the previous publisher that added the old one.
928
929 The author(s) and publisher(s) of the Document do not by this License
930 give permission to use their names for publicity for or to assert or
931 imply endorsement of any Modified Version.
932
933
934 5. COMBINING DOCUMENTS
935
936 You may combine the Document with other documents released under this
937 License, under the terms defined in section 4 above for modified
938 versions, provided that you include in the combination all of the
939 Invariant Sections of all of the original documents, unmodified, and
940 list them all as Invariant Sections of your combined work in its
941 license notice.
942
943 The combined work need only contain one copy of this License, and
944 multiple identical Invariant Sections may be replaced with a single
945 copy.  If there are multiple Invariant Sections with the same name but
946 different contents, make the title of each such section unique by
947 adding at the end of it, in parentheses, the name of the original
948 author or publisher of that section if known, or else a unique number.
949 Make the same adjustment to the section titles in the list of
950 Invariant Sections in the license notice of the combined work.
951
952 In the combination, you must combine any sections entitled "History"
953 in the various original documents, forming one section entitled
954 "History"; likewise combine any sections entitled "Acknowledgements",
955 and any sections entitled "Dedications".  You must delete all sections
956 entitled "Endorsements."
957
958
959 6. COLLECTIONS OF DOCUMENTS
960
961 You may make a collection consisting of the Document and other documents
962 released under this License, and replace the individual copies of this
963 License in the various documents with a single copy that is included in
964 the collection, provided that you follow the rules of this License for
965 verbatim copying of each of the documents in all other respects.
966
967 You may extract a single document from such a collection, and distribute
968 it individually under this License, provided you insert a copy of this
969 License into the extracted document, and follow this License in all
970 other respects regarding verbatim copying of that document.
971
972
973 7. AGGREGATION WITH INDEPENDENT WORKS
974
975 A compilation of the Document or its derivatives with other separate
976 and independent documents or works, in or on a volume of a storage or
977 distribution medium, does not as a whole count as a Modified Version
978 of the Document, provided no compilation copyright is claimed for the
979 compilation.  Such a compilation is called an "aggregate", and this
980 License does not apply to the other self-contained works thus compiled
981 with the Document, on account of their being thus compiled, if they
982 are not themselves derivative works of the Document.
983
984 If the Cover Text requirement of section 3 is applicable to these
985 copies of the Document, then if the Document is less than one quarter
986 of the entire aggregate, the Document's Cover Texts may be placed on
987 covers that surround only the Document within the aggregate.
988 Otherwise they must appear on covers around the whole aggregate.
989
990
991 8. TRANSLATION
992
993 Translation is considered a kind of modification, so you may
994 distribute translations of the Document under the terms of section 4.
995 Replacing Invariant Sections with translations requires special
996 permission from their copyright holders, but you may include
997 translations of some or all Invariant Sections in addition to the
998 original versions of these Invariant Sections.  You may include a
999 translation of this License provided that you also include the
1000 original English version of this License.  In case of a disagreement
1001 between the translation and the original English version of this
1002 License, the original English version will prevail.
1003
1004
1005 9. TERMINATION
1006
1007 You may not copy, modify, sublicense, or distribute the Document except
1008 as expressly provided for under this License.  Any other attempt to
1009 copy, modify, sublicense or distribute the Document is void, and will
1010 automatically terminate your rights under this License.  However,
1011 parties who have received copies, or rights, from you under this
1012 License will not have their licenses terminated so long as such
1013 parties remain in full compliance.
1014
1015
1016 10. FUTURE REVISIONS OF THIS LICENSE
1017
1018 The Free Software Foundation may publish new, revised versions
1019 of the GNU Free Documentation License from time to time.  Such new
1020 versions will be similar in spirit to the present version, but may
1021 differ in detail to address new problems or concerns.  See
1022 http://www.gnu.org/copyleft/.
1023
1024 Each version of the License is given a distinguishing version number.
1025 If the Document specifies that a particular numbered version of this
1026 License "or any later version" applies to it, you have the option of
1027 following the terms and conditions either of that specified version or
1028 of any later version that has been published (not as a draft) by the
1029 Free Software Foundation.  If the Document does not specify a version
1030 number of this License, you may choose any version ever published (not
1031 as a draft) by the Free Software Foundation.
1032
1033
1034 ADDENDUM: How to use this License for your documents
1035
1036 To use this License in a document you have written, include a copy of
1037 the License in the document and put the following copyright and
1038 license notices just after the title page:
1039
1040 @smallexample
1041     Copyright (c)  YEAR  YOUR NAME.
1042     Permission is granted to copy, distribute and/or modify this document
1043     under the terms of the GNU Free Documentation License, Version 1.1
1044     or any later version published by the Free Software Foundation;
1045     with the Invariant Sections being LIST THEIR TITLES, with the
1046     Front-Cover Texts being LIST, and with the Back-Cover Texts being LIST.
1047     A copy of the license is included in the section entitled "GNU
1048     Free Documentation License".
1049 @end smallexample
1050
1051 If you have no Invariant Sections, write "with no Invariant Sections"
1052 instead of saying which ones are invariant.  If you have no
1053 Front-Cover Texts, write "no Front-Cover Texts" instead of
1054 "Front-Cover Texts being LIST"; likewise for Back-Cover Texts.
1055
1056 If your document contains nontrivial examples of program code, we
1057 recommend releasing these examples in parallel under your choice of
1058 free software license, such as the GNU General Public License,
1059 to permit their use in free software.
1060
1061 @contents
1062 @bye