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