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