1 /* linker.c -- BFD linker routines
2 Copyright 1993 Free Software Foundation, Inc.
3 Written by Steve Chamberlain and Ian Lance Taylor, Cygnus Support
5 This file is part of BFD
7 GLD is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GLD is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GLD; see the file COPYING. If not, write to
19 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
27 static struct bfd_hash_entry
*generic_link_hash_newfunc
28 PARAMS ((struct bfd_hash_entry
*, struct bfd_hash_table
*,
30 static boolean generic_link_add_object_symbols
31 PARAMS ((bfd
*, struct bfd_link_info
*));
32 static boolean generic_link_check_archive_element
33 PARAMS ((bfd
*, struct bfd_link_info
*, boolean
*pneeded
));
34 static boolean generic_link_add_symbol_list
35 PARAMS ((bfd
*, struct bfd_link_info
*, bfd_size_type count
, asymbol
**));
36 static boolean generic_add_output_symbol
37 PARAMS ((bfd
*, size_t *psymalloc
, asymbol
*));
38 static boolean default_fill_link_order
39 PARAMS ((bfd
*, struct bfd_link_info
*, asection
*,
40 struct bfd_link_order
*));
41 static boolean default_indirect_link_order
42 PARAMS ((bfd
*, struct bfd_link_info
*, asection
*,
43 struct bfd_link_order
*));
45 /* The link hash table structure is defined in bfdlink.h. It provides
46 a base hash table which the backend specific hash tables are built
49 /* Routine to create an entry in the link hash table. */
51 struct bfd_hash_entry
*
52 _bfd_link_hash_newfunc (entry
, table
, string
)
53 struct bfd_hash_entry
*entry
;
54 struct bfd_hash_table
*table
;
57 struct bfd_link_hash_entry
*ret
= (struct bfd_link_hash_entry
*) entry
;
59 /* Allocate the structure if it has not already been allocated by a
61 if (ret
== (struct bfd_link_hash_entry
*) NULL
)
62 ret
= ((struct bfd_link_hash_entry
*)
63 bfd_hash_allocate (table
, sizeof (struct bfd_link_hash_entry
)));
65 /* Call the allocation method of the superclass. */
66 ret
= ((struct bfd_link_hash_entry
*)
67 bfd_hash_newfunc ((struct bfd_hash_entry
*) ret
, table
, string
));
69 /* Initialize the local fields. */
70 ret
->type
= bfd_link_hash_new
;
74 return (struct bfd_hash_entry
*) ret
;
77 /* Initialize a link hash table. The BFD argument is the one
78 responsible for creating this table. */
81 _bfd_link_hash_table_init (table
, abfd
, newfunc
)
82 struct bfd_link_hash_table
*table
;
84 struct bfd_hash_entry
*(*newfunc
) PARAMS ((struct bfd_hash_entry
*,
85 struct bfd_hash_table
*,
88 table
->creator
= abfd
->xvec
;
90 table
->undefs_tail
= NULL
;
91 return bfd_hash_table_init (&table
->table
, newfunc
);
94 /* Look up a symbol in a link hash table. If follow is true, we
95 follow bfd_link_hash_indirect and bfd_link_hash_warning links to
98 struct bfd_link_hash_entry
*
99 bfd_link_hash_lookup (table
, string
, create
, copy
, follow
)
100 struct bfd_link_hash_table
*table
;
106 struct bfd_link_hash_entry
*ret
;
108 ret
= ((struct bfd_link_hash_entry
*)
109 bfd_hash_lookup (&table
->table
, string
, create
, copy
));
111 if (follow
&& ret
!= (struct bfd_link_hash_entry
*) NULL
)
113 while (ret
->type
== bfd_link_hash_indirect
114 || ret
->type
== bfd_link_hash_warning
)
121 /* Traverse a generic link hash table. The only reason this is not a
122 macro is to do better type checking. This code presumes that an
123 argument passed as a struct bfd_hash_entry * may be cause as a
124 struct bfd_link_hash_entry * with no explicit cast required on the
128 bfd_link_hash_traverse (table
, func
, info
)
129 struct bfd_link_hash_table
*table
;
130 boolean (*func
) PARAMS ((struct bfd_link_hash_entry
*, PTR
));
133 bfd_hash_traverse (&table
->table
,
134 ((boolean (*) PARAMS ((struct bfd_hash_entry
*, PTR
)))
139 /* Add a symbol to the linker hash table undefs list. */
142 bfd_link_add_undef (table
, h
)
143 struct bfd_link_hash_table
*table
;
144 struct bfd_link_hash_entry
*h
;
146 BFD_ASSERT (h
->next
== NULL
);
147 if (table
->undefs_tail
!= (struct bfd_link_hash_entry
*) NULL
)
148 table
->undefs_tail
->next
= h
;
149 if (table
->undefs
== (struct bfd_link_hash_entry
*) NULL
)
151 table
->undefs_tail
= h
;
154 /* Routine to create an entry in an generic link hash table. */
156 static struct bfd_hash_entry
*
157 generic_link_hash_newfunc (entry
, table
, string
)
158 struct bfd_hash_entry
*entry
;
159 struct bfd_hash_table
*table
;
162 struct generic_link_hash_entry
*ret
=
163 (struct generic_link_hash_entry
*) entry
;
165 /* Allocate the structure if it has not already been allocated by a
167 if (ret
== (struct generic_link_hash_entry
*) NULL
)
168 ret
= ((struct generic_link_hash_entry
*)
169 bfd_hash_allocate (table
, sizeof (struct generic_link_hash_entry
)));
171 /* Call the allocation method of the superclass. */
172 ret
= ((struct generic_link_hash_entry
*)
173 _bfd_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
176 /* Set local fields. */
179 return (struct bfd_hash_entry
*) ret
;
182 /* Create an generic link hash table. */
184 struct bfd_link_hash_table
*
185 _bfd_generic_link_hash_table_create (abfd
)
188 struct generic_link_hash_table
*ret
;
190 ret
= ((struct generic_link_hash_table
*)
191 bfd_xmalloc (sizeof (struct generic_link_hash_table
)));
192 if (! _bfd_link_hash_table_init (&ret
->root
, abfd
,
193 generic_link_hash_newfunc
))
196 return (struct bfd_link_hash_table
*) NULL
;
201 /* Generic function to add symbols from an object file to the global
205 _bfd_generic_link_add_symbols (abfd
, info
)
207 struct bfd_link_info
*info
;
211 switch (bfd_get_format (abfd
))
214 ret
= generic_link_add_object_symbols (abfd
, info
);
217 ret
= _bfd_generic_link_add_archive_symbols
218 (abfd
, info
, generic_link_check_archive_element
);
221 bfd_error
= wrong_format
;
225 /* If we might be using the C based alloca function, make sure we
226 have dumped the symbol tables we just allocated. */
236 /* Add symbols from an object file to the global hash table. */
239 generic_link_add_object_symbols (abfd
, info
)
241 struct bfd_link_info
*info
;
245 bfd_size_type symbol_count
;
247 symsize
= get_symtab_upper_bound (abfd
);
248 symbols
= (asymbol
**) alloca (symsize
);
249 symbol_count
= bfd_canonicalize_symtab (abfd
, symbols
);
251 return generic_link_add_symbol_list (abfd
, info
, symbol_count
, symbols
);
254 /* We build a hash table of all symbols defined in an archive. */
256 /* An archive symbol may be defined by multiple archive elements.
257 This linked list is used to hold the elements. */
261 struct archive_list
*next
;
265 /* An entry in an archive hash table. */
267 struct archive_hash_entry
269 struct bfd_hash_entry root
;
270 /* Where the symbol is defined. */
271 struct archive_list
*defs
;
274 /* An archive hash table itself. */
276 struct archive_hash_table
278 struct bfd_hash_table table
;
281 static struct bfd_hash_entry
*archive_hash_newfunc
282 PARAMS ((struct bfd_hash_entry
*, struct bfd_hash_table
*, const char *));
283 static boolean archive_hash_table_init
284 PARAMS ((struct archive_hash_table
*,
285 struct bfd_hash_entry
*(*) (struct bfd_hash_entry
*,
286 struct bfd_hash_table
*,
289 /* Create a new entry for an archive hash table. */
291 static struct bfd_hash_entry
*
292 archive_hash_newfunc (entry
, table
, string
)
293 struct bfd_hash_entry
*entry
;
294 struct bfd_hash_table
*table
;
297 struct archive_hash_entry
*ret
= (struct archive_hash_entry
*) entry
;
299 /* Allocate the structure if it has not already been allocated by a
301 if (ret
== (struct archive_hash_entry
*) NULL
)
302 ret
= ((struct archive_hash_entry
*)
303 bfd_hash_allocate (table
, sizeof (struct archive_hash_entry
)));
305 /* Call the allocation method of the superclass. */
306 ret
= ((struct archive_hash_entry
*)
307 bfd_hash_newfunc ((struct bfd_hash_entry
*) ret
, table
, string
));
309 /* Initialize the local fields. */
310 ret
->defs
= (struct archive_list
*) NULL
;
312 return (struct bfd_hash_entry
*) ret
;
315 /* Initialize an archive hash table. */
318 archive_hash_table_init (table
, newfunc
)
319 struct archive_hash_table
*table
;
320 struct bfd_hash_entry
*(*newfunc
) PARAMS ((struct bfd_hash_entry
*,
321 struct bfd_hash_table
*,
324 return bfd_hash_table_init (&table
->table
, newfunc
);
327 /* Look up an entry in an archive hash table. */
329 #define archive_hash_lookup(t, string, create, copy) \
330 ((struct archive_hash_entry *) \
331 bfd_hash_lookup (&(t)->table, (string), (create), (copy)))
333 /* Free an archive hash table. */
335 #define archive_hash_table_free(t) bfd_hash_table_free (&(t)->table)
337 /* Generic function to add symbols from an archive file to the global
338 hash file. This function presumes that the archive symbol table
339 has already been read in (this is normally done by the
340 bfd_check_format entry point). It looks through the undefined and
341 common symbols and searches the archive symbol table for them. If
342 it finds an entry, it includes the associated object file in the
345 The old linker looked through the archive symbol table for
346 undefined symbols. We do it the other way around, looking through
347 undefined symbols for symbols defined in the archive. The
348 advantage of the newer scheme is that we only have to look through
349 the list of undefined symbols once, whereas the old method had to
350 re-search the symbol table each time a new object file was added.
352 The CHECKFN argument is used to see if an object file should be
353 included. CHECKFN should set *PNEEDED to true if the object file
354 should be included, and must also call the bfd_link_info
355 add_archive_element callback function and handle adding the symbols
356 to the global hash table. CHECKFN should only return false if some
357 sort of error occurs.
359 For some formats, such as a.out, it is possible to look through an
360 object file but not actually include it in the link. The
361 archive_pass field in a BFD is used to avoid checking the symbols
362 of an object files too many times. When an object is included in
363 the link, archive_pass is set to -1. If an object is scanned but
364 not included, archive_pass is set to the pass number. The pass
365 number is incremented each time a new object file is included. The
366 pass number is used because when a new object file is included it
367 may create new undefined symbols which cause a previously examined
368 object file to be included. */
371 _bfd_generic_link_add_archive_symbols (abfd
, info
, checkfn
)
373 struct bfd_link_info
*info
;
374 boolean (*checkfn
) PARAMS ((bfd
*, struct bfd_link_info
*,
379 register carsym
*arsym
;
381 struct archive_hash_table arsym_hash
;
383 struct bfd_link_hash_entry
**pundef
;
385 if (! bfd_has_map (abfd
))
387 bfd_error
= no_symbols
;
391 arsyms
= bfd_ardata (abfd
)->symdefs
;
392 arsym_end
= arsyms
+ bfd_ardata (abfd
)->symdef_count
;
394 /* In order to quickly determine whether an symbol is defined in
395 this archive, we build a hash table of the symbols. */
396 if (! archive_hash_table_init (&arsym_hash
, archive_hash_newfunc
))
398 for (arsym
= arsyms
, indx
= 0; arsym
< arsym_end
; arsym
++, indx
++)
400 struct archive_hash_entry
*arh
;
401 struct archive_list
*l
;
403 arh
= archive_hash_lookup (&arsym_hash
, arsym
->name
, true, false);
404 if (arh
== (struct archive_hash_entry
*) NULL
)
406 l
= (struct archive_list
*) alloca (sizeof (struct archive_list
));
414 /* New undefined symbols are added to the end of the list, so we
415 only need to look through it once. */
416 pundef
= &info
->hash
->undefs
;
417 while (*pundef
!= (struct bfd_link_hash_entry
*) NULL
)
419 struct bfd_link_hash_entry
*h
;
420 struct archive_hash_entry
*arh
;
421 struct archive_list
*l
;
425 /* When a symbol is defined, it is not necessarily removed from
427 if (h
->type
!= bfd_link_hash_undefined
428 && h
->type
!= bfd_link_hash_common
)
430 /* Remove this entry from the list, for general cleanliness
431 and because we are going to look through the list again
432 if we search any more libraries. We can't remove the
433 entry if it is the tail, because that would lose any
434 entries we add to the list later on. */
435 if (*pundef
!= info
->hash
->undefs_tail
)
436 *pundef
= (*pundef
)->next
;
438 pundef
= &(*pundef
)->next
;
442 /* Look for this symbol in the archive symbol map. */
443 arh
= archive_hash_lookup (&arsym_hash
, h
->root
.string
, false, false);
444 if (arh
== (struct archive_hash_entry
*) NULL
)
446 pundef
= &(*pundef
)->next
;
450 /* Look at all the objects which define this symbol. */
451 for (l
= arh
->defs
; l
!= (struct archive_list
*) NULL
; l
= l
->next
)
456 /* If the symbol has gotten defined along the way, quit. */
457 if (h
->type
!= bfd_link_hash_undefined
458 && h
->type
!= bfd_link_hash_common
)
461 element
= bfd_get_elt_at_index (abfd
, l
->indx
);
462 if (element
== (bfd
*) NULL
)
465 /* If we've already included this element, or if we've
466 already checked it on this pass, continue. */
467 if (element
->archive_pass
== -1
468 || element
->archive_pass
== pass
)
471 /* If we can't figure this element out, just ignore it. */
472 if (! bfd_check_format (element
, bfd_object
))
474 element
->archive_pass
= -1;
478 /* CHECKFN will see if this element should be included, and
479 go ahead and include it if appropriate. */
480 if (! (*checkfn
) (element
, info
, &needed
))
484 element
->archive_pass
= pass
;
487 element
->archive_pass
= -1;
489 /* Increment the pass count to show that we may need to
490 recheck object files which were already checked. */
495 pundef
= &(*pundef
)->next
;
498 archive_hash_table_free (&arsym_hash
);
503 /* See if we should include an archive element. */
506 generic_link_check_archive_element (abfd
, info
, pneeded
)
508 struct bfd_link_info
*info
;
513 bfd_size_type symbol_count
;
514 asymbol
**pp
, **ppend
;
518 symsize
= get_symtab_upper_bound (abfd
);
519 symbols
= (asymbol
**) alloca (symsize
);
520 symbol_count
= bfd_canonicalize_symtab (abfd
, symbols
);
523 ppend
= symbols
+ symbol_count
;
524 for (; pp
< ppend
; pp
++)
527 struct bfd_link_hash_entry
*h
;
531 /* We are only interested in globally visible symbols. */
532 if (! bfd_is_com_section (p
->section
)
533 && (p
->flags
& (BSF_GLOBAL
| BSF_INDIRECT
| BSF_WEAK
)) == 0)
536 /* We are only interested if we know something about this
537 symbol, and it is undefined or common. An undefined weak
538 symbol (type bfd_link_hash_weak) is not considered to be a
539 reference when pulling files out of an archive. See the SVR4
541 h
= bfd_link_hash_lookup (info
->hash
, bfd_asymbol_name (p
), false,
543 if (h
== (struct bfd_link_hash_entry
*) NULL
544 || (h
->type
!= bfd_link_hash_undefined
545 && h
->type
!= bfd_link_hash_common
))
548 /* P is a symbol we are looking for. */
550 if (! bfd_is_com_section (p
->section
))
552 /* This object file defines this symbol, so pull it in. */
553 if (! (*info
->callbacks
->add_archive_element
) (info
, abfd
,
554 bfd_asymbol_name (p
)))
556 if (! generic_link_add_symbol_list (abfd
, info
, symbol_count
,
563 /* P is a common symbol. */
565 if (h
->type
== bfd_link_hash_undefined
)
569 symbfd
= h
->u
.undef
.abfd
;
570 if (symbfd
== (bfd
*) NULL
)
572 /* This symbol was created as undefined from outside
573 BFD. We assume that we should link in the object
574 file. This is for the -u option in the linker. */
575 if (! (*info
->callbacks
->add_archive_element
)
576 (info
, abfd
, bfd_asymbol_name (p
)))
582 /* Turn the symbol into a common symbol but do not link in
583 the object file. This is how a.out works. Object
584 formats that require different semantics must implement
585 this function differently. This symbol is already on the
586 undefs list. We add the section to a common section
587 attached to symbfd to ensure that it is in a BFD which
588 will be linked in. */
589 h
->type
= bfd_link_hash_common
;
590 h
->u
.c
.size
= bfd_asymbol_value (p
);
591 if (p
->section
== &bfd_com_section
)
592 h
->u
.c
.section
= bfd_make_section_old_way (symbfd
, "COMMON");
594 h
->u
.c
.section
= bfd_make_section_old_way (symbfd
,
599 /* Adjust the size of the common symbol if necessary. This
600 is how a.out works. Object formats that require
601 different semantics must implement this function
603 if (bfd_asymbol_value (p
) > h
->u
.c
.size
)
604 h
->u
.c
.size
= bfd_asymbol_value (p
);
608 /* This archive element is not needed. */
612 /* Add the symbol from an object file to the global hash table. */
615 generic_link_add_symbol_list (abfd
, info
, symbol_count
, symbols
)
617 struct bfd_link_info
*info
;
618 bfd_size_type symbol_count
;
621 asymbol
**pp
, **ppend
;
624 ppend
= symbols
+ symbol_count
;
625 for (; pp
< ppend
; pp
++)
631 if ((p
->flags
& (BSF_INDIRECT
636 || bfd_get_section (p
) == &bfd_und_section
637 || bfd_is_com_section (bfd_get_section (p
))
638 || bfd_get_section (p
) == &bfd_ind_section
)
642 struct generic_link_hash_entry
*h
;
644 name
= bfd_asymbol_name (p
);
645 if ((p
->flags
& BSF_INDIRECT
) != 0
646 || p
->section
== &bfd_ind_section
)
647 string
= bfd_asymbol_name ((asymbol
*) p
->value
);
648 else if ((p
->flags
& BSF_WARNING
) != 0)
650 /* The name of P is actually the warning string, and the
651 value is actually a pointer to the symbol to warn
654 name
= bfd_asymbol_name ((asymbol
*) p
->value
);
659 /* We pass the constructor argument as false, for
660 compatibility. As backends are converted they can
661 arrange to pass the right value (the right value is the
662 size of a function pointer if gcc uses collect2 for the
663 object file format, zero if it does not).
664 FIXME: We pass the bitsize as 32, which is just plain
665 wrong, but actually doesn't matter very much. */
666 if (! (_bfd_generic_link_add_one_symbol
667 (info
, abfd
, name
, p
->flags
, bfd_get_section (p
),
668 p
->value
, string
, false, 0, 32,
669 (struct bfd_link_hash_entry
**) &h
)))
672 /* Save the BFD symbol so that we don't lose any backend
673 specific information that may be attached to it. We only
674 want this one if it gives more information than the
675 existing one; we don't want to replace a defined symbol
676 with an undefined one. This routine may be called with a
677 hash table other than the generic hash table, so we only
678 do this if we are certain that the hash table is a
680 if (info
->hash
->creator
== abfd
->xvec
)
682 if (h
->sym
== (asymbol
*) NULL
683 || (bfd_get_section (p
) != &bfd_und_section
684 && (! bfd_is_com_section (bfd_get_section (p
))
685 || (bfd_get_section (h
->sym
) == &bfd_und_section
))))
694 /* We use a state table to deal with adding symbols from an object
695 file. The first index into the state table describes the symbol
696 from the object file. The second index into the state table is the
697 type of the symbol in the hash table. */
699 /* The symbol from the object file is turned into one of these row
704 UNDEF_ROW
, /* Undefined. */
705 UNDEFW_ROW
, /* Weak undefined. */
706 DEF_ROW
, /* Defined. */
707 DEFW_ROW
, /* Weak defined. */
708 COMMON_ROW
, /* Common. */
709 INDR_ROW
, /* Indirect. */
710 WARN_ROW
, /* Warning. */
711 SET_ROW
/* Member of set. */
714 /* The actions to take in the state table. */
719 UND
, /* Mark symbol undefined. */
720 WEAK
, /* Mark symbol weak undefined. */
721 DEF
, /* Mark symbol defined. */
722 COM
, /* Mark symbol common. */
723 CREF
, /* Possibly warn about common reference to defined symbol. */
724 CDEF
, /* Define existing common symbol. */
725 NOACT
, /* No action. */
726 BIG
, /* Mark symbol common using largest size. */
727 MDEF
, /* Multiple definition error. */
728 IND
, /* Make indirect symbol. */
729 SET
, /* Add value to set. */
730 MWARN
, /* Make warning symbol. */
731 WARN
, /* Issue warning. */
732 CYCLE
, /* Repeat with symbol pointed to. */
733 WARNC
/* Issue warning and then CYCLE. */
736 /* The state table itself. The first index is a link_row and the
737 second index is a bfd_link_hash_type. */
739 static const enum link_action link_action
[8][7] =
741 /* current\prev new undef weak def com indr warn */
742 /* UNDEF_ROW */ {UND
, NOACT
, NOACT
, NOACT
, NOACT
, CYCLE
, WARNC
},
743 /* UNDEFW_ROW */ {WEAK
, WEAK
, NOACT
, NOACT
, NOACT
, CYCLE
, WARNC
},
744 /* DEF_ROW */ {DEF
, DEF
, DEF
, MDEF
, CDEF
, CYCLE
, CYCLE
},
745 /* DEFW_ROW */ {DEF
, DEF
, DEF
, NOACT
, NOACT
, CYCLE
, CYCLE
},
746 /* COMMON_ROW */ {COM
, COM
, COM
, CREF
, BIG
, CYCLE
, WARNC
},
747 /* INDR_ROW */ {IND
, IND
, IND
, MDEF
, MDEF
, MDEF
, WARNC
},
748 /* WARN_ROW */ {MWARN
, WARN
, WARN
, MWARN
, MWARN
, MWARN
, NOACT
},
749 /* SET_ROW */ {SET
, SET
, SET
, SET
, SET
, CYCLE
, WARNC
}
752 /* Add a symbol to the global hash table.
753 ABFD is the BFD the symbol comes from.
754 NAME is the name of the symbol.
755 FLAGS is the BSF_* bits associated with the symbol.
756 SECTION is the section in which the symbol is defined; this may be
757 bfd_und_section or bfd_com_section.
758 VALUE is the value of the symbol, relative to the section.
759 STRING is used for either an indirect symbol, in which case it is
760 the name of the symbol to indirect to, or a warning symbol, in
761 which case it is the warning string.
762 COPY is true if NAME or STRING must be copied into locally
763 allocated memory if they need to be saved.
764 CONSTRUCTOR is true if we should automatically collect gcc
765 constructor or destructor names.
766 BITSIZE is the number of bits in constructor or set entries.
767 HASHP, if not NULL, is a place to store the created hash table
771 _bfd_generic_link_add_one_symbol (info
, abfd
, name
, flags
, section
, value
,
772 string
, copy
, constructor
, bitsize
, hashp
)
773 struct bfd_link_info
*info
;
782 unsigned int bitsize
;
783 struct bfd_link_hash_entry
**hashp
;
786 struct bfd_link_hash_entry
*h
;
789 if (section
== &bfd_ind_section
790 || (flags
& BSF_INDIRECT
) != 0)
792 else if ((flags
& BSF_WARNING
) != 0)
794 else if ((flags
& BSF_CONSTRUCTOR
) != 0)
796 else if (section
== &bfd_und_section
)
798 if ((flags
& BSF_WEAK
) != 0)
803 else if ((flags
& BSF_WEAK
) != 0)
805 else if (bfd_is_com_section (section
))
810 h
= bfd_link_hash_lookup (info
->hash
, name
, true, copy
, false);
811 if (h
== (struct bfd_link_hash_entry
*) NULL
)
813 if (hashp
!= (struct bfd_link_hash_entry
**) NULL
)
818 if (info
->notice_hash
!= (struct bfd_hash_table
*) NULL
819 && (bfd_hash_lookup (info
->notice_hash
, name
, false, false)
820 != (struct bfd_hash_entry
*) NULL
))
822 if (! (*info
->callbacks
->notice
) (info
, name
, abfd
, section
, value
))
826 if (hashp
!= (struct bfd_link_hash_entry
**) NULL
)
831 enum link_action action
;
834 action
= link_action
[(int) row
][(int) h
->type
];
840 h
->type
= bfd_link_hash_undefined
;
841 h
->u
.undef
.abfd
= abfd
;
842 bfd_link_add_undef (info
->hash
, h
);
845 h
->type
= bfd_link_hash_weak
;
846 h
->u
.undef
.abfd
= abfd
;
849 BFD_ASSERT (h
->type
== bfd_link_hash_common
);
850 if (! ((*info
->callbacks
->multiple_common
)
852 h
->u
.c
.section
->owner
, bfd_link_hash_common
, h
->u
.c
.size
,
853 abfd
, bfd_link_hash_defined
, (bfd_vma
) 0)))
857 h
->type
= bfd_link_hash_defined
;
858 h
->u
.def
.section
= section
;
859 h
->u
.def
.value
= value
;
861 /* If we have been asked to, we act like collect2 and
862 identify all functions that might be global constructors
863 and destructors and pass them up in a callback. We only
864 do this for certain object file types, since many object
865 file types can handle this automatically. */
866 if (constructor
&& name
[0] == '_')
870 /* A constructor or destructor name starts like this:
871 _+GLOBAL_[_.$][ID][_.$]
872 where the first [_.$] and the second are the same
873 character (we accept any character there, in case a
874 new object file format comes along with even worse
875 naming restrictions). */
877 #define CONS_PREFIX "GLOBAL_"
878 #define CONS_PREFIX_LEN (sizeof CONS_PREFIX - 1)
884 && strncmp (s
, CONS_PREFIX
, CONS_PREFIX_LEN
- 1) == 0)
888 c
= s
[CONS_PREFIX_LEN
+ 1];
889 if ((c
== 'I' || c
== 'D')
890 && s
[CONS_PREFIX_LEN
] == s
[CONS_PREFIX_LEN
+ 2])
892 if (! ((*info
->callbacks
->constructor
)
894 c
== 'I' ? true : false, bitsize
,
895 name
, abfd
, section
, value
)))
903 if (h
->type
== bfd_link_hash_new
)
904 bfd_link_add_undef (info
->hash
, h
);
905 h
->type
= bfd_link_hash_common
;
907 if (section
== &bfd_com_section
)
908 h
->u
.c
.section
= bfd_make_section_old_way (abfd
, "COMMON");
909 else if (section
->owner
!= abfd
)
910 h
->u
.c
.section
= bfd_make_section_old_way (abfd
, section
->name
);
912 h
->u
.c
.section
= section
;
917 BFD_ASSERT (h
->type
== bfd_link_hash_common
);
918 if (! ((*info
->callbacks
->multiple_common
)
920 h
->u
.c
.section
->owner
, bfd_link_hash_common
, h
->u
.c
.size
,
921 abfd
, bfd_link_hash_common
, value
)))
923 if (value
> h
->u
.c
.size
)
927 BFD_ASSERT (h
->type
== bfd_link_hash_defined
);
928 if (! ((*info
->callbacks
->multiple_common
)
930 h
->u
.def
.section
->owner
, bfd_link_hash_defined
, (bfd_vma
) 0,
931 abfd
, bfd_link_hash_common
, value
)))
941 case bfd_link_hash_defined
:
942 msec
= h
->u
.def
.section
;
943 mval
= h
->u
.def
.value
;
945 case bfd_link_hash_common
:
946 msec
= &bfd_com_section
;
949 case bfd_link_hash_indirect
:
950 msec
= &bfd_ind_section
;
957 if (! ((*info
->callbacks
->multiple_definition
)
958 (info
, name
, msec
->owner
, msec
, mval
, abfd
, section
,
965 struct bfd_link_hash_entry
*inh
;
967 /* STRING is the name of the symbol we want to indirect
969 inh
= bfd_link_hash_lookup (info
->hash
, string
, true, copy
,
971 if (inh
== (struct bfd_link_hash_entry
*) NULL
)
973 if (inh
->type
== bfd_link_hash_new
)
975 inh
->type
= bfd_link_hash_undefined
;
976 inh
->u
.undef
.abfd
= abfd
;
977 bfd_link_add_undef (info
->hash
, inh
);
979 h
->type
= bfd_link_hash_indirect
;
984 if (! (*info
->callbacks
->add_to_set
) (info
, h
, bitsize
, abfd
,
990 if (h
->u
.i
.warning
!= NULL
)
992 if (! (*info
->callbacks
->warning
) (info
, h
->u
.i
.warning
))
994 /* Only issue a warning once. */
995 h
->u
.i
.warning
= NULL
;
1006 struct bfd_link_hash_entry
*sub
;
1008 /* STRING is the warning to give. */
1009 sub
= ((struct bfd_link_hash_entry
*)
1010 bfd_hash_allocate (&info
->hash
->table
,
1011 sizeof (struct bfd_link_hash_entry
)));
1013 h
->type
= bfd_link_hash_warning
;
1016 h
->u
.i
.warning
= string
;
1021 w
= bfd_hash_allocate (&info
->hash
->table
,
1022 strlen (string
) + 1);
1035 /* Generic final link routine. */
1038 _bfd_generic_final_link (abfd
, info
)
1040 struct bfd_link_info
*info
;
1044 struct bfd_link_order
*p
;
1046 struct generic_write_global_symbol_info wginfo
;
1048 abfd
->outsymbols
= (asymbol
**) NULL
;
1052 /* Build the output symbol table. This also reads in the symbols
1053 for all the input BFDs, keeping them in the outsymbols field. */
1054 for (sub
= info
->input_bfds
; sub
!= (bfd
*) NULL
; sub
= sub
->link_next
)
1055 if (! _bfd_generic_link_output_symbols (abfd
, sub
, info
, &outsymalloc
))
1058 /* Accumulate the global symbols. */
1059 wginfo
.output_bfd
= abfd
;
1060 wginfo
.psymalloc
= &outsymalloc
;
1061 _bfd_generic_link_hash_traverse (_bfd_generic_hash_table (info
),
1062 _bfd_generic_link_write_global_symbol
,
1065 if (info
->relocateable
)
1067 /* Allocate space for the output relocs for each section. */
1068 for (o
= abfd
->sections
;
1069 o
!= (asection
*) NULL
;
1073 for (p
= o
->link_order_head
;
1074 p
!= (struct bfd_link_order
*) NULL
;
1077 if (p
->type
== bfd_indirect_link_order
)
1079 asection
*input_section
;
1081 bfd_size_type relsize
;
1083 bfd_size_type reloc_count
;
1085 input_section
= p
->u
.indirect
.section
;
1086 input_bfd
= input_section
->owner
;
1087 relsize
= bfd_get_reloc_upper_bound (input_bfd
,
1089 relocs
= (arelent
**) bfd_xmalloc (relsize
);
1091 bfd_canonicalize_reloc (input_bfd
, input_section
,
1093 bfd_get_outsymbols (input_bfd
));
1094 BFD_ASSERT (reloc_count
== input_section
->reloc_count
);
1095 o
->reloc_count
+= reloc_count
;
1099 if (o
->reloc_count
> 0)
1101 o
->orelocation
= ((arelent
**)
1104 * sizeof (arelent
*))));
1105 /* Reset the count so that it can be used as an index
1106 when putting in the output relocs. */
1112 /* Handle all the link order information for the sections. */
1113 for (o
= abfd
->sections
;
1114 o
!= (asection
*) NULL
;
1117 for (p
= o
->link_order_head
;
1118 p
!= (struct bfd_link_order
*) NULL
;
1121 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
1129 /* Add an output symbol to the output BFD. */
1132 generic_add_output_symbol (output_bfd
, psymalloc
, sym
)
1137 if (output_bfd
->symcount
>= *psymalloc
)
1141 if (*psymalloc
== 0)
1145 if (output_bfd
->outsymbols
== (asymbol
**) NULL
)
1146 newsyms
= (asymbol
**) malloc (*psymalloc
* sizeof (asymbol
*));
1148 newsyms
= (asymbol
**) realloc (output_bfd
->outsymbols
,
1149 *psymalloc
* sizeof (asymbol
*));
1150 if (newsyms
== (asymbol
**) NULL
)
1152 bfd_error
= no_memory
;
1155 output_bfd
->outsymbols
= newsyms
;
1158 output_bfd
->outsymbols
[output_bfd
->symcount
] = sym
;
1159 ++output_bfd
->symcount
;
1164 /* Handle the symbols for an input BFD. */
1167 _bfd_generic_link_output_symbols (output_bfd
, input_bfd
, info
, psymalloc
)
1170 struct bfd_link_info
*info
;
1177 symsize
= get_symtab_upper_bound (input_bfd
);
1178 input_bfd
->outsymbols
= (asymbol
**) bfd_alloc (input_bfd
, symsize
);
1179 input_bfd
->symcount
= bfd_canonicalize_symtab (input_bfd
,
1180 input_bfd
->outsymbols
);
1182 /* Create a filename symbol if we are supposed to. */
1183 if (info
->create_object_symbols_section
!= (asection
*) NULL
)
1187 for (sec
= input_bfd
->sections
;
1188 sec
!= (asection
*) NULL
;
1191 if (sec
->output_section
== info
->create_object_symbols_section
)
1195 newsym
= bfd_make_empty_symbol (input_bfd
);
1196 newsym
->name
= input_bfd
->filename
;
1198 newsym
->flags
= BSF_LOCAL
| BSF_FILE
;
1199 newsym
->section
= sec
;
1201 if (! generic_add_output_symbol (output_bfd
, psymalloc
,
1210 /* Adjust the values of the globally visible symbols, and write out
1212 sym_ptr
= bfd_get_outsymbols (input_bfd
);
1213 sym_end
= sym_ptr
+ bfd_get_symcount (input_bfd
);
1214 for (; sym_ptr
< sym_end
; sym_ptr
++)
1217 struct generic_link_hash_entry
*h
;
1220 h
= (struct generic_link_hash_entry
*) NULL
;
1222 if ((sym
->flags
& (BSF_INDIRECT
1227 || bfd_get_section (sym
) == &bfd_und_section
1228 || bfd_is_com_section (bfd_get_section (sym
))
1229 || bfd_get_section (sym
) == &bfd_ind_section
)
1231 h
= _bfd_generic_link_hash_lookup (_bfd_generic_hash_table (info
),
1232 bfd_asymbol_name (sym
),
1233 false, false, true);
1234 if (h
!= (struct generic_link_hash_entry
*) NULL
)
1236 /* Force all references to this symbol to point to
1237 the same area in memory. It is possible that
1238 this routine will be called with a hash table
1239 other than a generic hash table, so we double
1241 if (info
->hash
->creator
== input_bfd
->xvec
)
1243 if (h
->sym
!= (asymbol
*) NULL
)
1244 *sym_ptr
= sym
= h
->sym
;
1247 switch (h
->root
.type
)
1250 case bfd_link_hash_new
:
1252 case bfd_link_hash_undefined
:
1253 case bfd_link_hash_weak
:
1255 case bfd_link_hash_defined
:
1256 sym
->value
= h
->root
.u
.def
.value
;
1257 sym
->section
= h
->root
.u
.def
.section
;
1258 sym
->flags
|= BSF_GLOBAL
;
1260 case bfd_link_hash_common
:
1261 sym
->value
= h
->root
.u
.c
.size
;
1262 sym
->flags
|= BSF_GLOBAL
;
1263 /* We do not set the section of the symbol to
1264 c.section. c.section is saved so that we know
1265 where to allocate the symbol if we define it. In
1266 this case the type is still bfd_link_hash_common,
1267 so we did not define it, so we do not want to use
1269 BFD_ASSERT (bfd_is_com_section (sym
->section
));
1275 /* This switch is straight from the old code in
1276 write_file_locals in ldsym.c. */
1277 if (info
->strip
== strip_some
1278 && (bfd_hash_lookup (info
->keep_hash
, bfd_asymbol_name (sym
),
1280 == (struct bfd_hash_entry
*) NULL
))
1282 else if ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0)
1284 /* If this symbol is marked as occurring now, rather
1285 than at the end, output it now. This is used for
1286 COFF C_EXT FCN symbols. FIXME: There must be a
1288 if (bfd_asymbol_bfd (sym
) == input_bfd
1289 && (sym
->flags
& BSF_NOT_AT_END
) != 0)
1294 else if (sym
->section
== &bfd_ind_section
)
1296 else if ((sym
->flags
& BSF_DEBUGGING
) != 0)
1298 if (info
->strip
== strip_none
)
1303 else if (sym
->section
== &bfd_und_section
1304 || bfd_is_com_section (sym
->section
))
1306 else if ((sym
->flags
& BSF_LOCAL
) != 0)
1308 if ((sym
->flags
& BSF_WARNING
) != 0)
1312 switch (info
->discard
)
1319 if (bfd_asymbol_name (sym
)[0] == info
->lprefix
[0]
1320 && (info
->lprefix_len
== 1
1321 || strncmp (bfd_asymbol_name (sym
), info
->lprefix
,
1322 info
->lprefix_len
) == 0))
1333 else if ((sym
->flags
& BSF_CONSTRUCTOR
))
1335 if (info
->strip
!= strip_all
)
1345 if (! generic_add_output_symbol (output_bfd
, psymalloc
, sym
))
1347 if (h
!= (struct generic_link_hash_entry
*) NULL
)
1348 h
->root
.written
= true;
1355 /* Write out a global symbol, if it hasn't already been written out.
1356 This is called for each symbol in the hash table. */
1359 _bfd_generic_link_write_global_symbol (h
, data
)
1360 struct generic_link_hash_entry
*h
;
1363 struct generic_write_global_symbol_info
*wginfo
=
1364 (struct generic_write_global_symbol_info
*) data
;
1367 if (h
->root
.written
)
1370 if (h
->sym
!= (asymbol
*) NULL
)
1373 BFD_ASSERT (strcmp (bfd_asymbol_name (sym
), h
->root
.root
.string
) == 0);
1377 sym
= bfd_make_empty_symbol (wginfo
->output_bfd
);
1378 sym
->name
= h
->root
.root
.string
;
1382 switch (h
->root
.type
)
1385 case bfd_link_hash_new
:
1387 case bfd_link_hash_undefined
:
1388 sym
->section
= &bfd_und_section
;
1391 case bfd_link_hash_weak
:
1392 sym
->section
= &bfd_und_section
;
1394 sym
->flags
|= BSF_WEAK
;
1395 case bfd_link_hash_defined
:
1396 sym
->section
= h
->root
.u
.def
.section
;
1397 sym
->value
= h
->root
.u
.def
.value
;
1399 case bfd_link_hash_common
:
1400 sym
->value
= h
->root
.u
.c
.size
;
1401 /* Do not set the section; see _bfd_generic_link_output_symbols. */
1402 BFD_ASSERT (bfd_is_com_section (sym
->section
));
1404 case bfd_link_hash_indirect
:
1405 case bfd_link_hash_warning
:
1406 /* FIXME: What should we do here? */
1410 sym
->flags
|= BSF_GLOBAL
;
1412 if (! generic_add_output_symbol (wginfo
->output_bfd
, wginfo
->psymalloc
,
1415 /* FIXME: No way to return failure. */
1419 h
->root
.written
= true;
1424 /* Allocate a new link_order for a section. */
1426 struct bfd_link_order
*
1427 bfd_new_link_order (abfd
, section
)
1431 struct bfd_link_order
*new;
1433 new = ((struct bfd_link_order
*)
1434 bfd_alloc_by_size_t (abfd
, sizeof (struct bfd_link_order
)));
1436 new->type
= bfd_undefined_link_order
;
1439 new->next
= (struct bfd_link_order
*) NULL
;
1441 if (section
->link_order_tail
!= (struct bfd_link_order
*) NULL
)
1442 section
->link_order_tail
->next
= new;
1444 section
->link_order_head
= new;
1445 section
->link_order_tail
= new;
1450 /* Default link order processing routine. */
1453 _bfd_default_link_order (abfd
, info
, sec
, link_order
)
1455 struct bfd_link_info
*info
;
1457 struct bfd_link_order
*link_order
;
1459 switch (link_order
->type
)
1461 case bfd_undefined_link_order
:
1464 case bfd_indirect_link_order
:
1465 return default_indirect_link_order (abfd
, info
, sec
, link_order
);
1466 case bfd_fill_link_order
:
1467 return default_fill_link_order (abfd
, info
, sec
, link_order
);
1471 /* Default routine to handle a bfd_fill_link_order. */
1475 default_fill_link_order (abfd
, info
, sec
, link_order
)
1477 struct bfd_link_info
*info
;
1479 struct bfd_link_order
*link_order
;
1486 BFD_ASSERT ((sec
->flags
& SEC_HAS_CONTENTS
) != 0);
1488 size
= (size_t) link_order
->size
;
1489 space
= (char *) alloca (size
);
1490 fill
= link_order
->u
.fill
.value
;
1491 for (i
= 0; i
< size
; i
+= 2)
1492 space
[i
] = fill
>> 8;
1493 for (i
= 1; i
< size
; i
+= 2)
1495 return bfd_set_section_contents (abfd
, sec
, space
,
1496 (file_ptr
) link_order
->offset
,
1500 /* Default routine to handle a bfd_indirect_link_order. */
1503 default_indirect_link_order (output_bfd
, info
, output_section
, link_order
)
1505 struct bfd_link_info
*info
;
1506 asection
*output_section
;
1507 struct bfd_link_order
*link_order
;
1509 asection
*input_section
;
1513 BFD_ASSERT ((output_section
->flags
& SEC_HAS_CONTENTS
) != 0);
1515 if (link_order
->size
== 0)
1518 input_section
= link_order
->u
.indirect
.section
;
1519 input_bfd
= input_section
->owner
;
1521 BFD_ASSERT (input_section
->output_section
== output_section
);
1522 BFD_ASSERT (input_section
->output_offset
== link_order
->offset
);
1523 BFD_ASSERT (bfd_section_size (input_bfd
, input_section
) == link_order
->size
);
1525 if (info
->relocateable
1526 && input_section
->reloc_count
> 0
1527 && output_section
->orelocation
== (arelent
**) NULL
)
1529 /* Space has not been allocated for the output relocations.
1530 This can happen when we are called by a specific backend
1531 because somebody is attempting to link together different
1532 types of object files. Handling this case correctly is
1533 difficult, and sometimes impossible. */
1537 /* Get the canonical symbols. The generic linker will always have
1538 retrieved them by this point, but we may be being called by a
1539 specific linker when linking different types of object files
1541 if (bfd_get_outsymbols (input_bfd
) == (asymbol
**) NULL
)
1545 symsize
= get_symtab_upper_bound (input_bfd
);
1546 input_bfd
->outsymbols
= (asymbol
**) bfd_alloc (input_bfd
, symsize
);
1547 input_bfd
->symcount
= bfd_canonicalize_symtab (input_bfd
,
1548 input_bfd
->outsymbols
);
1551 /* Get and relocate the section contents. */
1552 contents
= (bfd_byte
*) alloca (bfd_section_size (input_bfd
, input_section
));
1553 contents
= (bfd_get_relocated_section_contents
1554 (output_bfd
, info
, link_order
, contents
, info
->relocateable
,
1555 bfd_get_outsymbols (input_bfd
)));
1557 /* Output the section contents. */
1558 if (! bfd_set_section_contents (output_bfd
, output_section
, (PTR
) contents
,
1559 link_order
->offset
, link_order
->size
))