1 /* COFF specific linker code.
2 Copyright 1994, 1995, 1996, 1997, 1998, 1999 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 *));
37 static char *dores_com PARAMS ((char *, bfd *, int));
38 static char *get_name PARAMS ((char *, char **));
39 static int process_embedded_commands
40 PARAMS ((bfd *, struct bfd_link_info *, bfd *));
41 static void mark_relocs PARAMS ((struct coff_final_link_info *, bfd *));
43 /* Define macros so that the ISFCN, et. al., macros work correctly.
44 These macros are defined in include/coff/internal.h in terms of
45 N_TMASK, etc. These definitions require a user to define local
46 variables with the appropriate names, and with values from the
47 coff_data (abfd) structure. */
49 #define N_TMASK n_tmask
50 #define N_BTSHFT n_btshft
51 #define N_BTMASK n_btmask
53 /* Create an entry in a COFF linker hash table. */
55 struct bfd_hash_entry *
56 _bfd_coff_link_hash_newfunc (entry, table, string)
57 struct bfd_hash_entry *entry;
58 struct bfd_hash_table *table;
61 struct coff_link_hash_entry *ret = (struct coff_link_hash_entry *) entry;
63 /* Allocate the structure if it has not already been allocated by a
65 if (ret == (struct coff_link_hash_entry *) NULL)
66 ret = ((struct coff_link_hash_entry *)
67 bfd_hash_allocate (table, sizeof (struct coff_link_hash_entry)));
68 if (ret == (struct coff_link_hash_entry *) NULL)
69 return (struct bfd_hash_entry *) ret;
71 /* Call the allocation method of the superclass. */
72 ret = ((struct coff_link_hash_entry *)
73 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
75 if (ret != (struct coff_link_hash_entry *) NULL)
77 /* Set local fields. */
86 return (struct bfd_hash_entry *) ret;
89 /* Initialize a COFF linker hash table. */
92 _bfd_coff_link_hash_table_init (table, abfd, newfunc)
93 struct coff_link_hash_table *table;
95 struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *,
96 struct bfd_hash_table *,
99 table->stab_info = NULL;
100 return _bfd_link_hash_table_init (&table->root, abfd, newfunc);
103 /* Create a COFF linker hash table. */
105 struct bfd_link_hash_table *
106 _bfd_coff_link_hash_table_create (abfd)
109 struct coff_link_hash_table *ret;
111 ret = ((struct coff_link_hash_table *)
112 bfd_alloc (abfd, sizeof (struct coff_link_hash_table)));
115 if (! _bfd_coff_link_hash_table_init (ret, abfd,
116 _bfd_coff_link_hash_newfunc))
118 bfd_release (abfd, ret);
119 return (struct bfd_link_hash_table *) NULL;
124 /* Create an entry in a COFF debug merge hash table. */
126 struct bfd_hash_entry *
127 _bfd_coff_debug_merge_hash_newfunc (entry, table, string)
128 struct bfd_hash_entry *entry;
129 struct bfd_hash_table *table;
132 struct coff_debug_merge_hash_entry *ret =
133 (struct coff_debug_merge_hash_entry *) entry;
135 /* Allocate the structure if it has not already been allocated by a
137 if (ret == (struct coff_debug_merge_hash_entry *) NULL)
138 ret = ((struct coff_debug_merge_hash_entry *)
139 bfd_hash_allocate (table,
140 sizeof (struct coff_debug_merge_hash_entry)));
141 if (ret == (struct coff_debug_merge_hash_entry *) NULL)
142 return (struct bfd_hash_entry *) ret;
144 /* Call the allocation method of the superclass. */
145 ret = ((struct coff_debug_merge_hash_entry *)
146 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
147 if (ret != (struct coff_debug_merge_hash_entry *) NULL)
149 /* Set local fields. */
153 return (struct bfd_hash_entry *) ret;
156 /* Given a COFF BFD, add symbols to the global hash table as
160 _bfd_coff_link_add_symbols (abfd, info)
162 struct bfd_link_info *info;
164 switch (bfd_get_format (abfd))
167 return coff_link_add_object_symbols (abfd, info);
169 return (_bfd_generic_link_add_archive_symbols
170 (abfd, info, coff_link_check_archive_element));
172 bfd_set_error (bfd_error_wrong_format);
177 /* Add symbols from a COFF object file. */
180 coff_link_add_object_symbols (abfd, info)
182 struct bfd_link_info *info;
184 if (! _bfd_coff_get_external_symbols (abfd))
186 if (! coff_link_add_symbols (abfd, info))
189 if (! info->keep_memory)
191 if (! _bfd_coff_free_symbols (abfd))
197 /* Check a single archive element to see if we need to include it in
198 the link. *PNEEDED is set according to whether this element is
199 needed in the link or not. This is called via
200 _bfd_generic_link_add_archive_symbols. */
203 coff_link_check_archive_element (abfd, info, pneeded)
205 struct bfd_link_info *info;
208 if (! _bfd_coff_get_external_symbols (abfd))
211 if (! coff_link_check_ar_symbols (abfd, info, pneeded))
216 if (! coff_link_add_symbols (abfd, info))
220 if (! info->keep_memory || ! *pneeded)
222 if (! _bfd_coff_free_symbols (abfd))
229 /* Look through the symbols to see if this object file should be
230 included in the link. */
233 coff_link_check_ar_symbols (abfd, info, pneeded)
235 struct bfd_link_info *info;
238 bfd_size_type symesz;
244 symesz = bfd_coff_symesz (abfd);
245 esym = (bfd_byte *) obj_coff_external_syms (abfd);
246 esym_end = esym + obj_raw_syment_count (abfd) * symesz;
247 while (esym < esym_end)
249 struct internal_syment sym;
250 enum coff_symbol_classification classification;
252 bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym);
254 classification = bfd_coff_classify_symbol (abfd, &sym);
255 if (classification == COFF_SYMBOL_GLOBAL
256 || classification == COFF_SYMBOL_COMMON)
259 char buf[SYMNMLEN + 1];
260 struct bfd_link_hash_entry *h;
262 /* This symbol is externally visible, and is defined by this
265 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
268 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
270 /* We are only interested in symbols that are currently
271 undefined. If a symbol is currently known to be common,
272 COFF linkers do not bring in an object file which defines
274 if (h != (struct bfd_link_hash_entry *) NULL
275 && h->type == bfd_link_hash_undefined)
277 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
284 esym += (sym.n_numaux + 1) * symesz;
287 /* We do not need this object file. */
291 /* Add all the symbols from an object file to the hash table. */
294 coff_link_add_symbols (abfd, info)
296 struct bfd_link_info *info;
299 /* These aren't needed yet. */
300 unsigned int n_tmask = coff_data (abfd)->local_n_tmask;
301 unsigned int n_btshft = coff_data (abfd)->local_n_btshft;
302 unsigned int n_btmask = coff_data (abfd)->local_n_btmask;
305 boolean default_copy;
306 bfd_size_type symcount;
307 struct coff_link_hash_entry **sym_hash;
308 bfd_size_type symesz;
312 /* Keep the symbols during this function, in case the linker needs
313 to read the generic symbols in order to report an error message. */
314 keep_syms = obj_coff_keep_syms (abfd);
315 obj_coff_keep_syms (abfd) = true;
317 if (info->keep_memory)
318 default_copy = false;
322 symcount = obj_raw_syment_count (abfd);
324 /* We keep a list of the linker hash table entries that correspond
325 to particular symbols. */
326 sym_hash = ((struct coff_link_hash_entry **)
329 * sizeof (struct coff_link_hash_entry *))));
330 if (sym_hash == NULL && symcount != 0)
332 obj_coff_sym_hashes (abfd) = sym_hash;
334 (size_t) symcount * sizeof (struct coff_link_hash_entry *));
336 symesz = bfd_coff_symesz (abfd);
337 BFD_ASSERT (symesz == bfd_coff_auxesz (abfd));
338 esym = (bfd_byte *) obj_coff_external_syms (abfd);
339 esym_end = esym + symcount * symesz;
340 while (esym < esym_end)
342 struct internal_syment sym;
343 enum coff_symbol_classification classification;
346 bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym);
348 classification = bfd_coff_classify_symbol (abfd, &sym);
349 if (classification != COFF_SYMBOL_LOCAL)
352 char buf[SYMNMLEN + 1];
358 /* This symbol is externally visible. */
360 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
364 /* We must copy the name into memory if we got it from the
365 syment itself, rather than the string table. */
367 if (sym._n._n_n._n_zeroes != 0
368 || sym._n._n_n._n_offset == 0)
373 switch (classification)
378 case COFF_SYMBOL_GLOBAL:
379 flags = BSF_EXPORT | BSF_GLOBAL;
380 section = coff_section_from_bfd_index (abfd, sym.n_scnum);
382 value -= section->vma;
385 case COFF_SYMBOL_UNDEFINED:
387 section = bfd_und_section_ptr;
390 case COFF_SYMBOL_COMMON:
392 section = bfd_com_section_ptr;
395 case COFF_SYMBOL_PE_SECTION:
396 flags = BSF_SECTION_SYM | BSF_GLOBAL;
397 section = coff_section_from_bfd_index (abfd, sym.n_scnum);
401 if (sym.n_sclass == C_WEAKEXT
402 || (obj_pe (abfd) && sym.n_sclass == C_NT_WEAK))
407 /* In the PE format, section symbols actually refer to the
408 start of the output section. We handle them specially
410 if (obj_pe (abfd) && (flags & BSF_SECTION_SYM) != 0)
412 *sym_hash = coff_link_hash_lookup (coff_hash_table (info),
413 name, false, copy, false);
414 if (*sym_hash != NULL)
416 if (((*sym_hash)->coff_link_hash_flags
417 & COFF_LINK_HASH_PE_SECTION_SYMBOL) == 0
418 && (*sym_hash)->root.type != bfd_link_hash_undefined
419 && (*sym_hash)->root.type != bfd_link_hash_undefweak)
420 (*_bfd_error_handler)
421 ("Warning: symbol `%s' is both section and non-section",
430 if (! (bfd_coff_link_add_one_symbol
431 (info, abfd, name, flags, section, value,
432 (const char *) NULL, copy, false,
433 (struct bfd_link_hash_entry **) sym_hash)))
437 if (obj_pe (abfd) && (flags & BSF_SECTION_SYM) != 0)
438 (*sym_hash)->coff_link_hash_flags |=
439 COFF_LINK_HASH_PE_SECTION_SYMBOL;
441 if (section == bfd_com_section_ptr
442 && (*sym_hash)->root.type == bfd_link_hash_common
443 && ((*sym_hash)->root.u.c.p->alignment_power
444 > bfd_coff_default_section_alignment_power (abfd)))
445 (*sym_hash)->root.u.c.p->alignment_power
446 = bfd_coff_default_section_alignment_power (abfd);
448 if (info->hash->creator->flavour == bfd_get_flavour (abfd))
450 if (((*sym_hash)->class == C_NULL
451 && (*sym_hash)->type == T_NULL)
454 && (*sym_hash)->root.type != bfd_link_hash_defined
455 && (*sym_hash)->root.type != bfd_link_hash_defweak))
457 (*sym_hash)->class = sym.n_sclass;
458 if (sym.n_type != T_NULL)
460 if ((*sym_hash)->type != T_NULL
461 && (*sym_hash)->type != sym.n_type)
462 (*_bfd_error_handler)
463 (_("Warning: type of symbol `%s' changed from %d to %d in %s"),
464 name, (*sym_hash)->type, sym.n_type,
465 bfd_get_filename (abfd));
466 (*sym_hash)->type = sym.n_type;
468 (*sym_hash)->auxbfd = abfd;
469 if (sym.n_numaux != 0)
471 union internal_auxent *alloc;
474 union internal_auxent *iaux;
476 (*sym_hash)->numaux = sym.n_numaux;
477 alloc = ((union internal_auxent *)
478 bfd_hash_allocate (&info->hash->table,
480 * sizeof (*alloc))));
483 for (i = 0, eaux = esym + symesz, iaux = alloc;
485 i++, eaux += symesz, iaux++)
486 bfd_coff_swap_aux_in (abfd, (PTR) eaux, sym.n_type,
487 sym.n_sclass, i, sym.n_numaux,
489 (*sym_hash)->aux = alloc;
494 if (classification == COFF_SYMBOL_PE_SECTION
495 && (*sym_hash)->numaux != 0)
497 /* Some PE sections (such as .bss) have a zero size in
498 the section header, but a non-zero size in the AUX
499 record. Correct that here.
501 FIXME: This is not at all the right place to do this.
502 For example, it won't help objdump. This needs to be
503 done when we swap in the section header. */
505 BFD_ASSERT ((*sym_hash)->numaux == 1);
506 if (section->_raw_size == 0)
507 section->_raw_size = (*sym_hash)->aux[0].x_scn.x_scnlen;
509 /* FIXME: We could test whether the section sizes
510 matches the size in the aux entry, but apparently
511 that sometimes fails unexpectedly. */
515 esym += (sym.n_numaux + 1) * symesz;
516 sym_hash += sym.n_numaux + 1;
519 /* If this is a non-traditional, non-relocateable link, try to
520 optimize the handling of any .stab/.stabstr sections. */
521 if (! info->relocateable
522 && ! info->traditional_format
523 && info->hash->creator->flavour == bfd_get_flavour (abfd)
524 && (info->strip != strip_all && info->strip != strip_debugger))
526 asection *stab, *stabstr;
528 stab = bfd_get_section_by_name (abfd, ".stab");
531 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
535 struct coff_link_hash_table *table;
536 struct coff_section_tdata *secdata;
538 secdata = coff_section_data (abfd, stab);
542 (PTR) bfd_zalloc (abfd,
543 sizeof (struct coff_section_tdata));
544 if (stab->used_by_bfd == NULL)
546 secdata = coff_section_data (abfd, stab);
549 table = coff_hash_table (info);
551 if (! _bfd_link_section_stabs (abfd, &table->stab_info,
553 &secdata->stab_info))
559 obj_coff_keep_syms (abfd) = keep_syms;
564 obj_coff_keep_syms (abfd) = keep_syms;
568 /* Do the final link step. */
571 _bfd_coff_final_link (abfd, info)
573 struct bfd_link_info *info;
575 bfd_size_type symesz;
576 struct coff_final_link_info finfo;
577 boolean debug_merge_allocated;
578 boolean long_section_names;
580 struct bfd_link_order *p;
581 size_t max_sym_count;
582 size_t max_lineno_count;
583 size_t max_reloc_count;
584 size_t max_output_reloc_count;
585 size_t max_contents_size;
586 file_ptr rel_filepos;
588 file_ptr line_filepos;
591 bfd_byte *external_relocs = NULL;
592 char strbuf[STRING_SIZE_SIZE];
594 symesz = bfd_coff_symesz (abfd);
597 finfo.output_bfd = abfd;
599 finfo.section_info = NULL;
600 finfo.last_file_index = -1;
601 finfo.last_bf_index = -1;
602 finfo.internal_syms = NULL;
603 finfo.sec_ptrs = NULL;
604 finfo.sym_indices = NULL;
605 finfo.outsyms = NULL;
606 finfo.linenos = NULL;
607 finfo.contents = NULL;
608 finfo.external_relocs = NULL;
609 finfo.internal_relocs = NULL;
610 finfo.global_to_static = false;
611 debug_merge_allocated = false;
613 coff_data (abfd)->link_info = info;
615 finfo.strtab = _bfd_stringtab_init ();
616 if (finfo.strtab == NULL)
619 if (! coff_debug_merge_hash_table_init (&finfo.debug_merge))
621 debug_merge_allocated = true;
623 /* Compute the file positions for all the sections. */
624 if (! abfd->output_has_begun)
626 if (! bfd_coff_compute_section_file_positions (abfd))
630 /* Count the line numbers and relocation entries required for the
631 output file. Set the file positions for the relocs. */
632 rel_filepos = obj_relocbase (abfd);
633 relsz = bfd_coff_relsz (abfd);
634 max_contents_size = 0;
635 max_lineno_count = 0;
638 long_section_names = false;
639 for (o = abfd->sections; o != NULL; o = o->next)
643 for (p = o->link_order_head; p != NULL; p = p->next)
645 if (p->type == bfd_indirect_link_order)
649 sec = p->u.indirect.section;
651 /* Mark all sections which are to be included in the
652 link. This will normally be every section. We need
653 to do this so that we can identify any sections which
654 the linker has decided to not include. */
655 sec->linker_mark = true;
657 if (info->strip == strip_none
658 || info->strip == strip_some)
659 o->lineno_count += sec->lineno_count;
661 if (info->relocateable)
662 o->reloc_count += sec->reloc_count;
664 if (sec->_raw_size > max_contents_size)
665 max_contents_size = sec->_raw_size;
666 if (sec->lineno_count > max_lineno_count)
667 max_lineno_count = sec->lineno_count;
668 if (sec->reloc_count > max_reloc_count)
669 max_reloc_count = sec->reloc_count;
671 else if (info->relocateable
672 && (p->type == bfd_section_reloc_link_order
673 || p->type == bfd_symbol_reloc_link_order))
676 if (o->reloc_count == 0)
680 o->flags |= SEC_RELOC;
681 o->rel_filepos = rel_filepos;
682 rel_filepos += o->reloc_count * relsz;
685 if (bfd_coff_long_section_names (abfd)
686 && strlen (o->name) > SCNNMLEN)
688 /* This section has a long name which must go in the string
689 table. This must correspond to the code in
690 coff_write_object_contents which puts the string index
691 into the s_name field of the section header. That is why
692 we pass hash as false. */
693 if (_bfd_stringtab_add (finfo.strtab, o->name, false, false)
694 == (bfd_size_type) -1)
696 long_section_names = true;
700 /* If doing a relocateable link, allocate space for the pointers we
702 if (info->relocateable)
706 /* We use section_count + 1, rather than section_count, because
707 the target_index fields are 1 based. */
709 ((struct coff_link_section_info *)
710 bfd_malloc ((abfd->section_count + 1)
711 * sizeof (struct coff_link_section_info)));
712 if (finfo.section_info == NULL)
714 for (i = 0; i <= abfd->section_count; i++)
716 finfo.section_info[i].relocs = NULL;
717 finfo.section_info[i].rel_hashes = NULL;
721 /* We now know the size of the relocs, so we can determine the file
722 positions of the line numbers. */
723 line_filepos = rel_filepos;
724 linesz = bfd_coff_linesz (abfd);
725 max_output_reloc_count = 0;
726 for (o = abfd->sections; o != NULL; o = o->next)
728 if (o->lineno_count == 0)
732 o->line_filepos = line_filepos;
733 line_filepos += o->lineno_count * linesz;
736 if (o->reloc_count != 0)
738 /* We don't know the indices of global symbols until we have
739 written out all the local symbols. For each section in
740 the output file, we keep an array of pointers to hash
741 table entries. Each entry in the array corresponds to a
742 reloc. When we find a reloc against a global symbol, we
743 set the corresponding entry in this array so that we can
744 fix up the symbol index after we have written out all the
747 Because of this problem, we also keep the relocs in
748 memory until the end of the link. This wastes memory,
749 but only when doing a relocateable link, which is not the
751 BFD_ASSERT (info->relocateable);
752 finfo.section_info[o->target_index].relocs =
753 ((struct internal_reloc *)
754 bfd_malloc (o->reloc_count * sizeof (struct internal_reloc)));
755 finfo.section_info[o->target_index].rel_hashes =
756 ((struct coff_link_hash_entry **)
757 bfd_malloc (o->reloc_count
758 * sizeof (struct coff_link_hash_entry *)));
759 if (finfo.section_info[o->target_index].relocs == NULL
760 || finfo.section_info[o->target_index].rel_hashes == NULL)
763 if (o->reloc_count > max_output_reloc_count)
764 max_output_reloc_count = o->reloc_count;
767 /* Reset the reloc and lineno counts, so that we can use them to
768 count the number of entries we have output so far. */
773 obj_sym_filepos (abfd) = line_filepos;
775 /* Figure out the largest number of symbols in an input BFD. Take
776 the opportunity to clear the output_has_begun fields of all the
779 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
783 sub->output_has_begun = false;
784 sz = obj_raw_syment_count (sub);
785 if (sz > max_sym_count)
789 /* Allocate some buffers used while linking. */
790 finfo.internal_syms = ((struct internal_syment *)
791 bfd_malloc (max_sym_count
792 * sizeof (struct internal_syment)));
793 finfo.sec_ptrs = (asection **) bfd_malloc (max_sym_count
794 * sizeof (asection *));
795 finfo.sym_indices = (long *) bfd_malloc (max_sym_count * sizeof (long));
796 finfo.outsyms = ((bfd_byte *)
797 bfd_malloc ((size_t) ((max_sym_count + 1) * symesz)));
798 finfo.linenos = (bfd_byte *) bfd_malloc (max_lineno_count
799 * bfd_coff_linesz (abfd));
800 finfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
801 finfo.external_relocs = (bfd_byte *) bfd_malloc (max_reloc_count * relsz);
802 if (! info->relocateable)
803 finfo.internal_relocs = ((struct internal_reloc *)
804 bfd_malloc (max_reloc_count
805 * sizeof (struct internal_reloc)));
806 if ((finfo.internal_syms == NULL && max_sym_count > 0)
807 || (finfo.sec_ptrs == NULL && max_sym_count > 0)
808 || (finfo.sym_indices == NULL && max_sym_count > 0)
809 || finfo.outsyms == NULL
810 || (finfo.linenos == NULL && max_lineno_count > 0)
811 || (finfo.contents == NULL && max_contents_size > 0)
812 || (finfo.external_relocs == NULL && max_reloc_count > 0)
813 || (! info->relocateable
814 && finfo.internal_relocs == NULL
815 && max_reloc_count > 0))
818 /* We now know the position of everything in the file, except that
819 we don't know the size of the symbol table and therefore we don't
820 know where the string table starts. We just build the string
821 table in memory as we go along. We process all the relocations
822 for a single input file at once. */
823 obj_raw_syment_count (abfd) = 0;
825 if (coff_backend_info (abfd)->_bfd_coff_start_final_link)
827 if (! bfd_coff_start_final_link (abfd, info))
831 for (o = abfd->sections; o != NULL; o = o->next)
833 for (p = o->link_order_head; p != NULL; p = p->next)
835 if (p->type == bfd_indirect_link_order
836 && (bfd_get_flavour (p->u.indirect.section->owner)
837 == bfd_target_coff_flavour))
839 sub = p->u.indirect.section->owner;
840 if (! bfd_coff_link_output_has_begun (sub, & finfo))
842 if (! _bfd_coff_link_input_bfd (&finfo, sub))
844 sub->output_has_begun = true;
847 else if (p->type == bfd_section_reloc_link_order
848 || p->type == bfd_symbol_reloc_link_order)
850 if (! _bfd_coff_reloc_link_order (abfd, &finfo, o, p))
855 if (! _bfd_default_link_order (abfd, info, o, p))
861 if (! bfd_coff_final_link_postscript (abfd, & finfo))
864 /* Free up the buffers used by _bfd_coff_link_input_bfd. */
866 coff_debug_merge_hash_table_free (&finfo.debug_merge);
867 debug_merge_allocated = false;
869 if (finfo.internal_syms != NULL)
871 free (finfo.internal_syms);
872 finfo.internal_syms = NULL;
874 if (finfo.sec_ptrs != NULL)
876 free (finfo.sec_ptrs);
877 finfo.sec_ptrs = NULL;
879 if (finfo.sym_indices != NULL)
881 free (finfo.sym_indices);
882 finfo.sym_indices = NULL;
884 if (finfo.linenos != NULL)
886 free (finfo.linenos);
887 finfo.linenos = NULL;
889 if (finfo.contents != NULL)
891 free (finfo.contents);
892 finfo.contents = NULL;
894 if (finfo.external_relocs != NULL)
896 free (finfo.external_relocs);
897 finfo.external_relocs = NULL;
899 if (finfo.internal_relocs != NULL)
901 free (finfo.internal_relocs);
902 finfo.internal_relocs = NULL;
905 /* The value of the last C_FILE symbol is supposed to be the symbol
906 index of the first external symbol. Write it out again if
908 if (finfo.last_file_index != -1
909 && (unsigned int) finfo.last_file.n_value != obj_raw_syment_count (abfd))
911 finfo.last_file.n_value = obj_raw_syment_count (abfd);
912 bfd_coff_swap_sym_out (abfd, (PTR) &finfo.last_file,
913 (PTR) finfo.outsyms);
915 (obj_sym_filepos (abfd)
916 + finfo.last_file_index * symesz),
918 || bfd_write (finfo.outsyms, symesz, 1, abfd) != symesz)
922 /* If doing task linking (ld --task-link) then make a pass through the
923 global symbols, writing out any that are defined, and making them
927 finfo.failed = false;
928 coff_link_hash_traverse (coff_hash_table (info), _bfd_coff_write_task_globals,
934 /* Write out the global symbols. */
935 finfo.failed = false;
936 coff_link_hash_traverse (coff_hash_table (info), _bfd_coff_write_global_sym,
941 /* The outsyms buffer is used by _bfd_coff_write_global_sym. */
942 if (finfo.outsyms != NULL)
944 free (finfo.outsyms);
945 finfo.outsyms = NULL;
948 if (info->relocateable && max_output_reloc_count > 0)
950 /* Now that we have written out all the global symbols, we know
951 the symbol indices to use for relocs against them, and we can
952 finally write out the relocs. */
953 external_relocs = ((bfd_byte *)
954 bfd_malloc (max_output_reloc_count * relsz));
955 if (external_relocs == NULL)
958 for (o = abfd->sections; o != NULL; o = o->next)
960 struct internal_reloc *irel;
961 struct internal_reloc *irelend;
962 struct coff_link_hash_entry **rel_hash;
965 if (o->reloc_count == 0)
968 irel = finfo.section_info[o->target_index].relocs;
969 irelend = irel + o->reloc_count;
970 rel_hash = finfo.section_info[o->target_index].rel_hashes;
971 erel = external_relocs;
972 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
974 if (*rel_hash != NULL)
976 BFD_ASSERT ((*rel_hash)->indx >= 0);
977 irel->r_symndx = (*rel_hash)->indx;
979 bfd_coff_swap_reloc_out (abfd, (PTR) irel, (PTR) erel);
982 if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0
983 || bfd_write ((PTR) external_relocs, relsz, o->reloc_count,
984 abfd) != relsz * o->reloc_count)
988 free (external_relocs);
989 external_relocs = NULL;
992 /* Free up the section information. */
993 if (finfo.section_info != NULL)
997 for (i = 0; i < abfd->section_count; i++)
999 if (finfo.section_info[i].relocs != NULL)
1000 free (finfo.section_info[i].relocs);
1001 if (finfo.section_info[i].rel_hashes != NULL)
1002 free (finfo.section_info[i].rel_hashes);
1004 free (finfo.section_info);
1005 finfo.section_info = NULL;
1008 /* If we have optimized stabs strings, output them. */
1009 if (coff_hash_table (info)->stab_info != NULL)
1011 if (! _bfd_write_stab_strings (abfd, &coff_hash_table (info)->stab_info))
1015 /* Write out the string table. */
1016 if (obj_raw_syment_count (abfd) != 0 || long_section_names)
1019 (obj_sym_filepos (abfd)
1020 + obj_raw_syment_count (abfd) * symesz),
1024 #if STRING_SIZE_SIZE == 4
1026 _bfd_stringtab_size (finfo.strtab) + STRING_SIZE_SIZE,
1027 (bfd_byte *) strbuf);
1029 #error Change bfd_h_put_32
1032 if (bfd_write (strbuf, 1, STRING_SIZE_SIZE, abfd) != STRING_SIZE_SIZE)
1035 if (! _bfd_stringtab_emit (abfd, finfo.strtab))
1039 _bfd_stringtab_free (finfo.strtab);
1041 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
1042 not try to write out the symbols. */
1043 bfd_get_symcount (abfd) = 0;
1048 if (debug_merge_allocated)
1049 coff_debug_merge_hash_table_free (&finfo.debug_merge);
1050 if (finfo.strtab != NULL)
1051 _bfd_stringtab_free (finfo.strtab);
1052 if (finfo.section_info != NULL)
1056 for (i = 0; i < abfd->section_count; i++)
1058 if (finfo.section_info[i].relocs != NULL)
1059 free (finfo.section_info[i].relocs);
1060 if (finfo.section_info[i].rel_hashes != NULL)
1061 free (finfo.section_info[i].rel_hashes);
1063 free (finfo.section_info);
1065 if (finfo.internal_syms != NULL)
1066 free (finfo.internal_syms);
1067 if (finfo.sec_ptrs != NULL)
1068 free (finfo.sec_ptrs);
1069 if (finfo.sym_indices != NULL)
1070 free (finfo.sym_indices);
1071 if (finfo.outsyms != NULL)
1072 free (finfo.outsyms);
1073 if (finfo.linenos != NULL)
1074 free (finfo.linenos);
1075 if (finfo.contents != NULL)
1076 free (finfo.contents);
1077 if (finfo.external_relocs != NULL)
1078 free (finfo.external_relocs);
1079 if (finfo.internal_relocs != NULL)
1080 free (finfo.internal_relocs);
1081 if (external_relocs != NULL)
1082 free (external_relocs);
1086 /* parse out a -heap <reserved>,<commit> line */
1089 dores_com (ptr, output_bfd, heap)
1094 if (coff_data(output_bfd)->pe)
1096 int val = strtoul (ptr, &ptr, 0);
1098 pe_data(output_bfd)->pe_opthdr.SizeOfHeapReserve =val;
1100 pe_data(output_bfd)->pe_opthdr.SizeOfStackReserve =val;
1104 int val = strtoul (ptr+1, &ptr, 0);
1106 pe_data(output_bfd)->pe_opthdr.SizeOfHeapCommit =val;
1108 pe_data(output_bfd)->pe_opthdr.SizeOfStackCommit =val;
1114 static char *get_name(ptr, dst)
1121 while (*ptr && *ptr != ' ')
1127 /* Process any magic embedded commands in a section called .drectve */
1130 process_embedded_commands (output_bfd, info, abfd)
1132 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1135 asection *sec = bfd_get_section_by_name (abfd, ".drectve");
1142 copy = bfd_malloc ((size_t) sec->_raw_size);
1145 if (! bfd_get_section_contents(abfd, sec, copy, 0, sec->_raw_size))
1150 e = copy + sec->_raw_size;
1151 for (s = copy; s < e ; )
1157 if (strncmp (s,"-attr", 5) == 0)
1169 s = get_name(s, &name);
1170 s = get_name(s, &attribs);
1190 asec = bfd_get_section_by_name (abfd, name);
1193 asec->flags |= SEC_CODE;
1195 asec->flags |= SEC_READONLY;
1198 else if (strncmp (s,"-heap", 5) == 0)
1200 s = dores_com (s+5, output_bfd, 1);
1202 else if (strncmp (s,"-stack", 6) == 0)
1204 s = dores_com (s+6, output_bfd, 0);
1213 /* Place a marker against all symbols which are used by relocations.
1214 This marker can be picked up by the 'do we skip this symbol ?'
1215 loop in _bfd_coff_link_input_bfd() and used to prevent skipping
1220 mark_relocs (finfo, input_bfd)
1221 struct coff_final_link_info * finfo;
1226 if ((bfd_get_file_flags (input_bfd) & HAS_SYMS) == 0)
1229 for (a = input_bfd->sections; a != (asection *) NULL; a = a->next)
1231 struct internal_reloc * internal_relocs;
1232 struct internal_reloc * irel;
1233 struct internal_reloc * irelend;
1236 if ((a->flags & SEC_RELOC) == 0 || a->reloc_count < 1)
1239 /* Read in the relocs. */
1240 internal_relocs = _bfd_coff_read_internal_relocs
1241 (input_bfd, a, false,
1242 finfo->external_relocs,
1243 finfo->info->relocateable,
1244 (finfo->info->relocateable
1245 ? (finfo->section_info[ a->output_section->target_index ].relocs + a->output_section->reloc_count)
1246 : finfo->internal_relocs)
1249 if (internal_relocs == NULL)
1252 irel = internal_relocs;
1253 irelend = irel + a->reloc_count;
1255 /* Place a mark in the sym_indices array (whose entries have
1256 been initialised to 0) for all of the symbols that are used
1257 in the relocation table. This will then be picked up in the
1260 for (; irel < irelend; irel++)
1262 finfo->sym_indices[ irel->r_symndx ] = -1;
1267 /* Link an input file into the linker output file. This function
1268 handles all the sections and relocations of the input file at once. */
1271 _bfd_coff_link_input_bfd (finfo, input_bfd)
1272 struct coff_final_link_info *finfo;
1275 unsigned int n_tmask = coff_data (input_bfd)->local_n_tmask;
1276 unsigned int n_btshft = coff_data (input_bfd)->local_n_btshft;
1278 unsigned int n_btmask = coff_data (input_bfd)->local_n_btmask;
1280 boolean (*adjust_symndx) PARAMS ((bfd *, struct bfd_link_info *, bfd *,
1281 asection *, struct internal_reloc *,
1284 const char *strings;
1285 bfd_size_type syment_base;
1287 bfd_size_type isymesz;
1288 bfd_size_type osymesz;
1289 bfd_size_type linesz;
1292 struct internal_syment *isymp;
1295 unsigned long output_index;
1297 struct coff_link_hash_entry **sym_hash;
1300 /* Move all the symbols to the output file. */
1302 output_bfd = finfo->output_bfd;
1304 syment_base = obj_raw_syment_count (output_bfd);
1305 isymesz = bfd_coff_symesz (input_bfd);
1306 osymesz = bfd_coff_symesz (output_bfd);
1307 linesz = bfd_coff_linesz (input_bfd);
1308 BFD_ASSERT (linesz == bfd_coff_linesz (output_bfd));
1311 if (! finfo->info->keep_memory)
1314 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
1317 if (! _bfd_coff_get_external_symbols (input_bfd))
1320 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
1321 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
1322 isymp = finfo->internal_syms;
1323 secpp = finfo->sec_ptrs;
1324 indexp = finfo->sym_indices;
1325 output_index = syment_base;
1326 outsym = finfo->outsyms;
1328 if (coff_data (output_bfd)->pe)
1330 if (! process_embedded_commands (output_bfd, finfo->info, input_bfd))
1334 /* If we are going to perform relocations and also strip/discard some symbols
1335 then we must make sure that we do not strip/discard those symbols that are
1336 going to be involved in the relocations */
1337 if (( finfo->info->strip != strip_none
1338 || finfo->info->discard != discard_none)
1339 && finfo->info->relocateable)
1341 /* mark the symbol array as 'not-used' */
1342 memset (indexp, 0, obj_raw_syment_count (input_bfd) * sizeof * indexp);
1344 mark_relocs (finfo, input_bfd);
1347 while (esym < esym_end)
1349 struct internal_syment isym;
1350 enum coff_symbol_classification classification;
1353 boolean dont_skip_symbol;
1356 bfd_coff_swap_sym_in (input_bfd, (PTR) esym, (PTR) isymp);
1358 /* Make a copy of *isymp so that the relocate_section function
1359 always sees the original values. This is more reliable than
1360 always recomputing the symbol value even if we are stripping
1364 classification = bfd_coff_classify_symbol (input_bfd, &isym);
1365 switch (classification)
1369 case COFF_SYMBOL_GLOBAL:
1370 case COFF_SYMBOL_PE_SECTION:
1371 case COFF_SYMBOL_LOCAL:
1372 *secpp = coff_section_from_bfd_index (input_bfd, isym.n_scnum);
1374 case COFF_SYMBOL_COMMON:
1375 *secpp = bfd_com_section_ptr;
1377 case COFF_SYMBOL_UNDEFINED:
1378 *secpp = bfd_und_section_ptr;
1382 /* Extract the flag indicating if this symbol is used by a
1384 if ((finfo->info->strip != strip_none
1385 || finfo->info->discard != discard_none)
1386 && finfo->info->relocateable)
1387 dont_skip_symbol = *indexp;
1389 dont_skip_symbol = false;
1395 add = 1 + isym.n_numaux;
1397 /* If we are stripping all symbols, we want to skip this one. */
1398 if (finfo->info->strip == strip_all && ! dont_skip_symbol)
1403 switch (classification)
1407 case COFF_SYMBOL_GLOBAL:
1408 case COFF_SYMBOL_COMMON:
1409 case COFF_SYMBOL_PE_SECTION:
1410 /* This is a global symbol. Global symbols come at the
1411 end of the symbol table, so skip them for now.
1412 Locally defined function symbols, however, are an
1413 exception, and are not moved to the end. */
1415 if (! ISFCN (isym.n_type))
1419 case COFF_SYMBOL_UNDEFINED:
1420 /* Undefined symbols are left for the end. */
1425 case COFF_SYMBOL_LOCAL:
1426 /* This is a local symbol. Skip it if we are discarding
1428 if (finfo->info->discard == discard_all && ! dont_skip_symbol)
1434 /* If we stripping debugging symbols, and this is a debugging
1435 symbol, then skip it. FIXME: gas sets the section to N_ABS
1436 for some types of debugging symbols; I don't know if this is
1437 a bug or not. In any case, we handle it here. */
1439 && finfo->info->strip == strip_debugger
1440 && ! dont_skip_symbol
1441 && (isym.n_scnum == N_DEBUG
1442 || (isym.n_scnum == N_ABS
1443 && (isym.n_sclass == C_AUTO
1444 || isym.n_sclass == C_REG
1445 || isym.n_sclass == C_MOS
1446 || isym.n_sclass == C_MOE
1447 || isym.n_sclass == C_MOU
1448 || isym.n_sclass == C_ARG
1449 || isym.n_sclass == C_REGPARM
1450 || isym.n_sclass == C_FIELD
1451 || isym.n_sclass == C_EOS))))
1454 /* If some symbols are stripped based on the name, work out the
1455 name and decide whether to skip this symbol. */
1457 && (finfo->info->strip == strip_some
1458 || finfo->info->discard == discard_l))
1461 char buf[SYMNMLEN + 1];
1463 name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf);
1467 if (! dont_skip_symbol
1468 && ((finfo->info->strip == strip_some
1469 && (bfd_hash_lookup (finfo->info->keep_hash, name, false,
1472 && finfo->info->discard == discard_l
1473 && bfd_is_local_label_name (input_bfd, name))))
1477 /* If this is an enum, struct, or union tag, see if we have
1478 already output an identical type. */
1480 && (finfo->output_bfd->flags & BFD_TRADITIONAL_FORMAT) == 0
1481 && (isym.n_sclass == C_ENTAG
1482 || isym.n_sclass == C_STRTAG
1483 || isym.n_sclass == C_UNTAG)
1484 && isym.n_numaux == 1)
1487 char buf[SYMNMLEN + 1];
1488 struct coff_debug_merge_hash_entry *mh;
1489 struct coff_debug_merge_type *mt;
1490 union internal_auxent aux;
1491 struct coff_debug_merge_element **epp;
1492 bfd_byte *esl, *eslend;
1493 struct internal_syment *islp;
1495 name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf);
1499 /* Ignore fake names invented by compiler; treat them all as
1501 if (*name == '~' || *name == '.' || *name == '$'
1502 || (*name == bfd_get_symbol_leading_char (input_bfd)
1503 && (name[1] == '~' || name[1] == '.' || name[1] == '$')))
1506 mh = coff_debug_merge_hash_lookup (&finfo->debug_merge, name,
1511 /* Allocate memory to hold type information. If this turns
1512 out to be a duplicate, we pass this address to
1514 mt = ((struct coff_debug_merge_type *)
1515 bfd_alloc (input_bfd,
1516 sizeof (struct coff_debug_merge_type)));
1519 mt->class = isym.n_sclass;
1521 /* Pick up the aux entry, which points to the end of the tag
1523 bfd_coff_swap_aux_in (input_bfd, (PTR) (esym + isymesz),
1524 isym.n_type, isym.n_sclass, 0, isym.n_numaux,
1527 /* Gather the elements. */
1528 epp = &mt->elements;
1529 mt->elements = NULL;
1531 esl = esym + 2 * isymesz;
1532 eslend = ((bfd_byte *) obj_coff_external_syms (input_bfd)
1533 + aux.x_sym.x_fcnary.x_fcn.x_endndx.l * isymesz);
1534 while (esl < eslend)
1536 const char *elename;
1537 char elebuf[SYMNMLEN + 1];
1540 bfd_coff_swap_sym_in (input_bfd, (PTR) esl, (PTR) islp);
1542 *epp = ((struct coff_debug_merge_element *)
1543 bfd_alloc (input_bfd,
1544 sizeof (struct coff_debug_merge_element)));
1548 elename = _bfd_coff_internal_syment_name (input_bfd, islp,
1550 if (elename == NULL)
1553 name_copy = (char *) bfd_alloc (input_bfd,
1554 strlen (elename) + 1);
1555 if (name_copy == NULL)
1557 strcpy (name_copy, elename);
1559 (*epp)->name = name_copy;
1560 (*epp)->type = islp->n_type;
1562 if (islp->n_numaux >= 1
1563 && islp->n_type != T_NULL
1564 && islp->n_sclass != C_EOS)
1566 union internal_auxent eleaux;
1569 bfd_coff_swap_aux_in (input_bfd, (PTR) (esl + isymesz),
1570 islp->n_type, islp->n_sclass, 0,
1571 islp->n_numaux, (PTR) &eleaux);
1572 indx = eleaux.x_sym.x_tagndx.l;
1574 /* FIXME: If this tagndx entry refers to a symbol
1575 defined later in this file, we just ignore it.
1576 Handling this correctly would be tedious, and may
1582 (bfd_byte *) obj_coff_external_syms (input_bfd))
1585 (*epp)->tagndx = finfo->sym_indices[indx];
1586 if ((*epp)->tagndx < 0)
1590 epp = &(*epp)->next;
1593 esl += (islp->n_numaux + 1) * isymesz;
1594 islp += islp->n_numaux + 1;
1597 /* See if we already have a definition which matches this
1598 type. We always output the type if it has no elements,
1600 if (mt->elements == NULL)
1601 bfd_release (input_bfd, (PTR) mt);
1604 struct coff_debug_merge_type *mtl;
1606 for (mtl = mh->types; mtl != NULL; mtl = mtl->next)
1608 struct coff_debug_merge_element *me, *mel;
1610 if (mtl->class != mt->class)
1613 for (me = mt->elements, mel = mtl->elements;
1614 me != NULL && mel != NULL;
1615 me = me->next, mel = mel->next)
1617 if (strcmp (me->name, mel->name) != 0
1618 || me->type != mel->type
1619 || me->tagndx != mel->tagndx)
1623 if (me == NULL && mel == NULL)
1627 if (mtl == NULL || (bfd_size_type) mtl->indx >= syment_base)
1629 /* This is the first definition of this type. */
1630 mt->indx = output_index;
1631 mt->next = mh->types;
1636 /* This is a redefinition which can be merged. */
1637 bfd_release (input_bfd, (PTR) mt);
1638 *indexp = mtl->indx;
1639 add = (eslend - esym) / isymesz;
1645 /* We now know whether we are to skip this symbol or not. */
1648 /* Adjust the symbol in order to output it. */
1650 if (isym._n._n_n._n_zeroes == 0
1651 && isym._n._n_n._n_offset != 0)
1656 /* This symbol has a long name. Enter it in the string
1657 table we are building. Note that we do not check
1658 bfd_coff_symname_in_debug. That is only true for
1659 XCOFF, and XCOFF requires different linking code
1661 name = _bfd_coff_internal_syment_name (input_bfd, &isym,
1665 indx = _bfd_stringtab_add (finfo->strtab, name, hash, copy);
1666 if (indx == (bfd_size_type) -1)
1668 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
1671 if (isym.n_scnum > 0)
1673 isym.n_scnum = (*secpp)->output_section->target_index;
1674 isym.n_value += (*secpp)->output_offset;
1675 if (! obj_pe (input_bfd))
1676 isym.n_value -= (*secpp)->vma;
1677 if (! obj_pe (finfo->output_bfd))
1678 isym.n_value += (*secpp)->output_section->vma;
1681 /* The value of a C_FILE symbol is the symbol index of the
1682 next C_FILE symbol. The value of the last C_FILE symbol
1683 is the symbol index to the first external symbol
1684 (actually, coff_renumber_symbols does not get this
1685 right--it just sets the value of the last C_FILE symbol
1686 to zero--and nobody has ever complained about it). We
1687 try to get this right, below, just before we write the
1688 symbols out, but in the general case we may have to write
1689 the symbol out twice. */
1690 if (isym.n_sclass == C_FILE)
1692 if (finfo->last_file_index != -1
1693 && finfo->last_file.n_value != (long) output_index)
1695 /* We must correct the value of the last C_FILE entry. */
1696 finfo->last_file.n_value = output_index;
1697 if ((bfd_size_type) finfo->last_file_index >= syment_base)
1699 /* The last C_FILE symbol is in this input file. */
1700 bfd_coff_swap_sym_out (output_bfd,
1701 (PTR) &finfo->last_file,
1702 (PTR) (finfo->outsyms
1703 + ((finfo->last_file_index
1709 /* We have already written out the last C_FILE
1710 symbol. We need to write it out again. We
1711 borrow *outsym temporarily. */
1712 bfd_coff_swap_sym_out (output_bfd,
1713 (PTR) &finfo->last_file,
1715 if (bfd_seek (output_bfd,
1716 (obj_sym_filepos (output_bfd)
1717 + finfo->last_file_index * osymesz),
1719 || (bfd_write (outsym, osymesz, 1, output_bfd)
1725 finfo->last_file_index = output_index;
1726 finfo->last_file = isym;
1729 /* If doing task linking, convert normal global function symbols to
1730 static functions. */
1732 if (finfo->info->task_link
1733 && (isym.n_sclass == C_EXT
1734 || isym.n_sclass == C_WEAKEXT
1735 || (obj_pe (input_bfd) && isym.n_sclass == C_NT_WEAK)))
1736 isym.n_sclass = C_STAT;
1738 /* Output the symbol. */
1740 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
1742 *indexp = output_index;
1747 struct coff_link_hash_entry *h;
1749 indx = ((esym - (bfd_byte *) obj_coff_external_syms (input_bfd))
1751 h = obj_coff_sym_hashes (input_bfd)[indx];
1754 /* This can happen if there were errors earlier in
1756 bfd_set_error (bfd_error_bad_value);
1759 h->indx = output_index;
1762 output_index += add;
1763 outsym += add * osymesz;
1766 esym += add * isymesz;
1770 for (--add; add > 0; --add)
1777 /* Fix up the aux entries. This must be done in a separate pass,
1778 because we don't know the correct symbol indices until we have
1779 already decided which symbols we are going to keep. */
1781 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
1782 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
1783 isymp = finfo->internal_syms;
1784 indexp = finfo->sym_indices;
1785 sym_hash = obj_coff_sym_hashes (input_bfd);
1786 outsym = finfo->outsyms;
1787 while (esym < esym_end)
1791 add = 1 + isymp->n_numaux;
1794 || (bfd_size_type) *indexp < syment_base)
1795 && (*sym_hash == NULL
1796 || (*sym_hash)->auxbfd != input_bfd))
1797 esym += add * isymesz;
1800 struct coff_link_hash_entry *h;
1808 /* The m68k-motorola-sysv assembler will sometimes
1809 generate two symbols with the same name, but only one
1810 will have aux entries. */
1811 BFD_ASSERT (isymp->n_numaux == 0
1812 || h->numaux == isymp->n_numaux);
1820 /* Handle the aux entries. This handling is based on
1821 coff_pointerize_aux. I don't know if it always correct. */
1822 for (i = 0; i < isymp->n_numaux && esym < esym_end; i++)
1824 union internal_auxent aux;
1825 union internal_auxent *auxp;
1831 bfd_coff_swap_aux_in (input_bfd, (PTR) esym, isymp->n_type,
1832 isymp->n_sclass, i, isymp->n_numaux,
1837 if (isymp->n_sclass == C_FILE)
1839 /* If this is a long filename, we must put it in the
1841 if (auxp->x_file.x_n.x_zeroes == 0
1842 && auxp->x_file.x_n.x_offset != 0)
1844 const char *filename;
1847 BFD_ASSERT (auxp->x_file.x_n.x_offset
1848 >= STRING_SIZE_SIZE);
1849 if (strings == NULL)
1851 strings = _bfd_coff_read_string_table (input_bfd);
1852 if (strings == NULL)
1855 filename = strings + auxp->x_file.x_n.x_offset;
1856 indx = _bfd_stringtab_add (finfo->strtab, filename,
1858 if (indx == (bfd_size_type) -1)
1860 auxp->x_file.x_n.x_offset = STRING_SIZE_SIZE + indx;
1863 else if (isymp->n_sclass != C_STAT || isymp->n_type != T_NULL)
1867 if (ISFCN (isymp->n_type)
1868 || ISTAG (isymp->n_sclass)
1869 || isymp->n_sclass == C_BLOCK
1870 || isymp->n_sclass == C_FCN)
1872 indx = auxp->x_sym.x_fcnary.x_fcn.x_endndx.l;
1874 && indx < obj_raw_syment_count (input_bfd))
1876 /* We look forward through the symbol for
1877 the index of the next symbol we are going
1878 to include. I don't know if this is
1880 while ((finfo->sym_indices[indx] < 0
1881 || ((bfd_size_type) finfo->sym_indices[indx]
1883 && indx < obj_raw_syment_count (input_bfd))
1885 if (indx >= obj_raw_syment_count (input_bfd))
1886 indx = output_index;
1888 indx = finfo->sym_indices[indx];
1889 auxp->x_sym.x_fcnary.x_fcn.x_endndx.l = indx;
1893 indx = auxp->x_sym.x_tagndx.l;
1894 if (indx > 0 && indx < obj_raw_syment_count (input_bfd))
1898 symindx = finfo->sym_indices[indx];
1900 auxp->x_sym.x_tagndx.l = 0;
1902 auxp->x_sym.x_tagndx.l = symindx;
1905 /* The .bf symbols are supposed to be linked through
1906 the endndx field. We need to carry this list
1907 across object files. */
1910 && isymp->n_sclass == C_FCN
1911 && (isymp->_n._n_n._n_zeroes != 0
1912 || isymp->_n._n_n._n_offset == 0)
1913 && isymp->_n._n_name[0] == '.'
1914 && isymp->_n._n_name[1] == 'b'
1915 && isymp->_n._n_name[2] == 'f'
1916 && isymp->_n._n_name[3] == '\0')
1918 if (finfo->last_bf_index != -1)
1920 finfo->last_bf.x_sym.x_fcnary.x_fcn.x_endndx.l =
1923 if ((bfd_size_type) finfo->last_bf_index
1928 /* The last .bf symbol is in this input
1929 file. This will only happen if the
1930 assembler did not set up the .bf
1931 endndx symbols correctly. */
1932 auxout = (PTR) (finfo->outsyms
1933 + ((finfo->last_bf_index
1936 bfd_coff_swap_aux_out (output_bfd,
1937 (PTR) &finfo->last_bf,
1945 /* We have already written out the last
1946 .bf aux entry. We need to write it
1947 out again. We borrow *outsym
1948 temporarily. FIXME: This case should
1950 bfd_coff_swap_aux_out (output_bfd,
1951 (PTR) &finfo->last_bf,
1956 if (bfd_seek (output_bfd,
1957 (obj_sym_filepos (output_bfd)
1958 + finfo->last_bf_index * osymesz),
1960 || bfd_write (outsym, osymesz, 1,
1961 output_bfd) != osymesz)
1966 if (auxp->x_sym.x_fcnary.x_fcn.x_endndx.l != 0)
1967 finfo->last_bf_index = -1;
1970 /* The endndx field of this aux entry must
1971 be updated with the symbol number of the
1973 finfo->last_bf = *auxp;
1974 finfo->last_bf_index = (((outsym - finfo->outsyms)
1983 bfd_coff_swap_aux_out (output_bfd, (PTR) auxp, isymp->n_type,
1984 isymp->n_sclass, i, isymp->n_numaux,
1998 /* Relocate the line numbers, unless we are stripping them. */
1999 if (finfo->info->strip == strip_none
2000 || finfo->info->strip == strip_some)
2002 for (o = input_bfd->sections; o != NULL; o = o->next)
2008 /* FIXME: If SEC_HAS_CONTENTS is not for the section, then
2009 build_link_order in ldwrite.c will not have created a
2010 link order, which means that we will not have seen this
2011 input section in _bfd_coff_final_link, which means that
2012 we will not have allocated space for the line numbers of
2013 this section. I don't think line numbers can be
2014 meaningful for a section which does not have
2015 SEC_HAS_CONTENTS set, but, if they do, this must be
2017 if (o->lineno_count == 0
2018 || (o->output_section->flags & SEC_HAS_CONTENTS) == 0)
2021 if (bfd_seek (input_bfd, o->line_filepos, SEEK_SET) != 0
2022 || bfd_read (finfo->linenos, linesz, o->lineno_count,
2023 input_bfd) != linesz * o->lineno_count)
2026 offset = o->output_section->vma + o->output_offset - o->vma;
2027 eline = finfo->linenos;
2028 elineend = eline + linesz * o->lineno_count;
2029 for (; eline < elineend; eline += linesz)
2031 struct internal_lineno iline;
2033 bfd_coff_swap_lineno_in (input_bfd, (PTR) eline, (PTR) &iline);
2035 if (iline.l_lnno != 0)
2036 iline.l_addr.l_paddr += offset;
2037 else if (iline.l_addr.l_symndx >= 0
2038 && ((unsigned long) iline.l_addr.l_symndx
2039 < obj_raw_syment_count (input_bfd)))
2043 indx = finfo->sym_indices[iline.l_addr.l_symndx];
2047 /* These line numbers are attached to a symbol
2048 which we are stripping. We should really
2049 just discard the line numbers, but that would
2050 be a pain because we have already counted
2056 struct internal_syment is;
2057 union internal_auxent ia;
2059 /* Fix up the lnnoptr field in the aux entry of
2060 the symbol. It turns out that we can't do
2061 this when we modify the symbol aux entries,
2062 because gas sometimes screws up the lnnoptr
2063 field and makes it an offset from the start
2064 of the line numbers rather than an absolute
2066 bfd_coff_swap_sym_in (output_bfd,
2067 (PTR) (finfo->outsyms
2068 + ((indx - syment_base)
2071 if ((ISFCN (is.n_type)
2072 || is.n_sclass == C_BLOCK)
2073 && is.n_numaux >= 1)
2077 auxptr = (PTR) (finfo->outsyms
2078 + ((indx - syment_base + 1)
2080 bfd_coff_swap_aux_in (output_bfd, auxptr,
2081 is.n_type, is.n_sclass,
2082 0, is.n_numaux, (PTR) &ia);
2083 ia.x_sym.x_fcnary.x_fcn.x_lnnoptr =
2084 (o->output_section->line_filepos
2085 + o->output_section->lineno_count * linesz
2086 + eline - finfo->linenos);
2087 bfd_coff_swap_aux_out (output_bfd, (PTR) &ia,
2088 is.n_type, is.n_sclass, 0,
2089 is.n_numaux, auxptr);
2093 iline.l_addr.l_symndx = indx;
2096 bfd_coff_swap_lineno_out (output_bfd, (PTR) &iline, (PTR) eline);
2099 if (bfd_seek (output_bfd,
2100 (o->output_section->line_filepos
2101 + o->output_section->lineno_count * linesz),
2103 || bfd_write (finfo->linenos, linesz, o->lineno_count,
2104 output_bfd) != linesz * o->lineno_count)
2107 o->output_section->lineno_count += o->lineno_count;
2111 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
2112 symbol will be the first symbol in the next input file. In the
2113 normal case, this will save us from writing out the C_FILE symbol
2115 if (finfo->last_file_index != -1
2116 && (bfd_size_type) finfo->last_file_index >= syment_base)
2118 finfo->last_file.n_value = output_index;
2119 bfd_coff_swap_sym_out (output_bfd, (PTR) &finfo->last_file,
2120 (PTR) (finfo->outsyms
2121 + ((finfo->last_file_index - syment_base)
2125 /* Write the modified symbols to the output file. */
2126 if (outsym > finfo->outsyms)
2128 if (bfd_seek (output_bfd,
2129 obj_sym_filepos (output_bfd) + syment_base * osymesz,
2131 || (bfd_write (finfo->outsyms, outsym - finfo->outsyms, 1,
2133 != (bfd_size_type) (outsym - finfo->outsyms)))
2136 BFD_ASSERT ((obj_raw_syment_count (output_bfd)
2137 + (outsym - finfo->outsyms) / osymesz)
2140 obj_raw_syment_count (output_bfd) = output_index;
2143 /* Relocate the contents of each section. */
2144 adjust_symndx = coff_backend_info (input_bfd)->_bfd_coff_adjust_symndx;
2145 for (o = input_bfd->sections; o != NULL; o = o->next)
2148 struct coff_section_tdata *secdata;
2150 if (! o->linker_mark)
2152 /* This section was omitted from the link. */
2156 if ((o->flags & SEC_HAS_CONTENTS) == 0
2157 || (o->_raw_size == 0 && (o->flags & SEC_RELOC) == 0))
2159 if ((o->flags & SEC_RELOC) != 0
2160 && o->reloc_count != 0)
2162 ((*_bfd_error_handler)
2163 (_("%s: relocs in section `%s', but it has no contents"),
2164 bfd_get_filename (input_bfd),
2165 bfd_get_section_name (input_bfd, o)));
2166 bfd_set_error (bfd_error_no_contents);
2173 secdata = coff_section_data (input_bfd, o);
2174 if (secdata != NULL && secdata->contents != NULL)
2175 contents = secdata->contents;
2178 if (! bfd_get_section_contents (input_bfd, o, finfo->contents,
2179 (file_ptr) 0, o->_raw_size))
2181 contents = finfo->contents;
2184 if ((o->flags & SEC_RELOC) != 0)
2187 struct internal_reloc *internal_relocs;
2188 struct internal_reloc *irel;
2190 /* Read in the relocs. */
2191 target_index = o->output_section->target_index;
2192 internal_relocs = (_bfd_coff_read_internal_relocs
2193 (input_bfd, o, false, finfo->external_relocs,
2194 finfo->info->relocateable,
2195 (finfo->info->relocateable
2196 ? (finfo->section_info[target_index].relocs
2197 + o->output_section->reloc_count)
2198 : finfo->internal_relocs)));
2199 if (internal_relocs == NULL)
2202 /* Call processor specific code to relocate the section
2204 if (! bfd_coff_relocate_section (output_bfd, finfo->info,
2208 finfo->internal_syms,
2212 if (finfo->info->relocateable)
2215 struct internal_reloc *irelend;
2216 struct coff_link_hash_entry **rel_hash;
2218 offset = o->output_section->vma + o->output_offset - o->vma;
2219 irel = internal_relocs;
2220 irelend = irel + o->reloc_count;
2221 rel_hash = (finfo->section_info[target_index].rel_hashes
2222 + o->output_section->reloc_count);
2223 for (; irel < irelend; irel++, rel_hash++)
2225 struct coff_link_hash_entry *h;
2230 /* Adjust the reloc address and symbol index. */
2232 irel->r_vaddr += offset;
2234 if (irel->r_symndx == -1)
2239 if (! (*adjust_symndx) (output_bfd, finfo->info,
2247 h = obj_coff_sym_hashes (input_bfd)[irel->r_symndx];
2250 /* This is a global symbol. */
2252 irel->r_symndx = h->indx;
2255 /* This symbol is being written at the end
2256 of the file, and we do not yet know the
2257 symbol index. We save the pointer to the
2258 hash table entry in the rel_hash list.
2259 We set the indx field to -2 to indicate
2260 that this symbol must not be stripped. */
2269 indx = finfo->sym_indices[irel->r_symndx];
2271 irel->r_symndx = indx;
2274 struct internal_syment *is;
2276 char buf[SYMNMLEN + 1];
2278 /* This reloc is against a symbol we are
2279 stripping. This should have been handled
2280 by the 'dont_skip_symbol' code in the while
2281 loop at the top of this function. */
2283 is = finfo->internal_syms + irel->r_symndx;
2285 name = (_bfd_coff_internal_syment_name
2286 (input_bfd, is, buf));
2290 if (! ((*finfo->info->callbacks->unattached_reloc)
2291 (finfo->info, name, input_bfd, o,
2298 o->output_section->reloc_count += o->reloc_count;
2302 /* Write out the modified section contents. */
2303 if (secdata == NULL || secdata->stab_info == NULL)
2305 if (! bfd_set_section_contents (output_bfd, o->output_section,
2306 contents, o->output_offset,
2307 (o->_cooked_size != 0
2314 if (! (_bfd_write_section_stabs
2315 (output_bfd, &coff_hash_table (finfo->info)->stab_info,
2316 o, &secdata->stab_info, contents)))
2321 if (! finfo->info->keep_memory)
2323 if (! _bfd_coff_free_symbols (input_bfd))
2330 /* Write out a global symbol. Called via coff_link_hash_traverse. */
2333 _bfd_coff_write_global_sym (h, data)
2334 struct coff_link_hash_entry *h;
2337 struct coff_final_link_info *finfo = (struct coff_final_link_info *) data;
2339 struct internal_syment isym;
2340 bfd_size_type symesz;
2343 output_bfd = finfo->output_bfd;
2349 && (finfo->info->strip == strip_all
2350 || (finfo->info->strip == strip_some
2351 && (bfd_hash_lookup (finfo->info->keep_hash,
2352 h->root.root.string, false, false)
2356 switch (h->root.type)
2359 case bfd_link_hash_new:
2363 case bfd_link_hash_undefined:
2364 case bfd_link_hash_undefweak:
2365 isym.n_scnum = N_UNDEF;
2369 case bfd_link_hash_defined:
2370 case bfd_link_hash_defweak:
2374 sec = h->root.u.def.section->output_section;
2375 if (bfd_is_abs_section (sec))
2376 isym.n_scnum = N_ABS;
2378 isym.n_scnum = sec->target_index;
2379 isym.n_value = (h->root.u.def.value
2380 + h->root.u.def.section->output_offset);
2381 if (! obj_pe (finfo->output_bfd))
2382 isym.n_value += sec->vma;
2386 case bfd_link_hash_common:
2387 isym.n_scnum = N_UNDEF;
2388 isym.n_value = h->root.u.c.size;
2391 case bfd_link_hash_indirect:
2392 case bfd_link_hash_warning:
2393 /* Just ignore these. They can't be handled anyhow. */
2397 if (strlen (h->root.root.string) <= SYMNMLEN)
2398 strncpy (isym._n._n_name, h->root.root.string, SYMNMLEN);
2405 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
2407 indx = _bfd_stringtab_add (finfo->strtab, h->root.root.string, hash,
2409 if (indx == (bfd_size_type) -1)
2411 finfo->failed = true;
2414 isym._n._n_n._n_zeroes = 0;
2415 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
2418 isym.n_sclass = h->class;
2419 isym.n_type = h->type;
2421 if (isym.n_sclass == C_NULL)
2422 isym.n_sclass = C_EXT;
2424 /* If doing task linking and this is the pass where we convert
2425 defined globals to statics, then do that conversion now. If the
2426 symbol is not being converted, just ignore it and it will be
2427 output during a later pass. */
2428 if (finfo->global_to_static)
2430 if (isym.n_sclass != C_EXT
2431 && isym.n_sclass != C_WEAKEXT
2432 && (! obj_pe (output_bfd) || isym.n_sclass != C_NT_WEAK))
2436 isym.n_sclass = C_STAT;
2439 isym.n_numaux = h->numaux;
2441 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) finfo->outsyms);
2443 symesz = bfd_coff_symesz (output_bfd);
2445 if (bfd_seek (output_bfd,
2446 (obj_sym_filepos (output_bfd)
2447 + obj_raw_syment_count (output_bfd) * symesz),
2449 || bfd_write (finfo->outsyms, symesz, 1, output_bfd) != symesz)
2451 finfo->failed = true;
2455 h->indx = obj_raw_syment_count (output_bfd);
2457 ++obj_raw_syment_count (output_bfd);
2459 /* Write out any associated aux entries. There normally will be
2460 none. If there are any, I have no idea how to modify them. */
2461 for (i = 0; i < isym.n_numaux; i++)
2463 bfd_coff_swap_aux_out (output_bfd, (PTR) (h->aux + i), isym.n_type,
2464 isym.n_sclass, i, isym.n_numaux,
2465 (PTR) finfo->outsyms);
2466 if (bfd_write (finfo->outsyms, symesz, 1, output_bfd) != symesz)
2468 finfo->failed = true;
2471 ++obj_raw_syment_count (output_bfd);
2477 /* Write out task global symbols, converting them to statics. Called
2478 via coff_link_hash_traverse. Calls bfd_coff_write_global_sym to do
2479 the dirty work, if the symbol we are processing needs conversion. */
2482 _bfd_coff_write_task_globals (h, data)
2483 struct coff_link_hash_entry *h;
2486 struct coff_final_link_info *finfo = (struct coff_final_link_info *) data;
2487 boolean rtnval = true;
2488 boolean save_global_to_static;
2492 switch (h->root.type)
2494 case bfd_link_hash_defined:
2495 case bfd_link_hash_defweak:
2496 save_global_to_static = finfo->global_to_static;
2497 finfo->global_to_static = true;
2498 rtnval = _bfd_coff_write_global_sym (h, data);
2499 finfo->global_to_static = save_global_to_static;
2508 /* Handle a link order which is supposed to generate a reloc. */
2511 _bfd_coff_reloc_link_order (output_bfd, finfo, output_section, link_order)
2513 struct coff_final_link_info *finfo;
2514 asection *output_section;
2515 struct bfd_link_order *link_order;
2517 reloc_howto_type *howto;
2518 struct internal_reloc *irel;
2519 struct coff_link_hash_entry **rel_hash_ptr;
2521 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
2524 bfd_set_error (bfd_error_bad_value);
2528 if (link_order->u.reloc.p->addend != 0)
2532 bfd_reloc_status_type rstat;
2535 size = bfd_get_reloc_size (howto);
2536 buf = (bfd_byte *) bfd_zmalloc (size);
2540 rstat = _bfd_relocate_contents (howto, output_bfd,
2541 link_order->u.reloc.p->addend, buf);
2547 case bfd_reloc_outofrange:
2549 case bfd_reloc_overflow:
2550 if (! ((*finfo->info->callbacks->reloc_overflow)
2552 (link_order->type == bfd_section_reloc_link_order
2553 ? bfd_section_name (output_bfd,
2554 link_order->u.reloc.p->u.section)
2555 : link_order->u.reloc.p->u.name),
2556 howto->name, link_order->u.reloc.p->addend,
2557 (bfd *) NULL, (asection *) NULL, (bfd_vma) 0)))
2564 ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf,
2565 (file_ptr) link_order->offset, size);
2571 /* Store the reloc information in the right place. It will get
2572 swapped and written out at the end of the final_link routine. */
2574 irel = (finfo->section_info[output_section->target_index].relocs
2575 + output_section->reloc_count);
2576 rel_hash_ptr = (finfo->section_info[output_section->target_index].rel_hashes
2577 + output_section->reloc_count);
2579 memset (irel, 0, sizeof (struct internal_reloc));
2580 *rel_hash_ptr = NULL;
2582 irel->r_vaddr = output_section->vma + link_order->offset;
2584 if (link_order->type == bfd_section_reloc_link_order)
2586 /* We need to somehow locate a symbol in the right section. The
2587 symbol must either have a value of zero, or we must adjust
2588 the addend by the value of the symbol. FIXME: Write this
2589 when we need it. The old linker couldn't handle this anyhow. */
2591 *rel_hash_ptr = NULL;
2596 struct coff_link_hash_entry *h;
2598 h = ((struct coff_link_hash_entry *)
2599 bfd_wrapped_link_hash_lookup (output_bfd, finfo->info,
2600 link_order->u.reloc.p->u.name,
2601 false, false, true));
2605 irel->r_symndx = h->indx;
2608 /* Set the index to -2 to force this symbol to get
2617 if (! ((*finfo->info->callbacks->unattached_reloc)
2618 (finfo->info, link_order->u.reloc.p->u.name, (bfd *) NULL,
2619 (asection *) NULL, (bfd_vma) 0)))
2625 /* FIXME: Is this always right? */
2626 irel->r_type = howto->type;
2628 /* r_size is only used on the RS/6000, which needs its own linker
2629 routines anyhow. r_extern is only used for ECOFF. */
2631 /* FIXME: What is the right value for r_offset? Is zero OK? */
2633 ++output_section->reloc_count;
2638 /* A basic reloc handling routine which may be used by processors with
2642 _bfd_coff_generic_relocate_section (output_bfd, info, input_bfd,
2643 input_section, contents, relocs, syms,
2646 struct bfd_link_info *info;
2648 asection *input_section;
2650 struct internal_reloc *relocs;
2651 struct internal_syment *syms;
2652 asection **sections;
2654 struct internal_reloc *rel;
2655 struct internal_reloc *relend;
2658 relend = rel + input_section->reloc_count;
2659 for (; rel < relend; rel++)
2662 struct coff_link_hash_entry *h;
2663 struct internal_syment *sym;
2666 reloc_howto_type *howto;
2667 bfd_reloc_status_type rstat;
2669 symndx = rel->r_symndx;
2677 || (unsigned long) symndx >= obj_raw_syment_count (input_bfd))
2679 (*_bfd_error_handler)
2680 ("%s: illegal symbol index %ld in relocs",
2681 bfd_get_filename (input_bfd), symndx);
2686 h = obj_coff_sym_hashes (input_bfd)[symndx];
2687 sym = syms + symndx;
2690 /* COFF treats common symbols in one of two ways. Either the
2691 size of the symbol is included in the section contents, or it
2692 is not. We assume that the size is not included, and force
2693 the rtype_to_howto function to adjust the addend as needed. */
2695 if (sym != NULL && sym->n_scnum != 0)
2696 addend = - sym->n_value;
2701 howto = bfd_coff_rtype_to_howto (input_bfd, input_section, rel, h,
2706 /* If we are doing a relocateable link, then we can just ignore
2707 a PC relative reloc that is pcrel_offset. It will already
2708 have the correct value. If this is not a relocateable link,
2709 then we should ignore the symbol value. */
2710 if (howto->pc_relative && howto->pcrel_offset)
2712 if (info->relocateable)
2714 if (sym != NULL && sym->n_scnum != 0)
2715 addend += sym->n_value;
2726 sec = bfd_abs_section_ptr;
2731 sec = sections[symndx];
2732 val = (sec->output_section->vma
2733 + sec->output_offset
2735 if (! obj_pe (input_bfd))
2741 if (h->root.type == bfd_link_hash_defined
2742 || h->root.type == bfd_link_hash_defweak)
2746 sec = h->root.u.def.section;
2747 val = (h->root.u.def.value
2748 + sec->output_section->vma
2749 + sec->output_offset);
2752 else if (! info->relocateable)
2754 if (! ((*info->callbacks->undefined_symbol)
2755 (info, h->root.root.string, input_bfd, input_section,
2756 rel->r_vaddr - input_section->vma)))
2761 if (info->base_file)
2763 /* Emit a reloc if the backend thinks it needs it. */
2764 if (sym && pe_data (output_bfd)->in_reloc_p (output_bfd, howto))
2766 /* Relocation to a symbol in a section which isn't
2767 absolute. We output the address here to a file.
2768 This file is then read by dlltool when generating the
2769 reloc section. Note that the base file is not
2770 portable between systems. We write out a long here,
2771 and dlltool reads in a long. */
2772 long addr = (rel->r_vaddr
2773 - input_section->vma
2774 + input_section->output_offset
2775 + input_section->output_section->vma);
2776 if (coff_data (output_bfd)->pe)
2777 addr -= pe_data(output_bfd)->pe_opthdr.ImageBase;
2778 if (fwrite (&addr, 1, sizeof (long), (FILE *) info->base_file)
2781 bfd_set_error (bfd_error_system_call);
2787 rstat = _bfd_final_link_relocate (howto, input_bfd, input_section,
2789 rel->r_vaddr - input_section->vma,
2798 case bfd_reloc_outofrange:
2799 (*_bfd_error_handler)
2800 (_("%s: bad reloc address 0x%lx in section `%s'"),
2801 bfd_get_filename (input_bfd),
2802 (unsigned long) rel->r_vaddr,
2803 bfd_get_section_name (input_bfd, input_section));
2805 case bfd_reloc_overflow:
2808 char buf[SYMNMLEN + 1];
2813 name = h->root.root.string;
2816 name = _bfd_coff_internal_syment_name (input_bfd, sym, buf);
2821 if (! ((*info->callbacks->reloc_overflow)
2822 (info, name, howto->name, (bfd_vma) 0, input_bfd,
2823 input_section, rel->r_vaddr - input_section->vma)))