📄 linker.texi
字号:
The function passed to@code{_bfd_generic_link_add_archive_symbols} must read thesymbols of the archive element and decide whether the archiveelement should be included in the link. If the element is tobe included, the @code{add_archive_element} linker callbackroutine must be called with the element as an argument, andthe elements symbols must be added to the linker hash tablejust as though the element had itself been passed to the@code{_bfd_link_add_symbols} function.When the a.out @code{_bfd_link_add_symbols} function receives anarchive, it calls @code{_bfd_generic_link_add_archive_symbols}passing @code{aout_link_check_archive_element} as the functionargument. @code{aout_link_check_archive_element} calls@code{aout_link_check_ar_symbols}. If the latter decides to addthe element (an element is only added if it provides a real,non-common, definition for a previously undefined or commonsymbol) it calls the @code{add_archive_element} callback and then@code{aout_link_check_archive_element} calls@code{aout_link_add_symbols} to actually add the symbols to thelinker hash table.The ECOFF back end is unusual in that it does not normallycall @code{_bfd_generic_link_add_archive_symbols}, because ECOFFarchives already contain a hash table of symbols. The ECOFFback end searches the archive itself to avoid the overhead ofcreating a new hash table.@node Performing the Final Link, , Adding Symbols to the Hash Table, Linker Functions@subsection Performing the final link@cindex _bfd_link_final_link in target vector@cindex target vector (_bfd_final_link)When all the input files have been processed, the linker callsthe @code{_bfd_final_link} entry point of the output BFD. Thisroutine is responsible for producing the final output file,which has several aspects. It must relocate the contents ofthe input sections and copy the data into the output sections.It must build an output symbol table including any localsymbols from the input files and the global symbols from thehash table. When producing relocatable output, it mustmodify the input relocs and write them into the output file.There may also be object format dependent work to be done.The linker will also call the @code{write_object_contents} entrypoint when the BFD is closed. The two entry points must worktogether in order to produce the correct output file.The details of how this works are inevitably dependent uponthe specific object file format. The a.out@code{_bfd_final_link} routine is @code{NAME(aout,final_link)}.@menu* Information provided by the linker::* Relocating the section contents::* Writing the symbol table::@end menu@node Information provided by the linker, Relocating the section contents, Performing the Final Link, Performing the Final Link@subsubsection Information provided by the linkerBefore the linker calls the @code{_bfd_final_link} entry point,it sets up some data structures for the function to use.The @code{input_bfds} field of the @code{bfd_link_info} structurewill point to a list of all the input files included in thelink. These files are linked through the @code{link_next} fieldof the @code{bfd} structure.Each section in the output file will have a list of@code{link_order} structures attached to the @code{link_order_head}field (the @code{link_order} structure is defined in@code{bfdlink.h}). These structures describe how to create thecontents of the output section in terms of the contents ofvarious input sections, fill constants, and, eventually, othertypes of information. They also describe relocs that must becreated by the BFD backend, but do not correspond to any inputfile; this is used to support -Ur, which builds constructorswhile generating a relocatable object file.@node Relocating the section contents, Writing the symbol table, Information provided by the linker, Performing the Final Link@subsubsection Relocating the section contentsThe @code{_bfd_final_link} function should look through the@code{link_order} structures attached to each section of theoutput file. Each @code{link_order} structure should either behandled specially, or it should be passed to the function@code{_bfd_default_link_order} which will do the right thing(@code{_bfd_default_link_order} is defined in @code{linker.c}).For efficiency, a @code{link_order} of type@code{bfd_indirect_link_order} whose associated section belongsto a BFD of the same format as the output BFD must be handledspecially. This type of @code{link_order} describes part of anoutput section in terms of a section belonging to one of theinput files. The @code{_bfd_final_link} function should read thecontents of the section and any associated relocs, apply therelocs to the section contents, and write out the modifiedsection contents. If performing a relocatable link, therelocs themselves must also be modified and written out.@findex _bfd_relocate_contents@findex _bfd_final_link_relocateThe functions @code{_bfd_relocate_contents} and@code{_bfd_final_link_relocate} provide some general support forperforming the actual relocations, notably overflow checking.Their arguments include information about the symbol therelocation is against and a @code{reloc_howto_type} argumentwhich describes the relocation to perform. These functionsare defined in @code{reloc.c}.The a.out function which handles reading, relocating, andwriting section contents is @code{aout_link_input_section}. Theactual relocation is done in @code{aout_link_input_section_std}and @code{aout_link_input_section_ext}.@node Writing the symbol table, , Relocating the section contents, Performing the Final Link@subsubsection Writing the symbol tableThe @code{_bfd_final_link} function must gather all the symbolsin the input files and write them out. It must also write outall the symbols in the global hash table. This must becontrolled by the @code{strip} and @code{discard} fields of the@code{bfd_link_info} structure.The local symbols of the input files will not have beenentered into the linker hash table. The @code{_bfd_final_link}routine must consider each input file and include the symbolsin the output file. It may be convenient to do this whenlooking through the @code{link_order} structures, or it may bedone by stepping through the @code{input_bfds} list.The @code{_bfd_final_link} routine must also traverse the globalhash table to gather all the externally visible symbols. Itis possible that most of the externally visible symbols may bewritten out when considering the symbols of each input file,but it is still necessary to traverse the hash table since thelinker script may have defined some symbols that are not inany of the input files.The @code{strip} field of the @code{bfd_link_info} structurecontrols which symbols are written out. The possible valuesare listed in @code{bfdlink.h}. If the value is @code{strip_some},then the @code{keep_hash} field of the @code{bfd_link_info}structure is a hash table of symbols to keep; each symbolshould be looked up in this hash table, and only symbols whichare present should be included in the output file.If the @code{strip} field of the @code{bfd_link_info} structurepermits local symbols to be written out, the @code{discard} fieldis used to further controls which local symbols are includedin the output file. If the value is @code{discard_l}, then alllocal symbols which begin with a certain prefix are discarded;this is controlled by the @code{bfd_is_local_label_name} entry point.The a.out backend handles symbols by calling@code{aout_link_write_symbols} on each input BFD and thentraversing the global hash table with the function@code{aout_link_write_other_symbol}. It builds a string tablewhile writing out the symbols, which is written to the outputfile at the end of @code{NAME(aout,final_link)}.@findex bfd_link_split_section@subsubsection @code{bfd_link_split_section}@strong{Synopsis}@examplebfd_boolean bfd_link_split_section (bfd *abfd, asection *sec);@end example@strong{Description}@*Return nonzero if @var{sec} should be split during areloceatable or final link.@example#define bfd_link_split_section(abfd, sec) \ BFD_SEND (abfd, _bfd_link_split_section, (abfd, sec))@end example@findex bfd_section_already_linked@subsubsection @code{bfd_section_already_linked}@strong{Synopsis}@examplevoid bfd_section_already_linked (bfd *abfd, asection *sec);@end example@strong{Description}@*Check if @var{sec} has been already linked during a reloceatableor final link.@example#define bfd_section_already_linked(abfd, sec) \ BFD_SEND (abfd, _section_already_linked, (abfd, sec))@end example
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -