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 / nlm32-alpha.c < prev    next >
C/C++ Source or Header  |  1994-02-03  |  27KB  |  910 lines

  1. /* Support for 32-bit Alpha NLM (NetWare Loadable Module)
  2.    Copyright (C) 1993 Free Software Foundation, Inc.
  3.    Written by Ian Lance Taylor, Cygnus Support.
  4.  
  5. This file is part of BFD, the Binary File Descriptor library.
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. /* This file describes the 32 bit Alpha NLM format.  You might think
  22.    that an Alpha chip would use a 64 bit format, but, for some reason,
  23.    it doesn't.  */
  24.  
  25. #include "bfd.h"
  26. #include "sysdep.h"
  27. #include "libbfd.h"
  28.  
  29. #define ARCH_SIZE 32
  30.  
  31. #include "nlm/alpha-ext.h"
  32. #define Nlm_External_Fixed_Header    Nlm32_alpha_External_Fixed_Header
  33.  
  34. #include "libnlm.h"
  35.  
  36. static boolean nlm_alpha_backend_object_p
  37.   PARAMS ((bfd *));
  38. static boolean nlm_alpha_write_prefix
  39.   PARAMS ((bfd *));
  40. static boolean nlm_alpha_read_reloc
  41.   PARAMS ((bfd *, nlmNAME(symbol_type) *, asection **, arelent *));
  42. static boolean nlm_alpha_mangle_relocs
  43.   PARAMS ((bfd *, asection *, PTR, bfd_vma, bfd_size_type));
  44. static boolean nlm_alpha_read_import
  45.   PARAMS ((bfd *, nlmNAME(symbol_type) *));
  46. static boolean nlm_alpha_write_import
  47.   PARAMS ((bfd *, asection *, arelent *));
  48. static boolean nlm_alpha_set_public_section
  49.   PARAMS ((bfd *, nlmNAME(symbol_type) *));
  50. static bfd_vma nlm_alpha_get_public_offset
  51.   PARAMS ((bfd *, asymbol *));
  52. static boolean nlm_alpha_write_external
  53.   PARAMS ((bfd *, bfd_size_type, asymbol *, struct reloc_and_sec *));
  54.  
  55. /* Alpha NLM's have a prefix header before the standard NLM.  This
  56.    function reads it in, verifies the version, and seeks the bfd to
  57.    the location before the regular NLM header.  */
  58.  
  59. static boolean
  60. nlm_alpha_backend_object_p (abfd)
  61.      bfd *abfd;
  62. {
  63.   struct nlm32_alpha_external_prefix_header s;
  64.   bfd_size_type size;
  65.  
  66.   if (bfd_read ((PTR) &s, sizeof s, 1, abfd) != sizeof s)
  67.     return false;
  68.  
  69.   if (bfd_h_get_32 (abfd, s.magic) != NLM32_ALPHA_MAGIC)
  70.     return false;
  71.  
  72.   /* FIXME: Should we check the format number?  */
  73.  
  74.   /* Skip to the end of the header.  */
  75.   size = bfd_h_get_32 (abfd, s.size);
  76.   if (bfd_seek (abfd, size, SEEK_SET) != 0)
  77.     return false;
  78.  
  79.   return true;
  80. }
  81.  
  82. /* Write out the prefix.  */
  83.  
  84. static boolean
  85. nlm_alpha_write_prefix (abfd)
  86.      bfd *abfd;
  87. {
  88.   struct nlm32_alpha_external_prefix_header s;
  89.  
  90.   memset (&s, 0, sizeof s);
  91.   bfd_h_put_32 (abfd, (bfd_vma) NLM32_ALPHA_MAGIC, s.magic);
  92.   bfd_h_put_32 (abfd, (bfd_vma) 2, s.format);
  93.   bfd_h_put_32 (abfd, (bfd_vma) sizeof s, s.size);
  94.   if (bfd_write ((PTR) &s, sizeof s, 1, abfd) != sizeof s)
  95.     {
  96.       bfd_error = system_call_error;
  97.       return false;
  98.     }
  99.   return true;
  100. }
  101.  
  102. /* How to process the various reloc types.  */
  103.  
  104. static reloc_howto_type nlm32_alpha_howto_table[] =
  105. {
  106.   /* Reloc type 0 is ignored by itself.  However, it appears after a
  107.      GPDISP reloc to identify the location where the low order 16 bits
  108.      of the gp register are loaded.  */
  109.   HOWTO (ALPHA_R_IGNORE,    /* type */
  110.      0,            /* rightshift */
  111.      0,            /* size (0 = byte, 1 = short, 2 = long) */
  112.      8,            /* bitsize */
  113.      false,            /* pc_relative */
  114.      0,            /* bitpos */
  115.      complain_overflow_dont, /* complain_on_overflow */
  116.      0,            /* special_function */
  117.      "IGNORE",        /* name */
  118.      false,            /* partial_inplace */
  119.      0,            /* src_mask */
  120.      0,            /* dst_mask */
  121.      false),        /* pcrel_offset */
  122.  
  123.   /* A 32 bit reference to a symbol.  */
  124.   HOWTO (ALPHA_R_REFLONG,    /* type */
  125.      0,            /* rightshift */
  126.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  127.      32,            /* bitsize */
  128.      false,            /* pc_relative */
  129.      0,            /* bitpos */
  130.      complain_overflow_bitfield, /* complain_on_overflow */
  131.      0,            /* special_function */
  132.      "REFLONG",        /* name */
  133.      true,            /* partial_inplace */
  134.      0xffffffff,        /* src_mask */
  135.      0xffffffff,        /* dst_mask */
  136.      false),        /* pcrel_offset */
  137.  
  138.   /* A 64 bit reference to a symbol.  */
  139.   HOWTO (ALPHA_R_REFQUAD,    /* type */
  140.      0,            /* rightshift */
  141.      4,            /* size (0 = byte, 1 = short, 2 = long) */
  142.      64,            /* bitsize */
  143.      false,            /* pc_relative */
  144.      0,            /* bitpos */
  145.      complain_overflow_bitfield, /* complain_on_overflow */
  146.      0,            /* special_function */
  147.      "REFQUAD",        /* name */
  148.      true,            /* partial_inplace */
  149.      0xffffffffffffffff,    /* src_mask */
  150.      0xffffffffffffffff,    /* dst_mask */
  151.      false),        /* pcrel_offset */
  152.  
  153.   /* A 32 bit GP relative offset.  This is just like REFLONG except
  154.      that when the value is used the value of the gp register will be
  155.      added in.  */
  156.   HOWTO (ALPHA_R_GPREL32,    /* type */
  157.      0,            /* rightshift */
  158.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  159.      32,            /* bitsize */
  160.      false,            /* pc_relative */
  161.      0,            /* bitpos */
  162.      complain_overflow_bitfield, /* complain_on_overflow */
  163.      0,            /* special_function */
  164.      "GPREL32",        /* name */
  165.      true,            /* partial_inplace */
  166.      0xffffffff,        /* src_mask */
  167.      0xffffffff,        /* dst_mask */
  168.      false),        /* pcrel_offset */
  169.  
  170.   /* Used for an instruction that refers to memory off the GP
  171.      register.  The offset is 16 bits of the 32 bit instruction.  This
  172.      reloc always seems to be against the .lita section.  */
  173.   HOWTO (ALPHA_R_LITERAL,    /* type */
  174.      0,            /* rightshift */
  175.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  176.      16,            /* bitsize */
  177.      false,            /* pc_relative */
  178.      0,            /* bitpos */
  179.      complain_overflow_signed, /* complain_on_overflow */
  180.      0,            /* special_function */
  181.      "LITERAL",        /* name */
  182.      true,            /* partial_inplace */
  183.      0xffff,        /* src_mask */
  184.      0xffff,        /* dst_mask */
  185.      false),        /* pcrel_offset */
  186.  
  187.   /* This reloc only appears immediately following a LITERAL reloc.
  188.      It identifies a use of the literal.  It seems that the linker can
  189.      use this to eliminate a portion of the .lita section.  The symbol
  190.      index is special: 1 means the literal address is in the base
  191.      register of a memory format instruction; 2 means the literal
  192.      address is in the byte offset register of a byte-manipulation
  193.      instruction; 3 means the literal address is in the target
  194.      register of a jsr instruction.  This does not actually do any
  195.      relocation.  */
  196.   HOWTO (ALPHA_R_LITUSE,    /* type */
  197.      0,            /* rightshift */
  198.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  199.      32,            /* bitsize */
  200.      false,            /* pc_relative */
  201.      0,            /* bitpos */
  202.      complain_overflow_dont, /* complain_on_overflow */
  203.      0,            /* special_function */
  204.      "LITUSE",        /* name */
  205.      false,            /* partial_inplace */
  206.      0,            /* src_mask */
  207.      0,            /* dst_mask */
  208.      false),        /* pcrel_offset */
  209.  
  210.   /* Load the gp register.  This is always used for a ldah instruction
  211.      which loads the upper 16 bits of the gp register.  The next reloc
  212.      will be an IGNORE reloc which identifies the location of the lda
  213.      instruction which loads the lower 16 bits.  The symbol index of
  214.      the GPDISP instruction appears to actually be the number of bytes
  215.      between the ldah and lda instructions.  This gives two different
  216.      ways to determine where the lda instruction is; I don't know why
  217.      both are used.  The value to use for the relocation is the
  218.      difference between the GP value and the current location; the
  219.      load will always be done against a register holding the current
  220.      address.  */
  221.   HOWTO (ALPHA_R_GPDISP,    /* type */
  222.      16,            /* rightshift */
  223.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  224.      16,            /* bitsize */
  225.      true,            /* pc_relative */
  226.      0,            /* bitpos */
  227.      complain_overflow_dont, /* complain_on_overflow */
  228.      0,            /* special_function */
  229.      "GPDISP",        /* name */
  230.      true,            /* partial_inplace */
  231.      0xffff,        /* src_mask */
  232.      0xffff,        /* dst_mask */
  233.      true),            /* pcrel_offset */
  234.  
  235.   /* A 21 bit branch.  The native assembler generates these for
  236.      branches within the text segment, and also fills in the PC
  237.      relative offset in the instruction.  It seems to me that this
  238.      reloc, unlike the others, is not partial_inplace.  */
  239.   HOWTO (ALPHA_R_BRADDR,    /* type */
  240.      2,            /* rightshift */
  241.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  242.      21,            /* bitsize */
  243.      true,            /* pc_relative */
  244.      0,            /* bitpos */
  245.      complain_overflow_signed, /* complain_on_overflow */
  246.      0,            /* special_function */
  247.      "BRADDR",        /* name */
  248.      false,            /* partial_inplace */
  249.      0,            /* src_mask */
  250.      0x1fffff,        /* dst_mask */
  251.      false),        /* pcrel_offset */
  252.  
  253.   /* A hint for a jump to a register.  */
  254.   HOWTO (ALPHA_R_HINT,        /* type */
  255.      2,            /* rightshift */
  256.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  257.      14,            /* bitsize */
  258.      false,            /* pc_relative */
  259.      0,            /* bitpos */
  260.      complain_overflow_dont, /* complain_on_overflow */
  261.      0,            /* special_function */
  262.      "HINT",        /* name */
  263.      true,            /* partial_inplace */
  264.      0x3fff,        /* src_mask */
  265.      0x3fff,        /* dst_mask */
  266.      false),        /* pcrel_offset */
  267.  
  268.   /* 16 bit PC relative offset.  */
  269.   HOWTO (ALPHA_R_SREL16,    /* type */
  270.      0,            /* rightshift */
  271.      1,            /* size (0 = byte, 1 = short, 2 = long) */
  272.      16,            /* bitsize */
  273.      true,            /* pc_relative */
  274.      0,            /* bitpos */
  275.      complain_overflow_signed, /* complain_on_overflow */
  276.      0,            /* special_function */
  277.      "SREL16",        /* name */
  278.      true,            /* partial_inplace */
  279.      0xffff,        /* src_mask */
  280.      0xffff,        /* dst_mask */
  281.      false),        /* pcrel_offset */
  282.  
  283.   /* 32 bit PC relative offset.  */
  284.   HOWTO (ALPHA_R_SREL32,    /* type */
  285.      0,            /* rightshift */
  286.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  287.      32,            /* bitsize */
  288.      true,            /* pc_relative */
  289.      0,            /* bitpos */
  290.      complain_overflow_signed, /* complain_on_overflow */
  291.      0,            /* special_function */
  292.      "SREL32",        /* name */
  293.      true,            /* partial_inplace */
  294.      0xffffffff,        /* src_mask */
  295.      0xffffffff,        /* dst_mask */
  296.      false),        /* pcrel_offset */
  297.  
  298.   /* A 64 bit PC relative offset.  */
  299.   HOWTO (ALPHA_R_SREL64,    /* type */
  300.      0,            /* rightshift */
  301.      4,            /* size (0 = byte, 1 = short, 2 = long) */
  302.      64,            /* bitsize */
  303.      true,            /* pc_relative */
  304.      0,            /* bitpos */
  305.      complain_overflow_signed, /* complain_on_overflow */
  306.      0,            /* special_function */
  307.      "SREL64",        /* name */
  308.      true,            /* partial_inplace */
  309.      0xffffffffffffffff,    /* src_mask */
  310.      0xffffffffffffffff,    /* dst_mask */
  311.      false),        /* pcrel_offset */
  312.  
  313.   /* Push a value on the reloc evaluation stack.  */
  314.   HOWTO (ALPHA_R_OP_PUSH,    /* type */
  315.      0,            /* rightshift */
  316.      0,            /* size (0 = byte, 1 = short, 2 = long) */
  317.      0,            /* bitsize */
  318.      false,            /* pc_relative */
  319.      0,            /* bitpos */
  320.      complain_overflow_dont, /* complain_on_overflow */
  321.      0,            /* special_function */
  322.      "OP_PUSH",        /* name */
  323.      false,            /* partial_inplace */
  324.      0,            /* src_mask */
  325.      0,            /* dst_mask */
  326.      false),        /* pcrel_offset */
  327.  
  328.   /* Store the value from the stack at the given address.  Store it in
  329.      a bitfield of size r_size starting at bit position r_offset.  */
  330.   HOWTO (ALPHA_R_OP_STORE,    /* type */
  331.      0,            /* rightshift */
  332.      4,            /* size (0 = byte, 1 = short, 2 = long) */
  333.      64,            /* bitsize */
  334.      false,            /* pc_relative */
  335.      0,            /* bitpos */
  336.      complain_overflow_dont, /* complain_on_overflow */
  337.      0,            /* special_function */
  338.      "OP_STORE",        /* name */
  339.      false,            /* partial_inplace */
  340.      0,            /* src_mask */
  341.      0xffffffffffffffff,    /* dst_mask */
  342.      false),        /* pcrel_offset */
  343.  
  344.   /* Subtract the reloc address from the value on the top of the
  345.      relocation stack.  */
  346.   HOWTO (ALPHA_R_OP_PSUB,    /* type */
  347.      0,            /* rightshift */
  348.      0,            /* size (0 = byte, 1 = short, 2 = long) */
  349.      0,            /* bitsize */
  350.      false,            /* pc_relative */
  351.      0,            /* bitpos */
  352.      complain_overflow_dont, /* complain_on_overflow */
  353.      0,            /* special_function */
  354.      "OP_PSUB",        /* name */
  355.      false,            /* partial_inplace */
  356.      0,            /* src_mask */
  357.      0,            /* dst_mask */
  358.      false),        /* pcrel_offset */
  359.  
  360.   /* Shift the value on the top of the relocation stack right by the
  361.      given value.  */
  362.   HOWTO (ALPHA_R_OP_PRSHIFT,    /* type */
  363.      0,            /* rightshift */
  364.      0,            /* size (0 = byte, 1 = short, 2 = long) */
  365.      0,            /* bitsize */
  366.      false,            /* pc_relative */
  367.      0,            /* bitpos */
  368.      complain_overflow_dont, /* complain_on_overflow */
  369.      0,             /* special_function */
  370.      "OP_PRSHIFT",        /* name */
  371.      false,            /* partial_inplace */
  372.      0,            /* src_mask */
  373.      0,            /* dst_mask */
  374.      false),        /* pcrel_offset */
  375.  
  376.   /* Adjust the GP value for a new range in the object file.  */
  377.   HOWTO (ALPHA_R_GPVALUE,    /* type */
  378.      0,            /* rightshift */
  379.      0,            /* size (0 = byte, 1 = short, 2 = long) */
  380.      0,            /* bitsize */
  381.      false,            /* pc_relative */
  382.      0,            /* bitpos */
  383.      complain_overflow_dont, /* complain_on_overflow */
  384.      0,            /* special_function */
  385.      "GPVALUE",        /* name */
  386.      false,            /* partial_inplace */
  387.      0,            /* src_mask */
  388.      0,            /* dst_mask */
  389.      false)            /* pcrel_offset */
  390. };
  391.  
  392. static reloc_howto_type nlm32_alpha_nw_howto =
  393.   HOWTO (ALPHA_R_NW_RELOC,    /* type */
  394.      0,            /* rightshift */
  395.      0,            /* size (0 = byte, 1 = short, 2 = long) */
  396.      0,            /* bitsize */
  397.      false,            /* pc_relative */
  398.      0,            /* bitpos */
  399.      complain_overflow_dont, /* complain_on_overflow */
  400.      0,            /* special_function */
  401.      "NW_RELOC",        /* name */
  402.      false,            /* partial_inplace */
  403.      0,            /* src_mask */
  404.      0,            /* dst_mask */
  405.      false);        /* pcrel_offset */
  406.  
  407. /* Read an Alpha NLM reloc.  This routine keeps some static data which
  408.    it uses when handling local relocs.  This only works correctly
  409.    because all the local relocs are read at once.  */
  410.  
  411. static boolean
  412. nlm_alpha_read_reloc (abfd, sym, secp, rel)
  413.      bfd *abfd;
  414.      nlmNAME(symbol_type) *sym;
  415.      asection **secp;
  416.      arelent *rel;
  417. {
  418.   static bfd_vma gp_value;
  419.   static bfd_vma lita_address;
  420.   struct nlm32_alpha_external_reloc ext;
  421.   bfd_vma r_vaddr;
  422.   long r_symndx;
  423.   int r_type, r_extern, r_offset, r_size;
  424.   asection *code_sec, *data_sec;
  425.  
  426.   /* Read the reloc from the file.  */
  427.   if (bfd_read (&ext, sizeof ext, 1, abfd) != sizeof ext)
  428.     {
  429.       bfd_error = system_call_error;
  430.       return false;
  431.     }
  432.  
  433.   /* Swap in the reloc information.  */
  434.   r_vaddr = bfd_h_get_64 (abfd, (bfd_byte *) ext.r_vaddr);
  435.   r_symndx = bfd_h_get_32 (abfd, (bfd_byte *) ext.r_symndx);
  436.  
  437.   BFD_ASSERT (abfd->xvec->header_byteorder_big_p == false);
  438.  
  439.   r_type = ((ext.r_bits[0] & RELOC_BITS0_TYPE_LITTLE)
  440.         >> RELOC_BITS0_TYPE_SH_LITTLE);
  441.   r_extern = (ext.r_bits[1] & RELOC_BITS1_EXTERN_LITTLE) != 0;
  442.   r_offset = ((ext.r_bits[1] & RELOC_BITS1_OFFSET_LITTLE)
  443.           >> RELOC_BITS1_OFFSET_SH_LITTLE);
  444.   /* Ignore the reserved bits.  */
  445.   r_size = ((ext.r_bits[3] & RELOC_BITS3_SIZE_LITTLE)
  446.         >> RELOC_BITS3_SIZE_SH_LITTLE);
  447.  
  448.   /* Fill in the BFD arelent structure.  */
  449.   code_sec = bfd_get_section_by_name (abfd, NLM_CODE_NAME);
  450.   data_sec = bfd_get_section_by_name (abfd, NLM_INITIALIZED_DATA_NAME);
  451.   if (r_extern)
  452.     {
  453.       /* External relocations are only used for imports.  */
  454.       BFD_ASSERT (sym != NULL);
  455.       /* We don't need to set sym_ptr_ptr for this case.  It is set in
  456.      nlm_canonicalize_reloc.  */
  457.       rel->sym_ptr_ptr = NULL;
  458.       rel->addend = 0;
  459.     }
  460.   else
  461.     {
  462.       /* Internal relocations are only used for local relocation
  463.      fixups.  If they are not NW_RELOC or GPDISP or IGNORE, they
  464.      must be against .text or .data.  */
  465.       BFD_ASSERT (r_type == ALPHA_R_NW_RELOC || sym == NULL);
  466.       if (r_type == ALPHA_R_NW_RELOC
  467.       || r_type == ALPHA_R_GPDISP
  468.       || r_type == ALPHA_R_IGNORE)
  469.     {
  470.       rel->sym_ptr_ptr = bfd_abs_section.symbol_ptr_ptr;
  471.       rel->addend = 0;
  472.     }
  473.       else if (r_symndx == ALPHA_RELOC_SECTION_TEXT)
  474.     {
  475.       rel->sym_ptr_ptr = code_sec->symbol_ptr_ptr;
  476.       BFD_ASSERT (bfd_get_section_vma (abfd, code_sec) == 0);
  477.       rel->addend = 0;
  478.     }
  479.       else if (r_symndx == ALPHA_RELOC_SECTION_DATA)
  480.     {
  481.       rel->sym_ptr_ptr = data_sec->symbol_ptr_ptr;
  482.       rel->addend = - bfd_get_section_vma (abfd, data_sec);
  483.     }
  484.       else
  485.     {
  486.       BFD_ASSERT (0);
  487.       rel->sym_ptr_ptr = bfd_abs_section.symbol_ptr_ptr;
  488.       rel->addend = 0;
  489.     }
  490.     }
  491.  
  492.   /* We use the address to determine whether the reloc is in the .text
  493.      or .data section.  R_NW_RELOC relocs don't really have a section,
  494.      so we put them in .text.  */
  495.   if (r_type == ALPHA_R_NW_RELOC
  496.       || r_vaddr < bfd_section_size (abfd, code_sec))
  497.     {
  498.       *secp = code_sec;
  499.       rel->address = r_vaddr;
  500.     }
  501.   else
  502.     {
  503.       *secp = data_sec;
  504.       rel->address = r_vaddr - bfd_section_size (abfd, code_sec);
  505.     }
  506.  
  507.   /* We must adjust the addend based on the type.  */
  508.   BFD_ASSERT ((r_type >= 0 && r_type <= ALPHA_R_GPVALUE)
  509.           || r_type == ALPHA_R_NW_RELOC);
  510.  
  511.   switch (r_type)
  512.     {
  513.     case ALPHA_R_BRADDR:
  514.     case ALPHA_R_SREL16:
  515.     case ALPHA_R_SREL32:
  516.     case ALPHA_R_SREL64:
  517.       /* The PC relative relocs do not seem to use the section VMA as
  518.      a negative addend.  */
  519.       rel->addend = 0;
  520.       break;
  521.  
  522.     case ALPHA_R_GPREL32:
  523.       /* Copy the gp value for this object file into the addend, to
  524.      ensure that we are not confused by the linker.  */
  525.       if (! r_extern)
  526.     rel->addend += gp_value;
  527.       break;
  528.  
  529.     case ALPHA_R_LITERAL:
  530.       BFD_ASSERT (! r_extern);
  531.       rel->addend += lita_address;
  532.       break;
  533.  
  534.     case ALPHA_R_LITUSE:
  535.     case ALPHA_R_GPDISP:
  536.       /* The LITUSE and GPDISP relocs do not use a symbol, or an
  537.      addend, but they do use a special code.  Put this code in the
  538.      addend field.  */
  539.       rel->addend = r_symndx;
  540.       rel->sym_ptr_ptr = bfd_abs_section.symbol_ptr_ptr;
  541.       break;
  542.  
  543.     case ALPHA_R_OP_STORE:
  544.       /* The STORE reloc needs the size and offset fields.  We store
  545.      them in the addend.  */
  546.       BFD_ASSERT (r_offset < 256 && r_size < 256);
  547.       rel->addend = (r_offset << 8) + r_size;
  548.       break;
  549.  
  550.     case ALPHA_R_OP_PUSH:
  551.     case ALPHA_R_OP_PSUB:
  552.     case ALPHA_R_OP_PRSHIFT:
  553.       /* The PUSH, PSUB and PRSHIFT relocs do not actually use an
  554.      address.  I believe that the address supplied is really an
  555.      addend.  */
  556.       rel->addend = r_vaddr;
  557.       break;
  558.  
  559.     case ALPHA_R_GPVALUE:
  560.       /* Record the new gp value.  */
  561.       gp_value += r_symndx;
  562.       rel->addend = gp_value;
  563.       break;
  564.  
  565.     case ALPHA_R_IGNORE:
  566.       /* If the type is ALPHA_R_IGNORE, make sure this is a reference
  567.      to the absolute section so that the reloc is ignored.  For
  568.      some reason the address of this reloc type is not adjusted by
  569.      the section vma.  We record the gp value for this object file
  570.      here, for convenience when doing the GPDISP relocation.  */
  571.       rel->sym_ptr_ptr = bfd_abs_section.symbol_ptr_ptr;
  572.       rel->address = r_vaddr;
  573.       rel->addend = gp_value;
  574.       break;
  575.  
  576.     case ALPHA_R_NW_RELOC:
  577.       /* If this is SETGP, we set the addend to 0.  Otherwise we set
  578.      the addend to the size of the .lita section (this is
  579.      r_symndx) plus 1.  We have already set the address of the
  580.      reloc to r_vaddr.  */
  581.       if (r_size == ALPHA_R_NW_RELOC_SETGP)
  582.     {
  583.       gp_value = r_vaddr;
  584.       rel->addend = 0;
  585.     }
  586.       else if (r_size == ALPHA_R_NW_RELOC_LITA)
  587.     {
  588.       lita_address = r_vaddr;
  589.       rel->addend = r_symndx + 1;
  590.     }
  591.       else
  592.     BFD_ASSERT (0);
  593.       rel->sym_ptr_ptr = bfd_abs_section.symbol_ptr_ptr;
  594.       break;
  595.  
  596.     default:
  597.       break;
  598.     }
  599.  
  600.   if (r_type == ALPHA_R_NW_RELOC)
  601.     rel->howto = &nlm32_alpha_nw_howto;
  602.   else
  603.     rel->howto = &nlm32_alpha_howto_table[r_type];
  604.  
  605.   return true;
  606. }
  607.  
  608. /* Mangle Alpha NLM relocs for output.  */
  609.  
  610. static boolean
  611. nlm_alpha_mangle_relocs (abfd, sec, data, offset, count)
  612.      bfd *abfd;
  613.      asection *sec;
  614.      PTR data;
  615.      bfd_vma offset;
  616.      bfd_size_type count;
  617. {
  618.   return true;
  619. }
  620.  
  621. /* Read an ALPHA NLM import record */
  622.  
  623. static boolean
  624. nlm_alpha_read_import (abfd, sym)
  625.      bfd *abfd;
  626.      nlmNAME(symbol_type) *sym;
  627. {
  628.   struct nlm_relent *nlm_relocs;    /* relocation records for symbol */
  629.   bfd_size_type rcount;            /* number of relocs */
  630.   bfd_byte temp[NLM_TARGET_LONG_SIZE];    /* temporary 32-bit value */
  631.   unsigned char symlength;        /* length of symbol name */
  632.  
  633.   if (bfd_read ((PTR) &symlength, sizeof (symlength), 1, abfd)
  634.       != sizeof (symlength))
  635.     {
  636.       bfd_error = system_call_error;
  637.       return (false);
  638.     }
  639.   sym -> symbol.the_bfd = abfd;
  640.   sym -> symbol.name = bfd_alloc (abfd, symlength + 1);
  641.   if (bfd_read ((PTR) sym -> symbol.name, symlength, 1, abfd)
  642.       != symlength)
  643.     {
  644.       bfd_error = system_call_error;
  645.       return (false);
  646.     }
  647.   sym -> symbol.flags = 0;
  648.   sym -> symbol.value = 0;
  649.   sym -> symbol.section = &bfd_und_section;
  650.   if (bfd_read ((PTR) temp, sizeof (temp), 1, abfd) != sizeof (temp))
  651.     {
  652.       bfd_error = system_call_error;
  653.       return (false);
  654.     }
  655.   rcount = bfd_h_get_32 (abfd, temp);
  656.   nlm_relocs = ((struct nlm_relent *)
  657.         bfd_alloc (abfd, rcount * sizeof (struct nlm_relent)));
  658.   sym -> relocs = nlm_relocs;
  659.   sym -> rcnt = 0;
  660.   while (sym -> rcnt < rcount)
  661.     {
  662.       asection *section;
  663.       
  664.       if (nlm_alpha_read_reloc (abfd, sym, §ion,
  665.                 &nlm_relocs -> reloc)
  666.       == false)
  667.     return false;
  668.       nlm_relocs -> section = section;
  669.       nlm_relocs++;
  670.       sym -> rcnt++;
  671.     }
  672.  
  673.   return true;
  674. }
  675.  
  676. /* Write an Alpha NLM reloc.  */
  677.  
  678. static boolean
  679. nlm_alpha_write_import (abfd, sec, rel)
  680.      bfd *abfd;
  681.      asection *sec;
  682.      arelent *rel;
  683. {
  684.   asymbol *sym;
  685.   bfd_vma r_vaddr;
  686.   long r_symndx;
  687.   int r_type, r_extern, r_offset, r_size;
  688.   struct nlm32_alpha_external_reloc ext;
  689.  
  690.   sym = *rel->sym_ptr_ptr;
  691.  
  692.   /* Get values for the relocation fields.  */
  693.   r_type = rel->howto->type;
  694.   if (r_type != ALPHA_R_NW_RELOC)
  695.     {
  696.       r_vaddr = bfd_get_section_vma (abfd, sec) + rel->address;
  697.       if ((sec->flags & SEC_CODE) == 0)
  698.     r_vaddr += bfd_section_size (abfd,
  699.                      bfd_get_section_by_name (abfd,
  700.                                   NLM_CODE_NAME));
  701.       if (bfd_get_section (sym) == &bfd_und_section)
  702.     {
  703.       r_extern = 1;
  704.       r_symndx = 0;
  705.     }
  706.       else
  707.     {
  708.       r_extern = 0;
  709.       if (bfd_get_section_flags (abfd, bfd_get_section (sym)) & SEC_CODE)
  710.         r_symndx = ALPHA_RELOC_SECTION_TEXT;
  711.       else
  712.         r_symndx = ALPHA_RELOC_SECTION_DATA;
  713.     }
  714.       r_offset = 0;
  715.       r_size = 0;
  716.  
  717.       switch (r_type)
  718.     {
  719.     case ALPHA_R_LITUSE:
  720.     case ALPHA_R_GPDISP:
  721.       r_symndx = rel->addend;
  722.       break;
  723.  
  724.     case ALPHA_R_OP_STORE:
  725.       r_size = rel->addend & 0xff;
  726.       r_offset = (rel->addend >> 8) & 0xff;
  727.       break;
  728.  
  729.     case ALPHA_R_OP_PUSH:
  730.     case ALPHA_R_OP_PSUB:
  731.     case ALPHA_R_OP_PRSHIFT:
  732.       r_vaddr = rel->addend;
  733.       break;
  734.  
  735.     case ALPHA_R_IGNORE:
  736.       r_vaddr = rel->address;
  737.       break;
  738.  
  739.     default:
  740.       break;
  741.     }
  742.     }
  743.   else
  744.     {
  745.       /* r_type == ALPHA_R_NW_RELOC */
  746.       r_vaddr = rel->address;
  747.       if (rel->addend == 0)
  748.     {
  749.       r_symndx = 0;
  750.       r_size = ALPHA_R_NW_RELOC_SETGP;
  751.     }
  752.       else
  753.     {
  754.       r_symndx = rel->addend - 1;
  755.       r_size = ALPHA_R_NW_RELOC_LITA;
  756.     }
  757.       r_extern = 0;
  758.       r_offset = 0;
  759.     }
  760.  
  761.   /* Swap out the relocation fields.  */
  762.   bfd_h_put_64 (abfd, r_vaddr, (bfd_byte *) ext.r_vaddr);
  763.   bfd_h_put_32 (abfd, r_symndx, (bfd_byte *) ext.r_symndx);
  764.  
  765.   BFD_ASSERT (abfd->xvec->header_byteorder_big_p == false);
  766.  
  767.   ext.r_bits[0] = ((r_type << RELOC_BITS0_TYPE_SH_LITTLE)
  768.            & RELOC_BITS0_TYPE_LITTLE);
  769.   ext.r_bits[1] = ((r_extern ? RELOC_BITS1_EXTERN_LITTLE : 0)
  770.            | ((r_offset << RELOC_BITS1_OFFSET_SH_LITTLE)
  771.               & RELOC_BITS1_OFFSET_LITTLE));
  772.   ext.r_bits[2] = 0;
  773.   ext.r_bits[3] = ((r_size << RELOC_BITS3_SIZE_SH_LITTLE)
  774.            & RELOC_BITS3_SIZE_LITTLE);
  775.  
  776.   /* Write out the relocation.  */
  777.   if (bfd_write (&ext, sizeof ext, 1, abfd) != sizeof ext)
  778.     {
  779.       bfd_error = system_call_error;
  780.       return false;
  781.     }
  782.  
  783.   return true;
  784. }
  785.  
  786. /* Alpha NetWare does not use the high bit to determine whether a
  787.    public symbol is in the code segment or the data segment.  Instead,
  788.    it just uses the address.  The set_public_section and
  789.    get_public_offset routines override the default code which uses the
  790.    high bit.  */
  791.  
  792. /* Set the section for a public symbol.  */
  793.  
  794. static boolean
  795. nlm_alpha_set_public_section (abfd, sym)
  796.      bfd *abfd;
  797.      nlmNAME(symbol_type) *sym;
  798. {
  799.   asection *code_sec, *data_sec;
  800.  
  801.   code_sec = bfd_get_section_by_name (abfd, NLM_CODE_NAME);
  802.   data_sec = bfd_get_section_by_name (abfd, NLM_INITIALIZED_DATA_NAME);
  803.   if (sym->symbol.value < bfd_section_size (abfd, code_sec))
  804.     {
  805.       sym->symbol.section = code_sec;
  806.       sym->symbol.flags |= BSF_FUNCTION;
  807.     }
  808.   else
  809.     {
  810.       sym->symbol.section = data_sec;
  811.       sym->symbol.value -= bfd_section_size (abfd, code_sec);
  812.       /* The data segment had better be aligned.  */
  813.       BFD_ASSERT ((bfd_section_size (abfd, code_sec) & 0xf) == 0);
  814.     }
  815.   return true;
  816. }
  817.  
  818. /* Get the offset to write out for a public symbol.  */
  819.  
  820. static bfd_vma
  821. nlm_alpha_get_public_offset (abfd, sym)
  822.      bfd *abfd;
  823.      asymbol *sym;
  824. {
  825.   return bfd_asymbol_value (sym);
  826. }
  827.  
  828. /* Write an Alpha NLM external symbol.  */
  829.  
  830. static boolean
  831. nlm_alpha_write_external (abfd, count, sym, relocs)
  832.      bfd *abfd;
  833.      bfd_size_type count;
  834.      asymbol *sym;
  835.      struct reloc_and_sec *relocs;
  836. {
  837.   int i;
  838.   bfd_byte len;
  839.   unsigned char temp[NLM_TARGET_LONG_SIZE];
  840.   arelent r;
  841.  
  842.   len = strlen (sym->name);
  843.   if ((bfd_write (&len, sizeof (bfd_byte), 1, abfd) != sizeof(bfd_byte))
  844.       || bfd_write (sym->name, len, 1, abfd) != len)
  845.     {
  846.       bfd_error = system_call_error;
  847.       return false;
  848.     }
  849.  
  850.   bfd_put_32 (abfd, count + 2, temp);
  851.   if (bfd_write (temp, sizeof (temp), 1, abfd) != sizeof (temp))
  852.     {
  853.       bfd_error = system_call_error;
  854.       return false;
  855.     }
  856.  
  857.   /* The first two relocs for each external symbol are the .lita
  858.      address and the GP value.  */
  859.   r.sym_ptr_ptr = bfd_abs_section.symbol_ptr_ptr;
  860.   r.howto = &nlm32_alpha_nw_howto;
  861.  
  862.   r.address = nlm_alpha_backend_data (abfd)->lita_address;
  863.   r.addend = nlm_alpha_backend_data (abfd)->lita_size + 1;
  864.   if (nlm_alpha_write_import (abfd, (asection *) NULL, &r) == false)
  865.     return false;
  866.  
  867.   r.address = nlm_alpha_backend_data (abfd)->gp;
  868.   r.addend = 0;
  869.   if (nlm_alpha_write_import (abfd, (asection *) NULL, &r) == false)
  870.     return false;
  871.  
  872.   for (i = 0; i < count; i++)
  873.     {
  874.       if (nlm_alpha_write_import (abfd, relocs[i].sec,
  875.                   relocs[i].rel) == false)
  876.     return false;
  877.     }
  878.  
  879.   return true;
  880. }
  881.  
  882. #include "nlmswap.h"
  883.  
  884. static const struct nlm_backend_data nlm32_alpha_backend =
  885. {
  886.   "NetWare Alpha Module   \032",
  887.   sizeof (Nlm32_alpha_External_Fixed_Header),
  888.   sizeof (struct nlm32_alpha_external_prefix_header),
  889.   bfd_arch_alpha,
  890.   0,
  891.   true, /* no uninitialized data permitted by Alpha NetWare.  */
  892.   nlm_alpha_backend_object_p,
  893.   nlm_alpha_write_prefix,
  894.   nlm_alpha_read_reloc,
  895.   nlm_alpha_mangle_relocs,
  896.   nlm_alpha_read_import,
  897.   nlm_alpha_write_import,
  898.   nlm_alpha_set_public_section,
  899.   nlm_alpha_get_public_offset,
  900.   nlm_swap_fixed_header_in,
  901.   nlm_swap_fixed_header_out,
  902.   nlm_alpha_write_external,
  903. };
  904.  
  905. #define TARGET_LITTLE_NAME        "nlm32-alpha"
  906. #define TARGET_LITTLE_SYM        nlmNAME(alpha_vec)
  907. #define TARGET_BACKEND_DATA        &nlm32_alpha_backend
  908.  
  909. #include "nlm-target.h"
  910.