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
*));
42 /* The link hash table structure is defined in bfdlink.h. It provides
43 a base hash table which the backend specific hash tables are built
46 /* Routine to create an entry in the link hash table. */
48 struct bfd_hash_entry
*
49 _bfd_link_hash_newfunc (entry
, table
, string
)
50 struct bfd_hash_entry
*entry
;
51 struct bfd_hash_table
*table
;
54 struct bfd_link_hash_entry
*ret
= (struct bfd_link_hash_entry
*) entry
;
56 /* Allocate the structure if it has not already been allocated by a
58 if (ret
== (struct bfd_link_hash_entry
*) NULL
)
59 ret
= ((struct bfd_link_hash_entry
*)
60 bfd_hash_allocate (table
, sizeof (struct bfd_link_hash_entry
)));
62 /* Call the allocation method of the superclass. */
63 ret
= ((struct bfd_link_hash_entry
*)
64 bfd_hash_newfunc ((struct bfd_hash_entry
*) ret
, table
, string
));
66 /* Initialize the local fields. */
67 ret
->type
= bfd_link_hash_new
;
71 return (struct bfd_hash_entry
*) ret
;
74 /* Initialize a link hash table. The BFD argument is the one
75 responsible for creating this table. */
78 _bfd_link_hash_table_init (table
, abfd
, newfunc
)
79 struct bfd_link_hash_table
*table
;
81 struct bfd_hash_entry
*(*newfunc
) PARAMS ((struct bfd_hash_entry
*,
82 struct bfd_hash_table
*,
85 table
->creator
= abfd
->xvec
;
87 table
->undefs_tail
= NULL
;
88 return bfd_hash_table_init (&table
->table
, newfunc
);
91 /* Look up a symbol in a link hash table. If follow is true, we
92 follow bfd_link_hash_indirect and bfd_link_hash_warning links to
95 struct bfd_link_hash_entry
*
96 bfd_link_hash_lookup (table
, string
, create
, copy
, follow
)
97 struct bfd_link_hash_table
*table
;
103 struct bfd_link_hash_entry
*ret
;
105 ret
= ((struct bfd_link_hash_entry
*)
106 bfd_hash_lookup (&table
->table
, string
, create
, copy
));
108 if (follow
&& ret
!= (struct bfd_link_hash_entry
*) NULL
)
110 while (ret
->type
== bfd_link_hash_indirect
111 || ret
->type
== bfd_link_hash_warning
)
118 /* Traverse a generic link hash table. The only reason this is not a
119 macro is to do better type checking. This code presumes that an
120 argument passed as a struct bfd_hash_entry * may be cause as a
121 struct bfd_link_hash_entry * with no explicit cast required on the
125 bfd_link_hash_traverse (table
, func
, info
)
126 struct bfd_link_hash_table
*table
;
127 boolean (*func
) PARAMS ((struct bfd_link_hash_entry
*, PTR
));
130 bfd_hash_traverse (&table
->table
,
131 ((boolean (*) PARAMS ((struct bfd_hash_entry
*, PTR
)))
136 /* Add a symbol to the linker hash table undefs list. */
139 bfd_link_add_undef (table
, h
)
140 struct bfd_link_hash_table
*table
;
141 struct bfd_link_hash_entry
*h
;
143 BFD_ASSERT (h
->next
== NULL
);
144 if (table
->undefs_tail
!= (struct bfd_link_hash_entry
*) NULL
)
145 table
->undefs_tail
->next
= h
;
146 if (table
->undefs
== (struct bfd_link_hash_entry
*) NULL
)
148 table
->undefs_tail
= h
;
151 /* Routine to create an entry in an generic link hash table. */
153 static struct bfd_hash_entry
*
154 generic_link_hash_newfunc (entry
, table
, string
)
155 struct bfd_hash_entry
*entry
;
156 struct bfd_hash_table
*table
;
159 struct generic_link_hash_entry
*ret
=
160 (struct generic_link_hash_entry
*) entry
;
162 /* Allocate the structure if it has not already been allocated by a
164 if (ret
== (struct generic_link_hash_entry
*) NULL
)
165 ret
= ((struct generic_link_hash_entry
*)
166 bfd_hash_allocate (table
, sizeof (struct generic_link_hash_entry
)));
168 /* Call the allocation method of the superclass. */
169 ret
= ((struct generic_link_hash_entry
*)
170 _bfd_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
173 /* Set local fields. */
176 return (struct bfd_hash_entry
*) ret
;
179 /* Create an generic link hash table. */
181 struct bfd_link_hash_table
*
182 _bfd_generic_link_hash_table_create (abfd
)
185 struct generic_link_hash_table
*ret
;
187 ret
= ((struct generic_link_hash_table
*)
188 bfd_xmalloc (sizeof (struct generic_link_hash_table
)));
189 if (! _bfd_link_hash_table_init (&ret
->root
, abfd
,
190 generic_link_hash_newfunc
))
193 return (struct bfd_link_hash_table
*) NULL
;
198 /* Generic function to add symbols from an object file to the global
202 _bfd_generic_link_add_symbols (abfd
, info
)
204 struct bfd_link_info
*info
;
208 switch (bfd_get_format (abfd
))
211 ret
= generic_link_add_object_symbols (abfd
, info
);
214 ret
= _bfd_generic_link_add_archive_symbols
215 (abfd
, info
, generic_link_check_archive_element
);
218 bfd_error
= wrong_format
;
222 /* If we might be using the C based alloca function, make sure we
223 have dumped the symbol tables we just allocated. */
233 /* Add symbols from an object file to the global hash table. */
236 generic_link_add_object_symbols (abfd
, info
)
238 struct bfd_link_info
*info
;
242 bfd_size_type symbol_count
;
244 symsize
= get_symtab_upper_bound (abfd
);
245 symbols
= (asymbol
**) alloca (symsize
);
246 symbol_count
= bfd_canonicalize_symtab (abfd
, symbols
);
248 return generic_link_add_symbol_list (abfd
, info
, symbol_count
, symbols
);
251 /* We build a hash table of all symbols defined in an archive. */
253 /* An archive symbol may be defined by multiple archive elements.
254 This linked list is used to hold the elements. */
258 struct archive_list
*next
;
262 /* An entry in an archive hash table. */
264 struct archive_hash_entry
266 struct bfd_hash_entry root
;
267 /* Where the symbol is defined. */
268 struct archive_list
*defs
;
271 /* An archive hash table itself. */
273 struct archive_hash_table
275 struct bfd_hash_table table
;
278 static struct bfd_hash_entry
*archive_hash_newfunc
279 PARAMS ((struct bfd_hash_entry
*, struct bfd_hash_table
*, const char *));
280 static boolean archive_hash_table_init
281 PARAMS ((struct archive_hash_table
*,
282 struct bfd_hash_entry
*(*) (struct bfd_hash_entry
*,
283 struct bfd_hash_table
*,
286 /* Create a new entry for an archive hash table. */
288 static struct bfd_hash_entry
*
289 archive_hash_newfunc (entry
, table
, string
)
290 struct bfd_hash_entry
*entry
;
291 struct bfd_hash_table
*table
;
294 struct archive_hash_entry
*ret
= (struct archive_hash_entry
*) entry
;
296 /* Allocate the structure if it has not already been allocated by a
298 if (ret
== (struct archive_hash_entry
*) NULL
)
299 ret
= ((struct archive_hash_entry
*)
300 bfd_hash_allocate (table
, sizeof (struct archive_hash_entry
)));
302 /* Call the allocation method of the superclass. */
303 ret
= ((struct archive_hash_entry
*)
304 bfd_hash_newfunc ((struct bfd_hash_entry
*) ret
, table
, string
));
306 /* Initialize the local fields. */
307 ret
->defs
= (struct archive_list
*) NULL
;
309 return (struct bfd_hash_entry
*) ret
;
312 /* Initialize an archive hash table. */
315 archive_hash_table_init (table
, newfunc
)
316 struct archive_hash_table
*table
;
317 struct bfd_hash_entry
*(*newfunc
) PARAMS ((struct bfd_hash_entry
*,
318 struct bfd_hash_table
*,
321 return bfd_hash_table_init (&table
->table
, newfunc
);
324 /* Look up an entry in an archive hash table. */
326 #define archive_hash_lookup(t, string, create, copy) \
327 ((struct archive_hash_entry *) \
328 bfd_hash_lookup (&(t)->table, (string), (create), (copy)))
330 /* Free an archive hash table. */
332 #define archive_hash_table_free(t) bfd_hash_table_free (&(t)->table)
334 /* Generic function to add symbols from an archive file to the global
335 hash file. This function presumes that the archive symbol table
336 has already been read in (this is normally done by the
337 bfd_check_format entry point). It looks through the undefined and
338 common symbols and searches the archive symbol table for them. If
339 it finds an entry, it includes the associated object file in the
342 The old linker looked through the archive symbol table for
343 undefined symbols. We do it the other way around, looking through
344 undefined symbols for symbols defined in the archive. The
345 advantage of the newer scheme is that we only have to look through
346 the list of undefined symbols once, whereas the old method had to
347 re-search the symbol table each time a new object file was added.
349 The CHECKFN argument is used to see if an object file should be
350 included. CHECKFN should set *PNEEDED to true if the object file
351 should be included, and must also call the bfd_link_info
352 add_archive_element callback function and handle adding the symbols
353 to the global hash table. CHECKFN should only return false if some
354 sort of error occurs.
356 For some formats, such as a.out, it is possible to look through an
357 object file but not actually include it in the link. The
358 archive_pass field in a BFD is used to avoid checking the symbols
359 of an object files too many times. When an object is included in
360 the link, archive_pass is set to -1. If an object is scanned but
361 not included, archive_pass is set to the pass number. The pass
362 number is incremented each time a new object file is included. The
363 pass number is used because when a new object file is included it
364 may create new undefined symbols which cause a previously examined
365 object file to be included. */
368 _bfd_generic_link_add_archive_symbols (abfd
, info
, checkfn
)
370 struct bfd_link_info
*info
;
371 boolean (*checkfn
) PARAMS ((bfd
*, struct bfd_link_info
*,
376 register carsym
*arsym
;
378 struct archive_hash_table arsym_hash
;
380 struct bfd_link_hash_entry
**pundef
;
382 if (! bfd_has_map (abfd
))
384 bfd_error
= no_symbols
;
388 arsyms
= bfd_ardata (abfd
)->symdefs
;
389 arsym_end
= arsyms
+ bfd_ardata (abfd
)->symdef_count
;
391 /* In order to quickly determine whether an symbol is defined in
392 this archive, we build a hash table of the symbols. */
393 if (! archive_hash_table_init (&arsym_hash
, archive_hash_newfunc
))
395 for (arsym
= arsyms
, indx
= 0; arsym
< arsym_end
; arsym
++, indx
++)
397 struct archive_hash_entry
*arh
;
398 struct archive_list
*l
;
400 arh
= archive_hash_lookup (&arsym_hash
, arsym
->name
, true, false);
401 if (arh
== (struct archive_hash_entry
*) NULL
)
403 l
= (struct archive_list
*) alloca (sizeof (struct archive_list
));
411 /* New undefined symbols are added to the end of the list, so we
412 only need to look through it once. */
413 pundef
= &info
->hash
->undefs
;
414 while (*pundef
!= (struct bfd_link_hash_entry
*) NULL
)
416 struct bfd_link_hash_entry
*h
;
417 struct archive_hash_entry
*arh
;
418 struct archive_list
*l
;
422 /* When a symbol is defined, it is not necessarily removed from
424 if (h
->type
!= bfd_link_hash_undefined
425 && h
->type
!= bfd_link_hash_common
)
427 /* Remove this entry from the list, for general cleanliness
428 and because we are going to look through the list again
429 if we search any more libraries. We can't remove the
430 entry if it is the tail, because that would lose any
431 entries we add to the list later on. */
432 if (*pundef
!= info
->hash
->undefs_tail
)
433 *pundef
= (*pundef
)->next
;
435 pundef
= &(*pundef
)->next
;
439 /* Look for this symbol in the archive symbol map. */
440 arh
= archive_hash_lookup (&arsym_hash
, h
->root
.string
, false, false);
441 if (arh
== (struct archive_hash_entry
*) NULL
)
443 pundef
= &(*pundef
)->next
;
447 /* Look at all the objects which define this symbol. */
448 for (l
= arh
->defs
; l
!= (struct archive_list
*) NULL
; l
= l
->next
)
453 /* If the symbol has gotten defined along the way, quit. */
454 if (h
->type
!= bfd_link_hash_undefined
455 && h
->type
!= bfd_link_hash_common
)
458 element
= bfd_get_elt_at_index (abfd
, l
->indx
);
459 if (element
== (bfd
*) NULL
)
462 /* If we've already included this element, or if we've
463 already checked it on this pass, continue. */
464 if (element
->archive_pass
== -1
465 || element
->archive_pass
== pass
)
468 /* If we can't figure this element out, just ignore it. */
469 if (! bfd_check_format (element
, bfd_object
))
471 element
->archive_pass
= -1;
475 /* CHECKFN will see if this element should be included, and
476 go ahead and include it if appropriate. */
477 if (! (*checkfn
) (element
, info
, &needed
))
481 element
->archive_pass
= pass
;
484 element
->archive_pass
= -1;
486 /* Increment the pass count to show that we may need to
487 recheck object files which were already checked. */
492 pundef
= &(*pundef
)->next
;
495 archive_hash_table_free (&arsym_hash
);
500 /* See if we should include an archive element. */
503 generic_link_check_archive_element (abfd
, info
, pneeded
)
505 struct bfd_link_info
*info
;
510 bfd_size_type symbol_count
;
511 asymbol
**pp
, **ppend
;
515 symsize
= get_symtab_upper_bound (abfd
);
516 symbols
= (asymbol
**) alloca (symsize
);
517 symbol_count
= bfd_canonicalize_symtab (abfd
, symbols
);
520 ppend
= symbols
+ symbol_count
;
521 for (; pp
< ppend
; pp
++)
524 struct bfd_link_hash_entry
*h
;
528 /* We are only interested in globally visible symbols. */
529 if (! bfd_is_com_section (p
->section
)
530 && (p
->flags
& (BSF_GLOBAL
| BSF_INDIRECT
| BSF_WEAK
)) == 0)
533 /* We are only interested if we know something about this
534 symbol, and it is undefined or common. An undefined weak
535 symbol (type bfd_link_hash_weak) is not considered to be a
536 reference when pulling files out of an archive. See the SVR4
538 h
= bfd_link_hash_lookup (info
->hash
, bfd_asymbol_name (p
), false,
540 if (h
== (struct bfd_link_hash_entry
*) NULL
541 || (h
->type
!= bfd_link_hash_undefined
542 && h
->type
!= bfd_link_hash_common
))
545 /* P is a symbol we are looking for. */
547 if (! bfd_is_com_section (p
->section
))
549 /* This object file defines this symbol, so pull it in. */
550 if (! (*info
->callbacks
->add_archive_element
) (info
, abfd
,
551 bfd_asymbol_name (p
)))
553 if (! generic_link_add_symbol_list (abfd
, info
, symbol_count
,
560 /* P is a common symbol. */
562 if (h
->type
== bfd_link_hash_undefined
)
566 symbfd
= h
->u
.undef
.abfd
;
567 if (symbfd
== (bfd
*) NULL
)
569 /* This symbol was created as undefined from outside
570 BFD. We assume that we should link in the object
571 file. This is for the -u option in the linker. */
572 if (! (*info
->callbacks
->add_archive_element
)
573 (info
, abfd
, bfd_asymbol_name (p
)))
579 /* Turn the symbol into a common symbol but do not link in
580 the object file. This is how a.out works. Object
581 formats that require different semantics must implement
582 this function differently. This symbol is already on the
584 h
->type
= bfd_link_hash_common
;
585 h
->u
.c
.size
= bfd_asymbol_value (p
);
586 h
->u
.c
.section
= bfd_make_section_old_way (symbfd
,
591 /* Adjust the size of the common symbol if necessary. This
592 is how a.out works. Object formats that require
593 different semantics must implement this function
595 if (bfd_asymbol_value (p
) > h
->u
.c
.size
)
596 h
->u
.c
.size
= bfd_asymbol_value (p
);
600 /* This archive element is not needed. */
604 /* Add the symbol from an object file to the global hash table. */
607 generic_link_add_symbol_list (abfd
, info
, symbol_count
, symbols
)
609 struct bfd_link_info
*info
;
610 bfd_size_type symbol_count
;
613 asymbol
**pp
, **ppend
;
616 ppend
= symbols
+ symbol_count
;
617 for (; pp
< ppend
; pp
++)
623 if ((p
->flags
& (BSF_INDIRECT
628 || bfd_get_section (p
) == &bfd_und_section
629 || bfd_is_com_section (bfd_get_section (p
))
630 || bfd_get_section (p
) == &bfd_ind_section
)
634 struct generic_link_hash_entry
*h
;
636 name
= bfd_asymbol_name (p
);
637 if ((p
->flags
& BSF_INDIRECT
) != 0
638 || p
->section
== &bfd_ind_section
)
639 string
= bfd_asymbol_name ((asymbol
*) p
->value
);
640 else if ((p
->flags
& BSF_WARNING
) != 0)
642 /* The name of P is actually the warning string, and the
643 value is actually a pointer to the symbol to warn
646 name
= bfd_asymbol_name ((asymbol
*) p
->value
);
650 if (! (_bfd_generic_link_add_one_symbol
651 (info
, abfd
, name
, p
->flags
, bfd_get_section (p
),
652 p
->value
, string
, false,
653 (struct bfd_link_hash_entry
**) &h
)))
656 /* Save the BFD symbol so that we don't lose any backend
657 specific information that may be attached to it. We only
658 want this one if it gives more information than the
659 existing one; we don't want to replace a defined symbol
660 with an undefined one. This routine may be called with a
661 hash table other than the generic hash table, so we only
662 do this if we are certain that the hash table is a
664 if (info
->hash
->creator
== abfd
->xvec
)
666 if (h
->sym
== (asymbol
*) NULL
667 || (bfd_get_section (p
) != &bfd_und_section
668 && (! bfd_is_com_section (bfd_get_section (p
))
669 || (bfd_get_section (h
->sym
) == &bfd_und_section
))))
678 /* We use a state table to deal with adding symbols from an object
679 file. The first index into the state table describes the symbol
680 from the object file. The second index into the state table is the
681 type of the symbol in the hash table. */
683 /* The symbol from the object file is turned into one of these row
688 UNDEF_ROW
, /* Undefined. */
689 UNDEFW_ROW
, /* Weak undefined. */
690 DEF_ROW
, /* Defined. */
691 DEFW_ROW
, /* Weak defined. */
692 COMMON_ROW
, /* Common. */
693 INDR_ROW
, /* Indirect. */
694 WARN_ROW
, /* Warning. */
695 SET_ROW
/* Member of set. */
698 /* The actions to take in the state table. */
703 UND
, /* Mark symbol undefined. */
704 WEAK
, /* Mark symbol weak undefined. */
705 DEF
, /* Mark symbol defined. */
706 COM
, /* Mark symbol common. */
707 CREF
, /* Possibly warn about common reference to defined symbol. */
708 CDEF
, /* Define existing common symbol. */
709 NOACT
, /* No action. */
710 BIG
, /* Mark symbol common using largest size. */
711 MDEF
, /* Multiple definition error. */
712 IND
, /* Make indirect symbol. */
713 SET
, /* Add value to set. */
714 MWARN
, /* Make warning symbol. */
715 WARN
, /* Issue warning. */
716 CYCLE
, /* Repeat with symbol pointed to. */
717 WARNC
/* Issue warning and then CYCLE. */
720 /* The state table itself. The first index is a link_row and the
721 second index is a bfd_link_hash_type. */
723 static const enum link_action link_action
[8][7] =
725 /* current\prev new undef weak def com indr warn */
726 /* UNDEF_ROW */ {UND
, NOACT
, NOACT
, NOACT
, NOACT
, CYCLE
, WARNC
},
727 /* UNDEFW_ROW */ {WEAK
, WEAK
, NOACT
, NOACT
, NOACT
, CYCLE
, WARNC
},
728 /* DEF_ROW */ {DEF
, DEF
, DEF
, MDEF
, CDEF
, CYCLE
, CYCLE
},
729 /* DEFW_ROW */ {DEF
, DEF
, DEF
, NOACT
, NOACT
, CYCLE
, CYCLE
},
730 /* COMMON_ROW */ {COM
, COM
, COM
, CREF
, BIG
, CYCLE
, WARNC
},
731 /* INDR_ROW */ {IND
, IND
, IND
, MDEF
, MDEF
, MDEF
, WARNC
},
732 /* WARN_ROW */ {MWARN
, WARN
, WARN
, MWARN
, MWARN
, MWARN
, NOACT
},
733 /* SET_ROW */ {SET
, SET
, SET
, SET
, SET
, CYCLE
, WARNC
}
736 /* Add a symbol to the global hash table.
737 ABFD is the BFD the symbol comes from.
738 NAME is the name of the symbol.
739 FLAGS is the BSF_* bits associated with the symbol.
740 SECTION is the section in which the symbol is defined; this may be
741 bfd_und_section or bfd_com_section.
742 VALUE is the value of the symbol, relative to the section.
743 STRING is used for either an indirect symbol, in which case it is
744 the name of the symbol to indirect to, or a warning symbol, in
745 which case it is the warning string.
746 COPY is true if NAME or STRING must be copied into locally
747 allocated memory if they need to be saved.
748 HASHP, if not NULL, is a place to store the created hash table
752 _bfd_generic_link_add_one_symbol (info
, abfd
, name
, flags
, section
, value
,
754 struct bfd_link_info
*info
;
762 struct bfd_link_hash_entry
**hashp
;
765 struct bfd_link_hash_entry
*h
;
768 if (section
== &bfd_ind_section
769 || (flags
& BSF_INDIRECT
) != 0)
771 else if ((flags
& BSF_WARNING
) != 0)
773 else if ((flags
& BSF_CONSTRUCTOR
) != 0)
775 else if (section
== &bfd_und_section
)
777 if ((flags
& BSF_WEAK
) != 0)
782 else if ((flags
& BSF_WEAK
) != 0)
784 else if (bfd_is_com_section (section
))
789 h
= bfd_link_hash_lookup (info
->hash
, name
, true, copy
, false);
790 if (h
== (struct bfd_link_hash_entry
*) NULL
)
792 if (hashp
!= (struct bfd_link_hash_entry
**) NULL
)
797 if (info
->notice_hash
!= (struct bfd_hash_table
*) NULL
798 && (bfd_hash_lookup (info
->notice_hash
, name
, false, false)
799 != (struct bfd_hash_entry
*) NULL
))
801 if (! (*info
->callbacks
->notice
) (info
, name
, abfd
, section
, value
))
805 if (hashp
!= (struct bfd_link_hash_entry
**) NULL
)
810 enum link_action action
;
813 action
= link_action
[(int) row
][(int) h
->type
];
819 h
->type
= bfd_link_hash_undefined
;
820 h
->u
.undef
.abfd
= abfd
;
821 bfd_link_add_undef (info
->hash
, h
);
824 h
->type
= bfd_link_hash_weak
;
825 h
->u
.undef
.abfd
= abfd
;
828 BFD_ASSERT (h
->type
== bfd_link_hash_common
);
829 if (! ((*info
->callbacks
->multiple_common
)
831 h
->u
.c
.section
->owner
, bfd_link_hash_common
, h
->u
.c
.size
,
832 abfd
, bfd_link_hash_defined
, (bfd_vma
) 0)))
836 h
->type
= bfd_link_hash_defined
;
837 h
->u
.def
.section
= section
;
838 h
->u
.def
.value
= value
;
841 if (h
->type
== bfd_link_hash_new
)
842 bfd_link_add_undef (info
->hash
, h
);
843 h
->type
= bfd_link_hash_common
;
845 if (section
== &bfd_com_section
)
846 h
->u
.c
.section
= bfd_make_section_old_way (abfd
, "COMMON");
847 else if (section
->owner
!= abfd
)
848 h
->u
.c
.section
= bfd_make_section_old_way (abfd
, section
->name
);
850 h
->u
.c
.section
= section
;
855 BFD_ASSERT (h
->type
== bfd_link_hash_common
);
856 if (! ((*info
->callbacks
->multiple_common
)
858 h
->u
.c
.section
->owner
, bfd_link_hash_common
, h
->u
.c
.size
,
859 abfd
, bfd_link_hash_common
, value
)))
861 if (value
> h
->u
.c
.size
)
863 if (h
->u
.c
.section
== (asection
*) NULL
)
864 h
->u
.c
.section
= bfd_make_section_old_way (abfd
, "COMMON");
867 BFD_ASSERT (h
->type
== bfd_link_hash_defined
);
868 if (! ((*info
->callbacks
->multiple_common
)
870 h
->u
.def
.section
->owner
, bfd_link_hash_defined
, (bfd_vma
) 0,
871 abfd
, bfd_link_hash_common
, value
)))
881 case bfd_link_hash_defined
:
882 msec
= h
->u
.def
.section
;
883 mval
= h
->u
.def
.value
;
885 case bfd_link_hash_common
:
886 msec
= &bfd_com_section
;
889 case bfd_link_hash_indirect
:
890 msec
= &bfd_ind_section
;
897 if (! ((*info
->callbacks
->multiple_definition
)
898 (info
, name
, msec
->owner
, msec
, mval
, abfd
, section
,
905 struct bfd_link_hash_entry
*inh
;
907 /* STRING is the name of the symbol we want to indirect
909 inh
= bfd_link_hash_lookup (info
->hash
, string
, true, copy
,
911 if (inh
== (struct bfd_link_hash_entry
*) NULL
)
913 if (inh
->type
== bfd_link_hash_new
)
915 inh
->type
= bfd_link_hash_undefined
;
916 inh
->u
.undef
.abfd
= abfd
;
917 bfd_link_add_undef (info
->hash
, inh
);
919 h
->type
= bfd_link_hash_indirect
;
924 if (! (*info
->callbacks
->add_to_set
) (info
, h
, abfd
, section
, value
))
929 if (h
->u
.i
.warning
!= NULL
)
931 if (! (*info
->callbacks
->warning
) (info
, h
->u
.i
.warning
))
933 /* Only issue a warning once. */
934 h
->u
.i
.warning
= NULL
;
945 struct bfd_link_hash_entry
*sub
;
947 /* STRING is the warning to give. */
948 sub
= ((struct bfd_link_hash_entry
*)
949 bfd_hash_allocate (&info
->hash
->table
,
950 sizeof (struct bfd_link_hash_entry
)));
952 h
->type
= bfd_link_hash_warning
;
955 h
->u
.i
.warning
= string
;
960 w
= bfd_hash_allocate (&info
->hash
->table
,
961 strlen (string
) + 1);
974 /* Generic final link routine. */
977 _bfd_generic_final_link (abfd
, info
)
979 struct bfd_link_info
*info
;
983 struct bfd_link_order
*p
;
985 struct generic_write_global_symbol_info wginfo
;
987 abfd
->outsymbols
= (asymbol
**) NULL
;
991 /* Build the output symbol table. This also reads in the symbols
992 for all the input BFDs, keeping them in the outsymbols field. */
993 for (sub
= info
->input_bfds
; sub
!= (bfd
*) NULL
; sub
= sub
->link_next
)
994 if (! _bfd_generic_link_output_symbols (abfd
, sub
, info
, &outsymalloc
))
997 /* Accumulate the global symbols. */
998 wginfo
.output_bfd
= abfd
;
999 wginfo
.psymalloc
= &outsymalloc
;
1000 _bfd_generic_link_hash_traverse (_bfd_generic_hash_table (info
),
1001 _bfd_generic_link_write_global_symbol
,
1004 if (info
->relocateable
)
1006 /* Allocate space for the output relocs for each section. */
1007 for (o
= abfd
->sections
;
1008 o
!= (asection
*) NULL
;
1012 for (p
= o
->link_order_head
;
1013 p
!= (struct bfd_link_order
*) NULL
;
1016 if (p
->type
== bfd_indirect_link_order
)
1018 asection
*input_section
;
1020 bfd_size_type relsize
;
1022 bfd_size_type reloc_count
;
1024 input_section
= p
->u
.indirect
.section
;
1025 input_bfd
= input_section
->owner
;
1026 relsize
= bfd_get_reloc_upper_bound (input_bfd
,
1028 relocs
= (arelent
**) bfd_xmalloc (relsize
);
1030 bfd_canonicalize_reloc (input_bfd
, input_section
,
1032 bfd_get_outsymbols (input_bfd
));
1033 BFD_ASSERT (reloc_count
== input_section
->reloc_count
);
1034 o
->reloc_count
+= reloc_count
;
1038 if (o
->reloc_count
> 0)
1040 o
->orelocation
= ((arelent
**)
1043 * sizeof (arelent
*))));
1044 /* Reset the count so that it can be used as an index
1045 when putting in the output relocs. */
1051 /* Handle all the link order information for the sections. */
1052 for (o
= abfd
->sections
;
1053 o
!= (asection
*) NULL
;
1056 for (p
= o
->link_order_head
;
1057 p
!= (struct bfd_link_order
*) NULL
;
1062 case bfd_indirect_link_order
:
1063 if (! _bfd_generic_indirect_link_order (abfd
, info
, o
, p
))
1067 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
1077 /* Add an output symbol to the output BFD. */
1080 generic_add_output_symbol (output_bfd
, psymalloc
, sym
)
1085 if (output_bfd
->symcount
>= *psymalloc
)
1089 if (*psymalloc
== 0)
1093 if (output_bfd
->outsymbols
== (asymbol
**) NULL
)
1094 newsyms
= (asymbol
**) malloc (*psymalloc
* sizeof (asymbol
*));
1096 newsyms
= (asymbol
**) realloc (output_bfd
->outsymbols
,
1097 *psymalloc
* sizeof (asymbol
*));
1098 if (newsyms
== (asymbol
**) NULL
)
1100 bfd_error
= no_memory
;
1103 output_bfd
->outsymbols
= newsyms
;
1106 output_bfd
->outsymbols
[output_bfd
->symcount
] = sym
;
1107 ++output_bfd
->symcount
;
1112 /* Handle the symbols for an input BFD. */
1115 _bfd_generic_link_output_symbols (output_bfd
, input_bfd
, info
, psymalloc
)
1118 struct bfd_link_info
*info
;
1125 symsize
= get_symtab_upper_bound (input_bfd
);
1126 input_bfd
->outsymbols
= (asymbol
**) bfd_alloc (input_bfd
, symsize
);
1127 input_bfd
->symcount
= bfd_canonicalize_symtab (input_bfd
,
1128 input_bfd
->outsymbols
);
1130 /* Create a filename symbol if we are supposed to. */
1131 if (info
->create_object_symbols_section
!= (asection
*) NULL
)
1135 for (sec
= input_bfd
->sections
;
1136 sec
!= (asection
*) NULL
;
1139 if (sec
->output_section
== info
->create_object_symbols_section
)
1143 newsym
= bfd_make_empty_symbol (input_bfd
);
1144 newsym
->name
= input_bfd
->filename
;
1146 newsym
->flags
= BSF_LOCAL
| BSF_FILE
;
1147 newsym
->section
= sec
;
1149 if (! generic_add_output_symbol (output_bfd
, psymalloc
,
1158 /* Adjust the values of the globally visible symbols, and write out
1160 sym_ptr
= bfd_get_outsymbols (input_bfd
);
1161 sym_end
= sym_ptr
+ bfd_get_symcount (input_bfd
);
1162 for (; sym_ptr
< sym_end
; sym_ptr
++)
1165 struct generic_link_hash_entry
*h
;
1168 h
= (struct generic_link_hash_entry
*) NULL
;
1170 if ((sym
->flags
& (BSF_INDIRECT
1175 || bfd_get_section (sym
) == &bfd_und_section
1176 || bfd_is_com_section (bfd_get_section (sym
))
1177 || bfd_get_section (sym
) == &bfd_ind_section
)
1179 h
= _bfd_generic_link_hash_lookup (_bfd_generic_hash_table (info
),
1180 bfd_asymbol_name (sym
),
1181 false, false, true);
1182 if (h
!= (struct generic_link_hash_entry
*) NULL
)
1184 /* Force all references to this symbol to point to
1185 the same area in memory. It is possible that
1186 this routine will be called with a hash table
1187 other than a generic hash table, so we double
1189 if (info
->hash
->creator
== input_bfd
->xvec
)
1191 if (h
->sym
!= (asymbol
*) NULL
)
1192 *sym_ptr
= sym
= h
->sym
;
1195 switch (h
->root
.type
)
1198 case bfd_link_hash_new
:
1200 case bfd_link_hash_undefined
:
1201 case bfd_link_hash_weak
:
1203 case bfd_link_hash_defined
:
1204 sym
->value
= h
->root
.u
.def
.value
;
1205 sym
->section
= h
->root
.u
.def
.section
;
1206 sym
->flags
|= BSF_GLOBAL
;
1208 case bfd_link_hash_common
:
1209 sym
->value
= h
->root
.u
.c
.size
;
1210 sym
->flags
|= BSF_GLOBAL
;
1211 /* We do not set the section of the symbol to
1212 c.section. c.section is saved so that we know
1213 where to allocate the symbol if we define it. In
1214 this case the type is still bfd_link_hash_common,
1215 so we did not define it, so we do not want to use
1217 BFD_ASSERT (bfd_is_com_section (sym
->section
));
1223 /* This switch is straight from the old code in
1224 write_file_locals in ldsym.c. */
1225 if (info
->strip
== strip_some
1226 && (bfd_hash_lookup (info
->keep_hash
, bfd_asymbol_name (sym
),
1228 == (struct bfd_hash_entry
*) NULL
))
1230 else if ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0)
1232 /* If this symbol is marked as occurring now, rather
1233 than at the end, output it now. This is used for
1234 COFF C_EXT FCN symbols. FIXME: There must be a
1236 if (bfd_asymbol_bfd (sym
) == input_bfd
1237 && (sym
->flags
& BSF_NOT_AT_END
) != 0)
1242 else if (sym
->section
== &bfd_ind_section
)
1244 else if ((sym
->flags
& BSF_DEBUGGING
) != 0)
1246 if (info
->strip
== strip_none
)
1251 else if (sym
->section
== &bfd_und_section
1252 || bfd_is_com_section (sym
->section
))
1254 else if ((sym
->flags
& BSF_LOCAL
) != 0)
1256 if ((sym
->flags
& BSF_WARNING
) != 0)
1260 switch (info
->discard
)
1267 if (bfd_asymbol_name (sym
)[0] == info
->lprefix
[0]
1268 && (info
->lprefix_len
== 1
1269 || strncmp (bfd_asymbol_name (sym
), info
->lprefix
,
1270 info
->lprefix_len
) == 0))
1281 else if ((sym
->flags
& BSF_CONSTRUCTOR
))
1283 if (info
->strip
!= strip_all
)
1293 if (! generic_add_output_symbol (output_bfd
, psymalloc
, sym
))
1295 if (h
!= (struct generic_link_hash_entry
*) NULL
)
1296 h
->root
.written
= true;
1303 /* Write out a global symbol, if it hasn't already been written out.
1304 This is called for each symbol in the hash table. */
1307 _bfd_generic_link_write_global_symbol (h
, data
)
1308 struct generic_link_hash_entry
*h
;
1311 struct generic_write_global_symbol_info
*wginfo
=
1312 (struct generic_write_global_symbol_info
*) data
;
1315 if (h
->root
.written
)
1318 if (h
->sym
!= (asymbol
*) NULL
)
1321 BFD_ASSERT (strcmp (bfd_asymbol_name (sym
), h
->root
.root
.string
) == 0);
1325 sym
= bfd_make_empty_symbol (wginfo
->output_bfd
);
1326 sym
->name
= h
->root
.root
.string
;
1330 switch (h
->root
.type
)
1333 case bfd_link_hash_new
:
1335 case bfd_link_hash_undefined
:
1336 sym
->section
= &bfd_und_section
;
1339 case bfd_link_hash_weak
:
1340 sym
->section
= &bfd_und_section
;
1342 sym
->flags
|= BSF_WEAK
;
1343 case bfd_link_hash_defined
:
1344 sym
->section
= h
->root
.u
.def
.section
;
1345 sym
->value
= h
->root
.u
.def
.value
;
1347 case bfd_link_hash_common
:
1348 sym
->value
= h
->root
.u
.c
.size
;
1349 /* Do not set the section; see _bfd_generic_link_output_symbols. */
1350 BFD_ASSERT (bfd_is_com_section (sym
->section
));
1352 case bfd_link_hash_indirect
:
1353 case bfd_link_hash_warning
:
1354 /* FIXME: What should we do here? */
1358 sym
->flags
|= BSF_GLOBAL
;
1360 if (! generic_add_output_symbol (wginfo
->output_bfd
, wginfo
->psymalloc
,
1363 /* FIXME: No way to return failure. */
1367 h
->root
.written
= true;
1372 /* Handle an indirect section when doing a generic link. */
1375 _bfd_generic_indirect_link_order (output_bfd
, info
, output_section
, link_order
)
1377 struct bfd_link_info
*info
;
1378 asection
*output_section
;
1379 struct bfd_link_order
*link_order
;
1381 asection
*input_section
;
1385 BFD_ASSERT ((output_section
->flags
& SEC_HAS_CONTENTS
) != 0);
1387 if (link_order
->size
== 0)
1390 input_section
= link_order
->u
.indirect
.section
;
1391 input_bfd
= input_section
->owner
;
1393 BFD_ASSERT (input_section
->output_section
== output_section
);
1394 BFD_ASSERT (input_section
->output_offset
== link_order
->offset
);
1395 BFD_ASSERT (bfd_section_size (input_bfd
, input_section
) == link_order
->size
);
1397 /* Get and relocate the section contents. */
1398 contents
= (bfd_byte
*) alloca (bfd_section_size (input_bfd
, input_section
));
1399 contents
= (bfd_get_relocated_section_contents
1400 (output_bfd
, info
, link_order
, contents
, info
->relocateable
,
1401 bfd_get_outsymbols (input_bfd
)));
1403 /* Output the section contents. */
1404 if (! bfd_set_section_contents (output_bfd
, output_section
, contents
,
1405 link_order
->offset
, link_order
->size
))
1411 /* Allocate a new link_order for a section. */
1413 struct bfd_link_order
*
1414 bfd_new_link_order (abfd
, section
)
1418 struct bfd_link_order
*new;
1420 new = ((struct bfd_link_order
*)
1421 bfd_alloc_by_size_t (abfd
, sizeof (struct bfd_link_order
)));
1423 new->type
= bfd_undefined_link_order
;
1426 new->next
= (struct bfd_link_order
*) NULL
;
1428 if (section
->link_order_tail
!= (struct bfd_link_order
*) NULL
)
1429 section
->link_order_tail
->next
= new;
1431 section
->link_order_head
= new;
1432 section
->link_order_tail
= new;
1437 /* Default link order processing routine. */
1440 _bfd_default_link_order (abfd
, info
, sec
, link_order
)
1442 struct bfd_link_info
*info
;
1444 struct bfd_link_order
*link_order
;
1446 switch (link_order
->type
)
1448 case bfd_undefined_link_order
:
1451 case bfd_indirect_link_order
:
1453 case bfd_fill_link_order
:
1454 return default_fill_link_order (abfd
, info
, sec
, link_order
);
1458 /* Default routine to handle a bfd_fill_link_order. */
1461 default_fill_link_order (abfd
, info
, sec
, link_order
)
1463 struct bfd_link_info
*info
;
1465 struct bfd_link_order
*link_order
;
1472 BFD_ASSERT ((sec
->flags
& SEC_HAS_CONTENTS
) != 0);
1474 size
= (size_t) link_order
->size
;
1475 space
= (char *) alloca (size
);
1476 fill
= link_order
->u
.fill
.value
;
1477 for (i
= 0; i
< size
; i
+= 2)
1478 space
[i
] = fill
>> 8;
1479 for (i
= 1; i
< size
; i
+= 2)
1481 return bfd_set_section_contents (abfd
, sec
, space
,
1482 (file_ptr
) link_order
->offset
,