* elf.c (sym_is_global): Return a bfd_boolean.
[binutils.git] / bfd / cofflink.c
bloba1f6684bb61cc1fab98c71c0e5529198fdf71428
1 /* COFF specific linker code.
2 Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3 2004, 2005, 2006 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., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, 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"
30 #include "safe-ctype.h"
32 static bfd_boolean coff_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info);
33 static bfd_boolean coff_link_check_archive_element (bfd *abfd, struct bfd_link_info *info, bfd_boolean *pneeded);
34 static bfd_boolean coff_link_add_symbols (bfd *abfd, struct bfd_link_info *info);
36 /* Return TRUE if SYM is a weak, external symbol. */
37 #define IS_WEAK_EXTERNAL(abfd, sym) \
38 ((sym).n_sclass == C_WEAKEXT \
39 || (obj_pe (abfd) && (sym).n_sclass == C_NT_WEAK))
41 /* Return TRUE if SYM is an external symbol. */
42 #define IS_EXTERNAL(abfd, sym) \
43 ((sym).n_sclass == C_EXT || IS_WEAK_EXTERNAL (abfd, sym))
45 /* Define macros so that the ISFCN, et. al., macros work correctly.
46 These macros are defined in include/coff/internal.h in terms of
47 N_TMASK, etc. These definitions require a user to define local
48 variables with the appropriate names, and with values from the
49 coff_data (abfd) structure. */
51 #define N_TMASK n_tmask
52 #define N_BTSHFT n_btshft
53 #define N_BTMASK n_btmask
55 /* Create an entry in a COFF linker hash table. */
57 struct bfd_hash_entry *
58 _bfd_coff_link_hash_newfunc (struct bfd_hash_entry *entry,
59 struct bfd_hash_table *table,
60 const char *string)
62 struct coff_link_hash_entry *ret = (struct coff_link_hash_entry *) entry;
64 /* Allocate the structure if it has not already been allocated by a
65 subclass. */
66 if (ret == (struct coff_link_hash_entry *) NULL)
67 ret = ((struct coff_link_hash_entry *)
68 bfd_hash_allocate (table, sizeof (struct coff_link_hash_entry)));
69 if (ret == (struct coff_link_hash_entry *) NULL)
70 return (struct bfd_hash_entry *) ret;
72 /* Call the allocation method of the superclass. */
73 ret = ((struct coff_link_hash_entry *)
74 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
75 table, string));
76 if (ret != (struct coff_link_hash_entry *) NULL)
78 /* Set local fields. */
79 ret->indx = -1;
80 ret->type = T_NULL;
81 ret->class = C_NULL;
82 ret->numaux = 0;
83 ret->auxbfd = NULL;
84 ret->aux = NULL;
87 return (struct bfd_hash_entry *) ret;
90 /* Initialize a COFF linker hash table. */
92 bfd_boolean
93 _bfd_coff_link_hash_table_init (struct coff_link_hash_table *table,
94 bfd *abfd,
95 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
96 struct bfd_hash_table *,
97 const char *),
98 unsigned int entsize)
100 memset (&table->stab_info, 0, sizeof (table->stab_info));
101 return _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
104 /* Create a COFF linker hash table. */
106 struct bfd_link_hash_table *
107 _bfd_coff_link_hash_table_create (bfd *abfd)
109 struct coff_link_hash_table *ret;
110 bfd_size_type amt = sizeof (struct coff_link_hash_table);
112 ret = bfd_malloc (amt);
113 if (ret == NULL)
114 return NULL;
116 if (! _bfd_coff_link_hash_table_init (ret, abfd,
117 _bfd_coff_link_hash_newfunc,
118 sizeof (struct coff_link_hash_entry)))
120 free (ret);
121 return (struct bfd_link_hash_table *) NULL;
123 return &ret->root;
126 /* Create an entry in a COFF debug merge hash table. */
128 struct bfd_hash_entry *
129 _bfd_coff_debug_merge_hash_newfunc (struct bfd_hash_entry *entry,
130 struct bfd_hash_table *table,
131 const char *string)
133 struct coff_debug_merge_hash_entry *ret =
134 (struct coff_debug_merge_hash_entry *) entry;
136 /* Allocate the structure if it has not already been allocated by a
137 subclass. */
138 if (ret == (struct coff_debug_merge_hash_entry *) NULL)
139 ret = ((struct coff_debug_merge_hash_entry *)
140 bfd_hash_allocate (table,
141 sizeof (struct coff_debug_merge_hash_entry)));
142 if (ret == (struct coff_debug_merge_hash_entry *) NULL)
143 return (struct bfd_hash_entry *) ret;
145 /* Call the allocation method of the superclass. */
146 ret = ((struct coff_debug_merge_hash_entry *)
147 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
148 if (ret != (struct coff_debug_merge_hash_entry *) NULL)
150 /* Set local fields. */
151 ret->types = NULL;
154 return (struct bfd_hash_entry *) ret;
157 /* Given a COFF BFD, add symbols to the global hash table as
158 appropriate. */
160 bfd_boolean
161 _bfd_coff_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
163 switch (bfd_get_format (abfd))
165 case bfd_object:
166 return coff_link_add_object_symbols (abfd, info);
167 case bfd_archive:
168 return _bfd_generic_link_add_archive_symbols
169 (abfd, info, coff_link_check_archive_element);
170 default:
171 bfd_set_error (bfd_error_wrong_format);
172 return FALSE;
176 /* Add symbols from a COFF object file. */
178 static bfd_boolean
179 coff_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
181 if (! _bfd_coff_get_external_symbols (abfd))
182 return FALSE;
183 if (! coff_link_add_symbols (abfd, info))
184 return FALSE;
186 if (! info->keep_memory
187 && ! _bfd_coff_free_symbols (abfd))
188 return FALSE;
190 return TRUE;
193 /* Look through the symbols to see if this object file should be
194 included in the link. */
196 static bfd_boolean
197 coff_link_check_ar_symbols (bfd *abfd,
198 struct bfd_link_info *info,
199 bfd_boolean *pneeded)
201 bfd_size_type symesz;
202 bfd_byte *esym;
203 bfd_byte *esym_end;
205 *pneeded = FALSE;
207 symesz = bfd_coff_symesz (abfd);
208 esym = (bfd_byte *) obj_coff_external_syms (abfd);
209 esym_end = esym + obj_raw_syment_count (abfd) * symesz;
210 while (esym < esym_end)
212 struct internal_syment sym;
213 enum coff_symbol_classification classification;
215 bfd_coff_swap_sym_in (abfd, esym, &sym);
217 classification = bfd_coff_classify_symbol (abfd, &sym);
218 if (classification == COFF_SYMBOL_GLOBAL
219 || classification == COFF_SYMBOL_COMMON)
221 const char *name;
222 char buf[SYMNMLEN + 1];
223 struct bfd_link_hash_entry *h;
225 /* This symbol is externally visible, and is defined by this
226 object file. */
227 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
228 if (name == NULL)
229 return FALSE;
230 h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE);
232 /* Auto import. */
233 if (!h
234 && info->pei386_auto_import
235 && !strncmp (name,"__imp_", 6))
236 h = bfd_link_hash_lookup (info->hash, name + 6, FALSE, FALSE, TRUE);
238 /* We are only interested in symbols that are currently
239 undefined. If a symbol is currently known to be common,
240 COFF linkers do not bring in an object file which defines
241 it. */
242 if (h != (struct bfd_link_hash_entry *) NULL
243 && h->type == bfd_link_hash_undefined)
245 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
246 return FALSE;
247 *pneeded = TRUE;
248 return TRUE;
252 esym += (sym.n_numaux + 1) * symesz;
255 /* We do not need this object file. */
256 return TRUE;
259 /* Check a single archive element to see if we need to include it in
260 the link. *PNEEDED is set according to whether this element is
261 needed in the link or not. This is called via
262 _bfd_generic_link_add_archive_symbols. */
264 static bfd_boolean
265 coff_link_check_archive_element (bfd *abfd,
266 struct bfd_link_info *info,
267 bfd_boolean *pneeded)
269 if (! _bfd_coff_get_external_symbols (abfd))
270 return FALSE;
272 if (! coff_link_check_ar_symbols (abfd, info, pneeded))
273 return FALSE;
275 if (*pneeded
276 && ! coff_link_add_symbols (abfd, info))
277 return FALSE;
279 if ((! info->keep_memory || ! *pneeded)
280 && ! _bfd_coff_free_symbols (abfd))
281 return FALSE;
283 return TRUE;
286 /* Add all the symbols from an object file to the hash table. */
288 static bfd_boolean
289 coff_link_add_symbols (bfd *abfd,
290 struct bfd_link_info *info)
292 unsigned int n_tmask = coff_data (abfd)->local_n_tmask;
293 unsigned int n_btshft = coff_data (abfd)->local_n_btshft;
294 unsigned int n_btmask = coff_data (abfd)->local_n_btmask;
295 bfd_boolean keep_syms;
296 bfd_boolean default_copy;
297 bfd_size_type symcount;
298 struct coff_link_hash_entry **sym_hash;
299 bfd_size_type symesz;
300 bfd_byte *esym;
301 bfd_byte *esym_end;
302 bfd_size_type amt;
304 /* Keep the symbols during this function, in case the linker needs
305 to read the generic symbols in order to report an error message. */
306 keep_syms = obj_coff_keep_syms (abfd);
307 obj_coff_keep_syms (abfd) = TRUE;
309 if (info->keep_memory)
310 default_copy = FALSE;
311 else
312 default_copy = TRUE;
314 symcount = obj_raw_syment_count (abfd);
316 /* We keep a list of the linker hash table entries that correspond
317 to particular symbols. */
318 amt = symcount * sizeof (struct coff_link_hash_entry *);
319 sym_hash = bfd_zalloc (abfd, amt);
320 if (sym_hash == NULL && symcount != 0)
321 goto error_return;
322 obj_coff_sym_hashes (abfd) = sym_hash;
324 symesz = bfd_coff_symesz (abfd);
325 BFD_ASSERT (symesz == bfd_coff_auxesz (abfd));
326 esym = (bfd_byte *) obj_coff_external_syms (abfd);
327 esym_end = esym + symcount * symesz;
328 while (esym < esym_end)
330 struct internal_syment sym;
331 enum coff_symbol_classification classification;
332 bfd_boolean copy;
334 bfd_coff_swap_sym_in (abfd, esym, &sym);
336 classification = bfd_coff_classify_symbol (abfd, &sym);
337 if (classification != COFF_SYMBOL_LOCAL)
339 const char *name;
340 char buf[SYMNMLEN + 1];
341 flagword flags;
342 asection *section;
343 bfd_vma value;
344 bfd_boolean addit;
346 /* This symbol is externally visible. */
348 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
349 if (name == NULL)
350 goto error_return;
352 /* We must copy the name into memory if we got it from the
353 syment itself, rather than the string table. */
354 copy = default_copy;
355 if (sym._n._n_n._n_zeroes != 0
356 || sym._n._n_n._n_offset == 0)
357 copy = TRUE;
359 value = sym.n_value;
361 switch (classification)
363 default:
364 abort ();
366 case COFF_SYMBOL_GLOBAL:
367 flags = BSF_EXPORT | BSF_GLOBAL;
368 section = coff_section_from_bfd_index (abfd, sym.n_scnum);
369 if (! obj_pe (abfd))
370 value -= section->vma;
371 break;
373 case COFF_SYMBOL_UNDEFINED:
374 flags = 0;
375 section = bfd_und_section_ptr;
376 break;
378 case COFF_SYMBOL_COMMON:
379 flags = BSF_GLOBAL;
380 section = bfd_com_section_ptr;
381 break;
383 case COFF_SYMBOL_PE_SECTION:
384 flags = BSF_SECTION_SYM | BSF_GLOBAL;
385 section = coff_section_from_bfd_index (abfd, sym.n_scnum);
386 break;
389 if (IS_WEAK_EXTERNAL (abfd, sym))
390 flags = BSF_WEAK;
392 addit = TRUE;
394 /* In the PE format, section symbols actually refer to the
395 start of the output section. We handle them specially
396 here. */
397 if (obj_pe (abfd) && (flags & BSF_SECTION_SYM) != 0)
399 *sym_hash = coff_link_hash_lookup (coff_hash_table (info),
400 name, FALSE, copy, FALSE);
401 if (*sym_hash != NULL)
403 if (((*sym_hash)->coff_link_hash_flags
404 & COFF_LINK_HASH_PE_SECTION_SYMBOL) == 0
405 && (*sym_hash)->root.type != bfd_link_hash_undefined
406 && (*sym_hash)->root.type != bfd_link_hash_undefweak)
407 (*_bfd_error_handler)
408 ("Warning: symbol `%s' is both section and non-section",
409 name);
411 addit = FALSE;
415 /* The Microsoft Visual C compiler does string pooling by
416 hashing the constants to an internal symbol name, and
417 relying on the linker comdat support to discard
418 duplicate names. However, if one string is a literal and
419 one is a data initializer, one will end up in the .data
420 section and one will end up in the .rdata section. The
421 Microsoft linker will combine them into the .data
422 section, which seems to be wrong since it might cause the
423 literal to change.
425 As long as there are no external references to the
426 symbols, which there shouldn't be, we can treat the .data
427 and .rdata instances as separate symbols. The comdat
428 code in the linker will do the appropriate merging. Here
429 we avoid getting a multiple definition error for one of
430 these special symbols.
432 FIXME: I don't think this will work in the case where
433 there are two object files which use the constants as a
434 literal and two object files which use it as a data
435 initializer. One or the other of the second object files
436 is going to wind up with an inappropriate reference. */
437 if (obj_pe (abfd)
438 && (classification == COFF_SYMBOL_GLOBAL
439 || classification == COFF_SYMBOL_PE_SECTION)
440 && coff_section_data (abfd, section) != NULL
441 && coff_section_data (abfd, section)->comdat != NULL
442 && strncmp (name, "??_", 3) == 0
443 && strcmp (name, coff_section_data (abfd, section)->comdat->name) == 0)
445 if (*sym_hash == NULL)
446 *sym_hash = coff_link_hash_lookup (coff_hash_table (info),
447 name, FALSE, copy, FALSE);
448 if (*sym_hash != NULL
449 && (*sym_hash)->root.type == bfd_link_hash_defined
450 && coff_section_data (abfd, (*sym_hash)->root.u.def.section)->comdat != NULL
451 && strcmp (coff_section_data (abfd, (*sym_hash)->root.u.def.section)->comdat->name,
452 coff_section_data (abfd, section)->comdat->name) == 0)
453 addit = FALSE;
456 if (addit)
458 if (! (bfd_coff_link_add_one_symbol
459 (info, abfd, name, flags, section, value,
460 (const char *) NULL, copy, FALSE,
461 (struct bfd_link_hash_entry **) sym_hash)))
462 goto error_return;
465 if (obj_pe (abfd) && (flags & BSF_SECTION_SYM) != 0)
466 (*sym_hash)->coff_link_hash_flags |=
467 COFF_LINK_HASH_PE_SECTION_SYMBOL;
469 /* Limit the alignment of a common symbol to the possible
470 alignment of a section. There is no point to permitting
471 a higher alignment for a common symbol: we can not
472 guarantee it, and it may cause us to allocate extra space
473 in the common section. */
474 if (section == bfd_com_section_ptr
475 && (*sym_hash)->root.type == bfd_link_hash_common
476 && ((*sym_hash)->root.u.c.p->alignment_power
477 > bfd_coff_default_section_alignment_power (abfd)))
478 (*sym_hash)->root.u.c.p->alignment_power
479 = bfd_coff_default_section_alignment_power (abfd);
481 if (info->hash->creator->flavour == bfd_get_flavour (abfd))
483 /* If we don't have any symbol information currently in
484 the hash table, or if we are looking at a symbol
485 definition, then update the symbol class and type in
486 the hash table. */
487 if (((*sym_hash)->class == C_NULL
488 && (*sym_hash)->type == T_NULL)
489 || sym.n_scnum != 0
490 || (sym.n_value != 0
491 && (*sym_hash)->root.type != bfd_link_hash_defined
492 && (*sym_hash)->root.type != bfd_link_hash_defweak))
494 (*sym_hash)->class = sym.n_sclass;
495 if (sym.n_type != T_NULL)
497 /* We want to warn if the type changed, but not
498 if it changed from an unspecified type.
499 Testing the whole type byte may work, but the
500 change from (e.g.) a function of unspecified
501 type to function of known type also wants to
502 skip the warning. */
503 if ((*sym_hash)->type != T_NULL
504 && (*sym_hash)->type != sym.n_type
505 && !(DTYPE ((*sym_hash)->type) == DTYPE (sym.n_type)
506 && (BTYPE ((*sym_hash)->type) == T_NULL
507 || BTYPE (sym.n_type) == T_NULL)))
508 (*_bfd_error_handler)
509 (_("Warning: type of symbol `%s' changed from %d to %d in %B"),
510 abfd, name, (*sym_hash)->type, sym.n_type);
512 /* We don't want to change from a meaningful
513 base type to a null one, but if we know
514 nothing, take what little we might now know. */
515 if (BTYPE (sym.n_type) != T_NULL
516 || (*sym_hash)->type == T_NULL)
517 (*sym_hash)->type = sym.n_type;
519 (*sym_hash)->auxbfd = abfd;
520 if (sym.n_numaux != 0)
522 union internal_auxent *alloc;
523 unsigned int i;
524 bfd_byte *eaux;
525 union internal_auxent *iaux;
527 (*sym_hash)->numaux = sym.n_numaux;
528 alloc = ((union internal_auxent *)
529 bfd_hash_allocate (&info->hash->table,
530 (sym.n_numaux
531 * sizeof (*alloc))));
532 if (alloc == NULL)
533 goto error_return;
534 for (i = 0, eaux = esym + symesz, iaux = alloc;
535 i < sym.n_numaux;
536 i++, eaux += symesz, iaux++)
537 bfd_coff_swap_aux_in (abfd, eaux, sym.n_type,
538 sym.n_sclass, (int) i,
539 sym.n_numaux, iaux);
540 (*sym_hash)->aux = alloc;
545 if (classification == COFF_SYMBOL_PE_SECTION
546 && (*sym_hash)->numaux != 0)
548 /* Some PE sections (such as .bss) have a zero size in
549 the section header, but a non-zero size in the AUX
550 record. Correct that here.
552 FIXME: This is not at all the right place to do this.
553 For example, it won't help objdump. This needs to be
554 done when we swap in the section header. */
555 BFD_ASSERT ((*sym_hash)->numaux == 1);
556 if (section->size == 0)
557 section->size = (*sym_hash)->aux[0].x_scn.x_scnlen;
559 /* FIXME: We could test whether the section sizes
560 matches the size in the aux entry, but apparently
561 that sometimes fails unexpectedly. */
565 esym += (sym.n_numaux + 1) * symesz;
566 sym_hash += sym.n_numaux + 1;
569 /* If this is a non-traditional, non-relocatable link, try to
570 optimize the handling of any .stab/.stabstr sections. */
571 if (! info->relocatable
572 && ! info->traditional_format
573 && info->hash->creator->flavour == bfd_get_flavour (abfd)
574 && (info->strip != strip_all && info->strip != strip_debugger))
576 asection *stabstr;
578 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
580 if (stabstr != NULL)
582 bfd_size_type string_offset = 0;
583 asection *stab;
585 for (stab = abfd->sections; stab; stab = stab->next)
586 if (strncmp (".stab", stab->name, 5) == 0
587 && (!stab->name[5]
588 || (stab->name[5] == '.' && ISDIGIT (stab->name[6]))))
590 struct coff_link_hash_table *table;
591 struct coff_section_tdata *secdata
592 = coff_section_data (abfd, stab);
594 if (secdata == NULL)
596 amt = sizeof (struct coff_section_tdata);
597 stab->used_by_bfd = bfd_zalloc (abfd, amt);
598 if (stab->used_by_bfd == NULL)
599 goto error_return;
600 secdata = coff_section_data (abfd, stab);
603 table = coff_hash_table (info);
605 if (! _bfd_link_section_stabs (abfd, &table->stab_info,
606 stab, stabstr,
607 &secdata->stab_info,
608 &string_offset))
609 goto error_return;
614 obj_coff_keep_syms (abfd) = keep_syms;
616 return TRUE;
618 error_return:
619 obj_coff_keep_syms (abfd) = keep_syms;
620 return FALSE;
623 /* Do the final link step. */
625 bfd_boolean
626 _bfd_coff_final_link (bfd *abfd,
627 struct bfd_link_info *info)
629 bfd_size_type symesz;
630 struct coff_final_link_info finfo;
631 bfd_boolean debug_merge_allocated;
632 bfd_boolean long_section_names;
633 asection *o;
634 struct bfd_link_order *p;
635 bfd_size_type max_sym_count;
636 bfd_size_type max_lineno_count;
637 bfd_size_type max_reloc_count;
638 bfd_size_type max_output_reloc_count;
639 bfd_size_type max_contents_size;
640 file_ptr rel_filepos;
641 unsigned int relsz;
642 file_ptr line_filepos;
643 unsigned int linesz;
644 bfd *sub;
645 bfd_byte *external_relocs = NULL;
646 char strbuf[STRING_SIZE_SIZE];
647 bfd_size_type amt;
649 symesz = bfd_coff_symesz (abfd);
651 finfo.info = info;
652 finfo.output_bfd = abfd;
653 finfo.strtab = NULL;
654 finfo.section_info = NULL;
655 finfo.last_file_index = -1;
656 finfo.last_bf_index = -1;
657 finfo.internal_syms = NULL;
658 finfo.sec_ptrs = NULL;
659 finfo.sym_indices = NULL;
660 finfo.outsyms = NULL;
661 finfo.linenos = NULL;
662 finfo.contents = NULL;
663 finfo.external_relocs = NULL;
664 finfo.internal_relocs = NULL;
665 finfo.global_to_static = FALSE;
666 debug_merge_allocated = FALSE;
668 coff_data (abfd)->link_info = info;
670 finfo.strtab = _bfd_stringtab_init ();
671 if (finfo.strtab == NULL)
672 goto error_return;
674 if (! coff_debug_merge_hash_table_init (&finfo.debug_merge))
675 goto error_return;
676 debug_merge_allocated = TRUE;
678 /* Compute the file positions for all the sections. */
679 if (! abfd->output_has_begun)
681 if (! bfd_coff_compute_section_file_positions (abfd))
682 goto error_return;
685 /* Count the line numbers and relocation entries required for the
686 output file. Set the file positions for the relocs. */
687 rel_filepos = obj_relocbase (abfd);
688 relsz = bfd_coff_relsz (abfd);
689 max_contents_size = 0;
690 max_lineno_count = 0;
691 max_reloc_count = 0;
693 long_section_names = FALSE;
694 for (o = abfd->sections; o != NULL; o = o->next)
696 o->reloc_count = 0;
697 o->lineno_count = 0;
698 for (p = o->map_head.link_order; p != NULL; p = p->next)
700 if (p->type == bfd_indirect_link_order)
702 asection *sec;
704 sec = p->u.indirect.section;
706 /* Mark all sections which are to be included in the
707 link. This will normally be every section. We need
708 to do this so that we can identify any sections which
709 the linker has decided to not include. */
710 sec->linker_mark = TRUE;
712 if (info->strip == strip_none
713 || info->strip == strip_some)
714 o->lineno_count += sec->lineno_count;
716 if (info->relocatable)
717 o->reloc_count += sec->reloc_count;
719 if (sec->rawsize > max_contents_size)
720 max_contents_size = sec->rawsize;
721 if (sec->size > max_contents_size)
722 max_contents_size = sec->size;
723 if (sec->lineno_count > max_lineno_count)
724 max_lineno_count = sec->lineno_count;
725 if (sec->reloc_count > max_reloc_count)
726 max_reloc_count = sec->reloc_count;
728 else if (info->relocatable
729 && (p->type == bfd_section_reloc_link_order
730 || p->type == bfd_symbol_reloc_link_order))
731 ++o->reloc_count;
733 if (o->reloc_count == 0)
734 o->rel_filepos = 0;
735 else
737 o->flags |= SEC_RELOC;
738 o->rel_filepos = rel_filepos;
739 rel_filepos += o->reloc_count * relsz;
740 /* In PE COFF, if there are at least 0xffff relocations an
741 extra relocation will be written out to encode the count. */
742 if (obj_pe (abfd) && o->reloc_count >= 0xffff)
743 rel_filepos += relsz;
746 if (bfd_coff_long_section_names (abfd)
747 && strlen (o->name) > SCNNMLEN)
749 /* This section has a long name which must go in the string
750 table. This must correspond to the code in
751 coff_write_object_contents which puts the string index
752 into the s_name field of the section header. That is why
753 we pass hash as FALSE. */
754 if (_bfd_stringtab_add (finfo.strtab, o->name, FALSE, FALSE)
755 == (bfd_size_type) -1)
756 goto error_return;
757 long_section_names = TRUE;
761 /* If doing a relocatable link, allocate space for the pointers we
762 need to keep. */
763 if (info->relocatable)
765 unsigned int i;
767 /* We use section_count + 1, rather than section_count, because
768 the target_index fields are 1 based. */
769 amt = abfd->section_count + 1;
770 amt *= sizeof (struct coff_link_section_info);
771 finfo.section_info = bfd_malloc (amt);
772 if (finfo.section_info == NULL)
773 goto error_return;
774 for (i = 0; i <= abfd->section_count; i++)
776 finfo.section_info[i].relocs = NULL;
777 finfo.section_info[i].rel_hashes = NULL;
781 /* We now know the size of the relocs, so we can determine the file
782 positions of the line numbers. */
783 line_filepos = rel_filepos;
784 linesz = bfd_coff_linesz (abfd);
785 max_output_reloc_count = 0;
786 for (o = abfd->sections; o != NULL; o = o->next)
788 if (o->lineno_count == 0)
789 o->line_filepos = 0;
790 else
792 o->line_filepos = line_filepos;
793 line_filepos += o->lineno_count * linesz;
796 if (o->reloc_count != 0)
798 /* We don't know the indices of global symbols until we have
799 written out all the local symbols. For each section in
800 the output file, we keep an array of pointers to hash
801 table entries. Each entry in the array corresponds to a
802 reloc. When we find a reloc against a global symbol, we
803 set the corresponding entry in this array so that we can
804 fix up the symbol index after we have written out all the
805 local symbols.
807 Because of this problem, we also keep the relocs in
808 memory until the end of the link. This wastes memory,
809 but only when doing a relocatable link, which is not the
810 common case. */
811 BFD_ASSERT (info->relocatable);
812 amt = o->reloc_count;
813 amt *= sizeof (struct internal_reloc);
814 finfo.section_info[o->target_index].relocs = bfd_malloc (amt);
815 amt = o->reloc_count;
816 amt *= sizeof (struct coff_link_hash_entry *);
817 finfo.section_info[o->target_index].rel_hashes = bfd_malloc (amt);
818 if (finfo.section_info[o->target_index].relocs == NULL
819 || finfo.section_info[o->target_index].rel_hashes == NULL)
820 goto error_return;
822 if (o->reloc_count > max_output_reloc_count)
823 max_output_reloc_count = o->reloc_count;
826 /* Reset the reloc and lineno counts, so that we can use them to
827 count the number of entries we have output so far. */
828 o->reloc_count = 0;
829 o->lineno_count = 0;
832 obj_sym_filepos (abfd) = line_filepos;
834 /* Figure out the largest number of symbols in an input BFD. Take
835 the opportunity to clear the output_has_begun fields of all the
836 input BFD's. */
837 max_sym_count = 0;
838 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
840 size_t sz;
842 sub->output_has_begun = FALSE;
843 sz = obj_raw_syment_count (sub);
844 if (sz > max_sym_count)
845 max_sym_count = sz;
848 /* Allocate some buffers used while linking. */
849 amt = max_sym_count * sizeof (struct internal_syment);
850 finfo.internal_syms = bfd_malloc (amt);
851 amt = max_sym_count * sizeof (asection *);
852 finfo.sec_ptrs = bfd_malloc (amt);
853 amt = max_sym_count * sizeof (long);
854 finfo.sym_indices = bfd_malloc (amt);
855 finfo.outsyms = bfd_malloc ((max_sym_count + 1) * symesz);
856 amt = max_lineno_count * bfd_coff_linesz (abfd);
857 finfo.linenos = bfd_malloc (amt);
858 finfo.contents = bfd_malloc (max_contents_size);
859 amt = max_reloc_count * relsz;
860 finfo.external_relocs = bfd_malloc (amt);
861 if (! info->relocatable)
863 amt = max_reloc_count * sizeof (struct internal_reloc);
864 finfo.internal_relocs = bfd_malloc (amt);
866 if ((finfo.internal_syms == NULL && max_sym_count > 0)
867 || (finfo.sec_ptrs == NULL && max_sym_count > 0)
868 || (finfo.sym_indices == NULL && max_sym_count > 0)
869 || finfo.outsyms == NULL
870 || (finfo.linenos == NULL && max_lineno_count > 0)
871 || (finfo.contents == NULL && max_contents_size > 0)
872 || (finfo.external_relocs == NULL && max_reloc_count > 0)
873 || (! info->relocatable
874 && finfo.internal_relocs == NULL
875 && max_reloc_count > 0))
876 goto error_return;
878 /* We now know the position of everything in the file, except that
879 we don't know the size of the symbol table and therefore we don't
880 know where the string table starts. We just build the string
881 table in memory as we go along. We process all the relocations
882 for a single input file at once. */
883 obj_raw_syment_count (abfd) = 0;
885 if (coff_backend_info (abfd)->_bfd_coff_start_final_link)
887 if (! bfd_coff_start_final_link (abfd, info))
888 goto error_return;
891 for (o = abfd->sections; o != NULL; o = o->next)
893 for (p = o->map_head.link_order; p != NULL; p = p->next)
895 if (p->type == bfd_indirect_link_order
896 && bfd_family_coff (p->u.indirect.section->owner))
898 sub = p->u.indirect.section->owner;
899 if (! bfd_coff_link_output_has_begun (sub, & finfo))
901 if (! _bfd_coff_link_input_bfd (&finfo, sub))
902 goto error_return;
903 sub->output_has_begun = TRUE;
906 else if (p->type == bfd_section_reloc_link_order
907 || p->type == bfd_symbol_reloc_link_order)
909 if (! _bfd_coff_reloc_link_order (abfd, &finfo, o, p))
910 goto error_return;
912 else
914 if (! _bfd_default_link_order (abfd, info, o, p))
915 goto error_return;
920 if (! bfd_coff_final_link_postscript (abfd, & finfo))
921 goto error_return;
923 /* Free up the buffers used by _bfd_coff_link_input_bfd. */
925 coff_debug_merge_hash_table_free (&finfo.debug_merge);
926 debug_merge_allocated = FALSE;
928 if (finfo.internal_syms != NULL)
930 free (finfo.internal_syms);
931 finfo.internal_syms = NULL;
933 if (finfo.sec_ptrs != NULL)
935 free (finfo.sec_ptrs);
936 finfo.sec_ptrs = NULL;
938 if (finfo.sym_indices != NULL)
940 free (finfo.sym_indices);
941 finfo.sym_indices = NULL;
943 if (finfo.linenos != NULL)
945 free (finfo.linenos);
946 finfo.linenos = NULL;
948 if (finfo.contents != NULL)
950 free (finfo.contents);
951 finfo.contents = NULL;
953 if (finfo.external_relocs != NULL)
955 free (finfo.external_relocs);
956 finfo.external_relocs = NULL;
958 if (finfo.internal_relocs != NULL)
960 free (finfo.internal_relocs);
961 finfo.internal_relocs = NULL;
964 /* The value of the last C_FILE symbol is supposed to be the symbol
965 index of the first external symbol. Write it out again if
966 necessary. */
967 if (finfo.last_file_index != -1
968 && (unsigned int) finfo.last_file.n_value != obj_raw_syment_count (abfd))
970 file_ptr pos;
972 finfo.last_file.n_value = obj_raw_syment_count (abfd);
973 bfd_coff_swap_sym_out (abfd, &finfo.last_file,
974 finfo.outsyms);
976 pos = obj_sym_filepos (abfd) + finfo.last_file_index * symesz;
977 if (bfd_seek (abfd, pos, SEEK_SET) != 0
978 || bfd_bwrite (finfo.outsyms, symesz, abfd) != symesz)
979 return FALSE;
982 /* If doing task linking (ld --task-link) then make a pass through the
983 global symbols, writing out any that are defined, and making them
984 static. */
985 if (info->task_link)
987 finfo.failed = FALSE;
988 coff_link_hash_traverse (coff_hash_table (info),
989 _bfd_coff_write_task_globals, &finfo);
990 if (finfo.failed)
991 goto error_return;
994 /* Write out the global symbols. */
995 finfo.failed = FALSE;
996 coff_link_hash_traverse (coff_hash_table (info),
997 _bfd_coff_write_global_sym, &finfo);
998 if (finfo.failed)
999 goto error_return;
1001 /* The outsyms buffer is used by _bfd_coff_write_global_sym. */
1002 if (finfo.outsyms != NULL)
1004 free (finfo.outsyms);
1005 finfo.outsyms = NULL;
1008 if (info->relocatable && max_output_reloc_count > 0)
1010 /* Now that we have written out all the global symbols, we know
1011 the symbol indices to use for relocs against them, and we can
1012 finally write out the relocs. */
1013 amt = max_output_reloc_count * relsz;
1014 external_relocs = bfd_malloc (amt);
1015 if (external_relocs == NULL)
1016 goto error_return;
1018 for (o = abfd->sections; o != NULL; o = o->next)
1020 struct internal_reloc *irel;
1021 struct internal_reloc *irelend;
1022 struct coff_link_hash_entry **rel_hash;
1023 bfd_byte *erel;
1025 if (o->reloc_count == 0)
1026 continue;
1028 irel = finfo.section_info[o->target_index].relocs;
1029 irelend = irel + o->reloc_count;
1030 rel_hash = finfo.section_info[o->target_index].rel_hashes;
1031 erel = external_relocs;
1032 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
1034 if (*rel_hash != NULL)
1036 BFD_ASSERT ((*rel_hash)->indx >= 0);
1037 irel->r_symndx = (*rel_hash)->indx;
1039 bfd_coff_swap_reloc_out (abfd, irel, erel);
1042 if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0)
1043 goto error_return;
1044 if (obj_pe (abfd) && o->reloc_count >= 0xffff)
1046 /* In PE COFF, write the count of relocs as the first
1047 reloc. The header overflow bit will be set
1048 elsewhere. */
1049 struct internal_reloc incount;
1050 bfd_byte *excount = (bfd_byte *)bfd_malloc (relsz);
1052 memset (&incount, 0, sizeof (incount));
1053 incount.r_vaddr = o->reloc_count + 1;
1054 bfd_coff_swap_reloc_out (abfd, (PTR) &incount, (PTR) excount);
1055 if (bfd_bwrite (excount, relsz, abfd) != relsz)
1056 /* We'll leak, but it's an error anyway. */
1057 goto error_return;
1058 free (excount);
1060 if (bfd_bwrite (external_relocs,
1061 (bfd_size_type) relsz * o->reloc_count, abfd)
1062 != (bfd_size_type) relsz * o->reloc_count)
1063 goto error_return;
1066 free (external_relocs);
1067 external_relocs = NULL;
1070 /* Free up the section information. */
1071 if (finfo.section_info != NULL)
1073 unsigned int i;
1075 for (i = 0; i < abfd->section_count; i++)
1077 if (finfo.section_info[i].relocs != NULL)
1078 free (finfo.section_info[i].relocs);
1079 if (finfo.section_info[i].rel_hashes != NULL)
1080 free (finfo.section_info[i].rel_hashes);
1082 free (finfo.section_info);
1083 finfo.section_info = NULL;
1086 /* If we have optimized stabs strings, output them. */
1087 if (coff_hash_table (info)->stab_info.stabstr != NULL)
1089 if (! _bfd_write_stab_strings (abfd, &coff_hash_table (info)->stab_info))
1090 return FALSE;
1093 /* Write out the string table. */
1094 if (obj_raw_syment_count (abfd) != 0 || long_section_names)
1096 file_ptr pos;
1098 pos = obj_sym_filepos (abfd) + obj_raw_syment_count (abfd) * symesz;
1099 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
1100 return FALSE;
1102 #if STRING_SIZE_SIZE == 4
1103 H_PUT_32 (abfd,
1104 _bfd_stringtab_size (finfo.strtab) + STRING_SIZE_SIZE,
1105 strbuf);
1106 #else
1107 #error Change H_PUT_32 above
1108 #endif
1110 if (bfd_bwrite (strbuf, (bfd_size_type) STRING_SIZE_SIZE, abfd)
1111 != STRING_SIZE_SIZE)
1112 return FALSE;
1114 if (! _bfd_stringtab_emit (abfd, finfo.strtab))
1115 return FALSE;
1117 obj_coff_strings_written (abfd) = TRUE;
1120 _bfd_stringtab_free (finfo.strtab);
1122 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
1123 not try to write out the symbols. */
1124 bfd_get_symcount (abfd) = 0;
1126 return TRUE;
1128 error_return:
1129 if (debug_merge_allocated)
1130 coff_debug_merge_hash_table_free (&finfo.debug_merge);
1131 if (finfo.strtab != NULL)
1132 _bfd_stringtab_free (finfo.strtab);
1133 if (finfo.section_info != NULL)
1135 unsigned int i;
1137 for (i = 0; i < abfd->section_count; i++)
1139 if (finfo.section_info[i].relocs != NULL)
1140 free (finfo.section_info[i].relocs);
1141 if (finfo.section_info[i].rel_hashes != NULL)
1142 free (finfo.section_info[i].rel_hashes);
1144 free (finfo.section_info);
1146 if (finfo.internal_syms != NULL)
1147 free (finfo.internal_syms);
1148 if (finfo.sec_ptrs != NULL)
1149 free (finfo.sec_ptrs);
1150 if (finfo.sym_indices != NULL)
1151 free (finfo.sym_indices);
1152 if (finfo.outsyms != NULL)
1153 free (finfo.outsyms);
1154 if (finfo.linenos != NULL)
1155 free (finfo.linenos);
1156 if (finfo.contents != NULL)
1157 free (finfo.contents);
1158 if (finfo.external_relocs != NULL)
1159 free (finfo.external_relocs);
1160 if (finfo.internal_relocs != NULL)
1161 free (finfo.internal_relocs);
1162 if (external_relocs != NULL)
1163 free (external_relocs);
1164 return FALSE;
1167 /* Parse out a -heap <reserved>,<commit> line. */
1169 static char *
1170 dores_com (char *ptr, bfd *output_bfd, int heap)
1172 if (coff_data(output_bfd)->pe)
1174 int val = strtoul (ptr, &ptr, 0);
1176 if (heap)
1177 pe_data(output_bfd)->pe_opthdr.SizeOfHeapReserve = val;
1178 else
1179 pe_data(output_bfd)->pe_opthdr.SizeOfStackReserve = val;
1181 if (ptr[0] == ',')
1183 val = strtoul (ptr+1, &ptr, 0);
1184 if (heap)
1185 pe_data(output_bfd)->pe_opthdr.SizeOfHeapCommit = val;
1186 else
1187 pe_data(output_bfd)->pe_opthdr.SizeOfStackCommit = val;
1190 return ptr;
1193 static char *
1194 get_name (char *ptr, char **dst)
1196 while (*ptr == ' ')
1197 ptr++;
1198 *dst = ptr;
1199 while (*ptr && *ptr != ' ')
1200 ptr++;
1201 *ptr = 0;
1202 return ptr+1;
1205 /* Process any magic embedded commands in a section called .drectve. */
1207 static int
1208 process_embedded_commands (bfd *output_bfd,
1209 struct bfd_link_info *info ATTRIBUTE_UNUSED,
1210 bfd *abfd)
1212 asection *sec = bfd_get_section_by_name (abfd, ".drectve");
1213 char *s;
1214 char *e;
1215 bfd_byte *copy;
1217 if (!sec)
1218 return 1;
1220 if (!bfd_malloc_and_get_section (abfd, sec, &copy))
1222 if (copy != NULL)
1223 free (copy);
1224 return 0;
1226 e = (char *) copy + sec->size;
1228 for (s = (char *) copy; s < e ; )
1230 if (s[0] != '-')
1232 s++;
1233 continue;
1235 if (strncmp (s, "-attr", 5) == 0)
1237 char *name;
1238 char *attribs;
1239 asection *asec;
1240 int loop = 1;
1241 int had_write = 0;
1242 int had_read = 0;
1243 int had_exec= 0;
1244 int had_shared= 0;
1246 s += 5;
1247 s = get_name (s, &name);
1248 s = get_name (s, &attribs);
1250 while (loop)
1252 switch (*attribs++)
1254 case 'W':
1255 had_write = 1;
1256 break;
1257 case 'R':
1258 had_read = 1;
1259 break;
1260 case 'S':
1261 had_shared = 1;
1262 break;
1263 case 'X':
1264 had_exec = 1;
1265 break;
1266 default:
1267 loop = 0;
1270 asec = bfd_get_section_by_name (abfd, name);
1271 if (asec)
1273 if (had_exec)
1274 asec->flags |= SEC_CODE;
1275 if (!had_write)
1276 asec->flags |= SEC_READONLY;
1279 else if (strncmp (s,"-heap", 5) == 0)
1280 s = dores_com (s+5, output_bfd, 1);
1282 else if (strncmp (s,"-stack", 6) == 0)
1283 s = dores_com (s+6, output_bfd, 0);
1285 else
1286 s++;
1288 free (copy);
1289 return 1;
1292 /* Place a marker against all symbols which are used by relocations.
1293 This marker can be picked up by the 'do we skip this symbol ?'
1294 loop in _bfd_coff_link_input_bfd() and used to prevent skipping
1295 that symbol. */
1297 static void
1298 mark_relocs (struct coff_final_link_info *finfo, bfd *input_bfd)
1300 asection * a;
1302 if ((bfd_get_file_flags (input_bfd) & HAS_SYMS) == 0)
1303 return;
1305 for (a = input_bfd->sections; a != (asection *) NULL; a = a->next)
1307 struct internal_reloc * internal_relocs;
1308 struct internal_reloc * irel;
1309 struct internal_reloc * irelend;
1311 if ((a->flags & SEC_RELOC) == 0 || a->reloc_count < 1)
1312 continue;
1313 /* Don't mark relocs in excluded sections. */
1314 if (a->output_section == bfd_abs_section_ptr)
1315 continue;
1317 /* Read in the relocs. */
1318 internal_relocs = _bfd_coff_read_internal_relocs
1319 (input_bfd, a, FALSE,
1320 finfo->external_relocs,
1321 finfo->info->relocatable,
1322 (finfo->info->relocatable
1323 ? (finfo->section_info[ a->output_section->target_index ].relocs + a->output_section->reloc_count)
1324 : finfo->internal_relocs)
1327 if (internal_relocs == NULL)
1328 continue;
1330 irel = internal_relocs;
1331 irelend = irel + a->reloc_count;
1333 /* Place a mark in the sym_indices array (whose entries have
1334 been initialised to 0) for all of the symbols that are used
1335 in the relocation table. This will then be picked up in the
1336 skip/don't-skip pass. */
1337 for (; irel < irelend; irel++)
1338 finfo->sym_indices[ irel->r_symndx ] = -1;
1342 /* Link an input file into the linker output file. This function
1343 handles all the sections and relocations of the input file at once. */
1345 bfd_boolean
1346 _bfd_coff_link_input_bfd (struct coff_final_link_info *finfo, bfd *input_bfd)
1348 unsigned int n_tmask = coff_data (input_bfd)->local_n_tmask;
1349 unsigned int n_btshft = coff_data (input_bfd)->local_n_btshft;
1350 bfd_boolean (*adjust_symndx)
1351 (bfd *, struct bfd_link_info *, bfd *, asection *,
1352 struct internal_reloc *, bfd_boolean *);
1353 bfd *output_bfd;
1354 const char *strings;
1355 bfd_size_type syment_base;
1356 bfd_boolean copy, hash;
1357 bfd_size_type isymesz;
1358 bfd_size_type osymesz;
1359 bfd_size_type linesz;
1360 bfd_byte *esym;
1361 bfd_byte *esym_end;
1362 struct internal_syment *isymp;
1363 asection **secpp;
1364 long *indexp;
1365 unsigned long output_index;
1366 bfd_byte *outsym;
1367 struct coff_link_hash_entry **sym_hash;
1368 asection *o;
1370 /* Move all the symbols to the output file. */
1372 output_bfd = finfo->output_bfd;
1373 strings = NULL;
1374 syment_base = obj_raw_syment_count (output_bfd);
1375 isymesz = bfd_coff_symesz (input_bfd);
1376 osymesz = bfd_coff_symesz (output_bfd);
1377 linesz = bfd_coff_linesz (input_bfd);
1378 BFD_ASSERT (linesz == bfd_coff_linesz (output_bfd));
1380 copy = FALSE;
1381 if (! finfo->info->keep_memory)
1382 copy = TRUE;
1383 hash = TRUE;
1384 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
1385 hash = FALSE;
1387 if (! _bfd_coff_get_external_symbols (input_bfd))
1388 return FALSE;
1390 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
1391 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
1392 isymp = finfo->internal_syms;
1393 secpp = finfo->sec_ptrs;
1394 indexp = finfo->sym_indices;
1395 output_index = syment_base;
1396 outsym = finfo->outsyms;
1398 if (coff_data (output_bfd)->pe
1399 && ! process_embedded_commands (output_bfd, finfo->info, input_bfd))
1400 return FALSE;
1402 /* If we are going to perform relocations and also strip/discard some
1403 symbols then we must make sure that we do not strip/discard those
1404 symbols that are going to be involved in the relocations. */
1405 if (( finfo->info->strip != strip_none
1406 || finfo->info->discard != discard_none)
1407 && finfo->info->relocatable)
1409 /* Mark the symbol array as 'not-used'. */
1410 memset (indexp, 0, obj_raw_syment_count (input_bfd) * sizeof * indexp);
1412 mark_relocs (finfo, input_bfd);
1415 while (esym < esym_end)
1417 struct internal_syment isym;
1418 enum coff_symbol_classification classification;
1419 bfd_boolean skip;
1420 bfd_boolean global;
1421 bfd_boolean dont_skip_symbol;
1422 int add;
1424 bfd_coff_swap_sym_in (input_bfd, esym, isymp);
1426 /* Make a copy of *isymp so that the relocate_section function
1427 always sees the original values. This is more reliable than
1428 always recomputing the symbol value even if we are stripping
1429 the symbol. */
1430 isym = *isymp;
1432 classification = bfd_coff_classify_symbol (input_bfd, &isym);
1433 switch (classification)
1435 default:
1436 abort ();
1437 case COFF_SYMBOL_GLOBAL:
1438 case COFF_SYMBOL_PE_SECTION:
1439 case COFF_SYMBOL_LOCAL:
1440 *secpp = coff_section_from_bfd_index (input_bfd, isym.n_scnum);
1441 break;
1442 case COFF_SYMBOL_COMMON:
1443 *secpp = bfd_com_section_ptr;
1444 break;
1445 case COFF_SYMBOL_UNDEFINED:
1446 *secpp = bfd_und_section_ptr;
1447 break;
1450 /* Extract the flag indicating if this symbol is used by a
1451 relocation. */
1452 if ((finfo->info->strip != strip_none
1453 || finfo->info->discard != discard_none)
1454 && finfo->info->relocatable)
1455 dont_skip_symbol = *indexp;
1456 else
1457 dont_skip_symbol = FALSE;
1459 *indexp = -1;
1461 skip = FALSE;
1462 global = FALSE;
1463 add = 1 + isym.n_numaux;
1465 /* If we are stripping all symbols, we want to skip this one. */
1466 if (finfo->info->strip == strip_all && ! dont_skip_symbol)
1467 skip = TRUE;
1469 if (! skip)
1471 switch (classification)
1473 default:
1474 abort ();
1475 case COFF_SYMBOL_GLOBAL:
1476 case COFF_SYMBOL_COMMON:
1477 case COFF_SYMBOL_PE_SECTION:
1478 /* This is a global symbol. Global symbols come at the
1479 end of the symbol table, so skip them for now.
1480 Locally defined function symbols, however, are an
1481 exception, and are not moved to the end. */
1482 global = TRUE;
1483 if (! ISFCN (isym.n_type))
1484 skip = TRUE;
1485 break;
1487 case COFF_SYMBOL_UNDEFINED:
1488 /* Undefined symbols are left for the end. */
1489 global = TRUE;
1490 skip = TRUE;
1491 break;
1493 case COFF_SYMBOL_LOCAL:
1494 /* This is a local symbol. Skip it if we are discarding
1495 local symbols. */
1496 if (finfo->info->discard == discard_all && ! dont_skip_symbol)
1497 skip = TRUE;
1498 break;
1502 #ifndef COFF_WITH_PE
1503 /* Skip section symbols for sections which are not going to be
1504 emitted. */
1505 if (!skip
1506 && dont_skip_symbol == 0
1507 && isym.n_sclass == C_STAT
1508 && isym.n_type == T_NULL
1509 && isym.n_numaux > 0
1510 && (*secpp)->output_section == bfd_abs_section_ptr)
1511 skip = TRUE;
1512 #endif
1514 /* If we stripping debugging symbols, and this is a debugging
1515 symbol, then skip it. FIXME: gas sets the section to N_ABS
1516 for some types of debugging symbols; I don't know if this is
1517 a bug or not. In any case, we handle it here. */
1518 if (! skip
1519 && finfo->info->strip == strip_debugger
1520 && ! dont_skip_symbol
1521 && (isym.n_scnum == N_DEBUG
1522 || (isym.n_scnum == N_ABS
1523 && (isym.n_sclass == C_AUTO
1524 || isym.n_sclass == C_REG
1525 || isym.n_sclass == C_MOS
1526 || isym.n_sclass == C_MOE
1527 || isym.n_sclass == C_MOU
1528 || isym.n_sclass == C_ARG
1529 || isym.n_sclass == C_REGPARM
1530 || isym.n_sclass == C_FIELD
1531 || isym.n_sclass == C_EOS))))
1532 skip = TRUE;
1534 /* If some symbols are stripped based on the name, work out the
1535 name and decide whether to skip this symbol. */
1536 if (! skip
1537 && (finfo->info->strip == strip_some
1538 || finfo->info->discard == discard_l))
1540 const char *name;
1541 char buf[SYMNMLEN + 1];
1543 name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf);
1544 if (name == NULL)
1545 return FALSE;
1547 if (! dont_skip_symbol
1548 && ((finfo->info->strip == strip_some
1549 && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE,
1550 FALSE) == NULL))
1551 || (! global
1552 && finfo->info->discard == discard_l
1553 && bfd_is_local_label_name (input_bfd, name))))
1554 skip = TRUE;
1557 /* If this is an enum, struct, or union tag, see if we have
1558 already output an identical type. */
1559 if (! skip
1560 && (finfo->output_bfd->flags & BFD_TRADITIONAL_FORMAT) == 0
1561 && (isym.n_sclass == C_ENTAG
1562 || isym.n_sclass == C_STRTAG
1563 || isym.n_sclass == C_UNTAG)
1564 && isym.n_numaux == 1)
1566 const char *name;
1567 char buf[SYMNMLEN + 1];
1568 struct coff_debug_merge_hash_entry *mh;
1569 struct coff_debug_merge_type *mt;
1570 union internal_auxent aux;
1571 struct coff_debug_merge_element **epp;
1572 bfd_byte *esl, *eslend;
1573 struct internal_syment *islp;
1574 bfd_size_type amt;
1576 name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf);
1577 if (name == NULL)
1578 return FALSE;
1580 /* Ignore fake names invented by compiler; treat them all as
1581 the same name. */
1582 if (*name == '~' || *name == '.' || *name == '$'
1583 || (*name == bfd_get_symbol_leading_char (input_bfd)
1584 && (name[1] == '~' || name[1] == '.' || name[1] == '$')))
1585 name = "";
1587 mh = coff_debug_merge_hash_lookup (&finfo->debug_merge, name,
1588 TRUE, TRUE);
1589 if (mh == NULL)
1590 return FALSE;
1592 /* Allocate memory to hold type information. If this turns
1593 out to be a duplicate, we pass this address to
1594 bfd_release. */
1595 amt = sizeof (struct coff_debug_merge_type);
1596 mt = bfd_alloc (input_bfd, amt);
1597 if (mt == NULL)
1598 return FALSE;
1599 mt->class = isym.n_sclass;
1601 /* Pick up the aux entry, which points to the end of the tag
1602 entries. */
1603 bfd_coff_swap_aux_in (input_bfd, (esym + isymesz),
1604 isym.n_type, isym.n_sclass, 0, isym.n_numaux,
1605 &aux);
1607 /* Gather the elements. */
1608 epp = &mt->elements;
1609 mt->elements = NULL;
1610 islp = isymp + 2;
1611 esl = esym + 2 * isymesz;
1612 eslend = ((bfd_byte *) obj_coff_external_syms (input_bfd)
1613 + aux.x_sym.x_fcnary.x_fcn.x_endndx.l * isymesz);
1614 while (esl < eslend)
1616 const char *elename;
1617 char elebuf[SYMNMLEN + 1];
1618 char *name_copy;
1620 bfd_coff_swap_sym_in (input_bfd, esl, islp);
1622 amt = sizeof (struct coff_debug_merge_element);
1623 *epp = bfd_alloc (input_bfd, amt);
1624 if (*epp == NULL)
1625 return FALSE;
1627 elename = _bfd_coff_internal_syment_name (input_bfd, islp,
1628 elebuf);
1629 if (elename == NULL)
1630 return FALSE;
1632 amt = strlen (elename) + 1;
1633 name_copy = bfd_alloc (input_bfd, amt);
1634 if (name_copy == NULL)
1635 return FALSE;
1636 strcpy (name_copy, elename);
1638 (*epp)->name = name_copy;
1639 (*epp)->type = islp->n_type;
1640 (*epp)->tagndx = 0;
1641 if (islp->n_numaux >= 1
1642 && islp->n_type != T_NULL
1643 && islp->n_sclass != C_EOS)
1645 union internal_auxent eleaux;
1646 long indx;
1648 bfd_coff_swap_aux_in (input_bfd, (esl + isymesz),
1649 islp->n_type, islp->n_sclass, 0,
1650 islp->n_numaux, &eleaux);
1651 indx = eleaux.x_sym.x_tagndx.l;
1653 /* FIXME: If this tagndx entry refers to a symbol
1654 defined later in this file, we just ignore it.
1655 Handling this correctly would be tedious, and may
1656 not be required. */
1657 if (indx > 0
1658 && (indx
1659 < ((esym -
1660 (bfd_byte *) obj_coff_external_syms (input_bfd))
1661 / (long) isymesz)))
1663 (*epp)->tagndx = finfo->sym_indices[indx];
1664 if ((*epp)->tagndx < 0)
1665 (*epp)->tagndx = 0;
1668 epp = &(*epp)->next;
1669 *epp = NULL;
1671 esl += (islp->n_numaux + 1) * isymesz;
1672 islp += islp->n_numaux + 1;
1675 /* See if we already have a definition which matches this
1676 type. We always output the type if it has no elements,
1677 for simplicity. */
1678 if (mt->elements == NULL)
1679 bfd_release (input_bfd, mt);
1680 else
1682 struct coff_debug_merge_type *mtl;
1684 for (mtl = mh->types; mtl != NULL; mtl = mtl->next)
1686 struct coff_debug_merge_element *me, *mel;
1688 if (mtl->class != mt->class)
1689 continue;
1691 for (me = mt->elements, mel = mtl->elements;
1692 me != NULL && mel != NULL;
1693 me = me->next, mel = mel->next)
1695 if (strcmp (me->name, mel->name) != 0
1696 || me->type != mel->type
1697 || me->tagndx != mel->tagndx)
1698 break;
1701 if (me == NULL && mel == NULL)
1702 break;
1705 if (mtl == NULL || (bfd_size_type) mtl->indx >= syment_base)
1707 /* This is the first definition of this type. */
1708 mt->indx = output_index;
1709 mt->next = mh->types;
1710 mh->types = mt;
1712 else
1714 /* This is a redefinition which can be merged. */
1715 bfd_release (input_bfd, mt);
1716 *indexp = mtl->indx;
1717 add = (eslend - esym) / isymesz;
1718 skip = TRUE;
1723 /* We now know whether we are to skip this symbol or not. */
1724 if (! skip)
1726 /* Adjust the symbol in order to output it. */
1728 if (isym._n._n_n._n_zeroes == 0
1729 && isym._n._n_n._n_offset != 0)
1731 const char *name;
1732 bfd_size_type indx;
1734 /* This symbol has a long name. Enter it in the string
1735 table we are building. Note that we do not check
1736 bfd_coff_symname_in_debug. That is only true for
1737 XCOFF, and XCOFF requires different linking code
1738 anyhow. */
1739 name = _bfd_coff_internal_syment_name (input_bfd, &isym, NULL);
1740 if (name == NULL)
1741 return FALSE;
1742 indx = _bfd_stringtab_add (finfo->strtab, name, hash, copy);
1743 if (indx == (bfd_size_type) -1)
1744 return FALSE;
1745 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
1748 switch (isym.n_sclass)
1750 case C_AUTO:
1751 case C_MOS:
1752 case C_EOS:
1753 case C_MOE:
1754 case C_MOU:
1755 case C_UNTAG:
1756 case C_STRTAG:
1757 case C_ENTAG:
1758 case C_TPDEF:
1759 case C_ARG:
1760 case C_USTATIC:
1761 case C_REG:
1762 case C_REGPARM:
1763 case C_FIELD:
1764 /* The symbol value should not be modified. */
1765 break;
1767 case C_FCN:
1768 if (obj_pe (input_bfd)
1769 && strcmp (isym.n_name, ".bf") != 0
1770 && isym.n_scnum > 0)
1772 /* For PE, .lf and .ef get their value left alone,
1773 while .bf gets relocated. However, they all have
1774 "real" section numbers, and need to be moved into
1775 the new section. */
1776 isym.n_scnum = (*secpp)->output_section->target_index;
1777 break;
1779 /* Fall through. */
1780 default:
1781 case C_LABEL: /* Not completely sure about these 2 */
1782 case C_EXTDEF:
1783 case C_BLOCK:
1784 case C_EFCN:
1785 case C_NULL:
1786 case C_EXT:
1787 case C_STAT:
1788 case C_SECTION:
1789 case C_NT_WEAK:
1790 /* Compute new symbol location. */
1791 if (isym.n_scnum > 0)
1793 isym.n_scnum = (*secpp)->output_section->target_index;
1794 isym.n_value += (*secpp)->output_offset;
1795 if (! obj_pe (input_bfd))
1796 isym.n_value -= (*secpp)->vma;
1797 if (! obj_pe (finfo->output_bfd))
1798 isym.n_value += (*secpp)->output_section->vma;
1800 break;
1802 case C_FILE:
1803 /* The value of a C_FILE symbol is the symbol index of
1804 the next C_FILE symbol. The value of the last C_FILE
1805 symbol is the symbol index to the first external
1806 symbol (actually, coff_renumber_symbols does not get
1807 this right--it just sets the value of the last C_FILE
1808 symbol to zero--and nobody has ever complained about
1809 it). We try to get this right, below, just before we
1810 write the symbols out, but in the general case we may
1811 have to write the symbol out twice. */
1812 if (finfo->last_file_index != -1
1813 && finfo->last_file.n_value != (bfd_vma) output_index)
1815 /* We must correct the value of the last C_FILE
1816 entry. */
1817 finfo->last_file.n_value = output_index;
1818 if ((bfd_size_type) finfo->last_file_index >= syment_base)
1820 /* The last C_FILE symbol is in this input file. */
1821 bfd_coff_swap_sym_out (output_bfd,
1822 &finfo->last_file,
1823 (finfo->outsyms
1824 + ((finfo->last_file_index
1825 - syment_base)
1826 * osymesz)));
1828 else
1830 file_ptr pos;
1832 /* We have already written out the last C_FILE
1833 symbol. We need to write it out again. We
1834 borrow *outsym temporarily. */
1835 bfd_coff_swap_sym_out (output_bfd,
1836 &finfo->last_file, outsym);
1837 pos = obj_sym_filepos (output_bfd);
1838 pos += finfo->last_file_index * osymesz;
1839 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
1840 || bfd_bwrite (outsym, osymesz, output_bfd) != osymesz)
1841 return FALSE;
1845 finfo->last_file_index = output_index;
1846 finfo->last_file = isym;
1847 break;
1850 /* If doing task linking, convert normal global function symbols to
1851 static functions. */
1852 if (finfo->info->task_link && IS_EXTERNAL (input_bfd, isym))
1853 isym.n_sclass = C_STAT;
1855 /* Output the symbol. */
1856 bfd_coff_swap_sym_out (output_bfd, &isym, outsym);
1858 *indexp = output_index;
1860 if (global)
1862 long indx;
1863 struct coff_link_hash_entry *h;
1865 indx = ((esym - (bfd_byte *) obj_coff_external_syms (input_bfd))
1866 / isymesz);
1867 h = obj_coff_sym_hashes (input_bfd)[indx];
1868 if (h == NULL)
1870 /* This can happen if there were errors earlier in
1871 the link. */
1872 bfd_set_error (bfd_error_bad_value);
1873 return FALSE;
1875 h->indx = output_index;
1878 output_index += add;
1879 outsym += add * osymesz;
1882 esym += add * isymesz;
1883 isymp += add;
1884 ++secpp;
1885 ++indexp;
1886 for (--add; add > 0; --add)
1888 *secpp++ = NULL;
1889 *indexp++ = -1;
1893 /* Fix up the aux entries. This must be done in a separate pass,
1894 because we don't know the correct symbol indices until we have
1895 already decided which symbols we are going to keep. */
1896 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
1897 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
1898 isymp = finfo->internal_syms;
1899 indexp = finfo->sym_indices;
1900 sym_hash = obj_coff_sym_hashes (input_bfd);
1901 outsym = finfo->outsyms;
1903 while (esym < esym_end)
1905 int add;
1907 add = 1 + isymp->n_numaux;
1909 if ((*indexp < 0
1910 || (bfd_size_type) *indexp < syment_base)
1911 && (*sym_hash == NULL
1912 || (*sym_hash)->auxbfd != input_bfd))
1913 esym += add * isymesz;
1914 else
1916 struct coff_link_hash_entry *h;
1917 int i;
1919 h = NULL;
1920 if (*indexp < 0)
1922 h = *sym_hash;
1924 /* The m68k-motorola-sysv assembler will sometimes
1925 generate two symbols with the same name, but only one
1926 will have aux entries. */
1927 BFD_ASSERT (isymp->n_numaux == 0
1928 || h->numaux == isymp->n_numaux);
1931 esym += isymesz;
1933 if (h == NULL)
1934 outsym += osymesz;
1936 /* Handle the aux entries. This handling is based on
1937 coff_pointerize_aux. I don't know if it always correct. */
1938 for (i = 0; i < isymp->n_numaux && esym < esym_end; i++)
1940 union internal_auxent aux;
1941 union internal_auxent *auxp;
1943 if (h != NULL)
1944 auxp = h->aux + i;
1945 else
1947 bfd_coff_swap_aux_in (input_bfd, esym, isymp->n_type,
1948 isymp->n_sclass, i, isymp->n_numaux, &aux);
1949 auxp = &aux;
1952 if (isymp->n_sclass == C_FILE)
1954 /* If this is a long filename, we must put it in the
1955 string table. */
1956 if (auxp->x_file.x_n.x_zeroes == 0
1957 && auxp->x_file.x_n.x_offset != 0)
1959 const char *filename;
1960 bfd_size_type indx;
1962 BFD_ASSERT (auxp->x_file.x_n.x_offset
1963 >= STRING_SIZE_SIZE);
1964 if (strings == NULL)
1966 strings = _bfd_coff_read_string_table (input_bfd);
1967 if (strings == NULL)
1968 return FALSE;
1970 filename = strings + auxp->x_file.x_n.x_offset;
1971 indx = _bfd_stringtab_add (finfo->strtab, filename,
1972 hash, copy);
1973 if (indx == (bfd_size_type) -1)
1974 return FALSE;
1975 auxp->x_file.x_n.x_offset = STRING_SIZE_SIZE + indx;
1978 else if ((isymp->n_sclass != C_STAT || isymp->n_type != T_NULL)
1979 && isymp->n_sclass != C_NT_WEAK)
1981 unsigned long indx;
1983 if (ISFCN (isymp->n_type)
1984 || ISTAG (isymp->n_sclass)
1985 || isymp->n_sclass == C_BLOCK
1986 || isymp->n_sclass == C_FCN)
1988 indx = auxp->x_sym.x_fcnary.x_fcn.x_endndx.l;
1989 if (indx > 0
1990 && indx < obj_raw_syment_count (input_bfd))
1992 /* We look forward through the symbol for
1993 the index of the next symbol we are going
1994 to include. I don't know if this is
1995 entirely right. */
1996 while ((finfo->sym_indices[indx] < 0
1997 || ((bfd_size_type) finfo->sym_indices[indx]
1998 < syment_base))
1999 && indx < obj_raw_syment_count (input_bfd))
2000 ++indx;
2001 if (indx >= obj_raw_syment_count (input_bfd))
2002 indx = output_index;
2003 else
2004 indx = finfo->sym_indices[indx];
2005 auxp->x_sym.x_fcnary.x_fcn.x_endndx.l = indx;
2009 indx = auxp->x_sym.x_tagndx.l;
2010 if (indx > 0 && indx < obj_raw_syment_count (input_bfd))
2012 long symindx;
2014 symindx = finfo->sym_indices[indx];
2015 if (symindx < 0)
2016 auxp->x_sym.x_tagndx.l = 0;
2017 else
2018 auxp->x_sym.x_tagndx.l = symindx;
2021 /* The .bf symbols are supposed to be linked through
2022 the endndx field. We need to carry this list
2023 across object files. */
2024 if (i == 0
2025 && h == NULL
2026 && isymp->n_sclass == C_FCN
2027 && (isymp->_n._n_n._n_zeroes != 0
2028 || isymp->_n._n_n._n_offset == 0)
2029 && isymp->_n._n_name[0] == '.'
2030 && isymp->_n._n_name[1] == 'b'
2031 && isymp->_n._n_name[2] == 'f'
2032 && isymp->_n._n_name[3] == '\0')
2034 if (finfo->last_bf_index != -1)
2036 finfo->last_bf.x_sym.x_fcnary.x_fcn.x_endndx.l =
2037 *indexp;
2039 if ((bfd_size_type) finfo->last_bf_index
2040 >= syment_base)
2042 void *auxout;
2044 /* The last .bf symbol is in this input
2045 file. This will only happen if the
2046 assembler did not set up the .bf
2047 endndx symbols correctly. */
2048 auxout = (finfo->outsyms
2049 + ((finfo->last_bf_index
2050 - syment_base)
2051 * osymesz));
2053 bfd_coff_swap_aux_out (output_bfd,
2054 &finfo->last_bf,
2055 isymp->n_type,
2056 isymp->n_sclass,
2057 0, isymp->n_numaux,
2058 auxout);
2060 else
2062 file_ptr pos;
2064 /* We have already written out the last
2065 .bf aux entry. We need to write it
2066 out again. We borrow *outsym
2067 temporarily. FIXME: This case should
2068 be made faster. */
2069 bfd_coff_swap_aux_out (output_bfd,
2070 &finfo->last_bf,
2071 isymp->n_type,
2072 isymp->n_sclass,
2073 0, isymp->n_numaux,
2074 outsym);
2075 pos = obj_sym_filepos (output_bfd);
2076 pos += finfo->last_bf_index * osymesz;
2077 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
2078 || (bfd_bwrite (outsym, osymesz, output_bfd)
2079 != osymesz))
2080 return FALSE;
2084 if (auxp->x_sym.x_fcnary.x_fcn.x_endndx.l != 0)
2085 finfo->last_bf_index = -1;
2086 else
2088 /* The endndx field of this aux entry must
2089 be updated with the symbol number of the
2090 next .bf symbol. */
2091 finfo->last_bf = *auxp;
2092 finfo->last_bf_index = (((outsym - finfo->outsyms)
2093 / osymesz)
2094 + syment_base);
2099 if (h == NULL)
2101 bfd_coff_swap_aux_out (output_bfd, auxp, isymp->n_type,
2102 isymp->n_sclass, i, isymp->n_numaux,
2103 outsym);
2104 outsym += osymesz;
2107 esym += isymesz;
2111 indexp += add;
2112 isymp += add;
2113 sym_hash += add;
2116 /* Relocate the line numbers, unless we are stripping them. */
2117 if (finfo->info->strip == strip_none
2118 || finfo->info->strip == strip_some)
2120 for (o = input_bfd->sections; o != NULL; o = o->next)
2122 bfd_vma offset;
2123 bfd_byte *eline;
2124 bfd_byte *elineend;
2125 bfd_byte *oeline;
2126 bfd_boolean skipping;
2127 file_ptr pos;
2128 bfd_size_type amt;
2130 /* FIXME: If SEC_HAS_CONTENTS is not for the section, then
2131 build_link_order in ldwrite.c will not have created a
2132 link order, which means that we will not have seen this
2133 input section in _bfd_coff_final_link, which means that
2134 we will not have allocated space for the line numbers of
2135 this section. I don't think line numbers can be
2136 meaningful for a section which does not have
2137 SEC_HAS_CONTENTS set, but, if they do, this must be
2138 changed. */
2139 if (o->lineno_count == 0
2140 || (o->output_section->flags & SEC_HAS_CONTENTS) == 0)
2141 continue;
2143 if (bfd_seek (input_bfd, o->line_filepos, SEEK_SET) != 0
2144 || bfd_bread (finfo->linenos, linesz * o->lineno_count,
2145 input_bfd) != linesz * o->lineno_count)
2146 return FALSE;
2148 offset = o->output_section->vma + o->output_offset - o->vma;
2149 eline = finfo->linenos;
2150 oeline = finfo->linenos;
2151 elineend = eline + linesz * o->lineno_count;
2152 skipping = FALSE;
2153 for (; eline < elineend; eline += linesz)
2155 struct internal_lineno iline;
2157 bfd_coff_swap_lineno_in (input_bfd, eline, &iline);
2159 if (iline.l_lnno != 0)
2160 iline.l_addr.l_paddr += offset;
2161 else if (iline.l_addr.l_symndx >= 0
2162 && ((unsigned long) iline.l_addr.l_symndx
2163 < obj_raw_syment_count (input_bfd)))
2165 long indx;
2167 indx = finfo->sym_indices[iline.l_addr.l_symndx];
2169 if (indx < 0)
2171 /* These line numbers are attached to a symbol
2172 which we are stripping. We must discard the
2173 line numbers because reading them back with
2174 no associated symbol (or associating them all
2175 with symbol #0) will fail. We can't regain
2176 the space in the output file, but at least
2177 they're dense. */
2178 skipping = TRUE;
2180 else
2182 struct internal_syment is;
2183 union internal_auxent ia;
2185 /* Fix up the lnnoptr field in the aux entry of
2186 the symbol. It turns out that we can't do
2187 this when we modify the symbol aux entries,
2188 because gas sometimes screws up the lnnoptr
2189 field and makes it an offset from the start
2190 of the line numbers rather than an absolute
2191 file index. */
2192 bfd_coff_swap_sym_in (output_bfd,
2193 (finfo->outsyms
2194 + ((indx - syment_base)
2195 * osymesz)), &is);
2196 if ((ISFCN (is.n_type)
2197 || is.n_sclass == C_BLOCK)
2198 && is.n_numaux >= 1)
2200 void *auxptr;
2202 auxptr = (finfo->outsyms
2203 + ((indx - syment_base + 1)
2204 * osymesz));
2205 bfd_coff_swap_aux_in (output_bfd, auxptr,
2206 is.n_type, is.n_sclass,
2207 0, is.n_numaux, &ia);
2208 ia.x_sym.x_fcnary.x_fcn.x_lnnoptr =
2209 (o->output_section->line_filepos
2210 + o->output_section->lineno_count * linesz
2211 + eline - finfo->linenos);
2212 bfd_coff_swap_aux_out (output_bfd, &ia,
2213 is.n_type, is.n_sclass, 0,
2214 is.n_numaux, auxptr);
2217 skipping = FALSE;
2220 iline.l_addr.l_symndx = indx;
2223 if (!skipping)
2225 bfd_coff_swap_lineno_out (output_bfd, &iline, oeline);
2226 oeline += linesz;
2230 pos = o->output_section->line_filepos;
2231 pos += o->output_section->lineno_count * linesz;
2232 amt = oeline - finfo->linenos;
2233 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
2234 || bfd_bwrite (finfo->linenos, amt, output_bfd) != amt)
2235 return FALSE;
2237 o->output_section->lineno_count += amt / linesz;
2241 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
2242 symbol will be the first symbol in the next input file. In the
2243 normal case, this will save us from writing out the C_FILE symbol
2244 again. */
2245 if (finfo->last_file_index != -1
2246 && (bfd_size_type) finfo->last_file_index >= syment_base)
2248 finfo->last_file.n_value = output_index;
2249 bfd_coff_swap_sym_out (output_bfd, &finfo->last_file,
2250 (finfo->outsyms
2251 + ((finfo->last_file_index - syment_base)
2252 * osymesz)));
2255 /* Write the modified symbols to the output file. */
2256 if (outsym > finfo->outsyms)
2258 file_ptr pos;
2259 bfd_size_type amt;
2261 pos = obj_sym_filepos (output_bfd) + syment_base * osymesz;
2262 amt = outsym - finfo->outsyms;
2263 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
2264 || bfd_bwrite (finfo->outsyms, amt, output_bfd) != amt)
2265 return FALSE;
2267 BFD_ASSERT ((obj_raw_syment_count (output_bfd)
2268 + (outsym - finfo->outsyms) / osymesz)
2269 == output_index);
2271 obj_raw_syment_count (output_bfd) = output_index;
2274 /* Relocate the contents of each section. */
2275 adjust_symndx = coff_backend_info (input_bfd)->_bfd_coff_adjust_symndx;
2276 for (o = input_bfd->sections; o != NULL; o = o->next)
2278 bfd_byte *contents;
2279 struct coff_section_tdata *secdata;
2281 if (! o->linker_mark)
2282 /* This section was omitted from the link. */
2283 continue;
2285 if ((o->flags & SEC_LINKER_CREATED) != 0)
2286 continue;
2288 if ((o->flags & SEC_HAS_CONTENTS) == 0
2289 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
2291 if ((o->flags & SEC_RELOC) != 0
2292 && o->reloc_count != 0)
2294 (*_bfd_error_handler)
2295 (_("%B: relocs in section `%A', but it has no contents"),
2296 input_bfd, o);
2297 bfd_set_error (bfd_error_no_contents);
2298 return FALSE;
2301 continue;
2304 secdata = coff_section_data (input_bfd, o);
2305 if (secdata != NULL && secdata->contents != NULL)
2306 contents = secdata->contents;
2307 else
2309 bfd_size_type x = o->rawsize ? o->rawsize : o->size;
2310 if (! bfd_get_section_contents (input_bfd, o, finfo->contents, 0, x))
2311 return FALSE;
2312 contents = finfo->contents;
2315 if ((o->flags & SEC_RELOC) != 0)
2317 int target_index;
2318 struct internal_reloc *internal_relocs;
2319 struct internal_reloc *irel;
2321 /* Read in the relocs. */
2322 target_index = o->output_section->target_index;
2323 internal_relocs = (_bfd_coff_read_internal_relocs
2324 (input_bfd, o, FALSE, finfo->external_relocs,
2325 finfo->info->relocatable,
2326 (finfo->info->relocatable
2327 ? (finfo->section_info[target_index].relocs
2328 + o->output_section->reloc_count)
2329 : finfo->internal_relocs)));
2330 if (internal_relocs == NULL)
2331 return FALSE;
2333 /* Call processor specific code to relocate the section
2334 contents. */
2335 if (! bfd_coff_relocate_section (output_bfd, finfo->info,
2336 input_bfd, o,
2337 contents,
2338 internal_relocs,
2339 finfo->internal_syms,
2340 finfo->sec_ptrs))
2341 return FALSE;
2343 if (finfo->info->relocatable)
2345 bfd_vma offset;
2346 struct internal_reloc *irelend;
2347 struct coff_link_hash_entry **rel_hash;
2349 offset = o->output_section->vma + o->output_offset - o->vma;
2350 irel = internal_relocs;
2351 irelend = irel + o->reloc_count;
2352 rel_hash = (finfo->section_info[target_index].rel_hashes
2353 + o->output_section->reloc_count);
2354 for (; irel < irelend; irel++, rel_hash++)
2356 struct coff_link_hash_entry *h;
2357 bfd_boolean adjusted;
2359 *rel_hash = NULL;
2361 /* Adjust the reloc address and symbol index. */
2362 irel->r_vaddr += offset;
2364 if (irel->r_symndx == -1)
2365 continue;
2367 if (adjust_symndx)
2369 if (! (*adjust_symndx) (output_bfd, finfo->info,
2370 input_bfd, o, irel,
2371 &adjusted))
2372 return FALSE;
2373 if (adjusted)
2374 continue;
2377 h = obj_coff_sym_hashes (input_bfd)[irel->r_symndx];
2378 if (h != NULL)
2380 /* This is a global symbol. */
2381 if (h->indx >= 0)
2382 irel->r_symndx = h->indx;
2383 else
2385 /* This symbol is being written at the end
2386 of the file, and we do not yet know the
2387 symbol index. We save the pointer to the
2388 hash table entry in the rel_hash list.
2389 We set the indx field to -2 to indicate
2390 that this symbol must not be stripped. */
2391 *rel_hash = h;
2392 h->indx = -2;
2395 else
2397 long indx;
2399 indx = finfo->sym_indices[irel->r_symndx];
2400 if (indx != -1)
2401 irel->r_symndx = indx;
2402 else
2404 struct internal_syment *is;
2405 const char *name;
2406 char buf[SYMNMLEN + 1];
2408 /* This reloc is against a symbol we are
2409 stripping. This should have been handled
2410 by the 'dont_skip_symbol' code in the while
2411 loop at the top of this function. */
2412 is = finfo->internal_syms + irel->r_symndx;
2414 name = (_bfd_coff_internal_syment_name
2415 (input_bfd, is, buf));
2416 if (name == NULL)
2417 return FALSE;
2419 if (! ((*finfo->info->callbacks->unattached_reloc)
2420 (finfo->info, name, input_bfd, o,
2421 irel->r_vaddr)))
2422 return FALSE;
2427 o->output_section->reloc_count += o->reloc_count;
2431 /* Write out the modified section contents. */
2432 if (secdata == NULL || secdata->stab_info == NULL)
2434 file_ptr loc = o->output_offset * bfd_octets_per_byte (output_bfd);
2435 if (! bfd_set_section_contents (output_bfd, o->output_section,
2436 contents, loc, o->size))
2437 return FALSE;
2439 else
2441 if (! (_bfd_write_section_stabs
2442 (output_bfd, &coff_hash_table (finfo->info)->stab_info,
2443 o, &secdata->stab_info, contents)))
2444 return FALSE;
2448 if (! finfo->info->keep_memory
2449 && ! _bfd_coff_free_symbols (input_bfd))
2450 return FALSE;
2452 return TRUE;
2455 /* Write out a global symbol. Called via coff_link_hash_traverse. */
2457 bfd_boolean
2458 _bfd_coff_write_global_sym (struct coff_link_hash_entry *h, void *data)
2460 struct coff_final_link_info *finfo = (struct coff_final_link_info *) data;
2461 bfd *output_bfd;
2462 struct internal_syment isym;
2463 bfd_size_type symesz;
2464 unsigned int i;
2465 file_ptr pos;
2467 output_bfd = finfo->output_bfd;
2469 if (h->root.type == bfd_link_hash_warning)
2471 h = (struct coff_link_hash_entry *) h->root.u.i.link;
2472 if (h->root.type == bfd_link_hash_new)
2473 return TRUE;
2476 if (h->indx >= 0)
2477 return TRUE;
2479 if (h->indx != -2
2480 && (finfo->info->strip == strip_all
2481 || (finfo->info->strip == strip_some
2482 && (bfd_hash_lookup (finfo->info->keep_hash,
2483 h->root.root.string, FALSE, FALSE)
2484 == NULL))))
2485 return TRUE;
2487 switch (h->root.type)
2489 default:
2490 case bfd_link_hash_new:
2491 case bfd_link_hash_warning:
2492 abort ();
2493 return FALSE;
2495 case bfd_link_hash_undefined:
2496 case bfd_link_hash_undefweak:
2497 isym.n_scnum = N_UNDEF;
2498 isym.n_value = 0;
2499 break;
2501 case bfd_link_hash_defined:
2502 case bfd_link_hash_defweak:
2504 asection *sec;
2506 sec = h->root.u.def.section->output_section;
2507 if (bfd_is_abs_section (sec))
2508 isym.n_scnum = N_ABS;
2509 else
2510 isym.n_scnum = sec->target_index;
2511 isym.n_value = (h->root.u.def.value
2512 + h->root.u.def.section->output_offset);
2513 if (! obj_pe (finfo->output_bfd))
2514 isym.n_value += sec->vma;
2516 break;
2518 case bfd_link_hash_common:
2519 isym.n_scnum = N_UNDEF;
2520 isym.n_value = h->root.u.c.size;
2521 break;
2523 case bfd_link_hash_indirect:
2524 /* Just ignore these. They can't be handled anyhow. */
2525 return TRUE;
2528 if (strlen (h->root.root.string) <= SYMNMLEN)
2529 strncpy (isym._n._n_name, h->root.root.string, SYMNMLEN);
2530 else
2532 bfd_boolean hash;
2533 bfd_size_type indx;
2535 hash = TRUE;
2536 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
2537 hash = FALSE;
2538 indx = _bfd_stringtab_add (finfo->strtab, h->root.root.string, hash,
2539 FALSE);
2540 if (indx == (bfd_size_type) -1)
2542 finfo->failed = TRUE;
2543 return FALSE;
2545 isym._n._n_n._n_zeroes = 0;
2546 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
2549 isym.n_sclass = h->class;
2550 isym.n_type = h->type;
2552 if (isym.n_sclass == C_NULL)
2553 isym.n_sclass = C_EXT;
2555 /* If doing task linking and this is the pass where we convert
2556 defined globals to statics, then do that conversion now. If the
2557 symbol is not being converted, just ignore it and it will be
2558 output during a later pass. */
2559 if (finfo->global_to_static)
2561 if (! IS_EXTERNAL (output_bfd, isym))
2562 return TRUE;
2564 isym.n_sclass = C_STAT;
2567 /* When a weak symbol is not overridden by a strong one,
2568 turn it into an external symbol when not building a
2569 shared or relocatable object. */
2570 if (! finfo->info->shared
2571 && ! finfo->info->relocatable
2572 && IS_WEAK_EXTERNAL (finfo->output_bfd, isym))
2573 isym.n_sclass = C_EXT;
2575 isym.n_numaux = h->numaux;
2577 bfd_coff_swap_sym_out (output_bfd, &isym, finfo->outsyms);
2579 symesz = bfd_coff_symesz (output_bfd);
2581 pos = obj_sym_filepos (output_bfd);
2582 pos += obj_raw_syment_count (output_bfd) * symesz;
2583 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
2584 || bfd_bwrite (finfo->outsyms, symesz, output_bfd) != symesz)
2586 finfo->failed = TRUE;
2587 return FALSE;
2590 h->indx = obj_raw_syment_count (output_bfd);
2592 ++obj_raw_syment_count (output_bfd);
2594 /* Write out any associated aux entries. Most of the aux entries
2595 will have been modified in _bfd_coff_link_input_bfd. We have to
2596 handle section aux entries here, now that we have the final
2597 relocation and line number counts. */
2598 for (i = 0; i < isym.n_numaux; i++)
2600 union internal_auxent *auxp;
2602 auxp = h->aux + i;
2604 /* Look for a section aux entry here using the same tests that
2605 coff_swap_aux_out uses. */
2606 if (i == 0
2607 && (isym.n_sclass == C_STAT
2608 || isym.n_sclass == C_HIDDEN)
2609 && isym.n_type == T_NULL
2610 && (h->root.type == bfd_link_hash_defined
2611 || h->root.type == bfd_link_hash_defweak))
2613 asection *sec;
2615 sec = h->root.u.def.section->output_section;
2616 if (sec != NULL)
2618 auxp->x_scn.x_scnlen = sec->size;
2620 /* For PE, an overflow on the final link reportedly does
2621 not matter. FIXME: Why not? */
2622 if (sec->reloc_count > 0xffff
2623 && (! obj_pe (output_bfd)
2624 || finfo->info->relocatable))
2625 (*_bfd_error_handler)
2626 (_("%s: %s: reloc overflow: 0x%lx > 0xffff"),
2627 bfd_get_filename (output_bfd),
2628 bfd_get_section_name (output_bfd, sec),
2629 sec->reloc_count);
2631 if (sec->lineno_count > 0xffff
2632 && (! obj_pe (output_bfd)
2633 || finfo->info->relocatable))
2634 (*_bfd_error_handler)
2635 (_("%s: warning: %s: line number overflow: 0x%lx > 0xffff"),
2636 bfd_get_filename (output_bfd),
2637 bfd_get_section_name (output_bfd, sec),
2638 sec->lineno_count);
2640 auxp->x_scn.x_nreloc = sec->reloc_count;
2641 auxp->x_scn.x_nlinno = sec->lineno_count;
2642 auxp->x_scn.x_checksum = 0;
2643 auxp->x_scn.x_associated = 0;
2644 auxp->x_scn.x_comdat = 0;
2648 bfd_coff_swap_aux_out (output_bfd, auxp, isym.n_type,
2649 isym.n_sclass, (int) i, isym.n_numaux,
2650 finfo->outsyms);
2651 if (bfd_bwrite (finfo->outsyms, symesz, output_bfd) != symesz)
2653 finfo->failed = TRUE;
2654 return FALSE;
2656 ++obj_raw_syment_count (output_bfd);
2659 return TRUE;
2662 /* Write out task global symbols, converting them to statics. Called
2663 via coff_link_hash_traverse. Calls bfd_coff_write_global_sym to do
2664 the dirty work, if the symbol we are processing needs conversion. */
2666 bfd_boolean
2667 _bfd_coff_write_task_globals (struct coff_link_hash_entry *h, void *data)
2669 struct coff_final_link_info *finfo = (struct coff_final_link_info *) data;
2670 bfd_boolean rtnval = TRUE;
2671 bfd_boolean save_global_to_static;
2673 if (h->root.type == bfd_link_hash_warning)
2674 h = (struct coff_link_hash_entry *) h->root.u.i.link;
2676 if (h->indx < 0)
2678 switch (h->root.type)
2680 case bfd_link_hash_defined:
2681 case bfd_link_hash_defweak:
2682 save_global_to_static = finfo->global_to_static;
2683 finfo->global_to_static = TRUE;
2684 rtnval = _bfd_coff_write_global_sym (h, data);
2685 finfo->global_to_static = save_global_to_static;
2686 break;
2687 default:
2688 break;
2691 return (rtnval);
2694 /* Handle a link order which is supposed to generate a reloc. */
2696 bfd_boolean
2697 _bfd_coff_reloc_link_order (bfd *output_bfd,
2698 struct coff_final_link_info *finfo,
2699 asection *output_section,
2700 struct bfd_link_order *link_order)
2702 reloc_howto_type *howto;
2703 struct internal_reloc *irel;
2704 struct coff_link_hash_entry **rel_hash_ptr;
2706 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
2707 if (howto == NULL)
2709 bfd_set_error (bfd_error_bad_value);
2710 return FALSE;
2713 if (link_order->u.reloc.p->addend != 0)
2715 bfd_size_type size;
2716 bfd_byte *buf;
2717 bfd_reloc_status_type rstat;
2718 bfd_boolean ok;
2719 file_ptr loc;
2721 size = bfd_get_reloc_size (howto);
2722 buf = bfd_zmalloc (size);
2723 if (buf == NULL)
2724 return FALSE;
2726 rstat = _bfd_relocate_contents (howto, output_bfd,
2727 (bfd_vma) link_order->u.reloc.p->addend,\
2728 buf);
2729 switch (rstat)
2731 case bfd_reloc_ok:
2732 break;
2733 default:
2734 case bfd_reloc_outofrange:
2735 abort ();
2736 case bfd_reloc_overflow:
2737 if (! ((*finfo->info->callbacks->reloc_overflow)
2738 (finfo->info, NULL,
2739 (link_order->type == bfd_section_reloc_link_order
2740 ? bfd_section_name (output_bfd,
2741 link_order->u.reloc.p->u.section)
2742 : link_order->u.reloc.p->u.name),
2743 howto->name, link_order->u.reloc.p->addend,
2744 (bfd *) NULL, (asection *) NULL, (bfd_vma) 0)))
2746 free (buf);
2747 return FALSE;
2749 break;
2751 loc = link_order->offset * bfd_octets_per_byte (output_bfd);
2752 ok = bfd_set_section_contents (output_bfd, output_section, buf,
2753 loc, size);
2754 free (buf);
2755 if (! ok)
2756 return FALSE;
2759 /* Store the reloc information in the right place. It will get
2760 swapped and written out at the end of the final_link routine. */
2761 irel = (finfo->section_info[output_section->target_index].relocs
2762 + output_section->reloc_count);
2763 rel_hash_ptr = (finfo->section_info[output_section->target_index].rel_hashes
2764 + output_section->reloc_count);
2766 memset (irel, 0, sizeof (struct internal_reloc));
2767 *rel_hash_ptr = NULL;
2769 irel->r_vaddr = output_section->vma + link_order->offset;
2771 if (link_order->type == bfd_section_reloc_link_order)
2773 /* We need to somehow locate a symbol in the right section. The
2774 symbol must either have a value of zero, or we must adjust
2775 the addend by the value of the symbol. FIXME: Write this
2776 when we need it. The old linker couldn't handle this anyhow. */
2777 abort ();
2778 *rel_hash_ptr = NULL;
2779 irel->r_symndx = 0;
2781 else
2783 struct coff_link_hash_entry *h;
2785 h = ((struct coff_link_hash_entry *)
2786 bfd_wrapped_link_hash_lookup (output_bfd, finfo->info,
2787 link_order->u.reloc.p->u.name,
2788 FALSE, FALSE, TRUE));
2789 if (h != NULL)
2791 if (h->indx >= 0)
2792 irel->r_symndx = h->indx;
2793 else
2795 /* Set the index to -2 to force this symbol to get
2796 written out. */
2797 h->indx = -2;
2798 *rel_hash_ptr = h;
2799 irel->r_symndx = 0;
2802 else
2804 if (! ((*finfo->info->callbacks->unattached_reloc)
2805 (finfo->info, link_order->u.reloc.p->u.name, (bfd *) NULL,
2806 (asection *) NULL, (bfd_vma) 0)))
2807 return FALSE;
2808 irel->r_symndx = 0;
2812 /* FIXME: Is this always right? */
2813 irel->r_type = howto->type;
2815 /* r_size is only used on the RS/6000, which needs its own linker
2816 routines anyhow. r_extern is only used for ECOFF. */
2818 /* FIXME: What is the right value for r_offset? Is zero OK? */
2819 ++output_section->reloc_count;
2821 return TRUE;
2824 /* A basic reloc handling routine which may be used by processors with
2825 simple relocs. */
2827 bfd_boolean
2828 _bfd_coff_generic_relocate_section (bfd *output_bfd,
2829 struct bfd_link_info *info,
2830 bfd *input_bfd,
2831 asection *input_section,
2832 bfd_byte *contents,
2833 struct internal_reloc *relocs,
2834 struct internal_syment *syms,
2835 asection **sections)
2837 struct internal_reloc *rel;
2838 struct internal_reloc *relend;
2840 rel = relocs;
2841 relend = rel + input_section->reloc_count;
2842 for (; rel < relend; rel++)
2844 long symndx;
2845 struct coff_link_hash_entry *h;
2846 struct internal_syment *sym;
2847 bfd_vma addend;
2848 bfd_vma val;
2849 reloc_howto_type *howto;
2850 bfd_reloc_status_type rstat;
2852 symndx = rel->r_symndx;
2854 if (symndx == -1)
2856 h = NULL;
2857 sym = NULL;
2859 else if (symndx < 0
2860 || (unsigned long) symndx >= obj_raw_syment_count (input_bfd))
2862 (*_bfd_error_handler)
2863 ("%B: illegal symbol index %ld in relocs", input_bfd, symndx);
2864 return FALSE;
2866 else
2868 h = obj_coff_sym_hashes (input_bfd)[symndx];
2869 sym = syms + symndx;
2872 /* COFF treats common symbols in one of two ways. Either the
2873 size of the symbol is included in the section contents, or it
2874 is not. We assume that the size is not included, and force
2875 the rtype_to_howto function to adjust the addend as needed. */
2876 if (sym != NULL && sym->n_scnum != 0)
2877 addend = - sym->n_value;
2878 else
2879 addend = 0;
2881 howto = bfd_coff_rtype_to_howto (input_bfd, input_section, rel, h,
2882 sym, &addend);
2883 if (howto == NULL)
2884 return FALSE;
2886 /* If we are doing a relocatable link, then we can just ignore
2887 a PC relative reloc that is pcrel_offset. It will already
2888 have the correct value. If this is not a relocatable link,
2889 then we should ignore the symbol value. */
2890 if (howto->pc_relative && howto->pcrel_offset)
2892 if (info->relocatable)
2893 continue;
2894 if (sym != NULL && sym->n_scnum != 0)
2895 addend += sym->n_value;
2898 val = 0;
2900 if (h == NULL)
2902 asection *sec;
2904 if (symndx == -1)
2906 sec = bfd_abs_section_ptr;
2907 val = 0;
2909 else
2911 sec = sections[symndx];
2912 val = (sec->output_section->vma
2913 + sec->output_offset
2914 + sym->n_value);
2915 if (! obj_pe (input_bfd))
2916 val -= sec->vma;
2919 else
2921 if (h->root.type == bfd_link_hash_defined
2922 || h->root.type == bfd_link_hash_defweak)
2924 /* Defined weak symbols are a GNU extension. */
2925 asection *sec;
2927 sec = h->root.u.def.section;
2928 val = (h->root.u.def.value
2929 + sec->output_section->vma
2930 + sec->output_offset);
2933 else if (h->root.type == bfd_link_hash_undefweak)
2935 if (h->class == C_NT_WEAK && h->numaux == 1)
2937 /* See _Microsoft Portable Executable and Common Object
2938 File Format Specification_, section 5.5.3.
2939 Note that weak symbols without aux records are a GNU
2940 extension.
2941 FIXME: All weak externals are treated as having
2942 characteristic IMAGE_WEAK_EXTERN_SEARCH_NOLIBRARY (1).
2943 These behave as per SVR4 ABI: A library member
2944 will resolve a weak external only if a normal
2945 external causes the library member to be linked.
2946 See also linker.c: generic_link_check_archive_element. */
2947 asection *sec;
2948 struct coff_link_hash_entry *h2 =
2949 input_bfd->tdata.coff_obj_data->sym_hashes[
2950 h->aux->x_sym.x_tagndx.l];
2952 if (!h2 || h2->root.type == bfd_link_hash_undefined)
2954 sec = bfd_abs_section_ptr;
2955 val = 0;
2957 else
2959 sec = h2->root.u.def.section;
2960 val = h2->root.u.def.value
2961 + sec->output_section->vma + sec->output_offset;
2964 else
2965 /* This is a GNU extension. */
2966 val = 0;
2969 else if (! info->relocatable)
2971 if (! ((*info->callbacks->undefined_symbol)
2972 (info, h->root.root.string, input_bfd, input_section,
2973 rel->r_vaddr - input_section->vma, TRUE)))
2974 return FALSE;
2978 if (info->base_file)
2980 /* Emit a reloc if the backend thinks it needs it. */
2981 if (sym && pe_data (output_bfd)->in_reloc_p (output_bfd, howto))
2983 /* Relocation to a symbol in a section which isn't
2984 absolute. We output the address here to a file.
2985 This file is then read by dlltool when generating the
2986 reloc section. Note that the base file is not
2987 portable between systems. We write out a long here,
2988 and dlltool reads in a long. */
2989 long addr = (rel->r_vaddr
2990 - input_section->vma
2991 + input_section->output_offset
2992 + input_section->output_section->vma);
2993 if (coff_data (output_bfd)->pe)
2994 addr -= pe_data(output_bfd)->pe_opthdr.ImageBase;
2995 if (fwrite (&addr, 1, sizeof (long), (FILE *) info->base_file)
2996 != sizeof (long))
2998 bfd_set_error (bfd_error_system_call);
2999 return FALSE;
3004 rstat = _bfd_final_link_relocate (howto, input_bfd, input_section,
3005 contents,
3006 rel->r_vaddr - input_section->vma,
3007 val, addend);
3009 switch (rstat)
3011 default:
3012 abort ();
3013 case bfd_reloc_ok:
3014 break;
3015 case bfd_reloc_outofrange:
3016 (*_bfd_error_handler)
3017 (_("%B: bad reloc address 0x%lx in section `%A'"),
3018 input_bfd, input_section, (unsigned long) rel->r_vaddr);
3019 return FALSE;
3020 case bfd_reloc_overflow:
3022 const char *name;
3023 char buf[SYMNMLEN + 1];
3025 if (symndx == -1)
3026 name = "*ABS*";
3027 else if (h != NULL)
3028 name = NULL;
3029 else
3031 name = _bfd_coff_internal_syment_name (input_bfd, sym, buf);
3032 if (name == NULL)
3033 return FALSE;
3036 if (! ((*info->callbacks->reloc_overflow)
3037 (info, (h ? &h->root : NULL), name, howto->name,
3038 (bfd_vma) 0, input_bfd, input_section,
3039 rel->r_vaddr - input_section->vma)))
3040 return FALSE;
3044 return TRUE;