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