2 @setfilename ldint.info
3 @c Copyright (C) 1992-2025 Free Software Foundation, Inc.
6 @dircategory Software development
8 * Ld-Internals: (ldint). The GNU linker internals.
13 This file documents the internals of the GNU linker ld.
15 Copyright @copyright{} 1992-2025 Free Software Foundation, Inc.
16 Contributed by Cygnus Support.
18 Permission is granted to copy, distribute and/or modify this document
19 under the terms of the GNU Free Documentation License, Version 1.3 or
20 any later version published by the Free Software Foundation; with the
21 Invariant Sections being ``GNU General Public License'' and ``Funding
22 Free Software'', the Front-Cover texts being (a) (see below), and with
23 the Back-Cover Texts being (b) (see below). A copy of the license is
24 included in the section entitled ``GNU Free Documentation License''.
26 (a) The FSF's Front-Cover Text is:
30 (b) The FSF's Back-Cover Text is:
32 You have freedom to copy and modify this GNU Manual, like GNU
33 software. Copies published by the Free Software Foundation raise
34 funds for GNU development.
39 @setchapternewpage off
40 @settitle GNU Linker Internals
42 @title{A guide to the internals of the GNU linker}
43 @author Per Bothner, Steve Chamberlain, Ian Lance Taylor, DJ Delorie
44 @author Cygnus Support
48 \def\$#1${{#1}} % Kluge: collect RCS revision info without $...$
49 \xdef\manvers{2.10.91} % For use in headers, footers too
51 \hfill Cygnus Support\par
53 \hfill \TeX{}info \texinfoversion\par
57 @vskip 0pt plus 1filll
58 Copyright @copyright{} 1992-2025 Free Software Foundation, Inc.
60 Permission is granted to copy, distribute and/or modify this document
61 under the terms of the GNU Free Documentation License, Version 1.3
62 or any later version published by the Free Software Foundation;
63 with no Invariant Sections, with no Front-Cover Texts, and with no
64 Back-Cover Texts. A copy of the license is included in the
65 section entitled "GNU Free Documentation License".
73 This file documents the internals of the GNU linker @code{ld}. It is a
74 collection of miscellaneous information with little form at this point.
75 Mostly, it is a repository into which you can put information about
76 GNU @code{ld} as you discover it (or as you design changes to @code{ld}).
78 This document is distributed under the terms of the GNU Free
79 Documentation License. A copy of the license is included in the
80 section entitled "GNU Free Documentation License".
83 * README:: The README File
84 * Emulations:: How linker emulations are generated
85 * Emulation Walkthrough:: A Walkthrough of a Typical Emulation
86 * Architecture Specific:: Some Architecture Specific Notes
87 * GNU Free Documentation License:: GNU Free Documentation License
91 @chapter The @file{README} File
93 Check the @file{README} file; it often has useful information that does not
94 appear anywhere else in the directory.
97 @chapter How linker emulations are generated
99 Each linker target has an @dfn{emulation}. The emulation includes the
100 default linker script, and certain emulations also modify certain types
103 Emulations are created during the build process by the shell script
104 @file{genscripts.sh}.
106 The @file{genscripts.sh} script starts by reading a file in the
107 @file{emulparams} directory. This is a shell script which sets various
108 shell variables used by @file{genscripts.sh} and the other shell scripts
111 The @file{genscripts.sh} script will invoke a shell script in the
112 @file{scripttempl} directory in order to create default linker scripts
113 written in the linker command language. The @file{scripttempl} script
114 will be invoked 5 (or, in some cases, 6) times, with different
115 assignments to shell variables, to create different default scripts.
116 The choice of script is made based on the command-line options.
118 After creating the scripts, @file{genscripts.sh} will invoke yet another
119 shell script, this time in the @file{emultempl} directory. That shell
120 script will create the emulation source file, which contains C code.
121 This C code permits the linker emulation to override various linker
122 behaviours. Most targets use the generic emulation code, which is in
123 @file{emultempl/generic.em}.
125 To summarize, @file{genscripts.sh} reads three shell scripts: an
126 emulation parameters script in the @file{emulparams} directory, a linker
127 script generation script in the @file{scripttempl} directory, and an
128 emulation source file generation script in the @file{emultempl}
131 For example, the Sun 4 linker sets up variables in
132 @file{emulparams/sun4.sh}, creates linker scripts using
133 @file{scripttempl/aout.sc}, and creates the emulation code using
134 @file{emultempl/sunos.em}.
136 Note that the linker can support several emulations simultaneously,
137 depending upon how it is configured. An emulation can be selected with
138 the @code{-m} option. The @code{-V} option will list all supported
142 * emulation parameters:: @file{emulparams} scripts
143 * linker scripts:: @file{scripttempl} scripts
144 * linker emulations:: @file{emultempl} scripts
147 @node emulation parameters
148 @section @file{emulparams} scripts
150 Each target selects a particular file in the @file{emulparams} directory
151 by setting the shell variable @code{targ_emul} in @file{configure.tgt}.
152 This shell variable is used by the @file{configure} script to control
153 building an emulation source file.
155 Certain conventions are enforced. Suppose the @code{targ_emul} variable
156 is set to @var{emul} in @file{configure.tgt}. The name of the emulation
157 shell script will be @file{emulparams/@var{emul}.sh}. The
158 @file{Makefile} must have a target named @file{e@var{emul}.c}; this
159 target must depend upon @file{emulparams/@var{emul}.sh}, as well as the
160 appropriate scripts in the @file{scripttempl} and @file{emultempl}
161 directories. The @file{Makefile} target must invoke @code{GENSCRIPTS}
162 with two arguments: @var{emul}, and the value of the make variable
163 @code{tdir_@var{emul}}. The value of the latter variable will be set by
164 the @file{configure} script, and is used to set the default target
167 By convention, the @file{emulparams/@var{emul}.sh} shell script should
168 only set shell variables. It may set shell variables which are to be
169 interpreted by the @file{scripttempl} and the @file{emultempl} scripts.
170 Certain shell variables are interpreted directly by the
171 @file{genscripts.sh} script.
173 Here is a list of shell variables interpreted by @file{genscripts.sh},
174 as well as some conventional shell variables interpreted by the
175 @file{scripttempl} and @file{emultempl} scripts.
179 This is the name of the @file{scripttempl} script to use. If
180 @code{SCRIPT_NAME} is set to @var{script}, @file{genscripts.sh} will use
181 the script @file{scripttempl/@var{script}.sc}.
184 This is the name of the @file{emultempl} script to use. If
185 @code{TEMPLATE_NAME} is set to @var{template}, @file{genscripts.sh} will
186 use the script @file{emultempl/@var{template}.em}. If this variable is
187 not set, the default value is @samp{generic}.
189 @item GENERATE_SHLIB_SCRIPT
190 If this is set to a nonempty string, @file{genscripts.sh} will invoke
191 the @file{scripttempl} script an extra time to create a shared library
192 script. @ref{linker scripts}.
195 This is normally set to indicate the BFD output format use (e.g.,
196 @samp{"a.out-sunos-big"}. The @file{scripttempl} script will normally
197 use it in an @code{OUTPUT_FORMAT} expression in the linker script.
200 This is normally set to indicate the architecture to use (e.g.,
201 @samp{sparc}). The @file{scripttempl} script will normally use it in an
202 @code{OUTPUT_ARCH} expression in the linker script.
205 Some @file{scripttempl} scripts use this to set the entry address, in an
206 @code{ENTRY} expression in the linker script.
208 @item TEXT_START_ADDR
209 Some @file{scripttempl} scripts use this to set the start address of the
210 @samp{.text} section.
213 The @file{genscripts.sh} script uses this to set the default value of
214 @code{DATA_ALIGNMENT} when running the @file{scripttempl} script.
216 @item TARGET_PAGE_SIZE
217 If @code{SEGMENT_SIZE} is not defined, the @file{genscripts.sh} script
218 uses this to define it.
221 Some @file{scripttempl} scripts set this to a number to pass to
222 @code{ALIGN} to set the required alignment for the @code{end} symbol.
226 @section @file{scripttempl} scripts
228 Each linker target uses a @file{scripttempl} script to generate the
229 default linker scripts. The name of the @file{scripttempl} script is
230 set by the @code{SCRIPT_NAME} variable in the @file{emulparams} script.
231 If @code{SCRIPT_NAME} is set to @var{script}, @code{genscripts.sh} will
232 invoke @file{scripttempl/@var{script}.sc}.
234 The @file{genscripts.sh} script will invoke the @file{scripttempl}
235 script 5 to 9 times. Each time it will set the shell variable
236 @code{LD_FLAG} to a different value. When the linker is run, the
237 options used will direct it to select a particular script. (Script
238 selection is controlled by the @code{get_script} emulation entry point;
239 this describes the conventional behaviour).
241 The @file{scripttempl} script should just write a linker script, written
242 in the linker command language, to standard output. If the emulation
243 name--the name of the @file{emulparams} file without the @file{.sc}
244 extension--is @var{emul}, then the output will be directed to
245 @file{ldscripts/@var{emul}.@var{extension}} in the build directory,
246 where @var{extension} changes each time the @file{scripttempl} script is
249 Here is the list of values assigned to @code{LD_FLAG}.
253 The script generated is used by default (when none of the following
254 cases apply). The output has an extension of @file{.x}.
257 The script generated is used when the linker is invoked with the
258 @code{-n} option. The output has an extension of @file{.xn}.
261 The script generated is used when the linker is invoked with the
262 @code{-N} option. The output has an extension of @file{.xbn}.
265 The script generated is used when the linker is invoked with the
266 @code{-r} option. The output has an extension of @file{.xr}.
269 The script generated is used when the linker is invoked with the
270 @code{-Ur} option. The output has an extension of @file{.xu}.
273 The @file{scripttempl} script is only invoked with @code{LD_FLAG} set to
274 this value if @code{GENERATE_SHLIB_SCRIPT} is defined in the
275 @file{emulparams} file. The @file{emultempl} script must arrange to use
276 this script at the appropriate time, normally when the linker is invoked
277 with the @code{-shared} option. The output has an extension of
281 The @file{scripttempl} script is only invoked with @code{LD_FLAG} set to
282 this value if @code{GENERATE_COMBRELOC_SCRIPT} is defined in the
283 @file{emulparams} file or if @code{SCRIPT_NAME} is @code{elf}. The
284 @file{emultempl} script must arrange to use this script at the appropriate
285 time, normally when the linker is invoked with the @code{-z combreloc}
286 option. The output has an extension of @file{.xc}.
289 The @file{scripttempl} script is only invoked with @code{LD_FLAG} set to
290 this value if @code{GENERATE_COMBRELOC_SCRIPT} is defined in the
291 @file{emulparams} file or if @code{SCRIPT_NAME} is @code{elf} and
292 @code{GENERATE_SHLIB_SCRIPT} is defined in the @file{emulparams} file.
293 The @file{emultempl} script must arrange to use this script at the
294 appropriate time, normally when the linker is invoked with the @code{-shared
295 -z combreloc} option. The output has an extension of @file{.xsc}.
298 Like @emph{cshared} but in addition to needing the @option{-shared}
299 and @option{-z combreloc} options, it also needs the @option{-z relro}
300 option. If the @option{-z now} option is also used then the output has
301 an extension of @file{.xsw}, otherwise it is @file{.xsco}.
304 The @file{scripttempl} script is only invoked with @code{LD_FLAG} set to
305 this value if @code{GENERATE_PIE_SCRIPT} is defined in the
306 @file{emulparams} file. The @file{emultempl} script must arrange to use
307 this script at the appropriate time, normally when the linker is invoked
308 with the @code{-pie} option. The output has an extension of
311 In addition if the @file{scripttempl} script is also invoked with
312 @code{GENERATE_RELRO_SCRIPT} defined then a second script is created
313 with an extension of @file{.xdo} to match the @option{-pie} and
314 @option{-z relro} options.
317 Like @code{pie} but this version is used when the @file{scripttempl}
318 script is invoked with both @code{GENERATE_PIE_SCRIPT} and
319 @code{GENERATE_COMBRELOC_SCRIPT} defined. The @file{emultempl} script
320 must arrange to use this script at the appropriate time, normally when
321 the linker is invoked with the @code{-pie} and @option{-z combreloc}
322 options. The output has an extension of @file{.xdc}.
325 Like @code{cpie}, but in addition the @option{-z relro} and
326 @option{-z now} options need to be defined. The output has an
327 extension of @file{.xdw}.
337 @item csharedtextonly
338 @item wsharedtextonly
343 @item rosharedtextonly
344 @item rocsharedtextonly
345 @item rowsharedtextonly
352 The @file{scripttempl} script is only invoked with @code{LD_FLAG} set to
353 this value if @code{GENERATE_AUTO_IMPORT_SCRIPT} is defined in the
354 @file{emulparams} file. The @file{emultempl} script must arrange to
355 use this script at the appropriate time, normally when the linker is
356 invoked with the @code{--enable-auto-import} option. The output has
357 an extension of @file{.xa}.
362 Besides the shell variables set by the @file{emulparams} script, and the
363 @code{LD_FLAG} variable, the @file{genscripts.sh} script will set
364 certain variables for each run of the @file{scripttempl} script.
368 This will be set to a non-empty string when the linker is doing a final
369 relocation (e.g., all scripts other than @code{-r} and @code{-Ur}).
372 This will be set to a non-empty string when the linker is building
373 global constructor and destructor tables (e.g., all scripts other than
377 This will be set to an @code{ALIGN} expression when the output should be
378 page aligned, or to @samp{.} when generating the @code{-N} script.
381 This will be set to a non-empty string when generating a @code{-shared}
385 This will be set to a non-empty string when generating @code{-z combreloc}
386 scripts to a temporary file name which can be used during script generation.
389 The conventional way to write a @file{scripttempl} script is to first
390 set a few shell variables, and then write out a linker script using
391 @code{cat} with a here document. The linker script will use variable
392 substitutions, based on the above variables and those set in the
393 @file{emulparams} script, to control its behaviour.
395 When there are parts of the @file{scripttempl} script which should only
396 be run when doing a final relocation, they should be enclosed within a
397 variable substitution based on @code{RELOCATING}. For example, on many
398 targets special symbols such as @code{_end} should be defined when doing
399 a final link. Naturally, those symbols should not be defined when doing
400 a relocatable link using @code{-r}. The @file{scripttempl} script
401 could use a construct like this to define those symbols:
403 $@{RELOCATING+ _end = .;@}
405 This will do the symbol assignment only if the @code{RELOCATING}
408 The basic job of the linker script is to put the sections in the correct
409 order, and at the correct memory addresses. For some targets, the
410 linker script may have to do some other operations.
412 For example, on most MIPS platforms, the linker is responsible for
413 defining the special symbol @code{_gp}, used to initialize the
414 @code{$gp} register. It must be set to the start of the small data
415 section plus @code{0x8000}. Naturally, it should only be defined when
416 doing a final relocation. This will typically be done like this:
418 $@{RELOCATING+ _gp = ALIGN(16) + 0x8000;@}
420 This line would appear just before the sections which compose the small
421 data section (@samp{.sdata}, @samp{.sbss}). All those sections would be
422 contiguous in memory.
424 Many COFF systems build constructor tables in the linker script. The
425 compiler will arrange to output the address of each global constructor
426 in a @samp{.ctor} section, and the address of each global destructor in
427 a @samp{.dtor} section (this is done by defining
428 @code{ASM_OUTPUT_CONSTRUCTOR} and @code{ASM_OUTPUT_DESTRUCTOR} in the
429 @code{gcc} configuration files). The @code{gcc} runtime support
430 routines expect the constructor table to be named @code{__CTOR_LIST__}.
431 They expect it to be a list of words, with the first word being the
432 count of the number of entries. There should be a trailing zero word.
433 (Actually, the count may be -1 if the trailing word is present, and the
434 trailing word may be omitted if the count is correct, but, as the
435 @code{gcc} behaviour has changed slightly over the years, it is safest
436 to provide both). Here is a typical way that might be handled in a
437 @file{scripttempl} file.
439 $@{CONSTRUCTING+ __CTOR_LIST__ = .;@}
440 $@{CONSTRUCTING+ LONG((__CTOR_END__ - __CTOR_LIST__) / 4 - 2)@}
441 $@{CONSTRUCTING+ *(.ctors)@}
442 $@{CONSTRUCTING+ LONG(0)@}
443 $@{CONSTRUCTING+ __CTOR_END__ = .;@}
444 $@{CONSTRUCTING+ __DTOR_LIST__ = .;@}
445 $@{CONSTRUCTING+ LONG((__DTOR_END__ - __DTOR_LIST__) / 4 - 2)@}
446 $@{CONSTRUCTING+ *(.dtors)@}
447 $@{CONSTRUCTING+ LONG(0)@}
448 $@{CONSTRUCTING+ __DTOR_END__ = .;@}
450 The use of @code{CONSTRUCTING} ensures that these linker script commands
451 will only appear when the linker is supposed to be building the
452 constructor and destructor tables. This example is written for a target
453 which uses 4 byte pointers.
455 Embedded systems often need to set a stack address. This is normally
456 best done by using the @code{PROVIDE} construct with a default stack
457 address. This permits the user to easily override the stack address
458 using the @code{--defsym} option. Here is an example:
460 $@{RELOCATING+ PROVIDE (__stack = 0x80000000);@}
462 The value of the symbol @code{__stack} would then be used in the startup
463 code to initialize the stack pointer.
465 @node linker emulations
466 @section @file{emultempl} scripts
468 Each linker target uses an @file{emultempl} script to generate the
469 emulation code. The name of the @file{emultempl} script is set by the
470 @code{TEMPLATE_NAME} variable in the @file{emulparams} script. If the
471 @code{TEMPLATE_NAME} variable is not set, the default is
472 @samp{generic}. If the value of @code{TEMPLATE_NAME} is @var{template},
473 @file{genscripts.sh} will use @file{emultempl/@var{template}.em}.
475 Most targets use the generic @file{emultempl} script,
476 @file{emultempl/generic.em}. A different @file{emultempl} script is
477 only needed if the linker must support unusual actions, such as linking
478 against shared libraries.
480 The @file{emultempl} script is normally written as a simple invocation
481 of @code{cat} with a here document. The document will use a few
482 variable substitutions. Typically each function names uses a
483 substitution involving @code{EMULATION_NAME}, for ease of debugging when
484 the linker supports multiple emulations.
486 Every function and variable in the emitted file should be static. The
487 only globally visible object must be named
488 @code{ld_@var{EMULATION_NAME}_emulation}, where @var{EMULATION_NAME} is
489 the name of the emulation set in @file{configure.tgt} (this is also the
490 name of the @file{emulparams} file without the @file{.sh} extension).
491 The @file{genscripts.sh} script will set the shell variable
492 @code{EMULATION_NAME} before invoking the @file{emultempl} script.
494 The @code{ld_@var{EMULATION_NAME}_emulation} variable must be a
495 @code{struct ld_emulation_xfer_struct}, as defined in @file{ldemul.h}.
496 It defines a set of function pointers which are invoked by the linker,
497 as well as strings for the emulation name (normally set from the shell
498 variable @code{EMULATION_NAME} and the default BFD target name (normally
499 set from the shell variable @code{OUTPUT_FORMAT} which is normally set
500 by the @file{emulparams} file).
502 The @file{genscripts.sh} script will set the shell variable
503 @code{COMPILE_IN} when it invokes the @file{emultempl} script for the
504 default emulation. In this case, the @file{emultempl} script should
505 include the linker scripts directly, and return them from the
506 @code{get_scripts} entry point. When the emulation is not the default,
507 the @code{get_scripts} entry point should just return a file name. See
508 @file{emultempl/generic.em} for an example of how this is done.
510 At some point, the linker emulation entry points should be documented.
512 @node Emulation Walkthrough
513 @chapter A Walkthrough of a Typical Emulation
515 This chapter is to help people who are new to the way emulations
516 interact with the linker, or who are suddenly thrust into the position
517 of having to work with existing emulations. It will discuss the files
518 you need to be aware of. It will tell you when the given "hooks" in
519 the emulation will be called. It will, hopefully, give you enough
520 information about when and how things happen that you'll be able to
521 get by. As always, the source is the definitive reference to this.
523 The starting point for the linker is in @file{ldmain.c} where
524 @code{main} is defined. The bulk of the code that's emulation
525 specific will initially be in @code{emultempl/@var{emulation}.em} but
526 will end up in @code{e@var{emulation}.c} when the build is done.
527 Most of the work to select and interface with emulations is in
528 @code{ldemul.h} and @code{ldemul.c}. Specifically, @code{ldemul.h}
529 defines the @code{ld_emulation_xfer_struct} structure your emulation
532 Your emulation file exports a symbol
533 @code{ld_@var{EMULATION_NAME}_emulation}. If your emulation is
534 selected (it usually is, since usually there's only one),
535 @code{ldemul.c} sets the variable @var{ld_emulation} to point to it.
536 @code{ldemul.c} also defines a number of API functions that interface
537 to your emulation, like @code{ldemul_after_parse} which simply calls
538 your @code{ld_@var{EMULATION}_emulation.after_parse} function. For
539 the rest of this section, the functions will be mentioned, but you
540 should assume the indirect reference to your emulation also.
542 We will also skip or gloss over parts of the link process that don't
543 relate to emulations, like setting up internationalization.
545 After initialization, @code{main} selects an emulation by pre-scanning
546 the command-line arguments. It calls @code{ldemul_choose_target} to
547 choose a target. If you set @code{choose_target} to
548 @code{ldemul_default_target}, it picks your @code{target_name} by
551 @code{main} calls @code{ldemul_before_parse}, then @code{parse_args}.
552 @code{parse_args} calls @code{ldemul_parse_args} for each arg, which
553 must update the @code{getopt} globals if it recognizes the argument.
554 If the emulation doesn't recognize it, then parse_args checks to see
557 Now that the emulation has had access to all its command-line options,
558 @code{main} calls @code{ldemul_set_symbols}. This can be used for any
559 initialization that may be affected by options. It is also supposed
560 to set up any variables needed by the emulation script.
562 @code{main} now calls @code{ldemul_get_script} to get the emulation
563 script to use (based on arguments, no doubt, @pxref{Emulations}) and
564 runs it. While parsing, @code{ldgram.y} may call @code{ldemul_hll} or
565 @code{ldemul_syslib} to handle the @code{HLL} or @code{SYSLIB}
566 commands. It may call @code{ldemul_unrecognized_file} if you asked
567 the linker to link a file it doesn't recognize. It will call
568 @code{ldemul_recognized_file} for each file it does recognize, in case
569 the emulation wants to handle some files specially. All the while,
570 it's loading the files (possibly calling
571 @code{ldemul_open_dynamic_archive}) and symbols and stuff. After it's
572 done reading the script, @code{main} calls @code{ldemul_after_parse}.
573 Use the after-parse hook to set up anything that depends on stuff the
574 script might have set up, like the entry point.
576 @code{main} next calls @code{lang_process} in @code{ldlang.c}. This
577 appears to be the main core of the linking itself, as far as emulation
578 hooks are concerned(*). It first opens the output file's BFD, calling
579 @code{ldemul_set_output_arch}, and calls
580 @code{ldemul_create_output_section_statements} in case you need to use
581 other means to find or create object files (i.e. shared libraries
582 found on a path, or fake stub objects). Despite the name, nobody
583 creates output sections here.
585 (*) In most cases, the BFD library does the bulk of the actual
586 linking, handling symbol tables, symbol resolution, relocations, and
587 building the final output file. See the BFD reference for all the
588 details. Your emulation is usually concerned more with managing
589 things at the file and section level, like "put this here, add this
592 Next, the objects to be linked are opened and BFDs created for them,
593 and @code{ldemul_after_open} is called. At this point, you have all
594 the objects and symbols loaded, but none of the data has been placed
597 Next comes the Big Linking Thingy (except for the parts BFD does).
598 All input sections are mapped to output sections according to the
599 script. If a section doesn't get mapped by default,
600 @code{ldemul_place_orphan} will get called to figure out where it goes.
601 Next it figures out the offsets for each section, calling
602 @code{ldemul_before_allocation} before and
603 @code{ldemul_after_allocation} after deciding where each input section
604 ends up in the output sections.
606 The last part of @code{lang_process} is to figure out all the symbols'
607 values. After assigning final values to the symbols,
608 @code{ldemul_finish} is called, and after that, any undefined symbols
609 are turned into fatal errors.
611 OK, back to @code{main}, which calls @code{ldwrite} in
612 @file{ldwrite.c}. @code{ldwrite} calls BFD's final_link, which does
613 all the relocation fixups and writes the output bfd to disk, and we're
620 @item @code{main()} in @file{ldmain.c}
621 @item @file{emultempl/@var{EMULATION}.em} has your code
622 @item @code{ldemul_choose_target} (defaults to your @code{target_name})
623 @item @code{ldemul_before_parse}
624 @item Parse argv, calls @code{ldemul_parse_args} for each
625 @item @code{ldemul_set_symbols}
626 @item @code{ldemul_get_script}
630 @item may call @code{ldemul_hll} or @code{ldemul_syslib}
631 @item may call @code{ldemul_open_dynamic_archive}
634 @item @code{ldemul_after_parse}
635 @item @code{lang_process()} in @file{ldlang.c}
638 @item create @code{output_bfd}
639 @item @code{ldemul_set_output_arch}
640 @item @code{ldemul_create_output_section_statements}
641 @item read objects, create input bfds - all symbols exist, but have no values
642 @item may call @code{ldemul_unrecognized_file}
643 @item will call @code{ldemul_recognized_file}
644 @item @code{ldemul_after_open}
645 @item map input sections to output sections
646 @item may call @code{ldemul_place_orphan} for remaining sections
647 @item @code{ldemul_before_allocation}
648 @item gives input sections offsets into output sections, places output sections
649 @item @code{ldemul_after_allocation} - section addresses valid
650 @item assigns values to symbols
651 @item @code{ldemul_finish} - symbol values valid
654 @item output bfd is written to disk
658 @node Architecture Specific
659 @chapter Some Architecture Specific Notes
661 This is the place for notes on the behavior of @code{ld} on
662 specific platforms. Currently, only Intel x86 is documented (and
663 of that, only the auto-import behavior for DLLs).
673 @code{ld} can create DLLs that operate with various runtimes available
674 on a common x86 operating system. These runtimes include native (using
675 the mingw "platform"), cygwin, and pw.
677 @item auto-import from DLLs
680 With this feature on, DLL clients can import variables from DLL
681 without any concern from their side (for example, without any source
682 code modifications). Auto-import can be enabled using the
683 @code{--enable-auto-import} flag, or disabled via the
684 @code{--disable-auto-import} flag. Auto-import is disabled by default.
687 This is done completely in bounds of the PE specification (to be fair,
688 there's a minor violation of the spec at one point, but in practice
689 auto-import works on all known variants of that common x86 operating
690 system) So, the resulting DLL can be used with any other PE
694 Auto-import is fully compatible with standard import method, in which
695 variables are decorated using attribute modifiers. Libraries of either
696 type may be mixed together.
699 Overhead (space): 8 bytes per imported symbol, plus 20 for each
700 reference to it; Overhead (load time): negligible; Overhead
701 (virtual/physical memory): should be less than effect of DLL
707 The obvious and only way to get rid of dllimport insanity is
708 to make client access variable directly in the DLL, bypassing
709 the extra dereference imposed by ordinary DLL runtime linking.
710 I.e., whenever client contains something like
712 @code{mov dll_var,%eax,}
714 address of dll_var in the command should be relocated to point
715 into loaded DLL. The aim is to make OS loader do so, and than
716 make ld help with that. Import section of PE made following
717 way: there's a vector of structures each describing imports
718 from particular DLL. Each such structure points to two other
719 parallel vectors: one holding imported names, and one which
720 will hold address of corresponding imported name. So, the
721 solution is de-vectorize these structures, making import
722 locations be sparse and pointing directly into code.
726 For each reference of data symbol to be imported from DLL (to
727 set of which belong symbols with name <sym>, if __imp_<sym> is
728 found in implib), the import fixup entry is generated. That
729 entry is of type IMAGE_IMPORT_DESCRIPTOR and stored in .idata$3
730 subsection. Each fixup entry contains pointer to symbol's address
731 within .text section (marked with __fuN_<sym> symbol, where N is
732 integer), pointer to DLL name (so, DLL name is referenced by
733 multiple entries), and pointer to symbol name thunk. Symbol name
734 thunk is singleton vector (__nm_th_<symbol>) pointing to
735 IMAGE_IMPORT_BY_NAME structure (__nm_<symbol>) directly containing
736 imported name. Here comes that "om the edge" problem mentioned above:
737 PE specification rambles that name vector (OriginalFirstThunk) should
738 run in parallel with addresses vector (FirstThunk), i.e. that they
739 should have same number of elements and terminated with zero. We violate
740 this, since FirstThunk points directly into machine code. But in
741 practice, OS loader implemented the sane way: it goes thru
742 OriginalFirstThunk and puts addresses to FirstThunk, not something
743 else. It once again should be noted that dll and symbol name
744 structures are reused across fixup entries and should be there
745 anyway to support standard import stuff, so sustained overhead is
746 20 bytes per reference. Other question is whether having several
747 IMAGE_IMPORT_DESCRIPTORS for the same DLL is possible. Answer is yes,
748 it is done even by native compiler/linker (libth32's functions are in
749 fact resident in windows9x kernel32.dll, so if you use it, you have
750 two IMAGE_IMPORT_DESCRIPTORS for kernel32.dll). Yet other question is
751 whether referencing the same PE structures several times is valid.
752 The answer is why not, prohibiting that (detecting violation) would
753 require more work on behalf of loader than not doing it.
757 @node GNU Free Documentation License
758 @chapter GNU Free Documentation License