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