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