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