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