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