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