home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 9 / FreshFishVol9-CD2.bin / bbs / gnu / gdb-4.14-src.lha / gdb-4.14 / bfd / elf32-mips.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-28  |  78.5 KB  |  2,694 lines

  1. /* MIPS-specific support for 32-bit ELF
  2.    Copyright 1993, 1994, 1995 Free Software Foundation, Inc.
  3.  
  4.    Most of the information added by Ian Lance Taylor, Cygnus Support,
  5.    <ian@cygnus.com>.
  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 "bfd.h"
  24. #include "sysdep.h"
  25. #include "libbfd.h"
  26. #include "bfdlink.h"
  27. #include "genlink.h"
  28. #include "libelf.h"
  29. #include "elf/mips.h"
  30.  
  31. /* Get the ECOFF swapping routines.  */
  32. #include "coff/sym.h"
  33. #include "coff/symconst.h"
  34. #include "coff/internal.h"
  35. #include "coff/ecoff.h"
  36. #include "coff/mips.h"
  37. #define ECOFF_32
  38. #include "ecoffswap.h"
  39.  
  40. static bfd_reloc_status_type mips_elf_hi16_reloc PARAMS ((bfd *abfd,
  41.                               arelent *reloc,
  42.                               asymbol *symbol,
  43.                               PTR data,
  44.                               asection *section,
  45.                               bfd *output_bfd,
  46.                               char **error));
  47. static bfd_reloc_status_type mips_elf_got16_reloc PARAMS ((bfd *abfd,
  48.                                arelent *reloc,
  49.                                asymbol *symbol,
  50.                                PTR data,
  51.                                asection *section,
  52.                                bfd *output_bfd,
  53.                                char **error));
  54. static bfd_reloc_status_type mips_elf_lo16_reloc PARAMS ((bfd *abfd,
  55.                               arelent *reloc,
  56.                               asymbol *symbol,
  57.                               PTR data,
  58.                               asection *section,
  59.                               bfd *output_bfd,
  60.                               char **error));
  61. static bfd_reloc_status_type mips_elf_gprel16_reloc PARAMS ((bfd *abfd,
  62.                                  arelent *reloc,
  63.                                  asymbol *symbol,
  64.                                  PTR data,
  65.                                  asection *section,
  66.                                  bfd *output_bfd,
  67.                                  char **error));
  68. static reloc_howto_type *bfd_elf32_bfd_reloc_type_lookup
  69.   PARAMS ((bfd *, bfd_reloc_code_real_type));
  70. static void mips_info_to_howto_rel
  71.   PARAMS ((bfd *, arelent *, Elf32_Internal_Rel *));
  72. static boolean mips_elf_sym_is_global PARAMS ((bfd *, asymbol *));
  73. static boolean mips_elf_object_p PARAMS ((bfd *));
  74. static void mips_elf_final_write_processing
  75.   PARAMS ((bfd *, boolean));
  76. static boolean mips_elf_section_from_shdr
  77.   PARAMS ((bfd *, Elf32_Internal_Shdr *, char *));
  78. static boolean mips_elf_fake_sections
  79.   PARAMS ((bfd *, Elf32_Internal_Shdr *, asection *));
  80. static boolean mips_elf_section_from_bfd_section
  81.   PARAMS ((bfd *, Elf32_Internal_Shdr *, asection *, int *));
  82. static boolean mips_elf_section_processing
  83.   PARAMS ((bfd *, Elf32_Internal_Shdr *));
  84. static void mips_elf_symbol_processing PARAMS ((bfd *, asymbol *));
  85. static boolean mips_elf_read_ecoff_info
  86.   PARAMS ((bfd *, asection *, struct ecoff_debug_info *));
  87. static struct bfd_hash_entry *mips_elf_link_hash_newfunc
  88.   PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
  89. static struct bfd_link_hash_table *mips_elf_link_hash_table_create
  90.   PARAMS ((bfd *));
  91. static int gptab_compare PARAMS ((const void *, const void *));
  92. static boolean mips_elf_final_link
  93.   PARAMS ((bfd *, struct bfd_link_info *));
  94. static void mips_elf_relocate_hi16
  95.   PARAMS ((bfd *, Elf_Internal_Rela *, Elf_Internal_Rela *, bfd_byte *,
  96.        bfd_vma));
  97. static boolean mips_elf_relocate_section
  98.   PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
  99.        Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
  100. static boolean mips_elf_add_symbol_hook
  101.   PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Sym *,
  102.        const char **, flagword *, asection **, bfd_vma *));
  103.  
  104. #define USE_REL    1        /* MIPS uses REL relocations instead of RELA */
  105.  
  106. enum reloc_type
  107. {
  108.   R_MIPS_NONE = 0,
  109.   R_MIPS_16,        R_MIPS_32,
  110.   R_MIPS_REL32,        R_MIPS_26,
  111.   R_MIPS_HI16,        R_MIPS_LO16,
  112.   R_MIPS_GPREL16,    R_MIPS_LITERAL,
  113.   R_MIPS_GOT16,        R_MIPS_PC16,
  114.   R_MIPS_CALL16,    R_MIPS_GPREL32,
  115.   /* The remaining relocs are defined on Irix, although they are not
  116.      in the MIPS ELF ABI.  */
  117.   R_MIPS_UNUSED1,    R_MIPS_UNUSED2,
  118.   R_MIPS_UNUSED3,
  119.   R_MIPS_SHIFT5,    R_MIPS_SHIFT6,
  120.   R_MIPS_64,        R_MIPS_GOT_DISP,
  121.   R_MIPS_GOT_PAGE,    R_MIPS_GOT_OFST,
  122.   R_MIPS_GOT_HI16,    R_MIPS_GOT_LO16,
  123.   R_MIPS_SUB,        R_MIPS_INSERT_A,
  124.   R_MIPS_INSERT_B,    R_MIPS_DELETE,
  125.   R_MIPS_HIGHER,    R_MIPS_HIGHEST,
  126.   R_MIPS_CALL_HI16,    R_MIPS_CALL_LO16,
  127.   R_MIPS_max
  128. };
  129.  
  130. static reloc_howto_type elf_mips_howto_table[] =
  131. {
  132.   /* No relocation.  */
  133.   HOWTO (R_MIPS_NONE,        /* type */
  134.      0,            /* rightshift */
  135.      0,            /* size (0 = byte, 1 = short, 2 = long) */
  136.      0,            /* bitsize */
  137.      false,            /* pc_relative */
  138.      0,            /* bitpos */
  139.      complain_overflow_dont, /* complain_on_overflow */
  140.      bfd_elf_generic_reloc,    /* special_function */
  141.      "R_MIPS_NONE",        /* name */
  142.      false,            /* partial_inplace */
  143.      0,            /* src_mask */
  144.      0,            /* dst_mask */
  145.      false),        /* pcrel_offset */
  146.  
  147.   /* 16 bit relocation.  */
  148.   HOWTO (R_MIPS_16,        /* type */
  149.      0,            /* rightshift */
  150.      1,            /* size (0 = byte, 1 = short, 2 = long) */
  151.      16,            /* bitsize */
  152.      false,            /* pc_relative */
  153.      0,            /* bitpos */
  154.      complain_overflow_bitfield, /* complain_on_overflow */
  155.      bfd_elf_generic_reloc,    /* special_function */
  156.      "R_MIPS_16",        /* name */
  157.      true,            /* partial_inplace */
  158.      0xffff,        /* src_mask */
  159.      0xffff,        /* dst_mask */
  160.      false),        /* pcrel_offset */
  161.  
  162.   /* 32 bit relocation.  */
  163.   HOWTO (R_MIPS_32,        /* type */
  164.      0,            /* rightshift */
  165.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  166.      32,            /* bitsize */
  167.      false,            /* pc_relative */
  168.      0,            /* bitpos */
  169.      complain_overflow_bitfield, /* complain_on_overflow */
  170.      bfd_elf_generic_reloc,    /* special_function */
  171.      "R_MIPS_32",        /* name */
  172.      true,            /* partial_inplace */
  173.      0xffffffff,        /* src_mask */
  174.      0xffffffff,        /* dst_mask */
  175.      false),        /* pcrel_offset */
  176.  
  177.   /* 32 bit symbol relative relocation.  */
  178.   HOWTO (R_MIPS_REL32,        /* type */
  179.      0,            /* rightshift */
  180.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  181.      32,            /* bitsize */
  182.      false,            /* pc_relative */
  183.      0,            /* bitpos */
  184.      complain_overflow_bitfield, /* complain_on_overflow */
  185.      bfd_elf_generic_reloc,    /* special_function */
  186.      "R_MIPS_REL32",    /* name */
  187.      true,            /* partial_inplace */
  188.      0xffffffff,        /* src_mask */
  189.      0xffffffff,        /* dst_mask */
  190.      false),        /* pcrel_offset */
  191.  
  192.   /* 26 bit branch address.  */
  193.   HOWTO (R_MIPS_26,        /* type */
  194.      2,            /* rightshift */
  195.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  196.      26,            /* bitsize */
  197.      false,            /* pc_relative */
  198.      0,            /* bitpos */
  199.      complain_overflow_dont, /* complain_on_overflow */
  200.                  /* This needs complex overflow
  201.                    detection, because the upper four
  202.                    bits must match the PC.  */
  203.      bfd_elf_generic_reloc,    /* special_function */
  204.      "R_MIPS_26",        /* name */
  205.      true,            /* partial_inplace */
  206.      0x3ffffff,        /* src_mask */
  207.      0x3ffffff,        /* dst_mask */
  208.      false),        /* pcrel_offset */
  209.  
  210.   /* High 16 bits of symbol value.  */
  211.   HOWTO (R_MIPS_HI16,        /* type */
  212.      0,            /* rightshift */
  213.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  214.      16,            /* bitsize */
  215.      false,            /* pc_relative */
  216.      0,            /* bitpos */
  217.      complain_overflow_dont, /* complain_on_overflow */
  218.      mips_elf_hi16_reloc,    /* special_function */
  219.      "R_MIPS_HI16",        /* name */
  220.      true,            /* partial_inplace */
  221.      0xffff,        /* src_mask */
  222.      0xffff,        /* dst_mask */
  223.      false),        /* pcrel_offset */
  224.  
  225.   /* Low 16 bits of symbol value.  */
  226.   HOWTO (R_MIPS_LO16,        /* type */
  227.      0,            /* rightshift */
  228.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  229.      16,            /* bitsize */
  230.      false,            /* pc_relative */
  231.      0,            /* bitpos */
  232.      complain_overflow_dont, /* complain_on_overflow */
  233.      mips_elf_lo16_reloc,    /* special_function */
  234.      "R_MIPS_LO16",        /* name */
  235.      true,            /* partial_inplace */
  236.      0xffff,        /* src_mask */
  237.      0xffff,        /* dst_mask */
  238.      false),        /* pcrel_offset */
  239.  
  240.   /* GP relative reference.  */
  241.   HOWTO (R_MIPS_GPREL16,    /* type */
  242.      0,            /* rightshift */
  243.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  244.      16,            /* bitsize */
  245.      false,            /* pc_relative */
  246.      0,            /* bitpos */
  247.      complain_overflow_signed, /* complain_on_overflow */
  248.      mips_elf_gprel16_reloc, /* special_function */
  249.      "R_MIPS_GPREL16",    /* name */
  250.      true,            /* partial_inplace */
  251.      0xffff,        /* src_mask */
  252.      0xffff,        /* dst_mask */
  253.      false),        /* pcrel_offset */
  254.  
  255.   /* Reference to literal section.  */
  256.   HOWTO (R_MIPS_LITERAL,    /* type */
  257.      0,            /* rightshift */
  258.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  259.      16,            /* bitsize */
  260.      false,            /* pc_relative */
  261.      0,            /* bitpos */
  262.      complain_overflow_signed, /* complain_on_overflow */
  263.      mips_elf_gprel16_reloc, /* special_function */
  264.      "R_MIPS_LITERAL",    /* name */
  265.      true,            /* partial_inplace */
  266.      0xffff,        /* src_mask */
  267.      0xffff,        /* dst_mask */
  268.      false),        /* pcrel_offset */
  269.  
  270.   /* Reference to global offset table.  */
  271.   /* FIXME: This is not handled correctly.  */
  272.   HOWTO (R_MIPS_GOT16,        /* type */
  273.      0,            /* rightshift */
  274.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  275.      16,            /* bitsize */
  276.      false,            /* pc_relative */
  277.      0,            /* bitpos */
  278.      complain_overflow_signed, /* complain_on_overflow */
  279.      mips_elf_got16_reloc,    /* special_function */
  280.      "R_MIPS_GOT16",    /* name */
  281.      false,            /* partial_inplace */
  282.      0,            /* src_mask */
  283.      0xffff,        /* dst_mask */
  284.      false),        /* pcrel_offset */
  285.  
  286.   /* 16 bit PC relative reference.  */
  287.   HOWTO (R_MIPS_PC16,        /* type */
  288.      0,            /* rightshift */
  289.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  290.      16,            /* bitsize */
  291.      true,            /* pc_relative */
  292.      0,            /* bitpos */
  293.      complain_overflow_signed, /* complain_on_overflow */
  294.      bfd_elf_generic_reloc,    /* special_function */
  295.      "R_MIPS_PC16",        /* name */
  296.      true,            /* partial_inplace */
  297.      0xffff,        /* src_mask */
  298.      0xffff,        /* dst_mask */
  299.      false),        /* pcrel_offset */
  300.  
  301.   /* 16 bit call through global offset table.  */
  302.   /* FIXME: This is not handled correctly.  */
  303.   HOWTO (R_MIPS_CALL16,        /* type */
  304.      0,            /* rightshift */
  305.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  306.      16,            /* bitsize */
  307.      false,            /* pc_relative */
  308.      0,            /* bitpos */
  309.      complain_overflow_signed, /* complain_on_overflow */
  310.      bfd_elf_generic_reloc,    /* special_function */
  311.      "R_MIPS_CALL16",    /* name */
  312.      false,            /* partial_inplace */
  313.      0,            /* src_mask */
  314.      0xffff,        /* dst_mask */
  315.      false),        /* pcrel_offset */
  316.  
  317.   /* 32 bit GP relative reference.  */
  318.   /* FIXME: This is not handled correctly.  */
  319.   HOWTO (R_MIPS_GPREL32,    /* type */
  320.      0,            /* rightshift */
  321.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  322.      32,            /* bitsize */
  323.      false,            /* pc_relative */
  324.      0,            /* bitpos */
  325.      complain_overflow_bitfield, /* complain_on_overflow */
  326.      bfd_elf_generic_reloc,    /* special_function */
  327.      "R_MIPS_GPREL32",    /* name */
  328.      true,            /* partial_inplace */
  329.      0xffffffff,        /* src_mask */
  330.      0xffffffff,        /* dst_mask */
  331.      false),        /* pcrel_offset */
  332.  
  333.     /* The remaining relocs are defined on Irix 5, although they are
  334.        not defined by the ABI.  */
  335.     { 13 },
  336.     { 14 },
  337.     { 15 },
  338.  
  339.   /* A 5 bit shift field.  */
  340.   HOWTO (R_MIPS_SHIFT5,        /* type */
  341.      0,            /* rightshift */
  342.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  343.      5,            /* bitsize */
  344.      false,            /* pc_relative */
  345.      6,            /* bitpos */
  346.      complain_overflow_bitfield, /* complain_on_overflow */
  347.      bfd_elf_generic_reloc,    /* special_function */
  348.      "R_MIPS_SHIFT5",    /* name */
  349.      true,            /* partial_inplace */
  350.      0x000007c0,        /* src_mask */
  351.      0x000007c0,        /* dst_mask */
  352.      false),        /* pcrel_offset */
  353.  
  354.   /* A 6 bit shift field.  */
  355.   /* FIXME: This is not handled correctly; a special function is
  356.      needed to put the most significant bit in the right place.  */
  357.   HOWTO (R_MIPS_SHIFT6,        /* type */
  358.      0,            /* rightshift */
  359.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  360.      6,            /* bitsize */
  361.      false,            /* pc_relative */
  362.      6,            /* bitpos */
  363.      complain_overflow_bitfield, /* complain_on_overflow */
  364.      bfd_elf_generic_reloc,    /* special_function */
  365.      "R_MIPS_SHIFT6",    /* name */
  366.      true,            /* partial_inplace */
  367.      0x000007c4,        /* src_mask */
  368.      0x000007c4,        /* dst_mask */
  369.      false),        /* pcrel_offset */
  370.  
  371.   /* A 64 bit relocation.  Presumably not used in 32 bit ELF.  */
  372.   { R_MIPS_64 },
  373.  
  374.   /* Displacement in the global offset table.  */
  375.   /* FIXME: Not handled correctly.  */
  376.   HOWTO (R_MIPS_GOT_DISP,    /* type */
  377.      0,            /* rightshift */
  378.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  379.      16,            /* bitsize */
  380.      false,            /* pc_relative */
  381.      0,            /* bitpos */
  382.      complain_overflow_bitfield, /* complain_on_overflow */
  383.      bfd_elf_generic_reloc,    /* special_function */
  384.      "R_MIPS_GOT_DISP",    /* name */
  385.      true,            /* partial_inplace */
  386.      0x0000ffff,        /* src_mask */
  387.      0x0000ffff,        /* dst_mask */
  388.      false),        /* pcrel_offset */
  389.  
  390.   /* Displacement to page pointer in the global offset table.  */
  391.   /* FIXME: Not handled correctly.  */
  392.   HOWTO (R_MIPS_GOT_PAGE,    /* type */
  393.      0,            /* rightshift */
  394.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  395.      16,            /* bitsize */
  396.      false,            /* pc_relative */
  397.      0,            /* bitpos */
  398.      complain_overflow_bitfield, /* complain_on_overflow */
  399.      bfd_elf_generic_reloc,    /* special_function */
  400.      "R_MIPS_GOT_PAGE",    /* name */
  401.      true,            /* partial_inplace */
  402.      0x0000ffff,        /* src_mask */
  403.      0x0000ffff,        /* dst_mask */
  404.      false),        /* pcrel_offset */
  405.  
  406.   /* Offset from page pointer in the global offset table.  */
  407.   /* FIXME: Not handled correctly.  */
  408.   HOWTO (R_MIPS_GOT_OFST,    /* type */
  409.      0,            /* rightshift */
  410.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  411.      16,            /* bitsize */
  412.      false,            /* pc_relative */
  413.      0,            /* bitpos */
  414.      complain_overflow_bitfield, /* complain_on_overflow */
  415.      bfd_elf_generic_reloc,    /* special_function */
  416.      "R_MIPS_GOT_OFST",    /* name */
  417.      true,            /* partial_inplace */
  418.      0x0000ffff,        /* src_mask */
  419.      0x0000ffff,        /* dst_mask */
  420.      false),        /* pcrel_offset */
  421.  
  422.   /* High 16 bits of displacement in global offset table.  */
  423.   /* FIXME: Not handled correctly.  */
  424.   HOWTO (R_MIPS_GOT_HI16,    /* type */
  425.      0,            /* rightshift */
  426.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  427.      16,            /* bitsize */
  428.      false,            /* pc_relative */
  429.      0,            /* bitpos */
  430.      complain_overflow_dont, /* complain_on_overflow */
  431.      bfd_elf_generic_reloc,    /* special_function */
  432.      "R_MIPS_GOT_HI16",    /* name */
  433.      true,            /* partial_inplace */
  434.      0x0000ffff,        /* src_mask */
  435.      0x0000ffff,        /* dst_mask */
  436.      false),        /* pcrel_offset */
  437.  
  438.   /* Low 16 bits of displacement in global offset table.  */
  439.   /* FIXME: Not handled correctly.  */
  440.   HOWTO (R_MIPS_GOT_LO16,    /* type */
  441.      0,            /* rightshift */
  442.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  443.      16,            /* bitsize */
  444.      false,            /* pc_relative */
  445.      0,            /* bitpos */
  446.      complain_overflow_dont, /* complain_on_overflow */
  447.      bfd_elf_generic_reloc,    /* special_function */
  448.      "R_MIPS_GOT_LO16",    /* name */
  449.      true,            /* partial_inplace */
  450.      0x0000ffff,        /* src_mask */
  451.      0x0000ffff,        /* dst_mask */
  452.      false),        /* pcrel_offset */
  453.  
  454.   /* 64 bit subtraction.  Presumably not used in 32 bit ELF.  */
  455.   { R_MIPS_SUB },
  456.  
  457.   /* Used to cause the linker to insert and delete instructions?  */
  458.   { R_MIPS_INSERT_A },
  459.   { R_MIPS_INSERT_B },
  460.   { R_MIPS_DELETE },
  461.  
  462.   /* Get the higher values of a 64 bit addend.  Presumably not used in
  463.      32 bit ELF.  */
  464.   { R_MIPS_HIGHER },
  465.   { R_MIPS_HIGHEST },
  466.  
  467.   /* High 16 bits of displacement in global offset table.  */
  468.   /* FIXME: Not handled correctly.  */
  469.   HOWTO (R_MIPS_CALL_HI16,    /* type */
  470.      0,            /* rightshift */
  471.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  472.      16,            /* bitsize */
  473.      false,            /* pc_relative */
  474.      0,            /* bitpos */
  475.      complain_overflow_dont, /* complain_on_overflow */
  476.      bfd_elf_generic_reloc,    /* special_function */
  477.      "R_MIPS_CALL_HI16",    /* name */
  478.      true,            /* partial_inplace */
  479.      0x0000ffff,        /* src_mask */
  480.      0x0000ffff,        /* dst_mask */
  481.      false),        /* pcrel_offset */
  482.  
  483.   /* Low 16 bits of displacement in global offset table.  */
  484.   /* FIXME: Not handled correctly.  */
  485.   HOWTO (R_MIPS_CALL_LO16,    /* type */
  486.      0,            /* rightshift */
  487.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  488.      16,            /* bitsize */
  489.      false,            /* pc_relative */
  490.      0,            /* bitpos */
  491.      complain_overflow_dont, /* complain_on_overflow */
  492.      bfd_elf_generic_reloc,    /* special_function */
  493.      "R_MIPS_CALL_LO16",    /* name */
  494.      true,            /* partial_inplace */
  495.      0x0000ffff,        /* src_mask */
  496.      0x0000ffff,        /* dst_mask */
  497.      false)            /* pcrel_offset */
  498. };
  499.  
  500. /* Do a R_MIPS_HI16 relocation.  This has to be done in combination
  501.    with a R_MIPS_LO16 reloc, because there is a carry from the LO16 to
  502.    the HI16.  Here we just save the information we need; we do the
  503.    actual relocation when we see the LO16.  MIPS ELF requires that the
  504.    LO16 immediately follow the HI16, so this ought to work.  */
  505.  
  506. static bfd_byte *mips_hi16_addr;
  507. static bfd_vma mips_hi16_addend;
  508.  
  509. static bfd_reloc_status_type
  510. mips_elf_hi16_reloc (abfd,
  511.              reloc_entry,
  512.              symbol,
  513.              data,
  514.              input_section,
  515.              output_bfd,
  516.              error_message)
  517.      bfd *abfd;
  518.      arelent *reloc_entry;
  519.      asymbol *symbol;
  520.      PTR data;
  521.      asection *input_section;
  522.      bfd *output_bfd;
  523.      char **error_message;
  524. {
  525.   bfd_reloc_status_type ret;
  526.   bfd_vma relocation;
  527.  
  528.   /* If we're relocating, and this an external symbol, we don't want
  529.      to change anything.  */
  530.   if (output_bfd != (bfd *) NULL
  531.       && (symbol->flags & BSF_SECTION_SYM) == 0
  532.       && reloc_entry->addend == 0)
  533.     {
  534.       reloc_entry->address += input_section->output_offset;
  535.       return bfd_reloc_ok;
  536.     }
  537.  
  538.   /* FIXME: The symbol _gp_disp requires special handling, which we do
  539.      not do.  */
  540.   if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
  541.     abort ();
  542.  
  543.   ret = bfd_reloc_ok;
  544.   if (bfd_is_und_section (symbol->section)
  545.       && output_bfd == (bfd *) NULL)
  546.     ret = bfd_reloc_undefined;
  547.  
  548.   if (bfd_is_com_section (symbol->section))
  549.     relocation = 0;
  550.   else
  551.     relocation = symbol->value;
  552.  
  553.   relocation += symbol->section->output_section->vma;
  554.   relocation += symbol->section->output_offset;
  555.   relocation += reloc_entry->addend;
  556.  
  557.   if (reloc_entry->address > input_section->_cooked_size)
  558.     return bfd_reloc_outofrange;
  559.  
  560.   /* Save the information, and let LO16 do the actual relocation.  */
  561.   mips_hi16_addr = (bfd_byte *) data + reloc_entry->address;
  562.   mips_hi16_addend = relocation;
  563.  
  564.   if (output_bfd != (bfd *) NULL)
  565.     reloc_entry->address += input_section->output_offset;
  566.  
  567.   return ret;
  568. }
  569.  
  570. /* Do a R_MIPS_LO16 relocation.  This is a straightforward 16 bit
  571.    inplace relocation; this function exists in order to do the
  572.    R_MIPS_HI16 relocation described above.  */
  573.  
  574. static bfd_reloc_status_type
  575. mips_elf_lo16_reloc (abfd,
  576.              reloc_entry,
  577.              symbol,
  578.              data,
  579.              input_section,
  580.              output_bfd,
  581.              error_message)
  582.      bfd *abfd;
  583.      arelent *reloc_entry;
  584.      asymbol *symbol;
  585.      PTR data;
  586.      asection *input_section;
  587.      bfd *output_bfd;
  588.      char **error_message;
  589. {
  590.   /* FIXME: The symbol _gp_disp requires special handling, which we do
  591.      not do.  */
  592.   if (output_bfd == (bfd *) NULL
  593.       && strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
  594.     abort ();
  595.  
  596.   if (mips_hi16_addr != (bfd_byte *) NULL)
  597.     {
  598.       unsigned long insn;
  599.       unsigned long val;
  600.       unsigned long vallo;
  601.  
  602.       /* Do the HI16 relocation.  Note that we actually don't need to
  603.      know anything about the LO16 itself, except where to find the
  604.      low 16 bits of the addend needed by the LO16.  */
  605.       insn = bfd_get_32 (abfd, mips_hi16_addr);
  606.       vallo = (bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address)
  607.            & 0xffff);
  608.       val = ((insn & 0xffff) << 16) + vallo;
  609.       val += mips_hi16_addend;
  610.  
  611.       /* The low order 16 bits are always treated as a signed value.
  612.      Therefore, a negative value in the low order bits requires an
  613.      adjustment in the high order bits.  We need to make this
  614.      adjustment in two ways: once for the bits we took from the
  615.      data, and once for the bits we are putting back in to the
  616.      data.  */
  617.       if ((vallo & 0x8000) != 0)
  618.     val -= 0x10000;
  619.       if ((val & 0x8000) != 0)
  620.     val += 0x10000;
  621.  
  622.       insn = (insn &~ 0xffff) | ((val >> 16) & 0xffff);
  623.       bfd_put_32 (abfd, insn, mips_hi16_addr);
  624.  
  625.       mips_hi16_addr = (bfd_byte *) NULL;
  626.     }
  627.  
  628.   /* Now do the LO16 reloc in the usual way.  */
  629.   return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
  630.                 input_section, output_bfd, error_message);
  631. }
  632.  
  633. /* Do a R_MIPS_GOT16 reloc.  This is a reloc against the global offset
  634.    table used for PIC code.  If the symbol is an external symbol, the
  635.    instruction is modified to contain the offset of the appropriate
  636.    entry in the global offset table.  If the symbol is a section
  637.    symbol, the next reloc is a R_MIPS_LO16 reloc.  The two 16 bit
  638.    addends are combined to form the real addend against the section
  639.    symbol; the GOT16 is modified to contain the offset of an entry in
  640.    the global offset table, and the LO16 is modified to offset it
  641.    appropriately.  Thus an offset larger than 16 bits requires a
  642.    modified value in the global offset table.
  643.  
  644.    This implementation suffices for the assembler, but the linker does
  645.    not yet know how to create global offset tables.  */
  646.  
  647. static bfd_reloc_status_type
  648. mips_elf_got16_reloc (abfd,
  649.               reloc_entry,
  650.               symbol,
  651.               data,
  652.               input_section,
  653.               output_bfd,
  654.               error_message)
  655.      bfd *abfd;
  656.      arelent *reloc_entry;
  657.      asymbol *symbol;
  658.      PTR data;
  659.      asection *input_section;
  660.      bfd *output_bfd;
  661.      char **error_message;
  662. {
  663.   /* If we're relocating, and this an external symbol, we don't want
  664.      to change anything.  */
  665.   if (output_bfd != (bfd *) NULL
  666.       && (symbol->flags & BSF_SECTION_SYM) == 0
  667.       && reloc_entry->addend == 0)
  668.     {
  669.       reloc_entry->address += input_section->output_offset;
  670.       return bfd_reloc_ok;
  671.     }
  672.  
  673.   /* If we're relocating, and this is a local symbol, we can handle it
  674.      just like HI16.  */
  675.   if (output_bfd != (bfd *) NULL
  676.       && (symbol->flags & BSF_SECTION_SYM) != 0)
  677.     return mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
  678.                 input_section, output_bfd, error_message);
  679.  
  680.   abort ();
  681. }
  682.  
  683. /* Do a R_MIPS_GPREL16 relocation.  This is a 16 bit value which must
  684.    become the offset from the gp register.  This function also handles
  685.    R_MIPS_LITERAL relocations, although those can be handled more
  686.    cleverly because the entries in the .lit8 and .lit4 sections can be
  687.    merged.  */
  688.  
  689. static bfd_reloc_status_type
  690. mips_elf_gprel16_reloc (abfd,
  691.             reloc_entry,
  692.             symbol,
  693.             data,
  694.             input_section,
  695.             output_bfd,
  696.             error_message)
  697.      bfd *abfd;
  698.      arelent *reloc_entry;
  699.      asymbol *symbol;
  700.      PTR data;
  701.      asection *input_section;
  702.      bfd *output_bfd;
  703.      char **error_message;
  704. {
  705.   boolean relocateable;
  706.   bfd_vma relocation;
  707.   unsigned long val;
  708.   unsigned long insn;
  709.  
  710.   /* If we're relocating, and this is an external symbol with no
  711.      addend, we don't want to change anything.  We will only have an
  712.      addend if this is a newly created reloc, not read from an ELF
  713.      file.  */
  714.   if (output_bfd != (bfd *) NULL
  715.       && (symbol->flags & BSF_SECTION_SYM) == 0
  716.       && reloc_entry->addend == 0)
  717.     {
  718.       reloc_entry->address += input_section->output_offset;
  719.       return bfd_reloc_ok;
  720.     }
  721.  
  722.   if (output_bfd != (bfd *) NULL)
  723.     relocateable = true;
  724.   else
  725.     {
  726.       relocateable = false;
  727.       output_bfd = symbol->section->output_section->owner;
  728.     }
  729.  
  730.   if (bfd_is_und_section (symbol->section)
  731.       && relocateable == false)
  732.     return bfd_reloc_undefined;
  733.  
  734.   /* We have to figure out the gp value, so that we can adjust the
  735.      symbol value correctly.  We look up the symbol _gp in the output
  736.      BFD.  If we can't find it, we're stuck.  We cache it in the ELF
  737.      target data.  We don't need to adjust the symbol value for an
  738.      external symbol if we are producing relocateable output.  */
  739.   if (elf_gp (output_bfd) == 0
  740.       && (relocateable == false
  741.       || (symbol->flags & BSF_SECTION_SYM) != 0))
  742.     {
  743.       if (relocateable != false)
  744.     {
  745.       /* Make up a value.  */
  746.       elf_gp (output_bfd) =
  747.         symbol->section->output_section->vma + 0x4000;
  748.     }
  749.       else
  750.     {
  751.       unsigned int count;
  752.       asymbol **sym;
  753.       unsigned int i;
  754.  
  755.       count = bfd_get_symcount (output_bfd);
  756.       sym = bfd_get_outsymbols (output_bfd);
  757.  
  758.       if (sym == (asymbol **) NULL)
  759.         i = count;
  760.       else
  761.         {
  762.           for (i = 0; i < count; i++, sym++)
  763.         {
  764.           register CONST char *name;
  765.  
  766.           name = bfd_asymbol_name (*sym);
  767.           if (*name == '_' && strcmp (name, "_gp") == 0)
  768.             {
  769.               elf_gp (output_bfd) = bfd_asymbol_value (*sym);
  770.               break;
  771.             }
  772.         }
  773.         }
  774.  
  775.       if (i >= count)
  776.         {
  777.           /* Only get the error once.  */
  778.           elf_gp (output_bfd) = 4;
  779.           *error_message =
  780.         (char *) "GP relative relocation when _gp not defined";
  781.           return bfd_reloc_dangerous;
  782.         }
  783.     }
  784.     }
  785.  
  786.   if (bfd_is_com_section (symbol->section))
  787.     relocation = 0;
  788.   else
  789.     relocation = symbol->value;
  790.  
  791.   relocation += symbol->section->output_section->vma;
  792.   relocation += symbol->section->output_offset;
  793.  
  794.   if (reloc_entry->address > input_section->_cooked_size)
  795.     return bfd_reloc_outofrange;
  796.  
  797.   insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
  798.  
  799.   /* Set val to the offset into the section or symbol.  */
  800.   val = ((insn & 0xffff) + reloc_entry->addend) & 0xffff;
  801.   if (val & 0x8000)
  802.     val -= 0x10000;
  803.  
  804.   /* Adjust val for the final section location and GP value.  If we
  805.      are producing relocateable output, we don't want to do this for
  806.      an external symbol.  */
  807.   if (relocateable == false
  808.       || (symbol->flags & BSF_SECTION_SYM) != 0)
  809.     val += relocation - elf_gp (output_bfd);
  810.  
  811.   insn = (insn &~ 0xffff) | (val & 0xffff);
  812.   bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
  813.  
  814.   if (relocateable != false)
  815.     reloc_entry->address += input_section->output_offset;
  816.  
  817.   /* Make sure it fit in 16 bits.  */
  818.   if (val >= 0x8000 && val < 0xffff8000)
  819.     return bfd_reloc_overflow;
  820.  
  821.   return bfd_reloc_ok;
  822. }
  823.  
  824. /* A mapping from BFD reloc types to MIPS ELF reloc types.  */
  825.  
  826. struct elf_reloc_map {
  827.   bfd_reloc_code_real_type bfd_reloc_val;
  828.   enum reloc_type elf_reloc_val;
  829. };
  830.  
  831. static CONST struct elf_reloc_map mips_reloc_map[] =
  832. {
  833.   { BFD_RELOC_NONE, R_MIPS_NONE, },
  834.   { BFD_RELOC_16, R_MIPS_16 },
  835.   { BFD_RELOC_32, R_MIPS_32 },
  836.   { BFD_RELOC_CTOR, R_MIPS_32 },
  837.   { BFD_RELOC_32_PCREL, R_MIPS_REL32 },
  838.   { BFD_RELOC_MIPS_JMP, R_MIPS_26 },
  839.   { BFD_RELOC_HI16_S, R_MIPS_HI16 },
  840.   { BFD_RELOC_LO16, R_MIPS_LO16 },
  841.   { BFD_RELOC_MIPS_GPREL, R_MIPS_GPREL16 },
  842.   { BFD_RELOC_MIPS_LITERAL, R_MIPS_LITERAL },
  843.   { BFD_RELOC_MIPS_GOT16, R_MIPS_GOT16 },
  844.   { BFD_RELOC_16_PCREL, R_MIPS_PC16 },
  845.   { BFD_RELOC_MIPS_CALL16, R_MIPS_CALL16 },
  846.   { BFD_RELOC_MIPS_GPREL32, R_MIPS_GPREL32 }
  847. };
  848.  
  849. /* Given a BFD reloc type, return a howto structure.  */
  850.  
  851. static reloc_howto_type *
  852. bfd_elf32_bfd_reloc_type_lookup (abfd, code)
  853.      bfd *abfd;
  854.      bfd_reloc_code_real_type code;
  855. {
  856.   int i;
  857.  
  858.   for (i = 0; i < sizeof (mips_reloc_map) / sizeof (struct elf_reloc_map); i++)
  859.     {
  860.       if (mips_reloc_map[i].bfd_reloc_val == code)
  861.     return &elf_mips_howto_table[(int) mips_reloc_map[i].elf_reloc_val];
  862.     }
  863.   return NULL;
  864. }
  865.  
  866. /* Given a MIPS reloc type, fill in an arelent structure.  */
  867.  
  868. static void
  869. mips_info_to_howto_rel (abfd, cache_ptr, dst)
  870.      bfd *abfd;
  871.      arelent *cache_ptr;
  872.      Elf32_Internal_Rel *dst;
  873. {
  874.   unsigned int r_type;
  875.  
  876.   r_type = ELF32_R_TYPE (dst->r_info);
  877.   BFD_ASSERT (r_type < (unsigned int) R_MIPS_max);
  878.   cache_ptr->howto = &elf_mips_howto_table[r_type];
  879.  
  880.   /* The addend for a GPREL16 or LITERAL relocation comes from the GP
  881.      value for the object file.  We get the addend now, rather than
  882.      when we do the relocation, because the symbol manipulations done
  883.      by the linker may cause us to lose track of the input BFD.  */
  884.   if (((*cache_ptr->sym_ptr_ptr)->flags & BSF_SECTION_SYM) != 0
  885.       && (r_type == (unsigned int) R_MIPS_GPREL16
  886.       || r_type == (unsigned int) R_MIPS_LITERAL))
  887.     cache_ptr->addend = elf_gp (abfd);
  888. }
  889.  
  890. /* A .reginfo section holds a single Elf32_RegInfo structure.  These
  891.    routines swap this structure in and out.  They are used outside of
  892.    BFD, so they are globally visible.  */
  893.  
  894. void
  895. bfd_mips_elf32_swap_reginfo_in (abfd, ex, in)
  896.      bfd *abfd;
  897.      const Elf32_External_RegInfo *ex;
  898.      Elf32_RegInfo *in;
  899. {
  900.   in->ri_gprmask = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_gprmask);
  901.   in->ri_cprmask[0] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[0]);
  902.   in->ri_cprmask[1] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[1]);
  903.   in->ri_cprmask[2] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[2]);
  904.   in->ri_cprmask[3] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[3]);
  905.   in->ri_gp_value = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_gp_value);
  906. }
  907.  
  908. void
  909. bfd_mips_elf32_swap_reginfo_out (abfd, in, ex)
  910.      bfd *abfd;
  911.      const Elf32_RegInfo *in;
  912.      Elf32_External_RegInfo *ex;
  913. {
  914.   bfd_h_put_32 (abfd, (bfd_vma) in->ri_gprmask,
  915.         (bfd_byte *) ex->ri_gprmask);
  916.   bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[0],
  917.         (bfd_byte *) ex->ri_cprmask[0]);
  918.   bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[1],
  919.         (bfd_byte *) ex->ri_cprmask[1]);
  920.   bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[2],
  921.         (bfd_byte *) ex->ri_cprmask[2]);
  922.   bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[3],
  923.         (bfd_byte *) ex->ri_cprmask[3]);
  924.   bfd_h_put_32 (abfd, (bfd_vma) in->ri_gp_value,
  925.         (bfd_byte *) ex->ri_gp_value);
  926. }
  927.  
  928. /* Swap an entry in a .gptab section.  Note that these routines rely
  929.    on the equivalence of the two elements of the union.  */
  930.  
  931. static void
  932. bfd_mips_elf32_swap_gptab_in (abfd, ex, in)
  933.      bfd *abfd;
  934.      const Elf32_External_gptab *ex;
  935.      Elf32_gptab *in;
  936. {
  937.   in->gt_entry.gt_g_value = bfd_h_get_32 (abfd, ex->gt_entry.gt_g_value);
  938.   in->gt_entry.gt_bytes = bfd_h_get_32 (abfd, ex->gt_entry.gt_bytes);
  939. }
  940.  
  941. static void
  942. bfd_mips_elf32_swap_gptab_out (abfd, in, ex)
  943.      bfd *abfd;
  944.      const Elf32_gptab *in;
  945.      Elf32_External_gptab *ex;
  946. {
  947.   bfd_h_put_32 (abfd, (bfd_vma) in->gt_entry.gt_g_value,
  948.         ex->gt_entry.gt_g_value);
  949.   bfd_h_put_32 (abfd, (bfd_vma) in->gt_entry.gt_bytes,
  950.         ex->gt_entry.gt_bytes);
  951. }
  952.  
  953. /* Determine whether a symbol is global for the purposes of splitting
  954.    the symbol table into global symbols and local symbols.  At least
  955.    on Irix 5, this split must be between section symbols and all other
  956.    symbols.  On most ELF targets the split is between static symbols
  957.    and externally visible symbols.  */
  958.  
  959. /*ARGSUSED*/
  960. static boolean
  961. mips_elf_sym_is_global (abfd, sym)
  962.      bfd *abfd;
  963.      asymbol *sym;
  964. {
  965.   return (sym->flags & BSF_SECTION_SYM) == 0 ? true : false;
  966. }
  967.  
  968. /* Set the right machine number for a MIPS ELF file.  */
  969.  
  970. static boolean
  971. mips_elf_object_p (abfd)
  972.      bfd *abfd;
  973. {
  974.   switch (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH)
  975.     {
  976.     default:
  977.     case E_MIPS_ARCH_1:
  978.       /* Just use the default, which was set in elfcode.h.  */
  979.       break;
  980.  
  981.     case E_MIPS_ARCH_2:
  982.       (void) bfd_default_set_arch_mach (abfd, bfd_arch_mips, 6000);
  983.       break;
  984.  
  985.     case E_MIPS_ARCH_3:
  986.       (void) bfd_default_set_arch_mach (abfd, bfd_arch_mips, 4000);
  987.       break;
  988.     }
  989.  
  990.   /* Irix 5 is broken.  Object file symbol tables are not always
  991.      sorted correctly such that local symbols precede global symbols,
  992.      and the sh_info field in the symbol table is not always right.
  993.      We try to quickly check whether the symbol table is broken for
  994.      this BFD, and, if it is, we set elf_bad_symtab in tdata.  */
  995.   if (elf_onesymtab (abfd) != 0)
  996.     {
  997.       Elf_Internal_Shdr *symtab_hdr;
  998.       Elf32_External_Sym esym;
  999.  
  1000.       symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  1001.       if (bfd_seek (abfd,
  1002.             (symtab_hdr->sh_offset
  1003.              + symtab_hdr->sh_size
  1004.              - sizeof (Elf32_External_Sym)),
  1005.             SEEK_SET) != 0
  1006.       || (bfd_read ((PTR) &esym, 1, sizeof (Elf32_External_Sym), abfd)
  1007.           != sizeof (Elf32_External_Sym)))
  1008.     return false;
  1009.       if (ELF_ST_BIND (bfd_h_get_8 (abfd, (bfd_byte *) esym.st_info))
  1010.       == STB_LOCAL)
  1011.     elf_bad_symtab (abfd) = true;
  1012.     }
  1013.  
  1014.   return true;
  1015. }
  1016.  
  1017. /* The final processing done just before writing out a MIPS ELF object
  1018.    file.  This gets the MIPS architecture right based on the machine
  1019.    number.  */
  1020.  
  1021. /*ARGSUSED*/
  1022. static void
  1023. mips_elf_final_write_processing (abfd, linker)
  1024.      bfd *abfd;
  1025.      boolean linker;
  1026. {
  1027.   unsigned long val;
  1028.   unsigned int i;
  1029.   Elf_Internal_Shdr **hdrpp;
  1030.  
  1031.   switch (bfd_get_mach (abfd))
  1032.     {
  1033.     case 3000:
  1034.       val = E_MIPS_ARCH_1;
  1035.       break;
  1036.  
  1037.     case 6000:
  1038.       val = E_MIPS_ARCH_2;
  1039.       break;
  1040.  
  1041.     case 4000:
  1042.       val = E_MIPS_ARCH_3;
  1043.       break;
  1044.  
  1045.     default:
  1046.       return;
  1047.     }
  1048.  
  1049.   elf_elfheader (abfd)->e_flags &=~ EF_MIPS_ARCH;
  1050.   elf_elfheader (abfd)->e_flags |= val;
  1051.  
  1052.   /* Set the sh_info field for .gptab sections.  */
  1053.   for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
  1054.        i < elf_elfheader (abfd)->e_shnum;
  1055.        i++, hdrpp++)
  1056.     {
  1057.       if ((*hdrpp)->sh_type == SHT_MIPS_GPTAB)
  1058.     {
  1059.       const char *name;
  1060.       asection *sec;
  1061.  
  1062.       BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
  1063.       name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
  1064.       BFD_ASSERT (name != NULL
  1065.               && strncmp (name, ".gptab.", sizeof ".gptab." - 1) == 0);
  1066.       sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
  1067.       BFD_ASSERT (sec != NULL);
  1068.       (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
  1069.     }
  1070.     }
  1071. }
  1072.  
  1073. /* Handle a MIPS specific section when reading an object file.  This
  1074.    is called when elfcode.h finds a section with an unknown type.
  1075.    FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
  1076.    how to.  */
  1077.  
  1078. static boolean
  1079. mips_elf_section_from_shdr (abfd, hdr, name)
  1080.      bfd *abfd;
  1081.      Elf32_Internal_Shdr *hdr;
  1082.      char *name;
  1083. {
  1084.   asection *newsect;
  1085.  
  1086.   /* There ought to be a place to keep ELF backend specific flags, but
  1087.      at the moment there isn't one.  We just keep track of the
  1088.      sections by their name, instead.  Fortunately, the ABI gives
  1089.      suggested names for all the MIPS specific sections, so we will
  1090.      probably get away with this.  */
  1091.   switch (hdr->sh_type)
  1092.     {
  1093.     case SHT_MIPS_LIBLIST:
  1094.       if (strcmp (name, ".liblist") != 0)
  1095.     return false;
  1096.       break;
  1097.     case SHT_MIPS_MSYM:
  1098.       if (strcmp (name, ".msym") != 0)
  1099.     return false;
  1100.       break;
  1101.     case SHT_MIPS_CONFLICT:
  1102.       if (strcmp (name, ".conflict") != 0)
  1103.     return false;
  1104.       break;
  1105.     case SHT_MIPS_GPTAB:
  1106.       if (strncmp (name, ".gptab.", sizeof ".gptab." - 1) != 0)
  1107.     return false;
  1108.       break;
  1109.     case SHT_MIPS_UCODE:
  1110.       if (strcmp (name, ".ucode") != 0)
  1111.     return false;
  1112.       break;
  1113.     case SHT_MIPS_DEBUG:
  1114.       if (strcmp (name, ".mdebug") != 0)
  1115.     return false;
  1116.       break;
  1117.     case SHT_MIPS_REGINFO:
  1118.       if (strcmp (name, ".reginfo") != 0
  1119.       || hdr->sh_size != sizeof (Elf32_External_RegInfo))
  1120.     return false;
  1121.       break;
  1122.     case SHT_MIPS_OPTIONS:
  1123.       if (strcmp (name, ".options") != 0)
  1124.     return false;
  1125.       break;
  1126.     case SHT_MIPS_DWARF:
  1127.       if (strncmp (name, ".debug_", sizeof ".debug_" - 1) != 0)
  1128.     return false;
  1129.       break;
  1130.     case SHT_MIPS_EVENTS:
  1131.       if (strncmp (name, ".MIPS.events.", sizeof ".MIPS.events." - 1) != 0)
  1132.     return false;
  1133.       break;
  1134.     default:
  1135.       return false;
  1136.     }
  1137.  
  1138.   if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
  1139.     return false;
  1140.   newsect = hdr->bfd_section;
  1141.  
  1142.   if (hdr->sh_type == SHT_MIPS_DEBUG)
  1143.     {
  1144.       if (! bfd_set_section_flags (abfd, newsect,
  1145.                    (bfd_get_section_flags (abfd, newsect)
  1146.                     | SEC_DEBUGGING)))
  1147.     return false;
  1148.     }
  1149.  
  1150.   /* FIXME: We should record sh_info for a .gptab section.  */
  1151.  
  1152.   /* For a .reginfo section, set the gp value in the tdata information
  1153.      from the contents of this section.  We need the gp value while
  1154.      processing relocs, so we just get it now.  */
  1155.   if (hdr->sh_type == SHT_MIPS_REGINFO)
  1156.     {
  1157.       Elf32_External_RegInfo ext;
  1158.       Elf32_RegInfo s;
  1159.  
  1160.       if (! bfd_get_section_contents (abfd, newsect, (PTR) &ext,
  1161.                       (file_ptr) 0, sizeof ext))
  1162.     return false;
  1163.       bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
  1164.       elf_gp (abfd) = s.ri_gp_value;
  1165.     }
  1166.  
  1167.   return true;
  1168. }
  1169.  
  1170. /* Set the correct type for a MIPS ELF section.  We do this by the
  1171.    section name, which is a hack, but ought to work.  */
  1172.  
  1173. static boolean
  1174. mips_elf_fake_sections (abfd, hdr, sec)
  1175.      bfd *abfd;
  1176.      Elf32_Internal_Shdr *hdr;
  1177.      asection *sec;
  1178. {
  1179.   register const char *name;
  1180.  
  1181.   name = bfd_get_section_name (abfd, sec);
  1182.  
  1183.   if (strcmp (name, ".liblist") == 0)
  1184.     {
  1185.       hdr->sh_type = SHT_MIPS_LIBLIST;
  1186.       hdr->sh_info = sec->_raw_size / sizeof (Elf32_Lib);
  1187.       /* FIXME: Set the sh_link field.  */
  1188.     }
  1189.   else if (strcmp (name, ".msym") == 0)
  1190.     {
  1191.       hdr->sh_type = SHT_MIPS_MSYM;
  1192.       hdr->sh_entsize = 8;
  1193.       /* FIXME: Set the sh_info field.  */
  1194.     }
  1195.   else if (strcmp (name, ".conflict") == 0)
  1196.     hdr->sh_type = SHT_MIPS_CONFLICT;
  1197.   else if (strncmp (name, ".gptab.", sizeof ".gptab." - 1) == 0)
  1198.     {
  1199.       hdr->sh_type = SHT_MIPS_GPTAB;
  1200.       hdr->sh_entsize = sizeof (Elf32_External_gptab);
  1201.       /* The sh_info field is set in mips_elf_final_write_processing.  */
  1202.     }
  1203.   else if (strcmp (name, ".ucode") == 0)
  1204.     hdr->sh_type = SHT_MIPS_UCODE;
  1205.   else if (strcmp (name, ".mdebug") == 0)
  1206.     {
  1207.       hdr->sh_type = SHT_MIPS_DEBUG;
  1208.       hdr->sh_entsize = 1;
  1209.     }
  1210.   else if (strcmp (name, ".reginfo") == 0)
  1211.     {
  1212.       hdr->sh_type = SHT_MIPS_REGINFO;
  1213.       hdr->sh_entsize = 1;
  1214.  
  1215.       /* Force the section size to the correct value, even if the
  1216.      linker thinks it is larger.  The link routine below will only
  1217.      write out this much data for .reginfo.  */
  1218.       hdr->sh_size = sec->_raw_size = sizeof (Elf32_External_RegInfo);
  1219.     }
  1220.   else if (strcmp (name, ".options") == 0)
  1221.     {
  1222.       hdr->sh_type = SHT_MIPS_OPTIONS;
  1223.       hdr->sh_entsize = 1;
  1224.     }
  1225.   else if (strncmp (name, ".debug_", sizeof ".debug_" - 1) == 0)
  1226.     hdr->sh_type = SHT_MIPS_DWARF;
  1227.   else if (strncmp (name, ".MIPS.events.", sizeof ".MIPS.events." - 1) == 0)
  1228.     hdr->sh_type = SHT_MIPS_EVENTS;
  1229.  
  1230.   return true;
  1231. }
  1232.  
  1233. /* Given a BFD section, try to locate the corresponding ELF section
  1234.    index.  */
  1235.  
  1236. static boolean
  1237. mips_elf_section_from_bfd_section (abfd, hdr, sec, retval)
  1238.      bfd *abfd;
  1239.      Elf32_Internal_Shdr *hdr;
  1240.      asection *sec;
  1241.      int *retval;
  1242. {
  1243.   if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
  1244.     {
  1245.       *retval = SHN_MIPS_SCOMMON;
  1246.       return true;
  1247.     }
  1248.   if (strcmp (bfd_get_section_name (abfd, sec), ".acommon") == 0)
  1249.     {
  1250.       *retval = SHN_MIPS_ACOMMON;
  1251.       return true;
  1252.     }
  1253.   return false;
  1254. }
  1255.  
  1256. /* Work over a section just before writing it out.  We update the GP
  1257.    value in the .reginfo section based on the value we are using.
  1258.    FIXME: We recognize sections that need the SHF_MIPS_GPREL flag by
  1259.    name; there has to be a better way.  */
  1260.  
  1261. static boolean
  1262. mips_elf_section_processing (abfd, hdr)
  1263.      bfd *abfd;
  1264.      Elf32_Internal_Shdr *hdr;
  1265. {
  1266.   if (hdr->sh_type == SHT_MIPS_REGINFO)
  1267.     {
  1268.       bfd_byte buf[4];
  1269.  
  1270.       BFD_ASSERT (hdr->sh_size == sizeof (Elf32_External_RegInfo));
  1271.       BFD_ASSERT (hdr->contents == NULL);
  1272.  
  1273.       if (bfd_seek (abfd,
  1274.             hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
  1275.             SEEK_SET) == -1)
  1276.     return false;
  1277.       bfd_h_put_32 (abfd, (bfd_vma) elf_gp (abfd), buf);
  1278.       if (bfd_write (buf, (bfd_size_type) 1, (bfd_size_type) 4, abfd) != 4)
  1279.     return false;
  1280.     }
  1281.  
  1282.   if (hdr->bfd_section != NULL)
  1283.     {
  1284.       const char *name = bfd_get_section_name (abfd, hdr->bfd_section);
  1285.  
  1286.       if (strcmp (name, ".sdata") == 0)
  1287.     {
  1288.       hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
  1289.       hdr->sh_type = SHT_PROGBITS;
  1290.     }
  1291.       else if (strcmp (name, ".sbss") == 0)
  1292.     {
  1293.       hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
  1294.       hdr->sh_type = SHT_NOBITS;
  1295.     }
  1296.       else if (strcmp (name, ".lit8") == 0
  1297.            || strcmp (name, ".lit4") == 0)
  1298.     {
  1299.       hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
  1300.       hdr->sh_type = SHT_PROGBITS;
  1301.     }
  1302.     }
  1303.  
  1304.   return true;
  1305. }
  1306.  
  1307. /* MIPS ELF uses two common sections.  One is the usual one, and the
  1308.    other is for small objects.  All the small objects are kept
  1309.    together, and then referenced via the gp pointer, which yields
  1310.    faster assembler code.  This is what we use for the small common
  1311.    section.  This approach is copied from ecoff.c.  */
  1312. static asection mips_elf_scom_section;
  1313. static asymbol mips_elf_scom_symbol;
  1314. static asymbol *mips_elf_scom_symbol_ptr;
  1315.  
  1316. /* MIPS ELF also uses an acommon section, which represents an
  1317.    allocated common symbol which may be overridden by a     
  1318.    definition in a shared library.  */
  1319. static asection mips_elf_acom_section;
  1320. static asymbol mips_elf_acom_symbol;
  1321. static asymbol *mips_elf_acom_symbol_ptr;
  1322.  
  1323. /* Handle the special MIPS section numbers that a symbol may use.  */
  1324.  
  1325. static void
  1326. mips_elf_symbol_processing (abfd, asym)
  1327.      bfd *abfd;
  1328.      asymbol *asym;
  1329. {
  1330.   elf_symbol_type *elfsym;
  1331.  
  1332.   elfsym = (elf_symbol_type *) asym;
  1333.   switch (elfsym->internal_elf_sym.st_shndx)
  1334.     {
  1335.     case SHN_MIPS_ACOMMON:
  1336.       /* This section is used in a dynamically linked executable file.
  1337.      It is an allocated common section.  The dynamic linker can
  1338.      either resolve these symbols to something in a shared
  1339.      library, or it can just leave them here.  For our purposes,
  1340.      we can consider these symbols to be in a new section.  */
  1341.       if (mips_elf_acom_section.name == NULL)
  1342.     {
  1343.       /* Initialize the acommon section.  */
  1344.       mips_elf_acom_section.name = ".acommon";
  1345.       mips_elf_acom_section.flags = SEC_NO_FLAGS;
  1346.       mips_elf_acom_section.output_section = &mips_elf_acom_section;
  1347.       mips_elf_acom_section.symbol = &mips_elf_acom_symbol;
  1348.       mips_elf_acom_section.symbol_ptr_ptr = &mips_elf_acom_symbol_ptr;
  1349.       mips_elf_acom_symbol.name = ".acommon";
  1350.       mips_elf_acom_symbol.flags = BSF_SECTION_SYM;
  1351.       mips_elf_acom_symbol.section = &mips_elf_acom_section;
  1352.       mips_elf_acom_symbol_ptr = &mips_elf_acom_symbol;
  1353.     }
  1354.       asym->section = &mips_elf_acom_section;
  1355.       break;
  1356.  
  1357.     case SHN_COMMON:
  1358.       /* Common symbols less than the GP size are automatically
  1359.      treated as SHN_MIPS_SCOMMON symbols.  */
  1360.       if (asym->value > elf_gp_size (abfd))
  1361.     break;
  1362.       /* Fall through.  */
  1363.     case SHN_MIPS_SCOMMON:
  1364.       if (mips_elf_scom_section.name == NULL)
  1365.     {
  1366.       /* Initialize the small common section.  */
  1367.       mips_elf_scom_section.name = ".scommon";
  1368.       mips_elf_scom_section.flags = SEC_IS_COMMON;
  1369.       mips_elf_scom_section.output_section = &mips_elf_scom_section;
  1370.       mips_elf_scom_section.symbol = &mips_elf_scom_symbol;
  1371.       mips_elf_scom_section.symbol_ptr_ptr = &mips_elf_scom_symbol_ptr;
  1372.       mips_elf_scom_symbol.name = ".scommon";
  1373.       mips_elf_scom_symbol.flags = BSF_SECTION_SYM;
  1374.       mips_elf_scom_symbol.section = &mips_elf_scom_section;
  1375.       mips_elf_scom_symbol_ptr = &mips_elf_scom_symbol;
  1376.     }
  1377.       asym->section = &mips_elf_scom_section;
  1378.       asym->value = elfsym->internal_elf_sym.st_size;
  1379.       break;
  1380.  
  1381.     case SHN_MIPS_SUNDEFINED:
  1382.       asym->section = bfd_und_section_ptr;
  1383.       break;
  1384.     }
  1385. }
  1386.  
  1387. /* Read ECOFF debugging information from a .mdebug section into a
  1388.    ecoff_debug_info structure.  */
  1389.  
  1390. static boolean
  1391. mips_elf_read_ecoff_info (abfd, section, debug)
  1392.      bfd *abfd;
  1393.      asection *section;
  1394.      struct ecoff_debug_info *debug;
  1395. {
  1396.   HDRR *symhdr;
  1397.   const struct ecoff_debug_swap *swap;
  1398.   char *ext_hdr = NULL;
  1399.  
  1400.   swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
  1401.  
  1402.   ext_hdr = (char *) malloc (swap->external_hdr_size);
  1403.   if (ext_hdr == NULL && swap->external_hdr_size != 0)
  1404.     {
  1405.       bfd_set_error (bfd_error_no_memory);
  1406.       goto error_return;
  1407.     }
  1408.  
  1409.   if (bfd_get_section_contents (abfd, section, ext_hdr, (file_ptr) 0,
  1410.                 swap->external_hdr_size)
  1411.       == false)
  1412.     goto error_return;
  1413.  
  1414.   symhdr = &debug->symbolic_header;
  1415.   (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
  1416.  
  1417.   /* The symbolic header contains absolute file offsets and sizes to
  1418.      read.  */
  1419. #define READ(ptr, offset, count, size, type)                \
  1420.   if (symhdr->count == 0)                        \
  1421.     debug->ptr = NULL;                            \
  1422.   else                                    \
  1423.     {                                    \
  1424.       debug->ptr = (type) malloc (size * symhdr->count);        \
  1425.       if (debug->ptr == NULL)                        \
  1426.     {                                \
  1427.       bfd_set_error (bfd_error_no_memory);                \
  1428.       goto error_return;                        \
  1429.     }                                \
  1430.       if (bfd_seek (abfd, (file_ptr) symhdr->offset, SEEK_SET) != 0    \
  1431.       || (bfd_read (debug->ptr, size, symhdr->count,        \
  1432.             abfd) != size * symhdr->count))            \
  1433.     goto error_return;                        \
  1434.     }
  1435.  
  1436.   READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
  1437.   READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, PTR);
  1438.   READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, PTR);
  1439.   READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, PTR);
  1440.   READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, PTR);
  1441.   READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
  1442.     union aux_ext *);
  1443.   READ (ss, cbSsOffset, issMax, sizeof (char), char *);
  1444.   READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
  1445.   READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, PTR);
  1446.   READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, PTR);
  1447.   READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, PTR);
  1448. #undef READ
  1449.  
  1450.   debug->fdr = NULL;
  1451.   debug->adjust = NULL;
  1452.  
  1453.   return true;
  1454.  
  1455.  error_return:
  1456.   if (ext_hdr != NULL)
  1457.     free (ext_hdr);
  1458.   if (debug->line != NULL)
  1459.     free (debug->line);
  1460.   if (debug->external_dnr != NULL)
  1461.     free (debug->external_dnr);
  1462.   if (debug->external_pdr != NULL)
  1463.     free (debug->external_pdr);
  1464.   if (debug->external_sym != NULL)
  1465.     free (debug->external_sym);
  1466.   if (debug->external_opt != NULL)
  1467.     free (debug->external_opt);
  1468.   if (debug->external_aux != NULL)
  1469.     free (debug->external_aux);
  1470.   if (debug->ss != NULL)
  1471.     free (debug->ss);
  1472.   if (debug->ssext != NULL)
  1473.     free (debug->ssext);
  1474.   if (debug->external_fdr != NULL)
  1475.     free (debug->external_fdr);
  1476.   if (debug->external_rfd != NULL)
  1477.     free (debug->external_rfd);
  1478.   if (debug->external_ext != NULL)
  1479.     free (debug->external_ext);
  1480.   return false;
  1481. }
  1482.  
  1483. /* The MIPS ELF linker needs additional information for each symbol in
  1484.    the global hash table.  */
  1485.  
  1486. struct mips_elf_link_hash_entry
  1487. {
  1488.   struct elf_link_hash_entry root;
  1489.  
  1490.   /* External symbol information.  */
  1491.   EXTR esym;
  1492. };
  1493.  
  1494. /* MIPS ELF linker hash table.  */
  1495.  
  1496. struct mips_elf_link_hash_table
  1497. {
  1498.   struct elf_link_hash_table root;
  1499. };
  1500.  
  1501. /* Look up an entry in a MIPS ELF linker hash table.  */
  1502.  
  1503. #define mips_elf_link_hash_lookup(table, string, create, copy, follow)    \
  1504.   ((struct mips_elf_link_hash_entry *)                    \
  1505.    elf_link_hash_lookup (&(table)->root, (string), (create),        \
  1506.              (copy), (follow)))
  1507.  
  1508. /* Traverse a MIPS ELF linker hash table.  */
  1509.  
  1510. #define mips_elf_link_hash_traverse(table, func, info)            \
  1511.   (elf_link_hash_traverse                        \
  1512.    (&(table)->root,                            \
  1513.     (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func),    \
  1514.     (info)))
  1515.  
  1516. /* Get the MIPS ELF linker hash table from a link_info structure.  */
  1517.  
  1518. #define mips_elf_hash_table(p) \
  1519.   ((struct mips_elf_link_hash_table *) ((p)->hash))
  1520.  
  1521. static boolean mips_elf_output_extsym
  1522.   PARAMS ((struct mips_elf_link_hash_entry *, PTR));
  1523.  
  1524. /* Create an entry in a MIPS ELF linker hash table.  */
  1525.  
  1526. static struct bfd_hash_entry *
  1527. mips_elf_link_hash_newfunc (entry, table, string)
  1528.      struct bfd_hash_entry *entry;
  1529.      struct bfd_hash_table *table;
  1530.      const char *string;
  1531. {
  1532.   struct mips_elf_link_hash_entry *ret =
  1533.     (struct mips_elf_link_hash_entry *) entry;
  1534.  
  1535.   /* Allocate the structure if it has not already been allocated by a
  1536.      subclass.  */
  1537.   if (ret == (struct mips_elf_link_hash_entry *) NULL)
  1538.     ret = ((struct mips_elf_link_hash_entry *)
  1539.        bfd_hash_allocate (table,
  1540.                   sizeof (struct mips_elf_link_hash_entry)));
  1541.   if (ret == (struct mips_elf_link_hash_entry *) NULL)
  1542.     {
  1543.       bfd_set_error (bfd_error_no_memory);
  1544.       return (struct bfd_hash_entry *) ret;
  1545.     }
  1546.  
  1547.   /* Call the allocation method of the superclass.  */
  1548.   ret = ((struct mips_elf_link_hash_entry *)
  1549.      _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
  1550.                      table, string));
  1551.   if (ret != (struct mips_elf_link_hash_entry *) NULL)
  1552.     {
  1553.       /* Set local fields.  */
  1554.       memset (&ret->esym, 0, sizeof (EXTR));
  1555.       /* We use -2 as a marker to indicate that the information has
  1556.      not been set.  -1 means there is no associated ifd.  */
  1557.       ret->esym.ifd = -2;
  1558.     }
  1559.  
  1560.   return (struct bfd_hash_entry *) ret;
  1561. }
  1562.  
  1563. /* Create a MIPS ELF linker hash table.  */
  1564.  
  1565. static struct bfd_link_hash_table *
  1566. mips_elf_link_hash_table_create (abfd)
  1567.      bfd *abfd;
  1568. {
  1569.   struct mips_elf_link_hash_table *ret;
  1570.  
  1571.   ret = ((struct mips_elf_link_hash_table *)
  1572.      bfd_alloc (abfd, sizeof (struct mips_elf_link_hash_table)));
  1573.   if (ret == (struct mips_elf_link_hash_table *) NULL)
  1574.     {
  1575.       bfd_set_error (bfd_error_no_memory);
  1576.       return NULL;
  1577.     }
  1578.  
  1579.   if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
  1580.                        mips_elf_link_hash_newfunc))
  1581.     {
  1582.       bfd_release (abfd, ret);
  1583.       return NULL;
  1584.     }
  1585.  
  1586.   return &ret->root.root;
  1587. }
  1588.  
  1589. /* Hook called by the linker routine which adds symbols from an object
  1590.    file.  We must handle the special MIPS section numbers here.  */
  1591.  
  1592. /*ARGSUSED*/
  1593. static boolean
  1594. mips_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
  1595.      bfd *abfd;
  1596.      struct bfd_link_info *info;
  1597.      const Elf_Internal_Sym *sym;
  1598.      const char **namep;
  1599.      flagword *flagsp;
  1600.      asection **secp;
  1601.      bfd_vma *valp;
  1602. {
  1603.   switch (sym->st_shndx)
  1604.     {
  1605.     case SHN_COMMON:
  1606.       /* Common symbols less than the GP size are automatically
  1607.      treated as SHN_MIPS_SCOMMON symbols.  */
  1608.       if (sym->st_size > elf_gp_size (abfd))
  1609.     break;
  1610.       /* Fall through.  */
  1611.     case SHN_MIPS_SCOMMON:
  1612.       *secp = bfd_make_section_old_way (abfd, ".scommon");
  1613.       (*secp)->flags |= SEC_IS_COMMON;
  1614.       *valp = sym->st_size;
  1615.       break;
  1616.  
  1617.     case SHN_MIPS_SUNDEFINED:
  1618.       *secp = bfd_und_section_ptr;
  1619.       break;
  1620.     }
  1621.  
  1622.   return true;
  1623. }
  1624.  
  1625. /* Structure used to pass information to mips_elf_output_extsym.  */
  1626.  
  1627. struct extsym_info
  1628. {
  1629.   bfd *abfd;
  1630.   struct bfd_link_info *info;
  1631.   struct ecoff_debug_info *debug;
  1632.   const struct ecoff_debug_swap *swap;
  1633.   boolean failed;
  1634. };
  1635.  
  1636. /* This routine is used to write out ECOFF debugging external symbol
  1637.    information.  It is called via mips_elf_link_hash_traverse.  The
  1638.    ECOFF external symbol information must match the ELF external
  1639.    symbol information.  Unfortunately, at this point we don't know
  1640.    whether a symbol is required by reloc information, so the two
  1641.    tables may wind up being different.  We must sort out the external
  1642.    symbol information before we can set the final size of the .mdebug
  1643.    section, and we must set the size of the .mdebug section before we
  1644.    can relocate any sections, and we can't know which symbols are
  1645.    required by relocation until we relocate the sections.
  1646.    Fortunately, it is relatively unlikely that any symbol will be
  1647.    stripped but required by a reloc.  In particular, it can not happen
  1648.    when generating a final executable.  */
  1649.  
  1650. static boolean
  1651. mips_elf_output_extsym (h, data)
  1652.      struct mips_elf_link_hash_entry *h;
  1653.      PTR data;
  1654. {
  1655.   struct extsym_info *einfo = (struct extsym_info *) data;
  1656.   boolean strip;
  1657.  
  1658.   if (h->root.indx == -2)
  1659.     strip = false;
  1660.   else if (((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
  1661.         || (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
  1662.        && (h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
  1663.        && (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
  1664.     strip = true;
  1665.   else if (einfo->info->strip == strip_all
  1666.        || (einfo->info->strip == strip_some
  1667.            && bfd_hash_lookup (einfo->info->keep_hash,
  1668.                    h->root.root.root.string,
  1669.                    false, false) == NULL))
  1670.     strip = true;
  1671.   else
  1672.     strip = false;
  1673.  
  1674.   if (strip)
  1675.     return true;
  1676.  
  1677.   if (h->esym.ifd == -2)
  1678.     {
  1679.       h->esym.jmptbl = 0;
  1680.       h->esym.cobol_main = 0;
  1681.       h->esym.weakext = 0;
  1682.       h->esym.reserved = 0;
  1683.       h->esym.ifd = ifdNil;
  1684.       h->esym.asym.value = 0;
  1685.       h->esym.asym.st = stGlobal;
  1686.  
  1687.       if (h->root.root.type != bfd_link_hash_defined)
  1688.     h->esym.asym.sc = scAbs;
  1689.       else
  1690.     {
  1691.       asection *output_section;
  1692.       const char *name;
  1693.  
  1694.       output_section = h->root.root.u.def.section->output_section;
  1695.       name = bfd_section_name (output_section->owner, output_section);
  1696.     
  1697.       if (strcmp (name, ".text") == 0)
  1698.         h->esym.asym.sc = scText;
  1699.       else if (strcmp (name, ".data") == 0)
  1700.         h->esym.asym.sc = scData;
  1701.       else if (strcmp (name, ".sdata") == 0)
  1702.         h->esym.asym.sc = scSData;
  1703.       else if (strcmp (name, ".rodata") == 0
  1704.            || strcmp (name, ".rdata") == 0)
  1705.         h->esym.asym.sc = scRData;
  1706.       else if (strcmp (name, ".bss") == 0)
  1707.         h->esym.asym.sc = scBss;
  1708.       else if (strcmp (name, ".sbss") == 0)
  1709.         h->esym.asym.sc = scSBss;
  1710.       else if (strcmp (name, ".init") == 0)
  1711.         h->esym.asym.sc = scInit;
  1712.       else if (strcmp (name, ".fini") == 0)
  1713.         h->esym.asym.sc = scFini;
  1714.       else
  1715.         h->esym.asym.sc = scAbs;
  1716.     }
  1717.  
  1718.       h->esym.asym.reserved = 0;
  1719.       h->esym.asym.index = indexNil;
  1720.     }
  1721.  
  1722.   if (h->root.root.type == bfd_link_hash_common)
  1723.     h->esym.asym.value = h->root.root.u.c.size;
  1724.   else if (h->root.root.type == bfd_link_hash_defined)
  1725.     {
  1726.       asection *sec;
  1727.  
  1728.       if (h->esym.asym.sc == scCommon)
  1729.     h->esym.asym.sc = scBss;
  1730.       else if (h->esym.asym.sc == scSCommon)
  1731.     h->esym.asym.sc = scSBss;
  1732.  
  1733.       sec = h->root.root.u.def.section;
  1734.       h->esym.asym.value = (h->root.root.u.def.value
  1735.                 + sec->output_offset
  1736.                 + sec->output_section->vma);
  1737.     }
  1738.  
  1739.   if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
  1740.                       h->root.root.root.string,
  1741.                       &h->esym))
  1742.     {
  1743.       einfo->failed = true;
  1744.       return false;
  1745.     }
  1746.  
  1747.   return true;
  1748. }
  1749.  
  1750. /* A comparison routine used to sort .gptab entries.  */
  1751.  
  1752. static int
  1753. gptab_compare (p1, p2)
  1754.      const PTR p1;
  1755.      const PTR p2;
  1756. {
  1757.   const Elf32_gptab *a1 = (const Elf32_gptab *) p1;
  1758.   const Elf32_gptab *a2 = (const Elf32_gptab *) p2;
  1759.  
  1760.   return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
  1761. }
  1762.  
  1763. /* We need to use a special link routine to handle the .reginfo and
  1764.    the .mdebug sections.  We need to merge all instances of these
  1765.    sections together, not write them all out sequentially.  */
  1766.  
  1767. static boolean
  1768. mips_elf_final_link (abfd, info)
  1769.      bfd *abfd;
  1770.      struct bfd_link_info *info;
  1771. {
  1772.   asection **secpp;
  1773.   asection *o;
  1774.   struct bfd_link_order *p;
  1775.   asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
  1776.   Elf32_RegInfo reginfo;
  1777.   struct ecoff_debug_info debug;
  1778.   const struct ecoff_debug_swap *swap
  1779.     = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
  1780.   HDRR *symhdr = &debug.symbolic_header;
  1781.   PTR mdebug_handle = NULL;
  1782.  
  1783.   /* Drop the .options section, since it has special semantics which I
  1784.      haven't bothered to figure out.  */
  1785.   for (secpp = &abfd->sections; *secpp != NULL; secpp = &(*secpp)->next)
  1786.     {
  1787.       if (strcmp ((*secpp)->name, ".options") == 0)
  1788.     {
  1789.       for (p = (*secpp)->link_order_head; p != NULL; p = p->next)
  1790.         if (p->type == bfd_indirect_link_order)
  1791.           p->u.indirect.section->flags &=~ SEC_HAS_CONTENTS;
  1792.       (*secpp)->link_order_head = NULL;
  1793.       *secpp = (*secpp)->next;
  1794.       --abfd->section_count;
  1795.       break;
  1796.     }
  1797.     }
  1798.  
  1799.   /* Go through the sections and collect the .reginfo and .mdebug
  1800.      information.  */
  1801.   reginfo_sec = NULL;
  1802.   mdebug_sec = NULL;
  1803.   gptab_data_sec = NULL;
  1804.   gptab_bss_sec = NULL;
  1805.   for (o = abfd->sections; o != (asection *) NULL; o = o->next)
  1806.     {
  1807.       if (strcmp (o->name, ".reginfo") == 0)
  1808.     {
  1809.       memset (®info, 0, sizeof reginfo);
  1810.  
  1811.       /* We have found the .reginfo section in the output file.
  1812.          Look through all the link_orders comprising it and merge
  1813.          the information together.  */
  1814.       for (p = o->link_order_head;
  1815.            p != (struct bfd_link_order *) NULL;
  1816.            p = p->next)
  1817.         {
  1818.           asection *input_section;
  1819.           bfd *input_bfd;
  1820.           Elf32_External_RegInfo ext;
  1821.           Elf32_RegInfo sub;
  1822.  
  1823.           if (p->type != bfd_indirect_link_order)
  1824.         {
  1825.           if (p->type == bfd_fill_link_order)
  1826.             continue;
  1827.           abort ();
  1828.         }
  1829.  
  1830.           input_section = p->u.indirect.section;
  1831.           input_bfd = input_section->owner;
  1832.  
  1833.           /* The linker emulation code has probably clobbered the
  1834.                  size to be zero bytes.  */
  1835.           if (input_section->_raw_size == 0)
  1836.         input_section->_raw_size = sizeof (Elf32_External_RegInfo);
  1837.  
  1838.           if (! bfd_get_section_contents (input_bfd, input_section,
  1839.                           (PTR) &ext,
  1840.                           (file_ptr) 0,
  1841.                           sizeof ext))
  1842.         return false;
  1843.  
  1844.           bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
  1845.  
  1846.           reginfo.ri_gprmask |= sub.ri_gprmask;
  1847.           reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
  1848.           reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
  1849.           reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
  1850.           reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
  1851.  
  1852.           /* ri_gp_value is set by the function
  1853.          mips_elf_section_processing when the section is
  1854.          finally written out.  */
  1855.  
  1856.           /* Hack: reset the SEC_HAS_CONTENTS flag so that
  1857.          elf_link_input_bfd ignores this section.  */
  1858.           input_section->flags &=~ SEC_HAS_CONTENTS;
  1859.         }
  1860.  
  1861.       /* Force the section size to the value we want.  */
  1862.       o->_raw_size = sizeof (Elf32_External_RegInfo);
  1863.  
  1864.       /* Skip this section later on (I don't think this currently
  1865.          matters, but someday it might).  */
  1866.       o->link_order_head = (struct bfd_link_order *) NULL;
  1867.  
  1868.       reginfo_sec = o;
  1869.     }
  1870.  
  1871.       if (strcmp (o->name, ".mdebug") == 0)
  1872.     {
  1873.       struct extsym_info einfo;
  1874.  
  1875.       /* We have found the .mdebug section in the output file.
  1876.          Look through all the link_orders comprising it and merge
  1877.          the information together.  */
  1878.       symhdr->magic = swap->sym_magic;
  1879.       /* FIXME: What should the version stamp be?  */
  1880.       symhdr->vstamp = 0;
  1881.       symhdr->ilineMax = 0;
  1882.       symhdr->cbLine = 0;
  1883.       symhdr->idnMax = 0;
  1884.       symhdr->ipdMax = 0;
  1885.       symhdr->isymMax = 0;
  1886.       symhdr->ioptMax = 0;
  1887.       symhdr->iauxMax = 0;
  1888.       symhdr->issMax = 0;
  1889.       symhdr->issExtMax = 0;
  1890.       symhdr->ifdMax = 0;
  1891.       symhdr->crfd = 0;
  1892.       symhdr->iextMax = 0;
  1893.  
  1894.       /* We accumulate the debugging information itself in the
  1895.          debug_info structure.  */
  1896.       debug.line = NULL;
  1897.       debug.external_dnr = NULL;
  1898.       debug.external_pdr = NULL;
  1899.       debug.external_sym = NULL;
  1900.       debug.external_opt = NULL;
  1901.       debug.external_aux = NULL;
  1902.       debug.ss = NULL;
  1903.       debug.ssext = debug.ssext_end = NULL;
  1904.       debug.external_fdr = NULL;
  1905.       debug.external_rfd = NULL;
  1906.       debug.external_ext = debug.external_ext_end = NULL;
  1907.  
  1908.       mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
  1909.       if (mdebug_handle == (PTR) NULL)
  1910.         return false;
  1911.  
  1912.       for (p = o->link_order_head;
  1913.            p != (struct bfd_link_order *) NULL;
  1914.            p = p->next)
  1915.         {
  1916.           asection *input_section;
  1917.           bfd *input_bfd;
  1918.           const struct ecoff_debug_swap *input_swap;
  1919.           struct ecoff_debug_info input_debug;
  1920.           char *eraw_src;
  1921.           char *eraw_end;
  1922.  
  1923.           if (p->type != bfd_indirect_link_order)
  1924.         {
  1925.           if (p->type == bfd_fill_link_order)
  1926.             continue;
  1927.           abort ();
  1928.         }
  1929.  
  1930.           input_section = p->u.indirect.section;
  1931.           input_bfd = input_section->owner;
  1932.  
  1933.           if (bfd_get_flavour (input_bfd) != bfd_target_elf_flavour
  1934.           || (get_elf_backend_data (input_bfd)
  1935.               ->elf_backend_ecoff_debug_swap) == NULL)
  1936.         {
  1937.           /* I don't know what a non MIPS ELF bfd would be
  1938.              doing with a .mdebug section, but I don't really
  1939.              want to deal with it.  */
  1940.           continue;
  1941.         }
  1942.  
  1943.           input_swap = (get_elf_backend_data (input_bfd)
  1944.                 ->elf_backend_ecoff_debug_swap);
  1945.  
  1946.           BFD_ASSERT (p->size == input_section->_raw_size);
  1947.  
  1948.           /* The ECOFF linking code expects that we have already
  1949.          read in the debugging information and set up an
  1950.          ecoff_debug_info structure, so we do that now.  */
  1951.           if (! mips_elf_read_ecoff_info (input_bfd, input_section,
  1952.                           &input_debug))
  1953.         return false;
  1954.  
  1955.           if (! (bfd_ecoff_debug_accumulate
  1956.              (mdebug_handle, abfd, &debug, swap, input_bfd,
  1957.               &input_debug, input_swap, info)))
  1958.         return false;
  1959.  
  1960.           /* Loop through the external symbols.  For each one with
  1961.          interesting information, try to find the symbol in
  1962.          the linker global hash table and save the information
  1963.          for the output external symbols.  */
  1964.           eraw_src = input_debug.external_ext;
  1965.           eraw_end = (eraw_src
  1966.               + (input_debug.symbolic_header.iextMax
  1967.                  * input_swap->external_ext_size));
  1968.           for (;
  1969.            eraw_src < eraw_end;
  1970.            eraw_src += input_swap->external_ext_size)
  1971.         {
  1972.           EXTR ext;
  1973.           const char *name;
  1974.           struct mips_elf_link_hash_entry *h;
  1975.  
  1976.           (*input_swap->swap_ext_in) (input_bfd, (PTR) eraw_src, &ext);
  1977.           if (ext.asym.sc == scNil
  1978.               || ext.asym.sc == scUndefined
  1979.               || ext.asym.sc == scSUndefined)
  1980.             continue;
  1981.  
  1982.           name = input_debug.ssext + ext.asym.iss;
  1983.           h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
  1984.                          name, false, false, true);
  1985.           if (h == NULL || h->esym.ifd != -2)
  1986.             continue;
  1987.  
  1988.           if (ext.ifd != -1)
  1989.             {
  1990.               BFD_ASSERT (ext.ifd
  1991.                   < input_debug.symbolic_header.ifdMax);
  1992.               ext.ifd = input_debug.ifdmap[ext.ifd];
  1993.             }
  1994.  
  1995.           h->esym = ext;
  1996.         }
  1997.  
  1998.           /* Free up the information we just read.  */
  1999.           free (input_debug.line);
  2000.           free (input_debug.external_dnr);
  2001.           free (input_debug.external_pdr);
  2002.           free (input_debug.external_sym);
  2003.           free (input_debug.external_opt);
  2004.           free (input_debug.external_aux);
  2005.           free (input_debug.ss);
  2006.           free (input_debug.ssext);
  2007.           free (input_debug.external_fdr);
  2008.           free (input_debug.external_rfd);
  2009.           free (input_debug.external_ext);
  2010.  
  2011.           /* Hack: reset the SEC_HAS_CONTENTS flag so that
  2012.          elf_link_input_bfd ignores this section.  */
  2013.           input_section->flags &=~ SEC_HAS_CONTENTS;
  2014.         }
  2015.  
  2016.       /* Build the external symbol information.  */
  2017.       einfo.abfd = abfd;
  2018.       einfo.info = info;
  2019.       einfo.debug = &debug;
  2020.       einfo.swap = swap;
  2021.       einfo.failed = false;
  2022.       mips_elf_link_hash_traverse (mips_elf_hash_table (info),
  2023.                        mips_elf_output_extsym,
  2024.                        (PTR) &einfo);
  2025.       if (einfo.failed)
  2026.         return false;
  2027.  
  2028.       /* Set the size of the .mdebug section.  */
  2029.       o->_raw_size = bfd_ecoff_debug_size (abfd, &debug, swap);
  2030.  
  2031.       /* Skip this section later on (I don't think this currently
  2032.          matters, but someday it might).  */
  2033.       o->link_order_head = (struct bfd_link_order *) NULL;
  2034.  
  2035.       mdebug_sec = o;
  2036.     }
  2037.  
  2038.       if (strncmp (o->name, ".gptab.", sizeof ".gptab." - 1) == 0)
  2039.     {
  2040.       const char *subname;
  2041.       unsigned int c;
  2042.       Elf32_gptab *tab;
  2043.       Elf32_External_gptab *ext_tab;
  2044.       unsigned int i;
  2045.  
  2046.       /* The .gptab.sdata and .gptab.sbss sections hold
  2047.          information describing how the small data area would
  2048.          change depending upon the -G switch.  These sections
  2049.          not used in executables files.  */
  2050.       if (! info->relocateable)
  2051.         {
  2052.           asection **secpp;
  2053.  
  2054.           for (p = o->link_order_head;
  2055.            p != (struct bfd_link_order *) NULL;
  2056.            p = p->next)
  2057.         {
  2058.           asection *input_section;
  2059.  
  2060.           if (p->type != bfd_indirect_link_order)
  2061.             {
  2062.               if (p->type == bfd_fill_link_order)
  2063.             continue;
  2064.               abort ();
  2065.             }
  2066.  
  2067.           input_section = p->u.indirect.section;
  2068.  
  2069.           /* Hack: reset the SEC_HAS_CONTENTS flag so that
  2070.              elf_link_input_bfd ignores this section.  */
  2071.           input_section->flags &=~ SEC_HAS_CONTENTS;
  2072.         }
  2073.  
  2074.           /* Skip this section later on (I don't think this
  2075.          currently matters, but someday it might).  */
  2076.           o->link_order_head = (struct bfd_link_order *) NULL;
  2077.  
  2078.           /* Really remove the section.  */
  2079.           for (secpp = &abfd->sections;
  2080.            *secpp != o;
  2081.            secpp = &(*secpp)->next)
  2082.         ;
  2083.           *secpp = (*secpp)->next;
  2084.           --abfd->section_count;
  2085.  
  2086.           continue;
  2087.         }
  2088.  
  2089.       /* There is one gptab for initialized data, and one for
  2090.          uninitialized data.  */
  2091.       if (strcmp (o->name, ".gptab.sdata") == 0)
  2092.         gptab_data_sec = o;
  2093.       else if (strcmp (o->name, ".gptab.sbss") == 0)
  2094.         gptab_bss_sec = o;
  2095.       else
  2096.         {
  2097.           bfd_set_error (bfd_error_nonrepresentable_section);
  2098.           return false;
  2099.         }
  2100.  
  2101.       /* The linker script always combines .gptab.data and
  2102.          .gptab.sdata into .gptab.sdata, and likewise for
  2103.          .gptab.bss and .gptab.sbss.  It is possible that there is
  2104.          no .sdata or .sbss section in the output file, in which
  2105.          case we must change the name of the output section.  */
  2106.       subname = o->name + sizeof ".gptab" - 1;
  2107.       if (bfd_get_section_by_name (abfd, subname) == NULL)
  2108.         {
  2109.           if (o == gptab_data_sec)
  2110.         o->name = ".gptab.data";
  2111.           else
  2112.         o->name = ".gptab.bss";
  2113.           subname = o->name + sizeof ".gptab" - 1;
  2114.           BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
  2115.         }
  2116.  
  2117.       /* Set up the first entry.  */
  2118.       c = 1;
  2119.       tab = (Elf32_gptab *) malloc (c * sizeof (Elf32_gptab));
  2120.       if (tab == NULL)
  2121.         {
  2122.           bfd_set_error (bfd_error_no_memory);
  2123.           return false;
  2124.         }
  2125.       tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
  2126.       tab[0].gt_header.gt_unused = 0;
  2127.  
  2128.       /* Combine the input sections.  */
  2129.       for (p = o->link_order_head;
  2130.            p != (struct bfd_link_order *) NULL;
  2131.            p = p->next)
  2132.         {
  2133.           asection *input_section;
  2134.           bfd *input_bfd;
  2135.           bfd_size_type size;
  2136.           unsigned long last;
  2137.           bfd_size_type gpentry;
  2138.  
  2139.           if (p->type != bfd_indirect_link_order)
  2140.         {
  2141.           if (p->type == bfd_fill_link_order)
  2142.             continue;
  2143.           abort ();
  2144.         }
  2145.  
  2146.           input_section = p->u.indirect.section;
  2147.           input_bfd = input_section->owner;
  2148.  
  2149.           /* Combine the gptab entries for this input section one
  2150.          by one.  We know that the input gptab entries are
  2151.          sorted by ascending -G value.  */
  2152.           size = bfd_section_size (input_bfd, input_section);
  2153.           last = 0;
  2154.           for (gpentry = sizeof (Elf32_External_gptab);
  2155.            gpentry < size;
  2156.            gpentry += sizeof (Elf32_External_gptab))
  2157.         {
  2158.           Elf32_External_gptab ext_gptab;
  2159.           Elf32_gptab int_gptab;
  2160.           unsigned long val;
  2161.           unsigned long add;
  2162.           boolean exact;
  2163.           unsigned int look;
  2164.  
  2165.           if (! (bfd_get_section_contents
  2166.              (input_bfd, input_section, (PTR) &ext_gptab,
  2167.               gpentry, sizeof (Elf32_External_gptab))))
  2168.             {
  2169.               free (tab);
  2170.               return false;
  2171.             }
  2172.  
  2173.           bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
  2174.                         &int_gptab);
  2175.           val = int_gptab.gt_entry.gt_g_value;
  2176.           add = int_gptab.gt_entry.gt_bytes - last;
  2177.  
  2178.           exact = false;
  2179.           for (look = 1; look < c; look++)
  2180.             {
  2181.               if (tab[look].gt_entry.gt_g_value >= val)
  2182.             tab[look].gt_entry.gt_bytes += add;
  2183.  
  2184.               if (tab[look].gt_entry.gt_g_value == val)
  2185.             exact = true;
  2186.             }
  2187.  
  2188.           if (! exact)
  2189.             {
  2190.               Elf32_gptab *new_tab;
  2191.               unsigned int max;
  2192.  
  2193.               /* We need a new table entry.  */
  2194.               new_tab = ((Elf32_gptab *)
  2195.                   realloc ((PTR) tab,
  2196.                        (c + 1) * sizeof (Elf32_gptab)));
  2197.               if (new_tab == NULL)
  2198.             {
  2199.               bfd_set_error (bfd_error_no_memory);
  2200.               free (tab);
  2201.               return false;
  2202.             }
  2203.               tab = new_tab;
  2204.               tab[c].gt_entry.gt_g_value = val;
  2205.               tab[c].gt_entry.gt_bytes = add;
  2206.  
  2207.               /* Merge in the size for the next smallest -G
  2208.              value, since that will be implied by this new
  2209.              value.  */
  2210.               max = 0;
  2211.               for (look = 1; look < c; look++)
  2212.             {
  2213.               if (tab[look].gt_entry.gt_g_value < val
  2214.                   && (max == 0
  2215.                   || (tab[look].gt_entry.gt_g_value
  2216.                       > tab[max].gt_entry.gt_g_value)))
  2217.                 max = look;
  2218.             }
  2219.               if (max != 0)
  2220.             tab[c].gt_entry.gt_bytes +=
  2221.               tab[max].gt_entry.gt_bytes;
  2222.  
  2223.               ++c;
  2224.             }
  2225.  
  2226.           last = int_gptab.gt_entry.gt_bytes;
  2227.         }
  2228.  
  2229.           /* Hack: reset the SEC_HAS_CONTENTS flag so that
  2230.          elf_link_input_bfd ignores this section.  */
  2231.           input_section->flags &=~ SEC_HAS_CONTENTS;
  2232.         }
  2233.  
  2234.       /* The table must be sorted by -G value.  */
  2235.       if (c > 2)
  2236.         qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
  2237.  
  2238.       /* Swap out the table.  */
  2239.       ext_tab = ((Elf32_External_gptab *)
  2240.              bfd_alloc (abfd, c * sizeof (Elf32_External_gptab)));
  2241.       if (ext_tab == NULL)
  2242.         {
  2243.           bfd_set_error (bfd_error_no_memory);
  2244.           free (tab);
  2245.           return false;
  2246.         }
  2247.  
  2248.       for (i = 0; i < c; i++)
  2249.         bfd_mips_elf32_swap_gptab_out (abfd, tab + i, ext_tab + i);
  2250.       free (tab);
  2251.  
  2252.       o->_raw_size = c * sizeof (Elf32_External_gptab);
  2253.       o->contents = (bfd_byte *) ext_tab;
  2254.  
  2255.       /* Skip this section later on (I don't think this currently
  2256.          matters, but someday it might).  */
  2257.       o->link_order_head = (struct bfd_link_order *) NULL;
  2258.     }
  2259.     }
  2260.  
  2261.   /* Get a value for the GP register.  */
  2262.   if (elf_gp (abfd) == 0)
  2263.     {
  2264.       struct bfd_link_hash_entry *h;
  2265.  
  2266.       h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true);
  2267.       if (h != (struct bfd_link_hash_entry *) NULL
  2268.       && h->type == bfd_link_hash_defined)
  2269.     elf_gp (abfd) = (h->u.def.value
  2270.              + h->u.def.section->output_section->vma
  2271.              + h->u.def.section->output_offset);
  2272.       else if (info->relocateable)
  2273.     {
  2274.       bfd_vma lo;
  2275.  
  2276.       /* Make up a value.  */
  2277.       lo = (bfd_vma) -1;
  2278.       for (o = abfd->sections; o != (asection *) NULL; o = o->next)
  2279.         {
  2280.           if (o->vma < lo
  2281.           && (strcmp (o->name, ".sbss") == 0
  2282.               || strcmp (o->name, ".sdata") == 0
  2283.               || strcmp (o->name, ".lit4") == 0
  2284.               || strcmp (o->name, ".lit8") == 0))
  2285.         lo = o->vma;
  2286.         }
  2287.       elf_gp (abfd) = lo + 0x8000;
  2288.     }
  2289.       else
  2290.     {
  2291.       /* If the relocate_section function needs to do a reloc
  2292.          involving the GP value, it should make a reloc_dangerous
  2293.          callback to warn that GP is not defined.  */
  2294.     }
  2295.     }
  2296.  
  2297.   /* Invoke the regular ELF backend linker to do all the work.  */
  2298.   if (! bfd_elf32_bfd_final_link (abfd, info))
  2299.     return false;
  2300.  
  2301.   /* Now write out the computed sections.  */
  2302.  
  2303.   if (reginfo_sec != (asection *) NULL)
  2304.     {
  2305.       Elf32_External_RegInfo ext;
  2306.  
  2307.       bfd_mips_elf32_swap_reginfo_out (abfd, ®info, &ext);
  2308.       if (! bfd_set_section_contents (abfd, reginfo_sec, (PTR) &ext,
  2309.                       (file_ptr) 0, sizeof ext))
  2310.     return false;
  2311.     }
  2312.  
  2313.   if (mdebug_sec != (asection *) NULL)
  2314.     {
  2315.       BFD_ASSERT (abfd->output_has_begun);
  2316.       if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
  2317.                            swap, info,
  2318.                            mdebug_sec->filepos))
  2319.     return false;
  2320.  
  2321.       bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
  2322.     }
  2323.  
  2324.   if (gptab_data_sec != (asection *) NULL)
  2325.     {
  2326.       if (! bfd_set_section_contents (abfd, gptab_data_sec,
  2327.                       gptab_data_sec->contents,
  2328.                       (file_ptr) 0,
  2329.                       gptab_data_sec->_raw_size))
  2330.     return false;
  2331.     }
  2332.  
  2333.   if (gptab_bss_sec != (asection *) NULL)
  2334.     {
  2335.       if (! bfd_set_section_contents (abfd, gptab_bss_sec,
  2336.                       gptab_bss_sec->contents,
  2337.                       (file_ptr) 0,
  2338.                       gptab_bss_sec->_raw_size))
  2339.     return false;
  2340.     }
  2341.  
  2342.   return true;
  2343. }
  2344.  
  2345. /* Handle a MIPS ELF HI16 reloc.  */
  2346.  
  2347. static void
  2348. mips_elf_relocate_hi16 (input_bfd, relhi, rello, contents, addend)
  2349.      bfd *input_bfd;
  2350.      Elf_Internal_Rela *relhi;
  2351.      Elf_Internal_Rela *rello;
  2352.      bfd_byte *contents;
  2353.      bfd_vma addend;
  2354. {
  2355.   bfd_vma insn;
  2356.   bfd_vma addlo;
  2357.  
  2358.   insn = bfd_get_32 (input_bfd, contents + relhi->r_offset);
  2359.  
  2360.   addlo = bfd_get_32 (input_bfd, contents + rello->r_offset);
  2361.   addlo &= 0xffff;
  2362.  
  2363.   addend += ((insn & 0xffff) << 16) + addlo;
  2364.  
  2365.   if ((addlo & 0x8000) != 0)
  2366.     addend -= 0x10000;
  2367.   if ((addend & 0x8000) != 0)
  2368.     addend += 0x10000;
  2369.  
  2370.   bfd_put_32 (input_bfd,
  2371.           (insn & 0xffff0000) | ((addend >> 16) & 0xffff),
  2372.           contents + relhi->r_offset);
  2373. }
  2374.  
  2375. /* Relocate a MIPS ELF section.  */
  2376.  
  2377. static boolean
  2378. mips_elf_relocate_section (output_bfd, info, input_bfd, input_section,
  2379.                contents, relocs, local_syms, local_sections)
  2380.      bfd *output_bfd;
  2381.      struct bfd_link_info *info;
  2382.      bfd *input_bfd;
  2383.      asection *input_section;
  2384.      bfd_byte *contents;
  2385.      Elf_Internal_Rela *relocs;
  2386.      Elf_Internal_Sym *local_syms;
  2387.      asection **local_sections;
  2388. {
  2389.   Elf_Internal_Shdr *symtab_hdr;
  2390.   size_t locsymcount;
  2391.   size_t extsymoff;
  2392.   Elf_Internal_Rela *rel;
  2393.   Elf_Internal_Rela *relend;
  2394.  
  2395.   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
  2396.  
  2397.   if (elf_bad_symtab (input_bfd))
  2398.     {
  2399.       locsymcount = symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
  2400.       extsymoff = 0;
  2401.     }
  2402.   else
  2403.     {
  2404.       locsymcount = symtab_hdr->sh_info;
  2405.       extsymoff = symtab_hdr->sh_info;
  2406.     }
  2407.  
  2408.   rel = relocs;
  2409.   relend = relocs + input_section->reloc_count;
  2410.   for (; rel < relend; rel++)
  2411.     {
  2412.       int r_type;
  2413.       reloc_howto_type *howto;
  2414.       long r_symndx;
  2415.       bfd_vma addend;
  2416.       struct elf_link_hash_entry *h;
  2417.       asection *sec;
  2418.       Elf_Internal_Sym *sym;
  2419.       bfd_reloc_status_type r;
  2420.  
  2421.       r_type = ELF32_R_TYPE (rel->r_info);
  2422.       if (r_type < 0 || r_type >= (int) R_MIPS_max)
  2423.     {
  2424.       bfd_set_error (bfd_error_bad_value);
  2425.       return false;
  2426.     }
  2427.       howto = elf_mips_howto_table + r_type;
  2428.  
  2429.       r_symndx = ELF32_R_SYM (rel->r_info);
  2430.  
  2431.       /* Mix in the change in GP address for a GP relative reloc.  */
  2432.       if (r_type != R_MIPS_GPREL16
  2433.       && r_type != R_MIPS_LITERAL
  2434.       && r_type != R_MIPS_GPREL32)
  2435.     addend = 0;
  2436.       else
  2437.     {
  2438.       if (elf_gp (output_bfd) == 0)
  2439.         {
  2440.           if (! ((*info->callbacks->reloc_dangerous)
  2441.              (info,
  2442.               "GP relative relocation when GP not defined",
  2443.               input_bfd, input_section,
  2444.               rel->r_offset)))
  2445.         return false;
  2446.           /* Only give the error once per link.  */
  2447.           elf_gp (output_bfd) = 4;
  2448.         }
  2449.  
  2450.       if (r_symndx < extsymoff
  2451.           || (elf_bad_symtab (input_bfd)
  2452.           && local_sections[r_symndx] != NULL))
  2453.         {
  2454.           /* This is a relocation against a section.  The current
  2455.          addend in the instruction is the difference between
  2456.          INPUT_SECTION->vma and the GP value of INPUT_BFD.  We
  2457.          must change this to be the difference between the
  2458.          final definition (which will end up in RELOCATION)
  2459.          and the GP value of OUTPUT_BFD (which is in GP).  */
  2460.           addend = elf_gp (input_bfd) - elf_gp (output_bfd);
  2461.         }
  2462.       else if (! info->relocateable)
  2463.         {
  2464.           /* We are doing a final link.  The current addend in the
  2465.          instruction is simply the desired offset into the
  2466.          symbol (normally zero).  We want the instruction to
  2467.          hold the difference between the final definition of
  2468.          the symbol (which will end up in RELOCATION) and the
  2469.          GP value of OUTPUT_BFD (which is in GP).  */
  2470.           addend = - elf_gp (output_bfd);
  2471.         }
  2472.       else
  2473.         {
  2474.           /* We are generating relocateable output, and we aren't
  2475.          going to define this symbol, so we just leave the
  2476.          instruction alone.  */
  2477.           addend = 0;
  2478.         }
  2479.     }
  2480.  
  2481.       h = NULL;
  2482.       sym = NULL;
  2483.       sec = NULL;
  2484.       if (info->relocateable)
  2485.     {
  2486.       /* This is a relocateable link.  We don't have to change
  2487.          anything, unless the reloc is against a section symbol,
  2488.          in which case we have to adjust according to where the
  2489.          section symbol winds up in the output section.  */
  2490.       if (r_symndx >= locsymcount
  2491.           || (elf_bad_symtab (input_bfd)
  2492.           && local_sections[r_symndx] == NULL))
  2493.         r = bfd_reloc_ok;
  2494.       else
  2495.         {
  2496.           sym = local_syms + r_symndx;
  2497.           if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
  2498.         r = bfd_reloc_ok;
  2499.           else
  2500.         {
  2501.           sec = local_sections[r_symndx];
  2502.  
  2503.           /* It would be logical to add sym->st_value here,
  2504.              but Irix 5 sometimes generates a garbage symbol
  2505.              value.  */
  2506.           addend += sec->output_offset;
  2507.  
  2508.           /* If this is HI16 with an associated LO16, adjust
  2509.              the addend accordingly.  Otherwise, just
  2510.              relocate.  */
  2511.           if (r_type != R_MIPS_HI16
  2512.               || (rel + 1) >= relend
  2513.               || ELF32_R_TYPE ((rel + 1)->r_info) != R_MIPS_LO16)
  2514.             r = _bfd_relocate_contents (howto, input_bfd,
  2515.                         addend,
  2516.                         contents + rel->r_offset);
  2517.           else
  2518.             {
  2519.               mips_elf_relocate_hi16 (input_bfd, rel, rel + 1,
  2520.                           contents, addend);
  2521.               r = bfd_reloc_ok;
  2522.             }
  2523.         }
  2524.         }
  2525.     }
  2526.       else
  2527.     {
  2528.       bfd_vma relocation;
  2529.  
  2530.       /* This is a final link.  */
  2531.       sym = NULL;
  2532.       if (r_symndx < extsymoff
  2533.           || (elf_bad_symtab (input_bfd)
  2534.           && local_sections[r_symndx] != NULL))
  2535.         {
  2536.           sym = local_syms + r_symndx;
  2537.           sec = local_sections[r_symndx];
  2538.           relocation = (sec->output_section->vma
  2539.                 + sec->output_offset);
  2540.  
  2541.           /* It would be logical to always add sym->st_value here,
  2542.          but Irix 5 sometimes generates a garbage symbol
  2543.          value.  */
  2544.           if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
  2545.         relocation += sym->st_value;
  2546.         }
  2547.       else
  2548.         {
  2549.           long indx;
  2550.  
  2551.           indx = r_symndx - extsymoff;
  2552.           h = elf_sym_hashes (input_bfd)[indx];
  2553.           if (h->root.type == bfd_link_hash_defined)
  2554.         {
  2555.           sec = h->root.u.def.section;
  2556.           relocation = (h->root.u.def.value
  2557.                 + sec->output_section->vma
  2558.                 + sec->output_offset);
  2559.         }
  2560.           else if (h->root.type == bfd_link_hash_weak)
  2561.         relocation = 0;
  2562.           else
  2563.         {
  2564.           if (! ((*info->callbacks->undefined_symbol)
  2565.              (info, h->root.root.string, input_bfd,
  2566.               input_section, rel->r_offset)))
  2567.             return false;
  2568.           relocation = 0;
  2569.         }
  2570.         }
  2571.  
  2572.       if (r_type != R_MIPS_HI16
  2573.           || (rel + 1) >= relend
  2574.           || ELF32_R_TYPE ((rel + 1)->r_info) != R_MIPS_LO16)
  2575.         r = _bfd_final_link_relocate (howto, input_bfd, input_section,
  2576.                       contents, rel->r_offset,
  2577.                       relocation, addend);
  2578.       else
  2579.         {
  2580.           mips_elf_relocate_hi16 (input_bfd, rel, rel + 1,
  2581.                       contents, relocation + addend);
  2582.           r = bfd_reloc_ok;
  2583.         }
  2584.     }
  2585.  
  2586.       if (r != bfd_reloc_ok)
  2587.     {
  2588.       switch (r)
  2589.         {
  2590.         default:
  2591.         case bfd_reloc_outofrange:
  2592.           abort ();
  2593.         case bfd_reloc_overflow:
  2594.           {
  2595.         const char *name;
  2596.  
  2597.         if (h != NULL)
  2598.           name = h->root.root.string;
  2599.         else
  2600.           {
  2601.             name = elf_string_from_elf_section (input_bfd,
  2602.                             symtab_hdr->sh_link,
  2603.                             sym->st_name);
  2604.             if (name == NULL)
  2605.               return false;
  2606.             if (*name == '\0')
  2607.               name = bfd_section_name (input_bfd, sec);
  2608.           }
  2609.         if (! ((*info->callbacks->reloc_overflow)
  2610.                (info, name, howto->name, (bfd_vma) 0,
  2611.             input_bfd, input_section, rel->r_offset)))
  2612.           return false;
  2613.           }
  2614.           break;
  2615.         }
  2616.     }
  2617.     }
  2618.  
  2619.   return true;
  2620. }
  2621.  
  2622. /* ECOFF swapping routines.  These are used when dealing with the
  2623.    .mdebug section, which is in the ECOFF debugging format.  */
  2624. static const struct ecoff_debug_swap mips_elf_ecoff_debug_swap =
  2625. {
  2626.   /* Symbol table magic number.  */
  2627.   magicSym,
  2628.   /* Alignment of debugging information.  E.g., 4.  */
  2629.   4,
  2630.   /* Sizes of external symbolic information.  */
  2631.   sizeof (struct hdr_ext),
  2632.   sizeof (struct dnr_ext),
  2633.   sizeof (struct pdr_ext),
  2634.   sizeof (struct sym_ext),
  2635.   sizeof (struct opt_ext),
  2636.   sizeof (struct fdr_ext),
  2637.   sizeof (struct rfd_ext),
  2638.   sizeof (struct ext_ext),
  2639.   /* Functions to swap in external symbolic data.  */
  2640.   ecoff_swap_hdr_in,
  2641.   ecoff_swap_dnr_in,
  2642.   ecoff_swap_pdr_in,
  2643.   ecoff_swap_sym_in,
  2644.   ecoff_swap_opt_in,
  2645.   ecoff_swap_fdr_in,
  2646.   ecoff_swap_rfd_in,
  2647.   ecoff_swap_ext_in,
  2648.   _bfd_ecoff_swap_tir_in,
  2649.   _bfd_ecoff_swap_rndx_in,
  2650.   /* Functions to swap out external symbolic data.  */
  2651.   ecoff_swap_hdr_out,
  2652.   ecoff_swap_dnr_out,
  2653.   ecoff_swap_pdr_out,
  2654.   ecoff_swap_sym_out,
  2655.   ecoff_swap_opt_out,
  2656.   ecoff_swap_fdr_out,
  2657.   ecoff_swap_rfd_out,
  2658.   ecoff_swap_ext_out,
  2659.   _bfd_ecoff_swap_tir_out,
  2660.   _bfd_ecoff_swap_rndx_out,
  2661.   /* Function to read in symbolic data.  */
  2662.   mips_elf_read_ecoff_info
  2663. };
  2664.  
  2665. #define TARGET_LITTLE_SYM        bfd_elf32_littlemips_vec
  2666. #define TARGET_LITTLE_NAME        "elf32-littlemips"
  2667. #define TARGET_BIG_SYM            bfd_elf32_bigmips_vec
  2668. #define TARGET_BIG_NAME            "elf32-bigmips"
  2669. #define ELF_ARCH            bfd_arch_mips
  2670. #define ELF_MACHINE_CODE        EM_MIPS
  2671. #define ELF_MAXPAGESIZE            0x10000
  2672. #define elf_backend_collect        true
  2673. #define elf_info_to_howto        0
  2674. #define elf_info_to_howto_rel        mips_info_to_howto_rel
  2675. #define elf_backend_sym_is_global    mips_elf_sym_is_global
  2676. #define elf_backend_object_p        mips_elf_object_p
  2677. #define elf_backend_section_from_shdr    mips_elf_section_from_shdr
  2678. #define elf_backend_fake_sections    mips_elf_fake_sections
  2679. #define elf_backend_section_from_bfd_section \
  2680.                     mips_elf_section_from_bfd_section
  2681. #define elf_backend_section_processing    mips_elf_section_processing
  2682. #define elf_backend_symbol_processing    mips_elf_symbol_processing
  2683. #define elf_backend_final_write_processing \
  2684.                     mips_elf_final_write_processing
  2685. #define elf_backend_ecoff_debug_swap    &mips_elf_ecoff_debug_swap
  2686.  
  2687. #define bfd_elf32_bfd_link_hash_table_create \
  2688.                     mips_elf_link_hash_table_create
  2689. #define bfd_elf32_bfd_final_link    mips_elf_final_link
  2690. #define elf_backend_relocate_section    mips_elf_relocate_section
  2691. #define elf_backend_add_symbol_hook    mips_elf_add_symbol_hook
  2692.  
  2693. #include "elf32-target.h"
  2694.