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