Back to index

cell-binutils  2.17cvs20070401
coffgen.c
Go to the documentation of this file.
00001 /* Support for the generic parts of COFF, for BFD.
00002    Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
00003    2000, 2001, 2002, 2003, 2004, 2005
00004    Free Software Foundation, Inc.
00005    Written by Cygnus Support.
00006 
00007    This file is part of BFD, the Binary File Descriptor library.
00008 
00009    This program is free software; you can redistribute it and/or modify
00010    it under the terms of the GNU General Public License as published by
00011    the Free Software Foundation; either version 2 of the License, or
00012    (at your option) any later version.
00013 
00014    This program is distributed in the hope that it will be useful,
00015    but WITHOUT ANY WARRANTY; without even the implied warranty of
00016    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017    GNU General Public License for more details.
00018 
00019    You should have received a copy of the GNU General Public License
00020    along with this program; if not, write to the Free Software
00021    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
00022 
00023 /* Most of this hacked by  Steve Chamberlain, sac@cygnus.com.
00024    Split out of coffcode.h by Ian Taylor, ian@cygnus.com.  */
00025 
00026 /* This file contains COFF code that is not dependent on any
00027    particular COFF target.  There is only one version of this file in
00028    libbfd.a, so no target specific code may be put in here.  Or, to
00029    put it another way,
00030 
00031    ********** DO NOT PUT TARGET SPECIFIC CODE IN THIS FILE **********
00032 
00033    If you need to add some target specific behaviour, add a new hook
00034    function to bfd_coff_backend_data.
00035 
00036    Some of these functions are also called by the ECOFF routines.
00037    Those functions may not use any COFF specific information, such as
00038    coff_data (abfd).  */
00039 
00040 #include "bfd.h"
00041 #include "sysdep.h"
00042 #include "libbfd.h"
00043 #include "coff/internal.h"
00044 #include "libcoff.h"
00045 
00046 /* Take a section header read from a coff file (in HOST byte order),
00047    and make a BFD "section" out of it.  This is used by ECOFF.  */
00048 
00049 static bfd_boolean
00050 make_a_section_from_file (bfd *abfd,
00051                        struct internal_scnhdr *hdr,
00052                        unsigned int target_index)
00053 {
00054   asection *return_section;
00055   char *name;
00056   bfd_boolean result = TRUE;
00057   flagword flags;
00058 
00059   name = NULL;
00060 
00061   /* Handle long section names as in PE.  */
00062   if (bfd_coff_long_section_names (abfd)
00063       && hdr->s_name[0] == '/')
00064     {
00065       char buf[SCNNMLEN];
00066       long strindex;
00067       char *p;
00068       const char *strings;
00069 
00070       memcpy (buf, hdr->s_name + 1, SCNNMLEN - 1);
00071       buf[SCNNMLEN - 1] = '\0';
00072       strindex = strtol (buf, &p, 10);
00073       if (*p == '\0' && strindex >= 0)
00074        {
00075          strings = _bfd_coff_read_string_table (abfd);
00076          if (strings == NULL)
00077            return FALSE;
00078          /* FIXME: For extra safety, we should make sure that
00079              strindex does not run us past the end, but right now we
00080              don't know the length of the string table.  */
00081          strings += strindex;
00082          name = bfd_alloc (abfd, (bfd_size_type) strlen (strings) + 1);
00083          if (name == NULL)
00084            return FALSE;
00085          strcpy (name, strings);
00086        }
00087     }
00088 
00089   if (name == NULL)
00090     {
00091       /* Assorted wastage to null-terminate the name, thanks AT&T! */
00092       name = bfd_alloc (abfd, (bfd_size_type) sizeof (hdr->s_name) + 1);
00093       if (name == NULL)
00094        return FALSE;
00095       strncpy (name, (char *) &hdr->s_name[0], sizeof (hdr->s_name));
00096       name[sizeof (hdr->s_name)] = 0;
00097     }
00098 
00099   return_section = bfd_make_section_anyway (abfd, name);
00100   if (return_section == NULL)
00101     return FALSE;
00102 
00103   return_section->vma = hdr->s_vaddr;
00104   return_section->lma = hdr->s_paddr;
00105   return_section->size = hdr->s_size;
00106   return_section->filepos = hdr->s_scnptr;
00107   return_section->rel_filepos = hdr->s_relptr;
00108   return_section->reloc_count = hdr->s_nreloc;
00109 
00110   bfd_coff_set_alignment_hook (abfd, return_section, hdr);
00111 
00112   return_section->line_filepos = hdr->s_lnnoptr;
00113 
00114   return_section->lineno_count = hdr->s_nlnno;
00115   return_section->userdata = NULL;
00116   return_section->next = NULL;
00117   return_section->target_index = target_index;
00118 
00119   if (! bfd_coff_styp_to_sec_flags_hook (abfd, hdr, name, return_section,
00120                                     & flags))
00121     result = FALSE;
00122 
00123   return_section->flags = flags;
00124 
00125   /* At least on i386-coff, the line number count for a shared library
00126      section must be ignored.  */
00127   if ((return_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
00128     return_section->lineno_count = 0;
00129 
00130   if (hdr->s_nreloc != 0)
00131     return_section->flags |= SEC_RELOC;
00132   /* FIXME: should this check 'hdr->s_size > 0'.  */
00133   if (hdr->s_scnptr != 0)
00134     return_section->flags |= SEC_HAS_CONTENTS;
00135 
00136   return result;
00137 }
00138 
00139 /* Read in a COFF object and make it into a BFD.  This is used by
00140    ECOFF as well.  */
00141 
00142 static const bfd_target *
00143 coff_real_object_p (bfd *abfd,
00144                   unsigned nscns,
00145                   struct internal_filehdr *internal_f,
00146                   struct internal_aouthdr *internal_a)
00147 {
00148   flagword oflags = abfd->flags;
00149   bfd_vma ostart = bfd_get_start_address (abfd);
00150   void * tdata;
00151   void * tdata_save;
00152   bfd_size_type readsize;   /* Length of file_info.  */
00153   unsigned int scnhsz;
00154   char *external_sections;
00155 
00156   if (!(internal_f->f_flags & F_RELFLG))
00157     abfd->flags |= HAS_RELOC;
00158   if ((internal_f->f_flags & F_EXEC))
00159     abfd->flags |= EXEC_P;
00160   if (!(internal_f->f_flags & F_LNNO))
00161     abfd->flags |= HAS_LINENO;
00162   if (!(internal_f->f_flags & F_LSYMS))
00163     abfd->flags |= HAS_LOCALS;
00164 
00165   /* FIXME: How can we set D_PAGED correctly?  */
00166   if ((internal_f->f_flags & F_EXEC) != 0)
00167     abfd->flags |= D_PAGED;
00168 
00169   bfd_get_symcount (abfd) = internal_f->f_nsyms;
00170   if (internal_f->f_nsyms)
00171     abfd->flags |= HAS_SYMS;
00172 
00173   if (internal_a != (struct internal_aouthdr *) NULL)
00174     bfd_get_start_address (abfd) = internal_a->entry;
00175   else
00176     bfd_get_start_address (abfd) = 0;
00177 
00178   /* Set up the tdata area.  ECOFF uses its own routine, and overrides
00179      abfd->flags.  */
00180   tdata_save = abfd->tdata.any;
00181   tdata = bfd_coff_mkobject_hook (abfd, (void *) internal_f, (void *) internal_a);
00182   if (tdata == NULL)
00183     goto fail2;
00184 
00185   scnhsz = bfd_coff_scnhsz (abfd);
00186   readsize = (bfd_size_type) nscns * scnhsz;
00187   external_sections = bfd_alloc (abfd, readsize);
00188   if (!external_sections)
00189     goto fail;
00190 
00191   if (bfd_bread ((void *) external_sections, readsize, abfd) != readsize)
00192     goto fail;
00193 
00194   /* Set the arch/mach *before* swapping in sections; section header swapping
00195      may depend on arch/mach info.  */
00196   if (! bfd_coff_set_arch_mach_hook (abfd, (void *) internal_f))
00197     goto fail;
00198 
00199   /* Now copy data as required; construct all asections etc.  */
00200   if (nscns != 0)
00201     {
00202       unsigned int i;
00203       for (i = 0; i < nscns; i++)
00204        {
00205          struct internal_scnhdr tmp;
00206          bfd_coff_swap_scnhdr_in (abfd,
00207                                (void *) (external_sections + i * scnhsz),
00208                                (void *) & tmp);
00209          if (! make_a_section_from_file (abfd, &tmp, i + 1))
00210            goto fail;
00211        }
00212     }
00213 
00214   return abfd->xvec;
00215 
00216  fail:
00217   bfd_release (abfd, tdata);
00218  fail2:
00219   abfd->tdata.any = tdata_save;
00220   abfd->flags = oflags;
00221   bfd_get_start_address (abfd) = ostart;
00222   return (const bfd_target *) NULL;
00223 }
00224 
00225 /* Turn a COFF file into a BFD, but fail with bfd_error_wrong_format if it is
00226    not a COFF file.  This is also used by ECOFF.  */
00227 
00228 const bfd_target *
00229 coff_object_p (bfd *abfd)
00230 {
00231   bfd_size_type filhsz;
00232   bfd_size_type aoutsz;
00233   unsigned int nscns;
00234   void * filehdr;
00235   struct internal_filehdr internal_f;
00236   struct internal_aouthdr internal_a;
00237 
00238   /* Figure out how much to read.  */
00239   filhsz = bfd_coff_filhsz (abfd);
00240   aoutsz = bfd_coff_aoutsz (abfd);
00241 
00242   filehdr = bfd_alloc (abfd, filhsz);
00243   if (filehdr == NULL)
00244     return NULL;
00245   if (bfd_bread (filehdr, filhsz, abfd) != filhsz)
00246     {
00247       if (bfd_get_error () != bfd_error_system_call)
00248        bfd_set_error (bfd_error_wrong_format);
00249       bfd_release (abfd, filehdr);
00250       return NULL;
00251     }
00252   bfd_coff_swap_filehdr_in (abfd, filehdr, &internal_f);
00253   bfd_release (abfd, filehdr);
00254 
00255   /* The XCOFF format has two sizes for the f_opthdr.  SMALL_AOUTSZ
00256      (less than aoutsz) used in object files and AOUTSZ (equal to
00257      aoutsz) in executables.  The bfd_coff_swap_aouthdr_in function
00258      expects this header to be aoutsz bytes in length, so we use that
00259      value in the call to bfd_alloc below.  But we must be careful to
00260      only read in f_opthdr bytes in the call to bfd_bread.  We should
00261      also attempt to catch corrupt or non-COFF binaries with a strange
00262      value for f_opthdr.  */
00263   if (! bfd_coff_bad_format_hook (abfd, &internal_f)
00264       || internal_f.f_opthdr > aoutsz)
00265     {
00266       bfd_set_error (bfd_error_wrong_format);
00267       return NULL;
00268     }
00269   nscns = internal_f.f_nscns;
00270 
00271   if (internal_f.f_opthdr)
00272     {
00273       void * opthdr;
00274 
00275       opthdr = bfd_alloc (abfd, aoutsz);
00276       if (opthdr == NULL)
00277        return NULL;
00278       if (bfd_bread (opthdr, (bfd_size_type) internal_f.f_opthdr, abfd)
00279          != internal_f.f_opthdr)
00280        {
00281          bfd_release (abfd, opthdr);
00282          return NULL;
00283        }
00284       bfd_coff_swap_aouthdr_in (abfd, opthdr, (void *) &internal_a);
00285       bfd_release (abfd, opthdr);
00286     }
00287 
00288   return coff_real_object_p (abfd, nscns, &internal_f,
00289                           (internal_f.f_opthdr != 0
00290                            ? &internal_a
00291                            : (struct internal_aouthdr *) NULL));
00292 }
00293 
00294 /* Get the BFD section from a COFF symbol section number.  */
00295 
00296 asection *
00297 coff_section_from_bfd_index (bfd *abfd, int index)
00298 {
00299   struct bfd_section *answer = abfd->sections;
00300 
00301   if (index == N_ABS)
00302     return bfd_abs_section_ptr;
00303   if (index == N_UNDEF)
00304     return bfd_und_section_ptr;
00305   if (index == N_DEBUG)
00306     return bfd_abs_section_ptr;
00307 
00308   while (answer)
00309     {
00310       if (answer->target_index == index)
00311        return answer;
00312       answer = answer->next;
00313     }
00314 
00315   /* We should not reach this point, but the SCO 3.2v4 /lib/libc_s.a
00316      has a bad symbol table in biglitpow.o.  */
00317   return bfd_und_section_ptr;
00318 }
00319 
00320 /* Get the upper bound of a COFF symbol table.  */
00321 
00322 long
00323 coff_get_symtab_upper_bound (bfd *abfd)
00324 {
00325   if (!bfd_coff_slurp_symbol_table (abfd))
00326     return -1;
00327 
00328   return (bfd_get_symcount (abfd) + 1) * (sizeof (coff_symbol_type *));
00329 }
00330 
00331 /* Canonicalize a COFF symbol table.  */
00332 
00333 long
00334 coff_canonicalize_symtab (bfd *abfd, asymbol **alocation)
00335 {
00336   unsigned int counter;
00337   coff_symbol_type *symbase;
00338   coff_symbol_type **location = (coff_symbol_type **) alocation;
00339 
00340   if (!bfd_coff_slurp_symbol_table (abfd))
00341     return -1;
00342 
00343   symbase = obj_symbols (abfd);
00344   counter = bfd_get_symcount (abfd);
00345   while (counter-- > 0)
00346     *location++ = symbase++;
00347 
00348   *location = NULL;
00349 
00350   return bfd_get_symcount (abfd);
00351 }
00352 
00353 /* Get the name of a symbol.  The caller must pass in a buffer of size
00354    >= SYMNMLEN + 1.  */
00355 
00356 const char *
00357 _bfd_coff_internal_syment_name (bfd *abfd,
00358                             const struct internal_syment *sym,
00359                             char *buf)
00360 {
00361   /* FIXME: It's not clear this will work correctly if sizeof
00362      (_n_zeroes) != 4.  */
00363   if (sym->_n._n_n._n_zeroes != 0
00364       || sym->_n._n_n._n_offset == 0)
00365     {
00366       memcpy (buf, sym->_n._n_name, SYMNMLEN);
00367       buf[SYMNMLEN] = '\0';
00368       return buf;
00369     }
00370   else
00371     {
00372       const char *strings;
00373 
00374       BFD_ASSERT (sym->_n._n_n._n_offset >= STRING_SIZE_SIZE);
00375       strings = obj_coff_strings (abfd);
00376       if (strings == NULL)
00377        {
00378          strings = _bfd_coff_read_string_table (abfd);
00379          if (strings == NULL)
00380            return NULL;
00381        }
00382       return strings + sym->_n._n_n._n_offset;
00383     }
00384 }
00385 
00386 /* Read in and swap the relocs.  This returns a buffer holding the
00387    relocs for section SEC in file ABFD.  If CACHE is TRUE and
00388    INTERNAL_RELOCS is NULL, the relocs read in will be saved in case
00389    the function is called again.  If EXTERNAL_RELOCS is not NULL, it
00390    is a buffer large enough to hold the unswapped relocs.  If
00391    INTERNAL_RELOCS is not NULL, it is a buffer large enough to hold
00392    the swapped relocs.  If REQUIRE_INTERNAL is TRUE, then the return
00393    value must be INTERNAL_RELOCS.  The function returns NULL on error.  */
00394 
00395 struct internal_reloc *
00396 _bfd_coff_read_internal_relocs (bfd *abfd,
00397                             asection *sec,
00398                             bfd_boolean cache,
00399                             bfd_byte *external_relocs,
00400                             bfd_boolean require_internal,
00401                             struct internal_reloc *internal_relocs)
00402 {
00403   bfd_size_type relsz;
00404   bfd_byte *free_external = NULL;
00405   struct internal_reloc *free_internal = NULL;
00406   bfd_byte *erel;
00407   bfd_byte *erel_end;
00408   struct internal_reloc *irel;
00409   bfd_size_type amt;
00410 
00411   if (coff_section_data (abfd, sec) != NULL
00412       && coff_section_data (abfd, sec)->relocs != NULL)
00413     {
00414       if (! require_internal)
00415        return coff_section_data (abfd, sec)->relocs;
00416       memcpy (internal_relocs, coff_section_data (abfd, sec)->relocs,
00417              sec->reloc_count * sizeof (struct internal_reloc));
00418       return internal_relocs;
00419     }
00420 
00421   relsz = bfd_coff_relsz (abfd);
00422 
00423   amt = sec->reloc_count * relsz;
00424   if (external_relocs == NULL)
00425     {
00426       free_external = bfd_malloc (amt);
00427       if (free_external == NULL && sec->reloc_count > 0)
00428        goto error_return;
00429       external_relocs = free_external;
00430     }
00431 
00432   if (bfd_seek (abfd, sec->rel_filepos, SEEK_SET) != 0
00433       || bfd_bread (external_relocs, amt, abfd) != amt)
00434     goto error_return;
00435 
00436   if (internal_relocs == NULL)
00437     {
00438       amt = sec->reloc_count;
00439       amt *= sizeof (struct internal_reloc);
00440       free_internal = bfd_malloc (amt);
00441       if (free_internal == NULL && sec->reloc_count > 0)
00442        goto error_return;
00443       internal_relocs = free_internal;
00444     }
00445 
00446   /* Swap in the relocs.  */
00447   erel = external_relocs;
00448   erel_end = erel + relsz * sec->reloc_count;
00449   irel = internal_relocs;
00450   for (; erel < erel_end; erel += relsz, irel++)
00451     bfd_coff_swap_reloc_in (abfd, (void *) erel, (void *) irel);
00452 
00453   if (free_external != NULL)
00454     {
00455       free (free_external);
00456       free_external = NULL;
00457     }
00458 
00459   if (cache && free_internal != NULL)
00460     {
00461       if (coff_section_data (abfd, sec) == NULL)
00462        {
00463          amt = sizeof (struct coff_section_tdata);
00464          sec->used_by_bfd = bfd_zalloc (abfd, amt);
00465          if (sec->used_by_bfd == NULL)
00466            goto error_return;
00467          coff_section_data (abfd, sec)->contents = NULL;
00468        }
00469       coff_section_data (abfd, sec)->relocs = free_internal;
00470     }
00471 
00472   return internal_relocs;
00473 
00474  error_return:
00475   if (free_external != NULL)
00476     free (free_external);
00477   if (free_internal != NULL)
00478     free (free_internal);
00479   return NULL;
00480 }
00481 
00482 /* Set lineno_count for the output sections of a COFF file.  */
00483 
00484 int
00485 coff_count_linenumbers (bfd *abfd)
00486 {
00487   unsigned int limit = bfd_get_symcount (abfd);
00488   unsigned int i;
00489   int total = 0;
00490   asymbol **p;
00491   asection *s;
00492 
00493   if (limit == 0)
00494     {
00495       /* This may be from the backend linker, in which case the
00496          lineno_count in the sections is correct.  */
00497       for (s = abfd->sections; s != NULL; s = s->next)
00498        total += s->lineno_count;
00499       return total;
00500     }
00501 
00502   for (s = abfd->sections; s != NULL; s = s->next)
00503     BFD_ASSERT (s->lineno_count == 0);
00504 
00505   for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
00506     {
00507       asymbol *q_maybe = *p;
00508 
00509       if (bfd_family_coff (bfd_asymbol_bfd (q_maybe)))
00510        {
00511          coff_symbol_type *q = coffsymbol (q_maybe);
00512 
00513          /* The AIX 4.1 compiler can sometimes generate line numbers
00514              attached to debugging symbols.  We try to simply ignore
00515              those here.  */
00516          if (q->lineno != NULL
00517              && q->symbol.section->owner != NULL)
00518            {
00519              /* This symbol has line numbers.  Increment the owning
00520                 section's linenumber count.  */
00521              alent *l = q->lineno;
00522 
00523              do
00524               {
00525                 asection * sec = q->symbol.section->output_section;
00526 
00527                 /* Do not try to update fields in read-only sections.  */
00528                 if (! bfd_is_const_section (sec))
00529                   sec->lineno_count ++;
00530 
00531                 ++total;
00532                 ++l;
00533               }
00534              while (l->line_number != 0);
00535            }
00536        }
00537     }
00538 
00539   return total;
00540 }
00541 
00542 /* Takes a bfd and a symbol, returns a pointer to the coff specific
00543    area of the symbol if there is one.  */
00544 
00545 coff_symbol_type *
00546 coff_symbol_from (bfd *ignore_abfd ATTRIBUTE_UNUSED,
00547                 asymbol *symbol)
00548 {
00549   if (!bfd_family_coff (bfd_asymbol_bfd (symbol)))
00550     return (coff_symbol_type *) NULL;
00551 
00552   if (bfd_asymbol_bfd (symbol)->tdata.coff_obj_data == (coff_data_type *) NULL)
00553     return (coff_symbol_type *) NULL;
00554 
00555   return (coff_symbol_type *) symbol;
00556 }
00557 
00558 static void
00559 fixup_symbol_value (bfd *abfd,
00560                   coff_symbol_type *coff_symbol_ptr,
00561                   struct internal_syment *syment)
00562 {
00563   /* Normalize the symbol flags.  */
00564   if (bfd_is_com_section (coff_symbol_ptr->symbol.section))
00565     {
00566       /* A common symbol is undefined with a value.  */
00567       syment->n_scnum = N_UNDEF;
00568       syment->n_value = coff_symbol_ptr->symbol.value;
00569     }
00570   else if ((coff_symbol_ptr->symbol.flags & BSF_DEBUGGING) != 0
00571           && (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING_RELOC) == 0)
00572     {
00573       syment->n_value = coff_symbol_ptr->symbol.value;
00574     }
00575   else if (bfd_is_und_section (coff_symbol_ptr->symbol.section))
00576     {
00577       syment->n_scnum = N_UNDEF;
00578       syment->n_value = 0;
00579     }
00580   /* FIXME: Do we need to handle the absolute section here?  */
00581   else
00582     {
00583       if (coff_symbol_ptr->symbol.section)
00584        {
00585          syment->n_scnum =
00586            coff_symbol_ptr->symbol.section->output_section->target_index;
00587 
00588          syment->n_value = (coff_symbol_ptr->symbol.value
00589                           + coff_symbol_ptr->symbol.section->output_offset);
00590          if (! obj_pe (abfd))
00591             {
00592               syment->n_value += (syment->n_sclass == C_STATLAB)
00593                 ? coff_symbol_ptr->symbol.section->output_section->lma
00594                 : coff_symbol_ptr->symbol.section->output_section->vma;
00595             }
00596        }
00597       else
00598        {
00599          BFD_ASSERT (0);
00600          /* This can happen, but I don't know why yet (steve@cygnus.com) */
00601          syment->n_scnum = N_ABS;
00602          syment->n_value = coff_symbol_ptr->symbol.value;
00603        }
00604     }
00605 }
00606 
00607 /* Run through all the symbols in the symbol table and work out what
00608    their indexes into the symbol table will be when output.
00609 
00610    Coff requires that each C_FILE symbol points to the next one in the
00611    chain, and that the last one points to the first external symbol. We
00612    do that here too.  */
00613 
00614 bfd_boolean
00615 coff_renumber_symbols (bfd *bfd_ptr, int *first_undef)
00616 {
00617   unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
00618   asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
00619   unsigned int native_index = 0;
00620   struct internal_syment *last_file = NULL;
00621   unsigned int symbol_index;
00622 
00623   /* COFF demands that undefined symbols come after all other symbols.
00624      Since we don't need to impose this extra knowledge on all our
00625      client programs, deal with that here.  Sort the symbol table;
00626      just move the undefined symbols to the end, leaving the rest
00627      alone.  The O'Reilly book says that defined global symbols come
00628      at the end before the undefined symbols, so we do that here as
00629      well.  */
00630   /* @@ Do we have some condition we could test for, so we don't always
00631      have to do this?  I don't think relocatability is quite right, but
00632      I'm not certain.  [raeburn:19920508.1711EST]  */
00633   {
00634     asymbol **newsyms;
00635     unsigned int i;
00636     bfd_size_type amt;
00637 
00638     amt = sizeof (asymbol *) * ((bfd_size_type) symbol_count + 1);
00639     newsyms = bfd_alloc (bfd_ptr, amt);
00640     if (!newsyms)
00641       return FALSE;
00642     bfd_ptr->outsymbols = newsyms;
00643     for (i = 0; i < symbol_count; i++)
00644       if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) != 0
00645          || (!bfd_is_und_section (symbol_ptr_ptr[i]->section)
00646              && !bfd_is_com_section (symbol_ptr_ptr[i]->section)
00647              && ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) != 0
00648                 || ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK))
00649                     == 0))))
00650        *newsyms++ = symbol_ptr_ptr[i];
00651 
00652     for (i = 0; i < symbol_count; i++)
00653       if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
00654          && !bfd_is_und_section (symbol_ptr_ptr[i]->section)
00655          && (bfd_is_com_section (symbol_ptr_ptr[i]->section)
00656              || ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) == 0
00657                 && ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK))
00658                     != 0))))
00659        *newsyms++ = symbol_ptr_ptr[i];
00660 
00661     *first_undef = newsyms - bfd_ptr->outsymbols;
00662 
00663     for (i = 0; i < symbol_count; i++)
00664       if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
00665          && bfd_is_und_section (symbol_ptr_ptr[i]->section))
00666        *newsyms++ = symbol_ptr_ptr[i];
00667     *newsyms = (asymbol *) NULL;
00668     symbol_ptr_ptr = bfd_ptr->outsymbols;
00669   }
00670 
00671   for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
00672     {
00673       coff_symbol_type *coff_symbol_ptr = coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]);
00674       symbol_ptr_ptr[symbol_index]->udata.i = symbol_index;
00675       if (coff_symbol_ptr && coff_symbol_ptr->native)
00676        {
00677          combined_entry_type *s = coff_symbol_ptr->native;
00678          int i;
00679 
00680          if (s->u.syment.n_sclass == C_FILE)
00681            {
00682              if (last_file != NULL)
00683               last_file->n_value = native_index;
00684              last_file = &(s->u.syment);
00685            }
00686          else
00687            /* Modify the symbol values according to their section and
00688               type.  */
00689            fixup_symbol_value (bfd_ptr, coff_symbol_ptr, &(s->u.syment));
00690 
00691          for (i = 0; i < s->u.syment.n_numaux + 1; i++)
00692            s[i].offset = native_index++;
00693        }
00694       else
00695        native_index++;
00696     }
00697 
00698   obj_conv_table_size (bfd_ptr) = native_index;
00699 
00700   return TRUE;
00701 }
00702 
00703 /* Run thorough the symbol table again, and fix it so that all
00704    pointers to entries are changed to the entries' index in the output
00705    symbol table.  */
00706 
00707 void
00708 coff_mangle_symbols (bfd *bfd_ptr)
00709 {
00710   unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
00711   asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
00712   unsigned int symbol_index;
00713 
00714   for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
00715     {
00716       coff_symbol_type *coff_symbol_ptr =
00717       coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]);
00718 
00719       if (coff_symbol_ptr && coff_symbol_ptr->native)
00720        {
00721          int i;
00722          combined_entry_type *s = coff_symbol_ptr->native;
00723 
00724          if (s->fix_value)
00725            {
00726              /* FIXME: We should use a union here.  */
00727              s->u.syment.n_value =
00728               (bfd_vma)((combined_entry_type *)
00729                        ((unsigned long) s->u.syment.n_value))->offset;
00730              s->fix_value = 0;
00731            }
00732          if (s->fix_line)
00733            {
00734              /* The value is the offset into the line number entries
00735                  for the symbol's section.  On output, the symbol's
00736                  section should be N_DEBUG.  */
00737              s->u.syment.n_value =
00738               (coff_symbol_ptr->symbol.section->output_section->line_filepos
00739                + s->u.syment.n_value * bfd_coff_linesz (bfd_ptr));
00740              coff_symbol_ptr->symbol.section =
00741               coff_section_from_bfd_index (bfd_ptr, N_DEBUG);
00742              BFD_ASSERT (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING);
00743            }
00744          for (i = 0; i < s->u.syment.n_numaux; i++)
00745            {
00746              combined_entry_type *a = s + i + 1;
00747              if (a->fix_tag)
00748               {
00749                 a->u.auxent.x_sym.x_tagndx.l =
00750                   a->u.auxent.x_sym.x_tagndx.p->offset;
00751                 a->fix_tag = 0;
00752               }
00753              if (a->fix_end)
00754               {
00755                 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l =
00756                   a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p->offset;
00757                 a->fix_end = 0;
00758               }
00759              if (a->fix_scnlen)
00760               {
00761                 a->u.auxent.x_csect.x_scnlen.l =
00762                   a->u.auxent.x_csect.x_scnlen.p->offset;
00763                 a->fix_scnlen = 0;
00764               }
00765            }
00766        }
00767     }
00768 }
00769 
00770 static void
00771 coff_fix_symbol_name (bfd *abfd,
00772                     asymbol *symbol,
00773                     combined_entry_type *native,
00774                     bfd_size_type *string_size_p,
00775                     asection **debug_string_section_p,
00776                     bfd_size_type *debug_string_size_p)
00777 {
00778   unsigned int name_length;
00779   union internal_auxent *auxent;
00780   char *name = (char *) (symbol->name);
00781 
00782   if (name == NULL)
00783     {
00784       /* COFF symbols always have names, so we'll make one up.  */
00785       symbol->name = "strange";
00786       name = (char *) symbol->name;
00787     }
00788   name_length = strlen (name);
00789 
00790   if (native->u.syment.n_sclass == C_FILE
00791       && native->u.syment.n_numaux > 0)
00792     {
00793       unsigned int filnmlen;
00794 
00795       if (bfd_coff_force_symnames_in_strings (abfd))
00796        {
00797           native->u.syment._n._n_n._n_offset =
00798              (*string_size_p + STRING_SIZE_SIZE);
00799          native->u.syment._n._n_n._n_zeroes = 0;
00800          *string_size_p += 6;  /* strlen(".file") + 1 */
00801        }
00802       else
00803        strncpy (native->u.syment._n._n_name, ".file", SYMNMLEN);
00804 
00805       auxent = &(native + 1)->u.auxent;
00806 
00807       filnmlen = bfd_coff_filnmlen (abfd);
00808 
00809       if (bfd_coff_long_filenames (abfd))
00810        {
00811          if (name_length <= filnmlen)
00812            strncpy (auxent->x_file.x_fname, name, filnmlen);
00813          else
00814            {
00815              auxent->x_file.x_n.x_offset = *string_size_p + STRING_SIZE_SIZE;
00816              auxent->x_file.x_n.x_zeroes = 0;
00817              *string_size_p += name_length + 1;
00818            }
00819        }
00820       else
00821        {
00822          strncpy (auxent->x_file.x_fname, name, filnmlen);
00823          if (name_length > filnmlen)
00824            name[filnmlen] = '\0';
00825        }
00826     }
00827   else
00828     {
00829       if (name_length <= SYMNMLEN && !bfd_coff_force_symnames_in_strings (abfd))
00830        /* This name will fit into the symbol neatly.  */
00831        strncpy (native->u.syment._n._n_name, symbol->name, SYMNMLEN);
00832 
00833       else if (!bfd_coff_symname_in_debug (abfd, &native->u.syment))
00834        {
00835          native->u.syment._n._n_n._n_offset = (*string_size_p
00836                                           + STRING_SIZE_SIZE);
00837          native->u.syment._n._n_n._n_zeroes = 0;
00838          *string_size_p += name_length + 1;
00839        }
00840       else
00841        {
00842          file_ptr filepos;
00843          bfd_byte buf[4];
00844          int prefix_len = bfd_coff_debug_string_prefix_length (abfd);
00845 
00846          /* This name should be written into the .debug section.  For
00847             some reason each name is preceded by a two byte length
00848             and also followed by a null byte.  FIXME: We assume that
00849             the .debug section has already been created, and that it
00850             is large enough.  */
00851          if (*debug_string_section_p == (asection *) NULL)
00852            *debug_string_section_p = bfd_get_section_by_name (abfd, ".debug");
00853          filepos = bfd_tell (abfd);
00854          if (prefix_len == 4)
00855            bfd_put_32 (abfd, (bfd_vma) (name_length + 1), buf);
00856          else
00857            bfd_put_16 (abfd, (bfd_vma) (name_length + 1), buf);
00858 
00859          if (!bfd_set_section_contents (abfd,
00860                                     *debug_string_section_p,
00861                                     (void *) buf,
00862                                     (file_ptr) *debug_string_size_p,
00863                                     (bfd_size_type) prefix_len)
00864              || !bfd_set_section_contents (abfd,
00865                                        *debug_string_section_p,
00866                                        (void *) symbol->name,
00867                                        (file_ptr) (*debug_string_size_p
00868                                                  + prefix_len),
00869                                        (bfd_size_type) name_length + 1))
00870            abort ();
00871          if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
00872            abort ();
00873          native->u.syment._n._n_n._n_offset =
00874              *debug_string_size_p + prefix_len;
00875          native->u.syment._n._n_n._n_zeroes = 0;
00876          *debug_string_size_p += name_length + 1 + prefix_len;
00877        }
00878     }
00879 }
00880 
00881 /* We need to keep track of the symbol index so that when we write out
00882    the relocs we can get the index for a symbol.  This method is a
00883    hack.  FIXME.  */
00884 
00885 #define set_index(symbol, idx)     ((symbol)->udata.i = (idx))
00886 
00887 /* Write a symbol out to a COFF file.  */
00888 
00889 static bfd_boolean
00890 coff_write_symbol (bfd *abfd,
00891                  asymbol *symbol,
00892                  combined_entry_type *native,
00893                  bfd_vma *written,
00894                  bfd_size_type *string_size_p,
00895                  asection **debug_string_section_p,
00896                  bfd_size_type *debug_string_size_p)
00897 {
00898   unsigned int numaux = native->u.syment.n_numaux;
00899   int type = native->u.syment.n_type;
00900   int class = native->u.syment.n_sclass;
00901   void * buf;
00902   bfd_size_type symesz;
00903 
00904   if (native->u.syment.n_sclass == C_FILE)
00905     symbol->flags |= BSF_DEBUGGING;
00906 
00907   if (symbol->flags & BSF_DEBUGGING
00908       && bfd_is_abs_section (symbol->section))
00909     native->u.syment.n_scnum = N_DEBUG;
00910 
00911   else if (bfd_is_abs_section (symbol->section))
00912     native->u.syment.n_scnum = N_ABS;
00913 
00914   else if (bfd_is_und_section (symbol->section))
00915     native->u.syment.n_scnum = N_UNDEF;
00916 
00917   else
00918     native->u.syment.n_scnum =
00919       symbol->section->output_section->target_index;
00920 
00921   coff_fix_symbol_name (abfd, symbol, native, string_size_p,
00922                      debug_string_section_p, debug_string_size_p);
00923 
00924   symesz = bfd_coff_symesz (abfd);
00925   buf = bfd_alloc (abfd, symesz);
00926   if (!buf)
00927     return FALSE;
00928   bfd_coff_swap_sym_out (abfd, &native->u.syment, buf);
00929   if (bfd_bwrite (buf, symesz, abfd) != symesz)
00930     return FALSE;
00931   bfd_release (abfd, buf);
00932 
00933   if (native->u.syment.n_numaux > 0)
00934     {
00935       bfd_size_type auxesz;
00936       unsigned int j;
00937 
00938       auxesz = bfd_coff_auxesz (abfd);
00939       buf = bfd_alloc (abfd, auxesz);
00940       if (!buf)
00941        return FALSE;
00942       for (j = 0; j < native->u.syment.n_numaux; j++)
00943        {
00944          bfd_coff_swap_aux_out (abfd,
00945                              &((native + j + 1)->u.auxent),
00946                              type, class, (int) j,
00947                              native->u.syment.n_numaux,
00948                              buf);
00949          if (bfd_bwrite (buf, auxesz, abfd) != auxesz)
00950            return FALSE;
00951        }
00952       bfd_release (abfd, buf);
00953     }
00954 
00955   /* Store the index for use when we write out the relocs.  */
00956   set_index (symbol, *written);
00957 
00958   *written += numaux + 1;
00959   return TRUE;
00960 }
00961 
00962 /* Write out a symbol to a COFF file that does not come from a COFF
00963    file originally.  This symbol may have been created by the linker,
00964    or we may be linking a non COFF file to a COFF file.  */
00965 
00966 static bfd_boolean
00967 coff_write_alien_symbol (bfd *abfd,
00968                       asymbol *symbol,
00969                       bfd_vma *written,
00970                       bfd_size_type *string_size_p,
00971                       asection **debug_string_section_p,
00972                       bfd_size_type *debug_string_size_p)
00973 {
00974   combined_entry_type *native;
00975   combined_entry_type dummy;
00976 
00977   native = &dummy;
00978   native->u.syment.n_type = T_NULL;
00979   native->u.syment.n_flags = 0;
00980   if (bfd_is_und_section (symbol->section))
00981     {
00982       native->u.syment.n_scnum = N_UNDEF;
00983       native->u.syment.n_value = symbol->value;
00984     }
00985   else if (bfd_is_com_section (symbol->section))
00986     {
00987       native->u.syment.n_scnum = N_UNDEF;
00988       native->u.syment.n_value = symbol->value;
00989     }
00990   else if (symbol->flags & BSF_DEBUGGING)
00991     {
00992       /* There isn't much point to writing out a debugging symbol
00993          unless we are prepared to convert it into COFF debugging
00994          format.  So, we just ignore them.  We must clobber the symbol
00995          name to keep it from being put in the string table.  */
00996       symbol->name = "";
00997       return TRUE;
00998     }
00999   else
01000     {
01001       native->u.syment.n_scnum =
01002        symbol->section->output_section->target_index;
01003       native->u.syment.n_value = (symbol->value
01004                               + symbol->section->output_offset);
01005       if (! obj_pe (abfd))
01006        native->u.syment.n_value += symbol->section->output_section->vma;
01007 
01008       /* Copy the any flags from the file header into the symbol.
01009          FIXME: Why?  */
01010       {
01011        coff_symbol_type *c = coff_symbol_from (abfd, symbol);
01012        if (c != (coff_symbol_type *) NULL)
01013          native->u.syment.n_flags = bfd_asymbol_bfd (&c->symbol)->flags;
01014       }
01015     }
01016 
01017   native->u.syment.n_type = 0;
01018   if (symbol->flags & BSF_LOCAL)
01019     native->u.syment.n_sclass = C_STAT;
01020   else if (symbol->flags & BSF_WEAK)
01021     native->u.syment.n_sclass = obj_pe (abfd) ? C_NT_WEAK : C_WEAKEXT;
01022   else
01023     native->u.syment.n_sclass = C_EXT;
01024   native->u.syment.n_numaux = 0;
01025 
01026   return coff_write_symbol (abfd, symbol, native, written, string_size_p,
01027                          debug_string_section_p, debug_string_size_p);
01028 }
01029 
01030 /* Write a native symbol to a COFF file.  */
01031 
01032 static bfd_boolean
01033 coff_write_native_symbol (bfd *abfd,
01034                        coff_symbol_type *symbol,
01035                        bfd_vma *written,
01036                        bfd_size_type *string_size_p,
01037                        asection **debug_string_section_p,
01038                        bfd_size_type *debug_string_size_p)
01039 {
01040   combined_entry_type *native = symbol->native;
01041   alent *lineno = symbol->lineno;
01042 
01043   /* If this symbol has an associated line number, we must store the
01044      symbol index in the line number field.  We also tag the auxent to
01045      point to the right place in the lineno table.  */
01046   if (lineno && !symbol->done_lineno && symbol->symbol.section->owner != NULL)
01047     {
01048       unsigned int count = 0;
01049 
01050       lineno[count].u.offset = *written;
01051       if (native->u.syment.n_numaux)
01052        {
01053          union internal_auxent *a = &((native + 1)->u.auxent);
01054 
01055          a->x_sym.x_fcnary.x_fcn.x_lnnoptr =
01056            symbol->symbol.section->output_section->moving_line_filepos;
01057        }
01058 
01059       /* Count and relocate all other linenumbers.  */
01060       count++;
01061       while (lineno[count].line_number != 0)
01062        {
01063          lineno[count].u.offset +=
01064            (symbol->symbol.section->output_section->vma
01065             + symbol->symbol.section->output_offset);
01066          count++;
01067        }
01068       symbol->done_lineno = TRUE;
01069 
01070       if (! bfd_is_const_section (symbol->symbol.section->output_section))
01071        symbol->symbol.section->output_section->moving_line_filepos +=
01072          count * bfd_coff_linesz (abfd);
01073     }
01074 
01075   return coff_write_symbol (abfd, &(symbol->symbol), native, written,
01076                          string_size_p, debug_string_section_p,
01077                          debug_string_size_p);
01078 }
01079 
01080 /* Write out the COFF symbols.  */
01081 
01082 bfd_boolean
01083 coff_write_symbols (bfd *abfd)
01084 {
01085   bfd_size_type string_size;
01086   asection *debug_string_section;
01087   bfd_size_type debug_string_size;
01088   unsigned int i;
01089   unsigned int limit = bfd_get_symcount (abfd);
01090   bfd_vma written = 0;
01091   asymbol **p;
01092 
01093   string_size = 0;
01094   debug_string_section = NULL;
01095   debug_string_size = 0;
01096 
01097   /* If this target supports long section names, they must be put into
01098      the string table.  This is supported by PE.  This code must
01099      handle section names just as they are handled in
01100      coff_write_object_contents.  */
01101   if (bfd_coff_long_section_names (abfd))
01102     {
01103       asection *o;
01104 
01105       for (o = abfd->sections; o != NULL; o = o->next)
01106        {
01107          size_t len;
01108 
01109          len = strlen (o->name);
01110          if (len > SCNNMLEN)
01111            string_size += len + 1;
01112        }
01113     }
01114 
01115   /* Seek to the right place.  */
01116   if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0)
01117     return FALSE;
01118 
01119   /* Output all the symbols we have.  */
01120   written = 0;
01121   for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
01122     {
01123       asymbol *symbol = *p;
01124       coff_symbol_type *c_symbol = coff_symbol_from (abfd, symbol);
01125 
01126       if (c_symbol == (coff_symbol_type *) NULL
01127          || c_symbol->native == (combined_entry_type *) NULL)
01128        {
01129          if (!coff_write_alien_symbol (abfd, symbol, &written, &string_size,
01130                                    &debug_string_section,
01131                                    &debug_string_size))
01132            return FALSE;
01133        }
01134       else
01135        {
01136          if (!coff_write_native_symbol (abfd, c_symbol, &written,
01137                                     &string_size, &debug_string_section,
01138                                     &debug_string_size))
01139            return FALSE;
01140        }
01141     }
01142 
01143   obj_raw_syment_count (abfd) = written;
01144 
01145   /* Now write out strings.  */
01146   if (string_size != 0)
01147     {
01148       unsigned int size = string_size + STRING_SIZE_SIZE;
01149       bfd_byte buffer[STRING_SIZE_SIZE];
01150 
01151 #if STRING_SIZE_SIZE == 4
01152       H_PUT_32 (abfd, size, buffer);
01153 #else
01154  #error Change H_PUT_32
01155 #endif
01156       if (bfd_bwrite ((void *) buffer, (bfd_size_type) sizeof (buffer), abfd)
01157          != sizeof (buffer))
01158        return FALSE;
01159 
01160       /* Handle long section names.  This code must handle section
01161         names just as they are handled in coff_write_object_contents.  */
01162       if (bfd_coff_long_section_names (abfd))
01163        {
01164          asection *o;
01165 
01166          for (o = abfd->sections; o != NULL; o = o->next)
01167            {
01168              size_t len;
01169 
01170              len = strlen (o->name);
01171              if (len > SCNNMLEN)
01172               {
01173                 if (bfd_bwrite (o->name, (bfd_size_type) (len + 1), abfd)
01174                     != len + 1)
01175                   return FALSE;
01176               }
01177            }
01178        }
01179 
01180       for (p = abfd->outsymbols, i = 0;
01181           i < limit;
01182           i++, p++)
01183        {
01184          asymbol *q = *p;
01185          size_t name_length = strlen (q->name);
01186          coff_symbol_type *c_symbol = coff_symbol_from (abfd, q);
01187          size_t maxlen;
01188 
01189          /* Figure out whether the symbol name should go in the string
01190             table.  Symbol names that are short enough are stored
01191             directly in the syment structure.  File names permit a
01192             different, longer, length in the syment structure.  On
01193             XCOFF, some symbol names are stored in the .debug section
01194             rather than in the string table.  */
01195 
01196          if (c_symbol == NULL
01197              || c_symbol->native == NULL)
01198            /* This is not a COFF symbol, so it certainly is not a
01199               file name, nor does it go in the .debug section.  */
01200            maxlen = bfd_coff_force_symnames_in_strings (abfd) ? 0 : SYMNMLEN;
01201 
01202          else if (bfd_coff_symname_in_debug (abfd,
01203                                          &c_symbol->native->u.syment))
01204            /* This symbol name is in the XCOFF .debug section.
01205               Don't write it into the string table.  */
01206            maxlen = name_length;
01207 
01208          else if (c_symbol->native->u.syment.n_sclass == C_FILE
01209                  && c_symbol->native->u.syment.n_numaux > 0)
01210            {
01211              if (bfd_coff_force_symnames_in_strings (abfd))
01212               {
01213                 if (bfd_bwrite (".file", (bfd_size_type) 6, abfd) != 6)
01214                   return FALSE;
01215               }
01216              maxlen = bfd_coff_filnmlen (abfd);
01217            }
01218          else
01219            maxlen = bfd_coff_force_symnames_in_strings (abfd) ? 0 : SYMNMLEN;
01220 
01221          if (name_length > maxlen)
01222            {
01223              if (bfd_bwrite ((void *) (q->name), (bfd_size_type) name_length + 1,
01224                           abfd) != name_length + 1)
01225               return FALSE;
01226            }
01227        }
01228     }
01229   else
01230     {
01231       /* We would normally not write anything here, but we'll write
01232          out 4 so that any stupid coff reader which tries to read the
01233          string table even when there isn't one won't croak.  */
01234       unsigned int size = STRING_SIZE_SIZE;
01235       bfd_byte buffer[STRING_SIZE_SIZE];
01236 
01237 #if STRING_SIZE_SIZE == 4
01238       H_PUT_32 (abfd, size, buffer);
01239 #else
01240  #error Change H_PUT_32
01241 #endif
01242       if (bfd_bwrite ((void *) buffer, (bfd_size_type) STRING_SIZE_SIZE, abfd)
01243          != STRING_SIZE_SIZE)
01244        return FALSE;
01245     }
01246 
01247   /* Make sure the .debug section was created to be the correct size.
01248      We should create it ourselves on the fly, but we don't because
01249      BFD won't let us write to any section until we know how large all
01250      the sections are.  We could still do it by making another pass
01251      over the symbols.  FIXME.  */
01252   BFD_ASSERT (debug_string_size == 0
01253              || (debug_string_section != (asection *) NULL
01254                 && (BFD_ALIGN (debug_string_size,
01255                              1 << debug_string_section->alignment_power)
01256                     == debug_string_section->size)));
01257 
01258   return TRUE;
01259 }
01260 
01261 bfd_boolean
01262 coff_write_linenumbers (bfd *abfd)
01263 {
01264   asection *s;
01265   bfd_size_type linesz;
01266   void * buff;
01267 
01268   linesz = bfd_coff_linesz (abfd);
01269   buff = bfd_alloc (abfd, linesz);
01270   if (!buff)
01271     return FALSE;
01272   for (s = abfd->sections; s != (asection *) NULL; s = s->next)
01273     {
01274       if (s->lineno_count)
01275        {
01276          asymbol **q = abfd->outsymbols;
01277          if (bfd_seek (abfd, s->line_filepos, SEEK_SET) != 0)
01278            return FALSE;
01279          /* Find all the linenumbers in this section.  */
01280          while (*q)
01281            {
01282              asymbol *p = *q;
01283              if (p->section->output_section == s)
01284               {
01285                 alent *l =
01286                 BFD_SEND (bfd_asymbol_bfd (p), _get_lineno,
01287                          (bfd_asymbol_bfd (p), p));
01288                 if (l)
01289                   {
01290                     /* Found a linenumber entry, output.  */
01291                     struct internal_lineno out;
01292                     memset ((void *) & out, 0, sizeof (out));
01293                     out.l_lnno = 0;
01294                     out.l_addr.l_symndx = l->u.offset;
01295                     bfd_coff_swap_lineno_out (abfd, &out, buff);
01296                     if (bfd_bwrite (buff, (bfd_size_type) linesz, abfd)
01297                        != linesz)
01298                      return FALSE;
01299                     l++;
01300                     while (l->line_number)
01301                      {
01302                        out.l_lnno = l->line_number;
01303                        out.l_addr.l_symndx = l->u.offset;
01304                        bfd_coff_swap_lineno_out (abfd, &out, buff);
01305                        if (bfd_bwrite (buff, (bfd_size_type) linesz, abfd)
01306                            != linesz)
01307                          return FALSE;
01308                        l++;
01309                      }
01310                   }
01311               }
01312              q++;
01313            }
01314        }
01315     }
01316   bfd_release (abfd, buff);
01317   return TRUE;
01318 }
01319 
01320 alent *
01321 coff_get_lineno (bfd *ignore_abfd ATTRIBUTE_UNUSED, asymbol *symbol)
01322 {
01323   return coffsymbol (symbol)->lineno;
01324 }
01325 
01326 /* This function transforms the offsets into the symbol table into
01327    pointers to syments.  */
01328 
01329 static void
01330 coff_pointerize_aux (bfd *abfd,
01331                    combined_entry_type *table_base,
01332                    combined_entry_type *symbol,
01333                    unsigned int indaux,
01334                    combined_entry_type *auxent)
01335 {
01336   unsigned int type = symbol->u.syment.n_type;
01337   unsigned int class = symbol->u.syment.n_sclass;
01338 
01339   if (coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
01340     {
01341       if ((*coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
01342          (abfd, table_base, symbol, indaux, auxent))
01343        return;
01344     }
01345 
01346   /* Don't bother if this is a file or a section.  */
01347   if (class == C_STAT && type == T_NULL)
01348     return;
01349   if (class == C_FILE)
01350     return;
01351 
01352   /* Otherwise patch up.  */
01353 #define N_TMASK coff_data  (abfd)->local_n_tmask
01354 #define N_BTSHFT coff_data (abfd)->local_n_btshft
01355   
01356   if ((ISFCN (type) || ISTAG (class) || class == C_BLOCK || class == C_FCN)
01357       && auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l > 0)
01358     {
01359       auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p =
01360        table_base + auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l;
01361       auxent->fix_end = 1;
01362     }
01363   /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can
01364      generate one, so we must be careful to ignore it.  */
01365   if (auxent->u.auxent.x_sym.x_tagndx.l > 0)
01366     {
01367       auxent->u.auxent.x_sym.x_tagndx.p =
01368        table_base + auxent->u.auxent.x_sym.x_tagndx.l;
01369       auxent->fix_tag = 1;
01370     }
01371 }
01372 
01373 /* Allocate space for the ".debug" section, and read it.
01374    We did not read the debug section until now, because
01375    we didn't want to go to the trouble until someone needed it.  */
01376 
01377 static char *
01378 build_debug_section (bfd *abfd)
01379 {
01380   char *debug_section;
01381   file_ptr position;
01382   bfd_size_type sec_size;
01383 
01384   asection *sect = bfd_get_section_by_name (abfd, ".debug");
01385 
01386   if (!sect)
01387     {
01388       bfd_set_error (bfd_error_no_debug_section);
01389       return NULL;
01390     }
01391 
01392   sec_size = sect->size;
01393   debug_section = bfd_alloc (abfd, sec_size);
01394   if (debug_section == NULL)
01395     return NULL;
01396 
01397   /* Seek to the beginning of the `.debug' section and read it.
01398      Save the current position first; it is needed by our caller.
01399      Then read debug section and reset the file pointer.  */
01400 
01401   position = bfd_tell (abfd);
01402   if (bfd_seek (abfd, sect->filepos, SEEK_SET) != 0
01403       || bfd_bread (debug_section, sec_size, abfd) != sec_size
01404       || bfd_seek (abfd, position, SEEK_SET) != 0)
01405     return NULL;
01406   return debug_section;
01407 }
01408 
01409 /* Return a pointer to a malloc'd copy of 'name'.  'name' may not be
01410    \0-terminated, but will not exceed 'maxlen' characters.  The copy *will*
01411    be \0-terminated.  */
01412 
01413 static char *
01414 copy_name (bfd *abfd, char *name, size_t maxlen)
01415 {
01416   size_t len;
01417   char *newname;
01418 
01419   for (len = 0; len < maxlen; ++len)
01420     if (name[len] == '\0')
01421       break;
01422 
01423   if ((newname = bfd_alloc (abfd, (bfd_size_type) len + 1)) == NULL)
01424     return NULL;
01425 
01426   strncpy (newname, name, len);
01427   newname[len] = '\0';
01428   return newname;
01429 }
01430 
01431 /* Read in the external symbols.  */
01432 
01433 bfd_boolean
01434 _bfd_coff_get_external_symbols (bfd *abfd)
01435 {
01436   bfd_size_type symesz;
01437   bfd_size_type size;
01438   void * syms;
01439 
01440   if (obj_coff_external_syms (abfd) != NULL)
01441     return TRUE;
01442 
01443   symesz = bfd_coff_symesz (abfd);
01444 
01445   size = obj_raw_syment_count (abfd) * symesz;
01446 
01447   syms = bfd_malloc (size);
01448   if (syms == NULL && size != 0)
01449     return FALSE;
01450 
01451   if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0
01452       || bfd_bread (syms, size, abfd) != size)
01453     {
01454       if (syms != NULL)
01455        free (syms);
01456       return FALSE;
01457     }
01458 
01459   obj_coff_external_syms (abfd) = syms;
01460 
01461   return TRUE;
01462 }
01463 
01464 /* Read in the external strings.  The strings are not loaded until
01465    they are needed.  This is because we have no simple way of
01466    detecting a missing string table in an archive.  */
01467 
01468 const char *
01469 _bfd_coff_read_string_table (bfd *abfd)
01470 {
01471   char extstrsize[STRING_SIZE_SIZE];
01472   bfd_size_type strsize;
01473   char *strings;
01474   file_ptr pos;
01475 
01476   if (obj_coff_strings (abfd) != NULL)
01477     return obj_coff_strings (abfd);
01478 
01479   if (obj_sym_filepos (abfd) == 0)
01480     {
01481       bfd_set_error (bfd_error_no_symbols);
01482       return NULL;
01483     }
01484 
01485   pos = obj_sym_filepos (abfd);
01486   pos += obj_raw_syment_count (abfd) * bfd_coff_symesz (abfd);
01487   if (bfd_seek (abfd, pos, SEEK_SET) != 0)
01488     return NULL;
01489 
01490   if (bfd_bread (extstrsize, (bfd_size_type) sizeof extstrsize, abfd)
01491       != sizeof extstrsize)
01492     {
01493       if (bfd_get_error () != bfd_error_file_truncated)
01494        return NULL;
01495 
01496       /* There is no string table.  */
01497       strsize = STRING_SIZE_SIZE;
01498     }
01499   else
01500     {
01501 #if STRING_SIZE_SIZE == 4
01502       strsize = H_GET_32 (abfd, extstrsize);
01503 #else
01504  #error Change H_GET_32
01505 #endif
01506     }
01507 
01508   if (strsize < STRING_SIZE_SIZE)
01509     {
01510       (*_bfd_error_handler)
01511        (_("%B: bad string table size %lu"), abfd, (unsigned long) strsize);
01512       bfd_set_error (bfd_error_bad_value);
01513       return NULL;
01514     }
01515 
01516   strings = bfd_malloc (strsize);
01517   if (strings == NULL)
01518     return NULL;
01519 
01520   if (bfd_bread (strings + STRING_SIZE_SIZE, strsize - STRING_SIZE_SIZE, abfd)
01521       != strsize - STRING_SIZE_SIZE)
01522     {
01523       free (strings);
01524       return NULL;
01525     }
01526 
01527   obj_coff_strings (abfd) = strings;
01528 
01529   return strings;
01530 }
01531 
01532 /* Free up the external symbols and strings read from a COFF file.  */
01533 
01534 bfd_boolean
01535 _bfd_coff_free_symbols (bfd *abfd)
01536 {
01537   if (obj_coff_external_syms (abfd) != NULL
01538       && ! obj_coff_keep_syms (abfd))
01539     {
01540       free (obj_coff_external_syms (abfd));
01541       obj_coff_external_syms (abfd) = NULL;
01542     }
01543   if (obj_coff_strings (abfd) != NULL
01544       && ! obj_coff_keep_strings (abfd))
01545     {
01546       free (obj_coff_strings (abfd));
01547       obj_coff_strings (abfd) = NULL;
01548     }
01549   return TRUE;
01550 }
01551 
01552 /* Read a symbol table into freshly bfd_allocated memory, swap it, and
01553    knit the symbol names into a normalized form.  By normalized here I
01554    mean that all symbols have an n_offset pointer that points to a null-
01555    terminated string.  */
01556 
01557 combined_entry_type *
01558 coff_get_normalized_symtab (bfd *abfd)
01559 {
01560   combined_entry_type *internal;
01561   combined_entry_type *internal_ptr;
01562   combined_entry_type *symbol_ptr;
01563   combined_entry_type *internal_end;
01564   size_t symesz;
01565   char *raw_src;
01566   char *raw_end;
01567   const char *string_table = NULL;
01568   char *debug_section = NULL;
01569   bfd_size_type size;
01570 
01571   if (obj_raw_syments (abfd) != NULL)
01572     return obj_raw_syments (abfd);
01573 
01574   size = obj_raw_syment_count (abfd) * sizeof (combined_entry_type);
01575   internal = bfd_zalloc (abfd, size);
01576   if (internal == NULL && size != 0)
01577     return NULL;
01578   internal_end = internal + obj_raw_syment_count (abfd);
01579 
01580   if (! _bfd_coff_get_external_symbols (abfd))
01581     return NULL;
01582 
01583   raw_src = (char *) obj_coff_external_syms (abfd);
01584 
01585   /* Mark the end of the symbols.  */
01586   symesz = bfd_coff_symesz (abfd);
01587   raw_end = (char *) raw_src + obj_raw_syment_count (abfd) * symesz;
01588 
01589   /* FIXME SOMEDAY.  A string table size of zero is very weird, but
01590      probably possible.  If one shows up, it will probably kill us.  */
01591 
01592   /* Swap all the raw entries.  */
01593   for (internal_ptr = internal;
01594        raw_src < raw_end;
01595        raw_src += symesz, internal_ptr++)
01596     {
01597 
01598       unsigned int i;
01599       bfd_coff_swap_sym_in (abfd, (void *) raw_src,
01600                          (void *) & internal_ptr->u.syment);
01601       symbol_ptr = internal_ptr;
01602 
01603       for (i = 0;
01604           i < symbol_ptr->u.syment.n_numaux;
01605           i++)
01606        {
01607          internal_ptr++;
01608          raw_src += symesz;
01609          bfd_coff_swap_aux_in (abfd, (void *) raw_src,
01610                             symbol_ptr->u.syment.n_type,
01611                             symbol_ptr->u.syment.n_sclass,
01612                             (int) i, symbol_ptr->u.syment.n_numaux,
01613                             &(internal_ptr->u.auxent));
01614          coff_pointerize_aux (abfd, internal, symbol_ptr, i,
01615                             internal_ptr);
01616        }
01617     }
01618 
01619   /* Free the raw symbols, but not the strings (if we have them).  */
01620   obj_coff_keep_strings (abfd) = TRUE;
01621   if (! _bfd_coff_free_symbols (abfd))
01622     return NULL;
01623 
01624   for (internal_ptr = internal; internal_ptr < internal_end;
01625        internal_ptr++)
01626     {
01627       if (internal_ptr->u.syment.n_sclass == C_FILE
01628          && internal_ptr->u.syment.n_numaux > 0)
01629        {
01630          /* Make a file symbol point to the name in the auxent, since
01631             the text ".file" is redundant.  */
01632          if ((internal_ptr + 1)->u.auxent.x_file.x_n.x_zeroes == 0)
01633            {
01634              /* The filename is a long one, point into the string table.  */
01635              if (string_table == NULL)
01636               {
01637                 string_table = _bfd_coff_read_string_table (abfd);
01638                 if (string_table == NULL)
01639                   return NULL;
01640               }
01641 
01642              internal_ptr->u.syment._n._n_n._n_offset =
01643               ((long)
01644                (string_table
01645                 + (internal_ptr + 1)->u.auxent.x_file.x_n.x_offset));
01646            }
01647          else
01648            {
01649              /* Ordinary short filename, put into memory anyway.  The
01650                  Microsoft PE tools sometimes store a filename in
01651                  multiple AUX entries.  */
01652              if (internal_ptr->u.syment.n_numaux > 1
01653                 && coff_data (abfd)->pe)
01654               internal_ptr->u.syment._n._n_n._n_offset =
01655                 ((long)
01656                  copy_name (abfd,
01657                            (internal_ptr + 1)->u.auxent.x_file.x_fname,
01658                            internal_ptr->u.syment.n_numaux * symesz));
01659              else
01660               internal_ptr->u.syment._n._n_n._n_offset =
01661                 ((long)
01662                  copy_name (abfd,
01663                            (internal_ptr + 1)->u.auxent.x_file.x_fname,
01664                            (size_t) bfd_coff_filnmlen (abfd)));
01665            }
01666        }
01667       else
01668        {
01669          if (internal_ptr->u.syment._n._n_n._n_zeroes != 0)
01670            {
01671              /* This is a "short" name.  Make it long.  */
01672              size_t i;
01673              char *newstring;
01674 
01675              /* Find the length of this string without walking into memory
01676                 that isn't ours.  */
01677              for (i = 0; i < 8; ++i)
01678               if (internal_ptr->u.syment._n._n_name[i] == '\0')
01679                 break;
01680 
01681              newstring = bfd_zalloc (abfd, (bfd_size_type) (i + 1));
01682              if (newstring == NULL)
01683               return NULL;
01684              strncpy (newstring, internal_ptr->u.syment._n._n_name, i);
01685              internal_ptr->u.syment._n._n_n._n_offset = (long int) newstring;
01686              internal_ptr->u.syment._n._n_n._n_zeroes = 0;
01687            }
01688          else if (internal_ptr->u.syment._n._n_n._n_offset == 0)
01689            internal_ptr->u.syment._n._n_n._n_offset = (long int) "";
01690          else if (!bfd_coff_symname_in_debug (abfd, &internal_ptr->u.syment))
01691            {
01692              /* Long name already.  Point symbol at the string in the
01693                  table.  */
01694              if (string_table == NULL)
01695               {
01696                 string_table = _bfd_coff_read_string_table (abfd);
01697                 if (string_table == NULL)
01698                   return NULL;
01699               }
01700              internal_ptr->u.syment._n._n_n._n_offset =
01701               ((long int)
01702                (string_table
01703                 + internal_ptr->u.syment._n._n_n._n_offset));
01704            }
01705          else
01706            {
01707              /* Long name in debug section.  Very similar.  */
01708              if (debug_section == NULL)
01709               debug_section = build_debug_section (abfd);
01710              internal_ptr->u.syment._n._n_n._n_offset = (long int)
01711               (debug_section + internal_ptr->u.syment._n._n_n._n_offset);
01712            }
01713        }
01714       internal_ptr += internal_ptr->u.syment.n_numaux;
01715     }
01716 
01717   obj_raw_syments (abfd) = internal;
01718   BFD_ASSERT (obj_raw_syment_count (abfd)
01719              == (unsigned int) (internal_ptr - internal));
01720 
01721   return internal;
01722 }
01723 
01724 long
01725 coff_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
01726 {
01727   if (bfd_get_format (abfd) != bfd_object)
01728     {
01729       bfd_set_error (bfd_error_invalid_operation);
01730       return -1;
01731     }
01732   return (asect->reloc_count + 1) * sizeof (arelent *);
01733 }
01734 
01735 asymbol *
01736 coff_make_empty_symbol (bfd *abfd)
01737 {
01738   bfd_size_type amt = sizeof (coff_symbol_type);
01739   coff_symbol_type *new = bfd_zalloc (abfd, amt);
01740 
01741   if (new == NULL)
01742     return NULL;
01743   new->symbol.section = 0;
01744   new->native = 0;
01745   new->lineno = NULL;
01746   new->done_lineno = FALSE;
01747   new->symbol.the_bfd = abfd;
01748 
01749   return & new->symbol;
01750 }
01751 
01752 /* Make a debugging symbol.  */
01753 
01754 asymbol *
01755 coff_bfd_make_debug_symbol (bfd *abfd,
01756                          void * ptr ATTRIBUTE_UNUSED,
01757                          unsigned long sz ATTRIBUTE_UNUSED)
01758 {
01759   bfd_size_type amt = sizeof (coff_symbol_type);
01760   coff_symbol_type *new = bfd_alloc (abfd, amt);
01761 
01762   if (new == NULL)
01763     return NULL;
01764   /* @@ The 10 is a guess at a plausible maximum number of aux entries
01765      (but shouldn't be a constant).  */
01766   amt = sizeof (combined_entry_type) * 10;
01767   new->native = bfd_zalloc (abfd, amt);
01768   if (!new->native)
01769     return NULL;
01770   new->symbol.section = bfd_abs_section_ptr;
01771   new->symbol.flags = BSF_DEBUGGING;
01772   new->lineno = NULL;
01773   new->done_lineno = FALSE;
01774   new->symbol.the_bfd = abfd;
01775   
01776   return & new->symbol;
01777 }
01778 
01779 void
01780 coff_get_symbol_info (bfd *abfd, asymbol *symbol, symbol_info *ret)
01781 {
01782   bfd_symbol_info (symbol, ret);
01783 
01784   if (coffsymbol (symbol)->native != NULL
01785       && coffsymbol (symbol)->native->fix_value)
01786     ret->value = coffsymbol (symbol)->native->u.syment.n_value -
01787       (unsigned long) obj_raw_syments (abfd);
01788 }
01789 
01790 /* Return the COFF syment for a symbol.  */
01791 
01792 bfd_boolean
01793 bfd_coff_get_syment (bfd *abfd,
01794                    asymbol *symbol,
01795                    struct internal_syment *psyment)
01796 {
01797   coff_symbol_type *csym;
01798 
01799   csym = coff_symbol_from (abfd, symbol);
01800   if (csym == NULL || csym->native == NULL)
01801     {
01802       bfd_set_error (bfd_error_invalid_operation);
01803       return FALSE;
01804     }
01805 
01806   *psyment = csym->native->u.syment;
01807 
01808   if (csym->native->fix_value)
01809     psyment->n_value = psyment->n_value -
01810       (unsigned long) obj_raw_syments (abfd);
01811 
01812   /* FIXME: We should handle fix_line here.  */
01813 
01814   return TRUE;
01815 }
01816 
01817 /* Return the COFF auxent for a symbol.  */
01818 
01819 bfd_boolean
01820 bfd_coff_get_auxent (bfd *abfd,
01821                    asymbol *symbol,
01822                    int indx,
01823                    union internal_auxent *pauxent)
01824 {
01825   coff_symbol_type *csym;
01826   combined_entry_type *ent;
01827 
01828   csym = coff_symbol_from (abfd, symbol);
01829 
01830   if (csym == NULL
01831       || csym->native == NULL
01832       || indx >= csym->native->u.syment.n_numaux)
01833     {
01834       bfd_set_error (bfd_error_invalid_operation);
01835       return FALSE;
01836     }
01837 
01838   ent = csym->native + indx + 1;
01839 
01840   *pauxent = ent->u.auxent;
01841 
01842   if (ent->fix_tag)
01843     pauxent->x_sym.x_tagndx.l =
01844       ((combined_entry_type *) pauxent->x_sym.x_tagndx.p
01845        - obj_raw_syments (abfd));
01846 
01847   if (ent->fix_end)
01848     pauxent->x_sym.x_fcnary.x_fcn.x_endndx.l =
01849       ((combined_entry_type *) pauxent->x_sym.x_fcnary.x_fcn.x_endndx.p
01850        - obj_raw_syments (abfd));
01851 
01852   if (ent->fix_scnlen)
01853     pauxent->x_csect.x_scnlen.l =
01854       ((combined_entry_type *) pauxent->x_csect.x_scnlen.p
01855        - obj_raw_syments (abfd));
01856 
01857   return TRUE;
01858 }
01859 
01860 /* Print out information about COFF symbol.  */
01861 
01862 void
01863 coff_print_symbol (bfd *abfd,
01864                  void * filep,
01865                  asymbol *symbol,
01866                  bfd_print_symbol_type how)
01867 {
01868   FILE * file = (FILE *) filep;
01869 
01870   switch (how)
01871     {
01872     case bfd_print_symbol_name:
01873       fprintf (file, "%s", symbol->name);
01874       break;
01875 
01876     case bfd_print_symbol_more:
01877       fprintf (file, "coff %s %s",
01878               coffsymbol (symbol)->native ? "n" : "g",
01879               coffsymbol (symbol)->lineno ? "l" : " ");
01880       break;
01881 
01882     case bfd_print_symbol_all:
01883       if (coffsymbol (symbol)->native)
01884        {
01885          bfd_vma val;
01886          unsigned int aux;
01887          combined_entry_type *combined = coffsymbol (symbol)->native;
01888          combined_entry_type *root = obj_raw_syments (abfd);
01889          struct lineno_cache_entry *l = coffsymbol (symbol)->lineno;
01890 
01891          fprintf (file, "[%3ld]", (long) (combined - root));
01892 
01893          if (! combined->fix_value)
01894            val = (bfd_vma) combined->u.syment.n_value;
01895          else
01896            val = combined->u.syment.n_value - (unsigned long) root;
01897 
01898          fprintf (file, "(sec %2d)(fl 0x%02x)(ty %3x)(scl %3d) (nx %d) 0x",
01899                  combined->u.syment.n_scnum,
01900                  combined->u.syment.n_flags,
01901                  combined->u.syment.n_type,
01902                  combined->u.syment.n_sclass,
01903                  combined->u.syment.n_numaux);
01904          fprintf_vma (file, val);
01905          fprintf (file, " %s", symbol->name);
01906 
01907          for (aux = 0; aux < combined->u.syment.n_numaux; aux++)
01908            {
01909              combined_entry_type *auxp = combined + aux + 1;
01910              long tagndx;
01911 
01912              if (auxp->fix_tag)
01913               tagndx = auxp->u.auxent.x_sym.x_tagndx.p - root;
01914              else
01915               tagndx = auxp->u.auxent.x_sym.x_tagndx.l;
01916 
01917              fprintf (file, "\n");
01918 
01919              if (bfd_coff_print_aux (abfd, file, root, combined, auxp, aux))
01920               continue;
01921 
01922              switch (combined->u.syment.n_sclass)
01923               {
01924               case C_FILE:
01925                 fprintf (file, "File ");
01926                 break;
01927 
01928               case C_STAT:
01929                 if (combined->u.syment.n_type == T_NULL)
01930                   /* Probably a section symbol ?  */
01931                   {
01932                     fprintf (file, "AUX scnlen 0x%lx nreloc %d nlnno %d",
01933                             (long) auxp->u.auxent.x_scn.x_scnlen,
01934                             auxp->u.auxent.x_scn.x_nreloc,
01935                             auxp->u.auxent.x_scn.x_nlinno);
01936                     if (auxp->u.auxent.x_scn.x_checksum != 0
01937                        || auxp->u.auxent.x_scn.x_associated != 0
01938                        || auxp->u.auxent.x_scn.x_comdat != 0)
01939                      fprintf (file, " checksum 0x%lx assoc %d comdat %d",
01940                              auxp->u.auxent.x_scn.x_checksum,
01941                              auxp->u.auxent.x_scn.x_associated,
01942                              auxp->u.auxent.x_scn.x_comdat);
01943                     break;
01944                   }
01945                   /* Otherwise fall through.  */
01946               case C_EXT:
01947                 if (ISFCN (combined->u.syment.n_type))
01948                   {
01949                     long next, llnos;
01950 
01951                     if (auxp->fix_end)
01952                      next = (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p
01953                             - root);
01954                     else
01955                      next = auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l;
01956                     llnos = auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_lnnoptr;
01957                     fprintf (file,
01958                             "AUX tagndx %ld ttlsiz 0x%lx lnnos %ld next %ld",
01959                             tagndx, auxp->u.auxent.x_sym.x_misc.x_fsize,
01960                             llnos, next);
01961                     break;
01962                   }
01963                 /* Otherwise fall through.  */
01964               default:
01965                 fprintf (file, "AUX lnno %d size 0x%x tagndx %ld",
01966                         auxp->u.auxent.x_sym.x_misc.x_lnsz.x_lnno,
01967                         auxp->u.auxent.x_sym.x_misc.x_lnsz.x_size,
01968                         tagndx);
01969                 if (auxp->fix_end)
01970                   fprintf (file, " endndx %ld",
01971                           ((long)
01972                            (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p
01973                             - root)));
01974                 break;
01975               }
01976            }
01977 
01978          if (l)
01979            {
01980              fprintf (file, "\n%s :", l->u.sym->name);
01981              l++;
01982              while (l->line_number)
01983               {
01984                 fprintf (file, "\n%4d : ", l->line_number);
01985                 fprintf_vma (file, l->u.offset + symbol->section->vma);
01986                 l++;
01987               }
01988            }
01989        }
01990       else
01991        {
01992          bfd_print_symbol_vandf (abfd, (void *) file, symbol);
01993          fprintf (file, " %-5s %s %s %s",
01994                  symbol->section->name,
01995                  coffsymbol (symbol)->native ? "n" : "g",
01996                  coffsymbol (symbol)->lineno ? "l" : " ",
01997                  symbol->name);
01998        }
01999     }
02000 }
02001 
02002 /* Return whether a symbol name implies a local symbol.  In COFF,
02003    local symbols generally start with ``.L''.  Most targets use this
02004    function for the is_local_label_name entry point, but some may
02005    override it.  */
02006 
02007 bfd_boolean
02008 _bfd_coff_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
02009                             const char *name)
02010 {
02011   return name[0] == '.' && name[1] == 'L';
02012 }
02013 
02014 /* Provided a BFD, a section and an offset (in bytes, not octets) into the
02015    section, calculate and return the name of the source file and the line
02016    nearest to the wanted location.  */
02017 
02018 bfd_boolean
02019 coff_find_nearest_line (bfd *abfd,
02020                      asection *section,
02021                      asymbol **symbols,
02022                      bfd_vma offset,
02023                      const char **filename_ptr,
02024                      const char **functionname_ptr,
02025                      unsigned int *line_ptr)
02026 {
02027   bfd_boolean found;
02028   unsigned int i;
02029   unsigned int line_base;
02030   coff_data_type *cof = coff_data (abfd);
02031   /* Run through the raw syments if available.  */
02032   combined_entry_type *p;
02033   combined_entry_type *pend;
02034   alent *l;
02035   struct coff_section_tdata *sec_data;
02036   bfd_size_type amt;
02037 
02038   /* Before looking through the symbol table, try to use a .stab
02039      section to find the information.  */
02040   if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
02041                                         &found, filename_ptr,
02042                                         functionname_ptr, line_ptr,
02043                                         &coff_data(abfd)->line_info))
02044     return FALSE;
02045 
02046   if (found)
02047     return TRUE;
02048 
02049   /* Also try examining DWARF2 debugging information.  */
02050   if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
02051                                  filename_ptr, functionname_ptr,
02052                                  line_ptr, 0,
02053                                  &coff_data(abfd)->dwarf2_find_line_info))
02054     return TRUE;
02055 
02056   *filename_ptr = 0;
02057   *functionname_ptr = 0;
02058   *line_ptr = 0;
02059 
02060   /* Don't try and find line numbers in a non coff file.  */
02061   if (!bfd_family_coff (abfd))
02062     return FALSE;
02063 
02064   if (cof == NULL)
02065     return FALSE;
02066 
02067   /* Find the first C_FILE symbol.  */
02068   p = cof->raw_syments;
02069   if (!p)
02070     return FALSE;
02071 
02072   pend = p + cof->raw_syment_count;
02073   while (p < pend)
02074     {
02075       if (p->u.syment.n_sclass == C_FILE)
02076        break;
02077       p += 1 + p->u.syment.n_numaux;
02078     }
02079 
02080   if (p < pend)
02081     {
02082       bfd_vma sec_vma;
02083       bfd_vma maxdiff;
02084 
02085       /* Look through the C_FILE symbols to find the best one.  */
02086       sec_vma = bfd_get_section_vma (abfd, section);
02087       *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
02088       maxdiff = (bfd_vma) 0 - (bfd_vma) 1;
02089       while (1)
02090        {
02091          combined_entry_type *p2;
02092 
02093          for (p2 = p + 1 + p->u.syment.n_numaux;
02094               p2 < pend;
02095               p2 += 1 + p2->u.syment.n_numaux)
02096            {
02097              if (p2->u.syment.n_scnum > 0
02098                 && (section
02099                     == coff_section_from_bfd_index (abfd,
02100                                                 p2->u.syment.n_scnum)))
02101               break;
02102              if (p2->u.syment.n_sclass == C_FILE)
02103               {
02104                 p2 = pend;
02105                 break;
02106               }
02107            }
02108 
02109          /* We use <= MAXDIFF here so that if we get a zero length
02110              file, we actually use the next file entry.  */
02111          if (p2 < pend
02112              && offset + sec_vma >= (bfd_vma) p2->u.syment.n_value
02113              && offset + sec_vma - (bfd_vma) p2->u.syment.n_value <= maxdiff)
02114            {
02115              *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
02116              maxdiff = offset + sec_vma - p2->u.syment.n_value;
02117            }
02118 
02119          /* Avoid endless loops on erroneous files by ensuring that
02120             we always move forward in the file.  */
02121          if (p >= cof->raw_syments + p->u.syment.n_value)
02122            break;
02123 
02124          p = cof->raw_syments + p->u.syment.n_value;
02125          if (p > pend || p->u.syment.n_sclass != C_FILE)
02126            break;
02127        }
02128     }
02129 
02130   /* Now wander though the raw linenumbers of the section.  */
02131   /* If we have been called on this section before, and th. e offset we
02132      want is further down then we can prime the lookup loop.  */
02133   sec_data = coff_section_data (abfd, section);
02134   if (sec_data != NULL
02135       && sec_data->i > 0
02136       && offset >= sec_data->offset)
02137     {
02138       i = sec_data->i;
02139       *functionname_ptr = sec_data->function;
02140       line_base = sec_data->line_base;
02141     }
02142   else
02143     {
02144       i = 0;
02145       line_base = 0;
02146     }
02147 
02148   if (section->lineno != NULL)
02149     {
02150       bfd_vma last_value = 0;
02151 
02152       l = &section->lineno[i];
02153 
02154       for (; i < section->lineno_count; i++)
02155        {
02156          if (l->line_number == 0)
02157            {
02158              /* Get the symbol this line number points at.  */
02159              coff_symbol_type *coff = (coff_symbol_type *) (l->u.sym);
02160              if (coff->symbol.value > offset)
02161               break;
02162              *functionname_ptr = coff->symbol.name;
02163              last_value = coff->symbol.value;
02164              if (coff->native)
02165               {
02166                 combined_entry_type *s = coff->native;
02167                 s = s + 1 + s->u.syment.n_numaux;
02168 
02169                 /* In XCOFF a debugging symbol can follow the
02170                    function symbol.  */
02171                 if (s->u.syment.n_scnum == N_DEBUG)
02172                   s = s + 1 + s->u.syment.n_numaux;
02173 
02174                 /* S should now point to the .bf of the function.  */
02175                 if (s->u.syment.n_numaux)
02176                   {
02177                     /* The linenumber is stored in the auxent.  */
02178                     union internal_auxent *a = &((s + 1)->u.auxent);
02179                     line_base = a->x_sym.x_misc.x_lnsz.x_lnno;
02180                     *line_ptr = line_base;
02181                   }
02182               }
02183            }
02184          else
02185            {
02186              if (l->u.offset > offset)
02187               break;
02188              *line_ptr = l->line_number + line_base - 1;
02189            }
02190          l++;
02191        }
02192 
02193       /* If we fell off the end of the loop, then assume that this
02194         symbol has no line number info.  Otherwise, symbols with no
02195         line number info get reported with the line number of the
02196         last line of the last symbol which does have line number
02197         info.  We use 0x100 as a slop to account for cases where the
02198         last line has executable code.  */
02199       if (i >= section->lineno_count
02200          && last_value != 0
02201          && offset - last_value > 0x100)
02202        {
02203          *functionname_ptr = NULL;
02204          *line_ptr = 0;
02205        }
02206     }
02207 
02208   /* Cache the results for the next call.  */
02209   if (sec_data == NULL && section->owner == abfd)
02210     {
02211       amt = sizeof (struct coff_section_tdata);
02212       section->used_by_bfd = bfd_zalloc (abfd, amt);
02213       sec_data = (struct coff_section_tdata *) section->used_by_bfd;
02214     }
02215   if (sec_data != NULL)
02216     {
02217       sec_data->offset = offset;
02218       sec_data->i = i;
02219       sec_data->function = *functionname_ptr;
02220       sec_data->line_base = line_base;
02221     }
02222 
02223   return TRUE;
02224 }
02225 
02226 bfd_boolean
02227 coff_find_inliner_info (bfd *abfd,
02228                      const char **filename_ptr,
02229                      const char **functionname_ptr,
02230                      unsigned int *line_ptr)
02231 {
02232   bfd_boolean found;
02233 
02234   found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
02235                                     functionname_ptr, line_ptr,
02236                                     &coff_data(abfd)->dwarf2_find_line_info);
02237   return (found);
02238 }
02239 
02240 int
02241 coff_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
02242 {
02243   size_t size;
02244 
02245   if (!info->relocatable)
02246     size = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
02247   else
02248     size = bfd_coff_filhsz (abfd);
02249 
02250   size += abfd->section_count * bfd_coff_scnhsz (abfd);
02251   return size;
02252 }
02253 
02254 /* Change the class of a coff symbol held by BFD.  */
02255 
02256 bfd_boolean
02257 bfd_coff_set_symbol_class (bfd *         abfd,
02258                         asymbol *     symbol,
02259                         unsigned int  class)
02260 {
02261   coff_symbol_type * csym;
02262 
02263   csym = coff_symbol_from (abfd, symbol);
02264   if (csym == NULL)
02265     {
02266       bfd_set_error (bfd_error_invalid_operation);
02267       return FALSE;
02268     }
02269   else if (csym->native == NULL)
02270     {
02271       /* This is an alien symbol which no native coff backend data.
02272         We cheat here by creating a fake native entry for it and
02273         then filling in the class.  This code is based on that in
02274         coff_write_alien_symbol().  */
02275 
02276       combined_entry_type * native;
02277       bfd_size_type amt = sizeof (* native);
02278 
02279       native = bfd_zalloc (abfd, amt);
02280       if (native == NULL)
02281        return FALSE;
02282 
02283       native->u.syment.n_type   = T_NULL;
02284       native->u.syment.n_sclass = class;
02285 
02286       if (bfd_is_und_section (symbol->section))
02287        {
02288          native->u.syment.n_scnum = N_UNDEF;
02289          native->u.syment.n_value = symbol->value;
02290        }
02291       else if (bfd_is_com_section (symbol->section))
02292        {
02293          native->u.syment.n_scnum = N_UNDEF;
02294          native->u.syment.n_value = symbol->value;
02295        }
02296       else
02297        {
02298          native->u.syment.n_scnum =
02299            symbol->section->output_section->target_index;
02300          native->u.syment.n_value = (symbol->value
02301                                   + symbol->section->output_offset);
02302          if (! obj_pe (abfd))
02303            native->u.syment.n_value += symbol->section->output_section->vma;
02304 
02305          /* Copy the any flags from the file header into the symbol.
02306             FIXME: Why?  */
02307          native->u.syment.n_flags = bfd_asymbol_bfd (& csym->symbol)->flags;
02308        }
02309 
02310       csym->native = native;
02311     }
02312   else
02313     csym->native->u.syment.n_sclass = class;
02314 
02315   return TRUE;
02316 }
02317 
02318 struct coff_comdat_info *
02319 bfd_coff_get_comdat_section (bfd *abfd, struct bfd_section *sec)
02320 {
02321   if (bfd_get_flavour (abfd) == bfd_target_coff_flavour
02322       && coff_section_data (abfd, sec) != NULL)
02323     return coff_section_data (abfd, sec)->comdat;
02324   else
02325     return NULL;
02326 }