* config/tc-alpha.c (alpha_cur_ent_sym): Remove.
[binutils.git] / bfd / cofflink.c
blob4e6138812425c9445857dbe1eeb46ba61d2578c7
1 /* COFF specific linker code.
2 Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
3 Free Software Foundation, Inc.
4 Written by Ian Lance Taylor, Cygnus Support.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 /* This file contains the COFF backend linker code. */
24 #include "bfd.h"
25 #include "sysdep.h"
26 #include "bfdlink.h"
27 #include "libbfd.h"
28 #include "coff/internal.h"
29 #include "libcoff.h"
31 static bfd_boolean coff_link_add_object_symbols
32 PARAMS ((bfd *, struct bfd_link_info *));
33 static bfd_boolean coff_link_check_archive_element
34 PARAMS ((bfd *, struct bfd_link_info *, bfd_boolean *));
35 static bfd_boolean coff_link_check_ar_symbols
36 PARAMS ((bfd *, struct bfd_link_info *, bfd_boolean *));
37 static bfd_boolean coff_link_add_symbols
38 PARAMS ((bfd *, struct bfd_link_info *));
39 static char *dores_com
40 PARAMS ((char *, bfd *, int));
41 static char *get_name
42 PARAMS ((char *, char **));
43 static int process_embedded_commands
44 PARAMS ((bfd *, struct bfd_link_info *, bfd *));
45 static void mark_relocs
46 PARAMS ((struct coff_final_link_info *, bfd *));
48 /* Return TRUE if SYM is a weak, external symbol. */
49 #define IS_WEAK_EXTERNAL(abfd, sym) \
50 ((sym).n_sclass == C_WEAKEXT \
51 || (obj_pe (abfd) && (sym).n_sclass == C_NT_WEAK))
53 /* Return TRUE if SYM is an external symbol. */
54 #define IS_EXTERNAL(abfd, sym) \
55 ((sym).n_sclass == C_EXT || IS_WEAK_EXTERNAL (abfd, sym))
57 /* Define macros so that the ISFCN, et. al., macros work correctly.
58 These macros are defined in include/coff/internal.h in terms of
59 N_TMASK, etc. These definitions require a user to define local
60 variables with the appropriate names, and with values from the
61 coff_data (abfd) structure. */
63 #define N_TMASK n_tmask
64 #define N_BTSHFT n_btshft
65 #define N_BTMASK n_btmask
67 /* Create an entry in a COFF linker hash table. */
69 struct bfd_hash_entry *
70 _bfd_coff_link_hash_newfunc (entry, table, string)
71 struct bfd_hash_entry *entry;
72 struct bfd_hash_table *table;
73 const char *string;
75 struct coff_link_hash_entry *ret = (struct coff_link_hash_entry *) entry;
77 /* Allocate the structure if it has not already been allocated by a
78 subclass. */
79 if (ret == (struct coff_link_hash_entry *) NULL)
80 ret = ((struct coff_link_hash_entry *)
81 bfd_hash_allocate (table, sizeof (struct coff_link_hash_entry)));
82 if (ret == (struct coff_link_hash_entry *) NULL)
83 return (struct bfd_hash_entry *) ret;
85 /* Call the allocation method of the superclass. */
86 ret = ((struct coff_link_hash_entry *)
87 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
88 table, string));
89 if (ret != (struct coff_link_hash_entry *) NULL)
91 /* Set local fields. */
92 ret->indx = -1;
93 ret->type = T_NULL;
94 ret->class = C_NULL;
95 ret->numaux = 0;
96 ret->auxbfd = NULL;
97 ret->aux = NULL;
100 return (struct bfd_hash_entry *) ret;
103 /* Initialize a COFF linker hash table. */
105 bfd_boolean
106 _bfd_coff_link_hash_table_init (table, abfd, newfunc)
107 struct coff_link_hash_table *table;
108 bfd *abfd;
109 struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *,
110 struct bfd_hash_table *,
111 const char *));
113 table->stab_info = NULL;
114 return _bfd_link_hash_table_init (&table->root, abfd, newfunc);
117 /* Create a COFF linker hash table. */
119 struct bfd_link_hash_table *
120 _bfd_coff_link_hash_table_create (abfd)
121 bfd *abfd;
123 struct coff_link_hash_table *ret;
124 bfd_size_type amt = sizeof (struct coff_link_hash_table);
126 ret = (struct coff_link_hash_table *) bfd_malloc (amt);
127 if (ret == NULL)
128 return NULL;
129 if (! _bfd_coff_link_hash_table_init (ret, abfd,
130 _bfd_coff_link_hash_newfunc))
132 free (ret);
133 return (struct bfd_link_hash_table *) NULL;
135 return &ret->root;
138 /* Create an entry in a COFF debug merge hash table. */
140 struct bfd_hash_entry *
141 _bfd_coff_debug_merge_hash_newfunc (entry, table, string)
142 struct bfd_hash_entry *entry;
143 struct bfd_hash_table *table;
144 const char *string;
146 struct coff_debug_merge_hash_entry *ret =
147 (struct coff_debug_merge_hash_entry *) entry;
149 /* Allocate the structure if it has not already been allocated by a
150 subclass. */
151 if (ret == (struct coff_debug_merge_hash_entry *) NULL)
152 ret = ((struct coff_debug_merge_hash_entry *)
153 bfd_hash_allocate (table,
154 sizeof (struct coff_debug_merge_hash_entry)));
155 if (ret == (struct coff_debug_merge_hash_entry *) NULL)
156 return (struct bfd_hash_entry *) ret;
158 /* Call the allocation method of the superclass. */
159 ret = ((struct coff_debug_merge_hash_entry *)
160 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
161 if (ret != (struct coff_debug_merge_hash_entry *) NULL)
163 /* Set local fields. */
164 ret->types = NULL;
167 return (struct bfd_hash_entry *) ret;
170 /* Given a COFF BFD, add symbols to the global hash table as
171 appropriate. */
173 bfd_boolean
174 _bfd_coff_link_add_symbols (abfd, info)
175 bfd *abfd;
176 struct bfd_link_info *info;
178 switch (bfd_get_format (abfd))
180 case bfd_object:
181 return coff_link_add_object_symbols (abfd, info);
182 case bfd_archive:
183 return (_bfd_generic_link_add_archive_symbols
184 (abfd, info, coff_link_check_archive_element));
185 default:
186 bfd_set_error (bfd_error_wrong_format);
187 return FALSE;
191 /* Add symbols from a COFF object file. */
193 static bfd_boolean
194 coff_link_add_object_symbols (abfd, info)
195 bfd *abfd;
196 struct bfd_link_info *info;
198 if (! _bfd_coff_get_external_symbols (abfd))
199 return FALSE;
200 if (! coff_link_add_symbols (abfd, info))
201 return FALSE;
203 if (! info->keep_memory)
205 if (! _bfd_coff_free_symbols (abfd))
206 return FALSE;
208 return TRUE;
211 /* Check a single archive element to see if we need to include it in
212 the link. *PNEEDED is set according to whether this element is
213 needed in the link or not. This is called via
214 _bfd_generic_link_add_archive_symbols. */
216 static bfd_boolean
217 coff_link_check_archive_element (abfd, info, pneeded)
218 bfd *abfd;
219 struct bfd_link_info *info;
220 bfd_boolean *pneeded;
222 if (! _bfd_coff_get_external_symbols (abfd))
223 return FALSE;
225 if (! coff_link_check_ar_symbols (abfd, info, pneeded))
226 return FALSE;
228 if (*pneeded)
230 if (! coff_link_add_symbols (abfd, info))
231 return FALSE;
234 if (! info->keep_memory || ! *pneeded)
236 if (! _bfd_coff_free_symbols (abfd))
237 return FALSE;
240 return TRUE;
243 /* Look through the symbols to see if this object file should be
244 included in the link. */
246 static bfd_boolean
247 coff_link_check_ar_symbols (abfd, info, pneeded)
248 bfd *abfd;
249 struct bfd_link_info *info;
250 bfd_boolean *pneeded;
252 bfd_size_type symesz;
253 bfd_byte *esym;
254 bfd_byte *esym_end;
256 *pneeded = FALSE;
258 symesz = bfd_coff_symesz (abfd);
259 esym = (bfd_byte *) obj_coff_external_syms (abfd);
260 esym_end = esym + obj_raw_syment_count (abfd) * symesz;
261 while (esym < esym_end)
263 struct internal_syment sym;
264 enum coff_symbol_classification classification;
266 bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym);
268 classification = bfd_coff_classify_symbol (abfd, &sym);
269 if (classification == COFF_SYMBOL_GLOBAL
270 || classification == COFF_SYMBOL_COMMON)
272 const char *name;
273 char buf[SYMNMLEN + 1];
274 struct bfd_link_hash_entry *h;
276 /* This symbol is externally visible, and is defined by this
277 object file. */
279 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
280 if (name == NULL)
281 return FALSE;
282 h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE);
284 /* auto import */
285 if (!h && info->pei386_auto_import)
287 if (!strncmp (name,"__imp_", 6))
290 bfd_link_hash_lookup (info->hash, name + 6, FALSE, FALSE,
291 TRUE);
294 /* We are only interested in symbols that are currently
295 undefined. If a symbol is currently known to be common,
296 COFF linkers do not bring in an object file which defines
297 it. */
298 if (h != (struct bfd_link_hash_entry *) NULL
299 && h->type == bfd_link_hash_undefined)
301 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
302 return FALSE;
303 *pneeded = TRUE;
304 return TRUE;
308 esym += (sym.n_numaux + 1) * symesz;
311 /* We do not need this object file. */
312 return TRUE;
315 /* Add all the symbols from an object file to the hash table. */
317 static bfd_boolean
318 coff_link_add_symbols (abfd, info)
319 bfd *abfd;
320 struct bfd_link_info *info;
322 unsigned int n_tmask = coff_data (abfd)->local_n_tmask;
323 unsigned int n_btshft = coff_data (abfd)->local_n_btshft;
324 unsigned int n_btmask = coff_data (abfd)->local_n_btmask;
325 bfd_boolean keep_syms;
326 bfd_boolean default_copy;
327 bfd_size_type symcount;
328 struct coff_link_hash_entry **sym_hash;
329 bfd_size_type symesz;
330 bfd_byte *esym;
331 bfd_byte *esym_end;
332 bfd_size_type amt;
334 /* Keep the symbols during this function, in case the linker needs
335 to read the generic symbols in order to report an error message. */
336 keep_syms = obj_coff_keep_syms (abfd);
337 obj_coff_keep_syms (abfd) = TRUE;
339 if (info->keep_memory)
340 default_copy = FALSE;
341 else
342 default_copy = TRUE;
344 symcount = obj_raw_syment_count (abfd);
346 /* We keep a list of the linker hash table entries that correspond
347 to particular symbols. */
348 amt = symcount * sizeof (struct coff_link_hash_entry *);
349 sym_hash = (struct coff_link_hash_entry **) bfd_zalloc (abfd, amt);
350 if (sym_hash == NULL && symcount != 0)
351 goto error_return;
352 obj_coff_sym_hashes (abfd) = sym_hash;
354 symesz = bfd_coff_symesz (abfd);
355 BFD_ASSERT (symesz == bfd_coff_auxesz (abfd));
356 esym = (bfd_byte *) obj_coff_external_syms (abfd);
357 esym_end = esym + symcount * symesz;
358 while (esym < esym_end)
360 struct internal_syment sym;
361 enum coff_symbol_classification classification;
362 bfd_boolean copy;
364 bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym);
366 classification = bfd_coff_classify_symbol (abfd, &sym);
367 if (classification != COFF_SYMBOL_LOCAL)
369 const char *name;
370 char buf[SYMNMLEN + 1];
371 flagword flags;
372 asection *section;
373 bfd_vma value;
374 bfd_boolean addit;
376 /* This symbol is externally visible. */
378 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
379 if (name == NULL)
380 goto error_return;
382 /* We must copy the name into memory if we got it from the
383 syment itself, rather than the string table. */
384 copy = default_copy;
385 if (sym._n._n_n._n_zeroes != 0
386 || sym._n._n_n._n_offset == 0)
387 copy = TRUE;
389 value = sym.n_value;
391 switch (classification)
393 default:
394 abort ();
396 case COFF_SYMBOL_GLOBAL:
397 flags = BSF_EXPORT | BSF_GLOBAL;
398 section = coff_section_from_bfd_index (abfd, sym.n_scnum);
399 if (! obj_pe (abfd))
400 value -= section->vma;
401 break;
403 case COFF_SYMBOL_UNDEFINED:
404 flags = 0;
405 section = bfd_und_section_ptr;
406 break;
408 case COFF_SYMBOL_COMMON:
409 flags = BSF_GLOBAL;
410 section = bfd_com_section_ptr;
411 break;
413 case COFF_SYMBOL_PE_SECTION:
414 flags = BSF_SECTION_SYM | BSF_GLOBAL;
415 section = coff_section_from_bfd_index (abfd, sym.n_scnum);
416 break;
419 if (IS_WEAK_EXTERNAL (abfd, sym))
420 flags = BSF_WEAK;
422 addit = TRUE;
424 /* In the PE format, section symbols actually refer to the
425 start of the output section. We handle them specially
426 here. */
427 if (obj_pe (abfd) && (flags & BSF_SECTION_SYM) != 0)
429 *sym_hash = coff_link_hash_lookup (coff_hash_table (info),
430 name, FALSE, copy, FALSE);
431 if (*sym_hash != NULL)
433 if (((*sym_hash)->coff_link_hash_flags
434 & COFF_LINK_HASH_PE_SECTION_SYMBOL) == 0
435 && (*sym_hash)->root.type != bfd_link_hash_undefined
436 && (*sym_hash)->root.type != bfd_link_hash_undefweak)
437 (*_bfd_error_handler)
438 ("Warning: symbol `%s' is both section and non-section",
439 name);
441 addit = FALSE;
445 /* The Microsoft Visual C compiler does string pooling by
446 hashing the constants to an internal symbol name, and
447 relying on the linker comdat support to discard
448 duplicate names. However, if one string is a literal and
449 one is a data initializer, one will end up in the .data
450 section and one will end up in the .rdata section. The
451 Microsoft linker will combine them into the .data
452 section, which seems to be wrong since it might cause the
453 literal to change.
455 As long as there are no external references to the
456 symbols, which there shouldn't be, we can treat the .data
457 and .rdata instances as separate symbols. The comdat
458 code in the linker will do the appropriate merging. Here
459 we avoid getting a multiple definition error for one of
460 these special symbols.
462 FIXME: I don't think this will work in the case where
463 there are two object files which use the constants as a
464 literal and two object files which use it as a data
465 initializer. One or the other of the second object files
466 is going to wind up with an inappropriate reference. */
467 if (obj_pe (abfd)
468 && (classification == COFF_SYMBOL_GLOBAL
469 || classification == COFF_SYMBOL_PE_SECTION)
470 && section->comdat != NULL
471 && strncmp (name, "??_", 3) == 0
472 && strcmp (name, section->comdat->name) == 0)
474 if (*sym_hash == NULL)
475 *sym_hash = coff_link_hash_lookup (coff_hash_table (info),
476 name, FALSE, copy, FALSE);
477 if (*sym_hash != NULL
478 && (*sym_hash)->root.type == bfd_link_hash_defined
479 && (*sym_hash)->root.u.def.section->comdat != NULL
480 && strcmp ((*sym_hash)->root.u.def.section->comdat->name,
481 section->comdat->name) == 0)
482 addit = FALSE;
485 if (addit)
487 if (! (bfd_coff_link_add_one_symbol
488 (info, abfd, name, flags, section, value,
489 (const char *) NULL, copy, FALSE,
490 (struct bfd_link_hash_entry **) sym_hash)))
491 goto error_return;
494 if (obj_pe (abfd) && (flags & BSF_SECTION_SYM) != 0)
495 (*sym_hash)->coff_link_hash_flags |=
496 COFF_LINK_HASH_PE_SECTION_SYMBOL;
498 /* Limit the alignment of a common symbol to the possible
499 alignment of a section. There is no point to permitting
500 a higher alignment for a common symbol: we can not
501 guarantee it, and it may cause us to allocate extra space
502 in the common section. */
503 if (section == bfd_com_section_ptr
504 && (*sym_hash)->root.type == bfd_link_hash_common
505 && ((*sym_hash)->root.u.c.p->alignment_power
506 > bfd_coff_default_section_alignment_power (abfd)))
507 (*sym_hash)->root.u.c.p->alignment_power
508 = bfd_coff_default_section_alignment_power (abfd);
510 if (info->hash->creator->flavour == bfd_get_flavour (abfd))
512 /* If we don't have any symbol information currently in
513 the hash table, or if we are looking at a symbol
514 definition, then update the symbol class and type in
515 the hash table. */
516 if (((*sym_hash)->class == C_NULL
517 && (*sym_hash)->type == T_NULL)
518 || sym.n_scnum != 0
519 || (sym.n_value != 0
520 && (*sym_hash)->root.type != bfd_link_hash_defined
521 && (*sym_hash)->root.type != bfd_link_hash_defweak))
523 (*sym_hash)->class = sym.n_sclass;
524 if (sym.n_type != T_NULL)
526 /* We want to warn if the type changed, but not
527 if it changed from an unspecified type.
528 Testing the whole type byte may work, but the
529 change from (e.g.) a function of unspecified
530 type to function of known type also wants to
531 skip the warning. */
532 if ((*sym_hash)->type != T_NULL
533 && (*sym_hash)->type != sym.n_type
534 && !(DTYPE ((*sym_hash)->type) == DTYPE (sym.n_type)
535 && (BTYPE ((*sym_hash)->type) == T_NULL
536 || BTYPE (sym.n_type) == T_NULL)))
537 (*_bfd_error_handler)
538 (_("Warning: type of symbol `%s' changed from %d to %d in %s"),
539 name, (*sym_hash)->type, sym.n_type,
540 bfd_archive_filename (abfd));
542 /* We don't want to change from a meaningful
543 base type to a null one, but if we know
544 nothing, take what little we might now know. */
545 if (BTYPE (sym.n_type) != T_NULL
546 || (*sym_hash)->type == T_NULL)
547 (*sym_hash)->type = sym.n_type;
549 (*sym_hash)->auxbfd = abfd;
550 if (sym.n_numaux != 0)
552 union internal_auxent *alloc;
553 unsigned int i;
554 bfd_byte *eaux;
555 union internal_auxent *iaux;
557 (*sym_hash)->numaux = sym.n_numaux;
558 alloc = ((union internal_auxent *)
559 bfd_hash_allocate (&info->hash->table,
560 (sym.n_numaux
561 * sizeof (*alloc))));
562 if (alloc == NULL)
563 goto error_return;
564 for (i = 0, eaux = esym + symesz, iaux = alloc;
565 i < sym.n_numaux;
566 i++, eaux += symesz, iaux++)
567 bfd_coff_swap_aux_in (abfd, (PTR) eaux, sym.n_type,
568 sym.n_sclass, (int) i,
569 sym.n_numaux, (PTR) iaux);
570 (*sym_hash)->aux = alloc;
575 if (classification == COFF_SYMBOL_PE_SECTION
576 && (*sym_hash)->numaux != 0)
578 /* Some PE sections (such as .bss) have a zero size in
579 the section header, but a non-zero size in the AUX
580 record. Correct that here.
582 FIXME: This is not at all the right place to do this.
583 For example, it won't help objdump. This needs to be
584 done when we swap in the section header. */
586 BFD_ASSERT ((*sym_hash)->numaux == 1);
587 if (section->_raw_size == 0)
588 section->_raw_size = (*sym_hash)->aux[0].x_scn.x_scnlen;
590 /* FIXME: We could test whether the section sizes
591 matches the size in the aux entry, but apparently
592 that sometimes fails unexpectedly. */
596 esym += (sym.n_numaux + 1) * symesz;
597 sym_hash += sym.n_numaux + 1;
600 /* If this is a non-traditional, non-relocateable link, try to
601 optimize the handling of any .stab/.stabstr sections. */
602 if (! info->relocateable
603 && ! info->traditional_format
604 && info->hash->creator->flavour == bfd_get_flavour (abfd)
605 && (info->strip != strip_all && info->strip != strip_debugger))
607 asection *stab, *stabstr;
609 stab = bfd_get_section_by_name (abfd, ".stab");
610 if (stab != NULL)
612 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
614 if (stabstr != NULL)
616 struct coff_link_hash_table *table;
617 struct coff_section_tdata *secdata;
619 secdata = coff_section_data (abfd, stab);
620 if (secdata == NULL)
622 amt = sizeof (struct coff_section_tdata);
623 stab->used_by_bfd = (PTR) bfd_zalloc (abfd, amt);
624 if (stab->used_by_bfd == NULL)
625 goto error_return;
626 secdata = coff_section_data (abfd, stab);
629 table = coff_hash_table (info);
631 if (! _bfd_link_section_stabs (abfd, &table->stab_info,
632 stab, stabstr,
633 &secdata->stab_info))
634 goto error_return;
639 obj_coff_keep_syms (abfd) = keep_syms;
641 return TRUE;
643 error_return:
644 obj_coff_keep_syms (abfd) = keep_syms;
645 return FALSE;
648 /* Do the final link step. */
650 bfd_boolean
651 _bfd_coff_final_link (abfd, info)
652 bfd *abfd;
653 struct bfd_link_info *info;
655 bfd_size_type symesz;
656 struct coff_final_link_info finfo;
657 bfd_boolean debug_merge_allocated;
658 bfd_boolean long_section_names;
659 asection *o;
660 struct bfd_link_order *p;
661 bfd_size_type max_sym_count;
662 bfd_size_type max_lineno_count;
663 bfd_size_type max_reloc_count;
664 bfd_size_type max_output_reloc_count;
665 bfd_size_type max_contents_size;
666 file_ptr rel_filepos;
667 unsigned int relsz;
668 file_ptr line_filepos;
669 unsigned int linesz;
670 bfd *sub;
671 bfd_byte *external_relocs = NULL;
672 char strbuf[STRING_SIZE_SIZE];
673 bfd_size_type amt;
675 symesz = bfd_coff_symesz (abfd);
677 finfo.info = info;
678 finfo.output_bfd = abfd;
679 finfo.strtab = NULL;
680 finfo.section_info = NULL;
681 finfo.last_file_index = -1;
682 finfo.last_bf_index = -1;
683 finfo.internal_syms = NULL;
684 finfo.sec_ptrs = NULL;
685 finfo.sym_indices = NULL;
686 finfo.outsyms = NULL;
687 finfo.linenos = NULL;
688 finfo.contents = NULL;
689 finfo.external_relocs = NULL;
690 finfo.internal_relocs = NULL;
691 finfo.global_to_static = FALSE;
692 debug_merge_allocated = FALSE;
694 coff_data (abfd)->link_info = info;
696 finfo.strtab = _bfd_stringtab_init ();
697 if (finfo.strtab == NULL)
698 goto error_return;
700 if (! coff_debug_merge_hash_table_init (&finfo.debug_merge))
701 goto error_return;
702 debug_merge_allocated = TRUE;
704 /* Compute the file positions for all the sections. */
705 if (! abfd->output_has_begun)
707 if (! bfd_coff_compute_section_file_positions (abfd))
708 goto error_return;
711 /* Count the line numbers and relocation entries required for the
712 output file. Set the file positions for the relocs. */
713 rel_filepos = obj_relocbase (abfd);
714 relsz = bfd_coff_relsz (abfd);
715 max_contents_size = 0;
716 max_lineno_count = 0;
717 max_reloc_count = 0;
719 long_section_names = FALSE;
720 for (o = abfd->sections; o != NULL; o = o->next)
722 o->reloc_count = 0;
723 o->lineno_count = 0;
724 for (p = o->link_order_head; p != NULL; p = p->next)
726 if (p->type == bfd_indirect_link_order)
728 asection *sec;
730 sec = p->u.indirect.section;
732 /* Mark all sections which are to be included in the
733 link. This will normally be every section. We need
734 to do this so that we can identify any sections which
735 the linker has decided to not include. */
736 sec->linker_mark = TRUE;
738 if (info->strip == strip_none
739 || info->strip == strip_some)
740 o->lineno_count += sec->lineno_count;
742 if (info->relocateable)
743 o->reloc_count += sec->reloc_count;
745 if (sec->_raw_size > max_contents_size)
746 max_contents_size = sec->_raw_size;
747 if (sec->lineno_count > max_lineno_count)
748 max_lineno_count = sec->lineno_count;
749 if (sec->reloc_count > max_reloc_count)
750 max_reloc_count = sec->reloc_count;
752 else if (info->relocateable
753 && (p->type == bfd_section_reloc_link_order
754 || p->type == bfd_symbol_reloc_link_order))
755 ++o->reloc_count;
757 if (o->reloc_count == 0)
758 o->rel_filepos = 0;
759 else
761 o->flags |= SEC_RELOC;
762 o->rel_filepos = rel_filepos;
763 rel_filepos += o->reloc_count * relsz;
764 /* In PE COFF, if there are at least 0xffff relocations an
765 extra relocation will be written out to encode the count. */
766 if (obj_pe (abfd) && o->reloc_count >= 0xffff)
767 rel_filepos += relsz;
770 if (bfd_coff_long_section_names (abfd)
771 && strlen (o->name) > SCNNMLEN)
773 /* This section has a long name which must go in the string
774 table. This must correspond to the code in
775 coff_write_object_contents which puts the string index
776 into the s_name field of the section header. That is why
777 we pass hash as FALSE. */
778 if (_bfd_stringtab_add (finfo.strtab, o->name, FALSE, FALSE)
779 == (bfd_size_type) -1)
780 goto error_return;
781 long_section_names = TRUE;
785 /* If doing a relocateable link, allocate space for the pointers we
786 need to keep. */
787 if (info->relocateable)
789 unsigned int i;
791 /* We use section_count + 1, rather than section_count, because
792 the target_index fields are 1 based. */
793 amt = abfd->section_count + 1;
794 amt *= sizeof (struct coff_link_section_info);
795 finfo.section_info = (struct coff_link_section_info *) bfd_malloc (amt);
796 if (finfo.section_info == NULL)
797 goto error_return;
798 for (i = 0; i <= abfd->section_count; i++)
800 finfo.section_info[i].relocs = NULL;
801 finfo.section_info[i].rel_hashes = NULL;
805 /* We now know the size of the relocs, so we can determine the file
806 positions of the line numbers. */
807 line_filepos = rel_filepos;
808 linesz = bfd_coff_linesz (abfd);
809 max_output_reloc_count = 0;
810 for (o = abfd->sections; o != NULL; o = o->next)
812 if (o->lineno_count == 0)
813 o->line_filepos = 0;
814 else
816 o->line_filepos = line_filepos;
817 line_filepos += o->lineno_count * linesz;
820 if (o->reloc_count != 0)
822 /* We don't know the indices of global symbols until we have
823 written out all the local symbols. For each section in
824 the output file, we keep an array of pointers to hash
825 table entries. Each entry in the array corresponds to a
826 reloc. When we find a reloc against a global symbol, we
827 set the corresponding entry in this array so that we can
828 fix up the symbol index after we have written out all the
829 local symbols.
831 Because of this problem, we also keep the relocs in
832 memory until the end of the link. This wastes memory,
833 but only when doing a relocateable link, which is not the
834 common case. */
835 BFD_ASSERT (info->relocateable);
836 amt = o->reloc_count;
837 amt *= sizeof (struct internal_reloc);
838 finfo.section_info[o->target_index].relocs =
839 (struct internal_reloc *) bfd_malloc (amt);
840 amt = o->reloc_count;
841 amt *= sizeof (struct coff_link_hash_entry *);
842 finfo.section_info[o->target_index].rel_hashes =
843 (struct coff_link_hash_entry **) bfd_malloc (amt);
844 if (finfo.section_info[o->target_index].relocs == NULL
845 || finfo.section_info[o->target_index].rel_hashes == NULL)
846 goto error_return;
848 if (o->reloc_count > max_output_reloc_count)
849 max_output_reloc_count = o->reloc_count;
852 /* Reset the reloc and lineno counts, so that we can use them to
853 count the number of entries we have output so far. */
854 o->reloc_count = 0;
855 o->lineno_count = 0;
858 obj_sym_filepos (abfd) = line_filepos;
860 /* Figure out the largest number of symbols in an input BFD. Take
861 the opportunity to clear the output_has_begun fields of all the
862 input BFD's. */
863 max_sym_count = 0;
864 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
866 size_t sz;
868 sub->output_has_begun = FALSE;
869 sz = obj_raw_syment_count (sub);
870 if (sz > max_sym_count)
871 max_sym_count = sz;
874 /* Allocate some buffers used while linking. */
875 amt = max_sym_count * sizeof (struct internal_syment);
876 finfo.internal_syms = (struct internal_syment *) bfd_malloc (amt);
877 amt = max_sym_count * sizeof (asection *);
878 finfo.sec_ptrs = (asection **) bfd_malloc (amt);
879 amt = max_sym_count * sizeof (long);
880 finfo.sym_indices = (long *) bfd_malloc (amt);
881 finfo.outsyms = (bfd_byte *) bfd_malloc ((max_sym_count + 1) * symesz);
882 amt = max_lineno_count * bfd_coff_linesz (abfd);
883 finfo.linenos = (bfd_byte *) bfd_malloc (amt);
884 finfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
885 amt = max_reloc_count * relsz;
886 finfo.external_relocs = (bfd_byte *) bfd_malloc (amt);
887 if (! info->relocateable)
889 amt = max_reloc_count * sizeof (struct internal_reloc);
890 finfo.internal_relocs = (struct internal_reloc *) bfd_malloc (amt);
892 if ((finfo.internal_syms == NULL && max_sym_count > 0)
893 || (finfo.sec_ptrs == NULL && max_sym_count > 0)
894 || (finfo.sym_indices == NULL && max_sym_count > 0)
895 || finfo.outsyms == NULL
896 || (finfo.linenos == NULL && max_lineno_count > 0)
897 || (finfo.contents == NULL && max_contents_size > 0)
898 || (finfo.external_relocs == NULL && max_reloc_count > 0)
899 || (! info->relocateable
900 && finfo.internal_relocs == NULL
901 && max_reloc_count > 0))
902 goto error_return;
904 /* We now know the position of everything in the file, except that
905 we don't know the size of the symbol table and therefore we don't
906 know where the string table starts. We just build the string
907 table in memory as we go along. We process all the relocations
908 for a single input file at once. */
909 obj_raw_syment_count (abfd) = 0;
911 if (coff_backend_info (abfd)->_bfd_coff_start_final_link)
913 if (! bfd_coff_start_final_link (abfd, info))
914 goto error_return;
917 for (o = abfd->sections; o != NULL; o = o->next)
919 for (p = o->link_order_head; p != NULL; p = p->next)
921 if (p->type == bfd_indirect_link_order
922 && bfd_family_coff (p->u.indirect.section->owner))
924 sub = p->u.indirect.section->owner;
925 if (! bfd_coff_link_output_has_begun (sub, & finfo))
927 if (! _bfd_coff_link_input_bfd (&finfo, sub))
928 goto error_return;
929 sub->output_has_begun = TRUE;
932 else if (p->type == bfd_section_reloc_link_order
933 || p->type == bfd_symbol_reloc_link_order)
935 if (! _bfd_coff_reloc_link_order (abfd, &finfo, o, p))
936 goto error_return;
938 else
940 if (! _bfd_default_link_order (abfd, info, o, p))
941 goto error_return;
946 if (! bfd_coff_final_link_postscript (abfd, & finfo))
947 goto error_return;
949 /* Free up the buffers used by _bfd_coff_link_input_bfd. */
951 coff_debug_merge_hash_table_free (&finfo.debug_merge);
952 debug_merge_allocated = FALSE;
954 if (finfo.internal_syms != NULL)
956 free (finfo.internal_syms);
957 finfo.internal_syms = NULL;
959 if (finfo.sec_ptrs != NULL)
961 free (finfo.sec_ptrs);
962 finfo.sec_ptrs = NULL;
964 if (finfo.sym_indices != NULL)
966 free (finfo.sym_indices);
967 finfo.sym_indices = NULL;
969 if (finfo.linenos != NULL)
971 free (finfo.linenos);
972 finfo.linenos = NULL;
974 if (finfo.contents != NULL)
976 free (finfo.contents);
977 finfo.contents = NULL;
979 if (finfo.external_relocs != NULL)
981 free (finfo.external_relocs);
982 finfo.external_relocs = NULL;
984 if (finfo.internal_relocs != NULL)
986 free (finfo.internal_relocs);
987 finfo.internal_relocs = NULL;
990 /* The value of the last C_FILE symbol is supposed to be the symbol
991 index of the first external symbol. Write it out again if
992 necessary. */
993 if (finfo.last_file_index != -1
994 && (unsigned int) finfo.last_file.n_value != obj_raw_syment_count (abfd))
996 file_ptr pos;
998 finfo.last_file.n_value = obj_raw_syment_count (abfd);
999 bfd_coff_swap_sym_out (abfd, (PTR) &finfo.last_file,
1000 (PTR) finfo.outsyms);
1002 pos = obj_sym_filepos (abfd) + finfo.last_file_index * symesz;
1003 if (bfd_seek (abfd, pos, SEEK_SET) != 0
1004 || bfd_bwrite (finfo.outsyms, symesz, abfd) != symesz)
1005 return FALSE;
1008 /* If doing task linking (ld --task-link) then make a pass through the
1009 global symbols, writing out any that are defined, and making them
1010 static. */
1011 if (info->task_link)
1013 finfo.failed = FALSE;
1014 coff_link_hash_traverse (coff_hash_table (info),
1015 _bfd_coff_write_task_globals,
1016 (PTR) &finfo);
1017 if (finfo.failed)
1018 goto error_return;
1021 /* Write out the global symbols. */
1022 finfo.failed = FALSE;
1023 coff_link_hash_traverse (coff_hash_table (info),
1024 _bfd_coff_write_global_sym,
1025 (PTR) &finfo);
1026 if (finfo.failed)
1027 goto error_return;
1029 /* The outsyms buffer is used by _bfd_coff_write_global_sym. */
1030 if (finfo.outsyms != NULL)
1032 free (finfo.outsyms);
1033 finfo.outsyms = NULL;
1036 if (info->relocateable && max_output_reloc_count > 0)
1038 /* Now that we have written out all the global symbols, we know
1039 the symbol indices to use for relocs against them, and we can
1040 finally write out the relocs. */
1041 amt = max_output_reloc_count * relsz;
1042 external_relocs = (bfd_byte *) bfd_malloc (amt);
1043 if (external_relocs == NULL)
1044 goto error_return;
1046 for (o = abfd->sections; o != NULL; o = o->next)
1048 struct internal_reloc *irel;
1049 struct internal_reloc *irelend;
1050 struct coff_link_hash_entry **rel_hash;
1051 bfd_byte *erel;
1053 if (o->reloc_count == 0)
1054 continue;
1056 irel = finfo.section_info[o->target_index].relocs;
1057 irelend = irel + o->reloc_count;
1058 rel_hash = finfo.section_info[o->target_index].rel_hashes;
1059 erel = external_relocs;
1060 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
1062 if (*rel_hash != NULL)
1064 BFD_ASSERT ((*rel_hash)->indx >= 0);
1065 irel->r_symndx = (*rel_hash)->indx;
1067 bfd_coff_swap_reloc_out (abfd, (PTR) irel, (PTR) erel);
1070 if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0
1071 || (bfd_bwrite ((PTR) external_relocs,
1072 (bfd_size_type) relsz * o->reloc_count, abfd)
1073 != (bfd_size_type) relsz * o->reloc_count))
1074 goto error_return;
1077 free (external_relocs);
1078 external_relocs = NULL;
1081 /* Free up the section information. */
1082 if (finfo.section_info != NULL)
1084 unsigned int i;
1086 for (i = 0; i < abfd->section_count; i++)
1088 if (finfo.section_info[i].relocs != NULL)
1089 free (finfo.section_info[i].relocs);
1090 if (finfo.section_info[i].rel_hashes != NULL)
1091 free (finfo.section_info[i].rel_hashes);
1093 free (finfo.section_info);
1094 finfo.section_info = NULL;
1097 /* If we have optimized stabs strings, output them. */
1098 if (coff_hash_table (info)->stab_info != NULL)
1100 if (! _bfd_write_stab_strings (abfd, &coff_hash_table (info)->stab_info))
1101 return FALSE;
1104 /* Write out the string table. */
1105 if (obj_raw_syment_count (abfd) != 0 || long_section_names)
1107 file_ptr pos;
1109 pos = obj_sym_filepos (abfd) + obj_raw_syment_count (abfd) * symesz;
1110 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
1111 return FALSE;
1113 #if STRING_SIZE_SIZE == 4
1114 H_PUT_32 (abfd,
1115 _bfd_stringtab_size (finfo.strtab) + STRING_SIZE_SIZE,
1116 strbuf);
1117 #else
1118 #error Change H_PUT_32 above
1119 #endif
1121 if (bfd_bwrite (strbuf, (bfd_size_type) STRING_SIZE_SIZE, abfd)
1122 != STRING_SIZE_SIZE)
1123 return FALSE;
1125 if (! _bfd_stringtab_emit (abfd, finfo.strtab))
1126 return FALSE;
1128 obj_coff_strings_written (abfd) = TRUE;
1131 _bfd_stringtab_free (finfo.strtab);
1133 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
1134 not try to write out the symbols. */
1135 bfd_get_symcount (abfd) = 0;
1137 return TRUE;
1139 error_return:
1140 if (debug_merge_allocated)
1141 coff_debug_merge_hash_table_free (&finfo.debug_merge);
1142 if (finfo.strtab != NULL)
1143 _bfd_stringtab_free (finfo.strtab);
1144 if (finfo.section_info != NULL)
1146 unsigned int i;
1148 for (i = 0; i < abfd->section_count; i++)
1150 if (finfo.section_info[i].relocs != NULL)
1151 free (finfo.section_info[i].relocs);
1152 if (finfo.section_info[i].rel_hashes != NULL)
1153 free (finfo.section_info[i].rel_hashes);
1155 free (finfo.section_info);
1157 if (finfo.internal_syms != NULL)
1158 free (finfo.internal_syms);
1159 if (finfo.sec_ptrs != NULL)
1160 free (finfo.sec_ptrs);
1161 if (finfo.sym_indices != NULL)
1162 free (finfo.sym_indices);
1163 if (finfo.outsyms != NULL)
1164 free (finfo.outsyms);
1165 if (finfo.linenos != NULL)
1166 free (finfo.linenos);
1167 if (finfo.contents != NULL)
1168 free (finfo.contents);
1169 if (finfo.external_relocs != NULL)
1170 free (finfo.external_relocs);
1171 if (finfo.internal_relocs != NULL)
1172 free (finfo.internal_relocs);
1173 if (external_relocs != NULL)
1174 free (external_relocs);
1175 return FALSE;
1178 /* parse out a -heap <reserved>,<commit> line */
1180 static char *
1181 dores_com (ptr, output_bfd, heap)
1182 char *ptr;
1183 bfd *output_bfd;
1184 int heap;
1186 if (coff_data(output_bfd)->pe)
1188 int val = strtoul (ptr, &ptr, 0);
1189 if (heap)
1190 pe_data(output_bfd)->pe_opthdr.SizeOfHeapReserve = val;
1191 else
1192 pe_data(output_bfd)->pe_opthdr.SizeOfStackReserve = val;
1194 if (ptr[0] == ',')
1196 val = strtoul (ptr+1, &ptr, 0);
1197 if (heap)
1198 pe_data(output_bfd)->pe_opthdr.SizeOfHeapCommit = val;
1199 else
1200 pe_data(output_bfd)->pe_opthdr.SizeOfStackCommit = val;
1203 return ptr;
1206 static char *get_name(ptr, dst)
1207 char *ptr;
1208 char **dst;
1210 while (*ptr == ' ')
1211 ptr++;
1212 *dst = ptr;
1213 while (*ptr && *ptr != ' ')
1214 ptr++;
1215 *ptr = 0;
1216 return ptr+1;
1219 /* Process any magic embedded commands in a section called .drectve */
1221 static int
1222 process_embedded_commands (output_bfd, info, abfd)
1223 bfd *output_bfd;
1224 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1225 bfd *abfd;
1227 asection *sec = bfd_get_section_by_name (abfd, ".drectve");
1228 char *s;
1229 char *e;
1230 char *copy;
1231 if (!sec)
1232 return 1;
1234 copy = bfd_malloc (sec->_raw_size);
1235 if (!copy)
1236 return 0;
1237 if (! bfd_get_section_contents(abfd, sec, copy, (bfd_vma) 0, sec->_raw_size))
1239 free (copy);
1240 return 0;
1242 e = copy + sec->_raw_size;
1243 for (s = copy; s < e ; )
1245 if (s[0]!= '-') {
1246 s++;
1247 continue;
1249 if (strncmp (s,"-attr", 5) == 0)
1251 char *name;
1252 char *attribs;
1253 asection *asec;
1255 int loop = 1;
1256 int had_write = 0;
1257 int had_read = 0;
1258 int had_exec= 0;
1259 int had_shared= 0;
1260 s += 5;
1261 s = get_name(s, &name);
1262 s = get_name(s, &attribs);
1263 while (loop) {
1264 switch (*attribs++)
1266 case 'W':
1267 had_write = 1;
1268 break;
1269 case 'R':
1270 had_read = 1;
1271 break;
1272 case 'S':
1273 had_shared = 1;
1274 break;
1275 case 'X':
1276 had_exec = 1;
1277 break;
1278 default:
1279 loop = 0;
1282 asec = bfd_get_section_by_name (abfd, name);
1283 if (asec) {
1284 if (had_exec)
1285 asec->flags |= SEC_CODE;
1286 if (!had_write)
1287 asec->flags |= SEC_READONLY;
1290 else if (strncmp (s,"-heap", 5) == 0)
1292 s = dores_com (s+5, output_bfd, 1);
1294 else if (strncmp (s,"-stack", 6) == 0)
1296 s = dores_com (s+6, output_bfd, 0);
1298 else
1299 s++;
1301 free (copy);
1302 return 1;
1305 /* Place a marker against all symbols which are used by relocations.
1306 This marker can be picked up by the 'do we skip this symbol ?'
1307 loop in _bfd_coff_link_input_bfd() and used to prevent skipping
1308 that symbol.
1311 static void
1312 mark_relocs (finfo, input_bfd)
1313 struct coff_final_link_info * finfo;
1314 bfd * input_bfd;
1316 asection * a;
1318 if ((bfd_get_file_flags (input_bfd) & HAS_SYMS) == 0)
1319 return;
1321 for (a = input_bfd->sections; a != (asection *) NULL; a = a->next)
1323 struct internal_reloc * internal_relocs;
1324 struct internal_reloc * irel;
1325 struct internal_reloc * irelend;
1327 if ((a->flags & SEC_RELOC) == 0 || a->reloc_count < 1)
1328 continue;
1329 /* Don't mark relocs in excluded sections. */
1330 if (a->output_section == bfd_abs_section_ptr)
1331 continue;
1333 /* Read in the relocs. */
1334 internal_relocs = _bfd_coff_read_internal_relocs
1335 (input_bfd, a, FALSE,
1336 finfo->external_relocs,
1337 finfo->info->relocateable,
1338 (finfo->info->relocateable
1339 ? (finfo->section_info[ a->output_section->target_index ].relocs + a->output_section->reloc_count)
1340 : finfo->internal_relocs)
1343 if (internal_relocs == NULL)
1344 continue;
1346 irel = internal_relocs;
1347 irelend = irel + a->reloc_count;
1349 /* Place a mark in the sym_indices array (whose entries have
1350 been initialised to 0) for all of the symbols that are used
1351 in the relocation table. This will then be picked up in the
1352 skip/don't pass */
1354 for (; irel < irelend; irel++)
1356 finfo->sym_indices[ irel->r_symndx ] = -1;
1361 /* Link an input file into the linker output file. This function
1362 handles all the sections and relocations of the input file at once. */
1364 bfd_boolean
1365 _bfd_coff_link_input_bfd (finfo, input_bfd)
1366 struct coff_final_link_info *finfo;
1367 bfd *input_bfd;
1369 unsigned int n_tmask = coff_data (input_bfd)->local_n_tmask;
1370 unsigned int n_btshft = coff_data (input_bfd)->local_n_btshft;
1371 #if 0
1372 unsigned int n_btmask = coff_data (input_bfd)->local_n_btmask;
1373 #endif
1374 bfd_boolean (*adjust_symndx)
1375 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *,
1376 struct internal_reloc *, bfd_boolean *));
1377 bfd *output_bfd;
1378 const char *strings;
1379 bfd_size_type syment_base;
1380 bfd_boolean copy, hash;
1381 bfd_size_type isymesz;
1382 bfd_size_type osymesz;
1383 bfd_size_type linesz;
1384 bfd_byte *esym;
1385 bfd_byte *esym_end;
1386 struct internal_syment *isymp;
1387 asection **secpp;
1388 long *indexp;
1389 unsigned long output_index;
1390 bfd_byte *outsym;
1391 struct coff_link_hash_entry **sym_hash;
1392 asection *o;
1394 /* Move all the symbols to the output file. */
1396 output_bfd = finfo->output_bfd;
1397 strings = NULL;
1398 syment_base = obj_raw_syment_count (output_bfd);
1399 isymesz = bfd_coff_symesz (input_bfd);
1400 osymesz = bfd_coff_symesz (output_bfd);
1401 linesz = bfd_coff_linesz (input_bfd);
1402 BFD_ASSERT (linesz == bfd_coff_linesz (output_bfd));
1404 copy = FALSE;
1405 if (! finfo->info->keep_memory)
1406 copy = TRUE;
1407 hash = TRUE;
1408 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
1409 hash = FALSE;
1411 if (! _bfd_coff_get_external_symbols (input_bfd))
1412 return FALSE;
1414 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
1415 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
1416 isymp = finfo->internal_syms;
1417 secpp = finfo->sec_ptrs;
1418 indexp = finfo->sym_indices;
1419 output_index = syment_base;
1420 outsym = finfo->outsyms;
1422 if (coff_data (output_bfd)->pe)
1424 if (! process_embedded_commands (output_bfd, finfo->info, input_bfd))
1425 return FALSE;
1428 /* If we are going to perform relocations and also strip/discard some symbols
1429 then we must make sure that we do not strip/discard those symbols that are
1430 going to be involved in the relocations */
1431 if (( finfo->info->strip != strip_none
1432 || finfo->info->discard != discard_none)
1433 && finfo->info->relocateable)
1435 /* mark the symbol array as 'not-used' */
1436 memset (indexp, 0, obj_raw_syment_count (input_bfd) * sizeof * indexp);
1438 mark_relocs (finfo, input_bfd);
1441 while (esym < esym_end)
1443 struct internal_syment isym;
1444 enum coff_symbol_classification classification;
1445 bfd_boolean skip;
1446 bfd_boolean global;
1447 bfd_boolean dont_skip_symbol;
1448 int add;
1450 bfd_coff_swap_sym_in (input_bfd, (PTR) esym, (PTR) isymp);
1452 /* Make a copy of *isymp so that the relocate_section function
1453 always sees the original values. This is more reliable than
1454 always recomputing the symbol value even if we are stripping
1455 the symbol. */
1456 isym = *isymp;
1458 classification = bfd_coff_classify_symbol (input_bfd, &isym);
1459 switch (classification)
1461 default:
1462 abort ();
1463 case COFF_SYMBOL_GLOBAL:
1464 case COFF_SYMBOL_PE_SECTION:
1465 case COFF_SYMBOL_LOCAL:
1466 *secpp = coff_section_from_bfd_index (input_bfd, isym.n_scnum);
1467 break;
1468 case COFF_SYMBOL_COMMON:
1469 *secpp = bfd_com_section_ptr;
1470 break;
1471 case COFF_SYMBOL_UNDEFINED:
1472 *secpp = bfd_und_section_ptr;
1473 break;
1476 /* Extract the flag indicating if this symbol is used by a
1477 relocation. */
1478 if ((finfo->info->strip != strip_none
1479 || finfo->info->discard != discard_none)
1480 && finfo->info->relocateable)
1481 dont_skip_symbol = *indexp;
1482 else
1483 dont_skip_symbol = FALSE;
1485 *indexp = -1;
1487 skip = FALSE;
1488 global = FALSE;
1489 add = 1 + isym.n_numaux;
1491 /* If we are stripping all symbols, we want to skip this one. */
1492 if (finfo->info->strip == strip_all && ! dont_skip_symbol)
1493 skip = TRUE;
1495 if (! skip)
1497 switch (classification)
1499 default:
1500 abort ();
1501 case COFF_SYMBOL_GLOBAL:
1502 case COFF_SYMBOL_COMMON:
1503 case COFF_SYMBOL_PE_SECTION:
1504 /* This is a global symbol. Global symbols come at the
1505 end of the symbol table, so skip them for now.
1506 Locally defined function symbols, however, are an
1507 exception, and are not moved to the end. */
1508 global = TRUE;
1509 if (! ISFCN (isym.n_type))
1510 skip = TRUE;
1511 break;
1513 case COFF_SYMBOL_UNDEFINED:
1514 /* Undefined symbols are left for the end. */
1515 global = TRUE;
1516 skip = TRUE;
1517 break;
1519 case COFF_SYMBOL_LOCAL:
1520 /* This is a local symbol. Skip it if we are discarding
1521 local symbols. */
1522 if (finfo->info->discard == discard_all && ! dont_skip_symbol)
1523 skip = TRUE;
1524 break;
1528 #ifndef COFF_WITH_PE
1529 /* Skip section symbols for sections which are not going to be
1530 emitted. */
1531 if (!skip
1532 && isym.n_sclass == C_STAT
1533 && isym.n_type == T_NULL
1534 && isym.n_numaux > 0)
1536 if ((*secpp)->output_section == bfd_abs_section_ptr)
1537 skip = TRUE;
1539 #endif
1541 /* If we stripping debugging symbols, and this is a debugging
1542 symbol, then skip it. FIXME: gas sets the section to N_ABS
1543 for some types of debugging symbols; I don't know if this is
1544 a bug or not. In any case, we handle it here. */
1545 if (! skip
1546 && finfo->info->strip == strip_debugger
1547 && ! dont_skip_symbol
1548 && (isym.n_scnum == N_DEBUG
1549 || (isym.n_scnum == N_ABS
1550 && (isym.n_sclass == C_AUTO
1551 || isym.n_sclass == C_REG
1552 || isym.n_sclass == C_MOS
1553 || isym.n_sclass == C_MOE
1554 || isym.n_sclass == C_MOU
1555 || isym.n_sclass == C_ARG
1556 || isym.n_sclass == C_REGPARM
1557 || isym.n_sclass == C_FIELD
1558 || isym.n_sclass == C_EOS))))
1559 skip = TRUE;
1561 /* If some symbols are stripped based on the name, work out the
1562 name and decide whether to skip this symbol. */
1563 if (! skip
1564 && (finfo->info->strip == strip_some
1565 || finfo->info->discard == discard_l))
1567 const char *name;
1568 char buf[SYMNMLEN + 1];
1570 name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf);
1571 if (name == NULL)
1572 return FALSE;
1574 if (! dont_skip_symbol
1575 && ((finfo->info->strip == strip_some
1576 && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE,
1577 FALSE) == NULL))
1578 || (! global
1579 && finfo->info->discard == discard_l
1580 && bfd_is_local_label_name (input_bfd, name))))
1581 skip = TRUE;
1584 /* If this is an enum, struct, or union tag, see if we have
1585 already output an identical type. */
1586 if (! skip
1587 && (finfo->output_bfd->flags & BFD_TRADITIONAL_FORMAT) == 0
1588 && (isym.n_sclass == C_ENTAG
1589 || isym.n_sclass == C_STRTAG
1590 || isym.n_sclass == C_UNTAG)
1591 && isym.n_numaux == 1)
1593 const char *name;
1594 char buf[SYMNMLEN + 1];
1595 struct coff_debug_merge_hash_entry *mh;
1596 struct coff_debug_merge_type *mt;
1597 union internal_auxent aux;
1598 struct coff_debug_merge_element **epp;
1599 bfd_byte *esl, *eslend;
1600 struct internal_syment *islp;
1601 bfd_size_type amt;
1603 name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf);
1604 if (name == NULL)
1605 return FALSE;
1607 /* Ignore fake names invented by compiler; treat them all as
1608 the same name. */
1609 if (*name == '~' || *name == '.' || *name == '$'
1610 || (*name == bfd_get_symbol_leading_char (input_bfd)
1611 && (name[1] == '~' || name[1] == '.' || name[1] == '$')))
1612 name = "";
1614 mh = coff_debug_merge_hash_lookup (&finfo->debug_merge, name,
1615 TRUE, TRUE);
1616 if (mh == NULL)
1617 return FALSE;
1619 /* Allocate memory to hold type information. If this turns
1620 out to be a duplicate, we pass this address to
1621 bfd_release. */
1622 amt = sizeof (struct coff_debug_merge_type);
1623 mt = (struct coff_debug_merge_type *) bfd_alloc (input_bfd, amt);
1624 if (mt == NULL)
1625 return FALSE;
1626 mt->class = isym.n_sclass;
1628 /* Pick up the aux entry, which points to the end of the tag
1629 entries. */
1630 bfd_coff_swap_aux_in (input_bfd, (PTR) (esym + isymesz),
1631 isym.n_type, isym.n_sclass, 0, isym.n_numaux,
1632 (PTR) &aux);
1634 /* Gather the elements. */
1635 epp = &mt->elements;
1636 mt->elements = NULL;
1637 islp = isymp + 2;
1638 esl = esym + 2 * isymesz;
1639 eslend = ((bfd_byte *) obj_coff_external_syms (input_bfd)
1640 + aux.x_sym.x_fcnary.x_fcn.x_endndx.l * isymesz);
1641 while (esl < eslend)
1643 const char *elename;
1644 char elebuf[SYMNMLEN + 1];
1645 char *name_copy;
1647 bfd_coff_swap_sym_in (input_bfd, (PTR) esl, (PTR) islp);
1649 amt = sizeof (struct coff_debug_merge_element);
1650 *epp = ((struct coff_debug_merge_element *)
1651 bfd_alloc (input_bfd, amt));
1652 if (*epp == NULL)
1653 return FALSE;
1655 elename = _bfd_coff_internal_syment_name (input_bfd, islp,
1656 elebuf);
1657 if (elename == NULL)
1658 return FALSE;
1660 amt = strlen (elename) + 1;
1661 name_copy = (char *) bfd_alloc (input_bfd, amt);
1662 if (name_copy == NULL)
1663 return FALSE;
1664 strcpy (name_copy, elename);
1666 (*epp)->name = name_copy;
1667 (*epp)->type = islp->n_type;
1668 (*epp)->tagndx = 0;
1669 if (islp->n_numaux >= 1
1670 && islp->n_type != T_NULL
1671 && islp->n_sclass != C_EOS)
1673 union internal_auxent eleaux;
1674 long indx;
1676 bfd_coff_swap_aux_in (input_bfd, (PTR) (esl + isymesz),
1677 islp->n_type, islp->n_sclass, 0,
1678 islp->n_numaux, (PTR) &eleaux);
1679 indx = eleaux.x_sym.x_tagndx.l;
1681 /* FIXME: If this tagndx entry refers to a symbol
1682 defined later in this file, we just ignore it.
1683 Handling this correctly would be tedious, and may
1684 not be required. */
1686 if (indx > 0
1687 && (indx
1688 < ((esym -
1689 (bfd_byte *) obj_coff_external_syms (input_bfd))
1690 / (long) isymesz)))
1692 (*epp)->tagndx = finfo->sym_indices[indx];
1693 if ((*epp)->tagndx < 0)
1694 (*epp)->tagndx = 0;
1697 epp = &(*epp)->next;
1698 *epp = NULL;
1700 esl += (islp->n_numaux + 1) * isymesz;
1701 islp += islp->n_numaux + 1;
1704 /* See if we already have a definition which matches this
1705 type. We always output the type if it has no elements,
1706 for simplicity. */
1707 if (mt->elements == NULL)
1708 bfd_release (input_bfd, (PTR) mt);
1709 else
1711 struct coff_debug_merge_type *mtl;
1713 for (mtl = mh->types; mtl != NULL; mtl = mtl->next)
1715 struct coff_debug_merge_element *me, *mel;
1717 if (mtl->class != mt->class)
1718 continue;
1720 for (me = mt->elements, mel = mtl->elements;
1721 me != NULL && mel != NULL;
1722 me = me->next, mel = mel->next)
1724 if (strcmp (me->name, mel->name) != 0
1725 || me->type != mel->type
1726 || me->tagndx != mel->tagndx)
1727 break;
1730 if (me == NULL && mel == NULL)
1731 break;
1734 if (mtl == NULL || (bfd_size_type) mtl->indx >= syment_base)
1736 /* This is the first definition of this type. */
1737 mt->indx = output_index;
1738 mt->next = mh->types;
1739 mh->types = mt;
1741 else
1743 /* This is a redefinition which can be merged. */
1744 bfd_release (input_bfd, (PTR) mt);
1745 *indexp = mtl->indx;
1746 add = (eslend - esym) / isymesz;
1747 skip = TRUE;
1752 /* We now know whether we are to skip this symbol or not. */
1753 if (! skip)
1755 /* Adjust the symbol in order to output it. */
1757 if (isym._n._n_n._n_zeroes == 0
1758 && isym._n._n_n._n_offset != 0)
1760 const char *name;
1761 bfd_size_type indx;
1763 /* This symbol has a long name. Enter it in the string
1764 table we are building. Note that we do not check
1765 bfd_coff_symname_in_debug. That is only true for
1766 XCOFF, and XCOFF requires different linking code
1767 anyhow. */
1768 name = _bfd_coff_internal_syment_name (input_bfd, &isym,
1769 (char *) NULL);
1770 if (name == NULL)
1771 return FALSE;
1772 indx = _bfd_stringtab_add (finfo->strtab, name, hash, copy);
1773 if (indx == (bfd_size_type) -1)
1774 return FALSE;
1775 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
1778 switch (isym.n_sclass)
1780 case C_AUTO:
1781 case C_MOS:
1782 case C_EOS:
1783 case C_MOE:
1784 case C_MOU:
1785 case C_UNTAG:
1786 case C_STRTAG:
1787 case C_ENTAG:
1788 case C_TPDEF:
1789 case C_ARG:
1790 case C_USTATIC:
1791 case C_REG:
1792 case C_REGPARM:
1793 case C_FIELD:
1794 /* The symbol value should not be modified. */
1795 break;
1797 case C_FCN:
1798 if (obj_pe (input_bfd)
1799 && strcmp (isym.n_name, ".bf") != 0
1800 && isym.n_scnum > 0)
1802 /* For PE, .lf and .ef get their value left alone,
1803 while .bf gets relocated. However, they all have
1804 "real" section numbers, and need to be moved into
1805 the new section. */
1806 isym.n_scnum = (*secpp)->output_section->target_index;
1807 break;
1809 /* Fall through. */
1810 default:
1811 case C_LABEL: /* Not completely sure about these 2 */
1812 case C_EXTDEF:
1813 case C_BLOCK:
1814 case C_EFCN:
1815 case C_NULL:
1816 case C_EXT:
1817 case C_STAT:
1818 case C_SECTION:
1819 case C_NT_WEAK:
1820 /* Compute new symbol location. */
1821 if (isym.n_scnum > 0)
1823 isym.n_scnum = (*secpp)->output_section->target_index;
1824 isym.n_value += (*secpp)->output_offset;
1825 if (! obj_pe (input_bfd))
1826 isym.n_value -= (*secpp)->vma;
1827 if (! obj_pe (finfo->output_bfd))
1828 isym.n_value += (*secpp)->output_section->vma;
1830 break;
1832 case C_FILE:
1833 /* The value of a C_FILE symbol is the symbol index of
1834 the next C_FILE symbol. The value of the last C_FILE
1835 symbol is the symbol index to the first external
1836 symbol (actually, coff_renumber_symbols does not get
1837 this right--it just sets the value of the last C_FILE
1838 symbol to zero--and nobody has ever complained about
1839 it). We try to get this right, below, just before we
1840 write the symbols out, but in the general case we may
1841 have to write the symbol out twice. */
1843 if (finfo->last_file_index != -1
1844 && finfo->last_file.n_value != (bfd_vma) output_index)
1846 /* We must correct the value of the last C_FILE
1847 entry. */
1848 finfo->last_file.n_value = output_index;
1849 if ((bfd_size_type) finfo->last_file_index >= syment_base)
1851 /* The last C_FILE symbol is in this input file. */
1852 bfd_coff_swap_sym_out (output_bfd,
1853 (PTR) &finfo->last_file,
1854 (PTR) (finfo->outsyms
1855 + ((finfo->last_file_index
1856 - syment_base)
1857 * osymesz)));
1859 else
1861 file_ptr pos;
1863 /* We have already written out the last C_FILE
1864 symbol. We need to write it out again. We
1865 borrow *outsym temporarily. */
1866 bfd_coff_swap_sym_out (output_bfd,
1867 (PTR) &finfo->last_file,
1868 (PTR) outsym);
1869 pos = obj_sym_filepos (output_bfd);
1870 pos += finfo->last_file_index * osymesz;
1871 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
1872 || bfd_bwrite (outsym, osymesz, output_bfd) != osymesz)
1873 return FALSE;
1877 finfo->last_file_index = output_index;
1878 finfo->last_file = isym;
1879 break;
1882 /* If doing task linking, convert normal global function symbols to
1883 static functions. */
1884 if (finfo->info->task_link && IS_EXTERNAL (input_bfd, isym))
1885 isym.n_sclass = C_STAT;
1887 /* Output the symbol. */
1889 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
1891 *indexp = output_index;
1893 if (global)
1895 long indx;
1896 struct coff_link_hash_entry *h;
1898 indx = ((esym - (bfd_byte *) obj_coff_external_syms (input_bfd))
1899 / isymesz);
1900 h = obj_coff_sym_hashes (input_bfd)[indx];
1901 if (h == NULL)
1903 /* This can happen if there were errors earlier in
1904 the link. */
1905 bfd_set_error (bfd_error_bad_value);
1906 return FALSE;
1908 h->indx = output_index;
1911 output_index += add;
1912 outsym += add * osymesz;
1915 esym += add * isymesz;
1916 isymp += add;
1917 ++secpp;
1918 ++indexp;
1919 for (--add; add > 0; --add)
1921 *secpp++ = NULL;
1922 *indexp++ = -1;
1926 /* Fix up the aux entries. This must be done in a separate pass,
1927 because we don't know the correct symbol indices until we have
1928 already decided which symbols we are going to keep. */
1930 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
1931 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
1932 isymp = finfo->internal_syms;
1933 indexp = finfo->sym_indices;
1934 sym_hash = obj_coff_sym_hashes (input_bfd);
1935 outsym = finfo->outsyms;
1936 while (esym < esym_end)
1938 int add;
1940 add = 1 + isymp->n_numaux;
1942 if ((*indexp < 0
1943 || (bfd_size_type) *indexp < syment_base)
1944 && (*sym_hash == NULL
1945 || (*sym_hash)->auxbfd != input_bfd))
1946 esym += add * isymesz;
1947 else
1949 struct coff_link_hash_entry *h;
1950 int i;
1952 h = NULL;
1953 if (*indexp < 0)
1955 h = *sym_hash;
1957 /* The m68k-motorola-sysv assembler will sometimes
1958 generate two symbols with the same name, but only one
1959 will have aux entries. */
1960 BFD_ASSERT (isymp->n_numaux == 0
1961 || h->numaux == isymp->n_numaux);
1964 esym += isymesz;
1966 if (h == NULL)
1967 outsym += osymesz;
1969 /* Handle the aux entries. This handling is based on
1970 coff_pointerize_aux. I don't know if it always correct. */
1971 for (i = 0; i < isymp->n_numaux && esym < esym_end; i++)
1973 union internal_auxent aux;
1974 union internal_auxent *auxp;
1976 if (h != NULL)
1977 auxp = h->aux + i;
1978 else
1980 bfd_coff_swap_aux_in (input_bfd, (PTR) esym, isymp->n_type,
1981 isymp->n_sclass, i, isymp->n_numaux,
1982 (PTR) &aux);
1983 auxp = &aux;
1986 if (isymp->n_sclass == C_FILE)
1988 /* If this is a long filename, we must put it in the
1989 string table. */
1990 if (auxp->x_file.x_n.x_zeroes == 0
1991 && auxp->x_file.x_n.x_offset != 0)
1993 const char *filename;
1994 bfd_size_type indx;
1996 BFD_ASSERT (auxp->x_file.x_n.x_offset
1997 >= STRING_SIZE_SIZE);
1998 if (strings == NULL)
2000 strings = _bfd_coff_read_string_table (input_bfd);
2001 if (strings == NULL)
2002 return FALSE;
2004 filename = strings + auxp->x_file.x_n.x_offset;
2005 indx = _bfd_stringtab_add (finfo->strtab, filename,
2006 hash, copy);
2007 if (indx == (bfd_size_type) -1)
2008 return FALSE;
2009 auxp->x_file.x_n.x_offset = STRING_SIZE_SIZE + indx;
2012 else if (isymp->n_sclass != C_STAT || isymp->n_type != T_NULL)
2014 unsigned long indx;
2016 if (ISFCN (isymp->n_type)
2017 || ISTAG (isymp->n_sclass)
2018 || isymp->n_sclass == C_BLOCK
2019 || isymp->n_sclass == C_FCN)
2021 indx = auxp->x_sym.x_fcnary.x_fcn.x_endndx.l;
2022 if (indx > 0
2023 && indx < obj_raw_syment_count (input_bfd))
2025 /* We look forward through the symbol for
2026 the index of the next symbol we are going
2027 to include. I don't know if this is
2028 entirely right. */
2029 while ((finfo->sym_indices[indx] < 0
2030 || ((bfd_size_type) finfo->sym_indices[indx]
2031 < syment_base))
2032 && indx < obj_raw_syment_count (input_bfd))
2033 ++indx;
2034 if (indx >= obj_raw_syment_count (input_bfd))
2035 indx = output_index;
2036 else
2037 indx = finfo->sym_indices[indx];
2038 auxp->x_sym.x_fcnary.x_fcn.x_endndx.l = indx;
2042 indx = auxp->x_sym.x_tagndx.l;
2043 if (indx > 0 && indx < obj_raw_syment_count (input_bfd))
2045 long symindx;
2047 symindx = finfo->sym_indices[indx];
2048 if (symindx < 0)
2049 auxp->x_sym.x_tagndx.l = 0;
2050 else
2051 auxp->x_sym.x_tagndx.l = symindx;
2054 /* The .bf symbols are supposed to be linked through
2055 the endndx field. We need to carry this list
2056 across object files. */
2057 if (i == 0
2058 && h == NULL
2059 && isymp->n_sclass == C_FCN
2060 && (isymp->_n._n_n._n_zeroes != 0
2061 || isymp->_n._n_n._n_offset == 0)
2062 && isymp->_n._n_name[0] == '.'
2063 && isymp->_n._n_name[1] == 'b'
2064 && isymp->_n._n_name[2] == 'f'
2065 && isymp->_n._n_name[3] == '\0')
2067 if (finfo->last_bf_index != -1)
2069 finfo->last_bf.x_sym.x_fcnary.x_fcn.x_endndx.l =
2070 *indexp;
2072 if ((bfd_size_type) finfo->last_bf_index
2073 >= syment_base)
2075 PTR auxout;
2077 /* The last .bf symbol is in this input
2078 file. This will only happen if the
2079 assembler did not set up the .bf
2080 endndx symbols correctly. */
2081 auxout = (PTR) (finfo->outsyms
2082 + ((finfo->last_bf_index
2083 - syment_base)
2084 * osymesz));
2085 bfd_coff_swap_aux_out (output_bfd,
2086 (PTR) &finfo->last_bf,
2087 isymp->n_type,
2088 isymp->n_sclass,
2089 0, isymp->n_numaux,
2090 auxout);
2092 else
2094 file_ptr pos;
2096 /* We have already written out the last
2097 .bf aux entry. We need to write it
2098 out again. We borrow *outsym
2099 temporarily. FIXME: This case should
2100 be made faster. */
2101 bfd_coff_swap_aux_out (output_bfd,
2102 (PTR) &finfo->last_bf,
2103 isymp->n_type,
2104 isymp->n_sclass,
2105 0, isymp->n_numaux,
2106 (PTR) outsym);
2107 pos = obj_sym_filepos (output_bfd);
2108 pos += finfo->last_bf_index * osymesz;
2109 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
2110 || (bfd_bwrite (outsym, osymesz, output_bfd)
2111 != osymesz))
2112 return FALSE;
2116 if (auxp->x_sym.x_fcnary.x_fcn.x_endndx.l != 0)
2117 finfo->last_bf_index = -1;
2118 else
2120 /* The endndx field of this aux entry must
2121 be updated with the symbol number of the
2122 next .bf symbol. */
2123 finfo->last_bf = *auxp;
2124 finfo->last_bf_index = (((outsym - finfo->outsyms)
2125 / osymesz)
2126 + syment_base);
2131 if (h == NULL)
2133 bfd_coff_swap_aux_out (output_bfd, (PTR) auxp, isymp->n_type,
2134 isymp->n_sclass, i, isymp->n_numaux,
2135 (PTR) outsym);
2136 outsym += osymesz;
2139 esym += isymesz;
2143 indexp += add;
2144 isymp += add;
2145 sym_hash += add;
2148 /* Relocate the line numbers, unless we are stripping them. */
2149 if (finfo->info->strip == strip_none
2150 || finfo->info->strip == strip_some)
2152 for (o = input_bfd->sections; o != NULL; o = o->next)
2154 bfd_vma offset;
2155 bfd_byte *eline;
2156 bfd_byte *elineend;
2157 bfd_byte *oeline;
2158 bfd_boolean skipping;
2159 file_ptr pos;
2160 bfd_size_type amt;
2162 /* FIXME: If SEC_HAS_CONTENTS is not for the section, then
2163 build_link_order in ldwrite.c will not have created a
2164 link order, which means that we will not have seen this
2165 input section in _bfd_coff_final_link, which means that
2166 we will not have allocated space for the line numbers of
2167 this section. I don't think line numbers can be
2168 meaningful for a section which does not have
2169 SEC_HAS_CONTENTS set, but, if they do, this must be
2170 changed. */
2171 if (o->lineno_count == 0
2172 || (o->output_section->flags & SEC_HAS_CONTENTS) == 0)
2173 continue;
2175 if (bfd_seek (input_bfd, o->line_filepos, SEEK_SET) != 0
2176 || bfd_bread (finfo->linenos, linesz * o->lineno_count,
2177 input_bfd) != linesz * o->lineno_count)
2178 return FALSE;
2180 offset = o->output_section->vma + o->output_offset - o->vma;
2181 eline = finfo->linenos;
2182 oeline = finfo->linenos;
2183 elineend = eline + linesz * o->lineno_count;
2184 skipping = FALSE;
2185 for (; eline < elineend; eline += linesz)
2187 struct internal_lineno iline;
2189 bfd_coff_swap_lineno_in (input_bfd, (PTR) eline, (PTR) &iline);
2191 if (iline.l_lnno != 0)
2192 iline.l_addr.l_paddr += offset;
2193 else if (iline.l_addr.l_symndx >= 0
2194 && ((unsigned long) iline.l_addr.l_symndx
2195 < obj_raw_syment_count (input_bfd)))
2197 long indx;
2199 indx = finfo->sym_indices[iline.l_addr.l_symndx];
2201 if (indx < 0)
2203 /* These line numbers are attached to a symbol
2204 which we are stripping. We must discard the
2205 line numbers because reading them back with
2206 no associated symbol (or associating them all
2207 with symbol #0) will fail. We can't regain
2208 the space in the output file, but at least
2209 they're dense. */
2210 skipping = TRUE;
2212 else
2214 struct internal_syment is;
2215 union internal_auxent ia;
2217 /* Fix up the lnnoptr field in the aux entry of
2218 the symbol. It turns out that we can't do
2219 this when we modify the symbol aux entries,
2220 because gas sometimes screws up the lnnoptr
2221 field and makes it an offset from the start
2222 of the line numbers rather than an absolute
2223 file index. */
2224 bfd_coff_swap_sym_in (output_bfd,
2225 (PTR) (finfo->outsyms
2226 + ((indx - syment_base)
2227 * osymesz)),
2228 (PTR) &is);
2229 if ((ISFCN (is.n_type)
2230 || is.n_sclass == C_BLOCK)
2231 && is.n_numaux >= 1)
2233 PTR auxptr;
2235 auxptr = (PTR) (finfo->outsyms
2236 + ((indx - syment_base + 1)
2237 * osymesz));
2238 bfd_coff_swap_aux_in (output_bfd, auxptr,
2239 is.n_type, is.n_sclass,
2240 0, is.n_numaux, (PTR) &ia);
2241 ia.x_sym.x_fcnary.x_fcn.x_lnnoptr =
2242 (o->output_section->line_filepos
2243 + o->output_section->lineno_count * linesz
2244 + eline - finfo->linenos);
2245 bfd_coff_swap_aux_out (output_bfd, (PTR) &ia,
2246 is.n_type, is.n_sclass, 0,
2247 is.n_numaux, auxptr);
2250 skipping = FALSE;
2253 iline.l_addr.l_symndx = indx;
2256 if (!skipping)
2258 bfd_coff_swap_lineno_out (output_bfd, (PTR) &iline,
2259 (PTR) oeline);
2260 oeline += linesz;
2264 pos = o->output_section->line_filepos;
2265 pos += o->output_section->lineno_count * linesz;
2266 amt = oeline - finfo->linenos;
2267 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
2268 || bfd_bwrite (finfo->linenos, amt, output_bfd) != amt)
2269 return FALSE;
2271 o->output_section->lineno_count += amt / linesz;
2275 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
2276 symbol will be the first symbol in the next input file. In the
2277 normal case, this will save us from writing out the C_FILE symbol
2278 again. */
2279 if (finfo->last_file_index != -1
2280 && (bfd_size_type) finfo->last_file_index >= syment_base)
2282 finfo->last_file.n_value = output_index;
2283 bfd_coff_swap_sym_out (output_bfd, (PTR) &finfo->last_file,
2284 (PTR) (finfo->outsyms
2285 + ((finfo->last_file_index - syment_base)
2286 * osymesz)));
2289 /* Write the modified symbols to the output file. */
2290 if (outsym > finfo->outsyms)
2292 file_ptr pos;
2293 bfd_size_type amt;
2295 pos = obj_sym_filepos (output_bfd) + syment_base * osymesz;
2296 amt = outsym - finfo->outsyms;
2297 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
2298 || bfd_bwrite (finfo->outsyms, amt, output_bfd) != amt)
2299 return FALSE;
2301 BFD_ASSERT ((obj_raw_syment_count (output_bfd)
2302 + (outsym - finfo->outsyms) / osymesz)
2303 == output_index);
2305 obj_raw_syment_count (output_bfd) = output_index;
2308 /* Relocate the contents of each section. */
2309 adjust_symndx = coff_backend_info (input_bfd)->_bfd_coff_adjust_symndx;
2310 for (o = input_bfd->sections; o != NULL; o = o->next)
2312 bfd_byte *contents;
2313 struct coff_section_tdata *secdata;
2315 if (! o->linker_mark)
2317 /* This section was omitted from the link. */
2318 continue;
2321 if ((o->flags & SEC_HAS_CONTENTS) == 0
2322 || (o->_raw_size == 0 && (o->flags & SEC_RELOC) == 0))
2324 if ((o->flags & SEC_RELOC) != 0
2325 && o->reloc_count != 0)
2327 ((*_bfd_error_handler)
2328 (_("%s: relocs in section `%s', but it has no contents"),
2329 bfd_archive_filename (input_bfd),
2330 bfd_get_section_name (input_bfd, o)));
2331 bfd_set_error (bfd_error_no_contents);
2332 return FALSE;
2335 continue;
2338 secdata = coff_section_data (input_bfd, o);
2339 if (secdata != NULL && secdata->contents != NULL)
2340 contents = secdata->contents;
2341 else
2343 if (! bfd_get_section_contents (input_bfd, o, finfo->contents,
2344 (file_ptr) 0, o->_raw_size))
2345 return FALSE;
2346 contents = finfo->contents;
2349 if ((o->flags & SEC_RELOC) != 0)
2351 int target_index;
2352 struct internal_reloc *internal_relocs;
2353 struct internal_reloc *irel;
2355 /* Read in the relocs. */
2356 target_index = o->output_section->target_index;
2357 internal_relocs = (_bfd_coff_read_internal_relocs
2358 (input_bfd, o, FALSE, finfo->external_relocs,
2359 finfo->info->relocateable,
2360 (finfo->info->relocateable
2361 ? (finfo->section_info[target_index].relocs
2362 + o->output_section->reloc_count)
2363 : finfo->internal_relocs)));
2364 if (internal_relocs == NULL)
2365 return FALSE;
2367 /* Call processor specific code to relocate the section
2368 contents. */
2369 if (! bfd_coff_relocate_section (output_bfd, finfo->info,
2370 input_bfd, o,
2371 contents,
2372 internal_relocs,
2373 finfo->internal_syms,
2374 finfo->sec_ptrs))
2375 return FALSE;
2377 if (finfo->info->relocateable)
2379 bfd_vma offset;
2380 struct internal_reloc *irelend;
2381 struct coff_link_hash_entry **rel_hash;
2383 offset = o->output_section->vma + o->output_offset - o->vma;
2384 irel = internal_relocs;
2385 irelend = irel + o->reloc_count;
2386 rel_hash = (finfo->section_info[target_index].rel_hashes
2387 + o->output_section->reloc_count);
2388 for (; irel < irelend; irel++, rel_hash++)
2390 struct coff_link_hash_entry *h;
2391 bfd_boolean adjusted;
2393 *rel_hash = NULL;
2395 /* Adjust the reloc address and symbol index. */
2397 irel->r_vaddr += offset;
2399 if (irel->r_symndx == -1)
2400 continue;
2402 if (adjust_symndx)
2404 if (! (*adjust_symndx) (output_bfd, finfo->info,
2405 input_bfd, o, irel,
2406 &adjusted))
2407 return FALSE;
2408 if (adjusted)
2409 continue;
2412 h = obj_coff_sym_hashes (input_bfd)[irel->r_symndx];
2413 if (h != NULL)
2415 /* This is a global symbol. */
2416 if (h->indx >= 0)
2417 irel->r_symndx = h->indx;
2418 else
2420 /* This symbol is being written at the end
2421 of the file, and we do not yet know the
2422 symbol index. We save the pointer to the
2423 hash table entry in the rel_hash list.
2424 We set the indx field to -2 to indicate
2425 that this symbol must not be stripped. */
2426 *rel_hash = h;
2427 h->indx = -2;
2430 else
2432 long indx;
2434 indx = finfo->sym_indices[irel->r_symndx];
2435 if (indx != -1)
2436 irel->r_symndx = indx;
2437 else
2439 struct internal_syment *is;
2440 const char *name;
2441 char buf[SYMNMLEN + 1];
2443 /* This reloc is against a symbol we are
2444 stripping. This should have been handled
2445 by the 'dont_skip_symbol' code in the while
2446 loop at the top of this function. */
2448 is = finfo->internal_syms + irel->r_symndx;
2450 name = (_bfd_coff_internal_syment_name
2451 (input_bfd, is, buf));
2452 if (name == NULL)
2453 return FALSE;
2455 if (! ((*finfo->info->callbacks->unattached_reloc)
2456 (finfo->info, name, input_bfd, o,
2457 irel->r_vaddr)))
2458 return FALSE;
2463 o->output_section->reloc_count += o->reloc_count;
2467 /* Write out the modified section contents. */
2468 if (secdata == NULL || secdata->stab_info == NULL)
2470 file_ptr loc = o->output_offset * bfd_octets_per_byte (output_bfd);
2471 bfd_size_type amt = (o->_cooked_size != 0
2472 ? o->_cooked_size : o->_raw_size);
2473 if (! bfd_set_section_contents (output_bfd, o->output_section,
2474 contents, loc, amt))
2475 return FALSE;
2477 else
2479 if (! (_bfd_write_section_stabs
2480 (output_bfd, &coff_hash_table (finfo->info)->stab_info,
2481 o, &secdata->stab_info, contents)))
2482 return FALSE;
2486 if (! finfo->info->keep_memory)
2488 if (! _bfd_coff_free_symbols (input_bfd))
2489 return FALSE;
2492 return TRUE;
2495 /* Write out a global symbol. Called via coff_link_hash_traverse. */
2497 bfd_boolean
2498 _bfd_coff_write_global_sym (h, data)
2499 struct coff_link_hash_entry *h;
2500 PTR data;
2502 struct coff_final_link_info *finfo = (struct coff_final_link_info *) data;
2503 bfd *output_bfd;
2504 struct internal_syment isym;
2505 bfd_size_type symesz;
2506 unsigned int i;
2507 file_ptr pos;
2509 output_bfd = finfo->output_bfd;
2511 if (h->root.type == bfd_link_hash_warning)
2513 h = (struct coff_link_hash_entry *) h->root.u.i.link;
2514 if (h->root.type == bfd_link_hash_new)
2515 return TRUE;
2518 if (h->indx >= 0)
2519 return TRUE;
2521 if (h->indx != -2
2522 && (finfo->info->strip == strip_all
2523 || (finfo->info->strip == strip_some
2524 && (bfd_hash_lookup (finfo->info->keep_hash,
2525 h->root.root.string, FALSE, FALSE)
2526 == NULL))))
2527 return TRUE;
2529 switch (h->root.type)
2531 default:
2532 case bfd_link_hash_new:
2533 case bfd_link_hash_warning:
2534 abort ();
2535 return FALSE;
2537 case bfd_link_hash_undefined:
2538 case bfd_link_hash_undefweak:
2539 isym.n_scnum = N_UNDEF;
2540 isym.n_value = 0;
2541 break;
2543 case bfd_link_hash_defined:
2544 case bfd_link_hash_defweak:
2546 asection *sec;
2548 sec = h->root.u.def.section->output_section;
2549 if (bfd_is_abs_section (sec))
2550 isym.n_scnum = N_ABS;
2551 else
2552 isym.n_scnum = sec->target_index;
2553 isym.n_value = (h->root.u.def.value
2554 + h->root.u.def.section->output_offset);
2555 if (! obj_pe (finfo->output_bfd))
2556 isym.n_value += sec->vma;
2558 break;
2560 case bfd_link_hash_common:
2561 isym.n_scnum = N_UNDEF;
2562 isym.n_value = h->root.u.c.size;
2563 break;
2565 case bfd_link_hash_indirect:
2566 /* Just ignore these. They can't be handled anyhow. */
2567 return TRUE;
2570 if (strlen (h->root.root.string) <= SYMNMLEN)
2571 strncpy (isym._n._n_name, h->root.root.string, SYMNMLEN);
2572 else
2574 bfd_boolean hash;
2575 bfd_size_type indx;
2577 hash = TRUE;
2578 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
2579 hash = FALSE;
2580 indx = _bfd_stringtab_add (finfo->strtab, h->root.root.string, hash,
2581 FALSE);
2582 if (indx == (bfd_size_type) -1)
2584 finfo->failed = TRUE;
2585 return FALSE;
2587 isym._n._n_n._n_zeroes = 0;
2588 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
2591 isym.n_sclass = h->class;
2592 isym.n_type = h->type;
2594 if (isym.n_sclass == C_NULL)
2595 isym.n_sclass = C_EXT;
2597 /* If doing task linking and this is the pass where we convert
2598 defined globals to statics, then do that conversion now. If the
2599 symbol is not being converted, just ignore it and it will be
2600 output during a later pass. */
2601 if (finfo->global_to_static)
2603 if (! IS_EXTERNAL (output_bfd, isym))
2604 return TRUE;
2606 isym.n_sclass = C_STAT;
2609 /* When a weak symbol is not overriden by a strong one,
2610 turn it into an external symbol when not building a
2611 shared or relocateable object. */
2612 if (! finfo->info->shared
2613 && ! finfo->info->relocateable
2614 && IS_WEAK_EXTERNAL (finfo->output_bfd, isym))
2615 isym.n_sclass = C_EXT;
2617 isym.n_numaux = h->numaux;
2619 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) finfo->outsyms);
2621 symesz = bfd_coff_symesz (output_bfd);
2623 pos = obj_sym_filepos (output_bfd);
2624 pos += obj_raw_syment_count (output_bfd) * symesz;
2625 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
2626 || bfd_bwrite (finfo->outsyms, symesz, output_bfd) != symesz)
2628 finfo->failed = TRUE;
2629 return FALSE;
2632 h->indx = obj_raw_syment_count (output_bfd);
2634 ++obj_raw_syment_count (output_bfd);
2636 /* Write out any associated aux entries. Most of the aux entries
2637 will have been modified in _bfd_coff_link_input_bfd. We have to
2638 handle section aux entries here, now that we have the final
2639 relocation and line number counts. */
2640 for (i = 0; i < isym.n_numaux; i++)
2642 union internal_auxent *auxp;
2644 auxp = h->aux + i;
2646 /* Look for a section aux entry here using the same tests that
2647 coff_swap_aux_out uses. */
2648 if (i == 0
2649 && (isym.n_sclass == C_STAT
2650 || isym.n_sclass == C_HIDDEN)
2651 && isym.n_type == T_NULL
2652 && (h->root.type == bfd_link_hash_defined
2653 || h->root.type == bfd_link_hash_defweak))
2655 asection *sec;
2657 sec = h->root.u.def.section->output_section;
2658 if (sec != NULL)
2660 auxp->x_scn.x_scnlen = (sec->_cooked_size != 0
2661 ? sec->_cooked_size
2662 : sec->_raw_size);
2664 /* For PE, an overflow on the final link reportedly does
2665 not matter. FIXME: Why not? */
2667 if (sec->reloc_count > 0xffff
2668 && (! obj_pe (output_bfd)
2669 || finfo->info->relocateable))
2670 (*_bfd_error_handler)
2671 (_("%s: %s: reloc overflow: 0x%lx > 0xffff"),
2672 bfd_get_filename (output_bfd),
2673 bfd_get_section_name (output_bfd, sec),
2674 sec->reloc_count);
2676 if (sec->lineno_count > 0xffff
2677 && (! obj_pe (output_bfd)
2678 || finfo->info->relocateable))
2679 (*_bfd_error_handler)
2680 (_("%s: warning: %s: line number overflow: 0x%lx > 0xffff"),
2681 bfd_get_filename (output_bfd),
2682 bfd_get_section_name (output_bfd, sec),
2683 sec->lineno_count);
2685 auxp->x_scn.x_nreloc = sec->reloc_count;
2686 auxp->x_scn.x_nlinno = sec->lineno_count;
2687 auxp->x_scn.x_checksum = 0;
2688 auxp->x_scn.x_associated = 0;
2689 auxp->x_scn.x_comdat = 0;
2693 bfd_coff_swap_aux_out (output_bfd, (PTR) auxp, isym.n_type,
2694 isym.n_sclass, (int) i, isym.n_numaux,
2695 (PTR) finfo->outsyms);
2696 if (bfd_bwrite (finfo->outsyms, symesz, output_bfd) != symesz)
2698 finfo->failed = TRUE;
2699 return FALSE;
2701 ++obj_raw_syment_count (output_bfd);
2704 return TRUE;
2707 /* Write out task global symbols, converting them to statics. Called
2708 via coff_link_hash_traverse. Calls bfd_coff_write_global_sym to do
2709 the dirty work, if the symbol we are processing needs conversion. */
2711 bfd_boolean
2712 _bfd_coff_write_task_globals (h, data)
2713 struct coff_link_hash_entry *h;
2714 PTR data;
2716 struct coff_final_link_info *finfo = (struct coff_final_link_info *) data;
2717 bfd_boolean rtnval = TRUE;
2718 bfd_boolean save_global_to_static;
2720 if (h->root.type == bfd_link_hash_warning)
2721 h = (struct coff_link_hash_entry *) h->root.u.i.link;
2723 if (h->indx < 0)
2725 switch (h->root.type)
2727 case bfd_link_hash_defined:
2728 case bfd_link_hash_defweak:
2729 save_global_to_static = finfo->global_to_static;
2730 finfo->global_to_static = TRUE;
2731 rtnval = _bfd_coff_write_global_sym (h, data);
2732 finfo->global_to_static = save_global_to_static;
2733 break;
2734 default:
2735 break;
2738 return (rtnval);
2741 /* Handle a link order which is supposed to generate a reloc. */
2743 bfd_boolean
2744 _bfd_coff_reloc_link_order (output_bfd, finfo, output_section, link_order)
2745 bfd *output_bfd;
2746 struct coff_final_link_info *finfo;
2747 asection *output_section;
2748 struct bfd_link_order *link_order;
2750 reloc_howto_type *howto;
2751 struct internal_reloc *irel;
2752 struct coff_link_hash_entry **rel_hash_ptr;
2754 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
2755 if (howto == NULL)
2757 bfd_set_error (bfd_error_bad_value);
2758 return FALSE;
2761 if (link_order->u.reloc.p->addend != 0)
2763 bfd_size_type size;
2764 bfd_byte *buf;
2765 bfd_reloc_status_type rstat;
2766 bfd_boolean ok;
2767 file_ptr loc;
2769 size = bfd_get_reloc_size (howto);
2770 buf = (bfd_byte *) bfd_zmalloc (size);
2771 if (buf == NULL)
2772 return FALSE;
2774 rstat = _bfd_relocate_contents (howto, output_bfd,
2775 (bfd_vma) link_order->u.reloc.p->addend,\
2776 buf);
2777 switch (rstat)
2779 case bfd_reloc_ok:
2780 break;
2781 default:
2782 case bfd_reloc_outofrange:
2783 abort ();
2784 case bfd_reloc_overflow:
2785 if (! ((*finfo->info->callbacks->reloc_overflow)
2786 (finfo->info,
2787 (link_order->type == bfd_section_reloc_link_order
2788 ? bfd_section_name (output_bfd,
2789 link_order->u.reloc.p->u.section)
2790 : link_order->u.reloc.p->u.name),
2791 howto->name, link_order->u.reloc.p->addend,
2792 (bfd *) NULL, (asection *) NULL, (bfd_vma) 0)))
2794 free (buf);
2795 return FALSE;
2797 break;
2799 loc = link_order->offset * bfd_octets_per_byte (output_bfd);
2800 ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf,
2801 loc, size);
2802 free (buf);
2803 if (! ok)
2804 return FALSE;
2807 /* Store the reloc information in the right place. It will get
2808 swapped and written out at the end of the final_link routine. */
2810 irel = (finfo->section_info[output_section->target_index].relocs
2811 + output_section->reloc_count);
2812 rel_hash_ptr = (finfo->section_info[output_section->target_index].rel_hashes
2813 + output_section->reloc_count);
2815 memset (irel, 0, sizeof (struct internal_reloc));
2816 *rel_hash_ptr = NULL;
2818 irel->r_vaddr = output_section->vma + link_order->offset;
2820 if (link_order->type == bfd_section_reloc_link_order)
2822 /* We need to somehow locate a symbol in the right section. The
2823 symbol must either have a value of zero, or we must adjust
2824 the addend by the value of the symbol. FIXME: Write this
2825 when we need it. The old linker couldn't handle this anyhow. */
2826 abort ();
2827 *rel_hash_ptr = NULL;
2828 irel->r_symndx = 0;
2830 else
2832 struct coff_link_hash_entry *h;
2834 h = ((struct coff_link_hash_entry *)
2835 bfd_wrapped_link_hash_lookup (output_bfd, finfo->info,
2836 link_order->u.reloc.p->u.name,
2837 FALSE, FALSE, TRUE));
2838 if (h != NULL)
2840 if (h->indx >= 0)
2841 irel->r_symndx = h->indx;
2842 else
2844 /* Set the index to -2 to force this symbol to get
2845 written out. */
2846 h->indx = -2;
2847 *rel_hash_ptr = h;
2848 irel->r_symndx = 0;
2851 else
2853 if (! ((*finfo->info->callbacks->unattached_reloc)
2854 (finfo->info, link_order->u.reloc.p->u.name, (bfd *) NULL,
2855 (asection *) NULL, (bfd_vma) 0)))
2856 return FALSE;
2857 irel->r_symndx = 0;
2861 /* FIXME: Is this always right? */
2862 irel->r_type = howto->type;
2864 /* r_size is only used on the RS/6000, which needs its own linker
2865 routines anyhow. r_extern is only used for ECOFF. */
2867 /* FIXME: What is the right value for r_offset? Is zero OK? */
2869 ++output_section->reloc_count;
2871 return TRUE;
2874 /* A basic reloc handling routine which may be used by processors with
2875 simple relocs. */
2877 bfd_boolean
2878 _bfd_coff_generic_relocate_section (output_bfd, info, input_bfd,
2879 input_section, contents, relocs, syms,
2880 sections)
2881 bfd *output_bfd;
2882 struct bfd_link_info *info;
2883 bfd *input_bfd;
2884 asection *input_section;
2885 bfd_byte *contents;
2886 struct internal_reloc *relocs;
2887 struct internal_syment *syms;
2888 asection **sections;
2890 struct internal_reloc *rel;
2891 struct internal_reloc *relend;
2893 rel = relocs;
2894 relend = rel + input_section->reloc_count;
2895 for (; rel < relend; rel++)
2897 long symndx;
2898 struct coff_link_hash_entry *h;
2899 struct internal_syment *sym;
2900 bfd_vma addend;
2901 bfd_vma val;
2902 reloc_howto_type *howto;
2903 bfd_reloc_status_type rstat;
2905 symndx = rel->r_symndx;
2907 if (symndx == -1)
2909 h = NULL;
2910 sym = NULL;
2912 else if (symndx < 0
2913 || (unsigned long) symndx >= obj_raw_syment_count (input_bfd))
2915 (*_bfd_error_handler)
2916 ("%s: illegal symbol index %ld in relocs",
2917 bfd_archive_filename (input_bfd), symndx);
2918 return FALSE;
2920 else
2922 h = obj_coff_sym_hashes (input_bfd)[symndx];
2923 sym = syms + symndx;
2926 /* COFF treats common symbols in one of two ways. Either the
2927 size of the symbol is included in the section contents, or it
2928 is not. We assume that the size is not included, and force
2929 the rtype_to_howto function to adjust the addend as needed. */
2931 if (sym != NULL && sym->n_scnum != 0)
2932 addend = - sym->n_value;
2933 else
2934 addend = 0;
2936 howto = bfd_coff_rtype_to_howto (input_bfd, input_section, rel, h,
2937 sym, &addend);
2938 if (howto == NULL)
2939 return FALSE;
2941 /* If we are doing a relocateable link, then we can just ignore
2942 a PC relative reloc that is pcrel_offset. It will already
2943 have the correct value. If this is not a relocateable link,
2944 then we should ignore the symbol value. */
2945 if (howto->pc_relative && howto->pcrel_offset)
2947 if (info->relocateable)
2948 continue;
2949 if (sym != NULL && sym->n_scnum != 0)
2950 addend += sym->n_value;
2953 val = 0;
2955 if (h == NULL)
2957 asection *sec;
2959 if (symndx == -1)
2961 sec = bfd_abs_section_ptr;
2962 val = 0;
2964 else
2966 sec = sections[symndx];
2967 val = (sec->output_section->vma
2968 + sec->output_offset
2969 + sym->n_value);
2970 if (! obj_pe (input_bfd))
2971 val -= sec->vma;
2974 else
2976 if (h->root.type == bfd_link_hash_defined
2977 || h->root.type == bfd_link_hash_defweak)
2979 asection *sec;
2981 sec = h->root.u.def.section;
2982 val = (h->root.u.def.value
2983 + sec->output_section->vma
2984 + sec->output_offset);
2987 else if (h->root.type == bfd_link_hash_undefweak)
2988 val = 0;
2990 else if (! info->relocateable)
2992 if (! ((*info->callbacks->undefined_symbol)
2993 (info, h->root.root.string, input_bfd, input_section,
2994 rel->r_vaddr - input_section->vma, TRUE)))
2995 return FALSE;
2999 if (info->base_file)
3001 /* Emit a reloc if the backend thinks it needs it. */
3002 if (sym && pe_data (output_bfd)->in_reloc_p (output_bfd, howto))
3004 /* Relocation to a symbol in a section which isn't
3005 absolute. We output the address here to a file.
3006 This file is then read by dlltool when generating the
3007 reloc section. Note that the base file is not
3008 portable between systems. We write out a long here,
3009 and dlltool reads in a long. */
3010 long addr = (rel->r_vaddr
3011 - input_section->vma
3012 + input_section->output_offset
3013 + input_section->output_section->vma);
3014 if (coff_data (output_bfd)->pe)
3015 addr -= pe_data(output_bfd)->pe_opthdr.ImageBase;
3016 if (fwrite (&addr, 1, sizeof (long), (FILE *) info->base_file)
3017 != sizeof (long))
3019 bfd_set_error (bfd_error_system_call);
3020 return FALSE;
3025 rstat = _bfd_final_link_relocate (howto, input_bfd, input_section,
3026 contents,
3027 rel->r_vaddr - input_section->vma,
3028 val, addend);
3030 switch (rstat)
3032 default:
3033 abort ();
3034 case bfd_reloc_ok:
3035 break;
3036 case bfd_reloc_outofrange:
3037 (*_bfd_error_handler)
3038 (_("%s: bad reloc address 0x%lx in section `%s'"),
3039 bfd_archive_filename (input_bfd),
3040 (unsigned long) rel->r_vaddr,
3041 bfd_get_section_name (input_bfd, input_section));
3042 return FALSE;
3043 case bfd_reloc_overflow:
3045 const char *name;
3046 char buf[SYMNMLEN + 1];
3048 if (symndx == -1)
3049 name = "*ABS*";
3050 else if (h != NULL)
3051 name = h->root.root.string;
3052 else
3054 name = _bfd_coff_internal_syment_name (input_bfd, sym, buf);
3055 if (name == NULL)
3056 return FALSE;
3059 if (! ((*info->callbacks->reloc_overflow)
3060 (info, name, howto->name, (bfd_vma) 0, input_bfd,
3061 input_section, rel->r_vaddr - input_section->vma)))
3062 return FALSE;
3066 return TRUE;