OSDN Git Service

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