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