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