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