* ecoff.c: First cut at new style of linker backend for
[deliverable/binutils-gdb.git] / bfd / linker.c
CommitLineData
da6b2d99
ILT
1/* linker.c -- BFD linker routines
2 Copyright 1993 Free Software Foundation, Inc.
3 Written by Steve Chamberlain and Ian Lance Taylor, Cygnus Support
4
5This file is part of BFD
6
7GLD is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
11
12GLD is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GLD; see the file COPYING. If not, write to
19the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
20
21#include "bfd.h"
22#include "sysdep.h"
23#include "libbfd.h"
24#include "bfdlink.h"
25#include "genlink.h"
26
27static struct bfd_hash_entry *generic_link_hash_newfunc
28 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *,
29 const char *));
30static boolean generic_link_add_object_symbols
31 PARAMS ((bfd *, struct bfd_link_info *));
32static boolean generic_link_check_archive_element
33 PARAMS ((bfd *, struct bfd_link_info *, boolean *pneeded));
34static boolean generic_link_add_symbol_list
35 PARAMS ((bfd *, struct bfd_link_info *, bfd_size_type count, asymbol **));
36static boolean generic_add_output_symbol
37 PARAMS ((bfd *, size_t *psymalloc, asymbol *));
38static boolean default_fill_link_order
39 PARAMS ((bfd *, struct bfd_link_info *, asection *,
40 struct bfd_link_order *));
6e07e54f
ILT
41static boolean default_indirect_link_order
42 PARAMS ((bfd *, struct bfd_link_info *, asection *,
43 struct bfd_link_order *));
da6b2d99
ILT
44
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
47 upon. */
48
49/* Routine to create an entry in the link hash table. */
50
51struct bfd_hash_entry *
52_bfd_link_hash_newfunc (entry, table, string)
53 struct bfd_hash_entry *entry;
54 struct bfd_hash_table *table;
55 const char *string;
56{
57 struct bfd_link_hash_entry *ret = (struct bfd_link_hash_entry *) entry;
58
59 /* Allocate the structure if it has not already been allocated by a
60 subclass. */
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)));
64
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));
68
69 /* Initialize the local fields. */
70 ret->type = bfd_link_hash_new;
71 ret->written = false;
72 ret->next = NULL;
73
74 return (struct bfd_hash_entry *) ret;
75}
76
77/* Initialize a link hash table. The BFD argument is the one
78 responsible for creating this table. */
79
80boolean
81_bfd_link_hash_table_init (table, abfd, newfunc)
82 struct bfd_link_hash_table *table;
83 bfd *abfd;
84 struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *,
85 struct bfd_hash_table *,
86 const char *));
87{
88 table->creator = abfd->xvec;
89 table->undefs = NULL;
90 table->undefs_tail = NULL;
91 return bfd_hash_table_init (&table->table, newfunc);
92}
93
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
96 the real symbol. */
97
98struct bfd_link_hash_entry *
99bfd_link_hash_lookup (table, string, create, copy, follow)
100 struct bfd_link_hash_table *table;
101 const char *string;
102 boolean create;
103 boolean copy;
104 boolean follow;
105{
106 struct bfd_link_hash_entry *ret;
107
108 ret = ((struct bfd_link_hash_entry *)
109 bfd_hash_lookup (&table->table, string, create, copy));
110
111 if (follow && ret != (struct bfd_link_hash_entry *) NULL)
112 {
113 while (ret->type == bfd_link_hash_indirect
114 || ret->type == bfd_link_hash_warning)
115 ret = ret->u.i.link;
116 }
117
118 return ret;
119}
120
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
125 call. */
126
127void
128bfd_link_hash_traverse (table, func, info)
129 struct bfd_link_hash_table *table;
130 boolean (*func) PARAMS ((struct bfd_link_hash_entry *, PTR));
131 PTR info;
132{
133 bfd_hash_traverse (&table->table,
134 ((boolean (*) PARAMS ((struct bfd_hash_entry *, PTR)))
135 func),
136 info);
137}
138
139/* Add a symbol to the linker hash table undefs list. */
140
141INLINE void
142bfd_link_add_undef (table, h)
143 struct bfd_link_hash_table *table;
144 struct bfd_link_hash_entry *h;
145{
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)
150 table->undefs = h;
151 table->undefs_tail = h;
152}
153\f
154/* Routine to create an entry in an generic link hash table. */
155
156static struct bfd_hash_entry *
157generic_link_hash_newfunc (entry, table, string)
158 struct bfd_hash_entry *entry;
159 struct bfd_hash_table *table;
160 const char *string;
161{
162 struct generic_link_hash_entry *ret =
163 (struct generic_link_hash_entry *) entry;
164
165 /* Allocate the structure if it has not already been allocated by a
166 subclass. */
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)));
170
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,
174 table, string));
175
176 /* Set local fields. */
177 ret->sym = NULL;
178
179 return (struct bfd_hash_entry *) ret;
180}
181
182/* Create an generic link hash table. */
183
184struct bfd_link_hash_table *
185_bfd_generic_link_hash_table_create (abfd)
186 bfd *abfd;
187{
188 struct generic_link_hash_table *ret;
189
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))
194 {
195 free (ret);
196 return (struct bfd_link_hash_table *) NULL;
197 }
198 return &ret->root;
199}
200\f
201/* Generic function to add symbols from an object file to the global
202 hash table. */
203
204boolean
205_bfd_generic_link_add_symbols (abfd, info)
206 bfd *abfd;
207 struct bfd_link_info *info;
208{
209 boolean ret;
210
211 switch (bfd_get_format (abfd))
212 {
213 case bfd_object:
214 ret = generic_link_add_object_symbols (abfd, info);
215 break;
216 case bfd_archive:
217 ret = _bfd_generic_link_add_archive_symbols
218 (abfd, info, generic_link_check_archive_element);
219 break;
220 default:
221 bfd_error = wrong_format;
222 ret = false;
223 }
224
225 /* If we might be using the C based alloca function, make sure we
226 have dumped the symbol tables we just allocated. */
227#ifndef __GNUC__
228#ifndef alloca
229 alloca (0);
230#endif
231#endif
232
233 return ret;
234}
235
236/* Add symbols from an object file to the global hash table. */
237
238static boolean
239generic_link_add_object_symbols (abfd, info)
240 bfd *abfd;
241 struct bfd_link_info *info;
242{
243 size_t symsize;
244 asymbol **symbols;
245 bfd_size_type symbol_count;
246
247 symsize = get_symtab_upper_bound (abfd);
248 symbols = (asymbol **) alloca (symsize);
249 symbol_count = bfd_canonicalize_symtab (abfd, symbols);
250
251 return generic_link_add_symbol_list (abfd, info, symbol_count, symbols);
252}
253\f
254/* We build a hash table of all symbols defined in an archive. */
255
256/* An archive symbol may be defined by multiple archive elements.
257 This linked list is used to hold the elements. */
258
259struct archive_list
260{
261 struct archive_list *next;
262 int indx;
263};
264
265/* An entry in an archive hash table. */
266
267struct archive_hash_entry
268{
269 struct bfd_hash_entry root;
270 /* Where the symbol is defined. */
271 struct archive_list *defs;
272};
273
274/* An archive hash table itself. */
275
276struct archive_hash_table
277{
278 struct bfd_hash_table table;
279};
280
281static struct bfd_hash_entry *archive_hash_newfunc
282 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
283static 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 *,
287 const char *)));
288
289/* Create a new entry for an archive hash table. */
290
291static struct bfd_hash_entry *
292archive_hash_newfunc (entry, table, string)
293 struct bfd_hash_entry *entry;
294 struct bfd_hash_table *table;
295 const char *string;
296{
297 struct archive_hash_entry *ret = (struct archive_hash_entry *) entry;
298
299 /* Allocate the structure if it has not already been allocated by a
300 subclass. */
301 if (ret == (struct archive_hash_entry *) NULL)
302 ret = ((struct archive_hash_entry *)
303 bfd_hash_allocate (table, sizeof (struct archive_hash_entry)));
304
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));
308
309 /* Initialize the local fields. */
310 ret->defs = (struct archive_list *) NULL;
311
312 return (struct bfd_hash_entry *) ret;
313}
314
315/* Initialize an archive hash table. */
316
317static boolean
318archive_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 *,
322 const char *));
323{
324 return bfd_hash_table_init (&table->table, newfunc);
325}
326
327/* Look up an entry in an archive hash table. */
328
329#define archive_hash_lookup(t, string, create, copy) \
330 ((struct archive_hash_entry *) \
331 bfd_hash_lookup (&(t)->table, (string), (create), (copy)))
332
333/* Free an archive hash table. */
334
335#define archive_hash_table_free(t) bfd_hash_table_free (&(t)->table)
336
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
343 link.
344
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.
351
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.
358
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. */
369
370boolean
371_bfd_generic_link_add_archive_symbols (abfd, info, checkfn)
372 bfd *abfd;
373 struct bfd_link_info *info;
374 boolean (*checkfn) PARAMS ((bfd *, struct bfd_link_info *,
375 boolean *pneeded));
376{
377 carsym *arsyms;
378 carsym *arsym_end;
379 register carsym *arsym;
380 int pass;
381 struct archive_hash_table arsym_hash;
382 int indx;
383 struct bfd_link_hash_entry **pundef;
384
385 if (! bfd_has_map (abfd))
386 {
387 bfd_error = no_symbols;
388 return false;
389 }
390
391 arsyms = bfd_ardata (abfd)->symdefs;
392 arsym_end = arsyms + bfd_ardata (abfd)->symdef_count;
393
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))
397 return false;
398 for (arsym = arsyms, indx = 0; arsym < arsym_end; arsym++, indx++)
399 {
400 struct archive_hash_entry *arh;
401 struct archive_list *l;
402
403 arh = archive_hash_lookup (&arsym_hash, arsym->name, true, false);
404 if (arh == (struct archive_hash_entry *) NULL)
405 return false;
406 l = (struct archive_list *) alloca (sizeof (struct archive_list));
407 l->next = arh->defs;
408 arh->defs = l;
409 l->indx = indx;
410 }
411
412 pass = 1;
413
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)
418 {
419 struct bfd_link_hash_entry *h;
420 struct archive_hash_entry *arh;
421 struct archive_list *l;
422
423 h = *pundef;
424
425 /* When a symbol is defined, it is not necessarily removed from
426 the list. */
427 if (h->type != bfd_link_hash_undefined
428 && h->type != bfd_link_hash_common)
429 {
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;
437 else
438 pundef = &(*pundef)->next;
439 continue;
440 }
441
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)
445 {
446 pundef = &(*pundef)->next;
447 continue;
448 }
449
450 /* Look at all the objects which define this symbol. */
451 for (l = arh->defs; l != (struct archive_list *) NULL; l = l->next)
452 {
453 bfd *element;
454 boolean needed;
455
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)
459 break;
460
461 element = bfd_get_elt_at_index (abfd, l->indx);
462 if (element == (bfd *) NULL)
463 return false;
464
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)
469 continue;
470
471 /* If we can't figure this element out, just ignore it. */
472 if (! bfd_check_format (element, bfd_object))
473 {
474 element->archive_pass = -1;
475 continue;
476 }
477
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))
481 return false;
482
483 if (! needed)
484 element->archive_pass = pass;
485 else
486 {
487 element->archive_pass = -1;
488
489 /* Increment the pass count to show that we may need to
490 recheck object files which were already checked. */
491 ++pass;
492 }
493 }
494
495 pundef = &(*pundef)->next;
496 }
497
498 archive_hash_table_free (&arsym_hash);
499
500 return true;
501}
502\f
503/* See if we should include an archive element. */
504
505static boolean
506generic_link_check_archive_element (abfd, info, pneeded)
507 bfd *abfd;
508 struct bfd_link_info *info;
509 boolean *pneeded;
510{
511 size_t symsize;
512 asymbol **symbols;
513 bfd_size_type symbol_count;
514 asymbol **pp, **ppend;
515
516 *pneeded = false;
517
518 symsize = get_symtab_upper_bound (abfd);
519 symbols = (asymbol **) alloca (symsize);
520 symbol_count = bfd_canonicalize_symtab (abfd, symbols);
521
522 pp = symbols;
523 ppend = symbols + symbol_count;
524 for (; pp < ppend; pp++)
525 {
526 asymbol *p;
527 struct bfd_link_hash_entry *h;
528
529 p = *pp;
530
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)
534 continue;
535
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
540 ABI, p. 4-27. */
541 h = bfd_link_hash_lookup (info->hash, bfd_asymbol_name (p), false,
542 false, true);
543 if (h == (struct bfd_link_hash_entry *) NULL
544 || (h->type != bfd_link_hash_undefined
545 && h->type != bfd_link_hash_common))
546 continue;
547
548 /* P is a symbol we are looking for. */
549
550 if (! bfd_is_com_section (p->section))
551 {
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)))
555 return false;
556 if (! generic_link_add_symbol_list (abfd, info, symbol_count,
557 symbols))
558 return false;
559 *pneeded = true;
560 return true;
561 }
562
563 /* P is a common symbol. */
564
565 if (h->type == bfd_link_hash_undefined)
566 {
567 bfd *symbfd;
568
569 symbfd = h->u.undef.abfd;
570 if (symbfd == (bfd *) NULL)
571 {
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)))
577 return false;
578 *pneeded = true;
579 return true;
580 }
581
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
6e07e54f
ILT
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. */
da6b2d99
ILT
589 h->type = bfd_link_hash_common;
590 h->u.c.size = bfd_asymbol_value (p);
6e07e54f
ILT
591 if (p->section == &bfd_com_section)
592 h->u.c.section = bfd_make_section_old_way (symbfd, "COMMON");
593 else
594 h->u.c.section = bfd_make_section_old_way (symbfd,
595 p->section->name);
da6b2d99
ILT
596 }
597 else
598 {
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
602 differently. */
603 if (bfd_asymbol_value (p) > h->u.c.size)
604 h->u.c.size = bfd_asymbol_value (p);
605 }
606 }
607
608 /* This archive element is not needed. */
609 return true;
610}
611
612/* Add the symbol from an object file to the global hash table. */
613
614static boolean
615generic_link_add_symbol_list (abfd, info, symbol_count, symbols)
616 bfd *abfd;
617 struct bfd_link_info *info;
618 bfd_size_type symbol_count;
619 asymbol **symbols;
620{
621 asymbol **pp, **ppend;
622
623 pp = symbols;
624 ppend = symbols + symbol_count;
625 for (; pp < ppend; pp++)
626 {
627 asymbol *p;
628
629 p = *pp;
630
631 if ((p->flags & (BSF_INDIRECT
632 | BSF_WARNING
633 | BSF_GLOBAL
634 | BSF_CONSTRUCTOR
635 | BSF_WEAK)) != 0
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)
639 {
640 const char *name;
641 const char *string;
642 struct generic_link_hash_entry *h;
643
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)
649 {
650 /* The name of P is actually the warning string, and the
651 value is actually a pointer to the symbol to warn
652 about. */
653 string = name;
654 name = bfd_asymbol_name ((asymbol *) p->value);
655 }
656 else
657 string = NULL;
6e07e54f
ILT
658
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. */
da6b2d99
ILT
666 if (! (_bfd_generic_link_add_one_symbol
667 (info, abfd, name, p->flags, bfd_get_section (p),
6e07e54f 668 p->value, string, false, 0, 32,
da6b2d99
ILT
669 (struct bfd_link_hash_entry **) &h)))
670 return false;
671
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
679 generic one. */
680 if (info->hash->creator == abfd->xvec)
681 {
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))))
686 h->sym = p;
687 }
688 }
689 }
690
691 return true;
692}
693\f
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. */
698
699/* The symbol from the object file is turned into one of these row
700 values. */
701
702enum link_row
703{
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. */
712};
713
714/* The actions to take in the state table. */
715
716enum link_action
717{
718 FAIL, /* Abort. */
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. */
734};
735
736/* The state table itself. The first index is a link_row and the
737 second index is a bfd_link_hash_type. */
738
739static const enum link_action link_action[8][7] =
740{
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 }
750};
751
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.
6e07e54f
ILT
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.
da6b2d99
ILT
767 HASHP, if not NULL, is a place to store the created hash table
768 entry. */
769
770boolean
771_bfd_generic_link_add_one_symbol (info, abfd, name, flags, section, value,
6e07e54f 772 string, copy, constructor, bitsize, hashp)
da6b2d99
ILT
773 struct bfd_link_info *info;
774 bfd *abfd;
775 const char *name;
776 flagword flags;
777 asection *section;
778 bfd_vma value;
779 const char *string;
780 boolean copy;
6e07e54f
ILT
781 boolean constructor;
782 unsigned int bitsize;
da6b2d99
ILT
783 struct bfd_link_hash_entry **hashp;
784{
785 enum link_row row;
786 struct bfd_link_hash_entry *h;
787 boolean cycle;
788
789 if (section == &bfd_ind_section
790 || (flags & BSF_INDIRECT) != 0)
791 row = INDR_ROW;
792 else if ((flags & BSF_WARNING) != 0)
793 row = WARN_ROW;
794 else if ((flags & BSF_CONSTRUCTOR) != 0)
795 row = SET_ROW;
796 else if (section == &bfd_und_section)
797 {
798 if ((flags & BSF_WEAK) != 0)
799 row = UNDEFW_ROW;
800 else
801 row = UNDEF_ROW;
802 }
803 else if ((flags & BSF_WEAK) != 0)
804 row = DEFW_ROW;
805 else if (bfd_is_com_section (section))
806 row = COMMON_ROW;
807 else
808 row = DEF_ROW;
809
810 h = bfd_link_hash_lookup (info->hash, name, true, copy, false);
811 if (h == (struct bfd_link_hash_entry *) NULL)
812 {
813 if (hashp != (struct bfd_link_hash_entry **) NULL)
814 *hashp = NULL;
815 return false;
816 }
817
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))
821 {
822 if (! (*info->callbacks->notice) (info, name, abfd, section, value))
823 return false;
824 }
825
826 if (hashp != (struct bfd_link_hash_entry **) NULL)
827 *hashp = h;
828
829 do
830 {
831 enum link_action action;
832
833 cycle = false;
834 action = link_action[(int) row][(int) h->type];
835 switch (action)
836 {
837 case FAIL:
838 abort ();
839 case UND:
840 h->type = bfd_link_hash_undefined;
841 h->u.undef.abfd = abfd;
842 bfd_link_add_undef (info->hash, h);
843 break;
844 case WEAK:
845 h->type = bfd_link_hash_weak;
846 h->u.undef.abfd = abfd;
847 break;
848 case CDEF:
849 BFD_ASSERT (h->type == bfd_link_hash_common);
850 if (! ((*info->callbacks->multiple_common)
851 (info, name,
852 h->u.c.section->owner, bfd_link_hash_common, h->u.c.size,
853 abfd, bfd_link_hash_defined, (bfd_vma) 0)))
854 return false;
855 /* Fall through. */
856 case DEF:
857 h->type = bfd_link_hash_defined;
858 h->u.def.section = section;
859 h->u.def.value = value;
6e07e54f
ILT
860
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] == '_')
867 {
868 const char *s;
869
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). */
876
877#define CONS_PREFIX "GLOBAL_"
878#define CONS_PREFIX_LEN (sizeof CONS_PREFIX - 1)
879
880 s = name + 1;
881 while (*s == '_')
882 ++s;
883 if (s[0] == 'G'
884 && strncmp (s, CONS_PREFIX, CONS_PREFIX_LEN - 1) == 0)
885 {
886 char c;
887
888 c = s[CONS_PREFIX_LEN + 1];
889 if ((c == 'I' || c == 'D')
890 && s[CONS_PREFIX_LEN] == s[CONS_PREFIX_LEN + 2])
891 {
892 if (! ((*info->callbacks->constructor)
893 (info,
894 c == 'I' ? true : false, bitsize,
895 name, abfd, section, value)))
896 return false;
897 }
898 }
899 }
900
da6b2d99
ILT
901 break;
902 case COM:
903 if (h->type == bfd_link_hash_new)
904 bfd_link_add_undef (info->hash, h);
905 h->type = bfd_link_hash_common;
906 h->u.c.size = value;
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);
911 else
912 h->u.c.section = section;
913 break;
914 case NOACT:
915 break;
916 case BIG:
917 BFD_ASSERT (h->type == bfd_link_hash_common);
918 if (! ((*info->callbacks->multiple_common)
919 (info, name,
920 h->u.c.section->owner, bfd_link_hash_common, h->u.c.size,
921 abfd, bfd_link_hash_common, value)))
922 return false;
923 if (value > h->u.c.size)
924 h->u.c.size = value;
da6b2d99
ILT
925 break;
926 case CREF:
927 BFD_ASSERT (h->type == bfd_link_hash_defined);
928 if (! ((*info->callbacks->multiple_common)
929 (info, name,
930 h->u.def.section->owner, bfd_link_hash_defined, (bfd_vma) 0,
931 abfd, bfd_link_hash_common, value)))
932 return false;
933 break;
934 case MDEF:
935 {
936 asection *msec;
937 bfd_vma mval;
938
939 switch (h->type)
940 {
941 case bfd_link_hash_defined:
942 msec = h->u.def.section;
943 mval = h->u.def.value;
944 break;
945 case bfd_link_hash_common:
946 msec = &bfd_com_section;
947 mval = h->u.c.size;
948 break;
949 case bfd_link_hash_indirect:
950 msec = &bfd_ind_section;
951 mval = 0;
952 break;
953 default:
954 abort ();
955 }
956
957 if (! ((*info->callbacks->multiple_definition)
958 (info, name, msec->owner, msec, mval, abfd, section,
959 value)))
960 return false;
961 }
962 break;
963 case IND:
964 {
965 struct bfd_link_hash_entry *inh;
966
967 /* STRING is the name of the symbol we want to indirect
968 to. */
969 inh = bfd_link_hash_lookup (info->hash, string, true, copy,
970 false);
971 if (inh == (struct bfd_link_hash_entry *) NULL)
972 return false;
973 if (inh->type == bfd_link_hash_new)
974 {
975 inh->type = bfd_link_hash_undefined;
976 inh->u.undef.abfd = abfd;
977 bfd_link_add_undef (info->hash, inh);
978 }
979 h->type = bfd_link_hash_indirect;
980 h->u.i.link = inh;
981 }
982 break;
983 case SET:
6e07e54f
ILT
984 if (! (*info->callbacks->add_to_set) (info, h, bitsize, abfd,
985 section, value))
da6b2d99
ILT
986 return false;
987 break;
988 case WARN:
989 case WARNC:
990 if (h->u.i.warning != NULL)
991 {
992 if (! (*info->callbacks->warning) (info, h->u.i.warning))
993 return false;
994 /* Only issue a warning once. */
995 h->u.i.warning = NULL;
996 }
997 if (action == WARN)
998 break;
999 /* Fall through. */
1000 case CYCLE:
1001 h = h->u.i.link;
1002 cycle = true;
1003 break;
1004 case MWARN:
1005 {
1006 struct bfd_link_hash_entry *sub;
1007
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)));
1012 *sub = *h;
1013 h->type = bfd_link_hash_warning;
1014 h->u.i.link = sub;
1015 if (! copy)
1016 h->u.i.warning = string;
1017 else
1018 {
1019 char *w;
1020
1021 w = bfd_hash_allocate (&info->hash->table,
1022 strlen (string) + 1);
1023 strcpy (w, string);
1024 h->u.i.warning = w;
1025 }
1026 }
1027 break;
1028 }
1029 }
1030 while (cycle);
1031
1032 return true;
1033}
1034\f
1035/* Generic final link routine. */
1036
1037boolean
1038_bfd_generic_final_link (abfd, info)
1039 bfd *abfd;
1040 struct bfd_link_info *info;
1041{
1042 bfd *sub;
1043 asection *o;
1044 struct bfd_link_order *p;
1045 size_t outsymalloc;
1046 struct generic_write_global_symbol_info wginfo;
1047
1048 abfd->outsymbols = (asymbol **) NULL;
1049 abfd->symcount = 0;
1050 outsymalloc = 0;
1051
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))
1056 return false;
1057
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,
1063 (PTR) &wginfo);
1064
1065 if (info->relocateable)
1066 {
1067 /* Allocate space for the output relocs for each section. */
1068 for (o = abfd->sections;
1069 o != (asection *) NULL;
1070 o = o->next)
1071 {
1072 o->reloc_count = 0;
1073 for (p = o->link_order_head;
1074 p != (struct bfd_link_order *) NULL;
1075 p = p->next)
1076 {
1077 if (p->type == bfd_indirect_link_order)
1078 {
1079 asection *input_section;
1080 bfd *input_bfd;
1081 bfd_size_type relsize;
1082 arelent **relocs;
1083 bfd_size_type reloc_count;
1084
1085 input_section = p->u.indirect.section;
1086 input_bfd = input_section->owner;
1087 relsize = bfd_get_reloc_upper_bound (input_bfd,
1088 input_section);
1089 relocs = (arelent **) bfd_xmalloc (relsize);
1090 reloc_count =
1091 bfd_canonicalize_reloc (input_bfd, input_section,
1092 relocs,
1093 bfd_get_outsymbols (input_bfd));
1094 BFD_ASSERT (reloc_count == input_section->reloc_count);
1095 o->reloc_count += reloc_count;
1096 free (relocs);
1097 }
1098 }
1099 if (o->reloc_count > 0)
1100 {
1101 o->orelocation = ((arelent **)
1102 bfd_alloc (abfd,
1103 (o->reloc_count
1104 * sizeof (arelent *))));
1105 /* Reset the count so that it can be used as an index
1106 when putting in the output relocs. */
1107 o->reloc_count = 0;
1108 }
1109 }
1110 }
1111
1112 /* Handle all the link order information for the sections. */
1113 for (o = abfd->sections;
1114 o != (asection *) NULL;
1115 o = o->next)
1116 {
1117 for (p = o->link_order_head;
1118 p != (struct bfd_link_order *) NULL;
1119 p = p->next)
1120 {
6e07e54f
ILT
1121 if (! _bfd_default_link_order (abfd, info, o, p))
1122 return false;
da6b2d99
ILT
1123 }
1124 }
1125
1126 return true;
1127}
1128
1129/* Add an output symbol to the output BFD. */
1130
1131static boolean
1132generic_add_output_symbol (output_bfd, psymalloc, sym)
1133 bfd *output_bfd;
1134 size_t *psymalloc;
1135 asymbol *sym;
1136{
1137 if (output_bfd->symcount >= *psymalloc)
1138 {
1139 asymbol **newsyms;
1140
1141 if (*psymalloc == 0)
1142 *psymalloc = 124;
1143 else
1144 *psymalloc *= 2;
1145 if (output_bfd->outsymbols == (asymbol **) NULL)
1146 newsyms = (asymbol **) malloc (*psymalloc * sizeof (asymbol *));
1147 else
1148 newsyms = (asymbol **) realloc (output_bfd->outsymbols,
1149 *psymalloc * sizeof (asymbol *));
1150 if (newsyms == (asymbol **) NULL)
1151 {
1152 bfd_error = no_memory;
1153 return false;
1154 }
1155 output_bfd->outsymbols = newsyms;
1156 }
1157
1158 output_bfd->outsymbols[output_bfd->symcount] = sym;
1159 ++output_bfd->symcount;
1160
1161 return true;
1162}
1163
1164/* Handle the symbols for an input BFD. */
1165
1166boolean
1167_bfd_generic_link_output_symbols (output_bfd, input_bfd, info, psymalloc)
1168 bfd *output_bfd;
1169 bfd *input_bfd;
1170 struct bfd_link_info *info;
1171 size_t *psymalloc;
1172{
1173 size_t symsize;
1174 asymbol **sym_ptr;
1175 asymbol **sym_end;
1176
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);
1181
1182 /* Create a filename symbol if we are supposed to. */
1183 if (info->create_object_symbols_section != (asection *) NULL)
1184 {
1185 asection *sec;
1186
1187 for (sec = input_bfd->sections;
1188 sec != (asection *) NULL;
1189 sec = sec->next)
1190 {
1191 if (sec->output_section == info->create_object_symbols_section)
1192 {
1193 asymbol *newsym;
1194
1195 newsym = bfd_make_empty_symbol (input_bfd);
1196 newsym->name = input_bfd->filename;
1197 newsym->value = 0;
1198 newsym->flags = BSF_LOCAL | BSF_FILE;
1199 newsym->section = sec;
1200
1201 if (! generic_add_output_symbol (output_bfd, psymalloc,
1202 newsym))
1203 return false;
1204
1205 break;
1206 }
1207 }
1208 }
1209
1210 /* Adjust the values of the globally visible symbols, and write out
1211 local symbols. */
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++)
1215 {
1216 asymbol *sym;
1217 struct generic_link_hash_entry *h;
1218 boolean output;
1219
1220 h = (struct generic_link_hash_entry *) NULL;
1221 sym = *sym_ptr;
1222 if ((sym->flags & (BSF_INDIRECT
1223 | BSF_WARNING
1224 | BSF_GLOBAL
1225 | BSF_CONSTRUCTOR
1226 | BSF_WEAK)) != 0
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)
1230 {
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)
1235 {
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
1240 check that. */
1241 if (info->hash->creator == input_bfd->xvec)
1242 {
1243 if (h->sym != (asymbol *) NULL)
1244 *sym_ptr = sym = h->sym;
1245 }
1246
1247 switch (h->root.type)
1248 {
1249 default:
1250 case bfd_link_hash_new:
1251 abort ();
1252 case bfd_link_hash_undefined:
1253 case bfd_link_hash_weak:
1254 break;
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;
1259 break;
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
1268 that section. */
1269 BFD_ASSERT (bfd_is_com_section (sym->section));
1270 break;
1271 }
1272 }
1273 }
1274
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),
1279 false, false)
1280 == (struct bfd_hash_entry *) NULL))
1281 output = false;
1282 else if ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0)
1283 {
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
1287 better way. */
1288 if (bfd_asymbol_bfd (sym) == input_bfd
1289 && (sym->flags & BSF_NOT_AT_END) != 0)
1290 output = true;
1291 else
1292 output = false;
1293 }
1294 else if (sym->section == &bfd_ind_section)
1295 output = false;
1296 else if ((sym->flags & BSF_DEBUGGING) != 0)
1297 {
1298 if (info->strip == strip_none)
1299 output = true;
1300 else
1301 output = false;
1302 }
1303 else if (sym->section == &bfd_und_section
1304 || bfd_is_com_section (sym->section))
1305 output = false;
1306 else if ((sym->flags & BSF_LOCAL) != 0)
1307 {
1308 if ((sym->flags & BSF_WARNING) != 0)
1309 output = false;
1310 else
1311 {
1312 switch (info->discard)
1313 {
1314 default:
1315 case discard_all:
1316 output = false;
1317 break;
1318 case discard_l:
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))
1323 output = false;
1324 else
1325 output = true;
1326 break;
1327 case discard_none:
1328 output = true;
1329 break;
1330 }
1331 }
1332 }
1333 else if ((sym->flags & BSF_CONSTRUCTOR))
1334 {
1335 if (info->strip != strip_all)
1336 output = true;
1337 else
1338 output = false;
1339 }
1340 else
1341 abort ();
1342
1343 if (output)
1344 {
1345 if (! generic_add_output_symbol (output_bfd, psymalloc, sym))
1346 return false;
1347 if (h != (struct generic_link_hash_entry *) NULL)
1348 h->root.written = true;
1349 }
1350 }
1351
1352 return true;
1353}
1354
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. */
1357
1358boolean
1359_bfd_generic_link_write_global_symbol (h, data)
1360 struct generic_link_hash_entry *h;
1361 PTR data;
1362{
1363 struct generic_write_global_symbol_info *wginfo =
1364 (struct generic_write_global_symbol_info *) data;
1365 asymbol *sym;
1366
1367 if (h->root.written)
1368 return true;
1369
1370 if (h->sym != (asymbol *) NULL)
1371 {
1372 sym = h->sym;
1373 BFD_ASSERT (strcmp (bfd_asymbol_name (sym), h->root.root.string) == 0);
1374 }
1375 else
1376 {
1377 sym = bfd_make_empty_symbol (wginfo->output_bfd);
1378 sym->name = h->root.root.string;
1379 sym->flags = 0;
1380 }
1381
1382 switch (h->root.type)
1383 {
1384 default:
1385 case bfd_link_hash_new:
1386 abort ();
1387 case bfd_link_hash_undefined:
1388 sym->section = &bfd_und_section;
1389 sym->value = 0;
1390 break;
1391 case bfd_link_hash_weak:
1392 sym->section = &bfd_und_section;
1393 sym->value = 0;
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;
1398 break;
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));
1403 break;
1404 case bfd_link_hash_indirect:
1405 case bfd_link_hash_warning:
1406 /* FIXME: What should we do here? */
1407 break;
1408 }
1409
1410 sym->flags |= BSF_GLOBAL;
1411
1412 if (! generic_add_output_symbol (wginfo->output_bfd, wginfo->psymalloc,
1413 sym))
1414 {
1415 /* FIXME: No way to return failure. */
1416 abort ();
1417 }
1418
1419 h->root.written = true;
1420
1421 return true;
1422}
da6b2d99
ILT
1423\f
1424/* Allocate a new link_order for a section. */
1425
1426struct bfd_link_order *
1427bfd_new_link_order (abfd, section)
1428 bfd *abfd;
1429 asection *section;
1430{
1431 struct bfd_link_order *new;
1432
1433 new = ((struct bfd_link_order *)
1434 bfd_alloc_by_size_t (abfd, sizeof (struct bfd_link_order)));
1435
1436 new->type = bfd_undefined_link_order;
1437 new->offset = 0;
1438 new->size = 0;
1439 new->next = (struct bfd_link_order *) NULL;
1440
1441 if (section->link_order_tail != (struct bfd_link_order *) NULL)
1442 section->link_order_tail->next = new;
1443 else
1444 section->link_order_head = new;
1445 section->link_order_tail = new;
1446
1447 return new;
1448}
1449
1450/* Default link order processing routine. */
1451
1452boolean
1453_bfd_default_link_order (abfd, info, sec, link_order)
1454 bfd *abfd;
1455 struct bfd_link_info *info;
1456 asection *sec;
1457 struct bfd_link_order *link_order;
1458{
1459 switch (link_order->type)
1460 {
1461 case bfd_undefined_link_order:
1462 default:
1463 abort ();
1464 case bfd_indirect_link_order:
6e07e54f 1465 return default_indirect_link_order (abfd, info, sec, link_order);
da6b2d99
ILT
1466 case bfd_fill_link_order:
1467 return default_fill_link_order (abfd, info, sec, link_order);
1468 }
1469}
1470
1471/* Default routine to handle a bfd_fill_link_order. */
1472
6e07e54f 1473/*ARGSUSED*/
da6b2d99
ILT
1474static boolean
1475default_fill_link_order (abfd, info, sec, link_order)
1476 bfd *abfd;
1477 struct bfd_link_info *info;
1478 asection *sec;
1479 struct bfd_link_order *link_order;
1480{
1481 size_t size;
1482 char *space;
1483 size_t i;
1484 int fill;
1485
1486 BFD_ASSERT ((sec->flags & SEC_HAS_CONTENTS) != 0);
1487
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)
1494 space[i] = fill;
1495 return bfd_set_section_contents (abfd, sec, space,
1496 (file_ptr) link_order->offset,
1497 link_order->size);
1498}
6e07e54f
ILT
1499
1500/* Default routine to handle a bfd_indirect_link_order. */
1501
1502static boolean
1503default_indirect_link_order (output_bfd, info, output_section, link_order)
1504 bfd *output_bfd;
1505 struct bfd_link_info *info;
1506 asection *output_section;
1507 struct bfd_link_order *link_order;
1508{
1509 asection *input_section;
1510 bfd *input_bfd;
1511 bfd_byte *contents;
1512
1513 BFD_ASSERT ((output_section->flags & SEC_HAS_CONTENTS) != 0);
1514
1515 if (link_order->size == 0)
1516 return true;
1517
1518 input_section = link_order->u.indirect.section;
1519 input_bfd = input_section->owner;
1520
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);
1524
1525 if (info->relocateable
1526 && output_section->orelocation == (arelent **) NULL)
1527 {
1528 /* Space has not been allocated for the output relocations.
1529 This can happen when we are called by a specific backend
1530 because somebody is attempting to link together different
1531 types of object files. Handling this case correctly is
1532 difficult, and sometimes impossible. */
1533 abort ();
1534 }
1535
1536 /* Get the canonical symbols. The generic linker will always have
1537 retrieved them by this point, but we may be being called by a
1538 specific linker when linking different types of object files
1539 together. */
1540 if (bfd_get_outsymbols (input_bfd) == (asymbol **) NULL)
1541 {
1542 size_t symsize;
1543
1544 symsize = get_symtab_upper_bound (input_bfd);
1545 input_bfd->outsymbols = (asymbol **) bfd_alloc (input_bfd, symsize);
1546 input_bfd->symcount = bfd_canonicalize_symtab (input_bfd,
1547 input_bfd->outsymbols);
1548 }
1549
1550 /* Get and relocate the section contents. */
1551 contents = (bfd_byte *) alloca (bfd_section_size (input_bfd, input_section));
1552 contents = (bfd_get_relocated_section_contents
1553 (output_bfd, info, link_order, contents, info->relocateable,
1554 bfd_get_outsymbols (input_bfd)));
1555
1556 /* Output the section contents. */
1557 if (! bfd_set_section_contents (output_bfd, output_section, (PTR) contents,
1558 link_order->offset, link_order->size))
1559 return false;
1560
1561 return true;
1562}
This page took 0.080168 seconds and 4 git commands to generate.