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