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