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