1 /* COFF specific linker code.
2 Copyright 1994, 1995, 1996, 1997 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 table->stab_info = NULL;
85 return _bfd_link_hash_table_init (&table->root, abfd, newfunc);
88 /* Create a COFF linker hash table. */
90 struct bfd_link_hash_table *
91 _bfd_coff_link_hash_table_create (abfd)
94 struct coff_link_hash_table *ret;
96 ret = ((struct coff_link_hash_table *)
97 bfd_alloc (abfd, sizeof (struct coff_link_hash_table)));
100 if (! _bfd_coff_link_hash_table_init (ret, abfd,
101 _bfd_coff_link_hash_newfunc))
103 bfd_release (abfd, ret);
104 return (struct bfd_link_hash_table *) NULL;
109 /* Create an entry in a COFF debug merge hash table. */
111 struct bfd_hash_entry *
112 _bfd_coff_debug_merge_hash_newfunc (entry, table, string)
113 struct bfd_hash_entry *entry;
114 struct bfd_hash_table *table;
117 struct coff_debug_merge_hash_entry *ret =
118 (struct coff_debug_merge_hash_entry *) entry;
120 /* Allocate the structure if it has not already been allocated by a
122 if (ret == (struct coff_debug_merge_hash_entry *) NULL)
123 ret = ((struct coff_debug_merge_hash_entry *)
124 bfd_hash_allocate (table,
125 sizeof (struct coff_debug_merge_hash_entry)));
126 if (ret == (struct coff_debug_merge_hash_entry *) NULL)
127 return (struct bfd_hash_entry *) ret;
129 /* Call the allocation method of the superclass. */
130 ret = ((struct coff_debug_merge_hash_entry *)
131 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
132 if (ret != (struct coff_debug_merge_hash_entry *) NULL)
134 /* Set local fields. */
138 return (struct bfd_hash_entry *) ret;
141 /* Given a COFF BFD, add symbols to the global hash table as
145 _bfd_coff_link_add_symbols (abfd, info)
147 struct bfd_link_info *info;
149 switch (bfd_get_format (abfd))
152 return coff_link_add_object_symbols (abfd, info);
154 return (_bfd_generic_link_add_archive_symbols
155 (abfd, info, coff_link_check_archive_element));
157 bfd_set_error (bfd_error_wrong_format);
162 /* Add symbols from a COFF object file. */
165 coff_link_add_object_symbols (abfd, info)
167 struct bfd_link_info *info;
169 if (! _bfd_coff_get_external_symbols (abfd))
171 if (! coff_link_add_symbols (abfd, info))
174 if (! info->keep_memory)
176 if (! _bfd_coff_free_symbols (abfd))
182 /* Check a single archive element to see if we need to include it in
183 the link. *PNEEDED is set according to whether this element is
184 needed in the link or not. This is called via
185 _bfd_generic_link_add_archive_symbols. */
188 coff_link_check_archive_element (abfd, info, pneeded)
190 struct bfd_link_info *info;
193 if (! _bfd_coff_get_external_symbols (abfd))
196 if (! coff_link_check_ar_symbols (abfd, info, pneeded))
201 if (! coff_link_add_symbols (abfd, info))
205 if (! info->keep_memory || ! *pneeded)
207 if (! _bfd_coff_free_symbols (abfd))
214 /* Look through the symbols to see if this object file should be
215 included in the link. */
218 coff_link_check_ar_symbols (abfd, info, pneeded)
220 struct bfd_link_info *info;
223 boolean (*sym_is_global) PARAMS ((bfd *, struct internal_syment *));
224 bfd_size_type symesz;
230 sym_is_global = coff_backend_info (abfd)->_bfd_coff_sym_is_global;
232 symesz = bfd_coff_symesz (abfd);
233 esym = (bfd_byte *) obj_coff_external_syms (abfd);
234 esym_end = esym + obj_raw_syment_count (abfd) * symesz;
235 while (esym < esym_end)
237 struct internal_syment sym;
239 bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym);
241 if ((sym.n_sclass == C_EXT
242 || (sym_is_global && (*sym_is_global) (abfd, &sym)))
243 && (sym.n_scnum != 0 || sym.n_value != 0))
246 char buf[SYMNMLEN + 1];
247 struct bfd_link_hash_entry *h;
249 /* This symbol is externally visible, and is defined by this
252 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
255 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
257 /* We are only interested in symbols that are currently
258 undefined. If a symbol is currently known to be common,
259 COFF linkers do not bring in an object file which defines
261 if (h != (struct bfd_link_hash_entry *) NULL
262 && h->type == bfd_link_hash_undefined)
264 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
271 esym += (sym.n_numaux + 1) * symesz;
274 /* We do not need this object file. */
278 /* Add all the symbols from an object file to the hash table. */
281 coff_link_add_symbols (abfd, info)
283 struct bfd_link_info *info;
285 boolean (*sym_is_global) PARAMS ((bfd *, struct internal_syment *));
287 boolean default_copy;
288 bfd_size_type symcount;
289 struct coff_link_hash_entry **sym_hash;
290 bfd_size_type symesz;
294 /* Keep the symbols during this function, in case the linker needs
295 to read the generic symbols in order to report an error message. */
296 keep_syms = obj_coff_keep_syms (abfd);
297 obj_coff_keep_syms (abfd) = true;
299 sym_is_global = coff_backend_info (abfd)->_bfd_coff_sym_is_global;
301 if (info->keep_memory)
302 default_copy = false;
306 symcount = obj_raw_syment_count (abfd);
308 /* We keep a list of the linker hash table entries that correspond
309 to particular symbols. */
310 sym_hash = ((struct coff_link_hash_entry **)
313 * sizeof (struct coff_link_hash_entry *))));
314 if (sym_hash == NULL && symcount != 0)
316 obj_coff_sym_hashes (abfd) = sym_hash;
318 (size_t) symcount * sizeof (struct coff_link_hash_entry *));
320 symesz = bfd_coff_symesz (abfd);
321 BFD_ASSERT (symesz == bfd_coff_auxesz (abfd));
322 esym = (bfd_byte *) obj_coff_external_syms (abfd);
323 esym_end = esym + symcount * symesz;
324 while (esym < esym_end)
326 struct internal_syment sym;
329 bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym);
331 if (sym.n_sclass == C_EXT
332 || (sym_is_global && (*sym_is_global) (abfd, &sym)))
335 char buf[SYMNMLEN + 1];
340 /* This symbol is externally visible. */
342 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
346 /* We must copy the name into memory if we got it from the
347 syment itself, rather than the string table. */
349 if (sym._n._n_n._n_zeroes != 0
350 || sym._n._n_n._n_offset == 0)
355 if (sym.n_scnum == 0)
360 section = bfd_und_section_ptr;
365 section = bfd_com_section_ptr;
370 flags = BSF_EXPORT | BSF_GLOBAL;
371 section = coff_section_from_bfd_index (abfd, sym.n_scnum);
372 value -= section->vma;
375 if (! (bfd_coff_link_add_one_symbol
376 (info, abfd, name, flags, section, value,
377 (const char *) NULL, copy, false,
378 (struct bfd_link_hash_entry **) sym_hash)))
381 if (info->hash->creator->flavour == bfd_get_flavour (abfd))
383 if (((*sym_hash)->class == C_NULL
384 && (*sym_hash)->type == T_NULL)
387 && (*sym_hash)->root.type != bfd_link_hash_defined))
389 (*sym_hash)->class = sym.n_sclass;
390 if (sym.n_type != T_NULL)
392 if ((*sym_hash)->type != T_NULL
393 && (*sym_hash)->type != sym.n_type)
394 (*_bfd_error_handler)
395 ("Warning: type of symbol `%s' changed from %d to %d in %s",
396 name, (*sym_hash)->type, sym.n_type,
397 bfd_get_filename (abfd));
398 (*sym_hash)->type = sym.n_type;
400 (*sym_hash)->auxbfd = abfd;
401 if (sym.n_numaux != 0)
403 union internal_auxent *alloc;
406 union internal_auxent *iaux;
408 (*sym_hash)->numaux = sym.n_numaux;
409 alloc = ((union internal_auxent *)
410 bfd_hash_allocate (&info->hash->table,
412 * sizeof (*alloc))));
415 for (i = 0, eaux = esym + symesz, iaux = alloc;
417 i++, eaux += symesz, iaux++)
418 bfd_coff_swap_aux_in (abfd, (PTR) eaux, sym.n_type,
419 sym.n_sclass, i, sym.n_numaux,
421 (*sym_hash)->aux = alloc;
427 esym += (sym.n_numaux + 1) * symesz;
428 sym_hash += sym.n_numaux + 1;
431 /* If this is a non-traditional, non-relocateable link, try to
432 optimize the handling of any .stab/.stabstr sections. */
433 if (! info->relocateable
434 && ! info->traditional_format
435 && info->hash->creator->flavour == bfd_get_flavour (abfd)
436 && (info->strip != strip_all && info->strip != strip_debugger))
438 asection *stab, *stabstr;
440 stab = bfd_get_section_by_name (abfd, ".stab");
443 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
447 struct coff_link_hash_table *table;
448 struct coff_section_tdata *secdata;
450 secdata = coff_section_data (abfd, stab);
454 (PTR) bfd_zalloc (abfd,
455 sizeof (struct coff_section_tdata));
456 if (stab->used_by_bfd == NULL)
458 secdata = coff_section_data (abfd, stab);
461 table = coff_hash_table (info);
463 if (! _bfd_link_section_stabs (abfd, &table->stab_info,
465 &secdata->stab_info))
471 obj_coff_keep_syms (abfd) = keep_syms;
476 obj_coff_keep_syms (abfd) = keep_syms;
480 /* Do the final link step. */
483 _bfd_coff_final_link (abfd, info)
485 struct bfd_link_info *info;
487 bfd_size_type symesz;
488 struct coff_final_link_info finfo;
489 boolean debug_merge_allocated;
490 boolean long_section_names;
492 struct bfd_link_order *p;
493 size_t max_sym_count;
494 size_t max_lineno_count;
495 size_t max_reloc_count;
496 size_t max_output_reloc_count;
497 size_t max_contents_size;
498 file_ptr rel_filepos;
500 file_ptr line_filepos;
503 bfd_byte *external_relocs = NULL;
504 char strbuf[STRING_SIZE_SIZE];
506 symesz = bfd_coff_symesz (abfd);
509 finfo.output_bfd = abfd;
511 finfo.section_info = NULL;
512 finfo.last_file_index = -1;
513 finfo.last_bf_index = -1;
514 finfo.internal_syms = NULL;
515 finfo.sec_ptrs = NULL;
516 finfo.sym_indices = NULL;
517 finfo.outsyms = NULL;
518 finfo.linenos = NULL;
519 finfo.contents = NULL;
520 finfo.external_relocs = NULL;
521 finfo.internal_relocs = NULL;
522 debug_merge_allocated = false;
524 coff_data (abfd)->link_info = info;
526 finfo.strtab = _bfd_stringtab_init ();
527 if (finfo.strtab == NULL)
530 if (! coff_debug_merge_hash_table_init (&finfo.debug_merge))
532 debug_merge_allocated = true;
534 /* Compute the file positions for all the sections. */
535 if (! abfd->output_has_begun)
536 bfd_coff_compute_section_file_positions (abfd);
538 /* Count the line numbers and relocation entries required for the
539 output file. Set the file positions for the relocs. */
540 rel_filepos = obj_relocbase (abfd);
541 relsz = bfd_coff_relsz (abfd);
542 max_contents_size = 0;
543 max_lineno_count = 0;
546 long_section_names = false;
547 for (o = abfd->sections; o != NULL; o = o->next)
551 for (p = o->link_order_head; p != NULL; p = p->next)
553 if (p->type == bfd_indirect_link_order)
557 sec = p->u.indirect.section;
559 /* Mark all sections which are to be included in the
560 link. This will normally be every section. We need
561 to do this so that we can identify any sections which
562 the linker has decided to not include. */
563 sec->linker_mark = true;
565 if (info->strip == strip_none
566 || info->strip == strip_some)
567 o->lineno_count += sec->lineno_count;
569 if (info->relocateable)
570 o->reloc_count += sec->reloc_count;
572 if (sec->_raw_size > max_contents_size)
573 max_contents_size = sec->_raw_size;
574 if (sec->lineno_count > max_lineno_count)
575 max_lineno_count = sec->lineno_count;
576 if (sec->reloc_count > max_reloc_count)
577 max_reloc_count = sec->reloc_count;
579 else if (info->relocateable
580 && (p->type == bfd_section_reloc_link_order
581 || p->type == bfd_symbol_reloc_link_order))
584 if (o->reloc_count == 0)
588 o->flags |= SEC_RELOC;
589 o->rel_filepos = rel_filepos;
590 rel_filepos += o->reloc_count * relsz;
593 if (bfd_coff_long_section_names (abfd)
594 && strlen (o->name) > SCNNMLEN)
596 /* This section has a long name which must go in the string
597 table. This must correspond to the code in
598 coff_write_object_contents which puts the string index
599 into the s_name field of the section header. That is why
600 we pass hash as false. */
601 if (_bfd_stringtab_add (finfo.strtab, o->name, false, false)
602 == (bfd_size_type) -1)
604 long_section_names = true;
608 /* If doing a relocateable link, allocate space for the pointers we
610 if (info->relocateable)
614 /* We use section_count + 1, rather than section_count, because
615 the target_index fields are 1 based. */
617 ((struct coff_link_section_info *)
618 bfd_malloc ((abfd->section_count + 1)
619 * sizeof (struct coff_link_section_info)));
620 if (finfo.section_info == NULL)
622 for (i = 0; i <= abfd->section_count; i++)
624 finfo.section_info[i].relocs = NULL;
625 finfo.section_info[i].rel_hashes = NULL;
629 /* We now know the size of the relocs, so we can determine the file
630 positions of the line numbers. */
631 line_filepos = rel_filepos;
632 linesz = bfd_coff_linesz (abfd);
633 max_output_reloc_count = 0;
634 for (o = abfd->sections; o != NULL; o = o->next)
636 if (o->lineno_count == 0)
640 o->line_filepos = line_filepos;
641 line_filepos += o->lineno_count * linesz;
644 if (o->reloc_count != 0)
646 /* We don't know the indices of global symbols until we have
647 written out all the local symbols. For each section in
648 the output file, we keep an array of pointers to hash
649 table entries. Each entry in the array corresponds to a
650 reloc. When we find a reloc against a global symbol, we
651 set the corresponding entry in this array so that we can
652 fix up the symbol index after we have written out all the
655 Because of this problem, we also keep the relocs in
656 memory until the end of the link. This wastes memory,
657 but only when doing a relocateable link, which is not the
659 BFD_ASSERT (info->relocateable);
660 finfo.section_info[o->target_index].relocs =
661 ((struct internal_reloc *)
662 bfd_malloc (o->reloc_count * sizeof (struct internal_reloc)));
663 finfo.section_info[o->target_index].rel_hashes =
664 ((struct coff_link_hash_entry **)
665 bfd_malloc (o->reloc_count
666 * sizeof (struct coff_link_hash_entry *)));
667 if (finfo.section_info[o->target_index].relocs == NULL
668 || finfo.section_info[o->target_index].rel_hashes == NULL)
671 if (o->reloc_count > max_output_reloc_count)
672 max_output_reloc_count = o->reloc_count;
675 /* Reset the reloc and lineno counts, so that we can use them to
676 count the number of entries we have output so far. */
681 obj_sym_filepos (abfd) = line_filepos;
683 /* Figure out the largest number of symbols in an input BFD. Take
684 the opportunity to clear the output_has_begun fields of all the
687 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
691 sub->output_has_begun = false;
692 sz = obj_raw_syment_count (sub);
693 if (sz > max_sym_count)
697 /* Allocate some buffers used while linking. */
698 finfo.internal_syms = ((struct internal_syment *)
699 bfd_malloc (max_sym_count
700 * sizeof (struct internal_syment)));
701 finfo.sec_ptrs = (asection **) bfd_malloc (max_sym_count
702 * sizeof (asection *));
703 finfo.sym_indices = (long *) bfd_malloc (max_sym_count * sizeof (long));
704 finfo.outsyms = ((bfd_byte *)
705 bfd_malloc ((size_t) ((max_sym_count + 1) * symesz)));
706 finfo.linenos = (bfd_byte *) bfd_malloc (max_lineno_count
707 * bfd_coff_linesz (abfd));
708 finfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
709 finfo.external_relocs = (bfd_byte *) bfd_malloc (max_reloc_count * relsz);
710 if (! info->relocateable)
711 finfo.internal_relocs = ((struct internal_reloc *)
712 bfd_malloc (max_reloc_count
713 * sizeof (struct internal_reloc)));
714 if ((finfo.internal_syms == NULL && max_sym_count > 0)
715 || (finfo.sec_ptrs == NULL && max_sym_count > 0)
716 || (finfo.sym_indices == NULL && max_sym_count > 0)
717 || finfo.outsyms == NULL
718 || (finfo.linenos == NULL && max_lineno_count > 0)
719 || (finfo.contents == NULL && max_contents_size > 0)
720 || (finfo.external_relocs == NULL && max_reloc_count > 0)
721 || (! info->relocateable
722 && finfo.internal_relocs == NULL
723 && max_reloc_count > 0))
726 /* We now know the position of everything in the file, except that
727 we don't know the size of the symbol table and therefore we don't
728 know where the string table starts. We just build the string
729 table in memory as we go along. We process all the relocations
730 for a single input file at once. */
731 obj_raw_syment_count (abfd) = 0;
733 if (coff_backend_info (abfd)->_bfd_coff_start_final_link)
735 if (! bfd_coff_start_final_link (abfd, info))
739 for (o = abfd->sections; o != NULL; o = o->next)
741 for (p = o->link_order_head; p != NULL; p = p->next)
743 if (p->type == bfd_indirect_link_order
744 && (bfd_get_flavour (p->u.indirect.section->owner)
745 == bfd_target_coff_flavour))
747 sub = p->u.indirect.section->owner;
748 if (! sub->output_has_begun)
750 if (! _bfd_coff_link_input_bfd (&finfo, sub))
752 sub->output_has_begun = true;
755 else if (p->type == bfd_section_reloc_link_order
756 || p->type == bfd_symbol_reloc_link_order)
758 if (! _bfd_coff_reloc_link_order (abfd, &finfo, o, p))
763 if (! _bfd_default_link_order (abfd, info, o, p))
769 /* Free up the buffers used by _bfd_coff_link_input_bfd. */
771 coff_debug_merge_hash_table_free (&finfo.debug_merge);
772 debug_merge_allocated = false;
774 if (finfo.internal_syms != NULL)
776 free (finfo.internal_syms);
777 finfo.internal_syms = NULL;
779 if (finfo.sec_ptrs != NULL)
781 free (finfo.sec_ptrs);
782 finfo.sec_ptrs = NULL;
784 if (finfo.sym_indices != NULL)
786 free (finfo.sym_indices);
787 finfo.sym_indices = NULL;
789 if (finfo.linenos != NULL)
791 free (finfo.linenos);
792 finfo.linenos = NULL;
794 if (finfo.contents != NULL)
796 free (finfo.contents);
797 finfo.contents = NULL;
799 if (finfo.external_relocs != NULL)
801 free (finfo.external_relocs);
802 finfo.external_relocs = NULL;
804 if (finfo.internal_relocs != NULL)
806 free (finfo.internal_relocs);
807 finfo.internal_relocs = NULL;
810 /* The value of the last C_FILE symbol is supposed to be the symbol
811 index of the first external symbol. Write it out again if
813 if (finfo.last_file_index != -1
814 && (unsigned int) finfo.last_file.n_value != obj_raw_syment_count (abfd))
816 finfo.last_file.n_value = obj_raw_syment_count (abfd);
817 bfd_coff_swap_sym_out (abfd, (PTR) &finfo.last_file,
818 (PTR) finfo.outsyms);
820 (obj_sym_filepos (abfd)
821 + finfo.last_file_index * symesz),
823 || bfd_write (finfo.outsyms, symesz, 1, abfd) != symesz)
827 /* Write out the global symbols. */
828 finfo.failed = false;
829 coff_link_hash_traverse (coff_hash_table (info), _bfd_coff_write_global_sym,
834 /* The outsyms buffer is used by _bfd_coff_write_global_sym. */
835 if (finfo.outsyms != NULL)
837 free (finfo.outsyms);
838 finfo.outsyms = NULL;
841 if (info->relocateable && max_output_reloc_count > 0)
843 /* Now that we have written out all the global symbols, we know
844 the symbol indices to use for relocs against them, and we can
845 finally write out the relocs. */
846 external_relocs = ((bfd_byte *)
847 bfd_malloc (max_output_reloc_count * relsz));
848 if (external_relocs == NULL)
851 for (o = abfd->sections; o != NULL; o = o->next)
853 struct internal_reloc *irel;
854 struct internal_reloc *irelend;
855 struct coff_link_hash_entry **rel_hash;
858 if (o->reloc_count == 0)
861 irel = finfo.section_info[o->target_index].relocs;
862 irelend = irel + o->reloc_count;
863 rel_hash = finfo.section_info[o->target_index].rel_hashes;
864 erel = external_relocs;
865 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
867 if (*rel_hash != NULL)
869 BFD_ASSERT ((*rel_hash)->indx >= 0);
870 irel->r_symndx = (*rel_hash)->indx;
872 bfd_coff_swap_reloc_out (abfd, (PTR) irel, (PTR) erel);
875 if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0
876 || bfd_write ((PTR) external_relocs, relsz, o->reloc_count,
877 abfd) != relsz * o->reloc_count)
881 free (external_relocs);
882 external_relocs = NULL;
885 /* Free up the section information. */
886 if (finfo.section_info != NULL)
890 for (i = 0; i < abfd->section_count; i++)
892 if (finfo.section_info[i].relocs != NULL)
893 free (finfo.section_info[i].relocs);
894 if (finfo.section_info[i].rel_hashes != NULL)
895 free (finfo.section_info[i].rel_hashes);
897 free (finfo.section_info);
898 finfo.section_info = NULL;
901 /* If we have optimized stabs strings, output them. */
902 if (coff_hash_table (info)->stab_info != NULL)
904 if (! _bfd_write_stab_strings (abfd, &coff_hash_table (info)->stab_info))
908 /* Write out the string table. */
909 if (obj_raw_syment_count (abfd) != 0 || long_section_names)
912 (obj_sym_filepos (abfd)
913 + obj_raw_syment_count (abfd) * symesz),
917 #if STRING_SIZE_SIZE == 4
919 _bfd_stringtab_size (finfo.strtab) + STRING_SIZE_SIZE,
920 (bfd_byte *) strbuf);
922 #error Change bfd_h_put_32
925 if (bfd_write (strbuf, 1, STRING_SIZE_SIZE, abfd) != STRING_SIZE_SIZE)
928 if (! _bfd_stringtab_emit (abfd, finfo.strtab))
932 _bfd_stringtab_free (finfo.strtab);
934 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
935 not try to write out the symbols. */
936 bfd_get_symcount (abfd) = 0;
941 if (debug_merge_allocated)
942 coff_debug_merge_hash_table_free (&finfo.debug_merge);
943 if (finfo.strtab != NULL)
944 _bfd_stringtab_free (finfo.strtab);
945 if (finfo.section_info != NULL)
949 for (i = 0; i < abfd->section_count; i++)
951 if (finfo.section_info[i].relocs != NULL)
952 free (finfo.section_info[i].relocs);
953 if (finfo.section_info[i].rel_hashes != NULL)
954 free (finfo.section_info[i].rel_hashes);
956 free (finfo.section_info);
958 if (finfo.internal_syms != NULL)
959 free (finfo.internal_syms);
960 if (finfo.sec_ptrs != NULL)
961 free (finfo.sec_ptrs);
962 if (finfo.sym_indices != NULL)
963 free (finfo.sym_indices);
964 if (finfo.outsyms != NULL)
965 free (finfo.outsyms);
966 if (finfo.linenos != NULL)
967 free (finfo.linenos);
968 if (finfo.contents != NULL)
969 free (finfo.contents);
970 if (finfo.external_relocs != NULL)
971 free (finfo.external_relocs);
972 if (finfo.internal_relocs != NULL)
973 free (finfo.internal_relocs);
974 if (external_relocs != NULL)
975 free (external_relocs);
979 /* parse out a -heap <reserved>,<commit> line */
982 dores_com (ptr, output_bfd, heap)
987 if (coff_data(output_bfd)->pe)
989 int val = strtoul (ptr, &ptr, 0);
991 pe_data(output_bfd)->pe_opthdr.SizeOfHeapReserve =val;
993 pe_data(output_bfd)->pe_opthdr.SizeOfStackReserve =val;
997 int val = strtoul (ptr+1, &ptr, 0);
999 pe_data(output_bfd)->pe_opthdr.SizeOfHeapCommit =val;
1001 pe_data(output_bfd)->pe_opthdr.SizeOfStackCommit =val;
1007 static char *get_name(ptr, dst)
1014 while (*ptr && *ptr != ' ')
1020 /* Process any magic embedded commands in a section called .drectve */
1023 process_embedded_commands (output_bfd, info, abfd)
1025 struct bfd_link_info *info;
1028 asection *sec = bfd_get_section_by_name (abfd, ".drectve");
1035 copy = bfd_malloc ((size_t) sec->_raw_size);
1038 if (! bfd_get_section_contents(abfd, sec, copy, 0, sec->_raw_size))
1043 e = copy + sec->_raw_size;
1044 for (s = copy; s < e ; )
1050 if (strncmp (s,"-attr", 5) == 0)
1062 s = get_name(s, &name);
1063 s = get_name(s, &attribs);
1083 asec = bfd_get_section_by_name (abfd, name);
1086 asec->flags |= SEC_CODE;
1088 asec->flags |= SEC_READONLY;
1091 else if (strncmp (s,"-heap", 5) == 0)
1093 s = dores_com (s+5, output_bfd, 1);
1095 else if (strncmp (s,"-stack", 6) == 0)
1097 s = dores_com (s+6, output_bfd, 0);
1106 /* Link an input file into the linker output file. This function
1107 handles all the sections and relocations of the input file at once. */
1110 _bfd_coff_link_input_bfd (finfo, input_bfd)
1111 struct coff_final_link_info *finfo;
1114 boolean (*sym_is_global) PARAMS ((bfd *, struct internal_syment *));
1115 boolean (*adjust_symndx) PARAMS ((bfd *, struct bfd_link_info *, bfd *,
1116 asection *, struct internal_reloc *,
1119 const char *strings;
1120 bfd_size_type syment_base;
1121 unsigned int n_tmask;
1122 unsigned int n_btshft;
1124 bfd_size_type isymesz;
1125 bfd_size_type osymesz;
1126 bfd_size_type linesz;
1129 struct internal_syment *isymp;
1132 unsigned long output_index;
1134 struct coff_link_hash_entry **sym_hash;
1137 /* Move all the symbols to the output file. */
1139 output_bfd = finfo->output_bfd;
1140 sym_is_global = coff_backend_info (input_bfd)->_bfd_coff_sym_is_global;
1142 syment_base = obj_raw_syment_count (output_bfd);
1143 isymesz = bfd_coff_symesz (input_bfd);
1144 osymesz = bfd_coff_symesz (output_bfd);
1145 linesz = bfd_coff_linesz (input_bfd);
1146 BFD_ASSERT (linesz == bfd_coff_linesz (output_bfd));
1148 n_tmask = coff_data (input_bfd)->local_n_tmask;
1149 n_btshft = coff_data (input_bfd)->local_n_btshft;
1151 /* Define macros so that ISFCN, et. al., macros work correctly. */
1152 #define N_TMASK n_tmask
1153 #define N_BTSHFT n_btshft
1156 if (! finfo->info->keep_memory)
1159 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
1162 if (! _bfd_coff_get_external_symbols (input_bfd))
1165 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
1166 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
1167 isymp = finfo->internal_syms;
1168 secpp = finfo->sec_ptrs;
1169 indexp = finfo->sym_indices;
1170 output_index = syment_base;
1171 outsym = finfo->outsyms;
1173 if (coff_data (output_bfd)->pe)
1175 if (! process_embedded_commands (output_bfd, finfo->info, input_bfd))
1179 while (esym < esym_end)
1181 struct internal_syment isym;
1186 bfd_coff_swap_sym_in (input_bfd, (PTR) esym, (PTR) isymp);
1188 /* Make a copy of *isymp so that the relocate_section function
1189 always sees the original values. This is more reliable than
1190 always recomputing the symbol value even if we are stripping
1194 if (isym.n_scnum != 0)
1195 *secpp = coff_section_from_bfd_index (input_bfd, isym.n_scnum);
1198 if (isym.n_value == 0)
1199 *secpp = bfd_und_section_ptr;
1201 *secpp = bfd_com_section_ptr;
1208 add = 1 + isym.n_numaux;
1210 /* If we are stripping all symbols, we want to skip this one. */
1211 if (finfo->info->strip == strip_all)
1216 if (isym.n_sclass == C_EXT
1217 || (sym_is_global && (*sym_is_global) (input_bfd, &isym)))
1219 /* This is a global symbol. Global symbols come at the
1220 end of the symbol table, so skip them for now.
1221 Function symbols, however, are an exception, and are
1222 not moved to the end. */
1224 if (! ISFCN (isym.n_type))
1229 /* This is a local symbol. Skip it if we are discarding
1231 if (finfo->info->discard == discard_all)
1236 /* If we stripping debugging symbols, and this is a debugging
1237 symbol, then skip it. */
1239 && finfo->info->strip == strip_debugger
1240 && isym.n_scnum == N_DEBUG)
1243 /* If some symbols are stripped based on the name, work out the
1244 name and decide whether to skip this symbol. */
1246 && (finfo->info->strip == strip_some
1247 || finfo->info->discard == discard_l))
1250 char buf[SYMNMLEN + 1];
1252 name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf);
1256 if ((finfo->info->strip == strip_some
1257 && (bfd_hash_lookup (finfo->info->keep_hash, name, false,
1260 && finfo->info->discard == discard_l
1261 && strncmp (name, finfo->info->lprefix,
1262 finfo->info->lprefix_len) == 0))
1266 /* If this is an enum, struct, or union tag, see if we have
1267 already output an identical type. */
1269 && (finfo->output_bfd->flags & BFD_TRADITIONAL_FORMAT) == 0
1270 && (isym.n_sclass == C_ENTAG
1271 || isym.n_sclass == C_STRTAG
1272 || isym.n_sclass == C_UNTAG)
1273 && isym.n_numaux == 1)
1276 char buf[SYMNMLEN + 1];
1277 struct coff_debug_merge_hash_entry *mh;
1278 struct coff_debug_merge_type *mt;
1279 union internal_auxent aux;
1280 struct coff_debug_merge_element **epp;
1281 bfd_byte *esl, *eslend;
1282 struct internal_syment *islp;
1284 name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf);
1288 /* Ignore fake names invented by compiler; treat them all as
1290 if (*name == '~' || *name == '.' || *name == '$'
1291 || (*name == bfd_get_symbol_leading_char (input_bfd)
1292 && (name[1] == '~' || name[1] == '.' || name[1] == '$')))
1295 mh = coff_debug_merge_hash_lookup (&finfo->debug_merge, name,
1300 /* Allocate memory to hold type information. If this turns
1301 out to be a duplicate, we pass this address to
1303 mt = ((struct coff_debug_merge_type *)
1304 bfd_alloc (input_bfd,
1305 sizeof (struct coff_debug_merge_type)));
1308 mt->class = isym.n_sclass;
1310 /* Pick up the aux entry, which points to the end of the tag
1312 bfd_coff_swap_aux_in (input_bfd, (PTR) (esym + isymesz),
1313 isym.n_type, isym.n_sclass, 0, isym.n_numaux,
1316 /* Gather the elements. */
1317 epp = &mt->elements;
1318 mt->elements = NULL;
1320 esl = esym + 2 * isymesz;
1321 eslend = ((bfd_byte *) obj_coff_external_syms (input_bfd)
1322 + aux.x_sym.x_fcnary.x_fcn.x_endndx.l * isymesz);
1323 while (esl < eslend)
1325 const char *elename;
1326 char elebuf[SYMNMLEN + 1];
1329 bfd_coff_swap_sym_in (input_bfd, (PTR) esl, (PTR) islp);
1331 *epp = ((struct coff_debug_merge_element *)
1332 bfd_alloc (input_bfd,
1333 sizeof (struct coff_debug_merge_element)));
1337 elename = _bfd_coff_internal_syment_name (input_bfd, islp,
1339 if (elename == NULL)
1342 copy = (char *) bfd_alloc (input_bfd, strlen (elename) + 1);
1345 strcpy (copy, elename);
1347 (*epp)->name = copy;
1348 (*epp)->type = islp->n_type;
1350 if (islp->n_numaux >= 1
1351 && islp->n_type != T_NULL
1352 && islp->n_sclass != C_EOS)
1354 union internal_auxent eleaux;
1357 bfd_coff_swap_aux_in (input_bfd, (PTR) (esl + isymesz),
1358 islp->n_type, islp->n_sclass, 0,
1359 islp->n_numaux, (PTR) &eleaux);
1360 indx = eleaux.x_sym.x_tagndx.l;
1362 /* FIXME: If this tagndx entry refers to a symbol
1363 defined later in this file, we just ignore it.
1364 Handling this correctly would be tedious, and may
1370 (bfd_byte *) obj_coff_external_syms (input_bfd))
1373 (*epp)->tagndx = finfo->sym_indices[indx];
1374 if ((*epp)->tagndx < 0)
1378 epp = &(*epp)->next;
1381 esl += (islp->n_numaux + 1) * isymesz;
1382 islp += islp->n_numaux + 1;
1385 /* See if we already have a definition which matches this
1386 type. We always output the type if it has no elements,
1388 if (mt->elements == NULL)
1389 bfd_release (input_bfd, (PTR) mt);
1392 struct coff_debug_merge_type *mtl;
1394 for (mtl = mh->types; mtl != NULL; mtl = mtl->next)
1396 struct coff_debug_merge_element *me, *mel;
1398 if (mtl->class != mt->class)
1401 for (me = mt->elements, mel = mtl->elements;
1402 me != NULL && mel != NULL;
1403 me = me->next, mel = mel->next)
1405 if (strcmp (me->name, mel->name) != 0
1406 || me->type != mel->type
1407 || me->tagndx != mel->tagndx)
1411 if (me == NULL && mel == NULL)
1415 if (mtl == NULL || (bfd_size_type) mtl->indx >= syment_base)
1417 /* This is the first definition of this type. */
1418 mt->indx = output_index;
1419 mt->next = mh->types;
1424 /* This is a redefinition which can be merged. */
1425 bfd_release (input_bfd, (PTR) mt);
1426 *indexp = mtl->indx;
1427 add = (eslend - esym) / isymesz;
1433 /* We now know whether we are to skip this symbol or not. */
1436 /* Adjust the symbol in order to output it. */
1438 if (isym._n._n_n._n_zeroes == 0
1439 && isym._n._n_n._n_offset != 0)
1444 /* This symbol has a long name. Enter it in the string
1445 table we are building. Note that we do not check
1446 bfd_coff_symname_in_debug. That is only true for
1447 XCOFF, and XCOFF requires different linking code
1449 name = _bfd_coff_internal_syment_name (input_bfd, &isym,
1453 indx = _bfd_stringtab_add (finfo->strtab, name, hash, copy);
1454 if (indx == (bfd_size_type) -1)
1456 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
1459 if (isym.n_scnum > 0)
1461 isym.n_scnum = (*secpp)->output_section->target_index;
1462 isym.n_value += ((*secpp)->output_section->vma
1463 + (*secpp)->output_offset
1467 /* The value of a C_FILE symbol is the symbol index of the
1468 next C_FILE symbol. The value of the last C_FILE symbol
1469 is the symbol index to the first external symbol
1470 (actually, coff_renumber_symbols does not get this
1471 right--it just sets the value of the last C_FILE symbol
1472 to zero--and nobody has ever complained about it). We
1473 try to get this right, below, just before we write the
1474 symbols out, but in the general case we may have to write
1475 the symbol out twice. */
1476 if (isym.n_sclass == C_FILE)
1478 if (finfo->last_file_index != -1
1479 && finfo->last_file.n_value != (long) output_index)
1481 /* We must correct the value of the last C_FILE entry. */
1482 finfo->last_file.n_value = output_index;
1483 if ((bfd_size_type) finfo->last_file_index >= syment_base)
1485 /* The last C_FILE symbol is in this input file. */
1486 bfd_coff_swap_sym_out (output_bfd,
1487 (PTR) &finfo->last_file,
1488 (PTR) (finfo->outsyms
1489 + ((finfo->last_file_index
1495 /* We have already written out the last C_FILE
1496 symbol. We need to write it out again. We
1497 borrow *outsym temporarily. */
1498 bfd_coff_swap_sym_out (output_bfd,
1499 (PTR) &finfo->last_file,
1501 if (bfd_seek (output_bfd,
1502 (obj_sym_filepos (output_bfd)
1503 + finfo->last_file_index * osymesz),
1505 || (bfd_write (outsym, osymesz, 1, output_bfd)
1511 finfo->last_file_index = output_index;
1512 finfo->last_file = isym;
1515 /* Output the symbol. */
1517 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
1519 *indexp = output_index;
1524 struct coff_link_hash_entry *h;
1526 indx = ((esym - (bfd_byte *) obj_coff_external_syms (input_bfd))
1528 h = obj_coff_sym_hashes (input_bfd)[indx];
1531 /* This can happen if there were errors earlier in
1533 bfd_set_error (bfd_error_bad_value);
1536 h->indx = output_index;
1539 output_index += add;
1540 outsym += add * osymesz;
1543 esym += add * isymesz;
1547 for (--add; add > 0; --add)
1554 /* Fix up the aux entries. This must be done in a separate pass,
1555 because we don't know the correct symbol indices until we have
1556 already decided which symbols we are going to keep. */
1558 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
1559 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
1560 isymp = finfo->internal_syms;
1561 indexp = finfo->sym_indices;
1562 sym_hash = obj_coff_sym_hashes (input_bfd);
1563 outsym = finfo->outsyms;
1564 while (esym < esym_end)
1568 add = 1 + isymp->n_numaux;
1571 || (bfd_size_type) *indexp < syment_base)
1572 && (*sym_hash == NULL
1573 || (*sym_hash)->auxbfd != input_bfd))
1574 esym += add * isymesz;
1577 struct coff_link_hash_entry *h;
1585 /* The m68k-motorola-sysv assembler will sometimes
1586 generate two symbols with the same name, but only one
1587 will have aux entries. */
1588 BFD_ASSERT (isymp->n_numaux == 0
1589 || h->numaux == isymp->n_numaux);
1597 /* Handle the aux entries. This handling is based on
1598 coff_pointerize_aux. I don't know if it always correct. */
1599 for (i = 0; i < isymp->n_numaux && esym < esym_end; i++)
1601 union internal_auxent aux;
1602 union internal_auxent *auxp;
1608 bfd_coff_swap_aux_in (input_bfd, (PTR) esym, isymp->n_type,
1609 isymp->n_sclass, i, isymp->n_numaux,
1614 if (isymp->n_sclass == C_FILE)
1616 /* If this is a long filename, we must put it in the
1618 if (auxp->x_file.x_n.x_zeroes == 0
1619 && auxp->x_file.x_n.x_offset != 0)
1621 const char *filename;
1624 BFD_ASSERT (auxp->x_file.x_n.x_offset
1625 >= STRING_SIZE_SIZE);
1626 if (strings == NULL)
1628 strings = _bfd_coff_read_string_table (input_bfd);
1629 if (strings == NULL)
1632 filename = strings + auxp->x_file.x_n.x_offset;
1633 indx = _bfd_stringtab_add (finfo->strtab, filename,
1635 if (indx == (bfd_size_type) -1)
1637 auxp->x_file.x_n.x_offset = STRING_SIZE_SIZE + indx;
1640 else if (isymp->n_sclass != C_STAT || isymp->n_type != T_NULL)
1644 if (ISFCN (isymp->n_type)
1645 || ISTAG (isymp->n_sclass)
1646 || isymp->n_sclass == C_BLOCK
1647 || isymp->n_sclass == C_FCN)
1649 indx = auxp->x_sym.x_fcnary.x_fcn.x_endndx.l;
1651 && indx < obj_raw_syment_count (input_bfd))
1653 /* We look forward through the symbol for
1654 the index of the next symbol we are going
1655 to include. I don't know if this is
1657 while ((finfo->sym_indices[indx] < 0
1658 || ((bfd_size_type) finfo->sym_indices[indx]
1660 && indx < obj_raw_syment_count (input_bfd))
1662 if (indx >= obj_raw_syment_count (input_bfd))
1663 indx = output_index;
1665 indx = finfo->sym_indices[indx];
1666 auxp->x_sym.x_fcnary.x_fcn.x_endndx.l = indx;
1670 indx = auxp->x_sym.x_tagndx.l;
1671 if (indx > 0 && indx < obj_raw_syment_count (input_bfd))
1675 symindx = finfo->sym_indices[indx];
1677 auxp->x_sym.x_tagndx.l = 0;
1679 auxp->x_sym.x_tagndx.l = symindx;
1682 /* The .bf symbols are supposed to be linked through
1683 the endndx field. We need to carry this list
1684 across object files. */
1687 && isymp->n_sclass == C_FCN
1688 && (isymp->_n._n_n._n_zeroes != 0
1689 || isymp->_n._n_n._n_offset == 0)
1690 && isymp->_n._n_name[0] == '.'
1691 && isymp->_n._n_name[1] == 'b'
1692 && isymp->_n._n_name[2] == 'f'
1693 && isymp->_n._n_name[3] == '\0')
1695 if (finfo->last_bf_index != -1)
1697 finfo->last_bf.x_sym.x_fcnary.x_fcn.x_endndx.l =
1700 if ((bfd_size_type) finfo->last_bf_index
1705 /* The last .bf symbol is in this input
1706 file. This will only happen if the
1707 assembler did not set up the .bf
1708 endndx symbols correctly. */
1709 auxout = (PTR) (finfo->outsyms
1710 + ((finfo->last_bf_index
1713 bfd_coff_swap_aux_out (output_bfd,
1714 (PTR) &finfo->last_bf,
1722 /* We have already written out the last
1723 .bf aux entry. We need to write it
1724 out again. We borrow *outsym
1725 temporarily. FIXME: This case should
1727 bfd_coff_swap_aux_out (output_bfd,
1728 (PTR) &finfo->last_bf,
1733 if (bfd_seek (output_bfd,
1734 (obj_sym_filepos (output_bfd)
1735 + finfo->last_bf_index * osymesz),
1737 || bfd_write (outsym, osymesz, 1,
1738 output_bfd) != osymesz)
1743 if (auxp->x_sym.x_fcnary.x_fcn.x_endndx.l != 0)
1744 finfo->last_bf_index = -1;
1747 /* The endndx field of this aux entry must
1748 be updated with the symbol number of the
1750 finfo->last_bf = *auxp;
1751 finfo->last_bf_index = (((outsym - finfo->outsyms)
1760 bfd_coff_swap_aux_out (output_bfd, (PTR) auxp, isymp->n_type,
1761 isymp->n_sclass, i, isymp->n_numaux,
1775 /* Relocate the line numbers, unless we are stripping them. */
1776 if (finfo->info->strip == strip_none
1777 || finfo->info->strip == strip_some)
1779 for (o = input_bfd->sections; o != NULL; o = o->next)
1785 /* FIXME: If SEC_HAS_CONTENTS is not for the section, then
1786 build_link_order in ldwrite.c will not have created a
1787 link order, which means that we will not have seen this
1788 input section in _bfd_coff_final_link, which means that
1789 we will not have allocated space for the line numbers of
1790 this section. I don't think line numbers can be
1791 meaningful for a section which does not have
1792 SEC_HAS_CONTENTS set, but, if they do, this must be
1794 if (o->lineno_count == 0
1795 || (o->output_section->flags & SEC_HAS_CONTENTS) == 0)
1798 if (bfd_seek (input_bfd, o->line_filepos, SEEK_SET) != 0
1799 || bfd_read (finfo->linenos, linesz, o->lineno_count,
1800 input_bfd) != linesz * o->lineno_count)
1803 offset = o->output_section->vma + o->output_offset - o->vma;
1804 eline = finfo->linenos;
1805 elineend = eline + linesz * o->lineno_count;
1806 for (; eline < elineend; eline += linesz)
1808 struct internal_lineno iline;
1810 bfd_coff_swap_lineno_in (input_bfd, (PTR) eline, (PTR) &iline);
1812 if (iline.l_lnno != 0)
1813 iline.l_addr.l_paddr += offset;
1814 else if (iline.l_addr.l_symndx >= 0
1815 && ((unsigned long) iline.l_addr.l_symndx
1816 < obj_raw_syment_count (input_bfd)))
1820 indx = finfo->sym_indices[iline.l_addr.l_symndx];
1824 /* These line numbers are attached to a symbol
1825 which we are stripping. We should really
1826 just discard the line numbers, but that would
1827 be a pain because we have already counted
1833 struct internal_syment is;
1834 union internal_auxent ia;
1836 /* Fix up the lnnoptr field in the aux entry of
1837 the symbol. It turns out that we can't do
1838 this when we modify the symbol aux entries,
1839 because gas sometimes screws up the lnnoptr
1840 field and makes it an offset from the start
1841 of the line numbers rather than an absolute
1843 bfd_coff_swap_sym_in (output_bfd,
1844 (PTR) (finfo->outsyms
1845 + ((indx - syment_base)
1848 if ((ISFCN (is.n_type)
1849 || is.n_sclass == C_BLOCK)
1850 && is.n_numaux >= 1)
1854 auxptr = (PTR) (finfo->outsyms
1855 + ((indx - syment_base + 1)
1857 bfd_coff_swap_aux_in (output_bfd, auxptr,
1858 is.n_type, is.n_sclass,
1859 0, is.n_numaux, (PTR) &ia);
1860 ia.x_sym.x_fcnary.x_fcn.x_lnnoptr =
1861 (o->output_section->line_filepos
1862 + o->output_section->lineno_count * linesz
1863 + eline - finfo->linenos);
1864 bfd_coff_swap_aux_out (output_bfd, (PTR) &ia,
1865 is.n_type, is.n_sclass, 0,
1866 is.n_numaux, auxptr);
1870 iline.l_addr.l_symndx = indx;
1873 bfd_coff_swap_lineno_out (output_bfd, (PTR) &iline, (PTR) eline);
1876 if (bfd_seek (output_bfd,
1877 (o->output_section->line_filepos
1878 + o->output_section->lineno_count * linesz),
1880 || bfd_write (finfo->linenos, linesz, o->lineno_count,
1881 output_bfd) != linesz * o->lineno_count)
1884 o->output_section->lineno_count += o->lineno_count;
1888 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
1889 symbol will be the first symbol in the next input file. In the
1890 normal case, this will save us from writing out the C_FILE symbol
1892 if (finfo->last_file_index != -1
1893 && (bfd_size_type) finfo->last_file_index >= syment_base)
1895 finfo->last_file.n_value = output_index;
1896 bfd_coff_swap_sym_out (output_bfd, (PTR) &finfo->last_file,
1897 (PTR) (finfo->outsyms
1898 + ((finfo->last_file_index - syment_base)
1902 /* Write the modified symbols to the output file. */
1903 if (outsym > finfo->outsyms)
1905 if (bfd_seek (output_bfd,
1906 obj_sym_filepos (output_bfd) + syment_base * osymesz,
1908 || (bfd_write (finfo->outsyms, outsym - finfo->outsyms, 1,
1910 != (bfd_size_type) (outsym - finfo->outsyms)))
1913 BFD_ASSERT ((obj_raw_syment_count (output_bfd)
1914 + (outsym - finfo->outsyms) / osymesz)
1917 obj_raw_syment_count (output_bfd) = output_index;
1920 /* Relocate the contents of each section. */
1921 adjust_symndx = coff_backend_info (input_bfd)->_bfd_coff_adjust_symndx;
1922 for (o = input_bfd->sections; o != NULL; o = o->next)
1925 struct coff_section_tdata *secdata;
1927 if (! o->linker_mark)
1929 /* This section was omitted from the link. */
1933 if ((o->flags & SEC_HAS_CONTENTS) == 0
1934 || (o->_raw_size == 0 && (o->flags & SEC_RELOC) == 0))
1936 if ((o->flags & SEC_RELOC) != 0
1937 && o->reloc_count != 0)
1939 ((*_bfd_error_handler)
1940 ("%s: relocs in section `%s', but it has no contents",
1941 bfd_get_filename (input_bfd),
1942 bfd_get_section_name (input_bfd, o)));
1943 bfd_set_error (bfd_error_no_contents);
1950 secdata = coff_section_data (input_bfd, o);
1951 if (secdata != NULL && secdata->contents != NULL)
1952 contents = secdata->contents;
1955 if (! bfd_get_section_contents (input_bfd, o, finfo->contents,
1956 (file_ptr) 0, o->_raw_size))
1958 contents = finfo->contents;
1961 if ((o->flags & SEC_RELOC) != 0)
1964 struct internal_reloc *internal_relocs;
1965 struct internal_reloc *irel;
1967 /* Read in the relocs. */
1968 target_index = o->output_section->target_index;
1969 internal_relocs = (_bfd_coff_read_internal_relocs
1970 (input_bfd, o, false, finfo->external_relocs,
1971 finfo->info->relocateable,
1972 (finfo->info->relocateable
1973 ? (finfo->section_info[target_index].relocs
1974 + o->output_section->reloc_count)
1975 : finfo->internal_relocs)));
1976 if (internal_relocs == NULL)
1979 /* Call processor specific code to relocate the section
1981 if (! bfd_coff_relocate_section (output_bfd, finfo->info,
1985 finfo->internal_syms,
1989 if (finfo->info->relocateable)
1992 struct internal_reloc *irelend;
1993 struct coff_link_hash_entry **rel_hash;
1995 offset = o->output_section->vma + o->output_offset - o->vma;
1996 irel = internal_relocs;
1997 irelend = irel + o->reloc_count;
1998 rel_hash = (finfo->section_info[target_index].rel_hashes
1999 + o->output_section->reloc_count);
2000 for (; irel < irelend; irel++, rel_hash++)
2002 struct coff_link_hash_entry *h;
2007 /* Adjust the reloc address and symbol index. */
2009 irel->r_vaddr += offset;
2011 if (irel->r_symndx == -1)
2016 if (! (*adjust_symndx) (output_bfd, finfo->info,
2024 h = obj_coff_sym_hashes (input_bfd)[irel->r_symndx];
2027 /* This is a global symbol. */
2029 irel->r_symndx = h->indx;
2032 /* This symbol is being written at the end
2033 of the file, and we do not yet know the
2034 symbol index. We save the pointer to the
2035 hash table entry in the rel_hash list.
2036 We set the indx field to -2 to indicate
2037 that this symbol must not be stripped. */
2046 indx = finfo->sym_indices[irel->r_symndx];
2048 irel->r_symndx = indx;
2051 struct internal_syment *is;
2053 char buf[SYMNMLEN + 1];
2055 /* This reloc is against a symbol we are
2056 stripping. It would be possible to
2057 handle this case, but I don't think it's
2059 is = finfo->internal_syms + irel->r_symndx;
2061 name = (_bfd_coff_internal_syment_name
2062 (input_bfd, is, buf));
2066 if (! ((*finfo->info->callbacks->unattached_reloc)
2067 (finfo->info, name, input_bfd, o,
2074 o->output_section->reloc_count += o->reloc_count;
2078 /* Write out the modified section contents. */
2079 if (secdata == NULL || secdata->stab_info == NULL)
2081 if (! bfd_set_section_contents (output_bfd, o->output_section,
2082 contents, o->output_offset,
2083 (o->_cooked_size != 0
2090 if (! _bfd_write_section_stabs (output_bfd, o, &secdata->stab_info,
2096 if (! finfo->info->keep_memory)
2098 if (! _bfd_coff_free_symbols (input_bfd))
2105 /* Write out a global symbol. Called via coff_link_hash_traverse. */
2108 _bfd_coff_write_global_sym (h, data)
2109 struct coff_link_hash_entry *h;
2112 struct coff_final_link_info *finfo = (struct coff_final_link_info *) data;
2114 struct internal_syment isym;
2115 bfd_size_type symesz;
2118 output_bfd = finfo->output_bfd;
2124 && (finfo->info->strip == strip_all
2125 || (finfo->info->strip == strip_some
2126 && (bfd_hash_lookup (finfo->info->keep_hash,
2127 h->root.root.string, false, false)
2131 switch (h->root.type)
2134 case bfd_link_hash_new:
2138 case bfd_link_hash_undefined:
2139 case bfd_link_hash_undefweak:
2140 isym.n_scnum = N_UNDEF;
2144 case bfd_link_hash_defined:
2145 case bfd_link_hash_defweak:
2149 sec = h->root.u.def.section->output_section;
2150 if (bfd_is_abs_section (sec))
2151 isym.n_scnum = N_ABS;
2153 isym.n_scnum = sec->target_index;
2154 isym.n_value = (h->root.u.def.value
2156 + h->root.u.def.section->output_offset);
2160 case bfd_link_hash_common:
2161 isym.n_scnum = N_UNDEF;
2162 isym.n_value = h->root.u.c.size;
2165 case bfd_link_hash_indirect:
2166 case bfd_link_hash_warning:
2167 /* Just ignore these. They can't be handled anyhow. */
2171 if (strlen (h->root.root.string) <= SYMNMLEN)
2172 strncpy (isym._n._n_name, h->root.root.string, SYMNMLEN);
2179 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
2181 indx = _bfd_stringtab_add (finfo->strtab, h->root.root.string, hash,
2183 if (indx == (bfd_size_type) -1)
2185 finfo->failed = true;
2188 isym._n._n_n._n_zeroes = 0;
2189 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
2192 isym.n_sclass = h->class;
2193 isym.n_type = h->type;
2195 if (isym.n_sclass == C_NULL)
2196 isym.n_sclass = C_EXT;
2198 isym.n_numaux = h->numaux;
2200 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) finfo->outsyms);
2202 symesz = bfd_coff_symesz (output_bfd);
2204 if (bfd_seek (output_bfd,
2205 (obj_sym_filepos (output_bfd)
2206 + obj_raw_syment_count (output_bfd) * symesz),
2208 || bfd_write (finfo->outsyms, symesz, 1, output_bfd) != symesz)
2210 finfo->failed = true;
2214 h->indx = obj_raw_syment_count (output_bfd);
2216 ++obj_raw_syment_count (output_bfd);
2218 /* Write out any associated aux entries. There normally will be
2219 none. If there are any, I have no idea how to modify them. */
2220 for (i = 0; i < isym.n_numaux; i++)
2222 bfd_coff_swap_aux_out (output_bfd, (PTR) (h->aux + i), isym.n_type,
2223 isym.n_sclass, i, isym.n_numaux,
2224 (PTR) finfo->outsyms);
2225 if (bfd_write (finfo->outsyms, symesz, 1, output_bfd) != symesz)
2227 finfo->failed = true;
2230 ++obj_raw_syment_count (output_bfd);
2236 /* Handle a link order which is supposed to generate a reloc. */
2239 _bfd_coff_reloc_link_order (output_bfd, finfo, output_section, link_order)
2241 struct coff_final_link_info *finfo;
2242 asection *output_section;
2243 struct bfd_link_order *link_order;
2245 reloc_howto_type *howto;
2246 struct internal_reloc *irel;
2247 struct coff_link_hash_entry **rel_hash_ptr;
2249 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
2252 bfd_set_error (bfd_error_bad_value);
2256 if (link_order->u.reloc.p->addend != 0)
2260 bfd_reloc_status_type rstat;
2263 size = bfd_get_reloc_size (howto);
2264 buf = (bfd_byte *) bfd_zmalloc (size);
2268 rstat = _bfd_relocate_contents (howto, output_bfd,
2269 link_order->u.reloc.p->addend, buf);
2275 case bfd_reloc_outofrange:
2277 case bfd_reloc_overflow:
2278 if (! ((*finfo->info->callbacks->reloc_overflow)
2280 (link_order->type == bfd_section_reloc_link_order
2281 ? bfd_section_name (output_bfd,
2282 link_order->u.reloc.p->u.section)
2283 : link_order->u.reloc.p->u.name),
2284 howto->name, link_order->u.reloc.p->addend,
2285 (bfd *) NULL, (asection *) NULL, (bfd_vma) 0)))
2292 ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf,
2293 (file_ptr) link_order->offset, size);
2299 /* Store the reloc information in the right place. It will get
2300 swapped and written out at the end of the final_link routine. */
2302 irel = (finfo->section_info[output_section->target_index].relocs
2303 + output_section->reloc_count);
2304 rel_hash_ptr = (finfo->section_info[output_section->target_index].rel_hashes
2305 + output_section->reloc_count);
2307 memset (irel, 0, sizeof (struct internal_reloc));
2308 *rel_hash_ptr = NULL;
2310 irel->r_vaddr = output_section->vma + link_order->offset;
2312 if (link_order->type == bfd_section_reloc_link_order)
2314 /* We need to somehow locate a symbol in the right section. The
2315 symbol must either have a value of zero, or we must adjust
2316 the addend by the value of the symbol. FIXME: Write this
2317 when we need it. The old linker couldn't handle this anyhow. */
2319 *rel_hash_ptr = NULL;
2324 struct coff_link_hash_entry *h;
2326 h = ((struct coff_link_hash_entry *)
2327 bfd_wrapped_link_hash_lookup (output_bfd, finfo->info,
2328 link_order->u.reloc.p->u.name,
2329 false, false, true));
2333 irel->r_symndx = h->indx;
2336 /* Set the index to -2 to force this symbol to get
2345 if (! ((*finfo->info->callbacks->unattached_reloc)
2346 (finfo->info, link_order->u.reloc.p->u.name, (bfd *) NULL,
2347 (asection *) NULL, (bfd_vma) 0)))
2353 /* FIXME: Is this always right? */
2354 irel->r_type = howto->type;
2356 /* r_size is only used on the RS/6000, which needs its own linker
2357 routines anyhow. r_extern is only used for ECOFF. */
2359 /* FIXME: What is the right value for r_offset? Is zero OK? */
2361 ++output_section->reloc_count;
2366 /* A basic reloc handling routine which may be used by processors with
2370 _bfd_coff_generic_relocate_section (output_bfd, info, input_bfd,
2371 input_section, contents, relocs, syms,
2374 struct bfd_link_info *info;
2376 asection *input_section;
2378 struct internal_reloc *relocs;
2379 struct internal_syment *syms;
2380 asection **sections;
2382 struct internal_reloc *rel;
2383 struct internal_reloc *relend;
2386 relend = rel + input_section->reloc_count;
2387 for (; rel < relend; rel++)
2390 struct coff_link_hash_entry *h;
2391 struct internal_syment *sym;
2394 reloc_howto_type *howto;
2395 bfd_reloc_status_type rstat;
2397 symndx = rel->r_symndx;
2406 h = obj_coff_sym_hashes (input_bfd)[symndx];
2407 sym = syms + symndx;
2410 /* COFF treats common symbols in one of two ways. Either the
2411 size of the symbol is included in the section contents, or it
2412 is not. We assume that the size is not included, and force
2413 the rtype_to_howto function to adjust the addend as needed. */
2415 if (sym != NULL && sym->n_scnum != 0)
2416 addend = - sym->n_value;
2421 howto = bfd_coff_rtype_to_howto (input_bfd, input_section, rel, h,
2426 /* If we are doing a relocateable link, then we can just ignore
2427 a PC relative reloc that is pcrel_offset. It will already
2428 have the correct value. */
2429 if (info->relocateable
2430 && howto->pc_relative
2431 && howto->pcrel_offset)
2442 sec = bfd_abs_section_ptr;
2447 sec = sections[symndx];
2448 val = (sec->output_section->vma
2449 + sec->output_offset
2456 if (h->root.type == bfd_link_hash_defined
2457 || h->root.type == bfd_link_hash_defweak)
2461 sec = h->root.u.def.section;
2462 val = (h->root.u.def.value
2463 + sec->output_section->vma
2464 + sec->output_offset);
2467 else if (! info->relocateable)
2469 if (! ((*info->callbacks->undefined_symbol)
2470 (info, h->root.root.string, input_bfd, input_section,
2471 rel->r_vaddr - input_section->vma)))
2476 if (info->base_file)
2478 /* Emit a reloc if the backend thinks it needs it. */
2479 if (sym && pe_data(output_bfd)->in_reloc_p(output_bfd, howto))
2481 /* relocation to a symbol in a section which
2482 isn't absolute - we output the address here
2484 bfd_vma addr = rel->r_vaddr
2485 - input_section->vma
2486 + input_section->output_offset
2487 + input_section->output_section->vma;
2488 if (coff_data(output_bfd)->pe)
2489 addr -= pe_data(output_bfd)->pe_opthdr.ImageBase;
2490 /* FIXME: Shouldn't 4 be sizeof (addr)? */
2491 fwrite (&addr, 1,4, (FILE *) info->base_file);
2495 rstat = _bfd_final_link_relocate (howto, input_bfd, input_section,
2497 rel->r_vaddr - input_section->vma,
2506 case bfd_reloc_outofrange:
2507 (*_bfd_error_handler)
2508 ("%s: bad reloc address 0x%lx in section `%s'",
2509 bfd_get_filename (input_bfd),
2510 (unsigned long) rel->r_vaddr,
2511 bfd_get_section_name (input_bfd, input_section));
2513 case bfd_reloc_overflow:
2516 char buf[SYMNMLEN + 1];
2521 name = h->root.root.string;
2524 name = _bfd_coff_internal_syment_name (input_bfd, sym, buf);
2529 if (! ((*info->callbacks->reloc_overflow)
2530 (info, name, howto->name, (bfd_vma) 0, input_bfd,
2531 input_section, rel->r_vaddr - input_section->vma)))