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