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 #define STRING_SIZE_SIZE (4)
32 /* Information we keep for each section in the output file when doing
33 a relocateable link. */
35 struct coff_link_section_info
37 /* The relocs to be output. */
38 struct internal_reloc *relocs;
39 /* For each reloc against a global symbol whose index was not known
40 when the reloc was handled, the global hash table entry. */
41 struct coff_link_hash_entry **rel_hashes;
44 /* Information that we pass around while doing the final link step. */
46 struct coff_final_link_info
48 /* General link information. */
49 struct bfd_link_info *info;
52 /* Used to indicate failure in traversal routine. */
54 /* Hash table for long symbol name. */
55 struct bfd_strtab_hash *strtab;
56 /* When doing a relocateable link, an array of information kept for
57 each output section, indexed by the target_index field. */
58 struct coff_link_section_info *section_info;
59 /* Symbol index of last C_FILE symbol (-1 if none). */
61 /* Contents of last C_FILE symbol. */
62 struct internal_syment last_file;
63 /* Buffer large enough to hold swapped symbols of any input file. */
64 struct internal_syment *internal_syms;
65 /* Buffer large enough to hold sections of symbols of any input file. */
67 /* Buffer large enough to hold output indices of symbols of any
70 /* Buffer large enough to hold output symbols for any input file. */
72 /* Buffer large enough to hold external line numbers for any input
75 /* Buffer large enough to hold any input section. */
77 /* Buffer large enough to hold external relocs of any input section. */
78 bfd_byte *external_relocs;
79 /* Buffer large enough to hold swapped relocs of any input section. */
80 struct internal_reloc *internal_relocs;
84 static struct bfd_hash_entry *coff_link_hash_newfunc
85 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
86 static boolean coff_link_add_object_symbols
87 PARAMS ((bfd *, struct bfd_link_info *));
88 static boolean coff_link_check_archive_element
89 PARAMS ((bfd *, struct bfd_link_info *, boolean *));
90 static boolean coff_link_check_ar_symbols
91 PARAMS ((bfd *, struct bfd_link_info *, boolean *));
92 static boolean coff_link_add_symbols PARAMS ((bfd *, struct bfd_link_info *));
93 static boolean coff_link_input_bfd
94 PARAMS ((struct coff_final_link_info *, bfd *));
95 static boolean coff_write_global_sym
96 PARAMS ((struct coff_link_hash_entry *, PTR));
97 static boolean coff_reloc_link_order
98 PARAMS ((bfd *, struct coff_final_link_info *, asection *,
99 struct bfd_link_order *));
101 /* Create an entry in a COFF linker hash table. */
103 static struct bfd_hash_entry *
104 coff_link_hash_newfunc (entry, table, string)
105 struct bfd_hash_entry *entry;
106 struct bfd_hash_table *table;
109 struct coff_link_hash_entry *ret = (struct coff_link_hash_entry *) entry;
111 /* Allocate the structure if it has not already been allocated by a
113 if (ret == (struct coff_link_hash_entry *) NULL)
114 ret = ((struct coff_link_hash_entry *)
115 bfd_hash_allocate (table, sizeof (struct coff_link_hash_entry)));
116 if (ret == (struct coff_link_hash_entry *) NULL)
118 bfd_set_error (bfd_error_no_memory);
119 return (struct bfd_hash_entry *) ret;
122 /* Call the allocation method of the superclass. */
123 ret = ((struct coff_link_hash_entry *)
124 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
126 if (ret != (struct coff_link_hash_entry *) NULL)
128 /* Set local fields. */
137 return (struct bfd_hash_entry *) ret;
140 /* Create a COFF linker hash table. */
142 struct bfd_link_hash_table *
143 _bfd_coff_link_hash_table_create (abfd)
146 struct coff_link_hash_table *ret;
148 ret = ((struct coff_link_hash_table *)
149 bfd_alloc (abfd, sizeof (struct coff_link_hash_table)));
152 bfd_set_error (bfd_error_no_memory);
155 if (! _bfd_link_hash_table_init (&ret->root, abfd,
156 coff_link_hash_newfunc))
159 return (struct bfd_link_hash_table *) NULL;
164 /* Given a COFF BFD, add symbols to the global hash table as
168 _bfd_coff_link_add_symbols (abfd, info)
170 struct bfd_link_info *info;
172 switch (bfd_get_format (abfd))
175 return coff_link_add_object_symbols (abfd, info);
177 return (_bfd_generic_link_add_archive_symbols
178 (abfd, info, coff_link_check_archive_element));
180 bfd_set_error (bfd_error_wrong_format);
185 /* Add symbols from a COFF object file. */
188 coff_link_add_object_symbols (abfd, info)
190 struct bfd_link_info *info;
192 if (! _bfd_coff_get_external_symbols (abfd))
194 if (! coff_link_add_symbols (abfd, info))
196 if (! info->keep_memory)
198 if (! _bfd_coff_free_symbols (abfd))
204 /* Check a single archive element to see if we need to include it in
205 the link. *PNEEDED is set according to whether this element is
206 needed in the link or not. This is called via
207 _bfd_generic_link_add_archive_symbols. */
210 coff_link_check_archive_element (abfd, info, pneeded)
212 struct bfd_link_info *info;
215 if (! _bfd_coff_get_external_symbols (abfd))
218 if (! coff_link_check_ar_symbols (abfd, info, pneeded))
223 if (! coff_link_add_symbols (abfd, info))
227 if (! info->keep_memory || ! *pneeded)
229 if (! _bfd_coff_free_symbols (abfd))
236 /* Get the name of a symbol. The caller must pass in a buffer of size
240 _bfd_coff_internal_syment_name (abfd, sym, buf)
242 const struct internal_syment *sym;
245 /* FIXME: It's not clear this will work correctly if sizeof
247 if (sym->_n._n_n._n_zeroes != 0
248 || sym->_n._n_n._n_offset == 0)
250 memcpy (buf, sym->_n._n_name, SYMNMLEN);
251 buf[SYMNMLEN] = '\0';
258 BFD_ASSERT (sym->_n._n_n._n_offset >= STRING_SIZE_SIZE);
259 strings = obj_coff_strings (abfd);
262 strings = _bfd_coff_read_string_table (abfd);
266 return strings + sym->_n._n_n._n_offset;
270 /* Look through the symbols to see if this object file should be
271 included in the link. */
274 coff_link_check_ar_symbols (abfd, info, pneeded)
276 struct bfd_link_info *info;
279 boolean (*sym_is_global) PARAMS ((bfd *, struct internal_syment *));
280 bfd_size_type symesz;
286 sym_is_global = coff_backend_info (abfd)->_bfd_coff_sym_is_global;
288 symesz = bfd_coff_symesz (abfd);
289 esym = (bfd_byte *) obj_coff_external_syms (abfd);
290 esym_end = esym + obj_raw_syment_count (abfd) * symesz;
291 while (esym < esym_end)
293 struct internal_syment sym;
295 bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym);
297 if ((sym.n_sclass == C_EXT
298 || (sym_is_global && (*sym_is_global) (abfd, &sym)))
299 && (sym.n_scnum != 0 || sym.n_value != 0))
302 char buf[SYMNMLEN + 1];
303 struct bfd_link_hash_entry *h;
305 /* This symbol is externally visible, and is defined by this
308 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
311 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
313 /* We are only interested in symbols that are currently
314 undefined. If a symbol is currently known to be common,
315 COFF linkers do not bring in an object file which defines
317 if (h != (struct bfd_link_hash_entry *) NULL
318 && h->type == bfd_link_hash_undefined)
320 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
327 esym += (sym.n_numaux + 1) * symesz;
330 /* We do not need this object file. */
334 /* Add all the symbols from an object file to the hash table. */
337 coff_link_add_symbols (abfd, info)
339 struct bfd_link_info *info;
341 boolean (*sym_is_global) PARAMS ((bfd *, struct internal_syment *));
342 boolean default_copy;
343 bfd_size_type symcount;
344 struct coff_link_hash_entry **sym_hash;
345 bfd_size_type symesz;
349 sym_is_global = coff_backend_info (abfd)->_bfd_coff_sym_is_global;
351 if (info->keep_memory)
352 default_copy = false;
356 symcount = obj_raw_syment_count (abfd);
358 /* We keep a list of the linker hash table entries that correspond
359 to particular symbols. */
360 sym_hash = ((struct coff_link_hash_entry **)
363 * sizeof (struct coff_link_hash_entry *))));
364 if (sym_hash == NULL && symcount != 0)
366 bfd_set_error (bfd_error_no_memory);
369 obj_coff_sym_hashes (abfd) = sym_hash;
371 (size_t) symcount * sizeof (struct coff_link_hash_entry *));
373 symesz = bfd_coff_symesz (abfd);
374 BFD_ASSERT (symesz == bfd_coff_auxesz (abfd));
375 esym = (bfd_byte *) obj_coff_external_syms (abfd);
376 esym_end = esym + symcount * symesz;
377 while (esym < esym_end)
379 struct internal_syment sym;
382 bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym);
384 if (sym.n_sclass == C_EXT
385 || (sym_is_global && (*sym_is_global) (abfd, &sym)))
388 char buf[SYMNMLEN + 1];
393 /* This symbol is externally visible. */
395 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
399 /* We must copy the name into memory if we got it from the
400 syment itself, rather than the string table. */
402 if (sym._n._n_n._n_zeroes != 0
403 || sym._n._n_n._n_offset == 0)
408 if (sym.n_scnum == 0)
413 section = bfd_und_section_ptr;
418 section = bfd_com_section_ptr;
423 flags = BSF_EXPORT | BSF_GLOBAL;
424 section = coff_section_from_bfd_index (abfd, sym.n_scnum);
425 value -= section->vma;
428 if (! (_bfd_generic_link_add_one_symbol
429 (info, abfd, name, flags, section, value,
430 (const char *) NULL, copy, false,
431 (struct bfd_link_hash_entry **) sym_hash)))
434 if (info->hash->creator->flavour == bfd_get_flavour (abfd))
436 if (((*sym_hash)->class == C_NULL
437 && (*sym_hash)->type == T_NULL)
440 && (*sym_hash)->root.type != bfd_link_hash_defined))
442 (*sym_hash)->class = sym.n_sclass;
443 (*sym_hash)->type = sym.n_type;
444 (*sym_hash)->numaux = sym.n_numaux;
445 (*sym_hash)->auxbfd = abfd;
446 if (sym.n_numaux != 0)
448 union internal_auxent *alloc;
451 union internal_auxent *iaux;
453 alloc = ((union internal_auxent *)
454 bfd_hash_allocate (&info->hash->table,
456 * sizeof (*alloc))));
459 bfd_set_error (bfd_error_no_memory);
462 for (i = 0, eaux = esym + symesz, iaux = alloc;
464 i++, eaux += symesz, iaux++)
465 bfd_coff_swap_aux_in (abfd, (PTR) eaux, sym.n_type,
466 sym.n_sclass, i, sym.n_numaux,
468 (*sym_hash)->aux = alloc;
474 esym += (sym.n_numaux + 1) * symesz;
475 sym_hash += sym.n_numaux + 1;
482 /* Do the final link step. */
485 _bfd_coff_final_link (abfd, info)
487 struct bfd_link_info *info;
489 bfd_size_type symesz;
490 struct coff_final_link_info finfo;
492 struct bfd_link_order *p;
493 size_t max_contents_size;
494 size_t max_sym_count;
495 size_t max_lineno_count;
496 size_t max_reloc_count;
497 size_t max_output_reloc_count;
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.internal_syms = NULL;
514 finfo.sec_ptrs = NULL;
515 finfo.sym_indices = NULL;
516 finfo.outsyms = NULL;
517 finfo.linenos = NULL;
518 finfo.contents = NULL;
519 finfo.external_relocs = NULL;
520 finfo.internal_relocs = NULL;
522 coff_data (abfd)->link_info = info;
524 finfo.strtab = _bfd_stringtab_init ();
525 if (finfo.strtab == NULL)
528 /* Compute the file positions for all the sections. */
529 if (! abfd->output_has_begun)
530 bfd_coff_compute_section_file_positions (abfd);
532 /* Count the line numbers and relocation entries required for the
533 output file. Set the file positions for the relocs. */
534 rel_filepos = obj_relocbase (abfd);
535 relsz = bfd_coff_relsz (abfd);
536 max_contents_size = 0;
537 max_lineno_count = 0;
539 for (o = abfd->sections; o != NULL; o = o->next)
543 for (p = o->link_order_head; p != NULL; p = p->next)
545 if (p->type == bfd_indirect_link_order)
549 sec = p->u.indirect.section;
551 if (info->strip == strip_none
552 || info->strip == strip_some)
553 o->lineno_count += sec->lineno_count;
555 if (info->relocateable)
556 o->reloc_count += sec->reloc_count;
558 if (sec->_raw_size > max_contents_size)
559 max_contents_size = sec->_raw_size;
560 if (sec->lineno_count > max_lineno_count)
561 max_lineno_count = sec->lineno_count;
562 if (sec->reloc_count > max_reloc_count)
563 max_reloc_count = sec->reloc_count;
565 else if (info->relocateable
566 && (p->type == bfd_section_reloc_link_order
567 || p->type == bfd_symbol_reloc_link_order))
570 if (o->reloc_count == 0)
574 o->flags |= SEC_RELOC;
575 o->rel_filepos = rel_filepos;
576 rel_filepos += o->reloc_count * relsz;
580 /* If doing a relocateable link, allocate space for the pointers we
582 if (info->relocateable)
586 /* We use section_count + 1, rather than section_count, because
587 the target_index fields are 1 based. */
588 finfo.section_info = ((struct coff_link_section_info *)
589 malloc ((abfd->section_count + 1)
590 * sizeof (struct coff_link_section_info)));
591 if (finfo.section_info == NULL)
593 bfd_set_error (bfd_error_no_memory);
596 for (i = 0; i <= abfd->section_count; i++)
598 finfo.section_info[i].relocs = NULL;
599 finfo.section_info[i].rel_hashes = NULL;
603 /* We now know the size of the relocs, so we can determine the file
604 positions of the line numbers. */
605 line_filepos = rel_filepos;
606 linesz = bfd_coff_linesz (abfd);
607 max_output_reloc_count = 0;
608 for (o = abfd->sections; o != NULL; o = o->next)
610 if (o->lineno_count == 0)
614 o->line_filepos = line_filepos;
615 line_filepos += o->lineno_count * linesz;
618 if (o->reloc_count != 0)
620 /* We don't know the indices of global symbols until we have
621 written out all the local symbols. For each section in
622 the output file, we keep an array of pointers to hash
623 table entries. Each entry in the array corresponds to a
624 reloc. When we find a reloc against a global symbol, we
625 set the corresponding entry in this array so that we can
626 fix up the symbol index after we have written out all the
629 Because of this problem, we also keep the relocs in
630 memory until the end of the link. This wastes memory,
631 but only when doing a relocateable link, which is not the
633 BFD_ASSERT (info->relocateable);
634 finfo.section_info[o->target_index].relocs =
635 ((struct internal_reloc *)
636 malloc (o->reloc_count * sizeof (struct internal_reloc)));
637 finfo.section_info[o->target_index].rel_hashes =
638 ((struct coff_link_hash_entry **)
639 malloc (o->reloc_count
640 * sizeof (struct coff_link_hash_entry *)));
641 if (finfo.section_info[o->target_index].relocs == NULL
642 || finfo.section_info[o->target_index].rel_hashes == NULL)
644 bfd_set_error (bfd_error_no_memory);
648 if (o->reloc_count > max_output_reloc_count)
649 max_output_reloc_count = o->reloc_count;
652 /* Reset the reloc and lineno counts, so that we can use them to
653 count the number of entries we have output so far. */
658 obj_sym_filepos (abfd) = line_filepos;
660 /* Figure out the largest number of symbols in an input BFD. Take
661 the opportunity to clear the output_has_begun fields of all the
664 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
668 sub->output_has_begun = false;
669 sz = obj_raw_syment_count (sub);
670 if (sz > max_sym_count)
674 /* Allocate some buffers used while linking. */
675 finfo.internal_syms = ((struct internal_syment *)
676 malloc (max_sym_count
677 * sizeof (struct internal_syment)));
678 finfo.sec_ptrs = (asection **) malloc (max_sym_count * sizeof (asection *));
679 finfo.sym_indices = (long *) malloc (max_sym_count * sizeof (long));
680 finfo.outsyms = ((bfd_byte *)
681 malloc ((size_t) ((max_sym_count + 1) * symesz)));
682 finfo.linenos = (bfd_byte *) malloc (max_lineno_count
683 * bfd_coff_linesz (abfd));
684 finfo.contents = (bfd_byte *) malloc (max_contents_size);
685 finfo.external_relocs = (bfd_byte *) malloc (max_reloc_count * relsz);
686 if (! info->relocateable)
687 finfo.internal_relocs = ((struct internal_reloc *)
688 malloc (max_reloc_count
689 * sizeof (struct internal_reloc)));
690 if ((finfo.internal_syms == NULL && max_sym_count > 0)
691 || (finfo.sec_ptrs == NULL && max_sym_count > 0)
692 || (finfo.sym_indices == NULL && max_sym_count > 0)
693 || finfo.outsyms == NULL
694 || (finfo.linenos == NULL && max_lineno_count > 0)
695 || (finfo.contents == NULL && max_contents_size > 0)
696 || (finfo.external_relocs == NULL && max_reloc_count > 0)
697 || (! info->relocateable
698 && finfo.internal_relocs == NULL
699 && max_reloc_count > 0))
701 bfd_set_error (bfd_error_no_memory);
705 /* We now know the position of everything in the file, except that
706 we don't know the size of the symbol table and therefore we don't
707 know where the string table starts. We just build the string
708 table in memory as we go along. We process all the relocations
709 for a single input file at once. */
710 obj_raw_syment_count (abfd) = 0;
712 if (coff_backend_info (abfd)->_bfd_coff_start_final_link)
714 if (! bfd_coff_start_final_link (abfd, info))
718 for (o = abfd->sections; o != NULL; o = o->next)
720 for (p = o->link_order_head; p != NULL; p = p->next)
722 if (p->type == bfd_indirect_link_order
723 && (bfd_get_flavour (p->u.indirect.section->owner)
724 == bfd_target_coff_flavour))
726 sub = p->u.indirect.section->owner;
727 if (! sub->output_has_begun)
729 if (! coff_link_input_bfd (&finfo, sub))
731 sub->output_has_begun = true;
734 else if (p->type == bfd_section_reloc_link_order
735 || p->type == bfd_symbol_reloc_link_order)
737 if (! coff_reloc_link_order (abfd, &finfo, o, p))
742 if (! _bfd_default_link_order (abfd, info, o, p))
748 /* Free up the buffers used by coff_link_input_bfd. */
749 if (finfo.internal_syms != NULL)
751 free (finfo.internal_syms);
752 finfo.internal_syms = NULL;
754 if (finfo.sec_ptrs != NULL)
756 free (finfo.sec_ptrs);
757 finfo.sec_ptrs = NULL;
759 if (finfo.sym_indices != NULL)
761 free (finfo.sym_indices);
762 finfo.sym_indices = NULL;
764 if (finfo.linenos != NULL)
766 free (finfo.linenos);
767 finfo.linenos = NULL;
769 if (finfo.contents != NULL)
771 free (finfo.contents);
772 finfo.contents = NULL;
774 if (finfo.external_relocs != NULL)
776 free (finfo.external_relocs);
777 finfo.external_relocs = NULL;
779 if (finfo.internal_relocs != NULL)
781 free (finfo.internal_relocs);
782 finfo.internal_relocs = NULL;
785 /* The value of the last C_FILE symbol is supposed to be the symbol
786 index of the first external symbol. Write it out again if
788 if (finfo.last_file_index != -1
789 && (unsigned int) finfo.last_file.n_value != obj_raw_syment_count (abfd))
791 finfo.last_file.n_value = obj_raw_syment_count (abfd);
792 bfd_coff_swap_sym_out (abfd, (PTR) &finfo.last_file,
793 (PTR) finfo.outsyms);
795 (obj_sym_filepos (abfd)
796 + finfo.last_file_index * symesz),
798 || bfd_write (finfo.outsyms, symesz, 1, abfd) != symesz)
802 /* Write out the global symbols. */
803 finfo.failed = false;
804 coff_link_hash_traverse (coff_hash_table (info), coff_write_global_sym,
809 /* The outsyms buffer is used by coff_write_global_sym. */
810 if (finfo.outsyms != NULL)
812 free (finfo.outsyms);
813 finfo.outsyms = NULL;
816 if (info->relocateable)
818 /* Now that we have written out all the global symbols, we know
819 the symbol indices to use for relocs against them, and we can
820 finally write out the relocs. */
821 external_relocs = (bfd_byte *) malloc (max_output_reloc_count * relsz);
822 if (external_relocs == NULL)
824 bfd_set_error (bfd_error_no_memory);
828 for (o = abfd->sections; o != NULL; o = o->next)
830 struct internal_reloc *irel;
831 struct internal_reloc *irelend;
832 struct coff_link_hash_entry **rel_hash;
835 if (o->reloc_count == 0)
838 irel = finfo.section_info[o->target_index].relocs;
839 irelend = irel + o->reloc_count;
840 rel_hash = finfo.section_info[o->target_index].rel_hashes;
841 erel = external_relocs;
842 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
844 if (*rel_hash != NULL)
846 BFD_ASSERT ((*rel_hash)->indx >= 0);
847 irel->r_symndx = (*rel_hash)->indx;
849 bfd_coff_swap_reloc_out (abfd, (PTR) irel, (PTR) erel);
852 if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0
853 || bfd_write ((PTR) external_relocs, relsz, o->reloc_count,
854 abfd) != relsz * o->reloc_count)
858 free (external_relocs);
859 external_relocs = NULL;
862 /* Free up the section information. */
863 if (finfo.section_info != NULL)
867 for (i = 0; i < abfd->section_count; i++)
869 if (finfo.section_info[i].relocs != NULL)
870 free (finfo.section_info[i].relocs);
871 if (finfo.section_info[i].rel_hashes != NULL)
872 free (finfo.section_info[i].rel_hashes);
874 free (finfo.section_info);
875 finfo.section_info = NULL;
878 /* Write out the string table. */
880 (obj_sym_filepos (abfd)
881 + obj_raw_syment_count (abfd) * symesz),
885 #if STRING_SIZE_SIZE == 4
887 _bfd_stringtab_size (finfo.strtab) + STRING_SIZE_SIZE,
888 (bfd_byte *) strbuf);
890 #error Change bfd_h_put_32
893 if (bfd_write (strbuf, 1, STRING_SIZE_SIZE, abfd) != STRING_SIZE_SIZE)
896 if (! _bfd_stringtab_emit (abfd, finfo.strtab))
899 _bfd_stringtab_free (finfo.strtab);
901 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
902 not try to write out the symbols. */
903 bfd_get_symcount (abfd) = 0;
908 if (finfo.strtab != NULL)
909 _bfd_stringtab_free (finfo.strtab);
910 if (finfo.section_info != NULL)
914 for (i = 0; i < abfd->section_count; i++)
916 if (finfo.section_info[i].relocs != NULL)
917 free (finfo.section_info[i].relocs);
918 if (finfo.section_info[i].rel_hashes != NULL)
919 free (finfo.section_info[i].rel_hashes);
921 free (finfo.section_info);
923 if (finfo.internal_syms != NULL)
924 free (finfo.internal_syms);
925 if (finfo.sec_ptrs != NULL)
926 free (finfo.sec_ptrs);
927 if (finfo.sym_indices != NULL)
928 free (finfo.sym_indices);
929 if (finfo.outsyms != NULL)
930 free (finfo.outsyms);
931 if (finfo.linenos != NULL)
932 free (finfo.linenos);
933 if (finfo.contents != NULL)
934 free (finfo.contents);
935 if (finfo.external_relocs != NULL)
936 free (finfo.external_relocs);
937 if (finfo.internal_relocs != NULL)
938 free (finfo.internal_relocs);
939 if (external_relocs != NULL)
940 free (external_relocs);
944 /* Read in and swap the relocs. This returns a buffer holding the
945 relocs for section SEC in file ABFD. If CACHE is true and
946 INTERNAL_RELOCS is NULL, the relocs read in wil be saved in case
947 the function is called again. If EXTERNAL_RELOCS is not NULL, it
948 is a buffer large enough to hold the unswapped relocs. If
949 INTERNAL_RELOCS is not NULL, it is a buffer large enough to hold
950 the swapped relocs. If REQUIRE_INTERNAL is true, then the return
951 value must be INTERNAL_RELOCS. The function returns NULL on error. */
953 struct internal_reloc *
954 _bfd_coff_read_internal_relocs (abfd, sec, cache, external_relocs,
955 require_internal, internal_relocs)
959 bfd_byte *external_relocs;
960 boolean require_internal;
961 struct internal_reloc *internal_relocs;
964 bfd_byte *free_external = NULL;
965 struct internal_reloc *free_internal = NULL;
968 struct internal_reloc *irel;
970 if (coff_section_data (abfd, sec) != NULL
971 && coff_section_data (abfd, sec)->relocs != NULL)
973 if (! require_internal)
974 return coff_section_data (abfd, sec)->relocs;
975 memcpy (internal_relocs, coff_section_data (abfd, sec)->relocs,
976 sec->reloc_count * sizeof (struct internal_reloc));
977 return internal_relocs;
980 relsz = bfd_coff_relsz (abfd);
982 if (external_relocs == NULL)
984 free_external = (bfd_byte *) malloc (sec->reloc_count * relsz);
985 if (free_external == NULL && sec->reloc_count > 0)
987 bfd_set_error (bfd_error_no_memory);
990 external_relocs = free_external;
993 if (bfd_seek (abfd, sec->rel_filepos, SEEK_SET) != 0
994 || (bfd_read (external_relocs, relsz, sec->reloc_count, abfd)
995 != relsz * sec->reloc_count))
998 if (internal_relocs == NULL)
1000 free_internal = ((struct internal_reloc *)
1001 malloc (sec->reloc_count
1002 * sizeof (struct internal_reloc)));
1003 if (free_internal == NULL && sec->reloc_count > 0)
1005 bfd_set_error (bfd_error_no_memory);
1008 internal_relocs = free_internal;
1011 /* Swap in the relocs. */
1012 erel = external_relocs;
1013 erel_end = erel + relsz * sec->reloc_count;
1014 irel = internal_relocs;
1015 for (; erel < erel_end; erel += relsz, irel++)
1016 bfd_coff_swap_reloc_in (abfd, (PTR) erel, (PTR) irel);
1018 if (free_external != NULL)
1020 free (free_external);
1021 free_external = NULL;
1024 if (cache && free_internal != NULL)
1026 if (coff_section_data (abfd, sec) == NULL)
1028 sec->used_by_bfd = ((PTR) bfd_zalloc (abfd,
1029 sizeof (struct coff_section_tdata)));
1030 if (sec->used_by_bfd == NULL)
1032 bfd_set_error (bfd_error_no_memory);
1035 coff_section_data (abfd, sec)->contents = NULL;
1037 coff_section_data (abfd, sec)->relocs = free_internal;
1040 return internal_relocs;
1043 if (free_external != NULL)
1044 free (free_external);
1045 if (free_internal != NULL)
1046 free (free_internal);
1052 /* parse out a -heap <reserved>,<commit> line */
1055 dores_com (ptr, output_bfd, heap)
1060 if (coff_data(output_bfd)->pe)
1062 int val = strtoul (ptr, &ptr, 0);
1064 pe_data(output_bfd)->pe_opthdr.SizeOfHeapReserve =val;
1066 pe_data(output_bfd)->pe_opthdr.SizeOfStackReserve =val;
1070 int val = strtoul (ptr+1, &ptr, 0);
1072 pe_data(output_bfd)->pe_opthdr.SizeOfHeapCommit =val;
1074 pe_data(output_bfd)->pe_opthdr.SizeOfStackCommit =val;
1080 static char *get_name(ptr, dst)
1087 while (*ptr && *ptr != ' ')
1092 /* Process any magic embedded commands in a section called .drectve */
1095 process_embedded_commands (output_bfd, info, abfd)
1097 struct bfd_link_info *info;
1100 asection *sec = bfd_get_section_by_name (abfd, ".drectve");
1107 copy = malloc ((size_t) sec->_raw_size);
1110 bfd_set_error (bfd_error_no_memory);
1113 if (! bfd_get_section_contents(abfd, sec, copy, 0, sec->_raw_size))
1118 e = copy + sec->_raw_size;
1119 for (s = copy; s < e ; )
1125 if (strncmp (s,"-attr", 5) == 0)
1137 s = get_name(s, &name);
1138 s = get_name(s, &attribs);
1158 asec = bfd_get_section_by_name (abfd, name);
1161 asec->flags |= SEC_CODE;
1163 asec->flags |= SEC_READONLY;
1166 else if (strncmp (s,"-heap", 5) == 0)
1168 s = dores_com (s+5, output_bfd, 1);
1170 else if (strncmp (s,"-stack", 6) == 0)
1172 s = dores_com (s+6, output_bfd, 0);
1181 /* Link an input file into the linker output file. This function
1182 handles all the sections and relocations of the input file at once. */
1185 coff_link_input_bfd (finfo, input_bfd)
1186 struct coff_final_link_info *finfo;
1189 boolean (*sym_is_global) PARAMS ((bfd *, struct internal_syment *));
1190 boolean (*adjust_symndx) PARAMS ((bfd *, struct bfd_link_info *, bfd *,
1191 asection *, struct internal_reloc *,
1194 const char *strings;
1195 bfd_size_type syment_base;
1196 unsigned int n_tmask;
1197 unsigned int n_btshft;
1199 bfd_size_type isymesz;
1200 bfd_size_type osymesz;
1201 bfd_size_type linesz;
1204 struct internal_syment *isymp;
1207 unsigned long output_index;
1209 struct coff_link_hash_entry **sym_hash;
1212 /* Move all the symbols to the output file. */
1214 output_bfd = finfo->output_bfd;
1215 sym_is_global = coff_backend_info (input_bfd)->_bfd_coff_sym_is_global;
1217 syment_base = obj_raw_syment_count (output_bfd);
1218 isymesz = bfd_coff_symesz (input_bfd);
1219 osymesz = bfd_coff_symesz (output_bfd);
1220 linesz = bfd_coff_linesz (input_bfd);
1221 BFD_ASSERT (linesz == bfd_coff_linesz (output_bfd));
1223 n_tmask = coff_data (input_bfd)->local_n_tmask;
1224 n_btshft = coff_data (input_bfd)->local_n_btshft;
1226 /* Define macros so that ISFCN, et. al., macros work correctly. */
1227 #define N_TMASK n_tmask
1228 #define N_BTSHFT n_btshft
1231 if (! finfo->info->keep_memory)
1234 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
1237 if (! _bfd_coff_get_external_symbols (input_bfd))
1240 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
1241 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
1242 isymp = finfo->internal_syms;
1243 secpp = finfo->sec_ptrs;
1244 indexp = finfo->sym_indices;
1245 output_index = syment_base;
1246 outsym = finfo->outsyms;
1248 if (coff_data(output_bfd)->pe)
1250 if (!process_embedded_commands (output_bfd, finfo->info, input_bfd))
1254 while (esym < esym_end)
1256 struct internal_syment isym;
1261 bfd_coff_swap_sym_in (input_bfd, (PTR) esym, (PTR) isymp);
1263 /* Make a copy of *isymp so that the relocate_section function
1264 always sees the original values. This is more reliable than
1265 always recomputing the symbol value even if we are stripping
1269 if (isym.n_scnum != 0)
1270 *secpp = coff_section_from_bfd_index (input_bfd, isym.n_scnum);
1273 if (isym.n_value == 0)
1274 *secpp = bfd_und_section_ptr;
1276 *secpp = bfd_com_section_ptr;
1283 add = 1 + isym.n_numaux;
1285 /* If we are stripping all symbols, we want to skip this one. */
1286 if (finfo->info->strip == strip_all)
1291 if (isym.n_sclass == C_EXT
1292 || (sym_is_global && (*sym_is_global) (input_bfd, &isym)))
1294 /* This is a global symbol. Global symbols come at the
1295 end of the symbol table, so skip them for now.
1296 Function symbols, however, are an exception, and are
1297 not moved to the end. */
1299 if (! ISFCN (isym.n_type))
1304 /* This is a local symbol. Skip it if we are discarding
1306 if (finfo->info->discard == discard_all)
1311 /* If we stripping debugging symbols, and this is a debugging
1312 symbol, then skip it. */
1314 && finfo->info->strip == strip_debugger
1315 && isym.n_scnum == N_DEBUG)
1318 /* If some symbols are stripped based on the name, work out the
1319 name and decide whether to skip this symbol. */
1321 && (finfo->info->strip == strip_some
1322 || finfo->info->discard == discard_l))
1325 char buf[SYMNMLEN + 1];
1327 name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf);
1331 if ((finfo->info->strip == strip_some
1332 && (bfd_hash_lookup (finfo->info->keep_hash, name, false,
1335 && finfo->info->discard == discard_l
1336 && strncmp (name, finfo->info->lprefix,
1337 finfo->info->lprefix_len) == 0))
1341 /* We now know whether we are to skip this symbol or not. */
1344 /* Adjust the symbol in order to output it. */
1346 if (isym._n._n_n._n_zeroes == 0
1347 && isym._n._n_n._n_offset != 0)
1352 /* This symbol has a long name. Enter it in the string
1353 table we are building. Note that we do not check
1354 bfd_coff_symname_in_debug. That is only true for
1355 XCOFF, and XCOFF requires different linking code
1357 name = _bfd_coff_internal_syment_name (input_bfd, &isym,
1361 indx = _bfd_stringtab_add (finfo->strtab, name, hash, copy);
1362 if (indx == (bfd_size_type) -1)
1364 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
1367 if (isym.n_scnum > 0)
1369 isym.n_scnum = (*secpp)->output_section->target_index;
1370 isym.n_value += ((*secpp)->output_section->vma
1371 + (*secpp)->output_offset
1375 /* The value of a C_FILE symbol is the symbol index of the
1376 next C_FILE symbol. The value of the last C_FILE symbol
1377 is the symbol index to the first external symbol
1378 (actually, coff_renumber_symbols does not get this
1379 right--it just sets the value of the last C_FILE symbol
1380 to zero--and nobody has ever complained about it). We
1381 try to get this right, below, just before we write the
1382 symbols out, but in the general case we may have to write
1383 the symbol out twice. */
1384 if (isym.n_sclass == C_FILE)
1386 if (finfo->last_file_index != -1
1387 && finfo->last_file.n_value != (long) output_index)
1389 /* We must correct the value of the last C_FILE entry. */
1390 finfo->last_file.n_value = output_index;
1391 if ((bfd_size_type) finfo->last_file_index >= syment_base)
1393 /* The last C_FILE symbol is in this input file. */
1394 bfd_coff_swap_sym_out (output_bfd,
1395 (PTR) &finfo->last_file,
1396 (PTR) (finfo->outsyms
1397 + ((finfo->last_file_index
1403 /* We have already written out the last C_FILE
1404 symbol. We need to write it out again. We
1405 borrow *outsym temporarily. */
1406 bfd_coff_swap_sym_out (output_bfd,
1407 (PTR) &finfo->last_file,
1409 if (bfd_seek (output_bfd,
1410 (obj_sym_filepos (output_bfd)
1411 + finfo->last_file_index * osymesz),
1413 || (bfd_write (outsym, osymesz, 1, output_bfd)
1419 finfo->last_file_index = output_index;
1420 finfo->last_file = isym;
1423 /* Output the symbol. */
1425 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
1427 *indexp = output_index;
1432 struct coff_link_hash_entry *h;
1434 indx = ((esym - (bfd_byte *) obj_coff_external_syms (input_bfd))
1436 h = obj_coff_sym_hashes (input_bfd)[indx];
1437 BFD_ASSERT (h != NULL);
1438 h->indx = output_index;
1441 output_index += add;
1442 outsym += add * osymesz;
1445 esym += add * isymesz;
1449 for (--add; add > 0; --add)
1456 /* Fix up the aux entries. This must be done in a separate pass,
1457 because we don't know the correct symbol indices until we have
1458 already decided which symbols we are going to keep. */
1460 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
1461 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
1462 isymp = finfo->internal_syms;
1463 indexp = finfo->sym_indices;
1464 sym_hash = obj_coff_sym_hashes (input_bfd);
1465 outsym = finfo->outsyms;
1466 while (esym < esym_end)
1470 add = 1 + isymp->n_numaux;
1473 && (*sym_hash == NULL
1474 || (*sym_hash)->auxbfd != input_bfd))
1475 esym += add * isymesz;
1478 struct coff_link_hash_entry *h;
1485 BFD_ASSERT (h->numaux == isymp->n_numaux);
1493 /* Handle the aux entries. This handling is based on
1494 coff_pointerize_aux. I don't know if it always correct. */
1495 for (i = 0; i < isymp->n_numaux && esym < esym_end; i++)
1497 union internal_auxent aux;
1498 union internal_auxent *auxp;
1504 bfd_coff_swap_aux_in (input_bfd, (PTR) esym, isymp->n_type,
1505 isymp->n_sclass, i, isymp->n_numaux,
1510 if (isymp->n_sclass == C_FILE)
1512 /* If this is a long filename, we must put it in the
1514 if (auxp->x_file.x_n.x_zeroes == 0
1515 && auxp->x_file.x_n.x_offset != 0)
1517 const char *filename;
1520 BFD_ASSERT (auxp->x_file.x_n.x_offset
1521 >= STRING_SIZE_SIZE);
1522 if (strings == NULL)
1524 strings = _bfd_coff_read_string_table (input_bfd);
1525 if (strings == NULL)
1528 filename = strings + auxp->x_file.x_n.x_offset;
1529 indx = _bfd_stringtab_add (finfo->strtab, filename,
1531 if (indx == (bfd_size_type) -1)
1533 auxp->x_file.x_n.x_offset = STRING_SIZE_SIZE + indx;
1536 else if (isymp->n_sclass != C_STAT || isymp->n_type != T_NULL)
1540 if (ISFCN (isymp->n_type)
1541 || ISTAG (isymp->n_sclass)
1542 || isymp->n_sclass == C_BLOCK)
1544 indx = auxp->x_sym.x_fcnary.x_fcn.x_endndx.l;
1546 && indx < obj_raw_syment_count (input_bfd))
1548 /* We look forward through the symbol for
1549 the index of the next symbol we are going
1550 to include. I don't know if this is
1552 while (finfo->sym_indices[indx] < 0
1553 && indx < obj_raw_syment_count (input_bfd))
1555 if (indx >= obj_raw_syment_count (input_bfd))
1556 indx = output_index;
1558 indx = finfo->sym_indices[indx];
1559 auxp->x_sym.x_fcnary.x_fcn.x_endndx.l = indx;
1563 indx = auxp->x_sym.x_tagndx.l;
1564 if (indx > 0 && indx < obj_raw_syment_count (input_bfd))
1568 symindx = finfo->sym_indices[indx];
1570 auxp->x_sym.x_tagndx.l = 0;
1572 auxp->x_sym.x_tagndx.l = symindx;
1578 bfd_coff_swap_aux_out (output_bfd, (PTR) auxp, isymp->n_type,
1579 isymp->n_sclass, i, isymp->n_numaux,
1593 /* Relocate the line numbers, unless we are stripping them. */
1594 if (finfo->info->strip == strip_none
1595 || finfo->info->strip == strip_some)
1597 for (o = input_bfd->sections; o != NULL; o = o->next)
1603 /* FIXME: If SEC_HAS_CONTENTS is not for the section, then
1604 build_link_order in ldwrite.c will not have created a
1605 link order, which means that we will not have seen this
1606 input section in _bfd_coff_final_link, which means that
1607 we will not have allocated space for the line numbers of
1608 this section. I don't think line numbers can be
1609 meaningful for a section which does not have
1610 SEC_HAS_CONTENTS set, but, if they do, this must be
1612 if (o->lineno_count == 0
1613 || (o->output_section->flags & SEC_HAS_CONTENTS) == 0)
1616 if (bfd_seek (input_bfd, o->line_filepos, SEEK_SET) != 0
1617 || bfd_read (finfo->linenos, linesz, o->lineno_count,
1618 input_bfd) != linesz * o->lineno_count)
1621 offset = o->output_section->vma + o->output_offset - o->vma;
1622 eline = finfo->linenos;
1623 elineend = eline + linesz * o->lineno_count;
1624 for (; eline < elineend; eline += linesz)
1626 struct internal_lineno iline;
1628 bfd_coff_swap_lineno_in (input_bfd, (PTR) eline, (PTR) &iline);
1630 if (iline.l_lnno != 0)
1631 iline.l_addr.l_paddr += offset;
1632 else if (iline.l_addr.l_symndx >= 0
1633 && ((unsigned long) iline.l_addr.l_symndx
1634 < obj_raw_syment_count (input_bfd)))
1638 indx = finfo->sym_indices[iline.l_addr.l_symndx];
1642 /* These line numbers are attached to a symbol
1643 which we are stripping. We should really
1644 just discard the line numbers, but that would
1645 be a pain because we have already counted
1651 struct internal_syment is;
1652 union internal_auxent ia;
1654 /* Fix up the lnnoptr field in the aux entry of
1655 the symbol. It turns out that we can't do
1656 this when we modify the symbol aux entries,
1657 because gas sometimes screws up the lnnoptr
1658 field and makes it an offset from the start
1659 of the line numbers rather than an absolute
1661 bfd_coff_swap_sym_in (output_bfd,
1662 (PTR) (finfo->outsyms
1663 + ((indx - syment_base)
1666 if ((ISFCN (is.n_type)
1667 || is.n_sclass == C_BLOCK)
1668 && is.n_numaux >= 1)
1672 auxptr = (PTR) (finfo->outsyms
1673 + ((indx - syment_base + 1)
1675 bfd_coff_swap_aux_in (output_bfd, auxptr,
1676 is.n_type, is.n_sclass,
1677 0, is.n_numaux, (PTR) &ia);
1678 ia.x_sym.x_fcnary.x_fcn.x_lnnoptr =
1679 (o->output_section->line_filepos
1680 + o->output_section->lineno_count * linesz
1681 + eline - finfo->linenos);
1682 bfd_coff_swap_aux_out (output_bfd, (PTR) &ia,
1683 is.n_type, is.n_sclass, 0,
1684 is.n_numaux, auxptr);
1688 iline.l_addr.l_symndx = indx;
1691 bfd_coff_swap_lineno_out (output_bfd, (PTR) &iline, (PTR) eline);
1694 if (bfd_seek (output_bfd,
1695 (o->output_section->line_filepos
1696 + o->output_section->lineno_count * linesz),
1698 || bfd_write (finfo->linenos, linesz, o->lineno_count,
1699 output_bfd) != linesz * o->lineno_count)
1702 o->output_section->lineno_count += o->lineno_count;
1706 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
1707 symbol will be the first symbol in the next input file. In the
1708 normal case, this will save us from writing out the C_FILE symbol
1710 if (finfo->last_file_index != -1
1711 && (bfd_size_type) finfo->last_file_index >= syment_base)
1713 finfo->last_file.n_value = output_index;
1714 bfd_coff_swap_sym_out (output_bfd, (PTR) &finfo->last_file,
1715 (PTR) (finfo->outsyms
1716 + ((finfo->last_file_index - syment_base)
1720 /* Write the modified symbols to the output file. */
1721 if (outsym > finfo->outsyms)
1723 if (bfd_seek (output_bfd,
1724 obj_sym_filepos (output_bfd) + syment_base * osymesz,
1726 || (bfd_write (finfo->outsyms, outsym - finfo->outsyms, 1,
1728 != (bfd_size_type) (outsym - finfo->outsyms)))
1731 BFD_ASSERT ((obj_raw_syment_count (output_bfd)
1732 + (outsym - finfo->outsyms) / osymesz)
1735 obj_raw_syment_count (output_bfd) = output_index;
1738 /* Relocate the contents of each section. */
1739 adjust_symndx = coff_backend_info (input_bfd)->_bfd_coff_adjust_symndx;
1740 for (o = input_bfd->sections; o != NULL; o = o->next)
1744 if ((o->flags & SEC_HAS_CONTENTS) == 0)
1746 if ((o->flags & SEC_RELOC) != 0
1747 && o->reloc_count != 0)
1749 ((*_bfd_error_handler)
1750 ("%s: relocs in section `%s', but it has no contents",
1751 bfd_get_filename (input_bfd),
1752 bfd_get_section_name (input_bfd, o)));
1753 bfd_set_error (bfd_error_no_contents);
1760 if (coff_section_data (input_bfd, o) != NULL
1761 && coff_section_data (input_bfd, o)->contents != NULL)
1762 contents = coff_section_data (input_bfd, o)->contents;
1765 if (! bfd_get_section_contents (input_bfd, o, finfo->contents,
1766 (file_ptr) 0, o->_raw_size))
1768 contents = finfo->contents;
1771 if ((o->flags & SEC_RELOC) != 0)
1774 struct internal_reloc *internal_relocs;
1775 struct internal_reloc *irel;
1777 /* Read in the relocs. */
1778 target_index = o->output_section->target_index;
1779 internal_relocs = (_bfd_coff_read_internal_relocs
1780 (input_bfd, o, false, finfo->external_relocs,
1781 finfo->info->relocateable,
1782 (finfo->info->relocateable
1783 ? (finfo->section_info[target_index].relocs
1784 + o->output_section->reloc_count)
1785 : finfo->internal_relocs)));
1786 if (internal_relocs == NULL)
1789 /* Call processor specific code to relocate the section
1791 if (! bfd_coff_relocate_section (output_bfd, finfo->info,
1795 finfo->internal_syms,
1799 if (finfo->info->relocateable)
1802 struct internal_reloc *irelend;
1803 struct coff_link_hash_entry **rel_hash;
1805 offset = o->output_section->vma + o->output_offset - o->vma;
1806 irel = internal_relocs;
1807 irelend = irel + o->reloc_count;
1808 rel_hash = (finfo->section_info[target_index].rel_hashes
1809 + o->output_section->reloc_count);
1810 for (; irel < irelend; irel++, rel_hash++)
1812 struct coff_link_hash_entry *h;
1817 /* Adjust the reloc address and symbol index. */
1819 irel->r_vaddr += offset;
1821 if (irel->r_symndx == -1)
1826 if (! (*adjust_symndx) (output_bfd, finfo->info,
1834 h = obj_coff_sym_hashes (input_bfd)[irel->r_symndx];
1837 /* This is a global symbol. */
1839 irel->r_symndx = h->indx;
1842 /* This symbol is being written at the end
1843 of the file, and we do not yet know the
1844 symbol index. We save the pointer to the
1845 hash table entry in the rel_hash list.
1846 We set the indx field to -2 to indicate
1847 that this symbol must not be stripped. */
1856 indx = finfo->sym_indices[irel->r_symndx];
1858 irel->r_symndx = indx;
1861 struct internal_syment *is;
1863 char buf[SYMNMLEN + 1];
1865 /* This reloc is against a symbol we are
1866 stripping. It would be possible to
1867 handle this case, but I don't think it's
1869 is = finfo->internal_syms + irel->r_symndx;
1871 name = (_bfd_coff_internal_syment_name
1872 (input_bfd, is, buf));
1876 if (! ((*finfo->info->callbacks->unattached_reloc)
1877 (finfo->info, name, input_bfd, o,
1884 o->output_section->reloc_count += o->reloc_count;
1888 /* Write out the modified section contents. */
1889 if (! bfd_set_section_contents (output_bfd, o->output_section,
1890 contents, o->output_offset,
1891 (o->_cooked_size != 0
1897 if (! finfo->info->keep_memory)
1899 if (! _bfd_coff_free_symbols (input_bfd))
1906 /* Write out a global symbol. Called via coff_link_hash_traverse. */
1909 coff_write_global_sym (h, data)
1910 struct coff_link_hash_entry *h;
1913 struct coff_final_link_info *finfo = (struct coff_final_link_info *) data;
1915 struct internal_syment isym;
1916 bfd_size_type symesz;
1919 output_bfd = finfo->output_bfd;
1925 && (finfo->info->strip == strip_all
1926 || (finfo->info->strip == strip_some
1927 && (bfd_hash_lookup (finfo->info->keep_hash,
1928 h->root.root.string, false, false)
1932 switch (h->root.type)
1935 case bfd_link_hash_new:
1939 case bfd_link_hash_undefined:
1940 case bfd_link_hash_undefweak:
1941 isym.n_scnum = N_UNDEF;
1945 case bfd_link_hash_defined:
1946 case bfd_link_hash_defweak:
1950 sec = h->root.u.def.section->output_section;
1951 if (bfd_is_abs_section (sec))
1952 isym.n_scnum = N_ABS;
1954 isym.n_scnum = sec->target_index;
1955 isym.n_value = (h->root.u.def.value
1957 + h->root.u.def.section->output_offset);
1961 case bfd_link_hash_common:
1962 isym.n_scnum = N_UNDEF;
1963 isym.n_value = h->root.u.c.size;
1966 case bfd_link_hash_indirect:
1967 case bfd_link_hash_warning:
1968 /* Just ignore these. They can't be handled anyhow. */
1972 if (strlen (h->root.root.string) <= SYMNMLEN)
1973 strncpy (isym._n._n_name, h->root.root.string, SYMNMLEN);
1980 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
1982 indx = _bfd_stringtab_add (finfo->strtab, h->root.root.string, hash,
1984 if (indx == (bfd_size_type) -1)
1986 finfo->failed = true;
1989 isym._n._n_n._n_zeroes = 0;
1990 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
1993 isym.n_sclass = h->class;
1994 isym.n_type = h->type;
1996 if (isym.n_sclass == C_NULL)
1997 isym.n_sclass = C_EXT;
1999 isym.n_numaux = h->numaux;
2001 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) finfo->outsyms);
2003 symesz = bfd_coff_symesz (output_bfd);
2005 if (bfd_seek (output_bfd,
2006 (obj_sym_filepos (output_bfd)
2007 + obj_raw_syment_count (output_bfd) * symesz),
2009 || bfd_write (finfo->outsyms, symesz, 1, output_bfd) != symesz)
2011 finfo->failed = true;
2015 h->indx = obj_raw_syment_count (output_bfd);
2017 ++obj_raw_syment_count (output_bfd);
2019 /* Write out any associated aux entries. There normally will be
2020 none. If there are any, I have no idea how to modify them. */
2021 for (i = 0; i < isym.n_numaux; i++)
2023 bfd_coff_swap_aux_out (output_bfd, (PTR) (h->aux + i), isym.n_type,
2024 isym.n_sclass, i, isym.n_numaux,
2025 (PTR) finfo->outsyms);
2026 if (bfd_write (finfo->outsyms, symesz, 1, output_bfd) != symesz)
2028 finfo->failed = true;
2031 ++obj_raw_syment_count (output_bfd);
2037 /* Handle a link order which is supposed to generate a reloc. */
2040 coff_reloc_link_order (output_bfd, finfo, output_section, link_order)
2042 struct coff_final_link_info *finfo;
2043 asection *output_section;
2044 struct bfd_link_order *link_order;
2046 reloc_howto_type *howto;
2047 struct internal_reloc *irel;
2048 struct coff_link_hash_entry **rel_hash_ptr;
2050 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
2053 bfd_set_error (bfd_error_bad_value);
2057 if (link_order->u.reloc.p->addend != 0)
2061 bfd_reloc_status_type rstat;
2064 size = bfd_get_reloc_size (howto);
2065 buf = (bfd_byte *) bfd_zmalloc (size);
2068 bfd_set_error (bfd_error_no_memory);
2072 rstat = _bfd_relocate_contents (howto, output_bfd,
2073 link_order->u.reloc.p->addend, buf);
2079 case bfd_reloc_outofrange:
2081 case bfd_reloc_overflow:
2082 if (! ((*finfo->info->callbacks->reloc_overflow)
2084 (link_order->type == bfd_section_reloc_link_order
2085 ? bfd_section_name (output_bfd,
2086 link_order->u.reloc.p->u.section)
2087 : link_order->u.reloc.p->u.name),
2088 howto->name, link_order->u.reloc.p->addend,
2089 (bfd *) NULL, (asection *) NULL, (bfd_vma) 0)))
2096 ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf,
2097 (file_ptr) link_order->offset, size);
2103 /* Store the reloc information in the right place. It will get
2104 swapped and written out at the end of the final_link routine. */
2106 irel = (finfo->section_info[output_section->target_index].relocs
2107 + output_section->reloc_count);
2108 rel_hash_ptr = (finfo->section_info[output_section->target_index].rel_hashes
2109 + output_section->reloc_count);
2111 memset (irel, 0, sizeof (struct internal_reloc));
2112 *rel_hash_ptr = NULL;
2114 irel->r_vaddr = output_section->vma + link_order->offset;
2116 if (link_order->type == bfd_section_reloc_link_order)
2118 /* We need to somehow locate a symbol in the right section. The
2119 symbol must either have a value of zero, or we must adjust
2120 the addend by the value of the symbol. FIXME: Write this
2121 when we need it. The old linker couldn't handle this anyhow. */
2123 *rel_hash_ptr = NULL;
2128 struct coff_link_hash_entry *h;
2130 h = coff_link_hash_lookup (coff_hash_table (finfo->info),
2131 link_order->u.reloc.p->u.name,
2132 false, false, true);
2136 irel->r_symndx = h->indx;
2139 /* Set the index to -2 to force this symbol to get
2148 if (! ((*finfo->info->callbacks->unattached_reloc)
2149 (finfo->info, link_order->u.reloc.p->u.name, (bfd *) NULL,
2150 (asection *) NULL, (bfd_vma) 0)))
2156 /* FIXME: Is this always right? */
2157 irel->r_type = howto->type;
2159 /* r_size is only used on the RS/6000, which needs its own linker
2160 routines anyhow. r_extern is only used for ECOFF. */
2162 /* FIXME: What is the right value for r_offset? Is zero OK? */
2164 ++output_section->reloc_count;
2169 /* A basic reloc handling routine which may be used by processors with
2173 _bfd_coff_generic_relocate_section (output_bfd, info, input_bfd,
2174 input_section, contents, relocs, syms,
2177 struct bfd_link_info *info;
2179 asection *input_section;
2181 struct internal_reloc *relocs;
2182 struct internal_syment *syms;
2183 asection **sections;
2185 struct internal_reloc *rel;
2186 struct internal_reloc *relend;
2190 relend = rel + input_section->reloc_count;
2191 for (; rel < relend; rel++)
2194 struct coff_link_hash_entry *h;
2195 struct internal_syment *sym;
2198 reloc_howto_type *howto;
2199 bfd_reloc_status_type rstat;
2201 symndx = rel->r_symndx;
2210 h = obj_coff_sym_hashes (input_bfd)[symndx];
2211 sym = syms + symndx;
2214 /* COFF treats common symbols in one of two ways. Either the
2215 size of the symbol is included in the section contents, or it
2216 is not. We assume that the size is not included, and force
2217 the rtype_to_howto function to adjust the addend as needed. */
2219 if (sym != NULL && sym->n_scnum != 0)
2220 addend = - sym->n_value;
2225 howto = bfd_coff_rtype_to_howto (input_bfd, input_section, rel, h,
2238 sec = bfd_abs_section_ptr;
2243 sec = sections[symndx];
2244 val = (sec->output_section->vma
2245 + sec->output_offset
2252 if (h->root.type == bfd_link_hash_defined
2253 || h->root.type == bfd_link_hash_defweak)
2257 sec = h->root.u.def.section;
2258 val = (h->root.u.def.value
2259 + sec->output_section->vma
2260 + sec->output_offset);
2263 else if (! info->relocateable)
2265 if (! ((*info->callbacks->undefined_symbol)
2266 (info, h->root.root.string, input_bfd, input_section,
2267 rel->r_vaddr - input_section->vma)))
2272 if (info->base_file)
2274 /* So if this is non pcrelative, and is referenced
2275 to a section or a common symbol, then it needs a reloc */
2276 if (!howto->pc_relative
2277 && sym && (sym->n_scnum || sym->n_value))
2279 /* relocation to a symbol in a section which
2280 isn't absolute - we output the address here
2282 bfd_vma addr = rel->r_vaddr
2283 + input_section->output_offset
2284 + input_section->output_section->vma;
2285 if (coff_data(output_bfd)->pe)
2286 addr -= pe_data(output_bfd)->pe_opthdr.ImageBase;
2287 fwrite (&addr, 1,4, (FILE *) info->base_file);
2291 rstat = _bfd_final_link_relocate (howto, input_bfd, input_section,
2293 rel->r_vaddr - input_section->vma,
2302 case bfd_reloc_overflow:
2305 char buf[SYMNMLEN + 1];
2310 name = h->root.root.string;
2313 name = _bfd_coff_internal_syment_name (input_bfd, sym, buf);
2318 if (! ((*info->callbacks->reloc_overflow)
2319 (info, name, howto->name, (bfd_vma) 0, input_bfd,
2320 input_section, rel->r_vaddr - input_section->vma)))