These changes cut the size of libbfd.a on a Sun4 by about 11%.
[external/binutils.git] / bfd / coffgen.c
1 /* Support for the generic parts of COFF, for BFD.
2    Copyright 1990, 1991, 1992 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., 675 Mass Ave, Cambridge, MA 02139, 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 asection bfd_debug_section = { "*DEBUG*" };
45
46 /* Take a section header read from a coff file (in HOST byte order),
47    and make a BFD "section" out of it.  This is used by ECOFF.  */
48 static          boolean
49 DEFUN(make_a_section_from_file,(abfd, hdr, target_index),
50       bfd            *abfd AND
51       struct internal_scnhdr  *hdr AND
52       unsigned int target_index)
53 {
54   asection       *return_section;
55   char *name;
56     
57   /* Assorted wastage to null-terminate the name, thanks AT&T! */
58   name = bfd_alloc(abfd, sizeof (hdr->s_name)+1);
59   if (name == NULL) {
60       bfd_error = no_memory;
61       return false;
62     }
63   strncpy(name, (char *) &hdr->s_name[0], sizeof (hdr->s_name));
64   name[sizeof (hdr->s_name)] = 0;
65
66   return_section = bfd_make_section(abfd, name);
67   if (return_section == NULL)
68     return_section = bfd_coff_make_section_hook (abfd, name);
69   if (return_section == NULL)
70     return false;
71
72   /* s_paddr is presumed to be = to s_vaddr */
73
74   return_section->vma = hdr->s_vaddr;
75   return_section->_raw_size = hdr->s_size;
76   return_section->filepos = hdr->s_scnptr;
77   return_section->rel_filepos =  hdr->s_relptr;
78   return_section->reloc_count = hdr->s_nreloc;
79
80   bfd_coff_set_alignment_hook (abfd, return_section, hdr);
81
82   return_section->line_filepos =  hdr->s_lnnoptr;
83
84   return_section->lineno_count = hdr->s_nlnno;
85   return_section->userdata = NULL;
86   return_section->next = (asection *) NULL;
87   return_section->flags = bfd_coff_styp_to_sec_flags_hook (abfd, hdr);
88
89   return_section->target_index = target_index;
90
91   if (hdr->s_nreloc != 0)
92     return_section->flags |= SEC_RELOC;
93   /* FIXME: should this check 'hdr->s_size > 0' */
94   if (hdr->s_scnptr != 0)
95     return_section->flags |= SEC_HAS_CONTENTS;
96   return true;
97 }
98
99 /* Read in a COFF object and make it into a BFD.  This is used by
100    ECOFF as well.  */
101
102 static
103 bfd_target     *
104 DEFUN(coff_real_object_p,(abfd, nscns, internal_f, internal_a),
105     bfd            *abfd AND
106     unsigned        nscns AND
107   struct internal_filehdr *internal_f AND
108   struct internal_aouthdr *internal_a)
109 {
110   PTR tdata;
111   size_t          readsize;     /* length of file_info */
112   unsigned int scnhsz;
113   char *external_sections;
114
115   /* Build a play area */
116   tdata = bfd_coff_mkobject_hook (abfd, (PTR) internal_f);
117   if (tdata == NULL)
118     return 0;
119
120   scnhsz = bfd_coff_scnhsz (abfd);
121   readsize = nscns * scnhsz;
122   external_sections = (char *)bfd_alloc(abfd, readsize);
123
124   if (bfd_read((PTR)external_sections, 1, readsize, abfd) != readsize) {
125     goto fail;
126   }
127
128   /* Now copy data as required; construct all asections etc */
129   if (nscns != 0) {
130     unsigned int    i;
131     for (i = 0; i < nscns; i++) {
132       struct internal_scnhdr tmp;
133       bfd_coff_swap_scnhdr_in(abfd, (PTR) (external_sections + i * scnhsz),
134                               (PTR) &tmp);
135       make_a_section_from_file(abfd,&tmp, i+1);
136     }
137   }
138
139 /*  make_abs_section(abfd);*/
140   
141   if (bfd_coff_set_arch_mach_hook (abfd, (PTR) internal_f) == false)
142     goto fail;
143
144   if (!(internal_f->f_flags & F_RELFLG))
145     abfd->flags |= HAS_RELOC;
146   if ((internal_f->f_flags & F_EXEC))
147     abfd->flags |= EXEC_P;
148   if (!(internal_f->f_flags & F_LNNO))
149     abfd->flags |= HAS_LINENO;
150   if (!(internal_f->f_flags & F_LSYMS))
151     abfd->flags |= HAS_LOCALS;
152
153
154   bfd_get_symcount(abfd) = internal_f->f_nsyms;
155   if (internal_f->f_nsyms)
156     abfd->flags |= HAS_SYMS;
157
158   bfd_get_start_address(abfd) = internal_f->f_opthdr ? internal_a->entry : 0;
159
160   return abfd->xvec;
161  fail:
162   bfd_release(abfd, tdata);
163   return (bfd_target *)NULL;
164 }
165
166 /* Turn a COFF file into a BFD, but fail with wrong_format if it is
167    not a COFF file.  This is also used by ECOFF.  */
168
169 bfd_target *
170 DEFUN(coff_object_p,(abfd),
171       bfd            *abfd)
172 {
173   unsigned int filhsz;
174   unsigned int aoutsz;
175   int   nscns;
176   PTR filehdr;
177   struct internal_filehdr internal_f;
178   struct internal_aouthdr internal_a;
179
180   bfd_error = system_call_error;
181
182   /* figure out how much to read */
183   filhsz = bfd_coff_filhsz (abfd);
184   aoutsz = bfd_coff_aoutsz (abfd);
185
186   filehdr = bfd_alloc (abfd, filhsz);
187   if (filehdr == NULL)
188     return 0;
189   if (bfd_read(filehdr, 1, filhsz, abfd) != filhsz)
190     return 0;
191   bfd_coff_swap_filehdr_in(abfd, filehdr, &internal_f);
192   bfd_release (abfd, filehdr);
193
194   if (bfd_coff_bad_format_hook (abfd, &internal_f) == false) {
195     bfd_error = wrong_format;
196     return 0;
197   }
198   nscns =internal_f.f_nscns;
199
200   if (internal_f.f_opthdr) {
201     PTR opthdr;
202
203     opthdr = bfd_alloc (abfd, aoutsz);
204     if (opthdr == NULL)
205       return 0;;
206     if (bfd_read(opthdr, 1,aoutsz, abfd) != aoutsz) {
207       return 0;
208     }
209     bfd_coff_swap_aouthdr_in(abfd, opthdr, (PTR)&internal_a);
210   }
211
212   /* Seek past the opt hdr stuff */
213   bfd_seek(abfd, (file_ptr) (internal_f.f_opthdr + filhsz), SEEK_SET);
214
215   return coff_real_object_p(abfd, nscns, &internal_f, &internal_a);
216 }
217
218 /* Get the BFD section from a COFF symbol section number.  */
219
220 struct sec *
221 DEFUN(coff_section_from_bfd_index,(abfd, index),
222       bfd            *abfd AND
223       int             index)
224 {
225   struct sec *answer = abfd->sections;
226
227   if (index == N_ABS) 
228   {
229     return &bfd_abs_section;
230   }
231   if (index == N_UNDEF)
232   {
233     return &bfd_und_section;
234   }
235   if(index == N_DEBUG)
236   {
237     return &bfd_debug_section;
238     
239   }
240   
241   while (answer) {
242       if (answer->target_index == index)
243        return answer;
244       answer = answer->next;
245     }
246   BFD_ASSERT(0);
247   return &bfd_und_section;      /* For gcc -W and lint.  Never executed. */
248 }
249
250 /* Get the upper bound of a COFF symbol table.  */
251
252 unsigned int
253 coff_get_symtab_upper_bound(abfd)
254 bfd            *abfd;
255 {
256   if (!bfd_coff_slurp_symbol_table(abfd))
257     return 0;
258
259   return (bfd_get_symcount(abfd) + 1) * (sizeof(coff_symbol_type *));
260 }
261
262
263 /* Canonicalize a COFF symbol table.  */
264
265 unsigned int
266 DEFUN(coff_get_symtab, (abfd, alocation),
267       bfd            *abfd AND
268       asymbol       **alocation)
269 {
270     unsigned int    counter = 0;
271     coff_symbol_type *symbase;
272     coff_symbol_type **location = (coff_symbol_type **) (alocation);
273     if (!bfd_coff_slurp_symbol_table(abfd))
274      return 0;
275
276     symbase = obj_symbols(abfd);
277     while (counter <  bfd_get_symcount(abfd))
278     {
279         /* This nasty code looks at the symbol to decide whether or
280            not it is descibes a constructor/destructor entry point. It
281            is structured this way to (hopefully) speed non matches */
282 #if 0   
283         if (0 && symbase->symbol.name[9] == '$') 
284         {
285             bfd_constructor_entry(abfd, 
286                                  (asymbol **)location,
287                                   symbase->symbol.name[10] == 'I' ?
288                                   "CTOR" : "DTOR");
289         }
290 #endif
291         *(location++) = symbase++;
292         counter++;
293     }
294     *location++ = 0;
295     return bfd_get_symcount(abfd);
296 }
297
298 /* Set lineno_count for the output sections of a COFF file.  */
299
300 void
301 DEFUN(coff_count_linenumbers,(abfd),
302       bfd            *abfd)
303 {
304   unsigned int    limit = bfd_get_symcount(abfd);
305   unsigned int    i;
306   asymbol       **p;
307     {
308       asection       *s = abfd->sections->output_section;
309       while (s) {
310         BFD_ASSERT(s->lineno_count == 0);
311         s = s->next;
312       }
313     }
314
315
316   for (p = abfd->outsymbols, i = 0; i < limit; i++, p++) {
317     asymbol        *q_maybe = *p;
318     if (bfd_asymbol_flavour(q_maybe) == bfd_target_coff_flavour) {
319       coff_symbol_type *q = coffsymbol(q_maybe);
320       if (q->lineno) {
321         /*
322           This symbol has a linenumber, increment the owning
323           section's linenumber count
324           */
325         alent          *l = q->lineno;
326         q->symbol.section->output_section->lineno_count++;
327         l++;
328         while (l->line_number) {
329           q->symbol.section->output_section->lineno_count++;
330           l++;
331         }
332       }
333     }
334   }
335 }
336
337 /* Takes a bfd and a symbol, returns a pointer to the coff specific
338    area of the symbol if there is one.  */
339
340 coff_symbol_type *
341 DEFUN(coff_symbol_from,(ignore_abfd, symbol),
342       bfd            *ignore_abfd AND
343       asymbol        *symbol)
344 {
345   if (bfd_asymbol_flavour(symbol) != bfd_target_coff_flavour)
346     return (coff_symbol_type *)NULL;
347
348   if (bfd_asymbol_bfd(symbol)->tdata.coff_obj_data == (coff_data_type*)NULL)
349     return (coff_symbol_type *)NULL;
350
351   return  (coff_symbol_type *) symbol;
352 }
353
354 static void
355 DEFUN(fixup_symbol_value,(coff_symbol_ptr, syment),
356 coff_symbol_type *coff_symbol_ptr AND
357 struct internal_syment *syment)
358 {
359
360   /* Normalize the symbol flags */
361   if (coff_symbol_ptr->symbol.section == &bfd_com_section) {
362     /* a common symbol is undefined with a value */
363     syment->n_scnum = N_UNDEF;
364     syment->n_value = coff_symbol_ptr->symbol.value;
365   }
366   else if (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING) {
367     syment->n_value = coff_symbol_ptr->symbol.value;
368   }
369   else if (coff_symbol_ptr->symbol.section == & bfd_und_section) {
370     syment->n_scnum = N_UNDEF;
371     syment->n_value = 0;
372   }
373   else {
374     if (coff_symbol_ptr->symbol.section) {
375       syment->n_scnum    =
376        coff_symbol_ptr->symbol.section->output_section->target_index;
377
378       syment->n_value =
379        coff_symbol_ptr->symbol.value +
380         coff_symbol_ptr->symbol.section->output_offset +
381          coff_symbol_ptr->symbol.section->output_section->vma;
382     }
383     else {
384         BFD_ASSERT(0);
385       /* This can happen, but I don't know why yet (steve@cygnus.com) */
386       syment->n_scnum = N_ABS;
387       syment->n_value = coff_symbol_ptr->symbol.value;
388     }
389   }
390 }
391
392 /* run through all the symbols in the symbol table and work out what
393    their indexes into the symbol table will be when output
394
395  Coff requires that each C_FILE symbol points to the next one in the
396  chain, and that the last one points to the first external symbol. We
397  do that here too.
398
399 */
400 void
401 DEFUN(coff_renumber_symbols,(bfd_ptr),
402       bfd *bfd_ptr)
403 {
404   unsigned int symbol_count = bfd_get_symcount(bfd_ptr);
405   asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
406   unsigned int native_index = 0;
407   struct internal_syment *last_file = (struct internal_syment *)NULL;
408   unsigned int symbol_index;
409
410   /* COFF demands that undefined symbols come after all other symbols.
411      Since we don't need to impose this extra knowledge on all our client
412      programs, deal with that here.  Sort the symbol table; just move the
413      undefined symbols to the end, leaving the rest alone.  */
414   /* @@ Do we have some condition we could test for, so we don't always
415      have to do this?  I don't think relocatability is quite right, but
416      I'm not certain.  [raeburn:19920508.1711EST]  */
417   {
418     asymbol **newsyms;
419     int i;
420
421     newsyms = (asymbol **) bfd_alloc_by_size_t (bfd_ptr,
422                                                 sizeof (asymbol *)
423                                                 * (symbol_count + 1));
424     bfd_ptr->outsymbols = newsyms;
425     for (i = 0; i < symbol_count; i++)
426       if (symbol_ptr_ptr[i]->section != &bfd_und_section)
427         *newsyms++ = symbol_ptr_ptr[i];
428     for (i = 0; i < symbol_count; i++)
429       if (symbol_ptr_ptr[i]->section == &bfd_und_section)
430         *newsyms++ = symbol_ptr_ptr[i];
431     *newsyms = (asymbol *) NULL;
432     symbol_ptr_ptr = bfd_ptr->outsymbols;
433   }
434
435   for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
436       {
437         coff_symbol_type *coff_symbol_ptr = coff_symbol_from(bfd_ptr, symbol_ptr_ptr[symbol_index]);
438         if (coff_symbol_ptr && coff_symbol_ptr->native) {
439           combined_entry_type *s = coff_symbol_ptr->native;
440           int i;
441
442           if (s->u.syment.n_sclass == C_FILE)
443               {
444                 if (last_file != (struct internal_syment *)NULL) {
445                   last_file->n_value = native_index;
446                 }
447                 last_file = &(s->u.syment);
448               }
449           else {
450
451             /* Modify the symbol values according to their section and
452                type */
453
454             fixup_symbol_value(coff_symbol_ptr, &(s->u.syment));
455           }
456           for (i = 0; i < s->u.syment.n_numaux + 1; i++) {
457             s[i].offset = native_index ++;
458           }
459         }
460         else {
461           native_index++;
462         }
463       }
464   obj_conv_table_size (bfd_ptr) = native_index;
465 }
466
467 /*
468  Run thorough the symbol table again, and fix it so that all pointers to
469  entries are changed to the entries' index in the output symbol table.
470
471 */
472 void
473 DEFUN(coff_mangle_symbols,(bfd_ptr),
474       bfd *bfd_ptr)
475 {
476   unsigned int symbol_count = bfd_get_symcount(bfd_ptr);
477   asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
478   unsigned int symbol_index;
479
480   for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
481       {
482         coff_symbol_type *coff_symbol_ptr =
483           coff_symbol_from(bfd_ptr, symbol_ptr_ptr[symbol_index]);
484
485         if (coff_symbol_ptr && coff_symbol_ptr->native) {
486           int i;
487           combined_entry_type *s = coff_symbol_ptr->native;
488
489           for (i = 0; i < s->u.syment.n_numaux ; i++) {
490             combined_entry_type *a = s + i + 1;
491             if (a->fix_tag) {
492               a->u.auxent.x_sym.x_tagndx.l =
493                 a->u.auxent.x_sym.x_tagndx.p->offset;
494               a->fix_tag = 0;
495             }
496             if (a->fix_end) {
497               a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l =
498                 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p->offset;
499               a->fix_end = 0;
500               
501             }
502
503           }
504         }
505       }
506 }
507
508 static int string_size;
509
510 static void
511 DEFUN(coff_fix_symbol_name,(abfd, symbol, native),
512   bfd *abfd AND
513   asymbol *symbol AND
514   combined_entry_type *native)
515 {
516   unsigned int    name_length;
517   union internal_auxent *auxent;
518   char *  name = ( char *)(symbol->name);
519
520   if (name == (char *) NULL) {
521     /* coff symbols always have names, so we'll make one up */
522     symbol->name = "strange";
523     name = (char *)symbol->name;
524   }
525   name_length = strlen(name);
526
527   if (native->u.syment.n_sclass == C_FILE) {
528     strncpy(native->u.syment._n._n_name, ".file", SYMNMLEN);
529     auxent = &(native+1)->u.auxent;
530
531     if (bfd_coff_long_filenames (abfd)) {
532       if (name_length <= FILNMLEN) {
533         strncpy(auxent->x_file.x_fname, name, FILNMLEN);
534       }
535       else {
536         auxent->x_file.x_n.x_offset = string_size + 4;
537         auxent->x_file.x_n.x_zeroes = 0;
538         string_size += name_length + 1;
539       }
540     }
541     else {
542       strncpy(auxent->x_file.x_fname, name, FILNMLEN);
543       if (name_length > FILNMLEN) {
544         name[FILNMLEN] = '\0';
545       }
546     }
547   }
548   else
549       {                         /* NOT A C_FILE SYMBOL */
550         if (name_length <= SYMNMLEN) {
551           /* This name will fit into the symbol neatly */
552           strncpy(native->u.syment._n._n_name, symbol->name, SYMNMLEN);
553         }
554         else {
555           native->u.syment._n._n_n._n_offset =  string_size + 4;
556           native->u.syment._n._n_n._n_zeroes = 0;
557           string_size += name_length + 1;
558         }
559       }
560 }
561
562 #define set_index(symbol, idx)  ((symbol)->udata =(PTR) (idx))
563
564 static unsigned int
565 DEFUN(coff_write_symbol,(abfd, symbol, native, written),
566 bfd *abfd AND
567 asymbol *symbol AND
568 combined_entry_type *native AND
569 unsigned int written)
570 {
571   unsigned int    numaux = native->u.syment.n_numaux;
572   int             type = native->u.syment.n_type;
573   int             class =  native->u.syment.n_sclass;
574   PTR buf;
575   bfd_size_type symesz;
576
577   /* @@ bfd_debug_section isn't accessible outside this file, but we know
578      that C_FILE symbols belong there.  So move them.  */
579   if (native->u.syment.n_sclass == C_FILE)
580     symbol->section = &bfd_debug_section;
581
582   if (symbol->section == &bfd_abs_section) 
583   {
584     native->u.syment.n_scnum = N_ABS;
585   }
586   else if (symbol->section == &bfd_debug_section) 
587   {
588     native->u.syment.n_scnum = N_DEBUG;
589   }
590   else if (symbol->section == &bfd_und_section)   
591   {
592     native->u.syment.n_scnum = N_UNDEF;
593   }
594   else 
595   {
596     native->u.syment.n_scnum =
597      symbol->section->output_section->target_index;
598   }
599   
600   
601   coff_fix_symbol_name(abfd, symbol, native);
602
603   symesz = bfd_coff_symesz (abfd);
604   buf = bfd_alloc (abfd, symesz);
605   bfd_coff_swap_sym_out(abfd, &native->u.syment, buf);
606   bfd_write(buf, 1, symesz, abfd);
607   bfd_release (abfd, buf);
608
609   if (native->u.syment.n_numaux > 0)
610     {
611       bfd_size_type auxesz;
612       unsigned int j;
613
614       auxesz = bfd_coff_auxesz (abfd);
615       buf = bfd_alloc (abfd, auxesz);
616       for (j = 0; j < native->u.syment.n_numaux;  j++)
617         {
618           bfd_coff_swap_aux_out(abfd,
619                                 &((native + j + 1)->u.auxent),
620                                 type,
621                                 class,
622                                 buf);
623           bfd_write(buf, 1, auxesz, abfd);
624         }
625       bfd_release (abfd, buf);
626     }
627   /*
628     Reuse somewhere in the symbol to keep the index
629     */
630   set_index(symbol, written);
631   return   written + 1 + numaux;
632 }
633
634
635 static unsigned int
636 DEFUN(coff_write_alien_symbol,(abfd, symbol, written),
637       bfd *abfd AND
638       asymbol *symbol AND
639       unsigned int written)
640 {
641   /*
642     This symbol has been created by the loader, or come from a non
643     coff format. It  has no native element to inherit, make our
644     own
645     */
646   combined_entry_type *native;
647   combined_entry_type dummy;
648   native = &dummy;
649   native->u.syment.n_type =  T_NULL;
650   native->u.syment.n_flags =  0;
651   if (symbol->section == &bfd_und_section) 
652   {
653       native->u.syment.n_scnum =  N_UNDEF;
654       native->u.syment.n_value =  symbol->value;
655     }
656   else if (symbol->section == &bfd_com_section) 
657   {
658       native->u.syment.n_scnum =  N_UNDEF;
659       native->u.syment.n_value =  symbol->value;
660
661   }
662   
663   else if (symbol->flags & BSF_DEBUGGING) {
664       /*
665         remove name so it doesn't take up any space
666         */
667       symbol->name = "";
668     }
669   else {
670       native->u.syment.n_scnum  =   symbol->section->output_section->target_index;
671       native->u.syment.n_value =   symbol->value +
672        symbol->section->output_section->vma +
673         symbol->section->output_offset;
674       /* Copy the any flags from the the file hdr into the symbol  */
675     {
676       coff_symbol_type *c = coff_symbol_from(abfd, symbol);
677       if (c != (coff_symbol_type *)NULL) {
678           native->u.syment.n_flags =   bfd_asymbol_bfd(&c->symbol)->flags;
679         }
680     }
681     }
682
683   native->u.syment.n_type =  0;
684   if (symbol->flags & BSF_LOCAL)
685    native->u.syment.n_sclass =  C_STAT;
686   else
687    native->u.syment.n_sclass =  C_EXT;
688   native->u.syment.n_numaux =  0;
689
690   return   coff_write_symbol(abfd, symbol, native, written);
691 }
692
693 static unsigned int
694 DEFUN(coff_write_native_symbol,(abfd, symbol,   written),
695 bfd *abfd AND
696 coff_symbol_type *symbol AND
697 unsigned int written)
698 {
699   /*
700     Does this symbol have an ascociated line number - if so then
701     make it remember this symbol index. Also tag the auxent of
702     this symbol to point to the right place in the lineno table
703     */
704   combined_entry_type *native = symbol->native;
705
706   alent          *lineno = symbol->lineno;
707
708   if (lineno && !symbol->done_lineno) {
709     unsigned int    count = 0;
710     lineno[count].u.offset = written;
711     if (native->u.syment.n_numaux) {
712       union internal_auxent  *a = &((native+1)->u.auxent);
713
714       a->x_sym.x_fcnary.x_fcn.x_lnnoptr =
715         symbol->symbol.section->output_section->moving_line_filepos;
716     }
717     /*
718       And count and relocate all other linenumbers
719       */
720
721     count++;
722     while (lineno[count].line_number) {
723 #if 0
724 /* 13 april 92. sac 
725 I've been told this, but still need proof:
726 > The second bug is also in `bfd/coffcode.h'.  This bug causes the linker to screw
727 > up the pc-relocations for all the line numbers in COFF code.  This bug isn't
728 > only specific to A29K implementations, but affects all systems using COFF
729 > format binaries.  Note that in COFF object files, the line number core offsets
730 > output by the assembler are relative to the start of each procedure, not
731 > to the start of the .text section.  This patch relocates the line numbers
732 > relative to the `native->u.syment.n_value' instead of the section virtual
733 > address.  modular!olson@cs.arizona.edu (Jon Olson)
734 */
735        lineno[count].u.offset += native->u.syment.n_value;
736
737 #else
738       lineno[count].u.offset +=
739         symbol->symbol.section->output_section->vma +
740           symbol->symbol.section->output_offset;
741 #endif
742       count++;
743     }
744     symbol->done_lineno = true;
745     
746     symbol->symbol.section->output_section->moving_line_filepos +=
747       count * bfd_coff_linesz (abfd);
748   }
749   return coff_write_symbol(abfd, &( symbol->symbol), native,written);
750 }
751
752 void
753 DEFUN(coff_write_symbols,(abfd),
754       bfd            *abfd)
755 {
756   unsigned int    i;
757   unsigned int    limit = bfd_get_symcount(abfd);
758   unsigned int    written = 0;
759
760   asymbol       **p;
761
762   string_size = 0;
763
764
765   /* Seek to the right place */
766   bfd_seek(abfd, obj_sym_filepos(abfd), SEEK_SET);
767
768   /* Output all the symbols we have */
769
770   written = 0;
771   for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
772       {
773         asymbol        *symbol = *p;
774         coff_symbol_type *c_symbol = coff_symbol_from(abfd, symbol);
775
776         if (c_symbol == (coff_symbol_type *) NULL ||
777             c_symbol->native == (combined_entry_type *)NULL)
778             {
779               written = coff_write_alien_symbol(abfd, symbol, written);
780             }
781         else
782             {
783               written = coff_write_native_symbol(abfd, c_symbol, written);
784             }
785
786       }
787
788   bfd_get_symcount(abfd) = written;
789
790   /* Now write out strings */
791
792   if (string_size != 0)
793    {
794      unsigned int    size = string_size + 4;
795      bfd_byte buffer[4];
796
797      bfd_h_put_32(abfd, size, buffer);
798      bfd_write((PTR) buffer, 1, sizeof(buffer), abfd);
799      for (p = abfd->outsymbols, i = 0;
800           i < limit;
801           i++, p++)
802          {
803            asymbol        *q = *p;
804            size_t          name_length = strlen(q->name);
805            int maxlen;
806            coff_symbol_type*       c_symbol = coff_symbol_from(abfd, q);
807            maxlen = ((c_symbol != NULL && c_symbol->native != NULL) &&
808                      (c_symbol->native->u.syment.n_sclass == C_FILE)) ?
809              FILNMLEN : SYMNMLEN;
810
811            if (name_length > maxlen) {
812              bfd_write((PTR) (q->name), 1, name_length + 1, abfd);
813            }
814          }
815    }
816   else {
817     /* We would normally not write anything here, but we'll write
818        out 4 so that any stupid coff reader which tries to read
819        the string table even when there isn't one won't croak.
820        */
821
822     uint32e_type size = 4;
823     size =  size;
824     bfd_write((PTR)&size, 1, sizeof(size), abfd);
825
826   }
827 }
828
829 void
830 DEFUN(coff_write_linenumbers,(abfd),
831       bfd            *abfd)
832 {
833   asection       *s;
834   bfd_size_type linesz;
835   PTR buff;
836
837   linesz = bfd_coff_linesz (abfd);
838   buff = bfd_alloc (abfd, linesz);
839   for (s = abfd->sections; s != (asection *) NULL; s = s->next) {
840     if (s->lineno_count) {
841       asymbol       **q = abfd->outsymbols;
842       bfd_seek(abfd, s->line_filepos, SEEK_SET);
843       /* Find all the linenumbers in this section */
844       while (*q) {
845         asymbol        *p = *q;
846         alent          *l =
847           BFD_SEND(bfd_asymbol_bfd(p), _get_lineno, (bfd_asymbol_bfd(p), p));
848         if (l) {
849           /* Found a linenumber entry, output */
850           struct internal_lineno  out;
851           memset( (PTR)&out, 0, sizeof(out));
852           out.l_lnno = 0;
853           out.l_addr.l_symndx = l->u.offset;
854           bfd_coff_swap_lineno_out(abfd, &out, buff);
855           bfd_write(buff, 1, linesz, abfd);
856           l++;
857           while (l->line_number) {
858             out.l_lnno = l->line_number;
859             out.l_addr.l_symndx = l->u.offset;
860             bfd_coff_swap_lineno_out(abfd, &out, buff);
861             bfd_write(buff, 1, linesz, abfd);
862             l++;
863           }
864         }
865         q++;
866       }
867     }
868   }
869   bfd_release (abfd, buff);
870 }
871
872 alent   *
873 DEFUN(coff_get_lineno,(ignore_abfd, symbol),
874       bfd            *ignore_abfd AND
875       asymbol        *symbol)
876 {
877   return coffsymbol(symbol)->lineno;
878 }
879
880 asymbol *
881 coff_section_symbol (abfd, name)
882      bfd *abfd;
883      char *name;
884 {
885   asection *sec = bfd_make_section_old_way (abfd, name);
886   asymbol *sym;
887   combined_entry_type *csym;
888
889   sym = sec->symbol;
890   if (coff_symbol_from (abfd, sym))
891     csym = coff_symbol_from (abfd, sym)->native;
892   else
893     csym = 0;
894   /* Make sure back-end COFF stuff is there.  */
895   if (csym == 0)
896     {
897       struct foo {
898         coff_symbol_type sym;
899         /* @@FIXME This shouldn't use a fixed size!!  */
900         combined_entry_type e[10];
901       };
902       struct foo *f;
903       f = (struct foo *) bfd_alloc_by_size_t (abfd, sizeof (*f));
904       memset ((char *) f, 0, sizeof (*f));
905       coff_symbol_from (abfd, sym)->native = csym = f->e;
906     }
907   csym[0].u.syment.n_sclass = C_STAT;
908   csym[0].u.syment.n_numaux = 1;
909 /*  SF_SET_STATICS (sym);       @@ ??? */
910   if (sec)
911     {
912       csym[1].u.auxent.x_scn.x_scnlen = sec->_raw_size;
913       csym[1].u.auxent.x_scn.x_nreloc = sec->reloc_count;
914       csym[1].u.auxent.x_scn.x_nlinno = sec->lineno_count;
915     }
916   else
917     {
918       csym[1].u.auxent.x_scn.x_scnlen = 0;
919       csym[1].u.auxent.x_scn.x_nreloc = 0;
920       csym[1].u.auxent.x_scn.x_nlinno = 0;
921     }
922   return sym;
923 }
924
925 /* This function transforms the offsets into the symbol table into
926    pointers to syments.  */
927
928 static void
929 DEFUN(coff_pointerize_aux,(abfd, table_base, type, class, auxent),
930 bfd *abfd AND
931 combined_entry_type *table_base AND
932 int type AND
933 int class AND
934 combined_entry_type *auxent)
935 {
936   /* Don't bother if this is a file or a section */
937   if (class == C_STAT && type == T_NULL) return;
938   if (class == C_FILE) return;
939
940   /* Otherwise patch up */
941 #define N_TMASK coff_data (abfd)->local_n_tmask
942 #define N_BTSHFT coff_data (abfd)->local_n_btshft
943   if (ISFCN(type) || ISTAG(class) || class == C_BLOCK) {
944       auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p = table_base +
945        auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l;
946       auxent->fix_end = 1;
947     }
948   /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can
949      generate one, so we must be careful to ignore it.  */
950   if (auxent->u.auxent.x_sym.x_tagndx.l > 0) {
951       auxent->u.auxent.x_sym.x_tagndx.p =
952        table_base +  auxent->u.auxent.x_sym.x_tagndx.l;
953       auxent->fix_tag = 1;
954     }
955 }
956
957 static char *
958 DEFUN(build_string_table,(abfd),
959 bfd *abfd)
960 {
961   char string_table_size_buffer[4];
962   unsigned int string_table_size;
963   char *string_table;
964
965   /* At this point we should be "seek"'d to the end of the
966      symbols === the symbol table size.  */
967   if (bfd_read((char *) string_table_size_buffer,
968                sizeof(string_table_size_buffer),
969                1, abfd) != sizeof(string_table_size)) {
970     bfd_error = system_call_error;
971     return (NULL);
972   }                             /* on error */
973
974   string_table_size = bfd_h_get_32(abfd, (bfd_byte *) string_table_size_buffer);
975
976   if ((string_table = (PTR) bfd_alloc(abfd, string_table_size -= 4)) == NULL) {
977     bfd_error = no_memory;
978     return (NULL);
979   }                             /* on mallocation error */
980   if (bfd_read(string_table, string_table_size, 1, abfd) != string_table_size) {
981     bfd_error = system_call_error;
982     return (NULL);
983   }
984   return string_table;
985 }
986
987 /* Allocate space for the ".debug" section, and read it.
988    We did not read the debug section until now, because
989    we didn't want to go to the trouble until someone needed it. */
990
991 static char *
992 DEFUN(build_debug_section,(abfd),
993         bfd *abfd)
994 {
995   char *debug_section;
996   long position;
997
998   asection *sect = bfd_get_section_by_name (abfd, ".debug");
999
1000   if (!sect) {
1001      bfd_error = no_debug_section;
1002      return NULL;
1003   }
1004
1005   debug_section = (PTR) bfd_alloc (abfd,
1006                                    bfd_get_section_size_before_reloc (sect));
1007   if (debug_section == NULL) {
1008     bfd_error = no_memory;
1009     return NULL;
1010   }
1011
1012   /* Seek to the beginning of the `.debug' section and read it. 
1013      Save the current position first; it is needed by our caller.
1014      Then read debug section and reset the file pointer.  */
1015
1016   position = bfd_tell (abfd);
1017   bfd_seek (abfd, sect->filepos, SEEK_SET);
1018   if (bfd_read (debug_section, 
1019                 bfd_get_section_size_before_reloc (sect), 1, abfd)
1020       != bfd_get_section_size_before_reloc(sect)) {
1021     bfd_error = system_call_error;
1022     return NULL;
1023   }
1024   bfd_seek (abfd, position, SEEK_SET);
1025   return debug_section;
1026 }
1027
1028
1029 /* Return a pointer to a malloc'd copy of 'name'.  'name' may not be
1030  \0-terminated, but will not exceed 'maxlen' characters.  The copy *will*
1031  be \0-terminated.  */
1032 static char *
1033 DEFUN(copy_name,(abfd, name, maxlen),
1034       bfd *abfd AND
1035       char *name AND
1036       int maxlen)
1037 {
1038   int  len;
1039   char *newname;
1040
1041   for (len = 0; len < maxlen; ++len) {
1042     if (name[len] == '\0') {
1043       break;
1044     }
1045   }
1046
1047   if ((newname = (PTR) bfd_alloc(abfd, len+1)) == NULL) {
1048     bfd_error = no_memory;
1049     return (NULL);
1050   }
1051   strncpy(newname, name, len);
1052   newname[len] = '\0';
1053   return newname;
1054 }
1055
1056 /* Read a symbol table into freshly bfd_allocated memory, swap it, and
1057    knit the symbol names into a normalized form.  By normalized here I
1058    mean that all symbols have an n_offset pointer that points to a null-
1059    terminated string.  */
1060
1061 combined_entry_type *
1062 DEFUN(coff_get_normalized_symtab,(abfd),
1063 bfd            *abfd)
1064 {
1065   combined_entry_type          *internal;
1066   combined_entry_type          *internal_ptr;
1067   combined_entry_type          *symbol_ptr;
1068   combined_entry_type         *internal_end;
1069   bfd_size_type symesz;
1070   PTR raw;
1071   char *raw_src;
1072   char *raw_end;
1073   char           *string_table = NULL;
1074   char           *debug_section = NULL;
1075   unsigned long   size;
1076
1077   unsigned int raw_size;
1078   if (obj_raw_syments(abfd) != (combined_entry_type *)NULL) {
1079       return obj_raw_syments(abfd);
1080     }
1081   if ((size = bfd_get_symcount(abfd) * sizeof(combined_entry_type)) == 0) {
1082       bfd_error = no_symbols;
1083       return (NULL);
1084     }
1085
1086   internal = (combined_entry_type *)bfd_alloc(abfd, size);
1087   internal_end = internal + bfd_get_symcount(abfd);
1088
1089   symesz = bfd_coff_symesz (abfd);
1090   raw_size =      bfd_get_symcount(abfd) * symesz;
1091   raw = bfd_alloc(abfd,raw_size);
1092
1093   if (bfd_seek(abfd, obj_sym_filepos(abfd), SEEK_SET) == -1
1094       || bfd_read(raw, raw_size, 1, abfd) != raw_size) {
1095       bfd_error = system_call_error;
1096       return (NULL);
1097     }
1098   /* mark the end of the symbols */
1099   raw_end = (char *) raw + bfd_get_symcount(abfd) * symesz;
1100   /*
1101     FIXME SOMEDAY.  A string table size of zero is very weird, but
1102     probably possible.  If one shows up, it will probably kill us.
1103     */
1104
1105   /* Swap all the raw entries */
1106   for (raw_src = (char *) raw, internal_ptr = internal;
1107        raw_src < raw_end;
1108        raw_src += symesz, internal_ptr++) {
1109
1110       unsigned int i;
1111       bfd_coff_swap_sym_in(abfd, (PTR)raw_src, (PTR)&internal_ptr->u.syment);
1112       internal_ptr->fix_tag = 0;
1113       internal_ptr->fix_end = 0;
1114       symbol_ptr = internal_ptr;
1115     
1116       for (i = 0;
1117            i < symbol_ptr->u.syment.n_numaux;
1118            i++) 
1119       {
1120         internal_ptr++;
1121         raw_src += symesz;
1122       
1123         internal_ptr->fix_tag = 0;
1124         internal_ptr->fix_end = 0;
1125         bfd_coff_swap_aux_in(abfd, (PTR) raw_src,
1126                              symbol_ptr->u.syment.n_type,
1127                              symbol_ptr->u.syment.n_sclass,
1128                              &(internal_ptr->u.auxent));
1129         /* Remember that bal entries arn't pointerized */
1130         if (i != 1 || symbol_ptr->u.syment.n_sclass != C_LEAFPROC)
1131         {
1132           
1133         coff_pointerize_aux(abfd,
1134                             internal,
1135                             symbol_ptr->u.syment.n_type,
1136                             symbol_ptr->u.syment.n_sclass,
1137                             internal_ptr);
1138       }
1139         
1140       }
1141     }
1142
1143   /* Free all the raw stuff */
1144   bfd_release(abfd, raw);
1145
1146   for (internal_ptr = internal; internal_ptr < internal_end;
1147        internal_ptr ++)
1148   {
1149     if (internal_ptr->u.syment.n_sclass == C_FILE) {
1150         /* make a file symbol point to the name in the auxent, since
1151            the text ".file" is redundant */
1152         if ((internal_ptr+1)->u.auxent.x_file.x_n.x_zeroes == 0) {
1153             /* the filename is a long one, point into the string table */
1154             if (string_table == NULL) {
1155                 string_table = build_string_table(abfd);
1156               }
1157
1158             internal_ptr->u.syment._n._n_n._n_offset =
1159              (int) (string_table - 4 +
1160                     (internal_ptr+1)->u.auxent.x_file.x_n.x_offset);
1161           }
1162         else {
1163             /* ordinary short filename, put into memory anyway */
1164             internal_ptr->u.syment._n._n_n._n_offset = (int)
1165              copy_name(abfd, (internal_ptr+1)->u.auxent.x_file.x_fname,
1166                        FILNMLEN);
1167           }
1168       }
1169     else {
1170         if (internal_ptr->u.syment._n._n_n._n_zeroes != 0) {
1171             /* This is a "short" name.  Make it long.  */
1172             unsigned long   i = 0;
1173             char           *newstring = NULL;
1174
1175             /* find the length of this string without walking into memory
1176                that isn't ours.  */
1177             for (i = 0; i < 8; ++i) {
1178                 if (internal_ptr->u.syment._n._n_name[i] == '\0') {
1179                     break;
1180                   }             /* if end of string */
1181               }                 /* possible lengths of this string. */
1182
1183             if ((newstring = (PTR) bfd_alloc(abfd, ++i)) == NULL) {
1184                 bfd_error = no_memory;
1185                 return (NULL);
1186               }                 /* on error */
1187             memset(newstring, 0, i);
1188             strncpy(newstring, internal_ptr->u.syment._n._n_name, i-1);
1189             internal_ptr->u.syment._n._n_n._n_offset =  (int) newstring;
1190             internal_ptr->u.syment._n._n_n._n_zeroes = 0;
1191           }
1192         else if (!bfd_coff_symname_in_debug(abfd, &internal_ptr->u.syment)) {
1193             /* Long name already.  Point symbol at the string in the table.  */
1194             if (string_table == NULL) {
1195                 string_table = build_string_table(abfd);
1196               }
1197             internal_ptr->u.syment._n._n_n._n_offset = (int)
1198              (string_table - 4 + internal_ptr->u.syment._n._n_n._n_offset);
1199           }
1200         else {
1201             /* Long name in debug section.  Very similar.  */
1202             if (debug_section == NULL) {
1203                 debug_section = build_debug_section(abfd);
1204               }
1205             internal_ptr->u.syment._n._n_n._n_offset = (int)
1206              (debug_section + internal_ptr->u.syment._n._n_n._n_offset);
1207           }
1208       }
1209     internal_ptr += internal_ptr->u.syment.n_numaux;
1210   }
1211
1212   obj_raw_syments(abfd) = internal;
1213
1214   return (internal);
1215 }                               /* coff_get_normalized_symtab() */
1216
1217 unsigned int
1218 DEFUN (coff_get_reloc_upper_bound, (abfd, asect),
1219        bfd            *abfd AND
1220        sec_ptr         asect)
1221 {
1222   if (bfd_get_format(abfd) != bfd_object) {
1223     bfd_error = invalid_operation;
1224     return 0;
1225   }
1226   return (asect->reloc_count + 1) * sizeof(arelent *);
1227 }
1228
1229 asymbol *
1230 DEFUN (coff_make_empty_symbol, (abfd),
1231        bfd            *abfd)
1232 {
1233   coff_symbol_type *new = (coff_symbol_type *) bfd_alloc(abfd, sizeof(coff_symbol_type));
1234   if (new == NULL) {
1235     bfd_error = no_memory;
1236     return (NULL);
1237   }                             /* on error */
1238   new->symbol.section = 0;
1239   new->native = 0;
1240   new->lineno = (alent *) NULL;
1241   new->done_lineno = false;
1242   new->symbol.the_bfd = abfd;
1243   return &new->symbol;
1244 }
1245
1246 asymbol *
1247 DEFUN (coff_make_debug_symbol, (abfd, ptr, sz),
1248        bfd *abfd AND
1249        PTR ptr AND
1250        unsigned long sz)
1251 {
1252   coff_symbol_type *new = (coff_symbol_type *) bfd_alloc(abfd, sizeof(coff_symbol_type));
1253   if (new == NULL) {
1254     bfd_error = no_memory;
1255     return (NULL);
1256   }                             /* on error */
1257   /* @@ This shouldn't be using a constant multiplier.  */
1258   new->native = (combined_entry_type *) bfd_zalloc (abfd, sizeof (combined_entry_type) * 10);
1259   new->symbol.section = &bfd_debug_section;
1260   new->lineno = (alent *) NULL;
1261   new->done_lineno = false;
1262   new->symbol.the_bfd = abfd;
1263   return &new->symbol;
1264 }
1265
1266 void
1267 DEFUN(coff_print_symbol,(abfd, filep, symbol, how),
1268       bfd            *abfd AND
1269       PTR           filep AND
1270       asymbol        *symbol AND
1271       bfd_print_symbol_type how)
1272 {
1273   FILE *file = (FILE *)filep;
1274   switch (how) {
1275     case bfd_print_symbol_name:
1276       fprintf(file, "%s", symbol->name);
1277       break;
1278     case bfd_print_symbol_more:
1279       fprintf(file, "coff %lx %lx", (unsigned long) coffsymbol(symbol)->native,
1280               (unsigned long) coffsymbol(symbol)->lineno);
1281       break;
1282     case bfd_print_symbol_nm:
1283
1284     {
1285       CONST char *section_name = symbol->section->name;
1286       bfd_print_symbol_vandf((PTR) file, symbol);
1287
1288         
1289       fprintf(file, " %-5s %s %s %s",
1290               section_name,
1291               coffsymbol(symbol)->native ? "n" : "g",
1292               coffsymbol(symbol)->lineno ? "l" : " ",
1293               symbol->name);
1294     }
1295
1296
1297       break;
1298     case bfd_print_symbol_all:
1299       /* Print out the symbols in a reasonable way */
1300     {
1301       CONST char *section_name = symbol->section->name;
1302
1303
1304       if (coffsymbol(symbol)->native) 
1305       {
1306         unsigned int aux;
1307         combined_entry_type *combined = coffsymbol(symbol)->native;
1308         combined_entry_type *root = obj_raw_syments(abfd);
1309         
1310         fprintf(file,"[%3d]",
1311                 combined - root);
1312         
1313
1314         fprintf(file, "(sc %2d)(fl%4x)(ty%3x)(sc%3d) nx(%d) %08x %s",
1315                 combined->u.syment.n_scnum,
1316                 combined->u.syment.n_flags,
1317                 combined->u.syment.n_type,
1318                 combined->u.syment.n_sclass,
1319                 combined->u.syment.n_numaux,
1320                 combined->u.syment.n_value,
1321                 symbol->name
1322                 );
1323         for (aux = 0; aux < combined->u.syment.n_numaux; aux++) 
1324         {
1325           fprintf(file,"\n");
1326           switch (combined->u.syment.n_sclass) {
1327             case C_FILE:
1328               fprintf(file, "File ");
1329               break;
1330             default:
1331               fprintf(file, "AUX lnno %x size %x tagndx %x",
1332                       combined[aux+1].u.auxent.x_sym.x_misc.x_lnsz.x_lnno,
1333                       combined[aux+1].u.auxent.x_sym.x_misc.x_lnsz.x_size,
1334                       combined[aux+1].u.auxent.x_sym.x_tagndx.l);
1335               break;
1336     
1337             }
1338
1339         }
1340         
1341       {
1342         struct lineno_cache_entry *l = coffsymbol(symbol)->lineno;
1343         if (l) 
1344         {
1345           printf("\n%s :", l->u.sym->name);
1346           l++;
1347           while (l->line_number) 
1348           {
1349             printf("\n%4d : %x", 
1350                    l->line_number,
1351                    l->u.offset);
1352             l++;
1353             
1354           }
1355         }
1356       }
1357
1358     
1359
1360       } 
1361
1362       else {
1363           bfd_print_symbol_vandf((PTR) file, symbol);
1364           fprintf(file, " %-5s %s %s %s",
1365                   section_name,
1366                   coffsymbol(symbol)->native ? "n" : "g",
1367                   coffsymbol(symbol)->lineno ? "l" : " ",
1368                   symbol->name);
1369         }
1370
1371     }
1372         
1373     }
1374 }
1375
1376 /* Provided a BFD, a section and an offset into the section, calculate
1377    and return the name of the source file and the line nearest to the
1378    wanted location.  */
1379
1380 boolean
1381 DEFUN(coff_find_nearest_line,(abfd,
1382                               section,
1383                               ignore_symbols,
1384                               offset,
1385                               filename_ptr,
1386                               functionname_ptr,
1387                               line_ptr),
1388       bfd            *abfd AND
1389       asection       *section AND
1390       asymbol       **ignore_symbols AND
1391       bfd_vma         offset AND
1392       CONST char      **filename_ptr AND
1393       CONST char       **functionname_ptr AND
1394       unsigned int   *line_ptr)
1395 {
1396   static bfd     *cache_abfd;
1397   static asection *cache_section;
1398   static bfd_vma  cache_offset;
1399   static unsigned int cache_i;
1400   static alent   *cache_l;
1401
1402   unsigned int    i = 0;
1403   coff_data_type *cof = coff_data(abfd);
1404   /* Run through the raw syments if available */
1405   combined_entry_type *p;
1406   alent          *l;
1407   unsigned int    line_base = 0;
1408
1409
1410   *filename_ptr = 0;
1411   *functionname_ptr = 0;
1412   *line_ptr = 0;
1413
1414   /* Don't try and find line numbers in a non coff file */
1415   if (abfd->xvec->flavour != bfd_target_coff_flavour)
1416     return false;
1417
1418   if (cof == NULL)
1419     return false;
1420
1421   p = cof->raw_syments;
1422
1423   for (i = 0; i < cof->raw_syment_count; i++) {
1424     if (p->u.syment.n_sclass == C_FILE) {
1425       /* File name has been moved into symbol */
1426       *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
1427       break;
1428     }
1429     p += 1 +  p->u.syment.n_numaux;
1430   }
1431   /* Now wander though the raw linenumbers of the section */
1432   /*
1433     If this is the same BFD as we were previously called with and this is
1434     the same section, and the offset we want is further down then we can
1435     prime the lookup loop
1436     */
1437   if (abfd == cache_abfd &&
1438       section == cache_section &&
1439       offset >= cache_offset) {
1440     i = cache_i;
1441     l = cache_l;
1442   }
1443   else {
1444     i = 0;
1445     l = section->lineno;
1446   }
1447
1448   for (; i < section->lineno_count; i++) {
1449     if (l->line_number == 0) {
1450       /* Get the symbol this line number points at */
1451       coff_symbol_type *coff = (coff_symbol_type *) (l->u.sym);
1452       *functionname_ptr = coff->symbol.name;
1453       if (coff->native) {
1454         combined_entry_type  *s = coff->native;
1455         s = s + 1 + s->u.syment.n_numaux;
1456         /*
1457           S should now point to the .bf of the function
1458           */
1459         if (s->u.syment.n_numaux) {
1460           /*
1461             The linenumber is stored in the auxent
1462             */
1463           union internal_auxent   *a = &((s + 1)->u.auxent);
1464           line_base = a->x_sym.x_misc.x_lnsz.x_lnno;
1465         }
1466       }
1467     }
1468     else {
1469       if (l->u.offset > offset)
1470         break;
1471       *line_ptr = l->line_number + line_base + 1;
1472     }
1473     l++;
1474   }
1475
1476   cache_abfd = abfd;
1477   cache_section = section;
1478   cache_offset = offset;
1479   cache_i = i;
1480   cache_l = l;
1481
1482   return true;
1483 }
1484
1485 int
1486 DEFUN(coff_sizeof_headers,(abfd, reloc),
1487       bfd *abfd AND
1488       boolean reloc)
1489 {
1490     size_t size;
1491
1492     if (reloc == false) {
1493         size = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
1494     }
1495     else {
1496         size = bfd_coff_filhsz (abfd);
1497     }
1498
1499     size +=  abfd->section_count * bfd_coff_scnhsz (abfd);
1500     return size;
1501 }