1 /* COFF specific linker code.
2 Copyright 1994, 1995 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor, Cygnus Support.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program 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 of the License, or
10 (at your option) any later version.
12 This program 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 this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 /* This file contains the COFF backend linker code. */
27 #include "coff/internal.h"
30 static boolean coff_link_add_object_symbols
31 PARAMS ((bfd *, struct bfd_link_info *));
32 static boolean coff_link_check_archive_element
33 PARAMS ((bfd *, struct bfd_link_info *, boolean *));
34 static boolean coff_link_check_ar_symbols
35 PARAMS ((bfd *, struct bfd_link_info *, boolean *));
36 static boolean coff_link_add_symbols PARAMS ((bfd *, struct bfd_link_info *));
38 /* Create an entry in a COFF linker hash table. */
40 struct bfd_hash_entry *
41 _bfd_coff_link_hash_newfunc (entry, table, string)
42 struct bfd_hash_entry *entry;
43 struct bfd_hash_table *table;
46 struct coff_link_hash_entry *ret = (struct coff_link_hash_entry *) entry;
48 /* Allocate the structure if it has not already been allocated by a
50 if (ret == (struct coff_link_hash_entry *) NULL)
51 ret = ((struct coff_link_hash_entry *)
52 bfd_hash_allocate (table, sizeof (struct coff_link_hash_entry)));
53 if (ret == (struct coff_link_hash_entry *) NULL)
54 return (struct bfd_hash_entry *) ret;
56 /* Call the allocation method of the superclass. */
57 ret = ((struct coff_link_hash_entry *)
58 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
60 if (ret != (struct coff_link_hash_entry *) NULL)
62 /* Set local fields. */
71 return (struct bfd_hash_entry *) ret;
74 /* Initialize a COFF linker hash table. */
77 _bfd_coff_link_hash_table_init (table, abfd, newfunc)
78 struct coff_link_hash_table *table;
80 struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *,
81 struct bfd_hash_table *,
84 return _bfd_link_hash_table_init (&table->root, abfd, newfunc);
87 /* Create a COFF linker hash table. */
89 struct bfd_link_hash_table *
90 _bfd_coff_link_hash_table_create (abfd)
93 struct coff_link_hash_table *ret;
95 ret = ((struct coff_link_hash_table *)
96 bfd_alloc (abfd, sizeof (struct coff_link_hash_table)));
99 if (! _bfd_coff_link_hash_table_init (ret, abfd,
100 _bfd_coff_link_hash_newfunc))
102 bfd_release (abfd, ret);
103 return (struct bfd_link_hash_table *) NULL;
108 /* Create an entry in a COFF debug merge hash table. */
110 struct bfd_hash_entry *
111 coff_debug_merge_hash_newfunc (entry, table, string)
112 struct bfd_hash_entry *entry;
113 struct bfd_hash_table *table;
116 struct coff_debug_merge_hash_entry *ret =
117 (struct coff_debug_merge_hash_entry *) entry;
119 /* Allocate the structure if it has not already been allocated by a
121 if (ret == (struct coff_debug_merge_hash_entry *) NULL)
122 ret = ((struct coff_debug_merge_hash_entry *)
123 bfd_hash_allocate (table,
124 sizeof (struct coff_debug_merge_hash_entry)));
125 if (ret == (struct coff_debug_merge_hash_entry *) NULL)
126 return (struct bfd_hash_entry *) ret;
128 /* Call the allocation method of the superclass. */
129 ret = ((struct coff_debug_merge_hash_entry *)
130 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
131 if (ret != (struct coff_debug_merge_hash_entry *) NULL)
133 /* Set local fields. */
137 return (struct bfd_hash_entry *) ret;
140 /* Given a COFF BFD, add symbols to the global hash table as
144 _bfd_coff_link_add_symbols (abfd, info)
146 struct bfd_link_info *info;
148 switch (bfd_get_format (abfd))
151 return coff_link_add_object_symbols (abfd, info);
153 return (_bfd_generic_link_add_archive_symbols
154 (abfd, info, coff_link_check_archive_element));
156 bfd_set_error (bfd_error_wrong_format);
161 /* Add symbols from a COFF object file. */
164 coff_link_add_object_symbols (abfd, info)
166 struct bfd_link_info *info;
168 if (! _bfd_coff_get_external_symbols (abfd))
170 if (! coff_link_add_symbols (abfd, info))
173 if (! info->keep_memory)
175 if (! _bfd_coff_free_symbols (abfd))
181 /* Check a single archive element to see if we need to include it in
182 the link. *PNEEDED is set according to whether this element is
183 needed in the link or not. This is called via
184 _bfd_generic_link_add_archive_symbols. */
187 coff_link_check_archive_element (abfd, info, pneeded)
189 struct bfd_link_info *info;
192 if (! _bfd_coff_get_external_symbols (abfd))
195 if (! coff_link_check_ar_symbols (abfd, info, pneeded))
200 if (! coff_link_add_symbols (abfd, info))
204 if (! info->keep_memory || ! *pneeded)
206 if (! _bfd_coff_free_symbols (abfd))
213 /* Look through the symbols to see if this object file should be
214 included in the link. */
217 coff_link_check_ar_symbols (abfd, info, pneeded)
219 struct bfd_link_info *info;
222 boolean (*sym_is_global) PARAMS ((bfd *, struct internal_syment *));
223 bfd_size_type symesz;
229 sym_is_global = coff_backend_info (abfd)->_bfd_coff_sym_is_global;
231 symesz = bfd_coff_symesz (abfd);
232 esym = (bfd_byte *) obj_coff_external_syms (abfd);
233 esym_end = esym + obj_raw_syment_count (abfd) * symesz;
234 while (esym < esym_end)
236 struct internal_syment sym;
238 bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym);
240 if ((sym.n_sclass == C_EXT
241 || (sym_is_global && (*sym_is_global) (abfd, &sym)))
242 && (sym.n_scnum != 0 || sym.n_value != 0))
245 char buf[SYMNMLEN + 1];
246 struct bfd_link_hash_entry *h;
248 /* This symbol is externally visible, and is defined by this
251 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
254 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
256 /* We are only interested in symbols that are currently
257 undefined. If a symbol is currently known to be common,
258 COFF linkers do not bring in an object file which defines
260 if (h != (struct bfd_link_hash_entry *) NULL
261 && h->type == bfd_link_hash_undefined)
263 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
270 esym += (sym.n_numaux + 1) * symesz;
273 /* We do not need this object file. */
277 /* Add all the symbols from an object file to the hash table. */
280 coff_link_add_symbols (abfd, info)
282 struct bfd_link_info *info;
284 boolean (*sym_is_global) PARAMS ((bfd *, struct internal_syment *));
285 boolean default_copy;
286 bfd_size_type symcount;
287 struct coff_link_hash_entry **sym_hash;
288 bfd_size_type symesz;
292 sym_is_global = coff_backend_info (abfd)->_bfd_coff_sym_is_global;
294 if (info->keep_memory)
295 default_copy = false;
299 symcount = obj_raw_syment_count (abfd);
301 /* We keep a list of the linker hash table entries that correspond
302 to particular symbols. */
303 sym_hash = ((struct coff_link_hash_entry **)
306 * sizeof (struct coff_link_hash_entry *))));
307 if (sym_hash == NULL && symcount != 0)
309 obj_coff_sym_hashes (abfd) = sym_hash;
311 (size_t) symcount * sizeof (struct coff_link_hash_entry *));
313 symesz = bfd_coff_symesz (abfd);
314 BFD_ASSERT (symesz == bfd_coff_auxesz (abfd));
315 esym = (bfd_byte *) obj_coff_external_syms (abfd);
316 esym_end = esym + symcount * symesz;
317 while (esym < esym_end)
319 struct internal_syment sym;
322 bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym);
324 if (sym.n_sclass == C_EXT
325 || (sym_is_global && (*sym_is_global) (abfd, &sym)))
328 char buf[SYMNMLEN + 1];
333 /* This symbol is externally visible. */
335 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
339 /* We must copy the name into memory if we got it from the
340 syment itself, rather than the string table. */
342 if (sym._n._n_n._n_zeroes != 0
343 || sym._n._n_n._n_offset == 0)
348 if (sym.n_scnum == 0)
353 section = bfd_und_section_ptr;
358 section = bfd_com_section_ptr;
363 flags = BSF_EXPORT | BSF_GLOBAL;
364 section = coff_section_from_bfd_index (abfd, sym.n_scnum);
365 value -= section->vma;
368 if (! (_bfd_generic_link_add_one_symbol
369 (info, abfd, name, flags, section, value,
370 (const char *) NULL, copy, false,
371 (struct bfd_link_hash_entry **) sym_hash)))
374 if (info->hash->creator->flavour == bfd_get_flavour (abfd))
376 if (((*sym_hash)->class == C_NULL
377 && (*sym_hash)->type == T_NULL)
380 && (*sym_hash)->root.type != bfd_link_hash_defined))
382 (*sym_hash)->class = sym.n_sclass;
383 (*sym_hash)->type = sym.n_type;
384 (*sym_hash)->numaux = sym.n_numaux;
385 (*sym_hash)->auxbfd = abfd;
386 if (sym.n_numaux != 0)
388 union internal_auxent *alloc;
391 union internal_auxent *iaux;
393 alloc = ((union internal_auxent *)
394 bfd_hash_allocate (&info->hash->table,
396 * sizeof (*alloc))));
399 for (i = 0, eaux = esym + symesz, iaux = alloc;
401 i++, eaux += symesz, iaux++)
402 bfd_coff_swap_aux_in (abfd, (PTR) eaux, sym.n_type,
403 sym.n_sclass, i, sym.n_numaux,
405 (*sym_hash)->aux = alloc;
411 esym += (sym.n_numaux + 1) * symesz;
412 sym_hash += sym.n_numaux + 1;
418 /* Do the final link step. */
421 _bfd_coff_final_link (abfd, info)
423 struct bfd_link_info *info;
425 bfd_size_type symesz;
426 struct coff_final_link_info finfo;
427 boolean debug_merge_allocated;
429 struct bfd_link_order *p;
430 size_t max_sym_count;
431 size_t max_lineno_count;
432 size_t max_reloc_count;
433 size_t max_output_reloc_count;
434 size_t max_contents_size;
435 file_ptr rel_filepos;
437 file_ptr line_filepos;
440 bfd_byte *external_relocs = NULL;
441 char strbuf[STRING_SIZE_SIZE];
443 symesz = bfd_coff_symesz (abfd);
446 finfo.output_bfd = abfd;
448 finfo.section_info = NULL;
449 finfo.last_file_index = -1;
450 finfo.internal_syms = NULL;
451 finfo.sec_ptrs = NULL;
452 finfo.sym_indices = NULL;
453 finfo.outsyms = NULL;
454 finfo.linenos = NULL;
455 finfo.contents = NULL;
456 finfo.external_relocs = NULL;
457 finfo.internal_relocs = NULL;
458 debug_merge_allocated = false;
460 coff_data (abfd)->link_info = info;
462 finfo.strtab = _bfd_stringtab_init ();
463 if (finfo.strtab == NULL)
466 if (! coff_debug_merge_hash_table_init (&finfo.debug_merge))
468 debug_merge_allocated = true;
470 /* Compute the file positions for all the sections. */
471 if (! abfd->output_has_begun)
472 bfd_coff_compute_section_file_positions (abfd);
474 /* Count the line numbers and relocation entries required for the
475 output file. Set the file positions for the relocs. */
476 rel_filepos = obj_relocbase (abfd);
477 relsz = bfd_coff_relsz (abfd);
478 max_contents_size = 0;
479 max_lineno_count = 0;
482 for (o = abfd->sections; o != NULL; o = o->next)
486 for (p = o->link_order_head; p != NULL; p = p->next)
489 if (p->type == bfd_indirect_link_order)
493 sec = p->u.indirect.section;
495 if (info->strip == strip_none
496 || info->strip == strip_some)
497 o->lineno_count += sec->lineno_count;
499 if (info->relocateable)
500 o->reloc_count += sec->reloc_count;
502 if (sec->_raw_size > max_contents_size)
503 max_contents_size = sec->_raw_size;
504 if (sec->lineno_count > max_lineno_count)
505 max_lineno_count = sec->lineno_count;
506 if (sec->reloc_count > max_reloc_count)
507 max_reloc_count = sec->reloc_count;
509 else if (info->relocateable
510 && (p->type == bfd_section_reloc_link_order
511 || p->type == bfd_symbol_reloc_link_order))
514 if (o->reloc_count == 0)
518 o->flags |= SEC_RELOC;
519 o->rel_filepos = rel_filepos;
520 rel_filepos += o->reloc_count * relsz;
524 /* If doing a relocateable link, allocate space for the pointers we
526 if (info->relocateable)
530 /* We use section_count + 1, rather than section_count, because
531 the target_index fields are 1 based. */
533 ((struct coff_link_section_info *)
534 bfd_malloc ((abfd->section_count + 1)
535 * sizeof (struct coff_link_section_info)));
536 if (finfo.section_info == NULL)
538 for (i = 0; i <= abfd->section_count; i++)
540 finfo.section_info[i].relocs = NULL;
541 finfo.section_info[i].rel_hashes = NULL;
545 /* We now know the size of the relocs, so we can determine the file
546 positions of the line numbers. */
547 line_filepos = rel_filepos;
548 linesz = bfd_coff_linesz (abfd);
549 max_output_reloc_count = 0;
550 for (o = abfd->sections; o != NULL; o = o->next)
552 if (o->lineno_count == 0)
556 o->line_filepos = line_filepos;
557 line_filepos += o->lineno_count * linesz;
560 if (o->reloc_count != 0)
562 /* We don't know the indices of global symbols until we have
563 written out all the local symbols. For each section in
564 the output file, we keep an array of pointers to hash
565 table entries. Each entry in the array corresponds to a
566 reloc. When we find a reloc against a global symbol, we
567 set the corresponding entry in this array so that we can
568 fix up the symbol index after we have written out all the
571 Because of this problem, we also keep the relocs in
572 memory until the end of the link. This wastes memory,
573 but only when doing a relocateable link, which is not the
575 BFD_ASSERT (info->relocateable);
576 finfo.section_info[o->target_index].relocs =
577 ((struct internal_reloc *)
578 bfd_malloc (o->reloc_count * sizeof (struct internal_reloc)));
579 finfo.section_info[o->target_index].rel_hashes =
580 ((struct coff_link_hash_entry **)
581 bfd_malloc (o->reloc_count
582 * sizeof (struct coff_link_hash_entry *)));
583 if (finfo.section_info[o->target_index].relocs == NULL
584 || finfo.section_info[o->target_index].rel_hashes == NULL)
587 if (o->reloc_count > max_output_reloc_count)
588 max_output_reloc_count = o->reloc_count;
591 /* Reset the reloc and lineno counts, so that we can use them to
592 count the number of entries we have output so far. */
597 obj_sym_filepos (abfd) = line_filepos;
599 /* Figure out the largest number of symbols in an input BFD. Take
600 the opportunity to clear the output_has_begun fields of all the
603 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
607 sub->output_has_begun = false;
608 sz = obj_raw_syment_count (sub);
609 if (sz > max_sym_count)
613 /* Allocate some buffers used while linking. */
614 finfo.internal_syms = ((struct internal_syment *)
615 bfd_malloc (max_sym_count
616 * sizeof (struct internal_syment)));
617 finfo.sec_ptrs = (asection **) bfd_malloc (max_sym_count
618 * sizeof (asection *));
619 finfo.sym_indices = (long *) bfd_malloc (max_sym_count * sizeof (long));
620 finfo.outsyms = ((bfd_byte *)
621 bfd_malloc ((size_t) ((max_sym_count + 1) * symesz)));
622 finfo.linenos = (bfd_byte *) bfd_malloc (max_lineno_count
623 * bfd_coff_linesz (abfd));
624 finfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
625 finfo.external_relocs = (bfd_byte *) bfd_malloc (max_reloc_count * relsz);
626 if (! info->relocateable)
627 finfo.internal_relocs = ((struct internal_reloc *)
628 bfd_malloc (max_reloc_count
629 * sizeof (struct internal_reloc)));
630 if ((finfo.internal_syms == NULL && max_sym_count > 0)
631 || (finfo.sec_ptrs == NULL && max_sym_count > 0)
632 || (finfo.sym_indices == NULL && max_sym_count > 0)
633 || finfo.outsyms == NULL
634 || (finfo.linenos == NULL && max_lineno_count > 0)
635 || (finfo.contents == NULL && max_contents_size > 0)
636 || (finfo.external_relocs == NULL && max_reloc_count > 0)
637 || (! info->relocateable
638 && finfo.internal_relocs == NULL
639 && max_reloc_count > 0))
642 /* We now know the position of everything in the file, except that
643 we don't know the size of the symbol table and therefore we don't
644 know where the string table starts. We just build the string
645 table in memory as we go along. We process all the relocations
646 for a single input file at once. */
647 obj_raw_syment_count (abfd) = 0;
649 if (coff_backend_info (abfd)->_bfd_coff_start_final_link)
651 if (! bfd_coff_start_final_link (abfd, info))
655 for (o = abfd->sections; o != NULL; o = o->next)
657 for (p = o->link_order_head; p != NULL; p = p->next)
659 if (p->type == bfd_indirect_link_order
660 && (bfd_get_flavour (p->u.indirect.section->owner)
661 == bfd_target_coff_flavour))
663 sub = p->u.indirect.section->owner;
664 if (! sub->output_has_begun)
666 if (! coff_link_input_bfd (&finfo, sub))
668 sub->output_has_begun = true;
671 else if (p->type == bfd_section_reloc_link_order
672 || p->type == bfd_symbol_reloc_link_order)
674 if (! coff_reloc_link_order (abfd, &finfo, o, p))
679 if (! _bfd_default_link_order (abfd, info, o, p))
685 /* Free up the buffers used by coff_link_input_bfd. */
687 coff_debug_merge_hash_table_free (&finfo.debug_merge);
688 debug_merge_allocated = false;
690 if (finfo.internal_syms != NULL)
692 free (finfo.internal_syms);
693 finfo.internal_syms = NULL;
695 if (finfo.sec_ptrs != NULL)
697 free (finfo.sec_ptrs);
698 finfo.sec_ptrs = NULL;
700 if (finfo.sym_indices != NULL)
702 free (finfo.sym_indices);
703 finfo.sym_indices = NULL;
705 if (finfo.linenos != NULL)
707 free (finfo.linenos);
708 finfo.linenos = NULL;
710 if (finfo.contents != NULL)
712 free (finfo.contents);
713 finfo.contents = NULL;
715 if (finfo.external_relocs != NULL)
717 free (finfo.external_relocs);
718 finfo.external_relocs = NULL;
720 if (finfo.internal_relocs != NULL)
722 free (finfo.internal_relocs);
723 finfo.internal_relocs = NULL;
726 /* The value of the last C_FILE symbol is supposed to be the symbol
727 index of the first external symbol. Write it out again if
729 if (finfo.last_file_index != -1
730 && (unsigned int) finfo.last_file.n_value != obj_raw_syment_count (abfd))
732 finfo.last_file.n_value = obj_raw_syment_count (abfd);
733 bfd_coff_swap_sym_out (abfd, (PTR) &finfo.last_file,
734 (PTR) finfo.outsyms);
736 (obj_sym_filepos (abfd)
737 + finfo.last_file_index * symesz),
739 || bfd_write (finfo.outsyms, symesz, 1, abfd) != symesz)
743 /* Write out the global symbols. */
744 finfo.failed = false;
745 coff_link_hash_traverse (coff_hash_table (info), coff_write_global_sym,
750 /* The outsyms buffer is used by coff_write_global_sym. */
751 if (finfo.outsyms != NULL)
753 free (finfo.outsyms);
754 finfo.outsyms = NULL;
757 if (info->relocateable)
759 /* Now that we have written out all the global symbols, we know
760 the symbol indices to use for relocs against them, and we can
761 finally write out the relocs. */
762 external_relocs = ((bfd_byte *)
763 bfd_malloc (max_output_reloc_count * relsz));
764 if (external_relocs == NULL)
767 for (o = abfd->sections; o != NULL; o = o->next)
769 struct internal_reloc *irel;
770 struct internal_reloc *irelend;
771 struct coff_link_hash_entry **rel_hash;
774 if (o->reloc_count == 0)
777 irel = finfo.section_info[o->target_index].relocs;
778 irelend = irel + o->reloc_count;
779 rel_hash = finfo.section_info[o->target_index].rel_hashes;
780 erel = external_relocs;
781 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
783 if (*rel_hash != NULL)
785 BFD_ASSERT ((*rel_hash)->indx >= 0);
786 irel->r_symndx = (*rel_hash)->indx;
788 bfd_coff_swap_reloc_out (abfd, (PTR) irel, (PTR) erel);
791 if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0
792 || bfd_write ((PTR) external_relocs, relsz, o->reloc_count,
793 abfd) != relsz * o->reloc_count)
797 free (external_relocs);
798 external_relocs = NULL;
801 /* Free up the section information. */
802 if (finfo.section_info != NULL)
806 for (i = 0; i < abfd->section_count; i++)
808 if (finfo.section_info[i].relocs != NULL)
809 free (finfo.section_info[i].relocs);
810 if (finfo.section_info[i].rel_hashes != NULL)
811 free (finfo.section_info[i].rel_hashes);
813 free (finfo.section_info);
814 finfo.section_info = NULL;
817 /* Write out the string table. */
818 if (obj_raw_syment_count (abfd) != 0)
821 (obj_sym_filepos (abfd)
822 + obj_raw_syment_count (abfd) * symesz),
826 #if STRING_SIZE_SIZE == 4
828 _bfd_stringtab_size (finfo.strtab) + STRING_SIZE_SIZE,
829 (bfd_byte *) strbuf);
831 #error Change bfd_h_put_32
834 if (bfd_write (strbuf, 1, STRING_SIZE_SIZE, abfd) != STRING_SIZE_SIZE)
837 if (! _bfd_stringtab_emit (abfd, finfo.strtab))
841 _bfd_stringtab_free (finfo.strtab);
843 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
844 not try to write out the symbols. */
845 bfd_get_symcount (abfd) = 0;
850 if (debug_merge_allocated)
851 coff_debug_merge_hash_table_free (&finfo.debug_merge);
852 if (finfo.strtab != NULL)
853 _bfd_stringtab_free (finfo.strtab);
854 if (finfo.section_info != NULL)
858 for (i = 0; i < abfd->section_count; i++)
860 if (finfo.section_info[i].relocs != NULL)
861 free (finfo.section_info[i].relocs);
862 if (finfo.section_info[i].rel_hashes != NULL)
863 free (finfo.section_info[i].rel_hashes);
865 free (finfo.section_info);
867 if (finfo.internal_syms != NULL)
868 free (finfo.internal_syms);
869 if (finfo.sec_ptrs != NULL)
870 free (finfo.sec_ptrs);
871 if (finfo.sym_indices != NULL)
872 free (finfo.sym_indices);
873 if (finfo.outsyms != NULL)
874 free (finfo.outsyms);
875 if (finfo.linenos != NULL)
876 free (finfo.linenos);
877 if (finfo.contents != NULL)
878 free (finfo.contents);
879 if (finfo.external_relocs != NULL)
880 free (finfo.external_relocs);
881 if (finfo.internal_relocs != NULL)
882 free (finfo.internal_relocs);
883 if (external_relocs != NULL)
884 free (external_relocs);
888 /* parse out a -heap <reserved>,<commit> line */
891 dores_com (ptr, output_bfd, heap)
896 if (coff_data(output_bfd)->pe)
898 int val = strtoul (ptr, &ptr, 0);
900 pe_data(output_bfd)->pe_opthdr.SizeOfHeapReserve =val;
902 pe_data(output_bfd)->pe_opthdr.SizeOfStackReserve =val;
906 int val = strtoul (ptr+1, &ptr, 0);
908 pe_data(output_bfd)->pe_opthdr.SizeOfHeapCommit =val;
910 pe_data(output_bfd)->pe_opthdr.SizeOfStackCommit =val;
916 static char *get_name(ptr, dst)
923 while (*ptr && *ptr != ' ')
929 /* Process any magic embedded commands in a section called .drectve */
932 process_embedded_commands (output_bfd, info, abfd)
934 struct bfd_link_info *info;
937 asection *sec = bfd_get_section_by_name (abfd, ".drectve");
944 copy = bfd_malloc ((size_t) sec->_raw_size);
947 if (! bfd_get_section_contents(abfd, sec, copy, 0, sec->_raw_size))
952 e = copy + sec->_raw_size;
953 for (s = copy; s < e ; )
959 if (strncmp (s,"-attr", 5) == 0)
971 s = get_name(s, &name);
972 s = get_name(s, &attribs);
992 asec = bfd_get_section_by_name (abfd, name);
995 asec->flags |= SEC_CODE;
997 asec->flags |= SEC_READONLY;
1000 else if (strncmp (s,"-heap", 5) == 0)
1002 s = dores_com (s+5, output_bfd, 1);
1004 else if (strncmp (s,"-stack", 6) == 0)
1006 s = dores_com (s+6, output_bfd, 0);
1015 /* Link an input file into the linker output file. This function
1016 handles all the sections and relocations of the input file at once. */
1019 coff_link_input_bfd (finfo, input_bfd)
1020 struct coff_final_link_info *finfo;
1023 boolean (*sym_is_global) PARAMS ((bfd *, struct internal_syment *));
1024 boolean (*adjust_symndx) PARAMS ((bfd *, struct bfd_link_info *, bfd *,
1025 asection *, struct internal_reloc *,
1028 const char *strings;
1029 bfd_size_type syment_base;
1030 unsigned int n_tmask;
1031 unsigned int n_btshft;
1033 bfd_size_type isymesz;
1034 bfd_size_type osymesz;
1035 bfd_size_type linesz;
1038 struct internal_syment *isymp;
1041 unsigned long output_index;
1043 struct coff_link_hash_entry **sym_hash;
1046 /* Move all the symbols to the output file. */
1048 output_bfd = finfo->output_bfd;
1049 sym_is_global = coff_backend_info (input_bfd)->_bfd_coff_sym_is_global;
1051 syment_base = obj_raw_syment_count (output_bfd);
1052 isymesz = bfd_coff_symesz (input_bfd);
1053 osymesz = bfd_coff_symesz (output_bfd);
1054 linesz = bfd_coff_linesz (input_bfd);
1055 BFD_ASSERT (linesz == bfd_coff_linesz (output_bfd));
1057 n_tmask = coff_data (input_bfd)->local_n_tmask;
1058 n_btshft = coff_data (input_bfd)->local_n_btshft;
1060 /* Define macros so that ISFCN, et. al., macros work correctly. */
1061 #define N_TMASK n_tmask
1062 #define N_BTSHFT n_btshft
1065 if (! finfo->info->keep_memory)
1068 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
1071 if (! _bfd_coff_get_external_symbols (input_bfd))
1074 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
1075 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
1076 isymp = finfo->internal_syms;
1077 secpp = finfo->sec_ptrs;
1078 indexp = finfo->sym_indices;
1079 output_index = syment_base;
1080 outsym = finfo->outsyms;
1082 if (coff_data(output_bfd)->pe)
1084 if (!process_embedded_commands (output_bfd, finfo->info, input_bfd))
1088 while (esym < esym_end)
1090 struct internal_syment isym;
1095 bfd_coff_swap_sym_in (input_bfd, (PTR) esym, (PTR) isymp);
1097 /* Make a copy of *isymp so that the relocate_section function
1098 always sees the original values. This is more reliable than
1099 always recomputing the symbol value even if we are stripping
1103 if (isym.n_scnum != 0)
1104 *secpp = coff_section_from_bfd_index (input_bfd, isym.n_scnum);
1107 if (isym.n_value == 0)
1108 *secpp = bfd_und_section_ptr;
1110 *secpp = bfd_com_section_ptr;
1117 add = 1 + isym.n_numaux;
1119 /* If we are stripping all symbols, we want to skip this one. */
1120 if (finfo->info->strip == strip_all)
1125 if (isym.n_sclass == C_EXT
1126 || (sym_is_global && (*sym_is_global) (input_bfd, &isym)))
1128 /* This is a global symbol. Global symbols come at the
1129 end of the symbol table, so skip them for now.
1130 Function symbols, however, are an exception, and are
1131 not moved to the end. */
1133 if (! ISFCN (isym.n_type))
1138 /* This is a local symbol. Skip it if we are discarding
1140 if (finfo->info->discard == discard_all)
1145 /* If we stripping debugging symbols, and this is a debugging
1146 symbol, then skip it. */
1148 && finfo->info->strip == strip_debugger
1149 && isym.n_scnum == N_DEBUG)
1152 /* If some symbols are stripped based on the name, work out the
1153 name and decide whether to skip this symbol. */
1155 && (finfo->info->strip == strip_some
1156 || finfo->info->discard == discard_l))
1159 char buf[SYMNMLEN + 1];
1161 name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf);
1165 if ((finfo->info->strip == strip_some
1166 && (bfd_hash_lookup (finfo->info->keep_hash, name, false,
1169 && finfo->info->discard == discard_l
1170 && strncmp (name, finfo->info->lprefix,
1171 finfo->info->lprefix_len) == 0))
1175 /* If this is an enum, struct, or union tag, see if we have
1176 already output an identical type. */
1178 && (finfo->output_bfd->flags & BFD_TRADITIONAL_FORMAT) == 0
1179 && (isym.n_sclass == C_ENTAG
1180 || isym.n_sclass == C_STRTAG
1181 || isym.n_sclass == C_UNTAG)
1182 && isym.n_numaux == 1)
1185 char buf[SYMNMLEN + 1];
1186 struct coff_debug_merge_hash_entry *mh;
1187 struct coff_debug_merge_type *mt;
1188 union internal_auxent aux;
1189 struct coff_debug_merge_element **epp;
1190 bfd_byte *esl, *eslend;
1191 struct internal_syment *islp;
1192 struct coff_debug_merge_type *mtl;
1194 name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf);
1198 /* Ignore fake names invented by compiler; treat them all as
1200 if (*name == '~' || *name == '.'
1201 || (*name == bfd_get_symbol_leading_char (input_bfd)
1202 && (name[1] == '~' || name[1] == '.')))
1205 mh = coff_debug_merge_hash_lookup (&finfo->debug_merge, name,
1210 /* Allocate memory to hold type information. If this turns
1211 out to be a duplicate, we pass this address to
1213 mt = ((struct coff_debug_merge_type *)
1214 bfd_alloc (input_bfd,
1215 sizeof (struct coff_debug_merge_type)));
1218 mt->class = isym.n_sclass;
1220 /* Pick up the aux entry, which points to the end of the tag
1222 bfd_coff_swap_aux_in (input_bfd, (PTR) (esym + isymesz),
1223 isym.n_type, isym.n_sclass, 0, isym.n_numaux,
1226 /* Gather the elements. */
1227 epp = &mt->elements;
1228 mt->elements = NULL;
1230 esl = esym + 2 * isymesz;
1231 eslend = ((bfd_byte *) obj_coff_external_syms (input_bfd)
1232 + aux.x_sym.x_fcnary.x_fcn.x_endndx.l * isymesz);
1233 while (esl < eslend)
1235 const char *elename;
1236 char elebuf[SYMNMLEN + 1];
1239 bfd_coff_swap_sym_in (input_bfd, (PTR) esl, (PTR) islp);
1241 *epp = ((struct coff_debug_merge_element *)
1242 bfd_alloc (input_bfd,
1243 sizeof (struct coff_debug_merge_element)));
1247 elename = _bfd_coff_internal_syment_name (input_bfd, islp,
1249 if (elename == NULL)
1252 copy = (char *) bfd_alloc (input_bfd, strlen (elename) + 1);
1255 strcpy (copy, elename);
1257 (*epp)->name = copy;
1258 (*epp)->type = islp->n_type;
1260 if (islp->n_numaux >= 1
1261 && islp->n_type != T_NULL
1262 && islp->n_sclass != C_EOS)
1264 union internal_auxent eleaux;
1267 bfd_coff_swap_aux_in (input_bfd, (PTR) (esl + isymesz),
1268 islp->n_type, islp->n_sclass, 0,
1269 islp->n_numaux, (PTR) &eleaux);
1270 indx = eleaux.x_sym.x_tagndx.l;
1272 /* FIXME: If this tagndx entry refers to a symbol
1273 defined later in this file, we just ignore it.
1274 Handling this correctly would be tedious, and may
1280 (bfd_byte *) obj_coff_external_syms (input_bfd))
1283 (*epp)->tagndx = finfo->sym_indices[indx];
1284 if ((*epp)->tagndx < 0)
1288 epp = &(*epp)->next;
1291 esl += (islp->n_numaux + 1) * isymesz;
1292 islp += islp->n_numaux + 1;
1295 /* See if we already have a definition which matches this
1297 for (mtl = mh->types; mtl != NULL; mtl = mtl->next)
1299 struct coff_debug_merge_element *me, *mel;
1301 if (mtl->class != mt->class)
1304 for (me = mt->elements, mel = mtl->elements;
1305 me != NULL && mel != NULL;
1306 me = me->next, mel = mel->next)
1308 if (strcmp (me->name, mel->name) != 0
1309 || me->type != mel->type
1310 || me->tagndx != mel->tagndx)
1314 if (me == NULL && mel == NULL)
1318 if (mtl == NULL || (bfd_size_type) mtl->indx >= syment_base)
1320 /* This is the first definition of this type. */
1321 mt->indx = output_index;
1322 mt->next = mh->types;
1327 /* This is a redefinition which can be merged. */
1328 bfd_release (input_bfd, (PTR) mt);
1329 *indexp = mtl->indx;
1330 add = (eslend - esym) / isymesz;
1335 /* We now know whether we are to skip this symbol or not. */
1338 /* Adjust the symbol in order to output it. */
1340 if (isym._n._n_n._n_zeroes == 0
1341 && isym._n._n_n._n_offset != 0)
1346 /* This symbol has a long name. Enter it in the string
1347 table we are building. Note that we do not check
1348 bfd_coff_symname_in_debug. That is only true for
1349 XCOFF, and XCOFF requires different linking code
1351 name = _bfd_coff_internal_syment_name (input_bfd, &isym,
1355 indx = _bfd_stringtab_add (finfo->strtab, name, hash, copy);
1356 if (indx == (bfd_size_type) -1)
1358 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
1361 if (isym.n_scnum > 0)
1363 isym.n_scnum = (*secpp)->output_section->target_index;
1364 isym.n_value += ((*secpp)->output_section->vma
1365 + (*secpp)->output_offset
1369 /* The value of a C_FILE symbol is the symbol index of the
1370 next C_FILE symbol. The value of the last C_FILE symbol
1371 is the symbol index to the first external symbol
1372 (actually, coff_renumber_symbols does not get this
1373 right--it just sets the value of the last C_FILE symbol
1374 to zero--and nobody has ever complained about it). We
1375 try to get this right, below, just before we write the
1376 symbols out, but in the general case we may have to write
1377 the symbol out twice. */
1378 if (isym.n_sclass == C_FILE)
1380 if (finfo->last_file_index != -1
1381 && finfo->last_file.n_value != (long) output_index)
1383 /* We must correct the value of the last C_FILE entry. */
1384 finfo->last_file.n_value = output_index;
1385 if ((bfd_size_type) finfo->last_file_index >= syment_base)
1387 /* The last C_FILE symbol is in this input file. */
1388 bfd_coff_swap_sym_out (output_bfd,
1389 (PTR) &finfo->last_file,
1390 (PTR) (finfo->outsyms
1391 + ((finfo->last_file_index
1397 /* We have already written out the last C_FILE
1398 symbol. We need to write it out again. We
1399 borrow *outsym temporarily. */
1400 bfd_coff_swap_sym_out (output_bfd,
1401 (PTR) &finfo->last_file,
1403 if (bfd_seek (output_bfd,
1404 (obj_sym_filepos (output_bfd)
1405 + finfo->last_file_index * osymesz),
1407 || (bfd_write (outsym, osymesz, 1, output_bfd)
1413 finfo->last_file_index = output_index;
1414 finfo->last_file = isym;
1417 /* Output the symbol. */
1419 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
1421 *indexp = output_index;
1426 struct coff_link_hash_entry *h;
1428 indx = ((esym - (bfd_byte *) obj_coff_external_syms (input_bfd))
1430 h = obj_coff_sym_hashes (input_bfd)[indx];
1431 BFD_ASSERT (h != NULL);
1432 h->indx = output_index;
1435 output_index += add;
1436 outsym += add * osymesz;
1439 esym += add * isymesz;
1443 for (--add; add > 0; --add)
1450 /* Fix up the aux entries. This must be done in a separate pass,
1451 because we don't know the correct symbol indices until we have
1452 already decided which symbols we are going to keep. */
1454 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
1455 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
1456 isymp = finfo->internal_syms;
1457 indexp = finfo->sym_indices;
1458 sym_hash = obj_coff_sym_hashes (input_bfd);
1459 outsym = finfo->outsyms;
1460 while (esym < esym_end)
1464 add = 1 + isymp->n_numaux;
1467 || (bfd_size_type) *indexp < syment_base)
1468 && (*sym_hash == NULL
1469 || (*sym_hash)->auxbfd != input_bfd))
1470 esym += add * isymesz;
1473 struct coff_link_hash_entry *h;
1480 BFD_ASSERT (h->numaux == isymp->n_numaux);
1488 /* Handle the aux entries. This handling is based on
1489 coff_pointerize_aux. I don't know if it always correct. */
1490 for (i = 0; i < isymp->n_numaux && esym < esym_end; i++)
1492 union internal_auxent aux;
1493 union internal_auxent *auxp;
1499 bfd_coff_swap_aux_in (input_bfd, (PTR) esym, isymp->n_type,
1500 isymp->n_sclass, i, isymp->n_numaux,
1505 if (isymp->n_sclass == C_FILE)
1507 /* If this is a long filename, we must put it in the
1509 if (auxp->x_file.x_n.x_zeroes == 0
1510 && auxp->x_file.x_n.x_offset != 0)
1512 const char *filename;
1515 BFD_ASSERT (auxp->x_file.x_n.x_offset
1516 >= STRING_SIZE_SIZE);
1517 if (strings == NULL)
1519 strings = _bfd_coff_read_string_table (input_bfd);
1520 if (strings == NULL)
1523 filename = strings + auxp->x_file.x_n.x_offset;
1524 indx = _bfd_stringtab_add (finfo->strtab, filename,
1526 if (indx == (bfd_size_type) -1)
1528 auxp->x_file.x_n.x_offset = STRING_SIZE_SIZE + indx;
1531 else if (isymp->n_sclass != C_STAT || isymp->n_type != T_NULL)
1535 if (ISFCN (isymp->n_type)
1536 || ISTAG (isymp->n_sclass)
1537 || isymp->n_sclass == C_BLOCK)
1539 indx = auxp->x_sym.x_fcnary.x_fcn.x_endndx.l;
1541 && indx < obj_raw_syment_count (input_bfd))
1543 /* We look forward through the symbol for
1544 the index of the next symbol we are going
1545 to include. I don't know if this is
1547 while (finfo->sym_indices[indx] < 0
1548 && indx < obj_raw_syment_count (input_bfd))
1550 if (indx >= obj_raw_syment_count (input_bfd))
1551 indx = output_index;
1553 indx = finfo->sym_indices[indx];
1554 auxp->x_sym.x_fcnary.x_fcn.x_endndx.l = indx;
1558 indx = auxp->x_sym.x_tagndx.l;
1559 if (indx > 0 && indx < obj_raw_syment_count (input_bfd))
1563 symindx = finfo->sym_indices[indx];
1565 auxp->x_sym.x_tagndx.l = 0;
1567 auxp->x_sym.x_tagndx.l = symindx;
1573 bfd_coff_swap_aux_out (output_bfd, (PTR) auxp, isymp->n_type,
1574 isymp->n_sclass, i, isymp->n_numaux,
1588 /* Relocate the line numbers, unless we are stripping them. */
1589 if (finfo->info->strip == strip_none
1590 || finfo->info->strip == strip_some)
1592 for (o = input_bfd->sections; o != NULL; o = o->next)
1598 /* FIXME: If SEC_HAS_CONTENTS is not for the section, then
1599 build_link_order in ldwrite.c will not have created a
1600 link order, which means that we will not have seen this
1601 input section in _bfd_coff_final_link, which means that
1602 we will not have allocated space for the line numbers of
1603 this section. I don't think line numbers can be
1604 meaningful for a section which does not have
1605 SEC_HAS_CONTENTS set, but, if they do, this must be
1607 if (o->lineno_count == 0
1608 || (o->output_section->flags & SEC_HAS_CONTENTS) == 0)
1611 if (bfd_seek (input_bfd, o->line_filepos, SEEK_SET) != 0
1612 || bfd_read (finfo->linenos, linesz, o->lineno_count,
1613 input_bfd) != linesz * o->lineno_count)
1616 offset = o->output_section->vma + o->output_offset - o->vma;
1617 eline = finfo->linenos;
1618 elineend = eline + linesz * o->lineno_count;
1619 for (; eline < elineend; eline += linesz)
1621 struct internal_lineno iline;
1623 bfd_coff_swap_lineno_in (input_bfd, (PTR) eline, (PTR) &iline);
1625 if (iline.l_lnno != 0)
1626 iline.l_addr.l_paddr += offset;
1627 else if (iline.l_addr.l_symndx >= 0
1628 && ((unsigned long) iline.l_addr.l_symndx
1629 < obj_raw_syment_count (input_bfd)))
1633 indx = finfo->sym_indices[iline.l_addr.l_symndx];
1637 /* These line numbers are attached to a symbol
1638 which we are stripping. We should really
1639 just discard the line numbers, but that would
1640 be a pain because we have already counted
1646 struct internal_syment is;
1647 union internal_auxent ia;
1649 /* Fix up the lnnoptr field in the aux entry of
1650 the symbol. It turns out that we can't do
1651 this when we modify the symbol aux entries,
1652 because gas sometimes screws up the lnnoptr
1653 field and makes it an offset from the start
1654 of the line numbers rather than an absolute
1656 bfd_coff_swap_sym_in (output_bfd,
1657 (PTR) (finfo->outsyms
1658 + ((indx - syment_base)
1661 if ((ISFCN (is.n_type)
1662 || is.n_sclass == C_BLOCK)
1663 && is.n_numaux >= 1)
1667 auxptr = (PTR) (finfo->outsyms
1668 + ((indx - syment_base + 1)
1670 bfd_coff_swap_aux_in (output_bfd, auxptr,
1671 is.n_type, is.n_sclass,
1672 0, is.n_numaux, (PTR) &ia);
1673 ia.x_sym.x_fcnary.x_fcn.x_lnnoptr =
1674 (o->output_section->line_filepos
1675 + o->output_section->lineno_count * linesz
1676 + eline - finfo->linenos);
1677 bfd_coff_swap_aux_out (output_bfd, (PTR) &ia,
1678 is.n_type, is.n_sclass, 0,
1679 is.n_numaux, auxptr);
1683 iline.l_addr.l_symndx = indx;
1686 bfd_coff_swap_lineno_out (output_bfd, (PTR) &iline, (PTR) eline);
1689 if (bfd_seek (output_bfd,
1690 (o->output_section->line_filepos
1691 + o->output_section->lineno_count * linesz),
1693 || bfd_write (finfo->linenos, linesz, o->lineno_count,
1694 output_bfd) != linesz * o->lineno_count)
1697 o->output_section->lineno_count += o->lineno_count;
1701 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
1702 symbol will be the first symbol in the next input file. In the
1703 normal case, this will save us from writing out the C_FILE symbol
1705 if (finfo->last_file_index != -1
1706 && (bfd_size_type) finfo->last_file_index >= syment_base)
1708 finfo->last_file.n_value = output_index;
1709 bfd_coff_swap_sym_out (output_bfd, (PTR) &finfo->last_file,
1710 (PTR) (finfo->outsyms
1711 + ((finfo->last_file_index - syment_base)
1715 /* Write the modified symbols to the output file. */
1716 if (outsym > finfo->outsyms)
1718 if (bfd_seek (output_bfd,
1719 obj_sym_filepos (output_bfd) + syment_base * osymesz,
1721 || (bfd_write (finfo->outsyms, outsym - finfo->outsyms, 1,
1723 != (bfd_size_type) (outsym - finfo->outsyms)))
1726 BFD_ASSERT ((obj_raw_syment_count (output_bfd)
1727 + (outsym - finfo->outsyms) / osymesz)
1730 obj_raw_syment_count (output_bfd) = output_index;
1733 /* Relocate the contents of each section. */
1734 adjust_symndx = coff_backend_info (input_bfd)->_bfd_coff_adjust_symndx;
1735 for (o = input_bfd->sections; o != NULL; o = o->next)
1739 if ((o->flags & SEC_HAS_CONTENTS) == 0)
1741 if ((o->flags & SEC_RELOC) != 0
1742 && o->reloc_count != 0)
1744 ((*_bfd_error_handler)
1745 ("%s: relocs in section `%s', but it has no contents",
1746 bfd_get_filename (input_bfd),
1747 bfd_get_section_name (input_bfd, o)));
1748 bfd_set_error (bfd_error_no_contents);
1755 if (coff_section_data (input_bfd, o) != NULL
1756 && coff_section_data (input_bfd, o)->contents != NULL)
1757 contents = coff_section_data (input_bfd, o)->contents;
1760 if (! bfd_get_section_contents (input_bfd, o, finfo->contents,
1761 (file_ptr) 0, o->_raw_size))
1763 contents = finfo->contents;
1766 if ((o->flags & SEC_RELOC) != 0)
1769 struct internal_reloc *internal_relocs;
1770 struct internal_reloc *irel;
1772 /* Read in the relocs. */
1773 target_index = o->output_section->target_index;
1774 internal_relocs = (_bfd_coff_read_internal_relocs
1775 (input_bfd, o, false, finfo->external_relocs,
1776 finfo->info->relocateable,
1777 (finfo->info->relocateable
1778 ? (finfo->section_info[target_index].relocs
1779 + o->output_section->reloc_count)
1780 : finfo->internal_relocs)));
1781 if (internal_relocs == NULL)
1784 /* Call processor specific code to relocate the section
1786 if (! bfd_coff_relocate_section (output_bfd, finfo->info,
1790 finfo->internal_syms,
1794 if (finfo->info->relocateable)
1797 struct internal_reloc *irelend;
1798 struct coff_link_hash_entry **rel_hash;
1800 offset = o->output_section->vma + o->output_offset - o->vma;
1801 irel = internal_relocs;
1802 irelend = irel + o->reloc_count;
1803 rel_hash = (finfo->section_info[target_index].rel_hashes
1804 + o->output_section->reloc_count);
1805 for (; irel < irelend; irel++, rel_hash++)
1807 struct coff_link_hash_entry *h;
1812 /* Adjust the reloc address and symbol index. */
1814 irel->r_vaddr += offset;
1816 if (irel->r_symndx == -1)
1821 if (! (*adjust_symndx) (output_bfd, finfo->info,
1829 h = obj_coff_sym_hashes (input_bfd)[irel->r_symndx];
1832 /* This is a global symbol. */
1834 irel->r_symndx = h->indx;
1837 /* This symbol is being written at the end
1838 of the file, and we do not yet know the
1839 symbol index. We save the pointer to the
1840 hash table entry in the rel_hash list.
1841 We set the indx field to -2 to indicate
1842 that this symbol must not be stripped. */
1851 indx = finfo->sym_indices[irel->r_symndx];
1853 irel->r_symndx = indx;
1856 struct internal_syment *is;
1858 char buf[SYMNMLEN + 1];
1860 /* This reloc is against a symbol we are
1861 stripping. It would be possible to
1862 handle this case, but I don't think it's
1864 is = finfo->internal_syms + irel->r_symndx;
1866 name = (_bfd_coff_internal_syment_name
1867 (input_bfd, is, buf));
1871 if (! ((*finfo->info->callbacks->unattached_reloc)
1872 (finfo->info, name, input_bfd, o,
1879 o->output_section->reloc_count += o->reloc_count;
1883 /* Write out the modified section contents. */
1884 if (! bfd_set_section_contents (output_bfd, o->output_section,
1885 contents, o->output_offset,
1886 (o->_cooked_size != 0
1892 if (! finfo->info->keep_memory)
1894 if (! _bfd_coff_free_symbols (input_bfd))
1901 /* Write out a global symbol. Called via coff_link_hash_traverse. */
1904 coff_write_global_sym (h, data)
1905 struct coff_link_hash_entry *h;
1908 struct coff_final_link_info *finfo = (struct coff_final_link_info *) data;
1910 struct internal_syment isym;
1911 bfd_size_type symesz;
1914 output_bfd = finfo->output_bfd;
1920 && (finfo->info->strip == strip_all
1921 || (finfo->info->strip == strip_some
1922 && (bfd_hash_lookup (finfo->info->keep_hash,
1923 h->root.root.string, false, false)
1927 switch (h->root.type)
1930 case bfd_link_hash_new:
1934 case bfd_link_hash_undefined:
1935 case bfd_link_hash_undefweak:
1936 isym.n_scnum = N_UNDEF;
1940 case bfd_link_hash_defined:
1941 case bfd_link_hash_defweak:
1945 sec = h->root.u.def.section->output_section;
1946 if (bfd_is_abs_section (sec))
1947 isym.n_scnum = N_ABS;
1949 isym.n_scnum = sec->target_index;
1950 isym.n_value = (h->root.u.def.value
1952 + h->root.u.def.section->output_offset);
1956 case bfd_link_hash_common:
1957 isym.n_scnum = N_UNDEF;
1958 isym.n_value = h->root.u.c.size;
1961 case bfd_link_hash_indirect:
1962 case bfd_link_hash_warning:
1963 /* Just ignore these. They can't be handled anyhow. */
1967 if (strlen (h->root.root.string) <= SYMNMLEN)
1968 strncpy (isym._n._n_name, h->root.root.string, SYMNMLEN);
1975 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
1977 indx = _bfd_stringtab_add (finfo->strtab, h->root.root.string, hash,
1979 if (indx == (bfd_size_type) -1)
1981 finfo->failed = true;
1984 isym._n._n_n._n_zeroes = 0;
1985 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
1988 isym.n_sclass = h->class;
1989 isym.n_type = h->type;
1991 if (isym.n_sclass == C_NULL)
1992 isym.n_sclass = C_EXT;
1994 isym.n_numaux = h->numaux;
1996 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) finfo->outsyms);
1998 symesz = bfd_coff_symesz (output_bfd);
2000 if (bfd_seek (output_bfd,
2001 (obj_sym_filepos (output_bfd)
2002 + obj_raw_syment_count (output_bfd) * symesz),
2004 || bfd_write (finfo->outsyms, symesz, 1, output_bfd) != symesz)
2006 finfo->failed = true;
2010 h->indx = obj_raw_syment_count (output_bfd);
2012 ++obj_raw_syment_count (output_bfd);
2014 /* Write out any associated aux entries. There normally will be
2015 none. If there are any, I have no idea how to modify them. */
2016 for (i = 0; i < isym.n_numaux; i++)
2018 bfd_coff_swap_aux_out (output_bfd, (PTR) (h->aux + i), isym.n_type,
2019 isym.n_sclass, i, isym.n_numaux,
2020 (PTR) finfo->outsyms);
2021 if (bfd_write (finfo->outsyms, symesz, 1, output_bfd) != symesz)
2023 finfo->failed = true;
2026 ++obj_raw_syment_count (output_bfd);
2032 /* Handle a link order which is supposed to generate a reloc. */
2035 coff_reloc_link_order (output_bfd, finfo, output_section, link_order)
2037 struct coff_final_link_info *finfo;
2038 asection *output_section;
2039 struct bfd_link_order *link_order;
2041 reloc_howto_type *howto;
2042 struct internal_reloc *irel;
2043 struct coff_link_hash_entry **rel_hash_ptr;
2045 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
2048 bfd_set_error (bfd_error_bad_value);
2052 if (link_order->u.reloc.p->addend != 0)
2056 bfd_reloc_status_type rstat;
2059 size = bfd_get_reloc_size (howto);
2060 buf = (bfd_byte *) bfd_zmalloc (size);
2064 rstat = _bfd_relocate_contents (howto, output_bfd,
2065 link_order->u.reloc.p->addend, buf);
2071 case bfd_reloc_outofrange:
2073 case bfd_reloc_overflow:
2074 if (! ((*finfo->info->callbacks->reloc_overflow)
2076 (link_order->type == bfd_section_reloc_link_order
2077 ? bfd_section_name (output_bfd,
2078 link_order->u.reloc.p->u.section)
2079 : link_order->u.reloc.p->u.name),
2080 howto->name, link_order->u.reloc.p->addend,
2081 (bfd *) NULL, (asection *) NULL, (bfd_vma) 0)))
2088 ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf,
2089 (file_ptr) link_order->offset, size);
2095 /* Store the reloc information in the right place. It will get
2096 swapped and written out at the end of the final_link routine. */
2098 irel = (finfo->section_info[output_section->target_index].relocs
2099 + output_section->reloc_count);
2100 rel_hash_ptr = (finfo->section_info[output_section->target_index].rel_hashes
2101 + output_section->reloc_count);
2103 memset (irel, 0, sizeof (struct internal_reloc));
2104 *rel_hash_ptr = NULL;
2106 irel->r_vaddr = output_section->vma + link_order->offset;
2108 if (link_order->type == bfd_section_reloc_link_order)
2110 /* We need to somehow locate a symbol in the right section. The
2111 symbol must either have a value of zero, or we must adjust
2112 the addend by the value of the symbol. FIXME: Write this
2113 when we need it. The old linker couldn't handle this anyhow. */
2115 *rel_hash_ptr = NULL;
2120 struct coff_link_hash_entry *h;
2122 h = coff_link_hash_lookup (coff_hash_table (finfo->info),
2123 link_order->u.reloc.p->u.name,
2124 false, false, true);
2128 irel->r_symndx = h->indx;
2131 /* Set the index to -2 to force this symbol to get
2140 if (! ((*finfo->info->callbacks->unattached_reloc)
2141 (finfo->info, link_order->u.reloc.p->u.name, (bfd *) NULL,
2142 (asection *) NULL, (bfd_vma) 0)))
2148 /* FIXME: Is this always right? */
2149 irel->r_type = howto->type;
2151 /* r_size is only used on the RS/6000, which needs its own linker
2152 routines anyhow. r_extern is only used for ECOFF. */
2154 /* FIXME: What is the right value for r_offset? Is zero OK? */
2156 ++output_section->reloc_count;
2161 /* A basic reloc handling routine which may be used by processors with
2165 _bfd_coff_generic_relocate_section (output_bfd, info, input_bfd,
2166 input_section, contents, relocs, syms,
2169 struct bfd_link_info *info;
2171 asection *input_section;
2173 struct internal_reloc *relocs;
2174 struct internal_syment *syms;
2175 asection **sections;
2177 struct internal_reloc *rel;
2178 struct internal_reloc *relend;
2181 relend = rel + input_section->reloc_count;
2182 for (; rel < relend; rel++)
2185 struct coff_link_hash_entry *h;
2186 struct internal_syment *sym;
2189 reloc_howto_type *howto;
2190 bfd_reloc_status_type rstat;
2192 symndx = rel->r_symndx;
2201 h = obj_coff_sym_hashes (input_bfd)[symndx];
2202 sym = syms + symndx;
2205 /* COFF treats common symbols in one of two ways. Either the
2206 size of the symbol is included in the section contents, or it
2207 is not. We assume that the size is not included, and force
2208 the rtype_to_howto function to adjust the addend as needed. */
2210 if (sym != NULL && sym->n_scnum != 0)
2211 addend = - sym->n_value;
2216 howto = bfd_coff_rtype_to_howto (input_bfd, input_section, rel, h,
2229 sec = bfd_abs_section_ptr;
2234 sec = sections[symndx];
2235 val = (sec->output_section->vma
2236 + sec->output_offset
2243 if (h->root.type == bfd_link_hash_defined
2244 || h->root.type == bfd_link_hash_defweak)
2248 sec = h->root.u.def.section;
2249 val = (h->root.u.def.value
2250 + sec->output_section->vma
2251 + sec->output_offset);
2254 else if (! info->relocateable)
2256 if (! ((*info->callbacks->undefined_symbol)
2257 (info, h->root.root.string, input_bfd, input_section,
2258 rel->r_vaddr - input_section->vma)))
2263 if (info->base_file)
2265 /* Emit a reloc if the backend thinks it needs it. */
2266 if (sym && pe_data(output_bfd)->in_reloc_p(output_bfd, howto))
2268 /* relocation to a symbol in a section which
2269 isn't absolute - we output the address here
2271 bfd_vma addr = rel->r_vaddr
2272 - input_section->vma
2273 + input_section->output_offset
2274 + input_section->output_section->vma;
2275 if (coff_data(output_bfd)->pe)
2276 addr -= pe_data(output_bfd)->pe_opthdr.ImageBase;
2277 fwrite (&addr, 1,4, (FILE *) info->base_file);
2281 rstat = _bfd_final_link_relocate (howto, input_bfd, input_section,
2283 rel->r_vaddr - input_section->vma,
2292 case bfd_reloc_overflow:
2295 char buf[SYMNMLEN + 1];
2300 name = h->root.root.string;
2303 name = _bfd_coff_internal_syment_name (input_bfd, sym, buf);
2308 if (! ((*info->callbacks->reloc_overflow)
2309 (info, name, howto->name, (bfd_vma) 0, input_bfd,
2310 input_section, rel->r_vaddr - input_section->vma)))