⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 linker.texi

📁 这个是LINUX下的GDB调度工具的源码
💻 TEXI
📖 第 1 页 / 共 2 页
字号:
@section Linker Functions@cindex LinkerThe linker uses three special entry points in the BFD targetvector.  It is not necessary to write special routines forthese entry points when creating a new BFD back end, sincegeneric versions are provided.  However, writing them canspeed up linking and make it use significantly less runtimememory.The first routine creates a hash table used by the otherroutines.  The second routine adds the symbols from an objectfile to the hash table.  The third routine takes all theobject files and links them together to create the outputfile.  These routines are designed so that the linker properdoes not need to know anything about the symbols in the objectfiles that it is linking.  The linker merely arranges thesections as directed by the linker script and lets BFD handlethe details of symbols and relocs.The second routine and third routines are passed a pointer toa @code{struct bfd_link_info} structure (defined in@code{bfdlink.h}) which holds information relevant to the link,including the linker hash table (which was created by thefirst routine) and a set of callback functions to the linkerproper.The generic linker routines are in @code{linker.c}, and use theheader file @code{genlink.h}.  As of this writing, the only backends which have implemented versions of these routines area.out (in @code{aoutx.h}) and ECOFF (in @code{ecoff.c}).  The a.outroutines are used as examples throughout this section.@menu* Creating a Linker Hash Table::* Adding Symbols to the Hash Table::* Performing the Final Link::@end menu@node Creating a Linker Hash Table, Adding Symbols to the Hash Table, Linker Functions, Linker Functions@subsection Creating a linker hash table@cindex _bfd_link_hash_table_create in target vector@cindex target vector (_bfd_link_hash_table_create)The linker routines must create a hash table, which must bederived from @code{struct bfd_link_hash_table} described in@code{bfdlink.c}.  @xref{Hash Tables}, for information on how tocreate a derived hash table.  This entry point is called usingthe target vector of the linker output file.The @code{_bfd_link_hash_table_create} entry point must allocateand initialize an instance of the desired hash table.  If theback end does not require any additional information to bestored with the entries in the hash table, the entry point maysimply create a @code{struct bfd_link_hash_table}.  Most likely,however, some additional information will be needed.For example, with each entry in the hash table the a.outlinker keeps the index the symbol has in the final output file(this index number is used so that when doing a relocatablelink the symbol index used in the output file can be quicklyfilled in when copying over a reloc).  The a.out linker codedefines the required structures and functions for a hash tablederived from @code{struct bfd_link_hash_table}.  The a.out linkerhash table is created by the function@code{NAME(aout,link_hash_table_create)}; it simply allocatesspace for the hash table, initializes it, and returns apointer to it.When writing the linker routines for a new back end, you willgenerally not know exactly which fields will be required untilyou have finished.  You should simply create a new hash tablewhich defines no additional fields, and then simply add fieldsas they become necessary.@node Adding Symbols to the Hash Table, Performing the Final Link, Creating a Linker Hash Table, Linker Functions@subsection Adding symbols to the hash table@cindex _bfd_link_add_symbols in target vector@cindex target vector (_bfd_link_add_symbols)The linker proper will call the @code{_bfd_link_add_symbols}entry point for each object file or archive which is to belinked (typically these are the files named on the commandline, but some may also come from the linker script).  Theentry point is responsible for examining the file.  For anobject file, BFD must add any relevant symbol information tothe hash table.  For an archive, BFD must determine whichelements of the archive should be used and adding them to thelink.The a.out version of this entry point is@code{NAME(aout,link_add_symbols)}.@menu* Differing file formats::* Adding symbols from an object file::* Adding symbols from an archive::@end menu@node Differing file formats, Adding symbols from an object file, Adding Symbols to the Hash Table, Adding Symbols to the Hash Table@subsubsection Differing file formatsNormally all the files involved in a link will be of the sameformat, but it is also possible to link together differentformat object files, and the back end must support that.  The@code{_bfd_link_add_symbols} entry point is called via the targetvector of the file to be added.  This has an importantconsequence: the function may not assume that the hash tableis the type created by the corresponding@code{_bfd_link_hash_table_create} vector.  All the@code{_bfd_link_add_symbols} function can assume about the hashtable is that it is derived from @code{structbfd_link_hash_table}.Sometimes the @code{_bfd_link_add_symbols} function must storesome information in the hash table entry to be used by the@code{_bfd_final_link} function.  In such a case the @code{creator}field of the hash table must be checked to make sure that thehash table was created by an object file of the same format.The @code{_bfd_final_link} routine must be prepared to handle ahash entry without any extra information added by the@code{_bfd_link_add_symbols} function.  A hash entry withoutextra information will also occur when the linker scriptdirects the linker to create a symbol.  Note that, regardlessof how a hash table entry is added, all the fields will beinitialized to some sort of null value by the hash table entryinitialization function.See @code{ecoff_link_add_externals} for an example of how tocheck the @code{creator} field before saving information (in thiscase, the ECOFF external symbol debugging information) in ahash table entry.@node Adding symbols from an object file, Adding symbols from an archive, Differing file formats, Adding Symbols to the Hash Table@subsubsection Adding symbols from an object fileWhen the @code{_bfd_link_add_symbols} routine is passed an objectfile, it must add all externally visible symbols in thatobject file to the hash table.  The actual work of adding thesymbol to the hash table is normally handled by the function@code{_bfd_generic_link_add_one_symbol}.  The@code{_bfd_link_add_symbols} routine is responsible for readingall the symbols from the object file and passing the correctinformation to @code{_bfd_generic_link_add_one_symbol}.The @code{_bfd_link_add_symbols} routine should not use@code{bfd_canonicalize_symtab} to read the symbols.  The point ofproviding this routine is to avoid the overhead of convertingthe symbols into generic @code{asymbol} structures.@findex _bfd_generic_link_add_one_symbol@code{_bfd_generic_link_add_one_symbol} handles the details ofcombining common symbols, warning about multiple definitions,and so forth.  It takes arguments which describe the symbol toadd, notably symbol flags, a section, and an offset.  Thesymbol flags include such things as @code{BSF_WEAK} or@code{BSF_INDIRECT}.  The section is a section in the objectfile, or something like @code{bfd_und_section_ptr} for an undefinedsymbol or @code{bfd_com_section_ptr} for a common symbol.If the @code{_bfd_final_link} routine is also going to need toread the symbol information, the @code{_bfd_link_add_symbols}routine should save it somewhere attached to the object fileBFD.  However, the information should only be saved if the@code{keep_memory} field of the @code{info} argument is TRUE, sothat the @code{-no-keep-memory} linker switch is effective.The a.out function which adds symbols from an object file is@code{aout_link_add_object_symbols}, and most of the interestingwork is in @code{aout_link_add_symbols}.  The latter savespointers to the hash tables entries created by@code{_bfd_generic_link_add_one_symbol} indexed by symbol number,so that the @code{_bfd_final_link} routine does not have to callthe hash table lookup routine to locate the entry.@node Adding symbols from an archive, , Adding symbols from an object file, Adding Symbols to the Hash Table@subsubsection Adding symbols from an archiveWhen the @code{_bfd_link_add_symbols} routine is passed anarchive, it must look through the symbols defined by thearchive and decide which elements of the archive should beincluded in the link.  For each such element it must call the@code{add_archive_element} linker callback, and it must add thesymbols from the object file to the linker hash table.@findex _bfd_generic_link_add_archive_symbolsIn most cases the work of looking through the symbols in thearchive should be done by the@code{_bfd_generic_link_add_archive_symbols} function.  Thisfunction builds a hash table from the archive symbol table andlooks through the list of undefined symbols to see whichelements should be included.@code{_bfd_generic_link_add_archive_symbols} is passed a functionto call to make the final decision about adding an archiveelement to the link and to do the actual work of adding thesymbols to the linker hash table.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -