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