home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / binutils.7 / binutils / binutils-2.7 / bfd / nlmcode.h < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-04  |  64.5 KB  |  2,058 lines

  1. /* NLM (NetWare Loadable Module) executable support for BFD.
  2.    Copyright (C) 1993 Free Software Foundation, Inc.
  3.  
  4.    Written by Fred Fish @ Cygnus Support, using ELF support as the
  5.    template.
  6.  
  7. This file is part of BFD, the Binary File Descriptor library.
  8.  
  9. This program is free software; you can redistribute it and/or modify
  10. it under the terms of the GNU General Public License as published by
  11. the Free Software Foundation; either version 2 of the License, or
  12. (at your option) any later version.
  13.  
  14. This program is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. GNU General Public License for more details.
  18.  
  19. You should have received a copy of the GNU General Public License
  20. along with this program; if not, write to the Free Software
  21. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
  22.  
  23. #include <string.h>        /* For strrchr and friends */
  24. #include "bfd.h"
  25. #include "sysdep.h"
  26. #include "libbfd.h"
  27. #include "libnlm.h"
  28.  
  29. /* The functions in this file do not use the names they appear to use.
  30.    This file is actually compiled multiple times, once for each size
  31.    of NLM target we are using.  At each size we use a different name,
  32.    constructed by the macro nlmNAME.  For example, the function which
  33.    is named nlm_symbol_type below is actually named nlm32_symbol_type
  34.    in the final executable.  */
  35.  
  36. #define Nlm_External_Fixed_Header    NlmNAME(External_Fixed_Header)
  37. #define Nlm_External_Version_Header    NlmNAME(External_Version_Header)
  38. #define Nlm_External_Copyright_Header    NlmNAME(External_Copyright_Header)
  39. #define Nlm_External_Extended_Header    NlmNAME(External_Extended_Header)
  40. #define Nlm_External_Custom_Header    NlmNAME(External_Custom_Header)
  41. #define Nlm_External_Cygnus_Ext_Header    NlmNAME(External_Cygnus_Ext_Header)
  42.  
  43. #define nlm_symbol_type            nlmNAME(symbol_type)
  44. #define nlm_get_symtab_upper_bound    nlmNAME(get_symtab_upper_bound)
  45. #define nlm_get_symtab            nlmNAME(get_symtab)
  46. #define nlm_make_empty_symbol        nlmNAME(make_empty_symbol)
  47. #define nlm_print_symbol        nlmNAME(print_symbol)
  48. #define nlm_get_symbol_info        nlmNAME(get_symbol_info)
  49. #define nlm_get_reloc_upper_bound    nlmNAME(get_reloc_upper_bound)
  50. #define nlm_canonicalize_reloc        nlmNAME(canonicalize_reloc)
  51. #define nlm_object_p            nlmNAME(object_p)
  52. #define nlm_set_section_contents    nlmNAME(set_section_contents)
  53. #define nlm_write_object_contents    nlmNAME(write_object_contents)
  54.  
  55. #define nlm_swap_fixed_header_in(abfd,src,dst) \
  56.   (nlm_swap_fixed_header_in_func(abfd))(abfd,src,dst)
  57. #define nlm_swap_fixed_header_out(abfd,src,dst) \
  58.   (nlm_swap_fixed_header_out_func(abfd))(abfd,src,dst)
  59.  
  60. /* Forward declarations of static functions */
  61.  
  62. static boolean add_bfd_section
  63.   PARAMS ((bfd *, char *, file_ptr, bfd_size_type, flagword));
  64. static boolean nlm_swap_variable_header_in
  65.   PARAMS ((bfd *));
  66. static boolean nlm_swap_variable_header_out
  67.   PARAMS ((bfd *));
  68. static boolean find_nonzero
  69.   PARAMS ((PTR, size_t));
  70. static boolean nlm_swap_auxiliary_headers_in
  71.   PARAMS ((bfd *));
  72. static boolean nlm_swap_auxiliary_headers_out
  73.   PARAMS ((bfd *));
  74. static boolean nlm_slurp_symbol_table
  75.   PARAMS ((bfd *));
  76. static boolean nlm_slurp_reloc_fixups
  77.   PARAMS ((bfd *));
  78. static boolean nlm_compute_section_file_positions
  79.   PARAMS ((bfd *));
  80. static int nlm_external_reloc_compare
  81.   PARAMS ((const void *, const void *));
  82.  
  83. /* Should perhaps use put_offset, put_word, etc.  For now, the two versions
  84.    can be handled by explicitly specifying 32 bits or "the long type".  */
  85. #if ARCH_SIZE == 64
  86. #define put_word    bfd_h_put_64
  87. #define get_word    bfd_h_get_64
  88. #endif
  89. #if ARCH_SIZE == 32
  90. #define put_word    bfd_h_put_32
  91. #define get_word    bfd_h_get_32
  92. #endif
  93.  
  94. const bfd_target *
  95. nlm_object_p (abfd)
  96.      bfd *abfd;
  97. {
  98.   struct nlm_obj_tdata *preserved_tdata = nlm_tdata (abfd);
  99.   boolean (*backend_object_p) PARAMS ((bfd *));
  100.   PTR x_fxdhdr = NULL;
  101.   Nlm_Internal_Fixed_Header *i_fxdhdrp;
  102.   struct nlm_obj_tdata *new_tdata = NULL;
  103.   const char *signature;
  104.   enum bfd_architecture arch;
  105.  
  106.   /* Some NLM formats have a prefix before the standard NLM fixed
  107.      header.  */
  108.   backend_object_p = nlm_backend_object_p_func (abfd);
  109.   if (backend_object_p)
  110.     {
  111.       if (!(*backend_object_p) (abfd))
  112.     goto got_wrong_format_error;
  113.     }
  114.  
  115.   /* Read in the fixed length portion of the NLM header in external format.  */
  116.  
  117.   x_fxdhdr = (PTR) bfd_malloc ((size_t) nlm_fixed_header_size (abfd));
  118.   if (x_fxdhdr == NULL)
  119.     goto got_no_match;
  120.  
  121.   if (bfd_read ((PTR) x_fxdhdr, nlm_fixed_header_size (abfd), 1, abfd) !=
  122.       nlm_fixed_header_size (abfd))
  123.     {
  124.       if (bfd_get_error () != bfd_error_system_call)
  125.     goto got_wrong_format_error;
  126.       else
  127.     goto got_no_match;
  128.     }
  129.  
  130.   /* Allocate an instance of the nlm_obj_tdata structure and hook it up to
  131.      the tdata pointer in the bfd.  */
  132.  
  133.   new_tdata = ((struct nlm_obj_tdata *)
  134.            bfd_zalloc (abfd, sizeof (struct nlm_obj_tdata)));
  135.   if (new_tdata == NULL)
  136.     goto got_no_match;
  137.  
  138.   nlm_tdata (abfd) = new_tdata;
  139.  
  140.   i_fxdhdrp = nlm_fixed_header (abfd);
  141.   nlm_swap_fixed_header_in (abfd, x_fxdhdr, i_fxdhdrp);
  142.   free (x_fxdhdr);
  143.   x_fxdhdr = NULL;
  144.  
  145.   /* Check to see if we have an NLM file for this backend by matching
  146.      the NLM signature. */
  147.  
  148.   signature = nlm_signature (abfd);
  149.   if (signature != NULL
  150.       && *signature != '\0'
  151.       && strncmp ((char *) i_fxdhdrp->signature, signature,
  152.           NLM_SIGNATURE_SIZE) != 0)
  153.     goto got_wrong_format_error;
  154.  
  155.   /* There's no supported way to discover the endianess of an NLM, so test for
  156.      a sane version number after doing byte swapping appropriate for this
  157.      XVEC.  (Hack alert!) */
  158.  
  159.   if (i_fxdhdrp->version > 0xFFFF)
  160.     goto got_wrong_format_error;
  161.  
  162.   /* There's no supported way to check for 32 bit versus 64 bit addresses,
  163.      so ignore this distinction for now.  (FIXME) */
  164.  
  165.   /* Swap in the rest of the required header.  */
  166.   if (!nlm_swap_variable_header_in (abfd))
  167.     {
  168.       if (bfd_get_error () != bfd_error_system_call)
  169.     goto got_wrong_format_error;
  170.       else
  171.     goto got_no_match;
  172.     }
  173.  
  174.   /* Add the sections supplied by all NLM's, and then read in the
  175.      auxiliary headers.  Reading the auxiliary headers may create
  176.      additional sections described in the cygnus_ext header.
  177.      From this point on we assume that we have an NLM, and do not
  178.      treat errors as indicating the wrong format.  */
  179.  
  180.   if (!add_bfd_section (abfd, NLM_CODE_NAME,
  181.             i_fxdhdrp->codeImageOffset,
  182.             i_fxdhdrp->codeImageSize,
  183.             (SEC_CODE | SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
  184.              | SEC_RELOC))
  185.       || !add_bfd_section (abfd, NLM_INITIALIZED_DATA_NAME,
  186.                i_fxdhdrp->dataImageOffset,
  187.                i_fxdhdrp->dataImageSize,
  188.                (SEC_DATA | SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
  189.                 | SEC_RELOC))
  190.       || !add_bfd_section (abfd, NLM_UNINITIALIZED_DATA_NAME,
  191.                (file_ptr) 0,
  192.                i_fxdhdrp->uninitializedDataSize,
  193.                SEC_ALLOC))
  194.     goto got_no_match;
  195.  
  196.   if (!nlm_swap_auxiliary_headers_in (abfd))
  197.     goto got_no_match;
  198.  
  199.   if (nlm_fixed_header (abfd)->numberOfRelocationFixups != 0
  200.       || nlm_fixed_header (abfd)->numberOfExternalReferences != 0)
  201.     abfd->flags |= HAS_RELOC;
  202.   if (nlm_fixed_header (abfd)->numberOfPublics != 0
  203.       || nlm_fixed_header (abfd)->numberOfDebugRecords != 0
  204.       || nlm_fixed_header (abfd)->numberOfExternalReferences != 0)
  205.     abfd->flags |= HAS_SYMS;
  206.  
  207.   arch = nlm_architecture (abfd);
  208.   if (arch != bfd_arch_unknown)
  209.     bfd_default_set_arch_mach (abfd, arch, (unsigned long) 0);
  210.  
  211.   abfd->flags |= EXEC_P;
  212.   bfd_get_start_address (abfd) = nlm_fixed_header (abfd)->codeStartOffset;
  213.  
  214.   return (abfd->xvec);
  215.  
  216. got_wrong_format_error:
  217.   bfd_set_error (bfd_error_wrong_format);
  218. got_no_match:
  219.   nlm_tdata (abfd) = preserved_tdata;
  220.   if (new_tdata != NULL)
  221.     bfd_release (abfd, new_tdata);
  222.   if (x_fxdhdr != NULL)
  223.     free (x_fxdhdr);
  224.   return (NULL);
  225. }
  226.  
  227. /* Add a section to the bfd. */
  228.  
  229. static boolean
  230. add_bfd_section (abfd, name, offset, size, flags)
  231.      bfd *abfd;
  232.      char *name;
  233.      file_ptr offset;
  234.      bfd_size_type size;
  235.      flagword flags;
  236. {
  237.   asection *newsect;
  238.  
  239.   newsect = bfd_make_section (abfd, name);
  240.   if (newsect == NULL)
  241.     {
  242.       return (false);
  243.     }
  244.   newsect->vma = 0;        /* NLM's are relocatable. */
  245.   newsect->_raw_size = size;
  246.   newsect->filepos = offset;
  247.   newsect->flags = flags;
  248.   newsect->alignment_power = bfd_log2 (0);    /* FIXME */
  249.   return (true);
  250. }
  251.  
  252. /* Read and swap in the variable length header.  All the fields must
  253.    exist in the NLM, and must exist in the order they are read here. */
  254.  
  255. static boolean
  256. nlm_swap_variable_header_in (abfd)
  257.      bfd *abfd;
  258. {
  259.   unsigned char temp[NLM_TARGET_LONG_SIZE];
  260.  
  261.   /* Read the description length and text members. */
  262.  
  263.   if (bfd_read ((PTR) & nlm_variable_header (abfd)->descriptionLength,
  264.         sizeof (nlm_variable_header (abfd)->descriptionLength),
  265.         1, abfd) !=
  266.       sizeof (nlm_variable_header (abfd)->descriptionLength))
  267.     return (false);
  268.   if (bfd_read ((PTR) nlm_variable_header (abfd)->descriptionText,
  269.         nlm_variable_header (abfd)->descriptionLength + 1,
  270.         1, abfd) !=
  271.       (bfd_size_type) nlm_variable_header (abfd)->descriptionLength + 1)
  272.     return (false);
  273.  
  274.   /* Read and convert the stackSize field. */
  275.  
  276.   if (bfd_read ((PTR) temp, sizeof (temp), 1, abfd) != sizeof (temp))
  277.     return (false);
  278.   nlm_variable_header (abfd)->stackSize = get_word (abfd, (bfd_byte *) temp);
  279.  
  280.   /* Read and convert the reserved field. */
  281.  
  282.   if (bfd_read ((PTR) temp, sizeof (temp), 1, abfd) != sizeof (temp))
  283.     return (false);
  284.   nlm_variable_header (abfd)->reserved = get_word (abfd, (bfd_byte *) temp);
  285.  
  286.   /* Read the oldThreadName field.  This field is a fixed length string. */
  287.  
  288.   if (bfd_read ((PTR) nlm_variable_header (abfd)->oldThreadName,
  289.         sizeof (nlm_variable_header (abfd)->oldThreadName),
  290.         1, abfd) !=
  291.       sizeof (nlm_variable_header (abfd)->oldThreadName))
  292.     return (false);
  293.  
  294.   /* Read the screen name length and text members. */
  295.  
  296.   if (bfd_read ((PTR) & nlm_variable_header (abfd)->screenNameLength,
  297.         sizeof (nlm_variable_header (abfd)->screenNameLength),
  298.         1, abfd) !=
  299.       sizeof (nlm_variable_header (abfd)->screenNameLength))
  300.     return (false);
  301.   if (bfd_read ((PTR) nlm_variable_header (abfd)->screenName,
  302.         nlm_variable_header (abfd)->screenNameLength + 1,
  303.         1, abfd) !=
  304.       (bfd_size_type) nlm_variable_header (abfd)->screenNameLength + 1)
  305.     return (false);
  306.  
  307.   /* Read the thread name length and text members. */
  308.  
  309.   if (bfd_read ((PTR) & nlm_variable_header (abfd)->threadNameLength,
  310.         sizeof (nlm_variable_header (abfd)->threadNameLength),
  311.         1, abfd) !=
  312.       sizeof (nlm_variable_header (abfd)->threadNameLength))
  313.     return (false);
  314.   if (bfd_read ((PTR) nlm_variable_header (abfd)->threadName,
  315.         nlm_variable_header (abfd)->threadNameLength + 1,
  316.         1, abfd) !=
  317.       (bfd_size_type) nlm_variable_header (abfd)->threadNameLength + 1)
  318.     return (false);
  319.   return (true);
  320. }
  321.  
  322. /* Swap and write out the variable length header.  All the fields must
  323.    exist in the NLM, and must exist in this order.  */
  324.  
  325. static boolean
  326. nlm_swap_variable_header_out (abfd)
  327.      bfd *abfd;
  328. {
  329.   unsigned char temp[NLM_TARGET_LONG_SIZE];
  330.  
  331.   /* Write the description length and text members. */
  332.  
  333.   if (bfd_write ((PTR) & nlm_variable_header (abfd)->descriptionLength,
  334.          sizeof (nlm_variable_header (abfd)->descriptionLength),
  335.          1, abfd) !=
  336.       sizeof (nlm_variable_header (abfd)->descriptionLength))
  337.     return (false);
  338.   if (bfd_write ((PTR) nlm_variable_header (abfd)->descriptionText,
  339.          nlm_variable_header (abfd)->descriptionLength + 1,
  340.          1, abfd) !=
  341.       (bfd_size_type) nlm_variable_header (abfd)->descriptionLength + 1)
  342.     return (false);
  343.  
  344.   /* Convert and write the stackSize field. */
  345.  
  346.   put_word (abfd, (bfd_vma) nlm_variable_header (abfd)->stackSize,
  347.         (bfd_byte *) temp);
  348.   if (bfd_write ((PTR) temp, sizeof (temp), 1, abfd) != sizeof (temp))
  349.     return (false);
  350.  
  351.   /* Convert and write the reserved field. */
  352.  
  353.   put_word (abfd, (bfd_vma) nlm_variable_header (abfd)->reserved,
  354.         (bfd_byte *) temp);
  355.   if (bfd_write ((PTR) temp, sizeof (temp), 1, abfd) != sizeof (temp))
  356.     return (false);
  357.  
  358.   /* Write the oldThreadName field.  This field is a fixed length string. */
  359.  
  360.   if (bfd_write ((PTR) nlm_variable_header (abfd)->oldThreadName,
  361.          sizeof (nlm_variable_header (abfd)->oldThreadName),
  362.          1, abfd) !=
  363.       sizeof (nlm_variable_header (abfd)->oldThreadName))
  364.     return (false);
  365.  
  366.   /* Write the screen name length and text members. */
  367.  
  368.   if (bfd_write ((PTR) & nlm_variable_header (abfd)->screenNameLength,
  369.          sizeof (nlm_variable_header (abfd)->screenNameLength),
  370.          1, abfd) !=
  371.       sizeof (nlm_variable_header (abfd)->screenNameLength))
  372.     return (false);
  373.   if (bfd_write ((PTR) nlm_variable_header (abfd)->screenName,
  374.          nlm_variable_header (abfd)->screenNameLength + 1,
  375.          1, abfd) !=
  376.       (bfd_size_type) nlm_variable_header (abfd)->screenNameLength + 1)
  377.     return (false);
  378.  
  379.   /* Write the thread name length and text members. */
  380.  
  381.   if (bfd_write ((PTR) & nlm_variable_header (abfd)->threadNameLength,
  382.          sizeof (nlm_variable_header (abfd)->threadNameLength),
  383.          1, abfd) !=
  384.       sizeof (nlm_variable_header (abfd)->threadNameLength))
  385.     return (false);
  386.   if (bfd_write ((PTR) nlm_variable_header (abfd)->threadName,
  387.          nlm_variable_header (abfd)->threadNameLength + 1,
  388.          1, abfd) !=
  389.       (bfd_size_type) nlm_variable_header (abfd)->threadNameLength + 1)
  390.     return (false);
  391.   return (true);
  392. }
  393.  
  394. /* Read and swap in the contents of all the auxiliary headers.  Because of
  395.    the braindead design, we have to do strcmps on strings of indeterminate
  396.    length to figure out what each auxiliary header is.  Even worse, we have
  397.    no way of knowing how many auxiliary headers there are or where the end
  398.    of the auxiliary headers are, except by finding something that doesn't
  399.    look like a known auxiliary header.  This means that the first new type
  400.    of auxiliary header added will break all existing tools that don't
  401.    recognize it. */
  402.  
  403. static boolean
  404. nlm_swap_auxiliary_headers_in (abfd)
  405.      bfd *abfd;
  406. {
  407.   char tempstr[16];
  408.   long position;
  409.  
  410.   for (;;)
  411.     {
  412.       position = bfd_tell (abfd);
  413.       if (bfd_read ((PTR) tempstr, sizeof (tempstr), 1, abfd) !=
  414.       sizeof (tempstr))
  415.     return (false);
  416.       if (bfd_seek (abfd, position, SEEK_SET) == -1)
  417.     return (false);
  418.       if (strncmp (tempstr, "VeRsIoN#", 8) == 0)
  419.     {
  420.       Nlm_External_Version_Header thdr;
  421.       if (bfd_read ((PTR) & thdr, sizeof (thdr), 1, abfd) != sizeof (thdr))
  422.         return (false);
  423.       memcpy (nlm_version_header (abfd)->stamp, thdr.stamp,
  424.           sizeof (thdr.stamp));
  425.       nlm_version_header (abfd)->majorVersion =
  426.         get_word (abfd, (bfd_byte *) thdr.majorVersion);
  427.       nlm_version_header (abfd)->minorVersion =
  428.         get_word (abfd, (bfd_byte *) thdr.minorVersion);
  429.       nlm_version_header (abfd)->revision =
  430.         get_word (abfd, (bfd_byte *) thdr.revision);
  431.       nlm_version_header (abfd)->year =
  432.         get_word (abfd, (bfd_byte *) thdr.year);
  433.       nlm_version_header (abfd)->month =
  434.         get_word (abfd, (bfd_byte *) thdr.month);
  435.       nlm_version_header (abfd)->day =
  436.         get_word (abfd, (bfd_byte *) thdr.day);
  437.     }
  438.       else if (strncmp (tempstr, "MeSsAgEs", 8) == 0)
  439.     {
  440.       Nlm_External_Extended_Header thdr;
  441.       if (bfd_read ((PTR) & thdr, sizeof (thdr), 1, abfd) != sizeof (thdr))
  442.         return (false);
  443.       memcpy (nlm_extended_header (abfd)->stamp, thdr.stamp,
  444.           sizeof (thdr.stamp));
  445.       nlm_extended_header (abfd)->languageID =
  446.         get_word (abfd, (bfd_byte *) thdr.languageID);
  447.       nlm_extended_header (abfd)->messageFileOffset =
  448.         get_word (abfd, (bfd_byte *) thdr.messageFileOffset);
  449.       nlm_extended_header (abfd)->messageFileLength =
  450.         get_word (abfd, (bfd_byte *) thdr.messageFileLength);
  451.       nlm_extended_header (abfd)->messageCount =
  452.         get_word (abfd, (bfd_byte *) thdr.messageCount);
  453.       nlm_extended_header (abfd)->helpFileOffset =
  454.         get_word (abfd, (bfd_byte *) thdr.helpFileOffset);
  455.       nlm_extended_header (abfd)->helpFileLength =
  456.         get_word (abfd, (bfd_byte *) thdr.helpFileLength);
  457.       nlm_extended_header (abfd)->RPCDataOffset =
  458.         get_word (abfd, (bfd_byte *) thdr.RPCDataOffset);
  459.       nlm_extended_header (abfd)->RPCDataLength =
  460.         get_word (abfd, (bfd_byte *) thdr.RPCDataLength);
  461.       nlm_extended_header (abfd)->sharedCodeOffset =
  462.         get_word (abfd, (bfd_byte *) thdr.sharedCodeOffset);
  463.       nlm_extended_header (abfd)->sharedCodeLength =
  464.         get_word (abfd, (bfd_byte *) thdr.sharedCodeLength);
  465.       nlm_extended_header (abfd)->sharedDataOffset =
  466.         get_word (abfd, (bfd_byte *) thdr.sharedDataOffset);
  467.       nlm_extended_header (abfd)->sharedDataLength =
  468.         get_word (abfd, (bfd_byte *) thdr.sharedDataLength);
  469.       nlm_extended_header (abfd)->sharedRelocationFixupOffset =
  470.         get_word (abfd, (bfd_byte *) thdr.sharedRelocationFixupOffset);
  471.       nlm_extended_header (abfd)->sharedRelocationFixupCount =
  472.         get_word (abfd, (bfd_byte *) thdr.sharedRelocationFixupCount);
  473.       nlm_extended_header (abfd)->sharedExternalReferenceOffset =
  474.         get_word (abfd, (bfd_byte *) thdr.sharedExternalReferenceOffset);
  475.       nlm_extended_header (abfd)->sharedExternalReferenceCount =
  476.         get_word (abfd, (bfd_byte *) thdr.sharedExternalReferenceCount);
  477.       nlm_extended_header (abfd)->sharedPublicsOffset =
  478.         get_word (abfd, (bfd_byte *) thdr.sharedPublicsOffset);
  479.       nlm_extended_header (abfd)->sharedPublicsCount =
  480.         get_word (abfd, (bfd_byte *) thdr.sharedPublicsCount);
  481.       nlm_extended_header (abfd)->sharedDebugRecordOffset =
  482.         get_word (abfd, (bfd_byte *) thdr.sharedDebugRecordOffset);
  483.       nlm_extended_header (abfd)->sharedDebugRecordCount =
  484.         get_word (abfd, (bfd_byte *) thdr.sharedDebugRecordCount);
  485.       nlm_extended_header (abfd)->SharedInitializationOffset =
  486.         get_word (abfd, (bfd_byte *) thdr.sharedInitializationOffset);
  487.       nlm_extended_header (abfd)->SharedExitProcedureOffset =
  488.         get_word (abfd, (bfd_byte *) thdr.SharedExitProcedureOffset);
  489.       nlm_extended_header (abfd)->productID =
  490.         get_word (abfd, (bfd_byte *) thdr.productID);
  491.       nlm_extended_header (abfd)->reserved0 =
  492.         get_word (abfd, (bfd_byte *) thdr.reserved0);
  493.       nlm_extended_header (abfd)->reserved1 =
  494.         get_word (abfd, (bfd_byte *) thdr.reserved1);
  495.       nlm_extended_header (abfd)->reserved2 =
  496.         get_word (abfd, (bfd_byte *) thdr.reserved2);
  497.       nlm_extended_header (abfd)->reserved3 =
  498.         get_word (abfd, (bfd_byte *) thdr.reserved3);
  499.       nlm_extended_header (abfd)->reserved4 =
  500.         get_word (abfd, (bfd_byte *) thdr.reserved4);
  501.       nlm_extended_header (abfd)->reserved5 =
  502.         get_word (abfd, (bfd_byte *) thdr.reserved5);
  503.     }
  504.       else if (strncmp (tempstr, "CoPyRiGhT=", 10) == 0)
  505.     {
  506.       if (bfd_read ((PTR) nlm_copyright_header (abfd)->stamp,
  507.             sizeof (nlm_copyright_header (abfd)->stamp),
  508.             1, abfd)
  509.           != sizeof (nlm_copyright_header (abfd)->stamp))
  510.         return (false);
  511.       if (bfd_read ((PTR) & (nlm_copyright_header (abfd)
  512.                  ->copyrightMessageLength),
  513.             1, 1, abfd) != 1)
  514.         return (false);
  515.       /* The copyright message is a variable length string. */
  516.       if (bfd_read ((PTR) nlm_copyright_header (abfd)->copyrightMessage,
  517.             nlm_copyright_header (abfd)->copyrightMessageLength + 1,
  518.             1, abfd) !=
  519.           ((bfd_size_type)
  520.            nlm_copyright_header (abfd)->copyrightMessageLength + 1))
  521.         return (false);
  522.     }
  523.       else if (strncmp (tempstr, "CuStHeAd", 8) == 0)
  524.     {
  525.       Nlm_External_Custom_Header thdr;
  526.       bfd_size_type hdrLength;
  527.       file_ptr dataOffset;
  528.       bfd_size_type dataLength;
  529.       char dataStamp[8];
  530.       PTR hdr;
  531.  
  532.       /* Read the stamp ("CuStHeAd").  */
  533.       if (bfd_read ((PTR) thdr.stamp, 1, sizeof (thdr.stamp), abfd)
  534.           != sizeof (thdr.stamp))
  535.         return false;
  536.       /* Read the length of this custom header.  */
  537.       if (bfd_read ((PTR) thdr.length, 1, sizeof (thdr.length), abfd)
  538.           != sizeof (thdr.length))
  539.         return false;
  540.       hdrLength = get_word (abfd, (bfd_byte *) thdr.length);
  541.       /* Read further fields if we have them.  */
  542.       if (hdrLength < NLM_TARGET_LONG_SIZE)
  543.         dataOffset = 0;
  544.       else
  545.         {
  546.           if (bfd_read ((PTR) thdr.dataOffset, 1,
  547.                 sizeof (thdr.dataOffset), abfd)
  548.           != sizeof (thdr.dataOffset))
  549.         return false;
  550.           dataOffset = get_word (abfd, (bfd_byte *) thdr.dataOffset);
  551.         }
  552.       if (hdrLength < 2 * NLM_TARGET_LONG_SIZE)
  553.         dataLength = 0;
  554.       else
  555.         {
  556.           if (bfd_read ((PTR) thdr.dataLength, 1,
  557.                 sizeof (thdr.dataLength), abfd)
  558.           != sizeof (thdr.dataLength))
  559.         return false;
  560.           dataLength = get_word (abfd, (bfd_byte *) thdr.dataLength);
  561.         }
  562.       if (hdrLength < 2 * NLM_TARGET_LONG_SIZE + 8)
  563.         memset (dataStamp, 0, sizeof (dataStamp));
  564.       else
  565.         {
  566.           if (bfd_read ((PTR) dataStamp, 1, sizeof (dataStamp), abfd)
  567.           != sizeof (dataStamp))
  568.         return false;
  569.         }
  570.  
  571.       /* Read the rest of the header, if any.  */
  572.       if (hdrLength <= 2 * NLM_TARGET_LONG_SIZE + 8)
  573.         {
  574.           hdr = NULL;
  575.           hdrLength = 0;
  576.         }
  577.       else
  578.         {
  579.           hdrLength -= 2 * NLM_TARGET_LONG_SIZE + 8;
  580.           hdr = bfd_alloc (abfd, hdrLength);
  581.           if (hdr == NULL)
  582.         return false;
  583.           if (bfd_read (hdr, 1, hdrLength, abfd) != hdrLength)
  584.         return false;
  585.         }
  586.  
  587.       /* If we have found a Cygnus header, process it.  Otherwise,
  588.          just save the associated data without trying to interpret
  589.          it.  */
  590.       if (strncmp (dataStamp, "CyGnUsEx", 8) == 0)
  591.         {
  592.           file_ptr pos;
  593.           bfd_byte *contents;
  594.           bfd_byte *p, *pend;
  595.  
  596.           BFD_ASSERT (hdrLength == 0 && hdr == NULL);
  597.  
  598.           pos = bfd_tell (abfd);
  599.           if (bfd_seek (abfd, dataOffset, SEEK_SET) != 0)
  600.         return false;
  601.           contents = (bfd_byte *) bfd_alloc (abfd, dataLength);
  602.           if (contents == NULL)
  603.         return false;
  604.           if (bfd_read (contents, 1, dataLength, abfd) != dataLength)
  605.         return false;
  606.           if (bfd_seek (abfd, pos, SEEK_SET) != 0)
  607.         return false;
  608.  
  609.           memcpy (nlm_cygnus_ext_header (abfd), "CyGnUsEx", 8);
  610.           nlm_cygnus_ext_header (abfd)->offset = dataOffset;
  611.           nlm_cygnus_ext_header (abfd)->length = dataLength;
  612.  
  613.           /* This data this header points to provides a list of
  614.          the sections which were in the original object file
  615.          which was converted to become an NLM.  We locate
  616.          those sections and add them to the BFD.  Note that
  617.          this is likely to create a second .text, .data and
  618.          .bss section; retrieving the sections by name will
  619.          get the actual NLM sections, which is what we want to
  620.          happen.  The sections from the original file, which
  621.          may be subsets of the NLM section, can only be found
  622.          using bfd_map_over_sections.  */
  623.           p = contents;
  624.           pend = p + dataLength;
  625.           while (p < pend)
  626.         {
  627.           char *name;
  628.           size_t l;
  629.           file_ptr filepos;
  630.           bfd_size_type size;
  631.           asection *newsec;
  632.  
  633.           /* The format of this information is
  634.              null terminated section name
  635.              zeroes to adjust to 4 byte boundary
  636.              4 byte section data file pointer
  637.              4 byte section size
  638.              */
  639.  
  640.           name = (char *) p;
  641.           l = strlen (name) + 1;
  642.           l = (l + 3) &~ 3;
  643.           p += l;
  644.           filepos = bfd_h_get_32 (abfd, p);
  645.           p += 4;
  646.           size = bfd_h_get_32 (abfd, p);
  647.           p += 4;
  648.  
  649.           newsec = bfd_make_section_anyway (abfd, name);
  650.           if (newsec == (asection *) NULL)
  651.             return false;
  652.           newsec->_raw_size = size;
  653.           if (filepos != 0)
  654.             {
  655.               newsec->filepos = filepos;
  656.               newsec->flags |= SEC_HAS_CONTENTS;
  657.             }
  658.         }
  659.         }
  660.       else
  661.         {
  662.           memcpy (nlm_custom_header (abfd)->stamp, thdr.stamp,
  663.               sizeof (thdr.stamp));
  664.           nlm_custom_header (abfd)->hdrLength = hdrLength;
  665.           nlm_custom_header (abfd)->dataOffset = dataOffset;
  666.           nlm_custom_header (abfd)->dataLength = dataLength;
  667.           memcpy (nlm_custom_header (abfd)->dataStamp, dataStamp,
  668.               sizeof (dataStamp));
  669.           nlm_custom_header (abfd)->hdr = hdr;
  670.         }
  671.     }
  672.       else
  673.     {
  674.       break;
  675.     }
  676.     }
  677.   return (true);
  678. }
  679.  
  680. /* Return whether there is a non-zero byte in a memory block.  */
  681.  
  682. static boolean
  683. find_nonzero (buf, size)
  684.      PTR buf;
  685.      size_t size;
  686. {
  687.   char *p = (char *) buf;
  688.  
  689.   while (size-- != 0)
  690.     if (*p++ != 0)
  691.       return true;
  692.   return false;
  693. }
  694.  
  695. /* Swap out the contents of the auxiliary headers.  We create those
  696.    auxiliary headers which have been set non-zero.  We do not require
  697.    the caller to set up the stamp fields.  */
  698.  
  699. static boolean
  700. nlm_swap_auxiliary_headers_out (abfd)
  701.      bfd *abfd;
  702. {
  703.   /* Write out the version header if there is one.  */
  704.   if (find_nonzero ((PTR) nlm_version_header (abfd),
  705.             sizeof (Nlm_Internal_Version_Header)))
  706.     {
  707.       Nlm_External_Version_Header thdr;
  708.  
  709.       memcpy (thdr.stamp, "VeRsIoN#", 8);
  710.       put_word (abfd, (bfd_vma) nlm_version_header (abfd)->majorVersion,
  711.         (bfd_byte *) thdr.majorVersion);
  712.       put_word (abfd, (bfd_vma) nlm_version_header (abfd)->minorVersion,
  713.         (bfd_byte *) thdr.minorVersion);
  714.       put_word (abfd, (bfd_vma) nlm_version_header (abfd)->revision,
  715.         (bfd_byte *) thdr.revision);
  716.       put_word (abfd, (bfd_vma) nlm_version_header (abfd)->year,
  717.         (bfd_byte *) thdr.year);
  718.       put_word (abfd, (bfd_vma) nlm_version_header (abfd)->month,
  719.         (bfd_byte *) thdr.month);
  720.       put_word (abfd, (bfd_vma) nlm_version_header (abfd)->day,
  721.         (bfd_byte *) thdr.day);
  722.       if (bfd_write ((PTR) & thdr, sizeof (thdr), 1, abfd) != sizeof (thdr))
  723.     return false;
  724.     }
  725.  
  726.   /* Write out the extended header if there is one.  */
  727.   if (find_nonzero ((PTR) nlm_extended_header (abfd),
  728.             sizeof (Nlm_Internal_Extended_Header)))
  729.     {
  730.       Nlm_External_Extended_Header thdr;
  731.  
  732.       memcpy (thdr.stamp, "MeSsAgEs", 8);
  733.       put_word (abfd,
  734.         (bfd_vma) nlm_extended_header (abfd)->languageID,
  735.         (bfd_byte *) thdr.languageID);
  736.       put_word (abfd,
  737.         (bfd_vma) nlm_extended_header (abfd)->messageFileOffset,
  738.         (bfd_byte *) thdr.messageFileOffset);
  739.       put_word (abfd,
  740.         (bfd_vma) nlm_extended_header (abfd)->messageFileLength,
  741.         (bfd_byte *) thdr.messageFileLength);
  742.       put_word (abfd,
  743.         (bfd_vma) nlm_extended_header (abfd)->messageCount,
  744.         (bfd_byte *) thdr.messageCount);
  745.       put_word (abfd,
  746.         (bfd_vma) nlm_extended_header (abfd)->helpFileOffset,
  747.         (bfd_byte *) thdr.helpFileOffset);
  748.       put_word (abfd,
  749.         (bfd_vma) nlm_extended_header (abfd)->helpFileLength,
  750.         (bfd_byte *) thdr.helpFileLength);
  751.       put_word (abfd,
  752.         (bfd_vma) nlm_extended_header (abfd)->RPCDataOffset,
  753.         (bfd_byte *) thdr.RPCDataOffset);
  754.       put_word (abfd,
  755.         (bfd_vma) nlm_extended_header (abfd)->RPCDataLength,
  756.         (bfd_byte *) thdr.RPCDataLength);
  757.       put_word (abfd,
  758.         (bfd_vma) nlm_extended_header (abfd)->sharedCodeOffset,
  759.         (bfd_byte *) thdr.sharedCodeOffset);
  760.       put_word (abfd,
  761.         (bfd_vma) nlm_extended_header (abfd)->sharedCodeLength,
  762.         (bfd_byte *) thdr.sharedCodeLength);
  763.       put_word (abfd,
  764.         (bfd_vma) nlm_extended_header (abfd)->sharedDataOffset,
  765.         (bfd_byte *) thdr.sharedDataOffset);
  766.       put_word (abfd,
  767.         (bfd_vma) nlm_extended_header (abfd)->sharedDataLength,
  768.         (bfd_byte *) thdr.sharedDataLength);
  769.       put_word (abfd,
  770.       (bfd_vma) nlm_extended_header (abfd)->sharedRelocationFixupOffset,
  771.         (bfd_byte *) thdr.sharedRelocationFixupOffset);
  772.       put_word (abfd,
  773.        (bfd_vma) nlm_extended_header (abfd)->sharedRelocationFixupCount,
  774.         (bfd_byte *) thdr.sharedRelocationFixupCount);
  775.       put_word (abfd,
  776.     (bfd_vma) nlm_extended_header (abfd)->sharedExternalReferenceOffset,
  777.         (bfd_byte *) thdr.sharedExternalReferenceOffset);
  778.       put_word (abfd,
  779.      (bfd_vma) nlm_extended_header (abfd)->sharedExternalReferenceCount,
  780.         (bfd_byte *) thdr.sharedExternalReferenceCount);
  781.       put_word (abfd,
  782.         (bfd_vma) nlm_extended_header (abfd)->sharedPublicsOffset,
  783.         (bfd_byte *) thdr.sharedPublicsOffset);
  784.       put_word (abfd,
  785.         (bfd_vma) nlm_extended_header (abfd)->sharedPublicsCount,
  786.         (bfd_byte *) thdr.sharedPublicsCount);
  787.       put_word (abfd,
  788.           (bfd_vma) nlm_extended_header (abfd)->sharedDebugRecordOffset,
  789.         (bfd_byte *) thdr.sharedDebugRecordOffset);
  790.       put_word (abfd,
  791.         (bfd_vma) nlm_extended_header (abfd)->sharedDebugRecordCount,
  792.         (bfd_byte *) thdr.sharedDebugRecordCount);
  793.       put_word (abfd,
  794.        (bfd_vma) nlm_extended_header (abfd)->SharedInitializationOffset,
  795.         (bfd_byte *) thdr.sharedInitializationOffset);
  796.       put_word (abfd,
  797.         (bfd_vma) nlm_extended_header (abfd)->SharedExitProcedureOffset,
  798.         (bfd_byte *) thdr.SharedExitProcedureOffset);
  799.       put_word (abfd,
  800.         (bfd_vma) nlm_extended_header (abfd)->productID,
  801.         (bfd_byte *) thdr.productID);
  802.       put_word (abfd,
  803.         (bfd_vma) nlm_extended_header (abfd)->reserved0,
  804.         (bfd_byte *) thdr.reserved0);
  805.       put_word (abfd,
  806.         (bfd_vma) nlm_extended_header (abfd)->reserved1,
  807.         (bfd_byte *) thdr.reserved1);
  808.       put_word (abfd,
  809.         (bfd_vma) nlm_extended_header (abfd)->reserved2,
  810.         (bfd_byte *) thdr.reserved2);
  811.       put_word (abfd,
  812.         (bfd_vma) nlm_extended_header (abfd)->reserved3,
  813.         (bfd_byte *) thdr.reserved3);
  814.       put_word (abfd,
  815.         (bfd_vma) nlm_extended_header (abfd)->reserved4,
  816.         (bfd_byte *) thdr.reserved4);
  817.       put_word (abfd,
  818.         (bfd_vma) nlm_extended_header (abfd)->reserved5,
  819.         (bfd_byte *) thdr.reserved5);
  820.       if (bfd_write ((PTR) & thdr, sizeof (thdr), 1, abfd) != sizeof (thdr))
  821.     return false;
  822.     }
  823.  
  824.  
  825.   /* Write out the copyright header if there is one.  */
  826.   if (find_nonzero ((PTR) nlm_copyright_header (abfd),
  827.             sizeof (Nlm_Internal_Copyright_Header)))
  828.     {
  829.       Nlm_External_Copyright_Header thdr;
  830.  
  831.       memcpy (thdr.stamp, "CoPyRiGhT=", 10);
  832.       if (bfd_write ((PTR) thdr.stamp, sizeof (thdr.stamp), 1, abfd)
  833.       != sizeof (thdr.stamp))
  834.     return false;
  835.       thdr.copyrightMessageLength[0] =
  836.     nlm_copyright_header (abfd)->copyrightMessageLength;
  837.       if (bfd_write ((PTR) thdr.copyrightMessageLength, 1, 1, abfd) != 1)
  838.     return false;
  839.       /* The copyright message is a variable length string. */
  840.       if (bfd_write ((PTR) nlm_copyright_header (abfd)->copyrightMessage,
  841.              nlm_copyright_header (abfd)->copyrightMessageLength + 1,
  842.              1, abfd) !=
  843.       ((bfd_size_type)
  844.        nlm_copyright_header (abfd)->copyrightMessageLength + 1))
  845.     return false;
  846.     }
  847.  
  848.   /* Write out the custom header if there is one.   */
  849.   if (find_nonzero ((PTR) nlm_custom_header (abfd),
  850.             sizeof (Nlm_Internal_Custom_Header)))
  851.     {
  852.       Nlm_External_Custom_Header thdr;
  853.       boolean ds;
  854.       bfd_size_type hdrLength;
  855.  
  856.       ds = find_nonzero ((PTR) nlm_custom_header (abfd)->dataStamp,
  857.              sizeof (nlm_custom_header (abfd)->dataStamp));
  858.       memcpy (thdr.stamp, "CuStHeAd", 8);
  859.       hdrLength = (2 * NLM_TARGET_LONG_SIZE + (ds ? 8 : 0)
  860.            + nlm_custom_header (abfd)->hdrLength);
  861.       put_word (abfd, hdrLength, thdr.length);
  862.       put_word (abfd, (bfd_vma) nlm_custom_header (abfd)->dataOffset,
  863.         thdr.dataOffset);
  864.       put_word (abfd, (bfd_vma) nlm_custom_header (abfd)->dataLength,
  865.         thdr.dataLength);
  866.       if (! ds)
  867.     {
  868.       BFD_ASSERT (nlm_custom_header (abfd)->hdrLength == 0);
  869.       if (bfd_write ((PTR) &thdr, 1,
  870.              sizeof (thdr) - sizeof (thdr.dataStamp), abfd)
  871.           != sizeof (thdr) - sizeof (thdr.dataStamp))
  872.         return false;
  873.     }
  874.       else
  875.     {
  876.       memcpy (thdr.dataStamp, nlm_custom_header (abfd)->dataStamp,
  877.           sizeof (thdr.dataStamp));
  878.       if (bfd_write ((PTR) &thdr, sizeof (thdr), 1, abfd) != sizeof (thdr))
  879.         return false;
  880.       if (bfd_write (nlm_custom_header (abfd)->hdr, 1,
  881.              nlm_custom_header (abfd)->hdrLength, abfd)
  882.           != nlm_custom_header (abfd)->hdrLength)
  883.         return false;
  884.     }
  885.     }
  886.  
  887.   /* Write out the Cygnus debugging header if there is one.  */
  888.   if (find_nonzero ((PTR) nlm_cygnus_ext_header (abfd),
  889.             sizeof (Nlm_Internal_Cygnus_Ext_Header)))
  890.     {
  891.       Nlm_External_Custom_Header thdr;
  892.  
  893.       memcpy (thdr.stamp, "CuStHeAd", 8);
  894.       put_word (abfd, (bfd_vma) 2 * NLM_TARGET_LONG_SIZE + 8,
  895.         (bfd_byte *) thdr.length);
  896.       put_word (abfd, (bfd_vma) nlm_cygnus_ext_header (abfd)->offset,
  897.         (bfd_byte *) thdr.dataOffset);
  898.       put_word (abfd, (bfd_vma) nlm_cygnus_ext_header (abfd)->length,
  899.         (bfd_byte *) thdr.dataLength);
  900.       memcpy (thdr.dataStamp, "CyGnUsEx", 8);
  901.       if (bfd_write ((PTR) &thdr, sizeof (thdr), 1, abfd) != sizeof (thdr))
  902.     return false;
  903.     }
  904.  
  905.   return true;
  906. }
  907.  
  908. /* We read the NLM's public symbols and use it to generate a bfd symbol
  909.    table (hey, it's better than nothing) on a one-for-one basis.  Thus
  910.    use the number of public symbols as the number of bfd symbols we will
  911.    have once we actually get around to reading them in.
  912.  
  913.    Return the number of bytes required to hold the symtab vector, based on
  914.    the count plus 1, since we will NULL terminate the vector allocated based
  915.    on this size. */
  916.  
  917. long
  918. nlm_get_symtab_upper_bound (abfd)
  919.      bfd *abfd;
  920. {
  921.   Nlm_Internal_Fixed_Header *i_fxdhdrp;    /* Nlm file header, internal form */
  922.   long symcount;
  923.   long symtab_size = 0;
  924.  
  925.   i_fxdhdrp = nlm_fixed_header (abfd);
  926.   symcount = (i_fxdhdrp->numberOfPublics
  927.           + i_fxdhdrp->numberOfDebugRecords
  928.           + i_fxdhdrp->numberOfExternalReferences);
  929.   symtab_size = (symcount + 1) * (sizeof (asymbol));
  930.   return (symtab_size);
  931. }
  932.  
  933. /* Note that bfd_get_symcount is guaranteed to be zero if slurping the
  934.    symbol table fails. */
  935.  
  936. long
  937. nlm_get_symtab (abfd, alocation)
  938.      bfd *abfd;
  939.      asymbol **alocation;
  940. {
  941.   nlm_symbol_type *symbase;
  942.   bfd_size_type counter = 0;
  943.  
  944.   if (nlm_slurp_symbol_table (abfd) == false)
  945.     return -1;
  946.   symbase = nlm_get_symbols (abfd);
  947.   while (counter < bfd_get_symcount (abfd))
  948.     {
  949.       *alocation++ = &symbase->symbol;
  950.       symbase++;
  951.       counter++;
  952.     }
  953.   *alocation = (asymbol *) NULL;
  954.   return bfd_get_symcount (abfd);
  955. }
  956.  
  957. /* Make an NLM symbol.  There is nothing special to do here.  */
  958.  
  959. asymbol *
  960. nlm_make_empty_symbol (abfd)
  961.      bfd *abfd;
  962. {
  963.   nlm_symbol_type *new;
  964.  
  965.   new = (nlm_symbol_type *) bfd_zalloc (abfd, sizeof (nlm_symbol_type));
  966.   if (new)
  967.     new->symbol.the_bfd = abfd;
  968.   return &new->symbol;
  969. }
  970.  
  971. /* Get symbol information.  */
  972.  
  973. void
  974. nlm_get_symbol_info (ignore_abfd, symbol, ret)
  975.      bfd *ignore_abfd;
  976.      asymbol *symbol;
  977.      symbol_info *ret;
  978. {
  979.   bfd_symbol_info (symbol, ret);
  980. }
  981.  
  982. /* Print symbol information.  */
  983.  
  984. void
  985. nlm_print_symbol (abfd, afile, symbol, how)
  986.      bfd *abfd;
  987.      PTR afile;
  988.      asymbol *symbol;
  989.      bfd_print_symbol_type how;
  990. {
  991.   FILE *file = (FILE *) afile;
  992.  
  993.   switch (how)
  994.     {
  995.     case bfd_print_symbol_name:
  996.     case bfd_print_symbol_more:
  997.       if (symbol->name)
  998.     fprintf (file, "%s", symbol->name);
  999.       break;
  1000.     case bfd_print_symbol_all:
  1001.       bfd_print_symbol_vandf ((PTR) file, symbol);
  1002.       fprintf (file, " %-5s", symbol->section->name);
  1003.       if (symbol->name)
  1004.     fprintf (file, " %s", symbol->name);
  1005.       break;
  1006.     }
  1007. }
  1008.  
  1009. /* Slurp in nlm symbol table.
  1010.  
  1011.    In the external (in-file) form, NLM export records are variable length,
  1012.    with the following form:
  1013.  
  1014.     1 byte        length of the symbol name (N)
  1015.     N bytes        the symbol name
  1016.     4 bytes        the symbol offset from start of it's section
  1017.  
  1018.    We also read in the debugging symbols and import records.  Import
  1019.    records are treated as undefined symbols.  As we read the import
  1020.    records we also read in the associated reloc information, which is
  1021.    attached to the symbol.
  1022.  
  1023.    The bfd symbols are copied to SYMPTRS.
  1024.  
  1025.    When we return, the bfd symcount is either zero or contains the correct
  1026.    number of symbols.
  1027. */
  1028.  
  1029. static boolean
  1030. nlm_slurp_symbol_table (abfd)
  1031.      bfd *abfd;
  1032. {
  1033.   Nlm_Internal_Fixed_Header *i_fxdhdrp;    /* Nlm file header, internal form */
  1034.   bfd_size_type totsymcount;    /* Number of NLM symbols */
  1035.   bfd_size_type symcount;    /* Counter of NLM symbols */
  1036.   nlm_symbol_type *sym;        /* Pointer to current bfd symbol */
  1037.   unsigned char symlength;    /* Symbol length read into here */
  1038.   unsigned char symtype;    /* Type of debugging symbol */
  1039.   bfd_byte temp[NLM_TARGET_LONG_SIZE];    /* Symbol offsets read into here */
  1040.   boolean (*read_import_func) PARAMS ((bfd *, nlm_symbol_type *));
  1041.   boolean (*set_public_section_func) PARAMS ((bfd *, nlm_symbol_type *));
  1042.  
  1043.   if (nlm_get_symbols (abfd) != NULL)
  1044.     return (true);
  1045.  
  1046.   /* Read each raw NLM symbol, using the information to create a canonical bfd
  1047.      symbol table entry.
  1048.  
  1049.      Note that we allocate the initial bfd canonical symbol buffer based on a
  1050.      one-to-one mapping of the NLM symbols to canonical symbols.  We actually
  1051.      use all the NLM symbols, so there will be no space left over at the end.
  1052.      When we have all the symbols, we build the caller's pointer vector. */
  1053.  
  1054.   abfd->symcount = 0;
  1055.   i_fxdhdrp = nlm_fixed_header (abfd);
  1056.   totsymcount = (i_fxdhdrp->numberOfPublics
  1057.          + i_fxdhdrp->numberOfDebugRecords
  1058.          + i_fxdhdrp->numberOfExternalReferences);
  1059.   if (totsymcount == 0)
  1060.     {
  1061.       return (true);
  1062.     }
  1063.  
  1064.   if (bfd_seek (abfd, i_fxdhdrp->publicsOffset, SEEK_SET) == -1)
  1065.     return (false);
  1066.  
  1067.   sym = ((nlm_symbol_type *)
  1068.      bfd_zalloc (abfd, totsymcount * sizeof (nlm_symbol_type)));
  1069.   if (!sym)
  1070.     return false;
  1071.   nlm_set_symbols (abfd, sym);
  1072.  
  1073.   /* We use the bfd's symcount directly as the control count, so that early
  1074.      termination of the loop leaves the symcount correct for the symbols that
  1075.      were read. */
  1076.  
  1077.   set_public_section_func = nlm_set_public_section_func (abfd);
  1078.   symcount = i_fxdhdrp->numberOfPublics;
  1079.   while (abfd->symcount < symcount)
  1080.     {
  1081.       if (bfd_read ((PTR) & symlength, sizeof (symlength), 1, abfd)
  1082.       != sizeof (symlength))
  1083.     return (false);
  1084.       sym->symbol.the_bfd = abfd;
  1085.       sym->symbol.name = bfd_alloc (abfd, symlength + 1);
  1086.       if (!sym->symbol.name)
  1087.     return false;
  1088.       if (bfd_read ((PTR) sym->symbol.name, symlength, 1, abfd)
  1089.       != symlength)
  1090.     return (false);
  1091.       /* Cast away const.  */
  1092.       ((char *) (sym->symbol.name))[symlength] = '\0';
  1093.       if (bfd_read ((PTR) temp, sizeof (temp), 1, abfd) != sizeof (temp))
  1094.     return (false);
  1095.       sym->symbol.flags = BSF_GLOBAL | BSF_EXPORT;
  1096.       sym->symbol.value = get_word (abfd, temp);
  1097.       if (set_public_section_func)
  1098.     {
  1099.       /* Most backends can use the code below, but unfortunately
  1100.          some use a different scheme.  */
  1101.       if ((*set_public_section_func) (abfd, sym) == false)
  1102.         return false;
  1103.     }
  1104.       else
  1105.     {
  1106.       if (sym->symbol.value & NLM_HIBIT)
  1107.         {
  1108.           sym->symbol.value &= ~NLM_HIBIT;
  1109.           sym->symbol.flags |= BSF_FUNCTION;
  1110.           sym->symbol.section =
  1111.         bfd_get_section_by_name (abfd, NLM_CODE_NAME);
  1112.         }
  1113.       else
  1114.         {
  1115.           sym->symbol.section =
  1116.         bfd_get_section_by_name (abfd, NLM_INITIALIZED_DATA_NAME);
  1117.         }
  1118.     }
  1119.       sym->rcnt = 0;
  1120.       abfd->symcount++;
  1121.       sym++;
  1122.     }
  1123.  
  1124.   /* Read the debugging records.  */
  1125.  
  1126.   if (i_fxdhdrp->numberOfDebugRecords > 0)
  1127.     {
  1128.       if (bfd_seek (abfd, i_fxdhdrp->debugInfoOffset, SEEK_SET) == -1)
  1129.     return (false);
  1130.  
  1131.       symcount += i_fxdhdrp->numberOfDebugRecords;
  1132.       while (abfd->symcount < symcount)
  1133.     {
  1134.       if ((bfd_read ((PTR) & symtype, sizeof (symtype), 1, abfd)
  1135.            != sizeof (symtype))
  1136.        || bfd_read ((PTR) temp, sizeof (temp), 1, abfd) != sizeof (temp)
  1137.           || (bfd_read ((PTR) & symlength, sizeof (symlength), 1, abfd)
  1138.           != sizeof (symlength)))
  1139.         return false;
  1140.       sym->symbol.the_bfd = abfd;
  1141.       sym->symbol.name = bfd_alloc (abfd, symlength + 1);
  1142.       if (!sym->symbol.name)
  1143.         return false;
  1144.       if (bfd_read ((PTR) sym->symbol.name, symlength, 1, abfd)
  1145.           != symlength)
  1146.         return (false);
  1147.       /* Cast away const.  */
  1148.       ((char *) (sym->symbol.name))[symlength] = '\0';
  1149.       sym->symbol.flags = BSF_LOCAL;
  1150.       sym->symbol.value = get_word (abfd, temp);
  1151.       if (symtype == 0)
  1152.         {
  1153.           sym->symbol.section =
  1154.         bfd_get_section_by_name (abfd, NLM_INITIALIZED_DATA_NAME);
  1155.         }
  1156.       else if (symtype == 1)
  1157.         {
  1158.           sym->symbol.flags |= BSF_FUNCTION;
  1159.           sym->symbol.section =
  1160.         bfd_get_section_by_name (abfd, NLM_CODE_NAME);
  1161.         }
  1162.       else
  1163.         {
  1164.           sym->symbol.section = bfd_abs_section_ptr;
  1165.         }
  1166.       sym->rcnt = 0;
  1167.       abfd->symcount++;
  1168.       sym++;
  1169.     }
  1170.     }
  1171.  
  1172.   /* Read in the import records.  We can only do this if we know how
  1173.      to read relocs for this target.  */
  1174.  
  1175.   read_import_func = nlm_read_import_func (abfd);
  1176.   if (read_import_func != NULL)
  1177.     {
  1178.       if (bfd_seek (abfd, i_fxdhdrp->externalReferencesOffset, SEEK_SET)
  1179.       == -1)
  1180.     return (false);
  1181.  
  1182.       symcount += i_fxdhdrp->numberOfExternalReferences;
  1183.       while (abfd->symcount < symcount)
  1184.     {
  1185.       if ((*read_import_func) (abfd, sym) == false)
  1186.         return false;
  1187.       sym++;
  1188.       abfd->symcount++;
  1189.     }
  1190.     }
  1191.  
  1192.   return (true);
  1193. }
  1194.  
  1195. /* Get the relocs for an NLM file.  There are two types of relocs.
  1196.    Imports are relocs against symbols defined in other NLM files.  We
  1197.    treat these as relocs against global symbols.  Relocation fixups
  1198.    are internal relocs.
  1199.  
  1200.    The actual format used to store the relocs is machine specific.  */
  1201.  
  1202. /* Read in the relocation fixup information.  This is stored in
  1203.    nlm_relocation_fixups, an array of arelent structures, and
  1204.    nlm_relocation_fixup_secs, an array of section pointers.  The
  1205.    section pointers are needed because the relocs are not sorted by
  1206.    section.  */
  1207.  
  1208. static boolean
  1209. nlm_slurp_reloc_fixups (abfd)
  1210.      bfd *abfd;
  1211. {
  1212.   boolean (*read_func) PARAMS ((bfd *, nlm_symbol_type *, asection **,
  1213.                 arelent *));
  1214.   bfd_size_type count;
  1215.   arelent *rels;
  1216.   asection **secs;
  1217.  
  1218.   if (nlm_relocation_fixups (abfd) != NULL)
  1219.     return true;
  1220.   read_func = nlm_read_reloc_func (abfd);
  1221.   if (read_func == NULL)
  1222.     return true;
  1223.  
  1224.   if (bfd_seek (abfd, nlm_fixed_header (abfd)->relocationFixupOffset,
  1225.         SEEK_SET) != 0)
  1226.     return false;
  1227.  
  1228.   count = nlm_fixed_header (abfd)->numberOfRelocationFixups;
  1229.   rels = (arelent *) bfd_alloc (abfd, count * sizeof (arelent));
  1230.   secs = (asection **) bfd_alloc (abfd, count * sizeof (asection *));
  1231.   if ((rels == NULL || secs == NULL) && count != 0)
  1232.     return false;
  1233.   nlm_relocation_fixups (abfd) = rels;
  1234.   nlm_relocation_fixup_secs (abfd) = secs;
  1235.  
  1236.   /* We have to read piece by piece, because we don't know how large
  1237.      the machine specific reloc information is.  */
  1238.   while (count-- != 0)
  1239.     {
  1240.       if ((*read_func) (abfd, (nlm_symbol_type *) NULL, secs, rels) == false)
  1241.     {
  1242.       nlm_relocation_fixups (abfd) = NULL;
  1243.       nlm_relocation_fixup_secs (abfd) = NULL;
  1244.       return false;
  1245.     }
  1246.       ++secs;
  1247.       ++rels;
  1248.     }
  1249.  
  1250.   return true;
  1251. }
  1252.  
  1253. /* Get the number of relocs.  This really just returns an upper bound,
  1254.    since it does not attempt to distinguish them based on the section.
  1255.    That will be handled when they are actually read.  */
  1256.  
  1257. long
  1258. nlm_get_reloc_upper_bound (abfd, sec)
  1259.      bfd *abfd;
  1260.      asection *sec;
  1261. {
  1262.   nlm_symbol_type *syms;
  1263.   bfd_size_type count;
  1264.   unsigned int ret;
  1265.  
  1266.   /* If we don't know how to read relocs, just return 0.  */
  1267.   if (nlm_read_reloc_func (abfd) == NULL)
  1268.     return -1;
  1269.   /* Make sure we have either the code or the data section.  */
  1270.   if ((bfd_get_section_flags (abfd, sec) & (SEC_CODE | SEC_DATA)) == 0)
  1271.     return 0;
  1272.  
  1273.   syms = nlm_get_symbols (abfd);
  1274.   if (syms == NULL)
  1275.     {
  1276.       if (nlm_slurp_symbol_table (abfd) == false)
  1277.     return -1;
  1278.       syms = nlm_get_symbols (abfd);
  1279.     }
  1280.  
  1281.   ret = nlm_fixed_header (abfd)->numberOfRelocationFixups;
  1282.  
  1283.   count = bfd_get_symcount (abfd);
  1284.   while (count-- != 0)
  1285.     {
  1286.       ret += syms->rcnt;
  1287.       ++syms;
  1288.     }
  1289.  
  1290.   return (ret + 1) * sizeof (arelent *);
  1291. }
  1292.  
  1293. /* Get the relocs themselves.  */
  1294.  
  1295. long
  1296. nlm_canonicalize_reloc (abfd, sec, relptr, symbols)
  1297.      bfd *abfd;
  1298.      asection *sec;
  1299.      arelent **relptr;
  1300.      asymbol **symbols;
  1301. {
  1302.   arelent *rels;
  1303.   asection **secs;
  1304.   bfd_size_type count, i;
  1305.   unsigned int ret;
  1306.  
  1307.   /* Get the relocation fixups.  */
  1308.   rels = nlm_relocation_fixups (abfd);
  1309.   if (rels == NULL)
  1310.     {
  1311.       if (nlm_slurp_reloc_fixups (abfd) == false)
  1312.     return -1;
  1313.       rels = nlm_relocation_fixups (abfd);
  1314.     }
  1315.   secs = nlm_relocation_fixup_secs (abfd);
  1316.  
  1317.   ret = 0;
  1318.   count = nlm_fixed_header (abfd)->numberOfRelocationFixups;
  1319.   for (i = 0; i < count; i++, rels++, secs++)
  1320.     {
  1321.       if (*secs == sec)
  1322.     {
  1323.       *relptr++ = rels;
  1324.       ++ret;
  1325.     }
  1326.     }
  1327.  
  1328.   /* Get the import symbols.  */
  1329.   count = bfd_get_symcount (abfd);
  1330.   for (i = 0; i < count; i++, symbols++)
  1331.     {
  1332.       asymbol *sym;
  1333.  
  1334.       sym = *symbols;
  1335.       if (bfd_asymbol_flavour (sym) == bfd_target_nlm_flavour)
  1336.     {
  1337.       nlm_symbol_type *nlm_sym;
  1338.       bfd_size_type j;
  1339.  
  1340.       nlm_sym = (nlm_symbol_type *) sym;
  1341.       for (j = 0; j < nlm_sym->rcnt; j++)
  1342.         {
  1343.           if (nlm_sym->relocs[j].section == sec)
  1344.         {
  1345.           *relptr = &nlm_sym->relocs[j].reloc;
  1346.           (*relptr)->sym_ptr_ptr = symbols;
  1347.           ++relptr;
  1348.           ++ret;
  1349.         }
  1350.         }
  1351.     }
  1352.     }
  1353.  
  1354.   *relptr = NULL;
  1355.  
  1356.   return ret;
  1357. }
  1358.  
  1359. /* Compute the section file positions for an NLM file.  All variable
  1360.    length data in the file headers must be set before this function is
  1361.    called.  If the variable length data is changed later, the
  1362.    resulting object file will be incorrect.  Unfortunately, there is
  1363.    no way to check this.
  1364.  
  1365.    This routine also sets the Size and Offset fields in the fixed
  1366.    header.
  1367.  
  1368.    It also looks over the symbols and moves any common symbols into
  1369.    the .bss section; NLM has no way to represent a common symbol.
  1370.    This approach means that either the symbols must already have been
  1371.    set at this point, or there must be no common symbols.  We need to
  1372.    move the symbols at this point so that mangle_relocs can see the
  1373.    final values.  */
  1374.  
  1375. static boolean
  1376. nlm_compute_section_file_positions (abfd)
  1377.      bfd *abfd;
  1378. {
  1379.   file_ptr sofar;
  1380.   asection *sec;
  1381.   bfd_vma text, data, bss;
  1382.   bfd_vma text_low, data_low;
  1383.   unsigned int text_align, data_align, other_align;
  1384.   file_ptr text_ptr, data_ptr, other_ptr;
  1385.   asection *bss_sec;
  1386.   asymbol **sym_ptr_ptr;
  1387.  
  1388.   if (abfd->output_has_begun == true)
  1389.     return true;
  1390.  
  1391.   /* Make sure we have a section to hold uninitialized data.  */
  1392.   bss_sec = bfd_get_section_by_name (abfd, NLM_UNINITIALIZED_DATA_NAME);
  1393.   if (bss_sec == NULL)
  1394.     {
  1395.       if (!add_bfd_section (abfd, NLM_UNINITIALIZED_DATA_NAME,
  1396.                 (file_ptr) 0, (bfd_size_type) 0,
  1397.                 SEC_ALLOC))
  1398.     return false;
  1399.       bss_sec = bfd_get_section_by_name (abfd, NLM_UNINITIALIZED_DATA_NAME);
  1400.     }
  1401.  
  1402.   abfd->output_has_begun = true;
  1403.  
  1404.   /* The fixed header.  */
  1405.   sofar = nlm_optional_prefix_size (abfd) + nlm_fixed_header_size (abfd);
  1406.  
  1407.   /* The variable header.  */
  1408.   sofar += (sizeof (nlm_variable_header (abfd)->descriptionLength)
  1409.         + nlm_variable_header (abfd)->descriptionLength + 1
  1410.         + NLM_TARGET_LONG_SIZE    /* stackSize */
  1411.         + NLM_TARGET_LONG_SIZE    /* reserved */
  1412.         + sizeof (nlm_variable_header (abfd)->oldThreadName)
  1413.         + sizeof (nlm_variable_header (abfd)->screenNameLength)
  1414.         + nlm_variable_header (abfd)->screenNameLength + 1
  1415.         + sizeof (nlm_variable_header (abfd)->threadNameLength)
  1416.         + nlm_variable_header (abfd)->threadNameLength + 1);
  1417.  
  1418.   /* The auxiliary headers.  */
  1419.   if (find_nonzero ((PTR) nlm_version_header (abfd),
  1420.             sizeof (Nlm_Internal_Version_Header)))
  1421.     sofar += sizeof (Nlm_External_Version_Header);
  1422.   if (find_nonzero ((PTR) nlm_extended_header (abfd),
  1423.             sizeof (Nlm_Internal_Extended_Header)))
  1424.     sofar += sizeof (Nlm_External_Extended_Header);
  1425.   if (find_nonzero ((PTR) nlm_copyright_header (abfd),
  1426.             sizeof (Nlm_Internal_Copyright_Header)))
  1427.     sofar += (sizeof (Nlm_External_Copyright_Header)
  1428.           + nlm_copyright_header (abfd)->copyrightMessageLength + 1);
  1429.   if (find_nonzero ((PTR) nlm_custom_header (abfd),
  1430.             sizeof (Nlm_Internal_Custom_Header)))
  1431.     sofar += (sizeof (Nlm_External_Custom_Header)
  1432.           + nlm_custom_header (abfd)->hdrLength);
  1433.   if (find_nonzero ((PTR) nlm_cygnus_ext_header (abfd),
  1434.             sizeof (Nlm_Internal_Cygnus_Ext_Header)))
  1435.     sofar += sizeof (Nlm_External_Custom_Header);
  1436.  
  1437.   /* Compute the section file positions in two passes.  First get the
  1438.      sizes of the text and data sections, and then set the file
  1439.      positions.  This code aligns the sections in the file using the
  1440.      same alignment restrictions that apply to the sections in memory;
  1441.      this may not be necessary.  */
  1442.   text = 0;
  1443.   text_low = (bfd_vma) - 1;
  1444.   text_align = 0;
  1445.   data = 0;
  1446.   data_low = (bfd_vma) - 1;
  1447.   data_align = 0;
  1448.   bss = 0;
  1449.   other_align = 0;
  1450.   for (sec = abfd->sections; sec != (asection *) NULL; sec = sec->next)
  1451.     {
  1452.       flagword f;
  1453.  
  1454.       sec->_raw_size = BFD_ALIGN (sec->_raw_size, 1 << sec->alignment_power);
  1455.  
  1456.       f = bfd_get_section_flags (abfd, sec);
  1457.       if (f & SEC_CODE)
  1458.     {
  1459.       text += sec->_raw_size;
  1460.       if (bfd_get_section_vma (abfd, sec) < text_low)
  1461.         text_low = bfd_get_section_vma (abfd, sec);
  1462.       if (sec->alignment_power > text_align)
  1463.         text_align = sec->alignment_power;
  1464.     }
  1465.       else if (f & SEC_DATA)
  1466.     {
  1467.       data += sec->_raw_size;
  1468.       if (bfd_get_section_vma (abfd, sec) < data_low)
  1469.         data_low = bfd_get_section_vma (abfd, sec);
  1470.       if (sec->alignment_power > data_align)
  1471.         data_align = sec->alignment_power;
  1472.     }
  1473.       else if (f & SEC_HAS_CONTENTS)
  1474.     {
  1475.       if (sec->alignment_power > other_align)
  1476.         other_align = sec->alignment_power;
  1477.     }
  1478.       else if (f & SEC_ALLOC)
  1479.     bss += sec->_raw_size;
  1480.     }
  1481.  
  1482.   nlm_set_text_low (abfd, text_low);
  1483.   nlm_set_data_low (abfd, data_low);
  1484.  
  1485.   if (nlm_no_uninitialized_data (abfd))
  1486.     {
  1487.       /* This NetWare format does not use uninitialized data.  We must
  1488.      increase the size of the data section.  We will never wind up
  1489.      writing those file locations, so they will remain zero.  */
  1490.       data += bss;
  1491.       bss = 0;
  1492.     }
  1493.  
  1494.   text_ptr = BFD_ALIGN (sofar, 1 << text_align);
  1495.   data_ptr = BFD_ALIGN (text_ptr + text, 1 << data_align);
  1496.   other_ptr = BFD_ALIGN (data_ptr + data, 1 << other_align);
  1497.  
  1498.   /* Fill in some fields in the header for which we now have the
  1499.      information.  */
  1500.   nlm_fixed_header (abfd)->codeImageOffset = text_ptr;
  1501.   nlm_fixed_header (abfd)->codeImageSize = text;
  1502.   nlm_fixed_header (abfd)->dataImageOffset = data_ptr;
  1503.   nlm_fixed_header (abfd)->dataImageSize = data;
  1504.   nlm_fixed_header (abfd)->uninitializedDataSize = bss;
  1505.  
  1506.   for (sec = abfd->sections; sec != (asection *) NULL; sec = sec->next)
  1507.     {
  1508.       flagword f;
  1509.  
  1510.       f = bfd_get_section_flags (abfd, sec);
  1511.  
  1512.       if (f & SEC_CODE)
  1513.     {
  1514.       sec->filepos = text_ptr;
  1515.       text_ptr += sec->_raw_size;
  1516.     }
  1517.       else if (f & SEC_DATA)
  1518.     {
  1519.       sec->filepos = data_ptr;
  1520.       data_ptr += sec->_raw_size;
  1521.     }
  1522.       else if (f & SEC_HAS_CONTENTS)
  1523.     {
  1524.       sec->filepos = other_ptr;
  1525.       other_ptr += sec->_raw_size;
  1526.     }
  1527.     }
  1528.  
  1529.   nlm_fixed_header (abfd)->relocationFixupOffset = other_ptr;
  1530.  
  1531.   /* Move all common symbols into the .bss section.  */
  1532.  
  1533.   sym_ptr_ptr = bfd_get_outsymbols (abfd);
  1534.   if (sym_ptr_ptr != NULL)
  1535.     {
  1536.       asymbol **sym_end;
  1537.       bfd_vma add;
  1538.  
  1539.       sym_end = sym_ptr_ptr + bfd_get_symcount (abfd);
  1540.       add = 0;
  1541.       for (; sym_ptr_ptr < sym_end; sym_ptr_ptr++)
  1542.     {
  1543.       asymbol *sym;
  1544.       bfd_vma size;
  1545.  
  1546.       sym = *sym_ptr_ptr;
  1547.  
  1548.       if (!bfd_is_com_section (bfd_get_section (sym)))
  1549.         continue;
  1550.  
  1551.       /* Put the common symbol in the .bss section, and increase
  1552.          the size of the .bss section by the size of the common
  1553.          symbol (which is the old value of the symbol).  */
  1554.       sym->section = bss_sec;
  1555.       size = sym->value;
  1556.       sym->value = bss_sec->_raw_size + add;
  1557.       add += size;
  1558.       add = BFD_ALIGN (add, 1 << bss_sec->alignment_power);
  1559.     }
  1560.       if (add != 0)
  1561.     {
  1562.       if (nlm_no_uninitialized_data (abfd))
  1563.         {
  1564.           /* We could handle this case, but so far it hasn't been
  1565.          necessary.  */
  1566.           abort ();
  1567.         }
  1568.       nlm_fixed_header (abfd)->uninitializedDataSize += add;
  1569.       bss_sec->_raw_size += add;
  1570.     }
  1571.     }
  1572.  
  1573.   return true;
  1574. }
  1575.  
  1576. /* Set the contents of a section.  To do this we need to know where
  1577.    the section is going to be located in the output file.  That means
  1578.    that the sizes of all the sections must be set, and all the
  1579.    variable size header information must be known.  */
  1580.  
  1581. boolean
  1582. nlm_set_section_contents (abfd, section, location, offset, count)
  1583.      bfd *abfd;
  1584.      asection *section;
  1585.      PTR location;
  1586.      file_ptr offset;
  1587.      bfd_size_type count;
  1588. {
  1589.   if (abfd->output_has_begun == false
  1590.       && nlm_compute_section_file_positions (abfd) == false)
  1591.     return false;
  1592.  
  1593.   if (count == 0)
  1594.     return true;
  1595.  
  1596.   /* i386 NetWare has a very restricted set of relocs.  In order for
  1597.      objcopy to work, the NLM i386 backend needs a chance to rework
  1598.      the section contents so that its set of relocs will work.  If all
  1599.      the relocs are already acceptable, this will not do anything.  */
  1600.   if (section->reloc_count != 0)
  1601.     {
  1602.       boolean (*mangle_relocs_func) PARAMS ((bfd *, asection *, PTR,
  1603.                          bfd_vma, bfd_size_type));
  1604.  
  1605.       mangle_relocs_func = nlm_mangle_relocs_func (abfd);
  1606.       if (mangle_relocs_func != NULL)
  1607.     {
  1608.       if (!(*mangle_relocs_func) (abfd, section, location,
  1609.                       (bfd_vma) offset, count))
  1610.         return false;
  1611.     }
  1612.     }
  1613.  
  1614.   if (bfd_seek (abfd, (file_ptr) (section->filepos + offset), SEEK_SET) != 0
  1615.       || bfd_write (location, 1, count, abfd) != count)
  1616.     return false;
  1617.  
  1618.   return true;
  1619. }
  1620.  
  1621. /* We need to sort a list of relocs associated with sections when we
  1622.    write out the external relocs.  */
  1623.  
  1624. static int
  1625. nlm_external_reloc_compare (p1, p2)
  1626.      const void *p1;
  1627.      const void *p2;
  1628. {
  1629.   const struct reloc_and_sec *r1 = (const struct reloc_and_sec *) p1;
  1630.   const struct reloc_and_sec *r2 = (const struct reloc_and_sec *) p2;
  1631.   int cmp;
  1632.  
  1633.   cmp = strcmp ((*r1->rel->sym_ptr_ptr)->name,
  1634.         (*r2->rel->sym_ptr_ptr)->name);
  1635.   if (cmp != 0)
  1636.     return cmp;
  1637.  
  1638.   /* We sort by address within symbol to make the sort more stable and
  1639.      increase the chances that different hosts will generate bit for
  1640.      bit equivalent results.  */
  1641.   return (int) (r1->rel->address - r2->rel->address);
  1642. }
  1643.  
  1644. /* Write out an NLM file.  We write out the information in this order:
  1645.      fixed header
  1646.      variable header
  1647.      auxiliary headers
  1648.      code sections
  1649.      data sections
  1650.      other sections (custom data, messages, help, shared NLM, RPC,
  1651.                   module dependencies)
  1652.      relocation fixups
  1653.      external references (imports)
  1654.      public symbols (exports)
  1655.      debugging records
  1656.    This is similar to the order used by the NetWare tools; the
  1657.    difference is that NetWare puts the sections other than code, data
  1658.    and custom data at the end of the NLM.  It is convenient for us to
  1659.    know where the sections are going to be before worrying about the
  1660.    size of the other information.
  1661.  
  1662.    By the time this function is called, all the section data should
  1663.    have been output using set_section_contents.  Note that custom
  1664.    data, the message file, the help file, the shared NLM file, the RPC
  1665.    data, and the module dependencies are all considered to be
  1666.    sections; the caller is responsible for filling in the offset and
  1667.    length fields in the NLM headers.  The relocation fixups and
  1668.    imports are both obtained from the list of relocs attached to each
  1669.    section.  The exports and debugging records are obtained from the
  1670.    list of outsymbols.  */
  1671.  
  1672. boolean
  1673. nlm_write_object_contents (abfd)
  1674.      bfd *abfd;
  1675. {
  1676.   asection *sec;
  1677.   boolean (*write_import_func) PARAMS ((bfd *, asection *, arelent *));
  1678.   bfd_size_type external_reloc_count, internal_reloc_count, i, c;
  1679.   struct reloc_and_sec *external_relocs;
  1680.   asymbol **sym_ptr_ptr;
  1681.   file_ptr last;
  1682.   boolean (*write_prefix_func) PARAMS ((bfd *));
  1683.   unsigned char *fixed_header = NULL;
  1684.  
  1685.   fixed_header = ((unsigned char *)
  1686.           bfd_malloc ((size_t) nlm_fixed_header_size (abfd)));
  1687.   if (fixed_header == NULL)
  1688.     goto error_return;
  1689.  
  1690.   if (abfd->output_has_begun == false
  1691.       && nlm_compute_section_file_positions (abfd) == false)
  1692.     goto error_return;
  1693.  
  1694.   /* Write out the variable length headers.  */
  1695.   if (bfd_seek (abfd,
  1696.          nlm_optional_prefix_size (abfd) + nlm_fixed_header_size (abfd),
  1697.         SEEK_SET) != 0)
  1698.     goto error_return;
  1699.   if (nlm_swap_variable_header_out (abfd) == false
  1700.       || nlm_swap_auxiliary_headers_out (abfd) == false)
  1701.     {
  1702.       bfd_set_error (bfd_error_system_call);
  1703.       goto error_return;
  1704.     }
  1705.  
  1706.   /* A weak check on whether the section file positions were
  1707.      reasonable.  */
  1708.   if (bfd_tell (abfd) > nlm_fixed_header (abfd)->codeImageOffset)
  1709.     {
  1710.       bfd_set_error (bfd_error_invalid_operation);
  1711.       goto error_return;
  1712.     }
  1713.  
  1714.   /* Advance to the relocs.  */
  1715.   if (bfd_seek (abfd, nlm_fixed_header (abfd)->relocationFixupOffset,
  1716.         SEEK_SET) != 0)
  1717.     goto error_return;
  1718.  
  1719.   /* The format of the relocation entries is dependent upon the
  1720.      particular target.  We use an external routine to write the reloc
  1721.      out.  */
  1722.   write_import_func = nlm_write_import_func (abfd);
  1723.  
  1724.   /* Write out the internal relocation fixups.  While we're looping
  1725.      over the relocs, we also count the external relocs, which is
  1726.      needed when they are written out below.  */
  1727.   internal_reloc_count = 0;
  1728.   external_reloc_count = 0;
  1729.   for (sec = abfd->sections; sec != (asection *) NULL; sec = sec->next)
  1730.     {
  1731.       arelent **rel_ptr_ptr, **rel_end;
  1732.  
  1733.       if (sec->reloc_count == 0)
  1734.     continue;
  1735.  
  1736.       /* We can only represent relocs within a code or data
  1737.      section.  We ignore them for a debugging section.  */
  1738.       if ((bfd_get_section_flags (abfd, sec) & (SEC_CODE | SEC_DATA)) == 0)
  1739.     continue;
  1740.  
  1741.       /* We need to know how to write out imports */
  1742.       if (write_import_func == NULL)
  1743.     {
  1744.       bfd_set_error (bfd_error_invalid_operation);
  1745.       goto error_return;
  1746.     }
  1747.  
  1748.       rel_ptr_ptr = sec->orelocation;
  1749.       rel_end = rel_ptr_ptr + sec->reloc_count;
  1750.       for (; rel_ptr_ptr < rel_end; rel_ptr_ptr++)
  1751.     {
  1752.       arelent *rel;
  1753.       asymbol *sym;
  1754.  
  1755.       rel = *rel_ptr_ptr;
  1756.       sym = *rel->sym_ptr_ptr;
  1757.  
  1758.       if (! bfd_is_und_section (bfd_get_section (sym)))
  1759.         {
  1760.           ++internal_reloc_count;
  1761.           if ((*write_import_func) (abfd, sec, rel) == false)
  1762.         goto error_return;
  1763.         }
  1764.       else
  1765.         ++external_reloc_count;
  1766.     }
  1767.     }
  1768.   nlm_fixed_header (abfd)->numberOfRelocationFixups = internal_reloc_count;
  1769.  
  1770.   /* Write out the imports (relocs against external symbols).  These
  1771.      are output as a symbol name followed by all the relocs for that
  1772.      symbol, so we must first gather together all the relocs against
  1773.      external symbols and sort them.  */
  1774.   external_relocs =
  1775.     (struct reloc_and_sec *) bfd_alloc (abfd,
  1776.                     (external_reloc_count
  1777.                      * sizeof (struct reloc_and_sec)));
  1778.   if (external_relocs == (struct reloc_and_sec *) NULL)
  1779.     goto error_return;
  1780.   i = 0;
  1781.   for (sec = abfd->sections; sec != (asection *) NULL; sec = sec->next)
  1782.     {
  1783.       arelent **rel_ptr_ptr, **rel_end;
  1784.  
  1785.       if (sec->reloc_count == 0)
  1786.     continue;
  1787.  
  1788.       rel_ptr_ptr = sec->orelocation;
  1789.       rel_end = rel_ptr_ptr + sec->reloc_count;
  1790.       for (; rel_ptr_ptr < rel_end; rel_ptr_ptr++)
  1791.     {
  1792.       arelent *rel;
  1793.       asymbol *sym;
  1794.  
  1795.       rel = *rel_ptr_ptr;
  1796.       sym = *rel->sym_ptr_ptr;
  1797.  
  1798.       if (! bfd_is_und_section (bfd_get_section (sym)))
  1799.         continue;
  1800.  
  1801.       external_relocs[i].rel = rel;
  1802.       external_relocs[i].sec = sec;
  1803.       ++i;
  1804.     }
  1805.     }
  1806.  
  1807.   BFD_ASSERT (i == external_reloc_count);
  1808.  
  1809.   /* Sort the external relocs by name.  */
  1810.   qsort ((PTR) external_relocs, (size_t) external_reloc_count,
  1811.      sizeof (struct reloc_and_sec), nlm_external_reloc_compare);
  1812.  
  1813.   /* Write out the external relocs.  */
  1814.   nlm_fixed_header (abfd)->externalReferencesOffset = bfd_tell (abfd);
  1815.   c = 0;
  1816.   i = 0;
  1817.   while (i < external_reloc_count)
  1818.     {
  1819.       arelent *rel;
  1820.       asymbol *sym;
  1821.       bfd_size_type j, cnt;
  1822.  
  1823.       ++c;
  1824.  
  1825.       rel = external_relocs[i].rel;
  1826.       sym = *rel->sym_ptr_ptr;
  1827.  
  1828.       cnt = 0;
  1829.       for (j = i;
  1830.        (j < external_reloc_count
  1831.         && *external_relocs[j].rel->sym_ptr_ptr == sym);
  1832.        j++)
  1833.     ++cnt;
  1834.  
  1835.       if ((*nlm_write_external_func (abfd)) (abfd, cnt, sym,
  1836.                          &external_relocs[i])
  1837.       == false)
  1838.     goto error_return;
  1839.  
  1840.       i += cnt;
  1841.     }
  1842.  
  1843.   nlm_fixed_header (abfd)->numberOfExternalReferences = c;
  1844.  
  1845.   /* Write out the public symbols (exports).  */
  1846.   sym_ptr_ptr = bfd_get_outsymbols (abfd);
  1847.   if (sym_ptr_ptr != (asymbol **) NULL)
  1848.     {
  1849.       bfd_vma (*get_public_offset_func) PARAMS ((bfd *, asymbol *));
  1850.       boolean (*write_export_func) PARAMS ((bfd *, asymbol *, bfd_vma));
  1851.  
  1852.       asymbol **sym_end;
  1853.  
  1854.       nlm_fixed_header (abfd)->publicsOffset = bfd_tell (abfd);
  1855.       get_public_offset_func = nlm_get_public_offset_func (abfd);
  1856.       write_export_func = nlm_write_export_func (abfd);
  1857.       c = 0;
  1858.       sym_end = sym_ptr_ptr + bfd_get_symcount (abfd);
  1859.       for (; sym_ptr_ptr < sym_end; sym_ptr_ptr++)
  1860.     {
  1861.       asymbol *sym;
  1862.       bfd_byte len;
  1863.       bfd_vma offset;
  1864.       bfd_byte temp[NLM_TARGET_LONG_SIZE];
  1865.  
  1866.       sym = *sym_ptr_ptr;
  1867.  
  1868.       if ((sym->flags & (BSF_EXPORT | BSF_GLOBAL)) == 0
  1869.           || bfd_is_und_section (bfd_get_section (sym)))
  1870.         continue;
  1871.  
  1872.       ++c;
  1873.  
  1874.       if (get_public_offset_func)
  1875.         {
  1876.           /* Most backends can use the code below, but
  1877.          unfortunately some use a different scheme.  */
  1878.           offset = (*get_public_offset_func) (abfd, sym);
  1879.         }
  1880.       else
  1881.         {
  1882.           offset = bfd_asymbol_value (sym);
  1883.           sec = sym->section;
  1884.           if (sec->flags & SEC_CODE)
  1885.         {
  1886.           offset -= nlm_get_text_low (abfd);
  1887.           offset |= NLM_HIBIT;
  1888.         }
  1889.           else if (sec->flags & (SEC_DATA | SEC_ALLOC))
  1890.         {
  1891.           /* SEC_ALLOC is for the .bss section.  */
  1892.           offset -= nlm_get_data_low (abfd);
  1893.         }
  1894.           else
  1895.         {
  1896.           /* We can't handle an exported symbol that is not in
  1897.              the code or data segment.  */
  1898.           bfd_set_error (bfd_error_invalid_operation);
  1899.           goto error_return;
  1900.         }
  1901.         }
  1902.  
  1903.       if (write_export_func)
  1904.         {
  1905.           if ((*write_export_func) (abfd, sym, offset) == false)
  1906.         goto error_return;
  1907.         }
  1908.       else
  1909.         {
  1910.           len = strlen (sym->name);
  1911.           if ((bfd_write (&len, sizeof (bfd_byte), 1, abfd)
  1912.            != sizeof (bfd_byte))
  1913.           || bfd_write (sym->name, len, 1, abfd) != len)
  1914.         goto error_return;
  1915.  
  1916.           put_word (abfd, offset, temp);
  1917.           if (bfd_write (temp, sizeof (temp), 1, abfd) != sizeof (temp))
  1918.         goto error_return;
  1919.         }
  1920.     }
  1921.       nlm_fixed_header (abfd)->numberOfPublics = c;
  1922.  
  1923.       /* Write out the debugging records.  The NLM conversion program
  1924.      wants to be able to inhibit this, so as a special hack if
  1925.      debugInfoOffset is set to -1 we don't write any debugging
  1926.      information.  This can not be handled by fiddling with the
  1927.      symbol table, because exported symbols appear in both the
  1928.      exported symbol list and the debugging information.  */
  1929.       if (nlm_fixed_header (abfd)->debugInfoOffset == (file_ptr) - 1)
  1930.     {
  1931.       nlm_fixed_header (abfd)->debugInfoOffset = 0;
  1932.       nlm_fixed_header (abfd)->numberOfDebugRecords = 0;
  1933.     }
  1934.       else
  1935.     {
  1936.       nlm_fixed_header (abfd)->debugInfoOffset = bfd_tell (abfd);
  1937.       c = 0;
  1938.       sym_ptr_ptr = bfd_get_outsymbols (abfd);
  1939.       sym_end = sym_ptr_ptr + bfd_get_symcount (abfd);
  1940.       for (; sym_ptr_ptr < sym_end; sym_ptr_ptr++)
  1941.         {
  1942.           asymbol *sym;
  1943.           bfd_byte type, len;
  1944.           bfd_vma offset;
  1945.           bfd_byte temp[NLM_TARGET_LONG_SIZE];
  1946.  
  1947.           sym = *sym_ptr_ptr;
  1948.  
  1949.           /* The NLM notion of a debugging symbol is actually what
  1950.          BFD calls a local or global symbol.  What BFD calls a
  1951.          debugging symbol NLM does not understand at all.  */
  1952.           if ((sym->flags & (BSF_LOCAL | BSF_GLOBAL | BSF_EXPORT)) == 0
  1953.           || (sym->flags & BSF_DEBUGGING) != 0
  1954.           || bfd_is_und_section (bfd_get_section (sym)))
  1955.         continue;
  1956.  
  1957.           ++c;
  1958.  
  1959.           offset = bfd_asymbol_value (sym);
  1960.           sec = sym->section;
  1961.           if (sec->flags & SEC_CODE)
  1962.         {
  1963.           offset -= nlm_get_text_low (abfd);
  1964.           type = 1;
  1965.         }
  1966.           else if (sec->flags & (SEC_DATA | SEC_ALLOC))
  1967.         {
  1968.           /* SEC_ALLOC is for the .bss section.  */
  1969.           offset -= nlm_get_data_low (abfd);
  1970.           type = 0;
  1971.         }
  1972.           else
  1973.         type = 2;
  1974.  
  1975.           /* The type is 0 for data, 1 for code, 2 for absolute.  */
  1976.           if (bfd_write (&type, sizeof (bfd_byte), 1, abfd)
  1977.           != sizeof (bfd_byte))
  1978.         goto error_return;
  1979.  
  1980.           put_word (abfd, offset, temp);
  1981.           if (bfd_write (temp, sizeof (temp), 1, abfd) != sizeof (temp))
  1982.         goto error_return;
  1983.  
  1984.           len = strlen (sym->name);
  1985.           if ((bfd_write (&len, sizeof (bfd_byte), 1, abfd)
  1986.            != sizeof (bfd_byte))
  1987.           || bfd_write (sym->name, len, 1, abfd) != len)
  1988.         goto error_return;
  1989.         }
  1990.       nlm_fixed_header (abfd)->numberOfDebugRecords = c;
  1991.     }
  1992.     }
  1993.  
  1994.   /* NLMLINK fills in offset values even if there is no data, so we do
  1995.      the same.  */
  1996.   last = bfd_tell (abfd);
  1997.   if (nlm_fixed_header (abfd)->codeImageOffset == 0)
  1998.     nlm_fixed_header (abfd)->codeImageOffset = last;
  1999.   if (nlm_fixed_header (abfd)->dataImageOffset == 0)
  2000.     nlm_fixed_header (abfd)->dataImageOffset = last;
  2001.   if (nlm_fixed_header (abfd)->customDataOffset == 0)
  2002.     nlm_fixed_header (abfd)->customDataOffset = last;
  2003.   if (nlm_fixed_header (abfd)->moduleDependencyOffset == 0)
  2004.     nlm_fixed_header (abfd)->moduleDependencyOffset = last;
  2005.   if (nlm_fixed_header (abfd)->relocationFixupOffset == 0)
  2006.     nlm_fixed_header (abfd)->relocationFixupOffset = last;
  2007.   if (nlm_fixed_header (abfd)->externalReferencesOffset == 0)
  2008.     nlm_fixed_header (abfd)->externalReferencesOffset = last;
  2009.   if (nlm_fixed_header (abfd)->publicsOffset == 0)
  2010.     nlm_fixed_header (abfd)->publicsOffset = last;
  2011.   if (nlm_fixed_header (abfd)->debugInfoOffset == 0)
  2012.     nlm_fixed_header (abfd)->debugInfoOffset = last;
  2013.  
  2014.   /* At this point everything has been written out except the fixed
  2015.      header.  */
  2016.   memcpy (nlm_fixed_header (abfd)->signature, nlm_signature (abfd),
  2017.       NLM_SIGNATURE_SIZE);
  2018.   nlm_fixed_header (abfd)->version = NLM_HEADER_VERSION;
  2019.   nlm_fixed_header (abfd)->codeStartOffset =
  2020.     (bfd_get_start_address (abfd)
  2021.      - nlm_get_text_low (abfd));
  2022.  
  2023.   /* We have no convenient way for the caller to pass in the exit
  2024.      procedure or the check unload procedure, so the caller must set
  2025.      the values in the header to the values of the symbols.  */
  2026.   nlm_fixed_header (abfd)->exitProcedureOffset -= nlm_get_text_low (abfd);
  2027.   if (nlm_fixed_header (abfd)->checkUnloadProcedureOffset != 0)
  2028.     nlm_fixed_header (abfd)->checkUnloadProcedureOffset -=
  2029.       nlm_get_text_low (abfd);
  2030.  
  2031.   if (bfd_seek (abfd, 0, SEEK_SET) != 0)
  2032.     goto error_return;
  2033.  
  2034.   write_prefix_func = nlm_write_prefix_func (abfd);
  2035.   if (write_prefix_func)
  2036.     {
  2037.       if ((*write_prefix_func) (abfd) == false)
  2038.     goto error_return;
  2039.     }
  2040.  
  2041.   BFD_ASSERT ((bfd_size_type) bfd_tell (abfd)
  2042.           == nlm_optional_prefix_size (abfd));
  2043.  
  2044.   nlm_swap_fixed_header_out (abfd, nlm_fixed_header (abfd), fixed_header);
  2045.   if (bfd_write (fixed_header, nlm_fixed_header_size (abfd), 1, abfd)
  2046.       != nlm_fixed_header_size (abfd))
  2047.     goto error_return;
  2048.  
  2049.   if (fixed_header != NULL)
  2050.     free (fixed_header);
  2051.   return true;
  2052.  
  2053. error_return:
  2054.   if (fixed_header != NULL)
  2055.     free (fixed_header);
  2056.   return false;
  2057. }
  2058.