source: branches/libc-0.6/src/binutils/bfd/cofflink.c

Last change on this file was 610, checked in by bird, 22 years ago

This commit was generated by cvs2svn to compensate for changes in r609,
which included commits to RCS files with non-trunk default branches.

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