* aout-encap.c: Fix comment typos.
[external/binutils.git] / bfd / coffgen.c
1 /* Support for the generic parts of COFF, for BFD.
2    Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3    2000, 2001
4    Free Software Foundation, Inc.
5    Written by Cygnus Support.
6
7 This file is part of BFD, the Binary File Descriptor library.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
22
23 /* Most of this hacked by  Steve Chamberlain, sac@cygnus.com.
24    Split out of coffcode.h by Ian Taylor, ian@cygnus.com.  */
25
26 /* This file contains COFF code that is not dependent on any
27    particular COFF target.  There is only one version of this file in
28    libbfd.a, so no target specific code may be put in here.  Or, to
29    put it another way,
30
31    ********** DO NOT PUT TARGET SPECIFIC CODE IN THIS FILE **********
32
33    If you need to add some target specific behaviour, add a new hook
34    function to bfd_coff_backend_data.
35
36    Some of these functions are also called by the ECOFF routines.
37    Those functions may not use any COFF specific information, such as
38    coff_data (abfd).  */
39
40 #include "bfd.h"
41 #include "sysdep.h"
42 #include "libbfd.h"
43 #include "coff/internal.h"
44 #include "libcoff.h"
45
46 static void coff_fix_symbol_name
47   PARAMS ((bfd *, asymbol *, combined_entry_type *, bfd_size_type *,
48            asection **, bfd_size_type *));
49 static boolean coff_write_symbol
50   PARAMS ((bfd *, asymbol *, combined_entry_type *, bfd_vma *,
51            bfd_size_type *, asection **, bfd_size_type *));
52 static boolean coff_write_alien_symbol
53   PARAMS ((bfd *, asymbol *, bfd_vma *, bfd_size_type *,
54            asection **, bfd_size_type *));
55 static boolean coff_write_native_symbol
56   PARAMS ((bfd *, coff_symbol_type *, bfd_vma *, bfd_size_type *,
57            asection **, bfd_size_type *));
58 static void coff_pointerize_aux
59   PARAMS ((bfd *, combined_entry_type *, combined_entry_type *,
60            unsigned int, combined_entry_type *));
61 static boolean make_a_section_from_file
62   PARAMS ((bfd *, struct internal_scnhdr *, unsigned int));
63 static const bfd_target *coff_real_object_p
64   PARAMS ((bfd *, unsigned, struct internal_filehdr *,
65            struct internal_aouthdr *));
66 static void fixup_symbol_value
67   PARAMS ((bfd *, coff_symbol_type *, struct internal_syment *));
68 static char *build_debug_section
69   PARAMS ((bfd *));
70 static char *copy_name
71   PARAMS ((bfd *, char *, size_t));
72
73 #define STRING_SIZE_SIZE (4)
74
75 /* Take a section header read from a coff file (in HOST byte order),
76    and make a BFD "section" out of it.  This is used by ECOFF.  */
77 static boolean
78 make_a_section_from_file (abfd, hdr, target_index)
79      bfd *abfd;
80      struct internal_scnhdr *hdr;
81      unsigned int target_index;
82 {
83   asection *return_section;
84   char *name;
85   boolean result = true;
86   flagword flags;
87
88   name = NULL;
89
90   /* Handle long section names as in PE.  */
91   if (bfd_coff_long_section_names (abfd)
92       && hdr->s_name[0] == '/')
93     {
94       char buf[SCNNMLEN];
95       long strindex;
96       char *p;
97       const char *strings;
98
99       memcpy (buf, hdr->s_name + 1, SCNNMLEN - 1);
100       buf[SCNNMLEN - 1] = '\0';
101       strindex = strtol (buf, &p, 10);
102       if (*p == '\0' && strindex >= 0)
103         {
104           strings = _bfd_coff_read_string_table (abfd);
105           if (strings == NULL)
106             return false;
107           /* FIXME: For extra safety, we should make sure that
108              strindex does not run us past the end, but right now we
109              don't know the length of the string table.  */
110           strings += strindex;
111           name = bfd_alloc (abfd, (bfd_size_type) strlen (strings) + 1);
112           if (name == NULL)
113             return false;
114           strcpy (name, strings);
115         }
116     }
117
118   if (name == NULL)
119     {
120       /* Assorted wastage to null-terminate the name, thanks AT&T! */
121       name = bfd_alloc (abfd, (bfd_size_type) sizeof (hdr->s_name) + 1);
122       if (name == NULL)
123         return false;
124       strncpy (name, (char *) &hdr->s_name[0], sizeof (hdr->s_name));
125       name[sizeof (hdr->s_name)] = 0;
126     }
127
128   return_section = bfd_make_section_anyway (abfd, name);
129   if (return_section == NULL)
130     return false;
131
132   return_section->vma = hdr->s_vaddr;
133   return_section->lma = hdr->s_paddr;
134   return_section->_raw_size = hdr->s_size;
135   return_section->filepos = hdr->s_scnptr;
136   return_section->rel_filepos = hdr->s_relptr;
137   return_section->reloc_count = hdr->s_nreloc;
138
139   bfd_coff_set_alignment_hook (abfd, return_section, hdr);
140
141   return_section->line_filepos = hdr->s_lnnoptr;
142
143   return_section->lineno_count = hdr->s_nlnno;
144   return_section->userdata = NULL;
145   return_section->next = (asection *) NULL;
146   return_section->target_index = target_index;
147
148   if (! bfd_coff_styp_to_sec_flags_hook (abfd, hdr, name, return_section,
149                                          & flags))
150     result = false;
151
152   return_section->flags = flags;
153
154   /* At least on i386-coff, the line number count for a shared library
155      section must be ignored.  */
156   if ((return_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
157     return_section->lineno_count = 0;
158
159   if (hdr->s_nreloc != 0)
160     return_section->flags |= SEC_RELOC;
161   /* FIXME: should this check 'hdr->s_size > 0' */
162   if (hdr->s_scnptr != 0)
163     return_section->flags |= SEC_HAS_CONTENTS;
164
165   return result;
166 }
167
168 /* Read in a COFF object and make it into a BFD.  This is used by
169    ECOFF as well.  */
170
171 static const bfd_target *
172 coff_real_object_p (abfd, nscns, internal_f, internal_a)
173      bfd *abfd;
174      unsigned nscns;
175      struct internal_filehdr *internal_f;
176      struct internal_aouthdr *internal_a;
177 {
178   flagword oflags = abfd->flags;
179   bfd_vma ostart = bfd_get_start_address (abfd);
180   PTR tdata;
181   bfd_size_type readsize;       /* length of file_info */
182   unsigned int scnhsz;
183   char *external_sections;
184
185   if (!(internal_f->f_flags & F_RELFLG))
186     abfd->flags |= HAS_RELOC;
187   if ((internal_f->f_flags & F_EXEC))
188     abfd->flags |= EXEC_P;
189   if (!(internal_f->f_flags & F_LNNO))
190     abfd->flags |= HAS_LINENO;
191   if (!(internal_f->f_flags & F_LSYMS))
192     abfd->flags |= HAS_LOCALS;
193
194   /* FIXME: How can we set D_PAGED correctly?  */
195   if ((internal_f->f_flags & F_EXEC) != 0)
196     abfd->flags |= D_PAGED;
197
198   bfd_get_symcount (abfd) = internal_f->f_nsyms;
199   if (internal_f->f_nsyms)
200     abfd->flags |= HAS_SYMS;
201
202   if (internal_a != (struct internal_aouthdr *) NULL)
203     bfd_get_start_address (abfd) = internal_a->entry;
204   else
205     bfd_get_start_address (abfd) = 0;
206
207   /* Set up the tdata area.  ECOFF uses its own routine, and overrides
208      abfd->flags.  */
209   tdata = bfd_coff_mkobject_hook (abfd, (PTR) internal_f, (PTR) internal_a);
210   if (tdata == NULL)
211     return 0;
212
213   scnhsz = bfd_coff_scnhsz (abfd);
214   readsize = (bfd_size_type) nscns * scnhsz;
215   external_sections = (char *) bfd_alloc (abfd, readsize);
216   if (!external_sections)
217     goto fail;
218
219   if (bfd_bread ((PTR) external_sections, readsize, abfd) != readsize)
220     goto fail;
221
222   /* Set the arch/mach *before* swapping in sections; section header swapping
223      may depend on arch/mach info.  */
224   if (bfd_coff_set_arch_mach_hook (abfd, (PTR) internal_f) == false)
225     goto fail;
226
227   /* Now copy data as required; construct all asections etc */
228   if (nscns != 0)
229     {
230       unsigned int i;
231       for (i = 0; i < nscns; i++)
232         {
233           struct internal_scnhdr tmp;
234           bfd_coff_swap_scnhdr_in (abfd,
235                                    (PTR) (external_sections + i * scnhsz),
236                                    (PTR) & tmp);
237           if (! make_a_section_from_file (abfd, &tmp, i + 1))
238             goto fail;
239         }
240     }
241
242   /*  make_abs_section (abfd); */
243
244   return abfd->xvec;
245
246  fail:
247   bfd_release (abfd, tdata);
248   abfd->flags = oflags;
249   bfd_get_start_address (abfd) = ostart;
250   return (const bfd_target *) NULL;
251 }
252
253 /* Turn a COFF file into a BFD, but fail with bfd_error_wrong_format if it is
254    not a COFF file.  This is also used by ECOFF.  */
255
256 const bfd_target *
257 coff_object_p (abfd)
258      bfd *abfd;
259 {
260   bfd_size_type filhsz;
261   bfd_size_type aoutsz;
262   unsigned int nscns;
263   PTR filehdr;
264   struct internal_filehdr internal_f;
265   struct internal_aouthdr internal_a;
266
267   /* figure out how much to read */
268   filhsz = bfd_coff_filhsz (abfd);
269   aoutsz = bfd_coff_aoutsz (abfd);
270
271   filehdr = bfd_alloc (abfd, filhsz);
272   if (filehdr == NULL)
273     return 0;
274   if (bfd_bread (filehdr, filhsz, abfd) != filhsz)
275     {
276       if (bfd_get_error () != bfd_error_system_call)
277         bfd_set_error (bfd_error_wrong_format);
278       return 0;
279     }
280   bfd_coff_swap_filehdr_in (abfd, filehdr, &internal_f);
281   bfd_release (abfd, filehdr);
282
283   if (bfd_coff_bad_format_hook (abfd, &internal_f) == false)
284     {
285       bfd_set_error (bfd_error_wrong_format);
286       return 0;
287     }
288   nscns = internal_f.f_nscns;
289
290   if (internal_f.f_opthdr)
291     {
292       PTR opthdr;
293
294       opthdr = bfd_alloc (abfd, aoutsz);
295       if (opthdr == NULL)
296         return 0;;
297       if (bfd_bread (opthdr, (bfd_size_type) internal_f.f_opthdr, abfd)
298           != internal_f.f_opthdr)
299         {
300           return 0;
301         }
302       bfd_coff_swap_aouthdr_in (abfd, opthdr, (PTR) &internal_a);
303     }
304
305   return coff_real_object_p (abfd, nscns, &internal_f,
306                              (internal_f.f_opthdr != 0
307                               ? &internal_a
308                               : (struct internal_aouthdr *) NULL));
309 }
310
311 /* Get the BFD section from a COFF symbol section number.  */
312
313 asection *
314 coff_section_from_bfd_index (abfd, index)
315      bfd *abfd;
316      int index;
317 {
318   struct sec *answer = abfd->sections;
319
320   if (index == N_ABS)
321     return bfd_abs_section_ptr;
322   if (index == N_UNDEF)
323     return bfd_und_section_ptr;
324   if (index == N_DEBUG)
325     return bfd_abs_section_ptr;
326
327   while (answer)
328     {
329       if (answer->target_index == index)
330         return answer;
331       answer = answer->next;
332     }
333
334   /* We should not reach this point, but the SCO 3.2v4 /lib/libc_s.a
335      has a bad symbol table in biglitpow.o.  */
336   return bfd_und_section_ptr;
337 }
338
339 /* Get the upper bound of a COFF symbol table.  */
340
341 long
342 coff_get_symtab_upper_bound (abfd)
343      bfd *abfd;
344 {
345   if (!bfd_coff_slurp_symbol_table (abfd))
346     return -1;
347
348   return (bfd_get_symcount (abfd) + 1) * (sizeof (coff_symbol_type *));
349 }
350
351 /* Canonicalize a COFF symbol table.  */
352
353 long
354 coff_get_symtab (abfd, alocation)
355      bfd *abfd;
356      asymbol **alocation;
357 {
358   unsigned int counter;
359   coff_symbol_type *symbase;
360   coff_symbol_type **location = (coff_symbol_type **) alocation;
361
362   if (!bfd_coff_slurp_symbol_table (abfd))
363     return -1;
364
365   symbase = obj_symbols (abfd);
366   counter = bfd_get_symcount (abfd);
367   while (counter-- > 0)
368     *location++ = symbase++;
369
370   *location = NULL;
371
372   return bfd_get_symcount (abfd);
373 }
374
375 /* Get the name of a symbol.  The caller must pass in a buffer of size
376    >= SYMNMLEN + 1.  */
377
378 const char *
379 _bfd_coff_internal_syment_name (abfd, sym, buf)
380      bfd *abfd;
381      const struct internal_syment *sym;
382      char *buf;
383 {
384   /* FIXME: It's not clear this will work correctly if sizeof
385      (_n_zeroes) != 4.  */
386   if (sym->_n._n_n._n_zeroes != 0
387       || sym->_n._n_n._n_offset == 0)
388     {
389       memcpy (buf, sym->_n._n_name, SYMNMLEN);
390       buf[SYMNMLEN] = '\0';
391       return buf;
392     }
393   else
394     {
395       const char *strings;
396
397       BFD_ASSERT (sym->_n._n_n._n_offset >= STRING_SIZE_SIZE);
398       strings = obj_coff_strings (abfd);
399       if (strings == NULL)
400         {
401           strings = _bfd_coff_read_string_table (abfd);
402           if (strings == NULL)
403             return NULL;
404         }
405       return strings + sym->_n._n_n._n_offset;
406     }
407 }
408
409 /* Read in and swap the relocs.  This returns a buffer holding the
410    relocs for section SEC in file ABFD.  If CACHE is true and
411    INTERNAL_RELOCS is NULL, the relocs read in will be saved in case
412    the function is called again.  If EXTERNAL_RELOCS is not NULL, it
413    is a buffer large enough to hold the unswapped relocs.  If
414    INTERNAL_RELOCS is not NULL, it is a buffer large enough to hold
415    the swapped relocs.  If REQUIRE_INTERNAL is true, then the return
416    value must be INTERNAL_RELOCS.  The function returns NULL on error.  */
417
418 struct internal_reloc *
419 _bfd_coff_read_internal_relocs (abfd, sec, cache, external_relocs,
420                                 require_internal, internal_relocs)
421      bfd *abfd;
422      asection *sec;
423      boolean cache;
424      bfd_byte *external_relocs;
425      boolean require_internal;
426      struct internal_reloc *internal_relocs;
427 {
428   bfd_size_type relsz;
429   bfd_byte *free_external = NULL;
430   struct internal_reloc *free_internal = NULL;
431   bfd_byte *erel;
432   bfd_byte *erel_end;
433   struct internal_reloc *irel;
434   bfd_size_type amt;
435
436   if (coff_section_data (abfd, sec) != NULL
437       && coff_section_data (abfd, sec)->relocs != NULL)
438     {
439       if (! require_internal)
440         return coff_section_data (abfd, sec)->relocs;
441       memcpy (internal_relocs, coff_section_data (abfd, sec)->relocs,
442               sec->reloc_count * sizeof (struct internal_reloc));
443       return internal_relocs;
444     }
445
446   relsz = bfd_coff_relsz (abfd);
447
448   amt = sec->reloc_count * relsz;
449   if (external_relocs == NULL)
450     {
451       free_external = (bfd_byte *) bfd_malloc (amt);
452       if (free_external == NULL && sec->reloc_count > 0)
453         goto error_return;
454       external_relocs = free_external;
455     }
456
457   if (bfd_seek (abfd, sec->rel_filepos, SEEK_SET) != 0
458       || bfd_bread (external_relocs, amt, abfd) != amt)
459     goto error_return;
460
461   if (internal_relocs == NULL)
462     {
463       amt = sec->reloc_count;
464       amt *= sizeof (struct internal_reloc);
465       free_internal = (struct internal_reloc *) bfd_malloc (amt);
466       if (free_internal == NULL && sec->reloc_count > 0)
467         goto error_return;
468       internal_relocs = free_internal;
469     }
470
471   /* Swap in the relocs.  */
472   erel = external_relocs;
473   erel_end = erel + relsz * sec->reloc_count;
474   irel = internal_relocs;
475   for (; erel < erel_end; erel += relsz, irel++)
476     bfd_coff_swap_reloc_in (abfd, (PTR) erel, (PTR) irel);
477
478   if (free_external != NULL)
479     {
480       free (free_external);
481       free_external = NULL;
482     }
483
484   if (cache && free_internal != NULL)
485     {
486       if (coff_section_data (abfd, sec) == NULL)
487         {
488           amt = sizeof (struct coff_section_tdata);
489           sec->used_by_bfd = (PTR) bfd_zalloc (abfd, amt);
490           if (sec->used_by_bfd == NULL)
491             goto error_return;
492           coff_section_data (abfd, sec)->contents = NULL;
493         }
494       coff_section_data (abfd, sec)->relocs = free_internal;
495     }
496
497   return internal_relocs;
498
499  error_return:
500   if (free_external != NULL)
501     free (free_external);
502   if (free_internal != NULL)
503     free (free_internal);
504   return NULL;
505 }
506
507 /* Set lineno_count for the output sections of a COFF file.  */
508
509 int
510 coff_count_linenumbers (abfd)
511      bfd *abfd;
512 {
513   unsigned int limit = bfd_get_symcount (abfd);
514   unsigned int i;
515   int total = 0;
516   asymbol **p;
517   asection *s;
518
519   if (limit == 0)
520     {
521       /* This may be from the backend linker, in which case the
522          lineno_count in the sections is correct.  */
523       for (s = abfd->sections; s != NULL; s = s->next)
524         total += s->lineno_count;
525       return total;
526     }
527
528   for (s = abfd->sections; s != NULL; s = s->next)
529     BFD_ASSERT (s->lineno_count == 0);
530
531   for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
532     {
533       asymbol *q_maybe = *p;
534
535       if (bfd_family_coff (bfd_asymbol_bfd (q_maybe)))
536         {
537           coff_symbol_type *q = coffsymbol (q_maybe);
538
539           /* The AIX 4.1 compiler can sometimes generate line numbers
540              attached to debugging symbols.  We try to simply ignore
541              those here.  */
542           if (q->lineno != NULL
543               && q->symbol.section->owner != NULL)
544             {
545               /* This symbol has line numbers.  Increment the owning
546                  section's linenumber count.  */
547               alent *l = q->lineno;
548
549               ++q->symbol.section->output_section->lineno_count;
550               ++total;
551               ++l;
552               while (l->line_number != 0)
553                 {
554                   ++total;
555                   ++q->symbol.section->output_section->lineno_count;
556                   ++l;
557                 }
558             }
559         }
560     }
561
562   return total;
563 }
564
565 /* Takes a bfd and a symbol, returns a pointer to the coff specific
566    area of the symbol if there is one.  */
567
568 coff_symbol_type *
569 coff_symbol_from (ignore_abfd, symbol)
570      bfd *ignore_abfd ATTRIBUTE_UNUSED;
571      asymbol *symbol;
572 {
573   if (!bfd_family_coff (bfd_asymbol_bfd (symbol)))
574     return (coff_symbol_type *) NULL;
575
576   if (bfd_asymbol_bfd (symbol)->tdata.coff_obj_data == (coff_data_type *) NULL)
577     return (coff_symbol_type *) NULL;
578
579   return (coff_symbol_type *) symbol;
580 }
581
582 static void
583 fixup_symbol_value (abfd, coff_symbol_ptr, syment)
584      bfd *abfd;
585      coff_symbol_type *coff_symbol_ptr;
586      struct internal_syment *syment;
587 {
588
589   /* Normalize the symbol flags */
590   if (bfd_is_com_section (coff_symbol_ptr->symbol.section))
591     {
592       /* a common symbol is undefined with a value */
593       syment->n_scnum = N_UNDEF;
594       syment->n_value = coff_symbol_ptr->symbol.value;
595     }
596   else if ((coff_symbol_ptr->symbol.flags & BSF_DEBUGGING) != 0
597            && (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING_RELOC) == 0)
598     {
599       syment->n_value = coff_symbol_ptr->symbol.value;
600     }
601   else if (bfd_is_und_section (coff_symbol_ptr->symbol.section))
602     {
603       syment->n_scnum = N_UNDEF;
604       syment->n_value = 0;
605     }
606   /* FIXME: Do we need to handle the absolute section here?  */
607   else
608     {
609       if (coff_symbol_ptr->symbol.section)
610         {
611           syment->n_scnum =
612             coff_symbol_ptr->symbol.section->output_section->target_index;
613
614           syment->n_value = (coff_symbol_ptr->symbol.value
615                              + coff_symbol_ptr->symbol.section->output_offset);
616           if (! obj_pe (abfd))
617             {
618               syment->n_value += (syment->n_sclass == C_STATLAB)
619                 ? coff_symbol_ptr->symbol.section->output_section->lma
620                 : coff_symbol_ptr->symbol.section->output_section->vma;
621             }
622         }
623       else
624         {
625           BFD_ASSERT (0);
626           /* This can happen, but I don't know why yet (steve@cygnus.com) */
627           syment->n_scnum = N_ABS;
628           syment->n_value = coff_symbol_ptr->symbol.value;
629         }
630     }
631 }
632
633 /* Run through all the symbols in the symbol table and work out what
634    their indexes into the symbol table will be when output.
635
636    Coff requires that each C_FILE symbol points to the next one in the
637    chain, and that the last one points to the first external symbol. We
638    do that here too.  */
639
640 boolean
641 coff_renumber_symbols (bfd_ptr, first_undef)
642      bfd *bfd_ptr;
643      int *first_undef;
644 {
645   unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
646   asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
647   unsigned int native_index = 0;
648   struct internal_syment *last_file = (struct internal_syment *) NULL;
649   unsigned int symbol_index;
650
651   /* COFF demands that undefined symbols come after all other symbols.
652      Since we don't need to impose this extra knowledge on all our
653      client programs, deal with that here.  Sort the symbol table;
654      just move the undefined symbols to the end, leaving the rest
655      alone.  The O'Reilly book says that defined global symbols come
656      at the end before the undefined symbols, so we do that here as
657      well.  */
658   /* @@ Do we have some condition we could test for, so we don't always
659      have to do this?  I don't think relocatability is quite right, but
660      I'm not certain.  [raeburn:19920508.1711EST]  */
661   {
662     asymbol **newsyms;
663     unsigned int i;
664     bfd_size_type amt;
665
666     amt = sizeof (asymbol *) * ((bfd_size_type) symbol_count + 1);
667     newsyms = (asymbol **) bfd_alloc (bfd_ptr, amt);
668     if (!newsyms)
669       return false;
670     bfd_ptr->outsymbols = newsyms;
671     for (i = 0; i < symbol_count; i++)
672       if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) != 0
673           || (!bfd_is_und_section (symbol_ptr_ptr[i]->section)
674               && !bfd_is_com_section (symbol_ptr_ptr[i]->section)
675               && ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) != 0
676                   || ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK))
677                       == 0))))
678         *newsyms++ = symbol_ptr_ptr[i];
679
680     for (i = 0; i < symbol_count; i++)
681       if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
682           && !bfd_is_und_section (symbol_ptr_ptr[i]->section)
683           && (bfd_is_com_section (symbol_ptr_ptr[i]->section)
684               || ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) == 0
685                   && ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK))
686                       != 0))))
687         *newsyms++ = symbol_ptr_ptr[i];
688
689     *first_undef = newsyms - bfd_ptr->outsymbols;
690
691     for (i = 0; i < symbol_count; i++)
692       if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
693           && bfd_is_und_section (symbol_ptr_ptr[i]->section))
694         *newsyms++ = symbol_ptr_ptr[i];
695     *newsyms = (asymbol *) NULL;
696     symbol_ptr_ptr = bfd_ptr->outsymbols;
697   }
698
699   for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
700     {
701       coff_symbol_type *coff_symbol_ptr = coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]);
702       symbol_ptr_ptr[symbol_index]->udata.i = symbol_index;
703       if (coff_symbol_ptr && coff_symbol_ptr->native)
704         {
705           combined_entry_type *s = coff_symbol_ptr->native;
706           int i;
707
708           if (s->u.syment.n_sclass == C_FILE)
709             {
710               if (last_file != (struct internal_syment *) NULL)
711                 last_file->n_value = native_index;
712               last_file = &(s->u.syment);
713             }
714           else
715             {
716
717               /* Modify the symbol values according to their section and
718                  type */
719
720               fixup_symbol_value (bfd_ptr, coff_symbol_ptr, &(s->u.syment));
721             }
722           for (i = 0; i < s->u.syment.n_numaux + 1; i++)
723             s[i].offset = native_index++;
724         }
725       else
726         {
727           native_index++;
728         }
729     }
730   obj_conv_table_size (bfd_ptr) = native_index;
731
732   return true;
733 }
734
735 /* Run thorough the symbol table again, and fix it so that all
736    pointers to entries are changed to the entries' index in the output
737    symbol table.  */
738
739 void
740 coff_mangle_symbols (bfd_ptr)
741      bfd *bfd_ptr;
742 {
743   unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
744   asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
745   unsigned int symbol_index;
746
747   for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
748     {
749       coff_symbol_type *coff_symbol_ptr =
750       coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]);
751
752       if (coff_symbol_ptr && coff_symbol_ptr->native)
753         {
754           int i;
755           combined_entry_type *s = coff_symbol_ptr->native;
756
757           if (s->fix_value)
758             {
759               /* FIXME: We should use a union here.  */
760               s->u.syment.n_value =
761                 (bfd_vma)((combined_entry_type *)
762                           ((unsigned long) s->u.syment.n_value))->offset;
763               s->fix_value = 0;
764             }
765           if (s->fix_line)
766             {
767               /* The value is the offset into the line number entries
768                  for the symbol's section.  On output, the symbol's
769                  section should be N_DEBUG.  */
770               s->u.syment.n_value =
771                 (coff_symbol_ptr->symbol.section->output_section->line_filepos
772                  + s->u.syment.n_value * bfd_coff_linesz (bfd_ptr));
773               coff_symbol_ptr->symbol.section =
774                 coff_section_from_bfd_index (bfd_ptr, N_DEBUG);
775               BFD_ASSERT (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING);
776             }
777           for (i = 0; i < s->u.syment.n_numaux; i++)
778             {
779               combined_entry_type *a = s + i + 1;
780               if (a->fix_tag)
781                 {
782                   a->u.auxent.x_sym.x_tagndx.l =
783                     a->u.auxent.x_sym.x_tagndx.p->offset;
784                   a->fix_tag = 0;
785                 }
786               if (a->fix_end)
787                 {
788                   a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l =
789                     a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p->offset;
790                   a->fix_end = 0;
791                 }
792               if (a->fix_scnlen)
793                 {
794                   a->u.auxent.x_csect.x_scnlen.l =
795                     a->u.auxent.x_csect.x_scnlen.p->offset;
796                   a->fix_scnlen = 0;
797                 }
798             }
799         }
800     }
801 }
802
803 static void
804 coff_fix_symbol_name (abfd, symbol, native, string_size_p,
805                       debug_string_section_p, debug_string_size_p)
806      bfd *abfd;
807      asymbol *symbol;
808      combined_entry_type *native;
809      bfd_size_type *string_size_p;
810      asection **debug_string_section_p;
811      bfd_size_type *debug_string_size_p;
812 {
813   unsigned int name_length;
814   union internal_auxent *auxent;
815   char *name = (char *) (symbol->name);
816
817   if (name == (char *) NULL)
818     {
819       /* coff symbols always have names, so we'll make one up */
820       symbol->name = "strange";
821       name = (char *) symbol->name;
822     }
823   name_length = strlen (name);
824
825   if (native->u.syment.n_sclass == C_FILE
826       && native->u.syment.n_numaux > 0)
827     {
828       unsigned int filnmlen;
829
830       if (bfd_coff_force_symnames_in_strings (abfd))
831         {
832           native->u.syment._n._n_n._n_offset =
833               (*string_size_p + STRING_SIZE_SIZE);
834           native->u.syment._n._n_n._n_zeroes = 0;
835           *string_size_p += 6;  /* strlen(".file") + 1 */
836         }
837       else
838         strncpy (native->u.syment._n._n_name, ".file", SYMNMLEN);
839
840       auxent = &(native + 1)->u.auxent;
841
842       filnmlen = bfd_coff_filnmlen (abfd);
843
844       if (bfd_coff_long_filenames (abfd))
845         {
846           if (name_length <= filnmlen)
847             {
848               strncpy (auxent->x_file.x_fname, name, filnmlen);
849             }
850           else
851             {
852               auxent->x_file.x_n.x_offset = *string_size_p + STRING_SIZE_SIZE;
853               auxent->x_file.x_n.x_zeroes = 0;
854               *string_size_p += name_length + 1;
855             }
856         }
857       else
858         {
859           strncpy (auxent->x_file.x_fname, name, filnmlen);
860           if (name_length > filnmlen)
861             name[filnmlen] = '\0';
862         }
863     }
864   else
865     {
866       if (name_length <= SYMNMLEN && !bfd_coff_force_symnames_in_strings (abfd))
867         {
868           /* This name will fit into the symbol neatly */
869           strncpy (native->u.syment._n._n_name, symbol->name, SYMNMLEN);
870         }
871       else if (!bfd_coff_symname_in_debug (abfd, &native->u.syment))
872         {
873           native->u.syment._n._n_n._n_offset = (*string_size_p
874                                                 + STRING_SIZE_SIZE);
875           native->u.syment._n._n_n._n_zeroes = 0;
876           *string_size_p += name_length + 1;
877         }
878       else
879         {
880           file_ptr filepos;
881           bfd_byte buf[4];
882           int prefix_len = bfd_coff_debug_string_prefix_length (abfd);
883
884           /* This name should be written into the .debug section.  For
885              some reason each name is preceded by a two byte length
886              and also followed by a null byte.  FIXME: We assume that
887              the .debug section has already been created, and that it
888              is large enough.  */
889           if (*debug_string_section_p == (asection *) NULL)
890             *debug_string_section_p = bfd_get_section_by_name (abfd, ".debug");
891           filepos = bfd_tell (abfd);
892           if (prefix_len == 4)
893             bfd_put_32 (abfd, (bfd_vma) (name_length + 1), buf);
894           else
895             bfd_put_16 (abfd, (bfd_vma) (name_length + 1), buf);
896
897           if (!bfd_set_section_contents (abfd,
898                                          *debug_string_section_p,
899                                          (PTR) buf,
900                                          (file_ptr) *debug_string_size_p,
901                                          (bfd_size_type) prefix_len)
902               || !bfd_set_section_contents (abfd,
903                                             *debug_string_section_p,
904                                             (PTR) symbol->name,
905                                             (file_ptr) (*debug_string_size_p
906                                                         + prefix_len),
907                                             (bfd_size_type) name_length + 1))
908             abort ();
909           if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
910             abort ();
911           native->u.syment._n._n_n._n_offset =
912               *debug_string_size_p + prefix_len;
913           native->u.syment._n._n_n._n_zeroes = 0;
914           *debug_string_size_p += name_length + 1 + prefix_len;
915         }
916     }
917 }
918
919 /* We need to keep track of the symbol index so that when we write out
920    the relocs we can get the index for a symbol.  This method is a
921    hack.  FIXME.  */
922
923 #define set_index(symbol, idx)  ((symbol)->udata.i = (idx))
924
925 /* Write a symbol out to a COFF file.  */
926
927 static boolean
928 coff_write_symbol (abfd, symbol, native, written, string_size_p,
929                    debug_string_section_p, debug_string_size_p)
930      bfd *abfd;
931      asymbol *symbol;
932      combined_entry_type *native;
933      bfd_vma *written;
934      bfd_size_type *string_size_p;
935      asection **debug_string_section_p;
936      bfd_size_type *debug_string_size_p;
937 {
938   unsigned int numaux = native->u.syment.n_numaux;
939   int type = native->u.syment.n_type;
940   int class = native->u.syment.n_sclass;
941   PTR buf;
942   bfd_size_type symesz;
943
944   if (native->u.syment.n_sclass == C_FILE)
945     symbol->flags |= BSF_DEBUGGING;
946
947   if (symbol->flags & BSF_DEBUGGING
948       && bfd_is_abs_section (symbol->section))
949     {
950       native->u.syment.n_scnum = N_DEBUG;
951     }
952   else if (bfd_is_abs_section (symbol->section))
953     {
954       native->u.syment.n_scnum = N_ABS;
955     }
956   else if (bfd_is_und_section (symbol->section))
957     {
958       native->u.syment.n_scnum = N_UNDEF;
959     }
960   else
961     {
962       native->u.syment.n_scnum =
963         symbol->section->output_section->target_index;
964     }
965
966   coff_fix_symbol_name (abfd, symbol, native, string_size_p,
967                         debug_string_section_p, debug_string_size_p);
968
969   symesz = bfd_coff_symesz (abfd);
970   buf = bfd_alloc (abfd, symesz);
971   if (!buf)
972     return false;
973   bfd_coff_swap_sym_out (abfd, &native->u.syment, buf);
974   if (bfd_bwrite (buf, symesz, abfd) != symesz)
975     return false;
976   bfd_release (abfd, buf);
977
978   if (native->u.syment.n_numaux > 0)
979     {
980       bfd_size_type auxesz;
981       unsigned int j;
982
983       auxesz = bfd_coff_auxesz (abfd);
984       buf = bfd_alloc (abfd, auxesz);
985       if (!buf)
986         return false;
987       for (j = 0; j < native->u.syment.n_numaux; j++)
988         {
989           bfd_coff_swap_aux_out (abfd,
990                                  &((native + j + 1)->u.auxent),
991                                  type,
992                                  class,
993                                  (int) j,
994                                  native->u.syment.n_numaux,
995                                  buf);
996           if (bfd_bwrite (buf, auxesz, abfd) != auxesz)
997             return false;
998         }
999       bfd_release (abfd, buf);
1000     }
1001
1002   /* Store the index for use when we write out the relocs.  */
1003   set_index (symbol, *written);
1004
1005   *written += numaux + 1;
1006   return true;
1007 }
1008
1009 /* Write out a symbol to a COFF file that does not come from a COFF
1010    file originally.  This symbol may have been created by the linker,
1011    or we may be linking a non COFF file to a COFF file.  */
1012
1013 static boolean
1014 coff_write_alien_symbol (abfd, symbol, written, string_size_p,
1015                          debug_string_section_p, debug_string_size_p)
1016      bfd *abfd;
1017      asymbol *symbol;
1018      bfd_vma *written;
1019      bfd_size_type *string_size_p;
1020      asection **debug_string_section_p;
1021      bfd_size_type *debug_string_size_p;
1022 {
1023   combined_entry_type *native;
1024   combined_entry_type dummy;
1025
1026   native = &dummy;
1027   native->u.syment.n_type = T_NULL;
1028   native->u.syment.n_flags = 0;
1029   if (bfd_is_und_section (symbol->section))
1030     {
1031       native->u.syment.n_scnum = N_UNDEF;
1032       native->u.syment.n_value = symbol->value;
1033     }
1034   else if (bfd_is_com_section (symbol->section))
1035     {
1036       native->u.syment.n_scnum = N_UNDEF;
1037       native->u.syment.n_value = symbol->value;
1038     }
1039   else if (symbol->flags & BSF_DEBUGGING)
1040     {
1041       /* There isn't much point to writing out a debugging symbol
1042          unless we are prepared to convert it into COFF debugging
1043          format.  So, we just ignore them.  We must clobber the symbol
1044          name to keep it from being put in the string table.  */
1045       symbol->name = "";
1046       return true;
1047     }
1048   else
1049     {
1050       native->u.syment.n_scnum =
1051         symbol->section->output_section->target_index;
1052       native->u.syment.n_value = (symbol->value
1053                                   + symbol->section->output_offset);
1054       if (! obj_pe (abfd))
1055         native->u.syment.n_value += symbol->section->output_section->vma;
1056
1057       /* Copy the any flags from the file header into the symbol.
1058          FIXME: Why?  */
1059       {
1060         coff_symbol_type *c = coff_symbol_from (abfd, symbol);
1061         if (c != (coff_symbol_type *) NULL)
1062           native->u.syment.n_flags = bfd_asymbol_bfd (&c->symbol)->flags;
1063       }
1064     }
1065
1066   native->u.syment.n_type = 0;
1067   if (symbol->flags & BSF_LOCAL)
1068     native->u.syment.n_sclass = C_STAT;
1069   else if (symbol->flags & BSF_WEAK)
1070     native->u.syment.n_sclass = obj_pe (abfd) ? C_NT_WEAK : C_WEAKEXT;
1071   else
1072     native->u.syment.n_sclass = C_EXT;
1073   native->u.syment.n_numaux = 0;
1074
1075   return coff_write_symbol (abfd, symbol, native, written, string_size_p,
1076                             debug_string_section_p, debug_string_size_p);
1077 }
1078
1079 /* Write a native symbol to a COFF file.  */
1080
1081 static boolean
1082 coff_write_native_symbol (abfd, symbol, written, string_size_p,
1083                           debug_string_section_p, debug_string_size_p)
1084      bfd *abfd;
1085      coff_symbol_type *symbol;
1086      bfd_vma *written;
1087      bfd_size_type *string_size_p;
1088      asection **debug_string_section_p;
1089      bfd_size_type *debug_string_size_p;
1090 {
1091   combined_entry_type *native = symbol->native;
1092   alent *lineno = symbol->lineno;
1093
1094   /* If this symbol has an associated line number, we must store the
1095      symbol index in the line number field.  We also tag the auxent to
1096      point to the right place in the lineno table.  */
1097   if (lineno && !symbol->done_lineno && symbol->symbol.section->owner != NULL)
1098     {
1099       unsigned int count = 0;
1100       lineno[count].u.offset = *written;
1101       if (native->u.syment.n_numaux)
1102         {
1103           union internal_auxent *a = &((native + 1)->u.auxent);
1104
1105           a->x_sym.x_fcnary.x_fcn.x_lnnoptr =
1106             symbol->symbol.section->output_section->moving_line_filepos;
1107         }
1108
1109       /* Count and relocate all other linenumbers.  */
1110       count++;
1111       while (lineno[count].line_number != 0)
1112         {
1113 #if 0
1114           /* 13 april 92. sac
1115              I've been told this, but still need proof:
1116              > The second bug is also in `bfd/coffcode.h'.  This bug
1117              > causes the linker to screw up the pc-relocations for
1118              > all the line numbers in COFF code.  This bug isn't only
1119              > specific to A29K implementations, but affects all
1120              > systems using COFF format binaries.  Note that in COFF
1121              > object files, the line number core offsets output by
1122              > the assembler are relative to the start of each
1123              > procedure, not to the start of the .text section.  This
1124              > patch relocates the line numbers relative to the
1125              > `native->u.syment.n_value' instead of the section
1126              > virtual address.
1127              > modular!olson@cs.arizona.edu (Jon Olson)
1128            */
1129           lineno[count].u.offset += native->u.syment.n_value;
1130 #else
1131           lineno[count].u.offset +=
1132             (symbol->symbol.section->output_section->vma
1133              + symbol->symbol.section->output_offset);
1134 #endif
1135           count++;
1136         }
1137       symbol->done_lineno = true;
1138
1139       symbol->symbol.section->output_section->moving_line_filepos +=
1140         count * bfd_coff_linesz (abfd);
1141     }
1142
1143   return coff_write_symbol (abfd, &(symbol->symbol), native, written,
1144                             string_size_p, debug_string_section_p,
1145                             debug_string_size_p);
1146 }
1147
1148 /* Write out the COFF symbols.  */
1149
1150 boolean
1151 coff_write_symbols (abfd)
1152      bfd *abfd;
1153 {
1154   bfd_size_type string_size;
1155   asection *debug_string_section;
1156   bfd_size_type debug_string_size;
1157   unsigned int i;
1158   unsigned int limit = bfd_get_symcount (abfd);
1159   bfd_signed_vma written = 0;
1160   asymbol **p;
1161
1162   string_size = 0;
1163   debug_string_section = NULL;
1164   debug_string_size = 0;
1165
1166   /* If this target supports long section names, they must be put into
1167      the string table.  This is supported by PE.  This code must
1168      handle section names just as they are handled in
1169      coff_write_object_contents.  */
1170   if (bfd_coff_long_section_names (abfd))
1171     {
1172       asection *o;
1173
1174       for (o = abfd->sections; o != NULL; o = o->next)
1175         {
1176           size_t len;
1177
1178           len = strlen (o->name);
1179           if (len > SCNNMLEN)
1180             string_size += len + 1;
1181         }
1182     }
1183
1184   /* Seek to the right place */
1185   if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0)
1186     return false;
1187
1188   /* Output all the symbols we have */
1189
1190   written = 0;
1191   for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
1192     {
1193       asymbol *symbol = *p;
1194       coff_symbol_type *c_symbol = coff_symbol_from (abfd, symbol);
1195
1196       if (c_symbol == (coff_symbol_type *) NULL
1197           || c_symbol->native == (combined_entry_type *) NULL)
1198         {
1199           if (!coff_write_alien_symbol (abfd, symbol, &written, &string_size,
1200                                         &debug_string_section,
1201                                         &debug_string_size))
1202             return false;
1203         }
1204       else
1205         {
1206           if (!coff_write_native_symbol (abfd, c_symbol, &written,
1207                                          &string_size, &debug_string_section,
1208                                          &debug_string_size))
1209             return false;
1210         }
1211     }
1212
1213   obj_raw_syment_count (abfd) = written;
1214
1215   /* Now write out strings */
1216
1217   if (string_size != 0)
1218     {
1219       unsigned int size = string_size + STRING_SIZE_SIZE;
1220       bfd_byte buffer[STRING_SIZE_SIZE];
1221
1222 #if STRING_SIZE_SIZE == 4
1223       H_PUT_32 (abfd, size, buffer);
1224 #else
1225  #error Change H_PUT_32
1226 #endif
1227       if (bfd_bwrite ((PTR) buffer, (bfd_size_type) sizeof (buffer), abfd)
1228           != sizeof (buffer))
1229         return false;
1230
1231       /* Handle long section names.  This code must handle section
1232          names just as they are handled in coff_write_object_contents.  */
1233       if (bfd_coff_long_section_names (abfd))
1234         {
1235           asection *o;
1236
1237           for (o = abfd->sections; o != NULL; o = o->next)
1238             {
1239               size_t len;
1240
1241               len = strlen (o->name);
1242               if (len > SCNNMLEN)
1243                 {
1244                   if (bfd_bwrite (o->name, (bfd_size_type) (len + 1), abfd)
1245                       != len + 1)
1246                     return false;
1247                 }
1248             }
1249         }
1250
1251       for (p = abfd->outsymbols, i = 0;
1252            i < limit;
1253            i++, p++)
1254         {
1255           asymbol *q = *p;
1256           size_t name_length = strlen (q->name);
1257           coff_symbol_type *c_symbol = coff_symbol_from (abfd, q);
1258           size_t maxlen;
1259
1260           /* Figure out whether the symbol name should go in the string
1261              table.  Symbol names that are short enough are stored
1262              directly in the syment structure.  File names permit a
1263              different, longer, length in the syment structure.  On
1264              XCOFF, some symbol names are stored in the .debug section
1265              rather than in the string table.  */
1266
1267           if (c_symbol == NULL
1268               || c_symbol->native == NULL)
1269             {
1270               /* This is not a COFF symbol, so it certainly is not a
1271                  file name, nor does it go in the .debug section.  */
1272               maxlen = bfd_coff_force_symnames_in_strings (abfd) ? 0 : SYMNMLEN;
1273             }
1274           else if (bfd_coff_symname_in_debug (abfd,
1275                                               &c_symbol->native->u.syment))
1276             {
1277               /* This symbol name is in the XCOFF .debug section.
1278                  Don't write it into the string table.  */
1279               maxlen = name_length;
1280             }
1281           else if (c_symbol->native->u.syment.n_sclass == C_FILE
1282                    && c_symbol->native->u.syment.n_numaux > 0)
1283             {
1284               if (bfd_coff_force_symnames_in_strings (abfd))
1285                 {
1286                   if (bfd_bwrite (".file", (bfd_size_type) 6, abfd) != 6)
1287                     return false;
1288                 }
1289               maxlen = bfd_coff_filnmlen (abfd);
1290             }
1291           else
1292             maxlen = bfd_coff_force_symnames_in_strings (abfd) ? 0 : SYMNMLEN;
1293
1294           if (name_length > maxlen)
1295             {
1296               if (bfd_bwrite ((PTR) (q->name), (bfd_size_type) name_length + 1,
1297                              abfd) != name_length + 1)
1298                 return false;
1299             }
1300         }
1301     }
1302   else
1303     {
1304       /* We would normally not write anything here, but we'll write
1305          out 4 so that any stupid coff reader which tries to read the
1306          string table even when there isn't one won't croak.  */
1307       unsigned int size = STRING_SIZE_SIZE;
1308       bfd_byte buffer[STRING_SIZE_SIZE];
1309
1310 #if STRING_SIZE_SIZE == 4
1311       H_PUT_32 (abfd, size, buffer);
1312 #else
1313  #error Change H_PUT_32
1314 #endif
1315       if (bfd_bwrite ((PTR) buffer, (bfd_size_type) STRING_SIZE_SIZE, abfd)
1316           != STRING_SIZE_SIZE)
1317         return false;
1318     }
1319
1320   /* Make sure the .debug section was created to be the correct size.
1321      We should create it ourselves on the fly, but we don't because
1322      BFD won't let us write to any section until we know how large all
1323      the sections are.  We could still do it by making another pass
1324      over the symbols.  FIXME.  */
1325   BFD_ASSERT (debug_string_size == 0
1326               || (debug_string_section != (asection *) NULL
1327                   && (BFD_ALIGN (debug_string_size,
1328                                  1 << debug_string_section->alignment_power)
1329                       == bfd_section_size (abfd, debug_string_section))));
1330
1331   return true;
1332 }
1333
1334 boolean
1335 coff_write_linenumbers (abfd)
1336      bfd *abfd;
1337 {
1338   asection *s;
1339   bfd_size_type linesz;
1340   PTR buff;
1341
1342   linesz = bfd_coff_linesz (abfd);
1343   buff = bfd_alloc (abfd, linesz);
1344   if (!buff)
1345     return false;
1346   for (s = abfd->sections; s != (asection *) NULL; s = s->next)
1347     {
1348       if (s->lineno_count)
1349         {
1350           asymbol **q = abfd->outsymbols;
1351           if (bfd_seek (abfd, s->line_filepos, SEEK_SET) != 0)
1352             return false;
1353           /* Find all the linenumbers in this section */
1354           while (*q)
1355             {
1356               asymbol *p = *q;
1357               if (p->section->output_section == s)
1358                 {
1359                   alent *l =
1360                   BFD_SEND (bfd_asymbol_bfd (p), _get_lineno,
1361                             (bfd_asymbol_bfd (p), p));
1362                   if (l)
1363                     {
1364                       /* Found a linenumber entry, output */
1365                       struct internal_lineno out;
1366                       memset ((PTR) & out, 0, sizeof (out));
1367                       out.l_lnno = 0;
1368                       out.l_addr.l_symndx = l->u.offset;
1369                       bfd_coff_swap_lineno_out (abfd, &out, buff);
1370                       if (bfd_bwrite (buff, (bfd_size_type) linesz, abfd)
1371                           != linesz)
1372                         return false;
1373                       l++;
1374                       while (l->line_number)
1375                         {
1376                           out.l_lnno = l->line_number;
1377                           out.l_addr.l_symndx = l->u.offset;
1378                           bfd_coff_swap_lineno_out (abfd, &out, buff);
1379                           if (bfd_bwrite (buff, (bfd_size_type) linesz, abfd)
1380                               != linesz)
1381                             return false;
1382                           l++;
1383                         }
1384                     }
1385                 }
1386               q++;
1387             }
1388         }
1389     }
1390   bfd_release (abfd, buff);
1391   return true;
1392 }
1393
1394 alent *
1395 coff_get_lineno (ignore_abfd, symbol)
1396      bfd *ignore_abfd ATTRIBUTE_UNUSED;
1397      asymbol *symbol;
1398 {
1399   return coffsymbol (symbol)->lineno;
1400 }
1401
1402 #if 0
1403
1404 /* This is only called from coff_add_missing_symbols, which has been
1405    disabled.  */
1406
1407 asymbol *
1408 coff_section_symbol (abfd, name)
1409      bfd *abfd;
1410      char *name;
1411 {
1412   asection *sec = bfd_make_section_old_way (abfd, name);
1413   asymbol *sym;
1414   combined_entry_type *csym;
1415
1416   sym = sec->symbol;
1417   csym = coff_symbol_from (abfd, sym)->native;
1418   /* Make sure back-end COFF stuff is there.  */
1419   if (csym == 0)
1420     {
1421       struct foo
1422         {
1423           coff_symbol_type sym;
1424           /* @@FIXME This shouldn't use a fixed size!!  */
1425           combined_entry_type e[10];
1426         };
1427       struct foo *f;
1428       f = (struct foo *) bfd_alloc (abfd, (bfd_size_type) sizeof (*f));
1429       if (!f)
1430         {
1431           bfd_set_error (bfd_error_no_error);
1432           return NULL;
1433         }
1434       memset ((char *) f, 0, sizeof (*f));
1435       coff_symbol_from (abfd, sym)->native = csym = f->e;
1436     }
1437   csym[0].u.syment.n_sclass = C_STAT;
1438   csym[0].u.syment.n_numaux = 1;
1439 /*  SF_SET_STATICS (sym);       @@ ??? */
1440   csym[1].u.auxent.x_scn.x_scnlen = sec->_raw_size;
1441   csym[1].u.auxent.x_scn.x_nreloc = sec->reloc_count;
1442   csym[1].u.auxent.x_scn.x_nlinno = sec->lineno_count;
1443
1444   if (sec->output_section == NULL)
1445     {
1446       sec->output_section = sec;
1447       sec->output_offset = 0;
1448     }
1449
1450   return sym;
1451 }
1452
1453 #endif /* 0 */
1454
1455 /* This function transforms the offsets into the symbol table into
1456    pointers to syments.  */
1457
1458 static void
1459 coff_pointerize_aux (abfd, table_base, symbol, indaux, auxent)
1460      bfd *abfd;
1461      combined_entry_type *table_base;
1462      combined_entry_type *symbol;
1463      unsigned int indaux;
1464      combined_entry_type *auxent;
1465 {
1466   unsigned int type = symbol->u.syment.n_type;
1467   unsigned int class = symbol->u.syment.n_sclass;
1468
1469   if (coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1470     {
1471       if ((*coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1472           (abfd, table_base, symbol, indaux, auxent))
1473         return;
1474     }
1475
1476   /* Don't bother if this is a file or a section */
1477   if (class == C_STAT && type == T_NULL)
1478     return;
1479   if (class == C_FILE)
1480     return;
1481
1482   /* Otherwise patch up */
1483 #define N_TMASK coff_data (abfd)->local_n_tmask
1484 #define N_BTSHFT coff_data (abfd)->local_n_btshft
1485   if ((ISFCN (type) || ISTAG (class) || class == C_BLOCK || class == C_FCN)
1486       && auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l > 0)
1487     {
1488       auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p =
1489         table_base + auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l;
1490       auxent->fix_end = 1;
1491     }
1492   /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can
1493      generate one, so we must be careful to ignore it.  */
1494   if (auxent->u.auxent.x_sym.x_tagndx.l > 0)
1495     {
1496       auxent->u.auxent.x_sym.x_tagndx.p =
1497         table_base + auxent->u.auxent.x_sym.x_tagndx.l;
1498       auxent->fix_tag = 1;
1499     }
1500 }
1501
1502 /* Allocate space for the ".debug" section, and read it.
1503    We did not read the debug section until now, because
1504    we didn't want to go to the trouble until someone needed it.  */
1505
1506 static char *
1507 build_debug_section (abfd)
1508      bfd *abfd;
1509 {
1510   char *debug_section;
1511   file_ptr position;
1512   bfd_size_type sec_size;
1513
1514   asection *sect = bfd_get_section_by_name (abfd, ".debug");
1515
1516   if (!sect)
1517     {
1518       bfd_set_error (bfd_error_no_debug_section);
1519       return NULL;
1520     }
1521
1522   sec_size = bfd_get_section_size_before_reloc (sect);
1523   debug_section = (PTR) bfd_alloc (abfd, sec_size);
1524   if (debug_section == NULL)
1525     return NULL;
1526
1527   /* Seek to the beginning of the `.debug' section and read it.
1528      Save the current position first; it is needed by our caller.
1529      Then read debug section and reset the file pointer.  */
1530
1531   position = bfd_tell (abfd);
1532   if (bfd_seek (abfd, sect->filepos, SEEK_SET) != 0
1533       || bfd_bread (debug_section, sec_size, abfd) != sec_size
1534       || bfd_seek (abfd, position, SEEK_SET) != 0)
1535     return NULL;
1536   return debug_section;
1537 }
1538
1539 /* Return a pointer to a malloc'd copy of 'name'.  'name' may not be
1540    \0-terminated, but will not exceed 'maxlen' characters.  The copy *will*
1541    be \0-terminated.  */
1542 static char *
1543 copy_name (abfd, name, maxlen)
1544      bfd *abfd;
1545      char *name;
1546      size_t maxlen;
1547 {
1548   size_t len;
1549   char *newname;
1550
1551   for (len = 0; len < maxlen; ++len)
1552     {
1553       if (name[len] == '\0')
1554         {
1555           break;
1556         }
1557     }
1558
1559   if ((newname = (PTR) bfd_alloc (abfd, (bfd_size_type) len + 1)) == NULL)
1560     return (NULL);
1561   strncpy (newname, name, len);
1562   newname[len] = '\0';
1563   return newname;
1564 }
1565
1566 /* Read in the external symbols.  */
1567
1568 boolean
1569 _bfd_coff_get_external_symbols (abfd)
1570      bfd *abfd;
1571 {
1572   bfd_size_type symesz;
1573   bfd_size_type size;
1574   PTR syms;
1575
1576   if (obj_coff_external_syms (abfd) != NULL)
1577     return true;
1578
1579   symesz = bfd_coff_symesz (abfd);
1580
1581   size = obj_raw_syment_count (abfd) * symesz;
1582
1583   syms = (PTR) bfd_malloc (size);
1584   if (syms == NULL && size != 0)
1585     return false;
1586
1587   if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0
1588       || bfd_bread (syms, size, abfd) != size)
1589     {
1590       if (syms != NULL)
1591         free (syms);
1592       return false;
1593     }
1594
1595   obj_coff_external_syms (abfd) = syms;
1596
1597   return true;
1598 }
1599
1600 /* Read in the external strings.  The strings are not loaded until
1601    they are needed.  This is because we have no simple way of
1602    detecting a missing string table in an archive.  */
1603
1604 const char *
1605 _bfd_coff_read_string_table (abfd)
1606      bfd *abfd;
1607 {
1608   char extstrsize[STRING_SIZE_SIZE];
1609   bfd_size_type strsize;
1610   char *strings;
1611   file_ptr pos;
1612
1613   if (obj_coff_strings (abfd) != NULL)
1614     return obj_coff_strings (abfd);
1615
1616   if (obj_sym_filepos (abfd) == 0)
1617     {
1618       bfd_set_error (bfd_error_no_symbols);
1619       return NULL;
1620     }
1621
1622   pos = obj_sym_filepos (abfd);
1623   pos += obj_raw_syment_count (abfd) * bfd_coff_symesz (abfd);
1624   if (bfd_seek (abfd, pos, SEEK_SET) != 0)
1625     return NULL;
1626
1627   if (bfd_bread (extstrsize, (bfd_size_type) sizeof extstrsize, abfd)
1628       != sizeof extstrsize)
1629     {
1630       if (bfd_get_error () != bfd_error_file_truncated)
1631         return NULL;
1632
1633       /* There is no string table.  */
1634       strsize = STRING_SIZE_SIZE;
1635     }
1636   else
1637     {
1638 #if STRING_SIZE_SIZE == 4
1639       strsize = H_GET_32 (abfd, extstrsize);
1640 #else
1641  #error Change H_GET_32
1642 #endif
1643     }
1644
1645   if (strsize < STRING_SIZE_SIZE)
1646     {
1647       (*_bfd_error_handler)
1648         (_("%s: bad string table size %lu"), bfd_archive_filename (abfd),
1649          (unsigned long) strsize);
1650       bfd_set_error (bfd_error_bad_value);
1651       return NULL;
1652     }
1653
1654   strings = (char *) bfd_malloc (strsize);
1655   if (strings == NULL)
1656     return NULL;
1657
1658   if (bfd_bread (strings + STRING_SIZE_SIZE, strsize - STRING_SIZE_SIZE, abfd)
1659       != strsize - STRING_SIZE_SIZE)
1660     {
1661       free (strings);
1662       return NULL;
1663     }
1664
1665   obj_coff_strings (abfd) = strings;
1666
1667   return strings;
1668 }
1669
1670 /* Free up the external symbols and strings read from a COFF file.  */
1671
1672 boolean
1673 _bfd_coff_free_symbols (abfd)
1674      bfd *abfd;
1675 {
1676   if (obj_coff_external_syms (abfd) != NULL
1677       && ! obj_coff_keep_syms (abfd))
1678     {
1679       free (obj_coff_external_syms (abfd));
1680       obj_coff_external_syms (abfd) = NULL;
1681     }
1682   if (obj_coff_strings (abfd) != NULL
1683       && ! obj_coff_keep_strings (abfd))
1684     {
1685       free (obj_coff_strings (abfd));
1686       obj_coff_strings (abfd) = NULL;
1687     }
1688   return true;
1689 }
1690
1691 /* Read a symbol table into freshly bfd_allocated memory, swap it, and
1692    knit the symbol names into a normalized form.  By normalized here I
1693    mean that all symbols have an n_offset pointer that points to a null-
1694    terminated string.  */
1695
1696 combined_entry_type *
1697 coff_get_normalized_symtab (abfd)
1698      bfd *abfd;
1699 {
1700   combined_entry_type *internal;
1701   combined_entry_type *internal_ptr;
1702   combined_entry_type *symbol_ptr;
1703   combined_entry_type *internal_end;
1704   size_t symesz;
1705   char *raw_src;
1706   char *raw_end;
1707   const char *string_table = NULL;
1708   char *debug_section = NULL;
1709   bfd_size_type size;
1710
1711   if (obj_raw_syments (abfd) != NULL)
1712     return obj_raw_syments (abfd);
1713
1714   size = obj_raw_syment_count (abfd) * sizeof (combined_entry_type);
1715   internal = (combined_entry_type *) bfd_zalloc (abfd, size);
1716   if (internal == NULL && size != 0)
1717     return NULL;
1718   internal_end = internal + obj_raw_syment_count (abfd);
1719
1720   if (! _bfd_coff_get_external_symbols (abfd))
1721     return NULL;
1722
1723   raw_src = (char *) obj_coff_external_syms (abfd);
1724
1725   /* mark the end of the symbols */
1726   symesz = bfd_coff_symesz (abfd);
1727   raw_end = (char *) raw_src + obj_raw_syment_count (abfd) * symesz;
1728
1729   /* FIXME SOMEDAY.  A string table size of zero is very weird, but
1730      probably possible.  If one shows up, it will probably kill us.  */
1731
1732   /* Swap all the raw entries */
1733   for (internal_ptr = internal;
1734        raw_src < raw_end;
1735        raw_src += symesz, internal_ptr++)
1736     {
1737
1738       unsigned int i;
1739       bfd_coff_swap_sym_in (abfd, (PTR) raw_src,
1740                             (PTR) & internal_ptr->u.syment);
1741       symbol_ptr = internal_ptr;
1742
1743       for (i = 0;
1744            i < symbol_ptr->u.syment.n_numaux;
1745            i++)
1746         {
1747           internal_ptr++;
1748           raw_src += symesz;
1749           bfd_coff_swap_aux_in (abfd, (PTR) raw_src,
1750                                 symbol_ptr->u.syment.n_type,
1751                                 symbol_ptr->u.syment.n_sclass,
1752                                 (int) i, symbol_ptr->u.syment.n_numaux,
1753                                 &(internal_ptr->u.auxent));
1754           coff_pointerize_aux (abfd, internal, symbol_ptr, i,
1755                                internal_ptr);
1756         }
1757     }
1758
1759   /* Free the raw symbols, but not the strings (if we have them).  */
1760   obj_coff_keep_strings (abfd) = true;
1761   if (! _bfd_coff_free_symbols (abfd))
1762     return NULL;
1763
1764   for (internal_ptr = internal; internal_ptr < internal_end;
1765        internal_ptr++)
1766     {
1767       if (internal_ptr->u.syment.n_sclass == C_FILE
1768           && internal_ptr->u.syment.n_numaux > 0)
1769         {
1770           /* make a file symbol point to the name in the auxent, since
1771              the text ".file" is redundant */
1772           if ((internal_ptr + 1)->u.auxent.x_file.x_n.x_zeroes == 0)
1773             {
1774               /* the filename is a long one, point into the string table */
1775               if (string_table == NULL)
1776                 {
1777                   string_table = _bfd_coff_read_string_table (abfd);
1778                   if (string_table == NULL)
1779                     return NULL;
1780                 }
1781
1782               internal_ptr->u.syment._n._n_n._n_offset =
1783                 ((long)
1784                  (string_table
1785                   + (internal_ptr + 1)->u.auxent.x_file.x_n.x_offset));
1786             }
1787           else
1788             {
1789               /* Ordinary short filename, put into memory anyway.  The
1790                  Microsoft PE tools sometimes store a filename in
1791                  multiple AUX entries.  */
1792               if (internal_ptr->u.syment.n_numaux > 1
1793                   && coff_data (abfd)->pe)
1794                 {
1795                   internal_ptr->u.syment._n._n_n._n_offset =
1796                     ((long)
1797                      copy_name (abfd,
1798                                 (internal_ptr + 1)->u.auxent.x_file.x_fname,
1799                                 internal_ptr->u.syment.n_numaux * symesz));
1800                 }
1801               else
1802                 {
1803                   internal_ptr->u.syment._n._n_n._n_offset =
1804                     ((long)
1805                      copy_name (abfd,
1806                                 (internal_ptr + 1)->u.auxent.x_file.x_fname,
1807                                 (size_t) bfd_coff_filnmlen (abfd)));
1808                 }
1809             }
1810         }
1811       else
1812         {
1813           if (internal_ptr->u.syment._n._n_n._n_zeroes != 0)
1814             {
1815               /* This is a "short" name.  Make it long.  */
1816               size_t i;
1817               char *newstring;
1818
1819               /* find the length of this string without walking into memory
1820                  that isn't ours.  */
1821               for (i = 0; i < 8; ++i)
1822                 if (internal_ptr->u.syment._n._n_name[i] == '\0')
1823                   break;
1824
1825               newstring = (PTR) bfd_alloc (abfd, (bfd_size_type) (i + 1));
1826               if (newstring == NULL)
1827                 return (NULL);
1828               memset (newstring, 0, i + 1);
1829               strncpy (newstring, internal_ptr->u.syment._n._n_name, i);
1830               internal_ptr->u.syment._n._n_n._n_offset = (long int) newstring;
1831               internal_ptr->u.syment._n._n_n._n_zeroes = 0;
1832             }
1833           else if (internal_ptr->u.syment._n._n_n._n_offset == 0)
1834             internal_ptr->u.syment._n._n_n._n_offset = (long int) "";
1835           else if (!bfd_coff_symname_in_debug (abfd, &internal_ptr->u.syment))
1836             {
1837               /* Long name already.  Point symbol at the string in the
1838                  table.  */
1839               if (string_table == NULL)
1840                 {
1841                   string_table = _bfd_coff_read_string_table (abfd);
1842                   if (string_table == NULL)
1843                     return NULL;
1844                 }
1845               internal_ptr->u.syment._n._n_n._n_offset =
1846                 ((long int)
1847                  (string_table
1848                   + internal_ptr->u.syment._n._n_n._n_offset));
1849             }
1850           else
1851             {
1852               /* Long name in debug section.  Very similar.  */
1853               if (debug_section == NULL)
1854                 debug_section = build_debug_section (abfd);
1855               internal_ptr->u.syment._n._n_n._n_offset = (long int)
1856                 (debug_section + internal_ptr->u.syment._n._n_n._n_offset);
1857             }
1858         }
1859       internal_ptr += internal_ptr->u.syment.n_numaux;
1860     }
1861
1862   obj_raw_syments (abfd) = internal;
1863   BFD_ASSERT (obj_raw_syment_count (abfd)
1864               == (unsigned int) (internal_ptr - internal));
1865
1866   return (internal);
1867 }                               /* coff_get_normalized_symtab() */
1868
1869 long
1870 coff_get_reloc_upper_bound (abfd, asect)
1871      bfd *abfd;
1872      sec_ptr asect;
1873 {
1874   if (bfd_get_format (abfd) != bfd_object)
1875     {
1876       bfd_set_error (bfd_error_invalid_operation);
1877       return -1;
1878     }
1879   return (asect->reloc_count + 1) * sizeof (arelent *);
1880 }
1881
1882 asymbol *
1883 coff_make_empty_symbol (abfd)
1884      bfd *abfd;
1885 {
1886   bfd_size_type amt = sizeof (coff_symbol_type);
1887   coff_symbol_type *new = (coff_symbol_type *) bfd_alloc (abfd, amt);
1888   if (new == NULL)
1889     return (NULL);
1890   memset (new, 0, sizeof *new);
1891   new->symbol.section = 0;
1892   new->native = 0;
1893   new->lineno = (alent *) NULL;
1894   new->done_lineno = false;
1895   new->symbol.the_bfd = abfd;
1896   return &new->symbol;
1897 }
1898
1899 /* Make a debugging symbol.  */
1900
1901 asymbol *
1902 coff_bfd_make_debug_symbol (abfd, ptr, sz)
1903      bfd *abfd;
1904      PTR ptr ATTRIBUTE_UNUSED;
1905      unsigned long sz ATTRIBUTE_UNUSED;
1906 {
1907   bfd_size_type amt = sizeof (coff_symbol_type);
1908   coff_symbol_type *new = (coff_symbol_type *) bfd_alloc (abfd, amt);
1909   if (new == NULL)
1910     return (NULL);
1911   /* @@ The 10 is a guess at a plausible maximum number of aux entries
1912      (but shouldn't be a constant).  */
1913   amt = sizeof (combined_entry_type) * 10;
1914   new->native = (combined_entry_type *) bfd_zalloc (abfd, amt);
1915   if (!new->native)
1916     return (NULL);
1917   new->symbol.section = bfd_abs_section_ptr;
1918   new->symbol.flags = BSF_DEBUGGING;
1919   new->lineno = (alent *) NULL;
1920   new->done_lineno = false;
1921   new->symbol.the_bfd = abfd;
1922   return &new->symbol;
1923 }
1924
1925 void
1926 coff_get_symbol_info (abfd, symbol, ret)
1927      bfd *abfd;
1928      asymbol *symbol;
1929      symbol_info *ret;
1930 {
1931   bfd_symbol_info (symbol, ret);
1932   if (coffsymbol (symbol)->native != NULL
1933       && coffsymbol (symbol)->native->fix_value)
1934     {
1935       ret->value = coffsymbol (symbol)->native->u.syment.n_value -
1936         (unsigned long) obj_raw_syments (abfd);
1937     }
1938 }
1939
1940 /* Return the COFF syment for a symbol.  */
1941
1942 boolean
1943 bfd_coff_get_syment (abfd, symbol, psyment)
1944      bfd *abfd;
1945      asymbol *symbol;
1946      struct internal_syment *psyment;
1947 {
1948   coff_symbol_type *csym;
1949
1950   csym = coff_symbol_from (abfd, symbol);
1951   if (csym == NULL || csym->native == NULL)
1952     {
1953       bfd_set_error (bfd_error_invalid_operation);
1954       return false;
1955     }
1956
1957   *psyment = csym->native->u.syment;
1958
1959   if (csym->native->fix_value)
1960     psyment->n_value = psyment->n_value -
1961       (unsigned long) obj_raw_syments (abfd);
1962
1963   /* FIXME: We should handle fix_line here.  */
1964
1965   return true;
1966 }
1967
1968 /* Return the COFF auxent for a symbol.  */
1969
1970 boolean
1971 bfd_coff_get_auxent (abfd, symbol, indx, pauxent)
1972      bfd *abfd;
1973      asymbol *symbol;
1974      int indx;
1975      union internal_auxent *pauxent;
1976 {
1977   coff_symbol_type *csym;
1978   combined_entry_type *ent;
1979
1980   csym = coff_symbol_from (abfd, symbol);
1981
1982   if (csym == NULL
1983       || csym->native == NULL
1984       || indx >= csym->native->u.syment.n_numaux)
1985     {
1986       bfd_set_error (bfd_error_invalid_operation);
1987       return false;
1988     }
1989
1990   ent = csym->native + indx + 1;
1991
1992   *pauxent = ent->u.auxent;
1993
1994   if (ent->fix_tag)
1995     pauxent->x_sym.x_tagndx.l =
1996       ((combined_entry_type *) pauxent->x_sym.x_tagndx.p
1997        - obj_raw_syments (abfd));
1998
1999   if (ent->fix_end)
2000     pauxent->x_sym.x_fcnary.x_fcn.x_endndx.l =
2001       ((combined_entry_type *) pauxent->x_sym.x_fcnary.x_fcn.x_endndx.p
2002        - obj_raw_syments (abfd));
2003
2004   if (ent->fix_scnlen)
2005     pauxent->x_csect.x_scnlen.l =
2006       ((combined_entry_type *) pauxent->x_csect.x_scnlen.p
2007        - obj_raw_syments (abfd));
2008
2009   return true;
2010 }
2011
2012 /* Print out information about COFF symbol.  */
2013
2014 void
2015 coff_print_symbol (abfd, filep, symbol, how)
2016      bfd *abfd;
2017      PTR filep;
2018      asymbol *symbol;
2019      bfd_print_symbol_type how;
2020 {
2021   FILE *file = (FILE *) filep;
2022
2023   switch (how)
2024     {
2025     case bfd_print_symbol_name:
2026       fprintf (file, "%s", symbol->name);
2027       break;
2028
2029     case bfd_print_symbol_more:
2030       fprintf (file, "coff %s %s",
2031                coffsymbol (symbol)->native ? "n" : "g",
2032                coffsymbol (symbol)->lineno ? "l" : " ");
2033       break;
2034
2035     case bfd_print_symbol_all:
2036       if (coffsymbol (symbol)->native)
2037         {
2038           bfd_vma val;
2039           unsigned int aux;
2040           combined_entry_type *combined = coffsymbol (symbol)->native;
2041           combined_entry_type *root = obj_raw_syments (abfd);
2042           struct lineno_cache_entry *l = coffsymbol (symbol)->lineno;
2043
2044           fprintf (file, "[%3ld]", (long) (combined - root));
2045
2046           if (! combined->fix_value)
2047             val = (bfd_vma) combined->u.syment.n_value;
2048           else
2049             val = combined->u.syment.n_value - (unsigned long) root;
2050
2051 #ifndef XCOFF64
2052           fprintf (file,
2053                    "(sec %2d)(fl 0x%02x)(ty %3x)(scl %3d) (nx %d) 0x%08lx %s",
2054                    combined->u.syment.n_scnum,
2055                    combined->u.syment.n_flags,
2056                    combined->u.syment.n_type,
2057                    combined->u.syment.n_sclass,
2058                    combined->u.syment.n_numaux,
2059                    (unsigned long) val,
2060                    symbol->name);
2061 #else
2062           /* Print out the wide, 64 bit, symbol value */
2063           fprintf (file,
2064                    "(sec %2d)(fl 0x%02x)(ty %3x)(scl %3d) (nx %d) 0x%016llx %s",
2065                    combined->u.syment.n_scnum,
2066                    combined->u.syment.n_flags,
2067                    combined->u.syment.n_type,
2068                    combined->u.syment.n_sclass,
2069                    combined->u.syment.n_numaux,
2070                    val,
2071                    symbol->name);
2072 #endif
2073
2074           for (aux = 0; aux < combined->u.syment.n_numaux; aux++)
2075             {
2076               combined_entry_type *auxp = combined + aux + 1;
2077               long tagndx;
2078
2079               if (auxp->fix_tag)
2080                 tagndx = auxp->u.auxent.x_sym.x_tagndx.p - root;
2081               else
2082                 tagndx = auxp->u.auxent.x_sym.x_tagndx.l;
2083
2084               fprintf (file, "\n");
2085
2086               if (bfd_coff_print_aux (abfd, file, root, combined, auxp, aux))
2087                 continue;
2088
2089               switch (combined->u.syment.n_sclass)
2090                 {
2091                 case C_FILE:
2092                   fprintf (file, "File ");
2093                   break;
2094
2095                 case C_STAT:
2096                   if (combined->u.syment.n_type == T_NULL)
2097                     /* probably a section symbol? */
2098                     {
2099                       fprintf (file, "AUX scnlen 0x%lx nreloc %d nlnno %d",
2100                                (long) auxp->u.auxent.x_scn.x_scnlen,
2101                                auxp->u.auxent.x_scn.x_nreloc,
2102                                auxp->u.auxent.x_scn.x_nlinno);
2103                       if (auxp->u.auxent.x_scn.x_checksum != 0
2104                           || auxp->u.auxent.x_scn.x_associated != 0
2105                           || auxp->u.auxent.x_scn.x_comdat != 0)
2106                         fprintf (file, " checksum 0x%lx assoc %d comdat %d",
2107                                  auxp->u.auxent.x_scn.x_checksum,
2108                                  auxp->u.auxent.x_scn.x_associated,
2109                                  auxp->u.auxent.x_scn.x_comdat);
2110                       break;
2111                     }
2112                     /* else fall through */
2113                 case C_EXT:
2114                   if (ISFCN (combined->u.syment.n_type))
2115                     {
2116                       long next, llnos;
2117
2118                       if (auxp->fix_end)
2119                         next = (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p
2120                                - root);
2121                       else
2122                         next = auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l;
2123                       llnos = auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_lnnoptr;
2124                       fprintf (file,
2125                                _("AUX tagndx %ld ttlsiz 0x%lx lnnos %ld next %ld"),
2126                                tagndx, auxp->u.auxent.x_sym.x_misc.x_fsize,
2127                                llnos, next);
2128                       break;
2129                     }
2130                   /* else fall through */
2131                 default:
2132                   fprintf (file, "AUX lnno %d size 0x%x tagndx %ld",
2133                            auxp->u.auxent.x_sym.x_misc.x_lnsz.x_lnno,
2134                            auxp->u.auxent.x_sym.x_misc.x_lnsz.x_size,
2135                            tagndx);
2136                   if (auxp->fix_end)
2137                     fprintf (file, " endndx %ld",
2138                              ((long)
2139                               (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p
2140                                - root)));
2141                   break;
2142                 }
2143             }
2144
2145           if (l)
2146             {
2147               fprintf (file, "\n%s :", l->u.sym->name);
2148               l++;
2149               while (l->line_number)
2150                 {
2151                   fprintf (file, "\n%4d : 0x%lx",
2152                            l->line_number,
2153                            ((unsigned long)
2154                             (l->u.offset + symbol->section->vma)));
2155                   l++;
2156                 }
2157             }
2158         }
2159       else
2160         {
2161           bfd_print_symbol_vandf (abfd, (PTR) file, symbol);
2162           fprintf (file, " %-5s %s %s %s",
2163                    symbol->section->name,
2164                    coffsymbol (symbol)->native ? "n" : "g",
2165                    coffsymbol (symbol)->lineno ? "l" : " ",
2166                    symbol->name);
2167         }
2168     }
2169 }
2170
2171 /* Return whether a symbol name implies a local symbol.  In COFF,
2172    local symbols generally start with ``.L''.  Most targets use this
2173    function for the is_local_label_name entry point, but some may
2174    override it.  */
2175
2176 boolean
2177 _bfd_coff_is_local_label_name (abfd, name)
2178      bfd *abfd ATTRIBUTE_UNUSED;
2179      const char *name;
2180 {
2181   return name[0] == '.' && name[1] == 'L';
2182 }
2183
2184 /* Provided a BFD, a section and an offset (in bytes, not octets) into the
2185    section, calculate and return the name of the source file and the line
2186    nearest to the wanted location.  */
2187
2188 boolean
2189 coff_find_nearest_line (abfd, section, symbols, offset, filename_ptr,
2190                         functionname_ptr, line_ptr)
2191      bfd *abfd;
2192      asection *section;
2193      asymbol **symbols;
2194      bfd_vma offset;
2195      const char **filename_ptr;
2196      const char **functionname_ptr;
2197      unsigned int *line_ptr;
2198 {
2199   boolean found;
2200   unsigned int i;
2201   unsigned int line_base;
2202   coff_data_type *cof = coff_data (abfd);
2203   /* Run through the raw syments if available */
2204   combined_entry_type *p;
2205   combined_entry_type *pend;
2206   alent *l;
2207   struct coff_section_tdata *sec_data;
2208   bfd_size_type amt;
2209
2210   /* Before looking through the symbol table, try to use a .stab
2211      section to find the information.  */
2212   if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
2213                                              &found, filename_ptr,
2214                                              functionname_ptr, line_ptr,
2215                                              &coff_data(abfd)->line_info))
2216     return false;
2217
2218   if (found)
2219     return true;
2220
2221   /* Also try examining DWARF2 debugging information.  */
2222   if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
2223                                      filename_ptr, functionname_ptr,
2224                                      line_ptr, 0,
2225                                      &coff_data(abfd)->dwarf2_find_line_info))
2226     return true;
2227
2228   *filename_ptr = 0;
2229   *functionname_ptr = 0;
2230   *line_ptr = 0;
2231
2232   /* Don't try and find line numbers in a non coff file */
2233   if (!bfd_family_coff (abfd))
2234     return false;
2235
2236   if (cof == NULL)
2237     return false;
2238
2239   /* Find the first C_FILE symbol.  */
2240   p = cof->raw_syments;
2241   if (!p)
2242     return false;
2243
2244   pend = p + cof->raw_syment_count;
2245   while (p < pend)
2246     {
2247       if (p->u.syment.n_sclass == C_FILE)
2248         break;
2249       p += 1 + p->u.syment.n_numaux;
2250     }
2251
2252   if (p < pend)
2253     {
2254       bfd_vma sec_vma;
2255       bfd_vma maxdiff;
2256
2257       /* Look through the C_FILE symbols to find the best one.  */
2258       sec_vma = bfd_get_section_vma (abfd, section);
2259       *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
2260       maxdiff = (bfd_vma) 0 - (bfd_vma) 1;
2261       while (1)
2262         {
2263           combined_entry_type *p2;
2264
2265           for (p2 = p + 1 + p->u.syment.n_numaux;
2266                p2 < pend;
2267                p2 += 1 + p2->u.syment.n_numaux)
2268             {
2269               if (p2->u.syment.n_scnum > 0
2270                   && (section
2271                       == coff_section_from_bfd_index (abfd,
2272                                                       p2->u.syment.n_scnum)))
2273                 break;
2274               if (p2->u.syment.n_sclass == C_FILE)
2275                 {
2276                   p2 = pend;
2277                   break;
2278                 }
2279             }
2280
2281           /* We use <= MAXDIFF here so that if we get a zero length
2282              file, we actually use the next file entry.  */
2283           if (p2 < pend
2284               && offset + sec_vma >= (bfd_vma) p2->u.syment.n_value
2285               && offset + sec_vma - (bfd_vma) p2->u.syment.n_value <= maxdiff)
2286             {
2287               *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
2288               maxdiff = offset + sec_vma - p2->u.syment.n_value;
2289             }
2290
2291           /* Avoid endless loops on erroneous files by ensuring that
2292              we always move forward in the file.  */
2293           if (p >= cof->raw_syments + p->u.syment.n_value)
2294             break;
2295
2296           p = cof->raw_syments + p->u.syment.n_value;
2297           if (p > pend || p->u.syment.n_sclass != C_FILE)
2298             break;
2299         }
2300     }
2301
2302   /* Now wander though the raw linenumbers of the section */
2303   /* If we have been called on this section before, and the offset we
2304      want is further down then we can prime the lookup loop.  */
2305   sec_data = coff_section_data (abfd, section);
2306   if (sec_data != NULL
2307       && sec_data->i > 0
2308       && offset >= sec_data->offset)
2309     {
2310       i = sec_data->i;
2311       *functionname_ptr = sec_data->function;
2312       line_base = sec_data->line_base;
2313     }
2314   else
2315     {
2316       i = 0;
2317       line_base = 0;
2318     }
2319
2320   if (section->lineno != NULL)
2321     {
2322       bfd_vma last_value = 0;
2323
2324       l = &section->lineno[i];
2325
2326       for (; i < section->lineno_count; i++)
2327         {
2328           if (l->line_number == 0)
2329             {
2330               /* Get the symbol this line number points at */
2331               coff_symbol_type *coff = (coff_symbol_type *) (l->u.sym);
2332               if (coff->symbol.value > offset)
2333                 break;
2334               *functionname_ptr = coff->symbol.name;
2335               last_value = coff->symbol.value;
2336               if (coff->native)
2337                 {
2338                   combined_entry_type *s = coff->native;
2339                   s = s + 1 + s->u.syment.n_numaux;
2340
2341                   /* In XCOFF a debugging symbol can follow the
2342                      function symbol.  */
2343                   if (s->u.syment.n_scnum == N_DEBUG)
2344                     s = s + 1 + s->u.syment.n_numaux;
2345
2346                   /* S should now point to the .bf of the function.  */
2347                   if (s->u.syment.n_numaux)
2348                     {
2349                       /* The linenumber is stored in the auxent.  */
2350                       union internal_auxent *a = &((s + 1)->u.auxent);
2351                       line_base = a->x_sym.x_misc.x_lnsz.x_lnno;
2352                       *line_ptr = line_base;
2353                     }
2354                 }
2355             }
2356           else
2357             {
2358               if (l->u.offset > offset)
2359                 break;
2360               *line_ptr = l->line_number + line_base - 1;
2361             }
2362           l++;
2363         }
2364
2365       /* If we fell off the end of the loop, then assume that this
2366          symbol has no line number info.  Otherwise, symbols with no
2367          line number info get reported with the line number of the
2368          last line of the last symbol which does have line number
2369          info.  We use 0x100 as a slop to account for cases where the
2370          last line has executable code.  */
2371       if (i >= section->lineno_count
2372           && last_value != 0
2373           && offset - last_value > 0x100)
2374         {
2375           *functionname_ptr = NULL;
2376           *line_ptr = 0;
2377         }
2378     }
2379
2380   /* Cache the results for the next call.  */
2381   if (sec_data == NULL && section->owner == abfd)
2382     {
2383       amt = sizeof (struct coff_section_tdata);
2384       section->used_by_bfd = (PTR) bfd_zalloc (abfd, amt);
2385       sec_data = (struct coff_section_tdata *) section->used_by_bfd;
2386     }
2387   if (sec_data != NULL)
2388     {
2389       sec_data->offset = offset;
2390       sec_data->i = i;
2391       sec_data->function = *functionname_ptr;
2392       sec_data->line_base = line_base;
2393     }
2394
2395   return true;
2396 }
2397
2398 int
2399 coff_sizeof_headers (abfd, reloc)
2400      bfd *abfd;
2401      boolean reloc;
2402 {
2403   size_t size;
2404
2405   if (reloc == false)
2406     {
2407       size = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
2408     }
2409   else
2410     {
2411       size = bfd_coff_filhsz (abfd);
2412     }
2413
2414   size += abfd->section_count * bfd_coff_scnhsz (abfd);
2415   return size;
2416 }
2417
2418 /* Change the class of a coff symbol held by BFD.  */
2419 boolean
2420 bfd_coff_set_symbol_class (abfd, symbol, class)
2421      bfd *         abfd;
2422      asymbol *     symbol;
2423      unsigned int  class;
2424 {
2425   coff_symbol_type * csym;
2426
2427   csym = coff_symbol_from (abfd, symbol);
2428   if (csym == NULL)
2429     {
2430       bfd_set_error (bfd_error_invalid_operation);
2431       return false;
2432     }
2433   else if (csym->native == NULL)
2434     {
2435       /* This is an alien symbol which no native coff backend data.
2436          We cheat here by creating a fake native entry for it and
2437          then filling in the class.  This code is based on that in
2438          coff_write_alien_symbol().  */
2439
2440       combined_entry_type * native;
2441       bfd_size_type amt = sizeof (* native);
2442
2443       native = (combined_entry_type *) bfd_alloc (abfd, amt);
2444       if (native == NULL)
2445         return false;
2446
2447       memset (native, 0, sizeof (* native));
2448
2449       native->u.syment.n_type   = T_NULL;
2450       native->u.syment.n_sclass = class;
2451
2452       if (bfd_is_und_section (symbol->section))
2453         {
2454           native->u.syment.n_scnum = N_UNDEF;
2455           native->u.syment.n_value = symbol->value;
2456         }
2457       else if (bfd_is_com_section (symbol->section))
2458         {
2459           native->u.syment.n_scnum = N_UNDEF;
2460           native->u.syment.n_value = symbol->value;
2461         }
2462       else
2463         {
2464           native->u.syment.n_scnum =
2465             symbol->section->output_section->target_index;
2466           native->u.syment.n_value = (symbol->value
2467                                       + symbol->section->output_offset);
2468           if (! obj_pe (abfd))
2469             native->u.syment.n_value += symbol->section->output_section->vma;
2470
2471           /* Copy the any flags from the file header into the symbol.
2472              FIXME: Why?  */
2473           native->u.syment.n_flags = bfd_asymbol_bfd (& csym->symbol)->flags;
2474         }
2475
2476       csym->native = native;
2477     }
2478   else
2479     {
2480       csym->native->u.syment.n_sclass = class;
2481     }
2482
2483   return true;
2484 }