home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / gnu / gdb-4.12.tar.gz / gdb-4.12.tar / gdb-4.12 / bfd / coffgen.c < prev    next >
C/C++ Source or Header  |  1994-02-03  |  42KB  |  1,524 lines

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