Update HOWTO document after creating the 2.42 branch
[binutils-gdb.git] / bfd / cofflink.c
blob4a91d5a1833711ab45147d372f0c9f6052cad676
1 /* COFF specific linker code.
2 Copyright (C) 1994-2024 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor, Cygnus Support.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
22 /* This file contains the COFF backend linker code. */
24 #include "sysdep.h"
25 #include "bfd.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 bool coff_link_add_object_symbols (bfd *, struct bfd_link_info *);
33 static bool coff_link_check_archive_element
34 (bfd *, struct bfd_link_info *, struct bfd_link_hash_entry *, const char *,
35 bool *);
36 static bool coff_link_add_symbols (bfd *, struct bfd_link_info *);
38 /* Return TRUE if SYM is a weak, external symbol. */
39 #define IS_WEAK_EXTERNAL(abfd, sym) \
40 ((sym).n_sclass == C_WEAKEXT \
41 || (obj_pe (abfd) && (sym).n_sclass == C_NT_WEAK))
43 /* Return TRUE if SYM is an external symbol. */
44 #define IS_EXTERNAL(abfd, sym) \
45 ((sym).n_sclass == C_EXT || IS_WEAK_EXTERNAL (abfd, sym))
47 /* Define macros so that the ISFCN, et. al., macros work correctly.
48 These macros are defined in include/coff/internal.h in terms of
49 N_TMASK, etc. These definitions require a user to define local
50 variables with the appropriate names, and with values from the
51 coff_data (abfd) structure. */
53 #define N_TMASK n_tmask
54 #define N_BTSHFT n_btshft
55 #define N_BTMASK n_btmask
57 /* Create an entry in a COFF linker hash table. */
59 struct bfd_hash_entry *
60 _bfd_coff_link_hash_newfunc (struct bfd_hash_entry *entry,
61 struct bfd_hash_table *table,
62 const char *string)
64 struct coff_link_hash_entry *ret = (struct coff_link_hash_entry *) entry;
66 /* Allocate the structure if it has not already been allocated by a
67 subclass. */
68 if (ret == (struct coff_link_hash_entry *) NULL)
69 ret = ((struct coff_link_hash_entry *)
70 bfd_hash_allocate (table, sizeof (struct coff_link_hash_entry)));
71 if (ret == (struct coff_link_hash_entry *) NULL)
72 return (struct bfd_hash_entry *) ret;
74 /* Call the allocation method of the superclass. */
75 ret = ((struct coff_link_hash_entry *)
76 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
77 table, string));
78 if (ret != (struct coff_link_hash_entry *) NULL)
80 /* Set local fields. */
81 ret->indx = -1;
82 ret->type = T_NULL;
83 ret->symbol_class = C_NULL;
84 ret->numaux = 0;
85 ret->auxbfd = NULL;
86 ret->aux = NULL;
89 return (struct bfd_hash_entry *) ret;
92 struct bfd_hash_entry *
93 _decoration_hash_newfunc (struct bfd_hash_entry *entry,
94 struct bfd_hash_table *table,
95 const char *string)
97 struct decoration_hash_entry *ret = (struct decoration_hash_entry *) entry;
99 /* Allocate the structure if it has not already been allocated by a
100 subclass. */
101 if (ret == NULL)
103 ret = (struct decoration_hash_entry *)
104 bfd_hash_allocate (table, sizeof (struct decoration_hash_entry));
105 if (ret == NULL)
106 return NULL;
109 /* Call the allocation method of the superclass. */
110 ret = (struct decoration_hash_entry *)
111 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret, table, string);
112 if (ret != NULL)
114 ret->decorated_link = NULL;
117 return (struct bfd_hash_entry *) ret;
120 /* Initialize a COFF linker hash table. */
122 bool
123 _bfd_coff_link_hash_table_init (struct coff_link_hash_table *table,
124 bfd *abfd,
125 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
126 struct bfd_hash_table *,
127 const char *),
128 unsigned int entsize)
130 memset (&table->stab_info, 0, sizeof (table->stab_info));
132 return bfd_hash_table_init (&table->decoration_hash,
133 _decoration_hash_newfunc,
134 sizeof (struct decoration_hash_entry))
135 &&_bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
138 /* Create a COFF linker hash table. */
140 struct bfd_link_hash_table *
141 _bfd_coff_link_hash_table_create (bfd *abfd)
143 struct coff_link_hash_table *ret;
144 size_t amt = sizeof (struct coff_link_hash_table);
146 ret = (struct coff_link_hash_table *) bfd_malloc (amt);
147 if (ret == NULL)
148 return NULL;
150 if (! _bfd_coff_link_hash_table_init (ret, abfd,
151 _bfd_coff_link_hash_newfunc,
152 sizeof (struct coff_link_hash_entry)))
154 free (ret);
155 return (struct bfd_link_hash_table *) NULL;
157 return &ret->root;
160 /* Create an entry in a COFF debug merge hash table. */
162 struct bfd_hash_entry *
163 _bfd_coff_debug_merge_hash_newfunc (struct bfd_hash_entry *entry,
164 struct bfd_hash_table *table,
165 const char *string)
167 struct coff_debug_merge_hash_entry *ret =
168 (struct coff_debug_merge_hash_entry *) entry;
170 /* Allocate the structure if it has not already been allocated by a
171 subclass. */
172 if (ret == (struct coff_debug_merge_hash_entry *) NULL)
173 ret = ((struct coff_debug_merge_hash_entry *)
174 bfd_hash_allocate (table,
175 sizeof (struct coff_debug_merge_hash_entry)));
176 if (ret == (struct coff_debug_merge_hash_entry *) NULL)
177 return (struct bfd_hash_entry *) ret;
179 /* Call the allocation method of the superclass. */
180 ret = ((struct coff_debug_merge_hash_entry *)
181 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
182 if (ret != (struct coff_debug_merge_hash_entry *) NULL)
184 /* Set local fields. */
185 ret->types = NULL;
188 return (struct bfd_hash_entry *) ret;
191 /* Given a COFF BFD, add symbols to the global hash table as
192 appropriate. */
194 bool
195 _bfd_coff_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
197 switch (bfd_get_format (abfd))
199 case bfd_object:
200 return coff_link_add_object_symbols (abfd, info);
201 case bfd_archive:
202 return _bfd_generic_link_add_archive_symbols
203 (abfd, info, coff_link_check_archive_element);
204 default:
205 bfd_set_error (bfd_error_wrong_format);
206 return false;
210 /* Add symbols from a COFF object file. */
212 static bool
213 coff_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
215 if (! _bfd_coff_get_external_symbols (abfd))
216 return false;
217 if (! coff_link_add_symbols (abfd, info))
218 return false;
220 if (! info->keep_memory
221 && ! _bfd_coff_free_symbols (abfd))
222 return false;
224 return true;
227 /* Check a single archive element to see if we need to include it in
228 the link. *PNEEDED is set according to whether this element is
229 needed in the link or not. This is called via
230 _bfd_generic_link_add_archive_symbols. */
232 static bool
233 coff_link_check_archive_element (bfd *abfd,
234 struct bfd_link_info *info,
235 struct bfd_link_hash_entry *h,
236 const char *name,
237 bool *pneeded)
239 *pneeded = false;
241 /* PR 22369 - Skip non COFF objects in the archive. */
242 if (! bfd_family_coff (abfd))
243 return true;
245 /* We are only interested in symbols that are currently undefined.
246 If a symbol is currently known to be common, COFF linkers do not
247 bring in an object file which defines it. */
248 if (h->type != bfd_link_hash_undefined)
249 return true;
251 /* If the archive element has already been loaded then one
252 of the symbols defined by that element might have been
253 made undefined due to being in a discarded section. */
254 if (((struct coff_link_hash_entry *) h)->indx == -3)
255 return true;
257 /* Include this element? */
258 if (!(*info->callbacks->add_archive_element) (info, abfd, name, &abfd))
259 return true;
260 *pneeded = true;
262 return bfd_link_add_symbols (abfd, info);
265 /* Add all the symbols from an object file to the hash table. */
267 static bool
268 coff_link_add_symbols (bfd *abfd,
269 struct bfd_link_info *info)
271 unsigned int n_tmask = coff_data (abfd)->local_n_tmask;
272 unsigned int n_btshft = coff_data (abfd)->local_n_btshft;
273 unsigned int n_btmask = coff_data (abfd)->local_n_btmask;
274 bool keep_syms;
275 bool default_copy;
276 bfd_size_type symcount;
277 struct coff_link_hash_entry **sym_hash;
278 bfd_size_type symesz;
279 bfd_byte *esym;
280 bfd_byte *esym_end;
281 bfd_size_type amt;
283 symcount = obj_raw_syment_count (abfd);
285 if (symcount == 0)
286 return true; /* Nothing to do. */
288 /* Keep the symbols during this function, in case the linker needs
289 to read the generic symbols in order to report an error message. */
290 keep_syms = obj_coff_keep_syms (abfd);
291 obj_coff_keep_syms (abfd) = true;
293 if (info->keep_memory)
294 default_copy = false;
295 else
296 default_copy = true;
298 /* We keep a list of the linker hash table entries that correspond
299 to particular symbols. */
300 amt = symcount * sizeof (struct coff_link_hash_entry *);
301 sym_hash = (struct coff_link_hash_entry **) bfd_zalloc (abfd, amt);
302 if (sym_hash == NULL)
303 goto error_return;
304 obj_coff_sym_hashes (abfd) = sym_hash;
306 symesz = bfd_coff_symesz (abfd);
307 BFD_ASSERT (symesz == bfd_coff_auxesz (abfd));
308 esym = (bfd_byte *) obj_coff_external_syms (abfd);
309 esym_end = esym + symcount * symesz;
310 while (esym < esym_end)
312 struct internal_syment sym;
313 enum coff_symbol_classification classification;
314 bool copy;
316 bfd_coff_swap_sym_in (abfd, esym, &sym);
318 classification = bfd_coff_classify_symbol (abfd, &sym);
319 if (classification != COFF_SYMBOL_LOCAL)
321 const char *name;
322 char buf[SYMNMLEN + 1];
323 flagword flags;
324 asection *section;
325 bfd_vma value;
326 bool addit;
327 bool discarded = false;
329 /* This symbol is externally visible. */
331 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
332 if (name == NULL)
333 goto error_return;
335 /* We must copy the name into memory if we got it from the
336 syment itself, rather than the string table. */
337 copy = default_copy;
338 if (sym._n._n_n._n_zeroes != 0
339 || sym._n._n_n._n_offset == 0)
340 copy = true;
342 value = sym.n_value;
344 switch (classification)
346 default:
347 abort ();
349 case COFF_SYMBOL_GLOBAL:
350 flags = BSF_EXPORT | BSF_GLOBAL;
351 section = coff_section_from_bfd_index (abfd, sym.n_scnum);
352 if (discarded_section (section))
354 discarded = true;
355 section = bfd_und_section_ptr;
357 else if (! obj_pe (abfd))
358 value -= section->vma;
359 break;
361 case COFF_SYMBOL_UNDEFINED:
362 flags = 0;
363 section = bfd_und_section_ptr;
364 break;
366 case COFF_SYMBOL_COMMON:
367 flags = BSF_GLOBAL;
368 section = bfd_com_section_ptr;
369 break;
371 case COFF_SYMBOL_PE_SECTION:
372 flags = BSF_SECTION_SYM | BSF_GLOBAL;
373 section = coff_section_from_bfd_index (abfd, sym.n_scnum);
374 if (discarded_section (section))
375 section = bfd_und_section_ptr;
376 break;
379 if (IS_WEAK_EXTERNAL (abfd, sym))
380 flags = BSF_WEAK;
382 addit = true;
384 /* In the PE format, section symbols actually refer to the
385 start of the output section. We handle them specially
386 here. */
387 if (obj_pe (abfd) && (flags & BSF_SECTION_SYM) != 0)
389 *sym_hash = coff_link_hash_lookup (coff_hash_table (info),
390 name, false, copy, false);
391 if (*sym_hash != NULL)
393 if (((*sym_hash)->coff_link_hash_flags
394 & COFF_LINK_HASH_PE_SECTION_SYMBOL) == 0
395 && (*sym_hash)->root.type != bfd_link_hash_undefined
396 && (*sym_hash)->root.type != bfd_link_hash_undefweak)
397 _bfd_error_handler
398 (_("warning: symbol `%s' is both section and non-section"),
399 name);
401 addit = false;
405 /* The Microsoft Visual C compiler does string pooling by
406 hashing the constants to an internal symbol name, and
407 relying on the linker comdat support to discard
408 duplicate names. However, if one string is a literal and
409 one is a data initializer, one will end up in the .data
410 section and one will end up in the .rdata section. The
411 Microsoft linker will combine them into the .data
412 section, which seems to be wrong since it might cause the
413 literal to change.
415 As long as there are no external references to the
416 symbols, which there shouldn't be, we can treat the .data
417 and .rdata instances as separate symbols. The comdat
418 code in the linker will do the appropriate merging. Here
419 we avoid getting a multiple definition error for one of
420 these special symbols.
422 FIXME: I don't think this will work in the case where
423 there are two object files which use the constants as a
424 literal and two object files which use it as a data
425 initializer. One or the other of the second object files
426 is going to wind up with an inappropriate reference. */
427 if (obj_pe (abfd)
428 && (classification == COFF_SYMBOL_GLOBAL
429 || classification == COFF_SYMBOL_PE_SECTION)
430 && coff_section_data (abfd, section) != NULL
431 && coff_section_data (abfd, section)->comdat != NULL
432 && startswith (name, "??_")
433 && strcmp (name, coff_section_data (abfd, section)->comdat->name) == 0)
435 if (*sym_hash == NULL)
436 *sym_hash = coff_link_hash_lookup (coff_hash_table (info),
437 name, false, copy, false);
438 if (*sym_hash != NULL
439 && (*sym_hash)->root.type == bfd_link_hash_defined
440 && coff_section_data (abfd, (*sym_hash)->root.u.def.section)->comdat != NULL
441 && strcmp (coff_section_data (abfd, (*sym_hash)->root.u.def.section)->comdat->name,
442 coff_section_data (abfd, section)->comdat->name) == 0)
443 addit = false;
446 if (addit)
448 if (! (bfd_coff_link_add_one_symbol
449 (info, abfd, name, flags, section, value,
450 (const char *) NULL, copy, false,
451 (struct bfd_link_hash_entry **) sym_hash)))
452 goto error_return;
454 if (discarded)
455 (*sym_hash)->indx = -3;
458 if (obj_pe (abfd) && (flags & BSF_SECTION_SYM) != 0)
459 (*sym_hash)->coff_link_hash_flags |=
460 COFF_LINK_HASH_PE_SECTION_SYMBOL;
462 /* Limit the alignment of a common symbol to the possible
463 alignment of a section. There is no point to permitting
464 a higher alignment for a common symbol: we can not
465 guarantee it, and it may cause us to allocate extra space
466 in the common section. */
467 if (section == bfd_com_section_ptr
468 && (*sym_hash)->root.type == bfd_link_hash_common
469 && ((*sym_hash)->root.u.c.p->alignment_power
470 > bfd_coff_default_section_alignment_power (abfd)))
471 (*sym_hash)->root.u.c.p->alignment_power
472 = bfd_coff_default_section_alignment_power (abfd);
474 if (bfd_get_flavour (info->output_bfd) == bfd_get_flavour (abfd))
476 /* If we don't have any symbol information currently in
477 the hash table, or if we are looking at a symbol
478 definition, then update the symbol class and type in
479 the hash table. */
480 if (((*sym_hash)->symbol_class == C_NULL
481 && (*sym_hash)->type == T_NULL)
482 || sym.n_scnum != 0
483 || (sym.n_value != 0
484 && (*sym_hash)->root.type != bfd_link_hash_defined
485 && (*sym_hash)->root.type != bfd_link_hash_defweak))
487 (*sym_hash)->symbol_class = sym.n_sclass;
488 if (sym.n_type != T_NULL)
490 /* We want to warn if the type changed, but not
491 if it changed from an unspecified type.
492 Testing the whole type byte may work, but the
493 change from (e.g.) a function of unspecified
494 type to function of known type also wants to
495 skip the warning. */
496 if ((*sym_hash)->type != T_NULL
497 && (*sym_hash)->type != sym.n_type
498 && !(DTYPE ((*sym_hash)->type) == DTYPE (sym.n_type)
499 && (BTYPE ((*sym_hash)->type) == T_NULL
500 || BTYPE (sym.n_type) == T_NULL)))
501 _bfd_error_handler
502 /* xgettext: c-format */
503 (_("warning: type of symbol `%s' changed"
504 " from %d to %d in %pB"),
505 name, (*sym_hash)->type, sym.n_type, abfd);
507 /* We don't want to change from a meaningful
508 base type to a null one, but if we know
509 nothing, take what little we might now know. */
510 if (BTYPE (sym.n_type) != T_NULL
511 || (*sym_hash)->type == T_NULL)
512 (*sym_hash)->type = sym.n_type;
514 (*sym_hash)->auxbfd = abfd;
515 if (sym.n_numaux != 0)
517 union internal_auxent *alloc;
518 unsigned int i;
519 bfd_byte *eaux;
520 union internal_auxent *iaux;
522 (*sym_hash)->numaux = sym.n_numaux;
523 alloc = ((union internal_auxent *)
524 bfd_hash_allocate (&info->hash->table,
525 (sym.n_numaux
526 * sizeof (*alloc))));
527 if (alloc == NULL)
528 goto error_return;
529 for (i = 0, eaux = esym + symesz, iaux = alloc;
530 i < sym.n_numaux;
531 i++, eaux += symesz, iaux++)
532 bfd_coff_swap_aux_in (abfd, eaux, sym.n_type,
533 sym.n_sclass, (int) i,
534 sym.n_numaux, iaux);
535 (*sym_hash)->aux = alloc;
540 if (classification == COFF_SYMBOL_PE_SECTION
541 && (*sym_hash)->numaux != 0)
543 /* Some PE sections (such as .bss) have a zero size in
544 the section header, but a non-zero size in the AUX
545 record. Correct that here.
547 FIXME: This is not at all the right place to do this.
548 For example, it won't help objdump. This needs to be
549 done when we swap in the section header. */
550 BFD_ASSERT ((*sym_hash)->numaux == 1);
551 if (section->size == 0)
552 section->size = (*sym_hash)->aux[0].x_scn.x_scnlen;
554 /* FIXME: We could test whether the section sizes
555 matches the size in the aux entry, but apparently
556 that sometimes fails unexpectedly. */
560 esym += (sym.n_numaux + 1) * symesz;
561 sym_hash += sym.n_numaux + 1;
564 /* If this is a non-traditional, non-relocatable link, try to
565 optimize the handling of any .stab/.stabstr sections. */
566 if (! bfd_link_relocatable (info)
567 && ! info->traditional_format
568 && bfd_get_flavour (info->output_bfd) == bfd_get_flavour (abfd)
569 && (info->strip != strip_all && info->strip != strip_debugger))
571 asection *stabstr;
573 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
575 if (stabstr != NULL)
577 bfd_size_type string_offset = 0;
578 asection *stab;
580 for (stab = abfd->sections; stab; stab = stab->next)
581 if (startswith (stab->name, ".stab")
582 && (!stab->name[5]
583 || (stab->name[5] == '.' && ISDIGIT (stab->name[6]))))
585 struct coff_link_hash_table *table;
586 struct coff_section_tdata *secdata
587 = coff_section_data (abfd, stab);
589 if (secdata == NULL)
591 amt = sizeof (struct coff_section_tdata);
592 stab->used_by_bfd = bfd_zalloc (abfd, amt);
593 if (stab->used_by_bfd == NULL)
594 goto error_return;
595 secdata = coff_section_data (abfd, stab);
598 table = coff_hash_table (info);
600 if (! _bfd_link_section_stabs (abfd, &table->stab_info,
601 stab, stabstr,
602 &secdata->stab_info,
603 &string_offset))
604 goto error_return;
609 obj_coff_keep_syms (abfd) = keep_syms;
611 return true;
613 error_return:
614 obj_coff_keep_syms (abfd) = keep_syms;
615 return false;
618 /* Do the final link step. */
620 bool
621 _bfd_coff_final_link (bfd *abfd,
622 struct bfd_link_info *info)
624 bfd_size_type symesz;
625 struct coff_final_link_info flaginfo;
626 bool debug_merge_allocated;
627 bool long_section_names;
628 asection *o;
629 struct bfd_link_order *p;
630 bfd_size_type max_sym_count;
631 bfd_size_type max_lineno_count;
632 bfd_size_type max_reloc_count;
633 bfd_size_type max_output_reloc_count;
634 bfd_size_type max_contents_size;
635 file_ptr rel_filepos;
636 unsigned int relsz;
637 file_ptr line_filepos;
638 unsigned int linesz;
639 bfd *sub;
640 bfd_byte *external_relocs = NULL;
641 char strbuf[STRING_SIZE_SIZE];
642 bfd_size_type amt;
644 symesz = bfd_coff_symesz (abfd);
646 flaginfo.info = info;
647 flaginfo.output_bfd = abfd;
648 flaginfo.strtab = NULL;
649 flaginfo.section_info = NULL;
650 flaginfo.last_file_index = -1;
651 flaginfo.last_bf_index = -1;
652 flaginfo.internal_syms = NULL;
653 flaginfo.sec_ptrs = NULL;
654 flaginfo.sym_indices = NULL;
655 flaginfo.outsyms = NULL;
656 flaginfo.linenos = NULL;
657 flaginfo.contents = NULL;
658 flaginfo.external_relocs = NULL;
659 flaginfo.internal_relocs = NULL;
660 flaginfo.global_to_static = false;
661 debug_merge_allocated = false;
663 coff_data (abfd)->link_info = info;
665 flaginfo.strtab = _bfd_stringtab_init ();
666 if (flaginfo.strtab == NULL)
667 goto error_return;
669 if (! coff_debug_merge_hash_table_init (&flaginfo.debug_merge))
670 goto error_return;
671 debug_merge_allocated = true;
673 /* Compute the file positions for all the sections. */
674 if (! abfd->output_has_begun)
676 if (! bfd_coff_compute_section_file_positions (abfd))
677 goto error_return;
680 /* Count the line numbers and relocation entries required for the
681 output file. Set the file positions for the relocs. */
682 rel_filepos = obj_relocbase (abfd);
683 relsz = bfd_coff_relsz (abfd);
684 max_contents_size = 0;
685 max_lineno_count = 0;
686 max_reloc_count = 0;
688 long_section_names = false;
689 for (o = abfd->sections; o != NULL; o = o->next)
691 o->reloc_count = 0;
692 o->lineno_count = 0;
693 for (p = o->map_head.link_order; p != NULL; p = p->next)
695 if (p->type == bfd_indirect_link_order)
697 asection *sec;
699 sec = p->u.indirect.section;
701 /* Mark all sections which are to be included in the
702 link. This will normally be every section. We need
703 to do this so that we can identify any sections which
704 the linker has decided to not include. */
705 sec->linker_mark = true;
707 if (info->strip == strip_none
708 || info->strip == strip_some)
709 o->lineno_count += sec->lineno_count;
711 if (bfd_link_relocatable (info))
712 o->reloc_count += sec->reloc_count;
714 if (sec->rawsize > max_contents_size)
715 max_contents_size = sec->rawsize;
716 if (sec->size > max_contents_size)
717 max_contents_size = sec->size;
718 if (sec->lineno_count > max_lineno_count)
719 max_lineno_count = sec->lineno_count;
720 if (sec->reloc_count > max_reloc_count)
721 max_reloc_count = sec->reloc_count;
723 else if (bfd_link_relocatable (info)
724 && (p->type == bfd_section_reloc_link_order
725 || p->type == bfd_symbol_reloc_link_order))
726 ++o->reloc_count;
728 if (o->reloc_count == 0)
729 o->rel_filepos = 0;
730 else
732 o->flags |= SEC_RELOC;
733 o->rel_filepos = rel_filepos;
734 rel_filepos += o->reloc_count * relsz;
735 /* In PE COFF, if there are at least 0xffff relocations an
736 extra relocation will be written out to encode the count. */
737 if ((obj_pe (abfd) || obj_go32 (abfd)) && o->reloc_count >= 0xffff)
738 rel_filepos += relsz;
741 if (bfd_coff_long_section_names (abfd)
742 && strlen (o->name) > SCNNMLEN)
744 /* This section has a long name which must go in the string
745 table. This must correspond to the code in
746 coff_write_object_contents which puts the string index
747 into the s_name field of the section header. That is why
748 we pass hash as FALSE. */
749 if (_bfd_stringtab_add (flaginfo.strtab, o->name, false, false)
750 == (bfd_size_type) -1)
751 goto error_return;
752 long_section_names = true;
756 /* If doing a relocatable link, allocate space for the pointers we
757 need to keep. */
758 if (bfd_link_relocatable (info))
760 unsigned int i;
762 /* We use section_count + 1, rather than section_count, because
763 the target_index fields are 1 based. */
764 amt = abfd->section_count + 1;
765 amt *= sizeof (struct coff_link_section_info);
766 flaginfo.section_info = (struct coff_link_section_info *) bfd_malloc (amt);
767 if (flaginfo.section_info == NULL)
768 goto error_return;
769 for (i = 0; i <= abfd->section_count; i++)
771 flaginfo.section_info[i].relocs = NULL;
772 flaginfo.section_info[i].rel_hashes = NULL;
776 /* We now know the size of the relocs, so we can determine the file
777 positions of the line numbers. */
778 line_filepos = rel_filepos;
779 linesz = bfd_coff_linesz (abfd);
780 max_output_reloc_count = 0;
781 for (o = abfd->sections; o != NULL; o = o->next)
783 if (o->lineno_count == 0)
784 o->line_filepos = 0;
785 else
787 o->line_filepos = line_filepos;
788 line_filepos += o->lineno_count * linesz;
791 if (o->reloc_count != 0)
793 /* We don't know the indices of global symbols until we have
794 written out all the local symbols. For each section in
795 the output file, we keep an array of pointers to hash
796 table entries. Each entry in the array corresponds to a
797 reloc. When we find a reloc against a global symbol, we
798 set the corresponding entry in this array so that we can
799 fix up the symbol index after we have written out all the
800 local symbols.
802 Because of this problem, we also keep the relocs in
803 memory until the end of the link. This wastes memory,
804 but only when doing a relocatable link, which is not the
805 common case. */
806 BFD_ASSERT (bfd_link_relocatable (info));
807 amt = o->reloc_count;
808 amt *= sizeof (struct internal_reloc);
809 flaginfo.section_info[o->target_index].relocs =
810 (struct internal_reloc *) bfd_malloc (amt);
811 amt = o->reloc_count;
812 amt *= sizeof (struct coff_link_hash_entry *);
813 flaginfo.section_info[o->target_index].rel_hashes =
814 (struct coff_link_hash_entry **) bfd_malloc (amt);
815 if (flaginfo.section_info[o->target_index].relocs == NULL
816 || flaginfo.section_info[o->target_index].rel_hashes == NULL)
817 goto error_return;
819 if (o->reloc_count > max_output_reloc_count)
820 max_output_reloc_count = o->reloc_count;
823 /* Reset the reloc and lineno counts, so that we can use them to
824 count the number of entries we have output so far. */
825 o->reloc_count = 0;
826 o->lineno_count = 0;
829 obj_sym_filepos (abfd) = line_filepos;
831 /* Figure out the largest number of symbols in an input BFD. Take
832 the opportunity to clear the output_has_begun fields of all the
833 input BFD's. */
834 max_sym_count = 0;
835 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
837 size_t sz;
839 sub->output_has_begun = false;
840 sz = bfd_family_coff (sub) ? obj_raw_syment_count (sub) : 2;
841 if (sz > max_sym_count)
842 max_sym_count = sz;
845 /* Allocate some buffers used while linking. */
846 amt = max_sym_count * sizeof (struct internal_syment);
847 flaginfo.internal_syms = (struct internal_syment *) bfd_malloc (amt);
848 amt = max_sym_count * sizeof (asection *);
849 flaginfo.sec_ptrs = (asection **) bfd_malloc (amt);
850 amt = max_sym_count * sizeof (long);
851 flaginfo.sym_indices = (long int *) bfd_malloc (amt);
852 flaginfo.outsyms = (bfd_byte *) bfd_malloc ((max_sym_count + 1) * symesz);
853 amt = max_lineno_count * bfd_coff_linesz (abfd);
854 flaginfo.linenos = (bfd_byte *) bfd_malloc (amt);
855 flaginfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
856 amt = max_reloc_count * relsz;
857 flaginfo.external_relocs = (bfd_byte *) bfd_malloc (amt);
858 if (! bfd_link_relocatable (info))
860 amt = max_reloc_count * sizeof (struct internal_reloc);
861 flaginfo.internal_relocs = (struct internal_reloc *) bfd_malloc (amt);
863 if ((flaginfo.internal_syms == NULL && max_sym_count > 0)
864 || (flaginfo.sec_ptrs == NULL && max_sym_count > 0)
865 || (flaginfo.sym_indices == NULL && max_sym_count > 0)
866 || flaginfo.outsyms == NULL
867 || (flaginfo.linenos == NULL && max_lineno_count > 0)
868 || (flaginfo.contents == NULL && max_contents_size > 0)
869 || (flaginfo.external_relocs == NULL && max_reloc_count > 0)
870 || (! bfd_link_relocatable (info)
871 && flaginfo.internal_relocs == NULL
872 && max_reloc_count > 0))
873 goto error_return;
875 /* We now know the position of everything in the file, except that
876 we don't know the size of the symbol table and therefore we don't
877 know where the string table starts. We just build the string
878 table in memory as we go along. We process all the relocations
879 for a single input file at once. */
880 obj_raw_syment_count (abfd) = 0;
882 if (coff_backend_info (abfd)->_bfd_coff_start_final_link)
884 if (! bfd_coff_start_final_link (abfd, info))
885 goto error_return;
888 for (o = abfd->sections; o != NULL; o = o->next)
890 for (p = o->map_head.link_order; p != NULL; p = p->next)
892 if (p->type == bfd_indirect_link_order
893 && bfd_family_coff (p->u.indirect.section->owner))
895 sub = p->u.indirect.section->owner;
896 if (! bfd_coff_link_output_has_begun (sub, & flaginfo))
898 if (! _bfd_coff_link_input_bfd (&flaginfo, sub))
899 goto error_return;
900 sub->output_has_begun = true;
903 else if (p->type == bfd_section_reloc_link_order
904 || p->type == bfd_symbol_reloc_link_order)
906 if (! _bfd_coff_reloc_link_order (abfd, &flaginfo, o, p))
907 goto error_return;
909 else
911 if (! _bfd_default_link_order (abfd, info, o, p))
912 goto error_return;
917 if (flaginfo.info->strip != strip_all && flaginfo.info->discard != discard_all)
919 /* Add local symbols from foreign inputs. */
920 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
922 unsigned int i;
924 if (bfd_family_coff (sub) || ! bfd_get_outsymbols (sub))
925 continue;
926 for (i = 0; i < bfd_get_symcount (sub); ++i)
928 asymbol *sym = bfd_get_outsymbols (sub) [i];
929 file_ptr pos;
930 struct internal_syment isym;
931 bfd_vma written = 0;
932 bool rewrite = false;
934 if (! (sym->flags & BSF_LOCAL)
935 || (sym->flags & (BSF_SECTION_SYM | BSF_DEBUGGING_RELOC
936 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC
937 | BSF_SYNTHETIC))
938 || ((sym->flags & BSF_DEBUGGING)
939 && ! (sym->flags & BSF_FILE)))
940 continue;
942 /* See if we are discarding symbols with this name. */
943 if ((flaginfo.info->strip == strip_some
944 && (bfd_hash_lookup (flaginfo.info->keep_hash,
945 bfd_asymbol_name(sym), false, false)
946 == NULL))
947 || (((flaginfo.info->discard == discard_sec_merge
948 && (bfd_asymbol_section (sym)->flags & SEC_MERGE)
949 && ! bfd_link_relocatable (flaginfo.info))
950 || flaginfo.info->discard == discard_l)
951 && bfd_is_local_label_name (sub, bfd_asymbol_name(sym))))
952 continue;
954 pos = obj_sym_filepos (abfd) + obj_raw_syment_count (abfd)
955 * symesz;
956 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
957 goto error_return;
958 if (! coff_write_alien_symbol(abfd, sym, &isym, &written,
959 flaginfo.strtab,
960 !flaginfo.info->traditional_format,
961 NULL, NULL))
962 goto error_return;
964 if (isym.n_sclass == C_FILE)
966 if (flaginfo.last_file_index != -1)
968 flaginfo.last_file.n_value = obj_raw_syment_count (abfd);
969 bfd_coff_swap_sym_out (abfd, &flaginfo.last_file,
970 flaginfo.outsyms);
971 pos = obj_sym_filepos (abfd) + flaginfo.last_file_index
972 * symesz;
973 rewrite = true;
975 flaginfo.last_file_index = obj_raw_syment_count (abfd);
976 flaginfo.last_file = isym;
979 if (rewrite
980 && (bfd_seek (abfd, pos, SEEK_SET) != 0
981 || bfd_write (flaginfo.outsyms, symesz, abfd) != symesz))
982 goto error_return;
984 obj_raw_syment_count (abfd) += written;
989 if (! bfd_coff_final_link_postscript (abfd, & flaginfo))
990 goto error_return;
992 /* Free up the buffers used by _bfd_coff_link_input_bfd. */
994 coff_debug_merge_hash_table_free (&flaginfo.debug_merge);
995 debug_merge_allocated = false;
997 free (flaginfo.internal_syms);
998 flaginfo.internal_syms = NULL;
999 free (flaginfo.sec_ptrs);
1000 flaginfo.sec_ptrs = NULL;
1001 free (flaginfo.sym_indices);
1002 flaginfo.sym_indices = NULL;
1003 free (flaginfo.linenos);
1004 flaginfo.linenos = NULL;
1005 free (flaginfo.contents);
1006 flaginfo.contents = NULL;
1007 free (flaginfo.external_relocs);
1008 flaginfo.external_relocs = NULL;
1009 free (flaginfo.internal_relocs);
1010 flaginfo.internal_relocs = NULL;
1012 /* The value of the last C_FILE symbol is supposed to be the symbol
1013 index of the first external symbol. Write it out again if
1014 necessary. */
1015 if (flaginfo.last_file_index != -1
1016 && (unsigned int) flaginfo.last_file.n_value != obj_raw_syment_count (abfd))
1018 file_ptr pos;
1020 flaginfo.last_file.n_value = obj_raw_syment_count (abfd);
1021 bfd_coff_swap_sym_out (abfd, &flaginfo.last_file,
1022 flaginfo.outsyms);
1024 pos = obj_sym_filepos (abfd) + flaginfo.last_file_index * symesz;
1025 if (bfd_seek (abfd, pos, SEEK_SET) != 0
1026 || bfd_write (flaginfo.outsyms, symesz, abfd) != symesz)
1027 return false;
1030 /* If doing task linking (ld --task-link) then make a pass through the
1031 global symbols, writing out any that are defined, and making them
1032 static. */
1033 if (info->task_link)
1035 flaginfo.failed = false;
1036 coff_link_hash_traverse (coff_hash_table (info),
1037 _bfd_coff_write_task_globals, &flaginfo);
1038 if (flaginfo.failed)
1039 goto error_return;
1042 /* Write out the global symbols. */
1043 flaginfo.failed = false;
1044 bfd_hash_traverse (&info->hash->table, _bfd_coff_write_global_sym, &flaginfo);
1045 if (flaginfo.failed)
1046 goto error_return;
1048 /* The outsyms buffer is used by _bfd_coff_write_global_sym. */
1049 free (flaginfo.outsyms);
1050 flaginfo.outsyms = NULL;
1052 if (bfd_link_relocatable (info) && max_output_reloc_count > 0)
1054 /* Now that we have written out all the global symbols, we know
1055 the symbol indices to use for relocs against them, and we can
1056 finally write out the relocs. */
1057 amt = max_output_reloc_count * relsz;
1058 external_relocs = (bfd_byte *) bfd_malloc (amt);
1059 if (external_relocs == NULL)
1060 goto error_return;
1062 for (o = abfd->sections; o != NULL; o = o->next)
1064 struct internal_reloc *irel;
1065 struct internal_reloc *irelend;
1066 struct coff_link_hash_entry **rel_hash;
1067 bfd_byte *erel;
1069 if (o->reloc_count == 0)
1070 continue;
1072 irel = flaginfo.section_info[o->target_index].relocs;
1073 irelend = irel + o->reloc_count;
1074 rel_hash = flaginfo.section_info[o->target_index].rel_hashes;
1075 erel = external_relocs;
1076 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
1078 if (*rel_hash != NULL)
1080 BFD_ASSERT ((*rel_hash)->indx >= 0);
1081 irel->r_symndx = (*rel_hash)->indx;
1083 bfd_coff_swap_reloc_out (abfd, irel, erel);
1086 if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0)
1087 goto error_return;
1088 if ((obj_pe (abfd) || obj_go32 (abfd)) && o->reloc_count >= 0xffff)
1090 /* In PE COFF, write the count of relocs as the first
1091 reloc. The header overflow bit will be set
1092 elsewhere. */
1093 struct internal_reloc incount;
1094 bfd_byte *excount = (bfd_byte *)bfd_malloc (relsz);
1096 memset (&incount, 0, sizeof (incount));
1097 incount.r_vaddr = o->reloc_count + 1;
1098 bfd_coff_swap_reloc_out (abfd, &incount, excount);
1099 if (bfd_write (excount, relsz, abfd) != relsz)
1100 /* We'll leak, but it's an error anyway. */
1101 goto error_return;
1102 free (excount);
1104 if (bfd_write (external_relocs,
1105 (bfd_size_type) relsz * o->reloc_count, abfd)
1106 != (bfd_size_type) relsz * o->reloc_count)
1107 goto error_return;
1110 free (external_relocs);
1111 external_relocs = NULL;
1114 /* Free up the section information. */
1115 if (flaginfo.section_info != NULL)
1117 unsigned int i;
1119 for (i = 0; i < abfd->section_count; i++)
1121 free (flaginfo.section_info[i].relocs);
1122 free (flaginfo.section_info[i].rel_hashes);
1124 free (flaginfo.section_info);
1125 flaginfo.section_info = NULL;
1128 /* If we have optimized stabs strings, output them. */
1129 if (coff_hash_table (info)->stab_info.stabstr != NULL)
1131 if (! _bfd_write_stab_strings (abfd, &coff_hash_table (info)->stab_info))
1132 return false;
1135 /* Write out the string table. */
1136 if (obj_raw_syment_count (abfd) != 0 || long_section_names)
1138 file_ptr pos;
1140 pos = obj_sym_filepos (abfd) + obj_raw_syment_count (abfd) * symesz;
1141 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
1142 return false;
1144 #if STRING_SIZE_SIZE == 4
1145 H_PUT_32 (abfd,
1146 _bfd_stringtab_size (flaginfo.strtab) + STRING_SIZE_SIZE,
1147 strbuf);
1148 #else
1149 #error Change H_PUT_32 above
1150 #endif
1152 if (bfd_write (strbuf, STRING_SIZE_SIZE, abfd) != STRING_SIZE_SIZE)
1153 return false;
1155 if (! _bfd_stringtab_emit (abfd, flaginfo.strtab))
1156 return false;
1158 obj_coff_strings_written (abfd) = true;
1161 _bfd_stringtab_free (flaginfo.strtab);
1163 /* Setting symcount to 0 will cause write_object_contents to
1164 not try to write out the symbols. */
1165 abfd->symcount = 0;
1167 return true;
1169 error_return:
1170 if (debug_merge_allocated)
1171 coff_debug_merge_hash_table_free (&flaginfo.debug_merge);
1172 if (flaginfo.strtab != NULL)
1173 _bfd_stringtab_free (flaginfo.strtab);
1174 if (flaginfo.section_info != NULL)
1176 unsigned int i;
1178 for (i = 0; i < abfd->section_count; i++)
1180 free (flaginfo.section_info[i].relocs);
1181 free (flaginfo.section_info[i].rel_hashes);
1183 free (flaginfo.section_info);
1185 free (flaginfo.internal_syms);
1186 free (flaginfo.sec_ptrs);
1187 free (flaginfo.sym_indices);
1188 free (flaginfo.outsyms);
1189 free (flaginfo.linenos);
1190 free (flaginfo.contents);
1191 free (flaginfo.external_relocs);
1192 free (flaginfo.internal_relocs);
1193 free (external_relocs);
1194 return false;
1197 /* Parse out a -heap <reserved>,<commit> line. */
1199 static char *
1200 dores_com (char *ptr, bfd *output_bfd, int heap)
1202 if (obj_pe (output_bfd))
1204 int val = strtoul (ptr, &ptr, 0);
1206 if (heap)
1207 pe_data(output_bfd)->pe_opthdr.SizeOfHeapReserve = val;
1208 else
1209 pe_data(output_bfd)->pe_opthdr.SizeOfStackReserve = val;
1211 if (ptr[0] == ',')
1213 val = strtoul (ptr+1, &ptr, 0);
1214 if (heap)
1215 pe_data(output_bfd)->pe_opthdr.SizeOfHeapCommit = val;
1216 else
1217 pe_data(output_bfd)->pe_opthdr.SizeOfStackCommit = val;
1220 return ptr;
1223 static char *
1224 get_name (char *ptr, char **dst)
1226 while (*ptr == ' ')
1227 ptr++;
1228 *dst = ptr;
1229 while (*ptr && *ptr != ' ')
1230 ptr++;
1231 *ptr = 0;
1232 return ptr+1;
1235 /* Process any magic embedded commands in a section called .drectve. */
1237 static int
1238 process_embedded_commands (bfd *output_bfd,
1239 struct bfd_link_info *info ATTRIBUTE_UNUSED,
1240 bfd *abfd)
1242 asection *sec = bfd_get_section_by_name (abfd, ".drectve");
1243 char *s;
1244 char *e;
1245 bfd_byte *copy;
1247 if (sec == NULL || (sec->flags & SEC_HAS_CONTENTS) == 0)
1248 return 1;
1250 if (!bfd_malloc_and_get_section (abfd, sec, &copy))
1252 free (copy);
1253 return 0;
1255 e = (char *) copy + sec->size;
1257 for (s = (char *) copy; s < e ; )
1259 if (s[0] != '-')
1261 s++;
1262 continue;
1264 if (startswith (s, "-attr"))
1266 char *name;
1267 char *attribs;
1268 asection *asec;
1269 int loop = 1;
1270 int had_write = 0;
1271 int had_exec= 0;
1273 s += 5;
1274 s = get_name (s, &name);
1275 s = get_name (s, &attribs);
1277 while (loop)
1279 switch (*attribs++)
1281 case 'W':
1282 had_write = 1;
1283 break;
1284 case 'R':
1285 break;
1286 case 'S':
1287 break;
1288 case 'X':
1289 had_exec = 1;
1290 break;
1291 default:
1292 loop = 0;
1295 asec = bfd_get_section_by_name (abfd, name);
1296 if (asec)
1298 if (had_exec)
1299 asec->flags |= SEC_CODE;
1300 if (!had_write)
1301 asec->flags |= SEC_READONLY;
1304 else if (startswith (s, "-heap"))
1305 s = dores_com (s + 5, output_bfd, 1);
1307 else if (startswith (s, "-stack"))
1308 s = dores_com (s + 6, output_bfd, 0);
1310 /* GNU extension for aligned commons. */
1311 else if (startswith (s, "-aligncomm:"))
1313 /* Common symbols must be aligned on reading, as it
1314 is too late to do anything here, after they have
1315 already been allocated, so just skip the directive. */
1316 s += 11;
1319 else
1320 s++;
1322 free (copy);
1323 return 1;
1326 /* Place a marker against all symbols which are used by relocations.
1327 This marker can be picked up by the 'do we skip this symbol ?'
1328 loop in _bfd_coff_link_input_bfd() and used to prevent skipping
1329 that symbol. */
1331 static void
1332 mark_relocs (struct coff_final_link_info *flaginfo, bfd *input_bfd)
1334 asection * a;
1336 if ((bfd_get_file_flags (input_bfd) & HAS_SYMS) == 0)
1337 return;
1339 for (a = input_bfd->sections; a != (asection *) NULL; a = a->next)
1341 struct internal_reloc * internal_relocs;
1342 struct internal_reloc * irel;
1343 struct internal_reloc * irelend;
1345 if ((a->flags & SEC_RELOC) == 0 || a->reloc_count < 1
1346 || a->linker_mark == 0)
1347 continue;
1348 /* Don't mark relocs in excluded sections. */
1349 if (a->output_section == bfd_abs_section_ptr)
1350 continue;
1352 /* Read in the relocs. */
1353 internal_relocs = _bfd_coff_read_internal_relocs
1354 (input_bfd, a, false,
1355 flaginfo->external_relocs,
1356 bfd_link_relocatable (flaginfo->info),
1357 (bfd_link_relocatable (flaginfo->info)
1358 ? (flaginfo->section_info[ a->output_section->target_index ].relocs + a->output_section->reloc_count)
1359 : flaginfo->internal_relocs)
1362 if (internal_relocs == NULL)
1363 continue;
1365 irel = internal_relocs;
1366 irelend = irel + a->reloc_count;
1368 /* Place a mark in the sym_indices array (whose entries have
1369 been initialised to 0) for all of the symbols that are used
1370 in the relocation table. This will then be picked up in the
1371 skip/don't-skip pass. */
1372 for (; irel < irelend; irel++)
1373 if ((unsigned long) irel->r_symndx < obj_raw_syment_count (input_bfd))
1374 flaginfo->sym_indices[irel->r_symndx] = -1;
1378 /* Link an input file into the linker output file. This function
1379 handles all the sections and relocations of the input file at once. */
1381 bool
1382 _bfd_coff_link_input_bfd (struct coff_final_link_info *flaginfo, bfd *input_bfd)
1384 unsigned int n_tmask = coff_data (input_bfd)->local_n_tmask;
1385 unsigned int n_btshft = coff_data (input_bfd)->local_n_btshft;
1386 bool (*adjust_symndx)
1387 (bfd *, struct bfd_link_info *, bfd *, asection *,
1388 struct internal_reloc *, bool *);
1389 bfd *output_bfd;
1390 const char *strings;
1391 bfd_size_type syment_base;
1392 bool copy, hash;
1393 bfd_size_type isymesz;
1394 bfd_size_type osymesz;
1395 bfd_size_type linesz;
1396 bfd_byte *esym;
1397 bfd_byte *esym_end;
1398 struct internal_syment *isymp;
1399 asection **secpp;
1400 long *indexp;
1401 unsigned long output_index;
1402 bfd_byte *outsym;
1403 struct coff_link_hash_entry **sym_hash;
1404 asection *o;
1406 /* Move all the symbols to the output file. */
1408 output_bfd = flaginfo->output_bfd;
1409 strings = NULL;
1410 syment_base = obj_raw_syment_count (output_bfd);
1411 isymesz = bfd_coff_symesz (input_bfd);
1412 osymesz = bfd_coff_symesz (output_bfd);
1413 linesz = bfd_coff_linesz (input_bfd);
1414 BFD_ASSERT (linesz == bfd_coff_linesz (output_bfd));
1416 copy = false;
1417 if (! flaginfo->info->keep_memory)
1418 copy = true;
1419 hash = true;
1420 if (flaginfo->info->traditional_format)
1421 hash = false;
1423 if (! _bfd_coff_get_external_symbols (input_bfd))
1424 return false;
1426 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
1427 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
1428 isymp = flaginfo->internal_syms;
1429 secpp = flaginfo->sec_ptrs;
1430 indexp = flaginfo->sym_indices;
1431 output_index = syment_base;
1432 outsym = flaginfo->outsyms;
1434 if (obj_pe (output_bfd)
1435 && ! process_embedded_commands (output_bfd, flaginfo->info, input_bfd))
1436 return false;
1438 /* If we are going to perform relocations and also strip/discard some
1439 symbols then we must make sure that we do not strip/discard those
1440 symbols that are going to be involved in the relocations. */
1441 if (( flaginfo->info->strip != strip_none
1442 || flaginfo->info->discard != discard_none)
1443 && bfd_link_relocatable (flaginfo->info))
1445 /* Mark the symbol array as 'not-used'. */
1446 memset (indexp, 0, obj_raw_syment_count (input_bfd) * sizeof * indexp);
1448 mark_relocs (flaginfo, input_bfd);
1451 while (esym < esym_end)
1453 struct internal_syment isym;
1454 enum coff_symbol_classification classification;
1455 bool skip;
1456 bool global;
1457 bool dont_skip_symbol;
1458 int add;
1460 bfd_coff_swap_sym_in (input_bfd, esym, isymp);
1462 /* Make a copy of *isymp so that the relocate_section function
1463 always sees the original values. This is more reliable than
1464 always recomputing the symbol value even if we are stripping
1465 the symbol. */
1466 isym = *isymp;
1468 classification = bfd_coff_classify_symbol (input_bfd, &isym);
1469 switch (classification)
1471 default:
1472 abort ();
1473 case COFF_SYMBOL_GLOBAL:
1474 case COFF_SYMBOL_PE_SECTION:
1475 case COFF_SYMBOL_LOCAL:
1476 *secpp = coff_section_from_bfd_index (input_bfd, isym.n_scnum);
1477 break;
1478 case COFF_SYMBOL_COMMON:
1479 *secpp = bfd_com_section_ptr;
1480 break;
1481 case COFF_SYMBOL_UNDEFINED:
1482 *secpp = bfd_und_section_ptr;
1483 break;
1486 /* Extract the flag indicating if this symbol is used by a
1487 relocation. */
1488 if ((flaginfo->info->strip != strip_none
1489 || flaginfo->info->discard != discard_none)
1490 && bfd_link_relocatable (flaginfo->info))
1491 dont_skip_symbol = *indexp;
1492 else
1493 dont_skip_symbol = false;
1495 *indexp = -1;
1497 skip = false;
1498 global = false;
1499 add = 1 + isym.n_numaux;
1501 /* If we are stripping all symbols, we want to skip this one. */
1502 if (flaginfo->info->strip == strip_all && ! dont_skip_symbol)
1503 skip = true;
1505 if (! skip)
1507 switch (classification)
1509 default:
1510 abort ();
1511 case COFF_SYMBOL_GLOBAL:
1512 case COFF_SYMBOL_COMMON:
1513 case COFF_SYMBOL_PE_SECTION:
1514 /* This is a global symbol. Global symbols come at the
1515 end of the symbol table, so skip them for now.
1516 Locally defined function symbols, however, are an
1517 exception, and are not moved to the end. */
1518 global = true;
1519 if (! ISFCN (isym.n_type))
1520 skip = true;
1521 break;
1523 case COFF_SYMBOL_UNDEFINED:
1524 /* Undefined symbols are left for the end. */
1525 global = true;
1526 skip = true;
1527 break;
1529 case COFF_SYMBOL_LOCAL:
1530 /* This is a local symbol. Skip it if we are discarding
1531 local symbols. */
1532 if (flaginfo->info->discard == discard_all && ! dont_skip_symbol)
1533 skip = true;
1534 break;
1538 #ifndef COFF_WITH_PE
1539 /* Skip section symbols for sections which are not going to be
1540 emitted. */
1541 if (!skip
1542 && !dont_skip_symbol
1543 && isym.n_sclass == C_STAT
1544 && isym.n_type == T_NULL
1545 && isym.n_numaux > 0
1546 && ((*secpp)->output_section == bfd_abs_section_ptr
1547 || bfd_section_removed_from_list (output_bfd,
1548 (*secpp)->output_section)))
1549 skip = true;
1550 #endif
1552 /* If we stripping debugging symbols, and this is a debugging
1553 symbol, then skip it. FIXME: gas sets the section to N_ABS
1554 for some types of debugging symbols; I don't know if this is
1555 a bug or not. In any case, we handle it here. */
1556 if (! skip
1557 && flaginfo->info->strip == strip_debugger
1558 && ! dont_skip_symbol
1559 && (isym.n_scnum == N_DEBUG
1560 || (isym.n_scnum == N_ABS
1561 && (isym.n_sclass == C_AUTO
1562 || isym.n_sclass == C_REG
1563 || isym.n_sclass == C_MOS
1564 || isym.n_sclass == C_MOE
1565 || isym.n_sclass == C_MOU
1566 || isym.n_sclass == C_ARG
1567 || isym.n_sclass == C_REGPARM
1568 || isym.n_sclass == C_FIELD
1569 || isym.n_sclass == C_EOS))))
1570 skip = true;
1572 /* If some symbols are stripped based on the name, work out the
1573 name and decide whether to skip this symbol. */
1574 if (! skip
1575 && (flaginfo->info->strip == strip_some
1576 || flaginfo->info->discard == discard_l))
1578 const char *name;
1579 char buf[SYMNMLEN + 1];
1581 name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf);
1582 if (name == NULL)
1583 return false;
1585 if (! dont_skip_symbol
1586 && ((flaginfo->info->strip == strip_some
1587 && (bfd_hash_lookup (flaginfo->info->keep_hash, name, false,
1588 false) == NULL))
1589 || (! global
1590 && flaginfo->info->discard == discard_l
1591 && bfd_is_local_label_name (input_bfd, name))))
1592 skip = true;
1595 /* If this is an enum, struct, or union tag, see if we have
1596 already output an identical type. */
1597 if (! skip
1598 && !flaginfo->info->traditional_format
1599 && (isym.n_sclass == C_ENTAG
1600 || isym.n_sclass == C_STRTAG
1601 || isym.n_sclass == C_UNTAG)
1602 && isym.n_numaux == 1)
1604 const char *name;
1605 char buf[SYMNMLEN + 1];
1606 struct coff_debug_merge_hash_entry *mh;
1607 struct coff_debug_merge_type *mt;
1608 union internal_auxent aux;
1609 struct coff_debug_merge_element **epp;
1610 bfd_byte *esl, *eslend;
1611 struct internal_syment *islp;
1612 size_t amt;
1614 name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf);
1615 if (name == NULL)
1616 return false;
1618 /* Ignore fake names invented by compiler; treat them all as
1619 the same name. */
1620 if (*name == '~' || *name == '.' || *name == '$'
1621 || (*name
1622 && *name == bfd_get_symbol_leading_char (input_bfd)
1623 && (name[1] == '~' || name[1] == '.' || name[1] == '$')))
1624 name = "";
1626 mh = coff_debug_merge_hash_lookup (&flaginfo->debug_merge, name,
1627 true, true);
1628 if (mh == NULL)
1629 return false;
1631 /* Allocate memory to hold type information. If this turns
1632 out to be a duplicate, we pass this address to
1633 bfd_release. */
1634 amt = sizeof (struct coff_debug_merge_type);
1635 mt = (struct coff_debug_merge_type *) bfd_alloc (input_bfd, amt);
1636 if (mt == NULL)
1637 return false;
1638 mt->type_class = isym.n_sclass;
1640 /* Pick up the aux entry, which points to the end of the tag
1641 entries. */
1642 bfd_coff_swap_aux_in (input_bfd, (esym + isymesz),
1643 isym.n_type, isym.n_sclass, 0, isym.n_numaux,
1644 &aux);
1646 /* Gather the elements. */
1647 epp = &mt->elements;
1648 mt->elements = NULL;
1649 islp = isymp + 2;
1650 esl = esym + 2 * isymesz;
1651 eslend = ((bfd_byte *) obj_coff_external_syms (input_bfd)
1652 + aux.x_sym.x_fcnary.x_fcn.x_endndx.u32 * isymesz);
1653 while (esl < eslend)
1655 const char *elename;
1656 char elebuf[SYMNMLEN + 1];
1657 char *name_copy;
1659 bfd_coff_swap_sym_in (input_bfd, esl, islp);
1661 amt = sizeof (struct coff_debug_merge_element);
1662 *epp = (struct coff_debug_merge_element *)
1663 bfd_alloc (input_bfd, amt);
1664 if (*epp == NULL)
1665 return false;
1667 elename = _bfd_coff_internal_syment_name (input_bfd, islp,
1668 elebuf);
1669 if (elename == NULL)
1670 return false;
1672 amt = strlen (elename) + 1;
1673 name_copy = (char *) bfd_alloc (input_bfd, amt);
1674 if (name_copy == NULL)
1675 return false;
1676 strcpy (name_copy, elename);
1678 (*epp)->name = name_copy;
1679 (*epp)->type = islp->n_type;
1680 (*epp)->tagndx = 0;
1681 if (islp->n_numaux >= 1
1682 && islp->n_type != T_NULL
1683 && islp->n_sclass != C_EOS)
1685 union internal_auxent eleaux;
1686 long indx;
1688 bfd_coff_swap_aux_in (input_bfd, (esl + isymesz),
1689 islp->n_type, islp->n_sclass, 0,
1690 islp->n_numaux, &eleaux);
1691 indx = eleaux.x_sym.x_tagndx.u32;
1693 /* FIXME: If this tagndx entry refers to a symbol
1694 defined later in this file, we just ignore it.
1695 Handling this correctly would be tedious, and may
1696 not be required. */
1697 if (indx > 0
1698 && (indx
1699 < ((esym -
1700 (bfd_byte *) obj_coff_external_syms (input_bfd))
1701 / (long) isymesz)))
1703 (*epp)->tagndx = flaginfo->sym_indices[indx];
1704 if ((*epp)->tagndx < 0)
1705 (*epp)->tagndx = 0;
1708 epp = &(*epp)->next;
1709 *epp = NULL;
1711 esl += (islp->n_numaux + 1) * isymesz;
1712 islp += islp->n_numaux + 1;
1715 /* See if we already have a definition which matches this
1716 type. We always output the type if it has no elements,
1717 for simplicity. */
1718 if (mt->elements == NULL)
1719 bfd_release (input_bfd, mt);
1720 else
1722 struct coff_debug_merge_type *mtl;
1724 for (mtl = mh->types; mtl != NULL; mtl = mtl->next)
1726 struct coff_debug_merge_element *me, *mel;
1728 if (mtl->type_class != mt->type_class)
1729 continue;
1731 for (me = mt->elements, mel = mtl->elements;
1732 me != NULL && mel != NULL;
1733 me = me->next, mel = mel->next)
1735 if (strcmp (me->name, mel->name) != 0
1736 || me->type != mel->type
1737 || me->tagndx != mel->tagndx)
1738 break;
1741 if (me == NULL && mel == NULL)
1742 break;
1745 if (mtl == NULL || (bfd_size_type) mtl->indx >= syment_base)
1747 /* This is the first definition of this type. */
1748 mt->indx = output_index;
1749 mt->next = mh->types;
1750 mh->types = mt;
1752 else
1754 /* This is a redefinition which can be merged. */
1755 bfd_release (input_bfd, mt);
1756 *indexp = mtl->indx;
1757 add = (eslend - esym) / isymesz;
1758 skip = true;
1763 /* We now know whether we are to skip this symbol or not. */
1764 if (! skip)
1766 /* Adjust the symbol in order to output it. */
1768 if (isym._n._n_n._n_zeroes == 0
1769 && isym._n._n_n._n_offset != 0)
1771 const char *name;
1772 bfd_size_type indx;
1774 /* This symbol has a long name. Enter it in the string
1775 table we are building. Note that we do not check
1776 bfd_coff_symname_in_debug. That is only true for
1777 XCOFF, and XCOFF requires different linking code
1778 anyhow. */
1779 name = _bfd_coff_internal_syment_name (input_bfd, &isym, NULL);
1780 if (name == NULL)
1781 return false;
1782 indx = _bfd_stringtab_add (flaginfo->strtab, name, hash, copy);
1783 if (indx == (bfd_size_type) -1)
1784 return false;
1785 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
1788 switch (isym.n_sclass)
1790 case C_AUTO:
1791 case C_MOS:
1792 case C_EOS:
1793 case C_MOE:
1794 case C_MOU:
1795 case C_UNTAG:
1796 case C_STRTAG:
1797 case C_ENTAG:
1798 case C_TPDEF:
1799 case C_ARG:
1800 case C_USTATIC:
1801 case C_REG:
1802 case C_REGPARM:
1803 case C_FIELD:
1804 /* The symbol value should not be modified. */
1805 break;
1807 case C_FCN:
1808 if (obj_pe (input_bfd)
1809 && memcmp (isym.n_name, ".bf", sizeof ".bf") != 0
1810 && isym.n_scnum > 0)
1812 /* For PE, .lf and .ef get their value left alone,
1813 while .bf gets relocated. However, they all have
1814 "real" section numbers, and need to be moved into
1815 the new section. */
1816 isym.n_scnum = (*secpp)->output_section->target_index;
1817 break;
1819 /* Fall through. */
1820 default:
1821 case C_LABEL: /* Not completely sure about these 2 */
1822 case C_EXTDEF:
1823 case C_BLOCK:
1824 case C_EFCN:
1825 case C_NULL:
1826 case C_EXT:
1827 case C_STAT:
1828 case C_SECTION:
1829 case C_NT_WEAK:
1830 /* Compute new symbol location. */
1831 if (isym.n_scnum > 0)
1833 isym.n_scnum = (*secpp)->output_section->target_index;
1834 isym.n_value += (*secpp)->output_offset;
1835 if (! obj_pe (input_bfd))
1836 isym.n_value -= (*secpp)->vma;
1837 if (! obj_pe (flaginfo->output_bfd))
1838 isym.n_value += (*secpp)->output_section->vma;
1840 break;
1842 case C_FILE:
1843 /* The value of a C_FILE symbol is the symbol index of
1844 the next C_FILE symbol. The value of the last C_FILE
1845 symbol is the symbol index to the first external
1846 symbol (actually, coff_renumber_symbols does not get
1847 this right--it just sets the value of the last C_FILE
1848 symbol to zero--and nobody has ever complained about
1849 it). We try to get this right, below, just before we
1850 write the symbols out, but in the general case we may
1851 have to write the symbol out twice. */
1852 if (flaginfo->last_file_index != -1
1853 && flaginfo->last_file.n_value != (bfd_vma) output_index)
1855 /* We must correct the value of the last C_FILE
1856 entry. */
1857 flaginfo->last_file.n_value = output_index;
1858 if ((bfd_size_type) flaginfo->last_file_index >= syment_base)
1860 /* The last C_FILE symbol is in this input file. */
1861 bfd_coff_swap_sym_out (output_bfd,
1862 &flaginfo->last_file,
1863 (flaginfo->outsyms
1864 + ((flaginfo->last_file_index
1865 - syment_base)
1866 * osymesz)));
1868 else
1870 file_ptr pos;
1872 /* We have already written out the last C_FILE
1873 symbol. We need to write it out again. We
1874 borrow *outsym temporarily. */
1875 bfd_coff_swap_sym_out (output_bfd,
1876 &flaginfo->last_file, outsym);
1877 pos = obj_sym_filepos (output_bfd);
1878 pos += flaginfo->last_file_index * osymesz;
1879 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
1880 || bfd_write (outsym, osymesz, output_bfd) != osymesz)
1881 return false;
1885 flaginfo->last_file_index = output_index;
1886 flaginfo->last_file = isym;
1887 break;
1890 /* If doing task linking, convert normal global function symbols to
1891 static functions. */
1892 if (flaginfo->info->task_link && IS_EXTERNAL (input_bfd, isym))
1893 isym.n_sclass = C_STAT;
1895 /* Output the symbol. */
1896 bfd_coff_swap_sym_out (output_bfd, &isym, outsym);
1898 *indexp = output_index;
1900 if (global)
1902 long indx;
1903 struct coff_link_hash_entry *h;
1905 indx = ((esym - (bfd_byte *) obj_coff_external_syms (input_bfd))
1906 / isymesz);
1907 h = obj_coff_sym_hashes (input_bfd)[indx];
1908 if (h == NULL)
1910 /* This can happen if there were errors earlier in
1911 the link. */
1912 bfd_set_error (bfd_error_bad_value);
1913 return false;
1915 h->indx = output_index;
1918 output_index += add;
1919 outsym += add * osymesz;
1922 esym += add * isymesz;
1923 isymp += add;
1924 ++secpp;
1925 ++indexp;
1926 for (--add; add > 0; --add)
1928 *secpp++ = NULL;
1929 *indexp++ = -1;
1933 /* Fix up the aux entries. This must be done in a separate pass,
1934 because we don't know the correct symbol indices until we have
1935 already decided which symbols we are going to keep. */
1936 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
1937 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
1938 isymp = flaginfo->internal_syms;
1939 indexp = flaginfo->sym_indices;
1940 sym_hash = obj_coff_sym_hashes (input_bfd);
1941 outsym = flaginfo->outsyms;
1943 while (esym < esym_end)
1945 int add;
1947 add = 1 + isymp->n_numaux;
1949 if ((*indexp < 0
1950 || (bfd_size_type) *indexp < syment_base)
1951 && (*sym_hash == NULL
1952 || (*sym_hash)->auxbfd != input_bfd))
1953 esym += add * isymesz;
1954 else
1956 struct coff_link_hash_entry *h;
1957 int i;
1959 h = NULL;
1960 if (*indexp < 0)
1962 h = *sym_hash;
1964 /* The m68k-motorola-sysv assembler will sometimes
1965 generate two symbols with the same name, but only one
1966 will have aux entries. */
1967 BFD_ASSERT (isymp->n_numaux == 0
1968 || h->numaux == 0
1969 || h->numaux == isymp->n_numaux);
1972 esym += isymesz;
1974 if (h == NULL)
1975 outsym += osymesz;
1977 /* Handle the aux entries. This handling is based on
1978 coff_pointerize_aux. I don't know if it always correct. */
1979 for (i = 0; i < isymp->n_numaux && esym < esym_end; i++)
1981 union internal_auxent aux;
1982 union internal_auxent *auxp;
1984 if (h != NULL && h->aux != NULL && (h->numaux > i))
1985 auxp = h->aux + i;
1986 else
1988 bfd_coff_swap_aux_in (input_bfd, esym, isymp->n_type,
1989 isymp->n_sclass, i, isymp->n_numaux, &aux);
1990 auxp = &aux;
1993 if (isymp->n_sclass == C_FILE)
1995 /* If this is a long filename, we must put it in the
1996 string table. */
1997 if (auxp->x_file.x_n.x_n.x_zeroes == 0
1998 && auxp->x_file.x_n.x_n.x_offset != 0)
2000 const char *filename;
2001 bfd_size_type indx;
2003 BFD_ASSERT (auxp->x_file.x_n.x_n.x_offset
2004 >= STRING_SIZE_SIZE);
2005 if (strings == NULL)
2007 strings = _bfd_coff_read_string_table (input_bfd);
2008 if (strings == NULL)
2009 return false;
2011 if ((bfd_size_type) auxp->x_file.x_n.x_n.x_offset >= obj_coff_strings_len (input_bfd))
2012 filename = _("<corrupt>");
2013 else
2014 filename = strings + auxp->x_file.x_n.x_n.x_offset;
2015 indx = _bfd_stringtab_add (flaginfo->strtab, filename,
2016 hash, copy);
2017 if (indx == (bfd_size_type) -1)
2018 return false;
2019 auxp->x_file.x_n.x_n.x_offset = STRING_SIZE_SIZE + indx;
2022 else if ((isymp->n_sclass != C_STAT || isymp->n_type != T_NULL)
2023 && isymp->n_sclass != C_NT_WEAK)
2025 unsigned long indx;
2027 if (ISFCN (isymp->n_type)
2028 || ISTAG (isymp->n_sclass)
2029 || isymp->n_sclass == C_BLOCK
2030 || isymp->n_sclass == C_FCN)
2032 indx = auxp->x_sym.x_fcnary.x_fcn.x_endndx.u32;
2033 if (indx > 0
2034 && indx < obj_raw_syment_count (input_bfd))
2036 /* We look forward through the symbol for
2037 the index of the next symbol we are going
2038 to include. I don't know if this is
2039 entirely right. */
2040 while ((flaginfo->sym_indices[indx] < 0
2041 || ((bfd_size_type) flaginfo->sym_indices[indx]
2042 < syment_base))
2043 && indx < obj_raw_syment_count (input_bfd))
2044 ++indx;
2045 if (indx >= obj_raw_syment_count (input_bfd))
2046 indx = output_index;
2047 else
2048 indx = flaginfo->sym_indices[indx];
2049 auxp->x_sym.x_fcnary.x_fcn.x_endndx.u32 = indx;
2053 indx = auxp->x_sym.x_tagndx.u32;
2054 if (indx > 0 && indx < obj_raw_syment_count (input_bfd))
2056 long symindx;
2058 symindx = flaginfo->sym_indices[indx];
2059 if (symindx < 0)
2060 auxp->x_sym.x_tagndx.u32 = 0;
2061 else
2062 auxp->x_sym.x_tagndx.u32 = symindx;
2065 /* The .bf symbols are supposed to be linked through
2066 the endndx field. We need to carry this list
2067 across object files. */
2068 if (i == 0
2069 && h == NULL
2070 && isymp->n_sclass == C_FCN
2071 && (isymp->_n._n_n._n_zeroes != 0
2072 || isymp->_n._n_n._n_offset == 0)
2073 && isymp->_n._n_name[0] == '.'
2074 && isymp->_n._n_name[1] == 'b'
2075 && isymp->_n._n_name[2] == 'f'
2076 && isymp->_n._n_name[3] == '\0')
2078 if (flaginfo->last_bf_index != -1)
2080 flaginfo->last_bf.x_sym.x_fcnary.x_fcn.x_endndx.u32 =
2081 *indexp;
2083 if ((bfd_size_type) flaginfo->last_bf_index
2084 >= syment_base)
2086 void *auxout;
2088 /* The last .bf symbol is in this input
2089 file. This will only happen if the
2090 assembler did not set up the .bf
2091 endndx symbols correctly. */
2092 auxout = (flaginfo->outsyms
2093 + ((flaginfo->last_bf_index
2094 - syment_base)
2095 * osymesz));
2097 bfd_coff_swap_aux_out (output_bfd,
2098 &flaginfo->last_bf,
2099 isymp->n_type,
2100 isymp->n_sclass,
2101 0, isymp->n_numaux,
2102 auxout);
2104 else
2106 file_ptr pos;
2108 /* We have already written out the last
2109 .bf aux entry. We need to write it
2110 out again. We borrow *outsym
2111 temporarily. FIXME: This case should
2112 be made faster. */
2113 bfd_coff_swap_aux_out (output_bfd,
2114 &flaginfo->last_bf,
2115 isymp->n_type,
2116 isymp->n_sclass,
2117 0, isymp->n_numaux,
2118 outsym);
2119 pos = obj_sym_filepos (output_bfd);
2120 pos += flaginfo->last_bf_index * osymesz;
2121 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
2122 || (bfd_write (outsym, osymesz, output_bfd)
2123 != osymesz))
2124 return false;
2128 if (auxp->x_sym.x_fcnary.x_fcn.x_endndx.u32 != 0)
2129 flaginfo->last_bf_index = -1;
2130 else
2132 /* The endndx field of this aux entry must
2133 be updated with the symbol number of the
2134 next .bf symbol. */
2135 flaginfo->last_bf = *auxp;
2136 flaginfo->last_bf_index = (((outsym - flaginfo->outsyms)
2137 / osymesz)
2138 + syment_base);
2143 if (h == NULL)
2145 bfd_coff_swap_aux_out (output_bfd, auxp, isymp->n_type,
2146 isymp->n_sclass, i, isymp->n_numaux,
2147 outsym);
2148 outsym += osymesz;
2151 esym += isymesz;
2155 indexp += add;
2156 isymp += add;
2157 sym_hash += add;
2160 /* Relocate the line numbers, unless we are stripping them. */
2161 if (flaginfo->info->strip == strip_none
2162 || flaginfo->info->strip == strip_some)
2164 for (o = input_bfd->sections; o != NULL; o = o->next)
2166 bfd_vma offset;
2167 bfd_byte *eline;
2168 bfd_byte *elineend;
2169 bfd_byte *oeline;
2170 bool skipping;
2171 file_ptr pos;
2172 bfd_size_type amt;
2174 /* FIXME: If SEC_HAS_CONTENTS is not for the section, then
2175 build_link_order in ldwrite.c will not have created a
2176 link order, which means that we will not have seen this
2177 input section in _bfd_coff_final_link, which means that
2178 we will not have allocated space for the line numbers of
2179 this section. I don't think line numbers can be
2180 meaningful for a section which does not have
2181 SEC_HAS_CONTENTS set, but, if they do, this must be
2182 changed. */
2183 if (o->lineno_count == 0
2184 || (o->output_section->flags & SEC_HAS_CONTENTS) == 0)
2185 continue;
2187 if (bfd_seek (input_bfd, o->line_filepos, SEEK_SET) != 0
2188 || bfd_read (flaginfo->linenos, linesz * o->lineno_count,
2189 input_bfd) != linesz * o->lineno_count)
2190 return false;
2192 offset = o->output_section->vma + o->output_offset - o->vma;
2193 eline = flaginfo->linenos;
2194 oeline = flaginfo->linenos;
2195 elineend = eline + linesz * o->lineno_count;
2196 skipping = false;
2197 for (; eline < elineend; eline += linesz)
2199 struct internal_lineno iline;
2201 bfd_coff_swap_lineno_in (input_bfd, eline, &iline);
2203 if (iline.l_lnno != 0)
2204 iline.l_addr.l_paddr += offset;
2205 else if (iline.l_addr.l_symndx >= 0
2206 && ((unsigned long) iline.l_addr.l_symndx
2207 < obj_raw_syment_count (input_bfd)))
2209 long indx;
2211 indx = flaginfo->sym_indices[iline.l_addr.l_symndx];
2213 if (indx < 0)
2215 /* These line numbers are attached to a symbol
2216 which we are stripping. We must discard the
2217 line numbers because reading them back with
2218 no associated symbol (or associating them all
2219 with symbol #0) will fail. We can't regain
2220 the space in the output file, but at least
2221 they're dense. */
2222 skipping = true;
2224 else
2226 struct internal_syment is;
2227 union internal_auxent ia;
2229 /* Fix up the lnnoptr field in the aux entry of
2230 the symbol. It turns out that we can't do
2231 this when we modify the symbol aux entries,
2232 because gas sometimes screws up the lnnoptr
2233 field and makes it an offset from the start
2234 of the line numbers rather than an absolute
2235 file index. */
2236 bfd_coff_swap_sym_in (output_bfd,
2237 (flaginfo->outsyms
2238 + ((indx - syment_base)
2239 * osymesz)), &is);
2240 if ((ISFCN (is.n_type)
2241 || is.n_sclass == C_BLOCK)
2242 && is.n_numaux >= 1)
2244 void *auxptr;
2246 auxptr = (flaginfo->outsyms
2247 + ((indx - syment_base + 1)
2248 * osymesz));
2249 bfd_coff_swap_aux_in (output_bfd, auxptr,
2250 is.n_type, is.n_sclass,
2251 0, is.n_numaux, &ia);
2252 ia.x_sym.x_fcnary.x_fcn.x_lnnoptr =
2253 (o->output_section->line_filepos
2254 + o->output_section->lineno_count * linesz
2255 + eline - flaginfo->linenos);
2256 bfd_coff_swap_aux_out (output_bfd, &ia,
2257 is.n_type, is.n_sclass, 0,
2258 is.n_numaux, auxptr);
2261 skipping = false;
2264 iline.l_addr.l_symndx = indx;
2267 if (!skipping)
2269 bfd_coff_swap_lineno_out (output_bfd, &iline, oeline);
2270 oeline += linesz;
2274 pos = o->output_section->line_filepos;
2275 pos += o->output_section->lineno_count * linesz;
2276 amt = oeline - flaginfo->linenos;
2277 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
2278 || bfd_write (flaginfo->linenos, amt, output_bfd) != amt)
2279 return false;
2281 o->output_section->lineno_count += amt / linesz;
2285 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
2286 symbol will be the first symbol in the next input file. In the
2287 normal case, this will save us from writing out the C_FILE symbol
2288 again. */
2289 if (flaginfo->last_file_index != -1
2290 && (bfd_size_type) flaginfo->last_file_index >= syment_base)
2292 flaginfo->last_file.n_value = output_index;
2293 bfd_coff_swap_sym_out (output_bfd, &flaginfo->last_file,
2294 (flaginfo->outsyms
2295 + ((flaginfo->last_file_index - syment_base)
2296 * osymesz)));
2299 /* Write the modified symbols to the output file. */
2300 if (outsym > flaginfo->outsyms)
2302 file_ptr pos;
2303 bfd_size_type amt;
2305 pos = obj_sym_filepos (output_bfd) + syment_base * osymesz;
2306 amt = outsym - flaginfo->outsyms;
2307 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
2308 || bfd_write (flaginfo->outsyms, amt, output_bfd) != amt)
2309 return false;
2311 BFD_ASSERT ((obj_raw_syment_count (output_bfd)
2312 + (outsym - flaginfo->outsyms) / osymesz)
2313 == output_index);
2315 obj_raw_syment_count (output_bfd) = output_index;
2318 /* Relocate the contents of each section. */
2319 adjust_symndx = coff_backend_info (input_bfd)->_bfd_coff_adjust_symndx;
2320 for (o = input_bfd->sections; o != NULL; o = o->next)
2322 bfd_byte *contents;
2323 struct coff_section_tdata *secdata;
2325 if (! o->linker_mark)
2326 /* This section was omitted from the link. */
2327 continue;
2329 if ((o->flags & SEC_LINKER_CREATED) != 0)
2330 continue;
2332 if ((o->flags & SEC_HAS_CONTENTS) == 0
2333 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
2335 if ((o->flags & SEC_RELOC) != 0
2336 && o->reloc_count != 0)
2338 _bfd_error_handler
2339 /* xgettext: c-format */
2340 (_("%pB: relocs in section `%pA', but it has no contents"),
2341 input_bfd, o);
2342 bfd_set_error (bfd_error_no_contents);
2343 return false;
2346 continue;
2349 secdata = coff_section_data (input_bfd, o);
2350 if (secdata != NULL && secdata->contents != NULL)
2351 contents = secdata->contents;
2352 else
2354 contents = flaginfo->contents;
2355 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
2356 return false;
2359 if ((o->flags & SEC_RELOC) != 0)
2361 int target_index;
2362 struct internal_reloc *internal_relocs;
2363 struct internal_reloc *irel;
2365 /* Read in the relocs. */
2366 target_index = o->output_section->target_index;
2367 internal_relocs = (_bfd_coff_read_internal_relocs
2368 (input_bfd, o, false, flaginfo->external_relocs,
2369 bfd_link_relocatable (flaginfo->info),
2370 (bfd_link_relocatable (flaginfo->info)
2371 ? (flaginfo->section_info[target_index].relocs
2372 + o->output_section->reloc_count)
2373 : flaginfo->internal_relocs)));
2374 if (internal_relocs == NULL
2375 && o->reloc_count > 0)
2376 return false;
2378 /* Run through the relocs looking for relocs against symbols
2379 coming from discarded sections and complain about them. */
2380 irel = internal_relocs;
2381 for (; irel < &internal_relocs[o->reloc_count]; irel++)
2383 struct coff_link_hash_entry *h;
2384 asection *ps = NULL;
2385 long symndx = irel->r_symndx;
2386 if (symndx < 0)
2387 continue;
2388 h = obj_coff_sym_hashes (input_bfd)[symndx];
2389 if (h == NULL)
2390 continue;
2391 while (h->root.type == bfd_link_hash_indirect
2392 || h->root.type == bfd_link_hash_warning)
2393 h = (struct coff_link_hash_entry *) h->root.u.i.link;
2394 if (h->root.type == bfd_link_hash_defined
2395 || h->root.type == bfd_link_hash_defweak)
2396 ps = h->root.u.def.section;
2397 if (ps == NULL)
2398 continue;
2399 /* Complain if definition comes from an excluded section. */
2400 if (ps->flags & SEC_EXCLUDE)
2401 (*flaginfo->info->callbacks->einfo)
2402 /* xgettext: c-format */
2403 (_("%X`%s' referenced in section `%pA' of %pB: "
2404 "defined in discarded section `%pA' of %pB\n"),
2405 h->root.root.string, o, input_bfd, ps, ps->owner);
2408 /* Call processor specific code to relocate the section
2409 contents. */
2410 if (! bfd_coff_relocate_section (output_bfd, flaginfo->info,
2411 input_bfd, o,
2412 contents,
2413 internal_relocs,
2414 flaginfo->internal_syms,
2415 flaginfo->sec_ptrs))
2416 return false;
2418 if (bfd_link_relocatable (flaginfo->info))
2420 bfd_vma offset;
2421 struct internal_reloc *irelend;
2422 struct coff_link_hash_entry **rel_hash;
2424 offset = o->output_section->vma + o->output_offset - o->vma;
2425 irel = internal_relocs;
2426 irelend = irel + o->reloc_count;
2427 rel_hash = (flaginfo->section_info[target_index].rel_hashes
2428 + o->output_section->reloc_count);
2429 for (; irel < irelend; irel++, rel_hash++)
2431 struct coff_link_hash_entry *h;
2432 bool adjusted;
2434 *rel_hash = NULL;
2436 /* Adjust the reloc address and symbol index. */
2437 irel->r_vaddr += offset;
2439 if (irel->r_symndx == -1)
2440 continue;
2442 if (adjust_symndx)
2444 if (! (*adjust_symndx) (output_bfd, flaginfo->info,
2445 input_bfd, o, irel,
2446 &adjusted))
2447 return false;
2448 if (adjusted)
2449 continue;
2452 h = obj_coff_sym_hashes (input_bfd)[irel->r_symndx];
2453 if (h != NULL)
2455 /* This is a global symbol. */
2456 if (h->indx >= 0)
2457 irel->r_symndx = h->indx;
2458 else
2460 /* This symbol is being written at the end
2461 of the file, and we do not yet know the
2462 symbol index. We save the pointer to the
2463 hash table entry in the rel_hash list.
2464 We set the indx field to -2 to indicate
2465 that this symbol must not be stripped. */
2466 *rel_hash = h;
2467 h->indx = -2;
2470 else
2472 long indx;
2474 indx = flaginfo->sym_indices[irel->r_symndx];
2475 if (indx != -1)
2476 irel->r_symndx = indx;
2477 else
2479 struct internal_syment *is;
2480 const char *name;
2481 char buf[SYMNMLEN + 1];
2483 /* This reloc is against a symbol we are
2484 stripping. This should have been handled
2485 by the 'dont_skip_symbol' code in the while
2486 loop at the top of this function. */
2487 is = flaginfo->internal_syms + irel->r_symndx;
2489 name = (_bfd_coff_internal_syment_name
2490 (input_bfd, is, buf));
2491 if (name == NULL)
2492 return false;
2494 (*flaginfo->info->callbacks->unattached_reloc)
2495 (flaginfo->info, name, input_bfd, o, irel->r_vaddr);
2500 o->output_section->reloc_count += o->reloc_count;
2504 /* Write out the modified section contents. */
2505 if (secdata == NULL || secdata->stab_info == NULL)
2507 file_ptr loc = (o->output_offset
2508 * bfd_octets_per_byte (output_bfd, o));
2509 if (! bfd_set_section_contents (output_bfd, o->output_section,
2510 contents, loc, o->size))
2511 return false;
2513 else
2515 if (! (_bfd_write_section_stabs
2516 (output_bfd, &coff_hash_table (flaginfo->info)->stab_info,
2517 o, &secdata->stab_info, contents)))
2518 return false;
2522 if (! flaginfo->info->keep_memory
2523 && ! _bfd_coff_free_symbols (input_bfd))
2524 return false;
2526 return true;
2529 /* Write out a global symbol. Called via bfd_hash_traverse. */
2531 bool
2532 _bfd_coff_write_global_sym (struct bfd_hash_entry *bh, void *data)
2534 struct coff_link_hash_entry *h = (struct coff_link_hash_entry *) bh;
2535 struct coff_final_link_info *flaginfo = (struct coff_final_link_info *) data;
2536 bfd *output_bfd;
2537 struct internal_syment isym;
2538 bfd_size_type symesz;
2539 unsigned int i;
2540 file_ptr pos;
2542 output_bfd = flaginfo->output_bfd;
2544 if (h->root.type == bfd_link_hash_warning)
2546 h = (struct coff_link_hash_entry *) h->root.u.i.link;
2547 if (h->root.type == bfd_link_hash_new)
2548 return true;
2551 if (h->indx >= 0)
2552 return true;
2554 if (h->indx != -2
2555 && (flaginfo->info->strip == strip_all
2556 || (flaginfo->info->strip == strip_some
2557 && (bfd_hash_lookup (flaginfo->info->keep_hash,
2558 h->root.root.string, false, false)
2559 == NULL))))
2560 return true;
2562 switch (h->root.type)
2564 default:
2565 case bfd_link_hash_new:
2566 case bfd_link_hash_warning:
2567 abort ();
2568 return false;
2570 case bfd_link_hash_undefined:
2571 if (h->indx == -3)
2572 return true;
2573 /* Fall through. */
2574 case bfd_link_hash_undefweak:
2575 isym.n_scnum = N_UNDEF;
2576 isym.n_value = 0;
2577 break;
2579 case bfd_link_hash_defined:
2580 case bfd_link_hash_defweak:
2582 asection *sec;
2584 sec = h->root.u.def.section->output_section;
2585 if (bfd_is_abs_section (sec))
2586 isym.n_scnum = N_ABS;
2587 else
2588 isym.n_scnum = sec->target_index;
2589 isym.n_value = (h->root.u.def.value
2590 + h->root.u.def.section->output_offset);
2591 if (! obj_pe (flaginfo->output_bfd))
2592 isym.n_value += sec->vma;
2593 #ifdef BFD64
2594 if (isym.n_value > (bfd_vma) 0xffffffff)
2596 if (! h->root.linker_def)
2597 _bfd_error_handler
2598 (_("%pB: stripping non-representable symbol '%s' "
2599 "(value 0x%" PRIx64 ")"),
2600 output_bfd, h->root.root.string, isym.n_value);
2601 return true;
2603 #endif
2605 break;
2607 case bfd_link_hash_common:
2608 isym.n_scnum = N_UNDEF;
2609 isym.n_value = h->root.u.c.size;
2610 break;
2612 case bfd_link_hash_indirect:
2613 /* Just ignore these. They can't be handled anyhow. */
2614 return true;
2617 if (strlen (h->root.root.string) <= SYMNMLEN)
2618 strncpy (isym._n._n_name, h->root.root.string, SYMNMLEN);
2619 else
2621 bool hash;
2622 bfd_size_type indx;
2624 hash = true;
2625 if (flaginfo->info->traditional_format)
2626 hash = false;
2627 indx = _bfd_stringtab_add (flaginfo->strtab, h->root.root.string, hash,
2628 false);
2629 if (indx == (bfd_size_type) -1)
2631 flaginfo->failed = true;
2632 return false;
2634 isym._n._n_n._n_zeroes = 0;
2635 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
2638 isym.n_sclass = h->symbol_class;
2639 isym.n_type = h->type;
2641 if (isym.n_sclass == C_NULL)
2642 isym.n_sclass = C_EXT;
2644 /* If doing task linking and this is the pass where we convert
2645 defined globals to statics, then do that conversion now. If the
2646 symbol is not being converted, just ignore it and it will be
2647 output during a later pass. */
2648 if (flaginfo->global_to_static)
2650 if (! IS_EXTERNAL (output_bfd, isym))
2651 return true;
2653 isym.n_sclass = C_STAT;
2656 /* When a weak symbol is not overridden by a strong one,
2657 turn it into an external symbol when not building a
2658 shared or relocatable object. */
2659 if (! bfd_link_pic (flaginfo->info)
2660 && ! bfd_link_relocatable (flaginfo->info)
2661 && IS_WEAK_EXTERNAL (flaginfo->output_bfd, isym))
2662 isym.n_sclass = C_EXT;
2664 isym.n_numaux = h->numaux;
2666 bfd_coff_swap_sym_out (output_bfd, &isym, flaginfo->outsyms);
2668 symesz = bfd_coff_symesz (output_bfd);
2670 pos = obj_sym_filepos (output_bfd);
2671 pos += obj_raw_syment_count (output_bfd) * symesz;
2672 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
2673 || bfd_write (flaginfo->outsyms, symesz, output_bfd) != symesz)
2675 flaginfo->failed = true;
2676 return false;
2679 h->indx = obj_raw_syment_count (output_bfd);
2681 ++obj_raw_syment_count (output_bfd);
2683 /* Write out any associated aux entries. Most of the aux entries
2684 will have been modified in _bfd_coff_link_input_bfd. We have to
2685 handle section aux entries here, now that we have the final
2686 relocation and line number counts. */
2687 for (i = 0; i < isym.n_numaux; i++)
2689 union internal_auxent *auxp;
2691 auxp = h->aux + i;
2693 /* Look for a section aux entry here using the same tests that
2694 coff_swap_aux_out uses. */
2695 if (i == 0
2696 && (isym.n_sclass == C_STAT
2697 || isym.n_sclass == C_HIDDEN)
2698 && isym.n_type == T_NULL
2699 && (h->root.type == bfd_link_hash_defined
2700 || h->root.type == bfd_link_hash_defweak))
2702 asection *sec;
2704 sec = h->root.u.def.section->output_section;
2705 if (sec != NULL)
2707 auxp->x_scn.x_scnlen = sec->size;
2709 /* For PE, an overflow on the final link reportedly does
2710 not matter. FIXME: Why not? */
2711 if (sec->reloc_count > 0xffff
2712 && (! obj_pe (output_bfd)
2713 || bfd_link_relocatable (flaginfo->info)))
2714 _bfd_error_handler
2715 /* xgettext: c-format */
2716 (_("%pB: %pA: reloc overflow: %#x > 0xffff"),
2717 output_bfd, sec, sec->reloc_count);
2719 if (sec->lineno_count > 0xffff
2720 && (! obj_pe (output_bfd)
2721 || bfd_link_relocatable (flaginfo->info)))
2722 _bfd_error_handler
2723 /* xgettext: c-format */
2724 (_("%pB: warning: %pA: line number overflow: %#x > 0xffff"),
2725 output_bfd, sec, sec->lineno_count);
2727 auxp->x_scn.x_nreloc = sec->reloc_count;
2728 auxp->x_scn.x_nlinno = sec->lineno_count;
2729 auxp->x_scn.x_checksum = 0;
2730 auxp->x_scn.x_associated = 0;
2731 auxp->x_scn.x_comdat = 0;
2735 bfd_coff_swap_aux_out (output_bfd, auxp, isym.n_type,
2736 isym.n_sclass, (int) i, isym.n_numaux,
2737 flaginfo->outsyms);
2738 if (bfd_write (flaginfo->outsyms, symesz, output_bfd) != symesz)
2740 flaginfo->failed = true;
2741 return false;
2743 ++obj_raw_syment_count (output_bfd);
2746 return true;
2749 /* Write out task global symbols, converting them to statics. Called
2750 via coff_link_hash_traverse. Calls bfd_coff_write_global_sym to do
2751 the dirty work, if the symbol we are processing needs conversion. */
2753 bool
2754 _bfd_coff_write_task_globals (struct coff_link_hash_entry *h, void *data)
2756 struct coff_final_link_info *flaginfo = (struct coff_final_link_info *) data;
2757 bool rtnval = true;
2758 bool save_global_to_static;
2760 if (h->root.type == bfd_link_hash_warning)
2761 h = (struct coff_link_hash_entry *) h->root.u.i.link;
2763 if (h->indx < 0)
2765 switch (h->root.type)
2767 case bfd_link_hash_defined:
2768 case bfd_link_hash_defweak:
2769 save_global_to_static = flaginfo->global_to_static;
2770 flaginfo->global_to_static = true;
2771 rtnval = _bfd_coff_write_global_sym (&h->root.root, data);
2772 flaginfo->global_to_static = save_global_to_static;
2773 break;
2774 default:
2775 break;
2778 return (rtnval);
2781 /* Handle a link order which is supposed to generate a reloc. */
2783 bool
2784 _bfd_coff_reloc_link_order (bfd *output_bfd,
2785 struct coff_final_link_info *flaginfo,
2786 asection *output_section,
2787 struct bfd_link_order *link_order)
2789 reloc_howto_type *howto;
2790 struct internal_reloc *irel;
2791 struct coff_link_hash_entry **rel_hash_ptr;
2793 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
2794 if (howto == NULL)
2796 bfd_set_error (bfd_error_bad_value);
2797 return false;
2800 if (link_order->u.reloc.p->addend != 0)
2802 bfd_size_type size;
2803 bfd_byte *buf;
2804 bfd_reloc_status_type rstat;
2805 bool ok;
2806 file_ptr loc;
2808 size = bfd_get_reloc_size (howto);
2809 buf = (bfd_byte *) bfd_zmalloc (size);
2810 if (buf == NULL && size != 0)
2811 return false;
2813 rstat = _bfd_relocate_contents (howto, output_bfd,
2814 (bfd_vma) link_order->u.reloc.p->addend,
2815 buf);
2816 switch (rstat)
2818 case bfd_reloc_ok:
2819 break;
2820 default:
2821 case bfd_reloc_outofrange:
2822 abort ();
2823 case bfd_reloc_overflow:
2824 (*flaginfo->info->callbacks->reloc_overflow)
2825 (flaginfo->info, NULL,
2826 (link_order->type == bfd_section_reloc_link_order
2827 ? bfd_section_name (link_order->u.reloc.p->u.section)
2828 : link_order->u.reloc.p->u.name),
2829 howto->name, link_order->u.reloc.p->addend,
2830 (bfd *) NULL, (asection *) NULL, (bfd_vma) 0);
2831 break;
2833 loc = link_order->offset * bfd_octets_per_byte (output_bfd,
2834 output_section);
2835 ok = bfd_set_section_contents (output_bfd, output_section, buf,
2836 loc, size);
2837 free (buf);
2838 if (! ok)
2839 return false;
2842 /* Store the reloc information in the right place. It will get
2843 swapped and written out at the end of the final_link routine. */
2844 irel = (flaginfo->section_info[output_section->target_index].relocs
2845 + output_section->reloc_count);
2846 rel_hash_ptr = (flaginfo->section_info[output_section->target_index].rel_hashes
2847 + output_section->reloc_count);
2849 memset (irel, 0, sizeof (struct internal_reloc));
2850 *rel_hash_ptr = NULL;
2852 irel->r_vaddr = output_section->vma + link_order->offset;
2854 if (link_order->type == bfd_section_reloc_link_order)
2856 /* We need to somehow locate a symbol in the right section. The
2857 symbol must either have a value of zero, or we must adjust
2858 the addend by the value of the symbol. FIXME: Write this
2859 when we need it. The old linker couldn't handle this anyhow. */
2860 abort ();
2861 *rel_hash_ptr = NULL;
2862 irel->r_symndx = 0;
2864 else
2866 struct coff_link_hash_entry *h;
2868 h = ((struct coff_link_hash_entry *)
2869 bfd_wrapped_link_hash_lookup (output_bfd, flaginfo->info,
2870 link_order->u.reloc.p->u.name,
2871 false, false, true));
2872 if (h != NULL)
2874 if (h->indx >= 0)
2875 irel->r_symndx = h->indx;
2876 else
2878 /* Set the index to -2 to force this symbol to get
2879 written out. */
2880 h->indx = -2;
2881 *rel_hash_ptr = h;
2882 irel->r_symndx = 0;
2885 else
2887 (*flaginfo->info->callbacks->unattached_reloc)
2888 (flaginfo->info, link_order->u.reloc.p->u.name,
2889 (bfd *) NULL, (asection *) NULL, (bfd_vma) 0);
2890 irel->r_symndx = 0;
2894 /* FIXME: Is this always right? */
2895 irel->r_type = howto->type;
2897 /* r_size is only used on the RS/6000, which needs its own linker
2898 routines anyhow. r_extern is only used for ECOFF. */
2900 /* FIXME: What is the right value for r_offset? Is zero OK? */
2901 ++output_section->reloc_count;
2903 return true;
2906 /* A basic reloc handling routine which may be used by processors with
2907 simple relocs. */
2909 bool
2910 _bfd_coff_generic_relocate_section (bfd *output_bfd,
2911 struct bfd_link_info *info,
2912 bfd *input_bfd,
2913 asection *input_section,
2914 bfd_byte *contents,
2915 struct internal_reloc *relocs,
2916 struct internal_syment *syms,
2917 asection **sections)
2919 struct internal_reloc *rel;
2920 struct internal_reloc *relend;
2922 rel = relocs;
2923 relend = rel + input_section->reloc_count;
2924 for (; rel < relend; rel++)
2926 long symndx;
2927 struct coff_link_hash_entry *h;
2928 struct internal_syment *sym;
2929 bfd_vma addend;
2930 bfd_vma val;
2931 asection *sec;
2932 reloc_howto_type *howto;
2933 bfd_reloc_status_type rstat;
2935 symndx = rel->r_symndx;
2937 if (symndx == -1)
2939 h = NULL;
2940 sym = NULL;
2942 else if (symndx < 0
2943 || (unsigned long) symndx >= obj_raw_syment_count (input_bfd))
2945 _bfd_error_handler
2946 /* xgettext: c-format */
2947 (_("%pB: illegal symbol index %ld in relocs"), input_bfd, symndx);
2948 return false;
2950 else
2952 h = obj_coff_sym_hashes (input_bfd)[symndx];
2953 sym = syms + symndx;
2956 /* COFF treats common symbols in one of two ways. Either the
2957 size of the symbol is included in the section contents, or it
2958 is not. We assume that the size is not included, and force
2959 the rtype_to_howto function to adjust the addend as needed. */
2960 if (sym != NULL && sym->n_scnum != 0)
2961 addend = - sym->n_value;
2962 else
2963 addend = 0;
2965 howto = bfd_coff_rtype_to_howto (input_bfd, input_section, rel, h,
2966 sym, &addend);
2967 if (howto == NULL)
2968 return false;
2970 /* If we are doing a relocatable link, then we can just ignore
2971 a PC relative reloc that is pcrel_offset. It will already
2972 have the correct value. If this is not a relocatable link,
2973 then we should ignore the symbol value. */
2974 if (howto->pc_relative && howto->pcrel_offset)
2976 if (bfd_link_relocatable (info))
2977 continue;
2978 if (sym != NULL && sym->n_scnum != 0)
2979 addend += sym->n_value;
2982 val = 0;
2983 sec = NULL;
2984 if (h == NULL)
2986 if (symndx == -1)
2988 sec = bfd_abs_section_ptr;
2989 val = 0;
2991 else
2993 sec = sections[symndx];
2995 /* PR 19623: Relocations against symbols in
2996 the absolute sections should ignored.
2997 PR 29807: Also ignore relocs against file symbols or
2998 other such nonsense in fuzzed objects. */
2999 if (sec == NULL || bfd_is_abs_section (sec))
3000 continue;
3002 val = (sec->output_section->vma
3003 + sec->output_offset
3004 + sym->n_value);
3005 if (! obj_pe (input_bfd))
3006 val -= sec->vma;
3009 else
3011 if (h->root.type == bfd_link_hash_defined
3012 /* Defined weak symbols are a GNU extension. */
3013 || h->root.type == bfd_link_hash_defweak)
3015 sec = h->root.u.def.section;
3016 BFD_ASSERT (sec->output_section != NULL);
3017 val = (h->root.u.def.value
3018 + sec->output_section->vma
3019 + sec->output_offset);
3022 else if (h->root.type == bfd_link_hash_undefweak)
3024 if (h->symbol_class == C_NT_WEAK && h->numaux == 1)
3026 /* See _Microsoft Portable Executable and Common Object
3027 File Format Specification_, section 5.5.3.
3028 Note that weak symbols without aux records are a GNU
3029 extension.
3030 FIXME: All weak externals are treated as having
3031 characteristic IMAGE_WEAK_EXTERN_SEARCH_NOLIBRARY (1).
3032 These behave as per SVR4 ABI: A library member
3033 will resolve a weak external only if a normal
3034 external causes the library member to be linked.
3035 See also linker.c: generic_link_check_archive_element. */
3036 struct coff_link_hash_entry *h2 =
3037 h->auxbfd->tdata.coff_obj_data->sym_hashes
3038 [h->aux->x_sym.x_tagndx.u32];
3040 if (!h2 || h2->root.type == bfd_link_hash_undefined)
3042 sec = bfd_abs_section_ptr;
3043 val = 0;
3045 else
3047 sec = h2->root.u.def.section;
3048 val = h2->root.u.def.value
3049 + sec->output_section->vma + sec->output_offset;
3052 else
3053 /* This is a GNU extension. */
3054 val = 0;
3057 else if (! bfd_link_relocatable (info))
3059 (*info->callbacks->undefined_symbol)
3060 (info, h->root.root.string, input_bfd, input_section,
3061 rel->r_vaddr - input_section->vma, true);
3062 /* Stop the linker from issueing errors about truncated relocs
3063 referencing this undefined symbol by giving it an address
3064 that should be in range. */
3065 val = input_section->output_section->vma;
3069 /* If the input section defining the symbol has been discarded
3070 then zero this reloc field. */
3071 if (sec != NULL && discarded_section (sec))
3073 _bfd_clear_contents (howto, input_bfd, input_section,
3074 contents, rel->r_vaddr - input_section->vma);
3075 continue;
3078 if (info->base_file)
3080 /* Emit a reloc if the backend thinks it needs it. */
3081 if (sym && pe_data (output_bfd)->in_reloc_p (output_bfd, howto))
3083 /* Relocation to a symbol in a section which isn't
3084 absolute. We output the address here to a file.
3085 This file is then read by dlltool when generating the
3086 reloc section. Note that the base file is not
3087 portable between systems. We write out a bfd_vma here,
3088 and dlltool reads in a bfd_vma. */
3089 bfd_vma addr = (rel->r_vaddr
3090 - input_section->vma
3091 + input_section->output_offset
3092 + input_section->output_section->vma);
3093 if (obj_pe (output_bfd))
3094 addr -= pe_data(output_bfd)->pe_opthdr.ImageBase;
3095 if (fwrite (&addr, 1, sizeof (bfd_vma), (FILE *) info->base_file)
3096 != sizeof (bfd_vma))
3098 bfd_set_error (bfd_error_system_call);
3099 return false;
3104 rstat = _bfd_final_link_relocate (howto, input_bfd, input_section,
3105 contents,
3106 rel->r_vaddr - input_section->vma,
3107 val, addend);
3109 switch (rstat)
3111 default:
3112 abort ();
3113 case bfd_reloc_ok:
3114 break;
3115 case bfd_reloc_outofrange:
3116 _bfd_error_handler
3117 /* xgettext: c-format */
3118 (_("%pB: bad reloc address %#" PRIx64 " in section `%pA'"),
3119 input_bfd, (uint64_t) rel->r_vaddr, input_section);
3120 return false;
3121 case bfd_reloc_overflow:
3123 /* Ignore any weak undef symbols that may have overflowed. Due to
3124 PR ld/19011 the base address is now in the upper 64-bit address
3125 range. This means that when _bfd_final_link_relocate calculates
3126 the overlow it takes the distance between the symbol and the VMA
3127 which will now always overflow as 0 - 64-bit addr > 32-bit range
3128 of the relocation. This ends up creating PR ld/26659. */
3129 if (val == 0
3130 /* Reverse the hack where 4 is subtracted from the addend. */
3131 && (addend + 4) == 0
3132 && sym->n_sclass == C_NT_WEAK
3133 && bfd_coff_classify_symbol (output_bfd, sym)
3134 == COFF_SYMBOL_UNDEFINED)
3135 break;
3137 const char *name;
3138 char buf[SYMNMLEN + 1];
3140 if (symndx == -1)
3141 name = "*ABS*";
3142 else if (h != NULL)
3143 name = NULL;
3144 else
3146 name = _bfd_coff_internal_syment_name (input_bfd, sym, buf);
3147 if (name == NULL)
3148 return false;
3151 (*info->callbacks->reloc_overflow)
3152 (info, (h ? &h->root : NULL), name, howto->name,
3153 (bfd_vma) 0, input_bfd, input_section,
3154 rel->r_vaddr - input_section->vma);
3159 return true;