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