1 /* linker.c -- BFD linker routines
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
3 Free Software Foundation, Inc.
4 Written by Steve Chamberlain and Ian Lance Taylor, Cygnus Support
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
33 The linker uses three special entry points in the BFD target
34 vector. It is not necessary to write special routines for
35 these entry points when creating a new BFD back end, since
36 generic versions are provided. However, writing them can
37 speed up linking and make it use significantly less runtime
40 The first routine creates a hash table used by the other
41 routines. The second routine adds the symbols from an object
42 file to the hash table. The third routine takes all the
43 object files and links them together to create the output
44 file. These routines are designed so that the linker proper
45 does not need to know anything about the symbols in the object
46 files that it is linking. The linker merely arranges the
47 sections as directed by the linker script and lets BFD handle
48 the details of symbols and relocs.
50 The second routine and third routines are passed a pointer to
51 a <<struct bfd_link_info>> structure (defined in
52 <<bfdlink.h>>) which holds information relevant to the link,
53 including the linker hash table (which was created by the
54 first routine) and a set of callback functions to the linker
57 The generic linker routines are in <<linker.c>>, and use the
58 header file <<genlink.h>>. As of this writing, the only back
59 ends which have implemented versions of these routines are
60 a.out (in <<aoutx.h>>) and ECOFF (in <<ecoff.c>>). The a.out
61 routines are used as examples throughout this section.
64 @* Creating a Linker Hash Table::
65 @* Adding Symbols to the Hash Table::
66 @* Performing the Final Link::
70 Creating a Linker Hash Table, Adding Symbols to the Hash Table, Linker Functions, Linker Functions
72 Creating a linker hash table
74 @cindex _bfd_link_hash_table_create in target vector
75 @cindex target vector (_bfd_link_hash_table_create)
76 The linker routines must create a hash table, which must be
77 derived from <<struct bfd_link_hash_table>> described in
78 <<bfdlink.c>>. @xref{Hash Tables}, for information on how to
79 create a derived hash table. This entry point is called using
80 the target vector of the linker output file.
82 The <<_bfd_link_hash_table_create>> entry point must allocate
83 and initialize an instance of the desired hash table. If the
84 back end does not require any additional information to be
85 stored with the entries in the hash table, the entry point may
86 simply create a <<struct bfd_link_hash_table>>. Most likely,
87 however, some additional information will be needed.
89 For example, with each entry in the hash table the a.out
90 linker keeps the index the symbol has in the final output file
91 (this index number is used so that when doing a relocateable
92 link the symbol index used in the output file can be quickly
93 filled in when copying over a reloc). The a.out linker code
94 defines the required structures and functions for a hash table
95 derived from <<struct bfd_link_hash_table>>. The a.out linker
96 hash table is created by the function
97 <<NAME(aout,link_hash_table_create)>>; it simply allocates
98 space for the hash table, initializes it, and returns a
101 When writing the linker routines for a new back end, you will
102 generally not know exactly which fields will be required until
103 you have finished. You should simply create a new hash table
104 which defines no additional fields, and then simply add fields
105 as they become necessary.
108 Adding Symbols to the Hash Table, Performing the Final Link, Creating a Linker Hash Table, Linker Functions
110 Adding symbols to the hash table
112 @cindex _bfd_link_add_symbols in target vector
113 @cindex target vector (_bfd_link_add_symbols)
114 The linker proper will call the <<_bfd_link_add_symbols>>
115 entry point for each object file or archive which is to be
116 linked (typically these are the files named on the command
117 line, but some may also come from the linker script). The
118 entry point is responsible for examining the file. For an
119 object file, BFD must add any relevant symbol information to
120 the hash table. For an archive, BFD must determine which
121 elements of the archive should be used and adding them to the
124 The a.out version of this entry point is
125 <<NAME(aout,link_add_symbols)>>.
128 @* Differing file formats::
129 @* Adding symbols from an object file::
130 @* Adding symbols from an archive::
134 Differing file formats, Adding symbols from an object file, Adding Symbols to the Hash Table, Adding Symbols to the Hash Table
136 Differing file formats
138 Normally all the files involved in a link will be of the same
139 format, but it is also possible to link together different
140 format object files, and the back end must support that. The
141 <<_bfd_link_add_symbols>> entry point is called via the target
142 vector of the file to be added. This has an important
143 consequence: the function may not assume that the hash table
144 is the type created by the corresponding
145 <<_bfd_link_hash_table_create>> vector. All the
146 <<_bfd_link_add_symbols>> function can assume about the hash
147 table is that it is derived from <<struct
148 bfd_link_hash_table>>.
150 Sometimes the <<_bfd_link_add_symbols>> function must store
151 some information in the hash table entry to be used by the
152 <<_bfd_final_link>> function. In such a case the <<creator>>
153 field of the hash table must be checked to make sure that the
154 hash table was created by an object file of the same format.
156 The <<_bfd_final_link>> routine must be prepared to handle a
157 hash entry without any extra information added by the
158 <<_bfd_link_add_symbols>> function. A hash entry without
159 extra information will also occur when the linker script
160 directs the linker to create a symbol. Note that, regardless
161 of how a hash table entry is added, all the fields will be
162 initialized to some sort of null value by the hash table entry
163 initialization function.
165 See <<ecoff_link_add_externals>> for an example of how to
166 check the <<creator>> field before saving information (in this
167 case, the ECOFF external symbol debugging information) in a
171 Adding symbols from an object file, Adding symbols from an archive, Differing file formats, Adding Symbols to the Hash Table
173 Adding symbols from an object file
175 When the <<_bfd_link_add_symbols>> routine is passed an object
176 file, it must add all externally visible symbols in that
177 object file to the hash table. The actual work of adding the
178 symbol to the hash table is normally handled by the function
179 <<_bfd_generic_link_add_one_symbol>>. The
180 <<_bfd_link_add_symbols>> routine is responsible for reading
181 all the symbols from the object file and passing the correct
182 information to <<_bfd_generic_link_add_one_symbol>>.
184 The <<_bfd_link_add_symbols>> routine should not use
185 <<bfd_canonicalize_symtab>> to read the symbols. The point of
186 providing this routine is to avoid the overhead of converting
187 the symbols into generic <<asymbol>> structures.
189 @findex _bfd_generic_link_add_one_symbol
190 <<_bfd_generic_link_add_one_symbol>> handles the details of
191 combining common symbols, warning about multiple definitions,
192 and so forth. It takes arguments which describe the symbol to
193 add, notably symbol flags, a section, and an offset. The
194 symbol flags include such things as <<BSF_WEAK>> or
195 <<BSF_INDIRECT>>. The section is a section in the object
196 file, or something like <<bfd_und_section_ptr>> for an undefined
197 symbol or <<bfd_com_section_ptr>> for a common symbol.
199 If the <<_bfd_final_link>> routine is also going to need to
200 read the symbol information, the <<_bfd_link_add_symbols>>
201 routine should save it somewhere attached to the object file
202 BFD. However, the information should only be saved if the
203 <<keep_memory>> field of the <<info>> argument is TRUE, so
204 that the <<-no-keep-memory>> linker switch is effective.
206 The a.out function which adds symbols from an object file is
207 <<aout_link_add_object_symbols>>, and most of the interesting
208 work is in <<aout_link_add_symbols>>. The latter saves
209 pointers to the hash tables entries created by
210 <<_bfd_generic_link_add_one_symbol>> indexed by symbol number,
211 so that the <<_bfd_final_link>> routine does not have to call
212 the hash table lookup routine to locate the entry.
215 Adding symbols from an archive, , Adding symbols from an object file, Adding Symbols to the Hash Table
217 Adding symbols from an archive
219 When the <<_bfd_link_add_symbols>> routine is passed an
220 archive, it must look through the symbols defined by the
221 archive and decide which elements of the archive should be
222 included in the link. For each such element it must call the
223 <<add_archive_element>> linker callback, and it must add the
224 symbols from the object file to the linker hash table.
226 @findex _bfd_generic_link_add_archive_symbols
227 In most cases the work of looking through the symbols in the
228 archive should be done by the
229 <<_bfd_generic_link_add_archive_symbols>> function. This
230 function builds a hash table from the archive symbol table and
231 looks through the list of undefined symbols to see which
232 elements should be included.
233 <<_bfd_generic_link_add_archive_symbols>> is passed a function
234 to call to make the final decision about adding an archive
235 element to the link and to do the actual work of adding the
236 symbols to the linker hash table.
238 The function passed to
239 <<_bfd_generic_link_add_archive_symbols>> must read the
240 symbols of the archive element and decide whether the archive
241 element should be included in the link. If the element is to
242 be included, the <<add_archive_element>> linker callback
243 routine must be called with the element as an argument, and
244 the elements symbols must be added to the linker hash table
245 just as though the element had itself been passed to the
246 <<_bfd_link_add_symbols>> function.
248 When the a.out <<_bfd_link_add_symbols>> function receives an
249 archive, it calls <<_bfd_generic_link_add_archive_symbols>>
250 passing <<aout_link_check_archive_element>> as the function
251 argument. <<aout_link_check_archive_element>> calls
252 <<aout_link_check_ar_symbols>>. If the latter decides to add
253 the element (an element is only added if it provides a real,
254 non-common, definition for a previously undefined or common
255 symbol) it calls the <<add_archive_element>> callback and then
256 <<aout_link_check_archive_element>> calls
257 <<aout_link_add_symbols>> to actually add the symbols to the
260 The ECOFF back end is unusual in that it does not normally
261 call <<_bfd_generic_link_add_archive_symbols>>, because ECOFF
262 archives already contain a hash table of symbols. The ECOFF
263 back end searches the archive itself to avoid the overhead of
264 creating a new hash table.
267 Performing the Final Link, , Adding Symbols to the Hash Table, Linker Functions
269 Performing the final link
271 @cindex _bfd_link_final_link in target vector
272 @cindex target vector (_bfd_final_link)
273 When all the input files have been processed, the linker calls
274 the <<_bfd_final_link>> entry point of the output BFD. This
275 routine is responsible for producing the final output file,
276 which has several aspects. It must relocate the contents of
277 the input sections and copy the data into the output sections.
278 It must build an output symbol table including any local
279 symbols from the input files and the global symbols from the
280 hash table. When producing relocateable output, it must
281 modify the input relocs and write them into the output file.
282 There may also be object format dependent work to be done.
284 The linker will also call the <<write_object_contents>> entry
285 point when the BFD is closed. The two entry points must work
286 together in order to produce the correct output file.
288 The details of how this works are inevitably dependent upon
289 the specific object file format. The a.out
290 <<_bfd_final_link>> routine is <<NAME(aout,final_link)>>.
293 @* Information provided by the linker::
294 @* Relocating the section contents::
295 @* Writing the symbol table::
299 Information provided by the linker, Relocating the section contents, Performing the Final Link, Performing the Final Link
301 Information provided by the linker
303 Before the linker calls the <<_bfd_final_link>> entry point,
304 it sets up some data structures for the function to use.
306 The <<input_bfds>> field of the <<bfd_link_info>> structure
307 will point to a list of all the input files included in the
308 link. These files are linked through the <<link_next>> field
309 of the <<bfd>> structure.
311 Each section in the output file will have a list of
312 <<link_order>> structures attached to the <<link_order_head>>
313 field (the <<link_order>> structure is defined in
314 <<bfdlink.h>>). These structures describe how to create the
315 contents of the output section in terms of the contents of
316 various input sections, fill constants, and, eventually, other
317 types of information. They also describe relocs that must be
318 created by the BFD backend, but do not correspond to any input
319 file; this is used to support -Ur, which builds constructors
320 while generating a relocateable object file.
323 Relocating the section contents, Writing the symbol table, Information provided by the linker, Performing the Final Link
325 Relocating the section contents
327 The <<_bfd_final_link>> function should look through the
328 <<link_order>> structures attached to each section of the
329 output file. Each <<link_order>> structure should either be
330 handled specially, or it should be passed to the function
331 <<_bfd_default_link_order>> which will do the right thing
332 (<<_bfd_default_link_order>> is defined in <<linker.c>>).
334 For efficiency, a <<link_order>> of type
335 <<bfd_indirect_link_order>> whose associated section belongs
336 to a BFD of the same format as the output BFD must be handled
337 specially. This type of <<link_order>> describes part of an
338 output section in terms of a section belonging to one of the
339 input files. The <<_bfd_final_link>> function should read the
340 contents of the section and any associated relocs, apply the
341 relocs to the section contents, and write out the modified
342 section contents. If performing a relocateable link, the
343 relocs themselves must also be modified and written out.
345 @findex _bfd_relocate_contents
346 @findex _bfd_final_link_relocate
347 The functions <<_bfd_relocate_contents>> and
348 <<_bfd_final_link_relocate>> provide some general support for
349 performing the actual relocations, notably overflow checking.
350 Their arguments include information about the symbol the
351 relocation is against and a <<reloc_howto_type>> argument
352 which describes the relocation to perform. These functions
353 are defined in <<reloc.c>>.
355 The a.out function which handles reading, relocating, and
356 writing section contents is <<aout_link_input_section>>. The
357 actual relocation is done in <<aout_link_input_section_std>>
358 and <<aout_link_input_section_ext>>.
361 Writing the symbol table, , Relocating the section contents, Performing the Final Link
363 Writing the symbol table
365 The <<_bfd_final_link>> function must gather all the symbols
366 in the input files and write them out. It must also write out
367 all the symbols in the global hash table. This must be
368 controlled by the <<strip>> and <<discard>> fields of the
369 <<bfd_link_info>> structure.
371 The local symbols of the input files will not have been
372 entered into the linker hash table. The <<_bfd_final_link>>
373 routine must consider each input file and include the symbols
374 in the output file. It may be convenient to do this when
375 looking through the <<link_order>> structures, or it may be
376 done by stepping through the <<input_bfds>> list.
378 The <<_bfd_final_link>> routine must also traverse the global
379 hash table to gather all the externally visible symbols. It
380 is possible that most of the externally visible symbols may be
381 written out when considering the symbols of each input file,
382 but it is still necessary to traverse the hash table since the
383 linker script may have defined some symbols that are not in
384 any of the input files.
386 The <<strip>> field of the <<bfd_link_info>> structure
387 controls which symbols are written out. The possible values
388 are listed in <<bfdlink.h>>. If the value is <<strip_some>>,
389 then the <<keep_hash>> field of the <<bfd_link_info>>
390 structure is a hash table of symbols to keep; each symbol
391 should be looked up in this hash table, and only symbols which
392 are present should be included in the output file.
394 If the <<strip>> field of the <<bfd_link_info>> structure
395 permits local symbols to be written out, the <<discard>> field
396 is used to further controls which local symbols are included
397 in the output file. If the value is <<discard_l>>, then all
398 local symbols which begin with a certain prefix are discarded;
399 this is controlled by the <<bfd_is_local_label_name>> entry point.
401 The a.out backend handles symbols by calling
402 <<aout_link_write_symbols>> on each input BFD and then
403 traversing the global hash table with the function
404 <<aout_link_write_other_symbol>>. It builds a string table
405 while writing out the symbols, which is written to the output
406 file at the end of <<NAME(aout,final_link)>>.
409 static bfd_boolean generic_link_read_symbols
411 static bfd_boolean generic_link_add_symbols
412 PARAMS ((bfd
*, struct bfd_link_info
*, bfd_boolean collect
));
413 static bfd_boolean generic_link_add_object_symbols
414 PARAMS ((bfd
*, struct bfd_link_info
*, bfd_boolean collect
));
415 static bfd_boolean generic_link_check_archive_element_no_collect
416 PARAMS ((bfd
*, struct bfd_link_info
*, bfd_boolean
*pneeded
));
417 static bfd_boolean generic_link_check_archive_element_collect
418 PARAMS ((bfd
*, struct bfd_link_info
*, bfd_boolean
*pneeded
));
419 static bfd_boolean generic_link_check_archive_element
420 PARAMS ((bfd
*, struct bfd_link_info
*, bfd_boolean
*pneeded
,
421 bfd_boolean collect
));
422 static bfd_boolean generic_link_add_symbol_list
423 PARAMS ((bfd
*, struct bfd_link_info
*, bfd_size_type count
, asymbol
**,
424 bfd_boolean collect
));
425 static bfd
*hash_entry_bfd
426 PARAMS ((struct bfd_link_hash_entry
*));
427 static void set_symbol_from_hash
428 PARAMS ((asymbol
*, struct bfd_link_hash_entry
*));
429 static bfd_boolean generic_add_output_symbol
430 PARAMS ((bfd
*, size_t *psymalloc
, asymbol
*));
431 static bfd_boolean default_data_link_order
432 PARAMS ((bfd
*, struct bfd_link_info
*, asection
*,
433 struct bfd_link_order
*));
434 static bfd_boolean default_indirect_link_order
435 PARAMS ((bfd
*, struct bfd_link_info
*, asection
*,
436 struct bfd_link_order
*, bfd_boolean
));
438 /* The link hash table structure is defined in bfdlink.h. It provides
439 a base hash table which the backend specific hash tables are built
442 /* Routine to create an entry in the link hash table. */
444 struct bfd_hash_entry
*
445 _bfd_link_hash_newfunc (entry
, table
, string
)
446 struct bfd_hash_entry
*entry
;
447 struct bfd_hash_table
*table
;
450 /* Allocate the structure if it has not already been allocated by a
454 entry
= (struct bfd_hash_entry
*)
455 bfd_hash_allocate (table
, sizeof (struct bfd_link_hash_entry
));
460 /* Call the allocation method of the superclass. */
461 entry
= bfd_hash_newfunc (entry
, table
, string
);
464 struct bfd_link_hash_entry
*h
= (struct bfd_link_hash_entry
*) entry
;
466 /* Initialize the local fields. */
467 h
->type
= bfd_link_hash_new
;
474 /* Initialize a link hash table. The BFD argument is the one
475 responsible for creating this table. */
478 _bfd_link_hash_table_init (table
, abfd
, newfunc
)
479 struct bfd_link_hash_table
*table
;
481 struct bfd_hash_entry
*(*newfunc
) PARAMS ((struct bfd_hash_entry
*,
482 struct bfd_hash_table
*,
485 table
->creator
= abfd
->xvec
;
486 table
->undefs
= NULL
;
487 table
->undefs_tail
= NULL
;
488 table
->type
= bfd_link_generic_hash_table
;
490 return bfd_hash_table_init (&table
->table
, newfunc
);
493 /* Look up a symbol in a link hash table. If follow is TRUE, we
494 follow bfd_link_hash_indirect and bfd_link_hash_warning links to
497 struct bfd_link_hash_entry
*
498 bfd_link_hash_lookup (table
, string
, create
, copy
, follow
)
499 struct bfd_link_hash_table
*table
;
505 struct bfd_link_hash_entry
*ret
;
507 ret
= ((struct bfd_link_hash_entry
*)
508 bfd_hash_lookup (&table
->table
, string
, create
, copy
));
510 if (follow
&& ret
!= (struct bfd_link_hash_entry
*) NULL
)
512 while (ret
->type
== bfd_link_hash_indirect
513 || ret
->type
== bfd_link_hash_warning
)
520 /* Look up a symbol in the main linker hash table if the symbol might
521 be wrapped. This should only be used for references to an
522 undefined symbol, not for definitions of a symbol. */
524 struct bfd_link_hash_entry
*
525 bfd_wrapped_link_hash_lookup (abfd
, info
, string
, create
, copy
, follow
)
527 struct bfd_link_info
*info
;
535 if (info
->wrap_hash
!= NULL
)
540 if (*l
== bfd_get_symbol_leading_char (abfd
))
544 #define WRAP "__wrap_"
546 if (bfd_hash_lookup (info
->wrap_hash
, l
, FALSE
, FALSE
) != NULL
)
549 struct bfd_link_hash_entry
*h
;
551 /* This symbol is being wrapped. We want to replace all
552 references to SYM with references to __wrap_SYM. */
554 amt
= strlen (l
) + sizeof WRAP
+ 1;
555 n
= (char *) bfd_malloc (amt
);
559 /* Note that symbol_leading_char may be '\0'. */
560 n
[0] = bfd_get_symbol_leading_char (abfd
);
564 h
= bfd_link_hash_lookup (info
->hash
, n
, create
, TRUE
, follow
);
572 #define REAL "__real_"
575 && strncmp (l
, REAL
, sizeof REAL
- 1) == 0
576 && bfd_hash_lookup (info
->wrap_hash
, l
+ sizeof REAL
- 1,
577 FALSE
, FALSE
) != NULL
)
580 struct bfd_link_hash_entry
*h
;
582 /* This is a reference to __real_SYM, where SYM is being
583 wrapped. We want to replace all references to __real_SYM
584 with references to SYM. */
586 amt
= strlen (l
+ sizeof REAL
- 1) + 2;
587 n
= (char *) bfd_malloc (amt
);
591 /* Note that symbol_leading_char may be '\0'. */
592 n
[0] = bfd_get_symbol_leading_char (abfd
);
594 strcat (n
, l
+ sizeof REAL
- 1);
595 h
= bfd_link_hash_lookup (info
->hash
, n
, create
, TRUE
, follow
);
603 return bfd_link_hash_lookup (info
->hash
, string
, create
, copy
, follow
);
606 /* Traverse a generic link hash table. The only reason this is not a
607 macro is to do better type checking. This code presumes that an
608 argument passed as a struct bfd_hash_entry * may be caught as a
609 struct bfd_link_hash_entry * with no explicit cast required on the
613 bfd_link_hash_traverse (table
, func
, info
)
614 struct bfd_link_hash_table
*table
;
615 bfd_boolean (*func
) PARAMS ((struct bfd_link_hash_entry
*, PTR
));
618 bfd_hash_traverse (&table
->table
,
619 ((bfd_boolean (*) PARAMS ((struct bfd_hash_entry
*, PTR
)))
624 /* Add a symbol to the linker hash table undefs list. */
627 bfd_link_add_undef (table
, h
)
628 struct bfd_link_hash_table
*table
;
629 struct bfd_link_hash_entry
*h
;
631 BFD_ASSERT (h
->next
== NULL
);
632 if (table
->undefs_tail
!= (struct bfd_link_hash_entry
*) NULL
)
633 table
->undefs_tail
->next
= h
;
634 if (table
->undefs
== (struct bfd_link_hash_entry
*) NULL
)
636 table
->undefs_tail
= h
;
639 /* Routine to create an entry in an generic link hash table. */
641 struct bfd_hash_entry
*
642 _bfd_generic_link_hash_newfunc (entry
, table
, string
)
643 struct bfd_hash_entry
*entry
;
644 struct bfd_hash_table
*table
;
647 /* Allocate the structure if it has not already been allocated by a
651 entry
= (struct bfd_hash_entry
*)
652 bfd_hash_allocate (table
, sizeof (struct generic_link_hash_entry
));
657 /* Call the allocation method of the superclass. */
658 entry
= _bfd_link_hash_newfunc (entry
, table
, string
);
661 struct generic_link_hash_entry
*ret
;
663 /* Set local fields. */
664 ret
= (struct generic_link_hash_entry
*) entry
;
665 ret
->written
= FALSE
;
672 /* Create an generic link hash table. */
674 struct bfd_link_hash_table
*
675 _bfd_generic_link_hash_table_create (abfd
)
678 struct generic_link_hash_table
*ret
;
679 bfd_size_type amt
= sizeof (struct generic_link_hash_table
);
681 ret
= (struct generic_link_hash_table
*) bfd_malloc (amt
);
683 return (struct bfd_link_hash_table
*) NULL
;
684 if (! _bfd_link_hash_table_init (&ret
->root
, abfd
,
685 _bfd_generic_link_hash_newfunc
))
688 return (struct bfd_link_hash_table
*) NULL
;
694 _bfd_generic_link_hash_table_free (hash
)
695 struct bfd_link_hash_table
*hash
;
697 struct generic_link_hash_table
*ret
698 = (struct generic_link_hash_table
*) hash
;
700 bfd_hash_table_free (&ret
->root
.table
);
704 /* Grab the symbols for an object file when doing a generic link. We
705 store the symbols in the outsymbols field. We need to keep them
706 around for the entire link to ensure that we only read them once.
707 If we read them multiple times, we might wind up with relocs and
708 the hash table pointing to different instances of the symbol
712 generic_link_read_symbols (abfd
)
715 if (bfd_get_outsymbols (abfd
) == (asymbol
**) NULL
)
720 symsize
= bfd_get_symtab_upper_bound (abfd
);
723 bfd_get_outsymbols (abfd
) =
724 (asymbol
**) bfd_alloc (abfd
, (bfd_size_type
) symsize
);
725 if (bfd_get_outsymbols (abfd
) == NULL
&& symsize
!= 0)
727 symcount
= bfd_canonicalize_symtab (abfd
, bfd_get_outsymbols (abfd
));
730 bfd_get_symcount (abfd
) = symcount
;
736 /* Generic function to add symbols to from an object file to the
737 global hash table. This version does not automatically collect
738 constructors by name. */
741 _bfd_generic_link_add_symbols (abfd
, info
)
743 struct bfd_link_info
*info
;
745 return generic_link_add_symbols (abfd
, info
, FALSE
);
748 /* Generic function to add symbols from an object file to the global
749 hash table. This version automatically collects constructors by
750 name, as the collect2 program does. It should be used for any
751 target which does not provide some other mechanism for setting up
752 constructors and destructors; these are approximately those targets
753 for which gcc uses collect2 and do not support stabs. */
756 _bfd_generic_link_add_symbols_collect (abfd
, info
)
758 struct bfd_link_info
*info
;
760 return generic_link_add_symbols (abfd
, info
, TRUE
);
763 /* Indicate that we are only retrieving symbol values from this
764 section. We want the symbols to act as though the values in the
765 file are absolute. */
768 _bfd_generic_link_just_syms (sec
, info
)
770 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
772 sec
->output_section
= bfd_abs_section_ptr
;
773 sec
->output_offset
= sec
->vma
;
776 /* Add symbols from an object file to the global hash table. */
779 generic_link_add_symbols (abfd
, info
, collect
)
781 struct bfd_link_info
*info
;
786 switch (bfd_get_format (abfd
))
789 ret
= generic_link_add_object_symbols (abfd
, info
, collect
);
792 ret
= (_bfd_generic_link_add_archive_symbols
795 ? generic_link_check_archive_element_collect
796 : generic_link_check_archive_element_no_collect
)));
799 bfd_set_error (bfd_error_wrong_format
);
806 /* Add symbols from an object file to the global hash table. */
809 generic_link_add_object_symbols (abfd
, info
, collect
)
811 struct bfd_link_info
*info
;
814 bfd_size_type symcount
;
815 struct symbol_cache_entry
**outsyms
;
817 if (! generic_link_read_symbols (abfd
))
819 symcount
= _bfd_generic_link_get_symcount (abfd
);
820 outsyms
= _bfd_generic_link_get_symbols (abfd
);
821 return generic_link_add_symbol_list (abfd
, info
, symcount
, outsyms
, collect
);
824 /* We build a hash table of all symbols defined in an archive. */
826 /* An archive symbol may be defined by multiple archive elements.
827 This linked list is used to hold the elements. */
831 struct archive_list
*next
;
835 /* An entry in an archive hash table. */
837 struct archive_hash_entry
839 struct bfd_hash_entry root
;
840 /* Where the symbol is defined. */
841 struct archive_list
*defs
;
844 /* An archive hash table itself. */
846 struct archive_hash_table
848 struct bfd_hash_table table
;
851 static struct bfd_hash_entry
*archive_hash_newfunc
852 PARAMS ((struct bfd_hash_entry
*, struct bfd_hash_table
*, const char *));
853 static bfd_boolean archive_hash_table_init
854 PARAMS ((struct archive_hash_table
*,
855 struct bfd_hash_entry
*(*) (struct bfd_hash_entry
*,
856 struct bfd_hash_table
*,
859 /* Create a new entry for an archive hash table. */
861 static struct bfd_hash_entry
*
862 archive_hash_newfunc (entry
, table
, string
)
863 struct bfd_hash_entry
*entry
;
864 struct bfd_hash_table
*table
;
867 struct archive_hash_entry
*ret
= (struct archive_hash_entry
*) entry
;
869 /* Allocate the structure if it has not already been allocated by a
871 if (ret
== (struct archive_hash_entry
*) NULL
)
872 ret
= ((struct archive_hash_entry
*)
873 bfd_hash_allocate (table
, sizeof (struct archive_hash_entry
)));
874 if (ret
== (struct archive_hash_entry
*) NULL
)
877 /* Call the allocation method of the superclass. */
878 ret
= ((struct archive_hash_entry
*)
879 bfd_hash_newfunc ((struct bfd_hash_entry
*) ret
, table
, string
));
883 /* Initialize the local fields. */
884 ret
->defs
= (struct archive_list
*) NULL
;
887 return (struct bfd_hash_entry
*) ret
;
890 /* Initialize an archive hash table. */
893 archive_hash_table_init (table
, newfunc
)
894 struct archive_hash_table
*table
;
895 struct bfd_hash_entry
*(*newfunc
) PARAMS ((struct bfd_hash_entry
*,
896 struct bfd_hash_table
*,
899 return bfd_hash_table_init (&table
->table
, newfunc
);
902 /* Look up an entry in an archive hash table. */
904 #define archive_hash_lookup(t, string, create, copy) \
905 ((struct archive_hash_entry *) \
906 bfd_hash_lookup (&(t)->table, (string), (create), (copy)))
908 /* Allocate space in an archive hash table. */
910 #define archive_hash_allocate(t, size) bfd_hash_allocate (&(t)->table, (size))
912 /* Free an archive hash table. */
914 #define archive_hash_table_free(t) bfd_hash_table_free (&(t)->table)
916 /* Generic function to add symbols from an archive file to the global
917 hash file. This function presumes that the archive symbol table
918 has already been read in (this is normally done by the
919 bfd_check_format entry point). It looks through the undefined and
920 common symbols and searches the archive symbol table for them. If
921 it finds an entry, it includes the associated object file in the
924 The old linker looked through the archive symbol table for
925 undefined symbols. We do it the other way around, looking through
926 undefined symbols for symbols defined in the archive. The
927 advantage of the newer scheme is that we only have to look through
928 the list of undefined symbols once, whereas the old method had to
929 re-search the symbol table each time a new object file was added.
931 The CHECKFN argument is used to see if an object file should be
932 included. CHECKFN should set *PNEEDED to TRUE if the object file
933 should be included, and must also call the bfd_link_info
934 add_archive_element callback function and handle adding the symbols
935 to the global hash table. CHECKFN should only return FALSE if some
936 sort of error occurs.
938 For some formats, such as a.out, it is possible to look through an
939 object file but not actually include it in the link. The
940 archive_pass field in a BFD is used to avoid checking the symbols
941 of an object files too many times. When an object is included in
942 the link, archive_pass is set to -1. If an object is scanned but
943 not included, archive_pass is set to the pass number. The pass
944 number is incremented each time a new object file is included. The
945 pass number is used because when a new object file is included it
946 may create new undefined symbols which cause a previously examined
947 object file to be included. */
950 _bfd_generic_link_add_archive_symbols (abfd
, info
, checkfn
)
952 struct bfd_link_info
*info
;
953 bfd_boolean (*checkfn
)
954 PARAMS ((bfd
*, struct bfd_link_info
*, bfd_boolean
*pneeded
));
958 register carsym
*arsym
;
960 struct archive_hash_table arsym_hash
;
962 struct bfd_link_hash_entry
**pundef
;
964 if (! bfd_has_map (abfd
))
966 /* An empty archive is a special case. */
967 if (bfd_openr_next_archived_file (abfd
, (bfd
*) NULL
) == NULL
)
969 bfd_set_error (bfd_error_no_armap
);
973 arsyms
= bfd_ardata (abfd
)->symdefs
;
974 arsym_end
= arsyms
+ bfd_ardata (abfd
)->symdef_count
;
976 /* In order to quickly determine whether an symbol is defined in
977 this archive, we build a hash table of the symbols. */
978 if (! archive_hash_table_init (&arsym_hash
, archive_hash_newfunc
))
980 for (arsym
= arsyms
, indx
= 0; arsym
< arsym_end
; arsym
++, indx
++)
982 struct archive_hash_entry
*arh
;
983 struct archive_list
*l
, **pp
;
985 arh
= archive_hash_lookup (&arsym_hash
, arsym
->name
, TRUE
, FALSE
);
986 if (arh
== (struct archive_hash_entry
*) NULL
)
988 l
= ((struct archive_list
*)
989 archive_hash_allocate (&arsym_hash
, sizeof (struct archive_list
)));
993 for (pp
= &arh
->defs
;
994 *pp
!= (struct archive_list
*) NULL
;
1001 /* The archive_pass field in the archive itself is used to
1002 initialize PASS, sine we may search the same archive multiple
1004 pass
= abfd
->archive_pass
+ 1;
1006 /* New undefined symbols are added to the end of the list, so we
1007 only need to look through it once. */
1008 pundef
= &info
->hash
->undefs
;
1009 while (*pundef
!= (struct bfd_link_hash_entry
*) NULL
)
1011 struct bfd_link_hash_entry
*h
;
1012 struct archive_hash_entry
*arh
;
1013 struct archive_list
*l
;
1017 /* When a symbol is defined, it is not necessarily removed from
1019 if (h
->type
!= bfd_link_hash_undefined
1020 && h
->type
!= bfd_link_hash_common
)
1022 /* Remove this entry from the list, for general cleanliness
1023 and because we are going to look through the list again
1024 if we search any more libraries. We can't remove the
1025 entry if it is the tail, because that would lose any
1026 entries we add to the list later on (it would also cause
1027 us to lose track of whether the symbol has been
1029 if (*pundef
!= info
->hash
->undefs_tail
)
1030 *pundef
= (*pundef
)->next
;
1032 pundef
= &(*pundef
)->next
;
1036 /* Look for this symbol in the archive symbol map. */
1037 arh
= archive_hash_lookup (&arsym_hash
, h
->root
.string
, FALSE
, FALSE
);
1038 if (arh
== (struct archive_hash_entry
*) NULL
)
1040 /* If we haven't found the exact symbol we're looking for,
1041 let's look for its import thunk */
1042 if (info
->pei386_auto_import
)
1044 bfd_size_type amt
= strlen (h
->root
.string
) + 10;
1045 char *buf
= (char *) bfd_malloc (amt
);
1049 sprintf (buf
, "__imp_%s", h
->root
.string
);
1050 arh
= archive_hash_lookup (&arsym_hash
, buf
, FALSE
, FALSE
);
1053 if (arh
== (struct archive_hash_entry
*) NULL
)
1055 pundef
= &(*pundef
)->next
;
1059 /* Look at all the objects which define this symbol. */
1060 for (l
= arh
->defs
; l
!= (struct archive_list
*) NULL
; l
= l
->next
)
1065 /* If the symbol has gotten defined along the way, quit. */
1066 if (h
->type
!= bfd_link_hash_undefined
1067 && h
->type
!= bfd_link_hash_common
)
1070 element
= bfd_get_elt_at_index (abfd
, l
->indx
);
1071 if (element
== (bfd
*) NULL
)
1074 /* If we've already included this element, or if we've
1075 already checked it on this pass, continue. */
1076 if (element
->archive_pass
== -1
1077 || element
->archive_pass
== pass
)
1080 /* If we can't figure this element out, just ignore it. */
1081 if (! bfd_check_format (element
, bfd_object
))
1083 element
->archive_pass
= -1;
1087 /* CHECKFN will see if this element should be included, and
1088 go ahead and include it if appropriate. */
1089 if (! (*checkfn
) (element
, info
, &needed
))
1093 element
->archive_pass
= pass
;
1096 element
->archive_pass
= -1;
1098 /* Increment the pass count to show that we may need to
1099 recheck object files which were already checked. */
1104 pundef
= &(*pundef
)->next
;
1107 archive_hash_table_free (&arsym_hash
);
1109 /* Save PASS in case we are called again. */
1110 abfd
->archive_pass
= pass
;
1115 archive_hash_table_free (&arsym_hash
);
1119 /* See if we should include an archive element. This version is used
1120 when we do not want to automatically collect constructors based on
1121 the symbol name, presumably because we have some other mechanism
1122 for finding them. */
1125 generic_link_check_archive_element_no_collect (abfd
, info
, pneeded
)
1127 struct bfd_link_info
*info
;
1128 bfd_boolean
*pneeded
;
1130 return generic_link_check_archive_element (abfd
, info
, pneeded
, FALSE
);
1133 /* See if we should include an archive element. This version is used
1134 when we want to automatically collect constructors based on the
1135 symbol name, as collect2 does. */
1138 generic_link_check_archive_element_collect (abfd
, info
, pneeded
)
1140 struct bfd_link_info
*info
;
1141 bfd_boolean
*pneeded
;
1143 return generic_link_check_archive_element (abfd
, info
, pneeded
, TRUE
);
1146 /* See if we should include an archive element. Optionally collect
1150 generic_link_check_archive_element (abfd
, info
, pneeded
, collect
)
1152 struct bfd_link_info
*info
;
1153 bfd_boolean
*pneeded
;
1154 bfd_boolean collect
;
1156 asymbol
**pp
, **ppend
;
1160 if (! generic_link_read_symbols (abfd
))
1163 pp
= _bfd_generic_link_get_symbols (abfd
);
1164 ppend
= pp
+ _bfd_generic_link_get_symcount (abfd
);
1165 for (; pp
< ppend
; pp
++)
1168 struct bfd_link_hash_entry
*h
;
1172 /* We are only interested in globally visible symbols. */
1173 if (! bfd_is_com_section (p
->section
)
1174 && (p
->flags
& (BSF_GLOBAL
| BSF_INDIRECT
| BSF_WEAK
)) == 0)
1177 /* We are only interested if we know something about this
1178 symbol, and it is undefined or common. An undefined weak
1179 symbol (type bfd_link_hash_undefweak) is not considered to be
1180 a reference when pulling files out of an archive. See the
1181 SVR4 ABI, p. 4-27. */
1182 h
= bfd_link_hash_lookup (info
->hash
, bfd_asymbol_name (p
), FALSE
,
1184 if (h
== (struct bfd_link_hash_entry
*) NULL
1185 || (h
->type
!= bfd_link_hash_undefined
1186 && h
->type
!= bfd_link_hash_common
))
1189 /* P is a symbol we are looking for. */
1191 if (! bfd_is_com_section (p
->section
))
1193 bfd_size_type symcount
;
1196 /* This object file defines this symbol, so pull it in. */
1197 if (! (*info
->callbacks
->add_archive_element
) (info
, abfd
,
1198 bfd_asymbol_name (p
)))
1200 symcount
= _bfd_generic_link_get_symcount (abfd
);
1201 symbols
= _bfd_generic_link_get_symbols (abfd
);
1202 if (! generic_link_add_symbol_list (abfd
, info
, symcount
,
1209 /* P is a common symbol. */
1211 if (h
->type
== bfd_link_hash_undefined
)
1217 symbfd
= h
->u
.undef
.abfd
;
1218 if (symbfd
== (bfd
*) NULL
)
1220 /* This symbol was created as undefined from outside
1221 BFD. We assume that we should link in the object
1222 file. This is for the -u option in the linker. */
1223 if (! (*info
->callbacks
->add_archive_element
)
1224 (info
, abfd
, bfd_asymbol_name (p
)))
1230 /* Turn the symbol into a common symbol but do not link in
1231 the object file. This is how a.out works. Object
1232 formats that require different semantics must implement
1233 this function differently. This symbol is already on the
1234 undefs list. We add the section to a common section
1235 attached to symbfd to ensure that it is in a BFD which
1236 will be linked in. */
1237 h
->type
= bfd_link_hash_common
;
1239 ((struct bfd_link_hash_common_entry
*)
1240 bfd_hash_allocate (&info
->hash
->table
,
1241 sizeof (struct bfd_link_hash_common_entry
)));
1242 if (h
->u
.c
.p
== NULL
)
1245 size
= bfd_asymbol_value (p
);
1248 power
= bfd_log2 (size
);
1251 h
->u
.c
.p
->alignment_power
= power
;
1253 if (p
->section
== bfd_com_section_ptr
)
1254 h
->u
.c
.p
->section
= bfd_make_section_old_way (symbfd
, "COMMON");
1256 h
->u
.c
.p
->section
= bfd_make_section_old_way (symbfd
,
1258 h
->u
.c
.p
->section
->flags
= SEC_ALLOC
;
1262 /* Adjust the size of the common symbol if necessary. This
1263 is how a.out works. Object formats that require
1264 different semantics must implement this function
1266 if (bfd_asymbol_value (p
) > h
->u
.c
.size
)
1267 h
->u
.c
.size
= bfd_asymbol_value (p
);
1271 /* This archive element is not needed. */
1275 /* Add the symbols from an object file to the global hash table. ABFD
1276 is the object file. INFO is the linker information. SYMBOL_COUNT
1277 is the number of symbols. SYMBOLS is the list of symbols. COLLECT
1278 is TRUE if constructors should be automatically collected by name
1279 as is done by collect2. */
1282 generic_link_add_symbol_list (abfd
, info
, symbol_count
, symbols
, collect
)
1284 struct bfd_link_info
*info
;
1285 bfd_size_type symbol_count
;
1287 bfd_boolean collect
;
1289 asymbol
**pp
, **ppend
;
1292 ppend
= symbols
+ symbol_count
;
1293 for (; pp
< ppend
; pp
++)
1299 if ((p
->flags
& (BSF_INDIRECT
1304 || bfd_is_und_section (bfd_get_section (p
))
1305 || bfd_is_com_section (bfd_get_section (p
))
1306 || bfd_is_ind_section (bfd_get_section (p
)))
1310 struct generic_link_hash_entry
*h
;
1311 struct bfd_link_hash_entry
*bh
;
1313 name
= bfd_asymbol_name (p
);
1314 if (((p
->flags
& BSF_INDIRECT
) != 0
1315 || bfd_is_ind_section (p
->section
))
1319 string
= bfd_asymbol_name (*pp
);
1321 else if ((p
->flags
& BSF_WARNING
) != 0
1324 /* The name of P is actually the warning string, and the
1325 next symbol is the one to warn about. */
1328 name
= bfd_asymbol_name (*pp
);
1334 if (! (_bfd_generic_link_add_one_symbol
1335 (info
, abfd
, name
, p
->flags
, bfd_get_section (p
),
1336 p
->value
, string
, FALSE
, collect
, &bh
)))
1338 h
= (struct generic_link_hash_entry
*) bh
;
1340 /* If this is a constructor symbol, and the linker didn't do
1341 anything with it, then we want to just pass the symbol
1342 through to the output file. This will happen when
1344 if ((p
->flags
& BSF_CONSTRUCTOR
) != 0
1345 && (h
== NULL
|| h
->root
.type
== bfd_link_hash_new
))
1351 /* Save the BFD symbol so that we don't lose any backend
1352 specific information that may be attached to it. We only
1353 want this one if it gives more information than the
1354 existing one; we don't want to replace a defined symbol
1355 with an undefined one. This routine may be called with a
1356 hash table other than the generic hash table, so we only
1357 do this if we are certain that the hash table is a
1359 if (info
->hash
->creator
== abfd
->xvec
)
1361 if (h
->sym
== (asymbol
*) NULL
1362 || (! bfd_is_und_section (bfd_get_section (p
))
1363 && (! bfd_is_com_section (bfd_get_section (p
))
1364 || bfd_is_und_section (bfd_get_section (h
->sym
)))))
1367 /* BSF_OLD_COMMON is a hack to support COFF reloc
1368 reading, and it should go away when the COFF
1369 linker is switched to the new version. */
1370 if (bfd_is_com_section (bfd_get_section (p
)))
1371 p
->flags
|= BSF_OLD_COMMON
;
1375 /* Store a back pointer from the symbol to the hash
1376 table entry for the benefit of relaxation code until
1377 it gets rewritten to not use asymbol structures.
1378 Setting this is also used to check whether these
1379 symbols were set up by the generic linker. */
1380 p
->udata
.p
= (PTR
) h
;
1387 /* We use a state table to deal with adding symbols from an object
1388 file. The first index into the state table describes the symbol
1389 from the object file. The second index into the state table is the
1390 type of the symbol in the hash table. */
1392 /* The symbol from the object file is turned into one of these row
1397 UNDEF_ROW
, /* Undefined. */
1398 UNDEFW_ROW
, /* Weak undefined. */
1399 DEF_ROW
, /* Defined. */
1400 DEFW_ROW
, /* Weak defined. */
1401 COMMON_ROW
, /* Common. */
1402 INDR_ROW
, /* Indirect. */
1403 WARN_ROW
, /* Warning. */
1404 SET_ROW
/* Member of set. */
1407 /* apparently needed for Hitachi 3050R(HI-UX/WE2)? */
1410 /* The actions to take in the state table. */
1415 UND
, /* Mark symbol undefined. */
1416 WEAK
, /* Mark symbol weak undefined. */
1417 DEF
, /* Mark symbol defined. */
1418 DEFW
, /* Mark symbol weak defined. */
1419 COM
, /* Mark symbol common. */
1420 REF
, /* Mark defined symbol referenced. */
1421 CREF
, /* Possibly warn about common reference to defined symbol. */
1422 CDEF
, /* Define existing common symbol. */
1423 NOACT
, /* No action. */
1424 BIG
, /* Mark symbol common using largest size. */
1425 MDEF
, /* Multiple definition error. */
1426 MIND
, /* Multiple indirect symbols. */
1427 IND
, /* Make indirect symbol. */
1428 CIND
, /* Make indirect symbol from existing common symbol. */
1429 SET
, /* Add value to set. */
1430 MWARN
, /* Make warning symbol. */
1431 WARN
, /* Issue warning. */
1432 CWARN
, /* Warn if referenced, else MWARN. */
1433 CYCLE
, /* Repeat with symbol pointed to. */
1434 REFC
, /* Mark indirect symbol referenced and then CYCLE. */
1435 WARNC
/* Issue warning and then CYCLE. */
1438 /* The state table itself. The first index is a link_row and the
1439 second index is a bfd_link_hash_type. */
1441 static const enum link_action link_action
[8][8] =
1443 /* current\prev new undef undefw def defw com indr warn */
1444 /* UNDEF_ROW */ {UND
, NOACT
, UND
, REF
, REF
, NOACT
, REFC
, WARNC
},
1445 /* UNDEFW_ROW */ {WEAK
, NOACT
, NOACT
, REF
, REF
, NOACT
, REFC
, WARNC
},
1446 /* DEF_ROW */ {DEF
, DEF
, DEF
, MDEF
, DEF
, CDEF
, MDEF
, CYCLE
},
1447 /* DEFW_ROW */ {DEFW
, DEFW
, DEFW
, NOACT
, NOACT
, NOACT
, NOACT
, CYCLE
},
1448 /* COMMON_ROW */ {COM
, COM
, COM
, CREF
, COM
, BIG
, REFC
, WARNC
},
1449 /* INDR_ROW */ {IND
, IND
, IND
, MDEF
, IND
, CIND
, MIND
, CYCLE
},
1450 /* WARN_ROW */ {MWARN
, WARN
, WARN
, CWARN
, CWARN
, WARN
, CWARN
, NOACT
},
1451 /* SET_ROW */ {SET
, SET
, SET
, SET
, SET
, SET
, CYCLE
, CYCLE
}
1454 /* Most of the entries in the LINK_ACTION table are straightforward,
1455 but a few are somewhat subtle.
1457 A reference to an indirect symbol (UNDEF_ROW/indr or
1458 UNDEFW_ROW/indr) is counted as a reference both to the indirect
1459 symbol and to the symbol the indirect symbol points to.
1461 A reference to a warning symbol (UNDEF_ROW/warn or UNDEFW_ROW/warn)
1462 causes the warning to be issued.
1464 A common definition of an indirect symbol (COMMON_ROW/indr) is
1465 treated as a multiple definition error. Likewise for an indirect
1466 definition of a common symbol (INDR_ROW/com).
1468 An indirect definition of a warning (INDR_ROW/warn) does not cause
1469 the warning to be issued.
1471 If a warning is created for an indirect symbol (WARN_ROW/indr) no
1472 warning is created for the symbol the indirect symbol points to.
1474 Adding an entry to a set does not count as a reference to a set,
1475 and no warning is issued (SET_ROW/warn). */
1477 /* Return the BFD in which a hash entry has been defined, if known. */
1481 struct bfd_link_hash_entry
*h
;
1483 while (h
->type
== bfd_link_hash_warning
)
1489 case bfd_link_hash_undefined
:
1490 case bfd_link_hash_undefweak
:
1491 return h
->u
.undef
.abfd
;
1492 case bfd_link_hash_defined
:
1493 case bfd_link_hash_defweak
:
1494 return h
->u
.def
.section
->owner
;
1495 case bfd_link_hash_common
:
1496 return h
->u
.c
.p
->section
->owner
;
1501 /* Add a symbol to the global hash table.
1502 ABFD is the BFD the symbol comes from.
1503 NAME is the name of the symbol.
1504 FLAGS is the BSF_* bits associated with the symbol.
1505 SECTION is the section in which the symbol is defined; this may be
1506 bfd_und_section_ptr or bfd_com_section_ptr.
1507 VALUE is the value of the symbol, relative to the section.
1508 STRING is used for either an indirect symbol, in which case it is
1509 the name of the symbol to indirect to, or a warning symbol, in
1510 which case it is the warning string.
1511 COPY is TRUE if NAME or STRING must be copied into locally
1512 allocated memory if they need to be saved.
1513 COLLECT is TRUE if we should automatically collect gcc constructor
1514 or destructor names as collect2 does.
1515 HASHP, if not NULL, is a place to store the created hash table
1516 entry; if *HASHP is not NULL, the caller has already looked up
1517 the hash table entry, and stored it in *HASHP. */
1520 _bfd_generic_link_add_one_symbol (info
, abfd
, name
, flags
, section
, value
,
1521 string
, copy
, collect
, hashp
)
1522 struct bfd_link_info
*info
;
1530 bfd_boolean collect
;
1531 struct bfd_link_hash_entry
**hashp
;
1534 struct bfd_link_hash_entry
*h
;
1537 if (bfd_is_ind_section (section
)
1538 || (flags
& BSF_INDIRECT
) != 0)
1540 else if ((flags
& BSF_WARNING
) != 0)
1542 else if ((flags
& BSF_CONSTRUCTOR
) != 0)
1544 else if (bfd_is_und_section (section
))
1546 if ((flags
& BSF_WEAK
) != 0)
1551 else if ((flags
& BSF_WEAK
) != 0)
1553 else if (bfd_is_com_section (section
))
1558 if (hashp
!= NULL
&& *hashp
!= NULL
)
1562 if (row
== UNDEF_ROW
|| row
== UNDEFW_ROW
)
1563 h
= bfd_wrapped_link_hash_lookup (abfd
, info
, name
, TRUE
, copy
, FALSE
);
1565 h
= bfd_link_hash_lookup (info
->hash
, name
, TRUE
, copy
, FALSE
);
1574 if (info
->notice_all
1575 || (info
->notice_hash
!= (struct bfd_hash_table
*) NULL
1576 && (bfd_hash_lookup (info
->notice_hash
, name
, FALSE
, FALSE
)
1577 != (struct bfd_hash_entry
*) NULL
)))
1579 if (! (*info
->callbacks
->notice
) (info
, h
->root
.string
, abfd
, section
,
1584 if (hashp
!= (struct bfd_link_hash_entry
**) NULL
)
1589 enum link_action action
;
1592 action
= link_action
[(int) row
][(int) h
->type
];
1603 /* Make a new undefined symbol. */
1604 h
->type
= bfd_link_hash_undefined
;
1605 h
->u
.undef
.abfd
= abfd
;
1606 bfd_link_add_undef (info
->hash
, h
);
1610 /* Make a new weak undefined symbol. */
1611 h
->type
= bfd_link_hash_undefweak
;
1612 h
->u
.undef
.abfd
= abfd
;
1616 /* We have found a definition for a symbol which was
1617 previously common. */
1618 BFD_ASSERT (h
->type
== bfd_link_hash_common
);
1619 if (! ((*info
->callbacks
->multiple_common
)
1620 (info
, h
->root
.string
,
1621 h
->u
.c
.p
->section
->owner
, bfd_link_hash_common
, h
->u
.c
.size
,
1622 abfd
, bfd_link_hash_defined
, (bfd_vma
) 0)))
1628 enum bfd_link_hash_type oldtype
;
1630 /* Define a symbol. */
1633 h
->type
= bfd_link_hash_defweak
;
1635 h
->type
= bfd_link_hash_defined
;
1636 h
->u
.def
.section
= section
;
1637 h
->u
.def
.value
= value
;
1639 /* If we have been asked to, we act like collect2 and
1640 identify all functions that might be global
1641 constructors and destructors and pass them up in a
1642 callback. We only do this for certain object file
1643 types, since many object file types can handle this
1645 if (collect
&& name
[0] == '_')
1649 /* A constructor or destructor name starts like this:
1650 _+GLOBAL_[_.$][ID][_.$] where the first [_.$] and
1651 the second are the same character (we accept any
1652 character there, in case a new object file format
1653 comes along with even worse naming restrictions). */
1655 #define CONS_PREFIX "GLOBAL_"
1656 #define CONS_PREFIX_LEN (sizeof CONS_PREFIX - 1)
1662 && strncmp (s
, CONS_PREFIX
, CONS_PREFIX_LEN
- 1) == 0)
1666 c
= s
[CONS_PREFIX_LEN
+ 1];
1667 if ((c
== 'I' || c
== 'D')
1668 && s
[CONS_PREFIX_LEN
] == s
[CONS_PREFIX_LEN
+ 2])
1670 /* If this is a definition of a symbol which
1671 was previously weakly defined, we are in
1672 trouble. We have already added a
1673 constructor entry for the weak defined
1674 symbol, and now we are trying to add one
1675 for the new symbol. Fortunately, this case
1676 should never arise in practice. */
1677 if (oldtype
== bfd_link_hash_defweak
)
1680 if (! ((*info
->callbacks
->constructor
)
1682 h
->root
.string
, abfd
, section
, value
)))
1692 /* We have found a common definition for a symbol. */
1693 if (h
->type
== bfd_link_hash_new
)
1694 bfd_link_add_undef (info
->hash
, h
);
1695 h
->type
= bfd_link_hash_common
;
1697 ((struct bfd_link_hash_common_entry
*)
1698 bfd_hash_allocate (&info
->hash
->table
,
1699 sizeof (struct bfd_link_hash_common_entry
)));
1700 if (h
->u
.c
.p
== NULL
)
1703 h
->u
.c
.size
= value
;
1705 /* Select a default alignment based on the size. This may
1706 be overridden by the caller. */
1710 power
= bfd_log2 (value
);
1713 h
->u
.c
.p
->alignment_power
= power
;
1716 /* The section of a common symbol is only used if the common
1717 symbol is actually allocated. It basically provides a
1718 hook for the linker script to decide which output section
1719 the common symbols should be put in. In most cases, the
1720 section of a common symbol will be bfd_com_section_ptr,
1721 the code here will choose a common symbol section named
1722 "COMMON", and the linker script will contain *(COMMON) in
1723 the appropriate place. A few targets use separate common
1724 sections for small symbols, and they require special
1726 if (section
== bfd_com_section_ptr
)
1728 h
->u
.c
.p
->section
= bfd_make_section_old_way (abfd
, "COMMON");
1729 h
->u
.c
.p
->section
->flags
= SEC_ALLOC
;
1731 else if (section
->owner
!= abfd
)
1733 h
->u
.c
.p
->section
= bfd_make_section_old_way (abfd
,
1735 h
->u
.c
.p
->section
->flags
= SEC_ALLOC
;
1738 h
->u
.c
.p
->section
= section
;
1742 /* A reference to a defined symbol. */
1743 if (h
->next
== NULL
&& info
->hash
->undefs_tail
!= h
)
1748 /* We have found a common definition for a symbol which
1749 already had a common definition. Use the maximum of the
1750 two sizes, and use the section required by the larger symbol. */
1751 BFD_ASSERT (h
->type
== bfd_link_hash_common
);
1752 if (! ((*info
->callbacks
->multiple_common
)
1753 (info
, h
->root
.string
,
1754 h
->u
.c
.p
->section
->owner
, bfd_link_hash_common
, h
->u
.c
.size
,
1755 abfd
, bfd_link_hash_common
, value
)))
1757 if (value
> h
->u
.c
.size
)
1761 h
->u
.c
.size
= value
;
1763 /* Select a default alignment based on the size. This may
1764 be overridden by the caller. */
1765 power
= bfd_log2 (value
);
1768 h
->u
.c
.p
->alignment_power
= power
;
1770 /* Some systems have special treatment for small commons,
1771 hence we want to select the section used by the larger
1772 symbol. This makes sure the symbol does not go in a
1773 small common section if it is now too large. */
1774 if (section
== bfd_com_section_ptr
)
1777 = bfd_make_section_old_way (abfd
, "COMMON");
1778 h
->u
.c
.p
->section
->flags
= SEC_ALLOC
;
1780 else if (section
->owner
!= abfd
)
1783 = bfd_make_section_old_way (abfd
, section
->name
);
1784 h
->u
.c
.p
->section
->flags
= SEC_ALLOC
;
1787 h
->u
.c
.p
->section
= section
;
1795 /* We have found a common definition for a symbol which
1796 was already defined. FIXME: It would nice if we could
1797 report the BFD which defined an indirect symbol, but we
1798 don't have anywhere to store the information. */
1799 if (h
->type
== bfd_link_hash_defined
1800 || h
->type
== bfd_link_hash_defweak
)
1801 obfd
= h
->u
.def
.section
->owner
;
1804 if (! ((*info
->callbacks
->multiple_common
)
1805 (info
, h
->root
.string
, obfd
, h
->type
, (bfd_vma
) 0,
1806 abfd
, bfd_link_hash_common
, value
)))
1812 /* Multiple indirect symbols. This is OK if they both point
1813 to the same symbol. */
1814 if (strcmp (h
->u
.i
.link
->root
.string
, string
) == 0)
1818 /* Handle a multiple definition. */
1819 if (!info
->allow_multiple_definition
)
1821 asection
*msec
= NULL
;
1826 case bfd_link_hash_defined
:
1827 msec
= h
->u
.def
.section
;
1828 mval
= h
->u
.def
.value
;
1830 case bfd_link_hash_indirect
:
1831 msec
= bfd_ind_section_ptr
;
1838 /* Ignore a redefinition of an absolute symbol to the
1839 same value; it's harmless. */
1840 if (h
->type
== bfd_link_hash_defined
1841 && bfd_is_abs_section (msec
)
1842 && bfd_is_abs_section (section
)
1846 if (! ((*info
->callbacks
->multiple_definition
)
1847 (info
, h
->root
.string
, msec
->owner
, msec
, mval
,
1848 abfd
, section
, value
)))
1854 /* Create an indirect symbol from an existing common symbol. */
1855 BFD_ASSERT (h
->type
== bfd_link_hash_common
);
1856 if (! ((*info
->callbacks
->multiple_common
)
1857 (info
, h
->root
.string
,
1858 h
->u
.c
.p
->section
->owner
, bfd_link_hash_common
, h
->u
.c
.size
,
1859 abfd
, bfd_link_hash_indirect
, (bfd_vma
) 0)))
1863 /* Create an indirect symbol. */
1865 struct bfd_link_hash_entry
*inh
;
1867 /* STRING is the name of the symbol we want to indirect
1869 inh
= bfd_wrapped_link_hash_lookup (abfd
, info
, string
, TRUE
,
1871 if (inh
== (struct bfd_link_hash_entry
*) NULL
)
1873 if (inh
->type
== bfd_link_hash_indirect
1874 && inh
->u
.i
.link
== h
)
1876 (*_bfd_error_handler
)
1877 (_("%s: indirect symbol `%s' to `%s' is a loop"),
1878 bfd_archive_filename (abfd
), name
, string
);
1879 bfd_set_error (bfd_error_invalid_operation
);
1882 if (inh
->type
== bfd_link_hash_new
)
1884 inh
->type
= bfd_link_hash_undefined
;
1885 inh
->u
.undef
.abfd
= abfd
;
1886 bfd_link_add_undef (info
->hash
, inh
);
1889 /* If the indirect symbol has been referenced, we need to
1890 push the reference down to the symbol we are
1892 if (h
->type
!= bfd_link_hash_new
)
1898 h
->type
= bfd_link_hash_indirect
;
1904 /* Add an entry to a set. */
1905 if (! (*info
->callbacks
->add_to_set
) (info
, h
, BFD_RELOC_CTOR
,
1906 abfd
, section
, value
))
1911 /* Issue a warning and cycle. */
1912 if (h
->u
.i
.warning
!= NULL
)
1914 if (! (*info
->callbacks
->warning
) (info
, h
->u
.i
.warning
,
1915 h
->root
.string
, abfd
,
1919 /* Only issue a warning once. */
1920 h
->u
.i
.warning
= NULL
;
1924 /* Try again with the referenced symbol. */
1930 /* A reference to an indirect symbol. */
1931 if (h
->next
== NULL
&& info
->hash
->undefs_tail
!= h
)
1938 /* Issue a warning. */
1939 if (! (*info
->callbacks
->warning
) (info
, string
, h
->root
.string
,
1941 (asection
*) NULL
, (bfd_vma
) 0))
1946 /* Warn if this symbol has been referenced already,
1947 otherwise add a warning. A symbol has been referenced if
1948 the next field is not NULL, or it is the tail of the
1949 undefined symbol list. The REF case above helps to
1951 if (h
->next
!= NULL
|| info
->hash
->undefs_tail
== h
)
1953 if (! (*info
->callbacks
->warning
) (info
, string
, h
->root
.string
,
1962 /* Make a warning symbol. */
1964 struct bfd_link_hash_entry
*sub
;
1966 /* STRING is the warning to give. */
1967 sub
= ((struct bfd_link_hash_entry
*)
1968 ((*info
->hash
->table
.newfunc
)
1969 ((struct bfd_hash_entry
*) NULL
, &info
->hash
->table
,
1974 sub
->type
= bfd_link_hash_warning
;
1977 sub
->u
.i
.warning
= string
;
1981 size_t len
= strlen (string
) + 1;
1983 w
= bfd_hash_allocate (&info
->hash
->table
, len
);
1986 memcpy (w
, string
, len
);
1987 sub
->u
.i
.warning
= w
;
1990 bfd_hash_replace (&info
->hash
->table
,
1991 (struct bfd_hash_entry
*) h
,
1992 (struct bfd_hash_entry
*) sub
);
2004 /* Generic final link routine. */
2007 _bfd_generic_final_link (abfd
, info
)
2009 struct bfd_link_info
*info
;
2013 struct bfd_link_order
*p
;
2015 struct generic_write_global_symbol_info wginfo
;
2017 bfd_get_outsymbols (abfd
) = (asymbol
**) NULL
;
2018 bfd_get_symcount (abfd
) = 0;
2021 /* Mark all sections which will be included in the output file. */
2022 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
2023 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
2024 if (p
->type
== bfd_indirect_link_order
)
2025 p
->u
.indirect
.section
->linker_mark
= TRUE
;
2027 /* Build the output symbol table. */
2028 for (sub
= info
->input_bfds
; sub
!= (bfd
*) NULL
; sub
= sub
->link_next
)
2029 if (! _bfd_generic_link_output_symbols (abfd
, sub
, info
, &outsymalloc
))
2032 /* Accumulate the global symbols. */
2034 wginfo
.output_bfd
= abfd
;
2035 wginfo
.psymalloc
= &outsymalloc
;
2036 _bfd_generic_link_hash_traverse (_bfd_generic_hash_table (info
),
2037 _bfd_generic_link_write_global_symbol
,
2040 /* Make sure we have a trailing NULL pointer on OUTSYMBOLS. We
2041 shouldn't really need one, since we have SYMCOUNT, but some old
2042 code still expects one. */
2043 if (! generic_add_output_symbol (abfd
, &outsymalloc
, NULL
))
2046 if (info
->relocateable
)
2048 /* Allocate space for the output relocs for each section. */
2049 for (o
= abfd
->sections
;
2050 o
!= (asection
*) NULL
;
2054 for (p
= o
->link_order_head
;
2055 p
!= (struct bfd_link_order
*) NULL
;
2058 if (p
->type
== bfd_section_reloc_link_order
2059 || p
->type
== bfd_symbol_reloc_link_order
)
2061 else if (p
->type
== bfd_indirect_link_order
)
2063 asection
*input_section
;
2070 input_section
= p
->u
.indirect
.section
;
2071 input_bfd
= input_section
->owner
;
2072 relsize
= bfd_get_reloc_upper_bound (input_bfd
,
2076 relocs
= (arelent
**) bfd_malloc ((bfd_size_type
) relsize
);
2077 if (!relocs
&& relsize
!= 0)
2079 symbols
= _bfd_generic_link_get_symbols (input_bfd
);
2080 reloc_count
= bfd_canonicalize_reloc (input_bfd
,
2084 if (reloc_count
< 0)
2086 BFD_ASSERT ((unsigned long) reloc_count
2087 == input_section
->reloc_count
);
2088 o
->reloc_count
+= reloc_count
;
2092 if (o
->reloc_count
> 0)
2096 amt
= o
->reloc_count
;
2097 amt
*= sizeof (arelent
*);
2098 o
->orelocation
= (arelent
**) bfd_alloc (abfd
, amt
);
2099 if (!o
->orelocation
)
2101 o
->flags
|= SEC_RELOC
;
2102 /* Reset the count so that it can be used as an index
2103 when putting in the output relocs. */
2109 /* Handle all the link order information for the sections. */
2110 for (o
= abfd
->sections
;
2111 o
!= (asection
*) NULL
;
2114 for (p
= o
->link_order_head
;
2115 p
!= (struct bfd_link_order
*) NULL
;
2120 case bfd_section_reloc_link_order
:
2121 case bfd_symbol_reloc_link_order
:
2122 if (! _bfd_generic_reloc_link_order (abfd
, info
, o
, p
))
2125 case bfd_indirect_link_order
:
2126 if (! default_indirect_link_order (abfd
, info
, o
, p
, TRUE
))
2130 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
2140 /* Add an output symbol to the output BFD. */
2143 generic_add_output_symbol (output_bfd
, psymalloc
, sym
)
2148 if (bfd_get_symcount (output_bfd
) >= *psymalloc
)
2153 if (*psymalloc
== 0)
2158 amt
*= sizeof (asymbol
*);
2159 newsyms
= (asymbol
**) bfd_realloc (bfd_get_outsymbols (output_bfd
), amt
);
2160 if (newsyms
== (asymbol
**) NULL
)
2162 bfd_get_outsymbols (output_bfd
) = newsyms
;
2165 bfd_get_outsymbols (output_bfd
) [bfd_get_symcount (output_bfd
)] = sym
;
2167 ++ bfd_get_symcount (output_bfd
);
2172 /* Handle the symbols for an input BFD. */
2175 _bfd_generic_link_output_symbols (output_bfd
, input_bfd
, info
, psymalloc
)
2178 struct bfd_link_info
*info
;
2184 if (! generic_link_read_symbols (input_bfd
))
2187 /* Create a filename symbol if we are supposed to. */
2188 if (info
->create_object_symbols_section
!= (asection
*) NULL
)
2192 for (sec
= input_bfd
->sections
;
2193 sec
!= (asection
*) NULL
;
2196 if (sec
->output_section
== info
->create_object_symbols_section
)
2200 newsym
= bfd_make_empty_symbol (input_bfd
);
2203 newsym
->name
= input_bfd
->filename
;
2205 newsym
->flags
= BSF_LOCAL
| BSF_FILE
;
2206 newsym
->section
= sec
;
2208 if (! generic_add_output_symbol (output_bfd
, psymalloc
,
2217 /* Adjust the values of the globally visible symbols, and write out
2219 sym_ptr
= _bfd_generic_link_get_symbols (input_bfd
);
2220 sym_end
= sym_ptr
+ _bfd_generic_link_get_symcount (input_bfd
);
2221 for (; sym_ptr
< sym_end
; sym_ptr
++)
2224 struct generic_link_hash_entry
*h
;
2227 h
= (struct generic_link_hash_entry
*) NULL
;
2229 if ((sym
->flags
& (BSF_INDIRECT
2234 || bfd_is_und_section (bfd_get_section (sym
))
2235 || bfd_is_com_section (bfd_get_section (sym
))
2236 || bfd_is_ind_section (bfd_get_section (sym
)))
2238 if (sym
->udata
.p
!= NULL
)
2239 h
= (struct generic_link_hash_entry
*) sym
->udata
.p
;
2240 else if ((sym
->flags
& BSF_CONSTRUCTOR
) != 0)
2242 /* This case normally means that the main linker code
2243 deliberately ignored this constructor symbol. We
2244 should just pass it through. This will screw up if
2245 the constructor symbol is from a different,
2246 non-generic, object file format, but the case will
2247 only arise when linking with -r, which will probably
2248 fail anyhow, since there will be no way to represent
2249 the relocs in the output format being used. */
2252 else if (bfd_is_und_section (bfd_get_section (sym
)))
2253 h
= ((struct generic_link_hash_entry
*)
2254 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
2255 bfd_asymbol_name (sym
),
2256 FALSE
, FALSE
, TRUE
));
2258 h
= _bfd_generic_link_hash_lookup (_bfd_generic_hash_table (info
),
2259 bfd_asymbol_name (sym
),
2260 FALSE
, FALSE
, TRUE
);
2262 if (h
!= (struct generic_link_hash_entry
*) NULL
)
2264 /* Force all references to this symbol to point to
2265 the same area in memory. It is possible that
2266 this routine will be called with a hash table
2267 other than a generic hash table, so we double
2269 if (info
->hash
->creator
== input_bfd
->xvec
)
2271 if (h
->sym
!= (asymbol
*) NULL
)
2272 *sym_ptr
= sym
= h
->sym
;
2275 switch (h
->root
.type
)
2278 case bfd_link_hash_new
:
2280 case bfd_link_hash_undefined
:
2282 case bfd_link_hash_undefweak
:
2283 sym
->flags
|= BSF_WEAK
;
2285 case bfd_link_hash_indirect
:
2286 h
= (struct generic_link_hash_entry
*) h
->root
.u
.i
.link
;
2288 case bfd_link_hash_defined
:
2289 sym
->flags
|= BSF_GLOBAL
;
2290 sym
->flags
&=~ BSF_CONSTRUCTOR
;
2291 sym
->value
= h
->root
.u
.def
.value
;
2292 sym
->section
= h
->root
.u
.def
.section
;
2294 case bfd_link_hash_defweak
:
2295 sym
->flags
|= BSF_WEAK
;
2296 sym
->flags
&=~ BSF_CONSTRUCTOR
;
2297 sym
->value
= h
->root
.u
.def
.value
;
2298 sym
->section
= h
->root
.u
.def
.section
;
2300 case bfd_link_hash_common
:
2301 sym
->value
= h
->root
.u
.c
.size
;
2302 sym
->flags
|= BSF_GLOBAL
;
2303 if (! bfd_is_com_section (sym
->section
))
2305 BFD_ASSERT (bfd_is_und_section (sym
->section
));
2306 sym
->section
= bfd_com_section_ptr
;
2308 /* We do not set the section of the symbol to
2309 h->root.u.c.p->section. That value was saved so
2310 that we would know where to allocate the symbol
2311 if it was defined. In this case the type is
2312 still bfd_link_hash_common, so we did not define
2313 it, so we do not want to use that section. */
2319 /* This switch is straight from the old code in
2320 write_file_locals in ldsym.c. */
2321 if (info
->strip
== strip_all
2322 || (info
->strip
== strip_some
2323 && (bfd_hash_lookup (info
->keep_hash
, bfd_asymbol_name (sym
),
2325 == (struct bfd_hash_entry
*) NULL
)))
2327 else if ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0)
2329 /* If this symbol is marked as occurring now, rather
2330 than at the end, output it now. This is used for
2331 COFF C_EXT FCN symbols. FIXME: There must be a
2333 if (bfd_asymbol_bfd (sym
) == input_bfd
2334 && (sym
->flags
& BSF_NOT_AT_END
) != 0)
2339 else if (bfd_is_ind_section (sym
->section
))
2341 else if ((sym
->flags
& BSF_DEBUGGING
) != 0)
2343 if (info
->strip
== strip_none
)
2348 else if (bfd_is_und_section (sym
->section
)
2349 || bfd_is_com_section (sym
->section
))
2351 else if ((sym
->flags
& BSF_LOCAL
) != 0)
2353 if ((sym
->flags
& BSF_WARNING
) != 0)
2357 switch (info
->discard
)
2363 case discard_sec_merge
:
2365 if (info
->relocateable
2366 || ! (sym
->section
->flags
& SEC_MERGE
))
2370 if (bfd_is_local_label (input_bfd
, sym
))
2381 else if ((sym
->flags
& BSF_CONSTRUCTOR
))
2383 if (info
->strip
!= strip_all
)
2391 /* If this symbol is in a section which is not being included
2392 in the output file, then we don't want to output the symbol.
2394 Gross. .bss and similar sections won't have the linker_mark
2396 if ((sym
->section
->flags
& SEC_HAS_CONTENTS
) != 0
2397 && ! sym
->section
->linker_mark
)
2402 if (! generic_add_output_symbol (output_bfd
, psymalloc
, sym
))
2404 if (h
!= (struct generic_link_hash_entry
*) NULL
)
2412 /* Set the section and value of a generic BFD symbol based on a linker
2413 hash table entry. */
2416 set_symbol_from_hash (sym
, h
)
2418 struct bfd_link_hash_entry
*h
;
2425 case bfd_link_hash_new
:
2426 /* This can happen when a constructor symbol is seen but we are
2427 not building constructors. */
2428 if (sym
->section
!= NULL
)
2430 BFD_ASSERT ((sym
->flags
& BSF_CONSTRUCTOR
) != 0);
2434 sym
->flags
|= BSF_CONSTRUCTOR
;
2435 sym
->section
= bfd_abs_section_ptr
;
2439 case bfd_link_hash_undefined
:
2440 sym
->section
= bfd_und_section_ptr
;
2443 case bfd_link_hash_undefweak
:
2444 sym
->section
= bfd_und_section_ptr
;
2446 sym
->flags
|= BSF_WEAK
;
2448 case bfd_link_hash_defined
:
2449 sym
->section
= h
->u
.def
.section
;
2450 sym
->value
= h
->u
.def
.value
;
2452 case bfd_link_hash_defweak
:
2453 sym
->flags
|= BSF_WEAK
;
2454 sym
->section
= h
->u
.def
.section
;
2455 sym
->value
= h
->u
.def
.value
;
2457 case bfd_link_hash_common
:
2458 sym
->value
= h
->u
.c
.size
;
2459 if (sym
->section
== NULL
)
2460 sym
->section
= bfd_com_section_ptr
;
2461 else if (! bfd_is_com_section (sym
->section
))
2463 BFD_ASSERT (bfd_is_und_section (sym
->section
));
2464 sym
->section
= bfd_com_section_ptr
;
2466 /* Do not set the section; see _bfd_generic_link_output_symbols. */
2468 case bfd_link_hash_indirect
:
2469 case bfd_link_hash_warning
:
2470 /* FIXME: What should we do here? */
2475 /* Write out a global symbol, if it hasn't already been written out.
2476 This is called for each symbol in the hash table. */
2479 _bfd_generic_link_write_global_symbol (h
, data
)
2480 struct generic_link_hash_entry
*h
;
2483 struct generic_write_global_symbol_info
*wginfo
=
2484 (struct generic_write_global_symbol_info
*) data
;
2487 if (h
->root
.type
== bfd_link_hash_warning
)
2488 h
= (struct generic_link_hash_entry
*) h
->root
.u
.i
.link
;
2495 if (wginfo
->info
->strip
== strip_all
2496 || (wginfo
->info
->strip
== strip_some
2497 && bfd_hash_lookup (wginfo
->info
->keep_hash
, h
->root
.root
.string
,
2498 FALSE
, FALSE
) == NULL
))
2501 if (h
->sym
!= (asymbol
*) NULL
)
2505 sym
= bfd_make_empty_symbol (wginfo
->output_bfd
);
2508 sym
->name
= h
->root
.root
.string
;
2512 set_symbol_from_hash (sym
, &h
->root
);
2514 sym
->flags
|= BSF_GLOBAL
;
2516 if (! generic_add_output_symbol (wginfo
->output_bfd
, wginfo
->psymalloc
,
2519 /* FIXME: No way to return failure. */
2526 /* Create a relocation. */
2529 _bfd_generic_reloc_link_order (abfd
, info
, sec
, link_order
)
2531 struct bfd_link_info
*info
;
2533 struct bfd_link_order
*link_order
;
2537 if (! info
->relocateable
)
2539 if (sec
->orelocation
== (arelent
**) NULL
)
2542 r
= (arelent
*) bfd_alloc (abfd
, (bfd_size_type
) sizeof (arelent
));
2543 if (r
== (arelent
*) NULL
)
2546 r
->address
= link_order
->offset
;
2547 r
->howto
= bfd_reloc_type_lookup (abfd
, link_order
->u
.reloc
.p
->reloc
);
2550 bfd_set_error (bfd_error_bad_value
);
2554 /* Get the symbol to use for the relocation. */
2555 if (link_order
->type
== bfd_section_reloc_link_order
)
2556 r
->sym_ptr_ptr
= link_order
->u
.reloc
.p
->u
.section
->symbol_ptr_ptr
;
2559 struct generic_link_hash_entry
*h
;
2561 h
= ((struct generic_link_hash_entry
*)
2562 bfd_wrapped_link_hash_lookup (abfd
, info
,
2563 link_order
->u
.reloc
.p
->u
.name
,
2564 FALSE
, FALSE
, TRUE
));
2565 if (h
== (struct generic_link_hash_entry
*) NULL
2568 if (! ((*info
->callbacks
->unattached_reloc
)
2569 (info
, link_order
->u
.reloc
.p
->u
.name
,
2570 (bfd
*) NULL
, (asection
*) NULL
, (bfd_vma
) 0)))
2572 bfd_set_error (bfd_error_bad_value
);
2575 r
->sym_ptr_ptr
= &h
->sym
;
2578 /* If this is an inplace reloc, write the addend to the object file.
2579 Otherwise, store it in the reloc addend. */
2580 if (! r
->howto
->partial_inplace
)
2581 r
->addend
= link_order
->u
.reloc
.p
->addend
;
2585 bfd_reloc_status_type rstat
;
2590 size
= bfd_get_reloc_size (r
->howto
);
2591 buf
= (bfd_byte
*) bfd_zmalloc (size
);
2592 if (buf
== (bfd_byte
*) NULL
)
2594 rstat
= _bfd_relocate_contents (r
->howto
, abfd
,
2595 (bfd_vma
) link_order
->u
.reloc
.p
->addend
,
2602 case bfd_reloc_outofrange
:
2604 case bfd_reloc_overflow
:
2605 if (! ((*info
->callbacks
->reloc_overflow
)
2607 (link_order
->type
== bfd_section_reloc_link_order
2608 ? bfd_section_name (abfd
, link_order
->u
.reloc
.p
->u
.section
)
2609 : link_order
->u
.reloc
.p
->u
.name
),
2610 r
->howto
->name
, link_order
->u
.reloc
.p
->addend
,
2611 (bfd
*) NULL
, (asection
*) NULL
, (bfd_vma
) 0)))
2618 loc
= link_order
->offset
* bfd_octets_per_byte (abfd
);
2619 ok
= bfd_set_section_contents (abfd
, sec
, (PTR
) buf
, loc
,
2620 (bfd_size_type
) size
);
2628 sec
->orelocation
[sec
->reloc_count
] = r
;
2634 /* Allocate a new link_order for a section. */
2636 struct bfd_link_order
*
2637 bfd_new_link_order (abfd
, section
)
2641 bfd_size_type amt
= sizeof (struct bfd_link_order
);
2642 struct bfd_link_order
*new;
2644 new = (struct bfd_link_order
*) bfd_zalloc (abfd
, amt
);
2648 new->type
= bfd_undefined_link_order
;
2650 if (section
->link_order_tail
!= (struct bfd_link_order
*) NULL
)
2651 section
->link_order_tail
->next
= new;
2653 section
->link_order_head
= new;
2654 section
->link_order_tail
= new;
2659 /* Default link order processing routine. Note that we can not handle
2660 the reloc_link_order types here, since they depend upon the details
2661 of how the particular backends generates relocs. */
2664 _bfd_default_link_order (abfd
, info
, sec
, link_order
)
2666 struct bfd_link_info
*info
;
2668 struct bfd_link_order
*link_order
;
2670 switch (link_order
->type
)
2672 case bfd_undefined_link_order
:
2673 case bfd_section_reloc_link_order
:
2674 case bfd_symbol_reloc_link_order
:
2677 case bfd_indirect_link_order
:
2678 return default_indirect_link_order (abfd
, info
, sec
, link_order
,
2680 case bfd_data_link_order
:
2681 return default_data_link_order (abfd
, info
, sec
, link_order
);
2685 /* Default routine to handle a bfd_data_link_order. */
2688 default_data_link_order (abfd
, info
, sec
, link_order
)
2690 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
2692 struct bfd_link_order
*link_order
;
2700 BFD_ASSERT ((sec
->flags
& SEC_HAS_CONTENTS
) != 0);
2702 size
= link_order
->size
;
2706 fill
= link_order
->u
.data
.contents
;
2707 fill_size
= link_order
->u
.data
.size
;
2708 if (fill_size
!= 0 && fill_size
< size
)
2711 fill
= (bfd_byte
*) bfd_malloc (size
);
2716 memset (p
, (int) link_order
->u
.data
.contents
[0], (size_t) size
);
2721 memcpy (p
, link_order
->u
.data
.contents
, fill_size
);
2725 while (size
>= fill_size
);
2727 memcpy (p
, link_order
->u
.data
.contents
, (size_t) size
);
2728 size
= link_order
->size
;
2732 loc
= link_order
->offset
* bfd_octets_per_byte (abfd
);
2733 result
= bfd_set_section_contents (abfd
, sec
, fill
, loc
, size
);
2735 if (fill
!= link_order
->u
.data
.contents
)
2740 /* Default routine to handle a bfd_indirect_link_order. */
2743 default_indirect_link_order (output_bfd
, info
, output_section
, link_order
,
2746 struct bfd_link_info
*info
;
2747 asection
*output_section
;
2748 struct bfd_link_order
*link_order
;
2749 bfd_boolean generic_linker
;
2751 asection
*input_section
;
2753 bfd_byte
*contents
= NULL
;
2754 bfd_byte
*new_contents
;
2755 bfd_size_type sec_size
;
2758 BFD_ASSERT ((output_section
->flags
& SEC_HAS_CONTENTS
) != 0);
2760 if (link_order
->size
== 0)
2763 input_section
= link_order
->u
.indirect
.section
;
2764 input_bfd
= input_section
->owner
;
2766 BFD_ASSERT (input_section
->output_section
== output_section
);
2767 BFD_ASSERT (input_section
->output_offset
== link_order
->offset
);
2768 BFD_ASSERT (input_section
->_cooked_size
== link_order
->size
);
2770 if (info
->relocateable
2771 && input_section
->reloc_count
> 0
2772 && output_section
->orelocation
== (arelent
**) NULL
)
2774 /* Space has not been allocated for the output relocations.
2775 This can happen when we are called by a specific backend
2776 because somebody is attempting to link together different
2777 types of object files. Handling this case correctly is
2778 difficult, and sometimes impossible. */
2779 (*_bfd_error_handler
)
2780 (_("Attempt to do relocateable link with %s input and %s output"),
2781 bfd_get_target (input_bfd
), bfd_get_target (output_bfd
));
2782 bfd_set_error (bfd_error_wrong_format
);
2786 if (! generic_linker
)
2791 /* Get the canonical symbols. The generic linker will always
2792 have retrieved them by this point, but we are being called by
2793 a specific linker, presumably because we are linking
2794 different types of object files together. */
2795 if (! generic_link_read_symbols (input_bfd
))
2798 /* Since we have been called by a specific linker, rather than
2799 the generic linker, the values of the symbols will not be
2800 right. They will be the values as seen in the input file,
2801 not the values of the final link. We need to fix them up
2802 before we can relocate the section. */
2803 sympp
= _bfd_generic_link_get_symbols (input_bfd
);
2804 symppend
= sympp
+ _bfd_generic_link_get_symcount (input_bfd
);
2805 for (; sympp
< symppend
; sympp
++)
2808 struct bfd_link_hash_entry
*h
;
2812 if ((sym
->flags
& (BSF_INDIRECT
2817 || bfd_is_und_section (bfd_get_section (sym
))
2818 || bfd_is_com_section (bfd_get_section (sym
))
2819 || bfd_is_ind_section (bfd_get_section (sym
)))
2821 /* sym->udata may have been set by
2822 generic_link_add_symbol_list. */
2823 if (sym
->udata
.p
!= NULL
)
2824 h
= (struct bfd_link_hash_entry
*) sym
->udata
.p
;
2825 else if (bfd_is_und_section (bfd_get_section (sym
)))
2826 h
= bfd_wrapped_link_hash_lookup (output_bfd
, info
,
2827 bfd_asymbol_name (sym
),
2828 FALSE
, FALSE
, TRUE
);
2830 h
= bfd_link_hash_lookup (info
->hash
,
2831 bfd_asymbol_name (sym
),
2832 FALSE
, FALSE
, TRUE
);
2834 set_symbol_from_hash (sym
, h
);
2839 /* Get and relocate the section contents. */
2840 sec_size
= bfd_section_size (input_bfd
, input_section
);
2841 contents
= ((bfd_byte
*) bfd_malloc (sec_size
));
2842 if (contents
== NULL
&& sec_size
!= 0)
2844 new_contents
= (bfd_get_relocated_section_contents
2845 (output_bfd
, info
, link_order
, contents
, info
->relocateable
,
2846 _bfd_generic_link_get_symbols (input_bfd
)));
2850 /* Output the section contents. */
2851 loc
= link_order
->offset
* bfd_octets_per_byte (output_bfd
);
2852 if (! bfd_set_section_contents (output_bfd
, output_section
,
2853 (PTR
) new_contents
, loc
, link_order
->size
))
2856 if (contents
!= NULL
)
2861 if (contents
!= NULL
)
2866 /* A little routine to count the number of relocs in a link_order
2870 _bfd_count_link_order_relocs (link_order
)
2871 struct bfd_link_order
*link_order
;
2873 register unsigned int c
;
2874 register struct bfd_link_order
*l
;
2877 for (l
= link_order
; l
!= (struct bfd_link_order
*) NULL
; l
= l
->next
)
2879 if (l
->type
== bfd_section_reloc_link_order
2880 || l
->type
== bfd_symbol_reloc_link_order
)
2889 bfd_link_split_section
2892 bfd_boolean bfd_link_split_section(bfd *abfd, asection *sec);
2895 Return nonzero if @var{sec} should be split during a
2896 reloceatable or final link.
2898 .#define bfd_link_split_section(abfd, sec) \
2899 . BFD_SEND (abfd, _bfd_link_split_section, (abfd, sec))
2905 _bfd_generic_link_split_section (abfd
, sec
)
2906 bfd
*abfd ATTRIBUTE_UNUSED
;
2907 asection
*sec ATTRIBUTE_UNUSED
;