📄 ldint.texinfo
字号:
targets special symbols such as @code{_end} should be defined when doinga final link. Naturally, those symbols should not be defined when doinga relocateable link using @code{-r}. The @file{scripttempl} scriptcould use a construct like this to define those symbols:@smallexample $@{RELOCATING+ _end = .;@}@end smallexampleThis will do the symbol assignment only if the @code{RELOCATING}variable is defined.The basic job of the linker script is to put the sections in the correctorder, and at the correct memory addresses. For some targets, thelinker script may have to do some other operations.For example, on most MIPS platforms, the linker is responsible fordefining the special symbol @code{_gp}, used to initialize the@code{$gp} register. It must be set to the start of the small datasection plus @code{0x8000}. Naturally, it should only be defined whendoing a final relocation. This will typically be done like this:@smallexample $@{RELOCATING+ _gp = ALIGN(16) + 0x8000;@}@end smallexampleThis line would appear just before the sections which compose the smalldata section (@samp{.sdata}, @samp{.sbss}). All those sections would becontiguous in memory.Many COFF systems build constructor tables in the linker script. Thecompiler will arrange to output the address of each global constructorin a @samp{.ctor} section, and the address of each global destructor ina @samp{.dtor} section (this is done by defining@code{ASM_OUTPUT_CONSTRUCTOR} and @code{ASM_OUTPUT_DESTRUCTOR} in the@code{gcc} configuration files). The @code{gcc} runtime supportroutines expect the constructor table to be named @code{__CTOR_LIST__}.They expect it to be a list of words, with the first word being thecount of the number of entries. There should be a trailing zero word.(Actually, the count may be -1 if the trailing word is present, and thetrailing word may be omitted if the count is correct, but, as the@code{gcc} behaviour has changed slightly over the years, it is safestto provide both). Here is a typical way that might be handled in a@file{scripttempl} file.@smallexample $@{CONSTRUCTING+ __CTOR_LIST__ = .;@} $@{CONSTRUCTING+ LONG((__CTOR_END__ - __CTOR_LIST__) / 4 - 2)@} $@{CONSTRUCTING+ *(.ctors)@} $@{CONSTRUCTING+ LONG(0)@} $@{CONSTRUCTING+ __CTOR_END__ = .;@} $@{CONSTRUCTING+ __DTOR_LIST__ = .;@} $@{CONSTRUCTING+ LONG((__DTOR_END__ - __DTOR_LIST__) / 4 - 2)@} $@{CONSTRUCTING+ *(.dtors)@} $@{CONSTRUCTING+ LONG(0)@} $@{CONSTRUCTING+ __DTOR_END__ = .;@}@end smallexampleThe use of @code{CONSTRUCTING} ensures that these linker script commandswill only appear when the linker is supposed to be building theconstructor and destructor tables. This example is written for a targetwhich uses 4 byte pointers.Embedded systems often need to set a stack address. This is normallybest done by using the @code{PROVIDE} construct with a default stackaddress. This permits the user to easily override the stack addressusing the @code{--defsym} option. Here is an example:@smallexample $@{RELOCATING+ PROVIDE (__stack = 0x80000000);@}@end smallexampleThe value of the symbol @code{__stack} would then be used in the startupcode to initialize the stack pointer.@node linker emulations@section @file{emultempl} scriptsEach linker target uses an @file{emultempl} script to generate theemulation code. The name of the @file{emultempl} script is set by the@code{TEMPLATE_NAME} variable in the @file{emulparams} script. If the@code{TEMPLATE_NAME} variable is not set, the default is@samp{generic}. If the value of @code{TEMPLATE_NAME} is @var{template},@file{genscripts.sh} will use @file{emultempl/@var{template}.em}.Most targets use the generic @file{emultempl} script,@file{emultempl/generic.em}. A different @file{emultempl} script isonly needed if the linker must support unusual actions, such as linkingagainst shared libraries.The @file{emultempl} script is normally written as a simple invocationof @code{cat} with a here document. The document will use a fewvariable substitutions. Typically each function names uses asubstitution involving @code{EMULATION_NAME}, for ease of debugging whenthe linker supports multiple emulations.Every function and variable in the emitted file should be static. Theonly globally visible object must be named@code{ld_@var{EMULATION_NAME}_emulation}, where @var{EMULATION_NAME} isthe name of the emulation set in @file{configure.tgt} (this is also thename of the @file{emulparams} file without the @file{.sh} extension).The @file{genscripts.sh} script will set the shell variable@code{EMULATION_NAME} before invoking the @file{emultempl} script.The @code{ld_@var{EMULATION_NAME}_emulation} variable must be a@code{struct ld_emulation_xfer_struct}, as defined in @file{ldemul.h}.It defines a set of function pointers which are invoked by the linker,as well as strings for the emulation name (normally set from the shellvariable @code{EMULATION_NAME} and the default BFD target name (normallyset from the shell variable @code{OUTPUT_FORMAT} which is normally setby the @file{emulparams} file).The @file{genscripts.sh} script will set the shell variable@code{COMPILE_IN} when it invokes the @file{emultempl} script for thedefault emulation. In this case, the @file{emultempl} script shouldinclude the linker scripts directly, and return them from the@code{get_scripts} entry point. When the emulation is not the default,the @code{get_scripts} entry point should just return a file name. See@file{emultempl/generic.em} for an example of how this is done.At some point, the linker emulation entry points should be documented.@node Emulation Walkthrough@chapter A Walkthrough of a Typical EmulationThis chapter is to help people who are new to the way emulationsinteract with the linker, or who are suddenly thrust into the positionof having to work with existing emulations. It will discuss the filesyou need to be aware of. It will tell you when the given "hooks" inthe emulation will be called. It will, hopefully, give you enoughinformation about when and how things happen that you'll be able toget by. As always, the source is the definitive reference to this.The starting point for the linker is in @file{ldmain.c} where@code{main} is defined. The bulk of the code that's emulationspecific will initially be in @code{emultempl/@var{emulation}.em} butwill end up in @code{e@var{emulation}.c} when the build is done.Most of the work to select and interface with emulations is in@code{ldemul.h} and @code{ldemul.c}. Specifically, @code{ldemul.h}defines the @code{ld_emulation_xfer_struct} structure your emulationexports.Your emulation file exports a symbol@code{ld_@var{EMULATION_NAME}_emulation}. If your emulation isselected (it usually is, since usually there's only one),@code{ldemul.c} sets the variable @var{ld_emulation} to point to it.@code{ldemul.c} also defines a number of API functions that interfaceto your emulation, like @code{ldemul_after_parse} which simply callsyour @code{ld_@var{EMULATION}_emulation.after_parse} function. Forthe rest of this section, the functions will be mentioned, but youshould assume the indirect reference to your emulation also.We will also skip or gloss over parts of the link process that don'trelate to emulations, like setting up internationalization.After initialization, @code{main} selects an emulation by pre-scanningthe command line arguments. It calls @code{ldemul_choose_target} tochoose a target. If you set @code{choose_target} to@code{ldemul_default_target}, it picks your @code{target_name} bydefault.@code{main} calls @code{ldemul_before_parse}, then @code{parse_args}.@code{parse_args} calls @code{ldemul_parse_args} for each arg, whichmust update the @code{getopt} globals if it recognizes the argument.If the emulation doesn't recognize it, then parse_args checks to seeif it recognizes it.Now that the emulation has had access to all its command-line options,@code{main} calls @code{ldemul_set_symbols}. This can be used for anyinitialization that may be affected by options. It is also supposedto set up any variables needed by the emulation script.@code{main} now calls @code{ldemul_get_script} to get the emulationscript to use (based on arguments, no doubt, @pxref{Emulations}) andruns it. While parsing, @code{ldgram.y} may call @code{ldemul_hll} or@code{ldemul_syslib} to handle the @code{HLL} or @code{SYSLIB}commands. It may call @code{ldemul_unrecognized_file} if you askedthe linker to link a file it doesn't recognize. It will call@code{ldemul_recognized_file} for each file it does recognize, in casethe emulation wants to handle some files specially. All the while,it's loading the files (possibly calling@code{ldemul_open_dynamic_archive}) and symbols and stuff. After it'sdone reading the script, @code{main} calls @code{ldemul_after_parse}.Use the after-parse hook to set up anything that depends on stuff thescript might have set up, like the entry point.@code{main} next calls @code{lang_process} in @code{ldlang.c}. Thisappears to be the main core of the linking itself, as far as emulationhooks are concerned(*). It first opens the output file's BFD, calling@code{ldemul_set_output_arch}, and calls@code{ldemul_create_output_section_statements} in case you need to useother means to find or create object files (i.e. shared librariesfound on a path, or fake stub objects). Despite the name, nobodycreates output sections here.(*) In most cases, the BFD library does the bulk of the actuallinking, handling symbol tables, symbol resolution, relocations, andbuilding the final output file. See the BFD reference for all thedetails. Your emulation is usually concerned more with managingthings at the file and section level, like "put this here, add thissection", etc.Next, the objects to be linked are opened and BFDs created for them,and @code{ldemul_after_open} is called. At this point, you have allthe objects and symbols loaded, but none of the data has been placedyet.Next comes the Big Linking Thingy (except for the parts BFD does).All input sections are mapped to output sections according to thescript. If a section doesn't get mapped by default,@code{ldemul_place_orphan} will get called to figure out where it goes.Next it figures out the offsets for each section, calling@code{ldemul_before_allocation} before and@code{ldemul_after_allocation} after deciding where each input sectionends up in the output sections.The last part of @code{lang_process} is to figure out all the symbols'values. After assigning final values to the symbols,@code{ldemul_finish} is called, and after that, any undefined symbolsare turned into fatal errors.OK, back to @code{main}, which calls @code{ldwrite} in@file{ldwrite.c}. @code{ldwrite} calls BFD's final_link, which doesall the relocation fixups and writes the output bfd to disk, and we'redone.In summary,@itemize @bullet@item @code{main()} in @file{ldmain.c}@item @file{emultempl/@var{EMULATION}.em} has your code@item @code{ldemul_choose_target} (defaults to your @code{target_name})@item @code{ldemul_before_parse}@item Parse argv, calls @code{ldemul_parse_args} for each@item @code{ldemul_set_symbols}@item @code{ldemul_get_script}@item parse script@itemize @bullet@item may call @code{ldemul_hll} or @code{ldemul_syslib}@item may call @code{ldemul_open_dynamic_archive}@end itemize@item @code{ldemul_after_parse}@item @code{lang_process()} in @file{ldlang.c}@itemize @bullet@item create @code{output_bfd}@item @code{ldemul_set_output_arch}@item @code{ldemul_create_output_section_statements}@item read objects, create input bfds - all symbols exist, but have no values@item may call @code{ldemul_unrecognized_file}@item will call @code{ldemul_recognized_file}@item @code{ldemul_after_open}@item map input sections to output sections@item may call @code{ldemul_place_orphan} for remaining sections@item @code{ldemul_before_allocation}@item gives input sections offsets into output sections, places output sections@item @code{ldemul_after_allocation} - section addresses valid@item assigns values to symbols@item @code{ldemul_finish} - symbol values valid@end itemize@item output bfd is written to disk@end itemize@node GNU Free Documentation License@chapter GNU Free Documentation License GNU Free Documentation License Version 1.1, March 2000 Copyright (C) 2000 Free Software Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed.0. PREAMBLEThe purpose of this License is to make a manual, textbook, or otherwritten document "free" in the sense of freedom: to assure everyonethe effective freedom to copy and redistribute it, with or withoutmodifying it, either commercially or noncommercially. Secondarily,this License preserves for the author and publisher a way to getcredit for their work, while not being considered responsible formodifications made by others.This License is a kind of "copyleft", which means that derivativeworks of the document must themselves be free in the same sense. Itcomplements the GNU General Public License, which is a copyleftlicense designed for free software.We have designed this License in order to use it for manuals for freesoftware, because free software needs free documentation: a freeprogram should come with manuals providing the same freedoms that thesoftware does. But this License is not limited to software manuals;it can be used for any textual work, regardless of subject matter orwhether it is published as a printed book. We recommend this Licenseprincipally for works whose purpose is instruction or reference.1. APPLICABILITY AND DEFINITIONSThis License applies to any manual or other work that contains anotice placed by the copyright holder saying it can be distributedunder the terms of this License. The "Document", below, refers to anysuch manual or work. Any member of the public is a licensee, and isaddressed as "you".A "Modified Version" of the Document means any work containing theDocument or a portion of it, either copied verbatim, or withmodifications and/or translated into another language.A "Secondary Section" is a named appendix or a front-matter section ofthe Document that deals exclusively with the relationship of thepublishers or authors of the Document to the Document's overall subject
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -