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