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