OSDN Git Service

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