| 1 | @section Linker Functions | 
|---|
| 2 | @cindex Linker | 
|---|
| 3 | The linker uses three special entry points in the BFD target | 
|---|
| 4 | vector.  It is not necessary to write special routines for | 
|---|
| 5 | these entry points when creating a new BFD back end, since | 
|---|
| 6 | generic versions are provided.  However, writing them can | 
|---|
| 7 | speed up linking and make it use significantly less runtime | 
|---|
| 8 | memory. | 
|---|
| 9 |  | 
|---|
| 10 | The first routine creates a hash table used by the other | 
|---|
| 11 | routines.  The second routine adds the symbols from an object | 
|---|
| 12 | file to the hash table.  The third routine takes all the | 
|---|
| 13 | object files and links them together to create the output | 
|---|
| 14 | file.  These routines are designed so that the linker proper | 
|---|
| 15 | does not need to know anything about the symbols in the object | 
|---|
| 16 | files that it is linking.  The linker merely arranges the | 
|---|
| 17 | sections as directed by the linker script and lets BFD handle | 
|---|
| 18 | the details of symbols and relocs. | 
|---|
| 19 |  | 
|---|
| 20 | The second routine and third routines are passed a pointer to | 
|---|
| 21 | a @code{struct bfd_link_info} structure (defined in | 
|---|
| 22 | @code{bfdlink.h}) which holds information relevant to the link, | 
|---|
| 23 | including the linker hash table (which was created by the | 
|---|
| 24 | first routine) and a set of callback functions to the linker | 
|---|
| 25 | proper. | 
|---|
| 26 |  | 
|---|
| 27 | The generic linker routines are in @code{linker.c}, and use the | 
|---|
| 28 | header file @code{genlink.h}.  As of this writing, the only back | 
|---|
| 29 | ends which have implemented versions of these routines are | 
|---|
| 30 | a.out (in @code{aoutx.h}) and ECOFF (in @code{ecoff.c}).  The a.out | 
|---|
| 31 | routines are used as examples throughout this section. | 
|---|
| 32 |  | 
|---|
| 33 | @menu | 
|---|
| 34 | * Creating a Linker Hash Table:: | 
|---|
| 35 | * Adding Symbols to the Hash Table:: | 
|---|
| 36 | * Performing the Final Link:: | 
|---|
| 37 | @end menu | 
|---|
| 38 |  | 
|---|
| 39 | @node Creating a Linker Hash Table, Adding Symbols to the Hash Table, Linker Functions, Linker Functions | 
|---|
| 40 | @subsection Creating a linker hash table | 
|---|
| 41 | @cindex _bfd_link_hash_table_create in target vector | 
|---|
| 42 | @cindex target vector (_bfd_link_hash_table_create) | 
|---|
| 43 | The linker routines must create a hash table, which must be | 
|---|
| 44 | derived from @code{struct bfd_link_hash_table} described in | 
|---|
| 45 | @code{bfdlink.c}.  @xref{Hash Tables}, for information on how to | 
|---|
| 46 | create a derived hash table.  This entry point is called using | 
|---|
| 47 | the target vector of the linker output file. | 
|---|
| 48 |  | 
|---|
| 49 | The @code{_bfd_link_hash_table_create} entry point must allocate | 
|---|
| 50 | and initialize an instance of the desired hash table.  If the | 
|---|
| 51 | back end does not require any additional information to be | 
|---|
| 52 | stored with the entries in the hash table, the entry point may | 
|---|
| 53 | simply create a @code{struct bfd_link_hash_table}.  Most likely, | 
|---|
| 54 | however, some additional information will be needed. | 
|---|
| 55 |  | 
|---|
| 56 | For example, with each entry in the hash table the a.out | 
|---|
| 57 | linker keeps the index the symbol has in the final output file | 
|---|
| 58 | (this index number is used so that when doing a relocateable | 
|---|
| 59 | link the symbol index used in the output file can be quickly | 
|---|
| 60 | filled in when copying over a reloc).  The a.out linker code | 
|---|
| 61 | defines the required structures and functions for a hash table | 
|---|
| 62 | derived from @code{struct bfd_link_hash_table}.  The a.out linker | 
|---|
| 63 | hash table is created by the function | 
|---|
| 64 | @code{NAME(aout,link_hash_table_create)}; it simply allocates | 
|---|
| 65 | space for the hash table, initializes it, and returns a | 
|---|
| 66 | pointer to it. | 
|---|
| 67 |  | 
|---|
| 68 | When writing the linker routines for a new back end, you will | 
|---|
| 69 | generally not know exactly which fields will be required until | 
|---|
| 70 | you have finished.  You should simply create a new hash table | 
|---|
| 71 | which defines no additional fields, and then simply add fields | 
|---|
| 72 | as they become necessary. | 
|---|
| 73 |  | 
|---|
| 74 | @node Adding Symbols to the Hash Table, Performing the Final Link, Creating a Linker Hash Table, Linker Functions | 
|---|
| 75 | @subsection Adding symbols to the hash table | 
|---|
| 76 | @cindex _bfd_link_add_symbols in target vector | 
|---|
| 77 | @cindex target vector (_bfd_link_add_symbols) | 
|---|
| 78 | The linker proper will call the @code{_bfd_link_add_symbols} | 
|---|
| 79 | entry point for each object file or archive which is to be | 
|---|
| 80 | linked (typically these are the files named on the command | 
|---|
| 81 | line, but some may also come from the linker script).  The | 
|---|
| 82 | entry point is responsible for examining the file.  For an | 
|---|
| 83 | object file, BFD must add any relevant symbol information to | 
|---|
| 84 | the hash table.  For an archive, BFD must determine which | 
|---|
| 85 | elements of the archive should be used and adding them to the | 
|---|
| 86 | link. | 
|---|
| 87 |  | 
|---|
| 88 | The a.out version of this entry point is | 
|---|
| 89 | @code{NAME(aout,link_add_symbols)}. | 
|---|
| 90 |  | 
|---|
| 91 | @menu | 
|---|
| 92 | * Differing file formats:: | 
|---|
| 93 | * Adding symbols from an object file:: | 
|---|
| 94 | * Adding symbols from an archive:: | 
|---|
| 95 | @end menu | 
|---|
| 96 |  | 
|---|
| 97 | @node Differing file formats, Adding symbols from an object file, Adding Symbols to the Hash Table, Adding Symbols to the Hash Table | 
|---|
| 98 | @subsubsection Differing file formats | 
|---|
| 99 | Normally all the files involved in a link will be of the same | 
|---|
| 100 | format, but it is also possible to link together different | 
|---|
| 101 | format object files, and the back end must support that.  The | 
|---|
| 102 | @code{_bfd_link_add_symbols} entry point is called via the target | 
|---|
| 103 | vector of the file to be added.  This has an important | 
|---|
| 104 | consequence: the function may not assume that the hash table | 
|---|
| 105 | is the type created by the corresponding | 
|---|
| 106 | @code{_bfd_link_hash_table_create} vector.  All the | 
|---|
| 107 | @code{_bfd_link_add_symbols} function can assume about the hash | 
|---|
| 108 | table is that it is derived from @code{struct | 
|---|
| 109 | bfd_link_hash_table}. | 
|---|
| 110 |  | 
|---|
| 111 | Sometimes the @code{_bfd_link_add_symbols} function must store | 
|---|
| 112 | some information in the hash table entry to be used by the | 
|---|
| 113 | @code{_bfd_final_link} function.  In such a case the @code{creator} | 
|---|
| 114 | field of the hash table must be checked to make sure that the | 
|---|
| 115 | hash table was created by an object file of the same format. | 
|---|
| 116 |  | 
|---|
| 117 | The @code{_bfd_final_link} routine must be prepared to handle a | 
|---|
| 118 | hash entry without any extra information added by the | 
|---|
| 119 | @code{_bfd_link_add_symbols} function.  A hash entry without | 
|---|
| 120 | extra information will also occur when the linker script | 
|---|
| 121 | directs the linker to create a symbol.  Note that, regardless | 
|---|
| 122 | of how a hash table entry is added, all the fields will be | 
|---|
| 123 | initialized to some sort of null value by the hash table entry | 
|---|
| 124 | initialization function. | 
|---|
| 125 |  | 
|---|
| 126 | See @code{ecoff_link_add_externals} for an example of how to | 
|---|
| 127 | check the @code{creator} field before saving information (in this | 
|---|
| 128 | case, the ECOFF external symbol debugging information) in a | 
|---|
| 129 | hash table entry. | 
|---|
| 130 |  | 
|---|
| 131 | @node Adding symbols from an object file, Adding symbols from an archive, Differing file formats, Adding Symbols to the Hash Table | 
|---|
| 132 | @subsubsection Adding symbols from an object file | 
|---|
| 133 | When the @code{_bfd_link_add_symbols} routine is passed an object | 
|---|
| 134 | file, it must add all externally visible symbols in that | 
|---|
| 135 | object file to the hash table.  The actual work of adding the | 
|---|
| 136 | symbol to the hash table is normally handled by the function | 
|---|
| 137 | @code{_bfd_generic_link_add_one_symbol}.  The | 
|---|
| 138 | @code{_bfd_link_add_symbols} routine is responsible for reading | 
|---|
| 139 | all the symbols from the object file and passing the correct | 
|---|
| 140 | information to @code{_bfd_generic_link_add_one_symbol}. | 
|---|
| 141 |  | 
|---|
| 142 | The @code{_bfd_link_add_symbols} routine should not use | 
|---|
| 143 | @code{bfd_canonicalize_symtab} to read the symbols.  The point of | 
|---|
| 144 | providing this routine is to avoid the overhead of converting | 
|---|
| 145 | the symbols into generic @code{asymbol} structures. | 
|---|
| 146 |  | 
|---|
| 147 | @findex _bfd_generic_link_add_one_symbol | 
|---|
| 148 | @code{_bfd_generic_link_add_one_symbol} handles the details of | 
|---|
| 149 | combining common symbols, warning about multiple definitions, | 
|---|
| 150 | and so forth.  It takes arguments which describe the symbol to | 
|---|
| 151 | add, notably symbol flags, a section, and an offset.  The | 
|---|
| 152 | symbol flags include such things as @code{BSF_WEAK} or | 
|---|
| 153 | @code{BSF_INDIRECT}.  The section is a section in the object | 
|---|
| 154 | file, or something like @code{bfd_und_section_ptr} for an undefined | 
|---|
| 155 | symbol or @code{bfd_com_section_ptr} for a common symbol. | 
|---|
| 156 |  | 
|---|
| 157 | If the @code{_bfd_final_link} routine is also going to need to | 
|---|
| 158 | read the symbol information, the @code{_bfd_link_add_symbols} | 
|---|
| 159 | routine should save it somewhere attached to the object file | 
|---|
| 160 | BFD.  However, the information should only be saved if the | 
|---|
| 161 | @code{keep_memory} field of the @code{info} argument is TRUE, so | 
|---|
| 162 | that the @code{-no-keep-memory} linker switch is effective. | 
|---|
| 163 |  | 
|---|
| 164 | The a.out function which adds symbols from an object file is | 
|---|
| 165 | @code{aout_link_add_object_symbols}, and most of the interesting | 
|---|
| 166 | work is in @code{aout_link_add_symbols}.  The latter saves | 
|---|
| 167 | pointers to the hash tables entries created by | 
|---|
| 168 | @code{_bfd_generic_link_add_one_symbol} indexed by symbol number, | 
|---|
| 169 | so that the @code{_bfd_final_link} routine does not have to call | 
|---|
| 170 | the hash table lookup routine to locate the entry. | 
|---|
| 171 |  | 
|---|
| 172 | @node Adding symbols from an archive, , Adding symbols from an object file, Adding Symbols to the Hash Table | 
|---|
| 173 | @subsubsection Adding symbols from an archive | 
|---|
| 174 | When the @code{_bfd_link_add_symbols} routine is passed an | 
|---|
| 175 | archive, it must look through the symbols defined by the | 
|---|
| 176 | archive and decide which elements of the archive should be | 
|---|
| 177 | included in the link.  For each such element it must call the | 
|---|
| 178 | @code{add_archive_element} linker callback, and it must add the | 
|---|
| 179 | symbols from the object file to the linker hash table. | 
|---|
| 180 |  | 
|---|
| 181 | @findex _bfd_generic_link_add_archive_symbols | 
|---|
| 182 | In most cases the work of looking through the symbols in the | 
|---|
| 183 | archive should be done by the | 
|---|
| 184 | @code{_bfd_generic_link_add_archive_symbols} function.  This | 
|---|
| 185 | function builds a hash table from the archive symbol table and | 
|---|
| 186 | looks through the list of undefined symbols to see which | 
|---|
| 187 | elements should be included. | 
|---|
| 188 | @code{_bfd_generic_link_add_archive_symbols} is passed a function | 
|---|
| 189 | to call to make the final decision about adding an archive | 
|---|
| 190 | element to the link and to do the actual work of adding the | 
|---|
| 191 | symbols to the linker hash table. | 
|---|
| 192 |  | 
|---|
| 193 | The function passed to | 
|---|
| 194 | @code{_bfd_generic_link_add_archive_symbols} must read the | 
|---|
| 195 | symbols of the archive element and decide whether the archive | 
|---|
| 196 | element should be included in the link.  If the element is to | 
|---|
| 197 | be included, the @code{add_archive_element} linker callback | 
|---|
| 198 | routine must be called with the element as an argument, and | 
|---|
| 199 | the elements symbols must be added to the linker hash table | 
|---|
| 200 | just as though the element had itself been passed to the | 
|---|
| 201 | @code{_bfd_link_add_symbols} function. | 
|---|
| 202 |  | 
|---|
| 203 | When the a.out @code{_bfd_link_add_symbols} function receives an | 
|---|
| 204 | archive, it calls @code{_bfd_generic_link_add_archive_symbols} | 
|---|
| 205 | passing @code{aout_link_check_archive_element} as the function | 
|---|
| 206 | argument. @code{aout_link_check_archive_element} calls | 
|---|
| 207 | @code{aout_link_check_ar_symbols}.  If the latter decides to add | 
|---|
| 208 | the element (an element is only added if it provides a real, | 
|---|
| 209 | non-common, definition for a previously undefined or common | 
|---|
| 210 | symbol) it calls the @code{add_archive_element} callback and then | 
|---|
| 211 | @code{aout_link_check_archive_element} calls | 
|---|
| 212 | @code{aout_link_add_symbols} to actually add the symbols to the | 
|---|
| 213 | linker hash table. | 
|---|
| 214 |  | 
|---|
| 215 | The ECOFF back end is unusual in that it does not normally | 
|---|
| 216 | call @code{_bfd_generic_link_add_archive_symbols}, because ECOFF | 
|---|
| 217 | archives already contain a hash table of symbols.  The ECOFF | 
|---|
| 218 | back end searches the archive itself to avoid the overhead of | 
|---|
| 219 | creating a new hash table. | 
|---|
| 220 |  | 
|---|
| 221 | @node Performing the Final Link, , Adding Symbols to the Hash Table, Linker Functions | 
|---|
| 222 | @subsection Performing the final link | 
|---|
| 223 | @cindex _bfd_link_final_link in target vector | 
|---|
| 224 | @cindex target vector (_bfd_final_link) | 
|---|
| 225 | When all the input files have been processed, the linker calls | 
|---|
| 226 | the @code{_bfd_final_link} entry point of the output BFD.  This | 
|---|
| 227 | routine is responsible for producing the final output file, | 
|---|
| 228 | which has several aspects.  It must relocate the contents of | 
|---|
| 229 | the input sections and copy the data into the output sections. | 
|---|
| 230 | It must build an output symbol table including any local | 
|---|
| 231 | symbols from the input files and the global symbols from the | 
|---|
| 232 | hash table.  When producing relocateable output, it must | 
|---|
| 233 | modify the input relocs and write them into the output file. | 
|---|
| 234 | There may also be object format dependent work to be done. | 
|---|
| 235 |  | 
|---|
| 236 | The linker will also call the @code{write_object_contents} entry | 
|---|
| 237 | point when the BFD is closed.  The two entry points must work | 
|---|
| 238 | together in order to produce the correct output file. | 
|---|
| 239 |  | 
|---|
| 240 | The details of how this works are inevitably dependent upon | 
|---|
| 241 | the specific object file format.  The a.out | 
|---|
| 242 | @code{_bfd_final_link} routine is @code{NAME(aout,final_link)}. | 
|---|
| 243 |  | 
|---|
| 244 | @menu | 
|---|
| 245 | * Information provided by the linker:: | 
|---|
| 246 | * Relocating the section contents:: | 
|---|
| 247 | * Writing the symbol table:: | 
|---|
| 248 | @end menu | 
|---|
| 249 |  | 
|---|
| 250 | @node Information provided by the linker, Relocating the section contents, Performing the Final Link, Performing the Final Link | 
|---|
| 251 | @subsubsection Information provided by the linker | 
|---|
| 252 | Before the linker calls the @code{_bfd_final_link} entry point, | 
|---|
| 253 | it sets up some data structures for the function to use. | 
|---|
| 254 |  | 
|---|
| 255 | The @code{input_bfds} field of the @code{bfd_link_info} structure | 
|---|
| 256 | will point to a list of all the input files included in the | 
|---|
| 257 | link.  These files are linked through the @code{link_next} field | 
|---|
| 258 | of the @code{bfd} structure. | 
|---|
| 259 |  | 
|---|
| 260 | Each section in the output file will have a list of | 
|---|
| 261 | @code{link_order} structures attached to the @code{link_order_head} | 
|---|
| 262 | field (the @code{link_order} structure is defined in | 
|---|
| 263 | @code{bfdlink.h}).  These structures describe how to create the | 
|---|
| 264 | contents of the output section in terms of the contents of | 
|---|
| 265 | various input sections, fill constants, and, eventually, other | 
|---|
| 266 | types of information.  They also describe relocs that must be | 
|---|
| 267 | created by the BFD backend, but do not correspond to any input | 
|---|
| 268 | file; this is used to support -Ur, which builds constructors | 
|---|
| 269 | while generating a relocateable object file. | 
|---|
| 270 |  | 
|---|
| 271 | @node Relocating the section contents, Writing the symbol table, Information provided by the linker, Performing the Final Link | 
|---|
| 272 | @subsubsection Relocating the section contents | 
|---|
| 273 | The @code{_bfd_final_link} function should look through the | 
|---|
| 274 | @code{link_order} structures attached to each section of the | 
|---|
| 275 | output file.  Each @code{link_order} structure should either be | 
|---|
| 276 | handled specially, or it should be passed to the function | 
|---|
| 277 | @code{_bfd_default_link_order} which will do the right thing | 
|---|
| 278 | (@code{_bfd_default_link_order} is defined in @code{linker.c}). | 
|---|
| 279 |  | 
|---|
| 280 | For efficiency, a @code{link_order} of type | 
|---|
| 281 | @code{bfd_indirect_link_order} whose associated section belongs | 
|---|
| 282 | to a BFD of the same format as the output BFD must be handled | 
|---|
| 283 | specially.  This type of @code{link_order} describes part of an | 
|---|
| 284 | output section in terms of a section belonging to one of the | 
|---|
| 285 | input files.  The @code{_bfd_final_link} function should read the | 
|---|
| 286 | contents of the section and any associated relocs, apply the | 
|---|
| 287 | relocs to the section contents, and write out the modified | 
|---|
| 288 | section contents.  If performing a relocateable link, the | 
|---|
| 289 | relocs themselves must also be modified and written out. | 
|---|
| 290 |  | 
|---|
| 291 | @findex _bfd_relocate_contents | 
|---|
| 292 | @findex _bfd_final_link_relocate | 
|---|
| 293 | The functions @code{_bfd_relocate_contents} and | 
|---|
| 294 | @code{_bfd_final_link_relocate} provide some general support for | 
|---|
| 295 | performing the actual relocations, notably overflow checking. | 
|---|
| 296 | Their arguments include information about the symbol the | 
|---|
| 297 | relocation is against and a @code{reloc_howto_type} argument | 
|---|
| 298 | which describes the relocation to perform.  These functions | 
|---|
| 299 | are defined in @code{reloc.c}. | 
|---|
| 300 |  | 
|---|
| 301 | The a.out function which handles reading, relocating, and | 
|---|
| 302 | writing section contents is @code{aout_link_input_section}.  The | 
|---|
| 303 | actual relocation is done in @code{aout_link_input_section_std} | 
|---|
| 304 | and @code{aout_link_input_section_ext}. | 
|---|
| 305 |  | 
|---|
| 306 | @node Writing the symbol table, , Relocating the section contents, Performing the Final Link | 
|---|
| 307 | @subsubsection Writing the symbol table | 
|---|
| 308 | The @code{_bfd_final_link} function must gather all the symbols | 
|---|
| 309 | in the input files and write them out.  It must also write out | 
|---|
| 310 | all the symbols in the global hash table.  This must be | 
|---|
| 311 | controlled by the @code{strip} and @code{discard} fields of the | 
|---|
| 312 | @code{bfd_link_info} structure. | 
|---|
| 313 |  | 
|---|
| 314 | The local symbols of the input files will not have been | 
|---|
| 315 | entered into the linker hash table.  The @code{_bfd_final_link} | 
|---|
| 316 | routine must consider each input file and include the symbols | 
|---|
| 317 | in the output file.  It may be convenient to do this when | 
|---|
| 318 | looking through the @code{link_order} structures, or it may be | 
|---|
| 319 | done by stepping through the @code{input_bfds} list. | 
|---|
| 320 |  | 
|---|
| 321 | The @code{_bfd_final_link} routine must also traverse the global | 
|---|
| 322 | hash table to gather all the externally visible symbols.  It | 
|---|
| 323 | is possible that most of the externally visible symbols may be | 
|---|
| 324 | written out when considering the symbols of each input file, | 
|---|
| 325 | but it is still necessary to traverse the hash table since the | 
|---|
| 326 | linker script may have defined some symbols that are not in | 
|---|
| 327 | any of the input files. | 
|---|
| 328 |  | 
|---|
| 329 | The @code{strip} field of the @code{bfd_link_info} structure | 
|---|
| 330 | controls which symbols are written out.  The possible values | 
|---|
| 331 | are listed in @code{bfdlink.h}.  If the value is @code{strip_some}, | 
|---|
| 332 | then the @code{keep_hash} field of the @code{bfd_link_info} | 
|---|
| 333 | structure is a hash table of symbols to keep; each symbol | 
|---|
| 334 | should be looked up in this hash table, and only symbols which | 
|---|
| 335 | are present should be included in the output file. | 
|---|
| 336 |  | 
|---|
| 337 | If the @code{strip} field of the @code{bfd_link_info} structure | 
|---|
| 338 | permits local symbols to be written out, the @code{discard} field | 
|---|
| 339 | is used to further controls which local symbols are included | 
|---|
| 340 | in the output file.  If the value is @code{discard_l}, then all | 
|---|
| 341 | local symbols which begin with a certain prefix are discarded; | 
|---|
| 342 | this is controlled by the @code{bfd_is_local_label_name} entry point. | 
|---|
| 343 |  | 
|---|
| 344 | The a.out backend handles symbols by calling | 
|---|
| 345 | @code{aout_link_write_symbols} on each input BFD and then | 
|---|
| 346 | traversing the global hash table with the function | 
|---|
| 347 | @code{aout_link_write_other_symbol}.  It builds a string table | 
|---|
| 348 | while writing out the symbols, which is written to the output | 
|---|
| 349 | file at the end of @code{NAME(aout,final_link)}. | 
|---|
| 350 |  | 
|---|
| 351 | @findex bfd_link_split_section | 
|---|
| 352 | @subsubsection @code{bfd_link_split_section} | 
|---|
| 353 | @strong{Synopsis} | 
|---|
| 354 | @example | 
|---|
| 355 | bfd_boolean bfd_link_split_section(bfd *abfd, asection *sec); | 
|---|
| 356 | @end example | 
|---|
| 357 | @strong{Description}@* | 
|---|
| 358 | Return nonzero if @var{sec} should be split during a | 
|---|
| 359 | reloceatable or final link. | 
|---|
| 360 | @example | 
|---|
| 361 | #define bfd_link_split_section(abfd, sec) \ | 
|---|
| 362 | BFD_SEND (abfd, _bfd_link_split_section, (abfd, sec)) | 
|---|
| 363 |  | 
|---|
| 364 | @end example | 
|---|
| 365 |  | 
|---|