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