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