gdb: add target_ops::supports_displaced_step
[deliverable/binutils-gdb.git] / bfd / cofflink.c
1 /* COFF specific linker code.
2 Copyright (C) 1994-2020 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor, Cygnus Support.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
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.
11
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.
16
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. */
21
22 /* This file contains the COFF backend linker code. */
23
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"
31
32 static bfd_boolean coff_link_add_object_symbols (bfd *, struct bfd_link_info *);
33 static bfd_boolean coff_link_check_archive_element
34 (bfd *, struct bfd_link_info *, struct bfd_link_hash_entry *, const char *,
35 bfd_boolean *);
36 static bfd_boolean coff_link_add_symbols (bfd *, struct bfd_link_info *);
37
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))
42
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))
46
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. */
52
53 #define N_TMASK n_tmask
54 #define N_BTSHFT n_btshft
55 #define N_BTMASK n_btmask
56
57 /* Create an entry in a COFF linker hash table. */
58
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)
63 {
64 struct coff_link_hash_entry *ret = (struct coff_link_hash_entry *) entry;
65
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;
73
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)
79 {
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;
87 }
88
89 return (struct bfd_hash_entry *) ret;
90 }
91
92 /* Initialize a COFF linker hash table. */
93
94 bfd_boolean
95 _bfd_coff_link_hash_table_init (struct coff_link_hash_table *table,
96 bfd *abfd,
97 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
98 struct bfd_hash_table *,
99 const char *),
100 unsigned int entsize)
101 {
102 memset (&table->stab_info, 0, sizeof (table->stab_info));
103 return _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
104 }
105
106 /* Create a COFF linker hash table. */
107
108 struct bfd_link_hash_table *
109 _bfd_coff_link_hash_table_create (bfd *abfd)
110 {
111 struct coff_link_hash_table *ret;
112 size_t amt = sizeof (struct coff_link_hash_table);
113
114 ret = (struct coff_link_hash_table *) bfd_malloc (amt);
115 if (ret == NULL)
116 return NULL;
117
118 if (! _bfd_coff_link_hash_table_init (ret, abfd,
119 _bfd_coff_link_hash_newfunc,
120 sizeof (struct coff_link_hash_entry)))
121 {
122 free (ret);
123 return (struct bfd_link_hash_table *) NULL;
124 }
125 return &ret->root;
126 }
127
128 /* Create an entry in a COFF debug merge hash table. */
129
130 struct bfd_hash_entry *
131 _bfd_coff_debug_merge_hash_newfunc (struct bfd_hash_entry *entry,
132 struct bfd_hash_table *table,
133 const char *string)
134 {
135 struct coff_debug_merge_hash_entry *ret =
136 (struct coff_debug_merge_hash_entry *) entry;
137
138 /* Allocate the structure if it has not already been allocated by a
139 subclass. */
140 if (ret == (struct coff_debug_merge_hash_entry *) NULL)
141 ret = ((struct coff_debug_merge_hash_entry *)
142 bfd_hash_allocate (table,
143 sizeof (struct coff_debug_merge_hash_entry)));
144 if (ret == (struct coff_debug_merge_hash_entry *) NULL)
145 return (struct bfd_hash_entry *) ret;
146
147 /* Call the allocation method of the superclass. */
148 ret = ((struct coff_debug_merge_hash_entry *)
149 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
150 if (ret != (struct coff_debug_merge_hash_entry *) NULL)
151 {
152 /* Set local fields. */
153 ret->types = NULL;
154 }
155
156 return (struct bfd_hash_entry *) ret;
157 }
158
159 /* Given a COFF BFD, add symbols to the global hash table as
160 appropriate. */
161
162 bfd_boolean
163 _bfd_coff_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
164 {
165 switch (bfd_get_format (abfd))
166 {
167 case bfd_object:
168 return coff_link_add_object_symbols (abfd, info);
169 case bfd_archive:
170 return _bfd_generic_link_add_archive_symbols
171 (abfd, info, coff_link_check_archive_element);
172 default:
173 bfd_set_error (bfd_error_wrong_format);
174 return FALSE;
175 }
176 }
177
178 /* Add symbols from a COFF object file. */
179
180 static bfd_boolean
181 coff_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
182 {
183 if (! _bfd_coff_get_external_symbols (abfd))
184 return FALSE;
185 if (! coff_link_add_symbols (abfd, info))
186 return FALSE;
187
188 if (! info->keep_memory
189 && ! _bfd_coff_free_symbols (abfd))
190 return FALSE;
191
192 return TRUE;
193 }
194
195 /* Check a single archive element to see if we need to include it in
196 the link. *PNEEDED is set according to whether this element is
197 needed in the link or not. This is called via
198 _bfd_generic_link_add_archive_symbols. */
199
200 static bfd_boolean
201 coff_link_check_archive_element (bfd *abfd,
202 struct bfd_link_info *info,
203 struct bfd_link_hash_entry *h,
204 const char *name,
205 bfd_boolean *pneeded)
206 {
207 *pneeded = FALSE;
208
209 /* We are only interested in symbols that are currently undefined.
210 If a symbol is currently known to be common, COFF linkers do not
211 bring in an object file which defines it. */
212 if (h->type != bfd_link_hash_undefined)
213 return TRUE;
214
215 /* PR 22369 - Skip non COFF objects in the archive. */
216 if (! bfd_family_coff (abfd))
217 return TRUE;
218
219 /* Include this element? */
220 if (!(*info->callbacks->add_archive_element) (info, abfd, name, &abfd))
221 return TRUE;
222 *pneeded = TRUE;
223
224 return bfd_link_add_symbols (abfd, info);
225 }
226
227 /* Add all the symbols from an object file to the hash table. */
228
229 static bfd_boolean
230 coff_link_add_symbols (bfd *abfd,
231 struct bfd_link_info *info)
232 {
233 unsigned int n_tmask = coff_data (abfd)->local_n_tmask;
234 unsigned int n_btshft = coff_data (abfd)->local_n_btshft;
235 unsigned int n_btmask = coff_data (abfd)->local_n_btmask;
236 bfd_boolean keep_syms;
237 bfd_boolean default_copy;
238 bfd_size_type symcount;
239 struct coff_link_hash_entry **sym_hash;
240 bfd_size_type symesz;
241 bfd_byte *esym;
242 bfd_byte *esym_end;
243 bfd_size_type amt;
244
245 symcount = obj_raw_syment_count (abfd);
246
247 if (symcount == 0)
248 return TRUE; /* Nothing to do. */
249
250 /* Keep the symbols during this function, in case the linker needs
251 to read the generic symbols in order to report an error message. */
252 keep_syms = obj_coff_keep_syms (abfd);
253 obj_coff_keep_syms (abfd) = TRUE;
254
255 if (info->keep_memory)
256 default_copy = FALSE;
257 else
258 default_copy = TRUE;
259
260 /* We keep a list of the linker hash table entries that correspond
261 to particular symbols. */
262 amt = symcount * sizeof (struct coff_link_hash_entry *);
263 sym_hash = (struct coff_link_hash_entry **) bfd_zalloc (abfd, amt);
264 if (sym_hash == NULL)
265 goto error_return;
266 obj_coff_sym_hashes (abfd) = sym_hash;
267
268 symesz = bfd_coff_symesz (abfd);
269 BFD_ASSERT (symesz == bfd_coff_auxesz (abfd));
270 esym = (bfd_byte *) obj_coff_external_syms (abfd);
271 esym_end = esym + symcount * symesz;
272 while (esym < esym_end)
273 {
274 struct internal_syment sym;
275 enum coff_symbol_classification classification;
276 bfd_boolean copy;
277
278 bfd_coff_swap_sym_in (abfd, esym, &sym);
279
280 classification = bfd_coff_classify_symbol (abfd, &sym);
281 if (classification != COFF_SYMBOL_LOCAL)
282 {
283 const char *name;
284 char buf[SYMNMLEN + 1];
285 flagword flags;
286 asection *section;
287 bfd_vma value;
288 bfd_boolean addit;
289
290 /* This symbol is externally visible. */
291
292 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
293 if (name == NULL)
294 goto error_return;
295
296 /* We must copy the name into memory if we got it from the
297 syment itself, rather than the string table. */
298 copy = default_copy;
299 if (sym._n._n_n._n_zeroes != 0
300 || sym._n._n_n._n_offset == 0)
301 copy = TRUE;
302
303 value = sym.n_value;
304
305 switch (classification)
306 {
307 default:
308 abort ();
309
310 case COFF_SYMBOL_GLOBAL:
311 flags = BSF_EXPORT | BSF_GLOBAL;
312 section = coff_section_from_bfd_index (abfd, sym.n_scnum);
313 if (discarded_section (section))
314 section = bfd_und_section_ptr;
315 else if (! obj_pe (abfd))
316 value -= section->vma;
317 break;
318
319 case COFF_SYMBOL_UNDEFINED:
320 flags = 0;
321 section = bfd_und_section_ptr;
322 break;
323
324 case COFF_SYMBOL_COMMON:
325 flags = BSF_GLOBAL;
326 section = bfd_com_section_ptr;
327 break;
328
329 case COFF_SYMBOL_PE_SECTION:
330 flags = BSF_SECTION_SYM | BSF_GLOBAL;
331 section = coff_section_from_bfd_index (abfd, sym.n_scnum);
332 if (discarded_section (section))
333 section = bfd_und_section_ptr;
334 break;
335 }
336
337 if (IS_WEAK_EXTERNAL (abfd, sym))
338 flags = BSF_WEAK;
339
340 addit = TRUE;
341
342 /* In the PE format, section symbols actually refer to the
343 start of the output section. We handle them specially
344 here. */
345 if (obj_pe (abfd) && (flags & BSF_SECTION_SYM) != 0)
346 {
347 *sym_hash = coff_link_hash_lookup (coff_hash_table (info),
348 name, FALSE, copy, FALSE);
349 if (*sym_hash != NULL)
350 {
351 if (((*sym_hash)->coff_link_hash_flags
352 & COFF_LINK_HASH_PE_SECTION_SYMBOL) == 0
353 && (*sym_hash)->root.type != bfd_link_hash_undefined
354 && (*sym_hash)->root.type != bfd_link_hash_undefweak)
355 _bfd_error_handler
356 (_("warning: symbol `%s' is both section and non-section"),
357 name);
358
359 addit = FALSE;
360 }
361 }
362
363 /* The Microsoft Visual C compiler does string pooling by
364 hashing the constants to an internal symbol name, and
365 relying on the linker comdat support to discard
366 duplicate names. However, if one string is a literal and
367 one is a data initializer, one will end up in the .data
368 section and one will end up in the .rdata section. The
369 Microsoft linker will combine them into the .data
370 section, which seems to be wrong since it might cause the
371 literal to change.
372
373 As long as there are no external references to the
374 symbols, which there shouldn't be, we can treat the .data
375 and .rdata instances as separate symbols. The comdat
376 code in the linker will do the appropriate merging. Here
377 we avoid getting a multiple definition error for one of
378 these special symbols.
379
380 FIXME: I don't think this will work in the case where
381 there are two object files which use the constants as a
382 literal and two object files which use it as a data
383 initializer. One or the other of the second object files
384 is going to wind up with an inappropriate reference. */
385 if (obj_pe (abfd)
386 && (classification == COFF_SYMBOL_GLOBAL
387 || classification == COFF_SYMBOL_PE_SECTION)
388 && coff_section_data (abfd, section) != NULL
389 && coff_section_data (abfd, section)->comdat != NULL
390 && CONST_STRNEQ (name, "??_")
391 && strcmp (name, coff_section_data (abfd, section)->comdat->name) == 0)
392 {
393 if (*sym_hash == NULL)
394 *sym_hash = coff_link_hash_lookup (coff_hash_table (info),
395 name, FALSE, copy, FALSE);
396 if (*sym_hash != NULL
397 && (*sym_hash)->root.type == bfd_link_hash_defined
398 && coff_section_data (abfd, (*sym_hash)->root.u.def.section)->comdat != NULL
399 && strcmp (coff_section_data (abfd, (*sym_hash)->root.u.def.section)->comdat->name,
400 coff_section_data (abfd, section)->comdat->name) == 0)
401 addit = FALSE;
402 }
403
404 if (addit)
405 {
406 if (! (bfd_coff_link_add_one_symbol
407 (info, abfd, name, flags, section, value,
408 (const char *) NULL, copy, FALSE,
409 (struct bfd_link_hash_entry **) sym_hash)))
410 goto error_return;
411 }
412
413 if (obj_pe (abfd) && (flags & BSF_SECTION_SYM) != 0)
414 (*sym_hash)->coff_link_hash_flags |=
415 COFF_LINK_HASH_PE_SECTION_SYMBOL;
416
417 /* Limit the alignment of a common symbol to the possible
418 alignment of a section. There is no point to permitting
419 a higher alignment for a common symbol: we can not
420 guarantee it, and it may cause us to allocate extra space
421 in the common section. */
422 if (section == bfd_com_section_ptr
423 && (*sym_hash)->root.type == bfd_link_hash_common
424 && ((*sym_hash)->root.u.c.p->alignment_power
425 > bfd_coff_default_section_alignment_power (abfd)))
426 (*sym_hash)->root.u.c.p->alignment_power
427 = bfd_coff_default_section_alignment_power (abfd);
428
429 if (bfd_get_flavour (info->output_bfd) == bfd_get_flavour (abfd))
430 {
431 /* If we don't have any symbol information currently in
432 the hash table, or if we are looking at a symbol
433 definition, then update the symbol class and type in
434 the hash table. */
435 if (((*sym_hash)->symbol_class == C_NULL
436 && (*sym_hash)->type == T_NULL)
437 || sym.n_scnum != 0
438 || (sym.n_value != 0
439 && (*sym_hash)->root.type != bfd_link_hash_defined
440 && (*sym_hash)->root.type != bfd_link_hash_defweak))
441 {
442 (*sym_hash)->symbol_class = sym.n_sclass;
443 if (sym.n_type != T_NULL)
444 {
445 /* We want to warn if the type changed, but not
446 if it changed from an unspecified type.
447 Testing the whole type byte may work, but the
448 change from (e.g.) a function of unspecified
449 type to function of known type also wants to
450 skip the warning. */
451 if ((*sym_hash)->type != T_NULL
452 && (*sym_hash)->type != sym.n_type
453 && !(DTYPE ((*sym_hash)->type) == DTYPE (sym.n_type)
454 && (BTYPE ((*sym_hash)->type) == T_NULL
455 || BTYPE (sym.n_type) == T_NULL)))
456 _bfd_error_handler
457 /* xgettext: c-format */
458 (_("warning: type of symbol `%s' changed"
459 " from %d to %d in %pB"),
460 name, (*sym_hash)->type, sym.n_type, abfd);
461
462 /* We don't want to change from a meaningful
463 base type to a null one, but if we know
464 nothing, take what little we might now know. */
465 if (BTYPE (sym.n_type) != T_NULL
466 || (*sym_hash)->type == T_NULL)
467 (*sym_hash)->type = sym.n_type;
468 }
469 (*sym_hash)->auxbfd = abfd;
470 if (sym.n_numaux != 0)
471 {
472 union internal_auxent *alloc;
473 unsigned int i;
474 bfd_byte *eaux;
475 union internal_auxent *iaux;
476
477 (*sym_hash)->numaux = sym.n_numaux;
478 alloc = ((union internal_auxent *)
479 bfd_hash_allocate (&info->hash->table,
480 (sym.n_numaux
481 * sizeof (*alloc))));
482 if (alloc == NULL)
483 goto error_return;
484 for (i = 0, eaux = esym + symesz, iaux = alloc;
485 i < sym.n_numaux;
486 i++, eaux += symesz, iaux++)
487 bfd_coff_swap_aux_in (abfd, eaux, sym.n_type,
488 sym.n_sclass, (int) i,
489 sym.n_numaux, iaux);
490 (*sym_hash)->aux = alloc;
491 }
492 }
493 }
494
495 if (classification == COFF_SYMBOL_PE_SECTION
496 && (*sym_hash)->numaux != 0)
497 {
498 /* Some PE sections (such as .bss) have a zero size in
499 the section header, but a non-zero size in the AUX
500 record. Correct that here.
501
502 FIXME: This is not at all the right place to do this.
503 For example, it won't help objdump. This needs to be
504 done when we swap in the section header. */
505 BFD_ASSERT ((*sym_hash)->numaux == 1);
506 if (section->size == 0)
507 section->size = (*sym_hash)->aux[0].x_scn.x_scnlen;
508
509 /* FIXME: We could test whether the section sizes
510 matches the size in the aux entry, but apparently
511 that sometimes fails unexpectedly. */
512 }
513 }
514
515 esym += (sym.n_numaux + 1) * symesz;
516 sym_hash += sym.n_numaux + 1;
517 }
518
519 /* If this is a non-traditional, non-relocatable link, try to
520 optimize the handling of any .stab/.stabstr sections. */
521 if (! bfd_link_relocatable (info)
522 && ! info->traditional_format
523 && bfd_get_flavour (info->output_bfd) == bfd_get_flavour (abfd)
524 && (info->strip != strip_all && info->strip != strip_debugger))
525 {
526 asection *stabstr;
527
528 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
529
530 if (stabstr != NULL)
531 {
532 bfd_size_type string_offset = 0;
533 asection *stab;
534
535 for (stab = abfd->sections; stab; stab = stab->next)
536 if (CONST_STRNEQ (stab->name, ".stab")
537 && (!stab->name[5]
538 || (stab->name[5] == '.' && ISDIGIT (stab->name[6]))))
539 {
540 struct coff_link_hash_table *table;
541 struct coff_section_tdata *secdata
542 = coff_section_data (abfd, stab);
543
544 if (secdata == NULL)
545 {
546 amt = sizeof (struct coff_section_tdata);
547 stab->used_by_bfd = bfd_zalloc (abfd, amt);
548 if (stab->used_by_bfd == NULL)
549 goto error_return;
550 secdata = coff_section_data (abfd, stab);
551 }
552
553 table = coff_hash_table (info);
554
555 if (! _bfd_link_section_stabs (abfd, &table->stab_info,
556 stab, stabstr,
557 &secdata->stab_info,
558 &string_offset))
559 goto error_return;
560 }
561 }
562 }
563
564 obj_coff_keep_syms (abfd) = keep_syms;
565
566 return TRUE;
567
568 error_return:
569 obj_coff_keep_syms (abfd) = keep_syms;
570 return FALSE;
571 }
572 \f
573 /* Do the final link step. */
574
575 bfd_boolean
576 _bfd_coff_final_link (bfd *abfd,
577 struct bfd_link_info *info)
578 {
579 bfd_size_type symesz;
580 struct coff_final_link_info flaginfo;
581 bfd_boolean debug_merge_allocated;
582 bfd_boolean long_section_names;
583 asection *o;
584 struct bfd_link_order *p;
585 bfd_size_type max_sym_count;
586 bfd_size_type max_lineno_count;
587 bfd_size_type max_reloc_count;
588 bfd_size_type max_output_reloc_count;
589 bfd_size_type max_contents_size;
590 file_ptr rel_filepos;
591 unsigned int relsz;
592 file_ptr line_filepos;
593 unsigned int linesz;
594 bfd *sub;
595 bfd_byte *external_relocs = NULL;
596 char strbuf[STRING_SIZE_SIZE];
597 bfd_size_type amt;
598
599 symesz = bfd_coff_symesz (abfd);
600
601 flaginfo.info = info;
602 flaginfo.output_bfd = abfd;
603 flaginfo.strtab = NULL;
604 flaginfo.section_info = NULL;
605 flaginfo.last_file_index = -1;
606 flaginfo.last_bf_index = -1;
607 flaginfo.internal_syms = NULL;
608 flaginfo.sec_ptrs = NULL;
609 flaginfo.sym_indices = NULL;
610 flaginfo.outsyms = NULL;
611 flaginfo.linenos = NULL;
612 flaginfo.contents = NULL;
613 flaginfo.external_relocs = NULL;
614 flaginfo.internal_relocs = NULL;
615 flaginfo.global_to_static = FALSE;
616 debug_merge_allocated = FALSE;
617
618 coff_data (abfd)->link_info = info;
619
620 flaginfo.strtab = _bfd_stringtab_init ();
621 if (flaginfo.strtab == NULL)
622 goto error_return;
623
624 if (! coff_debug_merge_hash_table_init (&flaginfo.debug_merge))
625 goto error_return;
626 debug_merge_allocated = TRUE;
627
628 /* Compute the file positions for all the sections. */
629 if (! abfd->output_has_begun)
630 {
631 if (! bfd_coff_compute_section_file_positions (abfd))
632 goto error_return;
633 }
634
635 /* Count the line numbers and relocation entries required for the
636 output file. Set the file positions for the relocs. */
637 rel_filepos = obj_relocbase (abfd);
638 relsz = bfd_coff_relsz (abfd);
639 max_contents_size = 0;
640 max_lineno_count = 0;
641 max_reloc_count = 0;
642
643 long_section_names = FALSE;
644 for (o = abfd->sections; o != NULL; o = o->next)
645 {
646 o->reloc_count = 0;
647 o->lineno_count = 0;
648 for (p = o->map_head.link_order; p != NULL; p = p->next)
649 {
650 if (p->type == bfd_indirect_link_order)
651 {
652 asection *sec;
653
654 sec = p->u.indirect.section;
655
656 /* Mark all sections which are to be included in the
657 link. This will normally be every section. We need
658 to do this so that we can identify any sections which
659 the linker has decided to not include. */
660 sec->linker_mark = TRUE;
661
662 if (info->strip == strip_none
663 || info->strip == strip_some)
664 o->lineno_count += sec->lineno_count;
665
666 if (bfd_link_relocatable (info))
667 o->reloc_count += sec->reloc_count;
668
669 if (sec->rawsize > max_contents_size)
670 max_contents_size = sec->rawsize;
671 if (sec->size > max_contents_size)
672 max_contents_size = sec->size;
673 if (sec->lineno_count > max_lineno_count)
674 max_lineno_count = sec->lineno_count;
675 if (sec->reloc_count > max_reloc_count)
676 max_reloc_count = sec->reloc_count;
677 }
678 else if (bfd_link_relocatable (info)
679 && (p->type == bfd_section_reloc_link_order
680 || p->type == bfd_symbol_reloc_link_order))
681 ++o->reloc_count;
682 }
683 if (o->reloc_count == 0)
684 o->rel_filepos = 0;
685 else
686 {
687 o->flags |= SEC_RELOC;
688 o->rel_filepos = rel_filepos;
689 rel_filepos += o->reloc_count * relsz;
690 /* In PE COFF, if there are at least 0xffff relocations an
691 extra relocation will be written out to encode the count. */
692 if ((obj_pe (abfd) || obj_go32 (abfd)) && o->reloc_count >= 0xffff)
693 rel_filepos += relsz;
694 }
695
696 if (bfd_coff_long_section_names (abfd)
697 && strlen (o->name) > SCNNMLEN)
698 {
699 /* This section has a long name which must go in the string
700 table. This must correspond to the code in
701 coff_write_object_contents which puts the string index
702 into the s_name field of the section header. That is why
703 we pass hash as FALSE. */
704 if (_bfd_stringtab_add (flaginfo.strtab, o->name, FALSE, FALSE)
705 == (bfd_size_type) -1)
706 goto error_return;
707 long_section_names = TRUE;
708 }
709 }
710
711 /* If doing a relocatable link, allocate space for the pointers we
712 need to keep. */
713 if (bfd_link_relocatable (info))
714 {
715 unsigned int i;
716
717 /* We use section_count + 1, rather than section_count, because
718 the target_index fields are 1 based. */
719 amt = abfd->section_count + 1;
720 amt *= sizeof (struct coff_link_section_info);
721 flaginfo.section_info = (struct coff_link_section_info *) bfd_malloc (amt);
722 if (flaginfo.section_info == NULL)
723 goto error_return;
724 for (i = 0; i <= abfd->section_count; i++)
725 {
726 flaginfo.section_info[i].relocs = NULL;
727 flaginfo.section_info[i].rel_hashes = NULL;
728 }
729 }
730
731 /* We now know the size of the relocs, so we can determine the file
732 positions of the line numbers. */
733 line_filepos = rel_filepos;
734 linesz = bfd_coff_linesz (abfd);
735 max_output_reloc_count = 0;
736 for (o = abfd->sections; o != NULL; o = o->next)
737 {
738 if (o->lineno_count == 0)
739 o->line_filepos = 0;
740 else
741 {
742 o->line_filepos = line_filepos;
743 line_filepos += o->lineno_count * linesz;
744 }
745
746 if (o->reloc_count != 0)
747 {
748 /* We don't know the indices of global symbols until we have
749 written out all the local symbols. For each section in
750 the output file, we keep an array of pointers to hash
751 table entries. Each entry in the array corresponds to a
752 reloc. When we find a reloc against a global symbol, we
753 set the corresponding entry in this array so that we can
754 fix up the symbol index after we have written out all the
755 local symbols.
756
757 Because of this problem, we also keep the relocs in
758 memory until the end of the link. This wastes memory,
759 but only when doing a relocatable link, which is not the
760 common case. */
761 BFD_ASSERT (bfd_link_relocatable (info));
762 amt = o->reloc_count;
763 amt *= sizeof (struct internal_reloc);
764 flaginfo.section_info[o->target_index].relocs =
765 (struct internal_reloc *) bfd_malloc (amt);
766 amt = o->reloc_count;
767 amt *= sizeof (struct coff_link_hash_entry *);
768 flaginfo.section_info[o->target_index].rel_hashes =
769 (struct coff_link_hash_entry **) bfd_malloc (amt);
770 if (flaginfo.section_info[o->target_index].relocs == NULL
771 || flaginfo.section_info[o->target_index].rel_hashes == NULL)
772 goto error_return;
773
774 if (o->reloc_count > max_output_reloc_count)
775 max_output_reloc_count = o->reloc_count;
776 }
777
778 /* Reset the reloc and lineno counts, so that we can use them to
779 count the number of entries we have output so far. */
780 o->reloc_count = 0;
781 o->lineno_count = 0;
782 }
783
784 obj_sym_filepos (abfd) = line_filepos;
785
786 /* Figure out the largest number of symbols in an input BFD. Take
787 the opportunity to clear the output_has_begun fields of all the
788 input BFD's. */
789 max_sym_count = 0;
790 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
791 {
792 size_t sz;
793
794 sub->output_has_begun = FALSE;
795 sz = bfd_family_coff (sub) ? obj_raw_syment_count (sub) : 2;
796 if (sz > max_sym_count)
797 max_sym_count = sz;
798 }
799
800 /* Allocate some buffers used while linking. */
801 amt = max_sym_count * sizeof (struct internal_syment);
802 flaginfo.internal_syms = (struct internal_syment *) bfd_malloc (amt);
803 amt = max_sym_count * sizeof (asection *);
804 flaginfo.sec_ptrs = (asection **) bfd_malloc (amt);
805 amt = max_sym_count * sizeof (long);
806 flaginfo.sym_indices = (long int *) bfd_malloc (amt);
807 flaginfo.outsyms = (bfd_byte *) bfd_malloc ((max_sym_count + 1) * symesz);
808 amt = max_lineno_count * bfd_coff_linesz (abfd);
809 flaginfo.linenos = (bfd_byte *) bfd_malloc (amt);
810 flaginfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
811 amt = max_reloc_count * relsz;
812 flaginfo.external_relocs = (bfd_byte *) bfd_malloc (amt);
813 if (! bfd_link_relocatable (info))
814 {
815 amt = max_reloc_count * sizeof (struct internal_reloc);
816 flaginfo.internal_relocs = (struct internal_reloc *) bfd_malloc (amt);
817 }
818 if ((flaginfo.internal_syms == NULL && max_sym_count > 0)
819 || (flaginfo.sec_ptrs == NULL && max_sym_count > 0)
820 || (flaginfo.sym_indices == NULL && max_sym_count > 0)
821 || flaginfo.outsyms == NULL
822 || (flaginfo.linenos == NULL && max_lineno_count > 0)
823 || (flaginfo.contents == NULL && max_contents_size > 0)
824 || (flaginfo.external_relocs == NULL && max_reloc_count > 0)
825 || (! bfd_link_relocatable (info)
826 && flaginfo.internal_relocs == NULL
827 && max_reloc_count > 0))
828 goto error_return;
829
830 /* We now know the position of everything in the file, except that
831 we don't know the size of the symbol table and therefore we don't
832 know where the string table starts. We just build the string
833 table in memory as we go along. We process all the relocations
834 for a single input file at once. */
835 obj_raw_syment_count (abfd) = 0;
836
837 if (coff_backend_info (abfd)->_bfd_coff_start_final_link)
838 {
839 if (! bfd_coff_start_final_link (abfd, info))
840 goto error_return;
841 }
842
843 for (o = abfd->sections; o != NULL; o = o->next)
844 {
845 for (p = o->map_head.link_order; p != NULL; p = p->next)
846 {
847 if (p->type == bfd_indirect_link_order
848 && bfd_family_coff (p->u.indirect.section->owner))
849 {
850 sub = p->u.indirect.section->owner;
851 if (! bfd_coff_link_output_has_begun (sub, & flaginfo))
852 {
853 if (! _bfd_coff_link_input_bfd (&flaginfo, sub))
854 goto error_return;
855 sub->output_has_begun = TRUE;
856 }
857 }
858 else if (p->type == bfd_section_reloc_link_order
859 || p->type == bfd_symbol_reloc_link_order)
860 {
861 if (! _bfd_coff_reloc_link_order (abfd, &flaginfo, o, p))
862 goto error_return;
863 }
864 else
865 {
866 if (! _bfd_default_link_order (abfd, info, o, p))
867 goto error_return;
868 }
869 }
870 }
871
872 if (flaginfo.info->strip != strip_all && flaginfo.info->discard != discard_all)
873 {
874 /* Add local symbols from foreign inputs. */
875 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
876 {
877 unsigned int i;
878
879 if (bfd_family_coff (sub) || ! bfd_get_outsymbols (sub))
880 continue;
881 for (i = 0; i < bfd_get_symcount (sub); ++i)
882 {
883 asymbol *sym = bfd_get_outsymbols (sub) [i];
884 file_ptr pos;
885 struct internal_syment isym;
886 union internal_auxent iaux;
887 bfd_size_type string_size = 0, indx;
888 bfd_vma written = 0;
889 bfd_boolean rewrite = FALSE, hash;
890
891 if (! (sym->flags & BSF_LOCAL)
892 || (sym->flags & (BSF_SECTION_SYM | BSF_DEBUGGING_RELOC
893 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC
894 | BSF_SYNTHETIC))
895 || ((sym->flags & BSF_DEBUGGING)
896 && ! (sym->flags & BSF_FILE)))
897 continue;
898
899 /* See if we are discarding symbols with this name. */
900 if ((flaginfo.info->strip == strip_some
901 && (bfd_hash_lookup (flaginfo.info->keep_hash,
902 bfd_asymbol_name(sym), FALSE, FALSE)
903 == NULL))
904 || (((flaginfo.info->discard == discard_sec_merge
905 && (bfd_asymbol_section (sym)->flags & SEC_MERGE)
906 && ! bfd_link_relocatable (flaginfo.info))
907 || flaginfo.info->discard == discard_l)
908 && bfd_is_local_label_name (sub, bfd_asymbol_name(sym))))
909 continue;
910
911 pos = obj_sym_filepos (abfd) + obj_raw_syment_count (abfd)
912 * symesz;
913 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
914 goto error_return;
915 if (! coff_write_alien_symbol(abfd, sym, &isym, &iaux, &written,
916 &string_size, NULL, NULL))
917 goto error_return;
918
919 hash = !flaginfo.info->traditional_format;
920
921 if (string_size >= 6 && isym.n_sclass == C_FILE
922 && ! isym._n._n_n._n_zeroes && isym.n_numaux)
923 {
924 indx = _bfd_stringtab_add (flaginfo.strtab, ".file", hash,
925 FALSE);
926 if (indx == (bfd_size_type) -1)
927 goto error_return;
928 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
929 bfd_coff_swap_sym_out (abfd, &isym, flaginfo.outsyms);
930 if (bfd_seek (abfd, pos, SEEK_SET) != 0
931 || bfd_bwrite (flaginfo.outsyms, symesz,
932 abfd) != symesz)
933 goto error_return;
934 string_size -= 6;
935 }
936
937 if (string_size)
938 {
939 indx = _bfd_stringtab_add (flaginfo.strtab,
940 bfd_asymbol_name (sym), hash,
941 FALSE);
942 if (indx == (bfd_size_type) -1)
943 goto error_return;
944 if (isym.n_sclass != C_FILE)
945 {
946 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
947 bfd_coff_swap_sym_out (abfd, &isym, flaginfo.outsyms);
948 rewrite = TRUE;
949 }
950 else
951 {
952 BFD_ASSERT (isym.n_numaux == 1);
953 iaux.x_file.x_n.x_offset = STRING_SIZE_SIZE + indx;
954 bfd_coff_swap_aux_out (abfd, &iaux, isym.n_type, C_FILE,
955 0, 1, flaginfo.outsyms + symesz);
956 if (bfd_seek (abfd, pos + symesz, SEEK_SET) != 0
957 || bfd_bwrite (flaginfo.outsyms + symesz, symesz,
958 abfd) != symesz)
959 goto error_return;
960 }
961 }
962
963 if (isym.n_sclass == C_FILE)
964 {
965 if (flaginfo.last_file_index != -1)
966 {
967 flaginfo.last_file.n_value = obj_raw_syment_count (abfd);
968 bfd_coff_swap_sym_out (abfd, &flaginfo.last_file,
969 flaginfo.outsyms);
970 pos = obj_sym_filepos (abfd) + flaginfo.last_file_index
971 * symesz;
972 rewrite = TRUE;
973 }
974 flaginfo.last_file_index = obj_raw_syment_count (abfd);
975 flaginfo.last_file = isym;
976 }
977
978 if (rewrite
979 && (bfd_seek (abfd, pos, SEEK_SET) != 0
980 || bfd_bwrite (flaginfo.outsyms, symesz, abfd) != symesz))
981 goto error_return;
982
983 obj_raw_syment_count (abfd) += written;
984 }
985 }
986 }
987
988 if (! bfd_coff_final_link_postscript (abfd, & flaginfo))
989 goto error_return;
990
991 /* Free up the buffers used by _bfd_coff_link_input_bfd. */
992
993 coff_debug_merge_hash_table_free (&flaginfo.debug_merge);
994 debug_merge_allocated = FALSE;
995
996 free (flaginfo.internal_syms);
997 flaginfo.internal_syms = NULL;
998 free (flaginfo.sec_ptrs);
999 flaginfo.sec_ptrs = NULL;
1000 free (flaginfo.sym_indices);
1001 flaginfo.sym_indices = NULL;
1002 free (flaginfo.linenos);
1003 flaginfo.linenos = NULL;
1004 free (flaginfo.contents);
1005 flaginfo.contents = NULL;
1006 free (flaginfo.external_relocs);
1007 flaginfo.external_relocs = NULL;
1008 free (flaginfo.internal_relocs);
1009 flaginfo.internal_relocs = NULL;
1010
1011 /* The value of the last C_FILE symbol is supposed to be the symbol
1012 index of the first external symbol. Write it out again if
1013 necessary. */
1014 if (flaginfo.last_file_index != -1
1015 && (unsigned int) flaginfo.last_file.n_value != obj_raw_syment_count (abfd))
1016 {
1017 file_ptr pos;
1018
1019 flaginfo.last_file.n_value = obj_raw_syment_count (abfd);
1020 bfd_coff_swap_sym_out (abfd, &flaginfo.last_file,
1021 flaginfo.outsyms);
1022
1023 pos = obj_sym_filepos (abfd) + flaginfo.last_file_index * symesz;
1024 if (bfd_seek (abfd, pos, SEEK_SET) != 0
1025 || bfd_bwrite (flaginfo.outsyms, symesz, abfd) != symesz)
1026 return FALSE;
1027 }
1028
1029 /* If doing task linking (ld --task-link) then make a pass through the
1030 global symbols, writing out any that are defined, and making them
1031 static. */
1032 if (info->task_link)
1033 {
1034 flaginfo.failed = FALSE;
1035 coff_link_hash_traverse (coff_hash_table (info),
1036 _bfd_coff_write_task_globals, &flaginfo);
1037 if (flaginfo.failed)
1038 goto error_return;
1039 }
1040
1041 /* Write out the global symbols. */
1042 flaginfo.failed = FALSE;
1043 bfd_hash_traverse (&info->hash->table, _bfd_coff_write_global_sym, &flaginfo);
1044 if (flaginfo.failed)
1045 goto error_return;
1046
1047 /* The outsyms buffer is used by _bfd_coff_write_global_sym. */
1048 free (flaginfo.outsyms);
1049 flaginfo.outsyms = NULL;
1050
1051 if (bfd_link_relocatable (info) && max_output_reloc_count > 0)
1052 {
1053 /* Now that we have written out all the global symbols, we know
1054 the symbol indices to use for relocs against them, and we can
1055 finally write out the relocs. */
1056 amt = max_output_reloc_count * relsz;
1057 external_relocs = (bfd_byte *) bfd_malloc (amt);
1058 if (external_relocs == NULL)
1059 goto error_return;
1060
1061 for (o = abfd->sections; o != NULL; o = o->next)
1062 {
1063 struct internal_reloc *irel;
1064 struct internal_reloc *irelend;
1065 struct coff_link_hash_entry **rel_hash;
1066 bfd_byte *erel;
1067
1068 if (o->reloc_count == 0)
1069 continue;
1070
1071 irel = flaginfo.section_info[o->target_index].relocs;
1072 irelend = irel + o->reloc_count;
1073 rel_hash = flaginfo.section_info[o->target_index].rel_hashes;
1074 erel = external_relocs;
1075 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
1076 {
1077 if (*rel_hash != NULL)
1078 {
1079 BFD_ASSERT ((*rel_hash)->indx >= 0);
1080 irel->r_symndx = (*rel_hash)->indx;
1081 }
1082 bfd_coff_swap_reloc_out (abfd, irel, erel);
1083 }
1084
1085 if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0)
1086 goto error_return;
1087 if ((obj_pe (abfd) || obj_go32 (abfd)) && o->reloc_count >= 0xffff)
1088 {
1089 /* In PE COFF, write the count of relocs as the first
1090 reloc. The header overflow bit will be set
1091 elsewhere. */
1092 struct internal_reloc incount;
1093 bfd_byte *excount = (bfd_byte *)bfd_malloc (relsz);
1094
1095 memset (&incount, 0, sizeof (incount));
1096 incount.r_vaddr = o->reloc_count + 1;
1097 bfd_coff_swap_reloc_out (abfd, &incount, excount);
1098 if (bfd_bwrite (excount, relsz, abfd) != relsz)
1099 /* We'll leak, but it's an error anyway. */
1100 goto error_return;
1101 free (excount);
1102 }
1103 if (bfd_bwrite (external_relocs,
1104 (bfd_size_type) relsz * o->reloc_count, abfd)
1105 != (bfd_size_type) relsz * o->reloc_count)
1106 goto error_return;
1107 }
1108
1109 free (external_relocs);
1110 external_relocs = NULL;
1111 }
1112
1113 /* Free up the section information. */
1114 if (flaginfo.section_info != NULL)
1115 {
1116 unsigned int i;
1117
1118 for (i = 0; i < abfd->section_count; i++)
1119 {
1120 free (flaginfo.section_info[i].relocs);
1121 free (flaginfo.section_info[i].rel_hashes);
1122 }
1123 free (flaginfo.section_info);
1124 flaginfo.section_info = NULL;
1125 }
1126
1127 /* If we have optimized stabs strings, output them. */
1128 if (coff_hash_table (info)->stab_info.stabstr != NULL)
1129 {
1130 if (! _bfd_write_stab_strings (abfd, &coff_hash_table (info)->stab_info))
1131 return FALSE;
1132 }
1133
1134 /* Write out the string table. */
1135 if (obj_raw_syment_count (abfd) != 0 || long_section_names)
1136 {
1137 file_ptr pos;
1138
1139 pos = obj_sym_filepos (abfd) + obj_raw_syment_count (abfd) * symesz;
1140 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
1141 return FALSE;
1142
1143 #if STRING_SIZE_SIZE == 4
1144 H_PUT_32 (abfd,
1145 _bfd_stringtab_size (flaginfo.strtab) + STRING_SIZE_SIZE,
1146 strbuf);
1147 #else
1148 #error Change H_PUT_32 above
1149 #endif
1150
1151 if (bfd_bwrite (strbuf, (bfd_size_type) STRING_SIZE_SIZE, abfd)
1152 != STRING_SIZE_SIZE)
1153 return FALSE;
1154
1155 if (! _bfd_stringtab_emit (abfd, flaginfo.strtab))
1156 return FALSE;
1157
1158 obj_coff_strings_written (abfd) = TRUE;
1159 }
1160
1161 _bfd_stringtab_free (flaginfo.strtab);
1162
1163 /* Setting symcount to 0 will cause write_object_contents to
1164 not try to write out the symbols. */
1165 abfd->symcount = 0;
1166
1167 return TRUE;
1168
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)
1175 {
1176 unsigned int i;
1177
1178 for (i = 0; i < abfd->section_count; i++)
1179 {
1180 free (flaginfo.section_info[i].relocs);
1181 free (flaginfo.section_info[i].rel_hashes);
1182 }
1183 free (flaginfo.section_info);
1184 }
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;
1195 }
1196
1197 /* Parse out a -heap <reserved>,<commit> line. */
1198
1199 static char *
1200 dores_com (char *ptr, bfd *output_bfd, int heap)
1201 {
1202 if (coff_data(output_bfd)->pe)
1203 {
1204 int val = strtoul (ptr, &ptr, 0);
1205
1206 if (heap)
1207 pe_data(output_bfd)->pe_opthdr.SizeOfHeapReserve = val;
1208 else
1209 pe_data(output_bfd)->pe_opthdr.SizeOfStackReserve = val;
1210
1211 if (ptr[0] == ',')
1212 {
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;
1218 }
1219 }
1220 return ptr;
1221 }
1222
1223 static char *
1224 get_name (char *ptr, char **dst)
1225 {
1226 while (*ptr == ' ')
1227 ptr++;
1228 *dst = ptr;
1229 while (*ptr && *ptr != ' ')
1230 ptr++;
1231 *ptr = 0;
1232 return ptr+1;
1233 }
1234
1235 /* Process any magic embedded commands in a section called .drectve. */
1236
1237 static int
1238 process_embedded_commands (bfd *output_bfd,
1239 struct bfd_link_info *info ATTRIBUTE_UNUSED,
1240 bfd *abfd)
1241 {
1242 asection *sec = bfd_get_section_by_name (abfd, ".drectve");
1243 char *s;
1244 char *e;
1245 bfd_byte *copy;
1246
1247 if (!sec)
1248 return 1;
1249
1250 if (!bfd_malloc_and_get_section (abfd, sec, &copy))
1251 {
1252 free (copy);
1253 return 0;
1254 }
1255 e = (char *) copy + sec->size;
1256
1257 for (s = (char *) copy; s < e ; )
1258 {
1259 if (s[0] != '-')
1260 {
1261 s++;
1262 continue;
1263 }
1264 if (CONST_STRNEQ (s, "-attr"))
1265 {
1266 char *name;
1267 char *attribs;
1268 asection *asec;
1269 int loop = 1;
1270 int had_write = 0;
1271 int had_exec= 0;
1272
1273 s += 5;
1274 s = get_name (s, &name);
1275 s = get_name (s, &attribs);
1276
1277 while (loop)
1278 {
1279 switch (*attribs++)
1280 {
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;
1293 }
1294 }
1295 asec = bfd_get_section_by_name (abfd, name);
1296 if (asec)
1297 {
1298 if (had_exec)
1299 asec->flags |= SEC_CODE;
1300 if (!had_write)
1301 asec->flags |= SEC_READONLY;
1302 }
1303 }
1304 else if (CONST_STRNEQ (s, "-heap"))
1305 s = dores_com (s + 5, output_bfd, 1);
1306
1307 else if (CONST_STRNEQ (s, "-stack"))
1308 s = dores_com (s + 6, output_bfd, 0);
1309
1310 /* GNU extension for aligned commons. */
1311 else if (CONST_STRNEQ (s, "-aligncomm:"))
1312 {
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;
1317 }
1318
1319 else
1320 s++;
1321 }
1322 free (copy);
1323 return 1;
1324 }
1325
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. */
1330
1331 static void
1332 mark_relocs (struct coff_final_link_info *flaginfo, bfd *input_bfd)
1333 {
1334 asection * a;
1335
1336 if ((bfd_get_file_flags (input_bfd) & HAS_SYMS) == 0)
1337 return;
1338
1339 for (a = input_bfd->sections; a != (asection *) NULL; a = a->next)
1340 {
1341 struct internal_reloc * internal_relocs;
1342 struct internal_reloc * irel;
1343 struct internal_reloc * irelend;
1344
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;
1351
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)
1360 );
1361
1362 if (internal_relocs == NULL)
1363 continue;
1364
1365 irel = internal_relocs;
1366 irelend = irel + a->reloc_count;
1367
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;
1375 }
1376 }
1377
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. */
1380
1381 bfd_boolean
1382 _bfd_coff_link_input_bfd (struct coff_final_link_info *flaginfo, bfd *input_bfd)
1383 {
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 bfd_boolean (*adjust_symndx)
1387 (bfd *, struct bfd_link_info *, bfd *, asection *,
1388 struct internal_reloc *, bfd_boolean *);
1389 bfd *output_bfd;
1390 const char *strings;
1391 bfd_size_type syment_base;
1392 bfd_boolean 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;
1405
1406 /* Move all the symbols to the output file. */
1407
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));
1415
1416 copy = FALSE;
1417 if (! flaginfo->info->keep_memory)
1418 copy = TRUE;
1419 hash = TRUE;
1420 if (flaginfo->info->traditional_format)
1421 hash = FALSE;
1422
1423 if (! _bfd_coff_get_external_symbols (input_bfd))
1424 return FALSE;
1425
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;
1433
1434 if (coff_data (output_bfd)->pe
1435 && ! process_embedded_commands (output_bfd, flaginfo->info, input_bfd))
1436 return FALSE;
1437
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))
1444 {
1445 /* Mark the symbol array as 'not-used'. */
1446 memset (indexp, 0, obj_raw_syment_count (input_bfd) * sizeof * indexp);
1447
1448 mark_relocs (flaginfo, input_bfd);
1449 }
1450
1451 while (esym < esym_end)
1452 {
1453 struct internal_syment isym;
1454 enum coff_symbol_classification classification;
1455 bfd_boolean skip;
1456 bfd_boolean global;
1457 bfd_boolean dont_skip_symbol;
1458 int add;
1459
1460 bfd_coff_swap_sym_in (input_bfd, esym, isymp);
1461
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;
1467
1468 classification = bfd_coff_classify_symbol (input_bfd, &isym);
1469 switch (classification)
1470 {
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;
1484 }
1485
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;
1494
1495 *indexp = -1;
1496
1497 skip = FALSE;
1498 global = FALSE;
1499 add = 1 + isym.n_numaux;
1500
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;
1504
1505 if (! skip)
1506 {
1507 switch (classification)
1508 {
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;
1522
1523 case COFF_SYMBOL_UNDEFINED:
1524 /* Undefined symbols are left for the end. */
1525 global = TRUE;
1526 skip = TRUE;
1527 break;
1528
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;
1535 }
1536 }
1537
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
1551
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;
1571
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))
1577 {
1578 const char *name;
1579 char buf[SYMNMLEN + 1];
1580
1581 name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf);
1582 if (name == NULL)
1583 return FALSE;
1584
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;
1593 }
1594
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)
1603 {
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;
1613
1614 name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf);
1615 if (name == NULL)
1616 return FALSE;
1617
1618 /* Ignore fake names invented by compiler; treat them all as
1619 the same name. */
1620 if (*name == '~' || *name == '.' || *name == '$'
1621 || (*name == bfd_get_symbol_leading_char (input_bfd)
1622 && (name[1] == '~' || name[1] == '.' || name[1] == '$')))
1623 name = "";
1624
1625 mh = coff_debug_merge_hash_lookup (&flaginfo->debug_merge, name,
1626 TRUE, TRUE);
1627 if (mh == NULL)
1628 return FALSE;
1629
1630 /* Allocate memory to hold type information. If this turns
1631 out to be a duplicate, we pass this address to
1632 bfd_release. */
1633 amt = sizeof (struct coff_debug_merge_type);
1634 mt = (struct coff_debug_merge_type *) bfd_alloc (input_bfd, amt);
1635 if (mt == NULL)
1636 return FALSE;
1637 mt->type_class = isym.n_sclass;
1638
1639 /* Pick up the aux entry, which points to the end of the tag
1640 entries. */
1641 bfd_coff_swap_aux_in (input_bfd, (esym + isymesz),
1642 isym.n_type, isym.n_sclass, 0, isym.n_numaux,
1643 &aux);
1644
1645 /* Gather the elements. */
1646 epp = &mt->elements;
1647 mt->elements = NULL;
1648 islp = isymp + 2;
1649 esl = esym + 2 * isymesz;
1650 eslend = ((bfd_byte *) obj_coff_external_syms (input_bfd)
1651 + aux.x_sym.x_fcnary.x_fcn.x_endndx.l * isymesz);
1652 while (esl < eslend)
1653 {
1654 const char *elename;
1655 char elebuf[SYMNMLEN + 1];
1656 char *name_copy;
1657
1658 bfd_coff_swap_sym_in (input_bfd, esl, islp);
1659
1660 amt = sizeof (struct coff_debug_merge_element);
1661 *epp = (struct coff_debug_merge_element *)
1662 bfd_alloc (input_bfd, amt);
1663 if (*epp == NULL)
1664 return FALSE;
1665
1666 elename = _bfd_coff_internal_syment_name (input_bfd, islp,
1667 elebuf);
1668 if (elename == NULL)
1669 return FALSE;
1670
1671 amt = strlen (elename) + 1;
1672 name_copy = (char *) bfd_alloc (input_bfd, amt);
1673 if (name_copy == NULL)
1674 return FALSE;
1675 strcpy (name_copy, elename);
1676
1677 (*epp)->name = name_copy;
1678 (*epp)->type = islp->n_type;
1679 (*epp)->tagndx = 0;
1680 if (islp->n_numaux >= 1
1681 && islp->n_type != T_NULL
1682 && islp->n_sclass != C_EOS)
1683 {
1684 union internal_auxent eleaux;
1685 long indx;
1686
1687 bfd_coff_swap_aux_in (input_bfd, (esl + isymesz),
1688 islp->n_type, islp->n_sclass, 0,
1689 islp->n_numaux, &eleaux);
1690 indx = eleaux.x_sym.x_tagndx.l;
1691
1692 /* FIXME: If this tagndx entry refers to a symbol
1693 defined later in this file, we just ignore it.
1694 Handling this correctly would be tedious, and may
1695 not be required. */
1696 if (indx > 0
1697 && (indx
1698 < ((esym -
1699 (bfd_byte *) obj_coff_external_syms (input_bfd))
1700 / (long) isymesz)))
1701 {
1702 (*epp)->tagndx = flaginfo->sym_indices[indx];
1703 if ((*epp)->tagndx < 0)
1704 (*epp)->tagndx = 0;
1705 }
1706 }
1707 epp = &(*epp)->next;
1708 *epp = NULL;
1709
1710 esl += (islp->n_numaux + 1) * isymesz;
1711 islp += islp->n_numaux + 1;
1712 }
1713
1714 /* See if we already have a definition which matches this
1715 type. We always output the type if it has no elements,
1716 for simplicity. */
1717 if (mt->elements == NULL)
1718 bfd_release (input_bfd, mt);
1719 else
1720 {
1721 struct coff_debug_merge_type *mtl;
1722
1723 for (mtl = mh->types; mtl != NULL; mtl = mtl->next)
1724 {
1725 struct coff_debug_merge_element *me, *mel;
1726
1727 if (mtl->type_class != mt->type_class)
1728 continue;
1729
1730 for (me = mt->elements, mel = mtl->elements;
1731 me != NULL && mel != NULL;
1732 me = me->next, mel = mel->next)
1733 {
1734 if (strcmp (me->name, mel->name) != 0
1735 || me->type != mel->type
1736 || me->tagndx != mel->tagndx)
1737 break;
1738 }
1739
1740 if (me == NULL && mel == NULL)
1741 break;
1742 }
1743
1744 if (mtl == NULL || (bfd_size_type) mtl->indx >= syment_base)
1745 {
1746 /* This is the first definition of this type. */
1747 mt->indx = output_index;
1748 mt->next = mh->types;
1749 mh->types = mt;
1750 }
1751 else
1752 {
1753 /* This is a redefinition which can be merged. */
1754 bfd_release (input_bfd, mt);
1755 *indexp = mtl->indx;
1756 add = (eslend - esym) / isymesz;
1757 skip = TRUE;
1758 }
1759 }
1760 }
1761
1762 /* We now know whether we are to skip this symbol or not. */
1763 if (! skip)
1764 {
1765 /* Adjust the symbol in order to output it. */
1766
1767 if (isym._n._n_n._n_zeroes == 0
1768 && isym._n._n_n._n_offset != 0)
1769 {
1770 const char *name;
1771 bfd_size_type indx;
1772
1773 /* This symbol has a long name. Enter it in the string
1774 table we are building. Note that we do not check
1775 bfd_coff_symname_in_debug. That is only true for
1776 XCOFF, and XCOFF requires different linking code
1777 anyhow. */
1778 name = _bfd_coff_internal_syment_name (input_bfd, &isym, NULL);
1779 if (name == NULL)
1780 return FALSE;
1781 indx = _bfd_stringtab_add (flaginfo->strtab, name, hash, copy);
1782 if (indx == (bfd_size_type) -1)
1783 return FALSE;
1784 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
1785 }
1786
1787 switch (isym.n_sclass)
1788 {
1789 case C_AUTO:
1790 case C_MOS:
1791 case C_EOS:
1792 case C_MOE:
1793 case C_MOU:
1794 case C_UNTAG:
1795 case C_STRTAG:
1796 case C_ENTAG:
1797 case C_TPDEF:
1798 case C_ARG:
1799 case C_USTATIC:
1800 case C_REG:
1801 case C_REGPARM:
1802 case C_FIELD:
1803 /* The symbol value should not be modified. */
1804 break;
1805
1806 case C_FCN:
1807 if (obj_pe (input_bfd)
1808 && memcmp (isym.n_name, ".bf", sizeof ".bf") != 0
1809 && isym.n_scnum > 0)
1810 {
1811 /* For PE, .lf and .ef get their value left alone,
1812 while .bf gets relocated. However, they all have
1813 "real" section numbers, and need to be moved into
1814 the new section. */
1815 isym.n_scnum = (*secpp)->output_section->target_index;
1816 break;
1817 }
1818 /* Fall through. */
1819 default:
1820 case C_LABEL: /* Not completely sure about these 2 */
1821 case C_EXTDEF:
1822 case C_BLOCK:
1823 case C_EFCN:
1824 case C_NULL:
1825 case C_EXT:
1826 case C_STAT:
1827 case C_SECTION:
1828 case C_NT_WEAK:
1829 /* Compute new symbol location. */
1830 if (isym.n_scnum > 0)
1831 {
1832 isym.n_scnum = (*secpp)->output_section->target_index;
1833 isym.n_value += (*secpp)->output_offset;
1834 if (! obj_pe (input_bfd))
1835 isym.n_value -= (*secpp)->vma;
1836 if (! obj_pe (flaginfo->output_bfd))
1837 isym.n_value += (*secpp)->output_section->vma;
1838 }
1839 break;
1840
1841 case C_FILE:
1842 /* The value of a C_FILE symbol is the symbol index of
1843 the next C_FILE symbol. The value of the last C_FILE
1844 symbol is the symbol index to the first external
1845 symbol (actually, coff_renumber_symbols does not get
1846 this right--it just sets the value of the last C_FILE
1847 symbol to zero--and nobody has ever complained about
1848 it). We try to get this right, below, just before we
1849 write the symbols out, but in the general case we may
1850 have to write the symbol out twice. */
1851 if (flaginfo->last_file_index != -1
1852 && flaginfo->last_file.n_value != (bfd_vma) output_index)
1853 {
1854 /* We must correct the value of the last C_FILE
1855 entry. */
1856 flaginfo->last_file.n_value = output_index;
1857 if ((bfd_size_type) flaginfo->last_file_index >= syment_base)
1858 {
1859 /* The last C_FILE symbol is in this input file. */
1860 bfd_coff_swap_sym_out (output_bfd,
1861 &flaginfo->last_file,
1862 (flaginfo->outsyms
1863 + ((flaginfo->last_file_index
1864 - syment_base)
1865 * osymesz)));
1866 }
1867 else
1868 {
1869 file_ptr pos;
1870
1871 /* We have already written out the last C_FILE
1872 symbol. We need to write it out again. We
1873 borrow *outsym temporarily. */
1874 bfd_coff_swap_sym_out (output_bfd,
1875 &flaginfo->last_file, outsym);
1876 pos = obj_sym_filepos (output_bfd);
1877 pos += flaginfo->last_file_index * osymesz;
1878 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
1879 || bfd_bwrite (outsym, osymesz, output_bfd) != osymesz)
1880 return FALSE;
1881 }
1882 }
1883
1884 flaginfo->last_file_index = output_index;
1885 flaginfo->last_file = isym;
1886 break;
1887 }
1888
1889 /* If doing task linking, convert normal global function symbols to
1890 static functions. */
1891 if (flaginfo->info->task_link && IS_EXTERNAL (input_bfd, isym))
1892 isym.n_sclass = C_STAT;
1893
1894 /* Output the symbol. */
1895 bfd_coff_swap_sym_out (output_bfd, &isym, outsym);
1896
1897 *indexp = output_index;
1898
1899 if (global)
1900 {
1901 long indx;
1902 struct coff_link_hash_entry *h;
1903
1904 indx = ((esym - (bfd_byte *) obj_coff_external_syms (input_bfd))
1905 / isymesz);
1906 h = obj_coff_sym_hashes (input_bfd)[indx];
1907 if (h == NULL)
1908 {
1909 /* This can happen if there were errors earlier in
1910 the link. */
1911 bfd_set_error (bfd_error_bad_value);
1912 return FALSE;
1913 }
1914 h->indx = output_index;
1915 }
1916
1917 output_index += add;
1918 outsym += add * osymesz;
1919 }
1920
1921 esym += add * isymesz;
1922 isymp += add;
1923 ++secpp;
1924 ++indexp;
1925 for (--add; add > 0; --add)
1926 {
1927 *secpp++ = NULL;
1928 *indexp++ = -1;
1929 }
1930 }
1931
1932 /* Fix up the aux entries. This must be done in a separate pass,
1933 because we don't know the correct symbol indices until we have
1934 already decided which symbols we are going to keep. */
1935 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
1936 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
1937 isymp = flaginfo->internal_syms;
1938 indexp = flaginfo->sym_indices;
1939 sym_hash = obj_coff_sym_hashes (input_bfd);
1940 outsym = flaginfo->outsyms;
1941
1942 while (esym < esym_end)
1943 {
1944 int add;
1945
1946 add = 1 + isymp->n_numaux;
1947
1948 if ((*indexp < 0
1949 || (bfd_size_type) *indexp < syment_base)
1950 && (*sym_hash == NULL
1951 || (*sym_hash)->auxbfd != input_bfd))
1952 esym += add * isymesz;
1953 else
1954 {
1955 struct coff_link_hash_entry *h;
1956 int i;
1957
1958 h = NULL;
1959 if (*indexp < 0)
1960 {
1961 h = *sym_hash;
1962
1963 /* The m68k-motorola-sysv assembler will sometimes
1964 generate two symbols with the same name, but only one
1965 will have aux entries. */
1966 BFD_ASSERT (isymp->n_numaux == 0
1967 || h->numaux == 0
1968 || h->numaux == isymp->n_numaux);
1969 }
1970
1971 esym += isymesz;
1972
1973 if (h == NULL)
1974 outsym += osymesz;
1975
1976 /* Handle the aux entries. This handling is based on
1977 coff_pointerize_aux. I don't know if it always correct. */
1978 for (i = 0; i < isymp->n_numaux && esym < esym_end; i++)
1979 {
1980 union internal_auxent aux;
1981 union internal_auxent *auxp;
1982
1983 if (h != NULL && h->aux != NULL && (h->numaux > i))
1984 auxp = h->aux + i;
1985 else
1986 {
1987 bfd_coff_swap_aux_in (input_bfd, esym, isymp->n_type,
1988 isymp->n_sclass, i, isymp->n_numaux, &aux);
1989 auxp = &aux;
1990 }
1991
1992 if (isymp->n_sclass == C_FILE)
1993 {
1994 /* If this is a long filename, we must put it in the
1995 string table. */
1996 if (auxp->x_file.x_n.x_zeroes == 0
1997 && auxp->x_file.x_n.x_offset != 0)
1998 {
1999 const char *filename;
2000 bfd_size_type indx;
2001
2002 BFD_ASSERT (auxp->x_file.x_n.x_offset
2003 >= STRING_SIZE_SIZE);
2004 if (strings == NULL)
2005 {
2006 strings = _bfd_coff_read_string_table (input_bfd);
2007 if (strings == NULL)
2008 return FALSE;
2009 }
2010 if ((bfd_size_type) auxp->x_file.x_n.x_offset >= obj_coff_strings_len (input_bfd))
2011 filename = _("<corrupt>");
2012 else
2013 filename = strings + auxp->x_file.x_n.x_offset;
2014 indx = _bfd_stringtab_add (flaginfo->strtab, filename,
2015 hash, copy);
2016 if (indx == (bfd_size_type) -1)
2017 return FALSE;
2018 auxp->x_file.x_n.x_offset = STRING_SIZE_SIZE + indx;
2019 }
2020 }
2021 else if ((isymp->n_sclass != C_STAT || isymp->n_type != T_NULL)
2022 && isymp->n_sclass != C_NT_WEAK)
2023 {
2024 unsigned long indx;
2025
2026 if (ISFCN (isymp->n_type)
2027 || ISTAG (isymp->n_sclass)
2028 || isymp->n_sclass == C_BLOCK
2029 || isymp->n_sclass == C_FCN)
2030 {
2031 indx = auxp->x_sym.x_fcnary.x_fcn.x_endndx.l;
2032 if (indx > 0
2033 && indx < obj_raw_syment_count (input_bfd))
2034 {
2035 /* We look forward through the symbol for
2036 the index of the next symbol we are going
2037 to include. I don't know if this is
2038 entirely right. */
2039 while ((flaginfo->sym_indices[indx] < 0
2040 || ((bfd_size_type) flaginfo->sym_indices[indx]
2041 < syment_base))
2042 && indx < obj_raw_syment_count (input_bfd))
2043 ++indx;
2044 if (indx >= obj_raw_syment_count (input_bfd))
2045 indx = output_index;
2046 else
2047 indx = flaginfo->sym_indices[indx];
2048 auxp->x_sym.x_fcnary.x_fcn.x_endndx.l = indx;
2049 }
2050 }
2051
2052 indx = auxp->x_sym.x_tagndx.l;
2053 if (indx > 0 && indx < obj_raw_syment_count (input_bfd))
2054 {
2055 long symindx;
2056
2057 symindx = flaginfo->sym_indices[indx];
2058 if (symindx < 0)
2059 auxp->x_sym.x_tagndx.l = 0;
2060 else
2061 auxp->x_sym.x_tagndx.l = symindx;
2062 }
2063
2064 /* The .bf symbols are supposed to be linked through
2065 the endndx field. We need to carry this list
2066 across object files. */
2067 if (i == 0
2068 && h == NULL
2069 && isymp->n_sclass == C_FCN
2070 && (isymp->_n._n_n._n_zeroes != 0
2071 || isymp->_n._n_n._n_offset == 0)
2072 && isymp->_n._n_name[0] == '.'
2073 && isymp->_n._n_name[1] == 'b'
2074 && isymp->_n._n_name[2] == 'f'
2075 && isymp->_n._n_name[3] == '\0')
2076 {
2077 if (flaginfo->last_bf_index != -1)
2078 {
2079 flaginfo->last_bf.x_sym.x_fcnary.x_fcn.x_endndx.l =
2080 *indexp;
2081
2082 if ((bfd_size_type) flaginfo->last_bf_index
2083 >= syment_base)
2084 {
2085 void *auxout;
2086
2087 /* The last .bf symbol is in this input
2088 file. This will only happen if the
2089 assembler did not set up the .bf
2090 endndx symbols correctly. */
2091 auxout = (flaginfo->outsyms
2092 + ((flaginfo->last_bf_index
2093 - syment_base)
2094 * osymesz));
2095
2096 bfd_coff_swap_aux_out (output_bfd,
2097 &flaginfo->last_bf,
2098 isymp->n_type,
2099 isymp->n_sclass,
2100 0, isymp->n_numaux,
2101 auxout);
2102 }
2103 else
2104 {
2105 file_ptr pos;
2106
2107 /* We have already written out the last
2108 .bf aux entry. We need to write it
2109 out again. We borrow *outsym
2110 temporarily. FIXME: This case should
2111 be made faster. */
2112 bfd_coff_swap_aux_out (output_bfd,
2113 &flaginfo->last_bf,
2114 isymp->n_type,
2115 isymp->n_sclass,
2116 0, isymp->n_numaux,
2117 outsym);
2118 pos = obj_sym_filepos (output_bfd);
2119 pos += flaginfo->last_bf_index * osymesz;
2120 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
2121 || (bfd_bwrite (outsym, osymesz, output_bfd)
2122 != osymesz))
2123 return FALSE;
2124 }
2125 }
2126
2127 if (auxp->x_sym.x_fcnary.x_fcn.x_endndx.l != 0)
2128 flaginfo->last_bf_index = -1;
2129 else
2130 {
2131 /* The endndx field of this aux entry must
2132 be updated with the symbol number of the
2133 next .bf symbol. */
2134 flaginfo->last_bf = *auxp;
2135 flaginfo->last_bf_index = (((outsym - flaginfo->outsyms)
2136 / osymesz)
2137 + syment_base);
2138 }
2139 }
2140 }
2141
2142 if (h == NULL)
2143 {
2144 bfd_coff_swap_aux_out (output_bfd, auxp, isymp->n_type,
2145 isymp->n_sclass, i, isymp->n_numaux,
2146 outsym);
2147 outsym += osymesz;
2148 }
2149
2150 esym += isymesz;
2151 }
2152 }
2153
2154 indexp += add;
2155 isymp += add;
2156 sym_hash += add;
2157 }
2158
2159 /* Relocate the line numbers, unless we are stripping them. */
2160 if (flaginfo->info->strip == strip_none
2161 || flaginfo->info->strip == strip_some)
2162 {
2163 for (o = input_bfd->sections; o != NULL; o = o->next)
2164 {
2165 bfd_vma offset;
2166 bfd_byte *eline;
2167 bfd_byte *elineend;
2168 bfd_byte *oeline;
2169 bfd_boolean skipping;
2170 file_ptr pos;
2171 bfd_size_type amt;
2172
2173 /* FIXME: If SEC_HAS_CONTENTS is not for the section, then
2174 build_link_order in ldwrite.c will not have created a
2175 link order, which means that we will not have seen this
2176 input section in _bfd_coff_final_link, which means that
2177 we will not have allocated space for the line numbers of
2178 this section. I don't think line numbers can be
2179 meaningful for a section which does not have
2180 SEC_HAS_CONTENTS set, but, if they do, this must be
2181 changed. */
2182 if (o->lineno_count == 0
2183 || (o->output_section->flags & SEC_HAS_CONTENTS) == 0)
2184 continue;
2185
2186 if (bfd_seek (input_bfd, o->line_filepos, SEEK_SET) != 0
2187 || bfd_bread (flaginfo->linenos, linesz * o->lineno_count,
2188 input_bfd) != linesz * o->lineno_count)
2189 return FALSE;
2190
2191 offset = o->output_section->vma + o->output_offset - o->vma;
2192 eline = flaginfo->linenos;
2193 oeline = flaginfo->linenos;
2194 elineend = eline + linesz * o->lineno_count;
2195 skipping = FALSE;
2196 for (; eline < elineend; eline += linesz)
2197 {
2198 struct internal_lineno iline;
2199
2200 bfd_coff_swap_lineno_in (input_bfd, eline, &iline);
2201
2202 if (iline.l_lnno != 0)
2203 iline.l_addr.l_paddr += offset;
2204 else if (iline.l_addr.l_symndx >= 0
2205 && ((unsigned long) iline.l_addr.l_symndx
2206 < obj_raw_syment_count (input_bfd)))
2207 {
2208 long indx;
2209
2210 indx = flaginfo->sym_indices[iline.l_addr.l_symndx];
2211
2212 if (indx < 0)
2213 {
2214 /* These line numbers are attached to a symbol
2215 which we are stripping. We must discard the
2216 line numbers because reading them back with
2217 no associated symbol (or associating them all
2218 with symbol #0) will fail. We can't regain
2219 the space in the output file, but at least
2220 they're dense. */
2221 skipping = TRUE;
2222 }
2223 else
2224 {
2225 struct internal_syment is;
2226 union internal_auxent ia;
2227
2228 /* Fix up the lnnoptr field in the aux entry of
2229 the symbol. It turns out that we can't do
2230 this when we modify the symbol aux entries,
2231 because gas sometimes screws up the lnnoptr
2232 field and makes it an offset from the start
2233 of the line numbers rather than an absolute
2234 file index. */
2235 bfd_coff_swap_sym_in (output_bfd,
2236 (flaginfo->outsyms
2237 + ((indx - syment_base)
2238 * osymesz)), &is);
2239 if ((ISFCN (is.n_type)
2240 || is.n_sclass == C_BLOCK)
2241 && is.n_numaux >= 1)
2242 {
2243 void *auxptr;
2244
2245 auxptr = (flaginfo->outsyms
2246 + ((indx - syment_base + 1)
2247 * osymesz));
2248 bfd_coff_swap_aux_in (output_bfd, auxptr,
2249 is.n_type, is.n_sclass,
2250 0, is.n_numaux, &ia);
2251 ia.x_sym.x_fcnary.x_fcn.x_lnnoptr =
2252 (o->output_section->line_filepos
2253 + o->output_section->lineno_count * linesz
2254 + eline - flaginfo->linenos);
2255 bfd_coff_swap_aux_out (output_bfd, &ia,
2256 is.n_type, is.n_sclass, 0,
2257 is.n_numaux, auxptr);
2258 }
2259
2260 skipping = FALSE;
2261 }
2262
2263 iline.l_addr.l_symndx = indx;
2264 }
2265
2266 if (!skipping)
2267 {
2268 bfd_coff_swap_lineno_out (output_bfd, &iline, oeline);
2269 oeline += linesz;
2270 }
2271 }
2272
2273 pos = o->output_section->line_filepos;
2274 pos += o->output_section->lineno_count * linesz;
2275 amt = oeline - flaginfo->linenos;
2276 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
2277 || bfd_bwrite (flaginfo->linenos, amt, output_bfd) != amt)
2278 return FALSE;
2279
2280 o->output_section->lineno_count += amt / linesz;
2281 }
2282 }
2283
2284 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
2285 symbol will be the first symbol in the next input file. In the
2286 normal case, this will save us from writing out the C_FILE symbol
2287 again. */
2288 if (flaginfo->last_file_index != -1
2289 && (bfd_size_type) flaginfo->last_file_index >= syment_base)
2290 {
2291 flaginfo->last_file.n_value = output_index;
2292 bfd_coff_swap_sym_out (output_bfd, &flaginfo->last_file,
2293 (flaginfo->outsyms
2294 + ((flaginfo->last_file_index - syment_base)
2295 * osymesz)));
2296 }
2297
2298 /* Write the modified symbols to the output file. */
2299 if (outsym > flaginfo->outsyms)
2300 {
2301 file_ptr pos;
2302 bfd_size_type amt;
2303
2304 pos = obj_sym_filepos (output_bfd) + syment_base * osymesz;
2305 amt = outsym - flaginfo->outsyms;
2306 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
2307 || bfd_bwrite (flaginfo->outsyms, amt, output_bfd) != amt)
2308 return FALSE;
2309
2310 BFD_ASSERT ((obj_raw_syment_count (output_bfd)
2311 + (outsym - flaginfo->outsyms) / osymesz)
2312 == output_index);
2313
2314 obj_raw_syment_count (output_bfd) = output_index;
2315 }
2316
2317 /* Relocate the contents of each section. */
2318 adjust_symndx = coff_backend_info (input_bfd)->_bfd_coff_adjust_symndx;
2319 for (o = input_bfd->sections; o != NULL; o = o->next)
2320 {
2321 bfd_byte *contents;
2322 struct coff_section_tdata *secdata;
2323
2324 if (! o->linker_mark)
2325 /* This section was omitted from the link. */
2326 continue;
2327
2328 if ((o->flags & SEC_LINKER_CREATED) != 0)
2329 continue;
2330
2331 if ((o->flags & SEC_HAS_CONTENTS) == 0
2332 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
2333 {
2334 if ((o->flags & SEC_RELOC) != 0
2335 && o->reloc_count != 0)
2336 {
2337 _bfd_error_handler
2338 /* xgettext: c-format */
2339 (_("%pB: relocs in section `%pA', but it has no contents"),
2340 input_bfd, o);
2341 bfd_set_error (bfd_error_no_contents);
2342 return FALSE;
2343 }
2344
2345 continue;
2346 }
2347
2348 secdata = coff_section_data (input_bfd, o);
2349 if (secdata != NULL && secdata->contents != NULL)
2350 contents = secdata->contents;
2351 else
2352 {
2353 contents = flaginfo->contents;
2354 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
2355 return FALSE;
2356 }
2357
2358 if ((o->flags & SEC_RELOC) != 0)
2359 {
2360 int target_index;
2361 struct internal_reloc *internal_relocs;
2362 struct internal_reloc *irel;
2363
2364 /* Read in the relocs. */
2365 target_index = o->output_section->target_index;
2366 internal_relocs = (_bfd_coff_read_internal_relocs
2367 (input_bfd, o, FALSE, flaginfo->external_relocs,
2368 bfd_link_relocatable (flaginfo->info),
2369 (bfd_link_relocatable (flaginfo->info)
2370 ? (flaginfo->section_info[target_index].relocs
2371 + o->output_section->reloc_count)
2372 : flaginfo->internal_relocs)));
2373 if (internal_relocs == NULL
2374 && o->reloc_count > 0)
2375 return FALSE;
2376
2377 /* Run through the relocs looking for relocs against symbols
2378 coming from discarded sections and complain about them. */
2379 irel = internal_relocs;
2380 for (; irel < &internal_relocs[o->reloc_count]; irel++)
2381 {
2382 struct coff_link_hash_entry *h;
2383 asection *ps = NULL;
2384 long symndx = irel->r_symndx;
2385 if (symndx < 0)
2386 continue;
2387 h = obj_coff_sym_hashes (input_bfd)[symndx];
2388 if (h == NULL)
2389 continue;
2390 while (h->root.type == bfd_link_hash_indirect
2391 || h->root.type == bfd_link_hash_warning)
2392 h = (struct coff_link_hash_entry *) h->root.u.i.link;
2393 if (h->root.type == bfd_link_hash_defined
2394 || h->root.type == bfd_link_hash_defweak)
2395 ps = h->root.u.def.section;
2396 if (ps == NULL)
2397 continue;
2398 /* Complain if definition comes from an excluded section. */
2399 if (ps->flags & SEC_EXCLUDE)
2400 (*flaginfo->info->callbacks->einfo)
2401 /* xgettext: c-format */
2402 (_("%X`%s' referenced in section `%pA' of %pB: "
2403 "defined in discarded section `%pA' of %pB\n"),
2404 h->root.root.string, o, input_bfd, ps, ps->owner);
2405 }
2406
2407 /* Call processor specific code to relocate the section
2408 contents. */
2409 if (! bfd_coff_relocate_section (output_bfd, flaginfo->info,
2410 input_bfd, o,
2411 contents,
2412 internal_relocs,
2413 flaginfo->internal_syms,
2414 flaginfo->sec_ptrs))
2415 return FALSE;
2416
2417 if (bfd_link_relocatable (flaginfo->info))
2418 {
2419 bfd_vma offset;
2420 struct internal_reloc *irelend;
2421 struct coff_link_hash_entry **rel_hash;
2422
2423 offset = o->output_section->vma + o->output_offset - o->vma;
2424 irel = internal_relocs;
2425 irelend = irel + o->reloc_count;
2426 rel_hash = (flaginfo->section_info[target_index].rel_hashes
2427 + o->output_section->reloc_count);
2428 for (; irel < irelend; irel++, rel_hash++)
2429 {
2430 struct coff_link_hash_entry *h;
2431 bfd_boolean adjusted;
2432
2433 *rel_hash = NULL;
2434
2435 /* Adjust the reloc address and symbol index. */
2436 irel->r_vaddr += offset;
2437
2438 if (irel->r_symndx == -1)
2439 continue;
2440
2441 if (adjust_symndx)
2442 {
2443 if (! (*adjust_symndx) (output_bfd, flaginfo->info,
2444 input_bfd, o, irel,
2445 &adjusted))
2446 return FALSE;
2447 if (adjusted)
2448 continue;
2449 }
2450
2451 h = obj_coff_sym_hashes (input_bfd)[irel->r_symndx];
2452 if (h != NULL)
2453 {
2454 /* This is a global symbol. */
2455 if (h->indx >= 0)
2456 irel->r_symndx = h->indx;
2457 else
2458 {
2459 /* This symbol is being written at the end
2460 of the file, and we do not yet know the
2461 symbol index. We save the pointer to the
2462 hash table entry in the rel_hash list.
2463 We set the indx field to -2 to indicate
2464 that this symbol must not be stripped. */
2465 *rel_hash = h;
2466 h->indx = -2;
2467 }
2468 }
2469 else
2470 {
2471 long indx;
2472
2473 indx = flaginfo->sym_indices[irel->r_symndx];
2474 if (indx != -1)
2475 irel->r_symndx = indx;
2476 else
2477 {
2478 struct internal_syment *is;
2479 const char *name;
2480 char buf[SYMNMLEN + 1];
2481
2482 /* This reloc is against a symbol we are
2483 stripping. This should have been handled
2484 by the 'dont_skip_symbol' code in the while
2485 loop at the top of this function. */
2486 is = flaginfo->internal_syms + irel->r_symndx;
2487
2488 name = (_bfd_coff_internal_syment_name
2489 (input_bfd, is, buf));
2490 if (name == NULL)
2491 return FALSE;
2492
2493 (*flaginfo->info->callbacks->unattached_reloc)
2494 (flaginfo->info, name, input_bfd, o, irel->r_vaddr);
2495 }
2496 }
2497 }
2498
2499 o->output_section->reloc_count += o->reloc_count;
2500 }
2501 }
2502
2503 /* Write out the modified section contents. */
2504 if (secdata == NULL || secdata->stab_info == NULL)
2505 {
2506 file_ptr loc = (o->output_offset
2507 * bfd_octets_per_byte (output_bfd, o));
2508 if (! bfd_set_section_contents (output_bfd, o->output_section,
2509 contents, loc, o->size))
2510 return FALSE;
2511 }
2512 else
2513 {
2514 if (! (_bfd_write_section_stabs
2515 (output_bfd, &coff_hash_table (flaginfo->info)->stab_info,
2516 o, &secdata->stab_info, contents)))
2517 return FALSE;
2518 }
2519 }
2520
2521 if (! flaginfo->info->keep_memory
2522 && ! _bfd_coff_free_symbols (input_bfd))
2523 return FALSE;
2524
2525 return TRUE;
2526 }
2527
2528 /* Write out a global symbol. Called via bfd_hash_traverse. */
2529
2530 bfd_boolean
2531 _bfd_coff_write_global_sym (struct bfd_hash_entry *bh, void *data)
2532 {
2533 struct coff_link_hash_entry *h = (struct coff_link_hash_entry *) bh;
2534 struct coff_final_link_info *flaginfo = (struct coff_final_link_info *) data;
2535 bfd *output_bfd;
2536 struct internal_syment isym;
2537 bfd_size_type symesz;
2538 unsigned int i;
2539 file_ptr pos;
2540
2541 output_bfd = flaginfo->output_bfd;
2542
2543 if (h->root.type == bfd_link_hash_warning)
2544 {
2545 h = (struct coff_link_hash_entry *) h->root.u.i.link;
2546 if (h->root.type == bfd_link_hash_new)
2547 return TRUE;
2548 }
2549
2550 if (h->indx >= 0)
2551 return TRUE;
2552
2553 if (h->indx != -2
2554 && (flaginfo->info->strip == strip_all
2555 || (flaginfo->info->strip == strip_some
2556 && (bfd_hash_lookup (flaginfo->info->keep_hash,
2557 h->root.root.string, FALSE, FALSE)
2558 == NULL))))
2559 return TRUE;
2560
2561 switch (h->root.type)
2562 {
2563 default:
2564 case bfd_link_hash_new:
2565 case bfd_link_hash_warning:
2566 abort ();
2567 return FALSE;
2568
2569 case bfd_link_hash_undefined:
2570 case bfd_link_hash_undefweak:
2571 isym.n_scnum = N_UNDEF;
2572 isym.n_value = 0;
2573 break;
2574
2575 case bfd_link_hash_defined:
2576 case bfd_link_hash_defweak:
2577 {
2578 asection *sec;
2579
2580 sec = h->root.u.def.section->output_section;
2581 if (bfd_is_abs_section (sec))
2582 isym.n_scnum = N_ABS;
2583 else
2584 isym.n_scnum = sec->target_index;
2585 isym.n_value = (h->root.u.def.value
2586 + h->root.u.def.section->output_offset);
2587 if (! obj_pe (flaginfo->output_bfd))
2588 isym.n_value += sec->vma;
2589 }
2590 break;
2591
2592 case bfd_link_hash_common:
2593 isym.n_scnum = N_UNDEF;
2594 isym.n_value = h->root.u.c.size;
2595 break;
2596
2597 case bfd_link_hash_indirect:
2598 /* Just ignore these. They can't be handled anyhow. */
2599 return TRUE;
2600 }
2601
2602 if (strlen (h->root.root.string) <= SYMNMLEN)
2603 strncpy (isym._n._n_name, h->root.root.string, SYMNMLEN);
2604 else
2605 {
2606 bfd_boolean hash;
2607 bfd_size_type indx;
2608
2609 hash = TRUE;
2610 if (flaginfo->info->traditional_format)
2611 hash = FALSE;
2612 indx = _bfd_stringtab_add (flaginfo->strtab, h->root.root.string, hash,
2613 FALSE);
2614 if (indx == (bfd_size_type) -1)
2615 {
2616 flaginfo->failed = TRUE;
2617 return FALSE;
2618 }
2619 isym._n._n_n._n_zeroes = 0;
2620 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
2621 }
2622
2623 isym.n_sclass = h->symbol_class;
2624 isym.n_type = h->type;
2625
2626 if (isym.n_sclass == C_NULL)
2627 isym.n_sclass = C_EXT;
2628
2629 /* If doing task linking and this is the pass where we convert
2630 defined globals to statics, then do that conversion now. If the
2631 symbol is not being converted, just ignore it and it will be
2632 output during a later pass. */
2633 if (flaginfo->global_to_static)
2634 {
2635 if (! IS_EXTERNAL (output_bfd, isym))
2636 return TRUE;
2637
2638 isym.n_sclass = C_STAT;
2639 }
2640
2641 /* When a weak symbol is not overridden by a strong one,
2642 turn it into an external symbol when not building a
2643 shared or relocatable object. */
2644 if (! bfd_link_pic (flaginfo->info)
2645 && ! bfd_link_relocatable (flaginfo->info)
2646 && IS_WEAK_EXTERNAL (flaginfo->output_bfd, isym))
2647 isym.n_sclass = C_EXT;
2648
2649 isym.n_numaux = h->numaux;
2650
2651 bfd_coff_swap_sym_out (output_bfd, &isym, flaginfo->outsyms);
2652
2653 symesz = bfd_coff_symesz (output_bfd);
2654
2655 pos = obj_sym_filepos (output_bfd);
2656 pos += obj_raw_syment_count (output_bfd) * symesz;
2657 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
2658 || bfd_bwrite (flaginfo->outsyms, symesz, output_bfd) != symesz)
2659 {
2660 flaginfo->failed = TRUE;
2661 return FALSE;
2662 }
2663
2664 h->indx = obj_raw_syment_count (output_bfd);
2665
2666 ++obj_raw_syment_count (output_bfd);
2667
2668 /* Write out any associated aux entries. Most of the aux entries
2669 will have been modified in _bfd_coff_link_input_bfd. We have to
2670 handle section aux entries here, now that we have the final
2671 relocation and line number counts. */
2672 for (i = 0; i < isym.n_numaux; i++)
2673 {
2674 union internal_auxent *auxp;
2675
2676 auxp = h->aux + i;
2677
2678 /* Look for a section aux entry here using the same tests that
2679 coff_swap_aux_out uses. */
2680 if (i == 0
2681 && (isym.n_sclass == C_STAT
2682 || isym.n_sclass == C_HIDDEN)
2683 && isym.n_type == T_NULL
2684 && (h->root.type == bfd_link_hash_defined
2685 || h->root.type == bfd_link_hash_defweak))
2686 {
2687 asection *sec;
2688
2689 sec = h->root.u.def.section->output_section;
2690 if (sec != NULL)
2691 {
2692 auxp->x_scn.x_scnlen = sec->size;
2693
2694 /* For PE, an overflow on the final link reportedly does
2695 not matter. FIXME: Why not? */
2696 if (sec->reloc_count > 0xffff
2697 && (! obj_pe (output_bfd)
2698 || bfd_link_relocatable (flaginfo->info)))
2699 _bfd_error_handler
2700 /* xgettext: c-format */
2701 (_("%pB: %pA: reloc overflow: %#x > 0xffff"),
2702 output_bfd, sec, sec->reloc_count);
2703
2704 if (sec->lineno_count > 0xffff
2705 && (! obj_pe (output_bfd)
2706 || bfd_link_relocatable (flaginfo->info)))
2707 _bfd_error_handler
2708 /* xgettext: c-format */
2709 (_("%pB: warning: %pA: line number overflow: %#x > 0xffff"),
2710 output_bfd, sec, sec->lineno_count);
2711
2712 auxp->x_scn.x_nreloc = sec->reloc_count;
2713 auxp->x_scn.x_nlinno = sec->lineno_count;
2714 auxp->x_scn.x_checksum = 0;
2715 auxp->x_scn.x_associated = 0;
2716 auxp->x_scn.x_comdat = 0;
2717 }
2718 }
2719
2720 bfd_coff_swap_aux_out (output_bfd, auxp, isym.n_type,
2721 isym.n_sclass, (int) i, isym.n_numaux,
2722 flaginfo->outsyms);
2723 if (bfd_bwrite (flaginfo->outsyms, symesz, output_bfd) != symesz)
2724 {
2725 flaginfo->failed = TRUE;
2726 return FALSE;
2727 }
2728 ++obj_raw_syment_count (output_bfd);
2729 }
2730
2731 return TRUE;
2732 }
2733
2734 /* Write out task global symbols, converting them to statics. Called
2735 via coff_link_hash_traverse. Calls bfd_coff_write_global_sym to do
2736 the dirty work, if the symbol we are processing needs conversion. */
2737
2738 bfd_boolean
2739 _bfd_coff_write_task_globals (struct coff_link_hash_entry *h, void *data)
2740 {
2741 struct coff_final_link_info *flaginfo = (struct coff_final_link_info *) data;
2742 bfd_boolean rtnval = TRUE;
2743 bfd_boolean save_global_to_static;
2744
2745 if (h->root.type == bfd_link_hash_warning)
2746 h = (struct coff_link_hash_entry *) h->root.u.i.link;
2747
2748 if (h->indx < 0)
2749 {
2750 switch (h->root.type)
2751 {
2752 case bfd_link_hash_defined:
2753 case bfd_link_hash_defweak:
2754 save_global_to_static = flaginfo->global_to_static;
2755 flaginfo->global_to_static = TRUE;
2756 rtnval = _bfd_coff_write_global_sym (&h->root.root, data);
2757 flaginfo->global_to_static = save_global_to_static;
2758 break;
2759 default:
2760 break;
2761 }
2762 }
2763 return (rtnval);
2764 }
2765
2766 /* Handle a link order which is supposed to generate a reloc. */
2767
2768 bfd_boolean
2769 _bfd_coff_reloc_link_order (bfd *output_bfd,
2770 struct coff_final_link_info *flaginfo,
2771 asection *output_section,
2772 struct bfd_link_order *link_order)
2773 {
2774 reloc_howto_type *howto;
2775 struct internal_reloc *irel;
2776 struct coff_link_hash_entry **rel_hash_ptr;
2777
2778 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
2779 if (howto == NULL)
2780 {
2781 bfd_set_error (bfd_error_bad_value);
2782 return FALSE;
2783 }
2784
2785 if (link_order->u.reloc.p->addend != 0)
2786 {
2787 bfd_size_type size;
2788 bfd_byte *buf;
2789 bfd_reloc_status_type rstat;
2790 bfd_boolean ok;
2791 file_ptr loc;
2792
2793 size = bfd_get_reloc_size (howto);
2794 buf = (bfd_byte *) bfd_zmalloc (size);
2795 if (buf == NULL && size != 0)
2796 return FALSE;
2797
2798 rstat = _bfd_relocate_contents (howto, output_bfd,
2799 (bfd_vma) link_order->u.reloc.p->addend,
2800 buf);
2801 switch (rstat)
2802 {
2803 case bfd_reloc_ok:
2804 break;
2805 default:
2806 case bfd_reloc_outofrange:
2807 abort ();
2808 case bfd_reloc_overflow:
2809 (*flaginfo->info->callbacks->reloc_overflow)
2810 (flaginfo->info, NULL,
2811 (link_order->type == bfd_section_reloc_link_order
2812 ? bfd_section_name (link_order->u.reloc.p->u.section)
2813 : link_order->u.reloc.p->u.name),
2814 howto->name, link_order->u.reloc.p->addend,
2815 (bfd *) NULL, (asection *) NULL, (bfd_vma) 0);
2816 break;
2817 }
2818 loc = link_order->offset * bfd_octets_per_byte (output_bfd,
2819 output_section);
2820 ok = bfd_set_section_contents (output_bfd, output_section, buf,
2821 loc, size);
2822 free (buf);
2823 if (! ok)
2824 return FALSE;
2825 }
2826
2827 /* Store the reloc information in the right place. It will get
2828 swapped and written out at the end of the final_link routine. */
2829 irel = (flaginfo->section_info[output_section->target_index].relocs
2830 + output_section->reloc_count);
2831 rel_hash_ptr = (flaginfo->section_info[output_section->target_index].rel_hashes
2832 + output_section->reloc_count);
2833
2834 memset (irel, 0, sizeof (struct internal_reloc));
2835 *rel_hash_ptr = NULL;
2836
2837 irel->r_vaddr = output_section->vma + link_order->offset;
2838
2839 if (link_order->type == bfd_section_reloc_link_order)
2840 {
2841 /* We need to somehow locate a symbol in the right section. The
2842 symbol must either have a value of zero, or we must adjust
2843 the addend by the value of the symbol. FIXME: Write this
2844 when we need it. The old linker couldn't handle this anyhow. */
2845 abort ();
2846 *rel_hash_ptr = NULL;
2847 irel->r_symndx = 0;
2848 }
2849 else
2850 {
2851 struct coff_link_hash_entry *h;
2852
2853 h = ((struct coff_link_hash_entry *)
2854 bfd_wrapped_link_hash_lookup (output_bfd, flaginfo->info,
2855 link_order->u.reloc.p->u.name,
2856 FALSE, FALSE, TRUE));
2857 if (h != NULL)
2858 {
2859 if (h->indx >= 0)
2860 irel->r_symndx = h->indx;
2861 else
2862 {
2863 /* Set the index to -2 to force this symbol to get
2864 written out. */
2865 h->indx = -2;
2866 *rel_hash_ptr = h;
2867 irel->r_symndx = 0;
2868 }
2869 }
2870 else
2871 {
2872 (*flaginfo->info->callbacks->unattached_reloc)
2873 (flaginfo->info, link_order->u.reloc.p->u.name,
2874 (bfd *) NULL, (asection *) NULL, (bfd_vma) 0);
2875 irel->r_symndx = 0;
2876 }
2877 }
2878
2879 /* FIXME: Is this always right? */
2880 irel->r_type = howto->type;
2881
2882 /* r_size is only used on the RS/6000, which needs its own linker
2883 routines anyhow. r_extern is only used for ECOFF. */
2884
2885 /* FIXME: What is the right value for r_offset? Is zero OK? */
2886 ++output_section->reloc_count;
2887
2888 return TRUE;
2889 }
2890
2891 /* A basic reloc handling routine which may be used by processors with
2892 simple relocs. */
2893
2894 bfd_boolean
2895 _bfd_coff_generic_relocate_section (bfd *output_bfd,
2896 struct bfd_link_info *info,
2897 bfd *input_bfd,
2898 asection *input_section,
2899 bfd_byte *contents,
2900 struct internal_reloc *relocs,
2901 struct internal_syment *syms,
2902 asection **sections)
2903 {
2904 struct internal_reloc *rel;
2905 struct internal_reloc *relend;
2906
2907 rel = relocs;
2908 relend = rel + input_section->reloc_count;
2909 for (; rel < relend; rel++)
2910 {
2911 long symndx;
2912 struct coff_link_hash_entry *h;
2913 struct internal_syment *sym;
2914 bfd_vma addend;
2915 bfd_vma val;
2916 asection *sec;
2917 reloc_howto_type *howto;
2918 bfd_reloc_status_type rstat;
2919
2920 symndx = rel->r_symndx;
2921
2922 if (symndx == -1)
2923 {
2924 h = NULL;
2925 sym = NULL;
2926 }
2927 else if (symndx < 0
2928 || (unsigned long) symndx >= obj_raw_syment_count (input_bfd))
2929 {
2930 _bfd_error_handler
2931 /* xgettext: c-format */
2932 (_("%pB: illegal symbol index %ld in relocs"), input_bfd, symndx);
2933 return FALSE;
2934 }
2935 else
2936 {
2937 h = obj_coff_sym_hashes (input_bfd)[symndx];
2938 sym = syms + symndx;
2939 }
2940
2941 /* COFF treats common symbols in one of two ways. Either the
2942 size of the symbol is included in the section contents, or it
2943 is not. We assume that the size is not included, and force
2944 the rtype_to_howto function to adjust the addend as needed. */
2945 if (sym != NULL && sym->n_scnum != 0)
2946 addend = - sym->n_value;
2947 else
2948 addend = 0;
2949
2950 howto = bfd_coff_rtype_to_howto (input_bfd, input_section, rel, h,
2951 sym, &addend);
2952 if (howto == NULL)
2953 return FALSE;
2954
2955 /* If we are doing a relocatable link, then we can just ignore
2956 a PC relative reloc that is pcrel_offset. It will already
2957 have the correct value. If this is not a relocatable link,
2958 then we should ignore the symbol value. */
2959 if (howto->pc_relative && howto->pcrel_offset)
2960 {
2961 if (bfd_link_relocatable (info))
2962 continue;
2963 if (sym != NULL && sym->n_scnum != 0)
2964 addend += sym->n_value;
2965 }
2966
2967 val = 0;
2968 sec = NULL;
2969 if (h == NULL)
2970 {
2971 if (symndx == -1)
2972 {
2973 sec = bfd_abs_section_ptr;
2974 val = 0;
2975 }
2976 else
2977 {
2978 sec = sections[symndx];
2979
2980 /* PR 19623: Relocations against symbols in
2981 the absolute sections should ignored. */
2982 if (bfd_is_abs_section (sec))
2983 continue;
2984
2985 val = (sec->output_section->vma
2986 + sec->output_offset
2987 + sym->n_value);
2988 if (! obj_pe (input_bfd))
2989 val -= sec->vma;
2990 }
2991 }
2992 else
2993 {
2994 if (h->root.type == bfd_link_hash_defined
2995 || h->root.type == bfd_link_hash_defweak)
2996 {
2997 /* Defined weak symbols are a GNU extension. */
2998 sec = h->root.u.def.section;
2999 val = (h->root.u.def.value
3000 + sec->output_section->vma
3001 + sec->output_offset);
3002 }
3003
3004 else if (h->root.type == bfd_link_hash_undefweak)
3005 {
3006 if (h->symbol_class == C_NT_WEAK && h->numaux == 1)
3007 {
3008 /* See _Microsoft Portable Executable and Common Object
3009 File Format Specification_, section 5.5.3.
3010 Note that weak symbols without aux records are a GNU
3011 extension.
3012 FIXME: All weak externals are treated as having
3013 characteristic IMAGE_WEAK_EXTERN_SEARCH_NOLIBRARY (1).
3014 These behave as per SVR4 ABI: A library member
3015 will resolve a weak external only if a normal
3016 external causes the library member to be linked.
3017 See also linker.c: generic_link_check_archive_element. */
3018 struct coff_link_hash_entry *h2 =
3019 h->auxbfd->tdata.coff_obj_data->sym_hashes[
3020 h->aux->x_sym.x_tagndx.l];
3021
3022 if (!h2 || h2->root.type == bfd_link_hash_undefined)
3023 {
3024 sec = bfd_abs_section_ptr;
3025 val = 0;
3026 }
3027 else
3028 {
3029 sec = h2->root.u.def.section;
3030 val = h2->root.u.def.value
3031 + sec->output_section->vma + sec->output_offset;
3032 }
3033 }
3034 else
3035 /* This is a GNU extension. */
3036 val = 0;
3037 }
3038
3039 else if (! bfd_link_relocatable (info))
3040 (*info->callbacks->undefined_symbol)
3041 (info, h->root.root.string, input_bfd, input_section,
3042 rel->r_vaddr - input_section->vma, TRUE);
3043 }
3044
3045 /* If the input section defining the symbol has been discarded
3046 then zero this reloc field. */
3047 if (sec != NULL && discarded_section (sec))
3048 {
3049 _bfd_clear_contents (howto, input_bfd, input_section,
3050 contents, rel->r_vaddr - input_section->vma);
3051 continue;
3052 }
3053
3054 if (info->base_file)
3055 {
3056 /* Emit a reloc if the backend thinks it needs it. */
3057 if (sym && pe_data (output_bfd)->in_reloc_p (output_bfd, howto))
3058 {
3059 /* Relocation to a symbol in a section which isn't
3060 absolute. We output the address here to a file.
3061 This file is then read by dlltool when generating the
3062 reloc section. Note that the base file is not
3063 portable between systems. We write out a bfd_vma here,
3064 and dlltool reads in a bfd_vma. */
3065 bfd_vma addr = (rel->r_vaddr
3066 - input_section->vma
3067 + input_section->output_offset
3068 + input_section->output_section->vma);
3069 if (coff_data (output_bfd)->pe)
3070 addr -= pe_data(output_bfd)->pe_opthdr.ImageBase;
3071 if (fwrite (&addr, 1, sizeof (bfd_vma), (FILE *) info->base_file)
3072 != sizeof (bfd_vma))
3073 {
3074 bfd_set_error (bfd_error_system_call);
3075 return FALSE;
3076 }
3077 }
3078 }
3079
3080 rstat = _bfd_final_link_relocate (howto, input_bfd, input_section,
3081 contents,
3082 rel->r_vaddr - input_section->vma,
3083 val, addend);
3084
3085 switch (rstat)
3086 {
3087 default:
3088 abort ();
3089 case bfd_reloc_ok:
3090 break;
3091 case bfd_reloc_outofrange:
3092 _bfd_error_handler
3093 /* xgettext: c-format */
3094 (_("%pB: bad reloc address %#" PRIx64 " in section `%pA'"),
3095 input_bfd, (uint64_t) rel->r_vaddr, input_section);
3096 return FALSE;
3097 case bfd_reloc_overflow:
3098 {
3099 const char *name;
3100 char buf[SYMNMLEN + 1];
3101
3102 if (symndx == -1)
3103 name = "*ABS*";
3104 else if (h != NULL)
3105 name = NULL;
3106 else
3107 {
3108 name = _bfd_coff_internal_syment_name (input_bfd, sym, buf);
3109 if (name == NULL)
3110 return FALSE;
3111 }
3112
3113 (*info->callbacks->reloc_overflow)
3114 (info, (h ? &h->root : NULL), name, howto->name,
3115 (bfd_vma) 0, input_bfd, input_section,
3116 rel->r_vaddr - input_section->vma);
3117 }
3118 }
3119 }
3120 return TRUE;
3121 }
This page took 0.098128 seconds and 4 git commands to generate.